blob: dbc8d95a43cfdb411328f3de6424bbf02ad1bbb4 [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
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003130 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
3131 + (quality_name == NULL ? 0 : strlen(quality_name) + 2)));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003132 if (res != NULL)
3133 {
3134 p = res;
3135 /* make a normal font string out of the lf thing:*/
3136 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3137 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3138 while (*p)
3139 {
3140 if (*p == ' ')
3141 *p = '_';
3142 ++p;
3143 }
3144 if (lf.lfItalic)
3145 STRCAT(p, ":i");
3146 if (lf.lfWeight >= FW_BOLD)
3147 STRCAT(p, ":b");
3148 if (lf.lfUnderline)
3149 STRCAT(p, ":u");
3150 if (lf.lfStrikeOut)
3151 STRCAT(p, ":s");
3152 if (charset_name != NULL)
3153 {
3154 STRCAT(p, ":c");
3155 STRCAT(p, charset_name);
3156 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003157 if (quality_name != NULL)
3158 {
3159 STRCAT(p, ":q");
3160 STRCAT(p, quality_name);
3161 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003162 }
3163
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003164 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165 return (char_u *)res;
3166}
3167
3168
3169#ifdef FEAT_MBYTE_IME
3170/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003171 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 * 'guifont'
3173 */
3174 static void
3175update_im_font(void)
3176{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003177 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178
3179 if (p_guifontwide != NULL && *p_guifontwide != NUL
3180 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003181 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003182 norm_logfont = lf_wide;
3183 else
3184 norm_logfont = sub_logfont;
3185 im_set_font(&norm_logfont);
3186}
3187#endif
3188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189/*
3190 * Handler of gui.wide_font (p_guifontwide) changed notification.
3191 */
3192 void
3193gui_mch_wide_font_changed(void)
3194{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003195 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003197#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003199#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200
3201 gui_mch_free_font(gui.wide_ital_font);
3202 gui.wide_ital_font = NOFONT;
3203 gui_mch_free_font(gui.wide_bold_font);
3204 gui.wide_bold_font = NOFONT;
3205 gui_mch_free_font(gui.wide_boldital_font);
3206 gui.wide_boldital_font = NOFONT;
3207
3208 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003209 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003210 {
3211 if (!lf.lfItalic)
3212 {
3213 lf.lfItalic = TRUE;
3214 gui.wide_ital_font = get_font_handle(&lf);
3215 lf.lfItalic = FALSE;
3216 }
3217 if (lf.lfWeight < FW_BOLD)
3218 {
3219 lf.lfWeight = FW_BOLD;
3220 gui.wide_bold_font = get_font_handle(&lf);
3221 if (!lf.lfItalic)
3222 {
3223 lf.lfItalic = TRUE;
3224 gui.wide_boldital_font = get_font_handle(&lf);
3225 }
3226 }
3227 }
3228}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229
3230/*
3231 * Initialise vim to use the font with the given name.
3232 * Return FAIL if the font could not be loaded, OK otherwise.
3233 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003235gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003237 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 GuiFont font = NOFONT;
3239 char_u *p;
3240
3241 /* Load the font */
3242 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3243 font = get_font_handle(&lf);
3244 if (font == NOFONT)
3245 return FAIL;
3246
3247 if (font_name == NULL)
3248 font_name = (char_u *)lf.lfFaceName;
3249#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3250 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003251#endif
3252#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003253 sub_logfont = lf;
3254#endif
3255#ifdef FEAT_MBYTE_IME
3256 update_im_font();
3257#endif
3258 gui_mch_free_font(gui.norm_font);
3259 gui.norm_font = font;
3260 current_font_height = lf.lfHeight;
3261 GetFontSize(font);
3262
3263 p = logfont2name(lf);
3264 if (p != NULL)
3265 {
3266 hl_set_font_name(p);
3267
3268 /* When setting 'guifont' to "*" replace it with the actual font name.
3269 * */
3270 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3271 {
3272 vim_free(p_guifont);
3273 p_guifont = p;
3274 }
3275 else
3276 vim_free(p);
3277 }
3278
3279 gui_mch_free_font(gui.ital_font);
3280 gui.ital_font = NOFONT;
3281 gui_mch_free_font(gui.bold_font);
3282 gui.bold_font = NOFONT;
3283 gui_mch_free_font(gui.boldital_font);
3284 gui.boldital_font = NOFONT;
3285
3286 if (!lf.lfItalic)
3287 {
3288 lf.lfItalic = TRUE;
3289 gui.ital_font = get_font_handle(&lf);
3290 lf.lfItalic = FALSE;
3291 }
3292 if (lf.lfWeight < FW_BOLD)
3293 {
3294 lf.lfWeight = FW_BOLD;
3295 gui.bold_font = get_font_handle(&lf);
3296 if (!lf.lfItalic)
3297 {
3298 lf.lfItalic = TRUE;
3299 gui.boldital_font = get_font_handle(&lf);
3300 }
3301 }
3302
3303 return OK;
3304}
3305
3306#ifndef WPF_RESTORETOMAXIMIZED
3307# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3308#endif
3309
3310/*
3311 * Return TRUE if the GUI window is maximized, filling the whole screen.
3312 */
3313 int
3314gui_mch_maximized(void)
3315{
3316 WINDOWPLACEMENT wp;
3317
3318 wp.length = sizeof(WINDOWPLACEMENT);
3319 if (GetWindowPlacement(s_hwnd, &wp))
3320 return wp.showCmd == SW_SHOWMAXIMIZED
3321 || (wp.showCmd == SW_SHOWMINIMIZED
3322 && wp.flags == WPF_RESTORETOMAXIMIZED);
3323
3324 return 0;
3325}
3326
3327/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003328 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3329 * is set. Compute the new Rows and Columns. This is like resizing the
3330 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331 */
3332 void
3333gui_mch_newfont(void)
3334{
3335 RECT rect;
3336
3337 GetWindowRect(s_hwnd, &rect);
3338 if (win_socket_id == 0)
3339 {
3340 gui_resize_shell(rect.right - rect.left
3341 - (GetSystemMetrics(SM_CXFRAME) +
3342 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3343 rect.bottom - rect.top
3344 - (GetSystemMetrics(SM_CYFRAME) +
3345 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3346 - GetSystemMetrics(SM_CYCAPTION)
3347#ifdef FEAT_MENU
3348 - gui_mswin_get_menu_height(FALSE)
3349#endif
3350 );
3351 }
3352 else
3353 {
3354 /* Inside another window, don't use the frame and border. */
3355 gui_resize_shell(rect.right - rect.left,
3356 rect.bottom - rect.top
3357#ifdef FEAT_MENU
3358 - gui_mswin_get_menu_height(FALSE)
3359#endif
3360 );
3361 }
3362}
3363
3364/*
3365 * Set the window title
3366 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003367 void
3368gui_mch_settitle(
3369 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003370 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371{
3372 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3373}
3374
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003375#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3377 * misc2.c! */
3378static LPCSTR mshape_idcs[] =
3379{
3380 IDC_ARROW, /* arrow */
3381 MAKEINTRESOURCE(0), /* blank */
3382 IDC_IBEAM, /* beam */
3383 IDC_SIZENS, /* updown */
3384 IDC_SIZENS, /* udsizing */
3385 IDC_SIZEWE, /* leftright */
3386 IDC_SIZEWE, /* lrsizing */
3387 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389 IDC_ARROW, /* crosshair */
3390 IDC_ARROW, /* hand1 */
3391 IDC_ARROW, /* hand2 */
3392 IDC_ARROW, /* pencil */
3393 IDC_ARROW, /* question */
3394 IDC_ARROW, /* right-arrow */
3395 IDC_UPARROW, /* up-arrow */
3396 IDC_ARROW /* last one */
3397};
3398
3399 void
3400mch_set_mouse_shape(int shape)
3401{
3402 LPCSTR idc;
3403
3404 if (shape == MSHAPE_HIDE)
3405 ShowCursor(FALSE);
3406 else
3407 {
3408 if (shape >= MSHAPE_NUMBERED)
3409 idc = IDC_ARROW;
3410 else
3411 idc = mshape_idcs[shape];
3412#ifdef SetClassLongPtr
3413 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3414#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416#endif
3417 if (!p_mh)
3418 {
3419 POINT mp;
3420
3421 /* Set the position to make it redrawn with the new shape. */
3422 (void)GetCursorPos((LPPOINT)&mp);
3423 (void)SetCursorPos(mp.x, mp.y);
3424 ShowCursor(TRUE);
3425 }
3426 }
3427}
3428#endif
3429
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003430#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432 * Wide version of convert_filter().
3433 */
3434 static WCHAR *
3435convert_filterW(char_u *s)
3436{
3437 char_u *tmp;
3438 int len;
3439 WCHAR *res;
3440
3441 tmp = convert_filter(s);
3442 if (tmp == NULL)
3443 return NULL;
3444 len = (int)STRLEN(s) + 3;
3445 res = enc_to_utf16(tmp, &len);
3446 vim_free(tmp);
3447 return res;
3448}
3449
3450/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003451 * Pop open a file browser and return the file selected, in allocated memory,
3452 * or NULL if Cancel is hit.
3453 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3454 * title - Title message for the file browser dialog.
3455 * dflt - Default name of file.
3456 * ext - Default extension to be added to files without extensions.
3457 * initdir - directory in which to open the browser (NULL = current dir)
3458 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003460 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003461gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 int saving,
3463 char_u *title,
3464 char_u *dflt,
3465 char_u *ext,
3466 char_u *initdir,
3467 char_u *filter)
3468{
3469 /* We always use the wide function. This means enc_to_utf16() must work,
3470 * otherwise it fails miserably! */
3471 OPENFILENAMEW fileStruct;
3472 WCHAR fileBuf[MAXPATHL];
3473 WCHAR *wp;
3474 int i;
3475 WCHAR *titlep = NULL;
3476 WCHAR *extp = NULL;
3477 WCHAR *initdirp = NULL;
3478 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003479 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480
3481 if (dflt == NULL)
3482 fileBuf[0] = NUL;
3483 else
3484 {
3485 wp = enc_to_utf16(dflt, NULL);
3486 if (wp == NULL)
3487 fileBuf[0] = NUL;
3488 else
3489 {
3490 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3491 fileBuf[i] = wp[i];
3492 fileBuf[i] = NUL;
3493 vim_free(wp);
3494 }
3495 }
3496
3497 /* Convert the filter to Windows format. */
3498 filterp = convert_filterW(filter);
3499
3500 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003501# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003502 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003503 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003504# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003505 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003506# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507
3508 if (title != NULL)
3509 titlep = enc_to_utf16(title, NULL);
3510 fileStruct.lpstrTitle = titlep;
3511
3512 if (ext != NULL)
3513 extp = enc_to_utf16(ext, NULL);
3514 fileStruct.lpstrDefExt = extp;
3515
3516 fileStruct.lpstrFile = fileBuf;
3517 fileStruct.nMaxFile = MAXPATHL;
3518 fileStruct.lpstrFilter = filterp;
3519 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3520 /* has an initial dir been specified? */
3521 if (initdir != NULL && *initdir != NUL)
3522 {
3523 /* Must have backslashes here, no matter what 'shellslash' says */
3524 initdirp = enc_to_utf16(initdir, NULL);
3525 if (initdirp != NULL)
3526 {
3527 for (wp = initdirp; *wp != NUL; ++wp)
3528 if (*wp == '/')
3529 *wp = '\\';
3530 }
3531 fileStruct.lpstrInitialDir = initdirp;
3532 }
3533
3534 /*
3535 * TODO: Allow selection of multiple files. Needs another arg to this
3536 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3537 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3538 * files that don't exist yet, so I haven't put it in. What about
3539 * OFN_PATHMUSTEXIST?
3540 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3541 */
3542 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003543# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003544 if (curbuf->b_p_bin)
3545 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003546# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003547 if (saving)
3548 {
3549 if (!GetSaveFileNameW(&fileStruct))
3550 return NULL;
3551 }
3552 else
3553 {
3554 if (!GetOpenFileNameW(&fileStruct))
3555 return NULL;
3556 }
3557
3558 vim_free(filterp);
3559 vim_free(initdirp);
3560 vim_free(titlep);
3561 vim_free(extp);
3562
3563 /* Convert from UCS2 to 'encoding'. */
3564 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003565 if (p == NULL)
3566 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567
3568 /* Give focus back to main window (when using MDI). */
3569 SetFocus(s_hwnd);
3570
3571 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003572 q = vim_strsave(shorten_fname1(p));
3573 vim_free(p);
3574 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576
3577
3578/*
3579 * Convert the string s to the proper format for a filter string by replacing
3580 * the \t and \n delimiters with \0.
3581 * Returns the converted string in allocated memory.
3582 *
3583 * Keep in sync with convert_filterW() above!
3584 */
3585 static char_u *
3586convert_filter(char_u *s)
3587{
3588 char_u *res;
3589 unsigned s_len = (unsigned)STRLEN(s);
3590 unsigned i;
3591
3592 res = alloc(s_len + 3);
3593 if (res != NULL)
3594 {
3595 for (i = 0; i < s_len; ++i)
3596 if (s[i] == '\t' || s[i] == '\n')
3597 res[i] = '\0';
3598 else
3599 res[i] = s[i];
3600 res[s_len] = NUL;
3601 /* Add two extra NULs to make sure it's properly terminated. */
3602 res[s_len + 1] = NUL;
3603 res[s_len + 2] = NUL;
3604 }
3605 return res;
3606}
3607
3608/*
3609 * Select a directory.
3610 */
3611 char_u *
3612gui_mch_browsedir(char_u *title, char_u *initdir)
3613{
3614 /* We fake this: Use a filter that doesn't select anything and a default
3615 * file name that won't be used. */
3616 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3617 initdir, (char_u *)_("Directory\t*.nothing\n"));
3618}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003619#endif /* FEAT_BROWSE */
3620
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003621 static void
3622_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003623 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003624 HDROP hDrop)
3625{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003626#define BUFPATHLEN _MAX_PATH
3627#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003629 char szFile[BUFPATHLEN];
3630 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3631 UINT i;
3632 char_u **fnames;
3633 POINT pt;
3634 int_u modifiers = 0;
3635
3636 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3637
3638 /* Obtain dropped position */
3639 DragQueryPoint(hDrop, &pt);
3640 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3641
3642 reset_VIsual();
3643
3644 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3645
3646 if (fnames != NULL)
3647 for (i = 0; i < cFiles; ++i)
3648 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3650 fnames[i] = utf16_to_enc(wszFile, NULL);
3651 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 {
3653 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3654 fnames[i] = vim_strsave((char_u *)szFile);
3655 }
3656 }
3657
3658 DragFinish(hDrop);
3659
3660 if (fnames != NULL)
3661 {
3662 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3663 modifiers |= MOUSE_SHIFT;
3664 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3665 modifiers |= MOUSE_CTRL;
3666 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3667 modifiers |= MOUSE_ALT;
3668
3669 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3670
3671 s_need_activate = TRUE;
3672 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003673}
3674
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675 static int
3676_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003677 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678 HWND hwndCtl,
3679 UINT code,
3680 int pos)
3681{
3682 static UINT prev_code = 0; /* code of previous call */
3683 scrollbar_T *sb, *sb_info;
3684 long val;
3685 int dragging = FALSE;
3686 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003687 SCROLLINFO si;
3688
3689 si.cbSize = sizeof(si);
3690 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691
3692 sb = gui_mswin_find_scrollbar(hwndCtl);
3693 if (sb == NULL)
3694 return 0;
3695
3696 if (sb->wp != NULL) /* Left or right scrollbar */
3697 {
3698 /*
3699 * Careful: need to get scrollbar info out of first (left) scrollbar
3700 * for window, but keep real scrollbar too because we must pass it to
3701 * gui_drag_scrollbar().
3702 */
3703 sb_info = &sb->wp->w_scrollbars[0];
3704 }
3705 else /* Bottom scrollbar */
3706 sb_info = sb;
3707 val = sb_info->value;
3708
3709 switch (code)
3710 {
3711 case SB_THUMBTRACK:
3712 val = pos;
3713 dragging = TRUE;
3714 if (sb->scroll_shift > 0)
3715 val <<= sb->scroll_shift;
3716 break;
3717 case SB_LINEDOWN:
3718 val++;
3719 break;
3720 case SB_LINEUP:
3721 val--;
3722 break;
3723 case SB_PAGEDOWN:
3724 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3725 break;
3726 case SB_PAGEUP:
3727 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3728 break;
3729 case SB_TOP:
3730 val = 0;
3731 break;
3732 case SB_BOTTOM:
3733 val = sb_info->max;
3734 break;
3735 case SB_ENDSCROLL:
3736 if (prev_code == SB_THUMBTRACK)
3737 {
3738 /*
3739 * "pos" only gives us 16-bit data. In case of large file,
3740 * use GetScrollPos() which returns 32-bit. Unfortunately it
3741 * is not valid while the scrollbar is being dragged.
3742 */
3743 val = GetScrollPos(hwndCtl, SB_CTL);
3744 if (sb->scroll_shift > 0)
3745 val <<= sb->scroll_shift;
3746 }
3747 break;
3748
3749 default:
3750 /* TRACE("Unknown scrollbar event %d\n", code); */
3751 return 0;
3752 }
3753 prev_code = code;
3754
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3756 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757
3758 /*
3759 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3760 */
3761 if (sb->wp != NULL)
3762 {
3763 scrollbar_T *sba = sb->wp->w_scrollbars;
3764 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3765
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003766 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 }
3768
3769 /* Don't let us be interrupted here by another message. */
3770 s_busy_processing = TRUE;
3771
3772 /* When "allow_scrollbar" is FALSE still need to remember the new
3773 * position, but don't actually scroll by setting "dont_scroll". */
3774 dont_scroll = !allow_scrollbar;
3775
Bram Moolenaara338adc2018-01-31 20:51:47 +01003776 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003778 mch_enable_flush();
3779 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780
3781 s_busy_processing = FALSE;
3782 dont_scroll = dont_scroll_save;
3783
3784 return 0;
3785}
3786
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788#ifdef FEAT_XPM_W32
3789# include "xpm_w32.h"
3790#endif
3791
3792#ifdef PROTO
3793# define WINAPI
3794#endif
3795
3796#ifdef __MINGW32__
3797/*
3798 * Add a lot of missing defines.
3799 * They are not always missing, we need the #ifndef's.
3800 */
3801# ifndef _cdecl
3802# define _cdecl
3803# endif
3804# ifndef IsMinimized
3805# define IsMinimized(hwnd) IsIconic(hwnd)
3806# endif
3807# ifndef IsMaximized
3808# define IsMaximized(hwnd) IsZoomed(hwnd)
3809# endif
3810# ifndef SelectFont
3811# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3812# endif
3813# ifndef GetStockBrush
3814# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3815# endif
3816# ifndef DeleteBrush
3817# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3818# endif
3819
3820# ifndef HANDLE_WM_RBUTTONDBLCLK
3821# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3822 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3823# endif
3824# ifndef HANDLE_WM_MBUTTONUP
3825# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3826 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3827# endif
3828# ifndef HANDLE_WM_MBUTTONDBLCLK
3829# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3830 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3831# endif
3832# ifndef HANDLE_WM_LBUTTONDBLCLK
3833# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3834 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3835# endif
3836# ifndef HANDLE_WM_RBUTTONDOWN
3837# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3838 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3839# endif
3840# ifndef HANDLE_WM_MOUSEMOVE
3841# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3842 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3843# endif
3844# ifndef HANDLE_WM_RBUTTONUP
3845# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3846 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3847# endif
3848# ifndef HANDLE_WM_MBUTTONDOWN
3849# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3850 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3851# endif
3852# ifndef HANDLE_WM_LBUTTONUP
3853# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3854 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3855# endif
3856# ifndef HANDLE_WM_LBUTTONDOWN
3857# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3858 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3859# endif
3860# ifndef HANDLE_WM_SYSCHAR
3861# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3862 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3863# endif
3864# ifndef HANDLE_WM_ACTIVATEAPP
3865# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3866 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3867# endif
3868# ifndef HANDLE_WM_WINDOWPOSCHANGING
3869# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3870 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3871# endif
3872# ifndef HANDLE_WM_VSCROLL
3873# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3874 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3875# endif
3876# ifndef HANDLE_WM_SETFOCUS
3877# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3878 ((fn)((hwnd), (HWND)(wParam)), 0L)
3879# endif
3880# ifndef HANDLE_WM_KILLFOCUS
3881# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3882 ((fn)((hwnd), (HWND)(wParam)), 0L)
3883# endif
3884# ifndef HANDLE_WM_HSCROLL
3885# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3886 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3887# endif
3888# ifndef HANDLE_WM_DROPFILES
3889# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3890 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3891# endif
3892# ifndef HANDLE_WM_CHAR
3893# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3894 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3895# endif
3896# ifndef HANDLE_WM_SYSDEADCHAR
3897# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3898 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3899# endif
3900# ifndef HANDLE_WM_DEADCHAR
3901# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3902 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3903# endif
3904#endif /* __MINGW32__ */
3905
3906
3907/* Some parameters for tearoff menus. All in pixels. */
3908#define TEAROFF_PADDING_X 2
3909#define TEAROFF_BUTTON_PAD_X 8
3910#define TEAROFF_MIN_WIDTH 200
3911#define TEAROFF_SUBMENU_LABEL ">>"
3912#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3913
3914
3915/* For the Intellimouse: */
3916#ifndef WM_MOUSEWHEEL
3917#define WM_MOUSEWHEEL 0x20a
3918#endif
3919
3920
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003921#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922# define ID_BEVAL_TOOLTIP 200
3923# define BEVAL_TEXT_LEN MAXPATHL
3924
Bram Moolenaar167632f2010-05-26 21:42:54 +02003925#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003926/* Work around old versions of basetsd.h which wrongly declares
3927 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003928# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003929# define UINT_PTR UINT
3930#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003933static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003935
Bram Moolenaar82881492012-11-20 16:53:39 +01003936
3937/* cproto fails on missing include files */
3938#ifndef PROTO
3939
Bram Moolenaar45360022005-07-21 21:08:21 +00003940/*
3941 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003942 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003943 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003944# include <pshpack1.h>
3945
3946#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003947
3948typedef struct _DllVersionInfo
3949{
3950 DWORD cbSize;
3951 DWORD dwMajorVersion;
3952 DWORD dwMinorVersion;
3953 DWORD dwBuildNumber;
3954 DWORD dwPlatformID;
3955} DLLVERSIONINFO;
3956
Bram Moolenaar82881492012-11-20 16:53:39 +01003957#ifndef PROTO
3958# include <poppack.h>
3959#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003960
Bram Moolenaar45360022005-07-21 21:08:21 +00003961typedef struct tagTOOLINFOA_NEW
3962{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003963 UINT cbSize;
3964 UINT uFlags;
3965 HWND hwnd;
3966 UINT_PTR uId;
3967 RECT rect;
3968 HINSTANCE hinst;
3969 LPSTR lpszText;
3970 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003971} TOOLINFO_NEW;
3972
3973typedef struct tagNMTTDISPINFO_NEW
3974{
3975 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003976 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003977 char szText[80];
3978 HINSTANCE hinst;
3979 UINT uFlags;
3980 LPARAM lParam;
3981} NMTTDISPINFO_NEW;
3982
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003983typedef struct tagTOOLINFOW_NEW
3984{
3985 UINT cbSize;
3986 UINT uFlags;
3987 HWND hwnd;
3988 UINT_PTR uId;
3989 RECT rect;
3990 HINSTANCE hinst;
3991 LPWSTR lpszText;
3992 LPARAM lParam;
3993 void *lpReserved;
3994} TOOLINFOW_NEW;
3995
3996typedef struct tagNMTTDISPINFOW_NEW
3997{
3998 NMHDR hdr;
3999 LPWSTR lpszText;
4000 WCHAR szText[80];
4001 HINSTANCE hinst;
4002 UINT uFlags;
4003 LPARAM lParam;
4004} NMTTDISPINFOW_NEW;
4005
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004006
Bram Moolenaar45360022005-07-21 21:08:21 +00004007typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4008#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004009# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010#endif
4011
Bram Moolenaar45360022005-07-21 21:08:21 +00004012#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004013# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004014#endif
4015
4016#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004017# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004018#endif
4019
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004020#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004021
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004022#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4023/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4024 * it here if LPNMTTDISPINFO isn't defined.
4025 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4026 * _MSC_VER. */
4027# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4028typedef struct tagNMTTDISPINFOA {
4029 NMHDR hdr;
4030 LPSTR lpszText;
4031 char szText[80];
4032 HINSTANCE hinst;
4033 UINT uFlags;
4034 LPARAM lParam;
4035} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4036# define LPNMTTDISPINFO LPNMTTDISPINFOA
4037
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004038typedef struct tagNMTTDISPINFOW {
4039 NMHDR hdr;
4040 LPWSTR lpszText;
4041 WCHAR szText[80];
4042 HINSTANCE hinst;
4043 UINT uFlags;
4044 LPARAM lParam;
4045} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004046# endif
4047#endif
4048
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004049#ifndef TTN_GETDISPINFOW
4050# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4051#endif
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053/* Local variables: */
4054
4055#ifdef FEAT_MENU
4056static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059/*
4060 * Use the system font for dialogs and tear-off menus. Remove this line to
4061 * use DLG_FONT_NAME.
4062 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004063#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066#define VIM_CLASSW L"Vim"
4067
4068/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4069 * thus there should be room for every dialog. For tearoffs it's made bigger
4070 * when needed. */
4071#define DLG_ALLOC_SIZE 16 * 1024
4072
4073/*
4074 * stuff for dialogs, menus, tearoffs etc.
4075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076static PWORD
4077add_dialog_element(
4078 PWORD p,
4079 DWORD lStyle,
4080 WORD x,
4081 WORD y,
4082 WORD w,
4083 WORD h,
4084 WORD Id,
4085 WORD clss,
4086 const char *caption);
4087static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004088static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004089#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092static void get_dialog_font_metrics(void);
4093
4094static int dialog_default_button = -1;
4095
4096/* Intellimouse support */
4097static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
4099static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4100#ifdef FEAT_TOOLBAR
4101static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004102static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103static int get_toolbar_bitmap(vimmenu_T *menu);
4104#endif
4105
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004106#ifdef FEAT_GUI_TABLINE
4107static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004108static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004109#endif
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_MBYTE_IME
4112static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4113static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4114#endif
4115#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4116# ifdef NOIME
4117typedef struct tagCOMPOSITIONFORM {
4118 DWORD dwStyle;
4119 POINT ptCurrentPos;
4120 RECT rcArea;
4121} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4122typedef HANDLE HIMC;
4123# endif
4124
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004125static HINSTANCE hLibImm = NULL;
4126static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4127static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4128static HIMC (WINAPI *pImmGetContext)(HWND);
4129static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4130static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4131static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4132static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004133static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4134static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004135static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4136static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004137static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138static void dyn_imm_load(void);
4139#else
4140# define pImmGetCompositionStringA ImmGetCompositionStringA
4141# define pImmGetCompositionStringW ImmGetCompositionStringW
4142# define pImmGetContext ImmGetContext
4143# define pImmAssociateContext ImmAssociateContext
4144# define pImmReleaseContext ImmReleaseContext
4145# define pImmGetOpenStatus ImmGetOpenStatus
4146# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004147# define pImmGetCompositionFontW ImmGetCompositionFontW
4148# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149# define pImmSetCompositionWindow ImmSetCompositionWindow
4150# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004151# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#endif
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#ifdef FEAT_MENU
4155/*
4156 * Figure out how high the menu bar is at the moment.
4157 */
4158 static int
4159gui_mswin_get_menu_height(
4160 int fix_window) /* If TRUE, resize window if menu height changed */
4161{
4162 static int old_menu_height = -1;
4163
4164 RECT rc1, rc2;
4165 int num;
4166 int menu_height;
4167
4168 if (gui.menu_is_active)
4169 num = GetMenuItemCount(s_menuBar);
4170 else
4171 num = 0;
4172
4173 if (num == 0)
4174 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004175 else if (IsMinimized(s_hwnd))
4176 {
4177 /* The height of the menu cannot be determined while the window is
4178 * minimized. Take the previous height if the menu is changed in that
4179 * state, to avoid that Vim's vertical window size accidentally
4180 * increases due to the unaccounted-for menu height. */
4181 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 else
4184 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004185 /*
4186 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4187 * seem to have been set yet, so menu wraps in default window
4188 * width which is very narrow. Instead just return height of a
4189 * single menu item. Will still be wrong when the menu really
4190 * should wrap over more than one line.
4191 */
4192 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4193 if (gui.starting)
4194 menu_height = rc1.bottom - rc1.top + 1;
4195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004197 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4198 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201
4202 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004203 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004204 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205
4206 return menu_height;
4207}
4208#endif /*FEAT_MENU*/
4209
4210
4211/*
4212 * Setup for the Intellimouse
4213 */
4214 static void
4215init_mouse_wheel(void)
4216{
4217
4218#ifndef SPI_GETWHEELSCROLLLINES
4219# define SPI_GETWHEELSCROLLLINES 104
4220#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004221#ifndef SPI_SETWHEELSCROLLLINES
4222# define SPI_SETWHEELSCROLLLINES 105
4223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4226#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4227#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4228#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 mouse_scroll_lines = 3; /* reasonable default */
4231
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004232 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4233 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4234 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237
4238/* Intellimouse wheel handler */
4239 static void
4240_OnMouseWheel(
4241 HWND hwnd,
4242 short zDelta)
4243{
4244/* Treat a mouse wheel event as if it were a scroll request */
4245 int i;
4246 int size;
4247 HWND hwndCtl;
4248
4249 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4250 {
4251 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4252 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4253 }
4254 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4255 {
4256 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4257 size = curwin->w_scrollbars[SBAR_LEFT].size;
4258 }
4259 else
4260 return;
4261
4262 size = curwin->w_height;
4263 if (mouse_scroll_lines == 0)
4264 init_mouse_wheel();
4265
Bram Moolenaara338adc2018-01-31 20:51:47 +01004266 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 if (mouse_scroll_lines > 0
4268 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4269 {
4270 for (i = mouse_scroll_lines; i > 0; --i)
4271 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4272 }
4273 else
4274 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004275 mch_enable_flush();
4276 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277}
4278
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004279#ifdef USE_SYSMENU_FONT
4280/*
4281 * Get Menu Font.
4282 * Return OK or FAIL.
4283 */
4284 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004285gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004286{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004287 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004288
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004289 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4290 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004291 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004292 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004293 &nm,
4294 0))
4295 return FAIL;
4296 *lf = nm.lfMenuFont;
4297 return OK;
4298}
4299#endif
4300
4301
4302#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4303/*
4304 * Set the GUI tabline font to the system menu font
4305 */
4306 static void
4307set_tabline_font(void)
4308{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004309 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004310 HFONT font;
4311 HWND hwnd;
4312 HDC hdc;
4313 HFONT hfntOld;
4314 TEXTMETRIC tm;
4315
4316 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4317 return;
4318
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004319 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004320
4321 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4322
4323 /*
4324 * Compute the height of the font used for the tab text
4325 */
4326 hwnd = GetDesktopWindow();
4327 hdc = GetWindowDC(hwnd);
4328 hfntOld = SelectFont(hdc, font);
4329
4330 GetTextMetrics(hdc, &tm);
4331
4332 SelectFont(hdc, hfntOld);
4333 ReleaseDC(hwnd, hdc);
4334
4335 /*
4336 * The space used by the tab border and the space between the tab label
4337 * and the tab border is included as 7.
4338 */
4339 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4340}
4341#endif
4342
Bram Moolenaar520470a2005-06-16 21:59:56 +00004343/*
4344 * Invoked when a setting was changed.
4345 */
4346 static LRESULT CALLBACK
4347_OnSettingChange(UINT n)
4348{
4349 if (n == SPI_SETWHEELSCROLLLINES)
4350 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4351 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004352#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4353 if (n == SPI_SETNONCLIENTMETRICS)
4354 set_tabline_font();
4355#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004356 return 0;
4357}
4358
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359#ifdef FEAT_NETBEANS_INTG
4360 static void
4361_OnWindowPosChanged(
4362 HWND hwnd,
4363 const LPWINDOWPOS lpwpos)
4364{
4365 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004366 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4369 || lpwpos->cx != cx || lpwpos->cy != cy))
4370 {
4371 x = lpwpos->x;
4372 y = lpwpos->y;
4373 cx = lpwpos->cx;
4374 cy = lpwpos->cy;
4375 netbeans_frame_moved(x, y);
4376 }
4377 /* Allow to send WM_SIZE and WM_MOVE */
4378 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4379}
4380#endif
4381
4382 static int
4383_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 UINT fwSide,
4385 LPRECT lprc)
4386{
4387 int w, h;
4388 int valid_w, valid_h;
4389 int w_offset, h_offset;
4390
4391 w = lprc->right - lprc->left;
4392 h = lprc->bottom - lprc->top;
4393 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4394 w_offset = w - valid_w;
4395 h_offset = h - valid_h;
4396
4397 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4398 || fwSide == WMSZ_BOTTOMLEFT)
4399 lprc->left += w_offset;
4400 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4401 || fwSide == WMSZ_BOTTOMRIGHT)
4402 lprc->right -= w_offset;
4403
4404 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4405 || fwSide == WMSZ_TOPRIGHT)
4406 lprc->top += h_offset;
4407 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4408 || fwSide == WMSZ_BOTTOMRIGHT)
4409 lprc->bottom -= h_offset;
4410 return TRUE;
4411}
4412
4413
4414
4415 static LRESULT CALLBACK
4416_WndProc(
4417 HWND hwnd,
4418 UINT uMsg,
4419 WPARAM wParam,
4420 LPARAM lParam)
4421{
4422 /*
4423 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4424 hwnd, uMsg, wParam, lParam);
4425 */
4426
4427 HandleMouseHide(uMsg, lParam);
4428
4429 s_uMsg = uMsg;
4430 s_wParam = wParam;
4431 s_lParam = lParam;
4432
4433 switch (uMsg)
4434 {
4435 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4436 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4437 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4438 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4439 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4440 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4441 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4442 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4443 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4444#ifdef FEAT_MENU
4445 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4446#endif
4447 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4448 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4449 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4450 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4451 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4452 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4453 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4454 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4455 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4456#ifdef FEAT_NETBEANS_INTG
4457 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4458#endif
4459
Bram Moolenaarafa24992006-03-27 20:58:26 +00004460#ifdef FEAT_GUI_TABLINE
4461 case WM_RBUTTONUP:
4462 {
4463 if (gui_mch_showing_tabline())
4464 {
4465 POINT pt;
4466 RECT rect;
4467
4468 /*
4469 * If the cursor is on the tabline, display the tab menu
4470 */
4471 GetCursorPos((LPPOINT)&pt);
4472 GetWindowRect(s_textArea, &rect);
4473 if (pt.y < rect.top)
4474 {
4475 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004476 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004477 }
4478 }
4479 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4480 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004481 case WM_LBUTTONDBLCLK:
4482 {
4483 /*
4484 * If the user double clicked the tabline, create a new tab
4485 */
4486 if (gui_mch_showing_tabline())
4487 {
4488 POINT pt;
4489 RECT rect;
4490
4491 GetCursorPos((LPPOINT)&pt);
4492 GetWindowRect(s_textArea, &rect);
4493 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004494 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004495 }
4496 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4497 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004498#endif
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 case WM_QUERYENDSESSION: /* System wants to go down. */
4501 gui_shell_closed(); /* Will exit when no changed buffers. */
4502 return FALSE; /* Do NOT allow system to go down. */
4503
4504 case WM_ENDSESSION:
4505 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004508 return 0L;
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511
4512 case WM_CHAR:
4513 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4514 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004515 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 return 0L;
4517
4518 case WM_SYSCHAR:
4519 /*
4520 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4521 * shortcut key, handle like a typed ALT key, otherwise call Windows
4522 * ALT key handling.
4523 */
4524#ifdef FEAT_MENU
4525 if ( !gui.menu_is_active
4526 || p_wak[0] == 'n'
4527 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4528 )
4529#endif
4530 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004531 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 return 0L;
4533 }
4534#ifdef FEAT_MENU
4535 else
4536 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4537#endif
4538
4539 case WM_SYSKEYUP:
4540#ifdef FEAT_MENU
4541 /* This used to be done only when menu is active: ALT key is used for
4542 * that. But that caused problems when menu is disabled and using
4543 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4544 * are received, mouse pointer remains hidden. */
4545 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4546#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004547 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#endif
4549
4550 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004551 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 case WM_MOUSEWHEEL:
4554 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004555 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar520470a2005-06-16 21:59:56 +00004557 /* Notification for change in SystemParametersInfo() */
4558 case WM_SETTINGCHANGE:
4559 return _OnSettingChange((UINT)wParam);
4560
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004561#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 case WM_NOTIFY:
4563 switch (((LPNMHDR) lParam)->code)
4564 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004565 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004566 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004568 LPNMHDR hdr = (LPNMHDR)lParam;
4569 char_u *str = NULL;
4570 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004571
Bram Moolenaard23a8232018-02-10 18:45:26 +01004572 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004573
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004574# ifdef FEAT_GUI_TABLINE
4575 if (gui_mch_showing_tabline()
4576 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004577 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004578 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004579 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004580 * Mouse is over the GUI tabline. Display the
4581 * tooltip for the tab under the cursor
4582 *
4583 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004584 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004585 GetCursorPos(&pt);
4586 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004587 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004588 TCHITTESTINFO htinfo;
4589 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004590
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004591 /*
4592 * Get the tab under the cursor
4593 */
4594 htinfo.pt.x = pt.x;
4595 htinfo.pt.y = pt.y;
4596 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4597 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004598 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004599 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004600
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004601 tp = find_tabpage(idx + 1);
4602 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004603 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004604 get_tabline_label(tp, TRUE);
4605 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004606 }
4607 }
4608 }
4609 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004610# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004611# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004612# ifdef FEAT_GUI_TABLINE
4613 else
4614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004616 UINT idButton;
4617 vimmenu_T *pMenu;
4618
4619 idButton = (UINT) hdr->idFrom;
4620 pMenu = gui_mswin_find_menu(root_menu, idButton);
4621 if (pMenu)
4622 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004624# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004625 if (str != NULL)
4626 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004627 if (hdr->code == TTN_GETDISPINFOW)
4628 {
4629 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4630
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004631 /* Set the maximum width, this also enables using
4632 * \n for line break. */
4633 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4634 0, 500);
4635
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004636 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004637 lpdi->lpszText = tt_text;
4638 /* can't show tooltip if failed */
4639 }
4640 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004641 {
4642 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4643
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004644 /* Set the maximum width, this also enables using
4645 * \n for line break. */
4646 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4647 0, 500);
4648
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004649 if (STRLEN(str) < sizeof(lpdi->szText)
4650 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004651 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004652 sizeof(lpdi->szText) - 1);
4653 else
4654 lpdi->lpszText = tt_text;
4655 }
4656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
4658 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004659# ifdef FEAT_GUI_TABLINE
4660 case TCN_SELCHANGE:
4661 if (gui_mch_showing_tabline()
4662 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004663 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004664 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004665 return 0L;
4666 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004667 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004668
4669 case NM_RCLICK:
4670 if (gui_mch_showing_tabline()
4671 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004672 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004673 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004674 return 0L;
4675 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004676 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004679# ifdef FEAT_GUI_TABLINE
4680 if (gui_mch_showing_tabline()
4681 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4682 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 break;
4685 }
4686 break;
4687#endif
4688#if defined(MENUHINTS) && defined(FEAT_MENU)
4689 case WM_MENUSELECT:
4690 if (((UINT) HIWORD(wParam)
4691 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4692 == MF_HILITE
4693 && (State & CMDLINE) == 0)
4694 {
4695 UINT idButton;
4696 vimmenu_T *pMenu;
4697 static int did_menu_tip = FALSE;
4698
4699 if (did_menu_tip)
4700 {
4701 msg_clr_cmdline();
4702 setcursor();
4703 out_flush();
4704 did_menu_tip = FALSE;
4705 }
4706
4707 idButton = (UINT)LOWORD(wParam);
4708 pMenu = gui_mswin_find_menu(root_menu, idButton);
4709 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4710 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4711 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004712 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004713 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004714 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 setcursor();
4716 out_flush();
4717 did_menu_tip = TRUE;
4718 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004719 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 break;
4722#endif
4723 case WM_NCHITTEST:
4724 {
4725 LRESULT result;
4726 int x, y;
4727 int xPos = GET_X_LPARAM(lParam);
4728
4729 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4730 if (result == HTCLIENT)
4731 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004732#ifdef FEAT_GUI_TABLINE
4733 if (gui_mch_showing_tabline())
4734 {
4735 int yPos = GET_Y_LPARAM(lParam);
4736 RECT rct;
4737
4738 /* If the cursor is on the GUI tabline, don't process this
4739 * event */
4740 GetWindowRect(s_textArea, &rct);
4741 if (yPos < rct.top)
4742 return result;
4743 }
4744#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004745 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 xPos -= x;
4747
4748 if (xPos < 48) /* <VN> TODO should use system metric? */
4749 return HTBOTTOMLEFT;
4750 else
4751 return HTBOTTOMRIGHT;
4752 }
4753 else
4754 return result;
4755 }
4756 /* break; notreached */
4757
4758#ifdef FEAT_MBYTE_IME
4759 case WM_IME_NOTIFY:
4760 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4761 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004762 return 1L;
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 case WM_IME_COMPOSITION:
4765 if (!_OnImeComposition(hwnd, wParam, lParam))
4766 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004767 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768#endif
4769
4770 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004772 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774#endif
4775 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4776 }
4777
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004778 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779}
4780
4781/*
4782 * End of call-back routines
4783 */
4784
4785/* parent window, if specified with -P */
4786HWND vim_parent_hwnd = NULL;
4787
4788 static BOOL CALLBACK
4789FindWindowTitle(HWND hwnd, LPARAM lParam)
4790{
4791 char buf[2048];
4792 char *title = (char *)lParam;
4793
4794 if (GetWindowText(hwnd, buf, sizeof(buf)))
4795 {
4796 if (strstr(buf, title) != NULL)
4797 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004798 /* Found it. Store the window ref. and quit searching if MDI
4799 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004801 if (vim_parent_hwnd != NULL)
4802 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804 }
4805 return TRUE; /* continue searching */
4806}
4807
4808/*
4809 * Invoked for '-P "title"' argument: search for parent application to open
4810 * our window in.
4811 */
4812 void
4813gui_mch_set_parent(char *title)
4814{
4815 EnumWindows(FindWindowTitle, (LPARAM)title);
4816 if (vim_parent_hwnd == NULL)
4817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004818 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 mch_exit(2);
4820 }
4821}
4822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004823#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 static void
4825ole_error(char *arg)
4826{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004827 char buf[IOSIZE];
4828
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004829 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004830 vim_snprintf(buf, IOSIZE,
4831 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4832 arg);
4833 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837/*
4838 * Parse the GUI related command-line arguments. Any arguments used are
4839 * deleted from argv, and *argc is decremented accordingly. This is called
4840 * when vim is started, whether or not the GUI has been started.
4841 */
4842 void
4843gui_mch_prepare(int *argc, char **argv)
4844{
4845 int silent = FALSE;
4846 int idx;
4847
4848 /* Check for special OLE command line parameters */
4849 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4850 {
4851 /* Check for a "-silent" argument first. */
4852 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4853 && (argv[2][0] == '-' || argv[2][0] == '/'))
4854 {
4855 silent = TRUE;
4856 idx = 2;
4857 }
4858 else
4859 idx = 1;
4860
4861 /* Register Vim as an OLE Automation server */
4862 if (STRICMP(argv[idx] + 1, "register") == 0)
4863 {
4864#ifdef FEAT_OLE
4865 RegisterMe(silent);
4866 mch_exit(0);
4867#else
4868 if (!silent)
4869 ole_error("register");
4870 mch_exit(2);
4871#endif
4872 }
4873
4874 /* Unregister Vim as an OLE Automation server */
4875 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4876 {
4877#ifdef FEAT_OLE
4878 UnregisterMe(!silent);
4879 mch_exit(0);
4880#else
4881 if (!silent)
4882 ole_error("unregister");
4883 mch_exit(2);
4884#endif
4885 }
4886
4887 /* Ignore an -embedding argument. It is only relevant if the
4888 * application wants to treat the case when it is started manually
4889 * differently from the case where it is started via automation (and
4890 * we don't).
4891 */
4892 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4893 {
4894#ifdef FEAT_OLE
4895 *argc = 1;
4896#else
4897 ole_error("embedding");
4898 mch_exit(2);
4899#endif
4900 }
4901 }
4902
4903#ifdef FEAT_OLE
4904 {
4905 int bDoRestart = FALSE;
4906
4907 InitOLE(&bDoRestart);
4908 /* automatically exit after registering */
4909 if (bDoRestart)
4910 mch_exit(0);
4911 }
4912#endif
4913
4914#ifdef FEAT_NETBEANS_INTG
4915 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004916 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 int arg;
4918
4919 for (arg = 1; arg < *argc; arg++)
4920 if (strncmp("-nb", argv[arg], 3) == 0)
4921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 netbeansArg = argv[arg];
4923 mch_memmove(&argv[arg], &argv[arg + 1],
4924 (--*argc - arg) * sizeof(char *));
4925 argv[*argc] = NULL;
4926 break; /* enough? */
4927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930}
4931
4932/*
4933 * Initialise the GUI. Create all the windows, set up all the call-backs
4934 * etc.
4935 */
4936 int
4937gui_mch_init(void)
4938{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01004940 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#ifdef GLOBAL_IME
4943 ATOM atom;
4944#endif
4945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 /* Return here if the window was already opened (happens when
4947 * gui_mch_dialog() is called early). */
4948 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004949 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 /*
4952 * Load the tearoff bitmap
4953 */
4954#ifdef FEAT_TEAROFF
4955 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
4956#endif
4957
4958 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
4959 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
4960#ifdef FEAT_MENU
4961 gui.menu_height = 0; /* Windows takes care of this */
4962#endif
4963 gui.border_width = 0;
4964
4965 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 /* First try using the wide version, so that we can use any title.
4968 * Otherwise only characters in the active codepage will work. */
4969 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
4970 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004971 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 wndclassw.lpfnWndProc = _WndProc;
4973 wndclassw.cbClsExtra = 0;
4974 wndclassw.cbWndExtra = 0;
4975 wndclassw.hInstance = s_hinst;
4976 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
4977 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
4978 wndclassw.hbrBackground = s_brush;
4979 wndclassw.lpszMenuName = NULL;
4980 wndclassw.lpszClassName = szVimWndClassW;
4981
4982 if ((
4983#ifdef GLOBAL_IME
4984 atom =
4985#endif
4986 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004987 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (vim_parent_hwnd != NULL)
4991 {
4992#ifdef HAVE_TRY_EXCEPT
4993 __try
4994 {
4995#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02004996 // Open inside the specified parent window.
4997 // TODO: last argument should point to a CLIENTCREATESTRUCT
4998 // structure.
4999 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005001 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005002 WS_OVERLAPPEDWINDOW | WS_CHILD
5003 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5005 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005006 100, // Any value will do
5007 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 vim_parent_hwnd, NULL,
5009 s_hinst, NULL);
5010#ifdef HAVE_TRY_EXCEPT
5011 }
5012 __except(EXCEPTION_EXECUTE_HANDLER)
5013 {
5014 /* NOP */
5015 }
5016#endif
5017 if (s_hwnd == NULL)
5018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005019 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 mch_exit(2);
5021 }
5022 }
5023 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005024 {
5025 /* If the provided windowid is not valid reset it to zero, so that it
5026 * is ignored and we open our own window. */
5027 if (IsWindow((HWND)win_socket_id) <= 0)
5028 win_socket_id = 0;
5029
5030 /* Create a window. If win_socket_id is not zero without border and
5031 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005032 s_hwnd = CreateWindowW(
5033 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005034 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5035 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005036 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5037 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5038 100, /* Any value will do */
5039 100, /* Any value will do */
5040 NULL, NULL,
5041 s_hinst, NULL);
5042 if (s_hwnd != NULL && win_socket_id != 0)
5043 {
5044 SetParent(s_hwnd, (HWND)win_socket_id);
5045 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5046 }
5047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 if (s_hwnd == NULL)
5050 return FAIL;
5051
5052#ifdef GLOBAL_IME
5053 global_ime_init(atom, s_hwnd);
5054#endif
5055#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5056 dyn_imm_load();
5057#endif
5058
5059 /* Create the text area window */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005060 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005061 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005062 wndclassw.style = CS_OWNDC;
5063 wndclassw.lpfnWndProc = _TextAreaWndProc;
5064 wndclassw.cbClsExtra = 0;
5065 wndclassw.cbWndExtra = 0;
5066 wndclassw.hInstance = s_hinst;
5067 wndclassw.hIcon = NULL;
5068 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5069 wndclassw.hbrBackground = NULL;
5070 wndclassw.lpszMenuName = NULL;
5071 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005072
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005073 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 return FAIL;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005077 s_textArea = CreateWindowExW(
5078 0,
5079 szTextAreaClassW, L"Vim text area",
5080 WS_CHILD | WS_VISIBLE, 0, 0,
5081 100, // Any value will do for now
5082 100, // Any value will do for now
5083 s_hwnd, NULL,
5084 s_hinst, NULL);
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (s_textArea == NULL)
5087 return FAIL;
5088
Bram Moolenaar20321902016-02-17 12:30:17 +01005089#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005090 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5091 {
5092 HANDLE hIcon = NULL;
5093
5094 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005095 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005096 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005097#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#ifdef FEAT_MENU
5100 s_menuBar = CreateMenu();
5101#endif
5102 s_hdc = GetDC(s_textArea);
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
5106 /* Do we need to bother with this? */
5107 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5108
5109 /* Get background/foreground colors from the system */
5110 gui_mch_def_colors();
5111
5112 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5113 * file) */
5114 set_normal_colors();
5115
5116 /*
5117 * Check that none of the colors are the same as the background color.
5118 * Then store the current values as the defaults.
5119 */
5120 gui_check_colors();
5121 gui.def_norm_pixel = gui.norm_pixel;
5122 gui.def_back_pixel = gui.back_pixel;
5123
5124 /* Get the colors for the highlight groups (gui_check_colors() might have
5125 * changed them) */
5126 highlight_gui_started();
5127
5128 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005129 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005131 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 /*
5134 * Set up for Intellimouse processing
5135 */
5136 init_mouse_wheel();
5137
5138 /*
5139 * compute a couple of metrics used for the dialogs
5140 */
5141 get_dialog_font_metrics();
5142#ifdef FEAT_TOOLBAR
5143 /*
5144 * Create the toolbar
5145 */
5146 initialise_toolbar();
5147#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005148#ifdef FEAT_GUI_TABLINE
5149 /*
5150 * Create the tabline
5151 */
5152 initialise_tabline();
5153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef MSWIN_FIND_REPLACE
5155 /*
5156 * Initialise the dialog box stuff
5157 */
5158 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5159
5160 /* Initialise the struct */
5161 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005162 s_findrep_struct.lpstrFindWhat =
5163 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005165 s_findrep_struct.lpstrReplaceWith =
5166 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5168 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5169 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005172#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005173# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5174/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5175# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005176# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005177# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005178# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005179 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005180 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005181#endif
5182
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005183#ifdef FEAT_RENDER_OPTIONS
5184 if (p_rop)
5185 (void)gui_mch_set_rendering_options(p_rop);
5186#endif
5187
Bram Moolenaar748bf032005-02-02 23:04:36 +00005188theend:
5189 /* Display any pending error messages */
5190 display_errors();
5191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 return OK;
5193}
5194
5195/*
5196 * Get the size of the screen, taking position on multiple monitors into
5197 * account (if supported).
5198 */
5199 static void
5200get_work_area(RECT *spi_rect)
5201{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005202 HMONITOR mon;
5203 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005205 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005206 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005207 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005209 moninfo.cbSize = sizeof(MONITORINFO);
5210 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005212 *spi_rect = moninfo.rcWork;
5213 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 }
5216 /* this is the old method... */
5217 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5218}
5219
5220/*
5221 * Set the size of the window to the given width and height in pixels.
5222 */
5223 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005224gui_mch_set_shellsize(
5225 int width,
5226 int height,
5227 int min_width UNUSED,
5228 int min_height UNUSED,
5229 int base_width UNUSED,
5230 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005231 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 RECT workarea_rect;
5234 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 WINDOWPLACEMENT wndpl;
5236
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005237 /* Try to keep window completely on screen. */
5238 /* Get position of the screen work area. This is the part that is not
5239 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 get_work_area(&workarea_rect);
5241
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005242 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005243 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 wndpl.length = sizeof(WINDOWPLACEMENT);
5245 GetWindowPlacement(s_hwnd, &wndpl);
5246
5247 /* Resizing a maximized window looks very strange, unzoom it first.
5248 * But don't do it when still starting up, it may have been requested in
5249 * the shortcut. */
5250 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5251 {
5252 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5253 /* Need to get the settings of the normal window. */
5254 GetWindowPlacement(s_hwnd, &wndpl);
5255 }
5256
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005258 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005259 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005260 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005261 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 + GetSystemMetrics(SM_CYCAPTION)
5263#ifdef FEAT_MENU
5264 + gui_mswin_get_menu_height(FALSE)
5265#endif
5266 ;
5267
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005268 /* The following should take care of keeping Vim on the same monitor, no
5269 * matter if the secondary monitor is left or right of the primary
5270 * monitor. */
5271 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5272 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005273
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005274 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005275 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005276 && wndpl.rcNormalPosition.right > workarea_rect.right)
5277 OffsetRect(&wndpl.rcNormalPosition,
5278 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005280 if ((direction & RESIZE_HOR)
5281 && wndpl.rcNormalPosition.left < workarea_rect.left)
5282 OffsetRect(&wndpl.rcNormalPosition,
5283 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
Bram Moolenaarafa24992006-03-27 20:58:26 +00005285 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005286 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5287 OffsetRect(&wndpl.rcNormalPosition,
5288 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005290 if ((direction & RESIZE_VERT)
5291 && wndpl.rcNormalPosition.top < workarea_rect.top)
5292 OffsetRect(&wndpl.rcNormalPosition,
5293 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /* set window position - we should use SetWindowPlacement rather than
5296 * SetWindowPos as the MSDN docs say the coord systems returned by
5297 * these two are not compatible. */
5298 SetWindowPlacement(s_hwnd, &wndpl);
5299
5300 SetActiveWindow(s_hwnd);
5301 SetFocus(s_hwnd);
5302
5303#ifdef FEAT_MENU
5304 /* Menu may wrap differently now */
5305 gui_mswin_get_menu_height(!gui.starting);
5306#endif
5307}
5308
5309
5310 void
5311gui_mch_set_scrollbar_thumb(
5312 scrollbar_T *sb,
5313 long val,
5314 long size,
5315 long max)
5316{
5317 SCROLLINFO info;
5318
5319 sb->scroll_shift = 0;
5320 while (max > 32767)
5321 {
5322 max = (max + 1) >> 1;
5323 val >>= 1;
5324 size >>= 1;
5325 ++sb->scroll_shift;
5326 }
5327
5328 if (sb->scroll_shift > 0)
5329 ++size;
5330
5331 info.cbSize = sizeof(info);
5332 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5333 info.nPos = val;
5334 info.nMin = 0;
5335 info.nMax = max;
5336 info.nPage = size;
5337 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5338}
5339
5340
5341/*
5342 * Set the current text font.
5343 */
5344 void
5345gui_mch_set_font(GuiFont font)
5346{
5347 gui.currFont = font;
5348}
5349
5350
5351/*
5352 * Set the current text foreground color.
5353 */
5354 void
5355gui_mch_set_fg_color(guicolor_T color)
5356{
5357 gui.currFgColor = color;
5358}
5359
5360/*
5361 * Set the current text background color.
5362 */
5363 void
5364gui_mch_set_bg_color(guicolor_T color)
5365{
5366 gui.currBgColor = color;
5367}
5368
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005369/*
5370 * Set the current text special color.
5371 */
5372 void
5373gui_mch_set_sp_color(guicolor_T color)
5374{
5375 gui.currSpColor = color;
5376}
5377
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005378#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
5380 * Multi-byte handling, originally by Sung-Hoon Baek.
5381 * First static functions (no prototypes generated).
5382 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005383# ifdef _MSC_VER
5384# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5385# endif
5386# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
5388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 * handle WM_IME_NOTIFY message
5390 */
5391 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005392_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 LRESULT lResult = 0;
5395 HIMC hImc;
5396
5397 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5398 return lResult;
5399 switch (dwCommand)
5400 {
5401 case IMN_SETOPENSTATUS:
5402 if (pImmGetOpenStatus(hImc))
5403 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005404 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 im_set_position(gui.row, gui.col);
5406
5407 /* Disable langmap */
5408 State &= ~LANGMAP;
5409 if (State & INSERT)
5410 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005411#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 /* Unshown 'keymap' in status lines */
5413 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5414 {
5415 /* Save cursor position */
5416 int old_row = gui.row;
5417 int old_col = gui.col;
5418
5419 // This must be called here before
5420 // status_redraw_curbuf(), otherwise the mode
5421 // message may appear in the wrong position.
5422 showmode();
5423 status_redraw_curbuf();
5424 update_screen(0);
5425 /* Restore cursor position */
5426 gui.row = old_row;
5427 gui.col = old_col;
5428 }
5429#endif
5430 }
5431 }
5432 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005433 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 lResult = 0;
5435 break;
5436 }
5437 pImmReleaseContext(hWnd, hImc);
5438 return lResult;
5439}
5440
5441 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005442_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 char_u *ret;
5445 int len;
5446
5447 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5448 return 0;
5449
5450 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5451 if (ret != NULL)
5452 {
5453 add_to_input_buf_csi(ret, len);
5454 vim_free(ret);
5455 return 1;
5456 }
5457 return 0;
5458}
5459
5460/*
5461 * get the current composition string, in UCS-2; *lenp is the number of
5462 * *lenp is the number of Unicode characters.
5463 */
5464 static short_u *
5465GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5466{
5467 LONG ret;
5468 LPWSTR wbuf = NULL;
5469 char_u *buf;
5470
5471 if (!pImmGetContext)
5472 return NULL; /* no imm32.dll */
5473
5474 /* Try Unicode; this'll always work on NT regardless of codepage. */
5475 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5476 if (ret == 0)
5477 return NULL; /* empty */
5478
5479 if (ret > 0)
5480 {
5481 /* Allocate the requested buffer plus space for the NUL character. */
5482 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5483 if (wbuf != NULL)
5484 {
5485 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5486 *lenp = ret / sizeof(WCHAR);
5487 }
5488 return (short_u *)wbuf;
5489 }
5490
5491 /* ret < 0; we got an error, so try the ANSI version. This'll work
5492 * on 9x/ME, but only if the codepage happens to be set to whatever
5493 * we're inputting. */
5494 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5495 if (ret <= 0)
5496 return NULL; /* empty or error */
5497
5498 buf = alloc(ret);
5499 if (buf == NULL)
5500 return NULL;
5501 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5502
5503 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005504 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 vim_free(buf);
5506
5507 return (short_u *)wbuf;
5508}
5509
5510/*
5511 * void GetResultStr()
5512 *
5513 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5514 * get complete composition string
5515 */
5516 static char_u *
5517GetResultStr(HWND hwnd, int GCS, int *lenp)
5518{
5519 HIMC hIMC; /* Input context handle. */
5520 short_u *buf = NULL;
5521 char_u *convbuf = NULL;
5522
5523 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5524 return NULL;
5525
5526 /* Reads in the composition string. */
5527 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5528 if (buf == NULL)
5529 return NULL;
5530
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005531 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 pImmReleaseContext(hwnd, hIMC);
5533 vim_free(buf);
5534 return convbuf;
5535}
5536#endif
5537
5538/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005539#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
5541/*
5542 * set font to IM.
5543 */
5544 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005545im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 HIMC hImc;
5548
5549 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5550 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005551 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 pImmReleaseContext(s_hwnd, hImc);
5553 }
5554}
5555
5556/*
5557 * Notify cursor position to IM.
5558 */
5559 void
5560im_set_position(int row, int col)
5561{
5562 HIMC hImc;
5563
5564 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5565 {
5566 COMPOSITIONFORM cfs;
5567
5568 cfs.dwStyle = CFS_POINT;
5569 cfs.ptCurrentPos.x = FILL_X(col);
5570 cfs.ptCurrentPos.y = FILL_Y(row);
5571 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5572 pImmSetCompositionWindow(hImc, &cfs);
5573
5574 pImmReleaseContext(s_hwnd, hImc);
5575 }
5576}
5577
5578/*
5579 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5580 */
5581 void
5582im_set_active(int active)
5583{
5584 HIMC hImc;
5585 static HIMC hImcOld = (HIMC)0;
5586
5587 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5588 {
5589 if (p_imdisable)
5590 {
5591 if (hImcOld == (HIMC)0)
5592 {
5593 hImcOld = pImmGetContext(s_hwnd);
5594 if (hImcOld)
5595 pImmAssociateContext(s_hwnd, (HIMC)0);
5596 }
5597 active = FALSE;
5598 }
5599 else if (hImcOld != (HIMC)0)
5600 {
5601 pImmAssociateContext(s_hwnd, hImcOld);
5602 hImcOld = (HIMC)0;
5603 }
5604
5605 hImc = pImmGetContext(s_hwnd);
5606 if (hImc)
5607 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005608 /*
5609 * for Korean ime
5610 */
5611 HKL hKL = GetKeyboardLayout(0);
5612
5613 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5614 {
5615 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5616 static BOOL bSaved = FALSE;
5617
5618 if (active)
5619 {
5620 /* if we have a saved conversion status, restore it */
5621 if (bSaved)
5622 pImmSetConversionStatus(hImc, dwConversionSaved,
5623 dwSentenceSaved);
5624 bSaved = FALSE;
5625 }
5626 else
5627 {
5628 /* save conversion status and disable korean */
5629 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5630 &dwSentenceSaved))
5631 {
5632 bSaved = TRUE;
5633 pImmSetConversionStatus(hImc,
5634 dwConversionSaved & ~(IME_CMODE_NATIVE
5635 | IME_CMODE_FULLSHAPE),
5636 dwSentenceSaved);
5637 }
5638 }
5639 }
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 pImmSetOpenStatus(hImc, active);
5642 pImmReleaseContext(s_hwnd, hImc);
5643 }
5644 }
5645}
5646
5647/*
5648 * Get IM status. When IM is on, return not 0. Else return 0.
5649 */
5650 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005651im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 int status = 0;
5654 HIMC hImc;
5655
5656 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5657 {
5658 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5659 pImmReleaseContext(s_hwnd, hImc);
5660 }
5661 return status;
5662}
5663
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005664#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005666#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/* Win32 with GLOBAL IME */
5668
5669/*
5670 * Notify cursor position to IM.
5671 */
5672 void
5673im_set_position(int row, int col)
5674{
5675 /* Win32 with GLOBAL IME */
5676 POINT p;
5677
5678 p.x = FILL_X(col);
5679 p.y = FILL_Y(row);
5680 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5681 global_ime_set_position(&p);
5682}
5683
5684/*
5685 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5686 */
5687 void
5688im_set_active(int active)
5689{
5690 global_ime_set_status(active);
5691}
5692
5693/*
5694 * Get IM status. When IM is on, return not 0. Else return 0.
5695 */
5696 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005697im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
5699 return global_ime_get_status();
5700}
5701#endif
5702
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005703/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005704 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005705 */
5706 static void
5707latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5708{
5709 int c;
5710
Bram Moolenaarca003e12006-03-17 23:19:38 +00005711 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005712 {
5713 c = *text++;
5714 switch (c)
5715 {
5716 case 0xa4: c = 0x20ac; break; /* euro */
5717 case 0xa6: c = 0x0160; break; /* S hat */
5718 case 0xa8: c = 0x0161; break; /* S -hat */
5719 case 0xb4: c = 0x017d; break; /* Z hat */
5720 case 0xb8: c = 0x017e; break; /* Z -hat */
5721 case 0xbc: c = 0x0152; break; /* OE */
5722 case 0xbd: c = 0x0153; break; /* oe */
5723 case 0xbe: c = 0x0178; break; /* Y */
5724 }
5725 *unicodebuf++ = c;
5726 }
5727}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729#ifdef FEAT_RIGHTLEFT
5730/*
5731 * What is this for? In the case where you are using Win98 or Win2K or later,
5732 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5733 * reverses the string sent to the TextOut... family. This sucks, because we
5734 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5735 * way to tell Windblows not to do this!
5736 *
5737 * The short of it is that this 'RevOut' only gets called if you are running
5738 * one of the new, "improved" MS OSes, and only if you are running in
5739 * 'rightleft' mode. It makes display take *slightly* longer, but not
5740 * noticeably so.
5741 */
5742 static void
5743RevOut( HDC s_hdc,
5744 int col,
5745 int row,
5746 UINT foptions,
5747 CONST RECT *pcliprect,
5748 LPCTSTR text,
5749 UINT len,
5750 CONST INT *padding)
5751{
5752 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005754 for (ix = 0; ix < (int)len; ++ix)
5755 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5756 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758#endif
5759
Bram Moolenaar92467d32017-12-05 13:22:16 +01005760 static void
5761draw_line(
5762 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005763 int y1,
5764 int x2,
5765 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005766 COLORREF color)
5767{
5768#if defined(FEAT_DIRECTX)
5769 if (IS_ENABLE_DIRECTX())
5770 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5771 else
5772#endif
5773 {
5774 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5775 HPEN old_pen = SelectObject(s_hdc, hpen);
5776 MoveToEx(s_hdc, x1, y1, NULL);
5777 /* Note: LineTo() excludes the last pixel in the line. */
5778 LineTo(s_hdc, x2, y2);
5779 DeleteObject(SelectObject(s_hdc, old_pen));
5780 }
5781}
5782
5783 static void
5784set_pixel(
5785 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005786 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005787 COLORREF color)
5788{
5789#if defined(FEAT_DIRECTX)
5790 if (IS_ENABLE_DIRECTX())
5791 DWriteContext_SetPixel(s_dwc, x, y, color);
5792 else
5793#endif
5794 SetPixel(s_hdc, x, y, color);
5795}
5796
5797 static void
5798fill_rect(
5799 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005800 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005801 COLORREF color)
5802{
5803#if defined(FEAT_DIRECTX)
5804 if (IS_ENABLE_DIRECTX())
5805 DWriteContext_FillRect(s_dwc, rcp, color);
5806 else
5807#endif
5808 {
5809 HBRUSH hbr2;
5810
5811 if (hbr == NULL)
5812 hbr2 = CreateSolidBrush(color);
5813 else
5814 hbr2 = hbr;
5815 FillRect(s_hdc, rcp, hbr2);
5816 if (hbr == NULL)
5817 DeleteBrush(hbr2);
5818 }
5819}
5820
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 void
5822gui_mch_draw_string(
5823 int row,
5824 int col,
5825 char_u *text,
5826 int len,
5827 int flags)
5828{
5829 static int *padding = NULL;
5830 static int pad_size = 0;
5831 int i;
5832 const RECT *pcliprect = NULL;
5833 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 static WCHAR *unicodebuf = NULL;
5835 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005836 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 int y;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 /*
5841 * Italic and bold text seems to have an extra row of pixels at the bottom
5842 * (below where the bottom of the character should be). If we draw the
5843 * characters with a solid background, the top row of pixels in the
5844 * character below will be overwritten. We can fix this by filling in the
5845 * background ourselves, to the correct character proportions, and then
5846 * writing the character in transparent mode. Still have a problem when
5847 * the character is "_", which gets written on to the character below.
5848 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5849 * pixel in their slots, which fixes the problem with the bottom row of
5850 * pixels. We still need this code because otherwise the top row of pixels
5851 * becomes a problem. - webb.
5852 */
5853 static HBRUSH hbr_cache[2] = {NULL, NULL};
5854 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5855 static int brush_lru = 0;
5856 HBRUSH hbr;
5857 RECT rc;
5858
5859 if (!(flags & DRAW_TRANSP))
5860 {
5861 /*
5862 * Clear background first.
5863 * Note: FillRect() excludes right and bottom of rectangle.
5864 */
5865 rc.left = FILL_X(col);
5866 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 if (has_mbyte)
5868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02005870 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
5872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 rc.right = FILL_X(col + len);
5874 rc.bottom = FILL_Y(row + 1);
5875
5876 /* Cache the created brush, that saves a lot of time. We need two:
5877 * one for cursor background and one for the normal background. */
5878 if (gui.currBgColor == brush_color[0])
5879 {
5880 hbr = hbr_cache[0];
5881 brush_lru = 1;
5882 }
5883 else if (gui.currBgColor == brush_color[1])
5884 {
5885 hbr = hbr_cache[1];
5886 brush_lru = 0;
5887 }
5888 else
5889 {
5890 if (hbr_cache[brush_lru] != NULL)
5891 DeleteBrush(hbr_cache[brush_lru]);
5892 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
5893 brush_color[brush_lru] = gui.currBgColor;
5894 hbr = hbr_cache[brush_lru];
5895 brush_lru = !brush_lru;
5896 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005897
Bram Moolenaar92467d32017-12-05 13:22:16 +01005898 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 SetBkMode(s_hdc, TRANSPARENT);
5901
5902 /*
5903 * When drawing block cursor, prevent inverted character spilling
5904 * over character cell (can happen with bold/italic)
5905 */
5906 if (flags & DRAW_CURSOR)
5907 {
5908 pcliprect = &rc;
5909 foptions = ETO_CLIPPED;
5910 }
5911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 SetTextColor(s_hdc, gui.currFgColor);
5913 SelectFont(s_hdc, gui.currFont);
5914
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005915#ifdef FEAT_DIRECTX
5916 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01005917 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005918#endif
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
5921 {
5922 vim_free(padding);
5923 pad_size = Columns;
5924
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005925 /* Don't give an out-of-memory message here, it would call us
5926 * recursively. */
5927 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 if (padding != NULL)
5929 for (i = 0; i < pad_size; i++)
5930 padding[i] = gui.char_width;
5931 }
5932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 /*
5934 * We have to provide the padding argument because italic and bold versions
5935 * of fixed-width fonts are often one pixel or so wider than their normal
5936 * versions.
5937 * No check for DRAW_BOLD, Windows will have done it already.
5938 */
5939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 /* Check if there are any UTF-8 characters. If not, use normal text
5941 * output to speed up output. */
5942 if (enc_utf8)
5943 for (n = 0; n < len; ++n)
5944 if (text[n] >= 0x80)
5945 break;
5946
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005947#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005948 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
5949 * required that unicode drawing routine, currently. So this forces it
5950 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01005951 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005952 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005953#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005956 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005958 if ((enc_utf8
5959 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
5960 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 && (unicodebuf == NULL || len > unibuflen))
5962 {
5963 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005964 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965
5966 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00005967 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
5969 unibuflen = len;
5970 }
5971
5972 if (enc_utf8 && n < len && unicodebuf != NULL)
5973 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02005974 /* Output UTF-8 characters. Composing characters should be
5975 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005976 int i;
5977 int wlen; /* string length in words */
5978 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 int cells; /* cell width of string up to composing char */
5980 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005981 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00005983 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005984 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00005986 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005988 c = utf_ptr2char(text + i);
5989 if (c >= 0x10000)
5990 {
5991 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00005992 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
5993 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005994 }
5995 else
5996 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005997 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005998 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02005999
6000 if (utf_iscomposing(c))
6001 cw = 0;
6002 else
6003 {
6004 cw = utf_char2cells(c);
6005 if (cw > 2) /* don't use 4 for unprintable char */
6006 cw = 1;
6007 }
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (unicodepdy != NULL)
6010 {
6011 /* Use unicodepdy to make characters fit as we expect, even
6012 * when the font uses different widths (e.g., bold character
6013 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006014 if (c >= 0x10000)
6015 {
6016 unicodepdy[wlen - 2] = cw * gui.char_width;
6017 unicodepdy[wlen - 1] = 0;
6018 }
6019 else
6020 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006023 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006024 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006026#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006027 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006028 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006029 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006030 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006031 TEXT_X(col), TEXT_Y(row),
6032 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006033 gui.char_width, gui.currFgColor,
6034 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006035 }
6036 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006037#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006038 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6039 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 len = cells; /* used for underlining */
6041 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006042 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 {
6044 /* If we want to display codepage data, and the current CP is not the
6045 * ANSI one, we need to go via Unicode. */
6046 if (unicodebuf != NULL)
6047 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006048 if (enc_latin9)
6049 latin9_to_ucs(text, len, unicodebuf);
6050 else
6051 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 MB_PRECOMPOSED,
6053 (char *)text, len,
6054 (LPWSTR)unicodebuf, unibuflen);
6055 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006056 {
6057 /* Use unicodepdy to make characters fit as we expect, even
6058 * when the font uses different widths (e.g., bold character
6059 * is wider). */
6060 if (unicodepdy != NULL)
6061 {
6062 int i;
6063 int cw;
6064
6065 for (i = 0; i < len; ++i)
6066 {
6067 cw = utf_char2cells(unicodebuf[i]);
6068 if (cw > 2)
6069 cw = 1;
6070 unicodepdy[i] = cw * gui.char_width;
6071 }
6072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006074 foptions, pcliprect, unicodebuf, len, unicodepdy);
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 }
6078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
6080#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006081 /* Windows will mess up RL text, so we have to draw it character by
6082 * character. Only do this if RL is on, since it's slow. */
6083 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6085 foptions, pcliprect, (char *)text, len, padding);
6086 else
6087#endif
6088 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6089 foptions, pcliprect, (char *)text, len, padding);
6090 }
6091
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006092 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (flags & DRAW_UNDERL)
6094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 /* When p_linespace is 0, overwrite the bottom row of pixels.
6096 * Otherwise put the line just below the character. */
6097 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (p_linespace > 1)
6099 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006100 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006102
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006103 /* Strikethrough */
6104 if (flags & DRAW_STRIKE)
6105 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006106 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006107 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006108 }
6109
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006110 /* Undercurl */
6111 if (flags & DRAW_UNDERC)
6112 {
6113 int x;
6114 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006115 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006116
6117 y = FILL_Y(row + 1) - 1;
6118 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6119 {
6120 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006121 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006122 }
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124}
6125
6126
6127/*
6128 * Output routines.
6129 */
6130
6131/* Flush any output to the screen */
6132 void
6133gui_mch_flush(void)
6134{
6135# if defined(__BORLANDC__)
6136 /*
6137 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6138 * prototype declaration.
6139 * The compiler complains if __stdcall is not used in both declarations.
6140 */
6141 BOOL __stdcall GdiFlush(void);
6142# endif
6143
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006144#if defined(FEAT_DIRECTX)
6145 if (IS_ENABLE_DIRECTX())
6146 DWriteContext_Flush(s_dwc);
6147#endif
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 GdiFlush();
6150}
6151
6152 static void
6153clear_rect(RECT *rcp)
6154{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006155 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156}
6157
6158
Bram Moolenaarc716c302006-01-21 22:12:51 +00006159 void
6160gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6161{
6162 RECT workarea_rect;
6163
6164 get_work_area(&workarea_rect);
6165
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006166 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006167 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006168 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006169
6170 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6171 * the menubar for MSwin, we subtract it from the screen height, so that
6172 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006173 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006174 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006175 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006176 - GetSystemMetrics(SM_CYCAPTION)
6177#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006178 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006179#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006180 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006181}
6182
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#if defined(FEAT_MENU) || defined(PROTO)
6185/*
6186 * Add a sub menu to the menu bar.
6187 */
6188 void
6189gui_mch_add_menu(
6190 vimmenu_T *menu,
6191 int pos)
6192{
6193 vimmenu_T *parent = menu->parent;
6194
6195 menu->submenu_id = CreatePopupMenu();
6196 menu->id = s_menu_id++;
6197
6198 if (menu_is_menubar(menu->name))
6199 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006200 WCHAR *wn;
6201 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006203 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006204 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006207 infow.cbSize = sizeof(infow);
6208 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6209 | MIIM_SUBMENU;
6210 infow.dwItemData = (long_u)menu;
6211 infow.wID = menu->id;
6212 infow.fType = MFT_STRING;
6213 infow.dwTypeData = wn;
6214 infow.cch = (UINT)wcslen(wn);
6215 infow.hSubMenu = menu->submenu_id;
6216 InsertMenuItemW((parent == NULL)
6217 ? s_menuBar : parent->submenu_id,
6218 (UINT)pos, TRUE, &infow);
6219 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 }
6221
6222 /* Fix window size if menu may have wrapped */
6223 if (parent == NULL)
6224 gui_mswin_get_menu_height(!gui.starting);
6225#ifdef FEAT_TEAROFF
6226 else if (IsWindow(parent->tearoff_handle))
6227 rebuild_tearoff(parent);
6228#endif
6229}
6230
6231 void
6232gui_mch_show_popupmenu(vimmenu_T *menu)
6233{
6234 POINT mp;
6235
6236 (void)GetCursorPos((LPPOINT)&mp);
6237 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6238}
6239
6240 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006241gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 vimmenu_T *menu = gui_find_menu(path_name);
6244
6245 if (menu != NULL)
6246 {
6247 POINT p;
6248
6249 /* Find the position of the current cursor */
6250 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006251 if (mouse_pos)
6252 {
6253 int mx, my;
6254
6255 gui_mch_getmouse(&mx, &my);
6256 p.x += mx;
6257 p.y += my;
6258 }
6259 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006261 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6263 }
6264 msg_scroll = FALSE;
6265 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6266 }
6267}
6268
6269#if defined(FEAT_TEAROFF) || defined(PROTO)
6270/*
6271 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6272 * create it as a pseudo-"tearoff menu".
6273 */
6274 void
6275gui_make_tearoff(char_u *path_name)
6276{
6277 vimmenu_T *menu = gui_find_menu(path_name);
6278
6279 /* Found the menu, so tear it off. */
6280 if (menu != NULL)
6281 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6282}
6283#endif
6284
6285/*
6286 * Add a menu item to a menu
6287 */
6288 void
6289gui_mch_add_menu_item(
6290 vimmenu_T *menu,
6291 int idx)
6292{
6293 vimmenu_T *parent = menu->parent;
6294
6295 menu->id = s_menu_id++;
6296 menu->submenu_id = NULL;
6297
6298#ifdef FEAT_TEAROFF
6299 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6300 {
6301 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6302 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6303 }
6304 else
6305#endif
6306#ifdef FEAT_TOOLBAR
6307 if (menu_is_toolbar(parent->name))
6308 {
6309 TBBUTTON newtb;
6310
6311 vim_memset(&newtb, 0, sizeof(newtb));
6312 if (menu_is_separator(menu->name))
6313 {
6314 newtb.iBitmap = 0;
6315 newtb.fsStyle = TBSTYLE_SEP;
6316 }
6317 else
6318 {
6319 newtb.iBitmap = get_toolbar_bitmap(menu);
6320 newtb.fsStyle = TBSTYLE_BUTTON;
6321 }
6322 newtb.idCommand = menu->id;
6323 newtb.fsState = TBSTATE_ENABLED;
6324 newtb.iString = 0;
6325 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6326 (LPARAM)&newtb);
6327 menu->submenu_id = (HMENU)-1;
6328 }
6329 else
6330#endif
6331 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006332 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006334 wn = enc_to_utf16(menu->name, NULL);
6335 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006337 InsertMenuW(parent->submenu_id, (UINT)idx,
6338 (menu_is_separator(menu->name)
6339 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6340 (UINT)menu->id, wn);
6341 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343#ifdef FEAT_TEAROFF
6344 if (IsWindow(parent->tearoff_handle))
6345 rebuild_tearoff(parent);
6346#endif
6347 }
6348}
6349
6350/*
6351 * Destroy the machine specific menu widget.
6352 */
6353 void
6354gui_mch_destroy_menu(vimmenu_T *menu)
6355{
6356#ifdef FEAT_TOOLBAR
6357 /*
6358 * is this a toolbar button?
6359 */
6360 if (menu->submenu_id == (HMENU)-1)
6361 {
6362 int iButton;
6363
6364 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6365 (WPARAM)menu->id, 0);
6366 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6367 }
6368 else
6369#endif
6370 {
6371 if (menu->parent != NULL
6372 && menu_is_popup(menu->parent->dname)
6373 && menu->parent->submenu_id != NULL)
6374 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6375 else
6376 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6377 if (menu->submenu_id != NULL)
6378 DestroyMenu(menu->submenu_id);
6379#ifdef FEAT_TEAROFF
6380 if (IsWindow(menu->tearoff_handle))
6381 DestroyWindow(menu->tearoff_handle);
6382 if (menu->parent != NULL
6383 && menu->parent->children != NULL
6384 && IsWindow(menu->parent->tearoff_handle))
6385 {
6386 /* This menu must not show up when rebuilding the tearoff window. */
6387 menu->modes = 0;
6388 rebuild_tearoff(menu->parent);
6389 }
6390#endif
6391 }
6392}
6393
6394#ifdef FEAT_TEAROFF
6395 static void
6396rebuild_tearoff(vimmenu_T *menu)
6397{
6398 /*hackish*/
6399 char_u tbuf[128];
6400 RECT trect;
6401 RECT rct;
6402 RECT roct;
6403 int x, y;
6404
6405 HWND thwnd = menu->tearoff_handle;
6406
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006407 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (GetWindowRect(thwnd, &trect)
6409 && GetWindowRect(s_hwnd, &rct)
6410 && GetClientRect(s_hwnd, &roct))
6411 {
6412 x = trect.left - rct.left;
6413 y = (trect.top - rct.bottom + roct.bottom);
6414 }
6415 else
6416 {
6417 x = y = 0xffffL;
6418 }
6419 DestroyWindow(thwnd);
6420 if (menu->children != NULL)
6421 {
6422 gui_mch_tearoff(tbuf, menu, x, y);
6423 if (IsWindow(menu->tearoff_handle))
6424 (void) SetWindowPos(menu->tearoff_handle,
6425 NULL,
6426 (int)trect.left,
6427 (int)trect.top,
6428 0, 0,
6429 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6430 }
6431}
6432#endif /* FEAT_TEAROFF */
6433
6434/*
6435 * Make a menu either grey or not grey.
6436 */
6437 void
6438gui_mch_menu_grey(
6439 vimmenu_T *menu,
6440 int grey)
6441{
6442#ifdef FEAT_TOOLBAR
6443 /*
6444 * is this a toolbar button?
6445 */
6446 if (menu->submenu_id == (HMENU)-1)
6447 {
6448 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6449 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6450 }
6451 else
6452#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006453 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6454 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456#ifdef FEAT_TEAROFF
6457 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6458 {
6459 WORD menuID;
6460 HWND menuHandle;
6461
6462 /*
6463 * A tearoff button has changed state.
6464 */
6465 if (menu->children == NULL)
6466 menuID = (WORD)(menu->id);
6467 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006468 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6470 if (menuHandle)
6471 EnableWindow(menuHandle, !grey);
6472
6473 }
6474#endif
6475}
6476
6477#endif /* FEAT_MENU */
6478
6479
6480/* define some macros used to make the dialogue creation more readable */
6481
6482#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6483#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006484#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6487/*
6488 * stuff for dialogs
6489 */
6490
6491/*
6492 * The callback routine used by all the dialogs. Very simple. First,
6493 * acknowledges the INITDIALOG message so that Windows knows to do standard
6494 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6495 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6496 * number.
6497 */
6498 static LRESULT CALLBACK
6499dialog_callback(
6500 HWND hwnd,
6501 UINT message,
6502 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006503 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 if (message == WM_INITDIALOG)
6506 {
6507 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6508 /* Set focus to the dialog. Set the default button, if specified. */
6509 (void)SetFocus(hwnd);
6510 if (dialog_default_button > IDCANCEL)
6511 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006512 else
6513 /* We don't have a default, set focus on another element of the
6514 * dialog window, probably the icon */
6515 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 return FALSE;
6517 }
6518
6519 if (message == WM_COMMAND)
6520 {
6521 int button = LOWORD(wParam);
6522
6523 /* Don't end the dialog if something was selected that was
6524 * not a button.
6525 */
6526 if (button >= DLG_NONBUTTON_CONTROL)
6527 return TRUE;
6528
6529 /* If the edit box exists, copy the string. */
6530 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006531 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006532 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6533 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006534
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006535 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6536 p = utf16_to_enc(wp, NULL);
6537 vim_strncpy(s_textfield, p, IOSIZE);
6538 vim_free(p);
6539 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 /*
6543 * Need to check for IDOK because if the user just hits Return to
6544 * accept the default value, some reason this is what we get.
6545 */
6546 if (button == IDOK)
6547 {
6548 if (dialog_default_button > IDCANCEL)
6549 EndDialog(hwnd, dialog_default_button);
6550 }
6551 else
6552 EndDialog(hwnd, button - IDCANCEL);
6553 return TRUE;
6554 }
6555
6556 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6557 {
6558 EndDialog(hwnd, 0);
6559 return TRUE;
6560 }
6561 return FALSE;
6562}
6563
6564/*
6565 * Create a dialog dynamically from the parameter strings.
6566 * type = type of dialog (question, alert, etc.)
6567 * title = dialog title. may be NULL for default title.
6568 * message = text to display. Dialog sizes to accommodate it.
6569 * buttons = '\n' separated list of button captions, default first.
6570 * dfltbutton = number of default button.
6571 *
6572 * This routine returns 1 if the first button is pressed,
6573 * 2 for the second, etc.
6574 *
6575 * 0 indicates Esc was pressed.
6576 * -1 for unexpected error
6577 *
6578 * If stubbing out this fn, return 1.
6579 */
6580
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006581static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 "IDR_VIM",
6584 "IDR_VIM_ERROR",
6585 "IDR_VIM_ALERT",
6586 "IDR_VIM_INFO",
6587 "IDR_VIM_QUESTION"
6588};
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 int
6591gui_mch_dialog(
6592 int type,
6593 char_u *title,
6594 char_u *message,
6595 char_u *buttons,
6596 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006597 char_u *textfield,
6598 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006601 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 int numButtons;
6603 int *buttonWidths, *buttonPositions;
6604 int buttonYpos;
6605 int nchar, i;
6606 DWORD lStyle;
6607 int dlgwidth = 0;
6608 int dlgheight;
6609 int editboxheight;
6610 int horizWidth = 0;
6611 int msgheight;
6612 char_u *pstart;
6613 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006614 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 char_u *tbuffer;
6616 RECT rect;
6617 HWND hwnd;
6618 HDC hdc;
6619 HFONT font, oldFont;
6620 TEXTMETRIC fontInfo;
6621 int fontHeight;
6622 int textWidth, minButtonWidth, messageWidth;
6623 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006624 int maxDialogHeight;
6625 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 int vertical;
6627 int dlgPaddingX;
6628 int dlgPaddingY;
6629#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006630 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 int use_lfSysmenu = FALSE;
6632#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006633 garray_T ga;
6634 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636#ifndef NO_CONSOLE
6637 /* Don't output anything in silent mode ("ex -s") */
6638 if (silent_mode)
6639 return dfltbutton; /* return default option */
6640#endif
6641
Bram Moolenaar748bf032005-02-02 23:04:36 +00006642 if (s_hwnd == NULL)
6643 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 if ((type < 0) || (type > VIM_LAST_TYPE))
6646 type = 0;
6647
6648 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006649 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006650 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006651 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (p == NULL)
6654 return -1;
6655
6656 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006657 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6659 * const.
6660 */
6661 tbuffer = vim_strsave(buttons);
6662 if (tbuffer == NULL)
6663 return -1;
6664
6665 --dfltbutton; /* Change from one-based to zero-based */
6666
6667 /* Count buttons */
6668 numButtons = 1;
6669 for (i = 0; tbuffer[i] != '\0'; i++)
6670 {
6671 if (tbuffer[i] == DLG_BUTTON_SEP)
6672 numButtons++;
6673 }
6674 if (dfltbutton >= numButtons)
6675 dfltbutton = -1;
6676
6677 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006678 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 if (buttonWidths == NULL)
6680 return -1;
6681
6682 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006683 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 if (buttonPositions == NULL)
6685 return -1;
6686
6687 /*
6688 * Calculate how big the dialog must be.
6689 */
6690 hwnd = GetDesktopWindow();
6691 hdc = GetWindowDC(hwnd);
6692#ifdef USE_SYSMENU_FONT
6693 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6694 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006695 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 use_lfSysmenu = TRUE;
6697 }
6698 else
6699#endif
6700 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6701 VARIABLE_PITCH , DLG_FONT_NAME);
6702 if (s_usenewlook)
6703 {
6704 oldFont = SelectFont(hdc, font);
6705 dlgPaddingX = DLG_PADDING_X;
6706 dlgPaddingY = DLG_PADDING_Y;
6707 }
6708 else
6709 {
6710 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6711 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6712 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6713 }
6714 GetTextMetrics(hdc, &fontInfo);
6715 fontHeight = fontInfo.tmHeight;
6716
6717 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006718 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
6720 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006721 if (s_hwnd == NULL)
6722 {
6723 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724
Bram Moolenaarc716c302006-01-21 22:12:51 +00006725 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006726 get_work_area(&workarea_rect);
6727 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6728 if (maxDialogWidth > 600)
6729 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006730 /* Leave some room for the taskbar. */
6731 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006732 }
6733 else
6734 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006735 /* Use our own window for the size, unless it's very small. */
6736 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006737 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006738 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006739 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006740 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6741 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006742
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006743 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006744 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006745 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006746 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006747 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6748 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6749 }
6750
6751 /* Set dlgwidth to width of message.
6752 * Copy the message into "ga", changing NL to CR-NL and inserting line
6753 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 pstart = message;
6755 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006756 msgheight = 0;
6757 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 do
6759 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006760 msgheight += fontHeight; /* at least one line */
6761
6762 /* Need to figure out where to break the string. The system does it
6763 * at a word boundary, which would mean we can't compute the number of
6764 * wrapped lines. */
6765 textWidth = 0;
6766 last_white = NULL;
6767 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006768 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006769 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006770 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006771 && textWidth > maxDialogWidth * 3 / 4)
6772 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006773 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006774 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006776 /* Line will wrap. */
6777 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006778 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006779 textWidth = 0;
6780
6781 if (last_white != NULL)
6782 {
6783 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006784 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006785 pend = last_white + 1;
6786 last_white = NULL;
6787 }
6788 ga_append(&ga, '\r');
6789 ga_append(&ga, '\n');
6790 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006791 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006792
6793 while (--l >= 0)
6794 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006795 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006796 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006798
6799 ga_append(&ga, '\r');
6800 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 pstart = pend + 1;
6802 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006803
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006804 if (ga.ga_data != NULL)
6805 message = ga.ga_data;
6806
Bram Moolenaar748bf032005-02-02 23:04:36 +00006807 messageWidth += 10; /* roundoff space */
6808
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006810 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6811 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
6813 if (msgheight < DLG_ICON_HEIGHT)
6814 msgheight = DLG_ICON_HEIGHT;
6815
6816 /*
6817 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006818 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006820 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 if (!vertical)
6822 {
6823 // Place buttons horizontally if they fit.
6824 horizWidth = dlgPaddingX;
6825 pstart = tbuffer;
6826 i = 0;
6827 do
6828 {
6829 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6830 if (pend == NULL)
6831 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006832 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (textWidth < minButtonWidth)
6834 textWidth = minButtonWidth;
6835 textWidth += dlgPaddingX; /* Padding within button */
6836 buttonWidths[i] = textWidth;
6837 buttonPositions[i++] = horizWidth;
6838 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6839 pstart = pend + 1;
6840 } while (*pend != NUL);
6841
6842 if (horizWidth > maxDialogWidth)
6843 vertical = TRUE; // Too wide to fit on the screen.
6844 else if (horizWidth > dlgwidth)
6845 dlgwidth = horizWidth;
6846 }
6847
6848 if (vertical)
6849 {
6850 // Stack buttons vertically.
6851 pstart = tbuffer;
6852 do
6853 {
6854 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6855 if (pend == NULL)
6856 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006857 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 textWidth += dlgPaddingX; /* Padding within button */
6859 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
6860 if (textWidth > dlgwidth)
6861 dlgwidth = textWidth;
6862 pstart = pend + 1;
6863 } while (*pend != NUL);
6864 }
6865
6866 if (dlgwidth < DLG_MIN_WIDTH)
6867 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
6868
6869 /* start to fill in the dlgtemplate information. addressing by WORDs */
6870 if (s_usenewlook)
6871 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
6872 else
6873 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
6874
6875 add_long(lStyle);
6876 add_long(0); // (lExtendedStyle)
6877 pnumitems = p; /*save where the number of items must be stored*/
6878 add_word(0); // NumberOfItems(will change later)
6879 add_word(10); // x
6880 add_word(10); // y
6881 add_word(PixelToDialogX(dlgwidth)); // cx
6882
6883 // Dialog height.
6884 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02006885 dlgheight = msgheight + 2 * dlgPaddingY
6886 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 else
6888 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
6889
6890 // Dialog needs to be taller if contains an edit box.
6891 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
6892 if (textfield != NULL)
6893 dlgheight += editboxheight;
6894
Bram Moolenaara95d8232013-08-07 15:27:11 +02006895 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
6896 if (dlgheight > maxDialogHeight)
6897 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006898 msgheight = msgheight - (dlgheight - maxDialogHeight);
6899 dlgheight = maxDialogHeight;
6900 scroll_flag = WS_VSCROLL;
6901 /* Make sure scrollbar doesn't appear in the middle of the dialog */
6902 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02006903 }
6904
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 add_word(PixelToDialogY(dlgheight));
6906
6907 add_word(0); // Menu
6908 add_word(0); // Class
6909
6910 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006911 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
6912 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 p += nchar;
6914
6915 if (s_usenewlook)
6916 {
6917 /* do the font, since DS_3DLOOK doesn't work properly */
6918#ifdef USE_SYSMENU_FONT
6919 if (use_lfSysmenu)
6920 {
6921 /* point size */
6922 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
6923 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006924 wcscpy(p, lfSysmenu.lfFaceName);
6925 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
6927 else
6928#endif
6929 {
6930 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02006931 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933 p += nchar;
6934 }
6935
6936 buttonYpos = msgheight + 2 * dlgPaddingY;
6937
6938 if (textfield != NULL)
6939 buttonYpos += editboxheight;
6940
6941 pstart = tbuffer;
6942 if (!vertical)
6943 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
6944 for (i = 0; i < numButtons; i++)
6945 {
6946 /* get end of this button. */
6947 for ( pend = pstart;
6948 *pend && (*pend != DLG_BUTTON_SEP);
6949 pend++)
6950 ;
6951
6952 if (*pend)
6953 *pend = '\0';
6954
6955 /*
6956 * old NOTE:
6957 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
6958 * the focus to the first tab-able button and in so doing makes that
6959 * the default!! Grrr. Workaround: Make the default button the only
6960 * one with WS_TABSTOP style. Means user can't tab between buttons, but
6961 * he/she can use arrow keys.
6962 *
6963 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00006964 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 * dialog. Also needed for when the textfield is the default control.
6966 * It appears to work now (perhaps not on Win95?).
6967 */
6968 if (vertical)
6969 {
6970 p = add_dialog_element(p,
6971 (i == dfltbutton
6972 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6973 PixelToDialogX(DLG_VERT_PADDING_X),
6974 PixelToDialogY(buttonYpos /* TBK */
6975 + 2 * fontHeight * i),
6976 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
6977 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006978 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 else
6981 {
6982 p = add_dialog_element(p,
6983 (i == dfltbutton
6984 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
6985 PixelToDialogX(horizWidth + buttonPositions[i]),
6986 PixelToDialogY(buttonYpos), /* TBK */
6987 PixelToDialogX(buttonWidths[i]),
6988 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006989 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991 pstart = pend + 1; /*next button*/
6992 }
6993 *pnumitems += numButtons;
6994
6995 /* Vim icon */
6996 p = add_dialog_element(p, SS_ICON,
6997 PixelToDialogX(dlgPaddingX),
6998 PixelToDialogY(dlgPaddingY),
6999 PixelToDialogX(DLG_ICON_WIDTH),
7000 PixelToDialogY(DLG_ICON_HEIGHT),
7001 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7002 dlg_icons[type]);
7003
Bram Moolenaar748bf032005-02-02 23:04:36 +00007004 /* Dialog message */
7005 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7006 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7007 PixelToDialogY(dlgPaddingY),
7008 (WORD)(PixelToDialogX(messageWidth) + 1),
7009 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007010 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 /* Edit box */
7013 if (textfield != NULL)
7014 {
7015 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7016 PixelToDialogX(2 * dlgPaddingX),
7017 PixelToDialogY(2 * dlgPaddingY + msgheight),
7018 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7019 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007020 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 *pnumitems += 1;
7022 }
7023
7024 *pnumitems += 2;
7025
7026 SelectFont(hdc, oldFont);
7027 DeleteObject(font);
7028 ReleaseDC(hwnd, hdc);
7029
7030 /* Let the dialog_callback() function know which button to make default
7031 * If we have an edit box, make that the default. We also need to tell
7032 * dialog_callback() if this dialog contains an edit box or not. We do
7033 * this by setting s_textfield if it does.
7034 */
7035 if (textfield != NULL)
7036 {
7037 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7038 s_textfield = textfield;
7039 }
7040 else
7041 {
7042 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7043 s_textfield = NULL;
7044 }
7045
7046 /* show the dialog box modally and get a return value */
7047 nchar = (int)DialogBoxIndirect(
7048 s_hinst,
7049 (LPDLGTEMPLATE)pdlgtemplate,
7050 s_hwnd,
7051 (DLGPROC)dialog_callback);
7052
7053 LocalFree(LocalHandle(pdlgtemplate));
7054 vim_free(tbuffer);
7055 vim_free(buttonWidths);
7056 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007057 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058
7059 /* Focus back to our window (for when MDI is used). */
7060 (void)SetFocus(s_hwnd);
7061
7062 return nchar;
7063}
7064
7065#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007066
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067/*
7068 * Put a simple element (basic class) onto a dialog template in memory.
7069 * return a pointer to where the next item should be added.
7070 *
7071 * parameters:
7072 * lStyle = additional style flags
7073 * (be careful, NT3.51 & Win32s will ignore the new ones)
7074 * x,y = x & y positions IN DIALOG UNITS
7075 * w,h = width and height IN DIALOG UNITS
7076 * Id = ID used in messages
7077 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7078 * caption = usually text or resource name
7079 *
7080 * TODO: use the length information noted here to enable the dialog creation
7081 * routines to work out more exactly how much memory they need to alloc.
7082 */
7083 static PWORD
7084add_dialog_element(
7085 PWORD p,
7086 DWORD lStyle,
7087 WORD x,
7088 WORD y,
7089 WORD w,
7090 WORD h,
7091 WORD Id,
7092 WORD clss,
7093 const char *caption)
7094{
7095 int nchar;
7096
7097 p = lpwAlign(p); /* Align to dword boundary*/
7098 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7099 *p++ = LOWORD(lStyle);
7100 *p++ = HIWORD(lStyle);
7101 *p++ = 0; // LOWORD (lExtendedStyle)
7102 *p++ = 0; // HIWORD (lExtendedStyle)
7103 *p++ = x;
7104 *p++ = y;
7105 *p++ = w;
7106 *p++ = h;
7107 *p++ = Id; //9 or 10 words in all
7108
7109 *p++ = (WORD)0xffff;
7110 *p++ = clss; //2 more here
7111
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007112 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 p += nchar;
7114
7115 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7116
7117 return p; //total = 15+ (strlen(caption)) words
7118 // = 30 + 2(strlen(caption) bytes reqd
7119}
7120
7121
7122/*
7123 * Helper routine. Take an input pointer, return closest pointer that is
7124 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7125 */
7126 static LPWORD
7127lpwAlign(
7128 LPWORD lpIn)
7129{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007130 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007132 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 ul += 3;
7134 ul >>= 2;
7135 ul <<= 2;
7136 return (LPWORD)ul;
7137}
7138
7139/*
7140 * Helper routine. Takes second parameter as Ansi string, copies it to first
7141 * parameter as wide character (16-bits / char) string, and returns integer
7142 * number of wide characters (words) in string (including the trailing wide
7143 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007144 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7145 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 static int
7147nCopyAnsiToWideChar(
7148 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007149 LPSTR lpAnsiIn,
7150 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7154 int i;
7155 WCHAR *wn;
7156
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007157 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007160 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 if (wn != NULL)
7162 {
7163 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007164 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 vim_free(wn);
7166 }
7167 }
7168 if (nChar == 0)
7169 /* Use Win32 conversion function. */
7170 nChar = MultiByteToWideChar(
7171 enc_codepage > 0 ? enc_codepage : CP_ACP,
7172 MB_PRECOMPOSED,
7173 lpAnsiIn, len,
7174 lpWCStr, len);
7175 for (i = 0; i < nChar; ++i)
7176 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7177 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179 return nChar;
7180}
7181
7182
7183#ifdef FEAT_TEAROFF
7184/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007185 * Lookup menu handle from "menu_id".
7186 */
7187 static HMENU
7188tearoff_lookup_menuhandle(
7189 vimmenu_T *menu,
7190 WORD menu_id)
7191{
7192 for ( ; menu != NULL; menu = menu->next)
7193 {
7194 if (menu->modes == 0) /* this menu has just been deleted */
7195 continue;
7196 if (menu_is_separator(menu->dname))
7197 continue;
7198 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7199 return menu->submenu_id;
7200 }
7201 return NULL;
7202}
7203
7204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 * The callback function for all the modeless dialogs that make up the
7206 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7207 * thinking its menus have been clicked), and go away when closed.
7208 */
7209 static LRESULT CALLBACK
7210tearoff_callback(
7211 HWND hwnd,
7212 UINT message,
7213 WPARAM wParam,
7214 LPARAM lParam)
7215{
7216 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007217 {
7218 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 /* May show the mouse pointer again. */
7223 HandleMouseHide(message, lParam);
7224
7225 if (message == WM_COMMAND)
7226 {
7227 if ((WORD)(LOWORD(wParam)) & 0x8000)
7228 {
7229 POINT mp;
7230 RECT rect;
7231
7232 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7233 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007234 vimmenu_T *menu;
7235
7236 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007238 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7240 (int)rect.right - 8,
7241 (int)mp.y,
7242 (int)0, /*reserved param*/
7243 s_hwnd,
7244 NULL);
7245 /*
7246 * NOTE: The pop-up menu can eat the mouse up event.
7247 * We deal with this in normal.c.
7248 */
7249 }
7250 }
7251 else
7252 /* Pass on messages to the main Vim window */
7253 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7254 /*
7255 * Give main window the focus back: this is so after
7256 * choosing a tearoff button you can start typing again
7257 * straight away.
7258 */
7259 (void)SetFocus(s_hwnd);
7260 return TRUE;
7261 }
7262 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7263 {
7264 DestroyWindow(hwnd);
7265 return TRUE;
7266 }
7267
7268 /* When moved around, give main window the focus back. */
7269 if (message == WM_EXITSIZEMOVE)
7270 (void)SetActiveWindow(s_hwnd);
7271
7272 return FALSE;
7273}
7274#endif
7275
7276
7277/*
7278 * Decide whether to use the "new look" (small, non-bold font) or the "old
7279 * look" (big, clanky font) for dialogs, and work out a few values for use
7280 * later accordingly.
7281 */
7282 static void
7283get_dialog_font_metrics(void)
7284{
7285 HDC hdc;
7286 HFONT hfontTools = 0;
7287 DWORD dlgFontSize;
7288 SIZE size;
7289#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007290 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291#endif
7292
7293 s_usenewlook = FALSE;
7294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007296 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007297 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007298 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299#endif
7300 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7301 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7302
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007303 if (hfontTools)
7304 {
7305 hdc = GetDC(s_hwnd);
7306 SelectObject(hdc, hfontTools);
7307 /*
7308 * GetTextMetrics() doesn't return the right value in
7309 * tmAveCharWidth, so we have to figure out the dialog base units
7310 * ourselves.
7311 */
7312 GetTextExtentPoint(hdc,
7313 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7314 52, &size);
7315 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007317 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7318 s_dlgfntheight = (WORD)size.cy;
7319 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321
7322 if (!s_usenewlook)
7323 {
7324 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7325 s_dlgfntwidth = LOWORD(dlgFontSize);
7326 s_dlgfntheight = HIWORD(dlgFontSize);
7327 }
7328}
7329
7330#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7331/*
7332 * Create a pseudo-"tearoff menu" based on the child
7333 * items of a given menu pointer.
7334 */
7335 static void
7336gui_mch_tearoff(
7337 char_u *title,
7338 vimmenu_T *menu,
7339 int initX,
7340 int initY)
7341{
7342 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7343 int template_len;
7344 int nchar, textWidth, submenuWidth;
7345 DWORD lStyle;
7346 DWORD lExtendedStyle;
7347 WORD dlgwidth;
7348 WORD menuID;
7349 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007350 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 vimmenu_T *the_menu = menu;
7352 HWND hwnd;
7353 HDC hdc;
7354 HFONT font, oldFont;
7355 int col, spaceWidth, len;
7356 int columnWidths[2];
7357 char_u *label, *text;
7358 int acLen = 0;
7359 int nameLen;
7360 int padding0, padding1, padding2 = 0;
7361 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007362 int x;
7363 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007365 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 int use_lfSysmenu = FALSE;
7367#endif
7368
7369 /*
7370 * If this menu is already torn off, move it to the mouse position.
7371 */
7372 if (IsWindow(menu->tearoff_handle))
7373 {
7374 POINT mp;
7375 if (GetCursorPos((LPPOINT)&mp))
7376 {
7377 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7378 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7379 }
7380 return;
7381 }
7382
7383 /*
7384 * Create a new tearoff.
7385 */
7386 if (*title == MNU_HIDDEN_CHAR)
7387 title++;
7388
7389 /* Allocate memory to store the dialog template. It's made bigger when
7390 * needed. */
7391 template_len = DLG_ALLOC_SIZE;
7392 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7393 if (p == NULL)
7394 return;
7395
7396 hwnd = GetDesktopWindow();
7397 hdc = GetWindowDC(hwnd);
7398#ifdef USE_SYSMENU_FONT
7399 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7400 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007401 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 use_lfSysmenu = TRUE;
7403 }
7404 else
7405#endif
7406 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7407 VARIABLE_PITCH , DLG_FONT_NAME);
7408 if (s_usenewlook)
7409 oldFont = SelectFont(hdc, font);
7410 else
7411 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7412
7413 /* Calculate width of a single space. Used for padding columns to the
7414 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007415 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 /* Figure out max width of the text column, the accelerator column and the
7418 * optional submenu column. */
7419 submenuWidth = 0;
7420 for (col = 0; col < 2; col++)
7421 {
7422 columnWidths[col] = 0;
7423 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7424 {
7425 /* Use "dname" here to compute the width of the visible text. */
7426 text = (col == 0) ? pmenu->dname : pmenu->actext;
7427 if (text != NULL && *text != NUL)
7428 {
7429 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7430 if (textWidth > columnWidths[col])
7431 columnWidths[col] = textWidth;
7432 }
7433 if (pmenu->children != NULL)
7434 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7435 }
7436 }
7437 if (columnWidths[1] == 0)
7438 {
7439 /* no accelerators */
7440 if (submenuWidth != 0)
7441 columnWidths[0] += submenuWidth;
7442 else
7443 columnWidths[0] += spaceWidth;
7444 }
7445 else
7446 {
7447 /* there is an accelerator column */
7448 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7449 columnWidths[1] += submenuWidth;
7450 }
7451
7452 /*
7453 * Now find the total width of our 'menu'.
7454 */
7455 textWidth = columnWidths[0] + columnWidths[1];
7456 if (submenuWidth != 0)
7457 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007458 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7460 textWidth += submenuWidth;
7461 }
7462 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7463 if (textWidth > dlgwidth)
7464 dlgwidth = textWidth;
7465 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 /* start to fill in the dlgtemplate information. addressing by WORDs */
7468 if (s_usenewlook)
7469 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7470 else
7471 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7472
7473 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7474 *p++ = LOWORD(lStyle);
7475 *p++ = HIWORD(lStyle);
7476 *p++ = LOWORD(lExtendedStyle);
7477 *p++ = HIWORD(lExtendedStyle);
7478 pnumitems = p; /* save where the number of items must be stored */
7479 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007480 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007482 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 else
7484 *p++ = PixelToDialogX(initX); // x
7485 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007486 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
7488 *p++ = PixelToDialogY(initY); // y
7489 *p++ = PixelToDialogX(dlgwidth); // cx
7490 ptrueheight = p;
7491 *p++ = 0; // dialog height: changed later anyway
7492 *p++ = 0; // Menu
7493 *p++ = 0; // Class
7494
7495 /* copy the title of the dialog */
7496 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007497 ? (LPSTR)title
7498 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 p += nchar;
7500
7501 if (s_usenewlook)
7502 {
7503 /* do the font, since DS_3DLOOK doesn't work properly */
7504#ifdef USE_SYSMENU_FONT
7505 if (use_lfSysmenu)
7506 {
7507 /* point size */
7508 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7509 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007510 wcscpy(p, lfSysmenu.lfFaceName);
7511 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 }
7513 else
7514#endif
7515 {
7516 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007517 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 }
7519 p += nchar;
7520 }
7521
7522 /*
7523 * Loop over all the items in the menu.
7524 * But skip over the tearbar.
7525 */
7526 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7527 menu = menu->children->next;
7528 else
7529 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007530 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 for ( ; menu != NULL; menu = menu->next)
7532 {
7533 if (menu->modes == 0) /* this menu has just been deleted */
7534 continue;
7535 if (menu_is_separator(menu->dname))
7536 {
7537 sepPadding += 3;
7538 continue;
7539 }
7540
7541 /* Check if there still is plenty of room in the template. Make it
7542 * larger when needed. */
7543 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7544 {
7545 WORD *newp;
7546
7547 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7548 if (newp != NULL)
7549 {
7550 template_len += 4096;
7551 mch_memmove(newp, pdlgtemplate,
7552 (char *)p - (char *)pdlgtemplate);
7553 p = newp + (p - pdlgtemplate);
7554 pnumitems = newp + (pnumitems - pdlgtemplate);
7555 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7556 LocalFree(LocalHandle(pdlgtemplate));
7557 pdlgtemplate = newp;
7558 }
7559 }
7560
7561 /* Figure out minimal length of this menu label. Use "name" for the
7562 * actual text, "dname" for estimating the displayed size. "name"
7563 * has "&a" for mnemonic and includes the accelerator. */
7564 len = nameLen = (int)STRLEN(menu->name);
7565 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7566 (int)STRLEN(menu->dname))) / spaceWidth;
7567 len += padding0;
7568
7569 if (menu->actext != NULL)
7570 {
7571 acLen = (int)STRLEN(menu->actext);
7572 len += acLen;
7573 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7574 }
7575 else
7576 textWidth = 0;
7577 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7578 len += padding1;
7579
7580 if (menu->children == NULL)
7581 {
7582 padding2 = submenuWidth / spaceWidth;
7583 len += padding2;
7584 menuID = (WORD)(menu->id);
7585 }
7586 else
7587 {
7588 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007589 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 }
7591
7592 /* Allocate menu label and fill it in */
7593 text = label = alloc((unsigned)len + 1);
7594 if (label == NULL)
7595 break;
7596
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007597 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7599 if (text == NULL)
7600 text = label + nameLen; /* no actext, use whole name */
7601 while (padding0-- > 0)
7602 *text++ = ' ';
7603 if (menu->actext != NULL)
7604 {
7605 STRNCPY(text, menu->actext, acLen);
7606 text += acLen;
7607 }
7608 while (padding1-- > 0)
7609 *text++ = ' ';
7610 if (menu->children != NULL)
7611 {
7612 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7613 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7614 }
7615 else
7616 {
7617 while (padding2-- > 0)
7618 *text++ = ' ';
7619 }
7620 *text = NUL;
7621
7622 /*
7623 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7624 * W95/NT4 it makes the tear-off look more like a menu.
7625 */
7626 p = add_dialog_element(p,
7627 BS_PUSHBUTTON|BS_LEFT,
7628 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7629 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7630 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7631 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007632 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 vim_free(label);
7634 (*pnumitems)++;
7635 }
7636
7637 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7638
7639
7640 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007641 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 s_hinst,
7643 (LPDLGTEMPLATE)pdlgtemplate,
7644 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007645 (DLGPROC)tearoff_callback,
7646 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647
7648 LocalFree(LocalHandle(pdlgtemplate));
7649 SelectFont(hdc, oldFont);
7650 DeleteObject(font);
7651 ReleaseDC(hwnd, hdc);
7652
7653 /*
7654 * Reassert ourselves as the active window. This is so that after creating
7655 * a tearoff, the user doesn't have to click with the mouse just to start
7656 * typing again!
7657 */
7658 (void)SetActiveWindow(s_hwnd);
7659
7660 /* make sure the right buttons are enabled */
7661 force_menu_update = TRUE;
7662}
7663#endif
7664
7665#if defined(FEAT_TOOLBAR) || defined(PROTO)
7666#include "gui_w32_rc.h"
7667
7668/* This not defined in older SDKs */
7669# ifndef TBSTYLE_FLAT
7670# define TBSTYLE_FLAT 0x0800
7671# endif
7672
7673/*
7674 * Create the toolbar, initially unpopulated.
7675 * (just like the menu, there are no defaults, it's all
7676 * set up through menu.vim)
7677 */
7678 static void
7679initialise_toolbar(void)
7680{
7681 InitCommonControls();
7682 s_toolbarhwnd = CreateToolbarEx(
7683 s_hwnd,
7684 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7685 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007686 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 s_hinst,
7688 IDR_TOOLBAR1, // id of initial bitmap
7689 NULL,
7690 0, // initial number of buttons
7691 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7692 TOOLBAR_BUTTON_HEIGHT,
7693 TOOLBAR_BUTTON_WIDTH,
7694 TOOLBAR_BUTTON_HEIGHT,
7695 sizeof(TBBUTTON)
7696 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007697 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
7699 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7700}
7701
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007702 static LRESULT CALLBACK
7703toolbar_wndproc(
7704 HWND hwnd,
7705 UINT uMsg,
7706 WPARAM wParam,
7707 LPARAM lParam)
7708{
7709 HandleMouseHide(uMsg, lParam);
7710 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7711}
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 static int
7714get_toolbar_bitmap(vimmenu_T *menu)
7715{
7716 int i = -1;
7717
7718 /*
7719 * Check user bitmaps first, unless builtin is specified.
7720 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007721 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {
7723 char_u fname[MAXPATHL];
7724 HANDLE hbitmap = NULL;
7725
7726 if (menu->iconfile != NULL)
7727 {
7728 gui_find_iconfile(menu->iconfile, fname, "bmp");
7729 hbitmap = LoadImage(
7730 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007731 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 IMAGE_BITMAP,
7733 TOOLBAR_BUTTON_WIDTH,
7734 TOOLBAR_BUTTON_HEIGHT,
7735 LR_LOADFROMFILE |
7736 LR_LOADMAP3DCOLORS
7737 );
7738 }
7739
7740 /*
7741 * If the LoadImage call failed, or the "icon=" file
7742 * didn't exist or wasn't specified, try the menu name
7743 */
7744 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007745 && (gui_find_bitmap(
7746#ifdef FEAT_MULTI_LANG
7747 menu->en_dname != NULL ? menu->en_dname :
7748#endif
7749 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 hbitmap = LoadImage(
7751 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007752 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 IMAGE_BITMAP,
7754 TOOLBAR_BUTTON_WIDTH,
7755 TOOLBAR_BUTTON_HEIGHT,
7756 LR_LOADFROMFILE |
7757 LR_LOADMAP3DCOLORS
7758 );
7759
7760 if (hbitmap != NULL)
7761 {
7762 TBADDBITMAP tbAddBitmap;
7763
7764 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007765 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
7767 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7768 (WPARAM)1, (LPARAM)&tbAddBitmap);
7769 /* i will be set to -1 if it fails */
7770 }
7771 }
7772 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7773 i = menu->iconidx;
7774
7775 return i;
7776}
7777#endif
7778
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007779#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7780 static void
7781initialise_tabline(void)
7782{
7783 InitCommonControls();
7784
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007785 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007786 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007787 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
7788 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007789 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007790
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007791 gui.tabline_height = TABLINE_HEIGHT;
7792
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007793# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007794 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007795# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007796}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007797
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007798/*
7799 * Get tabpage_T from POINT.
7800 */
7801 static tabpage_T *
7802GetTabFromPoint(
7803 HWND hWnd,
7804 POINT pt)
7805{
7806 tabpage_T *ptp = NULL;
7807
7808 if (gui_mch_showing_tabline())
7809 {
7810 TCHITTESTINFO htinfo;
7811 htinfo.pt = pt;
7812 /* ignore if a window under cusor is not tabcontrol. */
7813 if (s_tabhwnd == hWnd)
7814 {
7815 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7816 if (idx != -1)
7817 ptp = find_tabpage(idx + 1);
7818 }
7819 }
7820 return ptp;
7821}
7822
7823static POINT s_pt = {0, 0};
7824static HCURSOR s_hCursor = NULL;
7825
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007826 static LRESULT CALLBACK
7827tabline_wndproc(
7828 HWND hwnd,
7829 UINT uMsg,
7830 WPARAM wParam,
7831 LPARAM lParam)
7832{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007833 POINT pt;
7834 tabpage_T *tp;
7835 RECT rect;
7836 int nCenter;
7837 int idx0;
7838 int idx1;
7839
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007840 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007841
7842 switch (uMsg)
7843 {
7844 case WM_LBUTTONDOWN:
7845 {
7846 s_pt.x = GET_X_LPARAM(lParam);
7847 s_pt.y = GET_Y_LPARAM(lParam);
7848 SetCapture(hwnd);
7849 s_hCursor = GetCursor(); /* backup default cursor */
7850 break;
7851 }
7852 case WM_MOUSEMOVE:
7853 if (GetCapture() == hwnd
7854 && ((wParam & MK_LBUTTON)) != 0)
7855 {
7856 pt.x = GET_X_LPARAM(lParam);
7857 pt.y = s_pt.y;
7858 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
7859 {
7860 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7861
7862 tp = GetTabFromPoint(hwnd, pt);
7863 if (tp != NULL)
7864 {
7865 idx0 = tabpage_index(curtab) - 1;
7866 idx1 = tabpage_index(tp) - 1;
7867
7868 TabCtrl_GetItemRect(hwnd, idx1, &rect);
7869 nCenter = rect.left + (rect.right - rect.left) / 2;
7870
7871 /* Check if the mouse cursor goes over the center of
7872 * the next tab to prevent "flickering". */
7873 if ((idx0 < idx1) && (nCenter < pt.x))
7874 {
7875 tabpage_move(idx1 + 1);
7876 update_screen(0);
7877 }
7878 else if ((idx1 < idx0) && (pt.x < nCenter))
7879 {
7880 tabpage_move(idx1);
7881 update_screen(0);
7882 }
7883 }
7884 }
7885 }
7886 break;
7887 case WM_LBUTTONUP:
7888 {
7889 if (GetCapture() == hwnd)
7890 {
7891 SetCursor(s_hCursor);
7892 ReleaseCapture();
7893 }
7894 break;
7895 }
7896 default:
7897 break;
7898 }
7899
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007900 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
7901}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007902#endif
7903
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
7905/*
7906 * Make the GUI window come to the foreground.
7907 */
7908 void
7909gui_mch_set_foreground(void)
7910{
7911 if (IsIconic(s_hwnd))
7912 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
7913 SetForegroundWindow(s_hwnd);
7914}
7915#endif
7916
7917#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
7918 static void
7919dyn_imm_load(void)
7920{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02007921 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (hLibImm == NULL)
7923 return;
7924
7925 pImmGetCompositionStringA
7926 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
7927 pImmGetCompositionStringW
7928 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
7929 pImmGetContext
7930 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
7931 pImmAssociateContext
7932 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
7933 pImmReleaseContext
7934 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
7935 pImmGetOpenStatus
7936 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
7937 pImmSetOpenStatus
7938 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007939 pImmGetCompositionFontW
7940 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
7941 pImmSetCompositionFontW
7942 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 pImmSetCompositionWindow
7944 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
7945 pImmGetConversionStatus
7946 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00007947 pImmSetConversionStatus
7948 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949
7950 if ( pImmGetCompositionStringA == NULL
7951 || pImmGetCompositionStringW == NULL
7952 || pImmGetContext == NULL
7953 || pImmAssociateContext == NULL
7954 || pImmReleaseContext == NULL
7955 || pImmGetOpenStatus == NULL
7956 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007957 || pImmGetCompositionFontW == NULL
7958 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00007960 || pImmGetConversionStatus == NULL
7961 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 {
7963 FreeLibrary(hLibImm);
7964 hLibImm = NULL;
7965 pImmGetContext = NULL;
7966 return;
7967 }
7968
7969 return;
7970}
7971
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#endif
7973
7974#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7975
7976# ifdef FEAT_XPM_W32
7977# define IMAGE_XPM 100
7978# endif
7979
7980typedef struct _signicon_t
7981{
7982 HANDLE hImage;
7983 UINT uType;
7984#ifdef FEAT_XPM_W32
7985 HANDLE hShape; /* Mask bitmap handle */
7986#endif
7987} signicon_t;
7988
7989 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01007990gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 signicon_t *sign;
7993 int x, y, w, h;
7994
7995 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
7996 return;
7997
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01007998#if defined(FEAT_DIRECTX)
7999 if (IS_ENABLE_DIRECTX())
8000 DWriteContext_Flush(s_dwc);
8001#endif
8002
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 x = TEXT_X(col);
8004 y = TEXT_Y(row);
8005 w = gui.char_width * 2;
8006 h = gui.char_height;
8007 switch (sign->uType)
8008 {
8009 case IMAGE_BITMAP:
8010 {
8011 HDC hdcMem;
8012 HBITMAP hbmpOld;
8013
8014 hdcMem = CreateCompatibleDC(s_hdc);
8015 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8016 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8017 SelectObject(hdcMem, hbmpOld);
8018 DeleteDC(hdcMem);
8019 }
8020 break;
8021 case IMAGE_ICON:
8022 case IMAGE_CURSOR:
8023 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8024 break;
8025#ifdef FEAT_XPM_W32
8026 case IMAGE_XPM:
8027 {
8028 HDC hdcMem;
8029 HBITMAP hbmpOld;
8030
8031 hdcMem = CreateCompatibleDC(s_hdc);
8032 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8033 /* Make hole */
8034 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8035
8036 SelectObject(hdcMem, sign->hImage);
8037 /* Paint sign */
8038 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8039 SelectObject(hdcMem, hbmpOld);
8040 DeleteDC(hdcMem);
8041 }
8042 break;
8043#endif
8044 }
8045}
8046
8047 static void
8048close_signicon_image(signicon_t *sign)
8049{
8050 if (sign)
8051 switch (sign->uType)
8052 {
8053 case IMAGE_BITMAP:
8054 DeleteObject((HGDIOBJ)sign->hImage);
8055 break;
8056 case IMAGE_CURSOR:
8057 DestroyCursor((HCURSOR)sign->hImage);
8058 break;
8059 case IMAGE_ICON:
8060 DestroyIcon((HICON)sign->hImage);
8061 break;
8062#ifdef FEAT_XPM_W32
8063 case IMAGE_XPM:
8064 DeleteObject((HBITMAP)sign->hImage);
8065 DeleteObject((HBITMAP)sign->hShape);
8066 break;
8067#endif
8068 }
8069}
8070
8071 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008072gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 signicon_t sign, *psign;
8075 char_u *ext;
8076
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008078 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 if (ext > signfile)
8080 {
8081 int do_load = 1;
8082
8083 if (!STRICMP(ext, ".bmp"))
8084 sign.uType = IMAGE_BITMAP;
8085 else if (!STRICMP(ext, ".ico"))
8086 sign.uType = IMAGE_ICON;
8087 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8088 sign.uType = IMAGE_CURSOR;
8089 else
8090 do_load = 0;
8091
8092 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008093 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 gui.char_width * 2, gui.char_height,
8095 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8096#ifdef FEAT_XPM_W32
8097 if (!STRICMP(ext, ".xpm"))
8098 {
8099 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008100 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8101 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 }
8103#endif
8104 }
8105
8106 psign = NULL;
8107 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8108 != NULL)
8109 *psign = sign;
8110
8111 if (!psign)
8112 {
8113 if (sign.hImage)
8114 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008115 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 }
8117 return (void *)psign;
8118
8119}
8120
8121 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008122gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123{
8124 if (sign)
8125 {
8126 close_signicon_image((signicon_t *)sign);
8127 vim_free(sign);
8128 }
8129}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008132#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133
8134/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008135 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008137 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8139 * to get current mouse position).
8140 *
8141 * Trying to use as more Windows services as possible, and as less
8142 * IE version as possible :)).
8143 *
8144 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8145 * BalloonEval struct.
8146 * 2) Enable/Disable simply create/kill BalloonEval Timer
8147 * 3) When there was enough inactivity, timer procedure posts
8148 * async request to debugger
8149 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8150 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008151 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 */
8153
Bram Moolenaar45360022005-07-21 21:08:21 +00008154/*
8155 * determine whether installed Common Controls support multiline tooltips
8156 * (i.e. their version is >= 4.70
8157 */
8158 int
8159multiline_balloon_available(void)
8160{
8161 HINSTANCE hDll;
8162 static char comctl_dll[] = "comctl32.dll";
8163 static int multiline_tip = MAYBE;
8164
8165 if (multiline_tip != MAYBE)
8166 return multiline_tip;
8167
8168 hDll = GetModuleHandle(comctl_dll);
8169 if (hDll != NULL)
8170 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008171 DLLGETVERSIONPROC pGetVer;
8172 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008173
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008174 if (pGetVer != NULL)
8175 {
8176 DLLVERSIONINFO dvi;
8177 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008178
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008179 ZeroMemory(&dvi, sizeof(dvi));
8180 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008181
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008182 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008183
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008184 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008185 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008186 || (dvi.dwMajorVersion == 4
8187 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008188 {
8189 multiline_tip = TRUE;
8190 return multiline_tip;
8191 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008192 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008193 else
8194 {
8195 /* there is chance we have ancient CommCtl 4.70
8196 which doesn't export DllGetVersion */
8197 DWORD dwHandle = 0;
8198 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8199 if (len > 0)
8200 {
8201 VS_FIXEDFILEINFO *ver;
8202 UINT vlen = 0;
8203 void *data = alloc(len);
8204
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008205 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008206 && GetFileVersionInfo(comctl_dll, 0, len, data)
8207 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8208 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008209 && HIWORD(ver->dwFileVersionMS) > 4)
8210 || ((HIWORD(ver->dwFileVersionMS) == 4
8211 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008212 {
8213 vim_free(data);
8214 multiline_tip = TRUE;
8215 return multiline_tip;
8216 }
8217 vim_free(data);
8218 }
8219 }
8220 }
8221 multiline_tip = FALSE;
8222 return multiline_tip;
8223}
8224
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008225 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008226make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008227{
8228 TOOLINFOW *pti;
8229 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008230
8231 if (multiline_balloon_available() == TRUE)
8232 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8233 else
8234 ToolInfoSize = sizeof(TOOLINFOW);
8235
8236 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8237 if (pti == NULL)
8238 return;
8239
8240 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8241 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8242 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8243 beval->target, NULL, s_hinst, NULL);
8244
8245 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8246 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8247
8248 pti->cbSize = ToolInfoSize;
8249 pti->uFlags = TTF_SUBCLASS;
8250 pti->hwnd = beval->target;
8251 pti->hinst = 0; // Don't use string resources
8252 pti->uId = ID_BEVAL_TOOLTIP;
8253
8254 if (multiline_balloon_available() == TRUE)
8255 {
8256 RECT rect;
8257 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8258 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008259 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8260 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008261 // switch multiline tooltips on
8262 if (GetClientRect(s_textArea, &rect))
8263 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8264 (LPARAM)rect.right);
8265 }
8266 else
8267 {
8268 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008269 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8270 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008271 }
8272
8273 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8274 pti->rect.left = pt.x - 3;
8275 pti->rect.top = pt.y - 3;
8276 pti->rect.right = pt.x + 3;
8277 pti->rect.bottom = pt.y + 3;
8278
8279 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8280 // Make tooltip appear sooner.
8281 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8282 // I've performed some tests and it seems the longest possible life time
8283 // of tooltip is 30 seconds.
8284 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8285 /*
8286 * HACK: force tooltip to appear, because it'll not appear until
8287 * first mouse move. D*mn M$
8288 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8289 */
8290 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8291 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8292 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008293}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008294
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008296delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008298 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299}
8300
8301 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008302BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008303 HWND hwnd UNUSED,
8304 UINT uMsg UNUSED,
8305 UINT_PTR idEvent UNUSED,
8306 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307{
8308 POINT pt;
8309 RECT rect;
8310
8311 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8312 return;
8313
8314 GetCursorPos(&pt);
8315 if (WindowFromPoint(pt) != s_textArea)
8316 return;
8317
8318 ScreenToClient(s_textArea, &pt);
8319 GetClientRect(s_textArea, &rect);
8320 if (!PtInRect(&rect, pt))
8321 return;
8322
8323 if (LastActivity > 0
8324 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8325 && (cur_beval->showState != ShS_PENDING
8326 || abs(cur_beval->x - pt.x) > 3
8327 || abs(cur_beval->y - pt.y) > 3))
8328 {
8329 /* Pointer resting in one place long enough, it's time to show
8330 * the tooltip. */
8331 cur_beval->showState = ShS_PENDING;
8332 cur_beval->x = pt.x;
8333 cur_beval->y = pt.y;
8334
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008335 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
8337 if (cur_beval->msgCB != NULL)
8338 (*cur_beval->msgCB)(cur_beval, 0);
8339 }
8340}
8341
8342 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008343gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008345 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008347 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
8350 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008351gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008353 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 if (beval == NULL)
8355 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008356 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008357 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008358 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359}
8360
8361 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008362gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363{
8364 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008365
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008366 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 if (beval->showState == ShS_SHOWING)
8368 return;
8369 GetCursorPos(&pt);
8370 ScreenToClient(s_textArea, &pt);
8371
8372 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008374 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 gui_mch_disable_beval_area(cur_beval);
8376 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008377 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008379 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
8381
8382 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008383gui_mch_create_beval_area(
8384 void *target, /* ignored, always use s_textArea */
8385 char_u *mesg,
8386 void (*mesgCB)(BalloonEval *, int),
8387 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388{
8389 /* partially stolen from gui_beval.c */
8390 BalloonEval *beval;
8391
8392 if (mesg != NULL && mesgCB != NULL)
8393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008394 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 return NULL;
8396 }
8397
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008398 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 if (beval != NULL)
8400 {
8401 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 beval->msg = mesg;
8405 beval->msgCB = mesgCB;
8406 beval->clientData = clientData;
8407
8408 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 cur_beval = beval;
8410
8411 if (p_beval)
8412 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414 return beval;
8415}
8416
8417 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008418Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8421 return;
8422
8423 if (cur_beval != NULL)
8424 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008425 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008427 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008428 // TRACE0("TTN_SHOW {{{");
8429 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008430 break;
8431 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008432 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 delete_tooltip(cur_beval);
8434 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008435 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436
8437 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008438 break;
8439 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008440 {
8441 /* if you get there then we have new common controls */
8442 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8443 info->lpszText = (LPSTR)info->lParam;
8444 info->uFlags |= TTF_DI_SETITEM;
8445 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008446 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008447 case TTN_GETDISPINFOW:
8448 {
8449 // if we get here then we have new common controls
8450 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8451 info->lpszText = (LPWSTR)info->lParam;
8452 info->uFlags |= TTF_DI_SETITEM;
8453 }
8454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 }
8456 }
8457}
8458
8459 static void
8460TrackUserActivity(UINT uMsg)
8461{
8462 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8463 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8464 LastActivity = GetTickCount();
8465}
8466
8467 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008468gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008470#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008471 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008472#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008473 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 vim_free(beval);
8475}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008476#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477
8478#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8479/*
8480 * We have multiple signs to draw at the same location. Draw the
8481 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8482 */
8483 void
8484netbeans_draw_multisign_indicator(int row)
8485{
8486 int i;
8487 int y;
8488 int x;
8489
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008490 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008491 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008492
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 x = 0;
8494 y = TEXT_Y(row);
8495
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008496#if defined(FEAT_DIRECTX)
8497 if (IS_ENABLE_DIRECTX())
8498 DWriteContext_Flush(s_dwc);
8499#endif
8500
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 for (i = 0; i < gui.char_height - 3; i++)
8502 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8503
8504 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8505 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8506 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8507 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8508 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8509 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8510 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8511}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008512#endif