blob: 30ce3eab038122a0831ed388b406cd346e21c795 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
Bram Moolenaar734a8672019-12-02 22:49:38 +010061 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100122 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100129 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
Bram Moolenaar734a8672019-12-02 22:49:38 +0100182#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188#ifndef PROTO
189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
204#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100205# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100206#endif
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200243# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define _cdecl
245typedef int BOOL;
246typedef int BYTE;
247typedef int DWORD;
248typedef int WCHAR;
249typedef int ENUMLOGFONT;
250typedef int FINDREPLACE;
251typedef int HANDLE;
252typedef int HBITMAP;
253typedef int HBRUSH;
254typedef int HDROP;
255typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100256typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257typedef int LPARAM;
258typedef int LPCREATESTRUCT;
259typedef int LPCSTR;
260typedef int LPCTSTR;
261typedef int LPRECT;
262typedef int LPSTR;
263typedef int LPWINDOWPOS;
264typedef int LPWORD;
265typedef int LRESULT;
266typedef int HRESULT;
267# undef MSG
268typedef int MSG;
269typedef int NEWTEXTMETRIC;
270typedef int OSVERSIONINFO;
271typedef int PWORD;
272typedef int RECT;
273typedef int UINT;
274typedef int WORD;
275typedef int WPARAM;
276typedef int POINT;
277typedef void *HINSTANCE;
278typedef void *HMENU;
279typedef void *HWND;
280typedef void *HDC;
281typedef void VOID;
282typedef int LPNMHDR;
283typedef int LONG;
284typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200285typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200286typedef int COLORREF;
287typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288#endif
289
290#ifndef GET_X_LPARAM
291# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
292#endif
293
294static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100295static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296static void clear_rect(RECT *rcp);
297
Bram Moolenaar734a8672019-12-02 22:49:38 +0100298static WORD s_dlgfntheight; // height of the dialog font
299static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100300
301#ifdef FEAT_MENU
302static HMENU s_menuBar = NULL;
303#endif
304#ifdef FEAT_TEAROFF
305static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100306static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307#endif
308
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309// Flag that is set while processing a message that must not be interrupted by
310// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100311static int s_busy_processing = FALSE;
312
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314
315#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200316static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
317static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200319static int s_findrep_is_find; // TRUE for find dialog, FALSE
320 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321#endif
322
Bram Moolenaar85b11762016-02-27 18:13:23 +0100323#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324static
325#endif
326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
Bram Moolenaar734a8672019-12-02 22:49:38 +0100347static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100348
349static int s_need_activate = FALSE;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351// This variable is set when waiting for an event, which is the only moment
352// scrollbar dragging can be done directly. It's not allowed while commands
353// are executed, because it may move the cursor and that may cause unexpected
354// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100355static int allow_scrollbar = FALSE;
356
357#ifdef GLOBAL_IME
358# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
359#else
360# define MyTranslateMessage(x) TranslateMessage(x)
361#endif
362
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100363#if defined(FEAT_DIRECTX)
364 static int
365directx_enabled(void)
366{
367 if (s_dwc != NULL)
368 return 1;
369 else if (s_directx_load_attempted)
370 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100371 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100372 DWrite_Init();
373 s_directx_load_attempted = 1;
374 s_dwc = DWriteContext_Open();
375 directx_binddc();
376 return s_dwc != NULL ? 1 : 0;
377}
378
379 static void
380directx_binddc(void)
381{
382 if (s_textArea != NULL)
383 {
384 RECT rect;
385 GetClientRect(s_textArea, &rect);
386 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
387 }
388}
389#endif
390
Bram Moolenaar734a8672019-12-02 22:49:38 +0100391// use of WindowProc depends on Global IME
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100392#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100393
Bram Moolenaar734a8672019-12-02 22:49:38 +0100394extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100395
396static struct
397{
398 UINT key_sym;
399 char_u vim_code0;
400 char_u vim_code1;
401} special_keys[] =
402{
403 {VK_UP, 'k', 'u'},
404 {VK_DOWN, 'k', 'd'},
405 {VK_LEFT, 'k', 'l'},
406 {VK_RIGHT, 'k', 'r'},
407
408 {VK_F1, 'k', '1'},
409 {VK_F2, 'k', '2'},
410 {VK_F3, 'k', '3'},
411 {VK_F4, 'k', '4'},
412 {VK_F5, 'k', '5'},
413 {VK_F6, 'k', '6'},
414 {VK_F7, 'k', '7'},
415 {VK_F8, 'k', '8'},
416 {VK_F9, 'k', '9'},
417 {VK_F10, 'k', ';'},
418
419 {VK_F11, 'F', '1'},
420 {VK_F12, 'F', '2'},
421 {VK_F13, 'F', '3'},
422 {VK_F14, 'F', '4'},
423 {VK_F15, 'F', '5'},
424 {VK_F16, 'F', '6'},
425 {VK_F17, 'F', '7'},
426 {VK_F18, 'F', '8'},
427 {VK_F19, 'F', '9'},
428 {VK_F20, 'F', 'A'},
429
430 {VK_F21, 'F', 'B'},
431#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100432 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100433#endif
434 {VK_F22, 'F', 'C'},
435 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100436 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100437
438 {VK_HELP, '%', '1'},
439 {VK_BACK, 'k', 'b'},
440 {VK_INSERT, 'k', 'I'},
441 {VK_DELETE, 'k', 'D'},
442 {VK_HOME, 'k', 'h'},
443 {VK_END, '@', '7'},
444 {VK_PRIOR, 'k', 'P'},
445 {VK_NEXT, 'k', 'N'},
446 {VK_PRINT, '%', '9'},
447 {VK_ADD, 'K', '6'},
448 {VK_SUBTRACT, 'K', '7'},
449 {VK_DIVIDE, 'K', '8'},
450 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100451 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100452 {VK_DECIMAL, 'K', 'B'},
453
454 {VK_NUMPAD0, 'K', 'C'},
455 {VK_NUMPAD1, 'K', 'D'},
456 {VK_NUMPAD2, 'K', 'E'},
457 {VK_NUMPAD3, 'K', 'F'},
458 {VK_NUMPAD4, 'K', 'G'},
459 {VK_NUMPAD5, 'K', 'H'},
460 {VK_NUMPAD6, 'K', 'I'},
461 {VK_NUMPAD7, 'K', 'J'},
462 {VK_NUMPAD8, 'K', 'K'},
463 {VK_NUMPAD9, 'K', 'L'},
464
Bram Moolenaar734a8672019-12-02 22:49:38 +0100465 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100466 {VK_SPACE, ' ', NUL},
467 {VK_TAB, TAB, NUL},
468 {VK_ESCAPE, ESC, NUL},
469 {NL, NL, NUL},
470 {CAR, CAR, NUL},
471
Bram Moolenaar734a8672019-12-02 22:49:38 +0100472 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100473 {0, 0, 0}
474};
475
Bram Moolenaar734a8672019-12-02 22:49:38 +0100476// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100477static int s_button_pending = -1;
478
Bram Moolenaar734a8672019-12-02 22:49:38 +0100479// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
480// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100481static int s_getting_focus = FALSE;
482
483static int s_x_pending;
484static int s_y_pending;
485static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200486static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100487static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200488static int dead_key = 0; // 0: no dead key, 1: dead key pressed
489static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
490 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100491
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100492#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
495static void TrackUserActivity(UINT uMsg);
496#endif
497
498/*
499 * For control IME.
500 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100501 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100502 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100503#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100504// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100505static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100506#endif
507#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100508// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100509static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100510#endif
511
512#ifdef FEAT_MBYTE_IME
513static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
514#endif
515
516#if defined(FEAT_BROWSE)
517static char_u *convert_filter(char_u *s);
518#endif
519
520#ifdef DEBUG_PRINT_ERROR
521/*
522 * Print out the last Windows error message
523 */
524 static void
525print_windows_error(void)
526{
527 LPVOID lpMsgBuf;
528
529 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
530 NULL, GetLastError(),
531 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
532 (LPTSTR) &lpMsgBuf, 0, NULL);
533 TRACE1("Error: %s\n", lpMsgBuf);
534 LocalFree(lpMsgBuf);
535}
536#endif
537
538/*
539 * Cursor blink functions.
540 *
541 * This is a simple state machine:
542 * BLINK_NONE not blinking at all
543 * BLINK_OFF blinking, cursor is not shown
544 * BLINK_ON blinking, cursor is shown
545 */
546
547#define BLINK_NONE 0
548#define BLINK_OFF 1
549#define BLINK_ON 2
550
551static int blink_state = BLINK_NONE;
552static long_u blink_waittime = 700;
553static long_u blink_ontime = 400;
554static long_u blink_offtime = 250;
555static UINT blink_timer = 0;
556
Bram Moolenaar703a8042016-06-04 16:24:32 +0200557 int
558gui_mch_is_blinking(void)
559{
560 return blink_state != BLINK_NONE;
561}
562
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200563 int
564gui_mch_is_blink_off(void)
565{
566 return blink_state == BLINK_OFF;
567}
568
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100569 void
570gui_mch_set_blinking(long wait, long on, long off)
571{
572 blink_waittime = wait;
573 blink_ontime = on;
574 blink_offtime = off;
575}
576
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100577 static VOID CALLBACK
578_OnBlinkTimer(
579 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100582 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100583{
584 MSG msg;
585
586 /*
587 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
588 */
589
590 KillTimer(NULL, idEvent);
591
Bram Moolenaar734a8672019-12-02 22:49:38 +0100592 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100593 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
594 ;
595
596 if (blink_state == BLINK_ON)
597 {
598 gui_undraw_cursor();
599 blink_state = BLINK_OFF;
600 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
601 (TIMERPROC)_OnBlinkTimer);
602 }
603 else
604 {
605 gui_update_cursor(TRUE, FALSE);
606 blink_state = BLINK_ON;
607 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100608 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100610 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100611}
612
613 static void
614gui_mswin_rm_blink_timer(void)
615{
616 MSG msg;
617
618 if (blink_timer != 0)
619 {
620 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100621 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100622 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
623 ;
624 blink_timer = 0;
625 }
626}
627
628/*
629 * Stop the cursor blinking. Show the cursor if it wasn't shown.
630 */
631 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100632gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633{
634 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100635 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100636 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100637 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100638 gui_mch_flush();
639 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100640 blink_state = BLINK_NONE;
641}
642
643/*
644 * Start the cursor blinking. If it was already blinking, this restarts the
645 * waiting time and shows the cursor.
646 */
647 void
648gui_mch_start_blink(void)
649{
650 gui_mswin_rm_blink_timer();
651
Bram Moolenaar734a8672019-12-02 22:49:38 +0100652 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
654 {
655 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
656 (TIMERPROC)_OnBlinkTimer);
657 blink_state = BLINK_ON;
658 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100659 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100660 }
661}
662
663/*
664 * Call-back routines.
665 */
666
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100667 static VOID CALLBACK
668_OnTimer(
669 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100672 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673{
674 MSG msg;
675
676 /*
677 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
678 */
679 KillTimer(NULL, idEvent);
680 s_timed_out = TRUE;
681
Bram Moolenaar734a8672019-12-02 22:49:38 +0100682 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100683 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
684 ;
685 if (idEvent == s_wait_timer)
686 s_wait_timer = 0;
687}
688
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100689 static void
690_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100691 HWND hwnd UNUSED,
692 UINT ch UNUSED,
693 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100694{
695 dead_key = 1;
696}
697
698/*
699 * Convert Unicode character "ch" to bytes in "string[slen]".
700 * When "had_alt" is TRUE the ALT key was included in "ch".
701 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200702 * Because the Windows API uses UTF-16, we have to deal with surrogate
703 * pairs; this is where we choose to deal with them: if "ch" is a high
704 * surrogate, it will be stored, and the length returned will be zero; the next
705 * char_to_string call will then include the high surrogate, decoding the pair
706 * of UTF-16 code units to a single Unicode code point, presuming it is the
707 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100708 */
709 static int
710char_to_string(int ch, char_u *string, int slen, int had_alt)
711{
712 int len;
713 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200715 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200717 if (surrogate_pending_ch != 0)
718 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100719 // We don't guarantee ch is a low surrogate to match the high surrogate
720 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200721 wstring[0] = surrogate_pending_ch;
722 wstring[1] = ch;
723 surrogate_pending_ch = 0;
724 len = 2;
725 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200727 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100728 // We don't have the entire code point yet, only the first UTF-16 code
729 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200730 surrogate_pending_ch = ch;
731 return 0;
732 }
733 else
734 {
735 wstring[0] = ch;
736 len = 1;
737 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200738
Bram Moolenaar734a8672019-12-02 22:49:38 +0100739 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
740 // "enc_codepage" is non-zero use the standard Win32 function,
741 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200742 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100743 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200744 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
745 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100746 // If we had included the ALT key into the character but now the
747 // upper bit is no longer set, that probably means the conversion
748 // failed. Convert the original character and set the upper bit
749 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200750 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100751 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200752 wstring[0] = ch & 0x7f;
753 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
754 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100755 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200756 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757 }
758 }
759 else
760 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200761 ws = utf16_to_enc(wstring, &len);
762 if (ws == NULL)
763 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100764 else
765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100766 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767 len = slen;
768 mch_memmove(string, ws, len);
769 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100770 }
771 }
772
773 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100774 {
775 string[0] = ch;
776 len = 1;
777 }
778
779 for (i = 0; i < len; ++i)
780 if (string[i] == CSI && len <= slen - 2)
781 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100782 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100783 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
784 string[++i] = KS_EXTRA;
785 string[++i] = (int)KE_CSI;
786 len += 2;
787 }
788
789 return len;
790}
791
792/*
793 * Key hit, add it to the input buffer.
794 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100795 static void
796_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100797 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100799 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100800{
801 char_u string[40];
802 int len = 0;
803
804 dead_key = 0;
805
806 len = char_to_string(ch, string, 40, FALSE);
807 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
808 {
809 trash_input_buf();
810 got_int = TRUE;
811 }
812
813 add_to_input_buf(string, len);
814}
815
816/*
817 * Alt-Key hit, add it to the input buffer.
818 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100819 static void
820_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100821 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100823 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100825 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826 int len;
827 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100828 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100829
830 dead_key = 0;
831
Bram Moolenaar734a8672019-12-02 22:49:38 +0100832 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100833
Bram Moolenaar734a8672019-12-02 22:49:38 +0100834 // OK, we have a character key (given by ch) which was entered with the
835 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
836 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
837 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100838 modifiers = MOD_MASK_ALT;
839 if (GetKeyState(VK_SHIFT) & 0x8000)
840 modifiers |= MOD_MASK_SHIFT;
841 if (GetKeyState(VK_CONTROL) & 0x8000)
842 modifiers |= MOD_MASK_CTRL;
843
844 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100845 // remove the SHIFT modifier for keys where it's already included, e.g.,
846 // '(' and '*'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100847 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
848 modifiers &= ~MOD_MASK_SHIFT;
849
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200850 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
851 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100852 if (ch == CSI)
853 ch = K_CSI;
854
855 len = 0;
856 if (modifiers)
857 {
858 string[len++] = CSI;
859 string[len++] = KS_MODIFIER;
860 string[len++] = modifiers;
861 }
862
863 if (IS_SPECIAL((int)ch))
864 {
865 string[len++] = CSI;
866 string[len++] = K_SECOND((int)ch);
867 string[len++] = K_THIRD((int)ch);
868 }
869 else
870 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100871 // Although the documentation isn't clear about it, we assume "ch" is
872 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100873 len += char_to_string(ch, string + len, 40 - len, TRUE);
874 }
875
876 add_to_input_buf(string, len);
877}
878
879 static void
880_OnMouseEvent(
881 int button,
882 int x,
883 int y,
884 int repeated_click,
885 UINT keyFlags)
886{
887 int vim_modifiers = 0x0;
888
889 s_getting_focus = FALSE;
890
891 if (keyFlags & MK_SHIFT)
892 vim_modifiers |= MOUSE_SHIFT;
893 if (keyFlags & MK_CONTROL)
894 vim_modifiers |= MOUSE_CTRL;
895 if (GetKeyState(VK_MENU) & 0x8000)
896 vim_modifiers |= MOUSE_ALT;
897
898 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
899}
900
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 static void
902_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100903 HWND hwnd UNUSED,
904 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 int x,
906 int y,
907 UINT keyFlags)
908{
909 static LONG s_prevTime = 0;
910
911 LONG currentTime = GetMessageTime();
912 int button = -1;
913 int repeated_click;
914
Bram Moolenaar734a8672019-12-02 22:49:38 +0100915 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100916 (void)SetFocus(s_hwnd);
917
918 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
919 button = MOUSE_LEFT;
920 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
921 button = MOUSE_MIDDLE;
922 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
923 button = MOUSE_RIGHT;
924 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
925 {
926#ifndef GET_XBUTTON_WPARAM
927# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
928#endif
929 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
930 }
931 else if (s_uMsg == WM_CAPTURECHANGED)
932 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100933 // on W95/NT4, somehow you get in here with an odd Msg
934 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100935 if (s_button_pending == MOUSE_LEFT)
936 button = MOUSE_RIGHT;
937 else
938 button = MOUSE_LEFT;
939 }
940 if (button >= 0)
941 {
942 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
943
944 /*
945 * Holding down the left and right buttons simulates pushing the middle
946 * button.
947 */
948 if (repeated_click
949 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
950 || (button == MOUSE_RIGHT
951 && s_button_pending == MOUSE_LEFT)))
952 {
953 /*
954 * Hmm, gui.c will ignore more than one button down at a time, so
955 * pretend we let go of it first.
956 */
957 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
958 button = MOUSE_MIDDLE;
959 repeated_click = FALSE;
960 s_button_pending = -1;
961 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
962 }
963 else if ((repeated_click)
964 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
965 {
966 if (s_button_pending > -1)
967 {
968 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
969 s_button_pending = -1;
970 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100971 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100972 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
973 }
974 else
975 {
976 /*
977 * If this is the first press (i.e. not a multiple click) don't
978 * action immediately, but store and wait for:
979 * i) button-up
980 * ii) mouse move
981 * iii) another button press
982 * before using it.
983 * This enables us to make left+right simulate middle button,
984 * without left or right being actioned first. The side-effect is
985 * that if you click and hold the mouse without dragging, the
986 * cursor doesn't move until you release the button. In practice
987 * this is hardly a problem.
988 */
989 s_button_pending = button;
990 s_x_pending = x;
991 s_y_pending = y;
992 s_kFlags_pending = keyFlags;
993 }
994
995 s_prevTime = currentTime;
996 }
997}
998
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100999 static void
1000_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001001 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001002 int x,
1003 int y,
1004 UINT keyFlags)
1005{
1006 int button;
1007
1008 s_getting_focus = FALSE;
1009 if (s_button_pending > -1)
1010 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001011 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001012 _OnMouseEvent(s_button_pending, s_x_pending,
1013 s_y_pending, FALSE, s_kFlags_pending);
1014 s_button_pending = -1;
1015 }
1016 if (s_uMsg == WM_MOUSEMOVE)
1017 {
1018 /*
1019 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1020 * down.
1021 */
1022 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1023 | MK_XBUTTON1 | MK_XBUTTON2)))
1024 {
1025 gui_mouse_moved(x, y);
1026 return;
1027 }
1028
1029 /*
1030 * While button is down, keep grabbing mouse move events when
1031 * the mouse goes outside the window
1032 */
1033 SetCapture(s_textArea);
1034 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001035 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001036 }
1037 else
1038 {
1039 ReleaseCapture();
1040 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001041 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001042 }
1043
1044 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1045}
1046
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001047 static void
1048_OnSizeTextArea(
1049 HWND hwnd UNUSED,
1050 UINT state UNUSED,
1051 int cx UNUSED,
1052 int cy UNUSED)
1053{
1054#if defined(FEAT_DIRECTX)
1055 if (IS_ENABLE_DIRECTX())
1056 directx_binddc();
1057#endif
1058}
1059
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001060#ifdef FEAT_MENU
1061/*
1062 * Find the vimmenu_T with the given id
1063 */
1064 static vimmenu_T *
1065gui_mswin_find_menu(
1066 vimmenu_T *pMenu,
1067 int id)
1068{
1069 vimmenu_T *pChildMenu;
1070
1071 while (pMenu)
1072 {
1073 if (pMenu->id == (UINT)id)
1074 break;
1075 if (pMenu->children != NULL)
1076 {
1077 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1078 if (pChildMenu)
1079 {
1080 pMenu = pChildMenu;
1081 break;
1082 }
1083 }
1084 pMenu = pMenu->next;
1085 }
1086 return pMenu;
1087}
1088
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001089 static void
1090_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001091 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwndCtl UNUSED,
1094 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001095{
1096 vimmenu_T *pMenu;
1097
1098 pMenu = gui_mswin_find_menu(root_menu, id);
1099 if (pMenu)
1100 gui_menu_cb(pMenu);
1101}
1102#endif
1103
1104#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001106 * Handle a Find/Replace window message.
1107 */
1108 static void
1109_OnFindRepl(void)
1110{
1111 int flags = 0;
1112 int down;
1113
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001115 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 (void)SetFocus(s_hwnd);
1117
1118 if (s_findrep_struct.Flags & FR_FINDNEXT)
1119 {
1120 flags = FRD_FINDNEXT;
1121
Bram Moolenaar734a8672019-12-02 22:49:38 +01001122 // Give main window the focus back: this is so the cursor isn't
1123 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001124 (void)SetFocus(s_hwnd);
1125 }
1126 else if (s_findrep_struct.Flags & FR_REPLACE)
1127 {
1128 flags = FRD_REPLACE;
1129
Bram Moolenaar734a8672019-12-02 22:49:38 +01001130 // Give main window the focus back: this is so the cursor isn't
1131 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001132 (void)SetFocus(s_hwnd);
1133 }
1134 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1135 {
1136 flags = FRD_REPLACEALL;
1137 }
1138
1139 if (flags != 0)
1140 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001141 char_u *p, *q;
1142
Bram Moolenaar734a8672019-12-02 22:49:38 +01001143 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001144 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1145 flags |= FRD_WHOLE_WORD;
1146 if (s_findrep_struct.Flags & FR_MATCHCASE)
1147 flags |= FRD_MATCH_CASE;
1148 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001149 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1150 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1151 if (p != NULL && q != NULL)
1152 gui_do_findrepl(flags, p, q, down);
1153 vim_free(p);
1154 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001155 }
1156}
1157#endif
1158
1159 static void
1160HandleMouseHide(UINT uMsg, LPARAM lParam)
1161{
1162 static LPARAM last_lParam = 0L;
1163
Bram Moolenaar734a8672019-12-02 22:49:38 +01001164 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001165 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1166 {
1167 if (lParam == last_lParam)
1168 return;
1169 last_lParam = lParam;
1170 }
1171
Bram Moolenaar734a8672019-12-02 22:49:38 +01001172 // Handle specially, to centralise coding. We need to be sure we catch all
1173 // possible events which should cause us to restore the cursor (as it is a
1174 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001175 switch (uMsg)
1176 {
1177 case WM_KEYUP:
1178 case WM_CHAR:
1179 /*
1180 * blank out the pointer if necessary
1181 */
1182 if (p_mh)
1183 gui_mch_mousehide(TRUE);
1184 break;
1185
Bram Moolenaar734a8672019-12-02 22:49:38 +01001186 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001188 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189 case WM_LBUTTONDOWN:
1190 case WM_LBUTTONUP:
1191 case WM_MBUTTONDOWN:
1192 case WM_MBUTTONUP:
1193 case WM_RBUTTONDOWN:
1194 case WM_RBUTTONUP:
1195 case WM_XBUTTONDOWN:
1196 case WM_XBUTTONUP:
1197 case WM_NCMOUSEMOVE:
1198 case WM_NCLBUTTONDOWN:
1199 case WM_NCLBUTTONUP:
1200 case WM_NCMBUTTONDOWN:
1201 case WM_NCMBUTTONUP:
1202 case WM_NCRBUTTONDOWN:
1203 case WM_NCRBUTTONUP:
1204 case WM_KILLFOCUS:
1205 /*
1206 * if the pointer is currently hidden, then we should show it.
1207 */
1208 gui_mch_mousehide(FALSE);
1209 break;
1210 }
1211}
1212
1213 static LRESULT CALLBACK
1214_TextAreaWndProc(
1215 HWND hwnd,
1216 UINT uMsg,
1217 WPARAM wParam,
1218 LPARAM lParam)
1219{
1220 /*
1221 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1222 hwnd, uMsg, wParam, lParam);
1223 */
1224
1225 HandleMouseHide(uMsg, lParam);
1226
1227 s_uMsg = uMsg;
1228 s_wParam = wParam;
1229 s_lParam = lParam;
1230
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001231#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001232 TrackUserActivity(uMsg);
1233#endif
1234
1235 switch (uMsg)
1236 {
1237 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1238 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1240 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1244 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1245 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1248 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001251 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001252
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001253#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001254 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1255 return TRUE;
1256#endif
1257 default:
1258 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1259 }
1260}
1261
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001262#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001264#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001265
1266 LRESULT WINAPI
1267vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1268{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001269#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001270 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001271#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001272 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273#endif
1274}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001275
1276/*
1277 * Called when the foreground or background color has been changed.
1278 */
1279 void
1280gui_mch_new_colors(void)
1281{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001282 HBRUSH prevBrush;
1283
1284 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001285 prevBrush = (HBRUSH)SetClassLongPtr(
1286 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001287 InvalidateRect(s_hwnd, NULL, TRUE);
1288 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001289}
1290
1291/*
1292 * Set the colors to their default values.
1293 */
1294 void
1295gui_mch_def_colors(void)
1296{
1297 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1298 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1299 gui.def_norm_pixel = gui.norm_pixel;
1300 gui.def_back_pixel = gui.back_pixel;
1301}
1302
1303/*
1304 * Open the GUI window which was created by a call to gui_mch_init().
1305 */
1306 int
1307gui_mch_open(void)
1308{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001309 // Actually open the window, if not already visible
1310 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311 if (!IsWindowVisible(s_hwnd))
1312 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1313
1314#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001315 // Init replace string here, so that we keep it when re-opening the
1316 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001317 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1318#endif
1319
1320 return OK;
1321}
1322
1323/*
1324 * Get the position of the top left corner of the window.
1325 */
1326 int
1327gui_mch_get_winpos(int *x, int *y)
1328{
1329 RECT rect;
1330
1331 GetWindowRect(s_hwnd, &rect);
1332 *x = rect.left;
1333 *y = rect.top;
1334 return OK;
1335}
1336
1337/*
1338 * Set the position of the top left corner of the window to the given
1339 * coordinates.
1340 */
1341 void
1342gui_mch_set_winpos(int x, int y)
1343{
1344 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1345 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1346}
1347 void
1348gui_mch_set_text_area_pos(int x, int y, int w, int h)
1349{
1350 static int oldx = 0;
1351 static int oldy = 0;
1352
1353 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1354
1355#ifdef FEAT_TOOLBAR
1356 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1357 SendMessage(s_toolbarhwnd, WM_SIZE,
1358 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1359#endif
1360#if defined(FEAT_GUI_TABLINE)
1361 if (showing_tabline)
1362 {
1363 int top = 0;
1364 RECT rect;
1365
1366# ifdef FEAT_TOOLBAR
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1369# endif
1370 GetClientRect(s_hwnd, &rect);
1371 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1372 }
1373#endif
1374
Bram Moolenaar734a8672019-12-02 22:49:38 +01001375 // When side scroll bar is unshown, the size of window will change.
1376 // then, the text area move left or right. thus client rect should be
1377 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001378 if (oldx != x || oldy != y)
1379 {
1380 InvalidateRect(s_hwnd, NULL, FALSE);
1381 oldx = x;
1382 oldy = y;
1383 }
1384}
1385
1386
1387/*
1388 * Scrollbar stuff:
1389 */
1390
1391 void
1392gui_mch_enable_scrollbar(
1393 scrollbar_T *sb,
1394 int flag)
1395{
1396 ShowScrollBar(sb->id, SB_CTL, flag);
1397
Bram Moolenaar734a8672019-12-02 22:49:38 +01001398 // TODO: When the window is maximized, the size of the window stays the
1399 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1400 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001401}
1402
1403 void
1404gui_mch_set_scrollbar_pos(
1405 scrollbar_T *sb,
1406 int x,
1407 int y,
1408 int w,
1409 int h)
1410{
1411 SetWindowPos(sb->id, NULL, x, y, w, h,
1412 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1413}
1414
Bram Moolenaar203ec772020-07-17 20:43:43 +02001415 int
1416gui_mch_get_scrollbar_xpadding(void)
1417{
1418 RECT rcTxt, rcWnd;
1419 int xpad;
1420
1421 GetWindowRect(s_textArea, &rcTxt);
1422 GetWindowRect(s_hwnd, &rcWnd);
1423 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1424 - GetSystemMetrics(SM_CXFRAME)
1425 - GetSystemMetrics(SM_CXPADDEDBORDER);
1426 return (xpad < 0) ? 0 : xpad;
1427}
1428
1429 int
1430gui_mch_get_scrollbar_ypadding(void)
1431{
1432 RECT rcTxt, rcWnd;
1433 int ypad;
1434
1435 GetWindowRect(s_textArea, &rcTxt);
1436 GetWindowRect(s_hwnd, &rcWnd);
1437 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1438 - GetSystemMetrics(SM_CYFRAME)
1439 - GetSystemMetrics(SM_CXPADDEDBORDER);
1440 return (ypad < 0) ? 0 : ypad;
1441}
1442
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001443 void
1444gui_mch_create_scrollbar(
1445 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001446 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001447{
1448 sb->id = CreateWindow(
1449 "SCROLLBAR", "Scrollbar",
1450 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001451 10, // Any value will do for now
1452 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001453 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001454 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001455}
1456
1457/*
1458 * Find the scrollbar with the given hwnd.
1459 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001460 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001461gui_mswin_find_scrollbar(HWND hwnd)
1462{
1463 win_T *wp;
1464
1465 if (gui.bottom_sbar.id == hwnd)
1466 return &gui.bottom_sbar;
1467 FOR_ALL_WINDOWS(wp)
1468 {
1469 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1470 return &wp->w_scrollbars[SBAR_LEFT];
1471 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1472 return &wp->w_scrollbars[SBAR_RIGHT];
1473 }
1474 return NULL;
1475}
1476
1477/*
1478 * Get the character size of a font.
1479 */
1480 static void
1481GetFontSize(GuiFont font)
1482{
1483 HWND hwnd = GetDesktopWindow();
1484 HDC hdc = GetWindowDC(hwnd);
1485 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001486 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001487 TEXTMETRIC tm;
1488
1489 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001490 // GetTextMetrics() may not return the right value in tmAveCharWidth
1491 // for some fonts. Do our own average computation.
1492 GetTextExtentPoint(hdc,
1493 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1494 52, &size);
1495 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001496
1497 gui.char_height = tm.tmHeight + p_linespace;
1498
1499 SelectFont(hdc, hfntOld);
1500
1501 ReleaseDC(hwnd, hdc);
1502}
1503
1504/*
1505 * Adjust gui.char_height (after 'linespace' was changed).
1506 */
1507 int
1508gui_mch_adjust_charheight(void)
1509{
1510 GetFontSize(gui.norm_font);
1511 return OK;
1512}
1513
1514 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001515get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001516{
1517 HFONT font = NULL;
1518
Bram Moolenaar734a8672019-12-02 22:49:38 +01001519 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001520 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001521
1522 if (font == NULL)
1523 return NOFONT;
1524
1525 return (GuiFont)font;
1526}
1527
1528 static int
1529pixels_to_points(int pixels, int vertical)
1530{
1531 int points;
1532 HWND hwnd;
1533 HDC hdc;
1534
1535 hwnd = GetDesktopWindow();
1536 hdc = GetWindowDC(hwnd);
1537
1538 points = MulDiv(pixels, 72,
1539 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1540
1541 ReleaseDC(hwnd, hdc);
1542
1543 return points;
1544}
1545
1546 GuiFont
1547gui_mch_get_font(
1548 char_u *name,
1549 int giveErrorIfMissing)
1550{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001551 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001552 GuiFont font = NOFONT;
1553
1554 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1555 font = get_font_handle(&lf);
1556 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001557 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001558 return font;
1559}
1560
1561#if defined(FEAT_EVAL) || defined(PROTO)
1562/*
1563 * Return the name of font "font" in allocated memory.
1564 * Don't know how to get the actual name, thus use the provided name.
1565 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001567gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568{
1569 if (name == NULL)
1570 return NULL;
1571 return vim_strsave(name);
1572}
1573#endif
1574
1575 void
1576gui_mch_free_font(GuiFont font)
1577{
1578 if (font)
1579 DeleteObject((HFONT)font);
1580}
1581
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582/*
1583 * Return the Pixel value (color) for the given color name.
1584 * Return INVALCOLOR for error.
1585 */
1586 guicolor_T
1587gui_mch_get_color(char_u *name)
1588{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001589 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590
1591 typedef struct SysColorTable
1592 {
1593 char *name;
1594 int color;
1595 } SysColorTable;
1596
1597 static SysColorTable sys_table[] =
1598 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1600 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001601#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001602 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1603#endif
1604 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1605 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1606 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1607 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1608 {"SYS_DESKTOP", COLOR_DESKTOP},
1609 {"SYS_INFOBK", COLOR_INFOBK},
1610 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1611 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001612 {"SYS_BTNFACE", COLOR_BTNFACE},
1613 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1614 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1615 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1616 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1617 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1618 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1619 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1620 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1621 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1622 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1623 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1624 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1625 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1626 {"SYS_MENU", COLOR_MENU},
1627 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1628 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1629 {"SYS_WINDOW", COLOR_WINDOW},
1630 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1631 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1632 };
1633
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634 /*
1635 * Try to look up a system colour.
1636 */
1637 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1638 if (STRICMP(name, sys_table[i].name) == 0)
1639 return GetSysColor(sys_table[i].color);
1640
Bram Moolenaarab302212016-04-26 20:59:29 +02001641 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001642}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001643
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001644 guicolor_T
1645gui_mch_get_rgb_color(int r, int g, int b)
1646{
1647 return gui_get_rgb_color_cmn(r, g, b);
1648}
1649
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001650/*
1651 * Return OK if the key with the termcap name "name" is supported.
1652 */
1653 int
1654gui_mch_haskey(char_u *name)
1655{
1656 int i;
1657
1658 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1659 if (name[0] == special_keys[i].vim_code0 &&
1660 name[1] == special_keys[i].vim_code1)
1661 return OK;
1662 return FAIL;
1663}
1664
1665 void
1666gui_mch_beep(void)
1667{
1668 MessageBeep(MB_OK);
1669}
1670/*
1671 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1672 */
1673 void
1674gui_mch_invert_rectangle(
1675 int r,
1676 int c,
1677 int nr,
1678 int nc)
1679{
1680 RECT rc;
1681
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001682#if defined(FEAT_DIRECTX)
1683 if (IS_ENABLE_DIRECTX())
1684 DWriteContext_Flush(s_dwc);
1685#endif
1686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687 /*
1688 * Note: InvertRect() excludes right and bottom of rectangle.
1689 */
1690 rc.left = FILL_X(c);
1691 rc.top = FILL_Y(r);
1692 rc.right = rc.left + nc * gui.char_width;
1693 rc.bottom = rc.top + nr * gui.char_height;
1694 InvertRect(s_hdc, &rc);
1695}
1696
1697/*
1698 * Iconify the GUI window.
1699 */
1700 void
1701gui_mch_iconify(void)
1702{
1703 ShowWindow(s_hwnd, SW_MINIMIZE);
1704}
1705
1706/*
1707 * Draw a cursor without focus.
1708 */
1709 void
1710gui_mch_draw_hollow_cursor(guicolor_T color)
1711{
1712 HBRUSH hbr;
1713 RECT rc;
1714
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001715#if defined(FEAT_DIRECTX)
1716 if (IS_ENABLE_DIRECTX())
1717 DWriteContext_Flush(s_dwc);
1718#endif
1719
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720 /*
1721 * Note: FrameRect() excludes right and bottom of rectangle.
1722 */
1723 rc.left = FILL_X(gui.col);
1724 rc.top = FILL_Y(gui.row);
1725 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001726 if (mb_lefthalve(gui.row, gui.col))
1727 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 rc.bottom = rc.top + gui.char_height;
1729 hbr = CreateSolidBrush(color);
1730 FrameRect(s_hdc, &rc, hbr);
1731 DeleteBrush(hbr);
1732}
1733/*
1734 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1735 * color "color".
1736 */
1737 void
1738gui_mch_draw_part_cursor(
1739 int w,
1740 int h,
1741 guicolor_T color)
1742{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001743 RECT rc;
1744
1745 /*
1746 * Note: FillRect() excludes right and bottom of rectangle.
1747 */
1748 rc.left =
1749#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001750 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1752#endif
1753 FILL_X(gui.col);
1754 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1755 rc.right = rc.left + w;
1756 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001757
Bram Moolenaar92467d32017-12-05 13:22:16 +01001758 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759}
1760
1761
1762/*
1763 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1764 * dead key's nominal character and re-post the original message.
1765 */
1766 static void
1767outputDeadKey_rePost(MSG originalMsg)
1768{
1769 static MSG deadCharExpel;
1770
1771 if (!dead_key)
1772 return;
1773
1774 dead_key = 0;
1775
Bram Moolenaar734a8672019-12-02 22:49:38 +01001776 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 deadCharExpel.message = originalMsg.message;
1778 deadCharExpel.hwnd = originalMsg.hwnd;
1779 deadCharExpel.wParam = VK_SPACE;
1780
1781 MyTranslateMessage(&deadCharExpel);
1782
Bram Moolenaar734a8672019-12-02 22:49:38 +01001783 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001784 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1785 originalMsg.lParam);
1786}
1787
1788
1789/*
1790 * Process a single Windows message.
1791 * If one is not available we hang until one is.
1792 */
1793 static void
1794process_message(void)
1795{
1796 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001797 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001798 char_u string[40];
1799 int i;
1800 int modifiers = 0;
1801 int key;
1802#ifdef FEAT_MENU
1803 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1804#endif
1805
1806 pGetMessage(&msg, NULL, 0, 0);
1807
1808#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001809 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 if (msg.message == WM_OLE)
1811 {
1812 char_u *str = (char_u *)msg.lParam;
1813 if (str == NULL || *str == NUL)
1814 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001815 // Message can't be ours, forward it. Fixes problem with Ultramon
1816 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 pDispatchMessage(&msg);
1818 }
1819 else
1820 {
1821 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001822 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 }
1824 return;
1825 }
1826#endif
1827
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001828#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001829 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1831 {
1832 HandleMouseHide(msg.message, msg.lParam);
1833 return;
1834 }
1835#endif
1836
1837 /*
1838 * Check if it's a special key that we recognise. If not, call
1839 * TranslateMessage().
1840 */
1841 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1842 {
1843 vk = (int) msg.wParam;
1844
1845 /*
1846 * Handle dead keys in special conditions in other cases we let Windows
1847 * handle them and do not interfere.
1848 *
1849 * The dead_key flag must be reset on several occasions:
1850 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1851 * consumed at that point (This is when we let Windows combine the
1852 * dead character on its own)
1853 *
1854 * - Before doing something special such as regenerating keypresses to
1855 * expel the dead character as this could trigger an infinite loop if
1856 * for some reason MyTranslateMessage() do not trigger a call
1857 * immediately to _OnChar() (or _OnSysChar()).
1858 */
1859 if (dead_key)
1860 {
1861 /*
1862 * If a dead key was pressed and the user presses VK_SPACE,
1863 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1864 * with the dead char now, so do nothing special and let Windows
1865 * handle it.
1866 *
1867 * Note that VK_SPACE combines with the dead_key's character and
1868 * only one WM_CHAR will be generated by TranslateMessage(), in
1869 * the two other cases two WM_CHAR will be generated: the dead
1870 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1871 * user expects.
1872 */
1873 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1874 {
1875 dead_key = 0;
1876 MyTranslateMessage(&msg);
1877 return;
1878 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001879 // In modes where we are not typing, dead keys should behave
1880 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1882 {
1883 outputDeadKey_rePost(msg);
1884 return;
1885 }
1886 }
1887
Bram Moolenaar734a8672019-12-02 22:49:38 +01001888 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001889 if (vk == VK_CANCEL)
1890 {
1891 trash_input_buf();
1892 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001893 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001894 string[0] = Ctrl_C;
1895 add_to_input_buf(string, 1);
1896 }
1897
1898 for (i = 0; special_keys[i].key_sym != 0; i++)
1899 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001900 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001901 if (special_keys[i].key_sym == vk
1902 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1903 {
1904 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001905 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906 * is a key that would normally trigger the dead key nominal
1907 * character output (such as a NUMPAD printable character or
1908 * the TAB key, etc...).
1909 */
1910 if (dead_key && (special_keys[i].vim_code0 == 'K'
1911 || vk == VK_TAB || vk == CAR))
1912 {
1913 outputDeadKey_rePost(msg);
1914 return;
1915 }
1916
1917#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001918 // Check for <F10>: Windows selects the menu. When <F10> is
1919 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001920 if (vk == VK_F10
1921 && gui.menu_is_active
1922 && check_map(k10, State, FALSE, TRUE, FALSE,
1923 NULL, NULL) == NULL)
1924 break;
1925#endif
1926 if (GetKeyState(VK_SHIFT) & 0x8000)
1927 modifiers |= MOD_MASK_SHIFT;
1928 /*
1929 * Don't use caps-lock as shift, because these are special keys
1930 * being considered here, and we only want letters to get
1931 * shifted -- webb
1932 */
1933 /*
1934 if (GetKeyState(VK_CAPITAL) & 0x0001)
1935 modifiers ^= MOD_MASK_SHIFT;
1936 */
1937 if (GetKeyState(VK_CONTROL) & 0x8000)
1938 modifiers |= MOD_MASK_CTRL;
1939 if (GetKeyState(VK_MENU) & 0x8000)
1940 modifiers |= MOD_MASK_ALT;
1941
1942 if (special_keys[i].vim_code1 == NUL)
1943 key = special_keys[i].vim_code0;
1944 else
1945 key = TO_SPECIAL(special_keys[i].vim_code0,
1946 special_keys[i].vim_code1);
1947 key = simplify_key(key, &modifiers);
1948 if (key == CSI)
1949 key = K_CSI;
1950
1951 if (modifiers)
1952 {
1953 string[0] = CSI;
1954 string[1] = KS_MODIFIER;
1955 string[2] = modifiers;
1956 add_to_input_buf(string, 3);
1957 }
1958
1959 if (IS_SPECIAL(key))
1960 {
1961 string[0] = CSI;
1962 string[1] = K_SECOND(key);
1963 string[2] = K_THIRD(key);
1964 add_to_input_buf(string, 3);
1965 }
1966 else
1967 {
1968 int len;
1969
Bram Moolenaar734a8672019-12-02 22:49:38 +01001970 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001971 len = char_to_string(key, string, 40, FALSE);
1972 add_to_input_buf(string, len);
1973 }
1974 break;
1975 }
1976 }
1977 if (special_keys[i].key_sym == 0)
1978 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001979 // Some keys need C-S- where they should only need C-.
1980 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1981 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001982 if (vk != 0xff
1983 && (GetKeyState(VK_CONTROL) & 0x8000)
1984 && !(GetKeyState(VK_SHIFT) & 0x8000)
1985 && !(GetKeyState(VK_MENU) & 0x8000))
1986 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001987 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001988 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1989 {
1990 string[0] = Ctrl_HAT;
1991 add_to_input_buf(string, 1);
1992 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001993 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1994 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001995 {
1996 string[0] = Ctrl__;
1997 add_to_input_buf(string, 1);
1998 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001999 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002000 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2001 {
2002 string[0] = Ctrl_AT;
2003 add_to_input_buf(string, 1);
2004 }
2005 else
2006 MyTranslateMessage(&msg);
2007 }
2008 else
2009 MyTranslateMessage(&msg);
2010 }
2011 }
2012#ifdef FEAT_MBYTE_IME
2013 else if (msg.message == WM_IME_NOTIFY)
2014 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2015 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002016 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002017 MyTranslateMessage(&msg);
2018#endif
2019#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002020// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002021 else if (msg.message == WM_IME_STARTCOMPOSITION)
2022 {
2023 POINT point;
2024
2025 global_ime_set_font(&norm_logfont);
2026 point.x = FILL_X(gui.col);
2027 point.y = FILL_Y(gui.row);
2028 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2029 global_ime_set_position(&point);
2030 }
2031#endif
2032
2033#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002034 // Check for <F10>: Default effect is to select the menu. When <F10> is
2035 // mapped we need to stop it here to avoid strange effects (e.g., for the
2036 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002037 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2038 NULL, NULL) == NULL)
2039#endif
2040 pDispatchMessage(&msg);
2041}
2042
2043/*
2044 * Catch up with any queued events. This may put keyboard input into the
2045 * input buffer, call resize call-backs, trigger timers etc. If there is
2046 * nothing in the event queue (& no timers pending), then we return
2047 * immediately.
2048 */
2049 void
2050gui_mch_update(void)
2051{
2052 MSG msg;
2053
2054 if (!s_busy_processing)
2055 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2056 && !vim_is_input_buf_full())
2057 process_message();
2058}
2059
Bram Moolenaar4231da42016-06-02 14:30:04 +02002060 static void
2061remove_any_timer(void)
2062{
2063 MSG msg;
2064
2065 if (s_wait_timer != 0 && !s_timed_out)
2066 {
2067 KillTimer(NULL, s_wait_timer);
2068
Bram Moolenaar734a8672019-12-02 22:49:38 +01002069 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002070 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2071 ;
2072 s_wait_timer = 0;
2073 }
2074}
2075
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002076/*
2077 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2078 * from the keyboard.
2079 * wtime == -1 Wait forever.
2080 * wtime == 0 This should never happen.
2081 * wtime > 0 Wait wtime milliseconds for a character.
2082 * Returns OK if a character was found to be available within the given time,
2083 * or FAIL otherwise.
2084 */
2085 int
2086gui_mch_wait_for_chars(int wtime)
2087{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 int focus;
2089
2090 s_timed_out = FALSE;
2091
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002092 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002094 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002095 if (s_busy_processing)
2096 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002097
2098 // When called with "wtime" zero, just want one msec.
2099 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002100 (TIMERPROC)_OnTimer);
2101 }
2102
2103 allow_scrollbar = TRUE;
2104
2105 focus = gui.in_focus;
2106 while (!s_timed_out)
2107 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002108 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002109 if (gui.in_focus != focus)
2110 {
2111 if (gui.in_focus)
2112 gui_mch_start_blink();
2113 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002114 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115 focus = gui.in_focus;
2116 }
2117
2118 if (s_need_activate)
2119 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 s_need_activate = FALSE;
2122 }
2123
Bram Moolenaar4231da42016-06-02 14:30:04 +02002124#ifdef FEAT_TIMERS
2125 did_add_timer = FALSE;
2126#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002127#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002128 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002129 for (;;)
2130 {
2131 MSG msg;
2132
2133 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002134# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002135 if (did_add_timer)
2136 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002137# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002138 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2139 {
2140 process_message();
2141 break;
2142 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002143 else if (input_available()
2144 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2145 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002146 break;
2147 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002148#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002149 // Don't use gui_mch_update() because then we will spin-lock until a
2150 // char arrives, instead we use GetMessage() to hang until an
2151 // event arrives. No need to check for input_buf_full because we are
2152 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155
2156 if (input_available())
2157 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002158 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002159 allow_scrollbar = FALSE;
2160
Bram Moolenaar89c00032019-09-04 13:53:21 +02002161 // Clear pending mouse button, the release event may have been
2162 // taken by the dialog window. But don't do this when getting
2163 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002164 if (!s_getting_focus)
2165 s_button_pending = -1;
2166
2167 return OK;
2168 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002169
2170#ifdef FEAT_TIMERS
2171 if (did_add_timer)
2172 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002173 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002174 remove_any_timer();
2175 break;
2176 }
2177#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002178 }
2179 allow_scrollbar = FALSE;
2180 return FAIL;
2181}
2182
2183/*
2184 * Clear a rectangular region of the screen from text pos (row1, col1) to
2185 * (row2, col2) inclusive.
2186 */
2187 void
2188gui_mch_clear_block(
2189 int row1,
2190 int col1,
2191 int row2,
2192 int col2)
2193{
2194 RECT rc;
2195
2196 /*
2197 * Clear one extra pixel at the far right, for when bold characters have
2198 * spilled over to the window border.
2199 * Note: FillRect() excludes right and bottom of rectangle.
2200 */
2201 rc.left = FILL_X(col1);
2202 rc.top = FILL_Y(row1);
2203 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2204 rc.bottom = FILL_Y(row2 + 1);
2205 clear_rect(&rc);
2206}
2207
2208/*
2209 * Clear the whole text window.
2210 */
2211 void
2212gui_mch_clear_all(void)
2213{
2214 RECT rc;
2215
2216 rc.left = 0;
2217 rc.top = 0;
2218 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2219 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2220 clear_rect(&rc);
2221}
2222/*
2223 * Menu stuff.
2224 */
2225
2226 void
2227gui_mch_enable_menu(int flag)
2228{
2229#ifdef FEAT_MENU
2230 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2231#endif
2232}
2233
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002234 void
2235gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002236 int x UNUSED,
2237 int y UNUSED,
2238 int w UNUSED,
2239 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002240{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002241 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242}
2243
2244#if defined(FEAT_MENU) || defined(PROTO)
2245/*
2246 * Make menu item hidden or not hidden
2247 */
2248 void
2249gui_mch_menu_hidden(
2250 vimmenu_T *menu,
2251 int hidden)
2252{
2253 /*
2254 * This doesn't do what we want. Hmm, just grey the menu items for now.
2255 */
2256 /*
2257 if (hidden)
2258 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2259 else
2260 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2261 */
2262 gui_mch_menu_grey(menu, hidden);
2263}
2264
2265/*
2266 * This is called after setting all the menus to grey/hidden or not.
2267 */
2268 void
2269gui_mch_draw_menubar(void)
2270{
2271 DrawMenuBar(s_hwnd);
2272}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002273#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002274
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275/*
2276 * Return the RGB value of a pixel as a long.
2277 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002278 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279gui_mch_get_rgb(guicolor_T pixel)
2280{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002281 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2282 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002283}
2284
2285#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002286/*
2287 * Convert pixels in X to dialog units
2288 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002289 static WORD
2290PixelToDialogX(int numPixels)
2291{
2292 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2293}
2294
Bram Moolenaar734a8672019-12-02 22:49:38 +01002295/*
2296 * Convert pixels in Y to dialog units
2297 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002298 static WORD
2299PixelToDialogY(int numPixels)
2300{
2301 return (WORD)((numPixels * 8) / s_dlgfntheight);
2302}
2303
Bram Moolenaar734a8672019-12-02 22:49:38 +01002304/*
2305 * Return the width in pixels of the given text in the given DC.
2306 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307 static int
2308GetTextWidth(HDC hdc, char_u *str, int len)
2309{
2310 SIZE size;
2311
2312 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2313 return size.cx;
2314}
2315
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002316/*
2317 * Return the width in pixels of the given text in the given DC, taking care
2318 * of 'encoding' to active codepage conversion.
2319 */
2320 static int
2321GetTextWidthEnc(HDC hdc, char_u *str, int len)
2322{
2323 SIZE size;
2324 WCHAR *wstr;
2325 int n;
2326 int wlen = len;
2327
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002328 wstr = enc_to_utf16(str, &wlen);
2329 if (wstr == NULL)
2330 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002332 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2333 vim_free(wstr);
2334 if (n)
2335 return size.cx;
2336 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002339static void get_work_area(RECT *spi_rect);
2340
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341/*
2342 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002343 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2344 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002345 */
2346 static BOOL
2347CenterWindow(
2348 HWND hwndChild,
2349 HWND hwndParent)
2350{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002351 HMONITOR mon;
2352 MONITORINFO moninfo;
2353 RECT rChild, rParent, rScreen;
2354 int wChild, hChild, wParent, hParent;
2355 int xNew, yNew;
2356 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357
2358 GetWindowRect(hwndChild, &rChild);
2359 wChild = rChild.right - rChild.left;
2360 hChild = rChild.bottom - rChild.top;
2361
Bram Moolenaar734a8672019-12-02 22:49:38 +01002362 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002363 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002365 else
2366 GetWindowRect(hwndParent, &rParent);
2367 wParent = rParent.right - rParent.left;
2368 hParent = rParent.bottom - rParent.top;
2369
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370 moninfo.cbSize = sizeof(MONITORINFO);
2371 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2372 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002373 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002374 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002375 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002376 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002378 hdc = GetDC(hwndChild);
2379 rScreen.left = 0;
2380 rScreen.top = 0;
2381 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2382 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2383 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384 }
2385
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002386 xNew = rParent.left + ((wParent - wChild) / 2);
2387 if (xNew < rScreen.left)
2388 xNew = rScreen.left;
2389 else if ((xNew + wChild) > rScreen.right)
2390 xNew = rScreen.right - wChild;
2391
2392 yNew = rParent.top + ((hParent - hChild) / 2);
2393 if (yNew < rScreen.top)
2394 yNew = rScreen.top;
2395 else if ((yNew + hChild) > rScreen.bottom)
2396 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397
2398 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2399 SWP_NOSIZE | SWP_NOZORDER);
2400}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002401#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002402
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002403#if defined(FEAT_TOOLBAR) || defined(PROTO)
2404 void
2405gui_mch_show_toolbar(int showit)
2406{
2407 if (s_toolbarhwnd == NULL)
2408 return;
2409
2410 if (showit)
2411 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002412# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002413 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002414# define TB_SETUNICODEFORMAT 0x2005
2415# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002416 // Enable unicode support
2417 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2418 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419 ShowWindow(s_toolbarhwnd, SW_SHOW);
2420 }
2421 else
2422 ShowWindow(s_toolbarhwnd, SW_HIDE);
2423}
2424
Bram Moolenaar734a8672019-12-02 22:49:38 +01002425// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002426# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002427
2428#endif
2429
2430#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2431 static void
2432add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2433{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002434 WCHAR *wn;
2435 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002437 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002439 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002440
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002441 infow.cbSize = sizeof(infow);
2442 infow.fMask = MIIM_TYPE | MIIM_ID;
2443 infow.wID = item_id;
2444 infow.fType = MFT_STRING;
2445 infow.dwTypeData = wn;
2446 infow.cch = (UINT)wcslen(wn);
2447 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2448 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002449}
2450
2451 static void
2452show_tabline_popup_menu(void)
2453{
2454 HMENU tab_pmenu;
2455 long rval;
2456 POINT pt;
2457
Bram Moolenaar734a8672019-12-02 22:49:38 +01002458 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002459 if (hold_gui_events
2460# ifdef FEAT_CMDWIN
2461 || cmdwin_type != 0
2462# endif
2463 )
2464 return;
2465
2466 tab_pmenu = CreatePopupMenu();
2467 if (tab_pmenu == NULL)
2468 return;
2469
2470 if (first_tabpage->tp_next != NULL)
2471 add_tabline_popup_menu_entry(tab_pmenu,
2472 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2473 add_tabline_popup_menu_entry(tab_pmenu,
2474 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2475 add_tabline_popup_menu_entry(tab_pmenu,
2476 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2477
2478 GetCursorPos(&pt);
2479 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2480 NULL);
2481
2482 DestroyMenu(tab_pmenu);
2483
Bram Moolenaar734a8672019-12-02 22:49:38 +01002484 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002485 if (rval > 0)
2486 {
2487 TCHITTESTINFO htinfo;
2488 int idx;
2489
2490 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2491 return;
2492
2493 htinfo.pt.x = pt.x;
2494 htinfo.pt.y = pt.y;
2495 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2496 if (idx == -1)
2497 idx = 0;
2498 else
2499 idx += 1;
2500
2501 send_tabline_menu_event(idx, (int)rval);
2502 }
2503}
2504
2505/*
2506 * Show or hide the tabline.
2507 */
2508 void
2509gui_mch_show_tabline(int showit)
2510{
2511 if (s_tabhwnd == NULL)
2512 return;
2513
2514 if (!showit != !showing_tabline)
2515 {
2516 if (showit)
2517 ShowWindow(s_tabhwnd, SW_SHOW);
2518 else
2519 ShowWindow(s_tabhwnd, SW_HIDE);
2520 showing_tabline = showit;
2521 }
2522}
2523
2524/*
2525 * Return TRUE when tabline is displayed.
2526 */
2527 int
2528gui_mch_showing_tabline(void)
2529{
2530 return s_tabhwnd != NULL && showing_tabline;
2531}
2532
2533/*
2534 * Update the labels of the tabline.
2535 */
2536 void
2537gui_mch_update_tabline(void)
2538{
2539 tabpage_T *tp;
2540 TCITEM tie;
2541 int nr = 0;
2542 int curtabidx = 0;
2543 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002544 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002545
2546 if (s_tabhwnd == NULL)
2547 return;
2548
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002549# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002550 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002551# define CCM_SETUNICODEFORMAT 0x2005
2552# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002553 // Enable unicode support
2554 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002555
2556 tie.mask = TCIF_TEXT;
2557 tie.iImage = -1;
2558
Bram Moolenaar734a8672019-12-02 22:49:38 +01002559 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002560 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2561
Bram Moolenaar734a8672019-12-02 22:49:38 +01002562 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002563 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2564 {
2565 if (tp == curtab)
2566 curtabidx = nr;
2567
2568 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2569 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002570 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571 tie.pszText = "-Empty-";
2572 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2573 tabadded = 1;
2574 }
2575
2576 get_tabline_label(tp, FALSE);
2577 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002578
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002579 wstr = enc_to_utf16(NameBuff, NULL);
2580 if (wstr != NULL)
2581 {
2582 TCITEMW tiw;
2583
2584 tiw.mask = TCIF_TEXT;
2585 tiw.iImage = -1;
2586 tiw.pszText = wstr;
2587 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2588 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590 }
2591
Bram Moolenaar734a8672019-12-02 22:49:38 +01002592 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2594 TabCtrl_DeleteItem(s_tabhwnd, nr);
2595
2596 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2597 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2598
Bram Moolenaar734a8672019-12-02 22:49:38 +01002599 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002600 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2601 RedrawWindow(s_tabhwnd, NULL, NULL,
2602 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2603
2604 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2605 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2606}
2607
2608/*
2609 * Set the current tab to "nr". First tab is 1.
2610 */
2611 void
2612gui_mch_set_curtab(int nr)
2613{
2614 if (s_tabhwnd == NULL)
2615 return;
2616
2617 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2618 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2619}
2620
2621#endif
2622
2623/*
2624 * ":simalt" command.
2625 */
2626 void
2627ex_simalt(exarg_T *eap)
2628{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002629 char_u *keys = eap->arg;
2630 int fill_typebuf = FALSE;
2631 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632
2633 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2634 while (*keys)
2635 {
2636 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002637 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002638 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2639 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002640 fill_typebuf = TRUE;
2641 }
2642 if (fill_typebuf)
2643 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002644 // Put a NOP in the typeahead buffer so that the message will get
2645 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002646 key_name[0] = K_SPECIAL;
2647 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002648 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002649 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002650#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002651 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002652#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002653 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002654 }
2655}
2656
2657/*
2658 * Create the find & replace dialogs.
2659 * You can't have both at once: ":find" when replace is showing, destroys
2660 * the replace dialog first, and the other way around.
2661 */
2662#ifdef MSWIN_FIND_REPLACE
2663 static void
2664initialise_findrep(char_u *initial_string)
2665{
2666 int wword = FALSE;
2667 int mcase = !p_ic;
2668 char_u *entry_text;
2669
Bram Moolenaar734a8672019-12-02 22:49:38 +01002670 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002671 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2672
2673 s_findrep_struct.hwndOwner = s_hwnd;
2674 s_findrep_struct.Flags = FR_DOWN;
2675 if (mcase)
2676 s_findrep_struct.Flags |= FR_MATCHCASE;
2677 if (wword)
2678 s_findrep_struct.Flags |= FR_WHOLEWORD;
2679 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002680 {
2681 WCHAR *p = enc_to_utf16(entry_text, NULL);
2682 if (p != NULL)
2683 {
2684 int len = s_findrep_struct.wFindWhatLen - 1;
2685
2686 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2687 s_findrep_struct.lpstrFindWhat[len] = NUL;
2688 vim_free(p);
2689 }
2690 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002691 vim_free(entry_text);
2692}
2693#endif
2694
2695 static void
2696set_window_title(HWND hwnd, char *title)
2697{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002698 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 {
2700 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701
Bram Moolenaar734a8672019-12-02 22:49:38 +01002702 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002703 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2704 if (wbuf != NULL)
2705 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002706 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 }
2709 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002710 else
2711 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712}
2713
2714 void
2715gui_mch_find_dialog(exarg_T *eap)
2716{
2717#ifdef MSWIN_FIND_REPLACE
2718 if (s_findrep_msg != 0)
2719 {
2720 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2721 DestroyWindow(s_findrep_hwnd);
2722
2723 if (!IsWindow(s_findrep_hwnd))
2724 {
2725 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002726 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002727 }
2728
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002729 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 (void)SetFocus(s_findrep_hwnd);
2731
2732 s_findrep_is_find = TRUE;
2733 }
2734#endif
2735}
2736
2737
2738 void
2739gui_mch_replace_dialog(exarg_T *eap)
2740{
2741#ifdef MSWIN_FIND_REPLACE
2742 if (s_findrep_msg != 0)
2743 {
2744 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2745 DestroyWindow(s_findrep_hwnd);
2746
2747 if (!IsWindow(s_findrep_hwnd))
2748 {
2749 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002750 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751 }
2752
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002753 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002754 (void)SetFocus(s_findrep_hwnd);
2755
2756 s_findrep_is_find = FALSE;
2757 }
2758#endif
2759}
2760
2761
2762/*
2763 * Set visibility of the pointer.
2764 */
2765 void
2766gui_mch_mousehide(int hide)
2767{
2768 if (hide != gui.pointer_hidden)
2769 {
2770 ShowCursor(!hide);
2771 gui.pointer_hidden = hide;
2772 }
2773}
2774
2775#ifdef FEAT_MENU
2776 static void
2777gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2778{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002779 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002780 gui_mch_mousehide(FALSE);
2781
2782 (void)TrackPopupMenu(
2783 (HMENU)menu->submenu_id,
2784 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2785 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002786 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002787 s_hwnd,
2788 NULL);
2789 /*
2790 * NOTE: The pop-up menu can eat the mouse up event.
2791 * We deal with this in normal.c.
2792 */
2793}
2794#endif
2795
2796/*
2797 * Got a message when the system will go down.
2798 */
2799 static void
2800_OnEndSession(void)
2801{
2802 getout_preserve_modified(1);
2803}
2804
2805/*
2806 * Get this message when the user clicks on the cross in the top right corner
2807 * of a Windows95 window.
2808 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002809 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002810_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811{
2812 gui_shell_closed();
2813}
2814
2815/*
2816 * Get a message when the window is being destroyed.
2817 */
2818 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002819_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820{
2821 if (!destroying)
2822 _OnClose(hwnd);
2823}
2824
2825 static void
2826_OnPaint(
2827 HWND hwnd)
2828{
2829 if (!IsMinimized(hwnd))
2830 {
2831 PAINTSTRUCT ps;
2832
Bram Moolenaar734a8672019-12-02 22:49:38 +01002833 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002835
Bram Moolenaar734a8672019-12-02 22:49:38 +01002836 // prevent multi-byte characters from misprinting on an invalid
2837 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002838 if (has_mbyte)
2839 {
2840 RECT rect;
2841
2842 GetClientRect(hwnd, &rect);
2843 ps.rcPaint.left = rect.left;
2844 ps.rcPaint.right = rect.right;
2845 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002846
2847 if (!IsRectEmpty(&ps.rcPaint))
2848 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002849 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2850 ps.rcPaint.right - ps.rcPaint.left + 1,
2851 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2852 }
2853
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002854 EndPaint(hwnd, &ps);
2855 }
2856}
2857
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 static void
2859_OnSize(
2860 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002861 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862 int cx,
2863 int cy)
2864{
2865 if (!IsMinimized(hwnd))
2866 {
2867 gui_resize_shell(cx, cy);
2868
2869#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002870 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 gui_mswin_get_menu_height(TRUE);
2872#endif
2873 }
2874}
2875
2876 static void
2877_OnSetFocus(
2878 HWND hwnd,
2879 HWND hwndOldFocus)
2880{
2881 gui_focus_change(TRUE);
2882 s_getting_focus = TRUE;
2883 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2884}
2885
2886 static void
2887_OnKillFocus(
2888 HWND hwnd,
2889 HWND hwndNewFocus)
2890{
2891 gui_focus_change(FALSE);
2892 s_getting_focus = FALSE;
2893 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2894}
2895
2896/*
2897 * Get a message when the user switches back to vim
2898 */
2899 static LRESULT
2900_OnActivateApp(
2901 HWND hwnd,
2902 BOOL fActivate,
2903 DWORD dwThreadId)
2904{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002905 // we call gui_focus_change() in _OnSetFocus()
2906 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2908}
2909
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002910 void
2911gui_mch_destroy_scrollbar(scrollbar_T *sb)
2912{
2913 DestroyWindow(sb->id);
2914}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915
2916/*
2917 * Get current mouse coordinates in text window.
2918 */
2919 void
2920gui_mch_getmouse(int *x, int *y)
2921{
2922 RECT rct;
2923 POINT mp;
2924
2925 (void)GetWindowRect(s_textArea, &rct);
2926 (void)GetCursorPos((LPPOINT)&mp);
2927 *x = (int)(mp.x - rct.left);
2928 *y = (int)(mp.y - rct.top);
2929}
2930
2931/*
2932 * Move mouse pointer to character at (x, y).
2933 */
2934 void
2935gui_mch_setmouse(int x, int y)
2936{
2937 RECT rct;
2938
2939 (void)GetWindowRect(s_textArea, &rct);
2940 (void)SetCursorPos(x + gui.border_offset + rct.left,
2941 y + gui.border_offset + rct.top);
2942}
2943
2944 static void
2945gui_mswin_get_valid_dimensions(
2946 int w,
2947 int h,
2948 int *valid_w,
2949 int *valid_h)
2950{
2951 int base_width, base_height;
2952
2953 base_width = gui_get_base_width()
2954 + (GetSystemMetrics(SM_CXFRAME) +
2955 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2956 base_height = gui_get_base_height()
2957 + (GetSystemMetrics(SM_CYFRAME) +
2958 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2959 + GetSystemMetrics(SM_CYCAPTION)
2960#ifdef FEAT_MENU
2961 + gui_mswin_get_menu_height(FALSE)
2962#endif
2963 ;
2964 *valid_w = base_width +
2965 ((w - base_width) / gui.char_width) * gui.char_width;
2966 *valid_h = base_height +
2967 ((h - base_height) / gui.char_height) * gui.char_height;
2968}
2969
2970 void
2971gui_mch_flash(int msec)
2972{
2973 RECT rc;
2974
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002975#if defined(FEAT_DIRECTX)
2976 if (IS_ENABLE_DIRECTX())
2977 DWriteContext_Flush(s_dwc);
2978#endif
2979
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980 /*
2981 * Note: InvertRect() excludes right and bottom of rectangle.
2982 */
2983 rc.left = 0;
2984 rc.top = 0;
2985 rc.right = gui.num_cols * gui.char_width;
2986 rc.bottom = gui.num_rows * gui.char_height;
2987 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002988 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002989
Bram Moolenaar734a8672019-12-02 22:49:38 +01002990 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002991
2992 InvertRect(s_hdc, &rc);
2993}
2994
2995/*
2996 * Return flags used for scrolling.
2997 * The SW_INVALIDATE is required when part of the window is covered or
2998 * off-screen. Refer to MS KB Q75236.
2999 */
3000 static int
3001get_scroll_flags(void)
3002{
3003 HWND hwnd;
3004 RECT rcVim, rcOther, rcDest;
3005
3006 GetWindowRect(s_hwnd, &rcVim);
3007
Bram Moolenaar734a8672019-12-02 22:49:38 +01003008 // Check if the window is partly above or below the screen. We don't care
3009 // about partly left or right of the screen, it is not relevant when
3010 // scrolling up or down.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003011 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3012 return SW_INVALIDATE;
3013
Bram Moolenaar734a8672019-12-02 22:49:38 +01003014 // Check if there is an window (partly) on top of us.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3016 if (IsWindowVisible(hwnd))
3017 {
3018 GetWindowRect(hwnd, &rcOther);
3019 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3020 return SW_INVALIDATE;
3021 }
3022 return 0;
3023}
3024
3025/*
3026 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3027 * may not be scrolled out properly.
3028 * For gVim, when _OnScroll() is repeated, the character at the
3029 * previous cursor position may be left drawn after scroll.
3030 * The problem can be avoided by calling GetPixel() to get a pixel in
3031 * the region before ScrollWindowEx().
3032 */
3033 static void
3034intel_gpu_workaround(void)
3035{
3036 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3037}
3038
3039/*
3040 * Delete the given number of lines from the given row, scrolling up any
3041 * text further down within the scroll region.
3042 */
3043 void
3044gui_mch_delete_lines(
3045 int row,
3046 int num_lines)
3047{
3048 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003049
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003050 rc.left = FILL_X(gui.scroll_region_left);
3051 rc.right = FILL_X(gui.scroll_region_right + 1);
3052 rc.top = FILL_Y(row);
3053 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3054
Bram Moolenaar92467d32017-12-05 13:22:16 +01003055#if defined(FEAT_DIRECTX)
3056 if (IS_ENABLE_DIRECTX())
3057 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003058 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3059 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003060 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003061 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003062#endif
3063 {
3064 intel_gpu_workaround();
3065 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003067 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003068 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003069
Bram Moolenaar734a8672019-12-02 22:49:38 +01003070 // This seems to be required to avoid the cursor disappearing when
3071 // scrolling such that the cursor ends up in the top-left character on
3072 // the screen... But why? (Webb)
3073 // It's probably fixed by disabling drawing the cursor while scrolling.
3074 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075
3076 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3077 gui.scroll_region_left,
3078 gui.scroll_region_bot, gui.scroll_region_right);
3079}
3080
3081/*
3082 * Insert the given number of lines before the given row, scrolling down any
3083 * following text within the scroll region.
3084 */
3085 void
3086gui_mch_insert_lines(
3087 int row,
3088 int num_lines)
3089{
3090 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003091
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003092 rc.left = FILL_X(gui.scroll_region_left);
3093 rc.right = FILL_X(gui.scroll_region_right + 1);
3094 rc.top = FILL_Y(row);
3095 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003096
3097#if defined(FEAT_DIRECTX)
3098 if (IS_ENABLE_DIRECTX())
3099 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003100 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3101 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003102 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003103 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003104#endif
3105 {
3106 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003107 // The SW_INVALIDATE is required when part of the window is covered or
3108 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003109 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003110 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003111 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003112 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003114 gui_clear_block(row, gui.scroll_region_left,
3115 row + num_lines - 1, gui.scroll_region_right);
3116}
3117
3118
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003120gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003121{
3122#if defined(FEAT_DIRECTX)
3123 DWriteContext_Close(s_dwc);
3124 DWrite_Final();
3125 s_dwc = NULL;
3126#endif
3127
3128 ReleaseDC(s_textArea, s_hdc);
3129 DeleteObject(s_brush);
3130
3131#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003132 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3134#endif
3135
Bram Moolenaar734a8672019-12-02 22:49:38 +01003136 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003137 if (s_hwnd != NULL)
3138 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003139 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140 DestroyWindow(s_hwnd);
3141 }
3142
3143#ifdef GLOBAL_IME
3144 global_ime_end();
3145#endif
3146}
3147
3148 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003149logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150{
3151 char *p;
3152 char *res;
3153 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003154 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003155 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003156 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003157
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003158 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3159 if (font_name == NULL)
3160 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003162 quality_name = quality_id2name((int)lf.lfQuality);
3163
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003164 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003165 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003166 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003167 if (res != NULL)
3168 {
3169 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003170 // make a normal font string out of the lf thing:
3171 points = pixels_to_points(
3172 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3173 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3174 sprintf((char *)p, "%s:h%d", font_name, points);
3175 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003176 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 while (*p)
3178 {
3179 if (*p == ' ')
3180 *p = '_';
3181 ++p;
3182 }
3183 if (lf.lfItalic)
3184 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003185 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186 STRCAT(p, ":b");
3187 if (lf.lfUnderline)
3188 STRCAT(p, ":u");
3189 if (lf.lfStrikeOut)
3190 STRCAT(p, ":s");
3191 if (charset_name != NULL)
3192 {
3193 STRCAT(p, ":c");
3194 STRCAT(p, charset_name);
3195 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003196 if (quality_name != NULL)
3197 {
3198 STRCAT(p, ":q");
3199 STRCAT(p, quality_name);
3200 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201 }
3202
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204 return (char_u *)res;
3205}
3206
3207
3208#ifdef FEAT_MBYTE_IME
3209/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003210 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 * 'guifont'
3212 */
3213 static void
3214update_im_font(void)
3215{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003216 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003217
3218 if (p_guifontwide != NULL && *p_guifontwide != NUL
3219 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003220 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221 norm_logfont = lf_wide;
3222 else
3223 norm_logfont = sub_logfont;
3224 im_set_font(&norm_logfont);
3225}
3226#endif
3227
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228/*
3229 * Handler of gui.wide_font (p_guifontwide) changed notification.
3230 */
3231 void
3232gui_mch_wide_font_changed(void)
3233{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003234 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003236#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003237 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003238#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239
3240 gui_mch_free_font(gui.wide_ital_font);
3241 gui.wide_ital_font = NOFONT;
3242 gui_mch_free_font(gui.wide_bold_font);
3243 gui.wide_bold_font = NOFONT;
3244 gui_mch_free_font(gui.wide_boldital_font);
3245 gui.wide_boldital_font = NOFONT;
3246
3247 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003248 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 {
3250 if (!lf.lfItalic)
3251 {
3252 lf.lfItalic = TRUE;
3253 gui.wide_ital_font = get_font_handle(&lf);
3254 lf.lfItalic = FALSE;
3255 }
3256 if (lf.lfWeight < FW_BOLD)
3257 {
3258 lf.lfWeight = FW_BOLD;
3259 gui.wide_bold_font = get_font_handle(&lf);
3260 if (!lf.lfItalic)
3261 {
3262 lf.lfItalic = TRUE;
3263 gui.wide_boldital_font = get_font_handle(&lf);
3264 }
3265 }
3266 }
3267}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268
3269/*
3270 * Initialise vim to use the font with the given name.
3271 * Return FAIL if the font could not be loaded, OK otherwise.
3272 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003274gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003276 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003277 GuiFont font = NOFONT;
3278 char_u *p;
3279
Bram Moolenaar734a8672019-12-02 22:49:38 +01003280 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003281 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3282 font = get_font_handle(&lf);
3283 if (font == NOFONT)
3284 return FAIL;
3285
3286 if (font_name == NULL)
3287 font_name = (char_u *)lf.lfFaceName;
3288#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3289 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003290#endif
3291#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003292 sub_logfont = lf;
3293#endif
3294#ifdef FEAT_MBYTE_IME
3295 update_im_font();
3296#endif
3297 gui_mch_free_font(gui.norm_font);
3298 gui.norm_font = font;
3299 current_font_height = lf.lfHeight;
3300 GetFontSize(font);
3301
3302 p = logfont2name(lf);
3303 if (p != NULL)
3304 {
3305 hl_set_font_name(p);
3306
Bram Moolenaar734a8672019-12-02 22:49:38 +01003307 // When setting 'guifont' to "*" replace it with the actual font name.
3308 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3310 {
3311 vim_free(p_guifont);
3312 p_guifont = p;
3313 }
3314 else
3315 vim_free(p);
3316 }
3317
3318 gui_mch_free_font(gui.ital_font);
3319 gui.ital_font = NOFONT;
3320 gui_mch_free_font(gui.bold_font);
3321 gui.bold_font = NOFONT;
3322 gui_mch_free_font(gui.boldital_font);
3323 gui.boldital_font = NOFONT;
3324
3325 if (!lf.lfItalic)
3326 {
3327 lf.lfItalic = TRUE;
3328 gui.ital_font = get_font_handle(&lf);
3329 lf.lfItalic = FALSE;
3330 }
3331 if (lf.lfWeight < FW_BOLD)
3332 {
3333 lf.lfWeight = FW_BOLD;
3334 gui.bold_font = get_font_handle(&lf);
3335 if (!lf.lfItalic)
3336 {
3337 lf.lfItalic = TRUE;
3338 gui.boldital_font = get_font_handle(&lf);
3339 }
3340 }
3341
3342 return OK;
3343}
3344
3345#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003346# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347#endif
3348
3349/*
3350 * Return TRUE if the GUI window is maximized, filling the whole screen.
3351 */
3352 int
3353gui_mch_maximized(void)
3354{
3355 WINDOWPLACEMENT wp;
3356
3357 wp.length = sizeof(WINDOWPLACEMENT);
3358 if (GetWindowPlacement(s_hwnd, &wp))
3359 return wp.showCmd == SW_SHOWMAXIMIZED
3360 || (wp.showCmd == SW_SHOWMINIMIZED
3361 && wp.flags == WPF_RESTORETOMAXIMIZED);
3362
3363 return 0;
3364}
3365
3366/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003367 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3368 * is set. Compute the new Rows and Columns. This is like resizing the
3369 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003370 */
3371 void
3372gui_mch_newfont(void)
3373{
3374 RECT rect;
3375
3376 GetWindowRect(s_hwnd, &rect);
3377 if (win_socket_id == 0)
3378 {
3379 gui_resize_shell(rect.right - rect.left
3380 - (GetSystemMetrics(SM_CXFRAME) +
3381 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3382 rect.bottom - rect.top
3383 - (GetSystemMetrics(SM_CYFRAME) +
3384 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3385 - GetSystemMetrics(SM_CYCAPTION)
3386#ifdef FEAT_MENU
3387 - gui_mswin_get_menu_height(FALSE)
3388#endif
3389 );
3390 }
3391 else
3392 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003393 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 gui_resize_shell(rect.right - rect.left,
3395 rect.bottom - rect.top
3396#ifdef FEAT_MENU
3397 - gui_mswin_get_menu_height(FALSE)
3398#endif
3399 );
3400 }
3401}
3402
3403/*
3404 * Set the window title
3405 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406 void
3407gui_mch_settitle(
3408 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003409 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003410{
3411 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3412}
3413
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003414#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003415// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3416// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417static LPCSTR mshape_idcs[] =
3418{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003419 IDC_ARROW, // arrow
3420 MAKEINTRESOURCE(0), // blank
3421 IDC_IBEAM, // beam
3422 IDC_SIZENS, // updown
3423 IDC_SIZENS, // udsizing
3424 IDC_SIZEWE, // leftright
3425 IDC_SIZEWE, // lrsizing
3426 IDC_WAIT, // busy
3427 IDC_NO, // no
3428 IDC_ARROW, // crosshair
3429 IDC_ARROW, // hand1
3430 IDC_ARROW, // hand2
3431 IDC_ARROW, // pencil
3432 IDC_ARROW, // question
3433 IDC_ARROW, // right-arrow
3434 IDC_UPARROW, // up-arrow
3435 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436};
3437
3438 void
3439mch_set_mouse_shape(int shape)
3440{
3441 LPCSTR idc;
3442
3443 if (shape == MSHAPE_HIDE)
3444 ShowCursor(FALSE);
3445 else
3446 {
3447 if (shape >= MSHAPE_NUMBERED)
3448 idc = IDC_ARROW;
3449 else
3450 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003451 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003452 if (!p_mh)
3453 {
3454 POINT mp;
3455
Bram Moolenaar734a8672019-12-02 22:49:38 +01003456 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457 (void)GetCursorPos((LPPOINT)&mp);
3458 (void)SetCursorPos(mp.x, mp.y);
3459 ShowCursor(TRUE);
3460 }
3461 }
3462}
3463#endif
3464
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003465#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467 * Wide version of convert_filter().
3468 */
3469 static WCHAR *
3470convert_filterW(char_u *s)
3471{
3472 char_u *tmp;
3473 int len;
3474 WCHAR *res;
3475
3476 tmp = convert_filter(s);
3477 if (tmp == NULL)
3478 return NULL;
3479 len = (int)STRLEN(s) + 3;
3480 res = enc_to_utf16(tmp, &len);
3481 vim_free(tmp);
3482 return res;
3483}
3484
3485/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003486 * Pop open a file browser and return the file selected, in allocated memory,
3487 * or NULL if Cancel is hit.
3488 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3489 * title - Title message for the file browser dialog.
3490 * dflt - Default name of file.
3491 * ext - Default extension to be added to files without extensions.
3492 * initdir - directory in which to open the browser (NULL = current dir)
3493 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003494 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003495 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003496gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497 int saving,
3498 char_u *title,
3499 char_u *dflt,
3500 char_u *ext,
3501 char_u *initdir,
3502 char_u *filter)
3503{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003504 // We always use the wide function. This means enc_to_utf16() must work,
3505 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 OPENFILENAMEW fileStruct;
3507 WCHAR fileBuf[MAXPATHL];
3508 WCHAR *wp;
3509 int i;
3510 WCHAR *titlep = NULL;
3511 WCHAR *extp = NULL;
3512 WCHAR *initdirp = NULL;
3513 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003514 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515
3516 if (dflt == NULL)
3517 fileBuf[0] = NUL;
3518 else
3519 {
3520 wp = enc_to_utf16(dflt, NULL);
3521 if (wp == NULL)
3522 fileBuf[0] = NUL;
3523 else
3524 {
3525 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3526 fileBuf[i] = wp[i];
3527 fileBuf[i] = NUL;
3528 vim_free(wp);
3529 }
3530 }
3531
Bram Moolenaar734a8672019-12-02 22:49:38 +01003532 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003533 filterp = convert_filterW(filter);
3534
Bram Moolenaara80faa82020-04-12 19:37:17 +02003535 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003536# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003537 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003538 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003539# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003541# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542
3543 if (title != NULL)
3544 titlep = enc_to_utf16(title, NULL);
3545 fileStruct.lpstrTitle = titlep;
3546
3547 if (ext != NULL)
3548 extp = enc_to_utf16(ext, NULL);
3549 fileStruct.lpstrDefExt = extp;
3550
3551 fileStruct.lpstrFile = fileBuf;
3552 fileStruct.nMaxFile = MAXPATHL;
3553 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003554 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3555 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556 if (initdir != NULL && *initdir != NUL)
3557 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003558 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559 initdirp = enc_to_utf16(initdir, NULL);
3560 if (initdirp != NULL)
3561 {
3562 for (wp = initdirp; *wp != NUL; ++wp)
3563 if (*wp == '/')
3564 *wp = '\\';
3565 }
3566 fileStruct.lpstrInitialDir = initdirp;
3567 }
3568
3569 /*
3570 * TODO: Allow selection of multiple files. Needs another arg to this
3571 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3572 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3573 * files that don't exist yet, so I haven't put it in. What about
3574 * OFN_PATHMUSTEXIST?
3575 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3576 */
3577 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003578# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579 if (curbuf->b_p_bin)
3580 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003581# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003582 if (saving)
3583 {
3584 if (!GetSaveFileNameW(&fileStruct))
3585 return NULL;
3586 }
3587 else
3588 {
3589 if (!GetOpenFileNameW(&fileStruct))
3590 return NULL;
3591 }
3592
3593 vim_free(filterp);
3594 vim_free(initdirp);
3595 vim_free(titlep);
3596 vim_free(extp);
3597
Bram Moolenaar734a8672019-12-02 22:49:38 +01003598 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003600 if (p == NULL)
3601 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003602
Bram Moolenaar734a8672019-12-02 22:49:38 +01003603 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604 SetFocus(s_hwnd);
3605
Bram Moolenaar734a8672019-12-02 22:49:38 +01003606 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003607 q = vim_strsave(shorten_fname1(p));
3608 vim_free(p);
3609 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611
3612
3613/*
3614 * Convert the string s to the proper format for a filter string by replacing
3615 * the \t and \n delimiters with \0.
3616 * Returns the converted string in allocated memory.
3617 *
3618 * Keep in sync with convert_filterW() above!
3619 */
3620 static char_u *
3621convert_filter(char_u *s)
3622{
3623 char_u *res;
3624 unsigned s_len = (unsigned)STRLEN(s);
3625 unsigned i;
3626
3627 res = alloc(s_len + 3);
3628 if (res != NULL)
3629 {
3630 for (i = 0; i < s_len; ++i)
3631 if (s[i] == '\t' || s[i] == '\n')
3632 res[i] = '\0';
3633 else
3634 res[i] = s[i];
3635 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003636 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003637 res[s_len + 1] = NUL;
3638 res[s_len + 2] = NUL;
3639 }
3640 return res;
3641}
3642
3643/*
3644 * Select a directory.
3645 */
3646 char_u *
3647gui_mch_browsedir(char_u *title, char_u *initdir)
3648{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003649 // We fake this: Use a filter that doesn't select anything and a default
3650 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3652 initdir, (char_u *)_("Directory\t*.nothing\n"));
3653}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003654#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656 static void
3657_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003658 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 HDROP hDrop)
3660{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003661#define BUFPATHLEN _MAX_PATH
3662#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003663 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664 char szFile[BUFPATHLEN];
3665 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3666 UINT i;
3667 char_u **fnames;
3668 POINT pt;
3669 int_u modifiers = 0;
3670
Bram Moolenaar734a8672019-12-02 22:49:38 +01003671 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672
Bram Moolenaar734a8672019-12-02 22:49:38 +01003673 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 DragQueryPoint(hDrop, &pt);
3675 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3676
3677 reset_VIsual();
3678
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003679 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680
3681 if (fnames != NULL)
3682 for (i = 0; i < cFiles; ++i)
3683 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003684 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3685 fnames[i] = utf16_to_enc(wszFile, NULL);
3686 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003687 {
3688 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3689 fnames[i] = vim_strsave((char_u *)szFile);
3690 }
3691 }
3692
3693 DragFinish(hDrop);
3694
3695 if (fnames != NULL)
3696 {
3697 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3698 modifiers |= MOUSE_SHIFT;
3699 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3700 modifiers |= MOUSE_CTRL;
3701 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3702 modifiers |= MOUSE_ALT;
3703
3704 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3705
3706 s_need_activate = TRUE;
3707 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708}
3709
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710 static int
3711_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003712 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003713 HWND hwndCtl,
3714 UINT code,
3715 int pos)
3716{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003717 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718 scrollbar_T *sb, *sb_info;
3719 long val;
3720 int dragging = FALSE;
3721 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003722 SCROLLINFO si;
3723
3724 si.cbSize = sizeof(si);
3725 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726
3727 sb = gui_mswin_find_scrollbar(hwndCtl);
3728 if (sb == NULL)
3729 return 0;
3730
Bram Moolenaar734a8672019-12-02 22:49:38 +01003731 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 {
3733 /*
3734 * Careful: need to get scrollbar info out of first (left) scrollbar
3735 * for window, but keep real scrollbar too because we must pass it to
3736 * gui_drag_scrollbar().
3737 */
3738 sb_info = &sb->wp->w_scrollbars[0];
3739 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003740 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741 sb_info = sb;
3742 val = sb_info->value;
3743
3744 switch (code)
3745 {
3746 case SB_THUMBTRACK:
3747 val = pos;
3748 dragging = TRUE;
3749 if (sb->scroll_shift > 0)
3750 val <<= sb->scroll_shift;
3751 break;
3752 case SB_LINEDOWN:
3753 val++;
3754 break;
3755 case SB_LINEUP:
3756 val--;
3757 break;
3758 case SB_PAGEDOWN:
3759 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3760 break;
3761 case SB_PAGEUP:
3762 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3763 break;
3764 case SB_TOP:
3765 val = 0;
3766 break;
3767 case SB_BOTTOM:
3768 val = sb_info->max;
3769 break;
3770 case SB_ENDSCROLL:
3771 if (prev_code == SB_THUMBTRACK)
3772 {
3773 /*
3774 * "pos" only gives us 16-bit data. In case of large file,
3775 * use GetScrollPos() which returns 32-bit. Unfortunately it
3776 * is not valid while the scrollbar is being dragged.
3777 */
3778 val = GetScrollPos(hwndCtl, SB_CTL);
3779 if (sb->scroll_shift > 0)
3780 val <<= sb->scroll_shift;
3781 }
3782 break;
3783
3784 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003785 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 return 0;
3787 }
3788 prev_code = code;
3789
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3791 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003792
3793 /*
3794 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3795 */
3796 if (sb->wp != NULL)
3797 {
3798 scrollbar_T *sba = sb->wp->w_scrollbars;
3799 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3800
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 }
3803
Bram Moolenaar734a8672019-12-02 22:49:38 +01003804 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 s_busy_processing = TRUE;
3806
Bram Moolenaar734a8672019-12-02 22:49:38 +01003807 // When "allow_scrollbar" is FALSE still need to remember the new
3808 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 dont_scroll = !allow_scrollbar;
3810
Bram Moolenaara338adc2018-01-31 20:51:47 +01003811 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003813 mch_enable_flush();
3814 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815
3816 s_busy_processing = FALSE;
3817 dont_scroll = dont_scroll_save;
3818
3819 return 0;
3820}
3821
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823#ifdef FEAT_XPM_W32
3824# include "xpm_w32.h"
3825#endif
3826
3827#ifdef PROTO
3828# define WINAPI
3829#endif
3830
3831#ifdef __MINGW32__
3832/*
3833 * Add a lot of missing defines.
3834 * They are not always missing, we need the #ifndef's.
3835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836# ifndef IsMinimized
3837# define IsMinimized(hwnd) IsIconic(hwnd)
3838# endif
3839# ifndef IsMaximized
3840# define IsMaximized(hwnd) IsZoomed(hwnd)
3841# endif
3842# ifndef SelectFont
3843# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3844# endif
3845# ifndef GetStockBrush
3846# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3847# endif
3848# ifndef DeleteBrush
3849# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3850# endif
3851
3852# ifndef HANDLE_WM_RBUTTONDBLCLK
3853# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3854 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3855# endif
3856# ifndef HANDLE_WM_MBUTTONUP
3857# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3858 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3859# endif
3860# ifndef HANDLE_WM_MBUTTONDBLCLK
3861# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3862 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3863# endif
3864# ifndef HANDLE_WM_LBUTTONDBLCLK
3865# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3866 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3867# endif
3868# ifndef HANDLE_WM_RBUTTONDOWN
3869# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3870 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3871# endif
3872# ifndef HANDLE_WM_MOUSEMOVE
3873# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3874 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3875# endif
3876# ifndef HANDLE_WM_RBUTTONUP
3877# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3878 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3879# endif
3880# ifndef HANDLE_WM_MBUTTONDOWN
3881# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3882 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3883# endif
3884# ifndef HANDLE_WM_LBUTTONUP
3885# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3886 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3887# endif
3888# ifndef HANDLE_WM_LBUTTONDOWN
3889# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3890 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3891# endif
3892# ifndef HANDLE_WM_SYSCHAR
3893# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3894 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3895# endif
3896# ifndef HANDLE_WM_ACTIVATEAPP
3897# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3898 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3899# endif
3900# ifndef HANDLE_WM_WINDOWPOSCHANGING
3901# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3902 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3903# endif
3904# ifndef HANDLE_WM_VSCROLL
3905# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3906 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3907# endif
3908# ifndef HANDLE_WM_SETFOCUS
3909# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3910 ((fn)((hwnd), (HWND)(wParam)), 0L)
3911# endif
3912# ifndef HANDLE_WM_KILLFOCUS
3913# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3914 ((fn)((hwnd), (HWND)(wParam)), 0L)
3915# endif
3916# ifndef HANDLE_WM_HSCROLL
3917# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3918 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3919# endif
3920# ifndef HANDLE_WM_DROPFILES
3921# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3922 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3923# endif
3924# ifndef HANDLE_WM_CHAR
3925# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3926 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3927# endif
3928# ifndef HANDLE_WM_SYSDEADCHAR
3929# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3930 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3931# endif
3932# ifndef HANDLE_WM_DEADCHAR
3933# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3934 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3935# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003936#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
3938
Bram Moolenaar734a8672019-12-02 22:49:38 +01003939// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#define TEAROFF_PADDING_X 2
3941#define TEAROFF_BUTTON_PAD_X 8
3942#define TEAROFF_MIN_WIDTH 200
3943#define TEAROFF_SUBMENU_LABEL ">>"
3944#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3945
3946
Bram Moolenaar734a8672019-12-02 22:49:38 +01003947// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003949# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950#endif
3951
3952
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003953#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954# define ID_BEVAL_TOOLTIP 200
3955# define BEVAL_TEXT_LEN MAXPATHL
3956
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003957# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003958// Work around old versions of basetsd.h which wrongly declares
3959// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003960# undef UINT_PTR
3961# define UINT_PTR UINT
3962# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003965static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003967
Bram Moolenaar82881492012-11-20 16:53:39 +01003968
Bram Moolenaar734a8672019-12-02 22:49:38 +01003969// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003970# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01003971
Bram Moolenaar45360022005-07-21 21:08:21 +00003972/*
3973 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003974 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003975 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003976# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01003977
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003978# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003979
3980typedef struct _DllVersionInfo
3981{
3982 DWORD cbSize;
3983 DWORD dwMajorVersion;
3984 DWORD dwMinorVersion;
3985 DWORD dwBuildNumber;
3986 DWORD dwPlatformID;
3987} DLLVERSIONINFO;
3988
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003989# ifndef PROTO
3990# include <poppack.h>
3991# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003992
Bram Moolenaar45360022005-07-21 21:08:21 +00003993typedef struct tagTOOLINFOA_NEW
3994{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003995 UINT cbSize;
3996 UINT uFlags;
3997 HWND hwnd;
3998 UINT_PTR uId;
3999 RECT rect;
4000 HINSTANCE hinst;
4001 LPSTR lpszText;
4002 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004003} TOOLINFO_NEW;
4004
4005typedef struct tagNMTTDISPINFO_NEW
4006{
4007 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004008 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004009 char szText[80];
4010 HINSTANCE hinst;
4011 UINT uFlags;
4012 LPARAM lParam;
4013} NMTTDISPINFO_NEW;
4014
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004015typedef struct tagTOOLINFOW_NEW
4016{
4017 UINT cbSize;
4018 UINT uFlags;
4019 HWND hwnd;
4020 UINT_PTR uId;
4021 RECT rect;
4022 HINSTANCE hinst;
4023 LPWSTR lpszText;
4024 LPARAM lParam;
4025 void *lpReserved;
4026} TOOLINFOW_NEW;
4027
4028typedef struct tagNMTTDISPINFOW_NEW
4029{
4030 NMHDR hdr;
4031 LPWSTR lpszText;
4032 WCHAR szText[80];
4033 HINSTANCE hinst;
4034 UINT uFlags;
4035 LPARAM lParam;
4036} NMTTDISPINFOW_NEW;
4037
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004038
Bram Moolenaar45360022005-07-21 21:08:21 +00004039typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004040# ifndef TTM_SETMAXTIPWIDTH
4041# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004044# ifndef TTF_DI_SETITEM
4045# define TTF_DI_SETITEM 0x8000
4046# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004047
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004048# ifndef TTN_GETDISPINFO
4049# define TTN_GETDISPINFO (TTN_FIRST - 0)
4050# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004051
Bram Moolenaar734a8672019-12-02 22:49:38 +01004052#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004053
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004054#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004055// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4056// it here if LPNMTTDISPINFO isn't defined.
4057// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4058// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004059# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4060typedef struct tagNMTTDISPINFOA {
4061 NMHDR hdr;
4062 LPSTR lpszText;
4063 char szText[80];
4064 HINSTANCE hinst;
4065 UINT uFlags;
4066 LPARAM lParam;
4067} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4068# define LPNMTTDISPINFO LPNMTTDISPINFOA
4069
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004070typedef struct tagNMTTDISPINFOW {
4071 NMHDR hdr;
4072 LPWSTR lpszText;
4073 WCHAR szText[80];
4074 HINSTANCE hinst;
4075 UINT uFlags;
4076 LPARAM lParam;
4077} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004078# endif
4079#endif
4080
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004081#ifndef TTN_GETDISPINFOW
4082# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4083#endif
4084
Bram Moolenaar734a8672019-12-02 22:49:38 +01004085// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087#ifdef FEAT_MENU
4088static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091/*
4092 * Use the system font for dialogs and tear-off menus. Remove this line to
4093 * use DLG_FONT_NAME.
4094 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004095#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
4097#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098#define VIM_CLASSW L"Vim"
4099
Bram Moolenaar734a8672019-12-02 22:49:38 +01004100// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4101// thus there should be room for every dialog. For tearoffs it's made bigger
4102// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103#define DLG_ALLOC_SIZE 16 * 1024
4104
4105/*
4106 * stuff for dialogs, menus, tearoffs etc.
4107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108static PWORD
4109add_dialog_element(
4110 PWORD p,
4111 DWORD lStyle,
4112 WORD x,
4113 WORD y,
4114 WORD w,
4115 WORD h,
4116 WORD Id,
4117 WORD clss,
4118 const char *caption);
4119static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004120static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004121#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124static void get_dialog_font_metrics(void);
4125
4126static int dialog_default_button = -1;
4127
Bram Moolenaar734a8672019-12-02 22:49:38 +01004128// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
Bram Moolenaar734a8672019-12-02 22:49:38 +01004131static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#ifdef FEAT_TOOLBAR
4133static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004134static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135static int get_toolbar_bitmap(vimmenu_T *menu);
4136#endif
4137
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004138#ifdef FEAT_GUI_TABLINE
4139static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004140static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004141#endif
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#ifdef FEAT_MBYTE_IME
4144static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4145static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4146#endif
4147#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4148# ifdef NOIME
4149typedef struct tagCOMPOSITIONFORM {
4150 DWORD dwStyle;
4151 POINT ptCurrentPos;
4152 RECT rcArea;
4153} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4154typedef HANDLE HIMC;
4155# endif
4156
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004157static HINSTANCE hLibImm = NULL;
4158static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4159static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4160static HIMC (WINAPI *pImmGetContext)(HWND);
4161static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4162static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4163static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4164static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004165static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4166static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004167static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4168static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004169static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170static void dyn_imm_load(void);
4171#else
4172# define pImmGetCompositionStringA ImmGetCompositionStringA
4173# define pImmGetCompositionStringW ImmGetCompositionStringW
4174# define pImmGetContext ImmGetContext
4175# define pImmAssociateContext ImmAssociateContext
4176# define pImmReleaseContext ImmReleaseContext
4177# define pImmGetOpenStatus ImmGetOpenStatus
4178# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004179# define pImmGetCompositionFontW ImmGetCompositionFontW
4180# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181# define pImmSetCompositionWindow ImmSetCompositionWindow
4182# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004183# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#endif
4185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#ifdef FEAT_MENU
4187/*
4188 * Figure out how high the menu bar is at the moment.
4189 */
4190 static int
4191gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004192 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193{
4194 static int old_menu_height = -1;
4195
4196 RECT rc1, rc2;
4197 int num;
4198 int menu_height;
4199
4200 if (gui.menu_is_active)
4201 num = GetMenuItemCount(s_menuBar);
4202 else
4203 num = 0;
4204
4205 if (num == 0)
4206 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004207 else if (IsMinimized(s_hwnd))
4208 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004209 // The height of the menu cannot be determined while the window is
4210 // minimized. Take the previous height if the menu is changed in that
4211 // state, to avoid that Vim's vertical window size accidentally
4212 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004213 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 else
4216 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004217 /*
4218 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4219 * seem to have been set yet, so menu wraps in default window
4220 * width which is very narrow. Instead just return height of a
4221 * single menu item. Will still be wrong when the menu really
4222 * should wrap over more than one line.
4223 */
4224 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4225 if (gui.starting)
4226 menu_height = rc1.bottom - rc1.top + 1;
4227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004229 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4230 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232 }
4233
4234 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004235 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004236 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 return menu_height;
4239}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004240#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242
4243/*
4244 * Setup for the Intellimouse
4245 */
4246 static void
4247init_mouse_wheel(void)
4248{
4249
4250#ifndef SPI_GETWHEELSCROLLLINES
4251# define SPI_GETWHEELSCROLLLINES 104
4252#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004253#ifndef SPI_SETWHEELSCROLLLINES
4254# define SPI_SETWHEELSCROLLLINES 105
4255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
Bram Moolenaar734a8672019-12-02 22:49:38 +01004257#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4258#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4260#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4261
Bram Moolenaar734a8672019-12-02 22:49:38 +01004262 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaar734a8672019-12-02 22:49:38 +01004264 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004265 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4266 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267}
4268
4269
Bram Moolenaarae20f342019-11-05 21:09:23 +01004270/*
4271 * Intellimouse wheel handler.
4272 * Treat a mouse wheel event as if it were a scroll request.
4273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 static void
4275_OnMouseWheel(
4276 HWND hwnd,
4277 short zDelta)
4278{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 int i;
4280 int size;
4281 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004282 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (mouse_scroll_lines == 0)
4285 init_mouse_wheel();
4286
Bram Moolenaarae20f342019-11-05 21:09:23 +01004287 wp = gui_mouse_window(FIND_POPUP);
4288
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004289#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004290 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004291 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004292 cmdarg_T cap;
4293 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004294
Bram Moolenaarae20f342019-11-05 21:09:23 +01004295 // Mouse hovers over popup window, scroll it if possible.
4296 mouse_row = wp->w_winrow;
4297 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004298 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004299 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4300 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4301 clear_oparg(&oa);
4302 cap.oap = &oa;
4303 nv_mousescroll(&cap);
4304 update_screen(0);
4305 setcursor();
4306 out_flush();
4307 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004308 }
4309#endif
4310
Bram Moolenaarae20f342019-11-05 21:09:23 +01004311 if (wp == NULL || !p_scf)
4312 wp = curwin;
4313
4314 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4315 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4316 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4317 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4318 else
4319 return;
4320 size = wp->w_height;
4321
Bram Moolenaara338adc2018-01-31 20:51:47 +01004322 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (mouse_scroll_lines > 0
4324 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4325 {
4326 for (i = mouse_scroll_lines; i > 0; --i)
4327 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4328 }
4329 else
4330 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004331 mch_enable_flush();
4332 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333}
4334
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004335#ifdef USE_SYSMENU_FONT
4336/*
4337 * Get Menu Font.
4338 * Return OK or FAIL.
4339 */
4340 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004341gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004342{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004343 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004344
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004345 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4346 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004347 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004348 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004349 &nm,
4350 0))
4351 return FAIL;
4352 *lf = nm.lfMenuFont;
4353 return OK;
4354}
4355#endif
4356
4357
4358#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4359/*
4360 * Set the GUI tabline font to the system menu font
4361 */
4362 static void
4363set_tabline_font(void)
4364{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004365 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004366 HFONT font;
4367 HWND hwnd;
4368 HDC hdc;
4369 HFONT hfntOld;
4370 TEXTMETRIC tm;
4371
4372 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4373 return;
4374
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004375 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004376
4377 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4378
4379 /*
4380 * Compute the height of the font used for the tab text
4381 */
4382 hwnd = GetDesktopWindow();
4383 hdc = GetWindowDC(hwnd);
4384 hfntOld = SelectFont(hdc, font);
4385
4386 GetTextMetrics(hdc, &tm);
4387
4388 SelectFont(hdc, hfntOld);
4389 ReleaseDC(hwnd, hdc);
4390
4391 /*
4392 * The space used by the tab border and the space between the tab label
4393 * and the tab border is included as 7.
4394 */
4395 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4396}
4397#endif
4398
Bram Moolenaar520470a2005-06-16 21:59:56 +00004399/*
4400 * Invoked when a setting was changed.
4401 */
4402 static LRESULT CALLBACK
4403_OnSettingChange(UINT n)
4404{
4405 if (n == SPI_SETWHEELSCROLLLINES)
4406 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4407 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004408#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4409 if (n == SPI_SETNONCLIENTMETRICS)
4410 set_tabline_font();
4411#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004412 return 0;
4413}
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#ifdef FEAT_NETBEANS_INTG
4416 static void
4417_OnWindowPosChanged(
4418 HWND hwnd,
4419 const LPWINDOWPOS lpwpos)
4420{
4421 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004422 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4425 || lpwpos->cx != cx || lpwpos->cy != cy))
4426 {
4427 x = lpwpos->x;
4428 y = lpwpos->y;
4429 cx = lpwpos->cx;
4430 cy = lpwpos->cy;
4431 netbeans_frame_moved(x, y);
4432 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004433 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4435}
4436#endif
4437
4438 static int
4439_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 UINT fwSide,
4441 LPRECT lprc)
4442{
4443 int w, h;
4444 int valid_w, valid_h;
4445 int w_offset, h_offset;
4446
4447 w = lprc->right - lprc->left;
4448 h = lprc->bottom - lprc->top;
4449 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4450 w_offset = w - valid_w;
4451 h_offset = h - valid_h;
4452
4453 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4454 || fwSide == WMSZ_BOTTOMLEFT)
4455 lprc->left += w_offset;
4456 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4457 || fwSide == WMSZ_BOTTOMRIGHT)
4458 lprc->right -= w_offset;
4459
4460 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4461 || fwSide == WMSZ_TOPRIGHT)
4462 lprc->top += h_offset;
4463 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4464 || fwSide == WMSZ_BOTTOMRIGHT)
4465 lprc->bottom -= h_offset;
4466 return TRUE;
4467}
4468
4469
4470
4471 static LRESULT CALLBACK
4472_WndProc(
4473 HWND hwnd,
4474 UINT uMsg,
4475 WPARAM wParam,
4476 LPARAM lParam)
4477{
4478 /*
4479 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4480 hwnd, uMsg, wParam, lParam);
4481 */
4482
4483 HandleMouseHide(uMsg, lParam);
4484
4485 s_uMsg = uMsg;
4486 s_wParam = wParam;
4487 s_lParam = lParam;
4488
4489 switch (uMsg)
4490 {
4491 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4492 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004493 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004495 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4497 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4498 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4499 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4500#ifdef FEAT_MENU
4501 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4502#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004503 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4504 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4506 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004507 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4508 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4510 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4511 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4512#ifdef FEAT_NETBEANS_INTG
4513 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4514#endif
4515
Bram Moolenaarafa24992006-03-27 20:58:26 +00004516#ifdef FEAT_GUI_TABLINE
4517 case WM_RBUTTONUP:
4518 {
4519 if (gui_mch_showing_tabline())
4520 {
4521 POINT pt;
4522 RECT rect;
4523
4524 /*
4525 * If the cursor is on the tabline, display the tab menu
4526 */
4527 GetCursorPos((LPPOINT)&pt);
4528 GetWindowRect(s_textArea, &rect);
4529 if (pt.y < rect.top)
4530 {
4531 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004532 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004533 }
4534 }
4535 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4536 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004537 case WM_LBUTTONDBLCLK:
4538 {
4539 /*
4540 * If the user double clicked the tabline, create a new tab
4541 */
4542 if (gui_mch_showing_tabline())
4543 {
4544 POINT pt;
4545 RECT rect;
4546
4547 GetCursorPos((LPPOINT)&pt);
4548 GetWindowRect(s_textArea, &rect);
4549 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004550 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004551 }
4552 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4553 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004554#endif
4555
Bram Moolenaar734a8672019-12-02 22:49:38 +01004556 case WM_QUERYENDSESSION: // System wants to go down.
4557 gui_shell_closed(); // Will exit when no changed buffers.
4558 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004561 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004564 return 0L;
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
4568 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004569 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4570 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004571 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 return 0L;
4573
4574 case WM_SYSCHAR:
4575 /*
4576 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4577 * shortcut key, handle like a typed ALT key, otherwise call Windows
4578 * ALT key handling.
4579 */
4580#ifdef FEAT_MENU
4581 if ( !gui.menu_is_active
4582 || p_wak[0] == 'n'
4583 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4584 )
4585#endif
4586 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004587 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 return 0L;
4589 }
4590#ifdef FEAT_MENU
4591 else
4592 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4593#endif
4594
4595 case WM_SYSKEYUP:
4596#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004597 // This used to be done only when menu is active: ALT key is used for
4598 // that. But that caused problems when menu is disabled and using
4599 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4600 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4602#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004603 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#endif
4605
Bram Moolenaar734a8672019-12-02 22:49:38 +01004606 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004607 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609 case WM_MOUSEWHEEL:
4610 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004611 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaar734a8672019-12-02 22:49:38 +01004613 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004614 case WM_SETTINGCHANGE:
4615 return _OnSettingChange((UINT)wParam);
4616
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004617#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 case WM_NOTIFY:
4619 switch (((LPNMHDR) lParam)->code)
4620 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004621 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004622 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004624 LPNMHDR hdr = (LPNMHDR)lParam;
4625 char_u *str = NULL;
4626 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004627
Bram Moolenaard23a8232018-02-10 18:45:26 +01004628 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004629
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004630# ifdef FEAT_GUI_TABLINE
4631 if (gui_mch_showing_tabline()
4632 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004633 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004634 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004635 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004636 * Mouse is over the GUI tabline. Display the
4637 * tooltip for the tab under the cursor
4638 *
4639 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004640 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004641 GetCursorPos(&pt);
4642 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004643 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004644 TCHITTESTINFO htinfo;
4645 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004646
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004647 /*
4648 * Get the tab under the cursor
4649 */
4650 htinfo.pt.x = pt.x;
4651 htinfo.pt.y = pt.y;
4652 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4653 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004654 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004655 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004656
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004657 tp = find_tabpage(idx + 1);
4658 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004659 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004660 get_tabline_label(tp, TRUE);
4661 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004662 }
4663 }
4664 }
4665 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004666# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004667# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004668# ifdef FEAT_GUI_TABLINE
4669 else
4670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004672 UINT idButton;
4673 vimmenu_T *pMenu;
4674
4675 idButton = (UINT) hdr->idFrom;
4676 pMenu = gui_mswin_find_menu(root_menu, idButton);
4677 if (pMenu)
4678 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004680# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004681 if (str != NULL)
4682 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004683 if (hdr->code == TTN_GETDISPINFOW)
4684 {
4685 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4686
Bram Moolenaar734a8672019-12-02 22:49:38 +01004687 // Set the maximum width, this also enables using
4688 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004689 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4690 0, 500);
4691
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004692 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004693 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004694 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004695 }
4696 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004697 {
4698 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4699
Bram Moolenaar734a8672019-12-02 22:49:38 +01004700 // Set the maximum width, this also enables using
4701 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004702 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4703 0, 500);
4704
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004705 if (STRLEN(str) < sizeof(lpdi->szText)
4706 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004707 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004708 sizeof(lpdi->szText) - 1);
4709 else
4710 lpdi->lpszText = tt_text;
4711 }
4712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004715# ifdef FEAT_GUI_TABLINE
4716 case TCN_SELCHANGE:
4717 if (gui_mch_showing_tabline()
4718 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004719 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004720 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004721 return 0L;
4722 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004723 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004724
4725 case NM_RCLICK:
4726 if (gui_mch_showing_tabline()
4727 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004728 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004729 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004730 return 0L;
4731 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004732 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004735# ifdef FEAT_GUI_TABLINE
4736 if (gui_mch_showing_tabline()
4737 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4738 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4739# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 break;
4741 }
4742 break;
4743#endif
4744#if defined(MENUHINTS) && defined(FEAT_MENU)
4745 case WM_MENUSELECT:
4746 if (((UINT) HIWORD(wParam)
4747 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4748 == MF_HILITE
4749 && (State & CMDLINE) == 0)
4750 {
4751 UINT idButton;
4752 vimmenu_T *pMenu;
4753 static int did_menu_tip = FALSE;
4754
4755 if (did_menu_tip)
4756 {
4757 msg_clr_cmdline();
4758 setcursor();
4759 out_flush();
4760 did_menu_tip = FALSE;
4761 }
4762
4763 idButton = (UINT)LOWORD(wParam);
4764 pMenu = gui_mswin_find_menu(root_menu, idButton);
4765 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4766 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4767 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004768 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004769 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004770 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 setcursor();
4772 out_flush();
4773 did_menu_tip = TRUE;
4774 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004775 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 break;
4778#endif
4779 case WM_NCHITTEST:
4780 {
4781 LRESULT result;
4782 int x, y;
4783 int xPos = GET_X_LPARAM(lParam);
4784
4785 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4786 if (result == HTCLIENT)
4787 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004788#ifdef FEAT_GUI_TABLINE
4789 if (gui_mch_showing_tabline())
4790 {
4791 int yPos = GET_Y_LPARAM(lParam);
4792 RECT rct;
4793
Bram Moolenaar734a8672019-12-02 22:49:38 +01004794 // If the cursor is on the GUI tabline, don't process this
4795 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004796 GetWindowRect(s_textArea, &rct);
4797 if (yPos < rct.top)
4798 return result;
4799 }
4800#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004801 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 xPos -= x;
4803
Bram Moolenaar734a8672019-12-02 22:49:38 +01004804 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return HTBOTTOMLEFT;
4806 else
4807 return HTBOTTOMRIGHT;
4808 }
4809 else
4810 return result;
4811 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004812 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814#ifdef FEAT_MBYTE_IME
4815 case WM_IME_NOTIFY:
4816 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4817 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004818 return 1L;
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 case WM_IME_COMPOSITION:
4821 if (!_OnImeComposition(hwnd, wParam, lParam))
4822 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004823 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825
4826 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004828 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
4831 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4832 }
4833
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004834 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835}
4836
4837/*
4838 * End of call-back routines
4839 */
4840
Bram Moolenaar734a8672019-12-02 22:49:38 +01004841// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842HWND vim_parent_hwnd = NULL;
4843
4844 static BOOL CALLBACK
4845FindWindowTitle(HWND hwnd, LPARAM lParam)
4846{
4847 char buf[2048];
4848 char *title = (char *)lParam;
4849
4850 if (GetWindowText(hwnd, buf, sizeof(buf)))
4851 {
4852 if (strstr(buf, title) != NULL)
4853 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004854 // Found it. Store the window ref. and quit searching if MDI
4855 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004857 if (vim_parent_hwnd != NULL)
4858 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
4860 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004861 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862}
4863
4864/*
4865 * Invoked for '-P "title"' argument: search for parent application to open
4866 * our window in.
4867 */
4868 void
4869gui_mch_set_parent(char *title)
4870{
4871 EnumWindows(FindWindowTitle, (LPARAM)title);
4872 if (vim_parent_hwnd == NULL)
4873 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004874 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 mch_exit(2);
4876 }
4877}
4878
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 static void
4881ole_error(char *arg)
4882{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004883 char buf[IOSIZE];
4884
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004885# ifdef VIMDLL
4886 gui.in_use = mch_is_gui_executable();
4887# endif
4888
Bram Moolenaar734a8672019-12-02 22:49:38 +01004889 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004890 vim_snprintf(buf, IOSIZE,
4891 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4892 arg);
4893 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004897#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4898 static char *
4899gvim_error(void)
4900{
4901 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4902
4903 if (starting)
4904 {
4905 mch_errmsg(msg);
4906 mch_errmsg("\n");
4907 mch_exit(2);
4908 }
4909 return msg;
4910}
4911
4912 char *
4913gui_mch_do_spawn(char_u *arg)
4914{
4915 int len;
4916# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4917 char_u *session = NULL;
4918 LPWSTR tofree1 = NULL;
4919# endif
4920 WCHAR name[MAX_PATH];
4921 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4922 STARTUPINFOW si = {sizeof(si)};
4923 PROCESS_INFORMATION pi;
4924
4925 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4926 goto error;
4927 p = wcsrchr(name, L'\\');
4928 if (p == NULL)
4929 goto error;
4930 // Replace the executable name from vim(d).exe to gvim(d).exe.
4931# ifdef DEBUG
4932 wcscpy(p + 1, L"gvimd.exe");
4933# else
4934 wcscpy(p + 1, L"gvim.exe");
4935# endif
4936
4937# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4938 if (starting)
4939# endif
4940 {
4941 // Pass the command line to the new process.
4942 p = GetCommandLineW();
4943 // Skip 1st argument.
4944 while (*p && *p != L' ' && *p != L'\t')
4945 {
4946 if (*p == L'"')
4947 {
4948 while (*p && *p != L'"')
4949 ++p;
4950 if (*p)
4951 ++p;
4952 }
4953 else
4954 ++p;
4955 }
4956 cmd = p;
4957 }
4958# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4959 else
4960 {
4961 // Create a session file and pass it to the new process.
4962 LPWSTR wsession;
4963 char_u *savebg;
4964 int ret;
4965
4966 session = vim_tempname('s', FALSE);
4967 if (session == NULL)
4968 goto error;
4969 savebg = p_bg;
4970 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4971 ret = write_session_file(session);
4972 vim_free(p_bg);
4973 p_bg = savebg;
4974 if (!ret)
4975 goto error;
4976 wsession = enc_to_utf16(session, NULL);
4977 if (wsession == NULL)
4978 goto error;
4979 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004980 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004981 if (cmd == NULL)
4982 {
4983 vim_free(wsession);
4984 goto error;
4985 }
4986 tofree1 = cmd;
4987 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4988 wsession, wsession);
4989 vim_free(wsession);
4990 }
4991# endif
4992
4993 // Check additional arguments to the `:gui` command.
4994 if (arg != NULL)
4995 {
4996 warg = enc_to_utf16(arg, NULL);
4997 if (warg == NULL)
4998 goto error;
4999 tofree2 = warg;
5000 }
5001 else
5002 warg = L"";
5003
5004 // Set up the new command line.
5005 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005006 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005007 if (newcmd == NULL)
5008 goto error;
5009 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5010
5011 // Spawn a new GUI process.
5012 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5013 NULL, NULL, &si, &pi))
5014 goto error;
5015 CloseHandle(pi.hProcess);
5016 CloseHandle(pi.hThread);
5017 mch_exit(0);
5018
5019error:
5020# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5021 if (session)
5022 mch_remove(session);
5023 vim_free(session);
5024 vim_free(tofree1);
5025# endif
5026 vim_free(newcmd);
5027 vim_free(tofree2);
5028 return gvim_error();
5029}
5030#endif
5031
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032/*
5033 * Parse the GUI related command-line arguments. Any arguments used are
5034 * deleted from argv, and *argc is decremented accordingly. This is called
5035 * when vim is started, whether or not the GUI has been started.
5036 */
5037 void
5038gui_mch_prepare(int *argc, char **argv)
5039{
5040 int silent = FALSE;
5041 int idx;
5042
Bram Moolenaar734a8672019-12-02 22:49:38 +01005043 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5045 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005046 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5048 && (argv[2][0] == '-' || argv[2][0] == '/'))
5049 {
5050 silent = TRUE;
5051 idx = 2;
5052 }
5053 else
5054 idx = 1;
5055
Bram Moolenaar734a8672019-12-02 22:49:38 +01005056 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (STRICMP(argv[idx] + 1, "register") == 0)
5058 {
5059#ifdef FEAT_OLE
5060 RegisterMe(silent);
5061 mch_exit(0);
5062#else
5063 if (!silent)
5064 ole_error("register");
5065 mch_exit(2);
5066#endif
5067 }
5068
Bram Moolenaar734a8672019-12-02 22:49:38 +01005069 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5071 {
5072#ifdef FEAT_OLE
5073 UnregisterMe(!silent);
5074 mch_exit(0);
5075#else
5076 if (!silent)
5077 ole_error("unregister");
5078 mch_exit(2);
5079#endif
5080 }
5081
Bram Moolenaar734a8672019-12-02 22:49:38 +01005082 // Ignore an -embedding argument. It is only relevant if the
5083 // application wants to treat the case when it is started manually
5084 // differently from the case where it is started via automation (and
5085 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5087 {
5088#ifdef FEAT_OLE
5089 *argc = 1;
5090#else
5091 ole_error("embedding");
5092 mch_exit(2);
5093#endif
5094 }
5095 }
5096
5097#ifdef FEAT_OLE
5098 {
5099 int bDoRestart = FALSE;
5100
5101 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005102 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (bDoRestart)
5104 mch_exit(0);
5105 }
5106#endif
5107
5108#ifdef FEAT_NETBEANS_INTG
5109 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005110 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 int arg;
5112
5113 for (arg = 1; arg < *argc; arg++)
5114 if (strncmp("-nb", argv[arg], 3) == 0)
5115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 netbeansArg = argv[arg];
5117 mch_memmove(&argv[arg], &argv[arg + 1],
5118 (--*argc - arg) * sizeof(char *));
5119 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005120 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124}
5125
5126/*
5127 * Initialise the GUI. Create all the windows, set up all the call-backs
5128 * etc.
5129 */
5130 int
5131gui_mch_init(void)
5132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005134 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136#ifdef GLOBAL_IME
5137 ATOM atom;
5138#endif
5139
Bram Moolenaar734a8672019-12-02 22:49:38 +01005140 // Return here if the window was already opened (happens when
5141 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005143 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 /*
5146 * Load the tearoff bitmap
5147 */
5148#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005149 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#endif
5151
5152 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5153 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5154#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156#endif
5157 gui.border_width = 0;
5158
5159 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5160
Bram Moolenaar734a8672019-12-02 22:49:38 +01005161 // First try using the wide version, so that we can use any title.
5162 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005163 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005165 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 wndclassw.lpfnWndProc = _WndProc;
5167 wndclassw.cbClsExtra = 0;
5168 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005169 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5171 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5172 wndclassw.hbrBackground = s_brush;
5173 wndclassw.lpszMenuName = NULL;
5174 wndclassw.lpszClassName = szVimWndClassW;
5175
5176 if ((
5177#ifdef GLOBAL_IME
5178 atom =
5179#endif
5180 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if (vim_parent_hwnd != NULL)
5185 {
5186#ifdef HAVE_TRY_EXCEPT
5187 __try
5188 {
5189#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005190 // Open inside the specified parent window.
5191 // TODO: last argument should point to a CLIENTCREATESTRUCT
5192 // structure.
5193 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005195 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005196 WS_OVERLAPPEDWINDOW | WS_CHILD
5197 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5199 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005200 100, // Any value will do
5201 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005203 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#ifdef HAVE_TRY_EXCEPT
5205 }
5206 __except(EXCEPTION_EXECUTE_HANDLER)
5207 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005208 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210#endif
5211 if (s_hwnd == NULL)
5212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005213 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 mch_exit(2);
5215 }
5216 }
5217 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005218 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005219 // If the provided windowid is not valid reset it to zero, so that it
5220 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005221 if (IsWindow((HWND)win_socket_id) <= 0)
5222 win_socket_id = 0;
5223
Bram Moolenaar734a8672019-12-02 22:49:38 +01005224 // Create a window. If win_socket_id is not zero without border and
5225 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005226 s_hwnd = CreateWindowW(
5227 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005228 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5229 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005230 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5231 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005232 100, // Any value will do
5233 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005234 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005235 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005236 if (s_hwnd != NULL && win_socket_id != 0)
5237 {
5238 SetParent(s_hwnd, (HWND)win_socket_id);
5239 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5240 }
5241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 if (s_hwnd == NULL)
5244 return FAIL;
5245
5246#ifdef GLOBAL_IME
5247 global_ime_init(atom, s_hwnd);
5248#endif
5249#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5250 dyn_imm_load();
5251#endif
5252
Bram Moolenaar734a8672019-12-02 22:49:38 +01005253 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005254 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005255 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005256 wndclassw.style = CS_OWNDC;
5257 wndclassw.lpfnWndProc = _TextAreaWndProc;
5258 wndclassw.cbClsExtra = 0;
5259 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005260 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005261 wndclassw.hIcon = NULL;
5262 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5263 wndclassw.hbrBackground = NULL;
5264 wndclassw.lpszMenuName = NULL;
5265 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005266
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005267 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 return FAIL;
5269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005271 s_textArea = CreateWindowExW(
5272 0,
5273 szTextAreaClassW, L"Vim text area",
5274 WS_CHILD | WS_VISIBLE, 0, 0,
5275 100, // Any value will do for now
5276 100, // Any value will do for now
5277 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005278 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (s_textArea == NULL)
5281 return FAIL;
5282
Bram Moolenaar20321902016-02-17 12:30:17 +01005283#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005284 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005285 {
5286 HANDLE hIcon = NULL;
5287
5288 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005289 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005290 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005291#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293#ifdef FEAT_MENU
5294 s_menuBar = CreateMenu();
5295#endif
5296 s_hdc = GetDC(s_textArea);
5297
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaar734a8672019-12-02 22:49:38 +01005300 // Do we need to bother with this?
5301 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
Bram Moolenaar734a8672019-12-02 22:49:38 +01005303 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 gui_mch_def_colors();
5305
Bram Moolenaar734a8672019-12-02 22:49:38 +01005306 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5307 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 set_normal_colors();
5309
5310 /*
5311 * Check that none of the colors are the same as the background color.
5312 * Then store the current values as the defaults.
5313 */
5314 gui_check_colors();
5315 gui.def_norm_pixel = gui.norm_pixel;
5316 gui.def_back_pixel = gui.back_pixel;
5317
Bram Moolenaar734a8672019-12-02 22:49:38 +01005318 // Get the colors for the highlight groups (gui_check_colors() might have
5319 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 highlight_gui_started();
5321
5322 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005323 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005325 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 /*
5328 * Set up for Intellimouse processing
5329 */
5330 init_mouse_wheel();
5331
5332 /*
5333 * compute a couple of metrics used for the dialogs
5334 */
5335 get_dialog_font_metrics();
5336#ifdef FEAT_TOOLBAR
5337 /*
5338 * Create the toolbar
5339 */
5340 initialise_toolbar();
5341#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005342#ifdef FEAT_GUI_TABLINE
5343 /*
5344 * Create the tabline
5345 */
5346 initialise_tabline();
5347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348#ifdef MSWIN_FIND_REPLACE
5349 /*
5350 * Initialise the dialog box stuff
5351 */
5352 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5353
Bram Moolenaar734a8672019-12-02 22:49:38 +01005354 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005356 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005358 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5360 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5361 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005364#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005365# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005366// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005367# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005368# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005369# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005370# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005372 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005373#endif
5374
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005375#ifdef FEAT_RENDER_OPTIONS
5376 if (p_rop)
5377 (void)gui_mch_set_rendering_options(p_rop);
5378#endif
5379
Bram Moolenaar748bf032005-02-02 23:04:36 +00005380theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005381 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005382 display_errors();
5383
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 return OK;
5385}
5386
5387/*
5388 * Get the size of the screen, taking position on multiple monitors into
5389 * account (if supported).
5390 */
5391 static void
5392get_work_area(RECT *spi_rect)
5393{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005394 HMONITOR mon;
5395 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaar734a8672019-12-02 22:49:38 +01005397 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005398 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005399 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005401 moninfo.cbSize = sizeof(MONITORINFO);
5402 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005404 *spi_rect = moninfo.rcWork;
5405 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005408 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5410}
5411
5412/*
5413 * Set the size of the window to the given width and height in pixels.
5414 */
5415 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005416gui_mch_set_shellsize(
5417 int width,
5418 int height,
5419 int min_width UNUSED,
5420 int min_height UNUSED,
5421 int base_width UNUSED,
5422 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005423 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005426 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
Bram Moolenaar734a8672019-12-02 22:49:38 +01005429 // Try to keep window completely on screen.
5430 // Get position of the screen work area. This is the part that is not
5431 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 get_work_area(&workarea_rect);
5433
Bram Moolenaar734a8672019-12-02 22:49:38 +01005434 // Resizing a maximized window looks very strange, unzoom it first.
5435 // But don't do it when still starting up, it may have been requested in
5436 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005437 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005439
5440 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
Bram Moolenaar734a8672019-12-02 22:49:38 +01005442 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005443 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005444 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005445 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005446 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 + GetSystemMetrics(SM_CYCAPTION)
5448#ifdef FEAT_MENU
5449 + gui_mswin_get_menu_height(FALSE)
5450#endif
5451 ;
5452
Bram Moolenaar734a8672019-12-02 22:49:38 +01005453 // The following should take care of keeping Vim on the same monitor, no
5454 // matter if the secondary monitor is left or right of the primary
5455 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005456 window_rect.right = window_rect.left + win_width;
5457 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005458
Bram Moolenaar734a8672019-12-02 22:49:38 +01005459 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005460 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5461 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005463 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5464 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005466 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5467 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005469 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5470 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005472 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5473 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 SetActiveWindow(s_hwnd);
5476 SetFocus(s_hwnd);
5477
5478#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005479 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 gui_mswin_get_menu_height(!gui.starting);
5481#endif
5482}
5483
5484
5485 void
5486gui_mch_set_scrollbar_thumb(
5487 scrollbar_T *sb,
5488 long val,
5489 long size,
5490 long max)
5491{
5492 SCROLLINFO info;
5493
5494 sb->scroll_shift = 0;
5495 while (max > 32767)
5496 {
5497 max = (max + 1) >> 1;
5498 val >>= 1;
5499 size >>= 1;
5500 ++sb->scroll_shift;
5501 }
5502
5503 if (sb->scroll_shift > 0)
5504 ++size;
5505
5506 info.cbSize = sizeof(info);
5507 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5508 info.nPos = val;
5509 info.nMin = 0;
5510 info.nMax = max;
5511 info.nPage = size;
5512 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5513}
5514
5515
5516/*
5517 * Set the current text font.
5518 */
5519 void
5520gui_mch_set_font(GuiFont font)
5521{
5522 gui.currFont = font;
5523}
5524
5525
5526/*
5527 * Set the current text foreground color.
5528 */
5529 void
5530gui_mch_set_fg_color(guicolor_T color)
5531{
5532 gui.currFgColor = color;
5533}
5534
5535/*
5536 * Set the current text background color.
5537 */
5538 void
5539gui_mch_set_bg_color(guicolor_T color)
5540{
5541 gui.currBgColor = color;
5542}
5543
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005544/*
5545 * Set the current text special color.
5546 */
5547 void
5548gui_mch_set_sp_color(guicolor_T color)
5549{
5550 gui.currSpColor = color;
5551}
5552
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005553#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554/*
5555 * Multi-byte handling, originally by Sung-Hoon Baek.
5556 * First static functions (no prototypes generated).
5557 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005558# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005559# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005560# endif
5561# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 * handle WM_IME_NOTIFY message
5565 */
5566 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005567_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 LRESULT lResult = 0;
5570 HIMC hImc;
5571
5572 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5573 return lResult;
5574 switch (dwCommand)
5575 {
5576 case IMN_SETOPENSTATUS:
5577 if (pImmGetOpenStatus(hImc))
5578 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005579 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 im_set_position(gui.row, gui.col);
5581
Bram Moolenaar734a8672019-12-02 22:49:38 +01005582 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 State &= ~LANGMAP;
5584 if (State & INSERT)
5585 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005586# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005587 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5589 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005590 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 int old_row = gui.row;
5592 int old_col = gui.col;
5593
5594 // This must be called here before
5595 // status_redraw_curbuf(), otherwise the mode
5596 // message may appear in the wrong position.
5597 showmode();
5598 status_redraw_curbuf();
5599 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 gui.row = old_row;
5602 gui.col = old_col;
5603 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
5606 }
5607 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005608 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 lResult = 0;
5610 break;
5611 }
5612 pImmReleaseContext(hWnd, hImc);
5613 return lResult;
5614}
5615
5616 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005617_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 char_u *ret;
5620 int len;
5621
Bram Moolenaar734a8672019-12-02 22:49:38 +01005622 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 return 0;
5624
5625 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5626 if (ret != NULL)
5627 {
5628 add_to_input_buf_csi(ret, len);
5629 vim_free(ret);
5630 return 1;
5631 }
5632 return 0;
5633}
5634
5635/*
5636 * get the current composition string, in UCS-2; *lenp is the number of
5637 * *lenp is the number of Unicode characters.
5638 */
5639 static short_u *
5640GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5641{
5642 LONG ret;
5643 LPWSTR wbuf = NULL;
5644 char_u *buf;
5645
5646 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005647 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar734a8672019-12-02 22:49:38 +01005649 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5651 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005652 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 if (ret > 0)
5655 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005656 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005657 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 if (wbuf != NULL)
5659 {
5660 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5661 *lenp = ret / sizeof(WCHAR);
5662 }
5663 return (short_u *)wbuf;
5664 }
5665
Bram Moolenaar734a8672019-12-02 22:49:38 +01005666 // ret < 0; we got an error, so try the ANSI version. This'll work
5667 // on 9x/ME, but only if the codepage happens to be set to whatever
5668 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5670 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005671 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
5673 buf = alloc(ret);
5674 if (buf == NULL)
5675 return NULL;
5676 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5677
Bram Moolenaar734a8672019-12-02 22:49:38 +01005678 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005679 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 vim_free(buf);
5681
5682 return (short_u *)wbuf;
5683}
5684
5685/*
5686 * void GetResultStr()
5687 *
5688 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5689 * get complete composition string
5690 */
5691 static char_u *
5692GetResultStr(HWND hwnd, int GCS, int *lenp)
5693{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005694 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 short_u *buf = NULL;
5696 char_u *convbuf = NULL;
5697
5698 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5699 return NULL;
5700
Bram Moolenaar734a8672019-12-02 22:49:38 +01005701 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5703 if (buf == NULL)
5704 return NULL;
5705
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005706 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 pImmReleaseContext(hwnd, hIMC);
5708 vim_free(buf);
5709 return convbuf;
5710}
5711#endif
5712
Bram Moolenaar734a8672019-12-02 22:49:38 +01005713// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005714#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716/*
5717 * set font to IM.
5718 */
5719 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005720im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 HIMC hImc;
5723
5724 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5725 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005726 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 pImmReleaseContext(s_hwnd, hImc);
5728 }
5729}
5730
5731/*
5732 * Notify cursor position to IM.
5733 */
5734 void
5735im_set_position(int row, int col)
5736{
5737 HIMC hImc;
5738
5739 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5740 {
5741 COMPOSITIONFORM cfs;
5742
5743 cfs.dwStyle = CFS_POINT;
5744 cfs.ptCurrentPos.x = FILL_X(col);
5745 cfs.ptCurrentPos.y = FILL_Y(row);
5746 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5747 pImmSetCompositionWindow(hImc, &cfs);
5748
5749 pImmReleaseContext(s_hwnd, hImc);
5750 }
5751}
5752
5753/*
5754 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5755 */
5756 void
5757im_set_active(int active)
5758{
5759 HIMC hImc;
5760 static HIMC hImcOld = (HIMC)0;
5761
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005762# ifdef VIMDLL
5763 if (!gui.in_use && !gui.starting)
5764 {
5765 mbyte_im_set_active(active);
5766 return;
5767 }
5768# endif
5769
Bram Moolenaar734a8672019-12-02 22:49:38 +01005770 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 if (p_imdisable)
5773 {
5774 if (hImcOld == (HIMC)0)
5775 {
5776 hImcOld = pImmGetContext(s_hwnd);
5777 if (hImcOld)
5778 pImmAssociateContext(s_hwnd, (HIMC)0);
5779 }
5780 active = FALSE;
5781 }
5782 else if (hImcOld != (HIMC)0)
5783 {
5784 pImmAssociateContext(s_hwnd, hImcOld);
5785 hImcOld = (HIMC)0;
5786 }
5787
5788 hImc = pImmGetContext(s_hwnd);
5789 if (hImc)
5790 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005791 /*
5792 * for Korean ime
5793 */
5794 HKL hKL = GetKeyboardLayout(0);
5795
5796 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5797 {
5798 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5799 static BOOL bSaved = FALSE;
5800
5801 if (active)
5802 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005803 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005804 if (bSaved)
5805 pImmSetConversionStatus(hImc, dwConversionSaved,
5806 dwSentenceSaved);
5807 bSaved = FALSE;
5808 }
5809 else
5810 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005811 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005812 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5813 &dwSentenceSaved))
5814 {
5815 bSaved = TRUE;
5816 pImmSetConversionStatus(hImc,
5817 dwConversionSaved & ~(IME_CMODE_NATIVE
5818 | IME_CMODE_FULLSHAPE),
5819 dwSentenceSaved);
5820 }
5821 }
5822 }
5823
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 pImmSetOpenStatus(hImc, active);
5825 pImmReleaseContext(s_hwnd, hImc);
5826 }
5827 }
5828}
5829
5830/*
5831 * Get IM status. When IM is on, return not 0. Else return 0.
5832 */
5833 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005834im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int status = 0;
5837 HIMC hImc;
5838
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005839# ifdef VIMDLL
5840 if (!gui.in_use && !gui.starting)
5841 return mbyte_im_get_status();
5842# endif
5843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5845 {
5846 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5847 pImmReleaseContext(s_hwnd, hImc);
5848 }
5849 return status;
5850}
5851
Bram Moolenaar734a8672019-12-02 22:49:38 +01005852#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005854#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005855// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
5857/*
5858 * Notify cursor position to IM.
5859 */
5860 void
5861im_set_position(int row, int col)
5862{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005863 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 POINT p;
5865
5866 p.x = FILL_X(col);
5867 p.y = FILL_Y(row);
5868 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5869 global_ime_set_position(&p);
5870}
5871
5872/*
5873 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5874 */
5875 void
5876im_set_active(int active)
5877{
5878 global_ime_set_status(active);
5879}
5880
5881/*
5882 * Get IM status. When IM is on, return not 0. Else return 0.
5883 */
5884 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005885im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 return global_ime_get_status();
5888}
5889#endif
5890
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005891/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005892 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005893 */
5894 static void
5895latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5896{
5897 int c;
5898
Bram Moolenaarca003e12006-03-17 23:19:38 +00005899 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005900 {
5901 c = *text++;
5902 switch (c)
5903 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005904 case 0xa4: c = 0x20ac; break; // euro
5905 case 0xa6: c = 0x0160; break; // S hat
5906 case 0xa8: c = 0x0161; break; // S -hat
5907 case 0xb4: c = 0x017d; break; // Z hat
5908 case 0xb8: c = 0x017e; break; // Z -hat
5909 case 0xbc: c = 0x0152; break; // OE
5910 case 0xbd: c = 0x0153; break; // oe
5911 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005912 }
5913 *unicodebuf++ = c;
5914 }
5915}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
5917#ifdef FEAT_RIGHTLEFT
5918/*
5919 * What is this for? In the case where you are using Win98 or Win2K or later,
5920 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5921 * reverses the string sent to the TextOut... family. This sucks, because we
5922 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5923 * way to tell Windblows not to do this!
5924 *
5925 * The short of it is that this 'RevOut' only gets called if you are running
5926 * one of the new, "improved" MS OSes, and only if you are running in
5927 * 'rightleft' mode. It makes display take *slightly* longer, but not
5928 * noticeably so.
5929 */
5930 static void
5931RevOut( HDC s_hdc,
5932 int col,
5933 int row,
5934 UINT foptions,
5935 CONST RECT *pcliprect,
5936 LPCTSTR text,
5937 UINT len,
5938 CONST INT *padding)
5939{
5940 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005942 for (ix = 0; ix < (int)len; ++ix)
5943 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5944 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945}
5946#endif
5947
Bram Moolenaar92467d32017-12-05 13:22:16 +01005948 static void
5949draw_line(
5950 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005951 int y1,
5952 int x2,
5953 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005954 COLORREF color)
5955{
5956#if defined(FEAT_DIRECTX)
5957 if (IS_ENABLE_DIRECTX())
5958 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5959 else
5960#endif
5961 {
5962 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5963 HPEN old_pen = SelectObject(s_hdc, hpen);
5964 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005965 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005966 LineTo(s_hdc, x2, y2);
5967 DeleteObject(SelectObject(s_hdc, old_pen));
5968 }
5969}
5970
5971 static void
5972set_pixel(
5973 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005974 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005975 COLORREF color)
5976{
5977#if defined(FEAT_DIRECTX)
5978 if (IS_ENABLE_DIRECTX())
5979 DWriteContext_SetPixel(s_dwc, x, y, color);
5980 else
5981#endif
5982 SetPixel(s_hdc, x, y, color);
5983}
5984
5985 static void
5986fill_rect(
5987 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005988 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005989 COLORREF color)
5990{
5991#if defined(FEAT_DIRECTX)
5992 if (IS_ENABLE_DIRECTX())
5993 DWriteContext_FillRect(s_dwc, rcp, color);
5994 else
5995#endif
5996 {
5997 HBRUSH hbr2;
5998
5999 if (hbr == NULL)
6000 hbr2 = CreateSolidBrush(color);
6001 else
6002 hbr2 = hbr;
6003 FillRect(s_hdc, rcp, hbr2);
6004 if (hbr == NULL)
6005 DeleteBrush(hbr2);
6006 }
6007}
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 void
6010gui_mch_draw_string(
6011 int row,
6012 int col,
6013 char_u *text,
6014 int len,
6015 int flags)
6016{
6017 static int *padding = NULL;
6018 static int pad_size = 0;
6019 int i;
6020 const RECT *pcliprect = NULL;
6021 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 static WCHAR *unicodebuf = NULL;
6023 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006024 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 int y;
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 /*
6029 * Italic and bold text seems to have an extra row of pixels at the bottom
6030 * (below where the bottom of the character should be). If we draw the
6031 * characters with a solid background, the top row of pixels in the
6032 * character below will be overwritten. We can fix this by filling in the
6033 * background ourselves, to the correct character proportions, and then
6034 * writing the character in transparent mode. Still have a problem when
6035 * the character is "_", which gets written on to the character below.
6036 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6037 * pixel in their slots, which fixes the problem with the bottom row of
6038 * pixels. We still need this code because otherwise the top row of pixels
6039 * becomes a problem. - webb.
6040 */
6041 static HBRUSH hbr_cache[2] = {NULL, NULL};
6042 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6043 static int brush_lru = 0;
6044 HBRUSH hbr;
6045 RECT rc;
6046
6047 if (!(flags & DRAW_TRANSP))
6048 {
6049 /*
6050 * Clear background first.
6051 * Note: FillRect() excludes right and bottom of rectangle.
6052 */
6053 rc.left = FILL_X(col);
6054 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (has_mbyte)
6056 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006057 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006058 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 rc.right = FILL_X(col + len);
6062 rc.bottom = FILL_Y(row + 1);
6063
Bram Moolenaar734a8672019-12-02 22:49:38 +01006064 // Cache the created brush, that saves a lot of time. We need two:
6065 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (gui.currBgColor == brush_color[0])
6067 {
6068 hbr = hbr_cache[0];
6069 brush_lru = 1;
6070 }
6071 else if (gui.currBgColor == brush_color[1])
6072 {
6073 hbr = hbr_cache[1];
6074 brush_lru = 0;
6075 }
6076 else
6077 {
6078 if (hbr_cache[brush_lru] != NULL)
6079 DeleteBrush(hbr_cache[brush_lru]);
6080 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6081 brush_color[brush_lru] = gui.currBgColor;
6082 hbr = hbr_cache[brush_lru];
6083 brush_lru = !brush_lru;
6084 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006085
Bram Moolenaar92467d32017-12-05 13:22:16 +01006086 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 SetBkMode(s_hdc, TRANSPARENT);
6089
6090 /*
6091 * When drawing block cursor, prevent inverted character spilling
6092 * over character cell (can happen with bold/italic)
6093 */
6094 if (flags & DRAW_CURSOR)
6095 {
6096 pcliprect = &rc;
6097 foptions = ETO_CLIPPED;
6098 }
6099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 SetTextColor(s_hdc, gui.currFgColor);
6101 SelectFont(s_hdc, gui.currFont);
6102
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006103#ifdef FEAT_DIRECTX
6104 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006105 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006106#endif
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6109 {
6110 vim_free(padding);
6111 pad_size = Columns;
6112
Bram Moolenaar734a8672019-12-02 22:49:38 +01006113 // Don't give an out-of-memory message here, it would call us
6114 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006115 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 if (padding != NULL)
6117 for (i = 0; i < pad_size; i++)
6118 padding[i] = gui.char_width;
6119 }
6120
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 /*
6122 * We have to provide the padding argument because italic and bold versions
6123 * of fixed-width fonts are often one pixel or so wider than their normal
6124 * versions.
6125 * No check for DRAW_BOLD, Windows will have done it already.
6126 */
6127
Bram Moolenaar734a8672019-12-02 22:49:38 +01006128 // Check if there are any UTF-8 characters. If not, use normal text
6129 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (enc_utf8)
6131 for (n = 0; n < len; ++n)
6132 if (text[n] >= 0x80)
6133 break;
6134
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006135#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006136 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6137 // required that unicode drawing routine, currently. So this forces it
6138 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006139 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006140 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006141#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006142
Bram Moolenaar734a8672019-12-02 22:49:38 +01006143 // Check if the Unicode buffer exists and is big enough. Create it
6144 // with the same length as the multi-byte string, the number of wide
6145 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006146 if ((enc_utf8
6147 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6148 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 && (unicodebuf == NULL || len > unibuflen))
6150 {
6151 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006152 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006155 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 unibuflen = len;
6158 }
6159
6160 if (enc_utf8 && n < len && unicodebuf != NULL)
6161 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006162 // Output UTF-8 characters. Composing characters should be
6163 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006164 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006165 int wlen; // string length in words
6166 int clen; // string length in characters
6167 int cells; // cell width of string up to composing char
6168 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006169 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006171 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006172 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006174 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006176 c = utf_ptr2char(text + i);
6177 if (c >= 0x10000)
6178 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006180 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6181 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006182 }
6183 else
6184 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006185 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006186 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006187
6188 if (utf_iscomposing(c))
6189 cw = 0;
6190 else
6191 {
6192 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006193 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006194 cw = 1;
6195 }
6196
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 if (unicodepdy != NULL)
6198 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006199 // Use unicodepdy to make characters fit as we expect, even
6200 // when the font uses different widths (e.g., bold character
6201 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006202 if (c >= 0x10000)
6203 {
6204 unicodepdy[wlen - 2] = cw * gui.char_width;
6205 unicodepdy[wlen - 1] = 0;
6206 }
6207 else
6208 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006211 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006212 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006214#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006215 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006216 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006217 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006218 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006219 TEXT_X(col), TEXT_Y(row),
6220 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006221 gui.char_width, gui.currFgColor,
6222 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006223 }
6224 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006225#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006226 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6227 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006228 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006230 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // If we want to display codepage data, and the current CP is not the
6233 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (unicodebuf != NULL)
6235 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006236 if (enc_latin9)
6237 latin9_to_ucs(text, len, unicodebuf);
6238 else
6239 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 MB_PRECOMPOSED,
6241 (char *)text, len,
6242 (LPWSTR)unicodebuf, unibuflen);
6243 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006244 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006245 // Use unicodepdy to make characters fit as we expect, even
6246 // when the font uses different widths (e.g., bold character
6247 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006248 if (unicodepdy != NULL)
6249 {
6250 int i;
6251 int cw;
6252
6253 for (i = 0; i < len; ++i)
6254 {
6255 cw = utf_char2cells(unicodebuf[i]);
6256 if (cw > 2)
6257 cw = 1;
6258 unicodepdy[i] = cw * gui.char_width;
6259 }
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006262 foptions, pcliprect, unicodebuf, len, unicodepdy);
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 }
6265 }
6266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
6268#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006269 // Windows will mess up RL text, so we have to draw it character by
6270 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006271 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6273 foptions, pcliprect, (char *)text, len, padding);
6274 else
6275#endif
6276 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6277 foptions, pcliprect, (char *)text, len, padding);
6278 }
6279
Bram Moolenaar734a8672019-12-02 22:49:38 +01006280 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 if (flags & DRAW_UNDERL)
6282 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006283 // When p_linespace is 0, overwrite the bottom row of pixels.
6284 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 if (p_linespace > 1)
6287 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006288 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006290
Bram Moolenaar734a8672019-12-02 22:49:38 +01006291 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006292 if (flags & DRAW_STRIKE)
6293 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006294 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006295 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006296 }
6297
Bram Moolenaar734a8672019-12-02 22:49:38 +01006298 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006299 if (flags & DRAW_UNDERC)
6300 {
6301 int x;
6302 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006303 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006304
6305 y = FILL_Y(row + 1) - 1;
6306 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6307 {
6308 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006309 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006310 }
6311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312}
6313
6314
6315/*
6316 * Output routines.
6317 */
6318
Bram Moolenaar734a8672019-12-02 22:49:38 +01006319/*
6320 * Flush any output to the screen
6321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 void
6323gui_mch_flush(void)
6324{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006325#if defined(FEAT_DIRECTX)
6326 if (IS_ENABLE_DIRECTX())
6327 DWriteContext_Flush(s_dwc);
6328#endif
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 GdiFlush();
6331}
6332
6333 static void
6334clear_rect(RECT *rcp)
6335{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006336 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339
Bram Moolenaarc716c302006-01-21 22:12:51 +00006340 void
6341gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6342{
6343 RECT workarea_rect;
6344
6345 get_work_area(&workarea_rect);
6346
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006347 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006348 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006349 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006350
Bram Moolenaar734a8672019-12-02 22:49:38 +01006351 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6352 // the menubar for MSwin, we subtract it from the screen height, so that
6353 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006354 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006355 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006356 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006357 - GetSystemMetrics(SM_CYCAPTION)
6358#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006359 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006360#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006361 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006362}
6363
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365#if defined(FEAT_MENU) || defined(PROTO)
6366/*
6367 * Add a sub menu to the menu bar.
6368 */
6369 void
6370gui_mch_add_menu(
6371 vimmenu_T *menu,
6372 int pos)
6373{
6374 vimmenu_T *parent = menu->parent;
6375
6376 menu->submenu_id = CreatePopupMenu();
6377 menu->id = s_menu_id++;
6378
6379 if (menu_is_menubar(menu->name))
6380 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006381 WCHAR *wn;
6382 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006384 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006385 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006386 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006388 infow.cbSize = sizeof(infow);
6389 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6390 | MIIM_SUBMENU;
6391 infow.dwItemData = (long_u)menu;
6392 infow.wID = menu->id;
6393 infow.fType = MFT_STRING;
6394 infow.dwTypeData = wn;
6395 infow.cch = (UINT)wcslen(wn);
6396 infow.hSubMenu = menu->submenu_id;
6397 InsertMenuItemW((parent == NULL)
6398 ? s_menuBar : parent->submenu_id,
6399 (UINT)pos, TRUE, &infow);
6400 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 }
6402
Bram Moolenaar734a8672019-12-02 22:49:38 +01006403 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (parent == NULL)
6405 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006406# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else if (IsWindow(parent->tearoff_handle))
6408 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006409# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
6412 void
6413gui_mch_show_popupmenu(vimmenu_T *menu)
6414{
6415 POINT mp;
6416
6417 (void)GetCursorPos((LPPOINT)&mp);
6418 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6419}
6420
6421 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006422gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
6424 vimmenu_T *menu = gui_find_menu(path_name);
6425
6426 if (menu != NULL)
6427 {
6428 POINT p;
6429
Bram Moolenaar734a8672019-12-02 22:49:38 +01006430 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006432 if (mouse_pos)
6433 {
6434 int mx, my;
6435
6436 gui_mch_getmouse(&mx, &my);
6437 p.x += mx;
6438 p.y += my;
6439 }
6440 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006442 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6444 }
6445 msg_scroll = FALSE;
6446 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6447 }
6448}
6449
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006450# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451/*
6452 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6453 * create it as a pseudo-"tearoff menu".
6454 */
6455 void
6456gui_make_tearoff(char_u *path_name)
6457{
6458 vimmenu_T *menu = gui_find_menu(path_name);
6459
Bram Moolenaar734a8672019-12-02 22:49:38 +01006460 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (menu != NULL)
6462 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6463}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006464# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466/*
6467 * Add a menu item to a menu
6468 */
6469 void
6470gui_mch_add_menu_item(
6471 vimmenu_T *menu,
6472 int idx)
6473{
6474 vimmenu_T *parent = menu->parent;
6475
6476 menu->id = s_menu_id++;
6477 menu->submenu_id = NULL;
6478
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006479# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6481 {
6482 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6483 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6484 }
6485 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006486# endif
6487# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (menu_is_toolbar(parent->name))
6489 {
6490 TBBUTTON newtb;
6491
Bram Moolenaara80faa82020-04-12 19:37:17 +02006492 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (menu_is_separator(menu->name))
6494 {
6495 newtb.iBitmap = 0;
6496 newtb.fsStyle = TBSTYLE_SEP;
6497 }
6498 else
6499 {
6500 newtb.iBitmap = get_toolbar_bitmap(menu);
6501 newtb.fsStyle = TBSTYLE_BUTTON;
6502 }
6503 newtb.idCommand = menu->id;
6504 newtb.fsState = TBSTATE_ENABLED;
6505 newtb.iString = 0;
6506 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6507 (LPARAM)&newtb);
6508 menu->submenu_id = (HMENU)-1;
6509 }
6510 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006513 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006515 wn = enc_to_utf16(menu->name, NULL);
6516 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006518 InsertMenuW(parent->submenu_id, (UINT)idx,
6519 (menu_is_separator(menu->name)
6520 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6521 (UINT)menu->id, wn);
6522 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006524# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 if (IsWindow(parent->tearoff_handle))
6526 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 }
6529}
6530
6531/*
6532 * Destroy the machine specific menu widget.
6533 */
6534 void
6535gui_mch_destroy_menu(vimmenu_T *menu)
6536{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006537# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 /*
6539 * is this a toolbar button?
6540 */
6541 if (menu->submenu_id == (HMENU)-1)
6542 {
6543 int iButton;
6544
6545 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6546 (WPARAM)menu->id, 0);
6547 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6548 }
6549 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
6552 if (menu->parent != NULL
6553 && menu_is_popup(menu->parent->dname)
6554 && menu->parent->submenu_id != NULL)
6555 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6556 else
6557 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6558 if (menu->submenu_id != NULL)
6559 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006560# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (IsWindow(menu->tearoff_handle))
6562 DestroyWindow(menu->tearoff_handle);
6563 if (menu->parent != NULL
6564 && menu->parent->children != NULL
6565 && IsWindow(menu->parent->tearoff_handle))
6566 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006567 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 menu->modes = 0;
6569 rebuild_tearoff(menu->parent);
6570 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573}
6574
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006575# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 static void
6577rebuild_tearoff(vimmenu_T *menu)
6578{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006579 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 char_u tbuf[128];
6581 RECT trect;
6582 RECT rct;
6583 RECT roct;
6584 int x, y;
6585
6586 HWND thwnd = menu->tearoff_handle;
6587
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006588 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (GetWindowRect(thwnd, &trect)
6590 && GetWindowRect(s_hwnd, &rct)
6591 && GetClientRect(s_hwnd, &roct))
6592 {
6593 x = trect.left - rct.left;
6594 y = (trect.top - rct.bottom + roct.bottom);
6595 }
6596 else
6597 {
6598 x = y = 0xffffL;
6599 }
6600 DestroyWindow(thwnd);
6601 if (menu->children != NULL)
6602 {
6603 gui_mch_tearoff(tbuf, menu, x, y);
6604 if (IsWindow(menu->tearoff_handle))
6605 (void) SetWindowPos(menu->tearoff_handle,
6606 NULL,
6607 (int)trect.left,
6608 (int)trect.top,
6609 0, 0,
6610 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6611 }
6612}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006613# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
6615/*
6616 * Make a menu either grey or not grey.
6617 */
6618 void
6619gui_mch_menu_grey(
6620 vimmenu_T *menu,
6621 int grey)
6622{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006623# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 /*
6625 * is this a toolbar button?
6626 */
6627 if (menu->submenu_id == (HMENU)-1)
6628 {
6629 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6630 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6631 }
6632 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006633# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006634 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6635 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006637# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6639 {
6640 WORD menuID;
6641 HWND menuHandle;
6642
6643 /*
6644 * A tearoff button has changed state.
6645 */
6646 if (menu->children == NULL)
6647 menuID = (WORD)(menu->id);
6648 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006649 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6651 if (menuHandle)
6652 EnableWindow(menuHandle, !grey);
6653
6654 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656}
6657
Bram Moolenaar734a8672019-12-02 22:49:38 +01006658#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
6660
Bram Moolenaar734a8672019-12-02 22:49:38 +01006661// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6664#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006665#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
6667#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6668/*
6669 * stuff for dialogs
6670 */
6671
6672/*
6673 * The callback routine used by all the dialogs. Very simple. First,
6674 * acknowledges the INITDIALOG message so that Windows knows to do standard
6675 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6676 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6677 * number.
6678 */
6679 static LRESULT CALLBACK
6680dialog_callback(
6681 HWND hwnd,
6682 UINT message,
6683 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006684 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 if (message == WM_INITDIALOG)
6687 {
6688 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006689 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 (void)SetFocus(hwnd);
6691 if (dialog_default_button > IDCANCEL)
6692 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006693 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006694 // We don't have a default, set focus on another element of the
6695 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006696 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 return FALSE;
6698 }
6699
6700 if (message == WM_COMMAND)
6701 {
6702 int button = LOWORD(wParam);
6703
Bram Moolenaar734a8672019-12-02 22:49:38 +01006704 // Don't end the dialog if something was selected that was
6705 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if (button >= DLG_NONBUTTON_CONTROL)
6707 return TRUE;
6708
Bram Moolenaar734a8672019-12-02 22:49:38 +01006709 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006711 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006712 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006713 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006714
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006715 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6716 p = utf16_to_enc(wp, NULL);
6717 vim_strncpy(s_textfield, p, IOSIZE);
6718 vim_free(p);
6719 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 /*
6723 * Need to check for IDOK because if the user just hits Return to
6724 * accept the default value, some reason this is what we get.
6725 */
6726 if (button == IDOK)
6727 {
6728 if (dialog_default_button > IDCANCEL)
6729 EndDialog(hwnd, dialog_default_button);
6730 }
6731 else
6732 EndDialog(hwnd, button - IDCANCEL);
6733 return TRUE;
6734 }
6735
6736 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6737 {
6738 EndDialog(hwnd, 0);
6739 return TRUE;
6740 }
6741 return FALSE;
6742}
6743
6744/*
6745 * Create a dialog dynamically from the parameter strings.
6746 * type = type of dialog (question, alert, etc.)
6747 * title = dialog title. may be NULL for default title.
6748 * message = text to display. Dialog sizes to accommodate it.
6749 * buttons = '\n' separated list of button captions, default first.
6750 * dfltbutton = number of default button.
6751 *
6752 * This routine returns 1 if the first button is pressed,
6753 * 2 for the second, etc.
6754 *
6755 * 0 indicates Esc was pressed.
6756 * -1 for unexpected error
6757 *
6758 * If stubbing out this fn, return 1.
6759 */
6760
Bram Moolenaar734a8672019-12-02 22:49:38 +01006761static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 "IDR_VIM",
6764 "IDR_VIM_ERROR",
6765 "IDR_VIM_ALERT",
6766 "IDR_VIM_INFO",
6767 "IDR_VIM_QUESTION"
6768};
6769
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 int
6771gui_mch_dialog(
6772 int type,
6773 char_u *title,
6774 char_u *message,
6775 char_u *buttons,
6776 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006777 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006778 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006781 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 int numButtons;
6783 int *buttonWidths, *buttonPositions;
6784 int buttonYpos;
6785 int nchar, i;
6786 DWORD lStyle;
6787 int dlgwidth = 0;
6788 int dlgheight;
6789 int editboxheight;
6790 int horizWidth = 0;
6791 int msgheight;
6792 char_u *pstart;
6793 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006794 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 char_u *tbuffer;
6796 RECT rect;
6797 HWND hwnd;
6798 HDC hdc;
6799 HFONT font, oldFont;
6800 TEXTMETRIC fontInfo;
6801 int fontHeight;
6802 int textWidth, minButtonWidth, messageWidth;
6803 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006804 int maxDialogHeight;
6805 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 int vertical;
6807 int dlgPaddingX;
6808 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006809# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006810 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006812# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006813 garray_T ga;
6814 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006816# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006817 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006818# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006819 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006820# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006821 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006822 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
Bram Moolenaar748bf032005-02-02 23:04:36 +00006825 if (s_hwnd == NULL)
6826 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827
6828 if ((type < 0) || (type > VIM_LAST_TYPE))
6829 type = 0;
6830
Bram Moolenaar734a8672019-12-02 22:49:38 +01006831 // allocate some memory for dialog template
6832 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006833 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006834 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835
6836 if (p == NULL)
6837 return -1;
6838
6839 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006840 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6842 * const.
6843 */
6844 tbuffer = vim_strsave(buttons);
6845 if (tbuffer == NULL)
6846 return -1;
6847
Bram Moolenaar734a8672019-12-02 22:49:38 +01006848 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
Bram Moolenaar734a8672019-12-02 22:49:38 +01006850 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 numButtons = 1;
6852 for (i = 0; tbuffer[i] != '\0'; i++)
6853 {
6854 if (tbuffer[i] == DLG_BUTTON_SEP)
6855 numButtons++;
6856 }
6857 if (dfltbutton >= numButtons)
6858 dfltbutton = -1;
6859
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006861 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (buttonWidths == NULL)
6863 return -1;
6864
Bram Moolenaar734a8672019-12-02 22:49:38 +01006865 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006866 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (buttonPositions == NULL)
6868 return -1;
6869
6870 /*
6871 * Calculate how big the dialog must be.
6872 */
6873 hwnd = GetDesktopWindow();
6874 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006875# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6877 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006878 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 use_lfSysmenu = TRUE;
6880 }
6881 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6884 VARIABLE_PITCH , DLG_FONT_NAME);
6885 if (s_usenewlook)
6886 {
6887 oldFont = SelectFont(hdc, font);
6888 dlgPaddingX = DLG_PADDING_X;
6889 dlgPaddingY = DLG_PADDING_Y;
6890 }
6891 else
6892 {
6893 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6894 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6895 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6896 }
6897 GetTextMetrics(hdc, &fontInfo);
6898 fontHeight = fontInfo.tmHeight;
6899
Bram Moolenaar734a8672019-12-02 22:49:38 +01006900 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006901 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
Bram Moolenaar734a8672019-12-02 22:49:38 +01006903 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006904 if (s_hwnd == NULL)
6905 {
6906 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
Bram Moolenaar734a8672019-12-02 22:49:38 +01006908 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006909 get_work_area(&workarea_rect);
6910 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6911 if (maxDialogWidth > 600)
6912 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006913 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006914 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006915 }
6916 else
6917 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006918 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006919 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006920 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006921 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006922 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006923 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6924 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006925
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006926 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006927 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006928 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006929 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006930 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6931 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6932 }
6933
Bram Moolenaar734a8672019-12-02 22:49:38 +01006934 // Set dlgwidth to width of message.
6935 // Copy the message into "ga", changing NL to CR-NL and inserting line
6936 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 pstart = message;
6938 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006939 msgheight = 0;
6940 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 do
6942 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006943 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944
Bram Moolenaar734a8672019-12-02 22:49:38 +01006945 // Need to figure out where to break the string. The system does it
6946 // at a word boundary, which would mean we can't compute the number of
6947 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948 textWidth = 0;
6949 last_white = NULL;
6950 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006951 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006952 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006953 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006954 && textWidth > maxDialogWidth * 3 / 4)
6955 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006956 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006958 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006959 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006960 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006961 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006962 textWidth = 0;
6963
6964 if (last_white != NULL)
6965 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006966 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006967 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006968 pend = last_white + 1;
6969 last_white = NULL;
6970 }
6971 ga_append(&ga, '\r');
6972 ga_append(&ga, '\n');
6973 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006974 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006975
6976 while (--l >= 0)
6977 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006978 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006979 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006981
6982 ga_append(&ga, '\r');
6983 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 pstart = pend + 1;
6985 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006986
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006987 if (ga.ga_data != NULL)
6988 message = ga.ga_data;
6989
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006991
Bram Moolenaar734a8672019-12-02 22:49:38 +01006992 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02006993 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6994 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 if (msgheight < DLG_ICON_HEIGHT)
6997 msgheight = DLG_ICON_HEIGHT;
6998
6999 /*
7000 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007001 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007003 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (!vertical)
7005 {
7006 // Place buttons horizontally if they fit.
7007 horizWidth = dlgPaddingX;
7008 pstart = tbuffer;
7009 i = 0;
7010 do
7011 {
7012 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7013 if (pend == NULL)
7014 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007015 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (textWidth < minButtonWidth)
7017 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007018 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 buttonWidths[i] = textWidth;
7020 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007021 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 pstart = pend + 1;
7023 } while (*pend != NUL);
7024
7025 if (horizWidth > maxDialogWidth)
7026 vertical = TRUE; // Too wide to fit on the screen.
7027 else if (horizWidth > dlgwidth)
7028 dlgwidth = horizWidth;
7029 }
7030
7031 if (vertical)
7032 {
7033 // Stack buttons vertically.
7034 pstart = tbuffer;
7035 do
7036 {
7037 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7038 if (pend == NULL)
7039 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007040 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007041 textWidth += dlgPaddingX; // Padding within button
7042 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (textWidth > dlgwidth)
7044 dlgwidth = textWidth;
7045 pstart = pend + 1;
7046 } while (*pend != NUL);
7047 }
7048
7049 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007050 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar734a8672019-12-02 22:49:38 +01007052 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (s_usenewlook)
7054 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7055 else
7056 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7057
7058 add_long(lStyle);
7059 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007060 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 add_word(0); // NumberOfItems(will change later)
7062 add_word(10); // x
7063 add_word(10); // y
7064 add_word(PixelToDialogX(dlgwidth)); // cx
7065
7066 // Dialog height.
7067 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007068 dlgheight = msgheight + 2 * dlgPaddingY
7069 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else
7071 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7072
7073 // Dialog needs to be taller if contains an edit box.
7074 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7075 if (textfield != NULL)
7076 dlgheight += editboxheight;
7077
Bram Moolenaar734a8672019-12-02 22:49:38 +01007078 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007079 if (dlgheight > maxDialogHeight)
7080 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007081 msgheight = msgheight - (dlgheight - maxDialogHeight);
7082 dlgheight = maxDialogHeight;
7083 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007084 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007085 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007086 }
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 add_word(PixelToDialogY(dlgheight));
7089
7090 add_word(0); // Menu
7091 add_word(0); // Class
7092
Bram Moolenaar734a8672019-12-02 22:49:38 +01007093 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007094 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7095 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 p += nchar;
7097
7098 if (s_usenewlook)
7099 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007100 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007101# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 if (use_lfSysmenu)
7103 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007104 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7106 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007107 wcscpy(p, lfSysmenu.lfFaceName);
7108 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 }
7110 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
7113 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007114 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 }
7116 p += nchar;
7117 }
7118
7119 buttonYpos = msgheight + 2 * dlgPaddingY;
7120
7121 if (textfield != NULL)
7122 buttonYpos += editboxheight;
7123
7124 pstart = tbuffer;
7125 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007126 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 for (i = 0; i < numButtons; i++)
7128 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007129 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 for ( pend = pstart;
7131 *pend && (*pend != DLG_BUTTON_SEP);
7132 pend++)
7133 ;
7134
7135 if (*pend)
7136 *pend = '\0';
7137
7138 /*
7139 * old NOTE:
7140 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7141 * the focus to the first tab-able button and in so doing makes that
7142 * the default!! Grrr. Workaround: Make the default button the only
7143 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7144 * he/she can use arrow keys.
7145 *
7146 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007147 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 * dialog. Also needed for when the textfield is the default control.
7149 * It appears to work now (perhaps not on Win95?).
7150 */
7151 if (vertical)
7152 {
7153 p = add_dialog_element(p,
7154 (i == dfltbutton
7155 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7156 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007157 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 + 2 * fontHeight * i),
7159 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7160 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007161 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 }
7163 else
7164 {
7165 p = add_dialog_element(p,
7166 (i == dfltbutton
7167 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7168 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007169 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 PixelToDialogX(buttonWidths[i]),
7171 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007172 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007174 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 }
7176 *pnumitems += numButtons;
7177
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 p = add_dialog_element(p, SS_ICON,
7180 PixelToDialogX(dlgPaddingX),
7181 PixelToDialogY(dlgPaddingY),
7182 PixelToDialogX(DLG_ICON_WIDTH),
7183 PixelToDialogY(DLG_ICON_HEIGHT),
7184 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7185 dlg_icons[type]);
7186
Bram Moolenaar734a8672019-12-02 22:49:38 +01007187 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007188 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7189 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7190 PixelToDialogY(dlgPaddingY),
7191 (WORD)(PixelToDialogX(messageWidth) + 1),
7192 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007193 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
Bram Moolenaar734a8672019-12-02 22:49:38 +01007195 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 if (textfield != NULL)
7197 {
7198 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7199 PixelToDialogX(2 * dlgPaddingX),
7200 PixelToDialogY(2 * dlgPaddingY + msgheight),
7201 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7202 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007203 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 *pnumitems += 1;
7205 }
7206
7207 *pnumitems += 2;
7208
7209 SelectFont(hdc, oldFont);
7210 DeleteObject(font);
7211 ReleaseDC(hwnd, hdc);
7212
Bram Moolenaar734a8672019-12-02 22:49:38 +01007213 // Let the dialog_callback() function know which button to make default
7214 // If we have an edit box, make that the default. We also need to tell
7215 // dialog_callback() if this dialog contains an edit box or not. We do
7216 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 if (textfield != NULL)
7218 {
7219 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7220 s_textfield = textfield;
7221 }
7222 else
7223 {
7224 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7225 s_textfield = NULL;
7226 }
7227
Bram Moolenaar734a8672019-12-02 22:49:38 +01007228 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007230 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 (LPDLGTEMPLATE)pdlgtemplate,
7232 s_hwnd,
7233 (DLGPROC)dialog_callback);
7234
7235 LocalFree(LocalHandle(pdlgtemplate));
7236 vim_free(tbuffer);
7237 vim_free(buttonWidths);
7238 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007239 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
Bram Moolenaar734a8672019-12-02 22:49:38 +01007241 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 (void)SetFocus(s_hwnd);
7243
7244 return nchar;
7245}
7246
Bram Moolenaar734a8672019-12-02 22:49:38 +01007247#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249/*
7250 * Put a simple element (basic class) onto a dialog template in memory.
7251 * return a pointer to where the next item should be added.
7252 *
7253 * parameters:
7254 * lStyle = additional style flags
7255 * (be careful, NT3.51 & Win32s will ignore the new ones)
7256 * x,y = x & y positions IN DIALOG UNITS
7257 * w,h = width and height IN DIALOG UNITS
7258 * Id = ID used in messages
7259 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7260 * caption = usually text or resource name
7261 *
7262 * TODO: use the length information noted here to enable the dialog creation
7263 * routines to work out more exactly how much memory they need to alloc.
7264 */
7265 static PWORD
7266add_dialog_element(
7267 PWORD p,
7268 DWORD lStyle,
7269 WORD x,
7270 WORD y,
7271 WORD w,
7272 WORD h,
7273 WORD Id,
7274 WORD clss,
7275 const char *caption)
7276{
7277 int nchar;
7278
Bram Moolenaar734a8672019-12-02 22:49:38 +01007279 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7281 *p++ = LOWORD(lStyle);
7282 *p++ = HIWORD(lStyle);
7283 *p++ = 0; // LOWORD (lExtendedStyle)
7284 *p++ = 0; // HIWORD (lExtendedStyle)
7285 *p++ = x;
7286 *p++ = y;
7287 *p++ = w;
7288 *p++ = h;
7289 *p++ = Id; //9 or 10 words in all
7290
7291 *p++ = (WORD)0xffff;
7292 *p++ = clss; //2 more here
7293
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007294 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 p += nchar;
7296
7297 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7298
7299 return p; //total = 15+ (strlen(caption)) words
7300 // = 30 + 2(strlen(caption) bytes reqd
7301}
7302
7303
7304/*
7305 * Helper routine. Take an input pointer, return closest pointer that is
7306 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7307 */
7308 static LPWORD
7309lpwAlign(
7310 LPWORD lpIn)
7311{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007312 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007314 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 ul += 3;
7316 ul >>= 2;
7317 ul <<= 2;
7318 return (LPWORD)ul;
7319}
7320
7321/*
7322 * Helper routine. Takes second parameter as Ansi string, copies it to first
7323 * parameter as wide character (16-bits / char) string, and returns integer
7324 * number of wide characters (words) in string (including the trailing wide
7325 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007326 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7327 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 static int
7329nCopyAnsiToWideChar(
7330 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007331 LPSTR lpAnsiIn,
7332 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007335 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 int i;
7337 WCHAR *wn;
7338
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007339 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007342 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (wn != NULL)
7344 {
7345 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007346 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 vim_free(wn);
7348 }
7349 }
7350 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007351 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 nChar = MultiByteToWideChar(
7353 enc_codepage > 0 ? enc_codepage : CP_ACP,
7354 MB_PRECOMPOSED,
7355 lpAnsiIn, len,
7356 lpWCStr, len);
7357 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007358 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360
7361 return nChar;
7362}
7363
7364
7365#ifdef FEAT_TEAROFF
7366/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007367 * Lookup menu handle from "menu_id".
7368 */
7369 static HMENU
7370tearoff_lookup_menuhandle(
7371 vimmenu_T *menu,
7372 WORD menu_id)
7373{
7374 for ( ; menu != NULL; menu = menu->next)
7375 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007376 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007377 continue;
7378 if (menu_is_separator(menu->dname))
7379 continue;
7380 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7381 return menu->submenu_id;
7382 }
7383 return NULL;
7384}
7385
7386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 * The callback function for all the modeless dialogs that make up the
7388 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7389 * thinking its menus have been clicked), and go away when closed.
7390 */
7391 static LRESULT CALLBACK
7392tearoff_callback(
7393 HWND hwnd,
7394 UINT message,
7395 WPARAM wParam,
7396 LPARAM lParam)
7397{
7398 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007399 {
7400 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
Bram Moolenaar734a8672019-12-02 22:49:38 +01007404 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 HandleMouseHide(message, lParam);
7406
7407 if (message == WM_COMMAND)
7408 {
7409 if ((WORD)(LOWORD(wParam)) & 0x8000)
7410 {
7411 POINT mp;
7412 RECT rect;
7413
7414 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7415 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007416 vimmenu_T *menu;
7417
7418 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007420 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7422 (int)rect.right - 8,
7423 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 s_hwnd,
7426 NULL);
7427 /*
7428 * NOTE: The pop-up menu can eat the mouse up event.
7429 * We deal with this in normal.c.
7430 */
7431 }
7432 }
7433 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007434 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7436 /*
7437 * Give main window the focus back: this is so after
7438 * choosing a tearoff button you can start typing again
7439 * straight away.
7440 */
7441 (void)SetFocus(s_hwnd);
7442 return TRUE;
7443 }
7444 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7445 {
7446 DestroyWindow(hwnd);
7447 return TRUE;
7448 }
7449
Bram Moolenaar734a8672019-12-02 22:49:38 +01007450 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 if (message == WM_EXITSIZEMOVE)
7452 (void)SetActiveWindow(s_hwnd);
7453
7454 return FALSE;
7455}
7456#endif
7457
7458
7459/*
7460 * Decide whether to use the "new look" (small, non-bold font) or the "old
7461 * look" (big, clanky font) for dialogs, and work out a few values for use
7462 * later accordingly.
7463 */
7464 static void
7465get_dialog_font_metrics(void)
7466{
7467 HDC hdc;
7468 HFONT hfontTools = 0;
7469 DWORD dlgFontSize;
7470 SIZE size;
7471#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007472 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473#endif
7474
7475 s_usenewlook = FALSE;
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007478 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007479 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481#endif
7482 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7483 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7484
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007485 if (hfontTools)
7486 {
7487 hdc = GetDC(s_hwnd);
7488 SelectObject(hdc, hfontTools);
7489 /*
7490 * GetTextMetrics() doesn't return the right value in
7491 * tmAveCharWidth, so we have to figure out the dialog base units
7492 * ourselves.
7493 */
7494 GetTextExtentPoint(hdc,
7495 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7496 52, &size);
7497 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007499 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7500 s_dlgfntheight = (WORD)size.cy;
7501 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503
7504 if (!s_usenewlook)
7505 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007506 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 s_dlgfntwidth = LOWORD(dlgFontSize);
7508 s_dlgfntheight = HIWORD(dlgFontSize);
7509 }
7510}
7511
7512#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7513/*
7514 * Create a pseudo-"tearoff menu" based on the child
7515 * items of a given menu pointer.
7516 */
7517 static void
7518gui_mch_tearoff(
7519 char_u *title,
7520 vimmenu_T *menu,
7521 int initX,
7522 int initY)
7523{
7524 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7525 int template_len;
7526 int nchar, textWidth, submenuWidth;
7527 DWORD lStyle;
7528 DWORD lExtendedStyle;
7529 WORD dlgwidth;
7530 WORD menuID;
7531 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007532 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 vimmenu_T *the_menu = menu;
7534 HWND hwnd;
7535 HDC hdc;
7536 HFONT font, oldFont;
7537 int col, spaceWidth, len;
7538 int columnWidths[2];
7539 char_u *label, *text;
7540 int acLen = 0;
7541 int nameLen;
7542 int padding0, padding1, padding2 = 0;
7543 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007544 int x;
7545 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007546# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007547 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007549# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550
7551 /*
7552 * If this menu is already torn off, move it to the mouse position.
7553 */
7554 if (IsWindow(menu->tearoff_handle))
7555 {
7556 POINT mp;
7557 if (GetCursorPos((LPPOINT)&mp))
7558 {
7559 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7560 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7561 }
7562 return;
7563 }
7564
7565 /*
7566 * Create a new tearoff.
7567 */
7568 if (*title == MNU_HIDDEN_CHAR)
7569 title++;
7570
Bram Moolenaar734a8672019-12-02 22:49:38 +01007571 // Allocate memory to store the dialog template. It's made bigger when
7572 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 template_len = DLG_ALLOC_SIZE;
7574 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7575 if (p == NULL)
7576 return;
7577
7578 hwnd = GetDesktopWindow();
7579 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007580# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7582 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007583 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 use_lfSysmenu = TRUE;
7585 }
7586 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7589 VARIABLE_PITCH , DLG_FONT_NAME);
7590 if (s_usenewlook)
7591 oldFont = SelectFont(hdc, font);
7592 else
7593 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7594
Bram Moolenaar734a8672019-12-02 22:49:38 +01007595 // Calculate width of a single space. Used for padding columns to the
7596 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007597 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598
Bram Moolenaar734a8672019-12-02 22:49:38 +01007599 // Figure out max width of the text column, the accelerator column and the
7600 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 submenuWidth = 0;
7602 for (col = 0; col < 2; col++)
7603 {
7604 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007605 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007607 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 text = (col == 0) ? pmenu->dname : pmenu->actext;
7609 if (text != NULL && *text != NUL)
7610 {
7611 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7612 if (textWidth > columnWidths[col])
7613 columnWidths[col] = textWidth;
7614 }
7615 if (pmenu->children != NULL)
7616 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7617 }
7618 }
7619 if (columnWidths[1] == 0)
7620 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007621 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (submenuWidth != 0)
7623 columnWidths[0] += submenuWidth;
7624 else
7625 columnWidths[0] += spaceWidth;
7626 }
7627 else
7628 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007629 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7631 columnWidths[1] += submenuWidth;
7632 }
7633
7634 /*
7635 * Now find the total width of our 'menu'.
7636 */
7637 textWidth = columnWidths[0] + columnWidths[1];
7638 if (submenuWidth != 0)
7639 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007640 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7642 textWidth += submenuWidth;
7643 }
7644 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7645 if (textWidth > dlgwidth)
7646 dlgwidth = textWidth;
7647 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7648
Bram Moolenaar734a8672019-12-02 22:49:38 +01007649 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (s_usenewlook)
7651 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7652 else
7653 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7654
7655 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7656 *p++ = LOWORD(lStyle);
7657 *p++ = HIWORD(lStyle);
7658 *p++ = LOWORD(lExtendedStyle);
7659 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007660 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007662 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007664 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 else
7666 *p++ = PixelToDialogX(initX); // x
7667 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007668 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 else
7670 *p++ = PixelToDialogY(initY); // y
7671 *p++ = PixelToDialogX(dlgwidth); // cx
7672 ptrueheight = p;
7673 *p++ = 0; // dialog height: changed later anyway
7674 *p++ = 0; // Menu
7675 *p++ = 0; // Class
7676
Bram Moolenaar734a8672019-12-02 22:49:38 +01007677 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007679 ? (LPSTR)title
7680 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 p += nchar;
7682
7683 if (s_usenewlook)
7684 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007685 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007686# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (use_lfSysmenu)
7688 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007689 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7691 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007692 wcscpy(p, lfSysmenu.lfFaceName);
7693 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 }
7695 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {
7698 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007699 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 p += nchar;
7702 }
7703
7704 /*
7705 * Loop over all the items in the menu.
7706 * But skip over the tearbar.
7707 */
7708 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7709 menu = menu->children->next;
7710 else
7711 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007712 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 for ( ; menu != NULL; menu = menu->next)
7714 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007715 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 continue;
7717 if (menu_is_separator(menu->dname))
7718 {
7719 sepPadding += 3;
7720 continue;
7721 }
7722
Bram Moolenaar734a8672019-12-02 22:49:38 +01007723 // Check if there still is plenty of room in the template. Make it
7724 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7726 {
7727 WORD *newp;
7728
7729 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7730 if (newp != NULL)
7731 {
7732 template_len += 4096;
7733 mch_memmove(newp, pdlgtemplate,
7734 (char *)p - (char *)pdlgtemplate);
7735 p = newp + (p - pdlgtemplate);
7736 pnumitems = newp + (pnumitems - pdlgtemplate);
7737 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7738 LocalFree(LocalHandle(pdlgtemplate));
7739 pdlgtemplate = newp;
7740 }
7741 }
7742
Bram Moolenaar734a8672019-12-02 22:49:38 +01007743 // Figure out minimal length of this menu label. Use "name" for the
7744 // actual text, "dname" for estimating the displayed size. "name"
7745 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 len = nameLen = (int)STRLEN(menu->name);
7747 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7748 (int)STRLEN(menu->dname))) / spaceWidth;
7749 len += padding0;
7750
7751 if (menu->actext != NULL)
7752 {
7753 acLen = (int)STRLEN(menu->actext);
7754 len += acLen;
7755 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7756 }
7757 else
7758 textWidth = 0;
7759 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7760 len += padding1;
7761
7762 if (menu->children == NULL)
7763 {
7764 padding2 = submenuWidth / spaceWidth;
7765 len += padding2;
7766 menuID = (WORD)(menu->id);
7767 }
7768 else
7769 {
7770 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007771 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773
Bram Moolenaar734a8672019-12-02 22:49:38 +01007774 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007775 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (label == NULL)
7777 break;
7778
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007779 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007780 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007782 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 while (padding0-- > 0)
7784 *text++ = ' ';
7785 if (menu->actext != NULL)
7786 {
7787 STRNCPY(text, menu->actext, acLen);
7788 text += acLen;
7789 }
7790 while (padding1-- > 0)
7791 *text++ = ' ';
7792 if (menu->children != NULL)
7793 {
7794 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7795 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7796 }
7797 else
7798 {
7799 while (padding2-- > 0)
7800 *text++ = ' ';
7801 }
7802 *text = NUL;
7803
7804 /*
7805 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7806 * W95/NT4 it makes the tear-off look more like a menu.
7807 */
7808 p = add_dialog_element(p,
7809 BS_PUSHBUTTON|BS_LEFT,
7810 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7811 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7812 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7813 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007814 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 vim_free(label);
7816 (*pnumitems)++;
7817 }
7818
7819 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7820
7821
Bram Moolenaar734a8672019-12-02 22:49:38 +01007822 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007823 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007824 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 (LPDLGTEMPLATE)pdlgtemplate,
7826 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007827 (DLGPROC)tearoff_callback,
7828 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 LocalFree(LocalHandle(pdlgtemplate));
7831 SelectFont(hdc, oldFont);
7832 DeleteObject(font);
7833 ReleaseDC(hwnd, hdc);
7834
7835 /*
7836 * Reassert ourselves as the active window. This is so that after creating
7837 * a tearoff, the user doesn't have to click with the mouse just to start
7838 * typing again!
7839 */
7840 (void)SetActiveWindow(s_hwnd);
7841
Bram Moolenaar734a8672019-12-02 22:49:38 +01007842 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 force_menu_update = TRUE;
7844}
7845#endif
7846
7847#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007848# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
Bram Moolenaar734a8672019-12-02 22:49:38 +01007850// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851# ifndef TBSTYLE_FLAT
7852# define TBSTYLE_FLAT 0x0800
7853# endif
7854
7855/*
7856 * Create the toolbar, initially unpopulated.
7857 * (just like the menu, there are no defaults, it's all
7858 * set up through menu.vim)
7859 */
7860 static void
7861initialise_toolbar(void)
7862{
7863 InitCommonControls();
7864 s_toolbarhwnd = CreateToolbarEx(
7865 s_hwnd,
7866 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7867 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007868 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007869 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 IDR_TOOLBAR1, // id of initial bitmap
7871 NULL,
7872 0, // initial number of buttons
7873 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7874 TOOLBAR_BUTTON_HEIGHT,
7875 TOOLBAR_BUTTON_WIDTH,
7876 TOOLBAR_BUTTON_HEIGHT,
7877 sizeof(TBBUTTON)
7878 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007879
7880 // Remove transparency from the toolbar to prevent the main window
7881 // background colour showing through
7882 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7883 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7884
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007885 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886
7887 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7888}
7889
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007890 static LRESULT CALLBACK
7891toolbar_wndproc(
7892 HWND hwnd,
7893 UINT uMsg,
7894 WPARAM wParam,
7895 LPARAM lParam)
7896{
7897 HandleMouseHide(uMsg, lParam);
7898 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7899}
7900
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 static int
7902get_toolbar_bitmap(vimmenu_T *menu)
7903{
7904 int i = -1;
7905
7906 /*
7907 * Check user bitmaps first, unless builtin is specified.
7908 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007909 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
7911 char_u fname[MAXPATHL];
7912 HANDLE hbitmap = NULL;
7913
7914 if (menu->iconfile != NULL)
7915 {
7916 gui_find_iconfile(menu->iconfile, fname, "bmp");
7917 hbitmap = LoadImage(
7918 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007919 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 IMAGE_BITMAP,
7921 TOOLBAR_BUTTON_WIDTH,
7922 TOOLBAR_BUTTON_HEIGHT,
7923 LR_LOADFROMFILE |
7924 LR_LOADMAP3DCOLORS
7925 );
7926 }
7927
7928 /*
7929 * If the LoadImage call failed, or the "icon=" file
7930 * didn't exist or wasn't specified, try the menu name
7931 */
7932 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007933 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007934# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007935 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007936# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007937 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 hbitmap = LoadImage(
7939 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007940 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 IMAGE_BITMAP,
7942 TOOLBAR_BUTTON_WIDTH,
7943 TOOLBAR_BUTTON_HEIGHT,
7944 LR_LOADFROMFILE |
7945 LR_LOADMAP3DCOLORS
7946 );
7947
7948 if (hbitmap != NULL)
7949 {
7950 TBADDBITMAP tbAddBitmap;
7951
7952 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007953 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7956 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007957 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 }
7959 }
7960 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7961 i = menu->iconidx;
7962
7963 return i;
7964}
7965#endif
7966
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007967#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7968 static void
7969initialise_tabline(void)
7970{
7971 InitCommonControls();
7972
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007973 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007974 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007975 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007976 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007977 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007978
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007979 gui.tabline_height = TABLINE_HEIGHT;
7980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007982 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007983# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007984}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007985
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007986/*
7987 * Get tabpage_T from POINT.
7988 */
7989 static tabpage_T *
7990GetTabFromPoint(
7991 HWND hWnd,
7992 POINT pt)
7993{
7994 tabpage_T *ptp = NULL;
7995
7996 if (gui_mch_showing_tabline())
7997 {
7998 TCHITTESTINFO htinfo;
7999 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008000 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008001 if (s_tabhwnd == hWnd)
8002 {
8003 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8004 if (idx != -1)
8005 ptp = find_tabpage(idx + 1);
8006 }
8007 }
8008 return ptp;
8009}
8010
8011static POINT s_pt = {0, 0};
8012static HCURSOR s_hCursor = NULL;
8013
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008014 static LRESULT CALLBACK
8015tabline_wndproc(
8016 HWND hwnd,
8017 UINT uMsg,
8018 WPARAM wParam,
8019 LPARAM lParam)
8020{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008021 POINT pt;
8022 tabpage_T *tp;
8023 RECT rect;
8024 int nCenter;
8025 int idx0;
8026 int idx1;
8027
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008028 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008029
8030 switch (uMsg)
8031 {
8032 case WM_LBUTTONDOWN:
8033 {
8034 s_pt.x = GET_X_LPARAM(lParam);
8035 s_pt.y = GET_Y_LPARAM(lParam);
8036 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008037 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008038 break;
8039 }
8040 case WM_MOUSEMOVE:
8041 if (GetCapture() == hwnd
8042 && ((wParam & MK_LBUTTON)) != 0)
8043 {
8044 pt.x = GET_X_LPARAM(lParam);
8045 pt.y = s_pt.y;
8046 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8047 {
8048 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8049
8050 tp = GetTabFromPoint(hwnd, pt);
8051 if (tp != NULL)
8052 {
8053 idx0 = tabpage_index(curtab) - 1;
8054 idx1 = tabpage_index(tp) - 1;
8055
8056 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8057 nCenter = rect.left + (rect.right - rect.left) / 2;
8058
Bram Moolenaar734a8672019-12-02 22:49:38 +01008059 // Check if the mouse cursor goes over the center of
8060 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008061 if ((idx0 < idx1) && (nCenter < pt.x))
8062 {
8063 tabpage_move(idx1 + 1);
8064 update_screen(0);
8065 }
8066 else if ((idx1 < idx0) && (pt.x < nCenter))
8067 {
8068 tabpage_move(idx1);
8069 update_screen(0);
8070 }
8071 }
8072 }
8073 }
8074 break;
8075 case WM_LBUTTONUP:
8076 {
8077 if (GetCapture() == hwnd)
8078 {
8079 SetCursor(s_hCursor);
8080 ReleaseCapture();
8081 }
8082 break;
8083 }
8084 default:
8085 break;
8086 }
8087
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008088 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8089}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008090#endif
8091
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8093/*
8094 * Make the GUI window come to the foreground.
8095 */
8096 void
8097gui_mch_set_foreground(void)
8098{
8099 if (IsIconic(s_hwnd))
8100 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8101 SetForegroundWindow(s_hwnd);
8102}
8103#endif
8104
8105#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8106 static void
8107dyn_imm_load(void)
8108{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008109 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (hLibImm == NULL)
8111 return;
8112
8113 pImmGetCompositionStringA
8114 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8115 pImmGetCompositionStringW
8116 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8117 pImmGetContext
8118 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8119 pImmAssociateContext
8120 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8121 pImmReleaseContext
8122 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8123 pImmGetOpenStatus
8124 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8125 pImmSetOpenStatus
8126 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008127 pImmGetCompositionFontW
8128 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8129 pImmSetCompositionFontW
8130 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 pImmSetCompositionWindow
8132 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8133 pImmGetConversionStatus
8134 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008135 pImmSetConversionStatus
8136 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
8138 if ( pImmGetCompositionStringA == NULL
8139 || pImmGetCompositionStringW == NULL
8140 || pImmGetContext == NULL
8141 || pImmAssociateContext == NULL
8142 || pImmReleaseContext == NULL
8143 || pImmGetOpenStatus == NULL
8144 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008145 || pImmGetCompositionFontW == NULL
8146 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008148 || pImmGetConversionStatus == NULL
8149 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 FreeLibrary(hLibImm);
8152 hLibImm = NULL;
8153 pImmGetContext = NULL;
8154 return;
8155 }
8156
8157 return;
8158}
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160#endif
8161
8162#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8163
8164# ifdef FEAT_XPM_W32
8165# define IMAGE_XPM 100
8166# endif
8167
8168typedef struct _signicon_t
8169{
8170 HANDLE hImage;
8171 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008172# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008173 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175} signicon_t;
8176
8177 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008178gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180 signicon_t *sign;
8181 int x, y, w, h;
8182
8183 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8184 return;
8185
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008186# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008187 if (IS_ENABLE_DIRECTX())
8188 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008189# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 x = TEXT_X(col);
8192 y = TEXT_Y(row);
8193 w = gui.char_width * 2;
8194 h = gui.char_height;
8195 switch (sign->uType)
8196 {
8197 case IMAGE_BITMAP:
8198 {
8199 HDC hdcMem;
8200 HBITMAP hbmpOld;
8201
8202 hdcMem = CreateCompatibleDC(s_hdc);
8203 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8204 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8205 SelectObject(hdcMem, hbmpOld);
8206 DeleteDC(hdcMem);
8207 }
8208 break;
8209 case IMAGE_ICON:
8210 case IMAGE_CURSOR:
8211 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8212 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008213# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 case IMAGE_XPM:
8215 {
8216 HDC hdcMem;
8217 HBITMAP hbmpOld;
8218
8219 hdcMem = CreateCompatibleDC(s_hdc);
8220 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008221 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8223
8224 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008225 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8227 SelectObject(hdcMem, hbmpOld);
8228 DeleteDC(hdcMem);
8229 }
8230 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
8233}
8234
8235 static void
8236close_signicon_image(signicon_t *sign)
8237{
8238 if (sign)
8239 switch (sign->uType)
8240 {
8241 case IMAGE_BITMAP:
8242 DeleteObject((HGDIOBJ)sign->hImage);
8243 break;
8244 case IMAGE_CURSOR:
8245 DestroyCursor((HCURSOR)sign->hImage);
8246 break;
8247 case IMAGE_ICON:
8248 DestroyIcon((HICON)sign->hImage);
8249 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008250# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 case IMAGE_XPM:
8252 DeleteObject((HBITMAP)sign->hImage);
8253 DeleteObject((HBITMAP)sign->hShape);
8254 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 }
8257}
8258
8259 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008260gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 signicon_t sign, *psign;
8263 char_u *ext;
8264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008266 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (ext > signfile)
8268 {
8269 int do_load = 1;
8270
8271 if (!STRICMP(ext, ".bmp"))
8272 sign.uType = IMAGE_BITMAP;
8273 else if (!STRICMP(ext, ".ico"))
8274 sign.uType = IMAGE_ICON;
8275 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8276 sign.uType = IMAGE_CURSOR;
8277 else
8278 do_load = 0;
8279
8280 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008281 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 gui.char_width * 2, gui.char_height,
8283 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008284# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 if (!STRICMP(ext, ".xpm"))
8286 {
8287 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008288 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8289 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008291# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 }
8293
8294 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008295 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 *psign = sign;
8297
8298 if (!psign)
8299 {
8300 if (sign.hImage)
8301 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304 return (void *)psign;
8305
8306}
8307
8308 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008309gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310{
8311 if (sign)
8312 {
8313 close_signicon_image((signicon_t *)sign);
8314 vim_free(sign);
8315 }
8316}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008319#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
Bram Moolenaar734a8672019-12-02 22:49:38 +01008321/*
8322 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008323 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008325 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8327 * to get current mouse position).
8328 *
8329 * Trying to use as more Windows services as possible, and as less
8330 * IE version as possible :)).
8331 *
8332 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8333 * BalloonEval struct.
8334 * 2) Enable/Disable simply create/kill BalloonEval Timer
8335 * 3) When there was enough inactivity, timer procedure posts
8336 * async request to debugger
8337 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8338 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008339 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 */
8341
Bram Moolenaar45360022005-07-21 21:08:21 +00008342/*
8343 * determine whether installed Common Controls support multiline tooltips
8344 * (i.e. their version is >= 4.70
8345 */
8346 int
8347multiline_balloon_available(void)
8348{
8349 HINSTANCE hDll;
8350 static char comctl_dll[] = "comctl32.dll";
8351 static int multiline_tip = MAYBE;
8352
8353 if (multiline_tip != MAYBE)
8354 return multiline_tip;
8355
8356 hDll = GetModuleHandle(comctl_dll);
8357 if (hDll != NULL)
8358 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008359 DLLGETVERSIONPROC pGetVer;
8360 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008361
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 if (pGetVer != NULL)
8363 {
8364 DLLVERSIONINFO dvi;
8365 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008366
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008367 ZeroMemory(&dvi, sizeof(dvi));
8368 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008369
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008370 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008371
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008372 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008373 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008374 || (dvi.dwMajorVersion == 4
8375 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008376 {
8377 multiline_tip = TRUE;
8378 return multiline_tip;
8379 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008380 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008381 else
8382 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008383 // there is chance we have ancient CommCtl 4.70
8384 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008385 DWORD dwHandle = 0;
8386 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8387 if (len > 0)
8388 {
8389 VS_FIXEDFILEINFO *ver;
8390 UINT vlen = 0;
8391 void *data = alloc(len);
8392
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008393 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008394 && GetFileVersionInfo(comctl_dll, 0, len, data)
8395 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8396 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008397 && HIWORD(ver->dwFileVersionMS) > 4)
8398 || ((HIWORD(ver->dwFileVersionMS) == 4
8399 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008400 {
8401 vim_free(data);
8402 multiline_tip = TRUE;
8403 return multiline_tip;
8404 }
8405 vim_free(data);
8406 }
8407 }
8408 }
8409 multiline_tip = FALSE;
8410 return multiline_tip;
8411}
8412
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008413 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008414make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008415{
8416 TOOLINFOW *pti;
8417 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418
8419 if (multiline_balloon_available() == TRUE)
8420 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8421 else
8422 ToolInfoSize = sizeof(TOOLINFOW);
8423
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008424 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008425 if (pti == NULL)
8426 return;
8427
8428 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8429 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8430 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008431 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008432
8433 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8434 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8435
8436 pti->cbSize = ToolInfoSize;
8437 pti->uFlags = TTF_SUBCLASS;
8438 pti->hwnd = beval->target;
8439 pti->hinst = 0; // Don't use string resources
8440 pti->uId = ID_BEVAL_TOOLTIP;
8441
8442 if (multiline_balloon_available() == TRUE)
8443 {
8444 RECT rect;
8445 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8446 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008447 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8448 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008449 // switch multiline tooltips on
8450 if (GetClientRect(s_textArea, &rect))
8451 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8452 (LPARAM)rect.right);
8453 }
8454 else
8455 {
8456 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008457 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8458 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008459 }
8460
8461 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8462 pti->rect.left = pt.x - 3;
8463 pti->rect.top = pt.y - 3;
8464 pti->rect.right = pt.x + 3;
8465 pti->rect.bottom = pt.y + 3;
8466
8467 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8468 // Make tooltip appear sooner.
8469 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8470 // I've performed some tests and it seems the longest possible life time
8471 // of tooltip is 30 seconds.
8472 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8473 /*
8474 * HACK: force tooltip to appear, because it'll not appear until
8475 * first mouse move. D*mn M$
8476 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8477 */
8478 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8479 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8480 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008481}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008484delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008486 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
8489 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008490BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008491 HWND hwnd UNUSED,
8492 UINT uMsg UNUSED,
8493 UINT_PTR idEvent UNUSED,
8494 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
8496 POINT pt;
8497 RECT rect;
8498
8499 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8500 return;
8501
8502 GetCursorPos(&pt);
8503 if (WindowFromPoint(pt) != s_textArea)
8504 return;
8505
8506 ScreenToClient(s_textArea, &pt);
8507 GetClientRect(s_textArea, &rect);
8508 if (!PtInRect(&rect, pt))
8509 return;
8510
8511 if (LastActivity > 0
8512 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8513 && (cur_beval->showState != ShS_PENDING
8514 || abs(cur_beval->x - pt.x) > 3
8515 || abs(cur_beval->y - pt.y) > 3))
8516 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008517 // Pointer resting in one place long enough, it's time to show
8518 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 cur_beval->showState = ShS_PENDING;
8520 cur_beval->x = pt.x;
8521 cur_beval->y = pt.y;
8522
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008523 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
8525 if (cur_beval->msgCB != NULL)
8526 (*cur_beval->msgCB)(cur_beval, 0);
8527 }
8528}
8529
8530 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008531gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008533 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008535 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536}
8537
8538 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008539gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008541 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 if (beval == NULL)
8543 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008544 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008545 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008546 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547}
8548
8549 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008550gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551{
8552 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008553
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008554 vim_free(beval->msg);
8555 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8556 if (beval->msg == NULL)
8557 {
8558 delete_tooltip(beval);
8559 beval->showState = ShS_NEUTRAL;
8560 return;
8561 }
8562
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008563 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (beval->showState == ShS_SHOWING)
8565 return;
8566 GetCursorPos(&pt);
8567 ScreenToClient(s_textArea, &pt);
8568
8569 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008571 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 gui_mch_disable_beval_area(cur_beval);
8573 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008574 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008576 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577}
8578
8579 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008580gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008581 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008582 char_u *mesg,
8583 void (*mesgCB)(BalloonEval *, int),
8584 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008586 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 BalloonEval *beval;
8588
8589 if (mesg != NULL && mesgCB != NULL)
8590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 return NULL;
8593 }
8594
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008595 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (beval != NULL)
8597 {
8598 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 beval->msg = mesg;
8602 beval->msgCB = mesgCB;
8603 beval->clientData = clientData;
8604
8605 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 cur_beval = beval;
8607
8608 if (p_beval)
8609 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 }
8611 return beval;
8612}
8613
8614 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008615Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008617 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 return;
8619
8620 if (cur_beval != NULL)
8621 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008622 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008624 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008625 // TRACE0("TTN_SHOW {{{");
8626 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008627 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008628 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008629 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 delete_tooltip(cur_beval);
8631 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008632 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633
8634 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008635 break;
8636 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008637 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008638 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008639 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8640 info->lpszText = (LPSTR)info->lParam;
8641 info->uFlags |= TTF_DI_SETITEM;
8642 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008643 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008644 case TTN_GETDISPINFOW:
8645 {
8646 // if we get here then we have new common controls
8647 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8648 info->lpszText = (LPWSTR)info->lParam;
8649 info->uFlags |= TTF_DI_SETITEM;
8650 }
8651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653 }
8654}
8655
8656 static void
8657TrackUserActivity(UINT uMsg)
8658{
8659 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8660 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8661 LastActivity = GetTickCount();
8662}
8663
8664 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008665gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008667# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008668 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008669# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008670 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 vim_free(beval);
8672}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008673#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
8675#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8676/*
8677 * We have multiple signs to draw at the same location. Draw the
8678 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8679 */
8680 void
8681netbeans_draw_multisign_indicator(int row)
8682{
8683 int i;
8684 int y;
8685 int x;
8686
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008687 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008688 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008689
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 x = 0;
8691 y = TEXT_Y(row);
8692
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008693# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008694 if (IS_ENABLE_DIRECTX())
8695 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008696# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008697
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 for (i = 0; i < gui.char_height - 3; i++)
8699 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8700
8701 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8702 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8703 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8704 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8705 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8706 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8707 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8708}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008709#endif