blob: 8d28148808e97fe89ff5d23367d4ddeddca47af7 [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
61 /* parse string as rendering options. */
62 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 Moolenaara338adc2018-01-31 20:51:47 +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)
129 return OK; /* only checking the syntax of the value */
130
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200131 /* Enable DirectX/DirectWrite */
132 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
182#include "version.h" /* used by dialog box routine for default title */
183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
187/* cproto fails on missing include files */
188#ifndef PROTO
189
190#ifndef __MINGW32__
191# include <shellapi.h>
192#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100193#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100194# include <commctrl.h>
195#endif
196#include <windowsx.h>
197
198#ifdef GLOBAL_IME
199# include "glbl_ime.h"
200#endif
201
202#endif /* PROTO */
203
204#ifdef FEAT_MENU
205# define MENUHINTS /* show menu hints in command line */
206#endif
207
208/* Some parameters for dialog boxes. All in pixels. */
209#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
213#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
214#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
223#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
224
225#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
226# 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
298static WORD s_dlgfntheight; /* height of the dialog font */
299static WORD s_dlgfntwidth; /* width of the dialog font */
300
301#ifdef FEAT_MENU
302static HMENU s_menuBar = NULL;
303#endif
304#ifdef FEAT_TEAROFF
305static void rebuild_tearoff(vimmenu_T *menu);
306static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
307#endif
308
309/* Flag that is set while processing a message that must not be interrupted by
310 * processing another message. */
311static int s_busy_processing = FALSE;
312
313static int destroying = FALSE; /* call DestroyWindow() ourselves */
314
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;
328static HBRUSH s_brush = NULL;
329
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
347static char_u *s_textfield; /* Used by dialogs to pass back strings */
348
349static int s_need_activate = FALSE;
350
351/* 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).
355 */
356static int allow_scrollbar = FALSE;
357
358#ifdef GLOBAL_IME
359# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
360#else
361# define MyTranslateMessage(x) TranslateMessage(x)
362#endif
363
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100364#if defined(FEAT_DIRECTX)
365 static int
366directx_enabled(void)
367{
368 if (s_dwc != NULL)
369 return 1;
370 else if (s_directx_load_attempted)
371 return 0;
372 /* load DirectX */
373 DWrite_Init();
374 s_directx_load_attempted = 1;
375 s_dwc = DWriteContext_Open();
376 directx_binddc();
377 return s_dwc != NULL ? 1 : 0;
378}
379
380 static void
381directx_binddc(void)
382{
383 if (s_textArea != NULL)
384 {
385 RECT rect;
386 GetClientRect(s_textArea, &rect);
387 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
388 }
389}
390#endif
391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200392/* use of WindowProc depends on Global IME */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100393#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100394
395extern int current_font_height; /* this is in os_mswin.c */
396
397static struct
398{
399 UINT key_sym;
400 char_u vim_code0;
401 char_u vim_code1;
402} special_keys[] =
403{
404 {VK_UP, 'k', 'u'},
405 {VK_DOWN, 'k', 'd'},
406 {VK_LEFT, 'k', 'l'},
407 {VK_RIGHT, 'k', 'r'},
408
409 {VK_F1, 'k', '1'},
410 {VK_F2, 'k', '2'},
411 {VK_F3, 'k', '3'},
412 {VK_F4, 'k', '4'},
413 {VK_F5, 'k', '5'},
414 {VK_F6, 'k', '6'},
415 {VK_F7, 'k', '7'},
416 {VK_F8, 'k', '8'},
417 {VK_F9, 'k', '9'},
418 {VK_F10, 'k', ';'},
419
420 {VK_F11, 'F', '1'},
421 {VK_F12, 'F', '2'},
422 {VK_F13, 'F', '3'},
423 {VK_F14, 'F', '4'},
424 {VK_F15, 'F', '5'},
425 {VK_F16, 'F', '6'},
426 {VK_F17, 'F', '7'},
427 {VK_F18, 'F', '8'},
428 {VK_F19, 'F', '9'},
429 {VK_F20, 'F', 'A'},
430
431 {VK_F21, 'F', 'B'},
432#ifdef FEAT_NETBEANS_INTG
433 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
434#endif
435 {VK_F22, 'F', 'C'},
436 {VK_F23, 'F', 'D'},
437 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
438
439 {VK_HELP, '%', '1'},
440 {VK_BACK, 'k', 'b'},
441 {VK_INSERT, 'k', 'I'},
442 {VK_DELETE, 'k', 'D'},
443 {VK_HOME, 'k', 'h'},
444 {VK_END, '@', '7'},
445 {VK_PRIOR, 'k', 'P'},
446 {VK_NEXT, 'k', 'N'},
447 {VK_PRINT, '%', '9'},
448 {VK_ADD, 'K', '6'},
449 {VK_SUBTRACT, 'K', '7'},
450 {VK_DIVIDE, 'K', '8'},
451 {VK_MULTIPLY, 'K', '9'},
452 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
453 {VK_DECIMAL, 'K', 'B'},
454
455 {VK_NUMPAD0, 'K', 'C'},
456 {VK_NUMPAD1, 'K', 'D'},
457 {VK_NUMPAD2, 'K', 'E'},
458 {VK_NUMPAD3, 'K', 'F'},
459 {VK_NUMPAD4, 'K', 'G'},
460 {VK_NUMPAD5, 'K', 'H'},
461 {VK_NUMPAD6, 'K', 'I'},
462 {VK_NUMPAD7, 'K', 'J'},
463 {VK_NUMPAD8, 'K', 'K'},
464 {VK_NUMPAD9, 'K', 'L'},
465
466 /* Keys that we want to be able to use any modifier with: */
467 {VK_SPACE, ' ', NUL},
468 {VK_TAB, TAB, NUL},
469 {VK_ESCAPE, ESC, NUL},
470 {NL, NL, NUL},
471 {CAR, CAR, NUL},
472
473 /* End of list marker: */
474 {0, 0, 0}
475};
476
477/* Local variables */
478static int s_button_pending = -1;
479
480/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
481 * so don't reset s_button_pending. */
482static int s_getting_focus = FALSE;
483
484static int s_x_pending;
485static int s_y_pending;
486static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200487static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100488static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200489static int dead_key = 0; // 0: no dead key, 1: dead key pressed
490static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
491 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100493#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494/* balloon-eval WM_NOTIFY_HANDLER */
495static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
496static void TrackUserActivity(UINT uMsg);
497#endif
498
499/*
500 * For control IME.
501 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100502 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100503 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100505/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
506static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100507#endif
508#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100509/* holds LOGFONTW for 'guifont' always. */
510static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511#endif
512
513#ifdef FEAT_MBYTE_IME
514static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
515#endif
516
517#if defined(FEAT_BROWSE)
518static char_u *convert_filter(char_u *s);
519#endif
520
521#ifdef DEBUG_PRINT_ERROR
522/*
523 * Print out the last Windows error message
524 */
525 static void
526print_windows_error(void)
527{
528 LPVOID lpMsgBuf;
529
530 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
531 NULL, GetLastError(),
532 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
533 (LPTSTR) &lpMsgBuf, 0, NULL);
534 TRACE1("Error: %s\n", lpMsgBuf);
535 LocalFree(lpMsgBuf);
536}
537#endif
538
539/*
540 * Cursor blink functions.
541 *
542 * This is a simple state machine:
543 * BLINK_NONE not blinking at all
544 * BLINK_OFF blinking, cursor is not shown
545 * BLINK_ON blinking, cursor is shown
546 */
547
548#define BLINK_NONE 0
549#define BLINK_OFF 1
550#define BLINK_ON 2
551
552static int blink_state = BLINK_NONE;
553static long_u blink_waittime = 700;
554static long_u blink_ontime = 400;
555static long_u blink_offtime = 250;
556static UINT blink_timer = 0;
557
Bram Moolenaar703a8042016-06-04 16:24:32 +0200558 int
559gui_mch_is_blinking(void)
560{
561 return blink_state != BLINK_NONE;
562}
563
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200564 int
565gui_mch_is_blink_off(void)
566{
567 return blink_state == BLINK_OFF;
568}
569
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100570 void
571gui_mch_set_blinking(long wait, long on, long off)
572{
573 blink_waittime = wait;
574 blink_ontime = on;
575 blink_offtime = off;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 static VOID CALLBACK
579_OnBlinkTimer(
580 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100581 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100583 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100584{
585 MSG msg;
586
587 /*
588 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
589 */
590
591 KillTimer(NULL, idEvent);
592
593 /* Eat spurious WM_TIMER messages */
594 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
595 ;
596
597 if (blink_state == BLINK_ON)
598 {
599 gui_undraw_cursor();
600 blink_state = BLINK_OFF;
601 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
602 (TIMERPROC)_OnBlinkTimer);
603 }
604 else
605 {
606 gui_update_cursor(TRUE, FALSE);
607 blink_state = BLINK_ON;
608 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100609 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100610 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100611 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612}
613
614 static void
615gui_mswin_rm_blink_timer(void)
616{
617 MSG msg;
618
619 if (blink_timer != 0)
620 {
621 KillTimer(NULL, blink_timer);
622 /* Eat spurious WM_TIMER messages */
623 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
624 ;
625 blink_timer = 0;
626 }
627}
628
629/*
630 * Stop the cursor blinking. Show the cursor if it wasn't shown.
631 */
632 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100634{
635 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100636 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100637 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100639 gui_mch_flush();
640 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 blink_state = BLINK_NONE;
642}
643
644/*
645 * Start the cursor blinking. If it was already blinking, this restarts the
646 * waiting time and shows the cursor.
647 */
648 void
649gui_mch_start_blink(void)
650{
651 gui_mswin_rm_blink_timer();
652
653 /* Only switch blinking on if none of the times is zero */
654 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
655 {
656 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
657 (TIMERPROC)_OnBlinkTimer);
658 blink_state = BLINK_ON;
659 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100660 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661 }
662}
663
664/*
665 * Call-back routines.
666 */
667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 static VOID CALLBACK
669_OnTimer(
670 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100671 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100673 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674{
675 MSG msg;
676
677 /*
678 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
679 */
680 KillTimer(NULL, idEvent);
681 s_timed_out = TRUE;
682
683 /* Eat spurious WM_TIMER messages */
684 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
685 ;
686 if (idEvent == s_wait_timer)
687 s_wait_timer = 0;
688}
689
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 static void
691_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100692 HWND hwnd UNUSED,
693 UINT ch UNUSED,
694 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695{
696 dead_key = 1;
697}
698
699/*
700 * Convert Unicode character "ch" to bytes in "string[slen]".
701 * When "had_alt" is TRUE the ALT key was included in "ch".
702 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200703 * Because the Windows API uses UTF-16, we have to deal with surrogate
704 * pairs; this is where we choose to deal with them: if "ch" is a high
705 * surrogate, it will be stored, and the length returned will be zero; the next
706 * char_to_string call will then include the high surrogate, decoding the pair
707 * of UTF-16 code units to a single Unicode code point, presuming it is the
708 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 */
710 static int
711char_to_string(int ch, char_u *string, int slen, int had_alt)
712{
713 int len;
714 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200716 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200718 if (surrogate_pending_ch != 0)
719 {
720 /* We don't guarantee ch is a low surrogate to match the high surrogate
721 * we already have; it should be, but if it isn't, tough luck. */
722 wstring[0] = surrogate_pending_ch;
723 wstring[1] = ch;
724 surrogate_pending_ch = 0;
725 len = 2;
726 }
727 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
728 {
729 /* We don't have the entire code point yet, only the first UTF-16 code
730 * unit; so just remember it and use it in the next call. */
731 surrogate_pending_ch = ch;
732 return 0;
733 }
734 else
735 {
736 wstring[0] = ch;
737 len = 1;
738 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200739
740 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
741 * "enc_codepage" is non-zero use the standard Win32 function,
742 * otherwise use our own conversion function (e.g., for UTF-8). */
743 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200745 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
746 (LPSTR)string, slen, 0, NULL);
747 /* If we had included the ALT key into the character but now the
748 * upper bit is no longer set, that probably means the conversion
749 * failed. Convert the original character and set the upper bit
750 * afterwards. */
751 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200753 wstring[0] = ch & 0x7f;
754 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
755 (LPSTR)string, slen, 0, NULL);
756 if (len == 1) /* safety check */
757 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758 }
759 }
760 else
761 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200762 ws = utf16_to_enc(wstring, &len);
763 if (ws == NULL)
764 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 else
766 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767 if (len > slen) /* just in case */
768 len = slen;
769 mch_memmove(string, ws, len);
770 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773
774 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 {
776 string[0] = ch;
777 len = 1;
778 }
779
780 for (i = 0; i < len; ++i)
781 if (string[i] == CSI && len <= slen - 2)
782 {
783 /* Insert CSI as K_CSI. */
784 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
785 string[++i] = KS_EXTRA;
786 string[++i] = (int)KE_CSI;
787 len += 2;
788 }
789
790 return len;
791}
792
793/*
794 * Key hit, add it to the input buffer.
795 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 static void
797_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100798 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801{
802 char_u string[40];
803 int len = 0;
804
805 dead_key = 0;
806
807 len = char_to_string(ch, string, 40, FALSE);
808 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
809 {
810 trash_input_buf();
811 got_int = TRUE;
812 }
813
814 add_to_input_buf(string, len);
815}
816
817/*
818 * Alt-Key hit, add it to the input buffer.
819 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 static void
821_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100822 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100823 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825{
826 char_u string[40]; /* Enough for multibyte character */
827 int len;
828 int modifiers;
829 int ch = cch; /* special keys are negative */
830
831 dead_key = 0;
832
833 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
834
835 /* OK, we have a character key (given by ch) which was entered with the
836 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
837 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
838 * CAPSLOCK is pressed) at this point.
839 */
840 modifiers = MOD_MASK_ALT;
841 if (GetKeyState(VK_SHIFT) & 0x8000)
842 modifiers |= MOD_MASK_SHIFT;
843 if (GetKeyState(VK_CONTROL) & 0x8000)
844 modifiers |= MOD_MASK_CTRL;
845
846 ch = simplify_key(ch, &modifiers);
847 /* remove the SHIFT modifier for keys where it's already included, e.g.,
848 * '(' and '*' */
849 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
850 modifiers &= ~MOD_MASK_SHIFT;
851
852 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
853 ch = extract_modifiers(ch, &modifiers);
854 if (ch == CSI)
855 ch = K_CSI;
856
857 len = 0;
858 if (modifiers)
859 {
860 string[len++] = CSI;
861 string[len++] = KS_MODIFIER;
862 string[len++] = modifiers;
863 }
864
865 if (IS_SPECIAL((int)ch))
866 {
867 string[len++] = CSI;
868 string[len++] = K_SECOND((int)ch);
869 string[len++] = K_THIRD((int)ch);
870 }
871 else
872 {
873 /* Although the documentation isn't clear about it, we assume "ch" is
874 * a Unicode character. */
875 len += char_to_string(ch, string + len, 40 - len, TRUE);
876 }
877
878 add_to_input_buf(string, len);
879}
880
881 static void
882_OnMouseEvent(
883 int button,
884 int x,
885 int y,
886 int repeated_click,
887 UINT keyFlags)
888{
889 int vim_modifiers = 0x0;
890
891 s_getting_focus = FALSE;
892
893 if (keyFlags & MK_SHIFT)
894 vim_modifiers |= MOUSE_SHIFT;
895 if (keyFlags & MK_CONTROL)
896 vim_modifiers |= MOUSE_CTRL;
897 if (GetKeyState(VK_MENU) & 0x8000)
898 vim_modifiers |= MOUSE_ALT;
899
900 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
901}
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 static void
904_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905 HWND hwnd UNUSED,
906 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 int x,
908 int y,
909 UINT keyFlags)
910{
911 static LONG s_prevTime = 0;
912
913 LONG currentTime = GetMessageTime();
914 int button = -1;
915 int repeated_click;
916
917 /* Give main window the focus: this is so the cursor isn't hollow. */
918 (void)SetFocus(s_hwnd);
919
920 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
921 button = MOUSE_LEFT;
922 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
923 button = MOUSE_MIDDLE;
924 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
925 button = MOUSE_RIGHT;
926 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
927 {
928#ifndef GET_XBUTTON_WPARAM
929# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
930#endif
931 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
932 }
933 else if (s_uMsg == WM_CAPTURECHANGED)
934 {
935 /* on W95/NT4, somehow you get in here with an odd Msg
936 * if you press one button while holding down the other..*/
937 if (s_button_pending == MOUSE_LEFT)
938 button = MOUSE_RIGHT;
939 else
940 button = MOUSE_LEFT;
941 }
942 if (button >= 0)
943 {
944 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
945
946 /*
947 * Holding down the left and right buttons simulates pushing the middle
948 * button.
949 */
950 if (repeated_click
951 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
952 || (button == MOUSE_RIGHT
953 && s_button_pending == MOUSE_LEFT)))
954 {
955 /*
956 * Hmm, gui.c will ignore more than one button down at a time, so
957 * pretend we let go of it first.
958 */
959 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
960 button = MOUSE_MIDDLE;
961 repeated_click = FALSE;
962 s_button_pending = -1;
963 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
964 }
965 else if ((repeated_click)
966 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
967 {
968 if (s_button_pending > -1)
969 {
970 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
971 s_button_pending = -1;
972 }
973 /* TRACE("Button down at x %d, y %d\n", x, y); */
974 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
975 }
976 else
977 {
978 /*
979 * If this is the first press (i.e. not a multiple click) don't
980 * action immediately, but store and wait for:
981 * i) button-up
982 * ii) mouse move
983 * iii) another button press
984 * before using it.
985 * This enables us to make left+right simulate middle button,
986 * without left or right being actioned first. The side-effect is
987 * that if you click and hold the mouse without dragging, the
988 * cursor doesn't move until you release the button. In practice
989 * this is hardly a problem.
990 */
991 s_button_pending = button;
992 s_x_pending = x;
993 s_y_pending = y;
994 s_kFlags_pending = keyFlags;
995 }
996
997 s_prevTime = currentTime;
998 }
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001004 int x,
1005 int y,
1006 UINT keyFlags)
1007{
1008 int button;
1009
1010 s_getting_focus = FALSE;
1011 if (s_button_pending > -1)
1012 {
1013 /* Delayed action for mouse down event */
1014 _OnMouseEvent(s_button_pending, s_x_pending,
1015 s_y_pending, FALSE, s_kFlags_pending);
1016 s_button_pending = -1;
1017 }
1018 if (s_uMsg == WM_MOUSEMOVE)
1019 {
1020 /*
1021 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1022 * down.
1023 */
1024 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1025 | MK_XBUTTON1 | MK_XBUTTON2)))
1026 {
1027 gui_mouse_moved(x, y);
1028 return;
1029 }
1030
1031 /*
1032 * While button is down, keep grabbing mouse move events when
1033 * the mouse goes outside the window
1034 */
1035 SetCapture(s_textArea);
1036 button = MOUSE_DRAG;
1037 /* TRACE(" move at x %d, y %d\n", x, y); */
1038 }
1039 else
1040 {
1041 ReleaseCapture();
1042 button = MOUSE_RELEASE;
1043 /* TRACE(" up at x %d, y %d\n", x, y); */
1044 }
1045
1046 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1047}
1048
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001049 static void
1050_OnSizeTextArea(
1051 HWND hwnd UNUSED,
1052 UINT state UNUSED,
1053 int cx UNUSED,
1054 int cy UNUSED)
1055{
1056#if defined(FEAT_DIRECTX)
1057 if (IS_ENABLE_DIRECTX())
1058 directx_binddc();
1059#endif
1060}
1061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062#ifdef FEAT_MENU
1063/*
1064 * Find the vimmenu_T with the given id
1065 */
1066 static vimmenu_T *
1067gui_mswin_find_menu(
1068 vimmenu_T *pMenu,
1069 int id)
1070{
1071 vimmenu_T *pChildMenu;
1072
1073 while (pMenu)
1074 {
1075 if (pMenu->id == (UINT)id)
1076 break;
1077 if (pMenu->children != NULL)
1078 {
1079 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1080 if (pChildMenu)
1081 {
1082 pMenu = pChildMenu;
1083 break;
1084 }
1085 }
1086 pMenu = pMenu->next;
1087 }
1088 return pMenu;
1089}
1090
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091 static void
1092_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwndCtl UNUSED,
1096 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097{
1098 vimmenu_T *pMenu;
1099
1100 pMenu = gui_mswin_find_menu(root_menu, id);
1101 if (pMenu)
1102 gui_menu_cb(pMenu);
1103}
1104#endif
1105
1106#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1117 /* Give main window the focus back. */
1118 (void)SetFocus(s_hwnd);
1119
1120 if (s_findrep_struct.Flags & FR_FINDNEXT)
1121 {
1122 flags = FRD_FINDNEXT;
1123
1124 /* Give main window the focus back: this is so the cursor isn't
1125 * hollow. */
1126 (void)SetFocus(s_hwnd);
1127 }
1128 else if (s_findrep_struct.Flags & FR_REPLACE)
1129 {
1130 flags = FRD_REPLACE;
1131
1132 /* Give main window the focus back: this is so the cursor isn't
1133 * hollow. */
1134 (void)SetFocus(s_hwnd);
1135 }
1136 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1137 {
1138 flags = FRD_REPLACEALL;
1139 }
1140
1141 if (flags != 0)
1142 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001143 char_u *p, *q;
1144
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 /* Call the generic GUI function to do the actual work. */
1146 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1147 flags |= FRD_WHOLE_WORD;
1148 if (s_findrep_struct.Flags & FR_MATCHCASE)
1149 flags |= FRD_MATCH_CASE;
1150 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001151 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1152 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1153 if (p != NULL && q != NULL)
1154 gui_do_findrepl(flags, p, q, down);
1155 vim_free(p);
1156 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 }
1158}
1159#endif
1160
1161 static void
1162HandleMouseHide(UINT uMsg, LPARAM lParam)
1163{
1164 static LPARAM last_lParam = 0L;
1165
1166 /* We sometimes get a mousemove when the mouse didn't move... */
1167 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1168 {
1169 if (lParam == last_lParam)
1170 return;
1171 last_lParam = lParam;
1172 }
1173
1174 /* Handle specially, to centralise coding. We need to be sure we catch all
1175 * possible events which should cause us to restore the cursor (as it is a
1176 * shared resource, we take full responsibility for it).
1177 */
1178 switch (uMsg)
1179 {
1180 case WM_KEYUP:
1181 case WM_CHAR:
1182 /*
1183 * blank out the pointer if necessary
1184 */
1185 if (p_mh)
1186 gui_mch_mousehide(TRUE);
1187 break;
1188
1189 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1190 case WM_SYSCHAR:
1191 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1192 case WM_LBUTTONDOWN:
1193 case WM_LBUTTONUP:
1194 case WM_MBUTTONDOWN:
1195 case WM_MBUTTONUP:
1196 case WM_RBUTTONDOWN:
1197 case WM_RBUTTONUP:
1198 case WM_XBUTTONDOWN:
1199 case WM_XBUTTONUP:
1200 case WM_NCMOUSEMOVE:
1201 case WM_NCLBUTTONDOWN:
1202 case WM_NCLBUTTONUP:
1203 case WM_NCMBUTTONDOWN:
1204 case WM_NCMBUTTONUP:
1205 case WM_NCRBUTTONDOWN:
1206 case WM_NCRBUTTONUP:
1207 case WM_KILLFOCUS:
1208 /*
1209 * if the pointer is currently hidden, then we should show it.
1210 */
1211 gui_mch_mousehide(FALSE);
1212 break;
1213 }
1214}
1215
1216 static LRESULT CALLBACK
1217_TextAreaWndProc(
1218 HWND hwnd,
1219 UINT uMsg,
1220 WPARAM wParam,
1221 LPARAM lParam)
1222{
1223 /*
1224 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1225 hwnd, uMsg, wParam, lParam);
1226 */
1227
1228 HandleMouseHide(uMsg, lParam);
1229
1230 s_uMsg = uMsg;
1231 s_wParam = wParam;
1232 s_lParam = lParam;
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 TrackUserActivity(uMsg);
1236#endif
1237
1238 switch (uMsg)
1239 {
1240 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1248 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001254 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1258 return TRUE;
1259#endif
1260 default:
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263}
1264
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001267#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269 LRESULT WINAPI
1270vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1271{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001272#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001274#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001275 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001276#endif
1277}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278
1279/*
1280 * Called when the foreground or background color has been changed.
1281 */
1282 void
1283gui_mch_new_colors(void)
1284{
1285 /* nothing to do? */
1286}
1287
1288/*
1289 * Set the colors to their default values.
1290 */
1291 void
1292gui_mch_def_colors(void)
1293{
1294 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1295 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1296 gui.def_norm_pixel = gui.norm_pixel;
1297 gui.def_back_pixel = gui.back_pixel;
1298}
1299
1300/*
1301 * Open the GUI window which was created by a call to gui_mch_init().
1302 */
1303 int
1304gui_mch_open(void)
1305{
1306#ifndef SW_SHOWDEFAULT
1307# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1308#endif
1309 /* Actually open the window, if not already visible
1310 * (may be done already in gui_mch_set_shellsize) */
1311 if (!IsWindowVisible(s_hwnd))
1312 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1313
1314#ifdef MSWIN_FIND_REPLACE
1315 /* Init replace string here, so that we keep it when re-opening the
1316 * dialog. */
1317 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
1375 /* 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) */
1378 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
1398 /* 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... */
1401}
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
1415 void
1416gui_mch_create_scrollbar(
1417 scrollbar_T *sb,
1418 int orient) /* SBAR_VERT or SBAR_HORIZ */
1419{
1420 sb->id = CreateWindow(
1421 "SCROLLBAR", "Scrollbar",
1422 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1423 10, /* Any value will do for now */
1424 10, /* Any value will do for now */
1425 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001426 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001427}
1428
1429/*
1430 * Find the scrollbar with the given hwnd.
1431 */
1432 static scrollbar_T *
1433gui_mswin_find_scrollbar(HWND hwnd)
1434{
1435 win_T *wp;
1436
1437 if (gui.bottom_sbar.id == hwnd)
1438 return &gui.bottom_sbar;
1439 FOR_ALL_WINDOWS(wp)
1440 {
1441 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1442 return &wp->w_scrollbars[SBAR_LEFT];
1443 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1444 return &wp->w_scrollbars[SBAR_RIGHT];
1445 }
1446 return NULL;
1447}
1448
1449/*
1450 * Get the character size of a font.
1451 */
1452 static void
1453GetFontSize(GuiFont font)
1454{
1455 HWND hwnd = GetDesktopWindow();
1456 HDC hdc = GetWindowDC(hwnd);
1457 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001458 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001459 TEXTMETRIC tm;
1460
1461 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001462 // GetTextMetrics() may not return the right value in tmAveCharWidth
1463 // for some fonts. Do our own average computation.
1464 GetTextExtentPoint(hdc,
1465 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1466 52, &size);
1467 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001468
1469 gui.char_height = tm.tmHeight + p_linespace;
1470
1471 SelectFont(hdc, hfntOld);
1472
1473 ReleaseDC(hwnd, hdc);
1474}
1475
1476/*
1477 * Adjust gui.char_height (after 'linespace' was changed).
1478 */
1479 int
1480gui_mch_adjust_charheight(void)
1481{
1482 GetFontSize(gui.norm_font);
1483 return OK;
1484}
1485
1486 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001487get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001488{
1489 HFONT font = NULL;
1490
1491 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001492 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001493
1494 if (font == NULL)
1495 return NOFONT;
1496
1497 return (GuiFont)font;
1498}
1499
1500 static int
1501pixels_to_points(int pixels, int vertical)
1502{
1503 int points;
1504 HWND hwnd;
1505 HDC hdc;
1506
1507 hwnd = GetDesktopWindow();
1508 hdc = GetWindowDC(hwnd);
1509
1510 points = MulDiv(pixels, 72,
1511 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1512
1513 ReleaseDC(hwnd, hdc);
1514
1515 return points;
1516}
1517
1518 GuiFont
1519gui_mch_get_font(
1520 char_u *name,
1521 int giveErrorIfMissing)
1522{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001523 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001524 GuiFont font = NOFONT;
1525
1526 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1527 font = get_font_handle(&lf);
1528 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001529 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001530 return font;
1531}
1532
1533#if defined(FEAT_EVAL) || defined(PROTO)
1534/*
1535 * Return the name of font "font" in allocated memory.
1536 * Don't know how to get the actual name, thus use the provided name.
1537 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001539gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001540{
1541 if (name == NULL)
1542 return NULL;
1543 return vim_strsave(name);
1544}
1545#endif
1546
1547 void
1548gui_mch_free_font(GuiFont font)
1549{
1550 if (font)
1551 DeleteObject((HFONT)font);
1552}
1553
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554/*
1555 * Return the Pixel value (color) for the given color name.
1556 * Return INVALCOLOR for error.
1557 */
1558 guicolor_T
1559gui_mch_get_color(char_u *name)
1560{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001561 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562
1563 typedef struct SysColorTable
1564 {
1565 char *name;
1566 int color;
1567 } SysColorTable;
1568
1569 static SysColorTable sys_table[] =
1570 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001571 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1572 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001573#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001574 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1575#endif
1576 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1577 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1578 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1579 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1580 {"SYS_DESKTOP", COLOR_DESKTOP},
1581 {"SYS_INFOBK", COLOR_INFOBK},
1582 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1583 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584 {"SYS_BTNFACE", COLOR_BTNFACE},
1585 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1586 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1587 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1588 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1589 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1590 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1591 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1592 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1593 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1594 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1595 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1596 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1597 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1598 {"SYS_MENU", COLOR_MENU},
1599 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1600 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1601 {"SYS_WINDOW", COLOR_WINDOW},
1602 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1603 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1604 };
1605
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001606 /*
1607 * Try to look up a system colour.
1608 */
1609 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1610 if (STRICMP(name, sys_table[i].name) == 0)
1611 return GetSysColor(sys_table[i].color);
1612
Bram Moolenaarab302212016-04-26 20:59:29 +02001613 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001614}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001615
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001616 guicolor_T
1617gui_mch_get_rgb_color(int r, int g, int b)
1618{
1619 return gui_get_rgb_color_cmn(r, g, b);
1620}
1621
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622/*
1623 * Return OK if the key with the termcap name "name" is supported.
1624 */
1625 int
1626gui_mch_haskey(char_u *name)
1627{
1628 int i;
1629
1630 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1631 if (name[0] == special_keys[i].vim_code0 &&
1632 name[1] == special_keys[i].vim_code1)
1633 return OK;
1634 return FAIL;
1635}
1636
1637 void
1638gui_mch_beep(void)
1639{
1640 MessageBeep(MB_OK);
1641}
1642/*
1643 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1644 */
1645 void
1646gui_mch_invert_rectangle(
1647 int r,
1648 int c,
1649 int nr,
1650 int nc)
1651{
1652 RECT rc;
1653
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001654#if defined(FEAT_DIRECTX)
1655 if (IS_ENABLE_DIRECTX())
1656 DWriteContext_Flush(s_dwc);
1657#endif
1658
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001659 /*
1660 * Note: InvertRect() excludes right and bottom of rectangle.
1661 */
1662 rc.left = FILL_X(c);
1663 rc.top = FILL_Y(r);
1664 rc.right = rc.left + nc * gui.char_width;
1665 rc.bottom = rc.top + nr * gui.char_height;
1666 InvertRect(s_hdc, &rc);
1667}
1668
1669/*
1670 * Iconify the GUI window.
1671 */
1672 void
1673gui_mch_iconify(void)
1674{
1675 ShowWindow(s_hwnd, SW_MINIMIZE);
1676}
1677
1678/*
1679 * Draw a cursor without focus.
1680 */
1681 void
1682gui_mch_draw_hollow_cursor(guicolor_T color)
1683{
1684 HBRUSH hbr;
1685 RECT rc;
1686
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001687#if defined(FEAT_DIRECTX)
1688 if (IS_ENABLE_DIRECTX())
1689 DWriteContext_Flush(s_dwc);
1690#endif
1691
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 /*
1693 * Note: FrameRect() excludes right and bottom of rectangle.
1694 */
1695 rc.left = FILL_X(gui.col);
1696 rc.top = FILL_Y(gui.row);
1697 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001698 if (mb_lefthalve(gui.row, gui.col))
1699 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 rc.bottom = rc.top + gui.char_height;
1701 hbr = CreateSolidBrush(color);
1702 FrameRect(s_hdc, &rc, hbr);
1703 DeleteBrush(hbr);
1704}
1705/*
1706 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1707 * color "color".
1708 */
1709 void
1710gui_mch_draw_part_cursor(
1711 int w,
1712 int h,
1713 guicolor_T color)
1714{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001715 RECT rc;
1716
1717 /*
1718 * Note: FillRect() excludes right and bottom of rectangle.
1719 */
1720 rc.left =
1721#ifdef FEAT_RIGHTLEFT
1722 /* vertical line should be on the right of current point */
1723 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1724#endif
1725 FILL_X(gui.col);
1726 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1727 rc.right = rc.left + w;
1728 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001729
Bram Moolenaar92467d32017-12-05 13:22:16 +01001730 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001731}
1732
1733
1734/*
1735 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1736 * dead key's nominal character and re-post the original message.
1737 */
1738 static void
1739outputDeadKey_rePost(MSG originalMsg)
1740{
1741 static MSG deadCharExpel;
1742
1743 if (!dead_key)
1744 return;
1745
1746 dead_key = 0;
1747
1748 /* Make Windows generate the dead key's character */
1749 deadCharExpel.message = originalMsg.message;
1750 deadCharExpel.hwnd = originalMsg.hwnd;
1751 deadCharExpel.wParam = VK_SPACE;
1752
1753 MyTranslateMessage(&deadCharExpel);
1754
1755 /* re-generate the current character free of the dead char influence */
1756 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1757 originalMsg.lParam);
1758}
1759
1760
1761/*
1762 * Process a single Windows message.
1763 * If one is not available we hang until one is.
1764 */
1765 static void
1766process_message(void)
1767{
1768 MSG msg;
1769 UINT vk = 0; /* Virtual key */
1770 char_u string[40];
1771 int i;
1772 int modifiers = 0;
1773 int key;
1774#ifdef FEAT_MENU
1775 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1776#endif
1777
1778 pGetMessage(&msg, NULL, 0, 0);
1779
1780#ifdef FEAT_OLE
1781 /* Look after OLE Automation commands */
1782 if (msg.message == WM_OLE)
1783 {
1784 char_u *str = (char_u *)msg.lParam;
1785 if (str == NULL || *str == NUL)
1786 {
1787 /* Message can't be ours, forward it. Fixes problem with Ultramon
1788 * 3.0.4 */
1789 pDispatchMessage(&msg);
1790 }
1791 else
1792 {
1793 add_to_input_buf(str, (int)STRLEN(str));
1794 vim_free(str); /* was allocated in CVim::SendKeys() */
1795 }
1796 return;
1797 }
1798#endif
1799
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001800#ifdef MSWIN_FIND_REPLACE
1801 /* Don't process messages used by the dialog */
1802 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1803 {
1804 HandleMouseHide(msg.message, msg.lParam);
1805 return;
1806 }
1807#endif
1808
1809 /*
1810 * Check if it's a special key that we recognise. If not, call
1811 * TranslateMessage().
1812 */
1813 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1814 {
1815 vk = (int) msg.wParam;
1816
1817 /*
1818 * Handle dead keys in special conditions in other cases we let Windows
1819 * handle them and do not interfere.
1820 *
1821 * The dead_key flag must be reset on several occasions:
1822 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1823 * consumed at that point (This is when we let Windows combine the
1824 * dead character on its own)
1825 *
1826 * - Before doing something special such as regenerating keypresses to
1827 * expel the dead character as this could trigger an infinite loop if
1828 * for some reason MyTranslateMessage() do not trigger a call
1829 * immediately to _OnChar() (or _OnSysChar()).
1830 */
1831 if (dead_key)
1832 {
1833 /*
1834 * If a dead key was pressed and the user presses VK_SPACE,
1835 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1836 * with the dead char now, so do nothing special and let Windows
1837 * handle it.
1838 *
1839 * Note that VK_SPACE combines with the dead_key's character and
1840 * only one WM_CHAR will be generated by TranslateMessage(), in
1841 * the two other cases two WM_CHAR will be generated: the dead
1842 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1843 * user expects.
1844 */
1845 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1846 {
1847 dead_key = 0;
1848 MyTranslateMessage(&msg);
1849 return;
1850 }
1851 /* In modes where we are not typing, dead keys should behave
1852 * normally */
1853 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1854 {
1855 outputDeadKey_rePost(msg);
1856 return;
1857 }
1858 }
1859
1860 /* Check for CTRL-BREAK */
1861 if (vk == VK_CANCEL)
1862 {
1863 trash_input_buf();
1864 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001865 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001866 string[0] = Ctrl_C;
1867 add_to_input_buf(string, 1);
1868 }
1869
1870 for (i = 0; special_keys[i].key_sym != 0; i++)
1871 {
1872 /* ignore VK_SPACE when ALT key pressed: system menu */
1873 if (special_keys[i].key_sym == vk
1874 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1875 {
1876 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001877 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001878 * is a key that would normally trigger the dead key nominal
1879 * character output (such as a NUMPAD printable character or
1880 * the TAB key, etc...).
1881 */
1882 if (dead_key && (special_keys[i].vim_code0 == 'K'
1883 || vk == VK_TAB || vk == CAR))
1884 {
1885 outputDeadKey_rePost(msg);
1886 return;
1887 }
1888
1889#ifdef FEAT_MENU
1890 /* Check for <F10>: Windows selects the menu. When <F10> is
1891 * mapped we want to use the mapping instead. */
1892 if (vk == VK_F10
1893 && gui.menu_is_active
1894 && check_map(k10, State, FALSE, TRUE, FALSE,
1895 NULL, NULL) == NULL)
1896 break;
1897#endif
1898 if (GetKeyState(VK_SHIFT) & 0x8000)
1899 modifiers |= MOD_MASK_SHIFT;
1900 /*
1901 * Don't use caps-lock as shift, because these are special keys
1902 * being considered here, and we only want letters to get
1903 * shifted -- webb
1904 */
1905 /*
1906 if (GetKeyState(VK_CAPITAL) & 0x0001)
1907 modifiers ^= MOD_MASK_SHIFT;
1908 */
1909 if (GetKeyState(VK_CONTROL) & 0x8000)
1910 modifiers |= MOD_MASK_CTRL;
1911 if (GetKeyState(VK_MENU) & 0x8000)
1912 modifiers |= MOD_MASK_ALT;
1913
1914 if (special_keys[i].vim_code1 == NUL)
1915 key = special_keys[i].vim_code0;
1916 else
1917 key = TO_SPECIAL(special_keys[i].vim_code0,
1918 special_keys[i].vim_code1);
1919 key = simplify_key(key, &modifiers);
1920 if (key == CSI)
1921 key = K_CSI;
1922
1923 if (modifiers)
1924 {
1925 string[0] = CSI;
1926 string[1] = KS_MODIFIER;
1927 string[2] = modifiers;
1928 add_to_input_buf(string, 3);
1929 }
1930
1931 if (IS_SPECIAL(key))
1932 {
1933 string[0] = CSI;
1934 string[1] = K_SECOND(key);
1935 string[2] = K_THIRD(key);
1936 add_to_input_buf(string, 3);
1937 }
1938 else
1939 {
1940 int len;
1941
1942 /* Handle "key" as a Unicode character. */
1943 len = char_to_string(key, string, 40, FALSE);
1944 add_to_input_buf(string, len);
1945 }
1946 break;
1947 }
1948 }
1949 if (special_keys[i].key_sym == 0)
1950 {
1951 /* Some keys need C-S- where they should only need C-.
1952 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1953 * system startup (Helmut Stiegler, 2003 Oct 3). */
1954 if (vk != 0xff
1955 && (GetKeyState(VK_CONTROL) & 0x8000)
1956 && !(GetKeyState(VK_SHIFT) & 0x8000)
1957 && !(GetKeyState(VK_MENU) & 0x8000))
1958 {
1959 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1960 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1961 {
1962 string[0] = Ctrl_HAT;
1963 add_to_input_buf(string, 1);
1964 }
1965 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1966 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1967 {
1968 string[0] = Ctrl__;
1969 add_to_input_buf(string, 1);
1970 }
1971 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1972 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1973 {
1974 string[0] = Ctrl_AT;
1975 add_to_input_buf(string, 1);
1976 }
1977 else
1978 MyTranslateMessage(&msg);
1979 }
1980 else
1981 MyTranslateMessage(&msg);
1982 }
1983 }
1984#ifdef FEAT_MBYTE_IME
1985 else if (msg.message == WM_IME_NOTIFY)
1986 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1987 else if (msg.message == WM_KEYUP && im_get_status())
1988 /* added for non-MS IME (Yasuhiro Matsumoto) */
1989 MyTranslateMessage(&msg);
1990#endif
1991#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1992/* GIME_TEST */
1993 else if (msg.message == WM_IME_STARTCOMPOSITION)
1994 {
1995 POINT point;
1996
1997 global_ime_set_font(&norm_logfont);
1998 point.x = FILL_X(gui.col);
1999 point.y = FILL_Y(gui.row);
2000 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2001 global_ime_set_position(&point);
2002 }
2003#endif
2004
2005#ifdef FEAT_MENU
2006 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2007 * mapped we need to stop it here to avoid strange effects (e.g., for the
2008 * key-up event) */
2009 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2010 NULL, NULL) == NULL)
2011#endif
2012 pDispatchMessage(&msg);
2013}
2014
2015/*
2016 * Catch up with any queued events. This may put keyboard input into the
2017 * input buffer, call resize call-backs, trigger timers etc. If there is
2018 * nothing in the event queue (& no timers pending), then we return
2019 * immediately.
2020 */
2021 void
2022gui_mch_update(void)
2023{
2024 MSG msg;
2025
2026 if (!s_busy_processing)
2027 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2028 && !vim_is_input_buf_full())
2029 process_message();
2030}
2031
Bram Moolenaar4231da42016-06-02 14:30:04 +02002032 static void
2033remove_any_timer(void)
2034{
2035 MSG msg;
2036
2037 if (s_wait_timer != 0 && !s_timed_out)
2038 {
2039 KillTimer(NULL, s_wait_timer);
2040
2041 /* Eat spurious WM_TIMER messages */
2042 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2043 ;
2044 s_wait_timer = 0;
2045 }
2046}
2047
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002048/*
2049 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2050 * from the keyboard.
2051 * wtime == -1 Wait forever.
2052 * wtime == 0 This should never happen.
2053 * wtime > 0 Wait wtime milliseconds for a character.
2054 * Returns OK if a character was found to be available within the given time,
2055 * or FAIL otherwise.
2056 */
2057 int
2058gui_mch_wait_for_chars(int wtime)
2059{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002060 int focus;
2061
2062 s_timed_out = FALSE;
2063
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002064 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002066 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 if (s_busy_processing)
2068 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002069
2070 // When called with "wtime" zero, just want one msec.
2071 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002072 (TIMERPROC)_OnTimer);
2073 }
2074
2075 allow_scrollbar = TRUE;
2076
2077 focus = gui.in_focus;
2078 while (!s_timed_out)
2079 {
2080 /* Stop or start blinking when focus changes */
2081 if (gui.in_focus != focus)
2082 {
2083 if (gui.in_focus)
2084 gui_mch_start_blink();
2085 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002086 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 focus = gui.in_focus;
2088 }
2089
2090 if (s_need_activate)
2091 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002092 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093 s_need_activate = FALSE;
2094 }
2095
Bram Moolenaar4231da42016-06-02 14:30:04 +02002096#ifdef FEAT_TIMERS
2097 did_add_timer = FALSE;
2098#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002099#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002100 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002101 for (;;)
2102 {
2103 MSG msg;
2104
2105 parse_queued_messages();
2106
Bram Moolenaar62426e12017-08-13 15:37:58 +02002107 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2108 {
2109 process_message();
2110 break;
2111 }
2112 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2113 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002114 break;
2115 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002116#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002117 /*
2118 * Don't use gui_mch_update() because then we will spin-lock until a
2119 * char arrives, instead we use GetMessage() to hang until an
2120 * event arrives. No need to check for input_buf_full because we are
2121 * returning as soon as it contains a single char -- webb
2122 */
2123 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002124#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002125
2126 if (input_available())
2127 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002128 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002129 allow_scrollbar = FALSE;
2130
2131 /* Clear pending mouse button, the release event may have been
2132 * taken by the dialog window. But don't do this when getting
2133 * focus, we need the mouse-up event then. */
2134 if (!s_getting_focus)
2135 s_button_pending = -1;
2136
2137 return OK;
2138 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002139
2140#ifdef FEAT_TIMERS
2141 if (did_add_timer)
2142 {
2143 /* Need to recompute the waiting time. */
2144 remove_any_timer();
2145 break;
2146 }
2147#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 }
2149 allow_scrollbar = FALSE;
2150 return FAIL;
2151}
2152
2153/*
2154 * Clear a rectangular region of the screen from text pos (row1, col1) to
2155 * (row2, col2) inclusive.
2156 */
2157 void
2158gui_mch_clear_block(
2159 int row1,
2160 int col1,
2161 int row2,
2162 int col2)
2163{
2164 RECT rc;
2165
2166 /*
2167 * Clear one extra pixel at the far right, for when bold characters have
2168 * spilled over to the window border.
2169 * Note: FillRect() excludes right and bottom of rectangle.
2170 */
2171 rc.left = FILL_X(col1);
2172 rc.top = FILL_Y(row1);
2173 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2174 rc.bottom = FILL_Y(row2 + 1);
2175 clear_rect(&rc);
2176}
2177
2178/*
2179 * Clear the whole text window.
2180 */
2181 void
2182gui_mch_clear_all(void)
2183{
2184 RECT rc;
2185
2186 rc.left = 0;
2187 rc.top = 0;
2188 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2189 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2190 clear_rect(&rc);
2191}
2192/*
2193 * Menu stuff.
2194 */
2195
2196 void
2197gui_mch_enable_menu(int flag)
2198{
2199#ifdef FEAT_MENU
2200 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2201#endif
2202}
2203
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002204 void
2205gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002206 int x UNUSED,
2207 int y UNUSED,
2208 int w UNUSED,
2209 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002210{
2211 /* It will be in the right place anyway */
2212}
2213
2214#if defined(FEAT_MENU) || defined(PROTO)
2215/*
2216 * Make menu item hidden or not hidden
2217 */
2218 void
2219gui_mch_menu_hidden(
2220 vimmenu_T *menu,
2221 int hidden)
2222{
2223 /*
2224 * This doesn't do what we want. Hmm, just grey the menu items for now.
2225 */
2226 /*
2227 if (hidden)
2228 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2229 else
2230 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2231 */
2232 gui_mch_menu_grey(menu, hidden);
2233}
2234
2235/*
2236 * This is called after setting all the menus to grey/hidden or not.
2237 */
2238 void
2239gui_mch_draw_menubar(void)
2240{
2241 DrawMenuBar(s_hwnd);
2242}
2243#endif /*FEAT_MENU*/
2244
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002245/*
2246 * Return the RGB value of a pixel as a long.
2247 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002248 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249gui_mch_get_rgb(guicolor_T pixel)
2250{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002251 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2252 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002253}
2254
2255#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2256/* Convert pixels in X to dialog units */
2257 static WORD
2258PixelToDialogX(int numPixels)
2259{
2260 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2261}
2262
2263/* Convert pixels in Y to dialog units */
2264 static WORD
2265PixelToDialogY(int numPixels)
2266{
2267 return (WORD)((numPixels * 8) / s_dlgfntheight);
2268}
2269
2270/* Return the width in pixels of the given text in the given DC. */
2271 static int
2272GetTextWidth(HDC hdc, char_u *str, int len)
2273{
2274 SIZE size;
2275
2276 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2277 return size.cx;
2278}
2279
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002280/*
2281 * Return the width in pixels of the given text in the given DC, taking care
2282 * of 'encoding' to active codepage conversion.
2283 */
2284 static int
2285GetTextWidthEnc(HDC hdc, char_u *str, int len)
2286{
2287 SIZE size;
2288 WCHAR *wstr;
2289 int n;
2290 int wlen = len;
2291
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002292 wstr = enc_to_utf16(str, &wlen);
2293 if (wstr == NULL)
2294 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002296 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2297 vim_free(wstr);
2298 if (n)
2299 return size.cx;
2300 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002303static void get_work_area(RECT *spi_rect);
2304
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305/*
2306 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002307 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2308 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309 */
2310 static BOOL
2311CenterWindow(
2312 HWND hwndChild,
2313 HWND hwndParent)
2314{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002315 HMONITOR mon;
2316 MONITORINFO moninfo;
2317 RECT rChild, rParent, rScreen;
2318 int wChild, hChild, wParent, hParent;
2319 int xNew, yNew;
2320 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321
2322 GetWindowRect(hwndChild, &rChild);
2323 wChild = rChild.right - rChild.left;
2324 hChild = rChild.bottom - rChild.top;
2325
2326 /* If Vim is minimized put the window in the middle of the screen. */
2327 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002328 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002329 else
2330 GetWindowRect(hwndParent, &rParent);
2331 wParent = rParent.right - rParent.left;
2332 hParent = rParent.bottom - rParent.top;
2333
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002334 moninfo.cbSize = sizeof(MONITORINFO);
2335 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2336 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002338 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002340 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002342 hdc = GetDC(hwndChild);
2343 rScreen.left = 0;
2344 rScreen.top = 0;
2345 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2346 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2347 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002348 }
2349
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002350 xNew = rParent.left + ((wParent - wChild) / 2);
2351 if (xNew < rScreen.left)
2352 xNew = rScreen.left;
2353 else if ((xNew + wChild) > rScreen.right)
2354 xNew = rScreen.right - wChild;
2355
2356 yNew = rParent.top + ((hParent - hChild) / 2);
2357 if (yNew < rScreen.top)
2358 yNew = rScreen.top;
2359 else if ((yNew + hChild) > rScreen.bottom)
2360 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361
2362 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2363 SWP_NOSIZE | SWP_NOZORDER);
2364}
2365#endif /* FEAT_GUI_DIALOG */
2366
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367#if defined(FEAT_TOOLBAR) || defined(PROTO)
2368 void
2369gui_mch_show_toolbar(int showit)
2370{
2371 if (s_toolbarhwnd == NULL)
2372 return;
2373
2374 if (showit)
2375 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002376# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002377 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002378# define TB_SETUNICODEFORMAT 0x2005
2379# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002380 // Enable unicode support
2381 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2382 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002383 ShowWindow(s_toolbarhwnd, SW_SHOW);
2384 }
2385 else
2386 ShowWindow(s_toolbarhwnd, SW_HIDE);
2387}
2388
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002389/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002390#define TOOLBAR_BITMAP_COUNT 31
2391
2392#endif
2393
2394#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2395 static void
2396add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2397{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002398 WCHAR *wn;
2399 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002401 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002402 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002403 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002405 infow.cbSize = sizeof(infow);
2406 infow.fMask = MIIM_TYPE | MIIM_ID;
2407 infow.wID = item_id;
2408 infow.fType = MFT_STRING;
2409 infow.dwTypeData = wn;
2410 infow.cch = (UINT)wcslen(wn);
2411 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2412 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413}
2414
2415 static void
2416show_tabline_popup_menu(void)
2417{
2418 HMENU tab_pmenu;
2419 long rval;
2420 POINT pt;
2421
2422 /* When ignoring events don't show the menu. */
2423 if (hold_gui_events
2424# ifdef FEAT_CMDWIN
2425 || cmdwin_type != 0
2426# endif
2427 )
2428 return;
2429
2430 tab_pmenu = CreatePopupMenu();
2431 if (tab_pmenu == NULL)
2432 return;
2433
2434 if (first_tabpage->tp_next != NULL)
2435 add_tabline_popup_menu_entry(tab_pmenu,
2436 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2437 add_tabline_popup_menu_entry(tab_pmenu,
2438 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2439 add_tabline_popup_menu_entry(tab_pmenu,
2440 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2441
2442 GetCursorPos(&pt);
2443 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2444 NULL);
2445
2446 DestroyMenu(tab_pmenu);
2447
2448 /* Add the string cmd into input buffer */
2449 if (rval > 0)
2450 {
2451 TCHITTESTINFO htinfo;
2452 int idx;
2453
2454 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2455 return;
2456
2457 htinfo.pt.x = pt.x;
2458 htinfo.pt.y = pt.y;
2459 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2460 if (idx == -1)
2461 idx = 0;
2462 else
2463 idx += 1;
2464
2465 send_tabline_menu_event(idx, (int)rval);
2466 }
2467}
2468
2469/*
2470 * Show or hide the tabline.
2471 */
2472 void
2473gui_mch_show_tabline(int showit)
2474{
2475 if (s_tabhwnd == NULL)
2476 return;
2477
2478 if (!showit != !showing_tabline)
2479 {
2480 if (showit)
2481 ShowWindow(s_tabhwnd, SW_SHOW);
2482 else
2483 ShowWindow(s_tabhwnd, SW_HIDE);
2484 showing_tabline = showit;
2485 }
2486}
2487
2488/*
2489 * Return TRUE when tabline is displayed.
2490 */
2491 int
2492gui_mch_showing_tabline(void)
2493{
2494 return s_tabhwnd != NULL && showing_tabline;
2495}
2496
2497/*
2498 * Update the labels of the tabline.
2499 */
2500 void
2501gui_mch_update_tabline(void)
2502{
2503 tabpage_T *tp;
2504 TCITEM tie;
2505 int nr = 0;
2506 int curtabidx = 0;
2507 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002508 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509
2510 if (s_tabhwnd == NULL)
2511 return;
2512
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002513#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002515# define CCM_SETUNICODEFORMAT 0x2005
2516#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002517 // Enable unicode support
2518 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002519
2520 tie.mask = TCIF_TEXT;
2521 tie.iImage = -1;
2522
2523 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2524 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2525
2526 /* Add a label for each tab page. They all contain the same text area. */
2527 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2528 {
2529 if (tp == curtab)
2530 curtabidx = nr;
2531
2532 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2533 {
2534 /* Add the tab */
2535 tie.pszText = "-Empty-";
2536 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2537 tabadded = 1;
2538 }
2539
2540 get_tabline_label(tp, FALSE);
2541 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002542
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002543 wstr = enc_to_utf16(NameBuff, NULL);
2544 if (wstr != NULL)
2545 {
2546 TCITEMW tiw;
2547
2548 tiw.mask = TCIF_TEXT;
2549 tiw.iImage = -1;
2550 tiw.pszText = wstr;
2551 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2552 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 }
2555
2556 /* Remove any old labels. */
2557 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2558 TabCtrl_DeleteItem(s_tabhwnd, nr);
2559
2560 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2561 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2562
2563 /* Re-enable redraw and redraw. */
2564 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2565 RedrawWindow(s_tabhwnd, NULL, NULL,
2566 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2567
2568 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2569 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2570}
2571
2572/*
2573 * Set the current tab to "nr". First tab is 1.
2574 */
2575 void
2576gui_mch_set_curtab(int nr)
2577{
2578 if (s_tabhwnd == NULL)
2579 return;
2580
2581 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2582 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2583}
2584
2585#endif
2586
2587/*
2588 * ":simalt" command.
2589 */
2590 void
2591ex_simalt(exarg_T *eap)
2592{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002593 char_u *keys = eap->arg;
2594 int fill_typebuf = FALSE;
2595 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596
2597 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2598 while (*keys)
2599 {
2600 if (*keys == '~')
2601 *keys = ' '; /* for showing system menu */
2602 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2603 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002604 fill_typebuf = TRUE;
2605 }
2606 if (fill_typebuf)
2607 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002608 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002609 * processed. */
2610 key_name[0] = K_SPECIAL;
2611 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002612 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002613 key_name[3] = NUL;
2614 typebuf_was_filled = TRUE;
2615 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002616 }
2617}
2618
2619/*
2620 * Create the find & replace dialogs.
2621 * You can't have both at once: ":find" when replace is showing, destroys
2622 * the replace dialog first, and the other way around.
2623 */
2624#ifdef MSWIN_FIND_REPLACE
2625 static void
2626initialise_findrep(char_u *initial_string)
2627{
2628 int wword = FALSE;
2629 int mcase = !p_ic;
2630 char_u *entry_text;
2631
2632 /* Get the search string to use. */
2633 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2634
2635 s_findrep_struct.hwndOwner = s_hwnd;
2636 s_findrep_struct.Flags = FR_DOWN;
2637 if (mcase)
2638 s_findrep_struct.Flags |= FR_MATCHCASE;
2639 if (wword)
2640 s_findrep_struct.Flags |= FR_WHOLEWORD;
2641 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002642 {
2643 WCHAR *p = enc_to_utf16(entry_text, NULL);
2644 if (p != NULL)
2645 {
2646 int len = s_findrep_struct.wFindWhatLen - 1;
2647
2648 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2649 s_findrep_struct.lpstrFindWhat[len] = NUL;
2650 vim_free(p);
2651 }
2652 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002653 vim_free(entry_text);
2654}
2655#endif
2656
2657 static void
2658set_window_title(HWND hwnd, char *title)
2659{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002660 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002661 {
2662 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663
2664 /* Convert the title from 'encoding' to UTF-16. */
2665 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2666 if (wbuf != NULL)
2667 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002668 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002669 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 }
2671 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002672 else
2673 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002674}
2675
2676 void
2677gui_mch_find_dialog(exarg_T *eap)
2678{
2679#ifdef MSWIN_FIND_REPLACE
2680 if (s_findrep_msg != 0)
2681 {
2682 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2683 DestroyWindow(s_findrep_hwnd);
2684
2685 if (!IsWindow(s_findrep_hwnd))
2686 {
2687 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002688 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002689 }
2690
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002691 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002692 (void)SetFocus(s_findrep_hwnd);
2693
2694 s_findrep_is_find = TRUE;
2695 }
2696#endif
2697}
2698
2699
2700 void
2701gui_mch_replace_dialog(exarg_T *eap)
2702{
2703#ifdef MSWIN_FIND_REPLACE
2704 if (s_findrep_msg != 0)
2705 {
2706 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2707 DestroyWindow(s_findrep_hwnd);
2708
2709 if (!IsWindow(s_findrep_hwnd))
2710 {
2711 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002712 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002713 }
2714
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002715 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 (void)SetFocus(s_findrep_hwnd);
2717
2718 s_findrep_is_find = FALSE;
2719 }
2720#endif
2721}
2722
2723
2724/*
2725 * Set visibility of the pointer.
2726 */
2727 void
2728gui_mch_mousehide(int hide)
2729{
2730 if (hide != gui.pointer_hidden)
2731 {
2732 ShowCursor(!hide);
2733 gui.pointer_hidden = hide;
2734 }
2735}
2736
2737#ifdef FEAT_MENU
2738 static void
2739gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2740{
2741 /* Unhide the mouse, we don't get move events here. */
2742 gui_mch_mousehide(FALSE);
2743
2744 (void)TrackPopupMenu(
2745 (HMENU)menu->submenu_id,
2746 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2747 x, y,
2748 (int)0, /*reserved param*/
2749 s_hwnd,
2750 NULL);
2751 /*
2752 * NOTE: The pop-up menu can eat the mouse up event.
2753 * We deal with this in normal.c.
2754 */
2755}
2756#endif
2757
2758/*
2759 * Got a message when the system will go down.
2760 */
2761 static void
2762_OnEndSession(void)
2763{
2764 getout_preserve_modified(1);
2765}
2766
2767/*
2768 * Get this message when the user clicks on the cross in the top right corner
2769 * of a Windows95 window.
2770 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002772_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773{
2774 gui_shell_closed();
2775}
2776
2777/*
2778 * Get a message when the window is being destroyed.
2779 */
2780 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002781_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782{
2783 if (!destroying)
2784 _OnClose(hwnd);
2785}
2786
2787 static void
2788_OnPaint(
2789 HWND hwnd)
2790{
2791 if (!IsMinimized(hwnd))
2792 {
2793 PAINTSTRUCT ps;
2794
2795 out_flush(); /* make sure all output has been processed */
2796 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002797
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002798 /* prevent multi-byte characters from misprinting on an invalid
2799 * rectangle */
2800 if (has_mbyte)
2801 {
2802 RECT rect;
2803
2804 GetClientRect(hwnd, &rect);
2805 ps.rcPaint.left = rect.left;
2806 ps.rcPaint.right = rect.right;
2807 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002808
2809 if (!IsRectEmpty(&ps.rcPaint))
2810 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2812 ps.rcPaint.right - ps.rcPaint.left + 1,
2813 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2814 }
2815
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816 EndPaint(hwnd, &ps);
2817 }
2818}
2819
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 static void
2821_OnSize(
2822 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002823 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 int cx,
2825 int cy)
2826{
2827 if (!IsMinimized(hwnd))
2828 {
2829 gui_resize_shell(cx, cy);
2830
2831#ifdef FEAT_MENU
2832 /* Menu bar may wrap differently now */
2833 gui_mswin_get_menu_height(TRUE);
2834#endif
2835 }
2836}
2837
2838 static void
2839_OnSetFocus(
2840 HWND hwnd,
2841 HWND hwndOldFocus)
2842{
2843 gui_focus_change(TRUE);
2844 s_getting_focus = TRUE;
2845 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2846}
2847
2848 static void
2849_OnKillFocus(
2850 HWND hwnd,
2851 HWND hwndNewFocus)
2852{
2853 gui_focus_change(FALSE);
2854 s_getting_focus = FALSE;
2855 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2856}
2857
2858/*
2859 * Get a message when the user switches back to vim
2860 */
2861 static LRESULT
2862_OnActivateApp(
2863 HWND hwnd,
2864 BOOL fActivate,
2865 DWORD dwThreadId)
2866{
2867 /* we call gui_focus_change() in _OnSetFocus() */
2868 /* gui_focus_change((int)fActivate); */
2869 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2870}
2871
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002872 void
2873gui_mch_destroy_scrollbar(scrollbar_T *sb)
2874{
2875 DestroyWindow(sb->id);
2876}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877
2878/*
2879 * Get current mouse coordinates in text window.
2880 */
2881 void
2882gui_mch_getmouse(int *x, int *y)
2883{
2884 RECT rct;
2885 POINT mp;
2886
2887 (void)GetWindowRect(s_textArea, &rct);
2888 (void)GetCursorPos((LPPOINT)&mp);
2889 *x = (int)(mp.x - rct.left);
2890 *y = (int)(mp.y - rct.top);
2891}
2892
2893/*
2894 * Move mouse pointer to character at (x, y).
2895 */
2896 void
2897gui_mch_setmouse(int x, int y)
2898{
2899 RECT rct;
2900
2901 (void)GetWindowRect(s_textArea, &rct);
2902 (void)SetCursorPos(x + gui.border_offset + rct.left,
2903 y + gui.border_offset + rct.top);
2904}
2905
2906 static void
2907gui_mswin_get_valid_dimensions(
2908 int w,
2909 int h,
2910 int *valid_w,
2911 int *valid_h)
2912{
2913 int base_width, base_height;
2914
2915 base_width = gui_get_base_width()
2916 + (GetSystemMetrics(SM_CXFRAME) +
2917 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2918 base_height = gui_get_base_height()
2919 + (GetSystemMetrics(SM_CYFRAME) +
2920 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2921 + GetSystemMetrics(SM_CYCAPTION)
2922#ifdef FEAT_MENU
2923 + gui_mswin_get_menu_height(FALSE)
2924#endif
2925 ;
2926 *valid_w = base_width +
2927 ((w - base_width) / gui.char_width) * gui.char_width;
2928 *valid_h = base_height +
2929 ((h - base_height) / gui.char_height) * gui.char_height;
2930}
2931
2932 void
2933gui_mch_flash(int msec)
2934{
2935 RECT rc;
2936
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002937#if defined(FEAT_DIRECTX)
2938 if (IS_ENABLE_DIRECTX())
2939 DWriteContext_Flush(s_dwc);
2940#endif
2941
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002942 /*
2943 * Note: InvertRect() excludes right and bottom of rectangle.
2944 */
2945 rc.left = 0;
2946 rc.top = 0;
2947 rc.right = gui.num_cols * gui.char_width;
2948 rc.bottom = gui.num_rows * gui.char_height;
2949 InvertRect(s_hdc, &rc);
2950 gui_mch_flush(); /* make sure it's displayed */
2951
2952 ui_delay((long)msec, TRUE); /* wait for a few msec */
2953
2954 InvertRect(s_hdc, &rc);
2955}
2956
2957/*
2958 * Return flags used for scrolling.
2959 * The SW_INVALIDATE is required when part of the window is covered or
2960 * off-screen. Refer to MS KB Q75236.
2961 */
2962 static int
2963get_scroll_flags(void)
2964{
2965 HWND hwnd;
2966 RECT rcVim, rcOther, rcDest;
2967
2968 GetWindowRect(s_hwnd, &rcVim);
2969
2970 /* Check if the window is partly above or below the screen. We don't care
2971 * about partly left or right of the screen, it is not relevant when
2972 * scrolling up or down. */
2973 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2974 return SW_INVALIDATE;
2975
2976 /* Check if there is an window (partly) on top of us. */
2977 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2978 if (IsWindowVisible(hwnd))
2979 {
2980 GetWindowRect(hwnd, &rcOther);
2981 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2982 return SW_INVALIDATE;
2983 }
2984 return 0;
2985}
2986
2987/*
2988 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
2989 * may not be scrolled out properly.
2990 * For gVim, when _OnScroll() is repeated, the character at the
2991 * previous cursor position may be left drawn after scroll.
2992 * The problem can be avoided by calling GetPixel() to get a pixel in
2993 * the region before ScrollWindowEx().
2994 */
2995 static void
2996intel_gpu_workaround(void)
2997{
2998 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
2999}
3000
3001/*
3002 * Delete the given number of lines from the given row, scrolling up any
3003 * text further down within the scroll region.
3004 */
3005 void
3006gui_mch_delete_lines(
3007 int row,
3008 int num_lines)
3009{
3010 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003011
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003012 rc.left = FILL_X(gui.scroll_region_left);
3013 rc.right = FILL_X(gui.scroll_region_right + 1);
3014 rc.top = FILL_Y(row);
3015 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3016
Bram Moolenaar92467d32017-12-05 13:22:16 +01003017#if defined(FEAT_DIRECTX)
3018 if (IS_ENABLE_DIRECTX())
3019 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003020 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3021 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003022 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003023 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003024#endif
3025 {
3026 intel_gpu_workaround();
3027 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003028 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003029 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003030 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003031
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032 /* This seems to be required to avoid the cursor disappearing when
3033 * scrolling such that the cursor ends up in the top-left character on
3034 * the screen... But why? (Webb) */
3035 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3036 /* gui.cursor_is_valid = FALSE; */
3037
3038 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3039 gui.scroll_region_left,
3040 gui.scroll_region_bot, gui.scroll_region_right);
3041}
3042
3043/*
3044 * Insert the given number of lines before the given row, scrolling down any
3045 * following text within the scroll region.
3046 */
3047 void
3048gui_mch_insert_lines(
3049 int row,
3050 int num_lines)
3051{
3052 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003053
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003054 rc.left = FILL_X(gui.scroll_region_left);
3055 rc.right = FILL_X(gui.scroll_region_right + 1);
3056 rc.top = FILL_Y(row);
3057 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003058
3059#if defined(FEAT_DIRECTX)
3060 if (IS_ENABLE_DIRECTX())
3061 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003062 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3063 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003064 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003065 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003066#endif
3067 {
3068 intel_gpu_workaround();
3069 /* The SW_INVALIDATE is required when part of the window is covered or
3070 * off-screen. How do we avoid it when it's not needed? */
3071 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003072 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003073 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003074 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 gui_clear_block(row, gui.scroll_region_left,
3077 row + num_lines - 1, gui.scroll_region_right);
3078}
3079
3080
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003081 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003082gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003083{
3084#if defined(FEAT_DIRECTX)
3085 DWriteContext_Close(s_dwc);
3086 DWrite_Final();
3087 s_dwc = NULL;
3088#endif
3089
3090 ReleaseDC(s_textArea, s_hdc);
3091 DeleteObject(s_brush);
3092
3093#ifdef FEAT_TEAROFF
3094 /* Unload the tearoff bitmap */
3095 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3096#endif
3097
3098 /* Destroy our window (if we have one). */
3099 if (s_hwnd != NULL)
3100 {
3101 destroying = TRUE; /* ignore WM_DESTROY message now */
3102 DestroyWindow(s_hwnd);
3103 }
3104
3105#ifdef GLOBAL_IME
3106 global_ime_end();
3107#endif
3108}
3109
3110 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003111logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003112{
3113 char *p;
3114 char *res;
3115 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003116 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003117 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003118 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003120 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3121 if (font_name == NULL)
3122 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003123 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003124 quality_name = quality_id2name((int)lf.lfQuality);
3125
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003126 res = (char *)alloc((unsigned)(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003127 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
3128 + (quality_name == NULL ? 0 : strlen(quality_name) + 2)));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003129 if (res != NULL)
3130 {
3131 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003132 // make a normal font string out of the lf thing:
3133 points = pixels_to_points(
3134 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3135 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3136 sprintf((char *)p, "%s:h%d", font_name, points);
3137 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003138 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139 while (*p)
3140 {
3141 if (*p == ' ')
3142 *p = '_';
3143 ++p;
3144 }
3145 if (lf.lfItalic)
3146 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003147 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003148 STRCAT(p, ":b");
3149 if (lf.lfUnderline)
3150 STRCAT(p, ":u");
3151 if (lf.lfStrikeOut)
3152 STRCAT(p, ":s");
3153 if (charset_name != NULL)
3154 {
3155 STRCAT(p, ":c");
3156 STRCAT(p, charset_name);
3157 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003158 if (quality_name != NULL)
3159 {
3160 STRCAT(p, ":q");
3161 STRCAT(p, quality_name);
3162 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003163 }
3164
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003165 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003166 return (char_u *)res;
3167}
3168
3169
3170#ifdef FEAT_MBYTE_IME
3171/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003172 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173 * 'guifont'
3174 */
3175 static void
3176update_im_font(void)
3177{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003178 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003179
3180 if (p_guifontwide != NULL && *p_guifontwide != NUL
3181 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003182 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183 norm_logfont = lf_wide;
3184 else
3185 norm_logfont = sub_logfont;
3186 im_set_font(&norm_logfont);
3187}
3188#endif
3189
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003190/*
3191 * Handler of gui.wide_font (p_guifontwide) changed notification.
3192 */
3193 void
3194gui_mch_wide_font_changed(void)
3195{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003196 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003198#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003200#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201
3202 gui_mch_free_font(gui.wide_ital_font);
3203 gui.wide_ital_font = NOFONT;
3204 gui_mch_free_font(gui.wide_bold_font);
3205 gui.wide_bold_font = NOFONT;
3206 gui_mch_free_font(gui.wide_boldital_font);
3207 gui.wide_boldital_font = NOFONT;
3208
3209 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003210 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 {
3212 if (!lf.lfItalic)
3213 {
3214 lf.lfItalic = TRUE;
3215 gui.wide_ital_font = get_font_handle(&lf);
3216 lf.lfItalic = FALSE;
3217 }
3218 if (lf.lfWeight < FW_BOLD)
3219 {
3220 lf.lfWeight = FW_BOLD;
3221 gui.wide_bold_font = get_font_handle(&lf);
3222 if (!lf.lfItalic)
3223 {
3224 lf.lfItalic = TRUE;
3225 gui.wide_boldital_font = get_font_handle(&lf);
3226 }
3227 }
3228 }
3229}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003230
3231/*
3232 * Initialise vim to use the font with the given name.
3233 * Return FAIL if the font could not be loaded, OK otherwise.
3234 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003236gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003237{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003238 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 GuiFont font = NOFONT;
3240 char_u *p;
3241
3242 /* Load the font */
3243 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3244 font = get_font_handle(&lf);
3245 if (font == NOFONT)
3246 return FAIL;
3247
3248 if (font_name == NULL)
3249 font_name = (char_u *)lf.lfFaceName;
3250#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3251 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003252#endif
3253#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003254 sub_logfont = lf;
3255#endif
3256#ifdef FEAT_MBYTE_IME
3257 update_im_font();
3258#endif
3259 gui_mch_free_font(gui.norm_font);
3260 gui.norm_font = font;
3261 current_font_height = lf.lfHeight;
3262 GetFontSize(font);
3263
3264 p = logfont2name(lf);
3265 if (p != NULL)
3266 {
3267 hl_set_font_name(p);
3268
3269 /* When setting 'guifont' to "*" replace it with the actual font name.
3270 * */
3271 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3272 {
3273 vim_free(p_guifont);
3274 p_guifont = p;
3275 }
3276 else
3277 vim_free(p);
3278 }
3279
3280 gui_mch_free_font(gui.ital_font);
3281 gui.ital_font = NOFONT;
3282 gui_mch_free_font(gui.bold_font);
3283 gui.bold_font = NOFONT;
3284 gui_mch_free_font(gui.boldital_font);
3285 gui.boldital_font = NOFONT;
3286
3287 if (!lf.lfItalic)
3288 {
3289 lf.lfItalic = TRUE;
3290 gui.ital_font = get_font_handle(&lf);
3291 lf.lfItalic = FALSE;
3292 }
3293 if (lf.lfWeight < FW_BOLD)
3294 {
3295 lf.lfWeight = FW_BOLD;
3296 gui.bold_font = get_font_handle(&lf);
3297 if (!lf.lfItalic)
3298 {
3299 lf.lfItalic = TRUE;
3300 gui.boldital_font = get_font_handle(&lf);
3301 }
3302 }
3303
3304 return OK;
3305}
3306
3307#ifndef WPF_RESTORETOMAXIMIZED
3308# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3309#endif
3310
3311/*
3312 * Return TRUE if the GUI window is maximized, filling the whole screen.
3313 */
3314 int
3315gui_mch_maximized(void)
3316{
3317 WINDOWPLACEMENT wp;
3318
3319 wp.length = sizeof(WINDOWPLACEMENT);
3320 if (GetWindowPlacement(s_hwnd, &wp))
3321 return wp.showCmd == SW_SHOWMAXIMIZED
3322 || (wp.showCmd == SW_SHOWMINIMIZED
3323 && wp.flags == WPF_RESTORETOMAXIMIZED);
3324
3325 return 0;
3326}
3327
3328/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003329 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3330 * is set. Compute the new Rows and Columns. This is like resizing the
3331 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 */
3333 void
3334gui_mch_newfont(void)
3335{
3336 RECT rect;
3337
3338 GetWindowRect(s_hwnd, &rect);
3339 if (win_socket_id == 0)
3340 {
3341 gui_resize_shell(rect.right - rect.left
3342 - (GetSystemMetrics(SM_CXFRAME) +
3343 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3344 rect.bottom - rect.top
3345 - (GetSystemMetrics(SM_CYFRAME) +
3346 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3347 - GetSystemMetrics(SM_CYCAPTION)
3348#ifdef FEAT_MENU
3349 - gui_mswin_get_menu_height(FALSE)
3350#endif
3351 );
3352 }
3353 else
3354 {
3355 /* Inside another window, don't use the frame and border. */
3356 gui_resize_shell(rect.right - rect.left,
3357 rect.bottom - rect.top
3358#ifdef FEAT_MENU
3359 - gui_mswin_get_menu_height(FALSE)
3360#endif
3361 );
3362 }
3363}
3364
3365/*
3366 * Set the window title
3367 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368 void
3369gui_mch_settitle(
3370 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003371 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372{
3373 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3374}
3375
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003376#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3378 * misc2.c! */
3379static LPCSTR mshape_idcs[] =
3380{
3381 IDC_ARROW, /* arrow */
3382 MAKEINTRESOURCE(0), /* blank */
3383 IDC_IBEAM, /* beam */
3384 IDC_SIZENS, /* updown */
3385 IDC_SIZENS, /* udsizing */
3386 IDC_SIZEWE, /* leftright */
3387 IDC_SIZEWE, /* lrsizing */
3388 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390 IDC_ARROW, /* crosshair */
3391 IDC_ARROW, /* hand1 */
3392 IDC_ARROW, /* hand2 */
3393 IDC_ARROW, /* pencil */
3394 IDC_ARROW, /* question */
3395 IDC_ARROW, /* right-arrow */
3396 IDC_UPARROW, /* up-arrow */
3397 IDC_ARROW /* last one */
3398};
3399
3400 void
3401mch_set_mouse_shape(int shape)
3402{
3403 LPCSTR idc;
3404
3405 if (shape == MSHAPE_HIDE)
3406 ShowCursor(FALSE);
3407 else
3408 {
3409 if (shape >= MSHAPE_NUMBERED)
3410 idc = IDC_ARROW;
3411 else
3412 idc = mshape_idcs[shape];
3413#ifdef SetClassLongPtr
3414 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3415#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417#endif
3418 if (!p_mh)
3419 {
3420 POINT mp;
3421
3422 /* Set the position to make it redrawn with the new shape. */
3423 (void)GetCursorPos((LPPOINT)&mp);
3424 (void)SetCursorPos(mp.x, mp.y);
3425 ShowCursor(TRUE);
3426 }
3427 }
3428}
3429#endif
3430
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003431#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433 * Wide version of convert_filter().
3434 */
3435 static WCHAR *
3436convert_filterW(char_u *s)
3437{
3438 char_u *tmp;
3439 int len;
3440 WCHAR *res;
3441
3442 tmp = convert_filter(s);
3443 if (tmp == NULL)
3444 return NULL;
3445 len = (int)STRLEN(s) + 3;
3446 res = enc_to_utf16(tmp, &len);
3447 vim_free(tmp);
3448 return res;
3449}
3450
3451/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003452 * Pop open a file browser and return the file selected, in allocated memory,
3453 * or NULL if Cancel is hit.
3454 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3455 * title - Title message for the file browser dialog.
3456 * dflt - Default name of file.
3457 * ext - Default extension to be added to files without extensions.
3458 * initdir - directory in which to open the browser (NULL = current dir)
3459 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003461 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003462gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463 int saving,
3464 char_u *title,
3465 char_u *dflt,
3466 char_u *ext,
3467 char_u *initdir,
3468 char_u *filter)
3469{
3470 /* We always use the wide function. This means enc_to_utf16() must work,
3471 * otherwise it fails miserably! */
3472 OPENFILENAMEW fileStruct;
3473 WCHAR fileBuf[MAXPATHL];
3474 WCHAR *wp;
3475 int i;
3476 WCHAR *titlep = NULL;
3477 WCHAR *extp = NULL;
3478 WCHAR *initdirp = NULL;
3479 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003480 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003481
3482 if (dflt == NULL)
3483 fileBuf[0] = NUL;
3484 else
3485 {
3486 wp = enc_to_utf16(dflt, NULL);
3487 if (wp == NULL)
3488 fileBuf[0] = NUL;
3489 else
3490 {
3491 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3492 fileBuf[i] = wp[i];
3493 fileBuf[i] = NUL;
3494 vim_free(wp);
3495 }
3496 }
3497
3498 /* Convert the filter to Windows format. */
3499 filterp = convert_filterW(filter);
3500
3501 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003502# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003503 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003504 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003505# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003507# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508
3509 if (title != NULL)
3510 titlep = enc_to_utf16(title, NULL);
3511 fileStruct.lpstrTitle = titlep;
3512
3513 if (ext != NULL)
3514 extp = enc_to_utf16(ext, NULL);
3515 fileStruct.lpstrDefExt = extp;
3516
3517 fileStruct.lpstrFile = fileBuf;
3518 fileStruct.nMaxFile = MAXPATHL;
3519 fileStruct.lpstrFilter = filterp;
3520 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3521 /* has an initial dir been specified? */
3522 if (initdir != NULL && *initdir != NUL)
3523 {
3524 /* Must have backslashes here, no matter what 'shellslash' says */
3525 initdirp = enc_to_utf16(initdir, NULL);
3526 if (initdirp != NULL)
3527 {
3528 for (wp = initdirp; *wp != NUL; ++wp)
3529 if (*wp == '/')
3530 *wp = '\\';
3531 }
3532 fileStruct.lpstrInitialDir = initdirp;
3533 }
3534
3535 /*
3536 * TODO: Allow selection of multiple files. Needs another arg to this
3537 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3538 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3539 * files that don't exist yet, so I haven't put it in. What about
3540 * OFN_PATHMUSTEXIST?
3541 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3542 */
3543 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003544# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003545 if (curbuf->b_p_bin)
3546 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003547# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003548 if (saving)
3549 {
3550 if (!GetSaveFileNameW(&fileStruct))
3551 return NULL;
3552 }
3553 else
3554 {
3555 if (!GetOpenFileNameW(&fileStruct))
3556 return NULL;
3557 }
3558
3559 vim_free(filterp);
3560 vim_free(initdirp);
3561 vim_free(titlep);
3562 vim_free(extp);
3563
3564 /* Convert from UCS2 to 'encoding'. */
3565 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003566 if (p == NULL)
3567 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003568
3569 /* Give focus back to main window (when using MDI). */
3570 SetFocus(s_hwnd);
3571
3572 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003573 q = vim_strsave(shorten_fname1(p));
3574 vim_free(p);
3575 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577
3578
3579/*
3580 * Convert the string s to the proper format for a filter string by replacing
3581 * the \t and \n delimiters with \0.
3582 * Returns the converted string in allocated memory.
3583 *
3584 * Keep in sync with convert_filterW() above!
3585 */
3586 static char_u *
3587convert_filter(char_u *s)
3588{
3589 char_u *res;
3590 unsigned s_len = (unsigned)STRLEN(s);
3591 unsigned i;
3592
3593 res = alloc(s_len + 3);
3594 if (res != NULL)
3595 {
3596 for (i = 0; i < s_len; ++i)
3597 if (s[i] == '\t' || s[i] == '\n')
3598 res[i] = '\0';
3599 else
3600 res[i] = s[i];
3601 res[s_len] = NUL;
3602 /* Add two extra NULs to make sure it's properly terminated. */
3603 res[s_len + 1] = NUL;
3604 res[s_len + 2] = NUL;
3605 }
3606 return res;
3607}
3608
3609/*
3610 * Select a directory.
3611 */
3612 char_u *
3613gui_mch_browsedir(char_u *title, char_u *initdir)
3614{
3615 /* We fake this: Use a filter that doesn't select anything and a default
3616 * file name that won't be used. */
3617 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3618 initdir, (char_u *)_("Directory\t*.nothing\n"));
3619}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620#endif /* FEAT_BROWSE */
3621
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 static void
3623_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003624 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625 HDROP hDrop)
3626{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003627#define BUFPATHLEN _MAX_PATH
3628#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003629 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630 char szFile[BUFPATHLEN];
3631 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3632 UINT i;
3633 char_u **fnames;
3634 POINT pt;
3635 int_u modifiers = 0;
3636
3637 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3638
3639 /* Obtain dropped position */
3640 DragQueryPoint(hDrop, &pt);
3641 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3642
3643 reset_VIsual();
3644
3645 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3646
3647 if (fnames != NULL)
3648 for (i = 0; i < cFiles; ++i)
3649 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3651 fnames[i] = utf16_to_enc(wszFile, NULL);
3652 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 {
3654 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3655 fnames[i] = vim_strsave((char_u *)szFile);
3656 }
3657 }
3658
3659 DragFinish(hDrop);
3660
3661 if (fnames != NULL)
3662 {
3663 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3664 modifiers |= MOUSE_SHIFT;
3665 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3666 modifiers |= MOUSE_CTRL;
3667 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3668 modifiers |= MOUSE_ALT;
3669
3670 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3671
3672 s_need_activate = TRUE;
3673 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674}
3675
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003676 static int
3677_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003678 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679 HWND hwndCtl,
3680 UINT code,
3681 int pos)
3682{
3683 static UINT prev_code = 0; /* code of previous call */
3684 scrollbar_T *sb, *sb_info;
3685 long val;
3686 int dragging = FALSE;
3687 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 SCROLLINFO si;
3689
3690 si.cbSize = sizeof(si);
3691 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692
3693 sb = gui_mswin_find_scrollbar(hwndCtl);
3694 if (sb == NULL)
3695 return 0;
3696
3697 if (sb->wp != NULL) /* Left or right scrollbar */
3698 {
3699 /*
3700 * Careful: need to get scrollbar info out of first (left) scrollbar
3701 * for window, but keep real scrollbar too because we must pass it to
3702 * gui_drag_scrollbar().
3703 */
3704 sb_info = &sb->wp->w_scrollbars[0];
3705 }
3706 else /* Bottom scrollbar */
3707 sb_info = sb;
3708 val = sb_info->value;
3709
3710 switch (code)
3711 {
3712 case SB_THUMBTRACK:
3713 val = pos;
3714 dragging = TRUE;
3715 if (sb->scroll_shift > 0)
3716 val <<= sb->scroll_shift;
3717 break;
3718 case SB_LINEDOWN:
3719 val++;
3720 break;
3721 case SB_LINEUP:
3722 val--;
3723 break;
3724 case SB_PAGEDOWN:
3725 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3726 break;
3727 case SB_PAGEUP:
3728 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3729 break;
3730 case SB_TOP:
3731 val = 0;
3732 break;
3733 case SB_BOTTOM:
3734 val = sb_info->max;
3735 break;
3736 case SB_ENDSCROLL:
3737 if (prev_code == SB_THUMBTRACK)
3738 {
3739 /*
3740 * "pos" only gives us 16-bit data. In case of large file,
3741 * use GetScrollPos() which returns 32-bit. Unfortunately it
3742 * is not valid while the scrollbar is being dragged.
3743 */
3744 val = GetScrollPos(hwndCtl, SB_CTL);
3745 if (sb->scroll_shift > 0)
3746 val <<= sb->scroll_shift;
3747 }
3748 break;
3749
3750 default:
3751 /* TRACE("Unknown scrollbar event %d\n", code); */
3752 return 0;
3753 }
3754 prev_code = code;
3755
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3757 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758
3759 /*
3760 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3761 */
3762 if (sb->wp != NULL)
3763 {
3764 scrollbar_T *sba = sb->wp->w_scrollbars;
3765 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3766
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 }
3769
3770 /* Don't let us be interrupted here by another message. */
3771 s_busy_processing = TRUE;
3772
3773 /* When "allow_scrollbar" is FALSE still need to remember the new
3774 * position, but don't actually scroll by setting "dont_scroll". */
3775 dont_scroll = !allow_scrollbar;
3776
Bram Moolenaara338adc2018-01-31 20:51:47 +01003777 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003779 mch_enable_flush();
3780 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781
3782 s_busy_processing = FALSE;
3783 dont_scroll = dont_scroll_save;
3784
3785 return 0;
3786}
3787
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789#ifdef FEAT_XPM_W32
3790# include "xpm_w32.h"
3791#endif
3792
3793#ifdef PROTO
3794# define WINAPI
3795#endif
3796
3797#ifdef __MINGW32__
3798/*
3799 * Add a lot of missing defines.
3800 * They are not always missing, we need the #ifndef's.
3801 */
3802# ifndef _cdecl
3803# define _cdecl
3804# endif
3805# ifndef IsMinimized
3806# define IsMinimized(hwnd) IsIconic(hwnd)
3807# endif
3808# ifndef IsMaximized
3809# define IsMaximized(hwnd) IsZoomed(hwnd)
3810# endif
3811# ifndef SelectFont
3812# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3813# endif
3814# ifndef GetStockBrush
3815# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3816# endif
3817# ifndef DeleteBrush
3818# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3819# endif
3820
3821# ifndef HANDLE_WM_RBUTTONDBLCLK
3822# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3823 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3824# endif
3825# ifndef HANDLE_WM_MBUTTONUP
3826# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3827 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3828# endif
3829# ifndef HANDLE_WM_MBUTTONDBLCLK
3830# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3831 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3832# endif
3833# ifndef HANDLE_WM_LBUTTONDBLCLK
3834# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3835 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3836# endif
3837# ifndef HANDLE_WM_RBUTTONDOWN
3838# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3839 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3840# endif
3841# ifndef HANDLE_WM_MOUSEMOVE
3842# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3843 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3844# endif
3845# ifndef HANDLE_WM_RBUTTONUP
3846# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3847 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3848# endif
3849# ifndef HANDLE_WM_MBUTTONDOWN
3850# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3851 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3852# endif
3853# ifndef HANDLE_WM_LBUTTONUP
3854# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3855 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3856# endif
3857# ifndef HANDLE_WM_LBUTTONDOWN
3858# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3859 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3860# endif
3861# ifndef HANDLE_WM_SYSCHAR
3862# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3863 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3864# endif
3865# ifndef HANDLE_WM_ACTIVATEAPP
3866# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3867 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3868# endif
3869# ifndef HANDLE_WM_WINDOWPOSCHANGING
3870# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3871 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3872# endif
3873# ifndef HANDLE_WM_VSCROLL
3874# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3875 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3876# endif
3877# ifndef HANDLE_WM_SETFOCUS
3878# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3879 ((fn)((hwnd), (HWND)(wParam)), 0L)
3880# endif
3881# ifndef HANDLE_WM_KILLFOCUS
3882# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3883 ((fn)((hwnd), (HWND)(wParam)), 0L)
3884# endif
3885# ifndef HANDLE_WM_HSCROLL
3886# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3887 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3888# endif
3889# ifndef HANDLE_WM_DROPFILES
3890# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3891 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3892# endif
3893# ifndef HANDLE_WM_CHAR
3894# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3895 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3896# endif
3897# ifndef HANDLE_WM_SYSDEADCHAR
3898# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3899 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3900# endif
3901# ifndef HANDLE_WM_DEADCHAR
3902# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3903 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3904# endif
3905#endif /* __MINGW32__ */
3906
3907
3908/* Some parameters for tearoff menus. All in pixels. */
3909#define TEAROFF_PADDING_X 2
3910#define TEAROFF_BUTTON_PAD_X 8
3911#define TEAROFF_MIN_WIDTH 200
3912#define TEAROFF_SUBMENU_LABEL ">>"
3913#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3914
3915
3916/* For the Intellimouse: */
3917#ifndef WM_MOUSEWHEEL
3918#define WM_MOUSEWHEEL 0x20a
3919#endif
3920
3921
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003922#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# define ID_BEVAL_TOOLTIP 200
3924# define BEVAL_TEXT_LEN MAXPATHL
3925
Bram Moolenaar167632f2010-05-26 21:42:54 +02003926#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003927/* Work around old versions of basetsd.h which wrongly declares
3928 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003929# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003930# define UINT_PTR UINT
3931#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003934static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003936
Bram Moolenaar82881492012-11-20 16:53:39 +01003937
3938/* cproto fails on missing include files */
3939#ifndef PROTO
3940
Bram Moolenaar45360022005-07-21 21:08:21 +00003941/*
3942 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003943 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003944 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003945# include <pshpack1.h>
3946
3947#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003948
3949typedef struct _DllVersionInfo
3950{
3951 DWORD cbSize;
3952 DWORD dwMajorVersion;
3953 DWORD dwMinorVersion;
3954 DWORD dwBuildNumber;
3955 DWORD dwPlatformID;
3956} DLLVERSIONINFO;
3957
Bram Moolenaar82881492012-11-20 16:53:39 +01003958#ifndef PROTO
3959# include <poppack.h>
3960#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003961
Bram Moolenaar45360022005-07-21 21:08:21 +00003962typedef struct tagTOOLINFOA_NEW
3963{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003964 UINT cbSize;
3965 UINT uFlags;
3966 HWND hwnd;
3967 UINT_PTR uId;
3968 RECT rect;
3969 HINSTANCE hinst;
3970 LPSTR lpszText;
3971 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003972} TOOLINFO_NEW;
3973
3974typedef struct tagNMTTDISPINFO_NEW
3975{
3976 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003977 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003978 char szText[80];
3979 HINSTANCE hinst;
3980 UINT uFlags;
3981 LPARAM lParam;
3982} NMTTDISPINFO_NEW;
3983
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003984typedef struct tagTOOLINFOW_NEW
3985{
3986 UINT cbSize;
3987 UINT uFlags;
3988 HWND hwnd;
3989 UINT_PTR uId;
3990 RECT rect;
3991 HINSTANCE hinst;
3992 LPWSTR lpszText;
3993 LPARAM lParam;
3994 void *lpReserved;
3995} TOOLINFOW_NEW;
3996
3997typedef struct tagNMTTDISPINFOW_NEW
3998{
3999 NMHDR hdr;
4000 LPWSTR lpszText;
4001 WCHAR szText[80];
4002 HINSTANCE hinst;
4003 UINT uFlags;
4004 LPARAM lParam;
4005} NMTTDISPINFOW_NEW;
4006
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004007
Bram Moolenaar45360022005-07-21 21:08:21 +00004008typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4009#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004010# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011#endif
4012
Bram Moolenaar45360022005-07-21 21:08:21 +00004013#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004014# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004015#endif
4016
4017#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004018# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004019#endif
4020
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004021#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004022
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004023#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4024/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4025 * it here if LPNMTTDISPINFO isn't defined.
4026 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4027 * _MSC_VER. */
4028# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4029typedef struct tagNMTTDISPINFOA {
4030 NMHDR hdr;
4031 LPSTR lpszText;
4032 char szText[80];
4033 HINSTANCE hinst;
4034 UINT uFlags;
4035 LPARAM lParam;
4036} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4037# define LPNMTTDISPINFO LPNMTTDISPINFOA
4038
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004039typedef struct tagNMTTDISPINFOW {
4040 NMHDR hdr;
4041 LPWSTR lpszText;
4042 WCHAR szText[80];
4043 HINSTANCE hinst;
4044 UINT uFlags;
4045 LPARAM lParam;
4046} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004047# endif
4048#endif
4049
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004050#ifndef TTN_GETDISPINFOW
4051# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4052#endif
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/* Local variables: */
4055
4056#ifdef FEAT_MENU
4057static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060/*
4061 * Use the system font for dialogs and tear-off menus. Remove this line to
4062 * use DLG_FONT_NAME.
4063 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004064#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#define VIM_CLASSW L"Vim"
4068
4069/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4070 * thus there should be room for every dialog. For tearoffs it's made bigger
4071 * when needed. */
4072#define DLG_ALLOC_SIZE 16 * 1024
4073
4074/*
4075 * stuff for dialogs, menus, tearoffs etc.
4076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077static PWORD
4078add_dialog_element(
4079 PWORD p,
4080 DWORD lStyle,
4081 WORD x,
4082 WORD y,
4083 WORD w,
4084 WORD h,
4085 WORD Id,
4086 WORD clss,
4087 const char *caption);
4088static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004089static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004090#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093static void get_dialog_font_metrics(void);
4094
4095static int dialog_default_button = -1;
4096
4097/* Intellimouse support */
4098static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4101#ifdef FEAT_TOOLBAR
4102static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004103static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104static int get_toolbar_bitmap(vimmenu_T *menu);
4105#endif
4106
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004107#ifdef FEAT_GUI_TABLINE
4108static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004109static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004110#endif
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifdef FEAT_MBYTE_IME
4113static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4114static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4115#endif
4116#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4117# ifdef NOIME
4118typedef struct tagCOMPOSITIONFORM {
4119 DWORD dwStyle;
4120 POINT ptCurrentPos;
4121 RECT rcArea;
4122} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4123typedef HANDLE HIMC;
4124# endif
4125
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004126static HINSTANCE hLibImm = NULL;
4127static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4128static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4129static HIMC (WINAPI *pImmGetContext)(HWND);
4130static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4131static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4132static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4133static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004134static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4135static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004136static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4137static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004138static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139static void dyn_imm_load(void);
4140#else
4141# define pImmGetCompositionStringA ImmGetCompositionStringA
4142# define pImmGetCompositionStringW ImmGetCompositionStringW
4143# define pImmGetContext ImmGetContext
4144# define pImmAssociateContext ImmAssociateContext
4145# define pImmReleaseContext ImmReleaseContext
4146# define pImmGetOpenStatus ImmGetOpenStatus
4147# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004148# define pImmGetCompositionFontW ImmGetCompositionFontW
4149# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150# define pImmSetCompositionWindow ImmSetCompositionWindow
4151# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004152# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#ifdef FEAT_MENU
4156/*
4157 * Figure out how high the menu bar is at the moment.
4158 */
4159 static int
4160gui_mswin_get_menu_height(
4161 int fix_window) /* If TRUE, resize window if menu height changed */
4162{
4163 static int old_menu_height = -1;
4164
4165 RECT rc1, rc2;
4166 int num;
4167 int menu_height;
4168
4169 if (gui.menu_is_active)
4170 num = GetMenuItemCount(s_menuBar);
4171 else
4172 num = 0;
4173
4174 if (num == 0)
4175 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004176 else if (IsMinimized(s_hwnd))
4177 {
4178 /* The height of the menu cannot be determined while the window is
4179 * minimized. Take the previous height if the menu is changed in that
4180 * state, to avoid that Vim's vertical window size accidentally
4181 * increases due to the unaccounted-for menu height. */
4182 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else
4185 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004186 /*
4187 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4188 * seem to have been set yet, so menu wraps in default window
4189 * width which is very narrow. Instead just return height of a
4190 * single menu item. Will still be wrong when the menu really
4191 * should wrap over more than one line.
4192 */
4193 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4194 if (gui.starting)
4195 menu_height = rc1.bottom - rc1.top + 1;
4196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004198 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4199 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004204 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004205 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 return menu_height;
4208}
4209#endif /*FEAT_MENU*/
4210
4211
4212/*
4213 * Setup for the Intellimouse
4214 */
4215 static void
4216init_mouse_wheel(void)
4217{
4218
4219#ifndef SPI_GETWHEELSCROLLLINES
4220# define SPI_GETWHEELSCROLLLINES 104
4221#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004222#ifndef SPI_SETWHEELSCROLLLINES
4223# define SPI_SETWHEELSCROLLLINES 105
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4227#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4228#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4229#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 mouse_scroll_lines = 3; /* reasonable default */
4232
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004233 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4234 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4235 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236}
4237
4238
4239/* Intellimouse wheel handler */
4240 static void
4241_OnMouseWheel(
4242 HWND hwnd,
4243 short zDelta)
4244{
4245/* Treat a mouse wheel event as if it were a scroll request */
4246 int i;
4247 int size;
4248 HWND hwndCtl;
4249
4250 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4251 {
4252 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4253 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4254 }
4255 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4256 {
4257 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4258 size = curwin->w_scrollbars[SBAR_LEFT].size;
4259 }
4260 else
4261 return;
4262
4263 size = curwin->w_height;
4264 if (mouse_scroll_lines == 0)
4265 init_mouse_wheel();
4266
Bram Moolenaara338adc2018-01-31 20:51:47 +01004267 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 if (mouse_scroll_lines > 0
4269 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4270 {
4271 for (i = mouse_scroll_lines; i > 0; --i)
4272 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4273 }
4274 else
4275 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004276 mch_enable_flush();
4277 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278}
4279
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004280#ifdef USE_SYSMENU_FONT
4281/*
4282 * Get Menu Font.
4283 * Return OK or FAIL.
4284 */
4285 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004286gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004287{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004288 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004289
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004290 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4291 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004292 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004293 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004294 &nm,
4295 0))
4296 return FAIL;
4297 *lf = nm.lfMenuFont;
4298 return OK;
4299}
4300#endif
4301
4302
4303#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4304/*
4305 * Set the GUI tabline font to the system menu font
4306 */
4307 static void
4308set_tabline_font(void)
4309{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004310 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311 HFONT font;
4312 HWND hwnd;
4313 HDC hdc;
4314 HFONT hfntOld;
4315 TEXTMETRIC tm;
4316
4317 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4318 return;
4319
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004320 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004321
4322 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4323
4324 /*
4325 * Compute the height of the font used for the tab text
4326 */
4327 hwnd = GetDesktopWindow();
4328 hdc = GetWindowDC(hwnd);
4329 hfntOld = SelectFont(hdc, font);
4330
4331 GetTextMetrics(hdc, &tm);
4332
4333 SelectFont(hdc, hfntOld);
4334 ReleaseDC(hwnd, hdc);
4335
4336 /*
4337 * The space used by the tab border and the space between the tab label
4338 * and the tab border is included as 7.
4339 */
4340 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4341}
4342#endif
4343
Bram Moolenaar520470a2005-06-16 21:59:56 +00004344/*
4345 * Invoked when a setting was changed.
4346 */
4347 static LRESULT CALLBACK
4348_OnSettingChange(UINT n)
4349{
4350 if (n == SPI_SETWHEELSCROLLLINES)
4351 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4352 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004353#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4354 if (n == SPI_SETNONCLIENTMETRICS)
4355 set_tabline_font();
4356#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004357 return 0;
4358}
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#ifdef FEAT_NETBEANS_INTG
4361 static void
4362_OnWindowPosChanged(
4363 HWND hwnd,
4364 const LPWINDOWPOS lpwpos)
4365{
4366 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004367 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4370 || lpwpos->cx != cx || lpwpos->cy != cy))
4371 {
4372 x = lpwpos->x;
4373 y = lpwpos->y;
4374 cx = lpwpos->cx;
4375 cy = lpwpos->cy;
4376 netbeans_frame_moved(x, y);
4377 }
4378 /* Allow to send WM_SIZE and WM_MOVE */
4379 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4380}
4381#endif
4382
4383 static int
4384_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 UINT fwSide,
4386 LPRECT lprc)
4387{
4388 int w, h;
4389 int valid_w, valid_h;
4390 int w_offset, h_offset;
4391
4392 w = lprc->right - lprc->left;
4393 h = lprc->bottom - lprc->top;
4394 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4395 w_offset = w - valid_w;
4396 h_offset = h - valid_h;
4397
4398 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4399 || fwSide == WMSZ_BOTTOMLEFT)
4400 lprc->left += w_offset;
4401 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4402 || fwSide == WMSZ_BOTTOMRIGHT)
4403 lprc->right -= w_offset;
4404
4405 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4406 || fwSide == WMSZ_TOPRIGHT)
4407 lprc->top += h_offset;
4408 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4409 || fwSide == WMSZ_BOTTOMRIGHT)
4410 lprc->bottom -= h_offset;
4411 return TRUE;
4412}
4413
4414
4415
4416 static LRESULT CALLBACK
4417_WndProc(
4418 HWND hwnd,
4419 UINT uMsg,
4420 WPARAM wParam,
4421 LPARAM lParam)
4422{
4423 /*
4424 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4425 hwnd, uMsg, wParam, lParam);
4426 */
4427
4428 HandleMouseHide(uMsg, lParam);
4429
4430 s_uMsg = uMsg;
4431 s_wParam = wParam;
4432 s_lParam = lParam;
4433
4434 switch (uMsg)
4435 {
4436 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4437 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4438 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4439 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4440 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4441 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4442 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4443 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4444 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4445#ifdef FEAT_MENU
4446 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4447#endif
4448 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4449 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4450 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4451 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4452 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4453 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4454 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4455 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4456 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4457#ifdef FEAT_NETBEANS_INTG
4458 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4459#endif
4460
Bram Moolenaarafa24992006-03-27 20:58:26 +00004461#ifdef FEAT_GUI_TABLINE
4462 case WM_RBUTTONUP:
4463 {
4464 if (gui_mch_showing_tabline())
4465 {
4466 POINT pt;
4467 RECT rect;
4468
4469 /*
4470 * If the cursor is on the tabline, display the tab menu
4471 */
4472 GetCursorPos((LPPOINT)&pt);
4473 GetWindowRect(s_textArea, &rect);
4474 if (pt.y < rect.top)
4475 {
4476 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004477 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004478 }
4479 }
4480 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4481 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004482 case WM_LBUTTONDBLCLK:
4483 {
4484 /*
4485 * If the user double clicked the tabline, create a new tab
4486 */
4487 if (gui_mch_showing_tabline())
4488 {
4489 POINT pt;
4490 RECT rect;
4491
4492 GetCursorPos((LPPOINT)&pt);
4493 GetWindowRect(s_textArea, &rect);
4494 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004495 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004496 }
4497 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4498 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004499#endif
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 case WM_QUERYENDSESSION: /* System wants to go down. */
4502 gui_shell_closed(); /* Will exit when no changed buffers. */
4503 return FALSE; /* Do NOT allow system to go down. */
4504
4505 case WM_ENDSESSION:
4506 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004507 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004509 return 0L;
4510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 break;
4512
4513 case WM_CHAR:
4514 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4515 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004516 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 return 0L;
4518
4519 case WM_SYSCHAR:
4520 /*
4521 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4522 * shortcut key, handle like a typed ALT key, otherwise call Windows
4523 * ALT key handling.
4524 */
4525#ifdef FEAT_MENU
4526 if ( !gui.menu_is_active
4527 || p_wak[0] == 'n'
4528 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4529 )
4530#endif
4531 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004532 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 return 0L;
4534 }
4535#ifdef FEAT_MENU
4536 else
4537 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4538#endif
4539
4540 case WM_SYSKEYUP:
4541#ifdef FEAT_MENU
4542 /* This used to be done only when menu is active: ALT key is used for
4543 * that. But that caused problems when menu is disabled and using
4544 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4545 * are received, mouse pointer remains hidden. */
4546 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4547#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004548 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549#endif
4550
4551 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
4554 case WM_MOUSEWHEEL:
4555 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004556 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar520470a2005-06-16 21:59:56 +00004558 /* Notification for change in SystemParametersInfo() */
4559 case WM_SETTINGCHANGE:
4560 return _OnSettingChange((UINT)wParam);
4561
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004562#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 case WM_NOTIFY:
4564 switch (((LPNMHDR) lParam)->code)
4565 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004566 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004567 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004569 LPNMHDR hdr = (LPNMHDR)lParam;
4570 char_u *str = NULL;
4571 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004572
Bram Moolenaard23a8232018-02-10 18:45:26 +01004573 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004574
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004575# ifdef FEAT_GUI_TABLINE
4576 if (gui_mch_showing_tabline()
4577 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004578 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004579 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004580 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004581 * Mouse is over the GUI tabline. Display the
4582 * tooltip for the tab under the cursor
4583 *
4584 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004585 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004586 GetCursorPos(&pt);
4587 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004588 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004589 TCHITTESTINFO htinfo;
4590 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004591
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004592 /*
4593 * Get the tab under the cursor
4594 */
4595 htinfo.pt.x = pt.x;
4596 htinfo.pt.y = pt.y;
4597 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4598 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004599 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004600 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004601
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004602 tp = find_tabpage(idx + 1);
4603 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004604 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004605 get_tabline_label(tp, TRUE);
4606 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004607 }
4608 }
4609 }
4610 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004611# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004612# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004613# ifdef FEAT_GUI_TABLINE
4614 else
4615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004617 UINT idButton;
4618 vimmenu_T *pMenu;
4619
4620 idButton = (UINT) hdr->idFrom;
4621 pMenu = gui_mswin_find_menu(root_menu, idButton);
4622 if (pMenu)
4623 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004625# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004626 if (str != NULL)
4627 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004628 if (hdr->code == TTN_GETDISPINFOW)
4629 {
4630 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4631
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004632 /* Set the maximum width, this also enables using
4633 * \n for line break. */
4634 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4635 0, 500);
4636
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004637 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004638 lpdi->lpszText = tt_text;
4639 /* can't show tooltip if failed */
4640 }
4641 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004642 {
4643 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4644
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004645 /* Set the maximum width, this also enables using
4646 * \n for line break. */
4647 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4648 0, 500);
4649
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 if (STRLEN(str) < sizeof(lpdi->szText)
4651 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004652 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004653 sizeof(lpdi->szText) - 1);
4654 else
4655 lpdi->lpszText = tt_text;
4656 }
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004660# ifdef FEAT_GUI_TABLINE
4661 case TCN_SELCHANGE:
4662 if (gui_mch_showing_tabline()
4663 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004664 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004665 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004666 return 0L;
4667 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004668 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004669
4670 case NM_RCLICK:
4671 if (gui_mch_showing_tabline()
4672 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004673 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004674 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004675 return 0L;
4676 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004677 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004680# ifdef FEAT_GUI_TABLINE
4681 if (gui_mch_showing_tabline()
4682 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4683 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 break;
4686 }
4687 break;
4688#endif
4689#if defined(MENUHINTS) && defined(FEAT_MENU)
4690 case WM_MENUSELECT:
4691 if (((UINT) HIWORD(wParam)
4692 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4693 == MF_HILITE
4694 && (State & CMDLINE) == 0)
4695 {
4696 UINT idButton;
4697 vimmenu_T *pMenu;
4698 static int did_menu_tip = FALSE;
4699
4700 if (did_menu_tip)
4701 {
4702 msg_clr_cmdline();
4703 setcursor();
4704 out_flush();
4705 did_menu_tip = FALSE;
4706 }
4707
4708 idButton = (UINT)LOWORD(wParam);
4709 pMenu = gui_mswin_find_menu(root_menu, idButton);
4710 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4711 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4712 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004713 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004714 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004715 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 setcursor();
4717 out_flush();
4718 did_menu_tip = TRUE;
4719 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004720 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 break;
4723#endif
4724 case WM_NCHITTEST:
4725 {
4726 LRESULT result;
4727 int x, y;
4728 int xPos = GET_X_LPARAM(lParam);
4729
4730 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4731 if (result == HTCLIENT)
4732 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004733#ifdef FEAT_GUI_TABLINE
4734 if (gui_mch_showing_tabline())
4735 {
4736 int yPos = GET_Y_LPARAM(lParam);
4737 RECT rct;
4738
4739 /* If the cursor is on the GUI tabline, don't process this
4740 * event */
4741 GetWindowRect(s_textArea, &rct);
4742 if (yPos < rct.top)
4743 return result;
4744 }
4745#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004746 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 xPos -= x;
4748
4749 if (xPos < 48) /* <VN> TODO should use system metric? */
4750 return HTBOTTOMLEFT;
4751 else
4752 return HTBOTTOMRIGHT;
4753 }
4754 else
4755 return result;
4756 }
4757 /* break; notreached */
4758
4759#ifdef FEAT_MBYTE_IME
4760 case WM_IME_NOTIFY:
4761 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4762 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004763 return 1L;
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 case WM_IME_COMPOSITION:
4766 if (!_OnImeComposition(hwnd, wParam, lParam))
4767 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004768 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#endif
4770
4771 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004773 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#endif
4776 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4777 }
4778
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004779 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780}
4781
4782/*
4783 * End of call-back routines
4784 */
4785
4786/* parent window, if specified with -P */
4787HWND vim_parent_hwnd = NULL;
4788
4789 static BOOL CALLBACK
4790FindWindowTitle(HWND hwnd, LPARAM lParam)
4791{
4792 char buf[2048];
4793 char *title = (char *)lParam;
4794
4795 if (GetWindowText(hwnd, buf, sizeof(buf)))
4796 {
4797 if (strstr(buf, title) != NULL)
4798 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004799 /* Found it. Store the window ref. and quit searching if MDI
4800 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004802 if (vim_parent_hwnd != NULL)
4803 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 }
4806 return TRUE; /* continue searching */
4807}
4808
4809/*
4810 * Invoked for '-P "title"' argument: search for parent application to open
4811 * our window in.
4812 */
4813 void
4814gui_mch_set_parent(char *title)
4815{
4816 EnumWindows(FindWindowTitle, (LPARAM)title);
4817 if (vim_parent_hwnd == NULL)
4818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004819 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 mch_exit(2);
4821 }
4822}
4823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 static void
4826ole_error(char *arg)
4827{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004828 char buf[IOSIZE];
4829
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004830# ifdef VIMDLL
4831 gui.in_use = mch_is_gui_executable();
4832# endif
4833
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004834 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004835 vim_snprintf(buf, IOSIZE,
4836 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4837 arg);
4838 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004842#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4843 static char *
4844gvim_error(void)
4845{
4846 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4847
4848 if (starting)
4849 {
4850 mch_errmsg(msg);
4851 mch_errmsg("\n");
4852 mch_exit(2);
4853 }
4854 return msg;
4855}
4856
4857 char *
4858gui_mch_do_spawn(char_u *arg)
4859{
4860 int len;
4861# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4862 char_u *session = NULL;
4863 LPWSTR tofree1 = NULL;
4864# endif
4865 WCHAR name[MAX_PATH];
4866 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4867 STARTUPINFOW si = {sizeof(si)};
4868 PROCESS_INFORMATION pi;
4869
4870 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4871 goto error;
4872 p = wcsrchr(name, L'\\');
4873 if (p == NULL)
4874 goto error;
4875 // Replace the executable name from vim(d).exe to gvim(d).exe.
4876# ifdef DEBUG
4877 wcscpy(p + 1, L"gvimd.exe");
4878# else
4879 wcscpy(p + 1, L"gvim.exe");
4880# endif
4881
4882# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4883 if (starting)
4884# endif
4885 {
4886 // Pass the command line to the new process.
4887 p = GetCommandLineW();
4888 // Skip 1st argument.
4889 while (*p && *p != L' ' && *p != L'\t')
4890 {
4891 if (*p == L'"')
4892 {
4893 while (*p && *p != L'"')
4894 ++p;
4895 if (*p)
4896 ++p;
4897 }
4898 else
4899 ++p;
4900 }
4901 cmd = p;
4902 }
4903# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4904 else
4905 {
4906 // Create a session file and pass it to the new process.
4907 LPWSTR wsession;
4908 char_u *savebg;
4909 int ret;
4910
4911 session = vim_tempname('s', FALSE);
4912 if (session == NULL)
4913 goto error;
4914 savebg = p_bg;
4915 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4916 ret = write_session_file(session);
4917 vim_free(p_bg);
4918 p_bg = savebg;
4919 if (!ret)
4920 goto error;
4921 wsession = enc_to_utf16(session, NULL);
4922 if (wsession == NULL)
4923 goto error;
4924 len = (int)wcslen(wsession) * 2 + 27 + 1;
4925 cmd = (LPWSTR)alloc(len * (int)sizeof(WCHAR));
4926 if (cmd == NULL)
4927 {
4928 vim_free(wsession);
4929 goto error;
4930 }
4931 tofree1 = cmd;
4932 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4933 wsession, wsession);
4934 vim_free(wsession);
4935 }
4936# endif
4937
4938 // Check additional arguments to the `:gui` command.
4939 if (arg != NULL)
4940 {
4941 warg = enc_to_utf16(arg, NULL);
4942 if (warg == NULL)
4943 goto error;
4944 tofree2 = warg;
4945 }
4946 else
4947 warg = L"";
4948
4949 // Set up the new command line.
4950 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
4951 newcmd = (LPWSTR)alloc(len * (int)sizeof(WCHAR));
4952 if (newcmd == NULL)
4953 goto error;
4954 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4955
4956 // Spawn a new GUI process.
4957 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4958 NULL, NULL, &si, &pi))
4959 goto error;
4960 CloseHandle(pi.hProcess);
4961 CloseHandle(pi.hThread);
4962 mch_exit(0);
4963
4964error:
4965# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4966 if (session)
4967 mch_remove(session);
4968 vim_free(session);
4969 vim_free(tofree1);
4970# endif
4971 vim_free(newcmd);
4972 vim_free(tofree2);
4973 return gvim_error();
4974}
4975#endif
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977/*
4978 * Parse the GUI related command-line arguments. Any arguments used are
4979 * deleted from argv, and *argc is decremented accordingly. This is called
4980 * when vim is started, whether or not the GUI has been started.
4981 */
4982 void
4983gui_mch_prepare(int *argc, char **argv)
4984{
4985 int silent = FALSE;
4986 int idx;
4987
4988 /* Check for special OLE command line parameters */
4989 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4990 {
4991 /* Check for a "-silent" argument first. */
4992 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4993 && (argv[2][0] == '-' || argv[2][0] == '/'))
4994 {
4995 silent = TRUE;
4996 idx = 2;
4997 }
4998 else
4999 idx = 1;
5000
5001 /* Register Vim as an OLE Automation server */
5002 if (STRICMP(argv[idx] + 1, "register") == 0)
5003 {
5004#ifdef FEAT_OLE
5005 RegisterMe(silent);
5006 mch_exit(0);
5007#else
5008 if (!silent)
5009 ole_error("register");
5010 mch_exit(2);
5011#endif
5012 }
5013
5014 /* Unregister Vim as an OLE Automation server */
5015 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5016 {
5017#ifdef FEAT_OLE
5018 UnregisterMe(!silent);
5019 mch_exit(0);
5020#else
5021 if (!silent)
5022 ole_error("unregister");
5023 mch_exit(2);
5024#endif
5025 }
5026
5027 /* Ignore an -embedding argument. It is only relevant if the
5028 * application wants to treat the case when it is started manually
5029 * differently from the case where it is started via automation (and
5030 * we don't).
5031 */
5032 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5033 {
5034#ifdef FEAT_OLE
5035 *argc = 1;
5036#else
5037 ole_error("embedding");
5038 mch_exit(2);
5039#endif
5040 }
5041 }
5042
5043#ifdef FEAT_OLE
5044 {
5045 int bDoRestart = FALSE;
5046
5047 InitOLE(&bDoRestart);
5048 /* automatically exit after registering */
5049 if (bDoRestart)
5050 mch_exit(0);
5051 }
5052#endif
5053
5054#ifdef FEAT_NETBEANS_INTG
5055 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005056 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 int arg;
5058
5059 for (arg = 1; arg < *argc; arg++)
5060 if (strncmp("-nb", argv[arg], 3) == 0)
5061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 netbeansArg = argv[arg];
5063 mch_memmove(&argv[arg], &argv[arg + 1],
5064 (--*argc - arg) * sizeof(char *));
5065 argv[*argc] = NULL;
5066 break; /* enough? */
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071
5072/*
5073 * Initialise the GUI. Create all the windows, set up all the call-backs
5074 * etc.
5075 */
5076 int
5077gui_mch_init(void)
5078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005080 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082#ifdef GLOBAL_IME
5083 ATOM atom;
5084#endif
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 /* Return here if the window was already opened (happens when
5087 * gui_mch_dialog() is called early). */
5088 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005089 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 /*
5092 * Load the tearoff bitmap
5093 */
5094#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005095 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096#endif
5097
5098 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5099 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5100#ifdef FEAT_MENU
5101 gui.menu_height = 0; /* Windows takes care of this */
5102#endif
5103 gui.border_width = 0;
5104
5105 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 /* First try using the wide version, so that we can use any title.
5108 * Otherwise only characters in the active codepage will work. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005109 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005111 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 wndclassw.lpfnWndProc = _WndProc;
5113 wndclassw.cbClsExtra = 0;
5114 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005115 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5117 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5118 wndclassw.hbrBackground = s_brush;
5119 wndclassw.lpszMenuName = NULL;
5120 wndclassw.lpszClassName = szVimWndClassW;
5121
5122 if ((
5123#ifdef GLOBAL_IME
5124 atom =
5125#endif
5126 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005127 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (vim_parent_hwnd != NULL)
5131 {
5132#ifdef HAVE_TRY_EXCEPT
5133 __try
5134 {
5135#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005136 // Open inside the specified parent window.
5137 // TODO: last argument should point to a CLIENTCREATESTRUCT
5138 // structure.
5139 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005141 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005142 WS_OVERLAPPEDWINDOW | WS_CHILD
5143 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5145 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005146 100, // Any value will do
5147 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005149 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150#ifdef HAVE_TRY_EXCEPT
5151 }
5152 __except(EXCEPTION_EXECUTE_HANDLER)
5153 {
5154 /* NOP */
5155 }
5156#endif
5157 if (s_hwnd == NULL)
5158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005159 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 mch_exit(2);
5161 }
5162 }
5163 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005164 {
5165 /* If the provided windowid is not valid reset it to zero, so that it
5166 * is ignored and we open our own window. */
5167 if (IsWindow((HWND)win_socket_id) <= 0)
5168 win_socket_id = 0;
5169
5170 /* Create a window. If win_socket_id is not zero without border and
5171 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005172 s_hwnd = CreateWindowW(
5173 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005174 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5175 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005176 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5177 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5178 100, /* Any value will do */
5179 100, /* Any value will do */
5180 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005181 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005182 if (s_hwnd != NULL && win_socket_id != 0)
5183 {
5184 SetParent(s_hwnd, (HWND)win_socket_id);
5185 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5186 }
5187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 if (s_hwnd == NULL)
5190 return FAIL;
5191
5192#ifdef GLOBAL_IME
5193 global_ime_init(atom, s_hwnd);
5194#endif
5195#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5196 dyn_imm_load();
5197#endif
5198
5199 /* Create the text area window */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005200 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005201 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005202 wndclassw.style = CS_OWNDC;
5203 wndclassw.lpfnWndProc = _TextAreaWndProc;
5204 wndclassw.cbClsExtra = 0;
5205 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005206 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005207 wndclassw.hIcon = NULL;
5208 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5209 wndclassw.hbrBackground = NULL;
5210 wndclassw.lpszMenuName = NULL;
5211 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005212
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005213 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 return FAIL;
5215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005217 s_textArea = CreateWindowExW(
5218 0,
5219 szTextAreaClassW, L"Vim text area",
5220 WS_CHILD | WS_VISIBLE, 0, 0,
5221 100, // Any value will do for now
5222 100, // Any value will do for now
5223 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005224 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 if (s_textArea == NULL)
5227 return FAIL;
5228
Bram Moolenaar20321902016-02-17 12:30:17 +01005229#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005230 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5231 {
5232 HANDLE hIcon = NULL;
5233
5234 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005235 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005236 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005237#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#ifdef FEAT_MENU
5240 s_menuBar = CreateMenu();
5241#endif
5242 s_hdc = GetDC(s_textArea);
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 /* Do we need to bother with this? */
5247 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5248
5249 /* Get background/foreground colors from the system */
5250 gui_mch_def_colors();
5251
5252 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5253 * file) */
5254 set_normal_colors();
5255
5256 /*
5257 * Check that none of the colors are the same as the background color.
5258 * Then store the current values as the defaults.
5259 */
5260 gui_check_colors();
5261 gui.def_norm_pixel = gui.norm_pixel;
5262 gui.def_back_pixel = gui.back_pixel;
5263
5264 /* Get the colors for the highlight groups (gui_check_colors() might have
5265 * changed them) */
5266 highlight_gui_started();
5267
5268 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005269 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005271 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
5274 * Set up for Intellimouse processing
5275 */
5276 init_mouse_wheel();
5277
5278 /*
5279 * compute a couple of metrics used for the dialogs
5280 */
5281 get_dialog_font_metrics();
5282#ifdef FEAT_TOOLBAR
5283 /*
5284 * Create the toolbar
5285 */
5286 initialise_toolbar();
5287#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005288#ifdef FEAT_GUI_TABLINE
5289 /*
5290 * Create the tabline
5291 */
5292 initialise_tabline();
5293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#ifdef MSWIN_FIND_REPLACE
5295 /*
5296 * Initialise the dialog box stuff
5297 */
5298 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5299
5300 /* Initialise the struct */
5301 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005302 s_findrep_struct.lpstrFindWhat =
5303 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005305 s_findrep_struct.lpstrReplaceWith =
5306 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5308 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5309 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005312#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005313# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5314/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5315# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005316# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005317# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005318# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005319 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005320 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005321#endif
5322
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005323#ifdef FEAT_RENDER_OPTIONS
5324 if (p_rop)
5325 (void)gui_mch_set_rendering_options(p_rop);
5326#endif
5327
Bram Moolenaar748bf032005-02-02 23:04:36 +00005328theend:
5329 /* Display any pending error messages */
5330 display_errors();
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 return OK;
5333}
5334
5335/*
5336 * Get the size of the screen, taking position on multiple monitors into
5337 * account (if supported).
5338 */
5339 static void
5340get_work_area(RECT *spi_rect)
5341{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005342 HMONITOR mon;
5343 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005345 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005346 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005347 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005349 moninfo.cbSize = sizeof(MONITORINFO);
5350 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005352 *spi_rect = moninfo.rcWork;
5353 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355 }
5356 /* this is the old method... */
5357 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5358}
5359
5360/*
5361 * Set the size of the window to the given width and height in pixels.
5362 */
5363 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005364gui_mch_set_shellsize(
5365 int width,
5366 int height,
5367 int min_width UNUSED,
5368 int min_height UNUSED,
5369 int base_width UNUSED,
5370 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005371 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 RECT workarea_rect;
5374 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 WINDOWPLACEMENT wndpl;
5376
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005377 /* Try to keep window completely on screen. */
5378 /* Get position of the screen work area. This is the part that is not
5379 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 get_work_area(&workarea_rect);
5381
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005382 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005383 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 wndpl.length = sizeof(WINDOWPLACEMENT);
5385 GetWindowPlacement(s_hwnd, &wndpl);
5386
5387 /* Resizing a maximized window looks very strange, unzoom it first.
5388 * But don't do it when still starting up, it may have been requested in
5389 * the shortcut. */
5390 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5391 {
5392 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5393 /* Need to get the settings of the normal window. */
5394 GetWindowPlacement(s_hwnd, &wndpl);
5395 }
5396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005398 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005399 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005400 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005401 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 + GetSystemMetrics(SM_CYCAPTION)
5403#ifdef FEAT_MENU
5404 + gui_mswin_get_menu_height(FALSE)
5405#endif
5406 ;
5407
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005408 /* The following should take care of keeping Vim on the same monitor, no
5409 * matter if the secondary monitor is left or right of the primary
5410 * monitor. */
5411 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5412 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005413
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005414 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005415 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005416 && wndpl.rcNormalPosition.right > workarea_rect.right)
5417 OffsetRect(&wndpl.rcNormalPosition,
5418 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005420 if ((direction & RESIZE_HOR)
5421 && wndpl.rcNormalPosition.left < workarea_rect.left)
5422 OffsetRect(&wndpl.rcNormalPosition,
5423 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
Bram Moolenaarafa24992006-03-27 20:58:26 +00005425 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005426 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5427 OffsetRect(&wndpl.rcNormalPosition,
5428 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005430 if ((direction & RESIZE_VERT)
5431 && wndpl.rcNormalPosition.top < workarea_rect.top)
5432 OffsetRect(&wndpl.rcNormalPosition,
5433 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 /* set window position - we should use SetWindowPlacement rather than
5436 * SetWindowPos as the MSDN docs say the coord systems returned by
5437 * these two are not compatible. */
5438 SetWindowPlacement(s_hwnd, &wndpl);
5439
5440 SetActiveWindow(s_hwnd);
5441 SetFocus(s_hwnd);
5442
5443#ifdef FEAT_MENU
5444 /* Menu may wrap differently now */
5445 gui_mswin_get_menu_height(!gui.starting);
5446#endif
5447}
5448
5449
5450 void
5451gui_mch_set_scrollbar_thumb(
5452 scrollbar_T *sb,
5453 long val,
5454 long size,
5455 long max)
5456{
5457 SCROLLINFO info;
5458
5459 sb->scroll_shift = 0;
5460 while (max > 32767)
5461 {
5462 max = (max + 1) >> 1;
5463 val >>= 1;
5464 size >>= 1;
5465 ++sb->scroll_shift;
5466 }
5467
5468 if (sb->scroll_shift > 0)
5469 ++size;
5470
5471 info.cbSize = sizeof(info);
5472 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5473 info.nPos = val;
5474 info.nMin = 0;
5475 info.nMax = max;
5476 info.nPage = size;
5477 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5478}
5479
5480
5481/*
5482 * Set the current text font.
5483 */
5484 void
5485gui_mch_set_font(GuiFont font)
5486{
5487 gui.currFont = font;
5488}
5489
5490
5491/*
5492 * Set the current text foreground color.
5493 */
5494 void
5495gui_mch_set_fg_color(guicolor_T color)
5496{
5497 gui.currFgColor = color;
5498}
5499
5500/*
5501 * Set the current text background color.
5502 */
5503 void
5504gui_mch_set_bg_color(guicolor_T color)
5505{
5506 gui.currBgColor = color;
5507}
5508
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005509/*
5510 * Set the current text special color.
5511 */
5512 void
5513gui_mch_set_sp_color(guicolor_T color)
5514{
5515 gui.currSpColor = color;
5516}
5517
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005518#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519/*
5520 * Multi-byte handling, originally by Sung-Hoon Baek.
5521 * First static functions (no prototypes generated).
5522 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005523# ifdef _MSC_VER
5524# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5525# endif
5526# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 * handle WM_IME_NOTIFY message
5530 */
5531 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005532_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 LRESULT lResult = 0;
5535 HIMC hImc;
5536
5537 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5538 return lResult;
5539 switch (dwCommand)
5540 {
5541 case IMN_SETOPENSTATUS:
5542 if (pImmGetOpenStatus(hImc))
5543 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005544 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 im_set_position(gui.row, gui.col);
5546
5547 /* Disable langmap */
5548 State &= ~LANGMAP;
5549 if (State & INSERT)
5550 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005551#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 /* Unshown 'keymap' in status lines */
5553 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5554 {
5555 /* Save cursor position */
5556 int old_row = gui.row;
5557 int old_col = gui.col;
5558
5559 // This must be called here before
5560 // status_redraw_curbuf(), otherwise the mode
5561 // message may appear in the wrong position.
5562 showmode();
5563 status_redraw_curbuf();
5564 update_screen(0);
5565 /* Restore cursor position */
5566 gui.row = old_row;
5567 gui.col = old_col;
5568 }
5569#endif
5570 }
5571 }
5572 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005573 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 lResult = 0;
5575 break;
5576 }
5577 pImmReleaseContext(hWnd, hImc);
5578 return lResult;
5579}
5580
5581 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005582_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 char_u *ret;
5585 int len;
5586
5587 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5588 return 0;
5589
5590 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5591 if (ret != NULL)
5592 {
5593 add_to_input_buf_csi(ret, len);
5594 vim_free(ret);
5595 return 1;
5596 }
5597 return 0;
5598}
5599
5600/*
5601 * get the current composition string, in UCS-2; *lenp is the number of
5602 * *lenp is the number of Unicode characters.
5603 */
5604 static short_u *
5605GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5606{
5607 LONG ret;
5608 LPWSTR wbuf = NULL;
5609 char_u *buf;
5610
5611 if (!pImmGetContext)
5612 return NULL; /* no imm32.dll */
5613
5614 /* Try Unicode; this'll always work on NT regardless of codepage. */
5615 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5616 if (ret == 0)
5617 return NULL; /* empty */
5618
5619 if (ret > 0)
5620 {
5621 /* Allocate the requested buffer plus space for the NUL character. */
5622 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5623 if (wbuf != NULL)
5624 {
5625 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5626 *lenp = ret / sizeof(WCHAR);
5627 }
5628 return (short_u *)wbuf;
5629 }
5630
5631 /* ret < 0; we got an error, so try the ANSI version. This'll work
5632 * on 9x/ME, but only if the codepage happens to be set to whatever
5633 * we're inputting. */
5634 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5635 if (ret <= 0)
5636 return NULL; /* empty or error */
5637
5638 buf = alloc(ret);
5639 if (buf == NULL)
5640 return NULL;
5641 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5642
5643 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005644 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 vim_free(buf);
5646
5647 return (short_u *)wbuf;
5648}
5649
5650/*
5651 * void GetResultStr()
5652 *
5653 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5654 * get complete composition string
5655 */
5656 static char_u *
5657GetResultStr(HWND hwnd, int GCS, int *lenp)
5658{
5659 HIMC hIMC; /* Input context handle. */
5660 short_u *buf = NULL;
5661 char_u *convbuf = NULL;
5662
5663 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5664 return NULL;
5665
5666 /* Reads in the composition string. */
5667 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5668 if (buf == NULL)
5669 return NULL;
5670
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005671 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 pImmReleaseContext(hwnd, hIMC);
5673 vim_free(buf);
5674 return convbuf;
5675}
5676#endif
5677
5678/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005679#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681/*
5682 * set font to IM.
5683 */
5684 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005685im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 HIMC hImc;
5688
5689 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5690 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005691 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 pImmReleaseContext(s_hwnd, hImc);
5693 }
5694}
5695
5696/*
5697 * Notify cursor position to IM.
5698 */
5699 void
5700im_set_position(int row, int col)
5701{
5702 HIMC hImc;
5703
5704 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5705 {
5706 COMPOSITIONFORM cfs;
5707
5708 cfs.dwStyle = CFS_POINT;
5709 cfs.ptCurrentPos.x = FILL_X(col);
5710 cfs.ptCurrentPos.y = FILL_Y(row);
5711 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5712 pImmSetCompositionWindow(hImc, &cfs);
5713
5714 pImmReleaseContext(s_hwnd, hImc);
5715 }
5716}
5717
5718/*
5719 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5720 */
5721 void
5722im_set_active(int active)
5723{
5724 HIMC hImc;
5725 static HIMC hImcOld = (HIMC)0;
5726
5727 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5728 {
5729 if (p_imdisable)
5730 {
5731 if (hImcOld == (HIMC)0)
5732 {
5733 hImcOld = pImmGetContext(s_hwnd);
5734 if (hImcOld)
5735 pImmAssociateContext(s_hwnd, (HIMC)0);
5736 }
5737 active = FALSE;
5738 }
5739 else if (hImcOld != (HIMC)0)
5740 {
5741 pImmAssociateContext(s_hwnd, hImcOld);
5742 hImcOld = (HIMC)0;
5743 }
5744
5745 hImc = pImmGetContext(s_hwnd);
5746 if (hImc)
5747 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005748 /*
5749 * for Korean ime
5750 */
5751 HKL hKL = GetKeyboardLayout(0);
5752
5753 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5754 {
5755 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5756 static BOOL bSaved = FALSE;
5757
5758 if (active)
5759 {
5760 /* if we have a saved conversion status, restore it */
5761 if (bSaved)
5762 pImmSetConversionStatus(hImc, dwConversionSaved,
5763 dwSentenceSaved);
5764 bSaved = FALSE;
5765 }
5766 else
5767 {
5768 /* save conversion status and disable korean */
5769 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5770 &dwSentenceSaved))
5771 {
5772 bSaved = TRUE;
5773 pImmSetConversionStatus(hImc,
5774 dwConversionSaved & ~(IME_CMODE_NATIVE
5775 | IME_CMODE_FULLSHAPE),
5776 dwSentenceSaved);
5777 }
5778 }
5779 }
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 pImmSetOpenStatus(hImc, active);
5782 pImmReleaseContext(s_hwnd, hImc);
5783 }
5784 }
5785}
5786
5787/*
5788 * Get IM status. When IM is on, return not 0. Else return 0.
5789 */
5790 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005791im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 int status = 0;
5794 HIMC hImc;
5795
5796 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5797 {
5798 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5799 pImmReleaseContext(s_hwnd, hImc);
5800 }
5801 return status;
5802}
5803
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005804#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005806#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807/* Win32 with GLOBAL IME */
5808
5809/*
5810 * Notify cursor position to IM.
5811 */
5812 void
5813im_set_position(int row, int col)
5814{
5815 /* Win32 with GLOBAL IME */
5816 POINT p;
5817
5818 p.x = FILL_X(col);
5819 p.y = FILL_Y(row);
5820 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5821 global_ime_set_position(&p);
5822}
5823
5824/*
5825 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5826 */
5827 void
5828im_set_active(int active)
5829{
5830 global_ime_set_status(active);
5831}
5832
5833/*
5834 * Get IM status. When IM is on, return not 0. Else return 0.
5835 */
5836 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005837im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 return global_ime_get_status();
5840}
5841#endif
5842
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005843/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005844 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005845 */
5846 static void
5847latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5848{
5849 int c;
5850
Bram Moolenaarca003e12006-03-17 23:19:38 +00005851 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005852 {
5853 c = *text++;
5854 switch (c)
5855 {
5856 case 0xa4: c = 0x20ac; break; /* euro */
5857 case 0xa6: c = 0x0160; break; /* S hat */
5858 case 0xa8: c = 0x0161; break; /* S -hat */
5859 case 0xb4: c = 0x017d; break; /* Z hat */
5860 case 0xb8: c = 0x017e; break; /* Z -hat */
5861 case 0xbc: c = 0x0152; break; /* OE */
5862 case 0xbd: c = 0x0153; break; /* oe */
5863 case 0xbe: c = 0x0178; break; /* Y */
5864 }
5865 *unicodebuf++ = c;
5866 }
5867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868
5869#ifdef FEAT_RIGHTLEFT
5870/*
5871 * What is this for? In the case where you are using Win98 or Win2K or later,
5872 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5873 * reverses the string sent to the TextOut... family. This sucks, because we
5874 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5875 * way to tell Windblows not to do this!
5876 *
5877 * The short of it is that this 'RevOut' only gets called if you are running
5878 * one of the new, "improved" MS OSes, and only if you are running in
5879 * 'rightleft' mode. It makes display take *slightly* longer, but not
5880 * noticeably so.
5881 */
5882 static void
5883RevOut( HDC s_hdc,
5884 int col,
5885 int row,
5886 UINT foptions,
5887 CONST RECT *pcliprect,
5888 LPCTSTR text,
5889 UINT len,
5890 CONST INT *padding)
5891{
5892 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005894 for (ix = 0; ix < (int)len; ++ix)
5895 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5896 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897}
5898#endif
5899
Bram Moolenaar92467d32017-12-05 13:22:16 +01005900 static void
5901draw_line(
5902 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005903 int y1,
5904 int x2,
5905 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005906 COLORREF color)
5907{
5908#if defined(FEAT_DIRECTX)
5909 if (IS_ENABLE_DIRECTX())
5910 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5911 else
5912#endif
5913 {
5914 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5915 HPEN old_pen = SelectObject(s_hdc, hpen);
5916 MoveToEx(s_hdc, x1, y1, NULL);
5917 /* Note: LineTo() excludes the last pixel in the line. */
5918 LineTo(s_hdc, x2, y2);
5919 DeleteObject(SelectObject(s_hdc, old_pen));
5920 }
5921}
5922
5923 static void
5924set_pixel(
5925 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005926 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005927 COLORREF color)
5928{
5929#if defined(FEAT_DIRECTX)
5930 if (IS_ENABLE_DIRECTX())
5931 DWriteContext_SetPixel(s_dwc, x, y, color);
5932 else
5933#endif
5934 SetPixel(s_hdc, x, y, color);
5935}
5936
5937 static void
5938fill_rect(
5939 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005940 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005941 COLORREF color)
5942{
5943#if defined(FEAT_DIRECTX)
5944 if (IS_ENABLE_DIRECTX())
5945 DWriteContext_FillRect(s_dwc, rcp, color);
5946 else
5947#endif
5948 {
5949 HBRUSH hbr2;
5950
5951 if (hbr == NULL)
5952 hbr2 = CreateSolidBrush(color);
5953 else
5954 hbr2 = hbr;
5955 FillRect(s_hdc, rcp, hbr2);
5956 if (hbr == NULL)
5957 DeleteBrush(hbr2);
5958 }
5959}
5960
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 void
5962gui_mch_draw_string(
5963 int row,
5964 int col,
5965 char_u *text,
5966 int len,
5967 int flags)
5968{
5969 static int *padding = NULL;
5970 static int pad_size = 0;
5971 int i;
5972 const RECT *pcliprect = NULL;
5973 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 static WCHAR *unicodebuf = NULL;
5975 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005976 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 int y;
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 /*
5981 * Italic and bold text seems to have an extra row of pixels at the bottom
5982 * (below where the bottom of the character should be). If we draw the
5983 * characters with a solid background, the top row of pixels in the
5984 * character below will be overwritten. We can fix this by filling in the
5985 * background ourselves, to the correct character proportions, and then
5986 * writing the character in transparent mode. Still have a problem when
5987 * the character is "_", which gets written on to the character below.
5988 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5989 * pixel in their slots, which fixes the problem with the bottom row of
5990 * pixels. We still need this code because otherwise the top row of pixels
5991 * becomes a problem. - webb.
5992 */
5993 static HBRUSH hbr_cache[2] = {NULL, NULL};
5994 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5995 static int brush_lru = 0;
5996 HBRUSH hbr;
5997 RECT rc;
5998
5999 if (!(flags & DRAW_TRANSP))
6000 {
6001 /*
6002 * Clear background first.
6003 * Note: FillRect() excludes right and bottom of rectangle.
6004 */
6005 rc.left = FILL_X(col);
6006 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (has_mbyte)
6008 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006010 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 }
6012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 rc.right = FILL_X(col + len);
6014 rc.bottom = FILL_Y(row + 1);
6015
6016 /* Cache the created brush, that saves a lot of time. We need two:
6017 * one for cursor background and one for the normal background. */
6018 if (gui.currBgColor == brush_color[0])
6019 {
6020 hbr = hbr_cache[0];
6021 brush_lru = 1;
6022 }
6023 else if (gui.currBgColor == brush_color[1])
6024 {
6025 hbr = hbr_cache[1];
6026 brush_lru = 0;
6027 }
6028 else
6029 {
6030 if (hbr_cache[brush_lru] != NULL)
6031 DeleteBrush(hbr_cache[brush_lru]);
6032 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6033 brush_color[brush_lru] = gui.currBgColor;
6034 hbr = hbr_cache[brush_lru];
6035 brush_lru = !brush_lru;
6036 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006037
Bram Moolenaar92467d32017-12-05 13:22:16 +01006038 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039
6040 SetBkMode(s_hdc, TRANSPARENT);
6041
6042 /*
6043 * When drawing block cursor, prevent inverted character spilling
6044 * over character cell (can happen with bold/italic)
6045 */
6046 if (flags & DRAW_CURSOR)
6047 {
6048 pcliprect = &rc;
6049 foptions = ETO_CLIPPED;
6050 }
6051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 SetTextColor(s_hdc, gui.currFgColor);
6053 SelectFont(s_hdc, gui.currFont);
6054
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006055#ifdef FEAT_DIRECTX
6056 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006057 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006058#endif
6059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6061 {
6062 vim_free(padding);
6063 pad_size = Columns;
6064
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006065 /* Don't give an out-of-memory message here, it would call us
6066 * recursively. */
6067 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 if (padding != NULL)
6069 for (i = 0; i < pad_size; i++)
6070 padding[i] = gui.char_width;
6071 }
6072
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 /*
6074 * We have to provide the padding argument because italic and bold versions
6075 * of fixed-width fonts are often one pixel or so wider than their normal
6076 * versions.
6077 * No check for DRAW_BOLD, Windows will have done it already.
6078 */
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 /* Check if there are any UTF-8 characters. If not, use normal text
6081 * output to speed up output. */
6082 if (enc_utf8)
6083 for (n = 0; n < len; ++n)
6084 if (text[n] >= 0x80)
6085 break;
6086
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006087#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006088 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6089 * required that unicode drawing routine, currently. So this forces it
6090 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006091 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006092 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006093#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006096 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006098 if ((enc_utf8
6099 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6100 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 && (unicodebuf == NULL || len > unibuflen))
6102 {
6103 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006104 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006107 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 unibuflen = len;
6110 }
6111
6112 if (enc_utf8 && n < len && unicodebuf != NULL)
6113 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006114 /* Output UTF-8 characters. Composing characters should be
6115 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006116 int i;
6117 int wlen; /* string length in words */
6118 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 int cells; /* cell width of string up to composing char */
6120 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006121 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006123 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006124 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006126 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006128 c = utf_ptr2char(text + i);
6129 if (c >= 0x10000)
6130 {
6131 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006132 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6133 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006134 }
6135 else
6136 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006137 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006138 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006139
6140 if (utf_iscomposing(c))
6141 cw = 0;
6142 else
6143 {
6144 cw = utf_char2cells(c);
6145 if (cw > 2) /* don't use 4 for unprintable char */
6146 cw = 1;
6147 }
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (unicodepdy != NULL)
6150 {
6151 /* Use unicodepdy to make characters fit as we expect, even
6152 * when the font uses different widths (e.g., bold character
6153 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006154 if (c >= 0x10000)
6155 {
6156 unicodepdy[wlen - 2] = cw * gui.char_width;
6157 unicodepdy[wlen - 1] = 0;
6158 }
6159 else
6160 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
6162 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006163 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006164 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006166#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006167 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006168 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006169 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006170 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006171 TEXT_X(col), TEXT_Y(row),
6172 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006173 gui.char_width, gui.currFgColor,
6174 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006175 }
6176 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006177#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006178 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6179 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 len = cells; /* used for underlining */
6181 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006182 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
6184 /* If we want to display codepage data, and the current CP is not the
6185 * ANSI one, we need to go via Unicode. */
6186 if (unicodebuf != NULL)
6187 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006188 if (enc_latin9)
6189 latin9_to_ucs(text, len, unicodebuf);
6190 else
6191 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 MB_PRECOMPOSED,
6193 (char *)text, len,
6194 (LPWSTR)unicodebuf, unibuflen);
6195 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006196 {
6197 /* Use unicodepdy to make characters fit as we expect, even
6198 * when the font uses different widths (e.g., bold character
6199 * is wider). */
6200 if (unicodepdy != NULL)
6201 {
6202 int i;
6203 int cw;
6204
6205 for (i = 0; i < len; ++i)
6206 {
6207 cw = utf_char2cells(unicodebuf[i]);
6208 if (cw > 2)
6209 cw = 1;
6210 unicodepdy[i] = cw * gui.char_width;
6211 }
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006214 foptions, pcliprect, unicodebuf, len, unicodepdy);
6215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217 }
6218 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006221 /* Windows will mess up RL text, so we have to draw it character by
6222 * character. Only do this if RL is on, since it's slow. */
6223 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6225 foptions, pcliprect, (char *)text, len, padding);
6226 else
6227#endif
6228 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6229 foptions, pcliprect, (char *)text, len, padding);
6230 }
6231
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006232 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (flags & DRAW_UNDERL)
6234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 /* When p_linespace is 0, overwrite the bottom row of pixels.
6236 * Otherwise put the line just below the character. */
6237 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 if (p_linespace > 1)
6239 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006240 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006242
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006243 /* Strikethrough */
6244 if (flags & DRAW_STRIKE)
6245 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006246 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006247 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006248 }
6249
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006250 /* Undercurl */
6251 if (flags & DRAW_UNDERC)
6252 {
6253 int x;
6254 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006255 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006256
6257 y = FILL_Y(row + 1) - 1;
6258 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6259 {
6260 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006261 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006262 }
6263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264}
6265
6266
6267/*
6268 * Output routines.
6269 */
6270
6271/* Flush any output to the screen */
6272 void
6273gui_mch_flush(void)
6274{
6275# if defined(__BORLANDC__)
6276 /*
6277 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6278 * prototype declaration.
6279 * The compiler complains if __stdcall is not used in both declarations.
6280 */
6281 BOOL __stdcall GdiFlush(void);
6282# endif
6283
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006284#if defined(FEAT_DIRECTX)
6285 if (IS_ENABLE_DIRECTX())
6286 DWriteContext_Flush(s_dwc);
6287#endif
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 GdiFlush();
6290}
6291
6292 static void
6293clear_rect(RECT *rcp)
6294{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006295 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296}
6297
6298
Bram Moolenaarc716c302006-01-21 22:12:51 +00006299 void
6300gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6301{
6302 RECT workarea_rect;
6303
6304 get_work_area(&workarea_rect);
6305
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006306 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006307 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006308 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006309
6310 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6311 * the menubar for MSwin, we subtract it from the screen height, so that
6312 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006313 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006314 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006315 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006316 - GetSystemMetrics(SM_CYCAPTION)
6317#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006318 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006319#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006320 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006321}
6322
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324#if defined(FEAT_MENU) || defined(PROTO)
6325/*
6326 * Add a sub menu to the menu bar.
6327 */
6328 void
6329gui_mch_add_menu(
6330 vimmenu_T *menu,
6331 int pos)
6332{
6333 vimmenu_T *parent = menu->parent;
6334
6335 menu->submenu_id = CreatePopupMenu();
6336 menu->id = s_menu_id++;
6337
6338 if (menu_is_menubar(menu->name))
6339 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006340 WCHAR *wn;
6341 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006343 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006344 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006345 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006347 infow.cbSize = sizeof(infow);
6348 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6349 | MIIM_SUBMENU;
6350 infow.dwItemData = (long_u)menu;
6351 infow.wID = menu->id;
6352 infow.fType = MFT_STRING;
6353 infow.dwTypeData = wn;
6354 infow.cch = (UINT)wcslen(wn);
6355 infow.hSubMenu = menu->submenu_id;
6356 InsertMenuItemW((parent == NULL)
6357 ? s_menuBar : parent->submenu_id,
6358 (UINT)pos, TRUE, &infow);
6359 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361
6362 /* Fix window size if menu may have wrapped */
6363 if (parent == NULL)
6364 gui_mswin_get_menu_height(!gui.starting);
6365#ifdef FEAT_TEAROFF
6366 else if (IsWindow(parent->tearoff_handle))
6367 rebuild_tearoff(parent);
6368#endif
6369}
6370
6371 void
6372gui_mch_show_popupmenu(vimmenu_T *menu)
6373{
6374 POINT mp;
6375
6376 (void)GetCursorPos((LPPOINT)&mp);
6377 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6378}
6379
6380 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006381gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 vimmenu_T *menu = gui_find_menu(path_name);
6384
6385 if (menu != NULL)
6386 {
6387 POINT p;
6388
6389 /* Find the position of the current cursor */
6390 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006391 if (mouse_pos)
6392 {
6393 int mx, my;
6394
6395 gui_mch_getmouse(&mx, &my);
6396 p.x += mx;
6397 p.y += my;
6398 }
6399 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006401 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6403 }
6404 msg_scroll = FALSE;
6405 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6406 }
6407}
6408
6409#if defined(FEAT_TEAROFF) || defined(PROTO)
6410/*
6411 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6412 * create it as a pseudo-"tearoff menu".
6413 */
6414 void
6415gui_make_tearoff(char_u *path_name)
6416{
6417 vimmenu_T *menu = gui_find_menu(path_name);
6418
6419 /* Found the menu, so tear it off. */
6420 if (menu != NULL)
6421 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6422}
6423#endif
6424
6425/*
6426 * Add a menu item to a menu
6427 */
6428 void
6429gui_mch_add_menu_item(
6430 vimmenu_T *menu,
6431 int idx)
6432{
6433 vimmenu_T *parent = menu->parent;
6434
6435 menu->id = s_menu_id++;
6436 menu->submenu_id = NULL;
6437
6438#ifdef FEAT_TEAROFF
6439 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6440 {
6441 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6442 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6443 }
6444 else
6445#endif
6446#ifdef FEAT_TOOLBAR
6447 if (menu_is_toolbar(parent->name))
6448 {
6449 TBBUTTON newtb;
6450
6451 vim_memset(&newtb, 0, sizeof(newtb));
6452 if (menu_is_separator(menu->name))
6453 {
6454 newtb.iBitmap = 0;
6455 newtb.fsStyle = TBSTYLE_SEP;
6456 }
6457 else
6458 {
6459 newtb.iBitmap = get_toolbar_bitmap(menu);
6460 newtb.fsStyle = TBSTYLE_BUTTON;
6461 }
6462 newtb.idCommand = menu->id;
6463 newtb.fsState = TBSTATE_ENABLED;
6464 newtb.iString = 0;
6465 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6466 (LPARAM)&newtb);
6467 menu->submenu_id = (HMENU)-1;
6468 }
6469 else
6470#endif
6471 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006472 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006474 wn = enc_to_utf16(menu->name, NULL);
6475 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006477 InsertMenuW(parent->submenu_id, (UINT)idx,
6478 (menu_is_separator(menu->name)
6479 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6480 (UINT)menu->id, wn);
6481 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483#ifdef FEAT_TEAROFF
6484 if (IsWindow(parent->tearoff_handle))
6485 rebuild_tearoff(parent);
6486#endif
6487 }
6488}
6489
6490/*
6491 * Destroy the machine specific menu widget.
6492 */
6493 void
6494gui_mch_destroy_menu(vimmenu_T *menu)
6495{
6496#ifdef FEAT_TOOLBAR
6497 /*
6498 * is this a toolbar button?
6499 */
6500 if (menu->submenu_id == (HMENU)-1)
6501 {
6502 int iButton;
6503
6504 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6505 (WPARAM)menu->id, 0);
6506 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6507 }
6508 else
6509#endif
6510 {
6511 if (menu->parent != NULL
6512 && menu_is_popup(menu->parent->dname)
6513 && menu->parent->submenu_id != NULL)
6514 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6515 else
6516 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6517 if (menu->submenu_id != NULL)
6518 DestroyMenu(menu->submenu_id);
6519#ifdef FEAT_TEAROFF
6520 if (IsWindow(menu->tearoff_handle))
6521 DestroyWindow(menu->tearoff_handle);
6522 if (menu->parent != NULL
6523 && menu->parent->children != NULL
6524 && IsWindow(menu->parent->tearoff_handle))
6525 {
6526 /* This menu must not show up when rebuilding the tearoff window. */
6527 menu->modes = 0;
6528 rebuild_tearoff(menu->parent);
6529 }
6530#endif
6531 }
6532}
6533
6534#ifdef FEAT_TEAROFF
6535 static void
6536rebuild_tearoff(vimmenu_T *menu)
6537{
6538 /*hackish*/
6539 char_u tbuf[128];
6540 RECT trect;
6541 RECT rct;
6542 RECT roct;
6543 int x, y;
6544
6545 HWND thwnd = menu->tearoff_handle;
6546
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006547 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (GetWindowRect(thwnd, &trect)
6549 && GetWindowRect(s_hwnd, &rct)
6550 && GetClientRect(s_hwnd, &roct))
6551 {
6552 x = trect.left - rct.left;
6553 y = (trect.top - rct.bottom + roct.bottom);
6554 }
6555 else
6556 {
6557 x = y = 0xffffL;
6558 }
6559 DestroyWindow(thwnd);
6560 if (menu->children != NULL)
6561 {
6562 gui_mch_tearoff(tbuf, menu, x, y);
6563 if (IsWindow(menu->tearoff_handle))
6564 (void) SetWindowPos(menu->tearoff_handle,
6565 NULL,
6566 (int)trect.left,
6567 (int)trect.top,
6568 0, 0,
6569 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6570 }
6571}
6572#endif /* FEAT_TEAROFF */
6573
6574/*
6575 * Make a menu either grey or not grey.
6576 */
6577 void
6578gui_mch_menu_grey(
6579 vimmenu_T *menu,
6580 int grey)
6581{
6582#ifdef FEAT_TOOLBAR
6583 /*
6584 * is this a toolbar button?
6585 */
6586 if (menu->submenu_id == (HMENU)-1)
6587 {
6588 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6589 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6590 }
6591 else
6592#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006593 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6594 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596#ifdef FEAT_TEAROFF
6597 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6598 {
6599 WORD menuID;
6600 HWND menuHandle;
6601
6602 /*
6603 * A tearoff button has changed state.
6604 */
6605 if (menu->children == NULL)
6606 menuID = (WORD)(menu->id);
6607 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006608 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6610 if (menuHandle)
6611 EnableWindow(menuHandle, !grey);
6612
6613 }
6614#endif
6615}
6616
6617#endif /* FEAT_MENU */
6618
6619
6620/* define some macros used to make the dialogue creation more readable */
6621
6622#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6623#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006624#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
6626#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6627/*
6628 * stuff for dialogs
6629 */
6630
6631/*
6632 * The callback routine used by all the dialogs. Very simple. First,
6633 * acknowledges the INITDIALOG message so that Windows knows to do standard
6634 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6635 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6636 * number.
6637 */
6638 static LRESULT CALLBACK
6639dialog_callback(
6640 HWND hwnd,
6641 UINT message,
6642 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006643 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 if (message == WM_INITDIALOG)
6646 {
6647 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6648 /* Set focus to the dialog. Set the default button, if specified. */
6649 (void)SetFocus(hwnd);
6650 if (dialog_default_button > IDCANCEL)
6651 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006652 else
6653 /* We don't have a default, set focus on another element of the
6654 * dialog window, probably the icon */
6655 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 return FALSE;
6657 }
6658
6659 if (message == WM_COMMAND)
6660 {
6661 int button = LOWORD(wParam);
6662
6663 /* Don't end the dialog if something was selected that was
6664 * not a button.
6665 */
6666 if (button >= DLG_NONBUTTON_CONTROL)
6667 return TRUE;
6668
6669 /* If the edit box exists, copy the string. */
6670 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006671 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006672 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6673 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006674
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006675 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6676 p = utf16_to_enc(wp, NULL);
6677 vim_strncpy(s_textfield, p, IOSIZE);
6678 vim_free(p);
6679 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681
6682 /*
6683 * Need to check for IDOK because if the user just hits Return to
6684 * accept the default value, some reason this is what we get.
6685 */
6686 if (button == IDOK)
6687 {
6688 if (dialog_default_button > IDCANCEL)
6689 EndDialog(hwnd, dialog_default_button);
6690 }
6691 else
6692 EndDialog(hwnd, button - IDCANCEL);
6693 return TRUE;
6694 }
6695
6696 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6697 {
6698 EndDialog(hwnd, 0);
6699 return TRUE;
6700 }
6701 return FALSE;
6702}
6703
6704/*
6705 * Create a dialog dynamically from the parameter strings.
6706 * type = type of dialog (question, alert, etc.)
6707 * title = dialog title. may be NULL for default title.
6708 * message = text to display. Dialog sizes to accommodate it.
6709 * buttons = '\n' separated list of button captions, default first.
6710 * dfltbutton = number of default button.
6711 *
6712 * This routine returns 1 if the first button is pressed,
6713 * 2 for the second, etc.
6714 *
6715 * 0 indicates Esc was pressed.
6716 * -1 for unexpected error
6717 *
6718 * If stubbing out this fn, return 1.
6719 */
6720
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006721static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 "IDR_VIM",
6724 "IDR_VIM_ERROR",
6725 "IDR_VIM_ALERT",
6726 "IDR_VIM_INFO",
6727 "IDR_VIM_QUESTION"
6728};
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 int
6731gui_mch_dialog(
6732 int type,
6733 char_u *title,
6734 char_u *message,
6735 char_u *buttons,
6736 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006737 char_u *textfield,
6738 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006741 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 int numButtons;
6743 int *buttonWidths, *buttonPositions;
6744 int buttonYpos;
6745 int nchar, i;
6746 DWORD lStyle;
6747 int dlgwidth = 0;
6748 int dlgheight;
6749 int editboxheight;
6750 int horizWidth = 0;
6751 int msgheight;
6752 char_u *pstart;
6753 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006754 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 char_u *tbuffer;
6756 RECT rect;
6757 HWND hwnd;
6758 HDC hdc;
6759 HFONT font, oldFont;
6760 TEXTMETRIC fontInfo;
6761 int fontHeight;
6762 int textWidth, minButtonWidth, messageWidth;
6763 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006764 int maxDialogHeight;
6765 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 int vertical;
6767 int dlgPaddingX;
6768 int dlgPaddingY;
6769#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006770 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 int use_lfSysmenu = FALSE;
6772#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006773 garray_T ga;
6774 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776#ifndef NO_CONSOLE
6777 /* Don't output anything in silent mode ("ex -s") */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006778# ifdef VIMDLL
6779 if (!(gui.in_use || gui.starting))
6780# endif
6781 if (silent_mode)
6782 return dfltbutton; /* return default option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783#endif
6784
Bram Moolenaar748bf032005-02-02 23:04:36 +00006785 if (s_hwnd == NULL)
6786 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788 if ((type < 0) || (type > VIM_LAST_TYPE))
6789 type = 0;
6790
6791 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006792 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006793 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006794 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
6796 if (p == NULL)
6797 return -1;
6798
6799 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006800 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6802 * const.
6803 */
6804 tbuffer = vim_strsave(buttons);
6805 if (tbuffer == NULL)
6806 return -1;
6807
6808 --dfltbutton; /* Change from one-based to zero-based */
6809
6810 /* Count buttons */
6811 numButtons = 1;
6812 for (i = 0; tbuffer[i] != '\0'; i++)
6813 {
6814 if (tbuffer[i] == DLG_BUTTON_SEP)
6815 numButtons++;
6816 }
6817 if (dfltbutton >= numButtons)
6818 dfltbutton = -1;
6819
6820 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006821 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (buttonWidths == NULL)
6823 return -1;
6824
6825 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006826 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 if (buttonPositions == NULL)
6828 return -1;
6829
6830 /*
6831 * Calculate how big the dialog must be.
6832 */
6833 hwnd = GetDesktopWindow();
6834 hdc = GetWindowDC(hwnd);
6835#ifdef USE_SYSMENU_FONT
6836 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6837 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006838 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 use_lfSysmenu = TRUE;
6840 }
6841 else
6842#endif
6843 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6844 VARIABLE_PITCH , DLG_FONT_NAME);
6845 if (s_usenewlook)
6846 {
6847 oldFont = SelectFont(hdc, font);
6848 dlgPaddingX = DLG_PADDING_X;
6849 dlgPaddingY = DLG_PADDING_Y;
6850 }
6851 else
6852 {
6853 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6854 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6855 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6856 }
6857 GetTextMetrics(hdc, &fontInfo);
6858 fontHeight = fontInfo.tmHeight;
6859
6860 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006861 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006864 if (s_hwnd == NULL)
6865 {
6866 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
Bram Moolenaarc716c302006-01-21 22:12:51 +00006868 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006869 get_work_area(&workarea_rect);
6870 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6871 if (maxDialogWidth > 600)
6872 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006873 /* Leave some room for the taskbar. */
6874 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006875 }
6876 else
6877 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006878 /* Use our own window for the size, unless it's very small. */
6879 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006880 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006881 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006882 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006883 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6884 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006885
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006887 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006888 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006889 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006890 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6891 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6892 }
6893
6894 /* Set dlgwidth to width of message.
6895 * Copy the message into "ga", changing NL to CR-NL and inserting line
6896 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 pstart = message;
6898 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006899 msgheight = 0;
6900 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 do
6902 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006903 msgheight += fontHeight; /* at least one line */
6904
6905 /* Need to figure out where to break the string. The system does it
6906 * at a word boundary, which would mean we can't compute the number of
6907 * wrapped lines. */
6908 textWidth = 0;
6909 last_white = NULL;
6910 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006911 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006912 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006913 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006914 && textWidth > maxDialogWidth * 3 / 4)
6915 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006916 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006917 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006918 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006919 /* Line will wrap. */
6920 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006921 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006922 textWidth = 0;
6923
6924 if (last_white != NULL)
6925 {
6926 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006927 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006928 pend = last_white + 1;
6929 last_white = NULL;
6930 }
6931 ga_append(&ga, '\r');
6932 ga_append(&ga, '\n');
6933 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006934 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935
6936 while (--l >= 0)
6937 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006938 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006939 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006941
6942 ga_append(&ga, '\r');
6943 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 pstart = pend + 1;
6945 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006946
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006947 if (ga.ga_data != NULL)
6948 message = ga.ga_data;
6949
Bram Moolenaar748bf032005-02-02 23:04:36 +00006950 messageWidth += 10; /* roundoff space */
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006953 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6954 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (msgheight < DLG_ICON_HEIGHT)
6957 msgheight = DLG_ICON_HEIGHT;
6958
6959 /*
6960 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006961 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006963 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (!vertical)
6965 {
6966 // Place buttons horizontally if they fit.
6967 horizWidth = dlgPaddingX;
6968 pstart = tbuffer;
6969 i = 0;
6970 do
6971 {
6972 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6973 if (pend == NULL)
6974 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006975 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (textWidth < minButtonWidth)
6977 textWidth = minButtonWidth;
6978 textWidth += dlgPaddingX; /* Padding within button */
6979 buttonWidths[i] = textWidth;
6980 buttonPositions[i++] = horizWidth;
6981 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6982 pstart = pend + 1;
6983 } while (*pend != NUL);
6984
6985 if (horizWidth > maxDialogWidth)
6986 vertical = TRUE; // Too wide to fit on the screen.
6987 else if (horizWidth > dlgwidth)
6988 dlgwidth = horizWidth;
6989 }
6990
6991 if (vertical)
6992 {
6993 // Stack buttons vertically.
6994 pstart = tbuffer;
6995 do
6996 {
6997 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6998 if (pend == NULL)
6999 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007000 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 textWidth += dlgPaddingX; /* Padding within button */
7002 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7003 if (textWidth > dlgwidth)
7004 dlgwidth = textWidth;
7005 pstart = pend + 1;
7006 } while (*pend != NUL);
7007 }
7008
7009 if (dlgwidth < DLG_MIN_WIDTH)
7010 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7011
7012 /* start to fill in the dlgtemplate information. addressing by WORDs */
7013 if (s_usenewlook)
7014 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7015 else
7016 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7017
7018 add_long(lStyle);
7019 add_long(0); // (lExtendedStyle)
7020 pnumitems = p; /*save where the number of items must be stored*/
7021 add_word(0); // NumberOfItems(will change later)
7022 add_word(10); // x
7023 add_word(10); // y
7024 add_word(PixelToDialogX(dlgwidth)); // cx
7025
7026 // Dialog height.
7027 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007028 dlgheight = msgheight + 2 * dlgPaddingY
7029 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 else
7031 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7032
7033 // Dialog needs to be taller if contains an edit box.
7034 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7035 if (textfield != NULL)
7036 dlgheight += editboxheight;
7037
Bram Moolenaara95d8232013-08-07 15:27:11 +02007038 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7039 if (dlgheight > maxDialogHeight)
7040 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007041 msgheight = msgheight - (dlgheight - maxDialogHeight);
7042 dlgheight = maxDialogHeight;
7043 scroll_flag = WS_VSCROLL;
7044 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7045 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007046 }
7047
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 add_word(PixelToDialogY(dlgheight));
7049
7050 add_word(0); // Menu
7051 add_word(0); // Class
7052
7053 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007054 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7055 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 p += nchar;
7057
7058 if (s_usenewlook)
7059 {
7060 /* do the font, since DS_3DLOOK doesn't work properly */
7061#ifdef USE_SYSMENU_FONT
7062 if (use_lfSysmenu)
7063 {
7064 /* point size */
7065 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7066 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007067 wcscpy(p, lfSysmenu.lfFaceName);
7068 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
7070 else
7071#endif
7072 {
7073 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007074 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
7076 p += nchar;
7077 }
7078
7079 buttonYpos = msgheight + 2 * dlgPaddingY;
7080
7081 if (textfield != NULL)
7082 buttonYpos += editboxheight;
7083
7084 pstart = tbuffer;
7085 if (!vertical)
7086 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7087 for (i = 0; i < numButtons; i++)
7088 {
7089 /* get end of this button. */
7090 for ( pend = pstart;
7091 *pend && (*pend != DLG_BUTTON_SEP);
7092 pend++)
7093 ;
7094
7095 if (*pend)
7096 *pend = '\0';
7097
7098 /*
7099 * old NOTE:
7100 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7101 * the focus to the first tab-able button and in so doing makes that
7102 * the default!! Grrr. Workaround: Make the default button the only
7103 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7104 * he/she can use arrow keys.
7105 *
7106 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007107 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 * dialog. Also needed for when the textfield is the default control.
7109 * It appears to work now (perhaps not on Win95?).
7110 */
7111 if (vertical)
7112 {
7113 p = add_dialog_element(p,
7114 (i == dfltbutton
7115 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7116 PixelToDialogX(DLG_VERT_PADDING_X),
7117 PixelToDialogY(buttonYpos /* TBK */
7118 + 2 * fontHeight * i),
7119 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7120 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007121 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 }
7123 else
7124 {
7125 p = add_dialog_element(p,
7126 (i == dfltbutton
7127 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7128 PixelToDialogX(horizWidth + buttonPositions[i]),
7129 PixelToDialogY(buttonYpos), /* TBK */
7130 PixelToDialogX(buttonWidths[i]),
7131 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007132 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134 pstart = pend + 1; /*next button*/
7135 }
7136 *pnumitems += numButtons;
7137
7138 /* Vim icon */
7139 p = add_dialog_element(p, SS_ICON,
7140 PixelToDialogX(dlgPaddingX),
7141 PixelToDialogY(dlgPaddingY),
7142 PixelToDialogX(DLG_ICON_WIDTH),
7143 PixelToDialogY(DLG_ICON_HEIGHT),
7144 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7145 dlg_icons[type]);
7146
Bram Moolenaar748bf032005-02-02 23:04:36 +00007147 /* Dialog message */
7148 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7149 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7150 PixelToDialogY(dlgPaddingY),
7151 (WORD)(PixelToDialogX(messageWidth) + 1),
7152 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007153 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 /* Edit box */
7156 if (textfield != NULL)
7157 {
7158 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7159 PixelToDialogX(2 * dlgPaddingX),
7160 PixelToDialogY(2 * dlgPaddingY + msgheight),
7161 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7162 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007163 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 *pnumitems += 1;
7165 }
7166
7167 *pnumitems += 2;
7168
7169 SelectFont(hdc, oldFont);
7170 DeleteObject(font);
7171 ReleaseDC(hwnd, hdc);
7172
7173 /* Let the dialog_callback() function know which button to make default
7174 * If we have an edit box, make that the default. We also need to tell
7175 * dialog_callback() if this dialog contains an edit box or not. We do
7176 * this by setting s_textfield if it does.
7177 */
7178 if (textfield != NULL)
7179 {
7180 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7181 s_textfield = textfield;
7182 }
7183 else
7184 {
7185 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7186 s_textfield = NULL;
7187 }
7188
7189 /* show the dialog box modally and get a return value */
7190 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007191 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 (LPDLGTEMPLATE)pdlgtemplate,
7193 s_hwnd,
7194 (DLGPROC)dialog_callback);
7195
7196 LocalFree(LocalHandle(pdlgtemplate));
7197 vim_free(tbuffer);
7198 vim_free(buttonWidths);
7199 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007200 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202 /* Focus back to our window (for when MDI is used). */
7203 (void)SetFocus(s_hwnd);
7204
7205 return nchar;
7206}
7207
7208#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210/*
7211 * Put a simple element (basic class) onto a dialog template in memory.
7212 * return a pointer to where the next item should be added.
7213 *
7214 * parameters:
7215 * lStyle = additional style flags
7216 * (be careful, NT3.51 & Win32s will ignore the new ones)
7217 * x,y = x & y positions IN DIALOG UNITS
7218 * w,h = width and height IN DIALOG UNITS
7219 * Id = ID used in messages
7220 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7221 * caption = usually text or resource name
7222 *
7223 * TODO: use the length information noted here to enable the dialog creation
7224 * routines to work out more exactly how much memory they need to alloc.
7225 */
7226 static PWORD
7227add_dialog_element(
7228 PWORD p,
7229 DWORD lStyle,
7230 WORD x,
7231 WORD y,
7232 WORD w,
7233 WORD h,
7234 WORD Id,
7235 WORD clss,
7236 const char *caption)
7237{
7238 int nchar;
7239
7240 p = lpwAlign(p); /* Align to dword boundary*/
7241 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7242 *p++ = LOWORD(lStyle);
7243 *p++ = HIWORD(lStyle);
7244 *p++ = 0; // LOWORD (lExtendedStyle)
7245 *p++ = 0; // HIWORD (lExtendedStyle)
7246 *p++ = x;
7247 *p++ = y;
7248 *p++ = w;
7249 *p++ = h;
7250 *p++ = Id; //9 or 10 words in all
7251
7252 *p++ = (WORD)0xffff;
7253 *p++ = clss; //2 more here
7254
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007255 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 p += nchar;
7257
7258 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7259
7260 return p; //total = 15+ (strlen(caption)) words
7261 // = 30 + 2(strlen(caption) bytes reqd
7262}
7263
7264
7265/*
7266 * Helper routine. Take an input pointer, return closest pointer that is
7267 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7268 */
7269 static LPWORD
7270lpwAlign(
7271 LPWORD lpIn)
7272{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007273 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007275 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 ul += 3;
7277 ul >>= 2;
7278 ul <<= 2;
7279 return (LPWORD)ul;
7280}
7281
7282/*
7283 * Helper routine. Takes second parameter as Ansi string, copies it to first
7284 * parameter as wide character (16-bits / char) string, and returns integer
7285 * number of wide characters (words) in string (including the trailing wide
7286 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007287 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7288 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 static int
7290nCopyAnsiToWideChar(
7291 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007292 LPSTR lpAnsiIn,
7293 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7297 int i;
7298 WCHAR *wn;
7299
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007300 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
7302 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007303 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (wn != NULL)
7305 {
7306 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007307 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 vim_free(wn);
7309 }
7310 }
7311 if (nChar == 0)
7312 /* Use Win32 conversion function. */
7313 nChar = MultiByteToWideChar(
7314 enc_codepage > 0 ? enc_codepage : CP_ACP,
7315 MB_PRECOMPOSED,
7316 lpAnsiIn, len,
7317 lpWCStr, len);
7318 for (i = 0; i < nChar; ++i)
7319 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7320 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322 return nChar;
7323}
7324
7325
7326#ifdef FEAT_TEAROFF
7327/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007328 * Lookup menu handle from "menu_id".
7329 */
7330 static HMENU
7331tearoff_lookup_menuhandle(
7332 vimmenu_T *menu,
7333 WORD menu_id)
7334{
7335 for ( ; menu != NULL; menu = menu->next)
7336 {
7337 if (menu->modes == 0) /* this menu has just been deleted */
7338 continue;
7339 if (menu_is_separator(menu->dname))
7340 continue;
7341 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7342 return menu->submenu_id;
7343 }
7344 return NULL;
7345}
7346
7347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 * The callback function for all the modeless dialogs that make up the
7349 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7350 * thinking its menus have been clicked), and go away when closed.
7351 */
7352 static LRESULT CALLBACK
7353tearoff_callback(
7354 HWND hwnd,
7355 UINT message,
7356 WPARAM wParam,
7357 LPARAM lParam)
7358{
7359 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007360 {
7361 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 /* May show the mouse pointer again. */
7366 HandleMouseHide(message, lParam);
7367
7368 if (message == WM_COMMAND)
7369 {
7370 if ((WORD)(LOWORD(wParam)) & 0x8000)
7371 {
7372 POINT mp;
7373 RECT rect;
7374
7375 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7376 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007377 vimmenu_T *menu;
7378
7379 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007381 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7383 (int)rect.right - 8,
7384 (int)mp.y,
7385 (int)0, /*reserved param*/
7386 s_hwnd,
7387 NULL);
7388 /*
7389 * NOTE: The pop-up menu can eat the mouse up event.
7390 * We deal with this in normal.c.
7391 */
7392 }
7393 }
7394 else
7395 /* Pass on messages to the main Vim window */
7396 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7397 /*
7398 * Give main window the focus back: this is so after
7399 * choosing a tearoff button you can start typing again
7400 * straight away.
7401 */
7402 (void)SetFocus(s_hwnd);
7403 return TRUE;
7404 }
7405 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7406 {
7407 DestroyWindow(hwnd);
7408 return TRUE;
7409 }
7410
7411 /* When moved around, give main window the focus back. */
7412 if (message == WM_EXITSIZEMOVE)
7413 (void)SetActiveWindow(s_hwnd);
7414
7415 return FALSE;
7416}
7417#endif
7418
7419
7420/*
7421 * Decide whether to use the "new look" (small, non-bold font) or the "old
7422 * look" (big, clanky font) for dialogs, and work out a few values for use
7423 * later accordingly.
7424 */
7425 static void
7426get_dialog_font_metrics(void)
7427{
7428 HDC hdc;
7429 HFONT hfontTools = 0;
7430 DWORD dlgFontSize;
7431 SIZE size;
7432#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007433 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434#endif
7435
7436 s_usenewlook = FALSE;
7437
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007439 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007440 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442#endif
7443 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7444 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7445
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007446 if (hfontTools)
7447 {
7448 hdc = GetDC(s_hwnd);
7449 SelectObject(hdc, hfontTools);
7450 /*
7451 * GetTextMetrics() doesn't return the right value in
7452 * tmAveCharWidth, so we have to figure out the dialog base units
7453 * ourselves.
7454 */
7455 GetTextExtentPoint(hdc,
7456 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7457 52, &size);
7458 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007460 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7461 s_dlgfntheight = (WORD)size.cy;
7462 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 }
7464
7465 if (!s_usenewlook)
7466 {
7467 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7468 s_dlgfntwidth = LOWORD(dlgFontSize);
7469 s_dlgfntheight = HIWORD(dlgFontSize);
7470 }
7471}
7472
7473#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7474/*
7475 * Create a pseudo-"tearoff menu" based on the child
7476 * items of a given menu pointer.
7477 */
7478 static void
7479gui_mch_tearoff(
7480 char_u *title,
7481 vimmenu_T *menu,
7482 int initX,
7483 int initY)
7484{
7485 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7486 int template_len;
7487 int nchar, textWidth, submenuWidth;
7488 DWORD lStyle;
7489 DWORD lExtendedStyle;
7490 WORD dlgwidth;
7491 WORD menuID;
7492 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007493 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 vimmenu_T *the_menu = menu;
7495 HWND hwnd;
7496 HDC hdc;
7497 HFONT font, oldFont;
7498 int col, spaceWidth, len;
7499 int columnWidths[2];
7500 char_u *label, *text;
7501 int acLen = 0;
7502 int nameLen;
7503 int padding0, padding1, padding2 = 0;
7504 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007505 int x;
7506 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007508 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 int use_lfSysmenu = FALSE;
7510#endif
7511
7512 /*
7513 * If this menu is already torn off, move it to the mouse position.
7514 */
7515 if (IsWindow(menu->tearoff_handle))
7516 {
7517 POINT mp;
7518 if (GetCursorPos((LPPOINT)&mp))
7519 {
7520 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7521 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7522 }
7523 return;
7524 }
7525
7526 /*
7527 * Create a new tearoff.
7528 */
7529 if (*title == MNU_HIDDEN_CHAR)
7530 title++;
7531
7532 /* Allocate memory to store the dialog template. It's made bigger when
7533 * needed. */
7534 template_len = DLG_ALLOC_SIZE;
7535 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7536 if (p == NULL)
7537 return;
7538
7539 hwnd = GetDesktopWindow();
7540 hdc = GetWindowDC(hwnd);
7541#ifdef USE_SYSMENU_FONT
7542 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7543 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007544 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 use_lfSysmenu = TRUE;
7546 }
7547 else
7548#endif
7549 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7550 VARIABLE_PITCH , DLG_FONT_NAME);
7551 if (s_usenewlook)
7552 oldFont = SelectFont(hdc, font);
7553 else
7554 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7555
7556 /* Calculate width of a single space. Used for padding columns to the
7557 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007558 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
7560 /* Figure out max width of the text column, the accelerator column and the
7561 * optional submenu column. */
7562 submenuWidth = 0;
7563 for (col = 0; col < 2; col++)
7564 {
7565 columnWidths[col] = 0;
7566 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7567 {
7568 /* Use "dname" here to compute the width of the visible text. */
7569 text = (col == 0) ? pmenu->dname : pmenu->actext;
7570 if (text != NULL && *text != NUL)
7571 {
7572 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7573 if (textWidth > columnWidths[col])
7574 columnWidths[col] = textWidth;
7575 }
7576 if (pmenu->children != NULL)
7577 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7578 }
7579 }
7580 if (columnWidths[1] == 0)
7581 {
7582 /* no accelerators */
7583 if (submenuWidth != 0)
7584 columnWidths[0] += submenuWidth;
7585 else
7586 columnWidths[0] += spaceWidth;
7587 }
7588 else
7589 {
7590 /* there is an accelerator column */
7591 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7592 columnWidths[1] += submenuWidth;
7593 }
7594
7595 /*
7596 * Now find the total width of our 'menu'.
7597 */
7598 textWidth = columnWidths[0] + columnWidths[1];
7599 if (submenuWidth != 0)
7600 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007601 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7603 textWidth += submenuWidth;
7604 }
7605 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7606 if (textWidth > dlgwidth)
7607 dlgwidth = textWidth;
7608 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 /* start to fill in the dlgtemplate information. addressing by WORDs */
7611 if (s_usenewlook)
7612 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7613 else
7614 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7615
7616 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7617 *p++ = LOWORD(lStyle);
7618 *p++ = HIWORD(lStyle);
7619 *p++ = LOWORD(lExtendedStyle);
7620 *p++ = HIWORD(lExtendedStyle);
7621 pnumitems = p; /* save where the number of items must be stored */
7622 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007623 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007625 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
7627 *p++ = PixelToDialogX(initX); // x
7628 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007629 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 else
7631 *p++ = PixelToDialogY(initY); // y
7632 *p++ = PixelToDialogX(dlgwidth); // cx
7633 ptrueheight = p;
7634 *p++ = 0; // dialog height: changed later anyway
7635 *p++ = 0; // Menu
7636 *p++ = 0; // Class
7637
7638 /* copy the title of the dialog */
7639 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007640 ? (LPSTR)title
7641 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 p += nchar;
7643
7644 if (s_usenewlook)
7645 {
7646 /* do the font, since DS_3DLOOK doesn't work properly */
7647#ifdef USE_SYSMENU_FONT
7648 if (use_lfSysmenu)
7649 {
7650 /* point size */
7651 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7652 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007653 wcscpy(p, lfSysmenu.lfFaceName);
7654 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 }
7656 else
7657#endif
7658 {
7659 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007660 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 }
7662 p += nchar;
7663 }
7664
7665 /*
7666 * Loop over all the items in the menu.
7667 * But skip over the tearbar.
7668 */
7669 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7670 menu = menu->children->next;
7671 else
7672 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007673 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 for ( ; menu != NULL; menu = menu->next)
7675 {
7676 if (menu->modes == 0) /* this menu has just been deleted */
7677 continue;
7678 if (menu_is_separator(menu->dname))
7679 {
7680 sepPadding += 3;
7681 continue;
7682 }
7683
7684 /* Check if there still is plenty of room in the template. Make it
7685 * larger when needed. */
7686 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7687 {
7688 WORD *newp;
7689
7690 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7691 if (newp != NULL)
7692 {
7693 template_len += 4096;
7694 mch_memmove(newp, pdlgtemplate,
7695 (char *)p - (char *)pdlgtemplate);
7696 p = newp + (p - pdlgtemplate);
7697 pnumitems = newp + (pnumitems - pdlgtemplate);
7698 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7699 LocalFree(LocalHandle(pdlgtemplate));
7700 pdlgtemplate = newp;
7701 }
7702 }
7703
7704 /* Figure out minimal length of this menu label. Use "name" for the
7705 * actual text, "dname" for estimating the displayed size. "name"
7706 * has "&a" for mnemonic and includes the accelerator. */
7707 len = nameLen = (int)STRLEN(menu->name);
7708 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7709 (int)STRLEN(menu->dname))) / spaceWidth;
7710 len += padding0;
7711
7712 if (menu->actext != NULL)
7713 {
7714 acLen = (int)STRLEN(menu->actext);
7715 len += acLen;
7716 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7717 }
7718 else
7719 textWidth = 0;
7720 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7721 len += padding1;
7722
7723 if (menu->children == NULL)
7724 {
7725 padding2 = submenuWidth / spaceWidth;
7726 len += padding2;
7727 menuID = (WORD)(menu->id);
7728 }
7729 else
7730 {
7731 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007732 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734
7735 /* Allocate menu label and fill it in */
7736 text = label = alloc((unsigned)len + 1);
7737 if (label == NULL)
7738 break;
7739
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007740 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7742 if (text == NULL)
7743 text = label + nameLen; /* no actext, use whole name */
7744 while (padding0-- > 0)
7745 *text++ = ' ';
7746 if (menu->actext != NULL)
7747 {
7748 STRNCPY(text, menu->actext, acLen);
7749 text += acLen;
7750 }
7751 while (padding1-- > 0)
7752 *text++ = ' ';
7753 if (menu->children != NULL)
7754 {
7755 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7756 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7757 }
7758 else
7759 {
7760 while (padding2-- > 0)
7761 *text++ = ' ';
7762 }
7763 *text = NUL;
7764
7765 /*
7766 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7767 * W95/NT4 it makes the tear-off look more like a menu.
7768 */
7769 p = add_dialog_element(p,
7770 BS_PUSHBUTTON|BS_LEFT,
7771 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7772 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7773 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7774 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007775 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 vim_free(label);
7777 (*pnumitems)++;
7778 }
7779
7780 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7781
7782
7783 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007784 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007785 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 (LPDLGTEMPLATE)pdlgtemplate,
7787 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007788 (DLGPROC)tearoff_callback,
7789 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790
7791 LocalFree(LocalHandle(pdlgtemplate));
7792 SelectFont(hdc, oldFont);
7793 DeleteObject(font);
7794 ReleaseDC(hwnd, hdc);
7795
7796 /*
7797 * Reassert ourselves as the active window. This is so that after creating
7798 * a tearoff, the user doesn't have to click with the mouse just to start
7799 * typing again!
7800 */
7801 (void)SetActiveWindow(s_hwnd);
7802
7803 /* make sure the right buttons are enabled */
7804 force_menu_update = TRUE;
7805}
7806#endif
7807
7808#if defined(FEAT_TOOLBAR) || defined(PROTO)
7809#include "gui_w32_rc.h"
7810
7811/* This not defined in older SDKs */
7812# ifndef TBSTYLE_FLAT
7813# define TBSTYLE_FLAT 0x0800
7814# endif
7815
7816/*
7817 * Create the toolbar, initially unpopulated.
7818 * (just like the menu, there are no defaults, it's all
7819 * set up through menu.vim)
7820 */
7821 static void
7822initialise_toolbar(void)
7823{
7824 InitCommonControls();
7825 s_toolbarhwnd = CreateToolbarEx(
7826 s_hwnd,
7827 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7828 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007829 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007830 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 IDR_TOOLBAR1, // id of initial bitmap
7832 NULL,
7833 0, // initial number of buttons
7834 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7835 TOOLBAR_BUTTON_HEIGHT,
7836 TOOLBAR_BUTTON_WIDTH,
7837 TOOLBAR_BUTTON_HEIGHT,
7838 sizeof(TBBUTTON)
7839 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007840 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841
7842 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7843}
7844
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007845 static LRESULT CALLBACK
7846toolbar_wndproc(
7847 HWND hwnd,
7848 UINT uMsg,
7849 WPARAM wParam,
7850 LPARAM lParam)
7851{
7852 HandleMouseHide(uMsg, lParam);
7853 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7854}
7855
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 static int
7857get_toolbar_bitmap(vimmenu_T *menu)
7858{
7859 int i = -1;
7860
7861 /*
7862 * Check user bitmaps first, unless builtin is specified.
7863 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007864 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {
7866 char_u fname[MAXPATHL];
7867 HANDLE hbitmap = NULL;
7868
7869 if (menu->iconfile != NULL)
7870 {
7871 gui_find_iconfile(menu->iconfile, fname, "bmp");
7872 hbitmap = LoadImage(
7873 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007874 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 IMAGE_BITMAP,
7876 TOOLBAR_BUTTON_WIDTH,
7877 TOOLBAR_BUTTON_HEIGHT,
7878 LR_LOADFROMFILE |
7879 LR_LOADMAP3DCOLORS
7880 );
7881 }
7882
7883 /*
7884 * If the LoadImage call failed, or the "icon=" file
7885 * didn't exist or wasn't specified, try the menu name
7886 */
7887 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007888 && (gui_find_bitmap(
7889#ifdef FEAT_MULTI_LANG
7890 menu->en_dname != NULL ? menu->en_dname :
7891#endif
7892 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 hbitmap = LoadImage(
7894 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007895 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 IMAGE_BITMAP,
7897 TOOLBAR_BUTTON_WIDTH,
7898 TOOLBAR_BUTTON_HEIGHT,
7899 LR_LOADFROMFILE |
7900 LR_LOADMAP3DCOLORS
7901 );
7902
7903 if (hbitmap != NULL)
7904 {
7905 TBADDBITMAP tbAddBitmap;
7906
7907 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007908 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909
7910 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7911 (WPARAM)1, (LPARAM)&tbAddBitmap);
7912 /* i will be set to -1 if it fails */
7913 }
7914 }
7915 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7916 i = menu->iconidx;
7917
7918 return i;
7919}
7920#endif
7921
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007922#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7923 static void
7924initialise_tabline(void)
7925{
7926 InitCommonControls();
7927
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007928 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007929 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007930 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007931 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007932 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007933
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007934 gui.tabline_height = TABLINE_HEIGHT;
7935
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007936# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007937 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007938# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007939}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007940
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007941/*
7942 * Get tabpage_T from POINT.
7943 */
7944 static tabpage_T *
7945GetTabFromPoint(
7946 HWND hWnd,
7947 POINT pt)
7948{
7949 tabpage_T *ptp = NULL;
7950
7951 if (gui_mch_showing_tabline())
7952 {
7953 TCHITTESTINFO htinfo;
7954 htinfo.pt = pt;
7955 /* ignore if a window under cusor is not tabcontrol. */
7956 if (s_tabhwnd == hWnd)
7957 {
7958 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7959 if (idx != -1)
7960 ptp = find_tabpage(idx + 1);
7961 }
7962 }
7963 return ptp;
7964}
7965
7966static POINT s_pt = {0, 0};
7967static HCURSOR s_hCursor = NULL;
7968
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007969 static LRESULT CALLBACK
7970tabline_wndproc(
7971 HWND hwnd,
7972 UINT uMsg,
7973 WPARAM wParam,
7974 LPARAM lParam)
7975{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007976 POINT pt;
7977 tabpage_T *tp;
7978 RECT rect;
7979 int nCenter;
7980 int idx0;
7981 int idx1;
7982
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007983 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007984
7985 switch (uMsg)
7986 {
7987 case WM_LBUTTONDOWN:
7988 {
7989 s_pt.x = GET_X_LPARAM(lParam);
7990 s_pt.y = GET_Y_LPARAM(lParam);
7991 SetCapture(hwnd);
7992 s_hCursor = GetCursor(); /* backup default cursor */
7993 break;
7994 }
7995 case WM_MOUSEMOVE:
7996 if (GetCapture() == hwnd
7997 && ((wParam & MK_LBUTTON)) != 0)
7998 {
7999 pt.x = GET_X_LPARAM(lParam);
8000 pt.y = s_pt.y;
8001 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8002 {
8003 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8004
8005 tp = GetTabFromPoint(hwnd, pt);
8006 if (tp != NULL)
8007 {
8008 idx0 = tabpage_index(curtab) - 1;
8009 idx1 = tabpage_index(tp) - 1;
8010
8011 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8012 nCenter = rect.left + (rect.right - rect.left) / 2;
8013
8014 /* Check if the mouse cursor goes over the center of
8015 * the next tab to prevent "flickering". */
8016 if ((idx0 < idx1) && (nCenter < pt.x))
8017 {
8018 tabpage_move(idx1 + 1);
8019 update_screen(0);
8020 }
8021 else if ((idx1 < idx0) && (pt.x < nCenter))
8022 {
8023 tabpage_move(idx1);
8024 update_screen(0);
8025 }
8026 }
8027 }
8028 }
8029 break;
8030 case WM_LBUTTONUP:
8031 {
8032 if (GetCapture() == hwnd)
8033 {
8034 SetCursor(s_hCursor);
8035 ReleaseCapture();
8036 }
8037 break;
8038 }
8039 default:
8040 break;
8041 }
8042
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008043 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8044}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008045#endif
8046
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8048/*
8049 * Make the GUI window come to the foreground.
8050 */
8051 void
8052gui_mch_set_foreground(void)
8053{
8054 if (IsIconic(s_hwnd))
8055 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8056 SetForegroundWindow(s_hwnd);
8057}
8058#endif
8059
8060#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8061 static void
8062dyn_imm_load(void)
8063{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008064 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (hLibImm == NULL)
8066 return;
8067
8068 pImmGetCompositionStringA
8069 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8070 pImmGetCompositionStringW
8071 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8072 pImmGetContext
8073 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8074 pImmAssociateContext
8075 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8076 pImmReleaseContext
8077 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8078 pImmGetOpenStatus
8079 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8080 pImmSetOpenStatus
8081 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008082 pImmGetCompositionFontW
8083 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8084 pImmSetCompositionFontW
8085 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 pImmSetCompositionWindow
8087 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8088 pImmGetConversionStatus
8089 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008090 pImmSetConversionStatus
8091 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
8093 if ( pImmGetCompositionStringA == NULL
8094 || pImmGetCompositionStringW == NULL
8095 || pImmGetContext == NULL
8096 || pImmAssociateContext == NULL
8097 || pImmReleaseContext == NULL
8098 || pImmGetOpenStatus == NULL
8099 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008100 || pImmGetCompositionFontW == NULL
8101 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008103 || pImmGetConversionStatus == NULL
8104 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {
8106 FreeLibrary(hLibImm);
8107 hLibImm = NULL;
8108 pImmGetContext = NULL;
8109 return;
8110 }
8111
8112 return;
8113}
8114
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#endif
8116
8117#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8118
8119# ifdef FEAT_XPM_W32
8120# define IMAGE_XPM 100
8121# endif
8122
8123typedef struct _signicon_t
8124{
8125 HANDLE hImage;
8126 UINT uType;
8127#ifdef FEAT_XPM_W32
8128 HANDLE hShape; /* Mask bitmap handle */
8129#endif
8130} signicon_t;
8131
8132 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008133gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
8135 signicon_t *sign;
8136 int x, y, w, h;
8137
8138 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8139 return;
8140
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008141#if defined(FEAT_DIRECTX)
8142 if (IS_ENABLE_DIRECTX())
8143 DWriteContext_Flush(s_dwc);
8144#endif
8145
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 x = TEXT_X(col);
8147 y = TEXT_Y(row);
8148 w = gui.char_width * 2;
8149 h = gui.char_height;
8150 switch (sign->uType)
8151 {
8152 case IMAGE_BITMAP:
8153 {
8154 HDC hdcMem;
8155 HBITMAP hbmpOld;
8156
8157 hdcMem = CreateCompatibleDC(s_hdc);
8158 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8159 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8160 SelectObject(hdcMem, hbmpOld);
8161 DeleteDC(hdcMem);
8162 }
8163 break;
8164 case IMAGE_ICON:
8165 case IMAGE_CURSOR:
8166 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8167 break;
8168#ifdef FEAT_XPM_W32
8169 case IMAGE_XPM:
8170 {
8171 HDC hdcMem;
8172 HBITMAP hbmpOld;
8173
8174 hdcMem = CreateCompatibleDC(s_hdc);
8175 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8176 /* Make hole */
8177 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8178
8179 SelectObject(hdcMem, sign->hImage);
8180 /* Paint sign */
8181 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8182 SelectObject(hdcMem, hbmpOld);
8183 DeleteDC(hdcMem);
8184 }
8185 break;
8186#endif
8187 }
8188}
8189
8190 static void
8191close_signicon_image(signicon_t *sign)
8192{
8193 if (sign)
8194 switch (sign->uType)
8195 {
8196 case IMAGE_BITMAP:
8197 DeleteObject((HGDIOBJ)sign->hImage);
8198 break;
8199 case IMAGE_CURSOR:
8200 DestroyCursor((HCURSOR)sign->hImage);
8201 break;
8202 case IMAGE_ICON:
8203 DestroyIcon((HICON)sign->hImage);
8204 break;
8205#ifdef FEAT_XPM_W32
8206 case IMAGE_XPM:
8207 DeleteObject((HBITMAP)sign->hImage);
8208 DeleteObject((HBITMAP)sign->hShape);
8209 break;
8210#endif
8211 }
8212}
8213
8214 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008215gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 signicon_t sign, *psign;
8218 char_u *ext;
8219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008221 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (ext > signfile)
8223 {
8224 int do_load = 1;
8225
8226 if (!STRICMP(ext, ".bmp"))
8227 sign.uType = IMAGE_BITMAP;
8228 else if (!STRICMP(ext, ".ico"))
8229 sign.uType = IMAGE_ICON;
8230 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8231 sign.uType = IMAGE_CURSOR;
8232 else
8233 do_load = 0;
8234
8235 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008236 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 gui.char_width * 2, gui.char_height,
8238 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8239#ifdef FEAT_XPM_W32
8240 if (!STRICMP(ext, ".xpm"))
8241 {
8242 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008243 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8244 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
8246#endif
8247 }
8248
8249 psign = NULL;
8250 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8251 != NULL)
8252 *psign = sign;
8253
8254 if (!psign)
8255 {
8256 if (sign.hImage)
8257 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008258 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260 return (void *)psign;
8261
8262}
8263
8264 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008265gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 if (sign)
8268 {
8269 close_signicon_image((signicon_t *)sign);
8270 vim_free(sign);
8271 }
8272}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008275#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276
8277/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008278 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008280 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8282 * to get current mouse position).
8283 *
8284 * Trying to use as more Windows services as possible, and as less
8285 * IE version as possible :)).
8286 *
8287 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8288 * BalloonEval struct.
8289 * 2) Enable/Disable simply create/kill BalloonEval Timer
8290 * 3) When there was enough inactivity, timer procedure posts
8291 * async request to debugger
8292 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8293 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008294 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 */
8296
Bram Moolenaar45360022005-07-21 21:08:21 +00008297/*
8298 * determine whether installed Common Controls support multiline tooltips
8299 * (i.e. their version is >= 4.70
8300 */
8301 int
8302multiline_balloon_available(void)
8303{
8304 HINSTANCE hDll;
8305 static char comctl_dll[] = "comctl32.dll";
8306 static int multiline_tip = MAYBE;
8307
8308 if (multiline_tip != MAYBE)
8309 return multiline_tip;
8310
8311 hDll = GetModuleHandle(comctl_dll);
8312 if (hDll != NULL)
8313 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008314 DLLGETVERSIONPROC pGetVer;
8315 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008316
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008317 if (pGetVer != NULL)
8318 {
8319 DLLVERSIONINFO dvi;
8320 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008321
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008322 ZeroMemory(&dvi, sizeof(dvi));
8323 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008324
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008325 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008326
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008327 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008328 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008329 || (dvi.dwMajorVersion == 4
8330 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008331 {
8332 multiline_tip = TRUE;
8333 return multiline_tip;
8334 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008335 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008336 else
8337 {
8338 /* there is chance we have ancient CommCtl 4.70
8339 which doesn't export DllGetVersion */
8340 DWORD dwHandle = 0;
8341 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8342 if (len > 0)
8343 {
8344 VS_FIXEDFILEINFO *ver;
8345 UINT vlen = 0;
8346 void *data = alloc(len);
8347
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008348 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008349 && GetFileVersionInfo(comctl_dll, 0, len, data)
8350 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8351 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008352 && HIWORD(ver->dwFileVersionMS) > 4)
8353 || ((HIWORD(ver->dwFileVersionMS) == 4
8354 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008355 {
8356 vim_free(data);
8357 multiline_tip = TRUE;
8358 return multiline_tip;
8359 }
8360 vim_free(data);
8361 }
8362 }
8363 }
8364 multiline_tip = FALSE;
8365 return multiline_tip;
8366}
8367
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008368 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008369make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008370{
8371 TOOLINFOW *pti;
8372 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008373
8374 if (multiline_balloon_available() == TRUE)
8375 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8376 else
8377 ToolInfoSize = sizeof(TOOLINFOW);
8378
8379 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8380 if (pti == NULL)
8381 return;
8382
8383 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8384 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8385 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008386 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008387
8388 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8389 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8390
8391 pti->cbSize = ToolInfoSize;
8392 pti->uFlags = TTF_SUBCLASS;
8393 pti->hwnd = beval->target;
8394 pti->hinst = 0; // Don't use string resources
8395 pti->uId = ID_BEVAL_TOOLTIP;
8396
8397 if (multiline_balloon_available() == TRUE)
8398 {
8399 RECT rect;
8400 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8401 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008402 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8403 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008404 // switch multiline tooltips on
8405 if (GetClientRect(s_textArea, &rect))
8406 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8407 (LPARAM)rect.right);
8408 }
8409 else
8410 {
8411 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008412 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8413 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008414 }
8415
8416 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8417 pti->rect.left = pt.x - 3;
8418 pti->rect.top = pt.y - 3;
8419 pti->rect.right = pt.x + 3;
8420 pti->rect.bottom = pt.y + 3;
8421
8422 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8423 // Make tooltip appear sooner.
8424 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8425 // I've performed some tests and it seems the longest possible life time
8426 // of tooltip is 30 seconds.
8427 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8428 /*
8429 * HACK: force tooltip to appear, because it'll not appear until
8430 * first mouse move. D*mn M$
8431 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8432 */
8433 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8434 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8435 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008436}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008437
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008439delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008441 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442}
8443
8444 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008445BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008446 HWND hwnd UNUSED,
8447 UINT uMsg UNUSED,
8448 UINT_PTR idEvent UNUSED,
8449 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 POINT pt;
8452 RECT rect;
8453
8454 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8455 return;
8456
8457 GetCursorPos(&pt);
8458 if (WindowFromPoint(pt) != s_textArea)
8459 return;
8460
8461 ScreenToClient(s_textArea, &pt);
8462 GetClientRect(s_textArea, &rect);
8463 if (!PtInRect(&rect, pt))
8464 return;
8465
8466 if (LastActivity > 0
8467 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8468 && (cur_beval->showState != ShS_PENDING
8469 || abs(cur_beval->x - pt.x) > 3
8470 || abs(cur_beval->y - pt.y) > 3))
8471 {
8472 /* Pointer resting in one place long enough, it's time to show
8473 * the tooltip. */
8474 cur_beval->showState = ShS_PENDING;
8475 cur_beval->x = pt.x;
8476 cur_beval->y = pt.y;
8477
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008478 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479
8480 if (cur_beval->msgCB != NULL)
8481 (*cur_beval->msgCB)(cur_beval, 0);
8482 }
8483}
8484
8485 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008486gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008488 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008490 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
8493 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008494gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008496 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (beval == NULL)
8498 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008499 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008500 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008501 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
8503
8504 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008505gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506{
8507 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008508
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008509 vim_free(beval->msg);
8510 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8511 if (beval->msg == NULL)
8512 {
8513 delete_tooltip(beval);
8514 beval->showState = ShS_NEUTRAL;
8515 return;
8516 }
8517
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008518 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (beval->showState == ShS_SHOWING)
8520 return;
8521 GetCursorPos(&pt);
8522 ScreenToClient(s_textArea, &pt);
8523
8524 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008526 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 gui_mch_disable_beval_area(cur_beval);
8528 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008529 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008531 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532}
8533
8534 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008535gui_mch_create_beval_area(
8536 void *target, /* ignored, always use s_textArea */
8537 char_u *mesg,
8538 void (*mesgCB)(BalloonEval *, int),
8539 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 /* partially stolen from gui_beval.c */
8542 BalloonEval *beval;
8543
8544 if (mesg != NULL && mesgCB != NULL)
8545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008546 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 return NULL;
8548 }
8549
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008550 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 if (beval != NULL)
8552 {
8553 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
8555 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 beval->msg = mesg;
8557 beval->msgCB = mesgCB;
8558 beval->clientData = clientData;
8559
8560 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 cur_beval = beval;
8562
8563 if (p_beval)
8564 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 return beval;
8567}
8568
8569 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008570Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8573 return;
8574
8575 if (cur_beval != NULL)
8576 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008577 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008579 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008580 // TRACE0("TTN_SHOW {{{");
8581 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008582 break;
8583 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008584 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 delete_tooltip(cur_beval);
8586 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008587 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
8589 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008590 break;
8591 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008592 {
8593 /* if you get there then we have new common controls */
8594 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8595 info->lpszText = (LPSTR)info->lParam;
8596 info->uFlags |= TTF_DI_SETITEM;
8597 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008598 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008599 case TTN_GETDISPINFOW:
8600 {
8601 // if we get here then we have new common controls
8602 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8603 info->lpszText = (LPWSTR)info->lParam;
8604 info->uFlags |= TTF_DI_SETITEM;
8605 }
8606 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
8608 }
8609}
8610
8611 static void
8612TrackUserActivity(UINT uMsg)
8613{
8614 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8615 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8616 LastActivity = GetTickCount();
8617}
8618
8619 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008620gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008622#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008623 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008624#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008625 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 vim_free(beval);
8627}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008628#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629
8630#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8631/*
8632 * We have multiple signs to draw at the same location. Draw the
8633 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8634 */
8635 void
8636netbeans_draw_multisign_indicator(int row)
8637{
8638 int i;
8639 int y;
8640 int x;
8641
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008642 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008643 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008644
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 x = 0;
8646 y = TEXT_Y(row);
8647
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008648#if defined(FEAT_DIRECTX)
8649 if (IS_ENABLE_DIRECTX())
8650 DWriteContext_Flush(s_dwc);
8651#endif
8652
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 for (i = 0; i < gui.char_height - 3; i++)
8654 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8655
8656 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8657 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8658 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8659 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8660 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8661 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8662 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8663}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008664#endif