blob: aac1398b3264f345cc9d02ffb51767cd8d7cdbb7 [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
323static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100324#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static
326#endif
327HWND s_hwnd = NULL;
328static HDC s_hdc = NULL;
329static HBRUSH s_brush = NULL;
330
331#ifdef FEAT_TOOLBAR
332static HWND s_toolbarhwnd = NULL;
333static WNDPROC s_toolbar_wndproc = NULL;
334#endif
335
336#ifdef FEAT_GUI_TABLINE
337static HWND s_tabhwnd = NULL;
338static WNDPROC s_tabline_wndproc = NULL;
339static int showing_tabline = 0;
340#endif
341
342static WPARAM s_wParam = 0;
343static LPARAM s_lParam = 0;
344
345static HWND s_textArea = NULL;
346static UINT s_uMsg = 0;
347
348static char_u *s_textfield; /* Used by dialogs to pass back strings */
349
350static int s_need_activate = FALSE;
351
352/* This variable is set when waiting for an event, which is the only moment
353 * scrollbar dragging can be done directly. It's not allowed while commands
354 * are executed, because it may move the cursor and that may cause unexpected
355 * problems (e.g., while ":s" is working).
356 */
357static int allow_scrollbar = FALSE;
358
359#ifdef GLOBAL_IME
360# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
361#else
362# define MyTranslateMessage(x) TranslateMessage(x)
363#endif
364
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100365#if defined(FEAT_DIRECTX)
366 static int
367directx_enabled(void)
368{
369 if (s_dwc != NULL)
370 return 1;
371 else if (s_directx_load_attempted)
372 return 0;
373 /* load DirectX */
374 DWrite_Init();
375 s_directx_load_attempted = 1;
376 s_dwc = DWriteContext_Open();
377 directx_binddc();
378 return s_dwc != NULL ? 1 : 0;
379}
380
381 static void
382directx_binddc(void)
383{
384 if (s_textArea != NULL)
385 {
386 RECT rect;
387 GetClientRect(s_textArea, &rect);
388 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
389 }
390}
391#endif
392
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200393/* use of WindowProc depends on Global IME */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100394#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100395
396extern int current_font_height; /* this is in os_mswin.c */
397
398static struct
399{
400 UINT key_sym;
401 char_u vim_code0;
402 char_u vim_code1;
403} special_keys[] =
404{
405 {VK_UP, 'k', 'u'},
406 {VK_DOWN, 'k', 'd'},
407 {VK_LEFT, 'k', 'l'},
408 {VK_RIGHT, 'k', 'r'},
409
410 {VK_F1, 'k', '1'},
411 {VK_F2, 'k', '2'},
412 {VK_F3, 'k', '3'},
413 {VK_F4, 'k', '4'},
414 {VK_F5, 'k', '5'},
415 {VK_F6, 'k', '6'},
416 {VK_F7, 'k', '7'},
417 {VK_F8, 'k', '8'},
418 {VK_F9, 'k', '9'},
419 {VK_F10, 'k', ';'},
420
421 {VK_F11, 'F', '1'},
422 {VK_F12, 'F', '2'},
423 {VK_F13, 'F', '3'},
424 {VK_F14, 'F', '4'},
425 {VK_F15, 'F', '5'},
426 {VK_F16, 'F', '6'},
427 {VK_F17, 'F', '7'},
428 {VK_F18, 'F', '8'},
429 {VK_F19, 'F', '9'},
430 {VK_F20, 'F', 'A'},
431
432 {VK_F21, 'F', 'B'},
433#ifdef FEAT_NETBEANS_INTG
434 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
435#endif
436 {VK_F22, 'F', 'C'},
437 {VK_F23, 'F', 'D'},
438 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
439
440 {VK_HELP, '%', '1'},
441 {VK_BACK, 'k', 'b'},
442 {VK_INSERT, 'k', 'I'},
443 {VK_DELETE, 'k', 'D'},
444 {VK_HOME, 'k', 'h'},
445 {VK_END, '@', '7'},
446 {VK_PRIOR, 'k', 'P'},
447 {VK_NEXT, 'k', 'N'},
448 {VK_PRINT, '%', '9'},
449 {VK_ADD, 'K', '6'},
450 {VK_SUBTRACT, 'K', '7'},
451 {VK_DIVIDE, 'K', '8'},
452 {VK_MULTIPLY, 'K', '9'},
453 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
454 {VK_DECIMAL, 'K', 'B'},
455
456 {VK_NUMPAD0, 'K', 'C'},
457 {VK_NUMPAD1, 'K', 'D'},
458 {VK_NUMPAD2, 'K', 'E'},
459 {VK_NUMPAD3, 'K', 'F'},
460 {VK_NUMPAD4, 'K', 'G'},
461 {VK_NUMPAD5, 'K', 'H'},
462 {VK_NUMPAD6, 'K', 'I'},
463 {VK_NUMPAD7, 'K', 'J'},
464 {VK_NUMPAD8, 'K', 'K'},
465 {VK_NUMPAD9, 'K', 'L'},
466
467 /* Keys that we want to be able to use any modifier with: */
468 {VK_SPACE, ' ', NUL},
469 {VK_TAB, TAB, NUL},
470 {VK_ESCAPE, ESC, NUL},
471 {NL, NL, NUL},
472 {CAR, CAR, NUL},
473
474 /* End of list marker: */
475 {0, 0, 0}
476};
477
478/* Local variables */
479static int s_button_pending = -1;
480
481/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
482 * so don't reset s_button_pending. */
483static int s_getting_focus = FALSE;
484
485static int s_x_pending;
486static int s_y_pending;
487static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200488static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200490static int dead_key = 0; // 0: no dead key, 1: dead key pressed
491static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
492 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100493
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100494#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100495/* balloon-eval WM_NOTIFY_HANDLER */
496static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
497static void TrackUserActivity(UINT uMsg);
498#endif
499
500/*
501 * For control IME.
502 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100503 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100505#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100506/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
507static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100508#endif
509#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100510/* holds LOGFONTW for 'guifont' always. */
511static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100512#endif
513
514#ifdef FEAT_MBYTE_IME
515static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
516#endif
517
518#if defined(FEAT_BROWSE)
519static char_u *convert_filter(char_u *s);
520#endif
521
522#ifdef DEBUG_PRINT_ERROR
523/*
524 * Print out the last Windows error message
525 */
526 static void
527print_windows_error(void)
528{
529 LPVOID lpMsgBuf;
530
531 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
532 NULL, GetLastError(),
533 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
534 (LPTSTR) &lpMsgBuf, 0, NULL);
535 TRACE1("Error: %s\n", lpMsgBuf);
536 LocalFree(lpMsgBuf);
537}
538#endif
539
540/*
541 * Cursor blink functions.
542 *
543 * This is a simple state machine:
544 * BLINK_NONE not blinking at all
545 * BLINK_OFF blinking, cursor is not shown
546 * BLINK_ON blinking, cursor is shown
547 */
548
549#define BLINK_NONE 0
550#define BLINK_OFF 1
551#define BLINK_ON 2
552
553static int blink_state = BLINK_NONE;
554static long_u blink_waittime = 700;
555static long_u blink_ontime = 400;
556static long_u blink_offtime = 250;
557static UINT blink_timer = 0;
558
Bram Moolenaar703a8042016-06-04 16:24:32 +0200559 int
560gui_mch_is_blinking(void)
561{
562 return blink_state != BLINK_NONE;
563}
564
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200565 int
566gui_mch_is_blink_off(void)
567{
568 return blink_state == BLINK_OFF;
569}
570
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100571 void
572gui_mch_set_blinking(long wait, long on, long off)
573{
574 blink_waittime = wait;
575 blink_ontime = on;
576 blink_offtime = off;
577}
578
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 static VOID CALLBACK
580_OnBlinkTimer(
581 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100582 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100583 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100584 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100585{
586 MSG msg;
587
588 /*
589 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
590 */
591
592 KillTimer(NULL, idEvent);
593
594 /* Eat spurious WM_TIMER messages */
595 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
596 ;
597
598 if (blink_state == BLINK_ON)
599 {
600 gui_undraw_cursor();
601 blink_state = BLINK_OFF;
602 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
603 (TIMERPROC)_OnBlinkTimer);
604 }
605 else
606 {
607 gui_update_cursor(TRUE, FALSE);
608 blink_state = BLINK_ON;
609 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100610 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100611 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100612 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100613}
614
615 static void
616gui_mswin_rm_blink_timer(void)
617{
618 MSG msg;
619
620 if (blink_timer != 0)
621 {
622 KillTimer(NULL, blink_timer);
623 /* Eat spurious WM_TIMER messages */
624 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
625 ;
626 blink_timer = 0;
627 }
628}
629
630/*
631 * Stop the cursor blinking. Show the cursor if it wasn't shown.
632 */
633 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100634gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635{
636 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100637 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100638 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100639 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100640 gui_mch_flush();
641 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100642 blink_state = BLINK_NONE;
643}
644
645/*
646 * Start the cursor blinking. If it was already blinking, this restarts the
647 * waiting time and shows the cursor.
648 */
649 void
650gui_mch_start_blink(void)
651{
652 gui_mswin_rm_blink_timer();
653
654 /* Only switch blinking on if none of the times is zero */
655 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
656 {
657 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
658 (TIMERPROC)_OnBlinkTimer);
659 blink_state = BLINK_ON;
660 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100661 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100662 }
663}
664
665/*
666 * Call-back routines.
667 */
668
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 static VOID CALLBACK
670_OnTimer(
671 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100672 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100674 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100675{
676 MSG msg;
677
678 /*
679 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
680 */
681 KillTimer(NULL, idEvent);
682 s_timed_out = TRUE;
683
684 /* Eat spurious WM_TIMER messages */
685 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
686 ;
687 if (idEvent == s_wait_timer)
688 s_wait_timer = 0;
689}
690
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100691 static void
692_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100693 HWND hwnd UNUSED,
694 UINT ch UNUSED,
695 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100696{
697 dead_key = 1;
698}
699
700/*
701 * Convert Unicode character "ch" to bytes in "string[slen]".
702 * When "had_alt" is TRUE the ALT key was included in "ch".
703 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200704 * Because the Windows API uses UTF-16, we have to deal with surrogate
705 * pairs; this is where we choose to deal with them: if "ch" is a high
706 * surrogate, it will be stored, and the length returned will be zero; the next
707 * char_to_string call will then include the high surrogate, decoding the pair
708 * of UTF-16 code units to a single Unicode code point, presuming it is the
709 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100710 */
711 static int
712char_to_string(int ch, char_u *string, int slen, int had_alt)
713{
714 int len;
715 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100716 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200717 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100718
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200719 if (surrogate_pending_ch != 0)
720 {
721 /* We don't guarantee ch is a low surrogate to match the high surrogate
722 * we already have; it should be, but if it isn't, tough luck. */
723 wstring[0] = surrogate_pending_ch;
724 wstring[1] = ch;
725 surrogate_pending_ch = 0;
726 len = 2;
727 }
728 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
729 {
730 /* We don't have the entire code point yet, only the first UTF-16 code
731 * unit; so just remember it and use it in the next call. */
732 surrogate_pending_ch = ch;
733 return 0;
734 }
735 else
736 {
737 wstring[0] = ch;
738 len = 1;
739 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740
741 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
742 * "enc_codepage" is non-zero use the standard Win32 function,
743 * otherwise use our own conversion function (e.g., for UTF-8). */
744 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200746 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
747 (LPSTR)string, slen, 0, NULL);
748 /* If we had included the ALT key into the character but now the
749 * upper bit is no longer set, that probably means the conversion
750 * failed. Convert the original character and set the upper bit
751 * afterwards. */
752 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100753 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200754 wstring[0] = ch & 0x7f;
755 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
756 (LPSTR)string, slen, 0, NULL);
757 if (len == 1) /* safety check */
758 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100759 }
760 }
761 else
762 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200763 ws = utf16_to_enc(wstring, &len);
764 if (ws == NULL)
765 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100766 else
767 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200768 if (len > slen) /* just in case */
769 len = slen;
770 mch_memmove(string, ws, len);
771 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 }
773 }
774
775 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100776 {
777 string[0] = ch;
778 len = 1;
779 }
780
781 for (i = 0; i < len; ++i)
782 if (string[i] == CSI && len <= slen - 2)
783 {
784 /* Insert CSI as K_CSI. */
785 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
786 string[++i] = KS_EXTRA;
787 string[++i] = (int)KE_CSI;
788 len += 2;
789 }
790
791 return len;
792}
793
794/*
795 * Key hit, add it to the input buffer.
796 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100797 static void
798_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100799 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100800 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100801 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100802{
803 char_u string[40];
804 int len = 0;
805
806 dead_key = 0;
807
808 len = char_to_string(ch, string, 40, FALSE);
809 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
810 {
811 trash_input_buf();
812 got_int = TRUE;
813 }
814
815 add_to_input_buf(string, len);
816}
817
818/*
819 * Alt-Key hit, add it to the input buffer.
820 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100821 static void
822_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100823 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100825 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826{
827 char_u string[40]; /* Enough for multibyte character */
828 int len;
829 int modifiers;
830 int ch = cch; /* special keys are negative */
831
832 dead_key = 0;
833
834 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
835
836 /* OK, we have a character key (given by ch) which was entered with the
837 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
838 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
839 * CAPSLOCK is pressed) at this point.
840 */
841 modifiers = MOD_MASK_ALT;
842 if (GetKeyState(VK_SHIFT) & 0x8000)
843 modifiers |= MOD_MASK_SHIFT;
844 if (GetKeyState(VK_CONTROL) & 0x8000)
845 modifiers |= MOD_MASK_CTRL;
846
847 ch = simplify_key(ch, &modifiers);
848 /* remove the SHIFT modifier for keys where it's already included, e.g.,
849 * '(' and '*' */
850 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
851 modifiers &= ~MOD_MASK_SHIFT;
852
853 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
854 ch = extract_modifiers(ch, &modifiers);
855 if (ch == CSI)
856 ch = K_CSI;
857
858 len = 0;
859 if (modifiers)
860 {
861 string[len++] = CSI;
862 string[len++] = KS_MODIFIER;
863 string[len++] = modifiers;
864 }
865
866 if (IS_SPECIAL((int)ch))
867 {
868 string[len++] = CSI;
869 string[len++] = K_SECOND((int)ch);
870 string[len++] = K_THIRD((int)ch);
871 }
872 else
873 {
874 /* Although the documentation isn't clear about it, we assume "ch" is
875 * a Unicode character. */
876 len += char_to_string(ch, string + len, 40 - len, TRUE);
877 }
878
879 add_to_input_buf(string, len);
880}
881
882 static void
883_OnMouseEvent(
884 int button,
885 int x,
886 int y,
887 int repeated_click,
888 UINT keyFlags)
889{
890 int vim_modifiers = 0x0;
891
892 s_getting_focus = FALSE;
893
894 if (keyFlags & MK_SHIFT)
895 vim_modifiers |= MOUSE_SHIFT;
896 if (keyFlags & MK_CONTROL)
897 vim_modifiers |= MOUSE_CTRL;
898 if (GetKeyState(VK_MENU) & 0x8000)
899 vim_modifiers |= MOUSE_ALT;
900
901 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
902}
903
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100904 static void
905_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100906 HWND hwnd UNUSED,
907 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100908 int x,
909 int y,
910 UINT keyFlags)
911{
912 static LONG s_prevTime = 0;
913
914 LONG currentTime = GetMessageTime();
915 int button = -1;
916 int repeated_click;
917
918 /* Give main window the focus: this is so the cursor isn't hollow. */
919 (void)SetFocus(s_hwnd);
920
921 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
922 button = MOUSE_LEFT;
923 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
924 button = MOUSE_MIDDLE;
925 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
926 button = MOUSE_RIGHT;
927 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
928 {
929#ifndef GET_XBUTTON_WPARAM
930# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
931#endif
932 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
933 }
934 else if (s_uMsg == WM_CAPTURECHANGED)
935 {
936 /* on W95/NT4, somehow you get in here with an odd Msg
937 * if you press one button while holding down the other..*/
938 if (s_button_pending == MOUSE_LEFT)
939 button = MOUSE_RIGHT;
940 else
941 button = MOUSE_LEFT;
942 }
943 if (button >= 0)
944 {
945 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
946
947 /*
948 * Holding down the left and right buttons simulates pushing the middle
949 * button.
950 */
951 if (repeated_click
952 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
953 || (button == MOUSE_RIGHT
954 && s_button_pending == MOUSE_LEFT)))
955 {
956 /*
957 * Hmm, gui.c will ignore more than one button down at a time, so
958 * pretend we let go of it first.
959 */
960 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
961 button = MOUSE_MIDDLE;
962 repeated_click = FALSE;
963 s_button_pending = -1;
964 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
965 }
966 else if ((repeated_click)
967 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
968 {
969 if (s_button_pending > -1)
970 {
971 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
972 s_button_pending = -1;
973 }
974 /* TRACE("Button down at x %d, y %d\n", x, y); */
975 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
976 }
977 else
978 {
979 /*
980 * If this is the first press (i.e. not a multiple click) don't
981 * action immediately, but store and wait for:
982 * i) button-up
983 * ii) mouse move
984 * iii) another button press
985 * before using it.
986 * This enables us to make left+right simulate middle button,
987 * without left or right being actioned first. The side-effect is
988 * that if you click and hold the mouse without dragging, the
989 * cursor doesn't move until you release the button. In practice
990 * this is hardly a problem.
991 */
992 s_button_pending = button;
993 s_x_pending = x;
994 s_y_pending = y;
995 s_kFlags_pending = keyFlags;
996 }
997
998 s_prevTime = currentTime;
999 }
1000}
1001
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001002 static void
1003_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001004 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001005 int x,
1006 int y,
1007 UINT keyFlags)
1008{
1009 int button;
1010
1011 s_getting_focus = FALSE;
1012 if (s_button_pending > -1)
1013 {
1014 /* Delayed action for mouse down event */
1015 _OnMouseEvent(s_button_pending, s_x_pending,
1016 s_y_pending, FALSE, s_kFlags_pending);
1017 s_button_pending = -1;
1018 }
1019 if (s_uMsg == WM_MOUSEMOVE)
1020 {
1021 /*
1022 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1023 * down.
1024 */
1025 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1026 | MK_XBUTTON1 | MK_XBUTTON2)))
1027 {
1028 gui_mouse_moved(x, y);
1029 return;
1030 }
1031
1032 /*
1033 * While button is down, keep grabbing mouse move events when
1034 * the mouse goes outside the window
1035 */
1036 SetCapture(s_textArea);
1037 button = MOUSE_DRAG;
1038 /* TRACE(" move at x %d, y %d\n", x, y); */
1039 }
1040 else
1041 {
1042 ReleaseCapture();
1043 button = MOUSE_RELEASE;
1044 /* TRACE(" up at x %d, y %d\n", x, y); */
1045 }
1046
1047 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1048}
1049
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001050 static void
1051_OnSizeTextArea(
1052 HWND hwnd UNUSED,
1053 UINT state UNUSED,
1054 int cx UNUSED,
1055 int cy UNUSED)
1056{
1057#if defined(FEAT_DIRECTX)
1058 if (IS_ENABLE_DIRECTX())
1059 directx_binddc();
1060#endif
1061}
1062
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001063#ifdef FEAT_MENU
1064/*
1065 * Find the vimmenu_T with the given id
1066 */
1067 static vimmenu_T *
1068gui_mswin_find_menu(
1069 vimmenu_T *pMenu,
1070 int id)
1071{
1072 vimmenu_T *pChildMenu;
1073
1074 while (pMenu)
1075 {
1076 if (pMenu->id == (UINT)id)
1077 break;
1078 if (pMenu->children != NULL)
1079 {
1080 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1081 if (pChildMenu)
1082 {
1083 pMenu = pChildMenu;
1084 break;
1085 }
1086 }
1087 pMenu = pMenu->next;
1088 }
1089 return pMenu;
1090}
1091
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092 static void
1093_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001094 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001095 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001096 HWND hwndCtl UNUSED,
1097 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001098{
1099 vimmenu_T *pMenu;
1100
1101 pMenu = gui_mswin_find_menu(root_menu, id);
1102 if (pMenu)
1103 gui_menu_cb(pMenu);
1104}
1105#endif
1106
1107#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001109 * Handle a Find/Replace window message.
1110 */
1111 static void
1112_OnFindRepl(void)
1113{
1114 int flags = 0;
1115 int down;
1116
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001117 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1118 /* Give main window the focus back. */
1119 (void)SetFocus(s_hwnd);
1120
1121 if (s_findrep_struct.Flags & FR_FINDNEXT)
1122 {
1123 flags = FRD_FINDNEXT;
1124
1125 /* Give main window the focus back: this is so the cursor isn't
1126 * hollow. */
1127 (void)SetFocus(s_hwnd);
1128 }
1129 else if (s_findrep_struct.Flags & FR_REPLACE)
1130 {
1131 flags = FRD_REPLACE;
1132
1133 /* Give main window the focus back: this is so the cursor isn't
1134 * hollow. */
1135 (void)SetFocus(s_hwnd);
1136 }
1137 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1138 {
1139 flags = FRD_REPLACEALL;
1140 }
1141
1142 if (flags != 0)
1143 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001144 char_u *p, *q;
1145
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001146 /* Call the generic GUI function to do the actual work. */
1147 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1148 flags |= FRD_WHOLE_WORD;
1149 if (s_findrep_struct.Flags & FR_MATCHCASE)
1150 flags |= FRD_MATCH_CASE;
1151 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001152 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1153 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1154 if (p != NULL && q != NULL)
1155 gui_do_findrepl(flags, p, q, down);
1156 vim_free(p);
1157 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001158 }
1159}
1160#endif
1161
1162 static void
1163HandleMouseHide(UINT uMsg, LPARAM lParam)
1164{
1165 static LPARAM last_lParam = 0L;
1166
1167 /* We sometimes get a mousemove when the mouse didn't move... */
1168 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1169 {
1170 if (lParam == last_lParam)
1171 return;
1172 last_lParam = lParam;
1173 }
1174
1175 /* Handle specially, to centralise coding. We need to be sure we catch all
1176 * possible events which should cause us to restore the cursor (as it is a
1177 * shared resource, we take full responsibility for it).
1178 */
1179 switch (uMsg)
1180 {
1181 case WM_KEYUP:
1182 case WM_CHAR:
1183 /*
1184 * blank out the pointer if necessary
1185 */
1186 if (p_mh)
1187 gui_mch_mousehide(TRUE);
1188 break;
1189
1190 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1191 case WM_SYSCHAR:
1192 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1193 case WM_LBUTTONDOWN:
1194 case WM_LBUTTONUP:
1195 case WM_MBUTTONDOWN:
1196 case WM_MBUTTONUP:
1197 case WM_RBUTTONDOWN:
1198 case WM_RBUTTONUP:
1199 case WM_XBUTTONDOWN:
1200 case WM_XBUTTONUP:
1201 case WM_NCMOUSEMOVE:
1202 case WM_NCLBUTTONDOWN:
1203 case WM_NCLBUTTONUP:
1204 case WM_NCMBUTTONDOWN:
1205 case WM_NCMBUTTONUP:
1206 case WM_NCRBUTTONDOWN:
1207 case WM_NCRBUTTONUP:
1208 case WM_KILLFOCUS:
1209 /*
1210 * if the pointer is currently hidden, then we should show it.
1211 */
1212 gui_mch_mousehide(FALSE);
1213 break;
1214 }
1215}
1216
1217 static LRESULT CALLBACK
1218_TextAreaWndProc(
1219 HWND hwnd,
1220 UINT uMsg,
1221 WPARAM wParam,
1222 LPARAM lParam)
1223{
1224 /*
1225 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1226 hwnd, uMsg, wParam, lParam);
1227 */
1228
1229 HandleMouseHide(uMsg, lParam);
1230
1231 s_uMsg = uMsg;
1232 s_wParam = wParam;
1233 s_lParam = lParam;
1234
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001235#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001236 TrackUserActivity(uMsg);
1237#endif
1238
1239 switch (uMsg)
1240 {
1241 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1243 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1246 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1248 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1251 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1254 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001255 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001256
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001257#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001258 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1259 return TRUE;
1260#endif
1261 default:
1262 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1263 }
1264}
1265
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001266#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001267typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001268#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001269
1270 LRESULT WINAPI
1271vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1272{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001273#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001274 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001275#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001276 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001277#endif
1278}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001279
1280/*
1281 * Called when the foreground or background color has been changed.
1282 */
1283 void
1284gui_mch_new_colors(void)
1285{
1286 /* nothing to do? */
1287}
1288
1289/*
1290 * Set the colors to their default values.
1291 */
1292 void
1293gui_mch_def_colors(void)
1294{
1295 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1296 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1297 gui.def_norm_pixel = gui.norm_pixel;
1298 gui.def_back_pixel = gui.back_pixel;
1299}
1300
1301/*
1302 * Open the GUI window which was created by a call to gui_mch_init().
1303 */
1304 int
1305gui_mch_open(void)
1306{
1307#ifndef SW_SHOWDEFAULT
1308# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1309#endif
1310 /* Actually open the window, if not already visible
1311 * (may be done already in gui_mch_set_shellsize) */
1312 if (!IsWindowVisible(s_hwnd))
1313 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1314
1315#ifdef MSWIN_FIND_REPLACE
1316 /* Init replace string here, so that we keep it when re-opening the
1317 * dialog. */
1318 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1319#endif
1320
1321 return OK;
1322}
1323
1324/*
1325 * Get the position of the top left corner of the window.
1326 */
1327 int
1328gui_mch_get_winpos(int *x, int *y)
1329{
1330 RECT rect;
1331
1332 GetWindowRect(s_hwnd, &rect);
1333 *x = rect.left;
1334 *y = rect.top;
1335 return OK;
1336}
1337
1338/*
1339 * Set the position of the top left corner of the window to the given
1340 * coordinates.
1341 */
1342 void
1343gui_mch_set_winpos(int x, int y)
1344{
1345 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1346 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1347}
1348 void
1349gui_mch_set_text_area_pos(int x, int y, int w, int h)
1350{
1351 static int oldx = 0;
1352 static int oldy = 0;
1353
1354 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1355
1356#ifdef FEAT_TOOLBAR
1357 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1358 SendMessage(s_toolbarhwnd, WM_SIZE,
1359 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1360#endif
1361#if defined(FEAT_GUI_TABLINE)
1362 if (showing_tabline)
1363 {
1364 int top = 0;
1365 RECT rect;
1366
1367# ifdef FEAT_TOOLBAR
1368 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1369 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1370# endif
1371 GetClientRect(s_hwnd, &rect);
1372 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1373 }
1374#endif
1375
1376 /* When side scroll bar is unshown, the size of window will change.
1377 * then, the text area move left or right. thus client rect should be
1378 * forcedly redrawn. (Yasuhiro Matsumoto) */
1379 if (oldx != x || oldy != y)
1380 {
1381 InvalidateRect(s_hwnd, NULL, FALSE);
1382 oldx = x;
1383 oldy = y;
1384 }
1385}
1386
1387
1388/*
1389 * Scrollbar stuff:
1390 */
1391
1392 void
1393gui_mch_enable_scrollbar(
1394 scrollbar_T *sb,
1395 int flag)
1396{
1397 ShowScrollBar(sb->id, SB_CTL, flag);
1398
1399 /* TODO: When the window is maximized, the size of the window stays the
1400 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1401 * NT 4.0 it's not... */
1402}
1403
1404 void
1405gui_mch_set_scrollbar_pos(
1406 scrollbar_T *sb,
1407 int x,
1408 int y,
1409 int w,
1410 int h)
1411{
1412 SetWindowPos(sb->id, NULL, x, y, w, h,
1413 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1414}
1415
1416 void
1417gui_mch_create_scrollbar(
1418 scrollbar_T *sb,
1419 int orient) /* SBAR_VERT or SBAR_HORIZ */
1420{
1421 sb->id = CreateWindow(
1422 "SCROLLBAR", "Scrollbar",
1423 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1424 10, /* Any value will do for now */
1425 10, /* Any value will do for now */
1426 s_hwnd, NULL,
1427 s_hinst, NULL);
1428}
1429
1430/*
1431 * Find the scrollbar with the given hwnd.
1432 */
1433 static scrollbar_T *
1434gui_mswin_find_scrollbar(HWND hwnd)
1435{
1436 win_T *wp;
1437
1438 if (gui.bottom_sbar.id == hwnd)
1439 return &gui.bottom_sbar;
1440 FOR_ALL_WINDOWS(wp)
1441 {
1442 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1443 return &wp->w_scrollbars[SBAR_LEFT];
1444 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1445 return &wp->w_scrollbars[SBAR_RIGHT];
1446 }
1447 return NULL;
1448}
1449
1450/*
1451 * Get the character size of a font.
1452 */
1453 static void
1454GetFontSize(GuiFont font)
1455{
1456 HWND hwnd = GetDesktopWindow();
1457 HDC hdc = GetWindowDC(hwnd);
1458 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1459 TEXTMETRIC tm;
1460
1461 GetTextMetrics(hdc, &tm);
1462 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1463
1464 gui.char_height = tm.tmHeight + p_linespace;
1465
1466 SelectFont(hdc, hfntOld);
1467
1468 ReleaseDC(hwnd, hdc);
1469}
1470
1471/*
1472 * Adjust gui.char_height (after 'linespace' was changed).
1473 */
1474 int
1475gui_mch_adjust_charheight(void)
1476{
1477 GetFontSize(gui.norm_font);
1478 return OK;
1479}
1480
1481 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001482get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001483{
1484 HFONT font = NULL;
1485
1486 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001487 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001488
1489 if (font == NULL)
1490 return NOFONT;
1491
1492 return (GuiFont)font;
1493}
1494
1495 static int
1496pixels_to_points(int pixels, int vertical)
1497{
1498 int points;
1499 HWND hwnd;
1500 HDC hdc;
1501
1502 hwnd = GetDesktopWindow();
1503 hdc = GetWindowDC(hwnd);
1504
1505 points = MulDiv(pixels, 72,
1506 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1507
1508 ReleaseDC(hwnd, hdc);
1509
1510 return points;
1511}
1512
1513 GuiFont
1514gui_mch_get_font(
1515 char_u *name,
1516 int giveErrorIfMissing)
1517{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001518 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519 GuiFont font = NOFONT;
1520
1521 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1522 font = get_font_handle(&lf);
1523 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001524 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 return font;
1526}
1527
1528#if defined(FEAT_EVAL) || defined(PROTO)
1529/*
1530 * Return the name of font "font" in allocated memory.
1531 * Don't know how to get the actual name, thus use the provided name.
1532 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001533 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001534gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001535{
1536 if (name == NULL)
1537 return NULL;
1538 return vim_strsave(name);
1539}
1540#endif
1541
1542 void
1543gui_mch_free_font(GuiFont font)
1544{
1545 if (font)
1546 DeleteObject((HFONT)font);
1547}
1548
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001549/*
1550 * Return the Pixel value (color) for the given color name.
1551 * Return INVALCOLOR for error.
1552 */
1553 guicolor_T
1554gui_mch_get_color(char_u *name)
1555{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001556 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001557
1558 typedef struct SysColorTable
1559 {
1560 char *name;
1561 int color;
1562 } SysColorTable;
1563
1564 static SysColorTable sys_table[] =
1565 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1567 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001568#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001569 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1570#endif
1571 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1572 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1573 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1574 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1575 {"SYS_DESKTOP", COLOR_DESKTOP},
1576 {"SYS_INFOBK", COLOR_INFOBK},
1577 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1578 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579 {"SYS_BTNFACE", COLOR_BTNFACE},
1580 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1581 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1582 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1583 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1584 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1585 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1586 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1587 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1588 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1589 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1590 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1591 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1592 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1593 {"SYS_MENU", COLOR_MENU},
1594 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1595 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1596 {"SYS_WINDOW", COLOR_WINDOW},
1597 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1598 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1599 };
1600
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001601 /*
1602 * Try to look up a system colour.
1603 */
1604 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1605 if (STRICMP(name, sys_table[i].name) == 0)
1606 return GetSysColor(sys_table[i].color);
1607
Bram Moolenaarab302212016-04-26 20:59:29 +02001608 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001610
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001611 guicolor_T
1612gui_mch_get_rgb_color(int r, int g, int b)
1613{
1614 return gui_get_rgb_color_cmn(r, g, b);
1615}
1616
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001617/*
1618 * Return OK if the key with the termcap name "name" is supported.
1619 */
1620 int
1621gui_mch_haskey(char_u *name)
1622{
1623 int i;
1624
1625 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1626 if (name[0] == special_keys[i].vim_code0 &&
1627 name[1] == special_keys[i].vim_code1)
1628 return OK;
1629 return FAIL;
1630}
1631
1632 void
1633gui_mch_beep(void)
1634{
1635 MessageBeep(MB_OK);
1636}
1637/*
1638 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1639 */
1640 void
1641gui_mch_invert_rectangle(
1642 int r,
1643 int c,
1644 int nr,
1645 int nc)
1646{
1647 RECT rc;
1648
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001649#if defined(FEAT_DIRECTX)
1650 if (IS_ENABLE_DIRECTX())
1651 DWriteContext_Flush(s_dwc);
1652#endif
1653
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001654 /*
1655 * Note: InvertRect() excludes right and bottom of rectangle.
1656 */
1657 rc.left = FILL_X(c);
1658 rc.top = FILL_Y(r);
1659 rc.right = rc.left + nc * gui.char_width;
1660 rc.bottom = rc.top + nr * gui.char_height;
1661 InvertRect(s_hdc, &rc);
1662}
1663
1664/*
1665 * Iconify the GUI window.
1666 */
1667 void
1668gui_mch_iconify(void)
1669{
1670 ShowWindow(s_hwnd, SW_MINIMIZE);
1671}
1672
1673/*
1674 * Draw a cursor without focus.
1675 */
1676 void
1677gui_mch_draw_hollow_cursor(guicolor_T color)
1678{
1679 HBRUSH hbr;
1680 RECT rc;
1681
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001682#if defined(FEAT_DIRECTX)
1683 if (IS_ENABLE_DIRECTX())
1684 DWriteContext_Flush(s_dwc);
1685#endif
1686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687 /*
1688 * Note: FrameRect() excludes right and bottom of rectangle.
1689 */
1690 rc.left = FILL_X(gui.col);
1691 rc.top = FILL_Y(gui.row);
1692 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001693 if (mb_lefthalve(gui.row, gui.col))
1694 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001695 rc.bottom = rc.top + gui.char_height;
1696 hbr = CreateSolidBrush(color);
1697 FrameRect(s_hdc, &rc, hbr);
1698 DeleteBrush(hbr);
1699}
1700/*
1701 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1702 * color "color".
1703 */
1704 void
1705gui_mch_draw_part_cursor(
1706 int w,
1707 int h,
1708 guicolor_T color)
1709{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001710 RECT rc;
1711
1712 /*
1713 * Note: FillRect() excludes right and bottom of rectangle.
1714 */
1715 rc.left =
1716#ifdef FEAT_RIGHTLEFT
1717 /* vertical line should be on the right of current point */
1718 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1719#endif
1720 FILL_X(gui.col);
1721 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1722 rc.right = rc.left + w;
1723 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001724
Bram Moolenaar92467d32017-12-05 13:22:16 +01001725 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001726}
1727
1728
1729/*
1730 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1731 * dead key's nominal character and re-post the original message.
1732 */
1733 static void
1734outputDeadKey_rePost(MSG originalMsg)
1735{
1736 static MSG deadCharExpel;
1737
1738 if (!dead_key)
1739 return;
1740
1741 dead_key = 0;
1742
1743 /* Make Windows generate the dead key's character */
1744 deadCharExpel.message = originalMsg.message;
1745 deadCharExpel.hwnd = originalMsg.hwnd;
1746 deadCharExpel.wParam = VK_SPACE;
1747
1748 MyTranslateMessage(&deadCharExpel);
1749
1750 /* re-generate the current character free of the dead char influence */
1751 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1752 originalMsg.lParam);
1753}
1754
1755
1756/*
1757 * Process a single Windows message.
1758 * If one is not available we hang until one is.
1759 */
1760 static void
1761process_message(void)
1762{
1763 MSG msg;
1764 UINT vk = 0; /* Virtual key */
1765 char_u string[40];
1766 int i;
1767 int modifiers = 0;
1768 int key;
1769#ifdef FEAT_MENU
1770 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1771#endif
1772
1773 pGetMessage(&msg, NULL, 0, 0);
1774
1775#ifdef FEAT_OLE
1776 /* Look after OLE Automation commands */
1777 if (msg.message == WM_OLE)
1778 {
1779 char_u *str = (char_u *)msg.lParam;
1780 if (str == NULL || *str == NUL)
1781 {
1782 /* Message can't be ours, forward it. Fixes problem with Ultramon
1783 * 3.0.4 */
1784 pDispatchMessage(&msg);
1785 }
1786 else
1787 {
1788 add_to_input_buf(str, (int)STRLEN(str));
1789 vim_free(str); /* was allocated in CVim::SendKeys() */
1790 }
1791 return;
1792 }
1793#endif
1794
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001795#ifdef MSWIN_FIND_REPLACE
1796 /* Don't process messages used by the dialog */
1797 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1798 {
1799 HandleMouseHide(msg.message, msg.lParam);
1800 return;
1801 }
1802#endif
1803
1804 /*
1805 * Check if it's a special key that we recognise. If not, call
1806 * TranslateMessage().
1807 */
1808 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1809 {
1810 vk = (int) msg.wParam;
1811
1812 /*
1813 * Handle dead keys in special conditions in other cases we let Windows
1814 * handle them and do not interfere.
1815 *
1816 * The dead_key flag must be reset on several occasions:
1817 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1818 * consumed at that point (This is when we let Windows combine the
1819 * dead character on its own)
1820 *
1821 * - Before doing something special such as regenerating keypresses to
1822 * expel the dead character as this could trigger an infinite loop if
1823 * for some reason MyTranslateMessage() do not trigger a call
1824 * immediately to _OnChar() (or _OnSysChar()).
1825 */
1826 if (dead_key)
1827 {
1828 /*
1829 * If a dead key was pressed and the user presses VK_SPACE,
1830 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1831 * with the dead char now, so do nothing special and let Windows
1832 * handle it.
1833 *
1834 * Note that VK_SPACE combines with the dead_key's character and
1835 * only one WM_CHAR will be generated by TranslateMessage(), in
1836 * the two other cases two WM_CHAR will be generated: the dead
1837 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1838 * user expects.
1839 */
1840 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1841 {
1842 dead_key = 0;
1843 MyTranslateMessage(&msg);
1844 return;
1845 }
1846 /* In modes where we are not typing, dead keys should behave
1847 * normally */
1848 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1849 {
1850 outputDeadKey_rePost(msg);
1851 return;
1852 }
1853 }
1854
1855 /* Check for CTRL-BREAK */
1856 if (vk == VK_CANCEL)
1857 {
1858 trash_input_buf();
1859 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001860 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001861 string[0] = Ctrl_C;
1862 add_to_input_buf(string, 1);
1863 }
1864
1865 for (i = 0; special_keys[i].key_sym != 0; i++)
1866 {
1867 /* ignore VK_SPACE when ALT key pressed: system menu */
1868 if (special_keys[i].key_sym == vk
1869 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1870 {
1871 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001872 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001873 * is a key that would normally trigger the dead key nominal
1874 * character output (such as a NUMPAD printable character or
1875 * the TAB key, etc...).
1876 */
1877 if (dead_key && (special_keys[i].vim_code0 == 'K'
1878 || vk == VK_TAB || vk == CAR))
1879 {
1880 outputDeadKey_rePost(msg);
1881 return;
1882 }
1883
1884#ifdef FEAT_MENU
1885 /* Check for <F10>: Windows selects the menu. When <F10> is
1886 * mapped we want to use the mapping instead. */
1887 if (vk == VK_F10
1888 && gui.menu_is_active
1889 && check_map(k10, State, FALSE, TRUE, FALSE,
1890 NULL, NULL) == NULL)
1891 break;
1892#endif
1893 if (GetKeyState(VK_SHIFT) & 0x8000)
1894 modifiers |= MOD_MASK_SHIFT;
1895 /*
1896 * Don't use caps-lock as shift, because these are special keys
1897 * being considered here, and we only want letters to get
1898 * shifted -- webb
1899 */
1900 /*
1901 if (GetKeyState(VK_CAPITAL) & 0x0001)
1902 modifiers ^= MOD_MASK_SHIFT;
1903 */
1904 if (GetKeyState(VK_CONTROL) & 0x8000)
1905 modifiers |= MOD_MASK_CTRL;
1906 if (GetKeyState(VK_MENU) & 0x8000)
1907 modifiers |= MOD_MASK_ALT;
1908
1909 if (special_keys[i].vim_code1 == NUL)
1910 key = special_keys[i].vim_code0;
1911 else
1912 key = TO_SPECIAL(special_keys[i].vim_code0,
1913 special_keys[i].vim_code1);
1914 key = simplify_key(key, &modifiers);
1915 if (key == CSI)
1916 key = K_CSI;
1917
1918 if (modifiers)
1919 {
1920 string[0] = CSI;
1921 string[1] = KS_MODIFIER;
1922 string[2] = modifiers;
1923 add_to_input_buf(string, 3);
1924 }
1925
1926 if (IS_SPECIAL(key))
1927 {
1928 string[0] = CSI;
1929 string[1] = K_SECOND(key);
1930 string[2] = K_THIRD(key);
1931 add_to_input_buf(string, 3);
1932 }
1933 else
1934 {
1935 int len;
1936
1937 /* Handle "key" as a Unicode character. */
1938 len = char_to_string(key, string, 40, FALSE);
1939 add_to_input_buf(string, len);
1940 }
1941 break;
1942 }
1943 }
1944 if (special_keys[i].key_sym == 0)
1945 {
1946 /* Some keys need C-S- where they should only need C-.
1947 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1948 * system startup (Helmut Stiegler, 2003 Oct 3). */
1949 if (vk != 0xff
1950 && (GetKeyState(VK_CONTROL) & 0x8000)
1951 && !(GetKeyState(VK_SHIFT) & 0x8000)
1952 && !(GetKeyState(VK_MENU) & 0x8000))
1953 {
1954 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1955 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1956 {
1957 string[0] = Ctrl_HAT;
1958 add_to_input_buf(string, 1);
1959 }
1960 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1961 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1962 {
1963 string[0] = Ctrl__;
1964 add_to_input_buf(string, 1);
1965 }
1966 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1967 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1968 {
1969 string[0] = Ctrl_AT;
1970 add_to_input_buf(string, 1);
1971 }
1972 else
1973 MyTranslateMessage(&msg);
1974 }
1975 else
1976 MyTranslateMessage(&msg);
1977 }
1978 }
1979#ifdef FEAT_MBYTE_IME
1980 else if (msg.message == WM_IME_NOTIFY)
1981 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1982 else if (msg.message == WM_KEYUP && im_get_status())
1983 /* added for non-MS IME (Yasuhiro Matsumoto) */
1984 MyTranslateMessage(&msg);
1985#endif
1986#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1987/* GIME_TEST */
1988 else if (msg.message == WM_IME_STARTCOMPOSITION)
1989 {
1990 POINT point;
1991
1992 global_ime_set_font(&norm_logfont);
1993 point.x = FILL_X(gui.col);
1994 point.y = FILL_Y(gui.row);
1995 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1996 global_ime_set_position(&point);
1997 }
1998#endif
1999
2000#ifdef FEAT_MENU
2001 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2002 * mapped we need to stop it here to avoid strange effects (e.g., for the
2003 * key-up event) */
2004 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2005 NULL, NULL) == NULL)
2006#endif
2007 pDispatchMessage(&msg);
2008}
2009
2010/*
2011 * Catch up with any queued events. This may put keyboard input into the
2012 * input buffer, call resize call-backs, trigger timers etc. If there is
2013 * nothing in the event queue (& no timers pending), then we return
2014 * immediately.
2015 */
2016 void
2017gui_mch_update(void)
2018{
2019 MSG msg;
2020
2021 if (!s_busy_processing)
2022 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2023 && !vim_is_input_buf_full())
2024 process_message();
2025}
2026
Bram Moolenaar4231da42016-06-02 14:30:04 +02002027 static void
2028remove_any_timer(void)
2029{
2030 MSG msg;
2031
2032 if (s_wait_timer != 0 && !s_timed_out)
2033 {
2034 KillTimer(NULL, s_wait_timer);
2035
2036 /* Eat spurious WM_TIMER messages */
2037 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2038 ;
2039 s_wait_timer = 0;
2040 }
2041}
2042
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002043/*
2044 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2045 * from the keyboard.
2046 * wtime == -1 Wait forever.
2047 * wtime == 0 This should never happen.
2048 * wtime > 0 Wait wtime milliseconds for a character.
2049 * Returns OK if a character was found to be available within the given time,
2050 * or FAIL otherwise.
2051 */
2052 int
2053gui_mch_wait_for_chars(int wtime)
2054{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002055 int focus;
2056
2057 s_timed_out = FALSE;
2058
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002059 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002060 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002061 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002062 if (s_busy_processing)
2063 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002064
2065 // When called with "wtime" zero, just want one msec.
2066 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 (TIMERPROC)_OnTimer);
2068 }
2069
2070 allow_scrollbar = TRUE;
2071
2072 focus = gui.in_focus;
2073 while (!s_timed_out)
2074 {
2075 /* Stop or start blinking when focus changes */
2076 if (gui.in_focus != focus)
2077 {
2078 if (gui.in_focus)
2079 gui_mch_start_blink();
2080 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002081 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082 focus = gui.in_focus;
2083 }
2084
2085 if (s_need_activate)
2086 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002088 s_need_activate = FALSE;
2089 }
2090
Bram Moolenaar4231da42016-06-02 14:30:04 +02002091#ifdef FEAT_TIMERS
2092 did_add_timer = FALSE;
2093#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002095 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002096 for (;;)
2097 {
2098 MSG msg;
2099
2100 parse_queued_messages();
2101
Bram Moolenaar62426e12017-08-13 15:37:58 +02002102 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2103 {
2104 process_message();
2105 break;
2106 }
2107 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2108 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002109 break;
2110 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002111#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112 /*
2113 * Don't use gui_mch_update() because then we will spin-lock until a
2114 * char arrives, instead we use GetMessage() to hang until an
2115 * event arrives. No need to check for input_buf_full because we are
2116 * returning as soon as it contains a single char -- webb
2117 */
2118 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002119#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120
2121 if (input_available())
2122 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002123 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124 allow_scrollbar = FALSE;
2125
2126 /* Clear pending mouse button, the release event may have been
2127 * taken by the dialog window. But don't do this when getting
2128 * focus, we need the mouse-up event then. */
2129 if (!s_getting_focus)
2130 s_button_pending = -1;
2131
2132 return OK;
2133 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002134
2135#ifdef FEAT_TIMERS
2136 if (did_add_timer)
2137 {
2138 /* Need to recompute the waiting time. */
2139 remove_any_timer();
2140 break;
2141 }
2142#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 }
2144 allow_scrollbar = FALSE;
2145 return FAIL;
2146}
2147
2148/*
2149 * Clear a rectangular region of the screen from text pos (row1, col1) to
2150 * (row2, col2) inclusive.
2151 */
2152 void
2153gui_mch_clear_block(
2154 int row1,
2155 int col1,
2156 int row2,
2157 int col2)
2158{
2159 RECT rc;
2160
2161 /*
2162 * Clear one extra pixel at the far right, for when bold characters have
2163 * spilled over to the window border.
2164 * Note: FillRect() excludes right and bottom of rectangle.
2165 */
2166 rc.left = FILL_X(col1);
2167 rc.top = FILL_Y(row1);
2168 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2169 rc.bottom = FILL_Y(row2 + 1);
2170 clear_rect(&rc);
2171}
2172
2173/*
2174 * Clear the whole text window.
2175 */
2176 void
2177gui_mch_clear_all(void)
2178{
2179 RECT rc;
2180
2181 rc.left = 0;
2182 rc.top = 0;
2183 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2184 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2185 clear_rect(&rc);
2186}
2187/*
2188 * Menu stuff.
2189 */
2190
2191 void
2192gui_mch_enable_menu(int flag)
2193{
2194#ifdef FEAT_MENU
2195 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2196#endif
2197}
2198
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002199 void
2200gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002201 int x UNUSED,
2202 int y UNUSED,
2203 int w UNUSED,
2204 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002205{
2206 /* It will be in the right place anyway */
2207}
2208
2209#if defined(FEAT_MENU) || defined(PROTO)
2210/*
2211 * Make menu item hidden or not hidden
2212 */
2213 void
2214gui_mch_menu_hidden(
2215 vimmenu_T *menu,
2216 int hidden)
2217{
2218 /*
2219 * This doesn't do what we want. Hmm, just grey the menu items for now.
2220 */
2221 /*
2222 if (hidden)
2223 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2224 else
2225 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2226 */
2227 gui_mch_menu_grey(menu, hidden);
2228}
2229
2230/*
2231 * This is called after setting all the menus to grey/hidden or not.
2232 */
2233 void
2234gui_mch_draw_menubar(void)
2235{
2236 DrawMenuBar(s_hwnd);
2237}
2238#endif /*FEAT_MENU*/
2239
2240#ifndef PROTO
2241void
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242_cdecl
2243SaveInst(HINSTANCE hInst)
2244{
2245 s_hinst = hInst;
2246}
2247#endif
2248
2249/*
2250 * Return the RGB value of a pixel as a long.
2251 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002252 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002253gui_mch_get_rgb(guicolor_T pixel)
2254{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002255 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2256 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002257}
2258
2259#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2260/* Convert pixels in X to dialog units */
2261 static WORD
2262PixelToDialogX(int numPixels)
2263{
2264 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2265}
2266
2267/* Convert pixels in Y to dialog units */
2268 static WORD
2269PixelToDialogY(int numPixels)
2270{
2271 return (WORD)((numPixels * 8) / s_dlgfntheight);
2272}
2273
2274/* Return the width in pixels of the given text in the given DC. */
2275 static int
2276GetTextWidth(HDC hdc, char_u *str, int len)
2277{
2278 SIZE size;
2279
2280 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2281 return size.cx;
2282}
2283
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002284/*
2285 * Return the width in pixels of the given text in the given DC, taking care
2286 * of 'encoding' to active codepage conversion.
2287 */
2288 static int
2289GetTextWidthEnc(HDC hdc, char_u *str, int len)
2290{
2291 SIZE size;
2292 WCHAR *wstr;
2293 int n;
2294 int wlen = len;
2295
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002296 wstr = enc_to_utf16(str, &wlen);
2297 if (wstr == NULL)
2298 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002299
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002300 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2301 vim_free(wstr);
2302 if (n)
2303 return size.cx;
2304 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002307static void get_work_area(RECT *spi_rect);
2308
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309/*
2310 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002311 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2312 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002313 */
2314 static BOOL
2315CenterWindow(
2316 HWND hwndChild,
2317 HWND hwndParent)
2318{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002319 HMONITOR mon;
2320 MONITORINFO moninfo;
2321 RECT rChild, rParent, rScreen;
2322 int wChild, hChild, wParent, hParent;
2323 int xNew, yNew;
2324 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002325
2326 GetWindowRect(hwndChild, &rChild);
2327 wChild = rChild.right - rChild.left;
2328 hChild = rChild.bottom - rChild.top;
2329
2330 /* If Vim is minimized put the window in the middle of the screen. */
2331 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002332 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002333 else
2334 GetWindowRect(hwndParent, &rParent);
2335 wParent = rParent.right - rParent.left;
2336 hParent = rParent.bottom - rParent.top;
2337
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002338 moninfo.cbSize = sizeof(MONITORINFO);
2339 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2340 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002341 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002342 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002343 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002344 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002345 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002346 hdc = GetDC(hwndChild);
2347 rScreen.left = 0;
2348 rScreen.top = 0;
2349 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2350 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2351 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002352 }
2353
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002354 xNew = rParent.left + ((wParent - wChild) / 2);
2355 if (xNew < rScreen.left)
2356 xNew = rScreen.left;
2357 else if ((xNew + wChild) > rScreen.right)
2358 xNew = rScreen.right - wChild;
2359
2360 yNew = rParent.top + ((hParent - hChild) / 2);
2361 if (yNew < rScreen.top)
2362 yNew = rScreen.top;
2363 else if ((yNew + hChild) > rScreen.bottom)
2364 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002365
2366 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2367 SWP_NOSIZE | SWP_NOZORDER);
2368}
2369#endif /* FEAT_GUI_DIALOG */
2370
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371#if defined(FEAT_TOOLBAR) || defined(PROTO)
2372 void
2373gui_mch_show_toolbar(int showit)
2374{
2375 if (s_toolbarhwnd == NULL)
2376 return;
2377
2378 if (showit)
2379 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002380# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002381 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002382# define TB_SETUNICODEFORMAT 0x2005
2383# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002384 // Enable unicode support
2385 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2386 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 ShowWindow(s_toolbarhwnd, SW_SHOW);
2388 }
2389 else
2390 ShowWindow(s_toolbarhwnd, SW_HIDE);
2391}
2392
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002393/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394#define TOOLBAR_BITMAP_COUNT 31
2395
2396#endif
2397
2398#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2399 static void
2400add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2401{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002402 WCHAR *wn;
2403 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002405 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002407 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002409 infow.cbSize = sizeof(infow);
2410 infow.fMask = MIIM_TYPE | MIIM_ID;
2411 infow.wID = item_id;
2412 infow.fType = MFT_STRING;
2413 infow.dwTypeData = wn;
2414 infow.cch = (UINT)wcslen(wn);
2415 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2416 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002417}
2418
2419 static void
2420show_tabline_popup_menu(void)
2421{
2422 HMENU tab_pmenu;
2423 long rval;
2424 POINT pt;
2425
2426 /* When ignoring events don't show the menu. */
2427 if (hold_gui_events
2428# ifdef FEAT_CMDWIN
2429 || cmdwin_type != 0
2430# endif
2431 )
2432 return;
2433
2434 tab_pmenu = CreatePopupMenu();
2435 if (tab_pmenu == NULL)
2436 return;
2437
2438 if (first_tabpage->tp_next != NULL)
2439 add_tabline_popup_menu_entry(tab_pmenu,
2440 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2441 add_tabline_popup_menu_entry(tab_pmenu,
2442 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2443 add_tabline_popup_menu_entry(tab_pmenu,
2444 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2445
2446 GetCursorPos(&pt);
2447 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2448 NULL);
2449
2450 DestroyMenu(tab_pmenu);
2451
2452 /* Add the string cmd into input buffer */
2453 if (rval > 0)
2454 {
2455 TCHITTESTINFO htinfo;
2456 int idx;
2457
2458 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2459 return;
2460
2461 htinfo.pt.x = pt.x;
2462 htinfo.pt.y = pt.y;
2463 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2464 if (idx == -1)
2465 idx = 0;
2466 else
2467 idx += 1;
2468
2469 send_tabline_menu_event(idx, (int)rval);
2470 }
2471}
2472
2473/*
2474 * Show or hide the tabline.
2475 */
2476 void
2477gui_mch_show_tabline(int showit)
2478{
2479 if (s_tabhwnd == NULL)
2480 return;
2481
2482 if (!showit != !showing_tabline)
2483 {
2484 if (showit)
2485 ShowWindow(s_tabhwnd, SW_SHOW);
2486 else
2487 ShowWindow(s_tabhwnd, SW_HIDE);
2488 showing_tabline = showit;
2489 }
2490}
2491
2492/*
2493 * Return TRUE when tabline is displayed.
2494 */
2495 int
2496gui_mch_showing_tabline(void)
2497{
2498 return s_tabhwnd != NULL && showing_tabline;
2499}
2500
2501/*
2502 * Update the labels of the tabline.
2503 */
2504 void
2505gui_mch_update_tabline(void)
2506{
2507 tabpage_T *tp;
2508 TCITEM tie;
2509 int nr = 0;
2510 int curtabidx = 0;
2511 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002512 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513
2514 if (s_tabhwnd == NULL)
2515 return;
2516
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002517#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002519# define CCM_SETUNICODEFORMAT 0x2005
2520#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002521 // Enable unicode support
2522 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523
2524 tie.mask = TCIF_TEXT;
2525 tie.iImage = -1;
2526
2527 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2528 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2529
2530 /* Add a label for each tab page. They all contain the same text area. */
2531 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2532 {
2533 if (tp == curtab)
2534 curtabidx = nr;
2535
2536 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2537 {
2538 /* Add the tab */
2539 tie.pszText = "-Empty-";
2540 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2541 tabadded = 1;
2542 }
2543
2544 get_tabline_label(tp, FALSE);
2545 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002546
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002547 wstr = enc_to_utf16(NameBuff, NULL);
2548 if (wstr != NULL)
2549 {
2550 TCITEMW tiw;
2551
2552 tiw.mask = TCIF_TEXT;
2553 tiw.iImage = -1;
2554 tiw.pszText = wstr;
2555 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2556 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002557 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002558 }
2559
2560 /* Remove any old labels. */
2561 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2562 TabCtrl_DeleteItem(s_tabhwnd, nr);
2563
2564 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2565 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2566
2567 /* Re-enable redraw and redraw. */
2568 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2569 RedrawWindow(s_tabhwnd, NULL, NULL,
2570 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2571
2572 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2573 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2574}
2575
2576/*
2577 * Set the current tab to "nr". First tab is 1.
2578 */
2579 void
2580gui_mch_set_curtab(int nr)
2581{
2582 if (s_tabhwnd == NULL)
2583 return;
2584
2585 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2586 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2587}
2588
2589#endif
2590
2591/*
2592 * ":simalt" command.
2593 */
2594 void
2595ex_simalt(exarg_T *eap)
2596{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002597 char_u *keys = eap->arg;
2598 int fill_typebuf = FALSE;
2599 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002600
2601 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2602 while (*keys)
2603 {
2604 if (*keys == '~')
2605 *keys = ' '; /* for showing system menu */
2606 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2607 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002608 fill_typebuf = TRUE;
2609 }
2610 if (fill_typebuf)
2611 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002612 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002613 * processed. */
2614 key_name[0] = K_SPECIAL;
2615 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002616 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002617 key_name[3] = NUL;
2618 typebuf_was_filled = TRUE;
2619 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 }
2621}
2622
2623/*
2624 * Create the find & replace dialogs.
2625 * You can't have both at once: ":find" when replace is showing, destroys
2626 * the replace dialog first, and the other way around.
2627 */
2628#ifdef MSWIN_FIND_REPLACE
2629 static void
2630initialise_findrep(char_u *initial_string)
2631{
2632 int wword = FALSE;
2633 int mcase = !p_ic;
2634 char_u *entry_text;
2635
2636 /* Get the search string to use. */
2637 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2638
2639 s_findrep_struct.hwndOwner = s_hwnd;
2640 s_findrep_struct.Flags = FR_DOWN;
2641 if (mcase)
2642 s_findrep_struct.Flags |= FR_MATCHCASE;
2643 if (wword)
2644 s_findrep_struct.Flags |= FR_WHOLEWORD;
2645 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002646 {
2647 WCHAR *p = enc_to_utf16(entry_text, NULL);
2648 if (p != NULL)
2649 {
2650 int len = s_findrep_struct.wFindWhatLen - 1;
2651
2652 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2653 s_findrep_struct.lpstrFindWhat[len] = NUL;
2654 vim_free(p);
2655 }
2656 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002657 vim_free(entry_text);
2658}
2659#endif
2660
2661 static void
2662set_window_title(HWND hwnd, char *title)
2663{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002664 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 {
2666 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002667
2668 /* Convert the title from 'encoding' to UTF-16. */
2669 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2670 if (wbuf != NULL)
2671 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002672 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002673 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002674 }
2675 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002676 else
2677 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002678}
2679
2680 void
2681gui_mch_find_dialog(exarg_T *eap)
2682{
2683#ifdef MSWIN_FIND_REPLACE
2684 if (s_findrep_msg != 0)
2685 {
2686 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2687 DestroyWindow(s_findrep_hwnd);
2688
2689 if (!IsWindow(s_findrep_hwnd))
2690 {
2691 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002692 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 }
2694
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002695 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002696 (void)SetFocus(s_findrep_hwnd);
2697
2698 s_findrep_is_find = TRUE;
2699 }
2700#endif
2701}
2702
2703
2704 void
2705gui_mch_replace_dialog(exarg_T *eap)
2706{
2707#ifdef MSWIN_FIND_REPLACE
2708 if (s_findrep_msg != 0)
2709 {
2710 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2711 DestroyWindow(s_findrep_hwnd);
2712
2713 if (!IsWindow(s_findrep_hwnd))
2714 {
2715 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002716 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002717 }
2718
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002719 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 (void)SetFocus(s_findrep_hwnd);
2721
2722 s_findrep_is_find = FALSE;
2723 }
2724#endif
2725}
2726
2727
2728/*
2729 * Set visibility of the pointer.
2730 */
2731 void
2732gui_mch_mousehide(int hide)
2733{
2734 if (hide != gui.pointer_hidden)
2735 {
2736 ShowCursor(!hide);
2737 gui.pointer_hidden = hide;
2738 }
2739}
2740
2741#ifdef FEAT_MENU
2742 static void
2743gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2744{
2745 /* Unhide the mouse, we don't get move events here. */
2746 gui_mch_mousehide(FALSE);
2747
2748 (void)TrackPopupMenu(
2749 (HMENU)menu->submenu_id,
2750 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2751 x, y,
2752 (int)0, /*reserved param*/
2753 s_hwnd,
2754 NULL);
2755 /*
2756 * NOTE: The pop-up menu can eat the mouse up event.
2757 * We deal with this in normal.c.
2758 */
2759}
2760#endif
2761
2762/*
2763 * Got a message when the system will go down.
2764 */
2765 static void
2766_OnEndSession(void)
2767{
2768 getout_preserve_modified(1);
2769}
2770
2771/*
2772 * Get this message when the user clicks on the cross in the top right corner
2773 * of a Windows95 window.
2774 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002775 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002776_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777{
2778 gui_shell_closed();
2779}
2780
2781/*
2782 * Get a message when the window is being destroyed.
2783 */
2784 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002785_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002786{
2787 if (!destroying)
2788 _OnClose(hwnd);
2789}
2790
2791 static void
2792_OnPaint(
2793 HWND hwnd)
2794{
2795 if (!IsMinimized(hwnd))
2796 {
2797 PAINTSTRUCT ps;
2798
2799 out_flush(); /* make sure all output has been processed */
2800 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002801
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002802 /* prevent multi-byte characters from misprinting on an invalid
2803 * rectangle */
2804 if (has_mbyte)
2805 {
2806 RECT rect;
2807
2808 GetClientRect(hwnd, &rect);
2809 ps.rcPaint.left = rect.left;
2810 ps.rcPaint.right = rect.right;
2811 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002812
2813 if (!IsRectEmpty(&ps.rcPaint))
2814 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002815 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2816 ps.rcPaint.right - ps.rcPaint.left + 1,
2817 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2818 }
2819
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 EndPaint(hwnd, &ps);
2821 }
2822}
2823
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 static void
2825_OnSize(
2826 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002827 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828 int cx,
2829 int cy)
2830{
2831 if (!IsMinimized(hwnd))
2832 {
2833 gui_resize_shell(cx, cy);
2834
2835#ifdef FEAT_MENU
2836 /* Menu bar may wrap differently now */
2837 gui_mswin_get_menu_height(TRUE);
2838#endif
2839 }
2840}
2841
2842 static void
2843_OnSetFocus(
2844 HWND hwnd,
2845 HWND hwndOldFocus)
2846{
2847 gui_focus_change(TRUE);
2848 s_getting_focus = TRUE;
2849 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2850}
2851
2852 static void
2853_OnKillFocus(
2854 HWND hwnd,
2855 HWND hwndNewFocus)
2856{
2857 gui_focus_change(FALSE);
2858 s_getting_focus = FALSE;
2859 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2860}
2861
2862/*
2863 * Get a message when the user switches back to vim
2864 */
2865 static LRESULT
2866_OnActivateApp(
2867 HWND hwnd,
2868 BOOL fActivate,
2869 DWORD dwThreadId)
2870{
2871 /* we call gui_focus_change() in _OnSetFocus() */
2872 /* gui_focus_change((int)fActivate); */
2873 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2874}
2875
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876 void
2877gui_mch_destroy_scrollbar(scrollbar_T *sb)
2878{
2879 DestroyWindow(sb->id);
2880}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881
2882/*
2883 * Get current mouse coordinates in text window.
2884 */
2885 void
2886gui_mch_getmouse(int *x, int *y)
2887{
2888 RECT rct;
2889 POINT mp;
2890
2891 (void)GetWindowRect(s_textArea, &rct);
2892 (void)GetCursorPos((LPPOINT)&mp);
2893 *x = (int)(mp.x - rct.left);
2894 *y = (int)(mp.y - rct.top);
2895}
2896
2897/*
2898 * Move mouse pointer to character at (x, y).
2899 */
2900 void
2901gui_mch_setmouse(int x, int y)
2902{
2903 RECT rct;
2904
2905 (void)GetWindowRect(s_textArea, &rct);
2906 (void)SetCursorPos(x + gui.border_offset + rct.left,
2907 y + gui.border_offset + rct.top);
2908}
2909
2910 static void
2911gui_mswin_get_valid_dimensions(
2912 int w,
2913 int h,
2914 int *valid_w,
2915 int *valid_h)
2916{
2917 int base_width, base_height;
2918
2919 base_width = gui_get_base_width()
2920 + (GetSystemMetrics(SM_CXFRAME) +
2921 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2922 base_height = gui_get_base_height()
2923 + (GetSystemMetrics(SM_CYFRAME) +
2924 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2925 + GetSystemMetrics(SM_CYCAPTION)
2926#ifdef FEAT_MENU
2927 + gui_mswin_get_menu_height(FALSE)
2928#endif
2929 ;
2930 *valid_w = base_width +
2931 ((w - base_width) / gui.char_width) * gui.char_width;
2932 *valid_h = base_height +
2933 ((h - base_height) / gui.char_height) * gui.char_height;
2934}
2935
2936 void
2937gui_mch_flash(int msec)
2938{
2939 RECT rc;
2940
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002941#if defined(FEAT_DIRECTX)
2942 if (IS_ENABLE_DIRECTX())
2943 DWriteContext_Flush(s_dwc);
2944#endif
2945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002946 /*
2947 * Note: InvertRect() excludes right and bottom of rectangle.
2948 */
2949 rc.left = 0;
2950 rc.top = 0;
2951 rc.right = gui.num_cols * gui.char_width;
2952 rc.bottom = gui.num_rows * gui.char_height;
2953 InvertRect(s_hdc, &rc);
2954 gui_mch_flush(); /* make sure it's displayed */
2955
2956 ui_delay((long)msec, TRUE); /* wait for a few msec */
2957
2958 InvertRect(s_hdc, &rc);
2959}
2960
2961/*
2962 * Return flags used for scrolling.
2963 * The SW_INVALIDATE is required when part of the window is covered or
2964 * off-screen. Refer to MS KB Q75236.
2965 */
2966 static int
2967get_scroll_flags(void)
2968{
2969 HWND hwnd;
2970 RECT rcVim, rcOther, rcDest;
2971
2972 GetWindowRect(s_hwnd, &rcVim);
2973
2974 /* Check if the window is partly above or below the screen. We don't care
2975 * about partly left or right of the screen, it is not relevant when
2976 * scrolling up or down. */
2977 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2978 return SW_INVALIDATE;
2979
2980 /* Check if there is an window (partly) on top of us. */
2981 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2982 if (IsWindowVisible(hwnd))
2983 {
2984 GetWindowRect(hwnd, &rcOther);
2985 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2986 return SW_INVALIDATE;
2987 }
2988 return 0;
2989}
2990
2991/*
2992 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
2993 * may not be scrolled out properly.
2994 * For gVim, when _OnScroll() is repeated, the character at the
2995 * previous cursor position may be left drawn after scroll.
2996 * The problem can be avoided by calling GetPixel() to get a pixel in
2997 * the region before ScrollWindowEx().
2998 */
2999 static void
3000intel_gpu_workaround(void)
3001{
3002 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3003}
3004
3005/*
3006 * Delete the given number of lines from the given row, scrolling up any
3007 * text further down within the scroll region.
3008 */
3009 void
3010gui_mch_delete_lines(
3011 int row,
3012 int num_lines)
3013{
3014 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 rc.left = FILL_X(gui.scroll_region_left);
3017 rc.right = FILL_X(gui.scroll_region_right + 1);
3018 rc.top = FILL_Y(row);
3019 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3020
Bram Moolenaar92467d32017-12-05 13:22:16 +01003021#if defined(FEAT_DIRECTX)
3022 if (IS_ENABLE_DIRECTX())
3023 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003024 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3025 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003026 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003027 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003028#endif
3029 {
3030 intel_gpu_workaround();
3031 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003033 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003034 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003035
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 /* This seems to be required to avoid the cursor disappearing when
3037 * scrolling such that the cursor ends up in the top-left character on
3038 * the screen... But why? (Webb) */
3039 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3040 /* gui.cursor_is_valid = FALSE; */
3041
3042 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3043 gui.scroll_region_left,
3044 gui.scroll_region_bot, gui.scroll_region_right);
3045}
3046
3047/*
3048 * Insert the given number of lines before the given row, scrolling down any
3049 * following text within the scroll region.
3050 */
3051 void
3052gui_mch_insert_lines(
3053 int row,
3054 int num_lines)
3055{
3056 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003057
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003058 rc.left = FILL_X(gui.scroll_region_left);
3059 rc.right = FILL_X(gui.scroll_region_right + 1);
3060 rc.top = FILL_Y(row);
3061 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003062
3063#if defined(FEAT_DIRECTX)
3064 if (IS_ENABLE_DIRECTX())
3065 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003066 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3067 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003068 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003069 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003070#endif
3071 {
3072 intel_gpu_workaround();
3073 /* The SW_INVALIDATE is required when part of the window is covered or
3074 * off-screen. How do we avoid it when it's not needed? */
3075 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003077 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003078 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003079
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003080 gui_clear_block(row, gui.scroll_region_left,
3081 row + num_lines - 1, gui.scroll_region_right);
3082}
3083
3084
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003085 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003086gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003087{
3088#if defined(FEAT_DIRECTX)
3089 DWriteContext_Close(s_dwc);
3090 DWrite_Final();
3091 s_dwc = NULL;
3092#endif
3093
3094 ReleaseDC(s_textArea, s_hdc);
3095 DeleteObject(s_brush);
3096
3097#ifdef FEAT_TEAROFF
3098 /* Unload the tearoff bitmap */
3099 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3100#endif
3101
3102 /* Destroy our window (if we have one). */
3103 if (s_hwnd != NULL)
3104 {
3105 destroying = TRUE; /* ignore WM_DESTROY message now */
3106 DestroyWindow(s_hwnd);
3107 }
3108
3109#ifdef GLOBAL_IME
3110 global_ime_end();
3111#endif
3112}
3113
3114 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003115logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003116{
3117 char *p;
3118 char *res;
3119 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003120 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003121 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003122 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003123
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003124 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3125 if (font_name == NULL)
3126 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003127 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003128 quality_name = quality_id2name((int)lf.lfQuality);
3129
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003130 res = (char *)alloc((unsigned)(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003131 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
3132 + (quality_name == NULL ? 0 : strlen(quality_name) + 2)));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133 if (res != NULL)
3134 {
3135 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003136 // make a normal font string out of the lf thing:
3137 points = pixels_to_points(
3138 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3139 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3140 sprintf((char *)p, "%s:h%d", font_name, points);
3141 else
3142 sprintf((char *)p, "%s:h%d:W%d", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003143 while (*p)
3144 {
3145 if (*p == ' ')
3146 *p = '_';
3147 ++p;
3148 }
3149 if (lf.lfItalic)
3150 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003151 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003152 STRCAT(p, ":b");
3153 if (lf.lfUnderline)
3154 STRCAT(p, ":u");
3155 if (lf.lfStrikeOut)
3156 STRCAT(p, ":s");
3157 if (charset_name != NULL)
3158 {
3159 STRCAT(p, ":c");
3160 STRCAT(p, charset_name);
3161 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003162 if (quality_name != NULL)
3163 {
3164 STRCAT(p, ":q");
3165 STRCAT(p, quality_name);
3166 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003167 }
3168
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003169 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 return (char_u *)res;
3171}
3172
3173
3174#ifdef FEAT_MBYTE_IME
3175/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003176 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 * 'guifont'
3178 */
3179 static void
3180update_im_font(void)
3181{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003182 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183
3184 if (p_guifontwide != NULL && *p_guifontwide != NUL
3185 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003186 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187 norm_logfont = lf_wide;
3188 else
3189 norm_logfont = sub_logfont;
3190 im_set_font(&norm_logfont);
3191}
3192#endif
3193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194/*
3195 * Handler of gui.wide_font (p_guifontwide) changed notification.
3196 */
3197 void
3198gui_mch_wide_font_changed(void)
3199{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003200 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003202#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003203 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003204#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205
3206 gui_mch_free_font(gui.wide_ital_font);
3207 gui.wide_ital_font = NOFONT;
3208 gui_mch_free_font(gui.wide_bold_font);
3209 gui.wide_bold_font = NOFONT;
3210 gui_mch_free_font(gui.wide_boldital_font);
3211 gui.wide_boldital_font = NOFONT;
3212
3213 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003214 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 {
3216 if (!lf.lfItalic)
3217 {
3218 lf.lfItalic = TRUE;
3219 gui.wide_ital_font = get_font_handle(&lf);
3220 lf.lfItalic = FALSE;
3221 }
3222 if (lf.lfWeight < FW_BOLD)
3223 {
3224 lf.lfWeight = FW_BOLD;
3225 gui.wide_bold_font = get_font_handle(&lf);
3226 if (!lf.lfItalic)
3227 {
3228 lf.lfItalic = TRUE;
3229 gui.wide_boldital_font = get_font_handle(&lf);
3230 }
3231 }
3232 }
3233}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234
3235/*
3236 * Initialise vim to use the font with the given name.
3237 * Return FAIL if the font could not be loaded, OK otherwise.
3238 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003240gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003241{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003242 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003243 GuiFont font = NOFONT;
3244 char_u *p;
3245
3246 /* Load the font */
3247 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3248 font = get_font_handle(&lf);
3249 if (font == NOFONT)
3250 return FAIL;
3251
3252 if (font_name == NULL)
3253 font_name = (char_u *)lf.lfFaceName;
3254#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3255 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003256#endif
3257#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 sub_logfont = lf;
3259#endif
3260#ifdef FEAT_MBYTE_IME
3261 update_im_font();
3262#endif
3263 gui_mch_free_font(gui.norm_font);
3264 gui.norm_font = font;
3265 current_font_height = lf.lfHeight;
3266 GetFontSize(font);
3267
3268 p = logfont2name(lf);
3269 if (p != NULL)
3270 {
3271 hl_set_font_name(p);
3272
3273 /* When setting 'guifont' to "*" replace it with the actual font name.
3274 * */
3275 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3276 {
3277 vim_free(p_guifont);
3278 p_guifont = p;
3279 }
3280 else
3281 vim_free(p);
3282 }
3283
3284 gui_mch_free_font(gui.ital_font);
3285 gui.ital_font = NOFONT;
3286 gui_mch_free_font(gui.bold_font);
3287 gui.bold_font = NOFONT;
3288 gui_mch_free_font(gui.boldital_font);
3289 gui.boldital_font = NOFONT;
3290
3291 if (!lf.lfItalic)
3292 {
3293 lf.lfItalic = TRUE;
3294 gui.ital_font = get_font_handle(&lf);
3295 lf.lfItalic = FALSE;
3296 }
3297 if (lf.lfWeight < FW_BOLD)
3298 {
3299 lf.lfWeight = FW_BOLD;
3300 gui.bold_font = get_font_handle(&lf);
3301 if (!lf.lfItalic)
3302 {
3303 lf.lfItalic = TRUE;
3304 gui.boldital_font = get_font_handle(&lf);
3305 }
3306 }
3307
3308 return OK;
3309}
3310
3311#ifndef WPF_RESTORETOMAXIMIZED
3312# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3313#endif
3314
3315/*
3316 * Return TRUE if the GUI window is maximized, filling the whole screen.
3317 */
3318 int
3319gui_mch_maximized(void)
3320{
3321 WINDOWPLACEMENT wp;
3322
3323 wp.length = sizeof(WINDOWPLACEMENT);
3324 if (GetWindowPlacement(s_hwnd, &wp))
3325 return wp.showCmd == SW_SHOWMAXIMIZED
3326 || (wp.showCmd == SW_SHOWMINIMIZED
3327 && wp.flags == WPF_RESTORETOMAXIMIZED);
3328
3329 return 0;
3330}
3331
3332/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003333 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3334 * is set. Compute the new Rows and Columns. This is like resizing the
3335 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336 */
3337 void
3338gui_mch_newfont(void)
3339{
3340 RECT rect;
3341
3342 GetWindowRect(s_hwnd, &rect);
3343 if (win_socket_id == 0)
3344 {
3345 gui_resize_shell(rect.right - rect.left
3346 - (GetSystemMetrics(SM_CXFRAME) +
3347 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3348 rect.bottom - rect.top
3349 - (GetSystemMetrics(SM_CYFRAME) +
3350 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3351 - GetSystemMetrics(SM_CYCAPTION)
3352#ifdef FEAT_MENU
3353 - gui_mswin_get_menu_height(FALSE)
3354#endif
3355 );
3356 }
3357 else
3358 {
3359 /* Inside another window, don't use the frame and border. */
3360 gui_resize_shell(rect.right - rect.left,
3361 rect.bottom - rect.top
3362#ifdef FEAT_MENU
3363 - gui_mswin_get_menu_height(FALSE)
3364#endif
3365 );
3366 }
3367}
3368
3369/*
3370 * Set the window title
3371 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372 void
3373gui_mch_settitle(
3374 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003375 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376{
3377 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3378}
3379
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003380#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003381/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3382 * misc2.c! */
3383static LPCSTR mshape_idcs[] =
3384{
3385 IDC_ARROW, /* arrow */
3386 MAKEINTRESOURCE(0), /* blank */
3387 IDC_IBEAM, /* beam */
3388 IDC_SIZENS, /* updown */
3389 IDC_SIZENS, /* udsizing */
3390 IDC_SIZEWE, /* leftright */
3391 IDC_SIZEWE, /* lrsizing */
3392 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003393 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 IDC_ARROW, /* crosshair */
3395 IDC_ARROW, /* hand1 */
3396 IDC_ARROW, /* hand2 */
3397 IDC_ARROW, /* pencil */
3398 IDC_ARROW, /* question */
3399 IDC_ARROW, /* right-arrow */
3400 IDC_UPARROW, /* up-arrow */
3401 IDC_ARROW /* last one */
3402};
3403
3404 void
3405mch_set_mouse_shape(int shape)
3406{
3407 LPCSTR idc;
3408
3409 if (shape == MSHAPE_HIDE)
3410 ShowCursor(FALSE);
3411 else
3412 {
3413 if (shape >= MSHAPE_NUMBERED)
3414 idc = IDC_ARROW;
3415 else
3416 idc = mshape_idcs[shape];
3417#ifdef SetClassLongPtr
3418 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3419#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421#endif
3422 if (!p_mh)
3423 {
3424 POINT mp;
3425
3426 /* Set the position to make it redrawn with the new shape. */
3427 (void)GetCursorPos((LPPOINT)&mp);
3428 (void)SetCursorPos(mp.x, mp.y);
3429 ShowCursor(TRUE);
3430 }
3431 }
3432}
3433#endif
3434
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003435#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003437 * Wide version of convert_filter().
3438 */
3439 static WCHAR *
3440convert_filterW(char_u *s)
3441{
3442 char_u *tmp;
3443 int len;
3444 WCHAR *res;
3445
3446 tmp = convert_filter(s);
3447 if (tmp == NULL)
3448 return NULL;
3449 len = (int)STRLEN(s) + 3;
3450 res = enc_to_utf16(tmp, &len);
3451 vim_free(tmp);
3452 return res;
3453}
3454
3455/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003456 * Pop open a file browser and return the file selected, in allocated memory,
3457 * or NULL if Cancel is hit.
3458 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3459 * title - Title message for the file browser dialog.
3460 * dflt - Default name of file.
3461 * ext - Default extension to be added to files without extensions.
3462 * initdir - directory in which to open the browser (NULL = current dir)
3463 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003465 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003466gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467 int saving,
3468 char_u *title,
3469 char_u *dflt,
3470 char_u *ext,
3471 char_u *initdir,
3472 char_u *filter)
3473{
3474 /* We always use the wide function. This means enc_to_utf16() must work,
3475 * otherwise it fails miserably! */
3476 OPENFILENAMEW fileStruct;
3477 WCHAR fileBuf[MAXPATHL];
3478 WCHAR *wp;
3479 int i;
3480 WCHAR *titlep = NULL;
3481 WCHAR *extp = NULL;
3482 WCHAR *initdirp = NULL;
3483 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003484 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485
3486 if (dflt == NULL)
3487 fileBuf[0] = NUL;
3488 else
3489 {
3490 wp = enc_to_utf16(dflt, NULL);
3491 if (wp == NULL)
3492 fileBuf[0] = NUL;
3493 else
3494 {
3495 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3496 fileBuf[i] = wp[i];
3497 fileBuf[i] = NUL;
3498 vim_free(wp);
3499 }
3500 }
3501
3502 /* Convert the filter to Windows format. */
3503 filterp = convert_filterW(filter);
3504
3505 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003506# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003508 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003509# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003511# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512
3513 if (title != NULL)
3514 titlep = enc_to_utf16(title, NULL);
3515 fileStruct.lpstrTitle = titlep;
3516
3517 if (ext != NULL)
3518 extp = enc_to_utf16(ext, NULL);
3519 fileStruct.lpstrDefExt = extp;
3520
3521 fileStruct.lpstrFile = fileBuf;
3522 fileStruct.nMaxFile = MAXPATHL;
3523 fileStruct.lpstrFilter = filterp;
3524 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3525 /* has an initial dir been specified? */
3526 if (initdir != NULL && *initdir != NUL)
3527 {
3528 /* Must have backslashes here, no matter what 'shellslash' says */
3529 initdirp = enc_to_utf16(initdir, NULL);
3530 if (initdirp != NULL)
3531 {
3532 for (wp = initdirp; *wp != NUL; ++wp)
3533 if (*wp == '/')
3534 *wp = '\\';
3535 }
3536 fileStruct.lpstrInitialDir = initdirp;
3537 }
3538
3539 /*
3540 * TODO: Allow selection of multiple files. Needs another arg to this
3541 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3542 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3543 * files that don't exist yet, so I haven't put it in. What about
3544 * OFN_PATHMUSTEXIST?
3545 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3546 */
3547 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003548# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549 if (curbuf->b_p_bin)
3550 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003551# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552 if (saving)
3553 {
3554 if (!GetSaveFileNameW(&fileStruct))
3555 return NULL;
3556 }
3557 else
3558 {
3559 if (!GetOpenFileNameW(&fileStruct))
3560 return NULL;
3561 }
3562
3563 vim_free(filterp);
3564 vim_free(initdirp);
3565 vim_free(titlep);
3566 vim_free(extp);
3567
3568 /* Convert from UCS2 to 'encoding'. */
3569 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003570 if (p == NULL)
3571 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572
3573 /* Give focus back to main window (when using MDI). */
3574 SetFocus(s_hwnd);
3575
3576 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003577 q = vim_strsave(shorten_fname1(p));
3578 vim_free(p);
3579 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581
3582
3583/*
3584 * Convert the string s to the proper format for a filter string by replacing
3585 * the \t and \n delimiters with \0.
3586 * Returns the converted string in allocated memory.
3587 *
3588 * Keep in sync with convert_filterW() above!
3589 */
3590 static char_u *
3591convert_filter(char_u *s)
3592{
3593 char_u *res;
3594 unsigned s_len = (unsigned)STRLEN(s);
3595 unsigned i;
3596
3597 res = alloc(s_len + 3);
3598 if (res != NULL)
3599 {
3600 for (i = 0; i < s_len; ++i)
3601 if (s[i] == '\t' || s[i] == '\n')
3602 res[i] = '\0';
3603 else
3604 res[i] = s[i];
3605 res[s_len] = NUL;
3606 /* Add two extra NULs to make sure it's properly terminated. */
3607 res[s_len + 1] = NUL;
3608 res[s_len + 2] = NUL;
3609 }
3610 return res;
3611}
3612
3613/*
3614 * Select a directory.
3615 */
3616 char_u *
3617gui_mch_browsedir(char_u *title, char_u *initdir)
3618{
3619 /* We fake this: Use a filter that doesn't select anything and a default
3620 * file name that won't be used. */
3621 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3622 initdir, (char_u *)_("Directory\t*.nothing\n"));
3623}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003624#endif /* FEAT_BROWSE */
3625
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003626 static void
3627_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003628 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003629 HDROP hDrop)
3630{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003631#define BUFPATHLEN _MAX_PATH
3632#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634 char szFile[BUFPATHLEN];
3635 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3636 UINT i;
3637 char_u **fnames;
3638 POINT pt;
3639 int_u modifiers = 0;
3640
3641 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3642
3643 /* Obtain dropped position */
3644 DragQueryPoint(hDrop, &pt);
3645 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3646
3647 reset_VIsual();
3648
3649 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3650
3651 if (fnames != NULL)
3652 for (i = 0; i < cFiles; ++i)
3653 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3655 fnames[i] = utf16_to_enc(wszFile, NULL);
3656 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657 {
3658 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3659 fnames[i] = vim_strsave((char_u *)szFile);
3660 }
3661 }
3662
3663 DragFinish(hDrop);
3664
3665 if (fnames != NULL)
3666 {
3667 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3668 modifiers |= MOUSE_SHIFT;
3669 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3670 modifiers |= MOUSE_CTRL;
3671 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3672 modifiers |= MOUSE_ALT;
3673
3674 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3675
3676 s_need_activate = TRUE;
3677 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678}
3679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 static int
3681_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003682 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683 HWND hwndCtl,
3684 UINT code,
3685 int pos)
3686{
3687 static UINT prev_code = 0; /* code of previous call */
3688 scrollbar_T *sb, *sb_info;
3689 long val;
3690 int dragging = FALSE;
3691 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692 SCROLLINFO si;
3693
3694 si.cbSize = sizeof(si);
3695 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003696
3697 sb = gui_mswin_find_scrollbar(hwndCtl);
3698 if (sb == NULL)
3699 return 0;
3700
3701 if (sb->wp != NULL) /* Left or right scrollbar */
3702 {
3703 /*
3704 * Careful: need to get scrollbar info out of first (left) scrollbar
3705 * for window, but keep real scrollbar too because we must pass it to
3706 * gui_drag_scrollbar().
3707 */
3708 sb_info = &sb->wp->w_scrollbars[0];
3709 }
3710 else /* Bottom scrollbar */
3711 sb_info = sb;
3712 val = sb_info->value;
3713
3714 switch (code)
3715 {
3716 case SB_THUMBTRACK:
3717 val = pos;
3718 dragging = TRUE;
3719 if (sb->scroll_shift > 0)
3720 val <<= sb->scroll_shift;
3721 break;
3722 case SB_LINEDOWN:
3723 val++;
3724 break;
3725 case SB_LINEUP:
3726 val--;
3727 break;
3728 case SB_PAGEDOWN:
3729 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3730 break;
3731 case SB_PAGEUP:
3732 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3733 break;
3734 case SB_TOP:
3735 val = 0;
3736 break;
3737 case SB_BOTTOM:
3738 val = sb_info->max;
3739 break;
3740 case SB_ENDSCROLL:
3741 if (prev_code == SB_THUMBTRACK)
3742 {
3743 /*
3744 * "pos" only gives us 16-bit data. In case of large file,
3745 * use GetScrollPos() which returns 32-bit. Unfortunately it
3746 * is not valid while the scrollbar is being dragged.
3747 */
3748 val = GetScrollPos(hwndCtl, SB_CTL);
3749 if (sb->scroll_shift > 0)
3750 val <<= sb->scroll_shift;
3751 }
3752 break;
3753
3754 default:
3755 /* TRACE("Unknown scrollbar event %d\n", code); */
3756 return 0;
3757 }
3758 prev_code = code;
3759
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003760 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3761 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762
3763 /*
3764 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3765 */
3766 if (sb->wp != NULL)
3767 {
3768 scrollbar_T *sba = sb->wp->w_scrollbars;
3769 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3770
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 }
3773
3774 /* Don't let us be interrupted here by another message. */
3775 s_busy_processing = TRUE;
3776
3777 /* When "allow_scrollbar" is FALSE still need to remember the new
3778 * position, but don't actually scroll by setting "dont_scroll". */
3779 dont_scroll = !allow_scrollbar;
3780
Bram Moolenaara338adc2018-01-31 20:51:47 +01003781 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003783 mch_enable_flush();
3784 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785
3786 s_busy_processing = FALSE;
3787 dont_scroll = dont_scroll_save;
3788
3789 return 0;
3790}
3791
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#ifdef FEAT_XPM_W32
3794# include "xpm_w32.h"
3795#endif
3796
3797#ifdef PROTO
3798# define WINAPI
3799#endif
3800
3801#ifdef __MINGW32__
3802/*
3803 * Add a lot of missing defines.
3804 * They are not always missing, we need the #ifndef's.
3805 */
3806# ifndef _cdecl
3807# define _cdecl
3808# endif
3809# ifndef IsMinimized
3810# define IsMinimized(hwnd) IsIconic(hwnd)
3811# endif
3812# ifndef IsMaximized
3813# define IsMaximized(hwnd) IsZoomed(hwnd)
3814# endif
3815# ifndef SelectFont
3816# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3817# endif
3818# ifndef GetStockBrush
3819# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3820# endif
3821# ifndef DeleteBrush
3822# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3823# endif
3824
3825# ifndef HANDLE_WM_RBUTTONDBLCLK
3826# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3827 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3828# endif
3829# ifndef HANDLE_WM_MBUTTONUP
3830# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3831 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3832# endif
3833# ifndef HANDLE_WM_MBUTTONDBLCLK
3834# define HANDLE_WM_MBUTTONDBLCLK(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_LBUTTONDBLCLK
3838# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3839 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3840# endif
3841# ifndef HANDLE_WM_RBUTTONDOWN
3842# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3843 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3844# endif
3845# ifndef HANDLE_WM_MOUSEMOVE
3846# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3847 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3848# endif
3849# ifndef HANDLE_WM_RBUTTONUP
3850# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3851 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3852# endif
3853# ifndef HANDLE_WM_MBUTTONDOWN
3854# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3855 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3856# endif
3857# ifndef HANDLE_WM_LBUTTONUP
3858# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3859 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3860# endif
3861# ifndef HANDLE_WM_LBUTTONDOWN
3862# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3863 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3864# endif
3865# ifndef HANDLE_WM_SYSCHAR
3866# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3867 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3868# endif
3869# ifndef HANDLE_WM_ACTIVATEAPP
3870# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3871 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3872# endif
3873# ifndef HANDLE_WM_WINDOWPOSCHANGING
3874# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3875 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3876# endif
3877# ifndef HANDLE_WM_VSCROLL
3878# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3879 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3880# endif
3881# ifndef HANDLE_WM_SETFOCUS
3882# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3883 ((fn)((hwnd), (HWND)(wParam)), 0L)
3884# endif
3885# ifndef HANDLE_WM_KILLFOCUS
3886# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3887 ((fn)((hwnd), (HWND)(wParam)), 0L)
3888# endif
3889# ifndef HANDLE_WM_HSCROLL
3890# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3891 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3892# endif
3893# ifndef HANDLE_WM_DROPFILES
3894# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3895 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3896# endif
3897# ifndef HANDLE_WM_CHAR
3898# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3899 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3900# endif
3901# ifndef HANDLE_WM_SYSDEADCHAR
3902# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3903 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3904# endif
3905# ifndef HANDLE_WM_DEADCHAR
3906# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3907 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3908# endif
3909#endif /* __MINGW32__ */
3910
3911
3912/* Some parameters for tearoff menus. All in pixels. */
3913#define TEAROFF_PADDING_X 2
3914#define TEAROFF_BUTTON_PAD_X 8
3915#define TEAROFF_MIN_WIDTH 200
3916#define TEAROFF_SUBMENU_LABEL ">>"
3917#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3918
3919
3920/* For the Intellimouse: */
3921#ifndef WM_MOUSEWHEEL
3922#define WM_MOUSEWHEEL 0x20a
3923#endif
3924
3925
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003926#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927# define ID_BEVAL_TOOLTIP 200
3928# define BEVAL_TEXT_LEN MAXPATHL
3929
Bram Moolenaar167632f2010-05-26 21:42:54 +02003930#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003931/* Work around old versions of basetsd.h which wrongly declares
3932 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003933# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003934# define UINT_PTR UINT
3935#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003938static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003940
Bram Moolenaar82881492012-11-20 16:53:39 +01003941
3942/* cproto fails on missing include files */
3943#ifndef PROTO
3944
Bram Moolenaar45360022005-07-21 21:08:21 +00003945/*
3946 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003947 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003948 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003949# include <pshpack1.h>
3950
3951#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003952
3953typedef struct _DllVersionInfo
3954{
3955 DWORD cbSize;
3956 DWORD dwMajorVersion;
3957 DWORD dwMinorVersion;
3958 DWORD dwBuildNumber;
3959 DWORD dwPlatformID;
3960} DLLVERSIONINFO;
3961
Bram Moolenaar82881492012-11-20 16:53:39 +01003962#ifndef PROTO
3963# include <poppack.h>
3964#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003965
Bram Moolenaar45360022005-07-21 21:08:21 +00003966typedef struct tagTOOLINFOA_NEW
3967{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003968 UINT cbSize;
3969 UINT uFlags;
3970 HWND hwnd;
3971 UINT_PTR uId;
3972 RECT rect;
3973 HINSTANCE hinst;
3974 LPSTR lpszText;
3975 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003976} TOOLINFO_NEW;
3977
3978typedef struct tagNMTTDISPINFO_NEW
3979{
3980 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003981 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003982 char szText[80];
3983 HINSTANCE hinst;
3984 UINT uFlags;
3985 LPARAM lParam;
3986} NMTTDISPINFO_NEW;
3987
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003988typedef struct tagTOOLINFOW_NEW
3989{
3990 UINT cbSize;
3991 UINT uFlags;
3992 HWND hwnd;
3993 UINT_PTR uId;
3994 RECT rect;
3995 HINSTANCE hinst;
3996 LPWSTR lpszText;
3997 LPARAM lParam;
3998 void *lpReserved;
3999} TOOLINFOW_NEW;
4000
4001typedef struct tagNMTTDISPINFOW_NEW
4002{
4003 NMHDR hdr;
4004 LPWSTR lpszText;
4005 WCHAR szText[80];
4006 HINSTANCE hinst;
4007 UINT uFlags;
4008 LPARAM lParam;
4009} NMTTDISPINFOW_NEW;
4010
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004011
Bram Moolenaar45360022005-07-21 21:08:21 +00004012typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4013#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004014# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#endif
4016
Bram Moolenaar45360022005-07-21 21:08:21 +00004017#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004018# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004019#endif
4020
4021#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004022# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004023#endif
4024
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004025#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004026
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004027#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4028/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4029 * it here if LPNMTTDISPINFO isn't defined.
4030 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4031 * _MSC_VER. */
4032# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4033typedef struct tagNMTTDISPINFOA {
4034 NMHDR hdr;
4035 LPSTR lpszText;
4036 char szText[80];
4037 HINSTANCE hinst;
4038 UINT uFlags;
4039 LPARAM lParam;
4040} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4041# define LPNMTTDISPINFO LPNMTTDISPINFOA
4042
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004043typedef struct tagNMTTDISPINFOW {
4044 NMHDR hdr;
4045 LPWSTR lpszText;
4046 WCHAR szText[80];
4047 HINSTANCE hinst;
4048 UINT uFlags;
4049 LPARAM lParam;
4050} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004051# endif
4052#endif
4053
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004054#ifndef TTN_GETDISPINFOW
4055# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4056#endif
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058/* Local variables: */
4059
4060#ifdef FEAT_MENU
4061static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064/*
4065 * Use the system font for dialogs and tear-off menus. Remove this line to
4066 * use DLG_FONT_NAME.
4067 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004068#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#define VIM_CLASSW L"Vim"
4072
4073/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4074 * thus there should be room for every dialog. For tearoffs it's made bigger
4075 * when needed. */
4076#define DLG_ALLOC_SIZE 16 * 1024
4077
4078/*
4079 * stuff for dialogs, menus, tearoffs etc.
4080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081static PWORD
4082add_dialog_element(
4083 PWORD p,
4084 DWORD lStyle,
4085 WORD x,
4086 WORD y,
4087 WORD w,
4088 WORD h,
4089 WORD Id,
4090 WORD clss,
4091 const char *caption);
4092static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004093static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004094#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097static void get_dialog_font_metrics(void);
4098
4099static int dialog_default_button = -1;
4100
4101/* Intellimouse support */
4102static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4105#ifdef FEAT_TOOLBAR
4106static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004107static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108static int get_toolbar_bitmap(vimmenu_T *menu);
4109#endif
4110
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004111#ifdef FEAT_GUI_TABLINE
4112static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004113static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004114#endif
4115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#ifdef FEAT_MBYTE_IME
4117static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4118static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4119#endif
4120#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4121# ifdef NOIME
4122typedef struct tagCOMPOSITIONFORM {
4123 DWORD dwStyle;
4124 POINT ptCurrentPos;
4125 RECT rcArea;
4126} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4127typedef HANDLE HIMC;
4128# endif
4129
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004130static HINSTANCE hLibImm = NULL;
4131static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4132static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4133static HIMC (WINAPI *pImmGetContext)(HWND);
4134static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4135static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4136static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4137static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004138static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4139static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004140static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4141static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004142static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143static void dyn_imm_load(void);
4144#else
4145# define pImmGetCompositionStringA ImmGetCompositionStringA
4146# define pImmGetCompositionStringW ImmGetCompositionStringW
4147# define pImmGetContext ImmGetContext
4148# define pImmAssociateContext ImmAssociateContext
4149# define pImmReleaseContext ImmReleaseContext
4150# define pImmGetOpenStatus ImmGetOpenStatus
4151# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004152# define pImmGetCompositionFontW ImmGetCompositionFontW
4153# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154# define pImmSetCompositionWindow ImmSetCompositionWindow
4155# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004156# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#endif
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159#ifdef FEAT_MENU
4160/*
4161 * Figure out how high the menu bar is at the moment.
4162 */
4163 static int
4164gui_mswin_get_menu_height(
4165 int fix_window) /* If TRUE, resize window if menu height changed */
4166{
4167 static int old_menu_height = -1;
4168
4169 RECT rc1, rc2;
4170 int num;
4171 int menu_height;
4172
4173 if (gui.menu_is_active)
4174 num = GetMenuItemCount(s_menuBar);
4175 else
4176 num = 0;
4177
4178 if (num == 0)
4179 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004180 else if (IsMinimized(s_hwnd))
4181 {
4182 /* The height of the menu cannot be determined while the window is
4183 * minimized. Take the previous height if the menu is changed in that
4184 * state, to avoid that Vim's vertical window size accidentally
4185 * increases due to the unaccounted-for menu height. */
4186 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 else
4189 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004190 /*
4191 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4192 * seem to have been set yet, so menu wraps in default window
4193 * width which is very narrow. Instead just return height of a
4194 * single menu item. Will still be wrong when the menu really
4195 * should wrap over more than one line.
4196 */
4197 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4198 if (gui.starting)
4199 menu_height = rc1.bottom - rc1.top + 1;
4200 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004202 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4203 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206
4207 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004208 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004209 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 return menu_height;
4212}
4213#endif /*FEAT_MENU*/
4214
4215
4216/*
4217 * Setup for the Intellimouse
4218 */
4219 static void
4220init_mouse_wheel(void)
4221{
4222
4223#ifndef SPI_GETWHEELSCROLLLINES
4224# define SPI_GETWHEELSCROLLLINES 104
4225#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004226#ifndef SPI_SETWHEELSCROLLLINES
4227# define SPI_SETWHEELSCROLLLINES 105
4228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4231#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4232#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4233#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4234
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 mouse_scroll_lines = 3; /* reasonable default */
4236
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004237 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4238 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4239 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240}
4241
4242
4243/* Intellimouse wheel handler */
4244 static void
4245_OnMouseWheel(
4246 HWND hwnd,
4247 short zDelta)
4248{
4249/* Treat a mouse wheel event as if it were a scroll request */
4250 int i;
4251 int size;
4252 HWND hwndCtl;
4253
4254 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4255 {
4256 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4257 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4258 }
4259 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4260 {
4261 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4262 size = curwin->w_scrollbars[SBAR_LEFT].size;
4263 }
4264 else
4265 return;
4266
4267 size = curwin->w_height;
4268 if (mouse_scroll_lines == 0)
4269 init_mouse_wheel();
4270
Bram Moolenaara338adc2018-01-31 20:51:47 +01004271 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 if (mouse_scroll_lines > 0
4273 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4274 {
4275 for (i = mouse_scroll_lines; i > 0; --i)
4276 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4277 }
4278 else
4279 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004280 mch_enable_flush();
4281 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282}
4283
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004284#ifdef USE_SYSMENU_FONT
4285/*
4286 * Get Menu Font.
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004290gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004291{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004292 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004293
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004294 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4295 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004296 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004297 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004298 &nm,
4299 0))
4300 return FAIL;
4301 *lf = nm.lfMenuFont;
4302 return OK;
4303}
4304#endif
4305
4306
4307#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4308/*
4309 * Set the GUI tabline font to the system menu font
4310 */
4311 static void
4312set_tabline_font(void)
4313{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004314 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004315 HFONT font;
4316 HWND hwnd;
4317 HDC hdc;
4318 HFONT hfntOld;
4319 TEXTMETRIC tm;
4320
4321 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4322 return;
4323
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004324 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004325
4326 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4327
4328 /*
4329 * Compute the height of the font used for the tab text
4330 */
4331 hwnd = GetDesktopWindow();
4332 hdc = GetWindowDC(hwnd);
4333 hfntOld = SelectFont(hdc, font);
4334
4335 GetTextMetrics(hdc, &tm);
4336
4337 SelectFont(hdc, hfntOld);
4338 ReleaseDC(hwnd, hdc);
4339
4340 /*
4341 * The space used by the tab border and the space between the tab label
4342 * and the tab border is included as 7.
4343 */
4344 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4345}
4346#endif
4347
Bram Moolenaar520470a2005-06-16 21:59:56 +00004348/*
4349 * Invoked when a setting was changed.
4350 */
4351 static LRESULT CALLBACK
4352_OnSettingChange(UINT n)
4353{
4354 if (n == SPI_SETWHEELSCROLLLINES)
4355 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4356 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004357#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4358 if (n == SPI_SETNONCLIENTMETRICS)
4359 set_tabline_font();
4360#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004361 return 0;
4362}
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#ifdef FEAT_NETBEANS_INTG
4365 static void
4366_OnWindowPosChanged(
4367 HWND hwnd,
4368 const LPWINDOWPOS lpwpos)
4369{
4370 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004371 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4374 || lpwpos->cx != cx || lpwpos->cy != cy))
4375 {
4376 x = lpwpos->x;
4377 y = lpwpos->y;
4378 cx = lpwpos->cx;
4379 cy = lpwpos->cy;
4380 netbeans_frame_moved(x, y);
4381 }
4382 /* Allow to send WM_SIZE and WM_MOVE */
4383 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4384}
4385#endif
4386
4387 static int
4388_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 UINT fwSide,
4390 LPRECT lprc)
4391{
4392 int w, h;
4393 int valid_w, valid_h;
4394 int w_offset, h_offset;
4395
4396 w = lprc->right - lprc->left;
4397 h = lprc->bottom - lprc->top;
4398 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4399 w_offset = w - valid_w;
4400 h_offset = h - valid_h;
4401
4402 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4403 || fwSide == WMSZ_BOTTOMLEFT)
4404 lprc->left += w_offset;
4405 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4406 || fwSide == WMSZ_BOTTOMRIGHT)
4407 lprc->right -= w_offset;
4408
4409 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4410 || fwSide == WMSZ_TOPRIGHT)
4411 lprc->top += h_offset;
4412 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4413 || fwSide == WMSZ_BOTTOMRIGHT)
4414 lprc->bottom -= h_offset;
4415 return TRUE;
4416}
4417
4418
4419
4420 static LRESULT CALLBACK
4421_WndProc(
4422 HWND hwnd,
4423 UINT uMsg,
4424 WPARAM wParam,
4425 LPARAM lParam)
4426{
4427 /*
4428 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4429 hwnd, uMsg, wParam, lParam);
4430 */
4431
4432 HandleMouseHide(uMsg, lParam);
4433
4434 s_uMsg = uMsg;
4435 s_wParam = wParam;
4436 s_lParam = lParam;
4437
4438 switch (uMsg)
4439 {
4440 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4441 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4442 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4443 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4444 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4445 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4446 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4447 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4448 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4449#ifdef FEAT_MENU
4450 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4451#endif
4452 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4453 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4454 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4455 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4456 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4457 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4458 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4459 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4460 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4461#ifdef FEAT_NETBEANS_INTG
4462 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4463#endif
4464
Bram Moolenaarafa24992006-03-27 20:58:26 +00004465#ifdef FEAT_GUI_TABLINE
4466 case WM_RBUTTONUP:
4467 {
4468 if (gui_mch_showing_tabline())
4469 {
4470 POINT pt;
4471 RECT rect;
4472
4473 /*
4474 * If the cursor is on the tabline, display the tab menu
4475 */
4476 GetCursorPos((LPPOINT)&pt);
4477 GetWindowRect(s_textArea, &rect);
4478 if (pt.y < rect.top)
4479 {
4480 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004481 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004482 }
4483 }
4484 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4485 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004486 case WM_LBUTTONDBLCLK:
4487 {
4488 /*
4489 * If the user double clicked the tabline, create a new tab
4490 */
4491 if (gui_mch_showing_tabline())
4492 {
4493 POINT pt;
4494 RECT rect;
4495
4496 GetCursorPos((LPPOINT)&pt);
4497 GetWindowRect(s_textArea, &rect);
4498 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004499 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004500 }
4501 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4502 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 case WM_QUERYENDSESSION: /* System wants to go down. */
4506 gui_shell_closed(); /* Will exit when no changed buffers. */
4507 return FALSE; /* Do NOT allow system to go down. */
4508
4509 case WM_ENDSESSION:
4510 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004513 return 0L;
4514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 case WM_CHAR:
4518 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4519 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004520 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 return 0L;
4522
4523 case WM_SYSCHAR:
4524 /*
4525 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4526 * shortcut key, handle like a typed ALT key, otherwise call Windows
4527 * ALT key handling.
4528 */
4529#ifdef FEAT_MENU
4530 if ( !gui.menu_is_active
4531 || p_wak[0] == 'n'
4532 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4533 )
4534#endif
4535 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004536 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 return 0L;
4538 }
4539#ifdef FEAT_MENU
4540 else
4541 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4542#endif
4543
4544 case WM_SYSKEYUP:
4545#ifdef FEAT_MENU
4546 /* This used to be done only when menu is active: ALT key is used for
4547 * that. But that caused problems when menu is disabled and using
4548 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4549 * are received, mouse pointer remains hidden. */
4550 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4551#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004552 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#endif
4554
4555 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004556 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 case WM_MOUSEWHEEL:
4559 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004560 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar520470a2005-06-16 21:59:56 +00004562 /* Notification for change in SystemParametersInfo() */
4563 case WM_SETTINGCHANGE:
4564 return _OnSettingChange((UINT)wParam);
4565
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004566#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 case WM_NOTIFY:
4568 switch (((LPNMHDR) lParam)->code)
4569 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004570 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004571 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004573 LPNMHDR hdr = (LPNMHDR)lParam;
4574 char_u *str = NULL;
4575 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004576
Bram Moolenaard23a8232018-02-10 18:45:26 +01004577 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004578
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004579# ifdef FEAT_GUI_TABLINE
4580 if (gui_mch_showing_tabline()
4581 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004582 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004583 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004584 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004585 * Mouse is over the GUI tabline. Display the
4586 * tooltip for the tab under the cursor
4587 *
4588 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004589 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004590 GetCursorPos(&pt);
4591 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004592 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004593 TCHITTESTINFO htinfo;
4594 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004595
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004596 /*
4597 * Get the tab under the cursor
4598 */
4599 htinfo.pt.x = pt.x;
4600 htinfo.pt.y = pt.y;
4601 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4602 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004603 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004604 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004605
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004606 tp = find_tabpage(idx + 1);
4607 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004608 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004609 get_tabline_label(tp, TRUE);
4610 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004611 }
4612 }
4613 }
4614 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004615# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004616# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004617# ifdef FEAT_GUI_TABLINE
4618 else
4619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004621 UINT idButton;
4622 vimmenu_T *pMenu;
4623
4624 idButton = (UINT) hdr->idFrom;
4625 pMenu = gui_mswin_find_menu(root_menu, idButton);
4626 if (pMenu)
4627 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004629# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004630 if (str != NULL)
4631 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004632 if (hdr->code == TTN_GETDISPINFOW)
4633 {
4634 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4635
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004636 /* Set the maximum width, this also enables using
4637 * \n for line break. */
4638 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4639 0, 500);
4640
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004641 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004642 lpdi->lpszText = tt_text;
4643 /* can't show tooltip if failed */
4644 }
4645 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004646 {
4647 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4648
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004649 /* Set the maximum width, this also enables using
4650 * \n for line break. */
4651 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4652 0, 500);
4653
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004654 if (STRLEN(str) < sizeof(lpdi->szText)
4655 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004656 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004657 sizeof(lpdi->szText) - 1);
4658 else
4659 lpdi->lpszText = tt_text;
4660 }
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
4663 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004664# ifdef FEAT_GUI_TABLINE
4665 case TCN_SELCHANGE:
4666 if (gui_mch_showing_tabline()
4667 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004668 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004669 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004670 return 0L;
4671 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004672 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004673
4674 case NM_RCLICK:
4675 if (gui_mch_showing_tabline()
4676 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004677 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004678 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004679 return 0L;
4680 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004681 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004684# ifdef FEAT_GUI_TABLINE
4685 if (gui_mch_showing_tabline()
4686 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4687 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 break;
4690 }
4691 break;
4692#endif
4693#if defined(MENUHINTS) && defined(FEAT_MENU)
4694 case WM_MENUSELECT:
4695 if (((UINT) HIWORD(wParam)
4696 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4697 == MF_HILITE
4698 && (State & CMDLINE) == 0)
4699 {
4700 UINT idButton;
4701 vimmenu_T *pMenu;
4702 static int did_menu_tip = FALSE;
4703
4704 if (did_menu_tip)
4705 {
4706 msg_clr_cmdline();
4707 setcursor();
4708 out_flush();
4709 did_menu_tip = FALSE;
4710 }
4711
4712 idButton = (UINT)LOWORD(wParam);
4713 pMenu = gui_mswin_find_menu(root_menu, idButton);
4714 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4715 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4716 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004717 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004718 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004719 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 setcursor();
4721 out_flush();
4722 did_menu_tip = TRUE;
4723 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004724 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726 break;
4727#endif
4728 case WM_NCHITTEST:
4729 {
4730 LRESULT result;
4731 int x, y;
4732 int xPos = GET_X_LPARAM(lParam);
4733
4734 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4735 if (result == HTCLIENT)
4736 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004737#ifdef FEAT_GUI_TABLINE
4738 if (gui_mch_showing_tabline())
4739 {
4740 int yPos = GET_Y_LPARAM(lParam);
4741 RECT rct;
4742
4743 /* If the cursor is on the GUI tabline, don't process this
4744 * event */
4745 GetWindowRect(s_textArea, &rct);
4746 if (yPos < rct.top)
4747 return result;
4748 }
4749#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004750 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 xPos -= x;
4752
4753 if (xPos < 48) /* <VN> TODO should use system metric? */
4754 return HTBOTTOMLEFT;
4755 else
4756 return HTBOTTOMRIGHT;
4757 }
4758 else
4759 return result;
4760 }
4761 /* break; notreached */
4762
4763#ifdef FEAT_MBYTE_IME
4764 case WM_IME_NOTIFY:
4765 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4766 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004767 return 1L;
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 case WM_IME_COMPOSITION:
4770 if (!_OnImeComposition(hwnd, wParam, lParam))
4771 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004772 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#endif
4774
4775 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004777 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779#endif
4780 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4781 }
4782
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004783 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784}
4785
4786/*
4787 * End of call-back routines
4788 */
4789
4790/* parent window, if specified with -P */
4791HWND vim_parent_hwnd = NULL;
4792
4793 static BOOL CALLBACK
4794FindWindowTitle(HWND hwnd, LPARAM lParam)
4795{
4796 char buf[2048];
4797 char *title = (char *)lParam;
4798
4799 if (GetWindowText(hwnd, buf, sizeof(buf)))
4800 {
4801 if (strstr(buf, title) != NULL)
4802 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004803 /* Found it. Store the window ref. and quit searching if MDI
4804 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004806 if (vim_parent_hwnd != NULL)
4807 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 }
4810 return TRUE; /* continue searching */
4811}
4812
4813/*
4814 * Invoked for '-P "title"' argument: search for parent application to open
4815 * our window in.
4816 */
4817 void
4818gui_mch_set_parent(char *title)
4819{
4820 EnumWindows(FindWindowTitle, (LPARAM)title);
4821 if (vim_parent_hwnd == NULL)
4822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004823 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 mch_exit(2);
4825 }
4826}
4827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004828#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 static void
4830ole_error(char *arg)
4831{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004832 char buf[IOSIZE];
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
4842/*
4843 * Parse the GUI related command-line arguments. Any arguments used are
4844 * deleted from argv, and *argc is decremented accordingly. This is called
4845 * when vim is started, whether or not the GUI has been started.
4846 */
4847 void
4848gui_mch_prepare(int *argc, char **argv)
4849{
4850 int silent = FALSE;
4851 int idx;
4852
4853 /* Check for special OLE command line parameters */
4854 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4855 {
4856 /* Check for a "-silent" argument first. */
4857 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4858 && (argv[2][0] == '-' || argv[2][0] == '/'))
4859 {
4860 silent = TRUE;
4861 idx = 2;
4862 }
4863 else
4864 idx = 1;
4865
4866 /* Register Vim as an OLE Automation server */
4867 if (STRICMP(argv[idx] + 1, "register") == 0)
4868 {
4869#ifdef FEAT_OLE
4870 RegisterMe(silent);
4871 mch_exit(0);
4872#else
4873 if (!silent)
4874 ole_error("register");
4875 mch_exit(2);
4876#endif
4877 }
4878
4879 /* Unregister Vim as an OLE Automation server */
4880 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4881 {
4882#ifdef FEAT_OLE
4883 UnregisterMe(!silent);
4884 mch_exit(0);
4885#else
4886 if (!silent)
4887 ole_error("unregister");
4888 mch_exit(2);
4889#endif
4890 }
4891
4892 /* Ignore an -embedding argument. It is only relevant if the
4893 * application wants to treat the case when it is started manually
4894 * differently from the case where it is started via automation (and
4895 * we don't).
4896 */
4897 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4898 {
4899#ifdef FEAT_OLE
4900 *argc = 1;
4901#else
4902 ole_error("embedding");
4903 mch_exit(2);
4904#endif
4905 }
4906 }
4907
4908#ifdef FEAT_OLE
4909 {
4910 int bDoRestart = FALSE;
4911
4912 InitOLE(&bDoRestart);
4913 /* automatically exit after registering */
4914 if (bDoRestart)
4915 mch_exit(0);
4916 }
4917#endif
4918
4919#ifdef FEAT_NETBEANS_INTG
4920 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004921 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int arg;
4923
4924 for (arg = 1; arg < *argc; arg++)
4925 if (strncmp("-nb", argv[arg], 3) == 0)
4926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 netbeansArg = argv[arg];
4928 mch_memmove(&argv[arg], &argv[arg + 1],
4929 (--*argc - arg) * sizeof(char *));
4930 argv[*argc] = NULL;
4931 break; /* enough? */
4932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * Initialise the GUI. Create all the windows, set up all the call-backs
4939 * etc.
4940 */
4941 int
4942gui_mch_init(void)
4943{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01004945 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#ifdef GLOBAL_IME
4948 ATOM atom;
4949#endif
4950
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 /* Return here if the window was already opened (happens when
4952 * gui_mch_dialog() is called early). */
4953 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004954 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955
4956 /*
4957 * Load the tearoff bitmap
4958 */
4959#ifdef FEAT_TEAROFF
4960 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
4961#endif
4962
4963 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
4964 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
4965#ifdef FEAT_MENU
4966 gui.menu_height = 0; /* Windows takes care of this */
4967#endif
4968 gui.border_width = 0;
4969
4970 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
4971
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* First try using the wide version, so that we can use any title.
4973 * Otherwise only characters in the active codepage will work. */
4974 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
4975 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004976 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 wndclassw.lpfnWndProc = _WndProc;
4978 wndclassw.cbClsExtra = 0;
4979 wndclassw.cbWndExtra = 0;
4980 wndclassw.hInstance = s_hinst;
4981 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
4982 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
4983 wndclassw.hbrBackground = s_brush;
4984 wndclassw.lpszMenuName = NULL;
4985 wndclassw.lpszClassName = szVimWndClassW;
4986
4987 if ((
4988#ifdef GLOBAL_IME
4989 atom =
4990#endif
4991 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004992 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (vim_parent_hwnd != NULL)
4996 {
4997#ifdef HAVE_TRY_EXCEPT
4998 __try
4999 {
5000#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005001 // Open inside the specified parent window.
5002 // TODO: last argument should point to a CLIENTCREATESTRUCT
5003 // structure.
5004 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005006 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005007 WS_OVERLAPPEDWINDOW | WS_CHILD
5008 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5010 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005011 100, // Any value will do
5012 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 vim_parent_hwnd, NULL,
5014 s_hinst, NULL);
5015#ifdef HAVE_TRY_EXCEPT
5016 }
5017 __except(EXCEPTION_EXECUTE_HANDLER)
5018 {
5019 /* NOP */
5020 }
5021#endif
5022 if (s_hwnd == NULL)
5023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005024 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 mch_exit(2);
5026 }
5027 }
5028 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005029 {
5030 /* If the provided windowid is not valid reset it to zero, so that it
5031 * is ignored and we open our own window. */
5032 if (IsWindow((HWND)win_socket_id) <= 0)
5033 win_socket_id = 0;
5034
5035 /* Create a window. If win_socket_id is not zero without border and
5036 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005037 s_hwnd = CreateWindowW(
5038 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005039 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5040 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005041 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5042 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5043 100, /* Any value will do */
5044 100, /* Any value will do */
5045 NULL, NULL,
5046 s_hinst, NULL);
5047 if (s_hwnd != NULL && win_socket_id != 0)
5048 {
5049 SetParent(s_hwnd, (HWND)win_socket_id);
5050 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5051 }
5052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053
5054 if (s_hwnd == NULL)
5055 return FAIL;
5056
5057#ifdef GLOBAL_IME
5058 global_ime_init(atom, s_hwnd);
5059#endif
5060#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5061 dyn_imm_load();
5062#endif
5063
5064 /* Create the text area window */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005065 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005066 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005067 wndclassw.style = CS_OWNDC;
5068 wndclassw.lpfnWndProc = _TextAreaWndProc;
5069 wndclassw.cbClsExtra = 0;
5070 wndclassw.cbWndExtra = 0;
5071 wndclassw.hInstance = s_hinst;
5072 wndclassw.hIcon = NULL;
5073 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5074 wndclassw.hbrBackground = NULL;
5075 wndclassw.lpszMenuName = NULL;
5076 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005077
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005078 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 return FAIL;
5080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005082 s_textArea = CreateWindowExW(
5083 0,
5084 szTextAreaClassW, L"Vim text area",
5085 WS_CHILD | WS_VISIBLE, 0, 0,
5086 100, // Any value will do for now
5087 100, // Any value will do for now
5088 s_hwnd, NULL,
5089 s_hinst, NULL);
5090
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 if (s_textArea == NULL)
5092 return FAIL;
5093
Bram Moolenaar20321902016-02-17 12:30:17 +01005094#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005095 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5096 {
5097 HANDLE hIcon = NULL;
5098
5099 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005100 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005101 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005102#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#ifdef FEAT_MENU
5105 s_menuBar = CreateMenu();
5106#endif
5107 s_hdc = GetDC(s_textArea);
5108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
5111 /* Do we need to bother with this? */
5112 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5113
5114 /* Get background/foreground colors from the system */
5115 gui_mch_def_colors();
5116
5117 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5118 * file) */
5119 set_normal_colors();
5120
5121 /*
5122 * Check that none of the colors are the same as the background color.
5123 * Then store the current values as the defaults.
5124 */
5125 gui_check_colors();
5126 gui.def_norm_pixel = gui.norm_pixel;
5127 gui.def_back_pixel = gui.back_pixel;
5128
5129 /* Get the colors for the highlight groups (gui_check_colors() might have
5130 * changed them) */
5131 highlight_gui_started();
5132
5133 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005134 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005136 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137
5138 /*
5139 * Set up for Intellimouse processing
5140 */
5141 init_mouse_wheel();
5142
5143 /*
5144 * compute a couple of metrics used for the dialogs
5145 */
5146 get_dialog_font_metrics();
5147#ifdef FEAT_TOOLBAR
5148 /*
5149 * Create the toolbar
5150 */
5151 initialise_toolbar();
5152#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005153#ifdef FEAT_GUI_TABLINE
5154 /*
5155 * Create the tabline
5156 */
5157 initialise_tabline();
5158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#ifdef MSWIN_FIND_REPLACE
5160 /*
5161 * Initialise the dialog box stuff
5162 */
5163 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5164
5165 /* Initialise the struct */
5166 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005167 s_findrep_struct.lpstrFindWhat =
5168 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005170 s_findrep_struct.lpstrReplaceWith =
5171 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5173 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5174 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005177#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005178# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5179/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5180# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005181# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005182# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005183# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005184 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005185 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005186#endif
5187
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005188#ifdef FEAT_RENDER_OPTIONS
5189 if (p_rop)
5190 (void)gui_mch_set_rendering_options(p_rop);
5191#endif
5192
Bram Moolenaar748bf032005-02-02 23:04:36 +00005193theend:
5194 /* Display any pending error messages */
5195 display_errors();
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 return OK;
5198}
5199
5200/*
5201 * Get the size of the screen, taking position on multiple monitors into
5202 * account (if supported).
5203 */
5204 static void
5205get_work_area(RECT *spi_rect)
5206{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005207 HMONITOR mon;
5208 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005210 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005211 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005212 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005214 moninfo.cbSize = sizeof(MONITORINFO);
5215 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005217 *spi_rect = moninfo.rcWork;
5218 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 }
5221 /* this is the old method... */
5222 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5223}
5224
5225/*
5226 * Set the size of the window to the given width and height in pixels.
5227 */
5228 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005229gui_mch_set_shellsize(
5230 int width,
5231 int height,
5232 int min_width UNUSED,
5233 int min_height UNUSED,
5234 int base_width UNUSED,
5235 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005236 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 RECT workarea_rect;
5239 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 WINDOWPLACEMENT wndpl;
5241
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005242 /* Try to keep window completely on screen. */
5243 /* Get position of the screen work area. This is the part that is not
5244 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 get_work_area(&workarea_rect);
5246
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005247 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005248 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 wndpl.length = sizeof(WINDOWPLACEMENT);
5250 GetWindowPlacement(s_hwnd, &wndpl);
5251
5252 /* Resizing a maximized window looks very strange, unzoom it first.
5253 * But don't do it when still starting up, it may have been requested in
5254 * the shortcut. */
5255 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5256 {
5257 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5258 /* Need to get the settings of the normal window. */
5259 GetWindowPlacement(s_hwnd, &wndpl);
5260 }
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005263 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005264 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005265 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005266 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 + GetSystemMetrics(SM_CYCAPTION)
5268#ifdef FEAT_MENU
5269 + gui_mswin_get_menu_height(FALSE)
5270#endif
5271 ;
5272
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005273 /* The following should take care of keeping Vim on the same monitor, no
5274 * matter if the secondary monitor is left or right of the primary
5275 * monitor. */
5276 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5277 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005278
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005279 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005280 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005281 && wndpl.rcNormalPosition.right > workarea_rect.right)
5282 OffsetRect(&wndpl.rcNormalPosition,
5283 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005285 if ((direction & RESIZE_HOR)
5286 && wndpl.rcNormalPosition.left < workarea_rect.left)
5287 OffsetRect(&wndpl.rcNormalPosition,
5288 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaarafa24992006-03-27 20:58:26 +00005290 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005291 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5292 OffsetRect(&wndpl.rcNormalPosition,
5293 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005295 if ((direction & RESIZE_VERT)
5296 && wndpl.rcNormalPosition.top < workarea_rect.top)
5297 OffsetRect(&wndpl.rcNormalPosition,
5298 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 /* set window position - we should use SetWindowPlacement rather than
5301 * SetWindowPos as the MSDN docs say the coord systems returned by
5302 * these two are not compatible. */
5303 SetWindowPlacement(s_hwnd, &wndpl);
5304
5305 SetActiveWindow(s_hwnd);
5306 SetFocus(s_hwnd);
5307
5308#ifdef FEAT_MENU
5309 /* Menu may wrap differently now */
5310 gui_mswin_get_menu_height(!gui.starting);
5311#endif
5312}
5313
5314
5315 void
5316gui_mch_set_scrollbar_thumb(
5317 scrollbar_T *sb,
5318 long val,
5319 long size,
5320 long max)
5321{
5322 SCROLLINFO info;
5323
5324 sb->scroll_shift = 0;
5325 while (max > 32767)
5326 {
5327 max = (max + 1) >> 1;
5328 val >>= 1;
5329 size >>= 1;
5330 ++sb->scroll_shift;
5331 }
5332
5333 if (sb->scroll_shift > 0)
5334 ++size;
5335
5336 info.cbSize = sizeof(info);
5337 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5338 info.nPos = val;
5339 info.nMin = 0;
5340 info.nMax = max;
5341 info.nPage = size;
5342 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5343}
5344
5345
5346/*
5347 * Set the current text font.
5348 */
5349 void
5350gui_mch_set_font(GuiFont font)
5351{
5352 gui.currFont = font;
5353}
5354
5355
5356/*
5357 * Set the current text foreground color.
5358 */
5359 void
5360gui_mch_set_fg_color(guicolor_T color)
5361{
5362 gui.currFgColor = color;
5363}
5364
5365/*
5366 * Set the current text background color.
5367 */
5368 void
5369gui_mch_set_bg_color(guicolor_T color)
5370{
5371 gui.currBgColor = color;
5372}
5373
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005374/*
5375 * Set the current text special color.
5376 */
5377 void
5378gui_mch_set_sp_color(guicolor_T color)
5379{
5380 gui.currSpColor = color;
5381}
5382
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005383#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384/*
5385 * Multi-byte handling, originally by Sung-Hoon Baek.
5386 * First static functions (no prototypes generated).
5387 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005388# ifdef _MSC_VER
5389# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5390# endif
5391# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 * handle WM_IME_NOTIFY message
5395 */
5396 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005397_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 LRESULT lResult = 0;
5400 HIMC hImc;
5401
5402 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5403 return lResult;
5404 switch (dwCommand)
5405 {
5406 case IMN_SETOPENSTATUS:
5407 if (pImmGetOpenStatus(hImc))
5408 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005409 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 im_set_position(gui.row, gui.col);
5411
5412 /* Disable langmap */
5413 State &= ~LANGMAP;
5414 if (State & INSERT)
5415 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005416#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 /* Unshown 'keymap' in status lines */
5418 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5419 {
5420 /* Save cursor position */
5421 int old_row = gui.row;
5422 int old_col = gui.col;
5423
5424 // This must be called here before
5425 // status_redraw_curbuf(), otherwise the mode
5426 // message may appear in the wrong position.
5427 showmode();
5428 status_redraw_curbuf();
5429 update_screen(0);
5430 /* Restore cursor position */
5431 gui.row = old_row;
5432 gui.col = old_col;
5433 }
5434#endif
5435 }
5436 }
5437 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005438 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 lResult = 0;
5440 break;
5441 }
5442 pImmReleaseContext(hWnd, hImc);
5443 return lResult;
5444}
5445
5446 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005447_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 char_u *ret;
5450 int len;
5451
5452 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5453 return 0;
5454
5455 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5456 if (ret != NULL)
5457 {
5458 add_to_input_buf_csi(ret, len);
5459 vim_free(ret);
5460 return 1;
5461 }
5462 return 0;
5463}
5464
5465/*
5466 * get the current composition string, in UCS-2; *lenp is the number of
5467 * *lenp is the number of Unicode characters.
5468 */
5469 static short_u *
5470GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5471{
5472 LONG ret;
5473 LPWSTR wbuf = NULL;
5474 char_u *buf;
5475
5476 if (!pImmGetContext)
5477 return NULL; /* no imm32.dll */
5478
5479 /* Try Unicode; this'll always work on NT regardless of codepage. */
5480 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5481 if (ret == 0)
5482 return NULL; /* empty */
5483
5484 if (ret > 0)
5485 {
5486 /* Allocate the requested buffer plus space for the NUL character. */
5487 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5488 if (wbuf != NULL)
5489 {
5490 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5491 *lenp = ret / sizeof(WCHAR);
5492 }
5493 return (short_u *)wbuf;
5494 }
5495
5496 /* ret < 0; we got an error, so try the ANSI version. This'll work
5497 * on 9x/ME, but only if the codepage happens to be set to whatever
5498 * we're inputting. */
5499 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5500 if (ret <= 0)
5501 return NULL; /* empty or error */
5502
5503 buf = alloc(ret);
5504 if (buf == NULL)
5505 return NULL;
5506 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5507
5508 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005509 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 vim_free(buf);
5511
5512 return (short_u *)wbuf;
5513}
5514
5515/*
5516 * void GetResultStr()
5517 *
5518 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5519 * get complete composition string
5520 */
5521 static char_u *
5522GetResultStr(HWND hwnd, int GCS, int *lenp)
5523{
5524 HIMC hIMC; /* Input context handle. */
5525 short_u *buf = NULL;
5526 char_u *convbuf = NULL;
5527
5528 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5529 return NULL;
5530
5531 /* Reads in the composition string. */
5532 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5533 if (buf == NULL)
5534 return NULL;
5535
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005536 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 pImmReleaseContext(hwnd, hIMC);
5538 vim_free(buf);
5539 return convbuf;
5540}
5541#endif
5542
5543/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005544#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546/*
5547 * set font to IM.
5548 */
5549 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005550im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 HIMC hImc;
5553
5554 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5555 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005556 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 pImmReleaseContext(s_hwnd, hImc);
5558 }
5559}
5560
5561/*
5562 * Notify cursor position to IM.
5563 */
5564 void
5565im_set_position(int row, int col)
5566{
5567 HIMC hImc;
5568
5569 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5570 {
5571 COMPOSITIONFORM cfs;
5572
5573 cfs.dwStyle = CFS_POINT;
5574 cfs.ptCurrentPos.x = FILL_X(col);
5575 cfs.ptCurrentPos.y = FILL_Y(row);
5576 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5577 pImmSetCompositionWindow(hImc, &cfs);
5578
5579 pImmReleaseContext(s_hwnd, hImc);
5580 }
5581}
5582
5583/*
5584 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5585 */
5586 void
5587im_set_active(int active)
5588{
5589 HIMC hImc;
5590 static HIMC hImcOld = (HIMC)0;
5591
5592 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5593 {
5594 if (p_imdisable)
5595 {
5596 if (hImcOld == (HIMC)0)
5597 {
5598 hImcOld = pImmGetContext(s_hwnd);
5599 if (hImcOld)
5600 pImmAssociateContext(s_hwnd, (HIMC)0);
5601 }
5602 active = FALSE;
5603 }
5604 else if (hImcOld != (HIMC)0)
5605 {
5606 pImmAssociateContext(s_hwnd, hImcOld);
5607 hImcOld = (HIMC)0;
5608 }
5609
5610 hImc = pImmGetContext(s_hwnd);
5611 if (hImc)
5612 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005613 /*
5614 * for Korean ime
5615 */
5616 HKL hKL = GetKeyboardLayout(0);
5617
5618 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5619 {
5620 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5621 static BOOL bSaved = FALSE;
5622
5623 if (active)
5624 {
5625 /* if we have a saved conversion status, restore it */
5626 if (bSaved)
5627 pImmSetConversionStatus(hImc, dwConversionSaved,
5628 dwSentenceSaved);
5629 bSaved = FALSE;
5630 }
5631 else
5632 {
5633 /* save conversion status and disable korean */
5634 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5635 &dwSentenceSaved))
5636 {
5637 bSaved = TRUE;
5638 pImmSetConversionStatus(hImc,
5639 dwConversionSaved & ~(IME_CMODE_NATIVE
5640 | IME_CMODE_FULLSHAPE),
5641 dwSentenceSaved);
5642 }
5643 }
5644 }
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 pImmSetOpenStatus(hImc, active);
5647 pImmReleaseContext(s_hwnd, hImc);
5648 }
5649 }
5650}
5651
5652/*
5653 * Get IM status. When IM is on, return not 0. Else return 0.
5654 */
5655 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005656im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 int status = 0;
5659 HIMC hImc;
5660
5661 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5662 {
5663 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5664 pImmReleaseContext(s_hwnd, hImc);
5665 }
5666 return status;
5667}
5668
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005669#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005671#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672/* Win32 with GLOBAL IME */
5673
5674/*
5675 * Notify cursor position to IM.
5676 */
5677 void
5678im_set_position(int row, int col)
5679{
5680 /* Win32 with GLOBAL IME */
5681 POINT p;
5682
5683 p.x = FILL_X(col);
5684 p.y = FILL_Y(row);
5685 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5686 global_ime_set_position(&p);
5687}
5688
5689/*
5690 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5691 */
5692 void
5693im_set_active(int active)
5694{
5695 global_ime_set_status(active);
5696}
5697
5698/*
5699 * Get IM status. When IM is on, return not 0. Else return 0.
5700 */
5701 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005702im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
5704 return global_ime_get_status();
5705}
5706#endif
5707
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005708/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005709 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005710 */
5711 static void
5712latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5713{
5714 int c;
5715
Bram Moolenaarca003e12006-03-17 23:19:38 +00005716 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005717 {
5718 c = *text++;
5719 switch (c)
5720 {
5721 case 0xa4: c = 0x20ac; break; /* euro */
5722 case 0xa6: c = 0x0160; break; /* S hat */
5723 case 0xa8: c = 0x0161; break; /* S -hat */
5724 case 0xb4: c = 0x017d; break; /* Z hat */
5725 case 0xb8: c = 0x017e; break; /* Z -hat */
5726 case 0xbc: c = 0x0152; break; /* OE */
5727 case 0xbd: c = 0x0153; break; /* oe */
5728 case 0xbe: c = 0x0178; break; /* Y */
5729 }
5730 *unicodebuf++ = c;
5731 }
5732}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734#ifdef FEAT_RIGHTLEFT
5735/*
5736 * What is this for? In the case where you are using Win98 or Win2K or later,
5737 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5738 * reverses the string sent to the TextOut... family. This sucks, because we
5739 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5740 * way to tell Windblows not to do this!
5741 *
5742 * The short of it is that this 'RevOut' only gets called if you are running
5743 * one of the new, "improved" MS OSes, and only if you are running in
5744 * 'rightleft' mode. It makes display take *slightly* longer, but not
5745 * noticeably so.
5746 */
5747 static void
5748RevOut( HDC s_hdc,
5749 int col,
5750 int row,
5751 UINT foptions,
5752 CONST RECT *pcliprect,
5753 LPCTSTR text,
5754 UINT len,
5755 CONST INT *padding)
5756{
5757 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005759 for (ix = 0; ix < (int)len; ++ix)
5760 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5761 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762}
5763#endif
5764
Bram Moolenaar92467d32017-12-05 13:22:16 +01005765 static void
5766draw_line(
5767 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005768 int y1,
5769 int x2,
5770 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005771 COLORREF color)
5772{
5773#if defined(FEAT_DIRECTX)
5774 if (IS_ENABLE_DIRECTX())
5775 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5776 else
5777#endif
5778 {
5779 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5780 HPEN old_pen = SelectObject(s_hdc, hpen);
5781 MoveToEx(s_hdc, x1, y1, NULL);
5782 /* Note: LineTo() excludes the last pixel in the line. */
5783 LineTo(s_hdc, x2, y2);
5784 DeleteObject(SelectObject(s_hdc, old_pen));
5785 }
5786}
5787
5788 static void
5789set_pixel(
5790 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005791 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005792 COLORREF color)
5793{
5794#if defined(FEAT_DIRECTX)
5795 if (IS_ENABLE_DIRECTX())
5796 DWriteContext_SetPixel(s_dwc, x, y, color);
5797 else
5798#endif
5799 SetPixel(s_hdc, x, y, color);
5800}
5801
5802 static void
5803fill_rect(
5804 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005805 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005806 COLORREF color)
5807{
5808#if defined(FEAT_DIRECTX)
5809 if (IS_ENABLE_DIRECTX())
5810 DWriteContext_FillRect(s_dwc, rcp, color);
5811 else
5812#endif
5813 {
5814 HBRUSH hbr2;
5815
5816 if (hbr == NULL)
5817 hbr2 = CreateSolidBrush(color);
5818 else
5819 hbr2 = hbr;
5820 FillRect(s_hdc, rcp, hbr2);
5821 if (hbr == NULL)
5822 DeleteBrush(hbr2);
5823 }
5824}
5825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 void
5827gui_mch_draw_string(
5828 int row,
5829 int col,
5830 char_u *text,
5831 int len,
5832 int flags)
5833{
5834 static int *padding = NULL;
5835 static int pad_size = 0;
5836 int i;
5837 const RECT *pcliprect = NULL;
5838 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 static WCHAR *unicodebuf = NULL;
5840 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005841 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 int y;
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 /*
5846 * Italic and bold text seems to have an extra row of pixels at the bottom
5847 * (below where the bottom of the character should be). If we draw the
5848 * characters with a solid background, the top row of pixels in the
5849 * character below will be overwritten. We can fix this by filling in the
5850 * background ourselves, to the correct character proportions, and then
5851 * writing the character in transparent mode. Still have a problem when
5852 * the character is "_", which gets written on to the character below.
5853 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5854 * pixel in their slots, which fixes the problem with the bottom row of
5855 * pixels. We still need this code because otherwise the top row of pixels
5856 * becomes a problem. - webb.
5857 */
5858 static HBRUSH hbr_cache[2] = {NULL, NULL};
5859 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5860 static int brush_lru = 0;
5861 HBRUSH hbr;
5862 RECT rc;
5863
5864 if (!(flags & DRAW_TRANSP))
5865 {
5866 /*
5867 * Clear background first.
5868 * Note: FillRect() excludes right and bottom of rectangle.
5869 */
5870 rc.left = FILL_X(col);
5871 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 if (has_mbyte)
5873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02005875 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 rc.right = FILL_X(col + len);
5879 rc.bottom = FILL_Y(row + 1);
5880
5881 /* Cache the created brush, that saves a lot of time. We need two:
5882 * one for cursor background and one for the normal background. */
5883 if (gui.currBgColor == brush_color[0])
5884 {
5885 hbr = hbr_cache[0];
5886 brush_lru = 1;
5887 }
5888 else if (gui.currBgColor == brush_color[1])
5889 {
5890 hbr = hbr_cache[1];
5891 brush_lru = 0;
5892 }
5893 else
5894 {
5895 if (hbr_cache[brush_lru] != NULL)
5896 DeleteBrush(hbr_cache[brush_lru]);
5897 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5898 brush_color[brush_lru] = gui.currBgColor;
5899 hbr = hbr_cache[brush_lru];
5900 brush_lru = !brush_lru;
5901 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005902
Bram Moolenaar92467d32017-12-05 13:22:16 +01005903 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
5905 SetBkMode(s_hdc, TRANSPARENT);
5906
5907 /*
5908 * When drawing block cursor, prevent inverted character spilling
5909 * over character cell (can happen with bold/italic)
5910 */
5911 if (flags & DRAW_CURSOR)
5912 {
5913 pcliprect = &rc;
5914 foptions = ETO_CLIPPED;
5915 }
5916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 SetTextColor(s_hdc, gui.currFgColor);
5918 SelectFont(s_hdc, gui.currFont);
5919
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005920#ifdef FEAT_DIRECTX
5921 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005922 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005923#endif
5924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5926 {
5927 vim_free(padding);
5928 pad_size = Columns;
5929
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005930 /* Don't give an out-of-memory message here, it would call us
5931 * recursively. */
5932 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 if (padding != NULL)
5934 for (i = 0; i < pad_size; i++)
5935 padding[i] = gui.char_width;
5936 }
5937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 /*
5939 * We have to provide the padding argument because italic and bold versions
5940 * of fixed-width fonts are often one pixel or so wider than their normal
5941 * versions.
5942 * No check for DRAW_BOLD, Windows will have done it already.
5943 */
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 /* Check if there are any UTF-8 characters. If not, use normal text
5946 * output to speed up output. */
5947 if (enc_utf8)
5948 for (n = 0; n < len; ++n)
5949 if (text[n] >= 0x80)
5950 break;
5951
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005952#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005953 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5954 * required that unicode drawing routine, currently. So this forces it
5955 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005956 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005957 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005958#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005961 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005963 if ((enc_utf8
5964 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
5965 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 && (unicodebuf == NULL || len > unibuflen))
5967 {
5968 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005969 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
5971 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005972 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
5974 unibuflen = len;
5975 }
5976
5977 if (enc_utf8 && n < len && unicodebuf != NULL)
5978 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02005979 /* Output UTF-8 characters. Composing characters should be
5980 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005981 int i;
5982 int wlen; /* string length in words */
5983 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 int cells; /* cell width of string up to composing char */
5985 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005986 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005988 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005989 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005991 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005993 c = utf_ptr2char(text + i);
5994 if (c >= 0x10000)
5995 {
5996 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005997 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
5998 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005999 }
6000 else
6001 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006002 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006003 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006004
6005 if (utf_iscomposing(c))
6006 cw = 0;
6007 else
6008 {
6009 cw = utf_char2cells(c);
6010 if (cw > 2) /* don't use 4 for unprintable char */
6011 cw = 1;
6012 }
6013
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (unicodepdy != NULL)
6015 {
6016 /* Use unicodepdy to make characters fit as we expect, even
6017 * when the font uses different widths (e.g., bold character
6018 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006019 if (c >= 0x10000)
6020 {
6021 unicodepdy[wlen - 2] = cw * gui.char_width;
6022 unicodepdy[wlen - 1] = 0;
6023 }
6024 else
6025 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
6027 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006028 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006029 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006031#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006032 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006033 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006034 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006035 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006036 TEXT_X(col), TEXT_Y(row),
6037 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006038 gui.char_width, gui.currFgColor,
6039 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006040 }
6041 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006043 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6044 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 len = cells; /* used for underlining */
6046 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006047 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
6049 /* If we want to display codepage data, and the current CP is not the
6050 * ANSI one, we need to go via Unicode. */
6051 if (unicodebuf != NULL)
6052 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006053 if (enc_latin9)
6054 latin9_to_ucs(text, len, unicodebuf);
6055 else
6056 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 MB_PRECOMPOSED,
6058 (char *)text, len,
6059 (LPWSTR)unicodebuf, unibuflen);
6060 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006061 {
6062 /* Use unicodepdy to make characters fit as we expect, even
6063 * when the font uses different widths (e.g., bold character
6064 * is wider). */
6065 if (unicodepdy != NULL)
6066 {
6067 int i;
6068 int cw;
6069
6070 for (i = 0; i < len; ++i)
6071 {
6072 cw = utf_char2cells(unicodebuf[i]);
6073 if (cw > 2)
6074 cw = 1;
6075 unicodepdy[i] = cw * gui.char_width;
6076 }
6077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006079 foptions, pcliprect, unicodebuf, len, unicodepdy);
6080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
6082 }
6083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {
6085#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006086 /* Windows will mess up RL text, so we have to draw it character by
6087 * character. Only do this if RL is on, since it's slow. */
6088 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6090 foptions, pcliprect, (char *)text, len, padding);
6091 else
6092#endif
6093 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6094 foptions, pcliprect, (char *)text, len, padding);
6095 }
6096
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006097 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (flags & DRAW_UNDERL)
6099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* When p_linespace is 0, overwrite the bottom row of pixels.
6101 * Otherwise put the line just below the character. */
6102 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (p_linespace > 1)
6104 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006105 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006107
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006108 /* Strikethrough */
6109 if (flags & DRAW_STRIKE)
6110 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006111 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006112 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006113 }
6114
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006115 /* Undercurl */
6116 if (flags & DRAW_UNDERC)
6117 {
6118 int x;
6119 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006120 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006121
6122 y = FILL_Y(row + 1) - 1;
6123 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6124 {
6125 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006126 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006127 }
6128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129}
6130
6131
6132/*
6133 * Output routines.
6134 */
6135
6136/* Flush any output to the screen */
6137 void
6138gui_mch_flush(void)
6139{
6140# if defined(__BORLANDC__)
6141 /*
6142 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6143 * prototype declaration.
6144 * The compiler complains if __stdcall is not used in both declarations.
6145 */
6146 BOOL __stdcall GdiFlush(void);
6147# endif
6148
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006149#if defined(FEAT_DIRECTX)
6150 if (IS_ENABLE_DIRECTX())
6151 DWriteContext_Flush(s_dwc);
6152#endif
6153
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 GdiFlush();
6155}
6156
6157 static void
6158clear_rect(RECT *rcp)
6159{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006160 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161}
6162
6163
Bram Moolenaarc716c302006-01-21 22:12:51 +00006164 void
6165gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6166{
6167 RECT workarea_rect;
6168
6169 get_work_area(&workarea_rect);
6170
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006171 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006172 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006173 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006174
6175 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6176 * the menubar for MSwin, we subtract it from the screen height, so that
6177 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006178 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006179 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006180 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006181 - GetSystemMetrics(SM_CYCAPTION)
6182#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006183 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006184#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006185 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006186}
6187
6188
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189#if defined(FEAT_MENU) || defined(PROTO)
6190/*
6191 * Add a sub menu to the menu bar.
6192 */
6193 void
6194gui_mch_add_menu(
6195 vimmenu_T *menu,
6196 int pos)
6197{
6198 vimmenu_T *parent = menu->parent;
6199
6200 menu->submenu_id = CreatePopupMenu();
6201 menu->id = s_menu_id++;
6202
6203 if (menu_is_menubar(menu->name))
6204 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006205 WCHAR *wn;
6206 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006208 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006209 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006210 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006212 infow.cbSize = sizeof(infow);
6213 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6214 | MIIM_SUBMENU;
6215 infow.dwItemData = (long_u)menu;
6216 infow.wID = menu->id;
6217 infow.fType = MFT_STRING;
6218 infow.dwTypeData = wn;
6219 infow.cch = (UINT)wcslen(wn);
6220 infow.hSubMenu = menu->submenu_id;
6221 InsertMenuItemW((parent == NULL)
6222 ? s_menuBar : parent->submenu_id,
6223 (UINT)pos, TRUE, &infow);
6224 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 }
6226
6227 /* Fix window size if menu may have wrapped */
6228 if (parent == NULL)
6229 gui_mswin_get_menu_height(!gui.starting);
6230#ifdef FEAT_TEAROFF
6231 else if (IsWindow(parent->tearoff_handle))
6232 rebuild_tearoff(parent);
6233#endif
6234}
6235
6236 void
6237gui_mch_show_popupmenu(vimmenu_T *menu)
6238{
6239 POINT mp;
6240
6241 (void)GetCursorPos((LPPOINT)&mp);
6242 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6243}
6244
6245 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006246gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 vimmenu_T *menu = gui_find_menu(path_name);
6249
6250 if (menu != NULL)
6251 {
6252 POINT p;
6253
6254 /* Find the position of the current cursor */
6255 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006256 if (mouse_pos)
6257 {
6258 int mx, my;
6259
6260 gui_mch_getmouse(&mx, &my);
6261 p.x += mx;
6262 p.y += my;
6263 }
6264 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006266 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6268 }
6269 msg_scroll = FALSE;
6270 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6271 }
6272}
6273
6274#if defined(FEAT_TEAROFF) || defined(PROTO)
6275/*
6276 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6277 * create it as a pseudo-"tearoff menu".
6278 */
6279 void
6280gui_make_tearoff(char_u *path_name)
6281{
6282 vimmenu_T *menu = gui_find_menu(path_name);
6283
6284 /* Found the menu, so tear it off. */
6285 if (menu != NULL)
6286 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6287}
6288#endif
6289
6290/*
6291 * Add a menu item to a menu
6292 */
6293 void
6294gui_mch_add_menu_item(
6295 vimmenu_T *menu,
6296 int idx)
6297{
6298 vimmenu_T *parent = menu->parent;
6299
6300 menu->id = s_menu_id++;
6301 menu->submenu_id = NULL;
6302
6303#ifdef FEAT_TEAROFF
6304 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6305 {
6306 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6307 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6308 }
6309 else
6310#endif
6311#ifdef FEAT_TOOLBAR
6312 if (menu_is_toolbar(parent->name))
6313 {
6314 TBBUTTON newtb;
6315
6316 vim_memset(&newtb, 0, sizeof(newtb));
6317 if (menu_is_separator(menu->name))
6318 {
6319 newtb.iBitmap = 0;
6320 newtb.fsStyle = TBSTYLE_SEP;
6321 }
6322 else
6323 {
6324 newtb.iBitmap = get_toolbar_bitmap(menu);
6325 newtb.fsStyle = TBSTYLE_BUTTON;
6326 }
6327 newtb.idCommand = menu->id;
6328 newtb.fsState = TBSTATE_ENABLED;
6329 newtb.iString = 0;
6330 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6331 (LPARAM)&newtb);
6332 menu->submenu_id = (HMENU)-1;
6333 }
6334 else
6335#endif
6336 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006337 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006339 wn = enc_to_utf16(menu->name, NULL);
6340 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006342 InsertMenuW(parent->submenu_id, (UINT)idx,
6343 (menu_is_separator(menu->name)
6344 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6345 (UINT)menu->id, wn);
6346 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348#ifdef FEAT_TEAROFF
6349 if (IsWindow(parent->tearoff_handle))
6350 rebuild_tearoff(parent);
6351#endif
6352 }
6353}
6354
6355/*
6356 * Destroy the machine specific menu widget.
6357 */
6358 void
6359gui_mch_destroy_menu(vimmenu_T *menu)
6360{
6361#ifdef FEAT_TOOLBAR
6362 /*
6363 * is this a toolbar button?
6364 */
6365 if (menu->submenu_id == (HMENU)-1)
6366 {
6367 int iButton;
6368
6369 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6370 (WPARAM)menu->id, 0);
6371 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6372 }
6373 else
6374#endif
6375 {
6376 if (menu->parent != NULL
6377 && menu_is_popup(menu->parent->dname)
6378 && menu->parent->submenu_id != NULL)
6379 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6380 else
6381 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6382 if (menu->submenu_id != NULL)
6383 DestroyMenu(menu->submenu_id);
6384#ifdef FEAT_TEAROFF
6385 if (IsWindow(menu->tearoff_handle))
6386 DestroyWindow(menu->tearoff_handle);
6387 if (menu->parent != NULL
6388 && menu->parent->children != NULL
6389 && IsWindow(menu->parent->tearoff_handle))
6390 {
6391 /* This menu must not show up when rebuilding the tearoff window. */
6392 menu->modes = 0;
6393 rebuild_tearoff(menu->parent);
6394 }
6395#endif
6396 }
6397}
6398
6399#ifdef FEAT_TEAROFF
6400 static void
6401rebuild_tearoff(vimmenu_T *menu)
6402{
6403 /*hackish*/
6404 char_u tbuf[128];
6405 RECT trect;
6406 RECT rct;
6407 RECT roct;
6408 int x, y;
6409
6410 HWND thwnd = menu->tearoff_handle;
6411
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006412 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (GetWindowRect(thwnd, &trect)
6414 && GetWindowRect(s_hwnd, &rct)
6415 && GetClientRect(s_hwnd, &roct))
6416 {
6417 x = trect.left - rct.left;
6418 y = (trect.top - rct.bottom + roct.bottom);
6419 }
6420 else
6421 {
6422 x = y = 0xffffL;
6423 }
6424 DestroyWindow(thwnd);
6425 if (menu->children != NULL)
6426 {
6427 gui_mch_tearoff(tbuf, menu, x, y);
6428 if (IsWindow(menu->tearoff_handle))
6429 (void) SetWindowPos(menu->tearoff_handle,
6430 NULL,
6431 (int)trect.left,
6432 (int)trect.top,
6433 0, 0,
6434 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6435 }
6436}
6437#endif /* FEAT_TEAROFF */
6438
6439/*
6440 * Make a menu either grey or not grey.
6441 */
6442 void
6443gui_mch_menu_grey(
6444 vimmenu_T *menu,
6445 int grey)
6446{
6447#ifdef FEAT_TOOLBAR
6448 /*
6449 * is this a toolbar button?
6450 */
6451 if (menu->submenu_id == (HMENU)-1)
6452 {
6453 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6454 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6455 }
6456 else
6457#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006458 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6459 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460
6461#ifdef FEAT_TEAROFF
6462 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6463 {
6464 WORD menuID;
6465 HWND menuHandle;
6466
6467 /*
6468 * A tearoff button has changed state.
6469 */
6470 if (menu->children == NULL)
6471 menuID = (WORD)(menu->id);
6472 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006473 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6475 if (menuHandle)
6476 EnableWindow(menuHandle, !grey);
6477
6478 }
6479#endif
6480}
6481
6482#endif /* FEAT_MENU */
6483
6484
6485/* define some macros used to make the dialogue creation more readable */
6486
6487#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6488#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006489#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6492/*
6493 * stuff for dialogs
6494 */
6495
6496/*
6497 * The callback routine used by all the dialogs. Very simple. First,
6498 * acknowledges the INITDIALOG message so that Windows knows to do standard
6499 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6500 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6501 * number.
6502 */
6503 static LRESULT CALLBACK
6504dialog_callback(
6505 HWND hwnd,
6506 UINT message,
6507 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006508 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509{
6510 if (message == WM_INITDIALOG)
6511 {
6512 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6513 /* Set focus to the dialog. Set the default button, if specified. */
6514 (void)SetFocus(hwnd);
6515 if (dialog_default_button > IDCANCEL)
6516 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006517 else
6518 /* We don't have a default, set focus on another element of the
6519 * dialog window, probably the icon */
6520 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 return FALSE;
6522 }
6523
6524 if (message == WM_COMMAND)
6525 {
6526 int button = LOWORD(wParam);
6527
6528 /* Don't end the dialog if something was selected that was
6529 * not a button.
6530 */
6531 if (button >= DLG_NONBUTTON_CONTROL)
6532 return TRUE;
6533
6534 /* If the edit box exists, copy the string. */
6535 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006536 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006537 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6538 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006539
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006540 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6541 p = utf16_to_enc(wp, NULL);
6542 vim_strncpy(s_textfield, p, IOSIZE);
6543 vim_free(p);
6544 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 /*
6548 * Need to check for IDOK because if the user just hits Return to
6549 * accept the default value, some reason this is what we get.
6550 */
6551 if (button == IDOK)
6552 {
6553 if (dialog_default_button > IDCANCEL)
6554 EndDialog(hwnd, dialog_default_button);
6555 }
6556 else
6557 EndDialog(hwnd, button - IDCANCEL);
6558 return TRUE;
6559 }
6560
6561 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6562 {
6563 EndDialog(hwnd, 0);
6564 return TRUE;
6565 }
6566 return FALSE;
6567}
6568
6569/*
6570 * Create a dialog dynamically from the parameter strings.
6571 * type = type of dialog (question, alert, etc.)
6572 * title = dialog title. may be NULL for default title.
6573 * message = text to display. Dialog sizes to accommodate it.
6574 * buttons = '\n' separated list of button captions, default first.
6575 * dfltbutton = number of default button.
6576 *
6577 * This routine returns 1 if the first button is pressed,
6578 * 2 for the second, etc.
6579 *
6580 * 0 indicates Esc was pressed.
6581 * -1 for unexpected error
6582 *
6583 * If stubbing out this fn, return 1.
6584 */
6585
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006586static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 "IDR_VIM",
6589 "IDR_VIM_ERROR",
6590 "IDR_VIM_ALERT",
6591 "IDR_VIM_INFO",
6592 "IDR_VIM_QUESTION"
6593};
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 int
6596gui_mch_dialog(
6597 int type,
6598 char_u *title,
6599 char_u *message,
6600 char_u *buttons,
6601 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006602 char_u *textfield,
6603 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006606 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 int numButtons;
6608 int *buttonWidths, *buttonPositions;
6609 int buttonYpos;
6610 int nchar, i;
6611 DWORD lStyle;
6612 int dlgwidth = 0;
6613 int dlgheight;
6614 int editboxheight;
6615 int horizWidth = 0;
6616 int msgheight;
6617 char_u *pstart;
6618 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006619 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 char_u *tbuffer;
6621 RECT rect;
6622 HWND hwnd;
6623 HDC hdc;
6624 HFONT font, oldFont;
6625 TEXTMETRIC fontInfo;
6626 int fontHeight;
6627 int textWidth, minButtonWidth, messageWidth;
6628 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006629 int maxDialogHeight;
6630 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 int vertical;
6632 int dlgPaddingX;
6633 int dlgPaddingY;
6634#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006635 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 int use_lfSysmenu = FALSE;
6637#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006638 garray_T ga;
6639 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641#ifndef NO_CONSOLE
6642 /* Don't output anything in silent mode ("ex -s") */
6643 if (silent_mode)
6644 return dfltbutton; /* return default option */
6645#endif
6646
Bram Moolenaar748bf032005-02-02 23:04:36 +00006647 if (s_hwnd == NULL)
6648 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650 if ((type < 0) || (type > VIM_LAST_TYPE))
6651 type = 0;
6652
6653 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006654 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006655 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006656 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
6658 if (p == NULL)
6659 return -1;
6660
6661 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006662 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6664 * const.
6665 */
6666 tbuffer = vim_strsave(buttons);
6667 if (tbuffer == NULL)
6668 return -1;
6669
6670 --dfltbutton; /* Change from one-based to zero-based */
6671
6672 /* Count buttons */
6673 numButtons = 1;
6674 for (i = 0; tbuffer[i] != '\0'; i++)
6675 {
6676 if (tbuffer[i] == DLG_BUTTON_SEP)
6677 numButtons++;
6678 }
6679 if (dfltbutton >= numButtons)
6680 dfltbutton = -1;
6681
6682 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006683 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (buttonWidths == NULL)
6685 return -1;
6686
6687 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006688 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (buttonPositions == NULL)
6690 return -1;
6691
6692 /*
6693 * Calculate how big the dialog must be.
6694 */
6695 hwnd = GetDesktopWindow();
6696 hdc = GetWindowDC(hwnd);
6697#ifdef USE_SYSMENU_FONT
6698 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6699 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006700 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 use_lfSysmenu = TRUE;
6702 }
6703 else
6704#endif
6705 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6706 VARIABLE_PITCH , DLG_FONT_NAME);
6707 if (s_usenewlook)
6708 {
6709 oldFont = SelectFont(hdc, font);
6710 dlgPaddingX = DLG_PADDING_X;
6711 dlgPaddingY = DLG_PADDING_Y;
6712 }
6713 else
6714 {
6715 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6716 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6717 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6718 }
6719 GetTextMetrics(hdc, &fontInfo);
6720 fontHeight = fontInfo.tmHeight;
6721
6722 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006723 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
6725 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006726 if (s_hwnd == NULL)
6727 {
6728 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
Bram Moolenaarc716c302006-01-21 22:12:51 +00006730 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006731 get_work_area(&workarea_rect);
6732 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6733 if (maxDialogWidth > 600)
6734 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006735 /* Leave some room for the taskbar. */
6736 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006737 }
6738 else
6739 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006740 /* Use our own window for the size, unless it's very small. */
6741 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006742 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006743 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006744 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006745 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6746 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006747
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006748 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006749 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006750 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006751 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006752 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6753 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6754 }
6755
6756 /* Set dlgwidth to width of message.
6757 * Copy the message into "ga", changing NL to CR-NL and inserting line
6758 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 pstart = message;
6760 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006761 msgheight = 0;
6762 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 do
6764 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006765 msgheight += fontHeight; /* at least one line */
6766
6767 /* Need to figure out where to break the string. The system does it
6768 * at a word boundary, which would mean we can't compute the number of
6769 * wrapped lines. */
6770 textWidth = 0;
6771 last_white = NULL;
6772 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006773 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006774 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006775 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006776 && textWidth > maxDialogWidth * 3 / 4)
6777 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006778 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006779 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006780 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006781 /* Line will wrap. */
6782 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006783 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006784 textWidth = 0;
6785
6786 if (last_white != NULL)
6787 {
6788 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006789 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006790 pend = last_white + 1;
6791 last_white = NULL;
6792 }
6793 ga_append(&ga, '\r');
6794 ga_append(&ga, '\n');
6795 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006796 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006797
6798 while (--l >= 0)
6799 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006800 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006801 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006803
6804 ga_append(&ga, '\r');
6805 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 pstart = pend + 1;
6807 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006808
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006809 if (ga.ga_data != NULL)
6810 message = ga.ga_data;
6811
Bram Moolenaar748bf032005-02-02 23:04:36 +00006812 messageWidth += 10; /* roundoff space */
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006815 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6816 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 if (msgheight < DLG_ICON_HEIGHT)
6819 msgheight = DLG_ICON_HEIGHT;
6820
6821 /*
6822 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006823 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006825 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 if (!vertical)
6827 {
6828 // Place buttons horizontally if they fit.
6829 horizWidth = dlgPaddingX;
6830 pstart = tbuffer;
6831 i = 0;
6832 do
6833 {
6834 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6835 if (pend == NULL)
6836 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006837 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (textWidth < minButtonWidth)
6839 textWidth = minButtonWidth;
6840 textWidth += dlgPaddingX; /* Padding within button */
6841 buttonWidths[i] = textWidth;
6842 buttonPositions[i++] = horizWidth;
6843 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6844 pstart = pend + 1;
6845 } while (*pend != NUL);
6846
6847 if (horizWidth > maxDialogWidth)
6848 vertical = TRUE; // Too wide to fit on the screen.
6849 else if (horizWidth > dlgwidth)
6850 dlgwidth = horizWidth;
6851 }
6852
6853 if (vertical)
6854 {
6855 // Stack buttons vertically.
6856 pstart = tbuffer;
6857 do
6858 {
6859 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6860 if (pend == NULL)
6861 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006862 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 textWidth += dlgPaddingX; /* Padding within button */
6864 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
6865 if (textWidth > dlgwidth)
6866 dlgwidth = textWidth;
6867 pstart = pend + 1;
6868 } while (*pend != NUL);
6869 }
6870
6871 if (dlgwidth < DLG_MIN_WIDTH)
6872 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
6873
6874 /* start to fill in the dlgtemplate information. addressing by WORDs */
6875 if (s_usenewlook)
6876 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
6877 else
6878 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
6879
6880 add_long(lStyle);
6881 add_long(0); // (lExtendedStyle)
6882 pnumitems = p; /*save where the number of items must be stored*/
6883 add_word(0); // NumberOfItems(will change later)
6884 add_word(10); // x
6885 add_word(10); // y
6886 add_word(PixelToDialogX(dlgwidth)); // cx
6887
6888 // Dialog height.
6889 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006890 dlgheight = msgheight + 2 * dlgPaddingY
6891 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 else
6893 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6894
6895 // Dialog needs to be taller if contains an edit box.
6896 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6897 if (textfield != NULL)
6898 dlgheight += editboxheight;
6899
Bram Moolenaara95d8232013-08-07 15:27:11 +02006900 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
6901 if (dlgheight > maxDialogHeight)
6902 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006903 msgheight = msgheight - (dlgheight - maxDialogHeight);
6904 dlgheight = maxDialogHeight;
6905 scroll_flag = WS_VSCROLL;
6906 /* Make sure scrollbar doesn't appear in the middle of the dialog */
6907 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006908 }
6909
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 add_word(PixelToDialogY(dlgheight));
6911
6912 add_word(0); // Menu
6913 add_word(0); // Class
6914
6915 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006916 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6917 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 p += nchar;
6919
6920 if (s_usenewlook)
6921 {
6922 /* do the font, since DS_3DLOOK doesn't work properly */
6923#ifdef USE_SYSMENU_FONT
6924 if (use_lfSysmenu)
6925 {
6926 /* point size */
6927 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6928 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006929 wcscpy(p, lfSysmenu.lfFaceName);
6930 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 else
6933#endif
6934 {
6935 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006936 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938 p += nchar;
6939 }
6940
6941 buttonYpos = msgheight + 2 * dlgPaddingY;
6942
6943 if (textfield != NULL)
6944 buttonYpos += editboxheight;
6945
6946 pstart = tbuffer;
6947 if (!vertical)
6948 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
6949 for (i = 0; i < numButtons; i++)
6950 {
6951 /* get end of this button. */
6952 for ( pend = pstart;
6953 *pend && (*pend != DLG_BUTTON_SEP);
6954 pend++)
6955 ;
6956
6957 if (*pend)
6958 *pend = '\0';
6959
6960 /*
6961 * old NOTE:
6962 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6963 * the focus to the first tab-able button and in so doing makes that
6964 * the default!! Grrr. Workaround: Make the default button the only
6965 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6966 * he/she can use arrow keys.
6967 *
6968 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006969 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 * dialog. Also needed for when the textfield is the default control.
6971 * It appears to work now (perhaps not on Win95?).
6972 */
6973 if (vertical)
6974 {
6975 p = add_dialog_element(p,
6976 (i == dfltbutton
6977 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6978 PixelToDialogX(DLG_VERT_PADDING_X),
6979 PixelToDialogY(buttonYpos /* TBK */
6980 + 2 * fontHeight * i),
6981 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
6982 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006983 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985 else
6986 {
6987 p = add_dialog_element(p,
6988 (i == dfltbutton
6989 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6990 PixelToDialogX(horizWidth + buttonPositions[i]),
6991 PixelToDialogY(buttonYpos), /* TBK */
6992 PixelToDialogX(buttonWidths[i]),
6993 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006994 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 pstart = pend + 1; /*next button*/
6997 }
6998 *pnumitems += numButtons;
6999
7000 /* Vim icon */
7001 p = add_dialog_element(p, SS_ICON,
7002 PixelToDialogX(dlgPaddingX),
7003 PixelToDialogY(dlgPaddingY),
7004 PixelToDialogX(DLG_ICON_WIDTH),
7005 PixelToDialogY(DLG_ICON_HEIGHT),
7006 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7007 dlg_icons[type]);
7008
Bram Moolenaar748bf032005-02-02 23:04:36 +00007009 /* Dialog message */
7010 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7011 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7012 PixelToDialogY(dlgPaddingY),
7013 (WORD)(PixelToDialogX(messageWidth) + 1),
7014 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007015 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
7017 /* Edit box */
7018 if (textfield != NULL)
7019 {
7020 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7021 PixelToDialogX(2 * dlgPaddingX),
7022 PixelToDialogY(2 * dlgPaddingY + msgheight),
7023 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7024 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007025 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 *pnumitems += 1;
7027 }
7028
7029 *pnumitems += 2;
7030
7031 SelectFont(hdc, oldFont);
7032 DeleteObject(font);
7033 ReleaseDC(hwnd, hdc);
7034
7035 /* Let the dialog_callback() function know which button to make default
7036 * If we have an edit box, make that the default. We also need to tell
7037 * dialog_callback() if this dialog contains an edit box or not. We do
7038 * this by setting s_textfield if it does.
7039 */
7040 if (textfield != NULL)
7041 {
7042 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7043 s_textfield = textfield;
7044 }
7045 else
7046 {
7047 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7048 s_textfield = NULL;
7049 }
7050
7051 /* show the dialog box modally and get a return value */
7052 nchar = (int)DialogBoxIndirect(
7053 s_hinst,
7054 (LPDLGTEMPLATE)pdlgtemplate,
7055 s_hwnd,
7056 (DLGPROC)dialog_callback);
7057
7058 LocalFree(LocalHandle(pdlgtemplate));
7059 vim_free(tbuffer);
7060 vim_free(buttonWidths);
7061 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007062 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 /* Focus back to our window (for when MDI is used). */
7065 (void)SetFocus(s_hwnd);
7066
7067 return nchar;
7068}
7069
7070#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007071
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072/*
7073 * Put a simple element (basic class) onto a dialog template in memory.
7074 * return a pointer to where the next item should be added.
7075 *
7076 * parameters:
7077 * lStyle = additional style flags
7078 * (be careful, NT3.51 & Win32s will ignore the new ones)
7079 * x,y = x & y positions IN DIALOG UNITS
7080 * w,h = width and height IN DIALOG UNITS
7081 * Id = ID used in messages
7082 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7083 * caption = usually text or resource name
7084 *
7085 * TODO: use the length information noted here to enable the dialog creation
7086 * routines to work out more exactly how much memory they need to alloc.
7087 */
7088 static PWORD
7089add_dialog_element(
7090 PWORD p,
7091 DWORD lStyle,
7092 WORD x,
7093 WORD y,
7094 WORD w,
7095 WORD h,
7096 WORD Id,
7097 WORD clss,
7098 const char *caption)
7099{
7100 int nchar;
7101
7102 p = lpwAlign(p); /* Align to dword boundary*/
7103 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7104 *p++ = LOWORD(lStyle);
7105 *p++ = HIWORD(lStyle);
7106 *p++ = 0; // LOWORD (lExtendedStyle)
7107 *p++ = 0; // HIWORD (lExtendedStyle)
7108 *p++ = x;
7109 *p++ = y;
7110 *p++ = w;
7111 *p++ = h;
7112 *p++ = Id; //9 or 10 words in all
7113
7114 *p++ = (WORD)0xffff;
7115 *p++ = clss; //2 more here
7116
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007117 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 p += nchar;
7119
7120 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7121
7122 return p; //total = 15+ (strlen(caption)) words
7123 // = 30 + 2(strlen(caption) bytes reqd
7124}
7125
7126
7127/*
7128 * Helper routine. Take an input pointer, return closest pointer that is
7129 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7130 */
7131 static LPWORD
7132lpwAlign(
7133 LPWORD lpIn)
7134{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007135 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007137 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 ul += 3;
7139 ul >>= 2;
7140 ul <<= 2;
7141 return (LPWORD)ul;
7142}
7143
7144/*
7145 * Helper routine. Takes second parameter as Ansi string, copies it to first
7146 * parameter as wide character (16-bits / char) string, and returns integer
7147 * number of wide characters (words) in string (including the trailing wide
7148 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007149 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7150 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 static int
7152nCopyAnsiToWideChar(
7153 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007154 LPSTR lpAnsiIn,
7155 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7159 int i;
7160 WCHAR *wn;
7161
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007162 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 {
7164 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007165 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 if (wn != NULL)
7167 {
7168 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007169 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 vim_free(wn);
7171 }
7172 }
7173 if (nChar == 0)
7174 /* Use Win32 conversion function. */
7175 nChar = MultiByteToWideChar(
7176 enc_codepage > 0 ? enc_codepage : CP_ACP,
7177 MB_PRECOMPOSED,
7178 lpAnsiIn, len,
7179 lpWCStr, len);
7180 for (i = 0; i < nChar; ++i)
7181 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7182 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 return nChar;
7185}
7186
7187
7188#ifdef FEAT_TEAROFF
7189/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007190 * Lookup menu handle from "menu_id".
7191 */
7192 static HMENU
7193tearoff_lookup_menuhandle(
7194 vimmenu_T *menu,
7195 WORD menu_id)
7196{
7197 for ( ; menu != NULL; menu = menu->next)
7198 {
7199 if (menu->modes == 0) /* this menu has just been deleted */
7200 continue;
7201 if (menu_is_separator(menu->dname))
7202 continue;
7203 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7204 return menu->submenu_id;
7205 }
7206 return NULL;
7207}
7208
7209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 * The callback function for all the modeless dialogs that make up the
7211 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7212 * thinking its menus have been clicked), and go away when closed.
7213 */
7214 static LRESULT CALLBACK
7215tearoff_callback(
7216 HWND hwnd,
7217 UINT message,
7218 WPARAM wParam,
7219 LPARAM lParam)
7220{
7221 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007222 {
7223 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227 /* May show the mouse pointer again. */
7228 HandleMouseHide(message, lParam);
7229
7230 if (message == WM_COMMAND)
7231 {
7232 if ((WORD)(LOWORD(wParam)) & 0x8000)
7233 {
7234 POINT mp;
7235 RECT rect;
7236
7237 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7238 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007239 vimmenu_T *menu;
7240
7241 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007243 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7245 (int)rect.right - 8,
7246 (int)mp.y,
7247 (int)0, /*reserved param*/
7248 s_hwnd,
7249 NULL);
7250 /*
7251 * NOTE: The pop-up menu can eat the mouse up event.
7252 * We deal with this in normal.c.
7253 */
7254 }
7255 }
7256 else
7257 /* Pass on messages to the main Vim window */
7258 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7259 /*
7260 * Give main window the focus back: this is so after
7261 * choosing a tearoff button you can start typing again
7262 * straight away.
7263 */
7264 (void)SetFocus(s_hwnd);
7265 return TRUE;
7266 }
7267 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7268 {
7269 DestroyWindow(hwnd);
7270 return TRUE;
7271 }
7272
7273 /* When moved around, give main window the focus back. */
7274 if (message == WM_EXITSIZEMOVE)
7275 (void)SetActiveWindow(s_hwnd);
7276
7277 return FALSE;
7278}
7279#endif
7280
7281
7282/*
7283 * Decide whether to use the "new look" (small, non-bold font) or the "old
7284 * look" (big, clanky font) for dialogs, and work out a few values for use
7285 * later accordingly.
7286 */
7287 static void
7288get_dialog_font_metrics(void)
7289{
7290 HDC hdc;
7291 HFONT hfontTools = 0;
7292 DWORD dlgFontSize;
7293 SIZE size;
7294#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007295 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296#endif
7297
7298 s_usenewlook = FALSE;
7299
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007301 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007302 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007303 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#endif
7305 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7306 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7307
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007308 if (hfontTools)
7309 {
7310 hdc = GetDC(s_hwnd);
7311 SelectObject(hdc, hfontTools);
7312 /*
7313 * GetTextMetrics() doesn't return the right value in
7314 * tmAveCharWidth, so we have to figure out the dialog base units
7315 * ourselves.
7316 */
7317 GetTextExtentPoint(hdc,
7318 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7319 52, &size);
7320 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007322 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7323 s_dlgfntheight = (WORD)size.cy;
7324 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
7326
7327 if (!s_usenewlook)
7328 {
7329 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7330 s_dlgfntwidth = LOWORD(dlgFontSize);
7331 s_dlgfntheight = HIWORD(dlgFontSize);
7332 }
7333}
7334
7335#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7336/*
7337 * Create a pseudo-"tearoff menu" based on the child
7338 * items of a given menu pointer.
7339 */
7340 static void
7341gui_mch_tearoff(
7342 char_u *title,
7343 vimmenu_T *menu,
7344 int initX,
7345 int initY)
7346{
7347 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7348 int template_len;
7349 int nchar, textWidth, submenuWidth;
7350 DWORD lStyle;
7351 DWORD lExtendedStyle;
7352 WORD dlgwidth;
7353 WORD menuID;
7354 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007355 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 vimmenu_T *the_menu = menu;
7357 HWND hwnd;
7358 HDC hdc;
7359 HFONT font, oldFont;
7360 int col, spaceWidth, len;
7361 int columnWidths[2];
7362 char_u *label, *text;
7363 int acLen = 0;
7364 int nameLen;
7365 int padding0, padding1, padding2 = 0;
7366 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007367 int x;
7368 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007370 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 int use_lfSysmenu = FALSE;
7372#endif
7373
7374 /*
7375 * If this menu is already torn off, move it to the mouse position.
7376 */
7377 if (IsWindow(menu->tearoff_handle))
7378 {
7379 POINT mp;
7380 if (GetCursorPos((LPPOINT)&mp))
7381 {
7382 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7383 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7384 }
7385 return;
7386 }
7387
7388 /*
7389 * Create a new tearoff.
7390 */
7391 if (*title == MNU_HIDDEN_CHAR)
7392 title++;
7393
7394 /* Allocate memory to store the dialog template. It's made bigger when
7395 * needed. */
7396 template_len = DLG_ALLOC_SIZE;
7397 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7398 if (p == NULL)
7399 return;
7400
7401 hwnd = GetDesktopWindow();
7402 hdc = GetWindowDC(hwnd);
7403#ifdef USE_SYSMENU_FONT
7404 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7405 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007406 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 use_lfSysmenu = TRUE;
7408 }
7409 else
7410#endif
7411 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7412 VARIABLE_PITCH , DLG_FONT_NAME);
7413 if (s_usenewlook)
7414 oldFont = SelectFont(hdc, font);
7415 else
7416 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7417
7418 /* Calculate width of a single space. Used for padding columns to the
7419 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007420 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421
7422 /* Figure out max width of the text column, the accelerator column and the
7423 * optional submenu column. */
7424 submenuWidth = 0;
7425 for (col = 0; col < 2; col++)
7426 {
7427 columnWidths[col] = 0;
7428 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7429 {
7430 /* Use "dname" here to compute the width of the visible text. */
7431 text = (col == 0) ? pmenu->dname : pmenu->actext;
7432 if (text != NULL && *text != NUL)
7433 {
7434 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7435 if (textWidth > columnWidths[col])
7436 columnWidths[col] = textWidth;
7437 }
7438 if (pmenu->children != NULL)
7439 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7440 }
7441 }
7442 if (columnWidths[1] == 0)
7443 {
7444 /* no accelerators */
7445 if (submenuWidth != 0)
7446 columnWidths[0] += submenuWidth;
7447 else
7448 columnWidths[0] += spaceWidth;
7449 }
7450 else
7451 {
7452 /* there is an accelerator column */
7453 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7454 columnWidths[1] += submenuWidth;
7455 }
7456
7457 /*
7458 * Now find the total width of our 'menu'.
7459 */
7460 textWidth = columnWidths[0] + columnWidths[1];
7461 if (submenuWidth != 0)
7462 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007463 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7465 textWidth += submenuWidth;
7466 }
7467 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7468 if (textWidth > dlgwidth)
7469 dlgwidth = textWidth;
7470 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7471
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 /* start to fill in the dlgtemplate information. addressing by WORDs */
7473 if (s_usenewlook)
7474 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7475 else
7476 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7477
7478 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7479 *p++ = LOWORD(lStyle);
7480 *p++ = HIWORD(lStyle);
7481 *p++ = LOWORD(lExtendedStyle);
7482 *p++ = HIWORD(lExtendedStyle);
7483 pnumitems = p; /* save where the number of items must be stored */
7484 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007485 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007487 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 else
7489 *p++ = PixelToDialogX(initX); // x
7490 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007491 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 else
7493 *p++ = PixelToDialogY(initY); // y
7494 *p++ = PixelToDialogX(dlgwidth); // cx
7495 ptrueheight = p;
7496 *p++ = 0; // dialog height: changed later anyway
7497 *p++ = 0; // Menu
7498 *p++ = 0; // Class
7499
7500 /* copy the title of the dialog */
7501 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007502 ? (LPSTR)title
7503 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 p += nchar;
7505
7506 if (s_usenewlook)
7507 {
7508 /* do the font, since DS_3DLOOK doesn't work properly */
7509#ifdef USE_SYSMENU_FONT
7510 if (use_lfSysmenu)
7511 {
7512 /* point size */
7513 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7514 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007515 wcscpy(p, lfSysmenu.lfFaceName);
7516 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 else
7519#endif
7520 {
7521 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007522 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 }
7524 p += nchar;
7525 }
7526
7527 /*
7528 * Loop over all the items in the menu.
7529 * But skip over the tearbar.
7530 */
7531 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7532 menu = menu->children->next;
7533 else
7534 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007535 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 for ( ; menu != NULL; menu = menu->next)
7537 {
7538 if (menu->modes == 0) /* this menu has just been deleted */
7539 continue;
7540 if (menu_is_separator(menu->dname))
7541 {
7542 sepPadding += 3;
7543 continue;
7544 }
7545
7546 /* Check if there still is plenty of room in the template. Make it
7547 * larger when needed. */
7548 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7549 {
7550 WORD *newp;
7551
7552 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7553 if (newp != NULL)
7554 {
7555 template_len += 4096;
7556 mch_memmove(newp, pdlgtemplate,
7557 (char *)p - (char *)pdlgtemplate);
7558 p = newp + (p - pdlgtemplate);
7559 pnumitems = newp + (pnumitems - pdlgtemplate);
7560 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7561 LocalFree(LocalHandle(pdlgtemplate));
7562 pdlgtemplate = newp;
7563 }
7564 }
7565
7566 /* Figure out minimal length of this menu label. Use "name" for the
7567 * actual text, "dname" for estimating the displayed size. "name"
7568 * has "&a" for mnemonic and includes the accelerator. */
7569 len = nameLen = (int)STRLEN(menu->name);
7570 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7571 (int)STRLEN(menu->dname))) / spaceWidth;
7572 len += padding0;
7573
7574 if (menu->actext != NULL)
7575 {
7576 acLen = (int)STRLEN(menu->actext);
7577 len += acLen;
7578 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7579 }
7580 else
7581 textWidth = 0;
7582 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7583 len += padding1;
7584
7585 if (menu->children == NULL)
7586 {
7587 padding2 = submenuWidth / spaceWidth;
7588 len += padding2;
7589 menuID = (WORD)(menu->id);
7590 }
7591 else
7592 {
7593 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007594 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596
7597 /* Allocate menu label and fill it in */
7598 text = label = alloc((unsigned)len + 1);
7599 if (label == NULL)
7600 break;
7601
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007602 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7604 if (text == NULL)
7605 text = label + nameLen; /* no actext, use whole name */
7606 while (padding0-- > 0)
7607 *text++ = ' ';
7608 if (menu->actext != NULL)
7609 {
7610 STRNCPY(text, menu->actext, acLen);
7611 text += acLen;
7612 }
7613 while (padding1-- > 0)
7614 *text++ = ' ';
7615 if (menu->children != NULL)
7616 {
7617 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7618 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7619 }
7620 else
7621 {
7622 while (padding2-- > 0)
7623 *text++ = ' ';
7624 }
7625 *text = NUL;
7626
7627 /*
7628 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7629 * W95/NT4 it makes the tear-off look more like a menu.
7630 */
7631 p = add_dialog_element(p,
7632 BS_PUSHBUTTON|BS_LEFT,
7633 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7634 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7635 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7636 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007637 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 vim_free(label);
7639 (*pnumitems)++;
7640 }
7641
7642 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7643
7644
7645 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007646 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 s_hinst,
7648 (LPDLGTEMPLATE)pdlgtemplate,
7649 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007650 (DLGPROC)tearoff_callback,
7651 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 LocalFree(LocalHandle(pdlgtemplate));
7654 SelectFont(hdc, oldFont);
7655 DeleteObject(font);
7656 ReleaseDC(hwnd, hdc);
7657
7658 /*
7659 * Reassert ourselves as the active window. This is so that after creating
7660 * a tearoff, the user doesn't have to click with the mouse just to start
7661 * typing again!
7662 */
7663 (void)SetActiveWindow(s_hwnd);
7664
7665 /* make sure the right buttons are enabled */
7666 force_menu_update = TRUE;
7667}
7668#endif
7669
7670#if defined(FEAT_TOOLBAR) || defined(PROTO)
7671#include "gui_w32_rc.h"
7672
7673/* This not defined in older SDKs */
7674# ifndef TBSTYLE_FLAT
7675# define TBSTYLE_FLAT 0x0800
7676# endif
7677
7678/*
7679 * Create the toolbar, initially unpopulated.
7680 * (just like the menu, there are no defaults, it's all
7681 * set up through menu.vim)
7682 */
7683 static void
7684initialise_toolbar(void)
7685{
7686 InitCommonControls();
7687 s_toolbarhwnd = CreateToolbarEx(
7688 s_hwnd,
7689 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7690 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007691 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 s_hinst,
7693 IDR_TOOLBAR1, // id of initial bitmap
7694 NULL,
7695 0, // initial number of buttons
7696 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7697 TOOLBAR_BUTTON_HEIGHT,
7698 TOOLBAR_BUTTON_WIDTH,
7699 TOOLBAR_BUTTON_HEIGHT,
7700 sizeof(TBBUTTON)
7701 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007702 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703
7704 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7705}
7706
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007707 static LRESULT CALLBACK
7708toolbar_wndproc(
7709 HWND hwnd,
7710 UINT uMsg,
7711 WPARAM wParam,
7712 LPARAM lParam)
7713{
7714 HandleMouseHide(uMsg, lParam);
7715 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7716}
7717
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 static int
7719get_toolbar_bitmap(vimmenu_T *menu)
7720{
7721 int i = -1;
7722
7723 /*
7724 * Check user bitmaps first, unless builtin is specified.
7725 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007726 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {
7728 char_u fname[MAXPATHL];
7729 HANDLE hbitmap = NULL;
7730
7731 if (menu->iconfile != NULL)
7732 {
7733 gui_find_iconfile(menu->iconfile, fname, "bmp");
7734 hbitmap = LoadImage(
7735 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007736 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 IMAGE_BITMAP,
7738 TOOLBAR_BUTTON_WIDTH,
7739 TOOLBAR_BUTTON_HEIGHT,
7740 LR_LOADFROMFILE |
7741 LR_LOADMAP3DCOLORS
7742 );
7743 }
7744
7745 /*
7746 * If the LoadImage call failed, or the "icon=" file
7747 * didn't exist or wasn't specified, try the menu name
7748 */
7749 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007750 && (gui_find_bitmap(
7751#ifdef FEAT_MULTI_LANG
7752 menu->en_dname != NULL ? menu->en_dname :
7753#endif
7754 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 hbitmap = LoadImage(
7756 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007757 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 IMAGE_BITMAP,
7759 TOOLBAR_BUTTON_WIDTH,
7760 TOOLBAR_BUTTON_HEIGHT,
7761 LR_LOADFROMFILE |
7762 LR_LOADMAP3DCOLORS
7763 );
7764
7765 if (hbitmap != NULL)
7766 {
7767 TBADDBITMAP tbAddBitmap;
7768
7769 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007770 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
7772 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7773 (WPARAM)1, (LPARAM)&tbAddBitmap);
7774 /* i will be set to -1 if it fails */
7775 }
7776 }
7777 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7778 i = menu->iconidx;
7779
7780 return i;
7781}
7782#endif
7783
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007784#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7785 static void
7786initialise_tabline(void)
7787{
7788 InitCommonControls();
7789
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007790 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007791 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007792 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
7793 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007794 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007795
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007796 gui.tabline_height = TABLINE_HEIGHT;
7797
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007798# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007799 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007800# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007801}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007802
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007803/*
7804 * Get tabpage_T from POINT.
7805 */
7806 static tabpage_T *
7807GetTabFromPoint(
7808 HWND hWnd,
7809 POINT pt)
7810{
7811 tabpage_T *ptp = NULL;
7812
7813 if (gui_mch_showing_tabline())
7814 {
7815 TCHITTESTINFO htinfo;
7816 htinfo.pt = pt;
7817 /* ignore if a window under cusor is not tabcontrol. */
7818 if (s_tabhwnd == hWnd)
7819 {
7820 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7821 if (idx != -1)
7822 ptp = find_tabpage(idx + 1);
7823 }
7824 }
7825 return ptp;
7826}
7827
7828static POINT s_pt = {0, 0};
7829static HCURSOR s_hCursor = NULL;
7830
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007831 static LRESULT CALLBACK
7832tabline_wndproc(
7833 HWND hwnd,
7834 UINT uMsg,
7835 WPARAM wParam,
7836 LPARAM lParam)
7837{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007838 POINT pt;
7839 tabpage_T *tp;
7840 RECT rect;
7841 int nCenter;
7842 int idx0;
7843 int idx1;
7844
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007845 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007846
7847 switch (uMsg)
7848 {
7849 case WM_LBUTTONDOWN:
7850 {
7851 s_pt.x = GET_X_LPARAM(lParam);
7852 s_pt.y = GET_Y_LPARAM(lParam);
7853 SetCapture(hwnd);
7854 s_hCursor = GetCursor(); /* backup default cursor */
7855 break;
7856 }
7857 case WM_MOUSEMOVE:
7858 if (GetCapture() == hwnd
7859 && ((wParam & MK_LBUTTON)) != 0)
7860 {
7861 pt.x = GET_X_LPARAM(lParam);
7862 pt.y = s_pt.y;
7863 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
7864 {
7865 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7866
7867 tp = GetTabFromPoint(hwnd, pt);
7868 if (tp != NULL)
7869 {
7870 idx0 = tabpage_index(curtab) - 1;
7871 idx1 = tabpage_index(tp) - 1;
7872
7873 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7874 nCenter = rect.left + (rect.right - rect.left) / 2;
7875
7876 /* Check if the mouse cursor goes over the center of
7877 * the next tab to prevent "flickering". */
7878 if ((idx0 < idx1) && (nCenter < pt.x))
7879 {
7880 tabpage_move(idx1 + 1);
7881 update_screen(0);
7882 }
7883 else if ((idx1 < idx0) && (pt.x < nCenter))
7884 {
7885 tabpage_move(idx1);
7886 update_screen(0);
7887 }
7888 }
7889 }
7890 }
7891 break;
7892 case WM_LBUTTONUP:
7893 {
7894 if (GetCapture() == hwnd)
7895 {
7896 SetCursor(s_hCursor);
7897 ReleaseCapture();
7898 }
7899 break;
7900 }
7901 default:
7902 break;
7903 }
7904
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007905 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7906}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007907#endif
7908
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7910/*
7911 * Make the GUI window come to the foreground.
7912 */
7913 void
7914gui_mch_set_foreground(void)
7915{
7916 if (IsIconic(s_hwnd))
7917 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7918 SetForegroundWindow(s_hwnd);
7919}
7920#endif
7921
7922#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7923 static void
7924dyn_imm_load(void)
7925{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007926 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 if (hLibImm == NULL)
7928 return;
7929
7930 pImmGetCompositionStringA
7931 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
7932 pImmGetCompositionStringW
7933 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7934 pImmGetContext
7935 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7936 pImmAssociateContext
7937 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7938 pImmReleaseContext
7939 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7940 pImmGetOpenStatus
7941 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7942 pImmSetOpenStatus
7943 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007944 pImmGetCompositionFontW
7945 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7946 pImmSetCompositionFontW
7947 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 pImmSetCompositionWindow
7949 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7950 pImmGetConversionStatus
7951 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007952 pImmSetConversionStatus
7953 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 if ( pImmGetCompositionStringA == NULL
7956 || pImmGetCompositionStringW == NULL
7957 || pImmGetContext == NULL
7958 || pImmAssociateContext == NULL
7959 || pImmReleaseContext == NULL
7960 || pImmGetOpenStatus == NULL
7961 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007962 || pImmGetCompositionFontW == NULL
7963 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007965 || pImmGetConversionStatus == NULL
7966 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {
7968 FreeLibrary(hLibImm);
7969 hLibImm = NULL;
7970 pImmGetContext = NULL;
7971 return;
7972 }
7973
7974 return;
7975}
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#endif
7978
7979#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7980
7981# ifdef FEAT_XPM_W32
7982# define IMAGE_XPM 100
7983# endif
7984
7985typedef struct _signicon_t
7986{
7987 HANDLE hImage;
7988 UINT uType;
7989#ifdef FEAT_XPM_W32
7990 HANDLE hShape; /* Mask bitmap handle */
7991#endif
7992} signicon_t;
7993
7994 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01007995gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996{
7997 signicon_t *sign;
7998 int x, y, w, h;
7999
8000 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8001 return;
8002
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008003#if defined(FEAT_DIRECTX)
8004 if (IS_ENABLE_DIRECTX())
8005 DWriteContext_Flush(s_dwc);
8006#endif
8007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 x = TEXT_X(col);
8009 y = TEXT_Y(row);
8010 w = gui.char_width * 2;
8011 h = gui.char_height;
8012 switch (sign->uType)
8013 {
8014 case IMAGE_BITMAP:
8015 {
8016 HDC hdcMem;
8017 HBITMAP hbmpOld;
8018
8019 hdcMem = CreateCompatibleDC(s_hdc);
8020 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8021 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8022 SelectObject(hdcMem, hbmpOld);
8023 DeleteDC(hdcMem);
8024 }
8025 break;
8026 case IMAGE_ICON:
8027 case IMAGE_CURSOR:
8028 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8029 break;
8030#ifdef FEAT_XPM_W32
8031 case IMAGE_XPM:
8032 {
8033 HDC hdcMem;
8034 HBITMAP hbmpOld;
8035
8036 hdcMem = CreateCompatibleDC(s_hdc);
8037 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8038 /* Make hole */
8039 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8040
8041 SelectObject(hdcMem, sign->hImage);
8042 /* Paint sign */
8043 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8044 SelectObject(hdcMem, hbmpOld);
8045 DeleteDC(hdcMem);
8046 }
8047 break;
8048#endif
8049 }
8050}
8051
8052 static void
8053close_signicon_image(signicon_t *sign)
8054{
8055 if (sign)
8056 switch (sign->uType)
8057 {
8058 case IMAGE_BITMAP:
8059 DeleteObject((HGDIOBJ)sign->hImage);
8060 break;
8061 case IMAGE_CURSOR:
8062 DestroyCursor((HCURSOR)sign->hImage);
8063 break;
8064 case IMAGE_ICON:
8065 DestroyIcon((HICON)sign->hImage);
8066 break;
8067#ifdef FEAT_XPM_W32
8068 case IMAGE_XPM:
8069 DeleteObject((HBITMAP)sign->hImage);
8070 DeleteObject((HBITMAP)sign->hShape);
8071 break;
8072#endif
8073 }
8074}
8075
8076 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008077gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 signicon_t sign, *psign;
8080 char_u *ext;
8081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008083 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 if (ext > signfile)
8085 {
8086 int do_load = 1;
8087
8088 if (!STRICMP(ext, ".bmp"))
8089 sign.uType = IMAGE_BITMAP;
8090 else if (!STRICMP(ext, ".ico"))
8091 sign.uType = IMAGE_ICON;
8092 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8093 sign.uType = IMAGE_CURSOR;
8094 else
8095 do_load = 0;
8096
8097 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008098 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 gui.char_width * 2, gui.char_height,
8100 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8101#ifdef FEAT_XPM_W32
8102 if (!STRICMP(ext, ".xpm"))
8103 {
8104 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008105 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8106 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108#endif
8109 }
8110
8111 psign = NULL;
8112 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8113 != NULL)
8114 *psign = sign;
8115
8116 if (!psign)
8117 {
8118 if (sign.hImage)
8119 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008120 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122 return (void *)psign;
8123
8124}
8125
8126 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008127gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 if (sign)
8130 {
8131 close_signicon_image((signicon_t *)sign);
8132 vim_free(sign);
8133 }
8134}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008137#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138
8139/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008140 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008142 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8144 * to get current mouse position).
8145 *
8146 * Trying to use as more Windows services as possible, and as less
8147 * IE version as possible :)).
8148 *
8149 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8150 * BalloonEval struct.
8151 * 2) Enable/Disable simply create/kill BalloonEval Timer
8152 * 3) When there was enough inactivity, timer procedure posts
8153 * async request to debugger
8154 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8155 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008156 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 */
8158
Bram Moolenaar45360022005-07-21 21:08:21 +00008159/*
8160 * determine whether installed Common Controls support multiline tooltips
8161 * (i.e. their version is >= 4.70
8162 */
8163 int
8164multiline_balloon_available(void)
8165{
8166 HINSTANCE hDll;
8167 static char comctl_dll[] = "comctl32.dll";
8168 static int multiline_tip = MAYBE;
8169
8170 if (multiline_tip != MAYBE)
8171 return multiline_tip;
8172
8173 hDll = GetModuleHandle(comctl_dll);
8174 if (hDll != NULL)
8175 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008176 DLLGETVERSIONPROC pGetVer;
8177 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008178
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008179 if (pGetVer != NULL)
8180 {
8181 DLLVERSIONINFO dvi;
8182 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008183
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008184 ZeroMemory(&dvi, sizeof(dvi));
8185 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008186
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008187 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008188
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008189 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008190 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008191 || (dvi.dwMajorVersion == 4
8192 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008193 {
8194 multiline_tip = TRUE;
8195 return multiline_tip;
8196 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008197 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008198 else
8199 {
8200 /* there is chance we have ancient CommCtl 4.70
8201 which doesn't export DllGetVersion */
8202 DWORD dwHandle = 0;
8203 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8204 if (len > 0)
8205 {
8206 VS_FIXEDFILEINFO *ver;
8207 UINT vlen = 0;
8208 void *data = alloc(len);
8209
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008210 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008211 && GetFileVersionInfo(comctl_dll, 0, len, data)
8212 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8213 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008214 && HIWORD(ver->dwFileVersionMS) > 4)
8215 || ((HIWORD(ver->dwFileVersionMS) == 4
8216 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008217 {
8218 vim_free(data);
8219 multiline_tip = TRUE;
8220 return multiline_tip;
8221 }
8222 vim_free(data);
8223 }
8224 }
8225 }
8226 multiline_tip = FALSE;
8227 return multiline_tip;
8228}
8229
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008230 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008231make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008232{
8233 TOOLINFOW *pti;
8234 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008235
8236 if (multiline_balloon_available() == TRUE)
8237 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8238 else
8239 ToolInfoSize = sizeof(TOOLINFOW);
8240
8241 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8242 if (pti == NULL)
8243 return;
8244
8245 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8246 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8247 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8248 beval->target, NULL, s_hinst, NULL);
8249
8250 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8251 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8252
8253 pti->cbSize = ToolInfoSize;
8254 pti->uFlags = TTF_SUBCLASS;
8255 pti->hwnd = beval->target;
8256 pti->hinst = 0; // Don't use string resources
8257 pti->uId = ID_BEVAL_TOOLTIP;
8258
8259 if (multiline_balloon_available() == TRUE)
8260 {
8261 RECT rect;
8262 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8263 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008264 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8265 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008266 // switch multiline tooltips on
8267 if (GetClientRect(s_textArea, &rect))
8268 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8269 (LPARAM)rect.right);
8270 }
8271 else
8272 {
8273 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008274 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8275 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008276 }
8277
8278 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8279 pti->rect.left = pt.x - 3;
8280 pti->rect.top = pt.y - 3;
8281 pti->rect.right = pt.x + 3;
8282 pti->rect.bottom = pt.y + 3;
8283
8284 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8285 // Make tooltip appear sooner.
8286 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8287 // I've performed some tests and it seems the longest possible life time
8288 // of tooltip is 30 seconds.
8289 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8290 /*
8291 * HACK: force tooltip to appear, because it'll not appear until
8292 * first mouse move. D*mn M$
8293 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8294 */
8295 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8296 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8297 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008298}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008301delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008303 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304}
8305
8306 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008307BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008308 HWND hwnd UNUSED,
8309 UINT uMsg UNUSED,
8310 UINT_PTR idEvent UNUSED,
8311 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
8313 POINT pt;
8314 RECT rect;
8315
8316 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8317 return;
8318
8319 GetCursorPos(&pt);
8320 if (WindowFromPoint(pt) != s_textArea)
8321 return;
8322
8323 ScreenToClient(s_textArea, &pt);
8324 GetClientRect(s_textArea, &rect);
8325 if (!PtInRect(&rect, pt))
8326 return;
8327
8328 if (LastActivity > 0
8329 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8330 && (cur_beval->showState != ShS_PENDING
8331 || abs(cur_beval->x - pt.x) > 3
8332 || abs(cur_beval->y - pt.y) > 3))
8333 {
8334 /* Pointer resting in one place long enough, it's time to show
8335 * the tooltip. */
8336 cur_beval->showState = ShS_PENDING;
8337 cur_beval->x = pt.x;
8338 cur_beval->y = pt.y;
8339
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008340 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 if (cur_beval->msgCB != NULL)
8343 (*cur_beval->msgCB)(cur_beval, 0);
8344 }
8345}
8346
8347 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008348gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008350 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008352 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353}
8354
8355 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008356gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008358 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (beval == NULL)
8360 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008361 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008362 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008363 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364}
8365
8366 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008367gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368{
8369 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008370
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008371 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (beval->showState == ShS_SHOWING)
8373 return;
8374 GetCursorPos(&pt);
8375 ScreenToClient(s_textArea, &pt);
8376
8377 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008379 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 gui_mch_disable_beval_area(cur_beval);
8381 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008382 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008384 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385}
8386
8387 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008388gui_mch_create_beval_area(
8389 void *target, /* ignored, always use s_textArea */
8390 char_u *mesg,
8391 void (*mesgCB)(BalloonEval *, int),
8392 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394 /* partially stolen from gui_beval.c */
8395 BalloonEval *beval;
8396
8397 if (mesg != NULL && mesgCB != NULL)
8398 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008399 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 return NULL;
8401 }
8402
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008403 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 if (beval != NULL)
8405 {
8406 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 beval->msg = mesg;
8410 beval->msgCB = mesgCB;
8411 beval->clientData = clientData;
8412
8413 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 cur_beval = beval;
8415
8416 if (p_beval)
8417 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
8419 return beval;
8420}
8421
8422 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008423Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8426 return;
8427
8428 if (cur_beval != NULL)
8429 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008430 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008432 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008433 // TRACE0("TTN_SHOW {{{");
8434 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008435 break;
8436 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008437 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 delete_tooltip(cur_beval);
8439 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008440 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441
8442 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008443 break;
8444 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008445 {
8446 /* if you get there then we have new common controls */
8447 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8448 info->lpszText = (LPSTR)info->lParam;
8449 info->uFlags |= TTF_DI_SETITEM;
8450 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008451 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008452 case TTN_GETDISPINFOW:
8453 {
8454 // if we get here then we have new common controls
8455 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8456 info->lpszText = (LPWSTR)info->lParam;
8457 info->uFlags |= TTF_DI_SETITEM;
8458 }
8459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461 }
8462}
8463
8464 static void
8465TrackUserActivity(UINT uMsg)
8466{
8467 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8468 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8469 LastActivity = GetTickCount();
8470}
8471
8472 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008473gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008475#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008476 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008477#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008478 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 vim_free(beval);
8480}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008481#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8484/*
8485 * We have multiple signs to draw at the same location. Draw the
8486 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8487 */
8488 void
8489netbeans_draw_multisign_indicator(int row)
8490{
8491 int i;
8492 int y;
8493 int x;
8494
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008495 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008496 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 x = 0;
8499 y = TEXT_Y(row);
8500
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008501#if defined(FEAT_DIRECTX)
8502 if (IS_ENABLE_DIRECTX())
8503 DWriteContext_Flush(s_dwc);
8504#endif
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 for (i = 0; i < gui.char_height - 3; i++)
8507 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8508
8509 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8510 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8511 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8512 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8513 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8514 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8515 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8516}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008517#endif