blob: 10af357cc3a89b8113980b94cff43b11c088bc24 [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 Moolenaarcf7164a2016-02-20 13:55:06 +01003122
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003123 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3124 if (font_name == NULL)
3125 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003127 quality_name = quality_id2name((int)lf.lfQuality);
3128
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003129 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3130 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3131 if (res != NULL)
3132 {
3133 p = res;
3134 /* make a normal font string out of the lf thing:*/
3135 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3136 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3137 while (*p)
3138 {
3139 if (*p == ' ')
3140 *p = '_';
3141 ++p;
3142 }
3143 if (lf.lfItalic)
3144 STRCAT(p, ":i");
3145 if (lf.lfWeight >= FW_BOLD)
3146 STRCAT(p, ":b");
3147 if (lf.lfUnderline)
3148 STRCAT(p, ":u");
3149 if (lf.lfStrikeOut)
3150 STRCAT(p, ":s");
3151 if (charset_name != NULL)
3152 {
3153 STRCAT(p, ":c");
3154 STRCAT(p, charset_name);
3155 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003156 if (quality_name != NULL)
3157 {
3158 STRCAT(p, ":q");
3159 STRCAT(p, quality_name);
3160 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 }
3162
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003163 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164 return (char_u *)res;
3165}
3166
3167
3168#ifdef FEAT_MBYTE_IME
3169/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003170 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171 * 'guifont'
3172 */
3173 static void
3174update_im_font(void)
3175{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003176 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177
3178 if (p_guifontwide != NULL && *p_guifontwide != NUL
3179 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003180 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003181 norm_logfont = lf_wide;
3182 else
3183 norm_logfont = sub_logfont;
3184 im_set_font(&norm_logfont);
3185}
3186#endif
3187
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003188/*
3189 * Handler of gui.wide_font (p_guifontwide) changed notification.
3190 */
3191 void
3192gui_mch_wide_font_changed(void)
3193{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003194 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003196#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003198#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199
3200 gui_mch_free_font(gui.wide_ital_font);
3201 gui.wide_ital_font = NOFONT;
3202 gui_mch_free_font(gui.wide_bold_font);
3203 gui.wide_bold_font = NOFONT;
3204 gui_mch_free_font(gui.wide_boldital_font);
3205 gui.wide_boldital_font = NOFONT;
3206
3207 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003208 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209 {
3210 if (!lf.lfItalic)
3211 {
3212 lf.lfItalic = TRUE;
3213 gui.wide_ital_font = get_font_handle(&lf);
3214 lf.lfItalic = FALSE;
3215 }
3216 if (lf.lfWeight < FW_BOLD)
3217 {
3218 lf.lfWeight = FW_BOLD;
3219 gui.wide_bold_font = get_font_handle(&lf);
3220 if (!lf.lfItalic)
3221 {
3222 lf.lfItalic = TRUE;
3223 gui.wide_boldital_font = get_font_handle(&lf);
3224 }
3225 }
3226 }
3227}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003228
3229/*
3230 * Initialise vim to use the font with the given name.
3231 * Return FAIL if the font could not be loaded, OK otherwise.
3232 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003234gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003236 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003237 GuiFont font = NOFONT;
3238 char_u *p;
3239
3240 /* Load the font */
3241 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3242 font = get_font_handle(&lf);
3243 if (font == NOFONT)
3244 return FAIL;
3245
3246 if (font_name == NULL)
3247 font_name = (char_u *)lf.lfFaceName;
3248#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3249 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003250#endif
3251#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252 sub_logfont = lf;
3253#endif
3254#ifdef FEAT_MBYTE_IME
3255 update_im_font();
3256#endif
3257 gui_mch_free_font(gui.norm_font);
3258 gui.norm_font = font;
3259 current_font_height = lf.lfHeight;
3260 GetFontSize(font);
3261
3262 p = logfont2name(lf);
3263 if (p != NULL)
3264 {
3265 hl_set_font_name(p);
3266
3267 /* When setting 'guifont' to "*" replace it with the actual font name.
3268 * */
3269 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3270 {
3271 vim_free(p_guifont);
3272 p_guifont = p;
3273 }
3274 else
3275 vim_free(p);
3276 }
3277
3278 gui_mch_free_font(gui.ital_font);
3279 gui.ital_font = NOFONT;
3280 gui_mch_free_font(gui.bold_font);
3281 gui.bold_font = NOFONT;
3282 gui_mch_free_font(gui.boldital_font);
3283 gui.boldital_font = NOFONT;
3284
3285 if (!lf.lfItalic)
3286 {
3287 lf.lfItalic = TRUE;
3288 gui.ital_font = get_font_handle(&lf);
3289 lf.lfItalic = FALSE;
3290 }
3291 if (lf.lfWeight < FW_BOLD)
3292 {
3293 lf.lfWeight = FW_BOLD;
3294 gui.bold_font = get_font_handle(&lf);
3295 if (!lf.lfItalic)
3296 {
3297 lf.lfItalic = TRUE;
3298 gui.boldital_font = get_font_handle(&lf);
3299 }
3300 }
3301
3302 return OK;
3303}
3304
3305#ifndef WPF_RESTORETOMAXIMIZED
3306# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3307#endif
3308
3309/*
3310 * Return TRUE if the GUI window is maximized, filling the whole screen.
3311 */
3312 int
3313gui_mch_maximized(void)
3314{
3315 WINDOWPLACEMENT wp;
3316
3317 wp.length = sizeof(WINDOWPLACEMENT);
3318 if (GetWindowPlacement(s_hwnd, &wp))
3319 return wp.showCmd == SW_SHOWMAXIMIZED
3320 || (wp.showCmd == SW_SHOWMINIMIZED
3321 && wp.flags == WPF_RESTORETOMAXIMIZED);
3322
3323 return 0;
3324}
3325
3326/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003327 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3328 * is set. Compute the new Rows and Columns. This is like resizing the
3329 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330 */
3331 void
3332gui_mch_newfont(void)
3333{
3334 RECT rect;
3335
3336 GetWindowRect(s_hwnd, &rect);
3337 if (win_socket_id == 0)
3338 {
3339 gui_resize_shell(rect.right - rect.left
3340 - (GetSystemMetrics(SM_CXFRAME) +
3341 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3342 rect.bottom - rect.top
3343 - (GetSystemMetrics(SM_CYFRAME) +
3344 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3345 - GetSystemMetrics(SM_CYCAPTION)
3346#ifdef FEAT_MENU
3347 - gui_mswin_get_menu_height(FALSE)
3348#endif
3349 );
3350 }
3351 else
3352 {
3353 /* Inside another window, don't use the frame and border. */
3354 gui_resize_shell(rect.right - rect.left,
3355 rect.bottom - rect.top
3356#ifdef FEAT_MENU
3357 - gui_mswin_get_menu_height(FALSE)
3358#endif
3359 );
3360 }
3361}
3362
3363/*
3364 * Set the window title
3365 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003366 void
3367gui_mch_settitle(
3368 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003369 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003370{
3371 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3372}
3373
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003374#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3376 * misc2.c! */
3377static LPCSTR mshape_idcs[] =
3378{
3379 IDC_ARROW, /* arrow */
3380 MAKEINTRESOURCE(0), /* blank */
3381 IDC_IBEAM, /* beam */
3382 IDC_SIZENS, /* updown */
3383 IDC_SIZENS, /* udsizing */
3384 IDC_SIZEWE, /* leftright */
3385 IDC_SIZEWE, /* lrsizing */
3386 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 IDC_ARROW, /* crosshair */
3389 IDC_ARROW, /* hand1 */
3390 IDC_ARROW, /* hand2 */
3391 IDC_ARROW, /* pencil */
3392 IDC_ARROW, /* question */
3393 IDC_ARROW, /* right-arrow */
3394 IDC_UPARROW, /* up-arrow */
3395 IDC_ARROW /* last one */
3396};
3397
3398 void
3399mch_set_mouse_shape(int shape)
3400{
3401 LPCSTR idc;
3402
3403 if (shape == MSHAPE_HIDE)
3404 ShowCursor(FALSE);
3405 else
3406 {
3407 if (shape >= MSHAPE_NUMBERED)
3408 idc = IDC_ARROW;
3409 else
3410 idc = mshape_idcs[shape];
3411#ifdef SetClassLongPtr
3412 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3413#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003414 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415#endif
3416 if (!p_mh)
3417 {
3418 POINT mp;
3419
3420 /* Set the position to make it redrawn with the new shape. */
3421 (void)GetCursorPos((LPPOINT)&mp);
3422 (void)SetCursorPos(mp.x, mp.y);
3423 ShowCursor(TRUE);
3424 }
3425 }
3426}
3427#endif
3428
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003429#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431 * Wide version of convert_filter().
3432 */
3433 static WCHAR *
3434convert_filterW(char_u *s)
3435{
3436 char_u *tmp;
3437 int len;
3438 WCHAR *res;
3439
3440 tmp = convert_filter(s);
3441 if (tmp == NULL)
3442 return NULL;
3443 len = (int)STRLEN(s) + 3;
3444 res = enc_to_utf16(tmp, &len);
3445 vim_free(tmp);
3446 return res;
3447}
3448
3449/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003450 * Pop open a file browser and return the file selected, in allocated memory,
3451 * or NULL if Cancel is hit.
3452 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3453 * title - Title message for the file browser dialog.
3454 * dflt - Default name of file.
3455 * ext - Default extension to be added to files without extensions.
3456 * initdir - directory in which to open the browser (NULL = current dir)
3457 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003458 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003459 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003460gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461 int saving,
3462 char_u *title,
3463 char_u *dflt,
3464 char_u *ext,
3465 char_u *initdir,
3466 char_u *filter)
3467{
3468 /* We always use the wide function. This means enc_to_utf16() must work,
3469 * otherwise it fails miserably! */
3470 OPENFILENAMEW fileStruct;
3471 WCHAR fileBuf[MAXPATHL];
3472 WCHAR *wp;
3473 int i;
3474 WCHAR *titlep = NULL;
3475 WCHAR *extp = NULL;
3476 WCHAR *initdirp = NULL;
3477 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003478 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479
3480 if (dflt == NULL)
3481 fileBuf[0] = NUL;
3482 else
3483 {
3484 wp = enc_to_utf16(dflt, NULL);
3485 if (wp == NULL)
3486 fileBuf[0] = NUL;
3487 else
3488 {
3489 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3490 fileBuf[i] = wp[i];
3491 fileBuf[i] = NUL;
3492 vim_free(wp);
3493 }
3494 }
3495
3496 /* Convert the filter to Windows format. */
3497 filterp = convert_filterW(filter);
3498
3499 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003500# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003502 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003503# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003505# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506
3507 if (title != NULL)
3508 titlep = enc_to_utf16(title, NULL);
3509 fileStruct.lpstrTitle = titlep;
3510
3511 if (ext != NULL)
3512 extp = enc_to_utf16(ext, NULL);
3513 fileStruct.lpstrDefExt = extp;
3514
3515 fileStruct.lpstrFile = fileBuf;
3516 fileStruct.nMaxFile = MAXPATHL;
3517 fileStruct.lpstrFilter = filterp;
3518 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3519 /* has an initial dir been specified? */
3520 if (initdir != NULL && *initdir != NUL)
3521 {
3522 /* Must have backslashes here, no matter what 'shellslash' says */
3523 initdirp = enc_to_utf16(initdir, NULL);
3524 if (initdirp != NULL)
3525 {
3526 for (wp = initdirp; *wp != NUL; ++wp)
3527 if (*wp == '/')
3528 *wp = '\\';
3529 }
3530 fileStruct.lpstrInitialDir = initdirp;
3531 }
3532
3533 /*
3534 * TODO: Allow selection of multiple files. Needs another arg to this
3535 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3536 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3537 * files that don't exist yet, so I haven't put it in. What about
3538 * OFN_PATHMUSTEXIST?
3539 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3540 */
3541 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003542# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543 if (curbuf->b_p_bin)
3544 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003545# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546 if (saving)
3547 {
3548 if (!GetSaveFileNameW(&fileStruct))
3549 return NULL;
3550 }
3551 else
3552 {
3553 if (!GetOpenFileNameW(&fileStruct))
3554 return NULL;
3555 }
3556
3557 vim_free(filterp);
3558 vim_free(initdirp);
3559 vim_free(titlep);
3560 vim_free(extp);
3561
3562 /* Convert from UCS2 to 'encoding'. */
3563 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003564 if (p == NULL)
3565 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566
3567 /* Give focus back to main window (when using MDI). */
3568 SetFocus(s_hwnd);
3569
3570 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003571 q = vim_strsave(shorten_fname1(p));
3572 vim_free(p);
3573 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003574}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575
3576
3577/*
3578 * Convert the string s to the proper format for a filter string by replacing
3579 * the \t and \n delimiters with \0.
3580 * Returns the converted string in allocated memory.
3581 *
3582 * Keep in sync with convert_filterW() above!
3583 */
3584 static char_u *
3585convert_filter(char_u *s)
3586{
3587 char_u *res;
3588 unsigned s_len = (unsigned)STRLEN(s);
3589 unsigned i;
3590
3591 res = alloc(s_len + 3);
3592 if (res != NULL)
3593 {
3594 for (i = 0; i < s_len; ++i)
3595 if (s[i] == '\t' || s[i] == '\n')
3596 res[i] = '\0';
3597 else
3598 res[i] = s[i];
3599 res[s_len] = NUL;
3600 /* Add two extra NULs to make sure it's properly terminated. */
3601 res[s_len + 1] = NUL;
3602 res[s_len + 2] = NUL;
3603 }
3604 return res;
3605}
3606
3607/*
3608 * Select a directory.
3609 */
3610 char_u *
3611gui_mch_browsedir(char_u *title, char_u *initdir)
3612{
3613 /* We fake this: Use a filter that doesn't select anything and a default
3614 * file name that won't be used. */
3615 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3616 initdir, (char_u *)_("Directory\t*.nothing\n"));
3617}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618#endif /* FEAT_BROWSE */
3619
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620 static void
3621_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003622 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 HDROP hDrop)
3624{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003625#define BUFPATHLEN _MAX_PATH
3626#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 char szFile[BUFPATHLEN];
3629 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3630 UINT i;
3631 char_u **fnames;
3632 POINT pt;
3633 int_u modifiers = 0;
3634
3635 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3636
3637 /* Obtain dropped position */
3638 DragQueryPoint(hDrop, &pt);
3639 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3640
3641 reset_VIsual();
3642
3643 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3644
3645 if (fnames != NULL)
3646 for (i = 0; i < cFiles; ++i)
3647 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003648 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3649 fnames[i] = utf16_to_enc(wszFile, NULL);
3650 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651 {
3652 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3653 fnames[i] = vim_strsave((char_u *)szFile);
3654 }
3655 }
3656
3657 DragFinish(hDrop);
3658
3659 if (fnames != NULL)
3660 {
3661 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3662 modifiers |= MOUSE_SHIFT;
3663 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3664 modifiers |= MOUSE_CTRL;
3665 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3666 modifiers |= MOUSE_ALT;
3667
3668 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3669
3670 s_need_activate = TRUE;
3671 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672}
3673
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 static int
3675_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003676 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 HWND hwndCtl,
3678 UINT code,
3679 int pos)
3680{
3681 static UINT prev_code = 0; /* code of previous call */
3682 scrollbar_T *sb, *sb_info;
3683 long val;
3684 int dragging = FALSE;
3685 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 SCROLLINFO si;
3687
3688 si.cbSize = sizeof(si);
3689 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003690
3691 sb = gui_mswin_find_scrollbar(hwndCtl);
3692 if (sb == NULL)
3693 return 0;
3694
3695 if (sb->wp != NULL) /* Left or right scrollbar */
3696 {
3697 /*
3698 * Careful: need to get scrollbar info out of first (left) scrollbar
3699 * for window, but keep real scrollbar too because we must pass it to
3700 * gui_drag_scrollbar().
3701 */
3702 sb_info = &sb->wp->w_scrollbars[0];
3703 }
3704 else /* Bottom scrollbar */
3705 sb_info = sb;
3706 val = sb_info->value;
3707
3708 switch (code)
3709 {
3710 case SB_THUMBTRACK:
3711 val = pos;
3712 dragging = TRUE;
3713 if (sb->scroll_shift > 0)
3714 val <<= sb->scroll_shift;
3715 break;
3716 case SB_LINEDOWN:
3717 val++;
3718 break;
3719 case SB_LINEUP:
3720 val--;
3721 break;
3722 case SB_PAGEDOWN:
3723 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3724 break;
3725 case SB_PAGEUP:
3726 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3727 break;
3728 case SB_TOP:
3729 val = 0;
3730 break;
3731 case SB_BOTTOM:
3732 val = sb_info->max;
3733 break;
3734 case SB_ENDSCROLL:
3735 if (prev_code == SB_THUMBTRACK)
3736 {
3737 /*
3738 * "pos" only gives us 16-bit data. In case of large file,
3739 * use GetScrollPos() which returns 32-bit. Unfortunately it
3740 * is not valid while the scrollbar is being dragged.
3741 */
3742 val = GetScrollPos(hwndCtl, SB_CTL);
3743 if (sb->scroll_shift > 0)
3744 val <<= sb->scroll_shift;
3745 }
3746 break;
3747
3748 default:
3749 /* TRACE("Unknown scrollbar event %d\n", code); */
3750 return 0;
3751 }
3752 prev_code = code;
3753
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003754 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3755 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756
3757 /*
3758 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3759 */
3760 if (sb->wp != NULL)
3761 {
3762 scrollbar_T *sba = sb->wp->w_scrollbars;
3763 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3764
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003765 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003766 }
3767
3768 /* Don't let us be interrupted here by another message. */
3769 s_busy_processing = TRUE;
3770
3771 /* When "allow_scrollbar" is FALSE still need to remember the new
3772 * position, but don't actually scroll by setting "dont_scroll". */
3773 dont_scroll = !allow_scrollbar;
3774
Bram Moolenaara338adc2018-01-31 20:51:47 +01003775 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003777 mch_enable_flush();
3778 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779
3780 s_busy_processing = FALSE;
3781 dont_scroll = dont_scroll_save;
3782
3783 return 0;
3784}
3785
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787#ifdef FEAT_XPM_W32
3788# include "xpm_w32.h"
3789#endif
3790
3791#ifdef PROTO
3792# define WINAPI
3793#endif
3794
3795#ifdef __MINGW32__
3796/*
3797 * Add a lot of missing defines.
3798 * They are not always missing, we need the #ifndef's.
3799 */
3800# ifndef _cdecl
3801# define _cdecl
3802# endif
3803# ifndef IsMinimized
3804# define IsMinimized(hwnd) IsIconic(hwnd)
3805# endif
3806# ifndef IsMaximized
3807# define IsMaximized(hwnd) IsZoomed(hwnd)
3808# endif
3809# ifndef SelectFont
3810# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3811# endif
3812# ifndef GetStockBrush
3813# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3814# endif
3815# ifndef DeleteBrush
3816# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3817# endif
3818
3819# ifndef HANDLE_WM_RBUTTONDBLCLK
3820# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3821 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3822# endif
3823# ifndef HANDLE_WM_MBUTTONUP
3824# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3825 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3826# endif
3827# ifndef HANDLE_WM_MBUTTONDBLCLK
3828# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3829 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3830# endif
3831# ifndef HANDLE_WM_LBUTTONDBLCLK
3832# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3833 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3834# endif
3835# ifndef HANDLE_WM_RBUTTONDOWN
3836# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3837 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3838# endif
3839# ifndef HANDLE_WM_MOUSEMOVE
3840# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3841 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3842# endif
3843# ifndef HANDLE_WM_RBUTTONUP
3844# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3845 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3846# endif
3847# ifndef HANDLE_WM_MBUTTONDOWN
3848# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3849 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3850# endif
3851# ifndef HANDLE_WM_LBUTTONUP
3852# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3853 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3854# endif
3855# ifndef HANDLE_WM_LBUTTONDOWN
3856# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3857 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3858# endif
3859# ifndef HANDLE_WM_SYSCHAR
3860# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3861 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3862# endif
3863# ifndef HANDLE_WM_ACTIVATEAPP
3864# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3865 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3866# endif
3867# ifndef HANDLE_WM_WINDOWPOSCHANGING
3868# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3869 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3870# endif
3871# ifndef HANDLE_WM_VSCROLL
3872# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3873 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3874# endif
3875# ifndef HANDLE_WM_SETFOCUS
3876# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3877 ((fn)((hwnd), (HWND)(wParam)), 0L)
3878# endif
3879# ifndef HANDLE_WM_KILLFOCUS
3880# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3881 ((fn)((hwnd), (HWND)(wParam)), 0L)
3882# endif
3883# ifndef HANDLE_WM_HSCROLL
3884# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3885 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3886# endif
3887# ifndef HANDLE_WM_DROPFILES
3888# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3889 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3890# endif
3891# ifndef HANDLE_WM_CHAR
3892# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3893 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3894# endif
3895# ifndef HANDLE_WM_SYSDEADCHAR
3896# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3897 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3898# endif
3899# ifndef HANDLE_WM_DEADCHAR
3900# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3901 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3902# endif
3903#endif /* __MINGW32__ */
3904
3905
3906/* Some parameters for tearoff menus. All in pixels. */
3907#define TEAROFF_PADDING_X 2
3908#define TEAROFF_BUTTON_PAD_X 8
3909#define TEAROFF_MIN_WIDTH 200
3910#define TEAROFF_SUBMENU_LABEL ">>"
3911#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3912
3913
3914/* For the Intellimouse: */
3915#ifndef WM_MOUSEWHEEL
3916#define WM_MOUSEWHEEL 0x20a
3917#endif
3918
3919
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003920#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921# define ID_BEVAL_TOOLTIP 200
3922# define BEVAL_TEXT_LEN MAXPATHL
3923
Bram Moolenaar167632f2010-05-26 21:42:54 +02003924#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003925/* Work around old versions of basetsd.h which wrongly declares
3926 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003927# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003928# define UINT_PTR UINT
3929#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003932static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003934
Bram Moolenaar82881492012-11-20 16:53:39 +01003935
3936/* cproto fails on missing include files */
3937#ifndef PROTO
3938
Bram Moolenaar45360022005-07-21 21:08:21 +00003939/*
3940 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003941 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003942 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003943# include <pshpack1.h>
3944
3945#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003946
3947typedef struct _DllVersionInfo
3948{
3949 DWORD cbSize;
3950 DWORD dwMajorVersion;
3951 DWORD dwMinorVersion;
3952 DWORD dwBuildNumber;
3953 DWORD dwPlatformID;
3954} DLLVERSIONINFO;
3955
Bram Moolenaar82881492012-11-20 16:53:39 +01003956#ifndef PROTO
3957# include <poppack.h>
3958#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003959
Bram Moolenaar45360022005-07-21 21:08:21 +00003960typedef struct tagTOOLINFOA_NEW
3961{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003962 UINT cbSize;
3963 UINT uFlags;
3964 HWND hwnd;
3965 UINT_PTR uId;
3966 RECT rect;
3967 HINSTANCE hinst;
3968 LPSTR lpszText;
3969 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003970} TOOLINFO_NEW;
3971
3972typedef struct tagNMTTDISPINFO_NEW
3973{
3974 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003975 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003976 char szText[80];
3977 HINSTANCE hinst;
3978 UINT uFlags;
3979 LPARAM lParam;
3980} NMTTDISPINFO_NEW;
3981
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003982typedef struct tagTOOLINFOW_NEW
3983{
3984 UINT cbSize;
3985 UINT uFlags;
3986 HWND hwnd;
3987 UINT_PTR uId;
3988 RECT rect;
3989 HINSTANCE hinst;
3990 LPWSTR lpszText;
3991 LPARAM lParam;
3992 void *lpReserved;
3993} TOOLINFOW_NEW;
3994
3995typedef struct tagNMTTDISPINFOW_NEW
3996{
3997 NMHDR hdr;
3998 LPWSTR lpszText;
3999 WCHAR szText[80];
4000 HINSTANCE hinst;
4001 UINT uFlags;
4002 LPARAM lParam;
4003} NMTTDISPINFOW_NEW;
4004
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004005
Bram Moolenaar45360022005-07-21 21:08:21 +00004006typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4007#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004008# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#endif
4010
Bram Moolenaar45360022005-07-21 21:08:21 +00004011#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004012# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004013#endif
4014
4015#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004016# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004017#endif
4018
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004019#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004020
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004021#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4022/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4023 * it here if LPNMTTDISPINFO isn't defined.
4024 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4025 * _MSC_VER. */
4026# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4027typedef struct tagNMTTDISPINFOA {
4028 NMHDR hdr;
4029 LPSTR lpszText;
4030 char szText[80];
4031 HINSTANCE hinst;
4032 UINT uFlags;
4033 LPARAM lParam;
4034} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4035# define LPNMTTDISPINFO LPNMTTDISPINFOA
4036
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004037typedef struct tagNMTTDISPINFOW {
4038 NMHDR hdr;
4039 LPWSTR lpszText;
4040 WCHAR szText[80];
4041 HINSTANCE hinst;
4042 UINT uFlags;
4043 LPARAM lParam;
4044} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004045# endif
4046#endif
4047
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004048#ifndef TTN_GETDISPINFOW
4049# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4050#endif
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052/* Local variables: */
4053
4054#ifdef FEAT_MENU
4055static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058/*
4059 * Use the system font for dialogs and tear-off menus. Remove this line to
4060 * use DLG_FONT_NAME.
4061 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004062#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#define VIM_CLASSW L"Vim"
4066
4067/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4068 * thus there should be room for every dialog. For tearoffs it's made bigger
4069 * when needed. */
4070#define DLG_ALLOC_SIZE 16 * 1024
4071
4072/*
4073 * stuff for dialogs, menus, tearoffs etc.
4074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075static PWORD
4076add_dialog_element(
4077 PWORD p,
4078 DWORD lStyle,
4079 WORD x,
4080 WORD y,
4081 WORD w,
4082 WORD h,
4083 WORD Id,
4084 WORD clss,
4085 const char *caption);
4086static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004087static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004088#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static void get_dialog_font_metrics(void);
4092
4093static int dialog_default_button = -1;
4094
4095/* Intellimouse support */
4096static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4099#ifdef FEAT_TOOLBAR
4100static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004101static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102static int get_toolbar_bitmap(vimmenu_T *menu);
4103#endif
4104
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004105#ifdef FEAT_GUI_TABLINE
4106static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004107static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004108#endif
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_MBYTE_IME
4111static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4112static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4113#endif
4114#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4115# ifdef NOIME
4116typedef struct tagCOMPOSITIONFORM {
4117 DWORD dwStyle;
4118 POINT ptCurrentPos;
4119 RECT rcArea;
4120} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4121typedef HANDLE HIMC;
4122# endif
4123
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004124static HINSTANCE hLibImm = NULL;
4125static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4126static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4127static HIMC (WINAPI *pImmGetContext)(HWND);
4128static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4129static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4130static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4131static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004132static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4133static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004134static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4135static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004136static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137static void dyn_imm_load(void);
4138#else
4139# define pImmGetCompositionStringA ImmGetCompositionStringA
4140# define pImmGetCompositionStringW ImmGetCompositionStringW
4141# define pImmGetContext ImmGetContext
4142# define pImmAssociateContext ImmAssociateContext
4143# define pImmReleaseContext ImmReleaseContext
4144# define pImmGetOpenStatus ImmGetOpenStatus
4145# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004146# define pImmGetCompositionFontW ImmGetCompositionFontW
4147# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148# define pImmSetCompositionWindow ImmSetCompositionWindow
4149# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004150# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#ifdef FEAT_MENU
4154/*
4155 * Figure out how high the menu bar is at the moment.
4156 */
4157 static int
4158gui_mswin_get_menu_height(
4159 int fix_window) /* If TRUE, resize window if menu height changed */
4160{
4161 static int old_menu_height = -1;
4162
4163 RECT rc1, rc2;
4164 int num;
4165 int menu_height;
4166
4167 if (gui.menu_is_active)
4168 num = GetMenuItemCount(s_menuBar);
4169 else
4170 num = 0;
4171
4172 if (num == 0)
4173 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004174 else if (IsMinimized(s_hwnd))
4175 {
4176 /* The height of the menu cannot be determined while the window is
4177 * minimized. Take the previous height if the menu is changed in that
4178 * state, to avoid that Vim's vertical window size accidentally
4179 * increases due to the unaccounted-for menu height. */
4180 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 else
4183 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004184 /*
4185 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4186 * seem to have been set yet, so menu wraps in default window
4187 * width which is very narrow. Instead just return height of a
4188 * single menu item. Will still be wrong when the menu really
4189 * should wrap over more than one line.
4190 */
4191 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4192 if (gui.starting)
4193 menu_height = rc1.bottom - rc1.top + 1;
4194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004196 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4197 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200
4201 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004202 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004203 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 return menu_height;
4206}
4207#endif /*FEAT_MENU*/
4208
4209
4210/*
4211 * Setup for the Intellimouse
4212 */
4213 static void
4214init_mouse_wheel(void)
4215{
4216
4217#ifndef SPI_GETWHEELSCROLLLINES
4218# define SPI_GETWHEELSCROLLLINES 104
4219#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004220#ifndef SPI_SETWHEELSCROLLLINES
4221# define SPI_SETWHEELSCROLLLINES 105
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4225#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4226#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4227#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 mouse_scroll_lines = 3; /* reasonable default */
4230
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004231 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4232 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4233 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234}
4235
4236
4237/* Intellimouse wheel handler */
4238 static void
4239_OnMouseWheel(
4240 HWND hwnd,
4241 short zDelta)
4242{
4243/* Treat a mouse wheel event as if it were a scroll request */
4244 int i;
4245 int size;
4246 HWND hwndCtl;
4247
4248 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4249 {
4250 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4251 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4252 }
4253 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4254 {
4255 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4256 size = curwin->w_scrollbars[SBAR_LEFT].size;
4257 }
4258 else
4259 return;
4260
4261 size = curwin->w_height;
4262 if (mouse_scroll_lines == 0)
4263 init_mouse_wheel();
4264
Bram Moolenaara338adc2018-01-31 20:51:47 +01004265 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 if (mouse_scroll_lines > 0
4267 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4268 {
4269 for (i = mouse_scroll_lines; i > 0; --i)
4270 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4271 }
4272 else
4273 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004274 mch_enable_flush();
4275 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276}
4277
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004278#ifdef USE_SYSMENU_FONT
4279/*
4280 * Get Menu Font.
4281 * Return OK or FAIL.
4282 */
4283 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004284gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004285{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004286 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004287
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004288 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4289 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004290 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004291 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004292 &nm,
4293 0))
4294 return FAIL;
4295 *lf = nm.lfMenuFont;
4296 return OK;
4297}
4298#endif
4299
4300
4301#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4302/*
4303 * Set the GUI tabline font to the system menu font
4304 */
4305 static void
4306set_tabline_font(void)
4307{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004308 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004309 HFONT font;
4310 HWND hwnd;
4311 HDC hdc;
4312 HFONT hfntOld;
4313 TEXTMETRIC tm;
4314
4315 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4316 return;
4317
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004318 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004319
4320 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4321
4322 /*
4323 * Compute the height of the font used for the tab text
4324 */
4325 hwnd = GetDesktopWindow();
4326 hdc = GetWindowDC(hwnd);
4327 hfntOld = SelectFont(hdc, font);
4328
4329 GetTextMetrics(hdc, &tm);
4330
4331 SelectFont(hdc, hfntOld);
4332 ReleaseDC(hwnd, hdc);
4333
4334 /*
4335 * The space used by the tab border and the space between the tab label
4336 * and the tab border is included as 7.
4337 */
4338 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4339}
4340#endif
4341
Bram Moolenaar520470a2005-06-16 21:59:56 +00004342/*
4343 * Invoked when a setting was changed.
4344 */
4345 static LRESULT CALLBACK
4346_OnSettingChange(UINT n)
4347{
4348 if (n == SPI_SETWHEELSCROLLLINES)
4349 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4350 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004351#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4352 if (n == SPI_SETNONCLIENTMETRICS)
4353 set_tabline_font();
4354#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004355 return 0;
4356}
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#ifdef FEAT_NETBEANS_INTG
4359 static void
4360_OnWindowPosChanged(
4361 HWND hwnd,
4362 const LPWINDOWPOS lpwpos)
4363{
4364 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004365 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4368 || lpwpos->cx != cx || lpwpos->cy != cy))
4369 {
4370 x = lpwpos->x;
4371 y = lpwpos->y;
4372 cx = lpwpos->cx;
4373 cy = lpwpos->cy;
4374 netbeans_frame_moved(x, y);
4375 }
4376 /* Allow to send WM_SIZE and WM_MOVE */
4377 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4378}
4379#endif
4380
4381 static int
4382_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 UINT fwSide,
4384 LPRECT lprc)
4385{
4386 int w, h;
4387 int valid_w, valid_h;
4388 int w_offset, h_offset;
4389
4390 w = lprc->right - lprc->left;
4391 h = lprc->bottom - lprc->top;
4392 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4393 w_offset = w - valid_w;
4394 h_offset = h - valid_h;
4395
4396 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4397 || fwSide == WMSZ_BOTTOMLEFT)
4398 lprc->left += w_offset;
4399 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4400 || fwSide == WMSZ_BOTTOMRIGHT)
4401 lprc->right -= w_offset;
4402
4403 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4404 || fwSide == WMSZ_TOPRIGHT)
4405 lprc->top += h_offset;
4406 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4407 || fwSide == WMSZ_BOTTOMRIGHT)
4408 lprc->bottom -= h_offset;
4409 return TRUE;
4410}
4411
4412
4413
4414 static LRESULT CALLBACK
4415_WndProc(
4416 HWND hwnd,
4417 UINT uMsg,
4418 WPARAM wParam,
4419 LPARAM lParam)
4420{
4421 /*
4422 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4423 hwnd, uMsg, wParam, lParam);
4424 */
4425
4426 HandleMouseHide(uMsg, lParam);
4427
4428 s_uMsg = uMsg;
4429 s_wParam = wParam;
4430 s_lParam = lParam;
4431
4432 switch (uMsg)
4433 {
4434 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4435 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4436 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4437 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4438 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4439 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4440 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4441 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4442 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4443#ifdef FEAT_MENU
4444 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4445#endif
4446 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4447 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4448 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4449 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4450 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4451 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4452 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4453 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4454 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4455#ifdef FEAT_NETBEANS_INTG
4456 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4457#endif
4458
Bram Moolenaarafa24992006-03-27 20:58:26 +00004459#ifdef FEAT_GUI_TABLINE
4460 case WM_RBUTTONUP:
4461 {
4462 if (gui_mch_showing_tabline())
4463 {
4464 POINT pt;
4465 RECT rect;
4466
4467 /*
4468 * If the cursor is on the tabline, display the tab menu
4469 */
4470 GetCursorPos((LPPOINT)&pt);
4471 GetWindowRect(s_textArea, &rect);
4472 if (pt.y < rect.top)
4473 {
4474 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004475 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004476 }
4477 }
4478 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4479 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004480 case WM_LBUTTONDBLCLK:
4481 {
4482 /*
4483 * If the user double clicked the tabline, create a new tab
4484 */
4485 if (gui_mch_showing_tabline())
4486 {
4487 POINT pt;
4488 RECT rect;
4489
4490 GetCursorPos((LPPOINT)&pt);
4491 GetWindowRect(s_textArea, &rect);
4492 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004493 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004494 }
4495 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4496 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004497#endif
4498
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 case WM_QUERYENDSESSION: /* System wants to go down. */
4500 gui_shell_closed(); /* Will exit when no changed buffers. */
4501 return FALSE; /* Do NOT allow system to go down. */
4502
4503 case WM_ENDSESSION:
4504 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004507 return 0L;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 break;
4510
4511 case WM_CHAR:
4512 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4513 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004514 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 return 0L;
4516
4517 case WM_SYSCHAR:
4518 /*
4519 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4520 * shortcut key, handle like a typed ALT key, otherwise call Windows
4521 * ALT key handling.
4522 */
4523#ifdef FEAT_MENU
4524 if ( !gui.menu_is_active
4525 || p_wak[0] == 'n'
4526 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4527 )
4528#endif
4529 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004530 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return 0L;
4532 }
4533#ifdef FEAT_MENU
4534 else
4535 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4536#endif
4537
4538 case WM_SYSKEYUP:
4539#ifdef FEAT_MENU
4540 /* This used to be done only when menu is active: ALT key is used for
4541 * that. But that caused problems when menu is disabled and using
4542 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4543 * are received, mouse pointer remains hidden. */
4544 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4545#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004546 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547#endif
4548
4549 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004550 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 case WM_MOUSEWHEEL:
4553 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004554 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
Bram Moolenaar520470a2005-06-16 21:59:56 +00004556 /* Notification for change in SystemParametersInfo() */
4557 case WM_SETTINGCHANGE:
4558 return _OnSettingChange((UINT)wParam);
4559
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004560#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 case WM_NOTIFY:
4562 switch (((LPNMHDR) lParam)->code)
4563 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004564 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004565 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004567 LPNMHDR hdr = (LPNMHDR)lParam;
4568 char_u *str = NULL;
4569 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004570
Bram Moolenaard23a8232018-02-10 18:45:26 +01004571 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004572
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004573# ifdef FEAT_GUI_TABLINE
4574 if (gui_mch_showing_tabline()
4575 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004576 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004577 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004578 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004579 * Mouse is over the GUI tabline. Display the
4580 * tooltip for the tab under the cursor
4581 *
4582 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004583 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004584 GetCursorPos(&pt);
4585 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004586 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004587 TCHITTESTINFO htinfo;
4588 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004589
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004590 /*
4591 * Get the tab under the cursor
4592 */
4593 htinfo.pt.x = pt.x;
4594 htinfo.pt.y = pt.y;
4595 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4596 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004597 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004598 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004599
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004600 tp = find_tabpage(idx + 1);
4601 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004602 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004603 get_tabline_label(tp, TRUE);
4604 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004605 }
4606 }
4607 }
4608 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004609# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004610# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004611# ifdef FEAT_GUI_TABLINE
4612 else
4613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004615 UINT idButton;
4616 vimmenu_T *pMenu;
4617
4618 idButton = (UINT) hdr->idFrom;
4619 pMenu = gui_mswin_find_menu(root_menu, idButton);
4620 if (pMenu)
4621 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004623# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004624 if (str != NULL)
4625 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004626 if (hdr->code == TTN_GETDISPINFOW)
4627 {
4628 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4629
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004630 /* Set the maximum width, this also enables using
4631 * \n for line break. */
4632 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4633 0, 500);
4634
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004635 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004636 lpdi->lpszText = tt_text;
4637 /* can't show tooltip if failed */
4638 }
4639 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004640 {
4641 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4642
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004643 /* Set the maximum width, this also enables using
4644 * \n for line break. */
4645 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4646 0, 500);
4647
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004648 if (STRLEN(str) < sizeof(lpdi->szText)
4649 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004650 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004651 sizeof(lpdi->szText) - 1);
4652 else
4653 lpdi->lpszText = tt_text;
4654 }
4655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 }
4657 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004658# ifdef FEAT_GUI_TABLINE
4659 case TCN_SELCHANGE:
4660 if (gui_mch_showing_tabline()
4661 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004662 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004663 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004664 return 0L;
4665 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004666 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004667
4668 case NM_RCLICK:
4669 if (gui_mch_showing_tabline()
4670 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004671 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004672 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004673 return 0L;
4674 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004675 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004678# ifdef FEAT_GUI_TABLINE
4679 if (gui_mch_showing_tabline()
4680 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4681 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4682# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 break;
4684 }
4685 break;
4686#endif
4687#if defined(MENUHINTS) && defined(FEAT_MENU)
4688 case WM_MENUSELECT:
4689 if (((UINT) HIWORD(wParam)
4690 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4691 == MF_HILITE
4692 && (State & CMDLINE) == 0)
4693 {
4694 UINT idButton;
4695 vimmenu_T *pMenu;
4696 static int did_menu_tip = FALSE;
4697
4698 if (did_menu_tip)
4699 {
4700 msg_clr_cmdline();
4701 setcursor();
4702 out_flush();
4703 did_menu_tip = FALSE;
4704 }
4705
4706 idButton = (UINT)LOWORD(wParam);
4707 pMenu = gui_mswin_find_menu(root_menu, idButton);
4708 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4709 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4710 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004711 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004712 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004713 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 setcursor();
4715 out_flush();
4716 did_menu_tip = TRUE;
4717 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004718 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 }
4720 break;
4721#endif
4722 case WM_NCHITTEST:
4723 {
4724 LRESULT result;
4725 int x, y;
4726 int xPos = GET_X_LPARAM(lParam);
4727
4728 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4729 if (result == HTCLIENT)
4730 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004731#ifdef FEAT_GUI_TABLINE
4732 if (gui_mch_showing_tabline())
4733 {
4734 int yPos = GET_Y_LPARAM(lParam);
4735 RECT rct;
4736
4737 /* If the cursor is on the GUI tabline, don't process this
4738 * event */
4739 GetWindowRect(s_textArea, &rct);
4740 if (yPos < rct.top)
4741 return result;
4742 }
4743#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004744 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 xPos -= x;
4746
4747 if (xPos < 48) /* <VN> TODO should use system metric? */
4748 return HTBOTTOMLEFT;
4749 else
4750 return HTBOTTOMRIGHT;
4751 }
4752 else
4753 return result;
4754 }
4755 /* break; notreached */
4756
4757#ifdef FEAT_MBYTE_IME
4758 case WM_IME_NOTIFY:
4759 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4760 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004761 return 1L;
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 case WM_IME_COMPOSITION:
4764 if (!_OnImeComposition(hwnd, wParam, lParam))
4765 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004766 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#endif
4768
4769 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004771 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#endif
4774 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4775 }
4776
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004777 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778}
4779
4780/*
4781 * End of call-back routines
4782 */
4783
4784/* parent window, if specified with -P */
4785HWND vim_parent_hwnd = NULL;
4786
4787 static BOOL CALLBACK
4788FindWindowTitle(HWND hwnd, LPARAM lParam)
4789{
4790 char buf[2048];
4791 char *title = (char *)lParam;
4792
4793 if (GetWindowText(hwnd, buf, sizeof(buf)))
4794 {
4795 if (strstr(buf, title) != NULL)
4796 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004797 /* Found it. Store the window ref. and quit searching if MDI
4798 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004800 if (vim_parent_hwnd != NULL)
4801 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 }
4804 return TRUE; /* continue searching */
4805}
4806
4807/*
4808 * Invoked for '-P "title"' argument: search for parent application to open
4809 * our window in.
4810 */
4811 void
4812gui_mch_set_parent(char *title)
4813{
4814 EnumWindows(FindWindowTitle, (LPARAM)title);
4815 if (vim_parent_hwnd == NULL)
4816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004817 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 mch_exit(2);
4819 }
4820}
4821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004822#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 static void
4824ole_error(char *arg)
4825{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004826 char buf[IOSIZE];
4827
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004828 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004829 vim_snprintf(buf, IOSIZE,
4830 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4831 arg);
4832 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836/*
4837 * Parse the GUI related command-line arguments. Any arguments used are
4838 * deleted from argv, and *argc is decremented accordingly. This is called
4839 * when vim is started, whether or not the GUI has been started.
4840 */
4841 void
4842gui_mch_prepare(int *argc, char **argv)
4843{
4844 int silent = FALSE;
4845 int idx;
4846
4847 /* Check for special OLE command line parameters */
4848 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4849 {
4850 /* Check for a "-silent" argument first. */
4851 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4852 && (argv[2][0] == '-' || argv[2][0] == '/'))
4853 {
4854 silent = TRUE;
4855 idx = 2;
4856 }
4857 else
4858 idx = 1;
4859
4860 /* Register Vim as an OLE Automation server */
4861 if (STRICMP(argv[idx] + 1, "register") == 0)
4862 {
4863#ifdef FEAT_OLE
4864 RegisterMe(silent);
4865 mch_exit(0);
4866#else
4867 if (!silent)
4868 ole_error("register");
4869 mch_exit(2);
4870#endif
4871 }
4872
4873 /* Unregister Vim as an OLE Automation server */
4874 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4875 {
4876#ifdef FEAT_OLE
4877 UnregisterMe(!silent);
4878 mch_exit(0);
4879#else
4880 if (!silent)
4881 ole_error("unregister");
4882 mch_exit(2);
4883#endif
4884 }
4885
4886 /* Ignore an -embedding argument. It is only relevant if the
4887 * application wants to treat the case when it is started manually
4888 * differently from the case where it is started via automation (and
4889 * we don't).
4890 */
4891 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4892 {
4893#ifdef FEAT_OLE
4894 *argc = 1;
4895#else
4896 ole_error("embedding");
4897 mch_exit(2);
4898#endif
4899 }
4900 }
4901
4902#ifdef FEAT_OLE
4903 {
4904 int bDoRestart = FALSE;
4905
4906 InitOLE(&bDoRestart);
4907 /* automatically exit after registering */
4908 if (bDoRestart)
4909 mch_exit(0);
4910 }
4911#endif
4912
4913#ifdef FEAT_NETBEANS_INTG
4914 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004915 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 int arg;
4917
4918 for (arg = 1; arg < *argc; arg++)
4919 if (strncmp("-nb", argv[arg], 3) == 0)
4920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 netbeansArg = argv[arg];
4922 mch_memmove(&argv[arg], &argv[arg + 1],
4923 (--*argc - arg) * sizeof(char *));
4924 argv[*argc] = NULL;
4925 break; /* enough? */
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930
4931/*
4932 * Initialise the GUI. Create all the windows, set up all the call-backs
4933 * etc.
4934 */
4935 int
4936gui_mch_init(void)
4937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01004939 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#ifdef GLOBAL_IME
4942 ATOM atom;
4943#endif
4944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /* Return here if the window was already opened (happens when
4946 * gui_mch_dialog() is called early). */
4947 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004948 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 /*
4951 * Load the tearoff bitmap
4952 */
4953#ifdef FEAT_TEAROFF
4954 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
4955#endif
4956
4957 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
4958 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
4959#ifdef FEAT_MENU
4960 gui.menu_height = 0; /* Windows takes care of this */
4961#endif
4962 gui.border_width = 0;
4963
4964 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 /* First try using the wide version, so that we can use any title.
4967 * Otherwise only characters in the active codepage will work. */
4968 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
4969 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004970 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 wndclassw.lpfnWndProc = _WndProc;
4972 wndclassw.cbClsExtra = 0;
4973 wndclassw.cbWndExtra = 0;
4974 wndclassw.hInstance = s_hinst;
4975 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
4976 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
4977 wndclassw.hbrBackground = s_brush;
4978 wndclassw.lpszMenuName = NULL;
4979 wndclassw.lpszClassName = szVimWndClassW;
4980
4981 if ((
4982#ifdef GLOBAL_IME
4983 atom =
4984#endif
4985 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 if (vim_parent_hwnd != NULL)
4990 {
4991#ifdef HAVE_TRY_EXCEPT
4992 __try
4993 {
4994#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004995 // Open inside the specified parent window.
4996 // TODO: last argument should point to a CLIENTCREATESTRUCT
4997 // structure.
4998 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005000 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005001 WS_OVERLAPPEDWINDOW | WS_CHILD
5002 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5004 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005005 100, // Any value will do
5006 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 vim_parent_hwnd, NULL,
5008 s_hinst, NULL);
5009#ifdef HAVE_TRY_EXCEPT
5010 }
5011 __except(EXCEPTION_EXECUTE_HANDLER)
5012 {
5013 /* NOP */
5014 }
5015#endif
5016 if (s_hwnd == NULL)
5017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005018 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 mch_exit(2);
5020 }
5021 }
5022 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005023 {
5024 /* If the provided windowid is not valid reset it to zero, so that it
5025 * is ignored and we open our own window. */
5026 if (IsWindow((HWND)win_socket_id) <= 0)
5027 win_socket_id = 0;
5028
5029 /* Create a window. If win_socket_id is not zero without border and
5030 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005031 s_hwnd = CreateWindowW(
5032 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005033 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5034 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005035 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5036 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5037 100, /* Any value will do */
5038 100, /* Any value will do */
5039 NULL, NULL,
5040 s_hinst, NULL);
5041 if (s_hwnd != NULL && win_socket_id != 0)
5042 {
5043 SetParent(s_hwnd, (HWND)win_socket_id);
5044 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5045 }
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 if (s_hwnd == NULL)
5049 return FAIL;
5050
5051#ifdef GLOBAL_IME
5052 global_ime_init(atom, s_hwnd);
5053#endif
5054#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5055 dyn_imm_load();
5056#endif
5057
5058 /* Create the text area window */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005059 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005060 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005061 wndclassw.style = CS_OWNDC;
5062 wndclassw.lpfnWndProc = _TextAreaWndProc;
5063 wndclassw.cbClsExtra = 0;
5064 wndclassw.cbWndExtra = 0;
5065 wndclassw.hInstance = s_hinst;
5066 wndclassw.hIcon = NULL;
5067 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5068 wndclassw.hbrBackground = NULL;
5069 wndclassw.lpszMenuName = NULL;
5070 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005071
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005072 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 return FAIL;
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005076 s_textArea = CreateWindowExW(
5077 0,
5078 szTextAreaClassW, L"Vim text area",
5079 WS_CHILD | WS_VISIBLE, 0, 0,
5080 100, // Any value will do for now
5081 100, // Any value will do for now
5082 s_hwnd, NULL,
5083 s_hinst, NULL);
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (s_textArea == NULL)
5086 return FAIL;
5087
Bram Moolenaar20321902016-02-17 12:30:17 +01005088#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005089 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5090 {
5091 HANDLE hIcon = NULL;
5092
5093 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005094 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005095 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005096#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#ifdef FEAT_MENU
5099 s_menuBar = CreateMenu();
5100#endif
5101 s_hdc = GetDC(s_textArea);
5102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
5105 /* Do we need to bother with this? */
5106 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5107
5108 /* Get background/foreground colors from the system */
5109 gui_mch_def_colors();
5110
5111 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5112 * file) */
5113 set_normal_colors();
5114
5115 /*
5116 * Check that none of the colors are the same as the background color.
5117 * Then store the current values as the defaults.
5118 */
5119 gui_check_colors();
5120 gui.def_norm_pixel = gui.norm_pixel;
5121 gui.def_back_pixel = gui.back_pixel;
5122
5123 /* Get the colors for the highlight groups (gui_check_colors() might have
5124 * changed them) */
5125 highlight_gui_started();
5126
5127 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005128 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005130 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
5132 /*
5133 * Set up for Intellimouse processing
5134 */
5135 init_mouse_wheel();
5136
5137 /*
5138 * compute a couple of metrics used for the dialogs
5139 */
5140 get_dialog_font_metrics();
5141#ifdef FEAT_TOOLBAR
5142 /*
5143 * Create the toolbar
5144 */
5145 initialise_toolbar();
5146#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005147#ifdef FEAT_GUI_TABLINE
5148 /*
5149 * Create the tabline
5150 */
5151 initialise_tabline();
5152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153#ifdef MSWIN_FIND_REPLACE
5154 /*
5155 * Initialise the dialog box stuff
5156 */
5157 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5158
5159 /* Initialise the struct */
5160 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005161 s_findrep_struct.lpstrFindWhat =
5162 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005164 s_findrep_struct.lpstrReplaceWith =
5165 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5167 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5168 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005171#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005172# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5173/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5174# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005175# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005176# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005177# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005178 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005179 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005180#endif
5181
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005182#ifdef FEAT_RENDER_OPTIONS
5183 if (p_rop)
5184 (void)gui_mch_set_rendering_options(p_rop);
5185#endif
5186
Bram Moolenaar748bf032005-02-02 23:04:36 +00005187theend:
5188 /* Display any pending error messages */
5189 display_errors();
5190
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return OK;
5192}
5193
5194/*
5195 * Get the size of the screen, taking position on multiple monitors into
5196 * account (if supported).
5197 */
5198 static void
5199get_work_area(RECT *spi_rect)
5200{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005201 HMONITOR mon;
5202 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005204 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005205 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005206 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005208 moninfo.cbSize = sizeof(MONITORINFO);
5209 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005211 *spi_rect = moninfo.rcWork;
5212 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214 }
5215 /* this is the old method... */
5216 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5217}
5218
5219/*
5220 * Set the size of the window to the given width and height in pixels.
5221 */
5222 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005223gui_mch_set_shellsize(
5224 int width,
5225 int height,
5226 int min_width UNUSED,
5227 int min_height UNUSED,
5228 int base_width UNUSED,
5229 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005230 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 RECT workarea_rect;
5233 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 WINDOWPLACEMENT wndpl;
5235
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005236 /* Try to keep window completely on screen. */
5237 /* Get position of the screen work area. This is the part that is not
5238 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 get_work_area(&workarea_rect);
5240
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005241 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005242 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 wndpl.length = sizeof(WINDOWPLACEMENT);
5244 GetWindowPlacement(s_hwnd, &wndpl);
5245
5246 /* Resizing a maximized window looks very strange, unzoom it first.
5247 * But don't do it when still starting up, it may have been requested in
5248 * the shortcut. */
5249 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5250 {
5251 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5252 /* Need to get the settings of the normal window. */
5253 GetWindowPlacement(s_hwnd, &wndpl);
5254 }
5255
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005257 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005258 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005259 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005260 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 + GetSystemMetrics(SM_CYCAPTION)
5262#ifdef FEAT_MENU
5263 + gui_mswin_get_menu_height(FALSE)
5264#endif
5265 ;
5266
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005267 /* The following should take care of keeping Vim on the same monitor, no
5268 * matter if the secondary monitor is left or right of the primary
5269 * monitor. */
5270 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5271 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005272
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005273 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005275 && wndpl.rcNormalPosition.right > workarea_rect.right)
5276 OffsetRect(&wndpl.rcNormalPosition,
5277 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005279 if ((direction & RESIZE_HOR)
5280 && wndpl.rcNormalPosition.left < workarea_rect.left)
5281 OffsetRect(&wndpl.rcNormalPosition,
5282 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
Bram Moolenaarafa24992006-03-27 20:58:26 +00005284 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005285 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5286 OffsetRect(&wndpl.rcNormalPosition,
5287 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005289 if ((direction & RESIZE_VERT)
5290 && wndpl.rcNormalPosition.top < workarea_rect.top)
5291 OffsetRect(&wndpl.rcNormalPosition,
5292 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293
5294 /* set window position - we should use SetWindowPlacement rather than
5295 * SetWindowPos as the MSDN docs say the coord systems returned by
5296 * these two are not compatible. */
5297 SetWindowPlacement(s_hwnd, &wndpl);
5298
5299 SetActiveWindow(s_hwnd);
5300 SetFocus(s_hwnd);
5301
5302#ifdef FEAT_MENU
5303 /* Menu may wrap differently now */
5304 gui_mswin_get_menu_height(!gui.starting);
5305#endif
5306}
5307
5308
5309 void
5310gui_mch_set_scrollbar_thumb(
5311 scrollbar_T *sb,
5312 long val,
5313 long size,
5314 long max)
5315{
5316 SCROLLINFO info;
5317
5318 sb->scroll_shift = 0;
5319 while (max > 32767)
5320 {
5321 max = (max + 1) >> 1;
5322 val >>= 1;
5323 size >>= 1;
5324 ++sb->scroll_shift;
5325 }
5326
5327 if (sb->scroll_shift > 0)
5328 ++size;
5329
5330 info.cbSize = sizeof(info);
5331 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5332 info.nPos = val;
5333 info.nMin = 0;
5334 info.nMax = max;
5335 info.nPage = size;
5336 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5337}
5338
5339
5340/*
5341 * Set the current text font.
5342 */
5343 void
5344gui_mch_set_font(GuiFont font)
5345{
5346 gui.currFont = font;
5347}
5348
5349
5350/*
5351 * Set the current text foreground color.
5352 */
5353 void
5354gui_mch_set_fg_color(guicolor_T color)
5355{
5356 gui.currFgColor = color;
5357}
5358
5359/*
5360 * Set the current text background color.
5361 */
5362 void
5363gui_mch_set_bg_color(guicolor_T color)
5364{
5365 gui.currBgColor = color;
5366}
5367
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005368/*
5369 * Set the current text special color.
5370 */
5371 void
5372gui_mch_set_sp_color(guicolor_T color)
5373{
5374 gui.currSpColor = color;
5375}
5376
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005377#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378/*
5379 * Multi-byte handling, originally by Sung-Hoon Baek.
5380 * First static functions (no prototypes generated).
5381 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005382# ifdef _MSC_VER
5383# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5384# endif
5385# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 * handle WM_IME_NOTIFY message
5389 */
5390 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005391_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 LRESULT lResult = 0;
5394 HIMC hImc;
5395
5396 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5397 return lResult;
5398 switch (dwCommand)
5399 {
5400 case IMN_SETOPENSTATUS:
5401 if (pImmGetOpenStatus(hImc))
5402 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005403 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 im_set_position(gui.row, gui.col);
5405
5406 /* Disable langmap */
5407 State &= ~LANGMAP;
5408 if (State & INSERT)
5409 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005410#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 /* Unshown 'keymap' in status lines */
5412 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5413 {
5414 /* Save cursor position */
5415 int old_row = gui.row;
5416 int old_col = gui.col;
5417
5418 // This must be called here before
5419 // status_redraw_curbuf(), otherwise the mode
5420 // message may appear in the wrong position.
5421 showmode();
5422 status_redraw_curbuf();
5423 update_screen(0);
5424 /* Restore cursor position */
5425 gui.row = old_row;
5426 gui.col = old_col;
5427 }
5428#endif
5429 }
5430 }
5431 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005432 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 lResult = 0;
5434 break;
5435 }
5436 pImmReleaseContext(hWnd, hImc);
5437 return lResult;
5438}
5439
5440 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005441_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 char_u *ret;
5444 int len;
5445
5446 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5447 return 0;
5448
5449 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5450 if (ret != NULL)
5451 {
5452 add_to_input_buf_csi(ret, len);
5453 vim_free(ret);
5454 return 1;
5455 }
5456 return 0;
5457}
5458
5459/*
5460 * get the current composition string, in UCS-2; *lenp is the number of
5461 * *lenp is the number of Unicode characters.
5462 */
5463 static short_u *
5464GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5465{
5466 LONG ret;
5467 LPWSTR wbuf = NULL;
5468 char_u *buf;
5469
5470 if (!pImmGetContext)
5471 return NULL; /* no imm32.dll */
5472
5473 /* Try Unicode; this'll always work on NT regardless of codepage. */
5474 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5475 if (ret == 0)
5476 return NULL; /* empty */
5477
5478 if (ret > 0)
5479 {
5480 /* Allocate the requested buffer plus space for the NUL character. */
5481 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5482 if (wbuf != NULL)
5483 {
5484 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5485 *lenp = ret / sizeof(WCHAR);
5486 }
5487 return (short_u *)wbuf;
5488 }
5489
5490 /* ret < 0; we got an error, so try the ANSI version. This'll work
5491 * on 9x/ME, but only if the codepage happens to be set to whatever
5492 * we're inputting. */
5493 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5494 if (ret <= 0)
5495 return NULL; /* empty or error */
5496
5497 buf = alloc(ret);
5498 if (buf == NULL)
5499 return NULL;
5500 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5501
5502 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005503 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 vim_free(buf);
5505
5506 return (short_u *)wbuf;
5507}
5508
5509/*
5510 * void GetResultStr()
5511 *
5512 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5513 * get complete composition string
5514 */
5515 static char_u *
5516GetResultStr(HWND hwnd, int GCS, int *lenp)
5517{
5518 HIMC hIMC; /* Input context handle. */
5519 short_u *buf = NULL;
5520 char_u *convbuf = NULL;
5521
5522 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5523 return NULL;
5524
5525 /* Reads in the composition string. */
5526 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5527 if (buf == NULL)
5528 return NULL;
5529
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005530 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 pImmReleaseContext(hwnd, hIMC);
5532 vim_free(buf);
5533 return convbuf;
5534}
5535#endif
5536
5537/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005538#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540/*
5541 * set font to IM.
5542 */
5543 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005544im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 HIMC hImc;
5547
5548 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5549 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005550 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 pImmReleaseContext(s_hwnd, hImc);
5552 }
5553}
5554
5555/*
5556 * Notify cursor position to IM.
5557 */
5558 void
5559im_set_position(int row, int col)
5560{
5561 HIMC hImc;
5562
5563 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5564 {
5565 COMPOSITIONFORM cfs;
5566
5567 cfs.dwStyle = CFS_POINT;
5568 cfs.ptCurrentPos.x = FILL_X(col);
5569 cfs.ptCurrentPos.y = FILL_Y(row);
5570 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5571 pImmSetCompositionWindow(hImc, &cfs);
5572
5573 pImmReleaseContext(s_hwnd, hImc);
5574 }
5575}
5576
5577/*
5578 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5579 */
5580 void
5581im_set_active(int active)
5582{
5583 HIMC hImc;
5584 static HIMC hImcOld = (HIMC)0;
5585
5586 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5587 {
5588 if (p_imdisable)
5589 {
5590 if (hImcOld == (HIMC)0)
5591 {
5592 hImcOld = pImmGetContext(s_hwnd);
5593 if (hImcOld)
5594 pImmAssociateContext(s_hwnd, (HIMC)0);
5595 }
5596 active = FALSE;
5597 }
5598 else if (hImcOld != (HIMC)0)
5599 {
5600 pImmAssociateContext(s_hwnd, hImcOld);
5601 hImcOld = (HIMC)0;
5602 }
5603
5604 hImc = pImmGetContext(s_hwnd);
5605 if (hImc)
5606 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005607 /*
5608 * for Korean ime
5609 */
5610 HKL hKL = GetKeyboardLayout(0);
5611
5612 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5613 {
5614 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5615 static BOOL bSaved = FALSE;
5616
5617 if (active)
5618 {
5619 /* if we have a saved conversion status, restore it */
5620 if (bSaved)
5621 pImmSetConversionStatus(hImc, dwConversionSaved,
5622 dwSentenceSaved);
5623 bSaved = FALSE;
5624 }
5625 else
5626 {
5627 /* save conversion status and disable korean */
5628 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5629 &dwSentenceSaved))
5630 {
5631 bSaved = TRUE;
5632 pImmSetConversionStatus(hImc,
5633 dwConversionSaved & ~(IME_CMODE_NATIVE
5634 | IME_CMODE_FULLSHAPE),
5635 dwSentenceSaved);
5636 }
5637 }
5638 }
5639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 pImmSetOpenStatus(hImc, active);
5641 pImmReleaseContext(s_hwnd, hImc);
5642 }
5643 }
5644}
5645
5646/*
5647 * Get IM status. When IM is on, return not 0. Else return 0.
5648 */
5649 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005650im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
5652 int status = 0;
5653 HIMC hImc;
5654
5655 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5656 {
5657 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5658 pImmReleaseContext(s_hwnd, hImc);
5659 }
5660 return status;
5661}
5662
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005663#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005665#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666/* Win32 with GLOBAL IME */
5667
5668/*
5669 * Notify cursor position to IM.
5670 */
5671 void
5672im_set_position(int row, int col)
5673{
5674 /* Win32 with GLOBAL IME */
5675 POINT p;
5676
5677 p.x = FILL_X(col);
5678 p.y = FILL_Y(row);
5679 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5680 global_ime_set_position(&p);
5681}
5682
5683/*
5684 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5685 */
5686 void
5687im_set_active(int active)
5688{
5689 global_ime_set_status(active);
5690}
5691
5692/*
5693 * Get IM status. When IM is on, return not 0. Else return 0.
5694 */
5695 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005696im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 return global_ime_get_status();
5699}
5700#endif
5701
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005702/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005703 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005704 */
5705 static void
5706latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5707{
5708 int c;
5709
Bram Moolenaarca003e12006-03-17 23:19:38 +00005710 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005711 {
5712 c = *text++;
5713 switch (c)
5714 {
5715 case 0xa4: c = 0x20ac; break; /* euro */
5716 case 0xa6: c = 0x0160; break; /* S hat */
5717 case 0xa8: c = 0x0161; break; /* S -hat */
5718 case 0xb4: c = 0x017d; break; /* Z hat */
5719 case 0xb8: c = 0x017e; break; /* Z -hat */
5720 case 0xbc: c = 0x0152; break; /* OE */
5721 case 0xbd: c = 0x0153; break; /* oe */
5722 case 0xbe: c = 0x0178; break; /* Y */
5723 }
5724 *unicodebuf++ = c;
5725 }
5726}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728#ifdef FEAT_RIGHTLEFT
5729/*
5730 * What is this for? In the case where you are using Win98 or Win2K or later,
5731 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5732 * reverses the string sent to the TextOut... family. This sucks, because we
5733 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5734 * way to tell Windblows not to do this!
5735 *
5736 * The short of it is that this 'RevOut' only gets called if you are running
5737 * one of the new, "improved" MS OSes, and only if you are running in
5738 * 'rightleft' mode. It makes display take *slightly* longer, but not
5739 * noticeably so.
5740 */
5741 static void
5742RevOut( HDC s_hdc,
5743 int col,
5744 int row,
5745 UINT foptions,
5746 CONST RECT *pcliprect,
5747 LPCTSTR text,
5748 UINT len,
5749 CONST INT *padding)
5750{
5751 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005753 for (ix = 0; ix < (int)len; ++ix)
5754 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5755 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756}
5757#endif
5758
Bram Moolenaar92467d32017-12-05 13:22:16 +01005759 static void
5760draw_line(
5761 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005762 int y1,
5763 int x2,
5764 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005765 COLORREF color)
5766{
5767#if defined(FEAT_DIRECTX)
5768 if (IS_ENABLE_DIRECTX())
5769 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5770 else
5771#endif
5772 {
5773 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5774 HPEN old_pen = SelectObject(s_hdc, hpen);
5775 MoveToEx(s_hdc, x1, y1, NULL);
5776 /* Note: LineTo() excludes the last pixel in the line. */
5777 LineTo(s_hdc, x2, y2);
5778 DeleteObject(SelectObject(s_hdc, old_pen));
5779 }
5780}
5781
5782 static void
5783set_pixel(
5784 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005785 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005786 COLORREF color)
5787{
5788#if defined(FEAT_DIRECTX)
5789 if (IS_ENABLE_DIRECTX())
5790 DWriteContext_SetPixel(s_dwc, x, y, color);
5791 else
5792#endif
5793 SetPixel(s_hdc, x, y, color);
5794}
5795
5796 static void
5797fill_rect(
5798 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005799 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005800 COLORREF color)
5801{
5802#if defined(FEAT_DIRECTX)
5803 if (IS_ENABLE_DIRECTX())
5804 DWriteContext_FillRect(s_dwc, rcp, color);
5805 else
5806#endif
5807 {
5808 HBRUSH hbr2;
5809
5810 if (hbr == NULL)
5811 hbr2 = CreateSolidBrush(color);
5812 else
5813 hbr2 = hbr;
5814 FillRect(s_hdc, rcp, hbr2);
5815 if (hbr == NULL)
5816 DeleteBrush(hbr2);
5817 }
5818}
5819
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 void
5821gui_mch_draw_string(
5822 int row,
5823 int col,
5824 char_u *text,
5825 int len,
5826 int flags)
5827{
5828 static int *padding = NULL;
5829 static int pad_size = 0;
5830 int i;
5831 const RECT *pcliprect = NULL;
5832 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 static WCHAR *unicodebuf = NULL;
5834 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005835 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 int y;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 /*
5840 * Italic and bold text seems to have an extra row of pixels at the bottom
5841 * (below where the bottom of the character should be). If we draw the
5842 * characters with a solid background, the top row of pixels in the
5843 * character below will be overwritten. We can fix this by filling in the
5844 * background ourselves, to the correct character proportions, and then
5845 * writing the character in transparent mode. Still have a problem when
5846 * the character is "_", which gets written on to the character below.
5847 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5848 * pixel in their slots, which fixes the problem with the bottom row of
5849 * pixels. We still need this code because otherwise the top row of pixels
5850 * becomes a problem. - webb.
5851 */
5852 static HBRUSH hbr_cache[2] = {NULL, NULL};
5853 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5854 static int brush_lru = 0;
5855 HBRUSH hbr;
5856 RECT rc;
5857
5858 if (!(flags & DRAW_TRANSP))
5859 {
5860 /*
5861 * Clear background first.
5862 * Note: FillRect() excludes right and bottom of rectangle.
5863 */
5864 rc.left = FILL_X(col);
5865 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (has_mbyte)
5867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02005869 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
5871 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 rc.right = FILL_X(col + len);
5873 rc.bottom = FILL_Y(row + 1);
5874
5875 /* Cache the created brush, that saves a lot of time. We need two:
5876 * one for cursor background and one for the normal background. */
5877 if (gui.currBgColor == brush_color[0])
5878 {
5879 hbr = hbr_cache[0];
5880 brush_lru = 1;
5881 }
5882 else if (gui.currBgColor == brush_color[1])
5883 {
5884 hbr = hbr_cache[1];
5885 brush_lru = 0;
5886 }
5887 else
5888 {
5889 if (hbr_cache[brush_lru] != NULL)
5890 DeleteBrush(hbr_cache[brush_lru]);
5891 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5892 brush_color[brush_lru] = gui.currBgColor;
5893 hbr = hbr_cache[brush_lru];
5894 brush_lru = !brush_lru;
5895 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005896
Bram Moolenaar92467d32017-12-05 13:22:16 +01005897 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899 SetBkMode(s_hdc, TRANSPARENT);
5900
5901 /*
5902 * When drawing block cursor, prevent inverted character spilling
5903 * over character cell (can happen with bold/italic)
5904 */
5905 if (flags & DRAW_CURSOR)
5906 {
5907 pcliprect = &rc;
5908 foptions = ETO_CLIPPED;
5909 }
5910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 SetTextColor(s_hdc, gui.currFgColor);
5912 SelectFont(s_hdc, gui.currFont);
5913
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005914#ifdef FEAT_DIRECTX
5915 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005916 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005917#endif
5918
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5920 {
5921 vim_free(padding);
5922 pad_size = Columns;
5923
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005924 /* Don't give an out-of-memory message here, it would call us
5925 * recursively. */
5926 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if (padding != NULL)
5928 for (i = 0; i < pad_size; i++)
5929 padding[i] = gui.char_width;
5930 }
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 /*
5933 * We have to provide the padding argument because italic and bold versions
5934 * of fixed-width fonts are often one pixel or so wider than their normal
5935 * versions.
5936 * No check for DRAW_BOLD, Windows will have done it already.
5937 */
5938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 /* Check if there are any UTF-8 characters. If not, use normal text
5940 * output to speed up output. */
5941 if (enc_utf8)
5942 for (n = 0; n < len; ++n)
5943 if (text[n] >= 0x80)
5944 break;
5945
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005946#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005947 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5948 * required that unicode drawing routine, currently. So this forces it
5949 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005950 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005951 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005952#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005953
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005955 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005957 if ((enc_utf8
5958 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
5959 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 && (unicodebuf == NULL || len > unibuflen))
5961 {
5962 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005963 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
5965 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005966 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
5968 unibuflen = len;
5969 }
5970
5971 if (enc_utf8 && n < len && unicodebuf != NULL)
5972 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02005973 /* Output UTF-8 characters. Composing characters should be
5974 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005975 int i;
5976 int wlen; /* string length in words */
5977 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 int cells; /* cell width of string up to composing char */
5979 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005980 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005982 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005983 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005985 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005987 c = utf_ptr2char(text + i);
5988 if (c >= 0x10000)
5989 {
5990 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005991 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
5992 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005993 }
5994 else
5995 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005996 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005997 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02005998
5999 if (utf_iscomposing(c))
6000 cw = 0;
6001 else
6002 {
6003 cw = utf_char2cells(c);
6004 if (cw > 2) /* don't use 4 for unprintable char */
6005 cw = 1;
6006 }
6007
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 if (unicodepdy != NULL)
6009 {
6010 /* Use unicodepdy to make characters fit as we expect, even
6011 * when the font uses different widths (e.g., bold character
6012 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006013 if (c >= 0x10000)
6014 {
6015 unicodepdy[wlen - 2] = cw * gui.char_width;
6016 unicodepdy[wlen - 1] = 0;
6017 }
6018 else
6019 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 }
6021 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006022 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006023 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006025#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006026 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006027 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006028 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006029 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006030 TEXT_X(col), TEXT_Y(row),
6031 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006032 gui.char_width, gui.currFgColor,
6033 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006034 }
6035 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006036#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006037 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6038 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 len = cells; /* used for underlining */
6040 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006041 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
6043 /* If we want to display codepage data, and the current CP is not the
6044 * ANSI one, we need to go via Unicode. */
6045 if (unicodebuf != NULL)
6046 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006047 if (enc_latin9)
6048 latin9_to_ucs(text, len, unicodebuf);
6049 else
6050 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 MB_PRECOMPOSED,
6052 (char *)text, len,
6053 (LPWSTR)unicodebuf, unibuflen);
6054 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006055 {
6056 /* Use unicodepdy to make characters fit as we expect, even
6057 * when the font uses different widths (e.g., bold character
6058 * is wider). */
6059 if (unicodepdy != NULL)
6060 {
6061 int i;
6062 int cw;
6063
6064 for (i = 0; i < len; ++i)
6065 {
6066 cw = utf_char2cells(unicodebuf[i]);
6067 if (cw > 2)
6068 cw = 1;
6069 unicodepdy[i] = cw * gui.char_width;
6070 }
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006073 foptions, pcliprect, unicodebuf, len, unicodepdy);
6074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076 }
6077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
6079#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006080 /* Windows will mess up RL text, so we have to draw it character by
6081 * character. Only do this if RL is on, since it's slow. */
6082 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6084 foptions, pcliprect, (char *)text, len, padding);
6085 else
6086#endif
6087 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6088 foptions, pcliprect, (char *)text, len, padding);
6089 }
6090
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006091 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (flags & DRAW_UNDERL)
6093 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 /* When p_linespace is 0, overwrite the bottom row of pixels.
6095 * Otherwise put the line just below the character. */
6096 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (p_linespace > 1)
6098 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006099 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006101
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006102 /* Strikethrough */
6103 if (flags & DRAW_STRIKE)
6104 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006105 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006106 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006107 }
6108
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006109 /* Undercurl */
6110 if (flags & DRAW_UNDERC)
6111 {
6112 int x;
6113 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006114 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006115
6116 y = FILL_Y(row + 1) - 1;
6117 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6118 {
6119 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006120 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006121 }
6122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123}
6124
6125
6126/*
6127 * Output routines.
6128 */
6129
6130/* Flush any output to the screen */
6131 void
6132gui_mch_flush(void)
6133{
6134# if defined(__BORLANDC__)
6135 /*
6136 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6137 * prototype declaration.
6138 * The compiler complains if __stdcall is not used in both declarations.
6139 */
6140 BOOL __stdcall GdiFlush(void);
6141# endif
6142
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006143#if defined(FEAT_DIRECTX)
6144 if (IS_ENABLE_DIRECTX())
6145 DWriteContext_Flush(s_dwc);
6146#endif
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 GdiFlush();
6149}
6150
6151 static void
6152clear_rect(RECT *rcp)
6153{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006154 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155}
6156
6157
Bram Moolenaarc716c302006-01-21 22:12:51 +00006158 void
6159gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6160{
6161 RECT workarea_rect;
6162
6163 get_work_area(&workarea_rect);
6164
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006165 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006166 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006167 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006168
6169 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6170 * the menubar for MSwin, we subtract it from the screen height, so that
6171 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006172 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006173 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006174 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006175 - GetSystemMetrics(SM_CYCAPTION)
6176#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006177 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006178#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006179 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006180}
6181
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183#if defined(FEAT_MENU) || defined(PROTO)
6184/*
6185 * Add a sub menu to the menu bar.
6186 */
6187 void
6188gui_mch_add_menu(
6189 vimmenu_T *menu,
6190 int pos)
6191{
6192 vimmenu_T *parent = menu->parent;
6193
6194 menu->submenu_id = CreatePopupMenu();
6195 menu->id = s_menu_id++;
6196
6197 if (menu_is_menubar(menu->name))
6198 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006199 WCHAR *wn;
6200 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006202 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006203 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006204 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006206 infow.cbSize = sizeof(infow);
6207 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6208 | MIIM_SUBMENU;
6209 infow.dwItemData = (long_u)menu;
6210 infow.wID = menu->id;
6211 infow.fType = MFT_STRING;
6212 infow.dwTypeData = wn;
6213 infow.cch = (UINT)wcslen(wn);
6214 infow.hSubMenu = menu->submenu_id;
6215 InsertMenuItemW((parent == NULL)
6216 ? s_menuBar : parent->submenu_id,
6217 (UINT)pos, TRUE, &infow);
6218 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220
6221 /* Fix window size if menu may have wrapped */
6222 if (parent == NULL)
6223 gui_mswin_get_menu_height(!gui.starting);
6224#ifdef FEAT_TEAROFF
6225 else if (IsWindow(parent->tearoff_handle))
6226 rebuild_tearoff(parent);
6227#endif
6228}
6229
6230 void
6231gui_mch_show_popupmenu(vimmenu_T *menu)
6232{
6233 POINT mp;
6234
6235 (void)GetCursorPos((LPPOINT)&mp);
6236 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6237}
6238
6239 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006240gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 vimmenu_T *menu = gui_find_menu(path_name);
6243
6244 if (menu != NULL)
6245 {
6246 POINT p;
6247
6248 /* Find the position of the current cursor */
6249 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006250 if (mouse_pos)
6251 {
6252 int mx, my;
6253
6254 gui_mch_getmouse(&mx, &my);
6255 p.x += mx;
6256 p.y += my;
6257 }
6258 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006260 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6262 }
6263 msg_scroll = FALSE;
6264 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6265 }
6266}
6267
6268#if defined(FEAT_TEAROFF) || defined(PROTO)
6269/*
6270 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6271 * create it as a pseudo-"tearoff menu".
6272 */
6273 void
6274gui_make_tearoff(char_u *path_name)
6275{
6276 vimmenu_T *menu = gui_find_menu(path_name);
6277
6278 /* Found the menu, so tear it off. */
6279 if (menu != NULL)
6280 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6281}
6282#endif
6283
6284/*
6285 * Add a menu item to a menu
6286 */
6287 void
6288gui_mch_add_menu_item(
6289 vimmenu_T *menu,
6290 int idx)
6291{
6292 vimmenu_T *parent = menu->parent;
6293
6294 menu->id = s_menu_id++;
6295 menu->submenu_id = NULL;
6296
6297#ifdef FEAT_TEAROFF
6298 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6299 {
6300 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6301 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6302 }
6303 else
6304#endif
6305#ifdef FEAT_TOOLBAR
6306 if (menu_is_toolbar(parent->name))
6307 {
6308 TBBUTTON newtb;
6309
6310 vim_memset(&newtb, 0, sizeof(newtb));
6311 if (menu_is_separator(menu->name))
6312 {
6313 newtb.iBitmap = 0;
6314 newtb.fsStyle = TBSTYLE_SEP;
6315 }
6316 else
6317 {
6318 newtb.iBitmap = get_toolbar_bitmap(menu);
6319 newtb.fsStyle = TBSTYLE_BUTTON;
6320 }
6321 newtb.idCommand = menu->id;
6322 newtb.fsState = TBSTATE_ENABLED;
6323 newtb.iString = 0;
6324 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6325 (LPARAM)&newtb);
6326 menu->submenu_id = (HMENU)-1;
6327 }
6328 else
6329#endif
6330 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006331 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006333 wn = enc_to_utf16(menu->name, NULL);
6334 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006336 InsertMenuW(parent->submenu_id, (UINT)idx,
6337 (menu_is_separator(menu->name)
6338 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6339 (UINT)menu->id, wn);
6340 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342#ifdef FEAT_TEAROFF
6343 if (IsWindow(parent->tearoff_handle))
6344 rebuild_tearoff(parent);
6345#endif
6346 }
6347}
6348
6349/*
6350 * Destroy the machine specific menu widget.
6351 */
6352 void
6353gui_mch_destroy_menu(vimmenu_T *menu)
6354{
6355#ifdef FEAT_TOOLBAR
6356 /*
6357 * is this a toolbar button?
6358 */
6359 if (menu->submenu_id == (HMENU)-1)
6360 {
6361 int iButton;
6362
6363 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6364 (WPARAM)menu->id, 0);
6365 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6366 }
6367 else
6368#endif
6369 {
6370 if (menu->parent != NULL
6371 && menu_is_popup(menu->parent->dname)
6372 && menu->parent->submenu_id != NULL)
6373 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6374 else
6375 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6376 if (menu->submenu_id != NULL)
6377 DestroyMenu(menu->submenu_id);
6378#ifdef FEAT_TEAROFF
6379 if (IsWindow(menu->tearoff_handle))
6380 DestroyWindow(menu->tearoff_handle);
6381 if (menu->parent != NULL
6382 && menu->parent->children != NULL
6383 && IsWindow(menu->parent->tearoff_handle))
6384 {
6385 /* This menu must not show up when rebuilding the tearoff window. */
6386 menu->modes = 0;
6387 rebuild_tearoff(menu->parent);
6388 }
6389#endif
6390 }
6391}
6392
6393#ifdef FEAT_TEAROFF
6394 static void
6395rebuild_tearoff(vimmenu_T *menu)
6396{
6397 /*hackish*/
6398 char_u tbuf[128];
6399 RECT trect;
6400 RECT rct;
6401 RECT roct;
6402 int x, y;
6403
6404 HWND thwnd = menu->tearoff_handle;
6405
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006406 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (GetWindowRect(thwnd, &trect)
6408 && GetWindowRect(s_hwnd, &rct)
6409 && GetClientRect(s_hwnd, &roct))
6410 {
6411 x = trect.left - rct.left;
6412 y = (trect.top - rct.bottom + roct.bottom);
6413 }
6414 else
6415 {
6416 x = y = 0xffffL;
6417 }
6418 DestroyWindow(thwnd);
6419 if (menu->children != NULL)
6420 {
6421 gui_mch_tearoff(tbuf, menu, x, y);
6422 if (IsWindow(menu->tearoff_handle))
6423 (void) SetWindowPos(menu->tearoff_handle,
6424 NULL,
6425 (int)trect.left,
6426 (int)trect.top,
6427 0, 0,
6428 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6429 }
6430}
6431#endif /* FEAT_TEAROFF */
6432
6433/*
6434 * Make a menu either grey or not grey.
6435 */
6436 void
6437gui_mch_menu_grey(
6438 vimmenu_T *menu,
6439 int grey)
6440{
6441#ifdef FEAT_TOOLBAR
6442 /*
6443 * is this a toolbar button?
6444 */
6445 if (menu->submenu_id == (HMENU)-1)
6446 {
6447 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6448 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6449 }
6450 else
6451#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006452 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6453 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455#ifdef FEAT_TEAROFF
6456 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6457 {
6458 WORD menuID;
6459 HWND menuHandle;
6460
6461 /*
6462 * A tearoff button has changed state.
6463 */
6464 if (menu->children == NULL)
6465 menuID = (WORD)(menu->id);
6466 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006467 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6469 if (menuHandle)
6470 EnableWindow(menuHandle, !grey);
6471
6472 }
6473#endif
6474}
6475
6476#endif /* FEAT_MENU */
6477
6478
6479/* define some macros used to make the dialogue creation more readable */
6480
6481#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6482#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006483#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
6485#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6486/*
6487 * stuff for dialogs
6488 */
6489
6490/*
6491 * The callback routine used by all the dialogs. Very simple. First,
6492 * acknowledges the INITDIALOG message so that Windows knows to do standard
6493 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6494 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6495 * number.
6496 */
6497 static LRESULT CALLBACK
6498dialog_callback(
6499 HWND hwnd,
6500 UINT message,
6501 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006502 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 if (message == WM_INITDIALOG)
6505 {
6506 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6507 /* Set focus to the dialog. Set the default button, if specified. */
6508 (void)SetFocus(hwnd);
6509 if (dialog_default_button > IDCANCEL)
6510 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006511 else
6512 /* We don't have a default, set focus on another element of the
6513 * dialog window, probably the icon */
6514 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 return FALSE;
6516 }
6517
6518 if (message == WM_COMMAND)
6519 {
6520 int button = LOWORD(wParam);
6521
6522 /* Don't end the dialog if something was selected that was
6523 * not a button.
6524 */
6525 if (button >= DLG_NONBUTTON_CONTROL)
6526 return TRUE;
6527
6528 /* If the edit box exists, copy the string. */
6529 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006530 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006531 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6532 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006533
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6535 p = utf16_to_enc(wp, NULL);
6536 vim_strncpy(s_textfield, p, IOSIZE);
6537 vim_free(p);
6538 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541 /*
6542 * Need to check for IDOK because if the user just hits Return to
6543 * accept the default value, some reason this is what we get.
6544 */
6545 if (button == IDOK)
6546 {
6547 if (dialog_default_button > IDCANCEL)
6548 EndDialog(hwnd, dialog_default_button);
6549 }
6550 else
6551 EndDialog(hwnd, button - IDCANCEL);
6552 return TRUE;
6553 }
6554
6555 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6556 {
6557 EndDialog(hwnd, 0);
6558 return TRUE;
6559 }
6560 return FALSE;
6561}
6562
6563/*
6564 * Create a dialog dynamically from the parameter strings.
6565 * type = type of dialog (question, alert, etc.)
6566 * title = dialog title. may be NULL for default title.
6567 * message = text to display. Dialog sizes to accommodate it.
6568 * buttons = '\n' separated list of button captions, default first.
6569 * dfltbutton = number of default button.
6570 *
6571 * This routine returns 1 if the first button is pressed,
6572 * 2 for the second, etc.
6573 *
6574 * 0 indicates Esc was pressed.
6575 * -1 for unexpected error
6576 *
6577 * If stubbing out this fn, return 1.
6578 */
6579
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006580static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 "IDR_VIM",
6583 "IDR_VIM_ERROR",
6584 "IDR_VIM_ALERT",
6585 "IDR_VIM_INFO",
6586 "IDR_VIM_QUESTION"
6587};
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 int
6590gui_mch_dialog(
6591 int type,
6592 char_u *title,
6593 char_u *message,
6594 char_u *buttons,
6595 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006596 char_u *textfield,
6597 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006600 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 int numButtons;
6602 int *buttonWidths, *buttonPositions;
6603 int buttonYpos;
6604 int nchar, i;
6605 DWORD lStyle;
6606 int dlgwidth = 0;
6607 int dlgheight;
6608 int editboxheight;
6609 int horizWidth = 0;
6610 int msgheight;
6611 char_u *pstart;
6612 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006613 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 char_u *tbuffer;
6615 RECT rect;
6616 HWND hwnd;
6617 HDC hdc;
6618 HFONT font, oldFont;
6619 TEXTMETRIC fontInfo;
6620 int fontHeight;
6621 int textWidth, minButtonWidth, messageWidth;
6622 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006623 int maxDialogHeight;
6624 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 int vertical;
6626 int dlgPaddingX;
6627 int dlgPaddingY;
6628#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006629 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int use_lfSysmenu = FALSE;
6631#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006632 garray_T ga;
6633 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635#ifndef NO_CONSOLE
6636 /* Don't output anything in silent mode ("ex -s") */
6637 if (silent_mode)
6638 return dfltbutton; /* return default option */
6639#endif
6640
Bram Moolenaar748bf032005-02-02 23:04:36 +00006641 if (s_hwnd == NULL)
6642 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643
6644 if ((type < 0) || (type > VIM_LAST_TYPE))
6645 type = 0;
6646
6647 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006648 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006649 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006650 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 if (p == NULL)
6653 return -1;
6654
6655 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006656 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6658 * const.
6659 */
6660 tbuffer = vim_strsave(buttons);
6661 if (tbuffer == NULL)
6662 return -1;
6663
6664 --dfltbutton; /* Change from one-based to zero-based */
6665
6666 /* Count buttons */
6667 numButtons = 1;
6668 for (i = 0; tbuffer[i] != '\0'; i++)
6669 {
6670 if (tbuffer[i] == DLG_BUTTON_SEP)
6671 numButtons++;
6672 }
6673 if (dfltbutton >= numButtons)
6674 dfltbutton = -1;
6675
6676 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006677 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if (buttonWidths == NULL)
6679 return -1;
6680
6681 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006682 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 if (buttonPositions == NULL)
6684 return -1;
6685
6686 /*
6687 * Calculate how big the dialog must be.
6688 */
6689 hwnd = GetDesktopWindow();
6690 hdc = GetWindowDC(hwnd);
6691#ifdef USE_SYSMENU_FONT
6692 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6693 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006694 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 use_lfSysmenu = TRUE;
6696 }
6697 else
6698#endif
6699 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6700 VARIABLE_PITCH , DLG_FONT_NAME);
6701 if (s_usenewlook)
6702 {
6703 oldFont = SelectFont(hdc, font);
6704 dlgPaddingX = DLG_PADDING_X;
6705 dlgPaddingY = DLG_PADDING_Y;
6706 }
6707 else
6708 {
6709 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6710 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6711 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6712 }
6713 GetTextMetrics(hdc, &fontInfo);
6714 fontHeight = fontInfo.tmHeight;
6715
6716 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006717 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006720 if (s_hwnd == NULL)
6721 {
6722 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
Bram Moolenaarc716c302006-01-21 22:12:51 +00006724 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006725 get_work_area(&workarea_rect);
6726 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6727 if (maxDialogWidth > 600)
6728 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006729 /* Leave some room for the taskbar. */
6730 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006731 }
6732 else
6733 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006734 /* Use our own window for the size, unless it's very small. */
6735 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006736 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006737 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006738 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006739 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6740 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006741
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006742 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006743 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006744 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006745 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006746 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6747 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6748 }
6749
6750 /* Set dlgwidth to width of message.
6751 * Copy the message into "ga", changing NL to CR-NL and inserting line
6752 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 pstart = message;
6754 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006755 msgheight = 0;
6756 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 do
6758 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006759 msgheight += fontHeight; /* at least one line */
6760
6761 /* Need to figure out where to break the string. The system does it
6762 * at a word boundary, which would mean we can't compute the number of
6763 * wrapped lines. */
6764 textWidth = 0;
6765 last_white = NULL;
6766 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006768 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006769 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006770 && textWidth > maxDialogWidth * 3 / 4)
6771 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006772 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006773 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006774 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006775 /* Line will wrap. */
6776 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006777 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006778 textWidth = 0;
6779
6780 if (last_white != NULL)
6781 {
6782 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006783 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006784 pend = last_white + 1;
6785 last_white = NULL;
6786 }
6787 ga_append(&ga, '\r');
6788 ga_append(&ga, '\n');
6789 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006790 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006791
6792 while (--l >= 0)
6793 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006794 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006795 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006797
6798 ga_append(&ga, '\r');
6799 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 pstart = pend + 1;
6801 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006802
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006803 if (ga.ga_data != NULL)
6804 message = ga.ga_data;
6805
Bram Moolenaar748bf032005-02-02 23:04:36 +00006806 messageWidth += 10; /* roundoff space */
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006809 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6810 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
6812 if (msgheight < DLG_ICON_HEIGHT)
6813 msgheight = DLG_ICON_HEIGHT;
6814
6815 /*
6816 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006817 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006819 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (!vertical)
6821 {
6822 // Place buttons horizontally if they fit.
6823 horizWidth = dlgPaddingX;
6824 pstart = tbuffer;
6825 i = 0;
6826 do
6827 {
6828 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6829 if (pend == NULL)
6830 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006831 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (textWidth < minButtonWidth)
6833 textWidth = minButtonWidth;
6834 textWidth += dlgPaddingX; /* Padding within button */
6835 buttonWidths[i] = textWidth;
6836 buttonPositions[i++] = horizWidth;
6837 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6838 pstart = pend + 1;
6839 } while (*pend != NUL);
6840
6841 if (horizWidth > maxDialogWidth)
6842 vertical = TRUE; // Too wide to fit on the screen.
6843 else if (horizWidth > dlgwidth)
6844 dlgwidth = horizWidth;
6845 }
6846
6847 if (vertical)
6848 {
6849 // Stack buttons vertically.
6850 pstart = tbuffer;
6851 do
6852 {
6853 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6854 if (pend == NULL)
6855 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006856 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 textWidth += dlgPaddingX; /* Padding within button */
6858 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
6859 if (textWidth > dlgwidth)
6860 dlgwidth = textWidth;
6861 pstart = pend + 1;
6862 } while (*pend != NUL);
6863 }
6864
6865 if (dlgwidth < DLG_MIN_WIDTH)
6866 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
6867
6868 /* start to fill in the dlgtemplate information. addressing by WORDs */
6869 if (s_usenewlook)
6870 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
6871 else
6872 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
6873
6874 add_long(lStyle);
6875 add_long(0); // (lExtendedStyle)
6876 pnumitems = p; /*save where the number of items must be stored*/
6877 add_word(0); // NumberOfItems(will change later)
6878 add_word(10); // x
6879 add_word(10); // y
6880 add_word(PixelToDialogX(dlgwidth)); // cx
6881
6882 // Dialog height.
6883 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006884 dlgheight = msgheight + 2 * dlgPaddingY
6885 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 else
6887 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6888
6889 // Dialog needs to be taller if contains an edit box.
6890 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6891 if (textfield != NULL)
6892 dlgheight += editboxheight;
6893
Bram Moolenaara95d8232013-08-07 15:27:11 +02006894 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
6895 if (dlgheight > maxDialogHeight)
6896 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006897 msgheight = msgheight - (dlgheight - maxDialogHeight);
6898 dlgheight = maxDialogHeight;
6899 scroll_flag = WS_VSCROLL;
6900 /* Make sure scrollbar doesn't appear in the middle of the dialog */
6901 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006902 }
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 add_word(PixelToDialogY(dlgheight));
6905
6906 add_word(0); // Menu
6907 add_word(0); // Class
6908
6909 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006910 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6911 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 p += nchar;
6913
6914 if (s_usenewlook)
6915 {
6916 /* do the font, since DS_3DLOOK doesn't work properly */
6917#ifdef USE_SYSMENU_FONT
6918 if (use_lfSysmenu)
6919 {
6920 /* point size */
6921 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6922 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006923 wcscpy(p, lfSysmenu.lfFaceName);
6924 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 }
6926 else
6927#endif
6928 {
6929 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006930 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932 p += nchar;
6933 }
6934
6935 buttonYpos = msgheight + 2 * dlgPaddingY;
6936
6937 if (textfield != NULL)
6938 buttonYpos += editboxheight;
6939
6940 pstart = tbuffer;
6941 if (!vertical)
6942 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
6943 for (i = 0; i < numButtons; i++)
6944 {
6945 /* get end of this button. */
6946 for ( pend = pstart;
6947 *pend && (*pend != DLG_BUTTON_SEP);
6948 pend++)
6949 ;
6950
6951 if (*pend)
6952 *pend = '\0';
6953
6954 /*
6955 * old NOTE:
6956 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6957 * the focus to the first tab-able button and in so doing makes that
6958 * the default!! Grrr. Workaround: Make the default button the only
6959 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6960 * he/she can use arrow keys.
6961 *
6962 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006963 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 * dialog. Also needed for when the textfield is the default control.
6965 * It appears to work now (perhaps not on Win95?).
6966 */
6967 if (vertical)
6968 {
6969 p = add_dialog_element(p,
6970 (i == dfltbutton
6971 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6972 PixelToDialogX(DLG_VERT_PADDING_X),
6973 PixelToDialogY(buttonYpos /* TBK */
6974 + 2 * fontHeight * i),
6975 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
6976 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006977 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 }
6979 else
6980 {
6981 p = add_dialog_element(p,
6982 (i == dfltbutton
6983 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6984 PixelToDialogX(horizWidth + buttonPositions[i]),
6985 PixelToDialogY(buttonYpos), /* TBK */
6986 PixelToDialogX(buttonWidths[i]),
6987 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006988 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 }
6990 pstart = pend + 1; /*next button*/
6991 }
6992 *pnumitems += numButtons;
6993
6994 /* Vim icon */
6995 p = add_dialog_element(p, SS_ICON,
6996 PixelToDialogX(dlgPaddingX),
6997 PixelToDialogY(dlgPaddingY),
6998 PixelToDialogX(DLG_ICON_WIDTH),
6999 PixelToDialogY(DLG_ICON_HEIGHT),
7000 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7001 dlg_icons[type]);
7002
Bram Moolenaar748bf032005-02-02 23:04:36 +00007003 /* Dialog message */
7004 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7005 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7006 PixelToDialogY(dlgPaddingY),
7007 (WORD)(PixelToDialogX(messageWidth) + 1),
7008 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007009 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 /* Edit box */
7012 if (textfield != NULL)
7013 {
7014 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7015 PixelToDialogX(2 * dlgPaddingX),
7016 PixelToDialogY(2 * dlgPaddingY + msgheight),
7017 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7018 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007019 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 *pnumitems += 1;
7021 }
7022
7023 *pnumitems += 2;
7024
7025 SelectFont(hdc, oldFont);
7026 DeleteObject(font);
7027 ReleaseDC(hwnd, hdc);
7028
7029 /* Let the dialog_callback() function know which button to make default
7030 * If we have an edit box, make that the default. We also need to tell
7031 * dialog_callback() if this dialog contains an edit box or not. We do
7032 * this by setting s_textfield if it does.
7033 */
7034 if (textfield != NULL)
7035 {
7036 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7037 s_textfield = textfield;
7038 }
7039 else
7040 {
7041 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7042 s_textfield = NULL;
7043 }
7044
7045 /* show the dialog box modally and get a return value */
7046 nchar = (int)DialogBoxIndirect(
7047 s_hinst,
7048 (LPDLGTEMPLATE)pdlgtemplate,
7049 s_hwnd,
7050 (DLGPROC)dialog_callback);
7051
7052 LocalFree(LocalHandle(pdlgtemplate));
7053 vim_free(tbuffer);
7054 vim_free(buttonWidths);
7055 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007056 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057
7058 /* Focus back to our window (for when MDI is used). */
7059 (void)SetFocus(s_hwnd);
7060
7061 return nchar;
7062}
7063
7064#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066/*
7067 * Put a simple element (basic class) onto a dialog template in memory.
7068 * return a pointer to where the next item should be added.
7069 *
7070 * parameters:
7071 * lStyle = additional style flags
7072 * (be careful, NT3.51 & Win32s will ignore the new ones)
7073 * x,y = x & y positions IN DIALOG UNITS
7074 * w,h = width and height IN DIALOG UNITS
7075 * Id = ID used in messages
7076 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7077 * caption = usually text or resource name
7078 *
7079 * TODO: use the length information noted here to enable the dialog creation
7080 * routines to work out more exactly how much memory they need to alloc.
7081 */
7082 static PWORD
7083add_dialog_element(
7084 PWORD p,
7085 DWORD lStyle,
7086 WORD x,
7087 WORD y,
7088 WORD w,
7089 WORD h,
7090 WORD Id,
7091 WORD clss,
7092 const char *caption)
7093{
7094 int nchar;
7095
7096 p = lpwAlign(p); /* Align to dword boundary*/
7097 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7098 *p++ = LOWORD(lStyle);
7099 *p++ = HIWORD(lStyle);
7100 *p++ = 0; // LOWORD (lExtendedStyle)
7101 *p++ = 0; // HIWORD (lExtendedStyle)
7102 *p++ = x;
7103 *p++ = y;
7104 *p++ = w;
7105 *p++ = h;
7106 *p++ = Id; //9 or 10 words in all
7107
7108 *p++ = (WORD)0xffff;
7109 *p++ = clss; //2 more here
7110
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007111 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 p += nchar;
7113
7114 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7115
7116 return p; //total = 15+ (strlen(caption)) words
7117 // = 30 + 2(strlen(caption) bytes reqd
7118}
7119
7120
7121/*
7122 * Helper routine. Take an input pointer, return closest pointer that is
7123 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7124 */
7125 static LPWORD
7126lpwAlign(
7127 LPWORD lpIn)
7128{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007129 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007131 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 ul += 3;
7133 ul >>= 2;
7134 ul <<= 2;
7135 return (LPWORD)ul;
7136}
7137
7138/*
7139 * Helper routine. Takes second parameter as Ansi string, copies it to first
7140 * parameter as wide character (16-bits / char) string, and returns integer
7141 * number of wide characters (words) in string (including the trailing wide
7142 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007143 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7144 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 static int
7146nCopyAnsiToWideChar(
7147 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007148 LPSTR lpAnsiIn,
7149 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7153 int i;
7154 WCHAR *wn;
7155
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007156 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 {
7158 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007159 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (wn != NULL)
7161 {
7162 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007163 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 vim_free(wn);
7165 }
7166 }
7167 if (nChar == 0)
7168 /* Use Win32 conversion function. */
7169 nChar = MultiByteToWideChar(
7170 enc_codepage > 0 ? enc_codepage : CP_ACP,
7171 MB_PRECOMPOSED,
7172 lpAnsiIn, len,
7173 lpWCStr, len);
7174 for (i = 0; i < nChar; ++i)
7175 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7176 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
7178 return nChar;
7179}
7180
7181
7182#ifdef FEAT_TEAROFF
7183/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007184 * Lookup menu handle from "menu_id".
7185 */
7186 static HMENU
7187tearoff_lookup_menuhandle(
7188 vimmenu_T *menu,
7189 WORD menu_id)
7190{
7191 for ( ; menu != NULL; menu = menu->next)
7192 {
7193 if (menu->modes == 0) /* this menu has just been deleted */
7194 continue;
7195 if (menu_is_separator(menu->dname))
7196 continue;
7197 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7198 return menu->submenu_id;
7199 }
7200 return NULL;
7201}
7202
7203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 * The callback function for all the modeless dialogs that make up the
7205 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7206 * thinking its menus have been clicked), and go away when closed.
7207 */
7208 static LRESULT CALLBACK
7209tearoff_callback(
7210 HWND hwnd,
7211 UINT message,
7212 WPARAM wParam,
7213 LPARAM lParam)
7214{
7215 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007216 {
7217 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 /* May show the mouse pointer again. */
7222 HandleMouseHide(message, lParam);
7223
7224 if (message == WM_COMMAND)
7225 {
7226 if ((WORD)(LOWORD(wParam)) & 0x8000)
7227 {
7228 POINT mp;
7229 RECT rect;
7230
7231 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7232 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007233 vimmenu_T *menu;
7234
7235 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007237 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7239 (int)rect.right - 8,
7240 (int)mp.y,
7241 (int)0, /*reserved param*/
7242 s_hwnd,
7243 NULL);
7244 /*
7245 * NOTE: The pop-up menu can eat the mouse up event.
7246 * We deal with this in normal.c.
7247 */
7248 }
7249 }
7250 else
7251 /* Pass on messages to the main Vim window */
7252 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7253 /*
7254 * Give main window the focus back: this is so after
7255 * choosing a tearoff button you can start typing again
7256 * straight away.
7257 */
7258 (void)SetFocus(s_hwnd);
7259 return TRUE;
7260 }
7261 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7262 {
7263 DestroyWindow(hwnd);
7264 return TRUE;
7265 }
7266
7267 /* When moved around, give main window the focus back. */
7268 if (message == WM_EXITSIZEMOVE)
7269 (void)SetActiveWindow(s_hwnd);
7270
7271 return FALSE;
7272}
7273#endif
7274
7275
7276/*
7277 * Decide whether to use the "new look" (small, non-bold font) or the "old
7278 * look" (big, clanky font) for dialogs, and work out a few values for use
7279 * later accordingly.
7280 */
7281 static void
7282get_dialog_font_metrics(void)
7283{
7284 HDC hdc;
7285 HFONT hfontTools = 0;
7286 DWORD dlgFontSize;
7287 SIZE size;
7288#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007289 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290#endif
7291
7292 s_usenewlook = FALSE;
7293
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007295 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007296 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298#endif
7299 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7300 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7301
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007302 if (hfontTools)
7303 {
7304 hdc = GetDC(s_hwnd);
7305 SelectObject(hdc, hfontTools);
7306 /*
7307 * GetTextMetrics() doesn't return the right value in
7308 * tmAveCharWidth, so we have to figure out the dialog base units
7309 * ourselves.
7310 */
7311 GetTextExtentPoint(hdc,
7312 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7313 52, &size);
7314 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007316 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7317 s_dlgfntheight = (WORD)size.cy;
7318 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 }
7320
7321 if (!s_usenewlook)
7322 {
7323 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7324 s_dlgfntwidth = LOWORD(dlgFontSize);
7325 s_dlgfntheight = HIWORD(dlgFontSize);
7326 }
7327}
7328
7329#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7330/*
7331 * Create a pseudo-"tearoff menu" based on the child
7332 * items of a given menu pointer.
7333 */
7334 static void
7335gui_mch_tearoff(
7336 char_u *title,
7337 vimmenu_T *menu,
7338 int initX,
7339 int initY)
7340{
7341 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7342 int template_len;
7343 int nchar, textWidth, submenuWidth;
7344 DWORD lStyle;
7345 DWORD lExtendedStyle;
7346 WORD dlgwidth;
7347 WORD menuID;
7348 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007349 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 vimmenu_T *the_menu = menu;
7351 HWND hwnd;
7352 HDC hdc;
7353 HFONT font, oldFont;
7354 int col, spaceWidth, len;
7355 int columnWidths[2];
7356 char_u *label, *text;
7357 int acLen = 0;
7358 int nameLen;
7359 int padding0, padding1, padding2 = 0;
7360 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007361 int x;
7362 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007364 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 int use_lfSysmenu = FALSE;
7366#endif
7367
7368 /*
7369 * If this menu is already torn off, move it to the mouse position.
7370 */
7371 if (IsWindow(menu->tearoff_handle))
7372 {
7373 POINT mp;
7374 if (GetCursorPos((LPPOINT)&mp))
7375 {
7376 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7377 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7378 }
7379 return;
7380 }
7381
7382 /*
7383 * Create a new tearoff.
7384 */
7385 if (*title == MNU_HIDDEN_CHAR)
7386 title++;
7387
7388 /* Allocate memory to store the dialog template. It's made bigger when
7389 * needed. */
7390 template_len = DLG_ALLOC_SIZE;
7391 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7392 if (p == NULL)
7393 return;
7394
7395 hwnd = GetDesktopWindow();
7396 hdc = GetWindowDC(hwnd);
7397#ifdef USE_SYSMENU_FONT
7398 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7399 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007400 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 use_lfSysmenu = TRUE;
7402 }
7403 else
7404#endif
7405 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7406 VARIABLE_PITCH , DLG_FONT_NAME);
7407 if (s_usenewlook)
7408 oldFont = SelectFont(hdc, font);
7409 else
7410 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7411
7412 /* Calculate width of a single space. Used for padding columns to the
7413 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007414 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415
7416 /* Figure out max width of the text column, the accelerator column and the
7417 * optional submenu column. */
7418 submenuWidth = 0;
7419 for (col = 0; col < 2; col++)
7420 {
7421 columnWidths[col] = 0;
7422 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7423 {
7424 /* Use "dname" here to compute the width of the visible text. */
7425 text = (col == 0) ? pmenu->dname : pmenu->actext;
7426 if (text != NULL && *text != NUL)
7427 {
7428 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7429 if (textWidth > columnWidths[col])
7430 columnWidths[col] = textWidth;
7431 }
7432 if (pmenu->children != NULL)
7433 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7434 }
7435 }
7436 if (columnWidths[1] == 0)
7437 {
7438 /* no accelerators */
7439 if (submenuWidth != 0)
7440 columnWidths[0] += submenuWidth;
7441 else
7442 columnWidths[0] += spaceWidth;
7443 }
7444 else
7445 {
7446 /* there is an accelerator column */
7447 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7448 columnWidths[1] += submenuWidth;
7449 }
7450
7451 /*
7452 * Now find the total width of our 'menu'.
7453 */
7454 textWidth = columnWidths[0] + columnWidths[1];
7455 if (submenuWidth != 0)
7456 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007457 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7459 textWidth += submenuWidth;
7460 }
7461 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7462 if (textWidth > dlgwidth)
7463 dlgwidth = textWidth;
7464 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7465
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 /* start to fill in the dlgtemplate information. addressing by WORDs */
7467 if (s_usenewlook)
7468 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7469 else
7470 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7471
7472 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7473 *p++ = LOWORD(lStyle);
7474 *p++ = HIWORD(lStyle);
7475 *p++ = LOWORD(lExtendedStyle);
7476 *p++ = HIWORD(lExtendedStyle);
7477 pnumitems = p; /* save where the number of items must be stored */
7478 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007479 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007481 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 else
7483 *p++ = PixelToDialogX(initX); // x
7484 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007485 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 else
7487 *p++ = PixelToDialogY(initY); // y
7488 *p++ = PixelToDialogX(dlgwidth); // cx
7489 ptrueheight = p;
7490 *p++ = 0; // dialog height: changed later anyway
7491 *p++ = 0; // Menu
7492 *p++ = 0; // Class
7493
7494 /* copy the title of the dialog */
7495 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007496 ? (LPSTR)title
7497 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 p += nchar;
7499
7500 if (s_usenewlook)
7501 {
7502 /* do the font, since DS_3DLOOK doesn't work properly */
7503#ifdef USE_SYSMENU_FONT
7504 if (use_lfSysmenu)
7505 {
7506 /* point size */
7507 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7508 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007509 wcscpy(p, lfSysmenu.lfFaceName);
7510 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 else
7513#endif
7514 {
7515 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007516 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518 p += nchar;
7519 }
7520
7521 /*
7522 * Loop over all the items in the menu.
7523 * But skip over the tearbar.
7524 */
7525 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7526 menu = menu->children->next;
7527 else
7528 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007529 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 for ( ; menu != NULL; menu = menu->next)
7531 {
7532 if (menu->modes == 0) /* this menu has just been deleted */
7533 continue;
7534 if (menu_is_separator(menu->dname))
7535 {
7536 sepPadding += 3;
7537 continue;
7538 }
7539
7540 /* Check if there still is plenty of room in the template. Make it
7541 * larger when needed. */
7542 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7543 {
7544 WORD *newp;
7545
7546 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7547 if (newp != NULL)
7548 {
7549 template_len += 4096;
7550 mch_memmove(newp, pdlgtemplate,
7551 (char *)p - (char *)pdlgtemplate);
7552 p = newp + (p - pdlgtemplate);
7553 pnumitems = newp + (pnumitems - pdlgtemplate);
7554 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7555 LocalFree(LocalHandle(pdlgtemplate));
7556 pdlgtemplate = newp;
7557 }
7558 }
7559
7560 /* Figure out minimal length of this menu label. Use "name" for the
7561 * actual text, "dname" for estimating the displayed size. "name"
7562 * has "&a" for mnemonic and includes the accelerator. */
7563 len = nameLen = (int)STRLEN(menu->name);
7564 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7565 (int)STRLEN(menu->dname))) / spaceWidth;
7566 len += padding0;
7567
7568 if (menu->actext != NULL)
7569 {
7570 acLen = (int)STRLEN(menu->actext);
7571 len += acLen;
7572 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7573 }
7574 else
7575 textWidth = 0;
7576 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7577 len += padding1;
7578
7579 if (menu->children == NULL)
7580 {
7581 padding2 = submenuWidth / spaceWidth;
7582 len += padding2;
7583 menuID = (WORD)(menu->id);
7584 }
7585 else
7586 {
7587 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007588 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590
7591 /* Allocate menu label and fill it in */
7592 text = label = alloc((unsigned)len + 1);
7593 if (label == NULL)
7594 break;
7595
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007596 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7598 if (text == NULL)
7599 text = label + nameLen; /* no actext, use whole name */
7600 while (padding0-- > 0)
7601 *text++ = ' ';
7602 if (menu->actext != NULL)
7603 {
7604 STRNCPY(text, menu->actext, acLen);
7605 text += acLen;
7606 }
7607 while (padding1-- > 0)
7608 *text++ = ' ';
7609 if (menu->children != NULL)
7610 {
7611 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7612 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7613 }
7614 else
7615 {
7616 while (padding2-- > 0)
7617 *text++ = ' ';
7618 }
7619 *text = NUL;
7620
7621 /*
7622 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7623 * W95/NT4 it makes the tear-off look more like a menu.
7624 */
7625 p = add_dialog_element(p,
7626 BS_PUSHBUTTON|BS_LEFT,
7627 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7628 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7629 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7630 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007631 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 vim_free(label);
7633 (*pnumitems)++;
7634 }
7635
7636 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7637
7638
7639 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007640 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 s_hinst,
7642 (LPDLGTEMPLATE)pdlgtemplate,
7643 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007644 (DLGPROC)tearoff_callback,
7645 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
7647 LocalFree(LocalHandle(pdlgtemplate));
7648 SelectFont(hdc, oldFont);
7649 DeleteObject(font);
7650 ReleaseDC(hwnd, hdc);
7651
7652 /*
7653 * Reassert ourselves as the active window. This is so that after creating
7654 * a tearoff, the user doesn't have to click with the mouse just to start
7655 * typing again!
7656 */
7657 (void)SetActiveWindow(s_hwnd);
7658
7659 /* make sure the right buttons are enabled */
7660 force_menu_update = TRUE;
7661}
7662#endif
7663
7664#if defined(FEAT_TOOLBAR) || defined(PROTO)
7665#include "gui_w32_rc.h"
7666
7667/* This not defined in older SDKs */
7668# ifndef TBSTYLE_FLAT
7669# define TBSTYLE_FLAT 0x0800
7670# endif
7671
7672/*
7673 * Create the toolbar, initially unpopulated.
7674 * (just like the menu, there are no defaults, it's all
7675 * set up through menu.vim)
7676 */
7677 static void
7678initialise_toolbar(void)
7679{
7680 InitCommonControls();
7681 s_toolbarhwnd = CreateToolbarEx(
7682 s_hwnd,
7683 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7684 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007685 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 s_hinst,
7687 IDR_TOOLBAR1, // id of initial bitmap
7688 NULL,
7689 0, // initial number of buttons
7690 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7691 TOOLBAR_BUTTON_HEIGHT,
7692 TOOLBAR_BUTTON_WIDTH,
7693 TOOLBAR_BUTTON_HEIGHT,
7694 sizeof(TBBUTTON)
7695 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007696 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
7698 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7699}
7700
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007701 static LRESULT CALLBACK
7702toolbar_wndproc(
7703 HWND hwnd,
7704 UINT uMsg,
7705 WPARAM wParam,
7706 LPARAM lParam)
7707{
7708 HandleMouseHide(uMsg, lParam);
7709 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7710}
7711
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 static int
7713get_toolbar_bitmap(vimmenu_T *menu)
7714{
7715 int i = -1;
7716
7717 /*
7718 * Check user bitmaps first, unless builtin is specified.
7719 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007720 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 {
7722 char_u fname[MAXPATHL];
7723 HANDLE hbitmap = NULL;
7724
7725 if (menu->iconfile != NULL)
7726 {
7727 gui_find_iconfile(menu->iconfile, fname, "bmp");
7728 hbitmap = LoadImage(
7729 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007730 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 IMAGE_BITMAP,
7732 TOOLBAR_BUTTON_WIDTH,
7733 TOOLBAR_BUTTON_HEIGHT,
7734 LR_LOADFROMFILE |
7735 LR_LOADMAP3DCOLORS
7736 );
7737 }
7738
7739 /*
7740 * If the LoadImage call failed, or the "icon=" file
7741 * didn't exist or wasn't specified, try the menu name
7742 */
7743 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007744 && (gui_find_bitmap(
7745#ifdef FEAT_MULTI_LANG
7746 menu->en_dname != NULL ? menu->en_dname :
7747#endif
7748 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 hbitmap = LoadImage(
7750 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007751 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 IMAGE_BITMAP,
7753 TOOLBAR_BUTTON_WIDTH,
7754 TOOLBAR_BUTTON_HEIGHT,
7755 LR_LOADFROMFILE |
7756 LR_LOADMAP3DCOLORS
7757 );
7758
7759 if (hbitmap != NULL)
7760 {
7761 TBADDBITMAP tbAddBitmap;
7762
7763 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007764 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765
7766 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7767 (WPARAM)1, (LPARAM)&tbAddBitmap);
7768 /* i will be set to -1 if it fails */
7769 }
7770 }
7771 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7772 i = menu->iconidx;
7773
7774 return i;
7775}
7776#endif
7777
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007778#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7779 static void
7780initialise_tabline(void)
7781{
7782 InitCommonControls();
7783
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007784 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007785 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007786 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
7787 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007788 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007789
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007790 gui.tabline_height = TABLINE_HEIGHT;
7791
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007792# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007793 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007794# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007795}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007796
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007797/*
7798 * Get tabpage_T from POINT.
7799 */
7800 static tabpage_T *
7801GetTabFromPoint(
7802 HWND hWnd,
7803 POINT pt)
7804{
7805 tabpage_T *ptp = NULL;
7806
7807 if (gui_mch_showing_tabline())
7808 {
7809 TCHITTESTINFO htinfo;
7810 htinfo.pt = pt;
7811 /* ignore if a window under cusor is not tabcontrol. */
7812 if (s_tabhwnd == hWnd)
7813 {
7814 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7815 if (idx != -1)
7816 ptp = find_tabpage(idx + 1);
7817 }
7818 }
7819 return ptp;
7820}
7821
7822static POINT s_pt = {0, 0};
7823static HCURSOR s_hCursor = NULL;
7824
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007825 static LRESULT CALLBACK
7826tabline_wndproc(
7827 HWND hwnd,
7828 UINT uMsg,
7829 WPARAM wParam,
7830 LPARAM lParam)
7831{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007832 POINT pt;
7833 tabpage_T *tp;
7834 RECT rect;
7835 int nCenter;
7836 int idx0;
7837 int idx1;
7838
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007839 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007840
7841 switch (uMsg)
7842 {
7843 case WM_LBUTTONDOWN:
7844 {
7845 s_pt.x = GET_X_LPARAM(lParam);
7846 s_pt.y = GET_Y_LPARAM(lParam);
7847 SetCapture(hwnd);
7848 s_hCursor = GetCursor(); /* backup default cursor */
7849 break;
7850 }
7851 case WM_MOUSEMOVE:
7852 if (GetCapture() == hwnd
7853 && ((wParam & MK_LBUTTON)) != 0)
7854 {
7855 pt.x = GET_X_LPARAM(lParam);
7856 pt.y = s_pt.y;
7857 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
7858 {
7859 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7860
7861 tp = GetTabFromPoint(hwnd, pt);
7862 if (tp != NULL)
7863 {
7864 idx0 = tabpage_index(curtab) - 1;
7865 idx1 = tabpage_index(tp) - 1;
7866
7867 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7868 nCenter = rect.left + (rect.right - rect.left) / 2;
7869
7870 /* Check if the mouse cursor goes over the center of
7871 * the next tab to prevent "flickering". */
7872 if ((idx0 < idx1) && (nCenter < pt.x))
7873 {
7874 tabpage_move(idx1 + 1);
7875 update_screen(0);
7876 }
7877 else if ((idx1 < idx0) && (pt.x < nCenter))
7878 {
7879 tabpage_move(idx1);
7880 update_screen(0);
7881 }
7882 }
7883 }
7884 }
7885 break;
7886 case WM_LBUTTONUP:
7887 {
7888 if (GetCapture() == hwnd)
7889 {
7890 SetCursor(s_hCursor);
7891 ReleaseCapture();
7892 }
7893 break;
7894 }
7895 default:
7896 break;
7897 }
7898
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007899 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7900}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007901#endif
7902
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7904/*
7905 * Make the GUI window come to the foreground.
7906 */
7907 void
7908gui_mch_set_foreground(void)
7909{
7910 if (IsIconic(s_hwnd))
7911 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7912 SetForegroundWindow(s_hwnd);
7913}
7914#endif
7915
7916#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7917 static void
7918dyn_imm_load(void)
7919{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007920 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 if (hLibImm == NULL)
7922 return;
7923
7924 pImmGetCompositionStringA
7925 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
7926 pImmGetCompositionStringW
7927 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7928 pImmGetContext
7929 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7930 pImmAssociateContext
7931 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7932 pImmReleaseContext
7933 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7934 pImmGetOpenStatus
7935 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7936 pImmSetOpenStatus
7937 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007938 pImmGetCompositionFontW
7939 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7940 pImmSetCompositionFontW
7941 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 pImmSetCompositionWindow
7943 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7944 pImmGetConversionStatus
7945 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007946 pImmSetConversionStatus
7947 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
7949 if ( pImmGetCompositionStringA == NULL
7950 || pImmGetCompositionStringW == NULL
7951 || pImmGetContext == NULL
7952 || pImmAssociateContext == NULL
7953 || pImmReleaseContext == NULL
7954 || pImmGetOpenStatus == NULL
7955 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007956 || pImmGetCompositionFontW == NULL
7957 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007959 || pImmGetConversionStatus == NULL
7960 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
7962 FreeLibrary(hLibImm);
7963 hLibImm = NULL;
7964 pImmGetContext = NULL;
7965 return;
7966 }
7967
7968 return;
7969}
7970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#endif
7972
7973#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7974
7975# ifdef FEAT_XPM_W32
7976# define IMAGE_XPM 100
7977# endif
7978
7979typedef struct _signicon_t
7980{
7981 HANDLE hImage;
7982 UINT uType;
7983#ifdef FEAT_XPM_W32
7984 HANDLE hShape; /* Mask bitmap handle */
7985#endif
7986} signicon_t;
7987
7988 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01007989gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 signicon_t *sign;
7992 int x, y, w, h;
7993
7994 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
7995 return;
7996
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01007997#if defined(FEAT_DIRECTX)
7998 if (IS_ENABLE_DIRECTX())
7999 DWriteContext_Flush(s_dwc);
8000#endif
8001
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 x = TEXT_X(col);
8003 y = TEXT_Y(row);
8004 w = gui.char_width * 2;
8005 h = gui.char_height;
8006 switch (sign->uType)
8007 {
8008 case IMAGE_BITMAP:
8009 {
8010 HDC hdcMem;
8011 HBITMAP hbmpOld;
8012
8013 hdcMem = CreateCompatibleDC(s_hdc);
8014 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8015 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8016 SelectObject(hdcMem, hbmpOld);
8017 DeleteDC(hdcMem);
8018 }
8019 break;
8020 case IMAGE_ICON:
8021 case IMAGE_CURSOR:
8022 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8023 break;
8024#ifdef FEAT_XPM_W32
8025 case IMAGE_XPM:
8026 {
8027 HDC hdcMem;
8028 HBITMAP hbmpOld;
8029
8030 hdcMem = CreateCompatibleDC(s_hdc);
8031 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8032 /* Make hole */
8033 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8034
8035 SelectObject(hdcMem, sign->hImage);
8036 /* Paint sign */
8037 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8038 SelectObject(hdcMem, hbmpOld);
8039 DeleteDC(hdcMem);
8040 }
8041 break;
8042#endif
8043 }
8044}
8045
8046 static void
8047close_signicon_image(signicon_t *sign)
8048{
8049 if (sign)
8050 switch (sign->uType)
8051 {
8052 case IMAGE_BITMAP:
8053 DeleteObject((HGDIOBJ)sign->hImage);
8054 break;
8055 case IMAGE_CURSOR:
8056 DestroyCursor((HCURSOR)sign->hImage);
8057 break;
8058 case IMAGE_ICON:
8059 DestroyIcon((HICON)sign->hImage);
8060 break;
8061#ifdef FEAT_XPM_W32
8062 case IMAGE_XPM:
8063 DeleteObject((HBITMAP)sign->hImage);
8064 DeleteObject((HBITMAP)sign->hShape);
8065 break;
8066#endif
8067 }
8068}
8069
8070 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008071gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 signicon_t sign, *psign;
8074 char_u *ext;
8075
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008077 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 if (ext > signfile)
8079 {
8080 int do_load = 1;
8081
8082 if (!STRICMP(ext, ".bmp"))
8083 sign.uType = IMAGE_BITMAP;
8084 else if (!STRICMP(ext, ".ico"))
8085 sign.uType = IMAGE_ICON;
8086 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8087 sign.uType = IMAGE_CURSOR;
8088 else
8089 do_load = 0;
8090
8091 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008092 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 gui.char_width * 2, gui.char_height,
8094 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8095#ifdef FEAT_XPM_W32
8096 if (!STRICMP(ext, ".xpm"))
8097 {
8098 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008099 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8100 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 }
8102#endif
8103 }
8104
8105 psign = NULL;
8106 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8107 != NULL)
8108 *psign = sign;
8109
8110 if (!psign)
8111 {
8112 if (sign.hImage)
8113 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008114 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 }
8116 return (void *)psign;
8117
8118}
8119
8120 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008121gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
8123 if (sign)
8124 {
8125 close_signicon_image((signicon_t *)sign);
8126 vim_free(sign);
8127 }
8128}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008131#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132
8133/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008134 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008136 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8138 * to get current mouse position).
8139 *
8140 * Trying to use as more Windows services as possible, and as less
8141 * IE version as possible :)).
8142 *
8143 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8144 * BalloonEval struct.
8145 * 2) Enable/Disable simply create/kill BalloonEval Timer
8146 * 3) When there was enough inactivity, timer procedure posts
8147 * async request to debugger
8148 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8149 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008150 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 */
8152
Bram Moolenaar45360022005-07-21 21:08:21 +00008153/*
8154 * determine whether installed Common Controls support multiline tooltips
8155 * (i.e. their version is >= 4.70
8156 */
8157 int
8158multiline_balloon_available(void)
8159{
8160 HINSTANCE hDll;
8161 static char comctl_dll[] = "comctl32.dll";
8162 static int multiline_tip = MAYBE;
8163
8164 if (multiline_tip != MAYBE)
8165 return multiline_tip;
8166
8167 hDll = GetModuleHandle(comctl_dll);
8168 if (hDll != NULL)
8169 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008170 DLLGETVERSIONPROC pGetVer;
8171 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008172
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008173 if (pGetVer != NULL)
8174 {
8175 DLLVERSIONINFO dvi;
8176 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008177
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008178 ZeroMemory(&dvi, sizeof(dvi));
8179 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008180
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008181 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008182
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008183 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008184 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008185 || (dvi.dwMajorVersion == 4
8186 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008187 {
8188 multiline_tip = TRUE;
8189 return multiline_tip;
8190 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008191 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008192 else
8193 {
8194 /* there is chance we have ancient CommCtl 4.70
8195 which doesn't export DllGetVersion */
8196 DWORD dwHandle = 0;
8197 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8198 if (len > 0)
8199 {
8200 VS_FIXEDFILEINFO *ver;
8201 UINT vlen = 0;
8202 void *data = alloc(len);
8203
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008204 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008205 && GetFileVersionInfo(comctl_dll, 0, len, data)
8206 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8207 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008208 && HIWORD(ver->dwFileVersionMS) > 4)
8209 || ((HIWORD(ver->dwFileVersionMS) == 4
8210 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008211 {
8212 vim_free(data);
8213 multiline_tip = TRUE;
8214 return multiline_tip;
8215 }
8216 vim_free(data);
8217 }
8218 }
8219 }
8220 multiline_tip = FALSE;
8221 return multiline_tip;
8222}
8223
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008224 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008225make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008226{
8227 TOOLINFOW *pti;
8228 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008229
8230 if (multiline_balloon_available() == TRUE)
8231 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8232 else
8233 ToolInfoSize = sizeof(TOOLINFOW);
8234
8235 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8236 if (pti == NULL)
8237 return;
8238
8239 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8240 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8241 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8242 beval->target, NULL, s_hinst, NULL);
8243
8244 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8245 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8246
8247 pti->cbSize = ToolInfoSize;
8248 pti->uFlags = TTF_SUBCLASS;
8249 pti->hwnd = beval->target;
8250 pti->hinst = 0; // Don't use string resources
8251 pti->uId = ID_BEVAL_TOOLTIP;
8252
8253 if (multiline_balloon_available() == TRUE)
8254 {
8255 RECT rect;
8256 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8257 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008258 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8259 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008260 // switch multiline tooltips on
8261 if (GetClientRect(s_textArea, &rect))
8262 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8263 (LPARAM)rect.right);
8264 }
8265 else
8266 {
8267 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008268 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8269 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008270 }
8271
8272 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8273 pti->rect.left = pt.x - 3;
8274 pti->rect.top = pt.y - 3;
8275 pti->rect.right = pt.x + 3;
8276 pti->rect.bottom = pt.y + 3;
8277
8278 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8279 // Make tooltip appear sooner.
8280 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8281 // I've performed some tests and it seems the longest possible life time
8282 // of tooltip is 30 seconds.
8283 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8284 /*
8285 * HACK: force tooltip to appear, because it'll not appear until
8286 * first mouse move. D*mn M$
8287 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8288 */
8289 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8290 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8291 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008292}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008295delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008297 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
8300 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008301BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008302 HWND hwnd UNUSED,
8303 UINT uMsg UNUSED,
8304 UINT_PTR idEvent UNUSED,
8305 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 POINT pt;
8308 RECT rect;
8309
8310 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8311 return;
8312
8313 GetCursorPos(&pt);
8314 if (WindowFromPoint(pt) != s_textArea)
8315 return;
8316
8317 ScreenToClient(s_textArea, &pt);
8318 GetClientRect(s_textArea, &rect);
8319 if (!PtInRect(&rect, pt))
8320 return;
8321
8322 if (LastActivity > 0
8323 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8324 && (cur_beval->showState != ShS_PENDING
8325 || abs(cur_beval->x - pt.x) > 3
8326 || abs(cur_beval->y - pt.y) > 3))
8327 {
8328 /* Pointer resting in one place long enough, it's time to show
8329 * the tooltip. */
8330 cur_beval->showState = ShS_PENDING;
8331 cur_beval->x = pt.x;
8332 cur_beval->y = pt.y;
8333
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008334 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
8336 if (cur_beval->msgCB != NULL)
8337 (*cur_beval->msgCB)(cur_beval, 0);
8338 }
8339}
8340
8341 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008342gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008344 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008346 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347}
8348
8349 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008350gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008352 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 if (beval == NULL)
8354 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008355 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008356 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008357 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358}
8359
8360 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008361gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
8363 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008364
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008365 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (beval->showState == ShS_SHOWING)
8367 return;
8368 GetCursorPos(&pt);
8369 ScreenToClient(s_textArea, &pt);
8370
8371 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008373 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 gui_mch_disable_beval_area(cur_beval);
8375 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008376 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008378 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379}
8380
8381 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008382gui_mch_create_beval_area(
8383 void *target, /* ignored, always use s_textArea */
8384 char_u *mesg,
8385 void (*mesgCB)(BalloonEval *, int),
8386 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
8388 /* partially stolen from gui_beval.c */
8389 BalloonEval *beval;
8390
8391 if (mesg != NULL && mesgCB != NULL)
8392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008393 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 return NULL;
8395 }
8396
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008397 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 if (beval != NULL)
8399 {
8400 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401
8402 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 beval->msg = mesg;
8404 beval->msgCB = mesgCB;
8405 beval->clientData = clientData;
8406
8407 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 cur_beval = beval;
8409
8410 if (p_beval)
8411 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413 return beval;
8414}
8415
8416 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008417Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8420 return;
8421
8422 if (cur_beval != NULL)
8423 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008424 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008426 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008427 // TRACE0("TTN_SHOW {{{");
8428 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008429 break;
8430 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008431 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 delete_tooltip(cur_beval);
8433 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008434 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008437 break;
8438 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008439 {
8440 /* if you get there then we have new common controls */
8441 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8442 info->lpszText = (LPSTR)info->lParam;
8443 info->uFlags |= TTF_DI_SETITEM;
8444 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008445 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008446 case TTN_GETDISPINFOW:
8447 {
8448 // if we get here then we have new common controls
8449 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8450 info->lpszText = (LPWSTR)info->lParam;
8451 info->uFlags |= TTF_DI_SETITEM;
8452 }
8453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 }
8455 }
8456}
8457
8458 static void
8459TrackUserActivity(UINT uMsg)
8460{
8461 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8462 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8463 LastActivity = GetTickCount();
8464}
8465
8466 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008467gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008469#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008470 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008471#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008472 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 vim_free(beval);
8474}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008475#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
8477#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8478/*
8479 * We have multiple signs to draw at the same location. Draw the
8480 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8481 */
8482 void
8483netbeans_draw_multisign_indicator(int row)
8484{
8485 int i;
8486 int y;
8487 int x;
8488
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008489 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008490 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 x = 0;
8493 y = TEXT_Y(row);
8494
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008495#if defined(FEAT_DIRECTX)
8496 if (IS_ENABLE_DIRECTX())
8497 DWriteContext_Flush(s_dwc);
8498#endif
8499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 for (i = 0; i < gui.char_height - 3; i++)
8501 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8502
8503 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8504 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8505 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8506 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8507 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8508 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8509 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8510}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008511#endif