blob: 1c17b110ea038f499e6bd189c053c656be9a3ba6 [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
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
202#endif /* PROTO */
203
204#ifdef FEAT_MENU
205# define MENUHINTS /* show menu hints in command line */
206#endif
207
208/* Some parameters for dialog boxes. All in pixels. */
209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
213#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
223#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
224
225#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200243# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define _cdecl
245typedef int BOOL;
246typedef int BYTE;
247typedef int DWORD;
248typedef int WCHAR;
249typedef int ENUMLOGFONT;
250typedef int FINDREPLACE;
251typedef int HANDLE;
252typedef int HBITMAP;
253typedef int HBRUSH;
254typedef int HDROP;
255typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100256typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257typedef int LPARAM;
258typedef int LPCREATESTRUCT;
259typedef int LPCSTR;
260typedef int LPCTSTR;
261typedef int LPRECT;
262typedef int LPSTR;
263typedef int LPWINDOWPOS;
264typedef int LPWORD;
265typedef int LRESULT;
266typedef int HRESULT;
267# undef MSG
268typedef int MSG;
269typedef int NEWTEXTMETRIC;
270typedef int OSVERSIONINFO;
271typedef int PWORD;
272typedef int RECT;
273typedef int UINT;
274typedef int WORD;
275typedef int WPARAM;
276typedef int POINT;
277typedef void *HINSTANCE;
278typedef void *HMENU;
279typedef void *HWND;
280typedef void *HDC;
281typedef void VOID;
282typedef int LPNMHDR;
283typedef int LONG;
284typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200285typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200286typedef int COLORREF;
287typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288#endif
289
290#ifndef GET_X_LPARAM
291# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
292#endif
293
294static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100295static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296static void clear_rect(RECT *rcp);
297
298static WORD s_dlgfntheight; /* height of the dialog font */
299static WORD s_dlgfntwidth; /* width of the dialog font */
300
301#ifdef FEAT_MENU
302static HMENU s_menuBar = NULL;
303#endif
304#ifdef FEAT_TEAROFF
305static void rebuild_tearoff(vimmenu_T *menu);
306static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
307#endif
308
309/* Flag that is set while processing a message that must not be interrupted by
310 * processing another message. */
311static int s_busy_processing = FALSE;
312
313static int destroying = FALSE; /* call DestroyWindow() ourselves */
314
315#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200316static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
317static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200319static int s_findrep_is_find; // TRUE for find dialog, FALSE
320 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321#endif
322
Bram Moolenaar85b11762016-02-27 18:13:23 +0100323#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324static
325#endif
326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
347static char_u *s_textfield; /* Used by dialogs to pass back strings */
348
349static int s_need_activate = FALSE;
350
351/* This variable is set when waiting for an event, which is the only moment
352 * scrollbar dragging can be done directly. It's not allowed while commands
353 * are executed, because it may move the cursor and that may cause unexpected
354 * problems (e.g., while ":s" is working).
355 */
356static int allow_scrollbar = FALSE;
357
358#ifdef GLOBAL_IME
359# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
360#else
361# define MyTranslateMessage(x) TranslateMessage(x)
362#endif
363
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100364#if defined(FEAT_DIRECTX)
365 static int
366directx_enabled(void)
367{
368 if (s_dwc != NULL)
369 return 1;
370 else if (s_directx_load_attempted)
371 return 0;
372 /* load DirectX */
373 DWrite_Init();
374 s_directx_load_attempted = 1;
375 s_dwc = DWriteContext_Open();
376 directx_binddc();
377 return s_dwc != NULL ? 1 : 0;
378}
379
380 static void
381directx_binddc(void)
382{
383 if (s_textArea != NULL)
384 {
385 RECT rect;
386 GetClientRect(s_textArea, &rect);
387 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
388 }
389}
390#endif
391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200392/* use of WindowProc depends on Global IME */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100393#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100394
395extern int current_font_height; /* this is in os_mswin.c */
396
397static struct
398{
399 UINT key_sym;
400 char_u vim_code0;
401 char_u vim_code1;
402} special_keys[] =
403{
404 {VK_UP, 'k', 'u'},
405 {VK_DOWN, 'k', 'd'},
406 {VK_LEFT, 'k', 'l'},
407 {VK_RIGHT, 'k', 'r'},
408
409 {VK_F1, 'k', '1'},
410 {VK_F2, 'k', '2'},
411 {VK_F3, 'k', '3'},
412 {VK_F4, 'k', '4'},
413 {VK_F5, 'k', '5'},
414 {VK_F6, 'k', '6'},
415 {VK_F7, 'k', '7'},
416 {VK_F8, 'k', '8'},
417 {VK_F9, 'k', '9'},
418 {VK_F10, 'k', ';'},
419
420 {VK_F11, 'F', '1'},
421 {VK_F12, 'F', '2'},
422 {VK_F13, 'F', '3'},
423 {VK_F14, 'F', '4'},
424 {VK_F15, 'F', '5'},
425 {VK_F16, 'F', '6'},
426 {VK_F17, 'F', '7'},
427 {VK_F18, 'F', '8'},
428 {VK_F19, 'F', '9'},
429 {VK_F20, 'F', 'A'},
430
431 {VK_F21, 'F', 'B'},
432#ifdef FEAT_NETBEANS_INTG
433 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
434#endif
435 {VK_F22, 'F', 'C'},
436 {VK_F23, 'F', 'D'},
437 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
438
439 {VK_HELP, '%', '1'},
440 {VK_BACK, 'k', 'b'},
441 {VK_INSERT, 'k', 'I'},
442 {VK_DELETE, 'k', 'D'},
443 {VK_HOME, 'k', 'h'},
444 {VK_END, '@', '7'},
445 {VK_PRIOR, 'k', 'P'},
446 {VK_NEXT, 'k', 'N'},
447 {VK_PRINT, '%', '9'},
448 {VK_ADD, 'K', '6'},
449 {VK_SUBTRACT, 'K', '7'},
450 {VK_DIVIDE, 'K', '8'},
451 {VK_MULTIPLY, 'K', '9'},
452 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
453 {VK_DECIMAL, 'K', 'B'},
454
455 {VK_NUMPAD0, 'K', 'C'},
456 {VK_NUMPAD1, 'K', 'D'},
457 {VK_NUMPAD2, 'K', 'E'},
458 {VK_NUMPAD3, 'K', 'F'},
459 {VK_NUMPAD4, 'K', 'G'},
460 {VK_NUMPAD5, 'K', 'H'},
461 {VK_NUMPAD6, 'K', 'I'},
462 {VK_NUMPAD7, 'K', 'J'},
463 {VK_NUMPAD8, 'K', 'K'},
464 {VK_NUMPAD9, 'K', 'L'},
465
466 /* Keys that we want to be able to use any modifier with: */
467 {VK_SPACE, ' ', NUL},
468 {VK_TAB, TAB, NUL},
469 {VK_ESCAPE, ESC, NUL},
470 {NL, NL, NUL},
471 {CAR, CAR, NUL},
472
473 /* End of list marker: */
474 {0, 0, 0}
475};
476
477/* Local variables */
478static int s_button_pending = -1;
479
480/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
481 * so don't reset s_button_pending. */
482static int s_getting_focus = FALSE;
483
484static int s_x_pending;
485static int s_y_pending;
486static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200487static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100488static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200489static int dead_key = 0; // 0: no dead key, 1: dead key pressed
490static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
491 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100493#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494/* balloon-eval WM_NOTIFY_HANDLER */
495static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
496static void TrackUserActivity(UINT uMsg);
497#endif
498
499/*
500 * For control IME.
501 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100502 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100503 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100505/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
506static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100507#endif
508#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100509/* holds LOGFONTW for 'guifont' always. */
510static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511#endif
512
513#ifdef FEAT_MBYTE_IME
514static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
515#endif
516
517#if defined(FEAT_BROWSE)
518static char_u *convert_filter(char_u *s);
519#endif
520
521#ifdef DEBUG_PRINT_ERROR
522/*
523 * Print out the last Windows error message
524 */
525 static void
526print_windows_error(void)
527{
528 LPVOID lpMsgBuf;
529
530 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
531 NULL, GetLastError(),
532 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
533 (LPTSTR) &lpMsgBuf, 0, NULL);
534 TRACE1("Error: %s\n", lpMsgBuf);
535 LocalFree(lpMsgBuf);
536}
537#endif
538
539/*
540 * Cursor blink functions.
541 *
542 * This is a simple state machine:
543 * BLINK_NONE not blinking at all
544 * BLINK_OFF blinking, cursor is not shown
545 * BLINK_ON blinking, cursor is shown
546 */
547
548#define BLINK_NONE 0
549#define BLINK_OFF 1
550#define BLINK_ON 2
551
552static int blink_state = BLINK_NONE;
553static long_u blink_waittime = 700;
554static long_u blink_ontime = 400;
555static long_u blink_offtime = 250;
556static UINT blink_timer = 0;
557
Bram Moolenaar703a8042016-06-04 16:24:32 +0200558 int
559gui_mch_is_blinking(void)
560{
561 return blink_state != BLINK_NONE;
562}
563
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200564 int
565gui_mch_is_blink_off(void)
566{
567 return blink_state == BLINK_OFF;
568}
569
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100570 void
571gui_mch_set_blinking(long wait, long on, long off)
572{
573 blink_waittime = wait;
574 blink_ontime = on;
575 blink_offtime = off;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 static VOID CALLBACK
579_OnBlinkTimer(
580 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100581 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100583 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100584{
585 MSG msg;
586
587 /*
588 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
589 */
590
591 KillTimer(NULL, idEvent);
592
593 /* Eat spurious WM_TIMER messages */
594 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
595 ;
596
597 if (blink_state == BLINK_ON)
598 {
599 gui_undraw_cursor();
600 blink_state = BLINK_OFF;
601 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
602 (TIMERPROC)_OnBlinkTimer);
603 }
604 else
605 {
606 gui_update_cursor(TRUE, FALSE);
607 blink_state = BLINK_ON;
608 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100609 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100610 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100611 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612}
613
614 static void
615gui_mswin_rm_blink_timer(void)
616{
617 MSG msg;
618
619 if (blink_timer != 0)
620 {
621 KillTimer(NULL, blink_timer);
622 /* Eat spurious WM_TIMER messages */
623 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
624 ;
625 blink_timer = 0;
626 }
627}
628
629/*
630 * Stop the cursor blinking. Show the cursor if it wasn't shown.
631 */
632 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100634{
635 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100636 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100637 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100639 gui_mch_flush();
640 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 blink_state = BLINK_NONE;
642}
643
644/*
645 * Start the cursor blinking. If it was already blinking, this restarts the
646 * waiting time and shows the cursor.
647 */
648 void
649gui_mch_start_blink(void)
650{
651 gui_mswin_rm_blink_timer();
652
653 /* Only switch blinking on if none of the times is zero */
654 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
655 {
656 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
657 (TIMERPROC)_OnBlinkTimer);
658 blink_state = BLINK_ON;
659 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100660 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661 }
662}
663
664/*
665 * Call-back routines.
666 */
667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 static VOID CALLBACK
669_OnTimer(
670 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100671 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100673 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674{
675 MSG msg;
676
677 /*
678 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
679 */
680 KillTimer(NULL, idEvent);
681 s_timed_out = TRUE;
682
683 /* Eat spurious WM_TIMER messages */
684 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
685 ;
686 if (idEvent == s_wait_timer)
687 s_wait_timer = 0;
688}
689
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 static void
691_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100692 HWND hwnd UNUSED,
693 UINT ch UNUSED,
694 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695{
696 dead_key = 1;
697}
698
699/*
700 * Convert Unicode character "ch" to bytes in "string[slen]".
701 * When "had_alt" is TRUE the ALT key was included in "ch".
702 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200703 * Because the Windows API uses UTF-16, we have to deal with surrogate
704 * pairs; this is where we choose to deal with them: if "ch" is a high
705 * surrogate, it will be stored, and the length returned will be zero; the next
706 * char_to_string call will then include the high surrogate, decoding the pair
707 * of UTF-16 code units to a single Unicode code point, presuming it is the
708 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 */
710 static int
711char_to_string(int ch, char_u *string, int slen, int had_alt)
712{
713 int len;
714 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200716 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200718 if (surrogate_pending_ch != 0)
719 {
720 /* We don't guarantee ch is a low surrogate to match the high surrogate
721 * we already have; it should be, but if it isn't, tough luck. */
722 wstring[0] = surrogate_pending_ch;
723 wstring[1] = ch;
724 surrogate_pending_ch = 0;
725 len = 2;
726 }
727 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
728 {
729 /* We don't have the entire code point yet, only the first UTF-16 code
730 * unit; so just remember it and use it in the next call. */
731 surrogate_pending_ch = ch;
732 return 0;
733 }
734 else
735 {
736 wstring[0] = ch;
737 len = 1;
738 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200739
740 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
741 * "enc_codepage" is non-zero use the standard Win32 function,
742 * otherwise use our own conversion function (e.g., for UTF-8). */
743 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200745 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
746 (LPSTR)string, slen, 0, NULL);
747 /* If we had included the ALT key into the character but now the
748 * upper bit is no longer set, that probably means the conversion
749 * failed. Convert the original character and set the upper bit
750 * afterwards. */
751 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200753 wstring[0] = ch & 0x7f;
754 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
755 (LPSTR)string, slen, 0, NULL);
756 if (len == 1) /* safety check */
757 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758 }
759 }
760 else
761 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200762 ws = utf16_to_enc(wstring, &len);
763 if (ws == NULL)
764 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 else
766 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767 if (len > slen) /* just in case */
768 len = slen;
769 mch_memmove(string, ws, len);
770 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773
774 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 {
776 string[0] = ch;
777 len = 1;
778 }
779
780 for (i = 0; i < len; ++i)
781 if (string[i] == CSI && len <= slen - 2)
782 {
783 /* Insert CSI as K_CSI. */
784 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
785 string[++i] = KS_EXTRA;
786 string[++i] = (int)KE_CSI;
787 len += 2;
788 }
789
790 return len;
791}
792
793/*
794 * Key hit, add it to the input buffer.
795 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 static void
797_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100798 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801{
802 char_u string[40];
803 int len = 0;
804
805 dead_key = 0;
806
807 len = char_to_string(ch, string, 40, FALSE);
808 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
809 {
810 trash_input_buf();
811 got_int = TRUE;
812 }
813
814 add_to_input_buf(string, len);
815}
816
817/*
818 * Alt-Key hit, add it to the input buffer.
819 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 static void
821_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100822 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100823 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825{
826 char_u string[40]; /* Enough for multibyte character */
827 int len;
828 int modifiers;
829 int ch = cch; /* special keys are negative */
830
831 dead_key = 0;
832
833 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
834
835 /* OK, we have a character key (given by ch) which was entered with the
836 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
837 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
838 * CAPSLOCK is pressed) at this point.
839 */
840 modifiers = MOD_MASK_ALT;
841 if (GetKeyState(VK_SHIFT) & 0x8000)
842 modifiers |= MOD_MASK_SHIFT;
843 if (GetKeyState(VK_CONTROL) & 0x8000)
844 modifiers |= MOD_MASK_CTRL;
845
846 ch = simplify_key(ch, &modifiers);
847 /* remove the SHIFT modifier for keys where it's already included, e.g.,
848 * '(' and '*' */
849 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
850 modifiers &= ~MOD_MASK_SHIFT;
851
852 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
Bram Moolenaar459fd782019-10-13 16:43:39 +0200853 ch = extract_modifiers(ch, &modifiers, TRUE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100854 if (ch == CSI)
855 ch = K_CSI;
856
857 len = 0;
858 if (modifiers)
859 {
860 string[len++] = CSI;
861 string[len++] = KS_MODIFIER;
862 string[len++] = modifiers;
863 }
864
865 if (IS_SPECIAL((int)ch))
866 {
867 string[len++] = CSI;
868 string[len++] = K_SECOND((int)ch);
869 string[len++] = K_THIRD((int)ch);
870 }
871 else
872 {
873 /* Although the documentation isn't clear about it, we assume "ch" is
874 * a Unicode character. */
875 len += char_to_string(ch, string + len, 40 - len, TRUE);
876 }
877
878 add_to_input_buf(string, len);
879}
880
881 static void
882_OnMouseEvent(
883 int button,
884 int x,
885 int y,
886 int repeated_click,
887 UINT keyFlags)
888{
889 int vim_modifiers = 0x0;
890
891 s_getting_focus = FALSE;
892
893 if (keyFlags & MK_SHIFT)
894 vim_modifiers |= MOUSE_SHIFT;
895 if (keyFlags & MK_CONTROL)
896 vim_modifiers |= MOUSE_CTRL;
897 if (GetKeyState(VK_MENU) & 0x8000)
898 vim_modifiers |= MOUSE_ALT;
899
900 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
901}
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 static void
904_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905 HWND hwnd UNUSED,
906 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 int x,
908 int y,
909 UINT keyFlags)
910{
911 static LONG s_prevTime = 0;
912
913 LONG currentTime = GetMessageTime();
914 int button = -1;
915 int repeated_click;
916
917 /* Give main window the focus: this is so the cursor isn't hollow. */
918 (void)SetFocus(s_hwnd);
919
920 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
921 button = MOUSE_LEFT;
922 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
923 button = MOUSE_MIDDLE;
924 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
925 button = MOUSE_RIGHT;
926 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
927 {
928#ifndef GET_XBUTTON_WPARAM
929# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
930#endif
931 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
932 }
933 else if (s_uMsg == WM_CAPTURECHANGED)
934 {
935 /* on W95/NT4, somehow you get in here with an odd Msg
936 * if you press one button while holding down the other..*/
937 if (s_button_pending == MOUSE_LEFT)
938 button = MOUSE_RIGHT;
939 else
940 button = MOUSE_LEFT;
941 }
942 if (button >= 0)
943 {
944 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
945
946 /*
947 * Holding down the left and right buttons simulates pushing the middle
948 * button.
949 */
950 if (repeated_click
951 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
952 || (button == MOUSE_RIGHT
953 && s_button_pending == MOUSE_LEFT)))
954 {
955 /*
956 * Hmm, gui.c will ignore more than one button down at a time, so
957 * pretend we let go of it first.
958 */
959 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
960 button = MOUSE_MIDDLE;
961 repeated_click = FALSE;
962 s_button_pending = -1;
963 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
964 }
965 else if ((repeated_click)
966 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
967 {
968 if (s_button_pending > -1)
969 {
970 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
971 s_button_pending = -1;
972 }
973 /* TRACE("Button down at x %d, y %d\n", x, y); */
974 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
975 }
976 else
977 {
978 /*
979 * If this is the first press (i.e. not a multiple click) don't
980 * action immediately, but store and wait for:
981 * i) button-up
982 * ii) mouse move
983 * iii) another button press
984 * before using it.
985 * This enables us to make left+right simulate middle button,
986 * without left or right being actioned first. The side-effect is
987 * that if you click and hold the mouse without dragging, the
988 * cursor doesn't move until you release the button. In practice
989 * this is hardly a problem.
990 */
991 s_button_pending = button;
992 s_x_pending = x;
993 s_y_pending = y;
994 s_kFlags_pending = keyFlags;
995 }
996
997 s_prevTime = currentTime;
998 }
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001004 int x,
1005 int y,
1006 UINT keyFlags)
1007{
1008 int button;
1009
1010 s_getting_focus = FALSE;
1011 if (s_button_pending > -1)
1012 {
1013 /* Delayed action for mouse down event */
1014 _OnMouseEvent(s_button_pending, s_x_pending,
1015 s_y_pending, FALSE, s_kFlags_pending);
1016 s_button_pending = -1;
1017 }
1018 if (s_uMsg == WM_MOUSEMOVE)
1019 {
1020 /*
1021 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1022 * down.
1023 */
1024 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1025 | MK_XBUTTON1 | MK_XBUTTON2)))
1026 {
1027 gui_mouse_moved(x, y);
1028 return;
1029 }
1030
1031 /*
1032 * While button is down, keep grabbing mouse move events when
1033 * the mouse goes outside the window
1034 */
1035 SetCapture(s_textArea);
1036 button = MOUSE_DRAG;
1037 /* TRACE(" move at x %d, y %d\n", x, y); */
1038 }
1039 else
1040 {
1041 ReleaseCapture();
1042 button = MOUSE_RELEASE;
1043 /* TRACE(" up at x %d, y %d\n", x, y); */
1044 }
1045
1046 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1047}
1048
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001049 static void
1050_OnSizeTextArea(
1051 HWND hwnd UNUSED,
1052 UINT state UNUSED,
1053 int cx UNUSED,
1054 int cy UNUSED)
1055{
1056#if defined(FEAT_DIRECTX)
1057 if (IS_ENABLE_DIRECTX())
1058 directx_binddc();
1059#endif
1060}
1061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062#ifdef FEAT_MENU
1063/*
1064 * Find the vimmenu_T with the given id
1065 */
1066 static vimmenu_T *
1067gui_mswin_find_menu(
1068 vimmenu_T *pMenu,
1069 int id)
1070{
1071 vimmenu_T *pChildMenu;
1072
1073 while (pMenu)
1074 {
1075 if (pMenu->id == (UINT)id)
1076 break;
1077 if (pMenu->children != NULL)
1078 {
1079 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1080 if (pChildMenu)
1081 {
1082 pMenu = pChildMenu;
1083 break;
1084 }
1085 }
1086 pMenu = pMenu->next;
1087 }
1088 return pMenu;
1089}
1090
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091 static void
1092_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwndCtl UNUSED,
1096 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097{
1098 vimmenu_T *pMenu;
1099
1100 pMenu = gui_mswin_find_menu(root_menu, id);
1101 if (pMenu)
1102 gui_menu_cb(pMenu);
1103}
1104#endif
1105
1106#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1117 /* Give main window the focus back. */
1118 (void)SetFocus(s_hwnd);
1119
1120 if (s_findrep_struct.Flags & FR_FINDNEXT)
1121 {
1122 flags = FRD_FINDNEXT;
1123
1124 /* Give main window the focus back: this is so the cursor isn't
1125 * hollow. */
1126 (void)SetFocus(s_hwnd);
1127 }
1128 else if (s_findrep_struct.Flags & FR_REPLACE)
1129 {
1130 flags = FRD_REPLACE;
1131
1132 /* Give main window the focus back: this is so the cursor isn't
1133 * hollow. */
1134 (void)SetFocus(s_hwnd);
1135 }
1136 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1137 {
1138 flags = FRD_REPLACEALL;
1139 }
1140
1141 if (flags != 0)
1142 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001143 char_u *p, *q;
1144
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 /* Call the generic GUI function to do the actual work. */
1146 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1147 flags |= FRD_WHOLE_WORD;
1148 if (s_findrep_struct.Flags & FR_MATCHCASE)
1149 flags |= FRD_MATCH_CASE;
1150 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001151 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1152 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1153 if (p != NULL && q != NULL)
1154 gui_do_findrepl(flags, p, q, down);
1155 vim_free(p);
1156 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 }
1158}
1159#endif
1160
1161 static void
1162HandleMouseHide(UINT uMsg, LPARAM lParam)
1163{
1164 static LPARAM last_lParam = 0L;
1165
1166 /* We sometimes get a mousemove when the mouse didn't move... */
1167 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1168 {
1169 if (lParam == last_lParam)
1170 return;
1171 last_lParam = lParam;
1172 }
1173
1174 /* Handle specially, to centralise coding. We need to be sure we catch all
1175 * possible events which should cause us to restore the cursor (as it is a
1176 * shared resource, we take full responsibility for it).
1177 */
1178 switch (uMsg)
1179 {
1180 case WM_KEYUP:
1181 case WM_CHAR:
1182 /*
1183 * blank out the pointer if necessary
1184 */
1185 if (p_mh)
1186 gui_mch_mousehide(TRUE);
1187 break;
1188
1189 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1190 case WM_SYSCHAR:
1191 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1192 case WM_LBUTTONDOWN:
1193 case WM_LBUTTONUP:
1194 case WM_MBUTTONDOWN:
1195 case WM_MBUTTONUP:
1196 case WM_RBUTTONDOWN:
1197 case WM_RBUTTONUP:
1198 case WM_XBUTTONDOWN:
1199 case WM_XBUTTONUP:
1200 case WM_NCMOUSEMOVE:
1201 case WM_NCLBUTTONDOWN:
1202 case WM_NCLBUTTONUP:
1203 case WM_NCMBUTTONDOWN:
1204 case WM_NCMBUTTONUP:
1205 case WM_NCRBUTTONDOWN:
1206 case WM_NCRBUTTONUP:
1207 case WM_KILLFOCUS:
1208 /*
1209 * if the pointer is currently hidden, then we should show it.
1210 */
1211 gui_mch_mousehide(FALSE);
1212 break;
1213 }
1214}
1215
1216 static LRESULT CALLBACK
1217_TextAreaWndProc(
1218 HWND hwnd,
1219 UINT uMsg,
1220 WPARAM wParam,
1221 LPARAM lParam)
1222{
1223 /*
1224 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1225 hwnd, uMsg, wParam, lParam);
1226 */
1227
1228 HandleMouseHide(uMsg, lParam);
1229
1230 s_uMsg = uMsg;
1231 s_wParam = wParam;
1232 s_lParam = lParam;
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 TrackUserActivity(uMsg);
1236#endif
1237
1238 switch (uMsg)
1239 {
1240 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1248 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001254 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1258 return TRUE;
1259#endif
1260 default:
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263}
1264
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001267#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269 LRESULT WINAPI
1270vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1271{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001272#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001274#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001275 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001276#endif
1277}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278
1279/*
1280 * Called when the foreground or background color has been changed.
1281 */
1282 void
1283gui_mch_new_colors(void)
1284{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001285 HBRUSH prevBrush;
1286
1287 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001288 prevBrush = (HBRUSH)SetClassLongPtr(
1289 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001290 InvalidateRect(s_hwnd, NULL, TRUE);
1291 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001292}
1293
1294/*
1295 * Set the colors to their default values.
1296 */
1297 void
1298gui_mch_def_colors(void)
1299{
1300 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1301 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1302 gui.def_norm_pixel = gui.norm_pixel;
1303 gui.def_back_pixel = gui.back_pixel;
1304}
1305
1306/*
1307 * Open the GUI window which was created by a call to gui_mch_init().
1308 */
1309 int
1310gui_mch_open(void)
1311{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001312 /* Actually open the window, if not already visible
1313 * (may be done already in gui_mch_set_shellsize) */
1314 if (!IsWindowVisible(s_hwnd))
1315 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1316
1317#ifdef MSWIN_FIND_REPLACE
1318 /* Init replace string here, so that we keep it when re-opening the
1319 * dialog. */
1320 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1321#endif
1322
1323 return OK;
1324}
1325
1326/*
1327 * Get the position of the top left corner of the window.
1328 */
1329 int
1330gui_mch_get_winpos(int *x, int *y)
1331{
1332 RECT rect;
1333
1334 GetWindowRect(s_hwnd, &rect);
1335 *x = rect.left;
1336 *y = rect.top;
1337 return OK;
1338}
1339
1340/*
1341 * Set the position of the top left corner of the window to the given
1342 * coordinates.
1343 */
1344 void
1345gui_mch_set_winpos(int x, int y)
1346{
1347 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1348 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1349}
1350 void
1351gui_mch_set_text_area_pos(int x, int y, int w, int h)
1352{
1353 static int oldx = 0;
1354 static int oldy = 0;
1355
1356 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1357
1358#ifdef FEAT_TOOLBAR
1359 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1360 SendMessage(s_toolbarhwnd, WM_SIZE,
1361 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1362#endif
1363#if defined(FEAT_GUI_TABLINE)
1364 if (showing_tabline)
1365 {
1366 int top = 0;
1367 RECT rect;
1368
1369# ifdef FEAT_TOOLBAR
1370 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1371 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1372# endif
1373 GetClientRect(s_hwnd, &rect);
1374 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1375 }
1376#endif
1377
1378 /* When side scroll bar is unshown, the size of window will change.
1379 * then, the text area move left or right. thus client rect should be
1380 * forcedly redrawn. (Yasuhiro Matsumoto) */
1381 if (oldx != x || oldy != y)
1382 {
1383 InvalidateRect(s_hwnd, NULL, FALSE);
1384 oldx = x;
1385 oldy = y;
1386 }
1387}
1388
1389
1390/*
1391 * Scrollbar stuff:
1392 */
1393
1394 void
1395gui_mch_enable_scrollbar(
1396 scrollbar_T *sb,
1397 int flag)
1398{
1399 ShowScrollBar(sb->id, SB_CTL, flag);
1400
1401 /* TODO: When the window is maximized, the size of the window stays the
1402 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1403 * NT 4.0 it's not... */
1404}
1405
1406 void
1407gui_mch_set_scrollbar_pos(
1408 scrollbar_T *sb,
1409 int x,
1410 int y,
1411 int w,
1412 int h)
1413{
1414 SetWindowPos(sb->id, NULL, x, y, w, h,
1415 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1416}
1417
1418 void
1419gui_mch_create_scrollbar(
1420 scrollbar_T *sb,
1421 int orient) /* SBAR_VERT or SBAR_HORIZ */
1422{
1423 sb->id = CreateWindow(
1424 "SCROLLBAR", "Scrollbar",
1425 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1426 10, /* Any value will do for now */
1427 10, /* Any value will do for now */
1428 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001429 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001430}
1431
1432/*
1433 * Find the scrollbar with the given hwnd.
1434 */
1435 static scrollbar_T *
1436gui_mswin_find_scrollbar(HWND hwnd)
1437{
1438 win_T *wp;
1439
1440 if (gui.bottom_sbar.id == hwnd)
1441 return &gui.bottom_sbar;
1442 FOR_ALL_WINDOWS(wp)
1443 {
1444 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1445 return &wp->w_scrollbars[SBAR_LEFT];
1446 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1447 return &wp->w_scrollbars[SBAR_RIGHT];
1448 }
1449 return NULL;
1450}
1451
1452/*
1453 * Get the character size of a font.
1454 */
1455 static void
1456GetFontSize(GuiFont font)
1457{
1458 HWND hwnd = GetDesktopWindow();
1459 HDC hdc = GetWindowDC(hwnd);
1460 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001461 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001462 TEXTMETRIC tm;
1463
1464 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001465 // GetTextMetrics() may not return the right value in tmAveCharWidth
1466 // for some fonts. Do our own average computation.
1467 GetTextExtentPoint(hdc,
1468 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1469 52, &size);
1470 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001471
1472 gui.char_height = tm.tmHeight + p_linespace;
1473
1474 SelectFont(hdc, hfntOld);
1475
1476 ReleaseDC(hwnd, hdc);
1477}
1478
1479/*
1480 * Adjust gui.char_height (after 'linespace' was changed).
1481 */
1482 int
1483gui_mch_adjust_charheight(void)
1484{
1485 GetFontSize(gui.norm_font);
1486 return OK;
1487}
1488
1489 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001490get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001491{
1492 HFONT font = NULL;
1493
1494 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001495 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001496
1497 if (font == NULL)
1498 return NOFONT;
1499
1500 return (GuiFont)font;
1501}
1502
1503 static int
1504pixels_to_points(int pixels, int vertical)
1505{
1506 int points;
1507 HWND hwnd;
1508 HDC hdc;
1509
1510 hwnd = GetDesktopWindow();
1511 hdc = GetWindowDC(hwnd);
1512
1513 points = MulDiv(pixels, 72,
1514 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1515
1516 ReleaseDC(hwnd, hdc);
1517
1518 return points;
1519}
1520
1521 GuiFont
1522gui_mch_get_font(
1523 char_u *name,
1524 int giveErrorIfMissing)
1525{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001526 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001527 GuiFont font = NOFONT;
1528
1529 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1530 font = get_font_handle(&lf);
1531 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001532 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001533 return font;
1534}
1535
1536#if defined(FEAT_EVAL) || defined(PROTO)
1537/*
1538 * Return the name of font "font" in allocated memory.
1539 * Don't know how to get the actual name, thus use the provided name.
1540 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001541 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001542gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001543{
1544 if (name == NULL)
1545 return NULL;
1546 return vim_strsave(name);
1547}
1548#endif
1549
1550 void
1551gui_mch_free_font(GuiFont font)
1552{
1553 if (font)
1554 DeleteObject((HFONT)font);
1555}
1556
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001557/*
1558 * Return the Pixel value (color) for the given color name.
1559 * Return INVALCOLOR for error.
1560 */
1561 guicolor_T
1562gui_mch_get_color(char_u *name)
1563{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001564 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001565
1566 typedef struct SysColorTable
1567 {
1568 char *name;
1569 int color;
1570 } SysColorTable;
1571
1572 static SysColorTable sys_table[] =
1573 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001574 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1575 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001576#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001577 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1578#endif
1579 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1580 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1581 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1582 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1583 {"SYS_DESKTOP", COLOR_DESKTOP},
1584 {"SYS_INFOBK", COLOR_INFOBK},
1585 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1586 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001587 {"SYS_BTNFACE", COLOR_BTNFACE},
1588 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1589 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1590 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1591 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1592 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1593 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1594 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1595 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1596 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1597 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1598 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1599 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1600 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1601 {"SYS_MENU", COLOR_MENU},
1602 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1603 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1604 {"SYS_WINDOW", COLOR_WINDOW},
1605 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1606 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1607 };
1608
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001609 /*
1610 * Try to look up a system colour.
1611 */
1612 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1613 if (STRICMP(name, sys_table[i].name) == 0)
1614 return GetSysColor(sys_table[i].color);
1615
Bram Moolenaarab302212016-04-26 20:59:29 +02001616 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001617}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001618
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001619 guicolor_T
1620gui_mch_get_rgb_color(int r, int g, int b)
1621{
1622 return gui_get_rgb_color_cmn(r, g, b);
1623}
1624
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001625/*
1626 * Return OK if the key with the termcap name "name" is supported.
1627 */
1628 int
1629gui_mch_haskey(char_u *name)
1630{
1631 int i;
1632
1633 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1634 if (name[0] == special_keys[i].vim_code0 &&
1635 name[1] == special_keys[i].vim_code1)
1636 return OK;
1637 return FAIL;
1638}
1639
1640 void
1641gui_mch_beep(void)
1642{
1643 MessageBeep(MB_OK);
1644}
1645/*
1646 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1647 */
1648 void
1649gui_mch_invert_rectangle(
1650 int r,
1651 int c,
1652 int nr,
1653 int nc)
1654{
1655 RECT rc;
1656
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001657#if defined(FEAT_DIRECTX)
1658 if (IS_ENABLE_DIRECTX())
1659 DWriteContext_Flush(s_dwc);
1660#endif
1661
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001662 /*
1663 * Note: InvertRect() excludes right and bottom of rectangle.
1664 */
1665 rc.left = FILL_X(c);
1666 rc.top = FILL_Y(r);
1667 rc.right = rc.left + nc * gui.char_width;
1668 rc.bottom = rc.top + nr * gui.char_height;
1669 InvertRect(s_hdc, &rc);
1670}
1671
1672/*
1673 * Iconify the GUI window.
1674 */
1675 void
1676gui_mch_iconify(void)
1677{
1678 ShowWindow(s_hwnd, SW_MINIMIZE);
1679}
1680
1681/*
1682 * Draw a cursor without focus.
1683 */
1684 void
1685gui_mch_draw_hollow_cursor(guicolor_T color)
1686{
1687 HBRUSH hbr;
1688 RECT rc;
1689
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001690#if defined(FEAT_DIRECTX)
1691 if (IS_ENABLE_DIRECTX())
1692 DWriteContext_Flush(s_dwc);
1693#endif
1694
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001695 /*
1696 * Note: FrameRect() excludes right and bottom of rectangle.
1697 */
1698 rc.left = FILL_X(gui.col);
1699 rc.top = FILL_Y(gui.row);
1700 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001701 if (mb_lefthalve(gui.row, gui.col))
1702 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001703 rc.bottom = rc.top + gui.char_height;
1704 hbr = CreateSolidBrush(color);
1705 FrameRect(s_hdc, &rc, hbr);
1706 DeleteBrush(hbr);
1707}
1708/*
1709 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1710 * color "color".
1711 */
1712 void
1713gui_mch_draw_part_cursor(
1714 int w,
1715 int h,
1716 guicolor_T color)
1717{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001718 RECT rc;
1719
1720 /*
1721 * Note: FillRect() excludes right and bottom of rectangle.
1722 */
1723 rc.left =
1724#ifdef FEAT_RIGHTLEFT
1725 /* vertical line should be on the right of current point */
1726 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1727#endif
1728 FILL_X(gui.col);
1729 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1730 rc.right = rc.left + w;
1731 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001732
Bram Moolenaar92467d32017-12-05 13:22:16 +01001733 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001734}
1735
1736
1737/*
1738 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1739 * dead key's nominal character and re-post the original message.
1740 */
1741 static void
1742outputDeadKey_rePost(MSG originalMsg)
1743{
1744 static MSG deadCharExpel;
1745
1746 if (!dead_key)
1747 return;
1748
1749 dead_key = 0;
1750
1751 /* Make Windows generate the dead key's character */
1752 deadCharExpel.message = originalMsg.message;
1753 deadCharExpel.hwnd = originalMsg.hwnd;
1754 deadCharExpel.wParam = VK_SPACE;
1755
1756 MyTranslateMessage(&deadCharExpel);
1757
1758 /* re-generate the current character free of the dead char influence */
1759 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1760 originalMsg.lParam);
1761}
1762
1763
1764/*
1765 * Process a single Windows message.
1766 * If one is not available we hang until one is.
1767 */
1768 static void
1769process_message(void)
1770{
1771 MSG msg;
1772 UINT vk = 0; /* Virtual key */
1773 char_u string[40];
1774 int i;
1775 int modifiers = 0;
1776 int key;
1777#ifdef FEAT_MENU
1778 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1779#endif
1780
1781 pGetMessage(&msg, NULL, 0, 0);
1782
1783#ifdef FEAT_OLE
1784 /* Look after OLE Automation commands */
1785 if (msg.message == WM_OLE)
1786 {
1787 char_u *str = (char_u *)msg.lParam;
1788 if (str == NULL || *str == NUL)
1789 {
1790 /* Message can't be ours, forward it. Fixes problem with Ultramon
1791 * 3.0.4 */
1792 pDispatchMessage(&msg);
1793 }
1794 else
1795 {
1796 add_to_input_buf(str, (int)STRLEN(str));
1797 vim_free(str); /* was allocated in CVim::SendKeys() */
1798 }
1799 return;
1800 }
1801#endif
1802
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001803#ifdef MSWIN_FIND_REPLACE
1804 /* Don't process messages used by the dialog */
1805 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1806 {
1807 HandleMouseHide(msg.message, msg.lParam);
1808 return;
1809 }
1810#endif
1811
1812 /*
1813 * Check if it's a special key that we recognise. If not, call
1814 * TranslateMessage().
1815 */
1816 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1817 {
1818 vk = (int) msg.wParam;
1819
1820 /*
1821 * Handle dead keys in special conditions in other cases we let Windows
1822 * handle them and do not interfere.
1823 *
1824 * The dead_key flag must be reset on several occasions:
1825 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1826 * consumed at that point (This is when we let Windows combine the
1827 * dead character on its own)
1828 *
1829 * - Before doing something special such as regenerating keypresses to
1830 * expel the dead character as this could trigger an infinite loop if
1831 * for some reason MyTranslateMessage() do not trigger a call
1832 * immediately to _OnChar() (or _OnSysChar()).
1833 */
1834 if (dead_key)
1835 {
1836 /*
1837 * If a dead key was pressed and the user presses VK_SPACE,
1838 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1839 * with the dead char now, so do nothing special and let Windows
1840 * handle it.
1841 *
1842 * Note that VK_SPACE combines with the dead_key's character and
1843 * only one WM_CHAR will be generated by TranslateMessage(), in
1844 * the two other cases two WM_CHAR will be generated: the dead
1845 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1846 * user expects.
1847 */
1848 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1849 {
1850 dead_key = 0;
1851 MyTranslateMessage(&msg);
1852 return;
1853 }
1854 /* In modes where we are not typing, dead keys should behave
1855 * normally */
1856 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1857 {
1858 outputDeadKey_rePost(msg);
1859 return;
1860 }
1861 }
1862
1863 /* Check for CTRL-BREAK */
1864 if (vk == VK_CANCEL)
1865 {
1866 trash_input_buf();
1867 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001868 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869 string[0] = Ctrl_C;
1870 add_to_input_buf(string, 1);
1871 }
1872
1873 for (i = 0; special_keys[i].key_sym != 0; i++)
1874 {
1875 /* ignore VK_SPACE when ALT key pressed: system menu */
1876 if (special_keys[i].key_sym == vk
1877 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1878 {
1879 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001880 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881 * is a key that would normally trigger the dead key nominal
1882 * character output (such as a NUMPAD printable character or
1883 * the TAB key, etc...).
1884 */
1885 if (dead_key && (special_keys[i].vim_code0 == 'K'
1886 || vk == VK_TAB || vk == CAR))
1887 {
1888 outputDeadKey_rePost(msg);
1889 return;
1890 }
1891
1892#ifdef FEAT_MENU
1893 /* Check for <F10>: Windows selects the menu. When <F10> is
1894 * mapped we want to use the mapping instead. */
1895 if (vk == VK_F10
1896 && gui.menu_is_active
1897 && check_map(k10, State, FALSE, TRUE, FALSE,
1898 NULL, NULL) == NULL)
1899 break;
1900#endif
1901 if (GetKeyState(VK_SHIFT) & 0x8000)
1902 modifiers |= MOD_MASK_SHIFT;
1903 /*
1904 * Don't use caps-lock as shift, because these are special keys
1905 * being considered here, and we only want letters to get
1906 * shifted -- webb
1907 */
1908 /*
1909 if (GetKeyState(VK_CAPITAL) & 0x0001)
1910 modifiers ^= MOD_MASK_SHIFT;
1911 */
1912 if (GetKeyState(VK_CONTROL) & 0x8000)
1913 modifiers |= MOD_MASK_CTRL;
1914 if (GetKeyState(VK_MENU) & 0x8000)
1915 modifiers |= MOD_MASK_ALT;
1916
1917 if (special_keys[i].vim_code1 == NUL)
1918 key = special_keys[i].vim_code0;
1919 else
1920 key = TO_SPECIAL(special_keys[i].vim_code0,
1921 special_keys[i].vim_code1);
1922 key = simplify_key(key, &modifiers);
1923 if (key == CSI)
1924 key = K_CSI;
1925
1926 if (modifiers)
1927 {
1928 string[0] = CSI;
1929 string[1] = KS_MODIFIER;
1930 string[2] = modifiers;
1931 add_to_input_buf(string, 3);
1932 }
1933
1934 if (IS_SPECIAL(key))
1935 {
1936 string[0] = CSI;
1937 string[1] = K_SECOND(key);
1938 string[2] = K_THIRD(key);
1939 add_to_input_buf(string, 3);
1940 }
1941 else
1942 {
1943 int len;
1944
1945 /* Handle "key" as a Unicode character. */
1946 len = char_to_string(key, string, 40, FALSE);
1947 add_to_input_buf(string, len);
1948 }
1949 break;
1950 }
1951 }
1952 if (special_keys[i].key_sym == 0)
1953 {
1954 /* Some keys need C-S- where they should only need C-.
1955 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1956 * system startup (Helmut Stiegler, 2003 Oct 3). */
1957 if (vk != 0xff
1958 && (GetKeyState(VK_CONTROL) & 0x8000)
1959 && !(GetKeyState(VK_SHIFT) & 0x8000)
1960 && !(GetKeyState(VK_MENU) & 0x8000))
1961 {
1962 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1963 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1964 {
1965 string[0] = Ctrl_HAT;
1966 add_to_input_buf(string, 1);
1967 }
1968 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1969 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1970 {
1971 string[0] = Ctrl__;
1972 add_to_input_buf(string, 1);
1973 }
1974 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1975 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1976 {
1977 string[0] = Ctrl_AT;
1978 add_to_input_buf(string, 1);
1979 }
1980 else
1981 MyTranslateMessage(&msg);
1982 }
1983 else
1984 MyTranslateMessage(&msg);
1985 }
1986 }
1987#ifdef FEAT_MBYTE_IME
1988 else if (msg.message == WM_IME_NOTIFY)
1989 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1990 else if (msg.message == WM_KEYUP && im_get_status())
1991 /* added for non-MS IME (Yasuhiro Matsumoto) */
1992 MyTranslateMessage(&msg);
1993#endif
1994#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1995/* GIME_TEST */
1996 else if (msg.message == WM_IME_STARTCOMPOSITION)
1997 {
1998 POINT point;
1999
2000 global_ime_set_font(&norm_logfont);
2001 point.x = FILL_X(gui.col);
2002 point.y = FILL_Y(gui.row);
2003 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2004 global_ime_set_position(&point);
2005 }
2006#endif
2007
2008#ifdef FEAT_MENU
2009 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2010 * mapped we need to stop it here to avoid strange effects (e.g., for the
2011 * key-up event) */
2012 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2013 NULL, NULL) == NULL)
2014#endif
2015 pDispatchMessage(&msg);
2016}
2017
2018/*
2019 * Catch up with any queued events. This may put keyboard input into the
2020 * input buffer, call resize call-backs, trigger timers etc. If there is
2021 * nothing in the event queue (& no timers pending), then we return
2022 * immediately.
2023 */
2024 void
2025gui_mch_update(void)
2026{
2027 MSG msg;
2028
2029 if (!s_busy_processing)
2030 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2031 && !vim_is_input_buf_full())
2032 process_message();
2033}
2034
Bram Moolenaar4231da42016-06-02 14:30:04 +02002035 static void
2036remove_any_timer(void)
2037{
2038 MSG msg;
2039
2040 if (s_wait_timer != 0 && !s_timed_out)
2041 {
2042 KillTimer(NULL, s_wait_timer);
2043
2044 /* Eat spurious WM_TIMER messages */
2045 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2046 ;
2047 s_wait_timer = 0;
2048 }
2049}
2050
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051/*
2052 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2053 * from the keyboard.
2054 * wtime == -1 Wait forever.
2055 * wtime == 0 This should never happen.
2056 * wtime > 0 Wait wtime milliseconds for a character.
2057 * Returns OK if a character was found to be available within the given time,
2058 * or FAIL otherwise.
2059 */
2060 int
2061gui_mch_wait_for_chars(int wtime)
2062{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002063 int focus;
2064
2065 s_timed_out = FALSE;
2066
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002067 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002069 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002070 if (s_busy_processing)
2071 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002072
2073 // When called with "wtime" zero, just want one msec.
2074 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075 (TIMERPROC)_OnTimer);
2076 }
2077
2078 allow_scrollbar = TRUE;
2079
2080 focus = gui.in_focus;
2081 while (!s_timed_out)
2082 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002083 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002084 if (gui.in_focus != focus)
2085 {
2086 if (gui.in_focus)
2087 gui_mch_start_blink();
2088 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002089 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002090 focus = gui.in_focus;
2091 }
2092
2093 if (s_need_activate)
2094 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002095 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002096 s_need_activate = FALSE;
2097 }
2098
Bram Moolenaar4231da42016-06-02 14:30:04 +02002099#ifdef FEAT_TIMERS
2100 did_add_timer = FALSE;
2101#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002102#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002103 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002104 for (;;)
2105 {
2106 MSG msg;
2107
2108 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002109# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002110 if (did_add_timer)
2111 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002112# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002113 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2114 {
2115 process_message();
2116 break;
2117 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002118 else if (input_available()
2119 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2120 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002121 break;
2122 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002123#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002124 // Don't use gui_mch_update() because then we will spin-lock until a
2125 // char arrives, instead we use GetMessage() to hang until an
2126 // event arrives. No need to check for input_buf_full because we are
2127 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002129#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002130
2131 if (input_available())
2132 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002133 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002134 allow_scrollbar = FALSE;
2135
Bram Moolenaar89c00032019-09-04 13:53:21 +02002136 // Clear pending mouse button, the release event may have been
2137 // taken by the dialog window. But don't do this when getting
2138 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002139 if (!s_getting_focus)
2140 s_button_pending = -1;
2141
2142 return OK;
2143 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002144
2145#ifdef FEAT_TIMERS
2146 if (did_add_timer)
2147 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002148 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002149 remove_any_timer();
2150 break;
2151 }
2152#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 }
2154 allow_scrollbar = FALSE;
2155 return FAIL;
2156}
2157
2158/*
2159 * Clear a rectangular region of the screen from text pos (row1, col1) to
2160 * (row2, col2) inclusive.
2161 */
2162 void
2163gui_mch_clear_block(
2164 int row1,
2165 int col1,
2166 int row2,
2167 int col2)
2168{
2169 RECT rc;
2170
2171 /*
2172 * Clear one extra pixel at the far right, for when bold characters have
2173 * spilled over to the window border.
2174 * Note: FillRect() excludes right and bottom of rectangle.
2175 */
2176 rc.left = FILL_X(col1);
2177 rc.top = FILL_Y(row1);
2178 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2179 rc.bottom = FILL_Y(row2 + 1);
2180 clear_rect(&rc);
2181}
2182
2183/*
2184 * Clear the whole text window.
2185 */
2186 void
2187gui_mch_clear_all(void)
2188{
2189 RECT rc;
2190
2191 rc.left = 0;
2192 rc.top = 0;
2193 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2194 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2195 clear_rect(&rc);
2196}
2197/*
2198 * Menu stuff.
2199 */
2200
2201 void
2202gui_mch_enable_menu(int flag)
2203{
2204#ifdef FEAT_MENU
2205 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2206#endif
2207}
2208
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209 void
2210gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002211 int x UNUSED,
2212 int y UNUSED,
2213 int w UNUSED,
2214 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002215{
2216 /* It will be in the right place anyway */
2217}
2218
2219#if defined(FEAT_MENU) || defined(PROTO)
2220/*
2221 * Make menu item hidden or not hidden
2222 */
2223 void
2224gui_mch_menu_hidden(
2225 vimmenu_T *menu,
2226 int hidden)
2227{
2228 /*
2229 * This doesn't do what we want. Hmm, just grey the menu items for now.
2230 */
2231 /*
2232 if (hidden)
2233 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2234 else
2235 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2236 */
2237 gui_mch_menu_grey(menu, hidden);
2238}
2239
2240/*
2241 * This is called after setting all the menus to grey/hidden or not.
2242 */
2243 void
2244gui_mch_draw_menubar(void)
2245{
2246 DrawMenuBar(s_hwnd);
2247}
2248#endif /*FEAT_MENU*/
2249
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002250/*
2251 * Return the RGB value of a pixel as a long.
2252 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002253 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002254gui_mch_get_rgb(guicolor_T pixel)
2255{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002256 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2257 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002258}
2259
2260#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2261/* Convert pixels in X to dialog units */
2262 static WORD
2263PixelToDialogX(int numPixels)
2264{
2265 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2266}
2267
2268/* Convert pixels in Y to dialog units */
2269 static WORD
2270PixelToDialogY(int numPixels)
2271{
2272 return (WORD)((numPixels * 8) / s_dlgfntheight);
2273}
2274
2275/* Return the width in pixels of the given text in the given DC. */
2276 static int
2277GetTextWidth(HDC hdc, char_u *str, int len)
2278{
2279 SIZE size;
2280
2281 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2282 return size.cx;
2283}
2284
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285/*
2286 * Return the width in pixels of the given text in the given DC, taking care
2287 * of 'encoding' to active codepage conversion.
2288 */
2289 static int
2290GetTextWidthEnc(HDC hdc, char_u *str, int len)
2291{
2292 SIZE size;
2293 WCHAR *wstr;
2294 int n;
2295 int wlen = len;
2296
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002297 wstr = enc_to_utf16(str, &wlen);
2298 if (wstr == NULL)
2299 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002301 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2302 vim_free(wstr);
2303 if (n)
2304 return size.cx;
2305 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002308static void get_work_area(RECT *spi_rect);
2309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310/*
2311 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002312 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2313 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314 */
2315 static BOOL
2316CenterWindow(
2317 HWND hwndChild,
2318 HWND hwndParent)
2319{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002320 HMONITOR mon;
2321 MONITORINFO moninfo;
2322 RECT rChild, rParent, rScreen;
2323 int wChild, hChild, wParent, hParent;
2324 int xNew, yNew;
2325 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002326
2327 GetWindowRect(hwndChild, &rChild);
2328 wChild = rChild.right - rChild.left;
2329 hChild = rChild.bottom - rChild.top;
2330
2331 /* If Vim is minimized put the window in the middle of the screen. */
2332 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002333 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002334 else
2335 GetWindowRect(hwndParent, &rParent);
2336 wParent = rParent.right - rParent.left;
2337 hParent = rParent.bottom - rParent.top;
2338
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002339 moninfo.cbSize = sizeof(MONITORINFO);
2340 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2341 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002342 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002343 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002345 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002346 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002347 hdc = GetDC(hwndChild);
2348 rScreen.left = 0;
2349 rScreen.top = 0;
2350 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2351 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2352 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353 }
2354
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002355 xNew = rParent.left + ((wParent - wChild) / 2);
2356 if (xNew < rScreen.left)
2357 xNew = rScreen.left;
2358 else if ((xNew + wChild) > rScreen.right)
2359 xNew = rScreen.right - wChild;
2360
2361 yNew = rParent.top + ((hParent - hChild) / 2);
2362 if (yNew < rScreen.top)
2363 yNew = rScreen.top;
2364 else if ((yNew + hChild) > rScreen.bottom)
2365 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366
2367 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2368 SWP_NOSIZE | SWP_NOZORDER);
2369}
2370#endif /* FEAT_GUI_DIALOG */
2371
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002372#if defined(FEAT_TOOLBAR) || defined(PROTO)
2373 void
2374gui_mch_show_toolbar(int showit)
2375{
2376 if (s_toolbarhwnd == NULL)
2377 return;
2378
2379 if (showit)
2380 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002381# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002382 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002383# define TB_SETUNICODEFORMAT 0x2005
2384# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002385 // Enable unicode support
2386 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2387 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388 ShowWindow(s_toolbarhwnd, SW_SHOW);
2389 }
2390 else
2391 ShowWindow(s_toolbarhwnd, SW_HIDE);
2392}
2393
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002394/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002395# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396
2397#endif
2398
2399#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2400 static void
2401add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2402{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002403 WCHAR *wn;
2404 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002405
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002406 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002408 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002409
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002410 infow.cbSize = sizeof(infow);
2411 infow.fMask = MIIM_TYPE | MIIM_ID;
2412 infow.wID = item_id;
2413 infow.fType = MFT_STRING;
2414 infow.dwTypeData = wn;
2415 infow.cch = (UINT)wcslen(wn);
2416 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2417 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002418}
2419
2420 static void
2421show_tabline_popup_menu(void)
2422{
2423 HMENU tab_pmenu;
2424 long rval;
2425 POINT pt;
2426
2427 /* When ignoring events don't show the menu. */
2428 if (hold_gui_events
2429# ifdef FEAT_CMDWIN
2430 || cmdwin_type != 0
2431# endif
2432 )
2433 return;
2434
2435 tab_pmenu = CreatePopupMenu();
2436 if (tab_pmenu == NULL)
2437 return;
2438
2439 if (first_tabpage->tp_next != NULL)
2440 add_tabline_popup_menu_entry(tab_pmenu,
2441 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2442 add_tabline_popup_menu_entry(tab_pmenu,
2443 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2444 add_tabline_popup_menu_entry(tab_pmenu,
2445 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2446
2447 GetCursorPos(&pt);
2448 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2449 NULL);
2450
2451 DestroyMenu(tab_pmenu);
2452
2453 /* Add the string cmd into input buffer */
2454 if (rval > 0)
2455 {
2456 TCHITTESTINFO htinfo;
2457 int idx;
2458
2459 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2460 return;
2461
2462 htinfo.pt.x = pt.x;
2463 htinfo.pt.y = pt.y;
2464 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2465 if (idx == -1)
2466 idx = 0;
2467 else
2468 idx += 1;
2469
2470 send_tabline_menu_event(idx, (int)rval);
2471 }
2472}
2473
2474/*
2475 * Show or hide the tabline.
2476 */
2477 void
2478gui_mch_show_tabline(int showit)
2479{
2480 if (s_tabhwnd == NULL)
2481 return;
2482
2483 if (!showit != !showing_tabline)
2484 {
2485 if (showit)
2486 ShowWindow(s_tabhwnd, SW_SHOW);
2487 else
2488 ShowWindow(s_tabhwnd, SW_HIDE);
2489 showing_tabline = showit;
2490 }
2491}
2492
2493/*
2494 * Return TRUE when tabline is displayed.
2495 */
2496 int
2497gui_mch_showing_tabline(void)
2498{
2499 return s_tabhwnd != NULL && showing_tabline;
2500}
2501
2502/*
2503 * Update the labels of the tabline.
2504 */
2505 void
2506gui_mch_update_tabline(void)
2507{
2508 tabpage_T *tp;
2509 TCITEM tie;
2510 int nr = 0;
2511 int curtabidx = 0;
2512 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514
2515 if (s_tabhwnd == NULL)
2516 return;
2517
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002518# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002519 /* For older compilers. We assume this never changes. */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002520# define CCM_SETUNICODEFORMAT 0x2005
2521# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002522 // Enable unicode support
2523 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524
2525 tie.mask = TCIF_TEXT;
2526 tie.iImage = -1;
2527
2528 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2529 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2530
2531 /* Add a label for each tab page. They all contain the same text area. */
2532 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2533 {
2534 if (tp == curtab)
2535 curtabidx = nr;
2536
2537 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2538 {
2539 /* Add the tab */
2540 tie.pszText = "-Empty-";
2541 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2542 tabadded = 1;
2543 }
2544
2545 get_tabline_label(tp, FALSE);
2546 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002547
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002548 wstr = enc_to_utf16(NameBuff, NULL);
2549 if (wstr != NULL)
2550 {
2551 TCITEMW tiw;
2552
2553 tiw.mask = TCIF_TEXT;
2554 tiw.iImage = -1;
2555 tiw.pszText = wstr;
2556 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2557 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002558 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559 }
2560
2561 /* Remove any old labels. */
2562 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2563 TabCtrl_DeleteItem(s_tabhwnd, nr);
2564
2565 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2566 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2567
2568 /* Re-enable redraw and redraw. */
2569 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2570 RedrawWindow(s_tabhwnd, NULL, NULL,
2571 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2572
2573 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2574 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2575}
2576
2577/*
2578 * Set the current tab to "nr". First tab is 1.
2579 */
2580 void
2581gui_mch_set_curtab(int nr)
2582{
2583 if (s_tabhwnd == NULL)
2584 return;
2585
2586 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2587 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2588}
2589
2590#endif
2591
2592/*
2593 * ":simalt" command.
2594 */
2595 void
2596ex_simalt(exarg_T *eap)
2597{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002598 char_u *keys = eap->arg;
2599 int fill_typebuf = FALSE;
2600 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002601
2602 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2603 while (*keys)
2604 {
2605 if (*keys == '~')
2606 *keys = ' '; /* for showing system menu */
2607 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2608 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002609 fill_typebuf = TRUE;
2610 }
2611 if (fill_typebuf)
2612 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002613 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002614 * processed. */
2615 key_name[0] = K_SPECIAL;
2616 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002617 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002618 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002619#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002620 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002621#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002622 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002623 }
2624}
2625
2626/*
2627 * Create the find & replace dialogs.
2628 * You can't have both at once: ":find" when replace is showing, destroys
2629 * the replace dialog first, and the other way around.
2630 */
2631#ifdef MSWIN_FIND_REPLACE
2632 static void
2633initialise_findrep(char_u *initial_string)
2634{
2635 int wword = FALSE;
2636 int mcase = !p_ic;
2637 char_u *entry_text;
2638
2639 /* Get the search string to use. */
2640 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2641
2642 s_findrep_struct.hwndOwner = s_hwnd;
2643 s_findrep_struct.Flags = FR_DOWN;
2644 if (mcase)
2645 s_findrep_struct.Flags |= FR_MATCHCASE;
2646 if (wword)
2647 s_findrep_struct.Flags |= FR_WHOLEWORD;
2648 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002649 {
2650 WCHAR *p = enc_to_utf16(entry_text, NULL);
2651 if (p != NULL)
2652 {
2653 int len = s_findrep_struct.wFindWhatLen - 1;
2654
2655 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2656 s_findrep_struct.lpstrFindWhat[len] = NUL;
2657 vim_free(p);
2658 }
2659 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002660 vim_free(entry_text);
2661}
2662#endif
2663
2664 static void
2665set_window_title(HWND hwnd, char *title)
2666{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002667 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002668 {
2669 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670
2671 /* Convert the title from 'encoding' to UTF-16. */
2672 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2673 if (wbuf != NULL)
2674 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002675 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002676 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002677 }
2678 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002679 else
2680 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002681}
2682
2683 void
2684gui_mch_find_dialog(exarg_T *eap)
2685{
2686#ifdef MSWIN_FIND_REPLACE
2687 if (s_findrep_msg != 0)
2688 {
2689 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2690 DestroyWindow(s_findrep_hwnd);
2691
2692 if (!IsWindow(s_findrep_hwnd))
2693 {
2694 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002695 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002696 }
2697
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002698 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002699 (void)SetFocus(s_findrep_hwnd);
2700
2701 s_findrep_is_find = TRUE;
2702 }
2703#endif
2704}
2705
2706
2707 void
2708gui_mch_replace_dialog(exarg_T *eap)
2709{
2710#ifdef MSWIN_FIND_REPLACE
2711 if (s_findrep_msg != 0)
2712 {
2713 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2714 DestroyWindow(s_findrep_hwnd);
2715
2716 if (!IsWindow(s_findrep_hwnd))
2717 {
2718 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002719 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 }
2721
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002722 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 (void)SetFocus(s_findrep_hwnd);
2724
2725 s_findrep_is_find = FALSE;
2726 }
2727#endif
2728}
2729
2730
2731/*
2732 * Set visibility of the pointer.
2733 */
2734 void
2735gui_mch_mousehide(int hide)
2736{
2737 if (hide != gui.pointer_hidden)
2738 {
2739 ShowCursor(!hide);
2740 gui.pointer_hidden = hide;
2741 }
2742}
2743
2744#ifdef FEAT_MENU
2745 static void
2746gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2747{
2748 /* Unhide the mouse, we don't get move events here. */
2749 gui_mch_mousehide(FALSE);
2750
2751 (void)TrackPopupMenu(
2752 (HMENU)menu->submenu_id,
2753 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2754 x, y,
2755 (int)0, /*reserved param*/
2756 s_hwnd,
2757 NULL);
2758 /*
2759 * NOTE: The pop-up menu can eat the mouse up event.
2760 * We deal with this in normal.c.
2761 */
2762}
2763#endif
2764
2765/*
2766 * Got a message when the system will go down.
2767 */
2768 static void
2769_OnEndSession(void)
2770{
2771 getout_preserve_modified(1);
2772}
2773
2774/*
2775 * Get this message when the user clicks on the cross in the top right corner
2776 * of a Windows95 window.
2777 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002778 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002779_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002780{
2781 gui_shell_closed();
2782}
2783
2784/*
2785 * Get a message when the window is being destroyed.
2786 */
2787 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002788_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002789{
2790 if (!destroying)
2791 _OnClose(hwnd);
2792}
2793
2794 static void
2795_OnPaint(
2796 HWND hwnd)
2797{
2798 if (!IsMinimized(hwnd))
2799 {
2800 PAINTSTRUCT ps;
2801
2802 out_flush(); /* make sure all output has been processed */
2803 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002804
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 /* prevent multi-byte characters from misprinting on an invalid
2806 * rectangle */
2807 if (has_mbyte)
2808 {
2809 RECT rect;
2810
2811 GetClientRect(hwnd, &rect);
2812 ps.rcPaint.left = rect.left;
2813 ps.rcPaint.right = rect.right;
2814 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002815
2816 if (!IsRectEmpty(&ps.rcPaint))
2817 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2819 ps.rcPaint.right - ps.rcPaint.left + 1,
2820 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2821 }
2822
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 EndPaint(hwnd, &ps);
2824 }
2825}
2826
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827 static void
2828_OnSize(
2829 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002830 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831 int cx,
2832 int cy)
2833{
2834 if (!IsMinimized(hwnd))
2835 {
2836 gui_resize_shell(cx, cy);
2837
2838#ifdef FEAT_MENU
2839 /* Menu bar may wrap differently now */
2840 gui_mswin_get_menu_height(TRUE);
2841#endif
2842 }
2843}
2844
2845 static void
2846_OnSetFocus(
2847 HWND hwnd,
2848 HWND hwndOldFocus)
2849{
2850 gui_focus_change(TRUE);
2851 s_getting_focus = TRUE;
2852 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2853}
2854
2855 static void
2856_OnKillFocus(
2857 HWND hwnd,
2858 HWND hwndNewFocus)
2859{
2860 gui_focus_change(FALSE);
2861 s_getting_focus = FALSE;
2862 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2863}
2864
2865/*
2866 * Get a message when the user switches back to vim
2867 */
2868 static LRESULT
2869_OnActivateApp(
2870 HWND hwnd,
2871 BOOL fActivate,
2872 DWORD dwThreadId)
2873{
2874 /* we call gui_focus_change() in _OnSetFocus() */
2875 /* gui_focus_change((int)fActivate); */
2876 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2877}
2878
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 void
2880gui_mch_destroy_scrollbar(scrollbar_T *sb)
2881{
2882 DestroyWindow(sb->id);
2883}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884
2885/*
2886 * Get current mouse coordinates in text window.
2887 */
2888 void
2889gui_mch_getmouse(int *x, int *y)
2890{
2891 RECT rct;
2892 POINT mp;
2893
2894 (void)GetWindowRect(s_textArea, &rct);
2895 (void)GetCursorPos((LPPOINT)&mp);
2896 *x = (int)(mp.x - rct.left);
2897 *y = (int)(mp.y - rct.top);
2898}
2899
2900/*
2901 * Move mouse pointer to character at (x, y).
2902 */
2903 void
2904gui_mch_setmouse(int x, int y)
2905{
2906 RECT rct;
2907
2908 (void)GetWindowRect(s_textArea, &rct);
2909 (void)SetCursorPos(x + gui.border_offset + rct.left,
2910 y + gui.border_offset + rct.top);
2911}
2912
2913 static void
2914gui_mswin_get_valid_dimensions(
2915 int w,
2916 int h,
2917 int *valid_w,
2918 int *valid_h)
2919{
2920 int base_width, base_height;
2921
2922 base_width = gui_get_base_width()
2923 + (GetSystemMetrics(SM_CXFRAME) +
2924 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2925 base_height = gui_get_base_height()
2926 + (GetSystemMetrics(SM_CYFRAME) +
2927 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2928 + GetSystemMetrics(SM_CYCAPTION)
2929#ifdef FEAT_MENU
2930 + gui_mswin_get_menu_height(FALSE)
2931#endif
2932 ;
2933 *valid_w = base_width +
2934 ((w - base_width) / gui.char_width) * gui.char_width;
2935 *valid_h = base_height +
2936 ((h - base_height) / gui.char_height) * gui.char_height;
2937}
2938
2939 void
2940gui_mch_flash(int msec)
2941{
2942 RECT rc;
2943
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002944#if defined(FEAT_DIRECTX)
2945 if (IS_ENABLE_DIRECTX())
2946 DWriteContext_Flush(s_dwc);
2947#endif
2948
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002949 /*
2950 * Note: InvertRect() excludes right and bottom of rectangle.
2951 */
2952 rc.left = 0;
2953 rc.top = 0;
2954 rc.right = gui.num_cols * gui.char_width;
2955 rc.bottom = gui.num_rows * gui.char_height;
2956 InvertRect(s_hdc, &rc);
2957 gui_mch_flush(); /* make sure it's displayed */
2958
2959 ui_delay((long)msec, TRUE); /* wait for a few msec */
2960
2961 InvertRect(s_hdc, &rc);
2962}
2963
2964/*
2965 * Return flags used for scrolling.
2966 * The SW_INVALIDATE is required when part of the window is covered or
2967 * off-screen. Refer to MS KB Q75236.
2968 */
2969 static int
2970get_scroll_flags(void)
2971{
2972 HWND hwnd;
2973 RECT rcVim, rcOther, rcDest;
2974
2975 GetWindowRect(s_hwnd, &rcVim);
2976
2977 /* Check if the window is partly above or below the screen. We don't care
2978 * about partly left or right of the screen, it is not relevant when
2979 * scrolling up or down. */
2980 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2981 return SW_INVALIDATE;
2982
2983 /* Check if there is an window (partly) on top of us. */
2984 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2985 if (IsWindowVisible(hwnd))
2986 {
2987 GetWindowRect(hwnd, &rcOther);
2988 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2989 return SW_INVALIDATE;
2990 }
2991 return 0;
2992}
2993
2994/*
2995 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
2996 * may not be scrolled out properly.
2997 * For gVim, when _OnScroll() is repeated, the character at the
2998 * previous cursor position may be left drawn after scroll.
2999 * The problem can be avoided by calling GetPixel() to get a pixel in
3000 * the region before ScrollWindowEx().
3001 */
3002 static void
3003intel_gpu_workaround(void)
3004{
3005 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3006}
3007
3008/*
3009 * Delete the given number of lines from the given row, scrolling up any
3010 * text further down within the scroll region.
3011 */
3012 void
3013gui_mch_delete_lines(
3014 int row,
3015 int num_lines)
3016{
3017 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003018
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019 rc.left = FILL_X(gui.scroll_region_left);
3020 rc.right = FILL_X(gui.scroll_region_right + 1);
3021 rc.top = FILL_Y(row);
3022 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3023
Bram Moolenaar92467d32017-12-05 13:22:16 +01003024#if defined(FEAT_DIRECTX)
3025 if (IS_ENABLE_DIRECTX())
3026 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003027 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3028 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003029 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003030 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003031#endif
3032 {
3033 intel_gpu_workaround();
3034 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003035 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003036 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003037 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003038
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039 /* This seems to be required to avoid the cursor disappearing when
3040 * scrolling such that the cursor ends up in the top-left character on
3041 * the screen... But why? (Webb) */
3042 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3043 /* gui.cursor_is_valid = FALSE; */
3044
3045 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3046 gui.scroll_region_left,
3047 gui.scroll_region_bot, gui.scroll_region_right);
3048}
3049
3050/*
3051 * Insert the given number of lines before the given row, scrolling down any
3052 * following text within the scroll region.
3053 */
3054 void
3055gui_mch_insert_lines(
3056 int row,
3057 int num_lines)
3058{
3059 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003060
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003061 rc.left = FILL_X(gui.scroll_region_left);
3062 rc.right = FILL_X(gui.scroll_region_right + 1);
3063 rc.top = FILL_Y(row);
3064 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003065
3066#if defined(FEAT_DIRECTX)
3067 if (IS_ENABLE_DIRECTX())
3068 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003069 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3070 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003071 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003072 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003073#endif
3074 {
3075 intel_gpu_workaround();
3076 /* The SW_INVALIDATE is required when part of the window is covered or
3077 * off-screen. How do we avoid it when it's not needed? */
3078 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003079 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003080 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003081 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003082
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003083 gui_clear_block(row, gui.scroll_region_left,
3084 row + num_lines - 1, gui.scroll_region_right);
3085}
3086
3087
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003089gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003090{
3091#if defined(FEAT_DIRECTX)
3092 DWriteContext_Close(s_dwc);
3093 DWrite_Final();
3094 s_dwc = NULL;
3095#endif
3096
3097 ReleaseDC(s_textArea, s_hdc);
3098 DeleteObject(s_brush);
3099
3100#ifdef FEAT_TEAROFF
3101 /* Unload the tearoff bitmap */
3102 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3103#endif
3104
3105 /* Destroy our window (if we have one). */
3106 if (s_hwnd != NULL)
3107 {
3108 destroying = TRUE; /* ignore WM_DESTROY message now */
3109 DestroyWindow(s_hwnd);
3110 }
3111
3112#ifdef GLOBAL_IME
3113 global_ime_end();
3114#endif
3115}
3116
3117 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003118logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119{
3120 char *p;
3121 char *res;
3122 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003123 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003124 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003125 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003127 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3128 if (font_name == NULL)
3129 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003131 quality_name = quality_id2name((int)lf.lfQuality);
3132
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003133 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003134 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003135 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003136 if (res != NULL)
3137 {
3138 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003139 // make a normal font string out of the lf thing:
3140 points = pixels_to_points(
3141 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3142 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3143 sprintf((char *)p, "%s:h%d", font_name, points);
3144 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003145 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 while (*p)
3147 {
3148 if (*p == ' ')
3149 *p = '_';
3150 ++p;
3151 }
3152 if (lf.lfItalic)
3153 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003154 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003155 STRCAT(p, ":b");
3156 if (lf.lfUnderline)
3157 STRCAT(p, ":u");
3158 if (lf.lfStrikeOut)
3159 STRCAT(p, ":s");
3160 if (charset_name != NULL)
3161 {
3162 STRCAT(p, ":c");
3163 STRCAT(p, charset_name);
3164 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003165 if (quality_name != NULL)
3166 {
3167 STRCAT(p, ":q");
3168 STRCAT(p, quality_name);
3169 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 }
3171
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003172 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173 return (char_u *)res;
3174}
3175
3176
3177#ifdef FEAT_MBYTE_IME
3178/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003179 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180 * 'guifont'
3181 */
3182 static void
3183update_im_font(void)
3184{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003185 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003186
3187 if (p_guifontwide != NULL && *p_guifontwide != NUL
3188 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003189 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003190 norm_logfont = lf_wide;
3191 else
3192 norm_logfont = sub_logfont;
3193 im_set_font(&norm_logfont);
3194}
3195#endif
3196
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003197/*
3198 * Handler of gui.wide_font (p_guifontwide) changed notification.
3199 */
3200 void
3201gui_mch_wide_font_changed(void)
3202{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003204
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003205#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003207#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003208
3209 gui_mch_free_font(gui.wide_ital_font);
3210 gui.wide_ital_font = NOFONT;
3211 gui_mch_free_font(gui.wide_bold_font);
3212 gui.wide_bold_font = NOFONT;
3213 gui_mch_free_font(gui.wide_boldital_font);
3214 gui.wide_boldital_font = NOFONT;
3215
3216 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003217 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 {
3219 if (!lf.lfItalic)
3220 {
3221 lf.lfItalic = TRUE;
3222 gui.wide_ital_font = get_font_handle(&lf);
3223 lf.lfItalic = FALSE;
3224 }
3225 if (lf.lfWeight < FW_BOLD)
3226 {
3227 lf.lfWeight = FW_BOLD;
3228 gui.wide_bold_font = get_font_handle(&lf);
3229 if (!lf.lfItalic)
3230 {
3231 lf.lfItalic = TRUE;
3232 gui.wide_boldital_font = get_font_handle(&lf);
3233 }
3234 }
3235 }
3236}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003237
3238/*
3239 * Initialise vim to use the font with the given name.
3240 * Return FAIL if the font could not be loaded, OK otherwise.
3241 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003243gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003244{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003245 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003246 GuiFont font = NOFONT;
3247 char_u *p;
3248
3249 /* Load the font */
3250 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3251 font = get_font_handle(&lf);
3252 if (font == NOFONT)
3253 return FAIL;
3254
3255 if (font_name == NULL)
3256 font_name = (char_u *)lf.lfFaceName;
3257#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3258 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003259#endif
3260#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261 sub_logfont = lf;
3262#endif
3263#ifdef FEAT_MBYTE_IME
3264 update_im_font();
3265#endif
3266 gui_mch_free_font(gui.norm_font);
3267 gui.norm_font = font;
3268 current_font_height = lf.lfHeight;
3269 GetFontSize(font);
3270
3271 p = logfont2name(lf);
3272 if (p != NULL)
3273 {
3274 hl_set_font_name(p);
3275
3276 /* When setting 'guifont' to "*" replace it with the actual font name.
3277 * */
3278 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3279 {
3280 vim_free(p_guifont);
3281 p_guifont = p;
3282 }
3283 else
3284 vim_free(p);
3285 }
3286
3287 gui_mch_free_font(gui.ital_font);
3288 gui.ital_font = NOFONT;
3289 gui_mch_free_font(gui.bold_font);
3290 gui.bold_font = NOFONT;
3291 gui_mch_free_font(gui.boldital_font);
3292 gui.boldital_font = NOFONT;
3293
3294 if (!lf.lfItalic)
3295 {
3296 lf.lfItalic = TRUE;
3297 gui.ital_font = get_font_handle(&lf);
3298 lf.lfItalic = FALSE;
3299 }
3300 if (lf.lfWeight < FW_BOLD)
3301 {
3302 lf.lfWeight = FW_BOLD;
3303 gui.bold_font = get_font_handle(&lf);
3304 if (!lf.lfItalic)
3305 {
3306 lf.lfItalic = TRUE;
3307 gui.boldital_font = get_font_handle(&lf);
3308 }
3309 }
3310
3311 return OK;
3312}
3313
3314#ifndef WPF_RESTORETOMAXIMIZED
3315# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3316#endif
3317
3318/*
3319 * Return TRUE if the GUI window is maximized, filling the whole screen.
3320 */
3321 int
3322gui_mch_maximized(void)
3323{
3324 WINDOWPLACEMENT wp;
3325
3326 wp.length = sizeof(WINDOWPLACEMENT);
3327 if (GetWindowPlacement(s_hwnd, &wp))
3328 return wp.showCmd == SW_SHOWMAXIMIZED
3329 || (wp.showCmd == SW_SHOWMINIMIZED
3330 && wp.flags == WPF_RESTORETOMAXIMIZED);
3331
3332 return 0;
3333}
3334
3335/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003336 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3337 * is set. Compute the new Rows and Columns. This is like resizing the
3338 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339 */
3340 void
3341gui_mch_newfont(void)
3342{
3343 RECT rect;
3344
3345 GetWindowRect(s_hwnd, &rect);
3346 if (win_socket_id == 0)
3347 {
3348 gui_resize_shell(rect.right - rect.left
3349 - (GetSystemMetrics(SM_CXFRAME) +
3350 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3351 rect.bottom - rect.top
3352 - (GetSystemMetrics(SM_CYFRAME) +
3353 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3354 - GetSystemMetrics(SM_CYCAPTION)
3355#ifdef FEAT_MENU
3356 - gui_mswin_get_menu_height(FALSE)
3357#endif
3358 );
3359 }
3360 else
3361 {
3362 /* Inside another window, don't use the frame and border. */
3363 gui_resize_shell(rect.right - rect.left,
3364 rect.bottom - rect.top
3365#ifdef FEAT_MENU
3366 - gui_mswin_get_menu_height(FALSE)
3367#endif
3368 );
3369 }
3370}
3371
3372/*
3373 * Set the window title
3374 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375 void
3376gui_mch_settitle(
3377 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003378 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379{
3380 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3381}
3382
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003383#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3385 * misc2.c! */
3386static LPCSTR mshape_idcs[] =
3387{
3388 IDC_ARROW, /* arrow */
3389 MAKEINTRESOURCE(0), /* blank */
3390 IDC_IBEAM, /* beam */
3391 IDC_SIZENS, /* updown */
3392 IDC_SIZENS, /* udsizing */
3393 IDC_SIZEWE, /* leftright */
3394 IDC_SIZEWE, /* lrsizing */
3395 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 IDC_ARROW, /* crosshair */
3398 IDC_ARROW, /* hand1 */
3399 IDC_ARROW, /* hand2 */
3400 IDC_ARROW, /* pencil */
3401 IDC_ARROW, /* question */
3402 IDC_ARROW, /* right-arrow */
3403 IDC_UPARROW, /* up-arrow */
3404 IDC_ARROW /* last one */
3405};
3406
3407 void
3408mch_set_mouse_shape(int shape)
3409{
3410 LPCSTR idc;
3411
3412 if (shape == MSHAPE_HIDE)
3413 ShowCursor(FALSE);
3414 else
3415 {
3416 if (shape >= MSHAPE_NUMBERED)
3417 idc = IDC_ARROW;
3418 else
3419 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003420 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003421 if (!p_mh)
3422 {
3423 POINT mp;
3424
3425 /* Set the position to make it redrawn with the new shape. */
3426 (void)GetCursorPos((LPPOINT)&mp);
3427 (void)SetCursorPos(mp.x, mp.y);
3428 ShowCursor(TRUE);
3429 }
3430 }
3431}
3432#endif
3433
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003434#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436 * Wide version of convert_filter().
3437 */
3438 static WCHAR *
3439convert_filterW(char_u *s)
3440{
3441 char_u *tmp;
3442 int len;
3443 WCHAR *res;
3444
3445 tmp = convert_filter(s);
3446 if (tmp == NULL)
3447 return NULL;
3448 len = (int)STRLEN(s) + 3;
3449 res = enc_to_utf16(tmp, &len);
3450 vim_free(tmp);
3451 return res;
3452}
3453
3454/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003455 * Pop open a file browser and return the file selected, in allocated memory,
3456 * or NULL if Cancel is hit.
3457 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3458 * title - Title message for the file browser dialog.
3459 * dflt - Default name of file.
3460 * ext - Default extension to be added to files without extensions.
3461 * initdir - directory in which to open the browser (NULL = current dir)
3462 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003464 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003465gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466 int saving,
3467 char_u *title,
3468 char_u *dflt,
3469 char_u *ext,
3470 char_u *initdir,
3471 char_u *filter)
3472{
3473 /* We always use the wide function. This means enc_to_utf16() must work,
3474 * otherwise it fails miserably! */
3475 OPENFILENAMEW fileStruct;
3476 WCHAR fileBuf[MAXPATHL];
3477 WCHAR *wp;
3478 int i;
3479 WCHAR *titlep = NULL;
3480 WCHAR *extp = NULL;
3481 WCHAR *initdirp = NULL;
3482 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003483 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484
3485 if (dflt == NULL)
3486 fileBuf[0] = NUL;
3487 else
3488 {
3489 wp = enc_to_utf16(dflt, NULL);
3490 if (wp == NULL)
3491 fileBuf[0] = NUL;
3492 else
3493 {
3494 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3495 fileBuf[i] = wp[i];
3496 fileBuf[i] = NUL;
3497 vim_free(wp);
3498 }
3499 }
3500
3501 /* Convert the filter to Windows format. */
3502 filterp = convert_filterW(filter);
3503
3504 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003505# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003507 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003508# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003509 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003510# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511
3512 if (title != NULL)
3513 titlep = enc_to_utf16(title, NULL);
3514 fileStruct.lpstrTitle = titlep;
3515
3516 if (ext != NULL)
3517 extp = enc_to_utf16(ext, NULL);
3518 fileStruct.lpstrDefExt = extp;
3519
3520 fileStruct.lpstrFile = fileBuf;
3521 fileStruct.nMaxFile = MAXPATHL;
3522 fileStruct.lpstrFilter = filterp;
3523 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3524 /* has an initial dir been specified? */
3525 if (initdir != NULL && *initdir != NUL)
3526 {
3527 /* Must have backslashes here, no matter what 'shellslash' says */
3528 initdirp = enc_to_utf16(initdir, NULL);
3529 if (initdirp != NULL)
3530 {
3531 for (wp = initdirp; *wp != NUL; ++wp)
3532 if (*wp == '/')
3533 *wp = '\\';
3534 }
3535 fileStruct.lpstrInitialDir = initdirp;
3536 }
3537
3538 /*
3539 * TODO: Allow selection of multiple files. Needs another arg to this
3540 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3541 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3542 * files that don't exist yet, so I haven't put it in. What about
3543 * OFN_PATHMUSTEXIST?
3544 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3545 */
3546 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003547# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003548 if (curbuf->b_p_bin)
3549 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003550# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003551 if (saving)
3552 {
3553 if (!GetSaveFileNameW(&fileStruct))
3554 return NULL;
3555 }
3556 else
3557 {
3558 if (!GetOpenFileNameW(&fileStruct))
3559 return NULL;
3560 }
3561
3562 vim_free(filterp);
3563 vim_free(initdirp);
3564 vim_free(titlep);
3565 vim_free(extp);
3566
3567 /* Convert from UCS2 to 'encoding'. */
3568 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003569 if (p == NULL)
3570 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571
3572 /* Give focus back to main window (when using MDI). */
3573 SetFocus(s_hwnd);
3574
3575 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003576 q = vim_strsave(shorten_fname1(p));
3577 vim_free(p);
3578 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003579}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580
3581
3582/*
3583 * Convert the string s to the proper format for a filter string by replacing
3584 * the \t and \n delimiters with \0.
3585 * Returns the converted string in allocated memory.
3586 *
3587 * Keep in sync with convert_filterW() above!
3588 */
3589 static char_u *
3590convert_filter(char_u *s)
3591{
3592 char_u *res;
3593 unsigned s_len = (unsigned)STRLEN(s);
3594 unsigned i;
3595
3596 res = alloc(s_len + 3);
3597 if (res != NULL)
3598 {
3599 for (i = 0; i < s_len; ++i)
3600 if (s[i] == '\t' || s[i] == '\n')
3601 res[i] = '\0';
3602 else
3603 res[i] = s[i];
3604 res[s_len] = NUL;
3605 /* Add two extra NULs to make sure it's properly terminated. */
3606 res[s_len + 1] = NUL;
3607 res[s_len + 2] = NUL;
3608 }
3609 return res;
3610}
3611
3612/*
3613 * Select a directory.
3614 */
3615 char_u *
3616gui_mch_browsedir(char_u *title, char_u *initdir)
3617{
3618 /* We fake this: Use a filter that doesn't select anything and a default
3619 * file name that won't be used. */
3620 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3621 initdir, (char_u *)_("Directory\t*.nothing\n"));
3622}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623#endif /* FEAT_BROWSE */
3624
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625 static void
3626_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003627 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 HDROP hDrop)
3629{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003630#define BUFPATHLEN _MAX_PATH
3631#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003633 char szFile[BUFPATHLEN];
3634 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3635 UINT i;
3636 char_u **fnames;
3637 POINT pt;
3638 int_u modifiers = 0;
3639
3640 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3641
3642 /* Obtain dropped position */
3643 DragQueryPoint(hDrop, &pt);
3644 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3645
3646 reset_VIsual();
3647
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003648 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649
3650 if (fnames != NULL)
3651 for (i = 0; i < cFiles; ++i)
3652 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3654 fnames[i] = utf16_to_enc(wszFile, NULL);
3655 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656 {
3657 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3658 fnames[i] = vim_strsave((char_u *)szFile);
3659 }
3660 }
3661
3662 DragFinish(hDrop);
3663
3664 if (fnames != NULL)
3665 {
3666 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3667 modifiers |= MOUSE_SHIFT;
3668 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3669 modifiers |= MOUSE_CTRL;
3670 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3671 modifiers |= MOUSE_ALT;
3672
3673 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3674
3675 s_need_activate = TRUE;
3676 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677}
3678
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679 static int
3680_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003681 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003682 HWND hwndCtl,
3683 UINT code,
3684 int pos)
3685{
3686 static UINT prev_code = 0; /* code of previous call */
3687 scrollbar_T *sb, *sb_info;
3688 long val;
3689 int dragging = FALSE;
3690 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 SCROLLINFO si;
3692
3693 si.cbSize = sizeof(si);
3694 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695
3696 sb = gui_mswin_find_scrollbar(hwndCtl);
3697 if (sb == NULL)
3698 return 0;
3699
3700 if (sb->wp != NULL) /* Left or right scrollbar */
3701 {
3702 /*
3703 * Careful: need to get scrollbar info out of first (left) scrollbar
3704 * for window, but keep real scrollbar too because we must pass it to
3705 * gui_drag_scrollbar().
3706 */
3707 sb_info = &sb->wp->w_scrollbars[0];
3708 }
3709 else /* Bottom scrollbar */
3710 sb_info = sb;
3711 val = sb_info->value;
3712
3713 switch (code)
3714 {
3715 case SB_THUMBTRACK:
3716 val = pos;
3717 dragging = TRUE;
3718 if (sb->scroll_shift > 0)
3719 val <<= sb->scroll_shift;
3720 break;
3721 case SB_LINEDOWN:
3722 val++;
3723 break;
3724 case SB_LINEUP:
3725 val--;
3726 break;
3727 case SB_PAGEDOWN:
3728 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3729 break;
3730 case SB_PAGEUP:
3731 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3732 break;
3733 case SB_TOP:
3734 val = 0;
3735 break;
3736 case SB_BOTTOM:
3737 val = sb_info->max;
3738 break;
3739 case SB_ENDSCROLL:
3740 if (prev_code == SB_THUMBTRACK)
3741 {
3742 /*
3743 * "pos" only gives us 16-bit data. In case of large file,
3744 * use GetScrollPos() which returns 32-bit. Unfortunately it
3745 * is not valid while the scrollbar is being dragged.
3746 */
3747 val = GetScrollPos(hwndCtl, SB_CTL);
3748 if (sb->scroll_shift > 0)
3749 val <<= sb->scroll_shift;
3750 }
3751 break;
3752
3753 default:
3754 /* TRACE("Unknown scrollbar event %d\n", code); */
3755 return 0;
3756 }
3757 prev_code = code;
3758
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3760 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761
3762 /*
3763 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3764 */
3765 if (sb->wp != NULL)
3766 {
3767 scrollbar_T *sba = sb->wp->w_scrollbars;
3768 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3769
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 }
3772
3773 /* Don't let us be interrupted here by another message. */
3774 s_busy_processing = TRUE;
3775
3776 /* When "allow_scrollbar" is FALSE still need to remember the new
3777 * position, but don't actually scroll by setting "dont_scroll". */
3778 dont_scroll = !allow_scrollbar;
3779
Bram Moolenaara338adc2018-01-31 20:51:47 +01003780 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003782 mch_enable_flush();
3783 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784
3785 s_busy_processing = FALSE;
3786 dont_scroll = dont_scroll_save;
3787
3788 return 0;
3789}
3790
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792#ifdef FEAT_XPM_W32
3793# include "xpm_w32.h"
3794#endif
3795
3796#ifdef PROTO
3797# define WINAPI
3798#endif
3799
3800#ifdef __MINGW32__
3801/*
3802 * Add a lot of missing defines.
3803 * They are not always missing, we need the #ifndef's.
3804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805# ifndef IsMinimized
3806# define IsMinimized(hwnd) IsIconic(hwnd)
3807# endif
3808# ifndef IsMaximized
3809# define IsMaximized(hwnd) IsZoomed(hwnd)
3810# endif
3811# ifndef SelectFont
3812# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3813# endif
3814# ifndef GetStockBrush
3815# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3816# endif
3817# ifndef DeleteBrush
3818# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3819# endif
3820
3821# ifndef HANDLE_WM_RBUTTONDBLCLK
3822# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3823 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3824# endif
3825# ifndef HANDLE_WM_MBUTTONUP
3826# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3827 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3828# endif
3829# ifndef HANDLE_WM_MBUTTONDBLCLK
3830# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3831 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3832# endif
3833# ifndef HANDLE_WM_LBUTTONDBLCLK
3834# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3835 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3836# endif
3837# ifndef HANDLE_WM_RBUTTONDOWN
3838# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3839 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3840# endif
3841# ifndef HANDLE_WM_MOUSEMOVE
3842# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3843 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3844# endif
3845# ifndef HANDLE_WM_RBUTTONUP
3846# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3847 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3848# endif
3849# ifndef HANDLE_WM_MBUTTONDOWN
3850# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3851 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3852# endif
3853# ifndef HANDLE_WM_LBUTTONUP
3854# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3855 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3856# endif
3857# ifndef HANDLE_WM_LBUTTONDOWN
3858# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3859 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3860# endif
3861# ifndef HANDLE_WM_SYSCHAR
3862# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3863 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3864# endif
3865# ifndef HANDLE_WM_ACTIVATEAPP
3866# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3867 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3868# endif
3869# ifndef HANDLE_WM_WINDOWPOSCHANGING
3870# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3871 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3872# endif
3873# ifndef HANDLE_WM_VSCROLL
3874# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3875 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3876# endif
3877# ifndef HANDLE_WM_SETFOCUS
3878# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3879 ((fn)((hwnd), (HWND)(wParam)), 0L)
3880# endif
3881# ifndef HANDLE_WM_KILLFOCUS
3882# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3883 ((fn)((hwnd), (HWND)(wParam)), 0L)
3884# endif
3885# ifndef HANDLE_WM_HSCROLL
3886# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3887 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3888# endif
3889# ifndef HANDLE_WM_DROPFILES
3890# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3891 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3892# endif
3893# ifndef HANDLE_WM_CHAR
3894# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3895 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3896# endif
3897# ifndef HANDLE_WM_SYSDEADCHAR
3898# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3899 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3900# endif
3901# ifndef HANDLE_WM_DEADCHAR
3902# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3903 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3904# endif
3905#endif /* __MINGW32__ */
3906
3907
3908/* Some parameters for tearoff menus. All in pixels. */
3909#define TEAROFF_PADDING_X 2
3910#define TEAROFF_BUTTON_PAD_X 8
3911#define TEAROFF_MIN_WIDTH 200
3912#define TEAROFF_SUBMENU_LABEL ">>"
3913#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3914
3915
3916/* For the Intellimouse: */
3917#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003918# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919#endif
3920
3921
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003922#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923# define ID_BEVAL_TOOLTIP 200
3924# define BEVAL_TEXT_LEN MAXPATHL
3925
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003926# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003927/* Work around old versions of basetsd.h which wrongly declares
3928 * UINT_PTR as unsigned long. */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003929# undef UINT_PTR
3930# define UINT_PTR UINT
3931# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003934static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003936
Bram Moolenaar82881492012-11-20 16:53:39 +01003937
3938/* cproto fails on missing include files */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003939# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01003940
Bram Moolenaar45360022005-07-21 21:08:21 +00003941/*
3942 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003943 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003944 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003945# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01003946
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003947# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003948
3949typedef struct _DllVersionInfo
3950{
3951 DWORD cbSize;
3952 DWORD dwMajorVersion;
3953 DWORD dwMinorVersion;
3954 DWORD dwBuildNumber;
3955 DWORD dwPlatformID;
3956} DLLVERSIONINFO;
3957
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003958# ifndef PROTO
3959# include <poppack.h>
3960# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003961
Bram Moolenaar45360022005-07-21 21:08:21 +00003962typedef struct tagTOOLINFOA_NEW
3963{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003964 UINT cbSize;
3965 UINT uFlags;
3966 HWND hwnd;
3967 UINT_PTR uId;
3968 RECT rect;
3969 HINSTANCE hinst;
3970 LPSTR lpszText;
3971 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003972} TOOLINFO_NEW;
3973
3974typedef struct tagNMTTDISPINFO_NEW
3975{
3976 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003977 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003978 char szText[80];
3979 HINSTANCE hinst;
3980 UINT uFlags;
3981 LPARAM lParam;
3982} NMTTDISPINFO_NEW;
3983
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003984typedef struct tagTOOLINFOW_NEW
3985{
3986 UINT cbSize;
3987 UINT uFlags;
3988 HWND hwnd;
3989 UINT_PTR uId;
3990 RECT rect;
3991 HINSTANCE hinst;
3992 LPWSTR lpszText;
3993 LPARAM lParam;
3994 void *lpReserved;
3995} TOOLINFOW_NEW;
3996
3997typedef struct tagNMTTDISPINFOW_NEW
3998{
3999 NMHDR hdr;
4000 LPWSTR lpszText;
4001 WCHAR szText[80];
4002 HINSTANCE hinst;
4003 UINT uFlags;
4004 LPARAM lParam;
4005} NMTTDISPINFOW_NEW;
4006
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004007
Bram Moolenaar45360022005-07-21 21:08:21 +00004008typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004009# ifndef TTM_SETMAXTIPWIDTH
4010# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004013# ifndef TTF_DI_SETITEM
4014# define TTF_DI_SETITEM 0x8000
4015# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004016
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004017# ifndef TTN_GETDISPINFO
4018# define TTN_GETDISPINFO (TTN_FIRST - 0)
4019# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004020
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004021#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004022
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004023#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4024/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4025 * it here if LPNMTTDISPINFO isn't defined.
4026 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4027 * _MSC_VER. */
4028# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4029typedef struct tagNMTTDISPINFOA {
4030 NMHDR hdr;
4031 LPSTR lpszText;
4032 char szText[80];
4033 HINSTANCE hinst;
4034 UINT uFlags;
4035 LPARAM lParam;
4036} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4037# define LPNMTTDISPINFO LPNMTTDISPINFOA
4038
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004039typedef struct tagNMTTDISPINFOW {
4040 NMHDR hdr;
4041 LPWSTR lpszText;
4042 WCHAR szText[80];
4043 HINSTANCE hinst;
4044 UINT uFlags;
4045 LPARAM lParam;
4046} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004047# endif
4048#endif
4049
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004050#ifndef TTN_GETDISPINFOW
4051# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4052#endif
4053
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054/* Local variables: */
4055
4056#ifdef FEAT_MENU
4057static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060/*
4061 * Use the system font for dialogs and tear-off menus. Remove this line to
4062 * use DLG_FONT_NAME.
4063 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004064#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#define VIM_CLASSW L"Vim"
4068
4069/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4070 * thus there should be room for every dialog. For tearoffs it's made bigger
4071 * when needed. */
4072#define DLG_ALLOC_SIZE 16 * 1024
4073
4074/*
4075 * stuff for dialogs, menus, tearoffs etc.
4076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077static PWORD
4078add_dialog_element(
4079 PWORD p,
4080 DWORD lStyle,
4081 WORD x,
4082 WORD y,
4083 WORD w,
4084 WORD h,
4085 WORD Id,
4086 WORD clss,
4087 const char *caption);
4088static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004089static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004090#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093static void get_dialog_font_metrics(void);
4094
4095static int dialog_default_button = -1;
4096
4097/* Intellimouse support */
4098static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4101#ifdef FEAT_TOOLBAR
4102static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004103static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104static int get_toolbar_bitmap(vimmenu_T *menu);
4105#endif
4106
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004107#ifdef FEAT_GUI_TABLINE
4108static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004109static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004110#endif
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifdef FEAT_MBYTE_IME
4113static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4114static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4115#endif
4116#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4117# ifdef NOIME
4118typedef struct tagCOMPOSITIONFORM {
4119 DWORD dwStyle;
4120 POINT ptCurrentPos;
4121 RECT rcArea;
4122} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4123typedef HANDLE HIMC;
4124# endif
4125
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004126static HINSTANCE hLibImm = NULL;
4127static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4128static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4129static HIMC (WINAPI *pImmGetContext)(HWND);
4130static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4131static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4132static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4133static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004134static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4135static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004136static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4137static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004138static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139static void dyn_imm_load(void);
4140#else
4141# define pImmGetCompositionStringA ImmGetCompositionStringA
4142# define pImmGetCompositionStringW ImmGetCompositionStringW
4143# define pImmGetContext ImmGetContext
4144# define pImmAssociateContext ImmAssociateContext
4145# define pImmReleaseContext ImmReleaseContext
4146# define pImmGetOpenStatus ImmGetOpenStatus
4147# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004148# define pImmGetCompositionFontW ImmGetCompositionFontW
4149# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150# define pImmSetCompositionWindow ImmSetCompositionWindow
4151# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004152# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#ifdef FEAT_MENU
4156/*
4157 * Figure out how high the menu bar is at the moment.
4158 */
4159 static int
4160gui_mswin_get_menu_height(
4161 int fix_window) /* If TRUE, resize window if menu height changed */
4162{
4163 static int old_menu_height = -1;
4164
4165 RECT rc1, rc2;
4166 int num;
4167 int menu_height;
4168
4169 if (gui.menu_is_active)
4170 num = GetMenuItemCount(s_menuBar);
4171 else
4172 num = 0;
4173
4174 if (num == 0)
4175 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004176 else if (IsMinimized(s_hwnd))
4177 {
4178 /* The height of the menu cannot be determined while the window is
4179 * minimized. Take the previous height if the menu is changed in that
4180 * state, to avoid that Vim's vertical window size accidentally
4181 * increases due to the unaccounted-for menu height. */
4182 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 else
4185 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004186 /*
4187 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4188 * seem to have been set yet, so menu wraps in default window
4189 * width which is very narrow. Instead just return height of a
4190 * single menu item. Will still be wrong when the menu really
4191 * should wrap over more than one line.
4192 */
4193 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4194 if (gui.starting)
4195 menu_height = rc1.bottom - rc1.top + 1;
4196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004198 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4199 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004204 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004205 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 return menu_height;
4208}
4209#endif /*FEAT_MENU*/
4210
4211
4212/*
4213 * Setup for the Intellimouse
4214 */
4215 static void
4216init_mouse_wheel(void)
4217{
4218
4219#ifndef SPI_GETWHEELSCROLLLINES
4220# define SPI_GETWHEELSCROLLLINES 104
4221#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004222#ifndef SPI_SETWHEELSCROLLLINES
4223# define SPI_SETWHEELSCROLLLINES 105
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4227#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4228#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4229#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 mouse_scroll_lines = 3; /* reasonable default */
4232
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004233 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4234 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4235 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236}
4237
4238
Bram Moolenaarae20f342019-11-05 21:09:23 +01004239/*
4240 * Intellimouse wheel handler.
4241 * Treat a mouse wheel event as if it were a scroll request.
4242 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 static void
4244_OnMouseWheel(
4245 HWND hwnd,
4246 short zDelta)
4247{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 int i;
4249 int size;
4250 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004251 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if (mouse_scroll_lines == 0)
4254 init_mouse_wheel();
4255
Bram Moolenaarae20f342019-11-05 21:09:23 +01004256 wp = gui_mouse_window(FIND_POPUP);
4257
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004258#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004259 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004260 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004261 cmdarg_T cap;
4262 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004263
Bram Moolenaarae20f342019-11-05 21:09:23 +01004264 // Mouse hovers over popup window, scroll it if possible.
4265 mouse_row = wp->w_winrow;
4266 mouse_col = wp->w_wincol;
4267 vim_memset(&cap, 0, sizeof(cap));
4268 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4269 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4270 clear_oparg(&oa);
4271 cap.oap = &oa;
4272 nv_mousescroll(&cap);
4273 update_screen(0);
4274 setcursor();
4275 out_flush();
4276 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004277 }
4278#endif
4279
Bram Moolenaarae20f342019-11-05 21:09:23 +01004280 if (wp == NULL || !p_scf)
4281 wp = curwin;
4282
4283 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4284 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4285 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4286 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4287 else
4288 return;
4289 size = wp->w_height;
4290
Bram Moolenaara338adc2018-01-31 20:51:47 +01004291 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (mouse_scroll_lines > 0
4293 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4294 {
4295 for (i = mouse_scroll_lines; i > 0; --i)
4296 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4297 }
4298 else
4299 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004300 mch_enable_flush();
4301 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302}
4303
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004304#ifdef USE_SYSMENU_FONT
4305/*
4306 * Get Menu Font.
4307 * Return OK or FAIL.
4308 */
4309 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004310gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004312 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004313
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004314 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4315 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004316 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004317 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004318 &nm,
4319 0))
4320 return FAIL;
4321 *lf = nm.lfMenuFont;
4322 return OK;
4323}
4324#endif
4325
4326
4327#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4328/*
4329 * Set the GUI tabline font to the system menu font
4330 */
4331 static void
4332set_tabline_font(void)
4333{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004334 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004335 HFONT font;
4336 HWND hwnd;
4337 HDC hdc;
4338 HFONT hfntOld;
4339 TEXTMETRIC tm;
4340
4341 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4342 return;
4343
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004344 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004345
4346 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4347
4348 /*
4349 * Compute the height of the font used for the tab text
4350 */
4351 hwnd = GetDesktopWindow();
4352 hdc = GetWindowDC(hwnd);
4353 hfntOld = SelectFont(hdc, font);
4354
4355 GetTextMetrics(hdc, &tm);
4356
4357 SelectFont(hdc, hfntOld);
4358 ReleaseDC(hwnd, hdc);
4359
4360 /*
4361 * The space used by the tab border and the space between the tab label
4362 * and the tab border is included as 7.
4363 */
4364 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4365}
4366#endif
4367
Bram Moolenaar520470a2005-06-16 21:59:56 +00004368/*
4369 * Invoked when a setting was changed.
4370 */
4371 static LRESULT CALLBACK
4372_OnSettingChange(UINT n)
4373{
4374 if (n == SPI_SETWHEELSCROLLLINES)
4375 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4376 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004377#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4378 if (n == SPI_SETNONCLIENTMETRICS)
4379 set_tabline_font();
4380#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004381 return 0;
4382}
4383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#ifdef FEAT_NETBEANS_INTG
4385 static void
4386_OnWindowPosChanged(
4387 HWND hwnd,
4388 const LPWINDOWPOS lpwpos)
4389{
4390 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004391 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4394 || lpwpos->cx != cx || lpwpos->cy != cy))
4395 {
4396 x = lpwpos->x;
4397 y = lpwpos->y;
4398 cx = lpwpos->cx;
4399 cy = lpwpos->cy;
4400 netbeans_frame_moved(x, y);
4401 }
4402 /* Allow to send WM_SIZE and WM_MOVE */
4403 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4404}
4405#endif
4406
4407 static int
4408_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 UINT fwSide,
4410 LPRECT lprc)
4411{
4412 int w, h;
4413 int valid_w, valid_h;
4414 int w_offset, h_offset;
4415
4416 w = lprc->right - lprc->left;
4417 h = lprc->bottom - lprc->top;
4418 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4419 w_offset = w - valid_w;
4420 h_offset = h - valid_h;
4421
4422 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4423 || fwSide == WMSZ_BOTTOMLEFT)
4424 lprc->left += w_offset;
4425 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4426 || fwSide == WMSZ_BOTTOMRIGHT)
4427 lprc->right -= w_offset;
4428
4429 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4430 || fwSide == WMSZ_TOPRIGHT)
4431 lprc->top += h_offset;
4432 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4433 || fwSide == WMSZ_BOTTOMRIGHT)
4434 lprc->bottom -= h_offset;
4435 return TRUE;
4436}
4437
4438
4439
4440 static LRESULT CALLBACK
4441_WndProc(
4442 HWND hwnd,
4443 UINT uMsg,
4444 WPARAM wParam,
4445 LPARAM lParam)
4446{
4447 /*
4448 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4449 hwnd, uMsg, wParam, lParam);
4450 */
4451
4452 HandleMouseHide(uMsg, lParam);
4453
4454 s_uMsg = uMsg;
4455 s_wParam = wParam;
4456 s_lParam = lParam;
4457
4458 switch (uMsg)
4459 {
4460 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4461 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4462 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4463 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4464 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4465 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4466 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4467 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4468 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4469#ifdef FEAT_MENU
4470 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4471#endif
4472 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4473 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4474 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4475 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4476 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4477 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4478 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4479 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4480 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4481#ifdef FEAT_NETBEANS_INTG
4482 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4483#endif
4484
Bram Moolenaarafa24992006-03-27 20:58:26 +00004485#ifdef FEAT_GUI_TABLINE
4486 case WM_RBUTTONUP:
4487 {
4488 if (gui_mch_showing_tabline())
4489 {
4490 POINT pt;
4491 RECT rect;
4492
4493 /*
4494 * If the cursor is on the tabline, display the tab menu
4495 */
4496 GetCursorPos((LPPOINT)&pt);
4497 GetWindowRect(s_textArea, &rect);
4498 if (pt.y < rect.top)
4499 {
4500 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004501 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004502 }
4503 }
4504 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4505 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004506 case WM_LBUTTONDBLCLK:
4507 {
4508 /*
4509 * If the user double clicked the tabline, create a new tab
4510 */
4511 if (gui_mch_showing_tabline())
4512 {
4513 POINT pt;
4514 RECT rect;
4515
4516 GetCursorPos((LPPOINT)&pt);
4517 GetWindowRect(s_textArea, &rect);
4518 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004519 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004520 }
4521 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4522 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004523#endif
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 case WM_QUERYENDSESSION: /* System wants to go down. */
4526 gui_shell_closed(); /* Will exit when no changed buffers. */
4527 return FALSE; /* Do NOT allow system to go down. */
4528
4529 case WM_ENDSESSION:
4530 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004533 return 0L;
4534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 break;
4536
4537 case WM_CHAR:
4538 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4539 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004540 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 return 0L;
4542
4543 case WM_SYSCHAR:
4544 /*
4545 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4546 * shortcut key, handle like a typed ALT key, otherwise call Windows
4547 * ALT key handling.
4548 */
4549#ifdef FEAT_MENU
4550 if ( !gui.menu_is_active
4551 || p_wak[0] == 'n'
4552 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4553 )
4554#endif
4555 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004556 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 return 0L;
4558 }
4559#ifdef FEAT_MENU
4560 else
4561 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4562#endif
4563
4564 case WM_SYSKEYUP:
4565#ifdef FEAT_MENU
4566 /* This used to be done only when menu is active: ALT key is used for
4567 * that. But that caused problems when menu is disabled and using
4568 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4569 * are received, mouse pointer remains hidden. */
4570 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4571#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004572 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573#endif
4574
4575 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004576 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
4578 case WM_MOUSEWHEEL:
4579 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004580 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581
Bram Moolenaar520470a2005-06-16 21:59:56 +00004582 /* Notification for change in SystemParametersInfo() */
4583 case WM_SETTINGCHANGE:
4584 return _OnSettingChange((UINT)wParam);
4585
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004586#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 case WM_NOTIFY:
4588 switch (((LPNMHDR) lParam)->code)
4589 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004590 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004591 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004593 LPNMHDR hdr = (LPNMHDR)lParam;
4594 char_u *str = NULL;
4595 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004596
Bram Moolenaard23a8232018-02-10 18:45:26 +01004597 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004598
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004599# ifdef FEAT_GUI_TABLINE
4600 if (gui_mch_showing_tabline()
4601 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004602 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004603 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004604 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004605 * Mouse is over the GUI tabline. Display the
4606 * tooltip for the tab under the cursor
4607 *
4608 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004609 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004610 GetCursorPos(&pt);
4611 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004612 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004613 TCHITTESTINFO htinfo;
4614 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004615
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004616 /*
4617 * Get the tab under the cursor
4618 */
4619 htinfo.pt.x = pt.x;
4620 htinfo.pt.y = pt.y;
4621 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4622 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004623 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004624 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004625
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004626 tp = find_tabpage(idx + 1);
4627 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004628 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004629 get_tabline_label(tp, TRUE);
4630 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004631 }
4632 }
4633 }
4634 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004635# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004636# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004637# ifdef FEAT_GUI_TABLINE
4638 else
4639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004641 UINT idButton;
4642 vimmenu_T *pMenu;
4643
4644 idButton = (UINT) hdr->idFrom;
4645 pMenu = gui_mswin_find_menu(root_menu, idButton);
4646 if (pMenu)
4647 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004649# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 if (str != NULL)
4651 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004652 if (hdr->code == TTN_GETDISPINFOW)
4653 {
4654 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4655
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004656 /* Set the maximum width, this also enables using
4657 * \n for line break. */
4658 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4659 0, 500);
4660
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004661 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004662 lpdi->lpszText = tt_text;
4663 /* can't show tooltip if failed */
4664 }
4665 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004666 {
4667 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4668
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004669 /* Set the maximum width, this also enables using
4670 * \n for line break. */
4671 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4672 0, 500);
4673
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004674 if (STRLEN(str) < sizeof(lpdi->szText)
4675 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004676 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004677 sizeof(lpdi->szText) - 1);
4678 else
4679 lpdi->lpszText = tt_text;
4680 }
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004684# ifdef FEAT_GUI_TABLINE
4685 case TCN_SELCHANGE:
4686 if (gui_mch_showing_tabline()
4687 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004688 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004689 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004690 return 0L;
4691 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004692 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004693
4694 case NM_RCLICK:
4695 if (gui_mch_showing_tabline()
4696 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004697 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004698 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004699 return 0L;
4700 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004701 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004702# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004704# ifdef FEAT_GUI_TABLINE
4705 if (gui_mch_showing_tabline()
4706 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4707 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 break;
4710 }
4711 break;
4712#endif
4713#if defined(MENUHINTS) && defined(FEAT_MENU)
4714 case WM_MENUSELECT:
4715 if (((UINT) HIWORD(wParam)
4716 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4717 == MF_HILITE
4718 && (State & CMDLINE) == 0)
4719 {
4720 UINT idButton;
4721 vimmenu_T *pMenu;
4722 static int did_menu_tip = FALSE;
4723
4724 if (did_menu_tip)
4725 {
4726 msg_clr_cmdline();
4727 setcursor();
4728 out_flush();
4729 did_menu_tip = FALSE;
4730 }
4731
4732 idButton = (UINT)LOWORD(wParam);
4733 pMenu = gui_mswin_find_menu(root_menu, idButton);
4734 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4735 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4736 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004737 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004738 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004739 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 setcursor();
4741 out_flush();
4742 did_menu_tip = TRUE;
4743 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004744 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 }
4746 break;
4747#endif
4748 case WM_NCHITTEST:
4749 {
4750 LRESULT result;
4751 int x, y;
4752 int xPos = GET_X_LPARAM(lParam);
4753
4754 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4755 if (result == HTCLIENT)
4756 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004757#ifdef FEAT_GUI_TABLINE
4758 if (gui_mch_showing_tabline())
4759 {
4760 int yPos = GET_Y_LPARAM(lParam);
4761 RECT rct;
4762
4763 /* If the cursor is on the GUI tabline, don't process this
4764 * event */
4765 GetWindowRect(s_textArea, &rct);
4766 if (yPos < rct.top)
4767 return result;
4768 }
4769#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004770 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 xPos -= x;
4772
4773 if (xPos < 48) /* <VN> TODO should use system metric? */
4774 return HTBOTTOMLEFT;
4775 else
4776 return HTBOTTOMRIGHT;
4777 }
4778 else
4779 return result;
4780 }
4781 /* break; notreached */
4782
4783#ifdef FEAT_MBYTE_IME
4784 case WM_IME_NOTIFY:
4785 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4786 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004787 return 1L;
4788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 case WM_IME_COMPOSITION:
4790 if (!_OnImeComposition(hwnd, wParam, lParam))
4791 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004792 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#endif
4794
4795 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004797 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799#endif
4800 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4801 }
4802
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004803 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804}
4805
4806/*
4807 * End of call-back routines
4808 */
4809
4810/* parent window, if specified with -P */
4811HWND vim_parent_hwnd = NULL;
4812
4813 static BOOL CALLBACK
4814FindWindowTitle(HWND hwnd, LPARAM lParam)
4815{
4816 char buf[2048];
4817 char *title = (char *)lParam;
4818
4819 if (GetWindowText(hwnd, buf, sizeof(buf)))
4820 {
4821 if (strstr(buf, title) != NULL)
4822 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004823 /* Found it. Store the window ref. and quit searching if MDI
4824 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004826 if (vim_parent_hwnd != NULL)
4827 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 }
4830 return TRUE; /* continue searching */
4831}
4832
4833/*
4834 * Invoked for '-P "title"' argument: search for parent application to open
4835 * our window in.
4836 */
4837 void
4838gui_mch_set_parent(char *title)
4839{
4840 EnumWindows(FindWindowTitle, (LPARAM)title);
4841 if (vim_parent_hwnd == NULL)
4842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004843 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 mch_exit(2);
4845 }
4846}
4847
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 static void
4850ole_error(char *arg)
4851{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004852 char buf[IOSIZE];
4853
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004854# ifdef VIMDLL
4855 gui.in_use = mch_is_gui_executable();
4856# endif
4857
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004859 vim_snprintf(buf, IOSIZE,
4860 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4861 arg);
4862 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004866#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4867 static char *
4868gvim_error(void)
4869{
4870 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4871
4872 if (starting)
4873 {
4874 mch_errmsg(msg);
4875 mch_errmsg("\n");
4876 mch_exit(2);
4877 }
4878 return msg;
4879}
4880
4881 char *
4882gui_mch_do_spawn(char_u *arg)
4883{
4884 int len;
4885# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4886 char_u *session = NULL;
4887 LPWSTR tofree1 = NULL;
4888# endif
4889 WCHAR name[MAX_PATH];
4890 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4891 STARTUPINFOW si = {sizeof(si)};
4892 PROCESS_INFORMATION pi;
4893
4894 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4895 goto error;
4896 p = wcsrchr(name, L'\\');
4897 if (p == NULL)
4898 goto error;
4899 // Replace the executable name from vim(d).exe to gvim(d).exe.
4900# ifdef DEBUG
4901 wcscpy(p + 1, L"gvimd.exe");
4902# else
4903 wcscpy(p + 1, L"gvim.exe");
4904# endif
4905
4906# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4907 if (starting)
4908# endif
4909 {
4910 // Pass the command line to the new process.
4911 p = GetCommandLineW();
4912 // Skip 1st argument.
4913 while (*p && *p != L' ' && *p != L'\t')
4914 {
4915 if (*p == L'"')
4916 {
4917 while (*p && *p != L'"')
4918 ++p;
4919 if (*p)
4920 ++p;
4921 }
4922 else
4923 ++p;
4924 }
4925 cmd = p;
4926 }
4927# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4928 else
4929 {
4930 // Create a session file and pass it to the new process.
4931 LPWSTR wsession;
4932 char_u *savebg;
4933 int ret;
4934
4935 session = vim_tempname('s', FALSE);
4936 if (session == NULL)
4937 goto error;
4938 savebg = p_bg;
4939 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4940 ret = write_session_file(session);
4941 vim_free(p_bg);
4942 p_bg = savebg;
4943 if (!ret)
4944 goto error;
4945 wsession = enc_to_utf16(session, NULL);
4946 if (wsession == NULL)
4947 goto error;
4948 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004949 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004950 if (cmd == NULL)
4951 {
4952 vim_free(wsession);
4953 goto error;
4954 }
4955 tofree1 = cmd;
4956 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4957 wsession, wsession);
4958 vim_free(wsession);
4959 }
4960# endif
4961
4962 // Check additional arguments to the `:gui` command.
4963 if (arg != NULL)
4964 {
4965 warg = enc_to_utf16(arg, NULL);
4966 if (warg == NULL)
4967 goto error;
4968 tofree2 = warg;
4969 }
4970 else
4971 warg = L"";
4972
4973 // Set up the new command line.
4974 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004975 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004976 if (newcmd == NULL)
4977 goto error;
4978 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4979
4980 // Spawn a new GUI process.
4981 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4982 NULL, NULL, &si, &pi))
4983 goto error;
4984 CloseHandle(pi.hProcess);
4985 CloseHandle(pi.hThread);
4986 mch_exit(0);
4987
4988error:
4989# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4990 if (session)
4991 mch_remove(session);
4992 vim_free(session);
4993 vim_free(tofree1);
4994# endif
4995 vim_free(newcmd);
4996 vim_free(tofree2);
4997 return gvim_error();
4998}
4999#endif
5000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001/*
5002 * Parse the GUI related command-line arguments. Any arguments used are
5003 * deleted from argv, and *argc is decremented accordingly. This is called
5004 * when vim is started, whether or not the GUI has been started.
5005 */
5006 void
5007gui_mch_prepare(int *argc, char **argv)
5008{
5009 int silent = FALSE;
5010 int idx;
5011
5012 /* Check for special OLE command line parameters */
5013 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5014 {
5015 /* Check for a "-silent" argument first. */
5016 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5017 && (argv[2][0] == '-' || argv[2][0] == '/'))
5018 {
5019 silent = TRUE;
5020 idx = 2;
5021 }
5022 else
5023 idx = 1;
5024
5025 /* Register Vim as an OLE Automation server */
5026 if (STRICMP(argv[idx] + 1, "register") == 0)
5027 {
5028#ifdef FEAT_OLE
5029 RegisterMe(silent);
5030 mch_exit(0);
5031#else
5032 if (!silent)
5033 ole_error("register");
5034 mch_exit(2);
5035#endif
5036 }
5037
5038 /* Unregister Vim as an OLE Automation server */
5039 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5040 {
5041#ifdef FEAT_OLE
5042 UnregisterMe(!silent);
5043 mch_exit(0);
5044#else
5045 if (!silent)
5046 ole_error("unregister");
5047 mch_exit(2);
5048#endif
5049 }
5050
5051 /* Ignore an -embedding argument. It is only relevant if the
5052 * application wants to treat the case when it is started manually
5053 * differently from the case where it is started via automation (and
5054 * we don't).
5055 */
5056 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5057 {
5058#ifdef FEAT_OLE
5059 *argc = 1;
5060#else
5061 ole_error("embedding");
5062 mch_exit(2);
5063#endif
5064 }
5065 }
5066
5067#ifdef FEAT_OLE
5068 {
5069 int bDoRestart = FALSE;
5070
5071 InitOLE(&bDoRestart);
5072 /* automatically exit after registering */
5073 if (bDoRestart)
5074 mch_exit(0);
5075 }
5076#endif
5077
5078#ifdef FEAT_NETBEANS_INTG
5079 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005080 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 int arg;
5082
5083 for (arg = 1; arg < *argc; arg++)
5084 if (strncmp("-nb", argv[arg], 3) == 0)
5085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 netbeansArg = argv[arg];
5087 mch_memmove(&argv[arg], &argv[arg + 1],
5088 (--*argc - arg) * sizeof(char *));
5089 argv[*argc] = NULL;
5090 break; /* enough? */
5091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094}
5095
5096/*
5097 * Initialise the GUI. Create all the windows, set up all the call-backs
5098 * etc.
5099 */
5100 int
5101gui_mch_init(void)
5102{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005104 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106#ifdef GLOBAL_IME
5107 ATOM atom;
5108#endif
5109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 /* Return here if the window was already opened (happens when
5111 * gui_mch_dialog() is called early). */
5112 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005113 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 /*
5116 * Load the tearoff bitmap
5117 */
5118#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005119 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#endif
5121
5122 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5123 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5124#ifdef FEAT_MENU
5125 gui.menu_height = 0; /* Windows takes care of this */
5126#endif
5127 gui.border_width = 0;
5128
5129 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 /* First try using the wide version, so that we can use any title.
5132 * Otherwise only characters in the active codepage will work. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005133 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005135 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 wndclassw.lpfnWndProc = _WndProc;
5137 wndclassw.cbClsExtra = 0;
5138 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005139 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5141 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5142 wndclassw.hbrBackground = s_brush;
5143 wndclassw.lpszMenuName = NULL;
5144 wndclassw.lpszClassName = szVimWndClassW;
5145
5146 if ((
5147#ifdef GLOBAL_IME
5148 atom =
5149#endif
5150 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (vim_parent_hwnd != NULL)
5155 {
5156#ifdef HAVE_TRY_EXCEPT
5157 __try
5158 {
5159#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005160 // Open inside the specified parent window.
5161 // TODO: last argument should point to a CLIENTCREATESTRUCT
5162 // structure.
5163 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005165 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005166 WS_OVERLAPPEDWINDOW | WS_CHILD
5167 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5169 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005170 100, // Any value will do
5171 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005173 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#ifdef HAVE_TRY_EXCEPT
5175 }
5176 __except(EXCEPTION_EXECUTE_HANDLER)
5177 {
5178 /* NOP */
5179 }
5180#endif
5181 if (s_hwnd == NULL)
5182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005183 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 mch_exit(2);
5185 }
5186 }
5187 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005188 {
5189 /* If the provided windowid is not valid reset it to zero, so that it
5190 * is ignored and we open our own window. */
5191 if (IsWindow((HWND)win_socket_id) <= 0)
5192 win_socket_id = 0;
5193
5194 /* Create a window. If win_socket_id is not zero without border and
5195 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005196 s_hwnd = CreateWindowW(
5197 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005198 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5199 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005200 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5201 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5202 100, /* Any value will do */
5203 100, /* Any value will do */
5204 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005205 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005206 if (s_hwnd != NULL && win_socket_id != 0)
5207 {
5208 SetParent(s_hwnd, (HWND)win_socket_id);
5209 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5210 }
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 if (s_hwnd == NULL)
5214 return FAIL;
5215
5216#ifdef GLOBAL_IME
5217 global_ime_init(atom, s_hwnd);
5218#endif
5219#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5220 dyn_imm_load();
5221#endif
5222
5223 /* Create the text area window */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005224 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005225 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005226 wndclassw.style = CS_OWNDC;
5227 wndclassw.lpfnWndProc = _TextAreaWndProc;
5228 wndclassw.cbClsExtra = 0;
5229 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005230 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005231 wndclassw.hIcon = NULL;
5232 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5233 wndclassw.hbrBackground = NULL;
5234 wndclassw.lpszMenuName = NULL;
5235 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005236
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005237 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 return FAIL;
5239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005241 s_textArea = CreateWindowExW(
5242 0,
5243 szTextAreaClassW, L"Vim text area",
5244 WS_CHILD | WS_VISIBLE, 0, 0,
5245 100, // Any value will do for now
5246 100, // Any value will do for now
5247 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005248 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 if (s_textArea == NULL)
5251 return FAIL;
5252
Bram Moolenaar20321902016-02-17 12:30:17 +01005253#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005254 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5255 {
5256 HANDLE hIcon = NULL;
5257
5258 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005259 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005260 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005261#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#ifdef FEAT_MENU
5264 s_menuBar = CreateMenu();
5265#endif
5266 s_hdc = GetDC(s_textArea);
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 /* Do we need to bother with this? */
5271 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5272
5273 /* Get background/foreground colors from the system */
5274 gui_mch_def_colors();
5275
5276 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5277 * file) */
5278 set_normal_colors();
5279
5280 /*
5281 * Check that none of the colors are the same as the background color.
5282 * Then store the current values as the defaults.
5283 */
5284 gui_check_colors();
5285 gui.def_norm_pixel = gui.norm_pixel;
5286 gui.def_back_pixel = gui.back_pixel;
5287
5288 /* Get the colors for the highlight groups (gui_check_colors() might have
5289 * changed them) */
5290 highlight_gui_started();
5291
5292 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005293 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005295 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296
5297 /*
5298 * Set up for Intellimouse processing
5299 */
5300 init_mouse_wheel();
5301
5302 /*
5303 * compute a couple of metrics used for the dialogs
5304 */
5305 get_dialog_font_metrics();
5306#ifdef FEAT_TOOLBAR
5307 /*
5308 * Create the toolbar
5309 */
5310 initialise_toolbar();
5311#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005312#ifdef FEAT_GUI_TABLINE
5313 /*
5314 * Create the tabline
5315 */
5316 initialise_tabline();
5317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318#ifdef MSWIN_FIND_REPLACE
5319 /*
5320 * Initialise the dialog box stuff
5321 */
5322 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5323
5324 /* Initialise the struct */
5325 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005326 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005328 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5330 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5331 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005334#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005335# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5336/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5337# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005338# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005339# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005340# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005341 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005342 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005343#endif
5344
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005345#ifdef FEAT_RENDER_OPTIONS
5346 if (p_rop)
5347 (void)gui_mch_set_rendering_options(p_rop);
5348#endif
5349
Bram Moolenaar748bf032005-02-02 23:04:36 +00005350theend:
5351 /* Display any pending error messages */
5352 display_errors();
5353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 return OK;
5355}
5356
5357/*
5358 * Get the size of the screen, taking position on multiple monitors into
5359 * account (if supported).
5360 */
5361 static void
5362get_work_area(RECT *spi_rect)
5363{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005364 HMONITOR mon;
5365 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005367 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005368 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005369 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005371 moninfo.cbSize = sizeof(MONITORINFO);
5372 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005374 *spi_rect = moninfo.rcWork;
5375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 }
5377 }
5378 /* this is the old method... */
5379 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5380}
5381
5382/*
5383 * Set the size of the window to the given width and height in pixels.
5384 */
5385 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005386gui_mch_set_shellsize(
5387 int width,
5388 int height,
5389 int min_width UNUSED,
5390 int min_height UNUSED,
5391 int base_width UNUSED,
5392 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005393 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 RECT workarea_rect;
5396 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 WINDOWPLACEMENT wndpl;
5398
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005399 /* Try to keep window completely on screen. */
5400 /* Get position of the screen work area. This is the part that is not
5401 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 get_work_area(&workarea_rect);
5403
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005404 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005405 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 wndpl.length = sizeof(WINDOWPLACEMENT);
5407 GetWindowPlacement(s_hwnd, &wndpl);
5408
5409 /* Resizing a maximized window looks very strange, unzoom it first.
5410 * But don't do it when still starting up, it may have been requested in
5411 * the shortcut. */
5412 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5413 {
5414 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5415 /* Need to get the settings of the normal window. */
5416 GetWindowPlacement(s_hwnd, &wndpl);
5417 }
5418
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005420 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005421 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005422 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005423 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 + GetSystemMetrics(SM_CYCAPTION)
5425#ifdef FEAT_MENU
5426 + gui_mswin_get_menu_height(FALSE)
5427#endif
5428 ;
5429
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005430 /* The following should take care of keeping Vim on the same monitor, no
5431 * matter if the secondary monitor is left or right of the primary
5432 * monitor. */
5433 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5434 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005435
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005436 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005437 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005438 && wndpl.rcNormalPosition.right > workarea_rect.right)
5439 OffsetRect(&wndpl.rcNormalPosition,
5440 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005442 if ((direction & RESIZE_HOR)
5443 && wndpl.rcNormalPosition.left < workarea_rect.left)
5444 OffsetRect(&wndpl.rcNormalPosition,
5445 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaarafa24992006-03-27 20:58:26 +00005447 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005448 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5449 OffsetRect(&wndpl.rcNormalPosition,
5450 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005452 if ((direction & RESIZE_VERT)
5453 && wndpl.rcNormalPosition.top < workarea_rect.top)
5454 OffsetRect(&wndpl.rcNormalPosition,
5455 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 /* set window position - we should use SetWindowPlacement rather than
5458 * SetWindowPos as the MSDN docs say the coord systems returned by
5459 * these two are not compatible. */
5460 SetWindowPlacement(s_hwnd, &wndpl);
5461
5462 SetActiveWindow(s_hwnd);
5463 SetFocus(s_hwnd);
5464
5465#ifdef FEAT_MENU
5466 /* Menu may wrap differently now */
5467 gui_mswin_get_menu_height(!gui.starting);
5468#endif
5469}
5470
5471
5472 void
5473gui_mch_set_scrollbar_thumb(
5474 scrollbar_T *sb,
5475 long val,
5476 long size,
5477 long max)
5478{
5479 SCROLLINFO info;
5480
5481 sb->scroll_shift = 0;
5482 while (max > 32767)
5483 {
5484 max = (max + 1) >> 1;
5485 val >>= 1;
5486 size >>= 1;
5487 ++sb->scroll_shift;
5488 }
5489
5490 if (sb->scroll_shift > 0)
5491 ++size;
5492
5493 info.cbSize = sizeof(info);
5494 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5495 info.nPos = val;
5496 info.nMin = 0;
5497 info.nMax = max;
5498 info.nPage = size;
5499 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5500}
5501
5502
5503/*
5504 * Set the current text font.
5505 */
5506 void
5507gui_mch_set_font(GuiFont font)
5508{
5509 gui.currFont = font;
5510}
5511
5512
5513/*
5514 * Set the current text foreground color.
5515 */
5516 void
5517gui_mch_set_fg_color(guicolor_T color)
5518{
5519 gui.currFgColor = color;
5520}
5521
5522/*
5523 * Set the current text background color.
5524 */
5525 void
5526gui_mch_set_bg_color(guicolor_T color)
5527{
5528 gui.currBgColor = color;
5529}
5530
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005531/*
5532 * Set the current text special color.
5533 */
5534 void
5535gui_mch_set_sp_color(guicolor_T color)
5536{
5537 gui.currSpColor = color;
5538}
5539
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005540#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541/*
5542 * Multi-byte handling, originally by Sung-Hoon Baek.
5543 * First static functions (no prototypes generated).
5544 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005545# ifdef _MSC_VER
Bram Moolenaareae1b912019-05-09 15:12:55 +02005546# include <ime.h> /* Apparently not needed for Cygwin or MinGW. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005547# endif
5548# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 * handle WM_IME_NOTIFY message
5552 */
5553 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005554_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
5556 LRESULT lResult = 0;
5557 HIMC hImc;
5558
5559 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5560 return lResult;
5561 switch (dwCommand)
5562 {
5563 case IMN_SETOPENSTATUS:
5564 if (pImmGetOpenStatus(hImc))
5565 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005566 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 im_set_position(gui.row, gui.col);
5568
5569 /* Disable langmap */
5570 State &= ~LANGMAP;
5571 if (State & INSERT)
5572 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005573# if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 /* Unshown 'keymap' in status lines */
5575 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5576 {
5577 /* Save cursor position */
5578 int old_row = gui.row;
5579 int old_col = gui.col;
5580
5581 // This must be called here before
5582 // status_redraw_curbuf(), otherwise the mode
5583 // message may appear in the wrong position.
5584 showmode();
5585 status_redraw_curbuf();
5586 update_screen(0);
5587 /* Restore cursor position */
5588 gui.row = old_row;
5589 gui.col = old_col;
5590 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593 }
5594 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005595 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 lResult = 0;
5597 break;
5598 }
5599 pImmReleaseContext(hWnd, hImc);
5600 return lResult;
5601}
5602
5603 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005604_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605{
5606 char_u *ret;
5607 int len;
5608
5609 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5610 return 0;
5611
5612 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5613 if (ret != NULL)
5614 {
5615 add_to_input_buf_csi(ret, len);
5616 vim_free(ret);
5617 return 1;
5618 }
5619 return 0;
5620}
5621
5622/*
5623 * get the current composition string, in UCS-2; *lenp is the number of
5624 * *lenp is the number of Unicode characters.
5625 */
5626 static short_u *
5627GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5628{
5629 LONG ret;
5630 LPWSTR wbuf = NULL;
5631 char_u *buf;
5632
5633 if (!pImmGetContext)
5634 return NULL; /* no imm32.dll */
5635
5636 /* Try Unicode; this'll always work on NT regardless of codepage. */
5637 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5638 if (ret == 0)
5639 return NULL; /* empty */
5640
5641 if (ret > 0)
5642 {
5643 /* Allocate the requested buffer plus space for the NUL character. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005644 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 if (wbuf != NULL)
5646 {
5647 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5648 *lenp = ret / sizeof(WCHAR);
5649 }
5650 return (short_u *)wbuf;
5651 }
5652
5653 /* ret < 0; we got an error, so try the ANSI version. This'll work
5654 * on 9x/ME, but only if the codepage happens to be set to whatever
5655 * we're inputting. */
5656 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5657 if (ret <= 0)
5658 return NULL; /* empty or error */
5659
5660 buf = alloc(ret);
5661 if (buf == NULL)
5662 return NULL;
5663 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5664
5665 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005666 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 vim_free(buf);
5668
5669 return (short_u *)wbuf;
5670}
5671
5672/*
5673 * void GetResultStr()
5674 *
5675 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5676 * get complete composition string
5677 */
5678 static char_u *
5679GetResultStr(HWND hwnd, int GCS, int *lenp)
5680{
5681 HIMC hIMC; /* Input context handle. */
5682 short_u *buf = NULL;
5683 char_u *convbuf = NULL;
5684
5685 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5686 return NULL;
5687
5688 /* Reads in the composition string. */
5689 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5690 if (buf == NULL)
5691 return NULL;
5692
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005693 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 pImmReleaseContext(hwnd, hIMC);
5695 vim_free(buf);
5696 return convbuf;
5697}
5698#endif
5699
5700/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005701#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703/*
5704 * set font to IM.
5705 */
5706 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005707im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 HIMC hImc;
5710
5711 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5712 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005713 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 pImmReleaseContext(s_hwnd, hImc);
5715 }
5716}
5717
5718/*
5719 * Notify cursor position to IM.
5720 */
5721 void
5722im_set_position(int row, int col)
5723{
5724 HIMC hImc;
5725
5726 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5727 {
5728 COMPOSITIONFORM cfs;
5729
5730 cfs.dwStyle = CFS_POINT;
5731 cfs.ptCurrentPos.x = FILL_X(col);
5732 cfs.ptCurrentPos.y = FILL_Y(row);
5733 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5734 pImmSetCompositionWindow(hImc, &cfs);
5735
5736 pImmReleaseContext(s_hwnd, hImc);
5737 }
5738}
5739
5740/*
5741 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5742 */
5743 void
5744im_set_active(int active)
5745{
5746 HIMC hImc;
5747 static HIMC hImcOld = (HIMC)0;
5748
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005749# ifdef VIMDLL
5750 if (!gui.in_use && !gui.starting)
5751 {
5752 mbyte_im_set_active(active);
5753 return;
5754 }
5755# endif
5756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5758 {
5759 if (p_imdisable)
5760 {
5761 if (hImcOld == (HIMC)0)
5762 {
5763 hImcOld = pImmGetContext(s_hwnd);
5764 if (hImcOld)
5765 pImmAssociateContext(s_hwnd, (HIMC)0);
5766 }
5767 active = FALSE;
5768 }
5769 else if (hImcOld != (HIMC)0)
5770 {
5771 pImmAssociateContext(s_hwnd, hImcOld);
5772 hImcOld = (HIMC)0;
5773 }
5774
5775 hImc = pImmGetContext(s_hwnd);
5776 if (hImc)
5777 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005778 /*
5779 * for Korean ime
5780 */
5781 HKL hKL = GetKeyboardLayout(0);
5782
5783 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5784 {
5785 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5786 static BOOL bSaved = FALSE;
5787
5788 if (active)
5789 {
5790 /* if we have a saved conversion status, restore it */
5791 if (bSaved)
5792 pImmSetConversionStatus(hImc, dwConversionSaved,
5793 dwSentenceSaved);
5794 bSaved = FALSE;
5795 }
5796 else
5797 {
5798 /* save conversion status and disable korean */
5799 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5800 &dwSentenceSaved))
5801 {
5802 bSaved = TRUE;
5803 pImmSetConversionStatus(hImc,
5804 dwConversionSaved & ~(IME_CMODE_NATIVE
5805 | IME_CMODE_FULLSHAPE),
5806 dwSentenceSaved);
5807 }
5808 }
5809 }
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 pImmSetOpenStatus(hImc, active);
5812 pImmReleaseContext(s_hwnd, hImc);
5813 }
5814 }
5815}
5816
5817/*
5818 * Get IM status. When IM is on, return not 0. Else return 0.
5819 */
5820 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005821im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
5823 int status = 0;
5824 HIMC hImc;
5825
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005826# ifdef VIMDLL
5827 if (!gui.in_use && !gui.starting)
5828 return mbyte_im_get_status();
5829# endif
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5832 {
5833 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5834 pImmReleaseContext(s_hwnd, hImc);
5835 }
5836 return status;
5837}
5838
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005839#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005841#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842/* Win32 with GLOBAL IME */
5843
5844/*
5845 * Notify cursor position to IM.
5846 */
5847 void
5848im_set_position(int row, int col)
5849{
5850 /* Win32 with GLOBAL IME */
5851 POINT p;
5852
5853 p.x = FILL_X(col);
5854 p.y = FILL_Y(row);
5855 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5856 global_ime_set_position(&p);
5857}
5858
5859/*
5860 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5861 */
5862 void
5863im_set_active(int active)
5864{
5865 global_ime_set_status(active);
5866}
5867
5868/*
5869 * Get IM status. When IM is on, return not 0. Else return 0.
5870 */
5871 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005872im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874 return global_ime_get_status();
5875}
5876#endif
5877
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005878/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005879 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005880 */
5881 static void
5882latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5883{
5884 int c;
5885
Bram Moolenaarca003e12006-03-17 23:19:38 +00005886 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005887 {
5888 c = *text++;
5889 switch (c)
5890 {
5891 case 0xa4: c = 0x20ac; break; /* euro */
5892 case 0xa6: c = 0x0160; break; /* S hat */
5893 case 0xa8: c = 0x0161; break; /* S -hat */
5894 case 0xb4: c = 0x017d; break; /* Z hat */
5895 case 0xb8: c = 0x017e; break; /* Z -hat */
5896 case 0xbc: c = 0x0152; break; /* OE */
5897 case 0xbd: c = 0x0153; break; /* oe */
5898 case 0xbe: c = 0x0178; break; /* Y */
5899 }
5900 *unicodebuf++ = c;
5901 }
5902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904#ifdef FEAT_RIGHTLEFT
5905/*
5906 * What is this for? In the case where you are using Win98 or Win2K or later,
5907 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5908 * reverses the string sent to the TextOut... family. This sucks, because we
5909 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5910 * way to tell Windblows not to do this!
5911 *
5912 * The short of it is that this 'RevOut' only gets called if you are running
5913 * one of the new, "improved" MS OSes, and only if you are running in
5914 * 'rightleft' mode. It makes display take *slightly* longer, but not
5915 * noticeably so.
5916 */
5917 static void
5918RevOut( HDC s_hdc,
5919 int col,
5920 int row,
5921 UINT foptions,
5922 CONST RECT *pcliprect,
5923 LPCTSTR text,
5924 UINT len,
5925 CONST INT *padding)
5926{
5927 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005929 for (ix = 0; ix < (int)len; ++ix)
5930 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5931 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932}
5933#endif
5934
Bram Moolenaar92467d32017-12-05 13:22:16 +01005935 static void
5936draw_line(
5937 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005938 int y1,
5939 int x2,
5940 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005941 COLORREF color)
5942{
5943#if defined(FEAT_DIRECTX)
5944 if (IS_ENABLE_DIRECTX())
5945 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5946 else
5947#endif
5948 {
5949 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5950 HPEN old_pen = SelectObject(s_hdc, hpen);
5951 MoveToEx(s_hdc, x1, y1, NULL);
5952 /* Note: LineTo() excludes the last pixel in the line. */
5953 LineTo(s_hdc, x2, y2);
5954 DeleteObject(SelectObject(s_hdc, old_pen));
5955 }
5956}
5957
5958 static void
5959set_pixel(
5960 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005961 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005962 COLORREF color)
5963{
5964#if defined(FEAT_DIRECTX)
5965 if (IS_ENABLE_DIRECTX())
5966 DWriteContext_SetPixel(s_dwc, x, y, color);
5967 else
5968#endif
5969 SetPixel(s_hdc, x, y, color);
5970}
5971
5972 static void
5973fill_rect(
5974 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005975 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005976 COLORREF color)
5977{
5978#if defined(FEAT_DIRECTX)
5979 if (IS_ENABLE_DIRECTX())
5980 DWriteContext_FillRect(s_dwc, rcp, color);
5981 else
5982#endif
5983 {
5984 HBRUSH hbr2;
5985
5986 if (hbr == NULL)
5987 hbr2 = CreateSolidBrush(color);
5988 else
5989 hbr2 = hbr;
5990 FillRect(s_hdc, rcp, hbr2);
5991 if (hbr == NULL)
5992 DeleteBrush(hbr2);
5993 }
5994}
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 void
5997gui_mch_draw_string(
5998 int row,
5999 int col,
6000 char_u *text,
6001 int len,
6002 int flags)
6003{
6004 static int *padding = NULL;
6005 static int pad_size = 0;
6006 int i;
6007 const RECT *pcliprect = NULL;
6008 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 static WCHAR *unicodebuf = NULL;
6010 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006011 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 int y;
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 /*
6016 * Italic and bold text seems to have an extra row of pixels at the bottom
6017 * (below where the bottom of the character should be). If we draw the
6018 * characters with a solid background, the top row of pixels in the
6019 * character below will be overwritten. We can fix this by filling in the
6020 * background ourselves, to the correct character proportions, and then
6021 * writing the character in transparent mode. Still have a problem when
6022 * the character is "_", which gets written on to the character below.
6023 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6024 * pixel in their slots, which fixes the problem with the bottom row of
6025 * pixels. We still need this code because otherwise the top row of pixels
6026 * becomes a problem. - webb.
6027 */
6028 static HBRUSH hbr_cache[2] = {NULL, NULL};
6029 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6030 static int brush_lru = 0;
6031 HBRUSH hbr;
6032 RECT rc;
6033
6034 if (!(flags & DRAW_TRANSP))
6035 {
6036 /*
6037 * Clear background first.
6038 * Note: FillRect() excludes right and bottom of rectangle.
6039 */
6040 rc.left = FILL_X(col);
6041 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 if (has_mbyte)
6043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006045 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 }
6047 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 rc.right = FILL_X(col + len);
6049 rc.bottom = FILL_Y(row + 1);
6050
6051 /* Cache the created brush, that saves a lot of time. We need two:
6052 * one for cursor background and one for the normal background. */
6053 if (gui.currBgColor == brush_color[0])
6054 {
6055 hbr = hbr_cache[0];
6056 brush_lru = 1;
6057 }
6058 else if (gui.currBgColor == brush_color[1])
6059 {
6060 hbr = hbr_cache[1];
6061 brush_lru = 0;
6062 }
6063 else
6064 {
6065 if (hbr_cache[brush_lru] != NULL)
6066 DeleteBrush(hbr_cache[brush_lru]);
6067 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6068 brush_color[brush_lru] = gui.currBgColor;
6069 hbr = hbr_cache[brush_lru];
6070 brush_lru = !brush_lru;
6071 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006072
Bram Moolenaar92467d32017-12-05 13:22:16 +01006073 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
6075 SetBkMode(s_hdc, TRANSPARENT);
6076
6077 /*
6078 * When drawing block cursor, prevent inverted character spilling
6079 * over character cell (can happen with bold/italic)
6080 */
6081 if (flags & DRAW_CURSOR)
6082 {
6083 pcliprect = &rc;
6084 foptions = ETO_CLIPPED;
6085 }
6086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 SetTextColor(s_hdc, gui.currFgColor);
6088 SelectFont(s_hdc, gui.currFont);
6089
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006090#ifdef FEAT_DIRECTX
6091 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006092 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006093#endif
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6096 {
6097 vim_free(padding);
6098 pad_size = Columns;
6099
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006100 /* Don't give an out-of-memory message here, it would call us
6101 * recursively. */
Bram Moolenaar59edb002019-05-28 23:32:47 +02006102 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (padding != NULL)
6104 for (i = 0; i < pad_size; i++)
6105 padding[i] = gui.char_width;
6106 }
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 /*
6109 * We have to provide the padding argument because italic and bold versions
6110 * of fixed-width fonts are often one pixel or so wider than their normal
6111 * versions.
6112 * No check for DRAW_BOLD, Windows will have done it already.
6113 */
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* Check if there are any UTF-8 characters. If not, use normal text
6116 * output to speed up output. */
6117 if (enc_utf8)
6118 for (n = 0; n < len; ++n)
6119 if (text[n] >= 0x80)
6120 break;
6121
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006122#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006123 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6124 * required that unicode drawing routine, currently. So this forces it
6125 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006126 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006127 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006128#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006131 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006133 if ((enc_utf8
6134 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6135 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 && (unicodebuf == NULL || len > unibuflen))
6137 {
6138 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006139 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006142 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 unibuflen = len;
6145 }
6146
6147 if (enc_utf8 && n < len && unicodebuf != NULL)
6148 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006149 /* Output UTF-8 characters. Composing characters should be
6150 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006151 int i;
6152 int wlen; /* string length in words */
6153 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 int cells; /* cell width of string up to composing char */
6155 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006156 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006158 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006159 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006161 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006163 c = utf_ptr2char(text + i);
6164 if (c >= 0x10000)
6165 {
6166 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006167 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6168 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006169 }
6170 else
6171 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006172 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006173 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006174
6175 if (utf_iscomposing(c))
6176 cw = 0;
6177 else
6178 {
6179 cw = utf_char2cells(c);
6180 if (cw > 2) /* don't use 4 for unprintable char */
6181 cw = 1;
6182 }
6183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 if (unicodepdy != NULL)
6185 {
6186 /* Use unicodepdy to make characters fit as we expect, even
6187 * when the font uses different widths (e.g., bold character
6188 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006189 if (c >= 0x10000)
6190 {
6191 unicodepdy[wlen - 2] = cw * gui.char_width;
6192 unicodepdy[wlen - 1] = 0;
6193 }
6194 else
6195 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006198 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006199 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006201#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006202 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006203 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006204 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006205 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006206 TEXT_X(col), TEXT_Y(row),
6207 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006208 gui.char_width, gui.currFgColor,
6209 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006210 }
6211 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006212#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006213 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6214 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 len = cells; /* used for underlining */
6216 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006217 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 /* If we want to display codepage data, and the current CP is not the
6220 * ANSI one, we need to go via Unicode. */
6221 if (unicodebuf != NULL)
6222 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006223 if (enc_latin9)
6224 latin9_to_ucs(text, len, unicodebuf);
6225 else
6226 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 MB_PRECOMPOSED,
6228 (char *)text, len,
6229 (LPWSTR)unicodebuf, unibuflen);
6230 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006231 {
6232 /* Use unicodepdy to make characters fit as we expect, even
6233 * when the font uses different widths (e.g., bold character
6234 * is wider). */
6235 if (unicodepdy != NULL)
6236 {
6237 int i;
6238 int cw;
6239
6240 for (i = 0; i < len; ++i)
6241 {
6242 cw = utf_char2cells(unicodebuf[i]);
6243 if (cw > 2)
6244 cw = 1;
6245 unicodepdy[i] = cw * gui.char_width;
6246 }
6247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006249 foptions, pcliprect, unicodebuf, len, unicodepdy);
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252 }
6253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 {
6255#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006256 /* Windows will mess up RL text, so we have to draw it character by
6257 * character. Only do this if RL is on, since it's slow. */
6258 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6260 foptions, pcliprect, (char *)text, len, padding);
6261 else
6262#endif
6263 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6264 foptions, pcliprect, (char *)text, len, padding);
6265 }
6266
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006267 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 if (flags & DRAW_UNDERL)
6269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 /* When p_linespace is 0, overwrite the bottom row of pixels.
6271 * Otherwise put the line just below the character. */
6272 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 if (p_linespace > 1)
6274 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006275 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006277
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006278 /* Strikethrough */
6279 if (flags & DRAW_STRIKE)
6280 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006281 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006282 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006283 }
6284
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006285 /* Undercurl */
6286 if (flags & DRAW_UNDERC)
6287 {
6288 int x;
6289 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006290 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006291
6292 y = FILL_Y(row + 1) - 1;
6293 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6294 {
6295 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006296 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006297 }
6298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299}
6300
6301
6302/*
6303 * Output routines.
6304 */
6305
6306/* Flush any output to the screen */
6307 void
6308gui_mch_flush(void)
6309{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006310#if defined(FEAT_DIRECTX)
6311 if (IS_ENABLE_DIRECTX())
6312 DWriteContext_Flush(s_dwc);
6313#endif
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 GdiFlush();
6316}
6317
6318 static void
6319clear_rect(RECT *rcp)
6320{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006321 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
6324
Bram Moolenaarc716c302006-01-21 22:12:51 +00006325 void
6326gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6327{
6328 RECT workarea_rect;
6329
6330 get_work_area(&workarea_rect);
6331
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006332 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006333 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006334 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006335
6336 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6337 * the menubar for MSwin, we subtract it from the screen height, so that
6338 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006339 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006340 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006341 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006342 - GetSystemMetrics(SM_CYCAPTION)
6343#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006344 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006345#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006346 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006347}
6348
6349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350#if defined(FEAT_MENU) || defined(PROTO)
6351/*
6352 * Add a sub menu to the menu bar.
6353 */
6354 void
6355gui_mch_add_menu(
6356 vimmenu_T *menu,
6357 int pos)
6358{
6359 vimmenu_T *parent = menu->parent;
6360
6361 menu->submenu_id = CreatePopupMenu();
6362 menu->id = s_menu_id++;
6363
6364 if (menu_is_menubar(menu->name))
6365 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006366 WCHAR *wn;
6367 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006369 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006370 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006371 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006373 infow.cbSize = sizeof(infow);
6374 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6375 | MIIM_SUBMENU;
6376 infow.dwItemData = (long_u)menu;
6377 infow.wID = menu->id;
6378 infow.fType = MFT_STRING;
6379 infow.dwTypeData = wn;
6380 infow.cch = (UINT)wcslen(wn);
6381 infow.hSubMenu = menu->submenu_id;
6382 InsertMenuItemW((parent == NULL)
6383 ? s_menuBar : parent->submenu_id,
6384 (UINT)pos, TRUE, &infow);
6385 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 }
6387
6388 /* Fix window size if menu may have wrapped */
6389 if (parent == NULL)
6390 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006391# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 else if (IsWindow(parent->tearoff_handle))
6393 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395}
6396
6397 void
6398gui_mch_show_popupmenu(vimmenu_T *menu)
6399{
6400 POINT mp;
6401
6402 (void)GetCursorPos((LPPOINT)&mp);
6403 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6404}
6405
6406 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006407gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 vimmenu_T *menu = gui_find_menu(path_name);
6410
6411 if (menu != NULL)
6412 {
6413 POINT p;
6414
6415 /* Find the position of the current cursor */
6416 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006417 if (mouse_pos)
6418 {
6419 int mx, my;
6420
6421 gui_mch_getmouse(&mx, &my);
6422 p.x += mx;
6423 p.y += my;
6424 }
6425 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006427 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6429 }
6430 msg_scroll = FALSE;
6431 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6432 }
6433}
6434
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006435# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436/*
6437 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6438 * create it as a pseudo-"tearoff menu".
6439 */
6440 void
6441gui_make_tearoff(char_u *path_name)
6442{
6443 vimmenu_T *menu = gui_find_menu(path_name);
6444
6445 /* Found the menu, so tear it off. */
6446 if (menu != NULL)
6447 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6448}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451/*
6452 * Add a menu item to a menu
6453 */
6454 void
6455gui_mch_add_menu_item(
6456 vimmenu_T *menu,
6457 int idx)
6458{
6459 vimmenu_T *parent = menu->parent;
6460
6461 menu->id = s_menu_id++;
6462 menu->submenu_id = NULL;
6463
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006464# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6466 {
6467 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6468 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6469 }
6470 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006471# endif
6472# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 if (menu_is_toolbar(parent->name))
6474 {
6475 TBBUTTON newtb;
6476
6477 vim_memset(&newtb, 0, sizeof(newtb));
6478 if (menu_is_separator(menu->name))
6479 {
6480 newtb.iBitmap = 0;
6481 newtb.fsStyle = TBSTYLE_SEP;
6482 }
6483 else
6484 {
6485 newtb.iBitmap = get_toolbar_bitmap(menu);
6486 newtb.fsStyle = TBSTYLE_BUTTON;
6487 }
6488 newtb.idCommand = menu->id;
6489 newtb.fsState = TBSTATE_ENABLED;
6490 newtb.iString = 0;
6491 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6492 (LPARAM)&newtb);
6493 menu->submenu_id = (HMENU)-1;
6494 }
6495 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006496# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006498 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006500 wn = enc_to_utf16(menu->name, NULL);
6501 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006503 InsertMenuW(parent->submenu_id, (UINT)idx,
6504 (menu_is_separator(menu->name)
6505 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6506 (UINT)menu->id, wn);
6507 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (IsWindow(parent->tearoff_handle))
6511 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006512# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
6514}
6515
6516/*
6517 * Destroy the machine specific menu widget.
6518 */
6519 void
6520gui_mch_destroy_menu(vimmenu_T *menu)
6521{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006522# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 /*
6524 * is this a toolbar button?
6525 */
6526 if (menu->submenu_id == (HMENU)-1)
6527 {
6528 int iButton;
6529
6530 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6531 (WPARAM)menu->id, 0);
6532 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6533 }
6534 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006535# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 {
6537 if (menu->parent != NULL
6538 && menu_is_popup(menu->parent->dname)
6539 && menu->parent->submenu_id != NULL)
6540 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6541 else
6542 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6543 if (menu->submenu_id != NULL)
6544 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006545# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (IsWindow(menu->tearoff_handle))
6547 DestroyWindow(menu->tearoff_handle);
6548 if (menu->parent != NULL
6549 && menu->parent->children != NULL
6550 && IsWindow(menu->parent->tearoff_handle))
6551 {
6552 /* This menu must not show up when rebuilding the tearoff window. */
6553 menu->modes = 0;
6554 rebuild_tearoff(menu->parent);
6555 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558}
6559
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006560# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 static void
6562rebuild_tearoff(vimmenu_T *menu)
6563{
6564 /*hackish*/
6565 char_u tbuf[128];
6566 RECT trect;
6567 RECT rct;
6568 RECT roct;
6569 int x, y;
6570
6571 HWND thwnd = menu->tearoff_handle;
6572
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006573 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (GetWindowRect(thwnd, &trect)
6575 && GetWindowRect(s_hwnd, &rct)
6576 && GetClientRect(s_hwnd, &roct))
6577 {
6578 x = trect.left - rct.left;
6579 y = (trect.top - rct.bottom + roct.bottom);
6580 }
6581 else
6582 {
6583 x = y = 0xffffL;
6584 }
6585 DestroyWindow(thwnd);
6586 if (menu->children != NULL)
6587 {
6588 gui_mch_tearoff(tbuf, menu, x, y);
6589 if (IsWindow(menu->tearoff_handle))
6590 (void) SetWindowPos(menu->tearoff_handle,
6591 NULL,
6592 (int)trect.left,
6593 (int)trect.top,
6594 0, 0,
6595 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6596 }
6597}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006598# endif /* FEAT_TEAROFF */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600/*
6601 * Make a menu either grey or not grey.
6602 */
6603 void
6604gui_mch_menu_grey(
6605 vimmenu_T *menu,
6606 int grey)
6607{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006608# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 /*
6610 * is this a toolbar button?
6611 */
6612 if (menu->submenu_id == (HMENU)-1)
6613 {
6614 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6615 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6616 }
6617 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006618# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006619 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6620 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006622# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6624 {
6625 WORD menuID;
6626 HWND menuHandle;
6627
6628 /*
6629 * A tearoff button has changed state.
6630 */
6631 if (menu->children == NULL)
6632 menuID = (WORD)(menu->id);
6633 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006634 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6636 if (menuHandle)
6637 EnableWindow(menuHandle, !grey);
6638
6639 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642
6643#endif /* FEAT_MENU */
6644
6645
6646/* define some macros used to make the dialogue creation more readable */
6647
6648#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6649#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006650#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6653/*
6654 * stuff for dialogs
6655 */
6656
6657/*
6658 * The callback routine used by all the dialogs. Very simple. First,
6659 * acknowledges the INITDIALOG message so that Windows knows to do standard
6660 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6661 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6662 * number.
6663 */
6664 static LRESULT CALLBACK
6665dialog_callback(
6666 HWND hwnd,
6667 UINT message,
6668 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006669 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670{
6671 if (message == WM_INITDIALOG)
6672 {
6673 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6674 /* Set focus to the dialog. Set the default button, if specified. */
6675 (void)SetFocus(hwnd);
6676 if (dialog_default_button > IDCANCEL)
6677 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006678 else
6679 /* We don't have a default, set focus on another element of the
6680 * dialog window, probably the icon */
6681 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 return FALSE;
6683 }
6684
6685 if (message == WM_COMMAND)
6686 {
6687 int button = LOWORD(wParam);
6688
6689 /* Don't end the dialog if something was selected that was
6690 * not a button.
6691 */
6692 if (button >= DLG_NONBUTTON_CONTROL)
6693 return TRUE;
6694
6695 /* If the edit box exists, copy the string. */
6696 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006697 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006698 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006699 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006700
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006701 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6702 p = utf16_to_enc(wp, NULL);
6703 vim_strncpy(s_textfield, p, IOSIZE);
6704 vim_free(p);
6705 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707
6708 /*
6709 * Need to check for IDOK because if the user just hits Return to
6710 * accept the default value, some reason this is what we get.
6711 */
6712 if (button == IDOK)
6713 {
6714 if (dialog_default_button > IDCANCEL)
6715 EndDialog(hwnd, dialog_default_button);
6716 }
6717 else
6718 EndDialog(hwnd, button - IDCANCEL);
6719 return TRUE;
6720 }
6721
6722 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6723 {
6724 EndDialog(hwnd, 0);
6725 return TRUE;
6726 }
6727 return FALSE;
6728}
6729
6730/*
6731 * Create a dialog dynamically from the parameter strings.
6732 * type = type of dialog (question, alert, etc.)
6733 * title = dialog title. may be NULL for default title.
6734 * message = text to display. Dialog sizes to accommodate it.
6735 * buttons = '\n' separated list of button captions, default first.
6736 * dfltbutton = number of default button.
6737 *
6738 * This routine returns 1 if the first button is pressed,
6739 * 2 for the second, etc.
6740 *
6741 * 0 indicates Esc was pressed.
6742 * -1 for unexpected error
6743 *
6744 * If stubbing out this fn, return 1.
6745 */
6746
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006747static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 "IDR_VIM",
6750 "IDR_VIM_ERROR",
6751 "IDR_VIM_ALERT",
6752 "IDR_VIM_INFO",
6753 "IDR_VIM_QUESTION"
6754};
6755
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 int
6757gui_mch_dialog(
6758 int type,
6759 char_u *title,
6760 char_u *message,
6761 char_u *buttons,
6762 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006763 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006764 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006767 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 int numButtons;
6769 int *buttonWidths, *buttonPositions;
6770 int buttonYpos;
6771 int nchar, i;
6772 DWORD lStyle;
6773 int dlgwidth = 0;
6774 int dlgheight;
6775 int editboxheight;
6776 int horizWidth = 0;
6777 int msgheight;
6778 char_u *pstart;
6779 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006780 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 char_u *tbuffer;
6782 RECT rect;
6783 HWND hwnd;
6784 HDC hdc;
6785 HFONT font, oldFont;
6786 TEXTMETRIC fontInfo;
6787 int fontHeight;
6788 int textWidth, minButtonWidth, messageWidth;
6789 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006790 int maxDialogHeight;
6791 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 int vertical;
6793 int dlgPaddingX;
6794 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006795# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006796 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006798# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006799 garray_T ga;
6800 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006802# ifndef NO_CONSOLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 /* Don't output anything in silent mode ("ex -s") */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006804# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006805 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006806# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006807 if (silent_mode)
6808 return dfltbutton; /* return default option */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810
Bram Moolenaar748bf032005-02-02 23:04:36 +00006811 if (s_hwnd == NULL)
6812 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 if ((type < 0) || (type > VIM_LAST_TYPE))
6815 type = 0;
6816
6817 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006818 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006819 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006820 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 if (p == NULL)
6823 return -1;
6824
6825 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006826 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6828 * const.
6829 */
6830 tbuffer = vim_strsave(buttons);
6831 if (tbuffer == NULL)
6832 return -1;
6833
6834 --dfltbutton; /* Change from one-based to zero-based */
6835
6836 /* Count buttons */
6837 numButtons = 1;
6838 for (i = 0; tbuffer[i] != '\0'; i++)
6839 {
6840 if (tbuffer[i] == DLG_BUTTON_SEP)
6841 numButtons++;
6842 }
6843 if (dfltbutton >= numButtons)
6844 dfltbutton = -1;
6845
6846 /* Allocate array to hold the width of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006847 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 if (buttonWidths == NULL)
6849 return -1;
6850
6851 /* Allocate array to hold the X position of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006852 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 if (buttonPositions == NULL)
6854 return -1;
6855
6856 /*
6857 * Calculate how big the dialog must be.
6858 */
6859 hwnd = GetDesktopWindow();
6860 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006861# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6863 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006864 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 use_lfSysmenu = TRUE;
6866 }
6867 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6870 VARIABLE_PITCH , DLG_FONT_NAME);
6871 if (s_usenewlook)
6872 {
6873 oldFont = SelectFont(hdc, font);
6874 dlgPaddingX = DLG_PADDING_X;
6875 dlgPaddingY = DLG_PADDING_Y;
6876 }
6877 else
6878 {
6879 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6880 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6881 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6882 }
6883 GetTextMetrics(hdc, &fontInfo);
6884 fontHeight = fontInfo.tmHeight;
6885
6886 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006887 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
6889 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006890 if (s_hwnd == NULL)
6891 {
6892 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893
Bram Moolenaarc716c302006-01-21 22:12:51 +00006894 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006895 get_work_area(&workarea_rect);
6896 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6897 if (maxDialogWidth > 600)
6898 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006899 /* Leave some room for the taskbar. */
6900 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006901 }
6902 else
6903 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006904 /* Use our own window for the size, unless it's very small. */
6905 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006906 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006907 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006908 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006909 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6910 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006911
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006912 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006913 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006914 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006915 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006916 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6917 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6918 }
6919
6920 /* Set dlgwidth to width of message.
6921 * Copy the message into "ga", changing NL to CR-NL and inserting line
6922 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 pstart = message;
6924 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925 msgheight = 0;
6926 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 do
6928 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929 msgheight += fontHeight; /* at least one line */
6930
6931 /* Need to figure out where to break the string. The system does it
6932 * at a word boundary, which would mean we can't compute the number of
6933 * wrapped lines. */
6934 textWidth = 0;
6935 last_white = NULL;
6936 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006937 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006938 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006939 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006940 && textWidth > maxDialogWidth * 3 / 4)
6941 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006942 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006943 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006944 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006945 /* Line will wrap. */
6946 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006947 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948 textWidth = 0;
6949
6950 if (last_white != NULL)
6951 {
6952 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006953 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006954 pend = last_white + 1;
6955 last_white = NULL;
6956 }
6957 ga_append(&ga, '\r');
6958 ga_append(&ga, '\n');
6959 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006960 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961
6962 while (--l >= 0)
6963 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006964 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006965 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006967
6968 ga_append(&ga, '\r');
6969 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 pstart = pend + 1;
6971 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006972
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006973 if (ga.ga_data != NULL)
6974 message = ga.ga_data;
6975
Bram Moolenaar748bf032005-02-02 23:04:36 +00006976 messageWidth += 10; /* roundoff space */
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006979 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6980 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 if (msgheight < DLG_ICON_HEIGHT)
6983 msgheight = DLG_ICON_HEIGHT;
6984
6985 /*
6986 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006987 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006989 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (!vertical)
6991 {
6992 // Place buttons horizontally if they fit.
6993 horizWidth = dlgPaddingX;
6994 pstart = tbuffer;
6995 i = 0;
6996 do
6997 {
6998 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6999 if (pend == NULL)
7000 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007001 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (textWidth < minButtonWidth)
7003 textWidth = minButtonWidth;
7004 textWidth += dlgPaddingX; /* Padding within button */
7005 buttonWidths[i] = textWidth;
7006 buttonPositions[i++] = horizWidth;
7007 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7008 pstart = pend + 1;
7009 } while (*pend != NUL);
7010
7011 if (horizWidth > maxDialogWidth)
7012 vertical = TRUE; // Too wide to fit on the screen.
7013 else if (horizWidth > dlgwidth)
7014 dlgwidth = horizWidth;
7015 }
7016
7017 if (vertical)
7018 {
7019 // Stack buttons vertically.
7020 pstart = tbuffer;
7021 do
7022 {
7023 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7024 if (pend == NULL)
7025 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007026 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 textWidth += dlgPaddingX; /* Padding within button */
7028 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7029 if (textWidth > dlgwidth)
7030 dlgwidth = textWidth;
7031 pstart = pend + 1;
7032 } while (*pend != NUL);
7033 }
7034
7035 if (dlgwidth < DLG_MIN_WIDTH)
7036 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7037
7038 /* start to fill in the dlgtemplate information. addressing by WORDs */
7039 if (s_usenewlook)
7040 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7041 else
7042 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7043
7044 add_long(lStyle);
7045 add_long(0); // (lExtendedStyle)
7046 pnumitems = p; /*save where the number of items must be stored*/
7047 add_word(0); // NumberOfItems(will change later)
7048 add_word(10); // x
7049 add_word(10); // y
7050 add_word(PixelToDialogX(dlgwidth)); // cx
7051
7052 // Dialog height.
7053 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007054 dlgheight = msgheight + 2 * dlgPaddingY
7055 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 else
7057 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7058
7059 // Dialog needs to be taller if contains an edit box.
7060 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7061 if (textfield != NULL)
7062 dlgheight += editboxheight;
7063
Bram Moolenaara95d8232013-08-07 15:27:11 +02007064 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7065 if (dlgheight > maxDialogHeight)
7066 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007067 msgheight = msgheight - (dlgheight - maxDialogHeight);
7068 dlgheight = maxDialogHeight;
7069 scroll_flag = WS_VSCROLL;
7070 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7071 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007072 }
7073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 add_word(PixelToDialogY(dlgheight));
7075
7076 add_word(0); // Menu
7077 add_word(0); // Class
7078
7079 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007080 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7081 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 p += nchar;
7083
7084 if (s_usenewlook)
7085 {
7086 /* do the font, since DS_3DLOOK doesn't work properly */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007087# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (use_lfSysmenu)
7089 {
7090 /* point size */
7091 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7092 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007093 wcscpy(p, lfSysmenu.lfFaceName);
7094 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 }
7096 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
7099 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007100 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102 p += nchar;
7103 }
7104
7105 buttonYpos = msgheight + 2 * dlgPaddingY;
7106
7107 if (textfield != NULL)
7108 buttonYpos += editboxheight;
7109
7110 pstart = tbuffer;
7111 if (!vertical)
7112 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7113 for (i = 0; i < numButtons; i++)
7114 {
7115 /* get end of this button. */
7116 for ( pend = pstart;
7117 *pend && (*pend != DLG_BUTTON_SEP);
7118 pend++)
7119 ;
7120
7121 if (*pend)
7122 *pend = '\0';
7123
7124 /*
7125 * old NOTE:
7126 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7127 * the focus to the first tab-able button and in so doing makes that
7128 * the default!! Grrr. Workaround: Make the default button the only
7129 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7130 * he/she can use arrow keys.
7131 *
7132 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007133 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * dialog. Also needed for when the textfield is the default control.
7135 * It appears to work now (perhaps not on Win95?).
7136 */
7137 if (vertical)
7138 {
7139 p = add_dialog_element(p,
7140 (i == dfltbutton
7141 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7142 PixelToDialogX(DLG_VERT_PADDING_X),
7143 PixelToDialogY(buttonYpos /* TBK */
7144 + 2 * fontHeight * i),
7145 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7146 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007147 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149 else
7150 {
7151 p = add_dialog_element(p,
7152 (i == dfltbutton
7153 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7154 PixelToDialogX(horizWidth + buttonPositions[i]),
7155 PixelToDialogY(buttonYpos), /* TBK */
7156 PixelToDialogX(buttonWidths[i]),
7157 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007158 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 }
7160 pstart = pend + 1; /*next button*/
7161 }
7162 *pnumitems += numButtons;
7163
7164 /* Vim icon */
7165 p = add_dialog_element(p, SS_ICON,
7166 PixelToDialogX(dlgPaddingX),
7167 PixelToDialogY(dlgPaddingY),
7168 PixelToDialogX(DLG_ICON_WIDTH),
7169 PixelToDialogY(DLG_ICON_HEIGHT),
7170 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7171 dlg_icons[type]);
7172
Bram Moolenaar748bf032005-02-02 23:04:36 +00007173 /* Dialog message */
7174 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7175 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7176 PixelToDialogY(dlgPaddingY),
7177 (WORD)(PixelToDialogX(messageWidth) + 1),
7178 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007179 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
7181 /* Edit box */
7182 if (textfield != NULL)
7183 {
7184 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7185 PixelToDialogX(2 * dlgPaddingX),
7186 PixelToDialogY(2 * dlgPaddingY + msgheight),
7187 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7188 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007189 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 *pnumitems += 1;
7191 }
7192
7193 *pnumitems += 2;
7194
7195 SelectFont(hdc, oldFont);
7196 DeleteObject(font);
7197 ReleaseDC(hwnd, hdc);
7198
7199 /* Let the dialog_callback() function know which button to make default
7200 * If we have an edit box, make that the default. We also need to tell
7201 * dialog_callback() if this dialog contains an edit box or not. We do
7202 * this by setting s_textfield if it does.
7203 */
7204 if (textfield != NULL)
7205 {
7206 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7207 s_textfield = textfield;
7208 }
7209 else
7210 {
7211 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7212 s_textfield = NULL;
7213 }
7214
7215 /* show the dialog box modally and get a return value */
7216 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007217 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 (LPDLGTEMPLATE)pdlgtemplate,
7219 s_hwnd,
7220 (DLGPROC)dialog_callback);
7221
7222 LocalFree(LocalHandle(pdlgtemplate));
7223 vim_free(tbuffer);
7224 vim_free(buttonWidths);
7225 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007226 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
7228 /* Focus back to our window (for when MDI is used). */
7229 (void)SetFocus(s_hwnd);
7230
7231 return nchar;
7232}
7233
7234#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236/*
7237 * Put a simple element (basic class) onto a dialog template in memory.
7238 * return a pointer to where the next item should be added.
7239 *
7240 * parameters:
7241 * lStyle = additional style flags
7242 * (be careful, NT3.51 & Win32s will ignore the new ones)
7243 * x,y = x & y positions IN DIALOG UNITS
7244 * w,h = width and height IN DIALOG UNITS
7245 * Id = ID used in messages
7246 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7247 * caption = usually text or resource name
7248 *
7249 * TODO: use the length information noted here to enable the dialog creation
7250 * routines to work out more exactly how much memory they need to alloc.
7251 */
7252 static PWORD
7253add_dialog_element(
7254 PWORD p,
7255 DWORD lStyle,
7256 WORD x,
7257 WORD y,
7258 WORD w,
7259 WORD h,
7260 WORD Id,
7261 WORD clss,
7262 const char *caption)
7263{
7264 int nchar;
7265
7266 p = lpwAlign(p); /* Align to dword boundary*/
7267 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7268 *p++ = LOWORD(lStyle);
7269 *p++ = HIWORD(lStyle);
7270 *p++ = 0; // LOWORD (lExtendedStyle)
7271 *p++ = 0; // HIWORD (lExtendedStyle)
7272 *p++ = x;
7273 *p++ = y;
7274 *p++ = w;
7275 *p++ = h;
7276 *p++ = Id; //9 or 10 words in all
7277
7278 *p++ = (WORD)0xffff;
7279 *p++ = clss; //2 more here
7280
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007281 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 p += nchar;
7283
7284 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7285
7286 return p; //total = 15+ (strlen(caption)) words
7287 // = 30 + 2(strlen(caption) bytes reqd
7288}
7289
7290
7291/*
7292 * Helper routine. Take an input pointer, return closest pointer that is
7293 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7294 */
7295 static LPWORD
7296lpwAlign(
7297 LPWORD lpIn)
7298{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007299 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007301 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 ul += 3;
7303 ul >>= 2;
7304 ul <<= 2;
7305 return (LPWORD)ul;
7306}
7307
7308/*
7309 * Helper routine. Takes second parameter as Ansi string, copies it to first
7310 * parameter as wide character (16-bits / char) string, and returns integer
7311 * number of wide characters (words) in string (including the trailing wide
7312 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007313 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7314 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 static int
7316nCopyAnsiToWideChar(
7317 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007318 LPSTR lpAnsiIn,
7319 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7323 int i;
7324 WCHAR *wn;
7325
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007326 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 {
7328 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007329 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (wn != NULL)
7331 {
7332 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 vim_free(wn);
7335 }
7336 }
7337 if (nChar == 0)
7338 /* Use Win32 conversion function. */
7339 nChar = MultiByteToWideChar(
7340 enc_codepage > 0 ? enc_codepage : CP_ACP,
7341 MB_PRECOMPOSED,
7342 lpAnsiIn, len,
7343 lpWCStr, len);
7344 for (i = 0; i < nChar; ++i)
7345 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7346 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 return nChar;
7349}
7350
7351
7352#ifdef FEAT_TEAROFF
7353/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007354 * Lookup menu handle from "menu_id".
7355 */
7356 static HMENU
7357tearoff_lookup_menuhandle(
7358 vimmenu_T *menu,
7359 WORD menu_id)
7360{
7361 for ( ; menu != NULL; menu = menu->next)
7362 {
7363 if (menu->modes == 0) /* this menu has just been deleted */
7364 continue;
7365 if (menu_is_separator(menu->dname))
7366 continue;
7367 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7368 return menu->submenu_id;
7369 }
7370 return NULL;
7371}
7372
7373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 * The callback function for all the modeless dialogs that make up the
7375 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7376 * thinking its menus have been clicked), and go away when closed.
7377 */
7378 static LRESULT CALLBACK
7379tearoff_callback(
7380 HWND hwnd,
7381 UINT message,
7382 WPARAM wParam,
7383 LPARAM lParam)
7384{
7385 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007386 {
7387 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
7391 /* May show the mouse pointer again. */
7392 HandleMouseHide(message, lParam);
7393
7394 if (message == WM_COMMAND)
7395 {
7396 if ((WORD)(LOWORD(wParam)) & 0x8000)
7397 {
7398 POINT mp;
7399 RECT rect;
7400
7401 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7402 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007403 vimmenu_T *menu;
7404
7405 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007407 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7409 (int)rect.right - 8,
7410 (int)mp.y,
7411 (int)0, /*reserved param*/
7412 s_hwnd,
7413 NULL);
7414 /*
7415 * NOTE: The pop-up menu can eat the mouse up event.
7416 * We deal with this in normal.c.
7417 */
7418 }
7419 }
7420 else
7421 /* Pass on messages to the main Vim window */
7422 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7423 /*
7424 * Give main window the focus back: this is so after
7425 * choosing a tearoff button you can start typing again
7426 * straight away.
7427 */
7428 (void)SetFocus(s_hwnd);
7429 return TRUE;
7430 }
7431 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7432 {
7433 DestroyWindow(hwnd);
7434 return TRUE;
7435 }
7436
7437 /* When moved around, give main window the focus back. */
7438 if (message == WM_EXITSIZEMOVE)
7439 (void)SetActiveWindow(s_hwnd);
7440
7441 return FALSE;
7442}
7443#endif
7444
7445
7446/*
7447 * Decide whether to use the "new look" (small, non-bold font) or the "old
7448 * look" (big, clanky font) for dialogs, and work out a few values for use
7449 * later accordingly.
7450 */
7451 static void
7452get_dialog_font_metrics(void)
7453{
7454 HDC hdc;
7455 HFONT hfontTools = 0;
7456 DWORD dlgFontSize;
7457 SIZE size;
7458#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007459 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460#endif
7461
7462 s_usenewlook = FALSE;
7463
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007465 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007466 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468#endif
7469 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7470 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7471
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007472 if (hfontTools)
7473 {
7474 hdc = GetDC(s_hwnd);
7475 SelectObject(hdc, hfontTools);
7476 /*
7477 * GetTextMetrics() doesn't return the right value in
7478 * tmAveCharWidth, so we have to figure out the dialog base units
7479 * ourselves.
7480 */
7481 GetTextExtentPoint(hdc,
7482 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7483 52, &size);
7484 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007486 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7487 s_dlgfntheight = (WORD)size.cy;
7488 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 }
7490
7491 if (!s_usenewlook)
7492 {
7493 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7494 s_dlgfntwidth = LOWORD(dlgFontSize);
7495 s_dlgfntheight = HIWORD(dlgFontSize);
7496 }
7497}
7498
7499#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7500/*
7501 * Create a pseudo-"tearoff menu" based on the child
7502 * items of a given menu pointer.
7503 */
7504 static void
7505gui_mch_tearoff(
7506 char_u *title,
7507 vimmenu_T *menu,
7508 int initX,
7509 int initY)
7510{
7511 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7512 int template_len;
7513 int nchar, textWidth, submenuWidth;
7514 DWORD lStyle;
7515 DWORD lExtendedStyle;
7516 WORD dlgwidth;
7517 WORD menuID;
7518 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007519 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 vimmenu_T *the_menu = menu;
7521 HWND hwnd;
7522 HDC hdc;
7523 HFONT font, oldFont;
7524 int col, spaceWidth, len;
7525 int columnWidths[2];
7526 char_u *label, *text;
7527 int acLen = 0;
7528 int nameLen;
7529 int padding0, padding1, padding2 = 0;
7530 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007531 int x;
7532 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007533# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007534 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007536# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537
7538 /*
7539 * If this menu is already torn off, move it to the mouse position.
7540 */
7541 if (IsWindow(menu->tearoff_handle))
7542 {
7543 POINT mp;
7544 if (GetCursorPos((LPPOINT)&mp))
7545 {
7546 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7547 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7548 }
7549 return;
7550 }
7551
7552 /*
7553 * Create a new tearoff.
7554 */
7555 if (*title == MNU_HIDDEN_CHAR)
7556 title++;
7557
7558 /* Allocate memory to store the dialog template. It's made bigger when
7559 * needed. */
7560 template_len = DLG_ALLOC_SIZE;
7561 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7562 if (p == NULL)
7563 return;
7564
7565 hwnd = GetDesktopWindow();
7566 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007567# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7569 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007570 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 use_lfSysmenu = TRUE;
7572 }
7573 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7576 VARIABLE_PITCH , DLG_FONT_NAME);
7577 if (s_usenewlook)
7578 oldFont = SelectFont(hdc, font);
7579 else
7580 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7581
7582 /* Calculate width of a single space. Used for padding columns to the
7583 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007584 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
7586 /* Figure out max width of the text column, the accelerator column and the
7587 * optional submenu column. */
7588 submenuWidth = 0;
7589 for (col = 0; col < 2; col++)
7590 {
7591 columnWidths[col] = 0;
7592 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7593 {
7594 /* Use "dname" here to compute the width of the visible text. */
7595 text = (col == 0) ? pmenu->dname : pmenu->actext;
7596 if (text != NULL && *text != NUL)
7597 {
7598 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7599 if (textWidth > columnWidths[col])
7600 columnWidths[col] = textWidth;
7601 }
7602 if (pmenu->children != NULL)
7603 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7604 }
7605 }
7606 if (columnWidths[1] == 0)
7607 {
7608 /* no accelerators */
7609 if (submenuWidth != 0)
7610 columnWidths[0] += submenuWidth;
7611 else
7612 columnWidths[0] += spaceWidth;
7613 }
7614 else
7615 {
7616 /* there is an accelerator column */
7617 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7618 columnWidths[1] += submenuWidth;
7619 }
7620
7621 /*
7622 * Now find the total width of our 'menu'.
7623 */
7624 textWidth = columnWidths[0] + columnWidths[1];
7625 if (submenuWidth != 0)
7626 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007627 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7629 textWidth += submenuWidth;
7630 }
7631 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7632 if (textWidth > dlgwidth)
7633 dlgwidth = textWidth;
7634 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 /* start to fill in the dlgtemplate information. addressing by WORDs */
7637 if (s_usenewlook)
7638 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7639 else
7640 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7641
7642 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7643 *p++ = LOWORD(lStyle);
7644 *p++ = HIWORD(lStyle);
7645 *p++ = LOWORD(lExtendedStyle);
7646 *p++ = HIWORD(lExtendedStyle);
7647 pnumitems = p; /* save where the number of items must be stored */
7648 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007649 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007651 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 else
7653 *p++ = PixelToDialogX(initX); // x
7654 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007655 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 else
7657 *p++ = PixelToDialogY(initY); // y
7658 *p++ = PixelToDialogX(dlgwidth); // cx
7659 ptrueheight = p;
7660 *p++ = 0; // dialog height: changed later anyway
7661 *p++ = 0; // Menu
7662 *p++ = 0; // Class
7663
7664 /* copy the title of the dialog */
7665 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007666 ? (LPSTR)title
7667 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 p += nchar;
7669
7670 if (s_usenewlook)
7671 {
7672 /* do the font, since DS_3DLOOK doesn't work properly */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007673# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 if (use_lfSysmenu)
7675 {
7676 /* point size */
7677 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7678 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007679 wcscpy(p, lfSysmenu.lfFaceName);
7680 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
7682 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007683# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
7685 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007686 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 }
7688 p += nchar;
7689 }
7690
7691 /*
7692 * Loop over all the items in the menu.
7693 * But skip over the tearbar.
7694 */
7695 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7696 menu = menu->children->next;
7697 else
7698 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007699 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 for ( ; menu != NULL; menu = menu->next)
7701 {
7702 if (menu->modes == 0) /* this menu has just been deleted */
7703 continue;
7704 if (menu_is_separator(menu->dname))
7705 {
7706 sepPadding += 3;
7707 continue;
7708 }
7709
7710 /* Check if there still is plenty of room in the template. Make it
7711 * larger when needed. */
7712 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7713 {
7714 WORD *newp;
7715
7716 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7717 if (newp != NULL)
7718 {
7719 template_len += 4096;
7720 mch_memmove(newp, pdlgtemplate,
7721 (char *)p - (char *)pdlgtemplate);
7722 p = newp + (p - pdlgtemplate);
7723 pnumitems = newp + (pnumitems - pdlgtemplate);
7724 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7725 LocalFree(LocalHandle(pdlgtemplate));
7726 pdlgtemplate = newp;
7727 }
7728 }
7729
7730 /* Figure out minimal length of this menu label. Use "name" for the
7731 * actual text, "dname" for estimating the displayed size. "name"
7732 * has "&a" for mnemonic and includes the accelerator. */
7733 len = nameLen = (int)STRLEN(menu->name);
7734 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7735 (int)STRLEN(menu->dname))) / spaceWidth;
7736 len += padding0;
7737
7738 if (menu->actext != NULL)
7739 {
7740 acLen = (int)STRLEN(menu->actext);
7741 len += acLen;
7742 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7743 }
7744 else
7745 textWidth = 0;
7746 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7747 len += padding1;
7748
7749 if (menu->children == NULL)
7750 {
7751 padding2 = submenuWidth / spaceWidth;
7752 len += padding2;
7753 menuID = (WORD)(menu->id);
7754 }
7755 else
7756 {
7757 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007758 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760
7761 /* Allocate menu label and fill it in */
Bram Moolenaar964b3742019-05-24 18:54:09 +02007762 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (label == NULL)
7764 break;
7765
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007766 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7768 if (text == NULL)
7769 text = label + nameLen; /* no actext, use whole name */
7770 while (padding0-- > 0)
7771 *text++ = ' ';
7772 if (menu->actext != NULL)
7773 {
7774 STRNCPY(text, menu->actext, acLen);
7775 text += acLen;
7776 }
7777 while (padding1-- > 0)
7778 *text++ = ' ';
7779 if (menu->children != NULL)
7780 {
7781 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7782 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7783 }
7784 else
7785 {
7786 while (padding2-- > 0)
7787 *text++ = ' ';
7788 }
7789 *text = NUL;
7790
7791 /*
7792 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7793 * W95/NT4 it makes the tear-off look more like a menu.
7794 */
7795 p = add_dialog_element(p,
7796 BS_PUSHBUTTON|BS_LEFT,
7797 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7798 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7799 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7800 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007801 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 vim_free(label);
7803 (*pnumitems)++;
7804 }
7805
7806 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7807
7808
7809 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007810 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007811 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 (LPDLGTEMPLATE)pdlgtemplate,
7813 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007814 (DLGPROC)tearoff_callback,
7815 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 LocalFree(LocalHandle(pdlgtemplate));
7818 SelectFont(hdc, oldFont);
7819 DeleteObject(font);
7820 ReleaseDC(hwnd, hdc);
7821
7822 /*
7823 * Reassert ourselves as the active window. This is so that after creating
7824 * a tearoff, the user doesn't have to click with the mouse just to start
7825 * typing again!
7826 */
7827 (void)SetActiveWindow(s_hwnd);
7828
7829 /* make sure the right buttons are enabled */
7830 force_menu_update = TRUE;
7831}
7832#endif
7833
7834#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007835# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837/* This not defined in older SDKs */
7838# ifndef TBSTYLE_FLAT
7839# define TBSTYLE_FLAT 0x0800
7840# endif
7841
7842/*
7843 * Create the toolbar, initially unpopulated.
7844 * (just like the menu, there are no defaults, it's all
7845 * set up through menu.vim)
7846 */
7847 static void
7848initialise_toolbar(void)
7849{
7850 InitCommonControls();
7851 s_toolbarhwnd = CreateToolbarEx(
7852 s_hwnd,
7853 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7854 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007855 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007856 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 IDR_TOOLBAR1, // id of initial bitmap
7858 NULL,
7859 0, // initial number of buttons
7860 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7861 TOOLBAR_BUTTON_HEIGHT,
7862 TOOLBAR_BUTTON_WIDTH,
7863 TOOLBAR_BUTTON_HEIGHT,
7864 sizeof(TBBUTTON)
7865 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007866
7867 // Remove transparency from the toolbar to prevent the main window
7868 // background colour showing through
7869 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7870 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7871
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007872 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873
7874 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7875}
7876
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007877 static LRESULT CALLBACK
7878toolbar_wndproc(
7879 HWND hwnd,
7880 UINT uMsg,
7881 WPARAM wParam,
7882 LPARAM lParam)
7883{
7884 HandleMouseHide(uMsg, lParam);
7885 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7886}
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 static int
7889get_toolbar_bitmap(vimmenu_T *menu)
7890{
7891 int i = -1;
7892
7893 /*
7894 * Check user bitmaps first, unless builtin is specified.
7895 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007896 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {
7898 char_u fname[MAXPATHL];
7899 HANDLE hbitmap = NULL;
7900
7901 if (menu->iconfile != NULL)
7902 {
7903 gui_find_iconfile(menu->iconfile, fname, "bmp");
7904 hbitmap = LoadImage(
7905 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007906 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 IMAGE_BITMAP,
7908 TOOLBAR_BUTTON_WIDTH,
7909 TOOLBAR_BUTTON_HEIGHT,
7910 LR_LOADFROMFILE |
7911 LR_LOADMAP3DCOLORS
7912 );
7913 }
7914
7915 /*
7916 * If the LoadImage call failed, or the "icon=" file
7917 * didn't exist or wasn't specified, try the menu name
7918 */
7919 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007920 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007921# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007922 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007923# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007924 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 hbitmap = LoadImage(
7926 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007927 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 IMAGE_BITMAP,
7929 TOOLBAR_BUTTON_WIDTH,
7930 TOOLBAR_BUTTON_HEIGHT,
7931 LR_LOADFROMFILE |
7932 LR_LOADMAP3DCOLORS
7933 );
7934
7935 if (hbitmap != NULL)
7936 {
7937 TBADDBITMAP tbAddBitmap;
7938
7939 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007940 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
7942 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7943 (WPARAM)1, (LPARAM)&tbAddBitmap);
7944 /* i will be set to -1 if it fails */
7945 }
7946 }
7947 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7948 i = menu->iconidx;
7949
7950 return i;
7951}
7952#endif
7953
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007954#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7955 static void
7956initialise_tabline(void)
7957{
7958 InitCommonControls();
7959
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007960 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007961 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007962 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007963 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007964 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007965
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007966 gui.tabline_height = TABLINE_HEIGHT;
7967
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007968# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007969 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007970# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007971}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007972
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007973/*
7974 * Get tabpage_T from POINT.
7975 */
7976 static tabpage_T *
7977GetTabFromPoint(
7978 HWND hWnd,
7979 POINT pt)
7980{
7981 tabpage_T *ptp = NULL;
7982
7983 if (gui_mch_showing_tabline())
7984 {
7985 TCHITTESTINFO htinfo;
7986 htinfo.pt = pt;
7987 /* ignore if a window under cusor is not tabcontrol. */
7988 if (s_tabhwnd == hWnd)
7989 {
7990 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7991 if (idx != -1)
7992 ptp = find_tabpage(idx + 1);
7993 }
7994 }
7995 return ptp;
7996}
7997
7998static POINT s_pt = {0, 0};
7999static HCURSOR s_hCursor = NULL;
8000
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008001 static LRESULT CALLBACK
8002tabline_wndproc(
8003 HWND hwnd,
8004 UINT uMsg,
8005 WPARAM wParam,
8006 LPARAM lParam)
8007{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008008 POINT pt;
8009 tabpage_T *tp;
8010 RECT rect;
8011 int nCenter;
8012 int idx0;
8013 int idx1;
8014
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008015 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008016
8017 switch (uMsg)
8018 {
8019 case WM_LBUTTONDOWN:
8020 {
8021 s_pt.x = GET_X_LPARAM(lParam);
8022 s_pt.y = GET_Y_LPARAM(lParam);
8023 SetCapture(hwnd);
8024 s_hCursor = GetCursor(); /* backup default cursor */
8025 break;
8026 }
8027 case WM_MOUSEMOVE:
8028 if (GetCapture() == hwnd
8029 && ((wParam & MK_LBUTTON)) != 0)
8030 {
8031 pt.x = GET_X_LPARAM(lParam);
8032 pt.y = s_pt.y;
8033 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8034 {
8035 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8036
8037 tp = GetTabFromPoint(hwnd, pt);
8038 if (tp != NULL)
8039 {
8040 idx0 = tabpage_index(curtab) - 1;
8041 idx1 = tabpage_index(tp) - 1;
8042
8043 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8044 nCenter = rect.left + (rect.right - rect.left) / 2;
8045
8046 /* Check if the mouse cursor goes over the center of
8047 * the next tab to prevent "flickering". */
8048 if ((idx0 < idx1) && (nCenter < pt.x))
8049 {
8050 tabpage_move(idx1 + 1);
8051 update_screen(0);
8052 }
8053 else if ((idx1 < idx0) && (pt.x < nCenter))
8054 {
8055 tabpage_move(idx1);
8056 update_screen(0);
8057 }
8058 }
8059 }
8060 }
8061 break;
8062 case WM_LBUTTONUP:
8063 {
8064 if (GetCapture() == hwnd)
8065 {
8066 SetCursor(s_hCursor);
8067 ReleaseCapture();
8068 }
8069 break;
8070 }
8071 default:
8072 break;
8073 }
8074
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008075 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8076}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008077#endif
8078
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8080/*
8081 * Make the GUI window come to the foreground.
8082 */
8083 void
8084gui_mch_set_foreground(void)
8085{
8086 if (IsIconic(s_hwnd))
8087 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8088 SetForegroundWindow(s_hwnd);
8089}
8090#endif
8091
8092#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8093 static void
8094dyn_imm_load(void)
8095{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008096 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 if (hLibImm == NULL)
8098 return;
8099
8100 pImmGetCompositionStringA
8101 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8102 pImmGetCompositionStringW
8103 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8104 pImmGetContext
8105 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8106 pImmAssociateContext
8107 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8108 pImmReleaseContext
8109 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8110 pImmGetOpenStatus
8111 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8112 pImmSetOpenStatus
8113 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008114 pImmGetCompositionFontW
8115 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8116 pImmSetCompositionFontW
8117 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 pImmSetCompositionWindow
8119 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8120 pImmGetConversionStatus
8121 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008122 pImmSetConversionStatus
8123 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124
8125 if ( pImmGetCompositionStringA == NULL
8126 || pImmGetCompositionStringW == NULL
8127 || pImmGetContext == NULL
8128 || pImmAssociateContext == NULL
8129 || pImmReleaseContext == NULL
8130 || pImmGetOpenStatus == NULL
8131 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008132 || pImmGetCompositionFontW == NULL
8133 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008135 || pImmGetConversionStatus == NULL
8136 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
8138 FreeLibrary(hLibImm);
8139 hLibImm = NULL;
8140 pImmGetContext = NULL;
8141 return;
8142 }
8143
8144 return;
8145}
8146
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147#endif
8148
8149#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8150
8151# ifdef FEAT_XPM_W32
8152# define IMAGE_XPM 100
8153# endif
8154
8155typedef struct _signicon_t
8156{
8157 HANDLE hImage;
8158 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008159# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 HANDLE hShape; /* Mask bitmap handle */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162} signicon_t;
8163
8164 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008165gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166{
8167 signicon_t *sign;
8168 int x, y, w, h;
8169
8170 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8171 return;
8172
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008173# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008174 if (IS_ENABLE_DIRECTX())
8175 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008176# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 x = TEXT_X(col);
8179 y = TEXT_Y(row);
8180 w = gui.char_width * 2;
8181 h = gui.char_height;
8182 switch (sign->uType)
8183 {
8184 case IMAGE_BITMAP:
8185 {
8186 HDC hdcMem;
8187 HBITMAP hbmpOld;
8188
8189 hdcMem = CreateCompatibleDC(s_hdc);
8190 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8191 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8192 SelectObject(hdcMem, hbmpOld);
8193 DeleteDC(hdcMem);
8194 }
8195 break;
8196 case IMAGE_ICON:
8197 case IMAGE_CURSOR:
8198 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8199 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008200# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 case IMAGE_XPM:
8202 {
8203 HDC hdcMem;
8204 HBITMAP hbmpOld;
8205
8206 hdcMem = CreateCompatibleDC(s_hdc);
8207 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8208 /* Make hole */
8209 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8210
8211 SelectObject(hdcMem, sign->hImage);
8212 /* Paint sign */
8213 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8214 SelectObject(hdcMem, hbmpOld);
8215 DeleteDC(hdcMem);
8216 }
8217 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 }
8220}
8221
8222 static void
8223close_signicon_image(signicon_t *sign)
8224{
8225 if (sign)
8226 switch (sign->uType)
8227 {
8228 case IMAGE_BITMAP:
8229 DeleteObject((HGDIOBJ)sign->hImage);
8230 break;
8231 case IMAGE_CURSOR:
8232 DestroyCursor((HCURSOR)sign->hImage);
8233 break;
8234 case IMAGE_ICON:
8235 DestroyIcon((HICON)sign->hImage);
8236 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008237# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 case IMAGE_XPM:
8239 DeleteObject((HBITMAP)sign->hImage);
8240 DeleteObject((HBITMAP)sign->hShape);
8241 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 }
8244}
8245
8246 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008247gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248{
8249 signicon_t sign, *psign;
8250 char_u *ext;
8251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008253 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 if (ext > signfile)
8255 {
8256 int do_load = 1;
8257
8258 if (!STRICMP(ext, ".bmp"))
8259 sign.uType = IMAGE_BITMAP;
8260 else if (!STRICMP(ext, ".ico"))
8261 sign.uType = IMAGE_ICON;
8262 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8263 sign.uType = IMAGE_CURSOR;
8264 else
8265 do_load = 0;
8266
8267 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008268 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 gui.char_width * 2, gui.char_height,
8270 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008271# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (!STRICMP(ext, ".xpm"))
8273 {
8274 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008275 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8276 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 }
8280
8281 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008282 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 *psign = sign;
8284
8285 if (!psign)
8286 {
8287 if (sign.hImage)
8288 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008289 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
8291 return (void *)psign;
8292
8293}
8294
8295 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008296gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 if (sign)
8299 {
8300 close_signicon_image((signicon_t *)sign);
8301 vim_free(sign);
8302 }
8303}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008306#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307
8308/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008309 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008311 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8313 * to get current mouse position).
8314 *
8315 * Trying to use as more Windows services as possible, and as less
8316 * IE version as possible :)).
8317 *
8318 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8319 * BalloonEval struct.
8320 * 2) Enable/Disable simply create/kill BalloonEval Timer
8321 * 3) When there was enough inactivity, timer procedure posts
8322 * async request to debugger
8323 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8324 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008325 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 */
8327
Bram Moolenaar45360022005-07-21 21:08:21 +00008328/*
8329 * determine whether installed Common Controls support multiline tooltips
8330 * (i.e. their version is >= 4.70
8331 */
8332 int
8333multiline_balloon_available(void)
8334{
8335 HINSTANCE hDll;
8336 static char comctl_dll[] = "comctl32.dll";
8337 static int multiline_tip = MAYBE;
8338
8339 if (multiline_tip != MAYBE)
8340 return multiline_tip;
8341
8342 hDll = GetModuleHandle(comctl_dll);
8343 if (hDll != NULL)
8344 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008345 DLLGETVERSIONPROC pGetVer;
8346 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008347
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008348 if (pGetVer != NULL)
8349 {
8350 DLLVERSIONINFO dvi;
8351 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008352
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008353 ZeroMemory(&dvi, sizeof(dvi));
8354 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008355
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008356 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008357
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008358 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008359 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008360 || (dvi.dwMajorVersion == 4
8361 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008362 {
8363 multiline_tip = TRUE;
8364 return multiline_tip;
8365 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008366 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008367 else
8368 {
8369 /* there is chance we have ancient CommCtl 4.70
8370 which doesn't export DllGetVersion */
8371 DWORD dwHandle = 0;
8372 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8373 if (len > 0)
8374 {
8375 VS_FIXEDFILEINFO *ver;
8376 UINT vlen = 0;
8377 void *data = alloc(len);
8378
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008379 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008380 && GetFileVersionInfo(comctl_dll, 0, len, data)
8381 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8382 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008383 && HIWORD(ver->dwFileVersionMS) > 4)
8384 || ((HIWORD(ver->dwFileVersionMS) == 4
8385 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008386 {
8387 vim_free(data);
8388 multiline_tip = TRUE;
8389 return multiline_tip;
8390 }
8391 vim_free(data);
8392 }
8393 }
8394 }
8395 multiline_tip = FALSE;
8396 return multiline_tip;
8397}
8398
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008399 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008400make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008401{
8402 TOOLINFOW *pti;
8403 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008404
8405 if (multiline_balloon_available() == TRUE)
8406 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8407 else
8408 ToolInfoSize = sizeof(TOOLINFOW);
8409
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008410 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008411 if (pti == NULL)
8412 return;
8413
8414 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8415 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8416 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008417 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418
8419 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8420 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8421
8422 pti->cbSize = ToolInfoSize;
8423 pti->uFlags = TTF_SUBCLASS;
8424 pti->hwnd = beval->target;
8425 pti->hinst = 0; // Don't use string resources
8426 pti->uId = ID_BEVAL_TOOLTIP;
8427
8428 if (multiline_balloon_available() == TRUE)
8429 {
8430 RECT rect;
8431 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8432 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008433 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8434 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008435 // switch multiline tooltips on
8436 if (GetClientRect(s_textArea, &rect))
8437 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8438 (LPARAM)rect.right);
8439 }
8440 else
8441 {
8442 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008443 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8444 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008445 }
8446
8447 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8448 pti->rect.left = pt.x - 3;
8449 pti->rect.top = pt.y - 3;
8450 pti->rect.right = pt.x + 3;
8451 pti->rect.bottom = pt.y + 3;
8452
8453 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8454 // Make tooltip appear sooner.
8455 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8456 // I've performed some tests and it seems the longest possible life time
8457 // of tooltip is 30 seconds.
8458 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8459 /*
8460 * HACK: force tooltip to appear, because it'll not appear until
8461 * first mouse move. D*mn M$
8462 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8463 */
8464 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8465 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8466 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008467}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008470delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008472 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473}
8474
8475 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008476BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008477 HWND hwnd UNUSED,
8478 UINT uMsg UNUSED,
8479 UINT_PTR idEvent UNUSED,
8480 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 POINT pt;
8483 RECT rect;
8484
8485 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8486 return;
8487
8488 GetCursorPos(&pt);
8489 if (WindowFromPoint(pt) != s_textArea)
8490 return;
8491
8492 ScreenToClient(s_textArea, &pt);
8493 GetClientRect(s_textArea, &rect);
8494 if (!PtInRect(&rect, pt))
8495 return;
8496
8497 if (LastActivity > 0
8498 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8499 && (cur_beval->showState != ShS_PENDING
8500 || abs(cur_beval->x - pt.x) > 3
8501 || abs(cur_beval->y - pt.y) > 3))
8502 {
8503 /* Pointer resting in one place long enough, it's time to show
8504 * the tooltip. */
8505 cur_beval->showState = ShS_PENDING;
8506 cur_beval->x = pt.x;
8507 cur_beval->y = pt.y;
8508
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008509 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
8511 if (cur_beval->msgCB != NULL)
8512 (*cur_beval->msgCB)(cur_beval, 0);
8513 }
8514}
8515
8516 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008517gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008519 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008521 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522}
8523
8524 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008525gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008527 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (beval == NULL)
8529 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008530 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008531 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008532 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533}
8534
8535 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008536gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008539
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008540 vim_free(beval->msg);
8541 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8542 if (beval->msg == NULL)
8543 {
8544 delete_tooltip(beval);
8545 beval->showState = ShS_NEUTRAL;
8546 return;
8547 }
8548
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008549 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (beval->showState == ShS_SHOWING)
8551 return;
8552 GetCursorPos(&pt);
8553 ScreenToClient(s_textArea, &pt);
8554
8555 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008557 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 gui_mch_disable_beval_area(cur_beval);
8559 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008560 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008562 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563}
8564
8565 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008566gui_mch_create_beval_area(
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008567 void *target UNUSED, /* ignored, always use s_textArea */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008568 char_u *mesg,
8569 void (*mesgCB)(BalloonEval *, int),
8570 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 /* partially stolen from gui_beval.c */
8573 BalloonEval *beval;
8574
8575 if (mesg != NULL && mesgCB != NULL)
8576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008577 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 return NULL;
8579 }
8580
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008581 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 if (beval != NULL)
8583 {
8584 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 beval->msg = mesg;
8588 beval->msgCB = mesgCB;
8589 beval->clientData = clientData;
8590
8591 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 cur_beval = beval;
8593
8594 if (p_beval)
8595 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 }
8597 return beval;
8598}
8599
8600 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008601Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8604 return;
8605
8606 if (cur_beval != NULL)
8607 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008608 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008610 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008611 // TRACE0("TTN_SHOW {{{");
8612 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008613 break;
8614 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008615 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 delete_tooltip(cur_beval);
8617 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008618 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619
8620 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008621 break;
8622 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008623 {
8624 /* if you get there then we have new common controls */
8625 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8626 info->lpszText = (LPSTR)info->lParam;
8627 info->uFlags |= TTF_DI_SETITEM;
8628 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008629 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008630 case TTN_GETDISPINFOW:
8631 {
8632 // if we get here then we have new common controls
8633 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8634 info->lpszText = (LPWSTR)info->lParam;
8635 info->uFlags |= TTF_DI_SETITEM;
8636 }
8637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639 }
8640}
8641
8642 static void
8643TrackUserActivity(UINT uMsg)
8644{
8645 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8646 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8647 LastActivity = GetTickCount();
8648}
8649
8650 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008651gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008653# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008654 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008655# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008656 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 vim_free(beval);
8658}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008659#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
8661#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8662/*
8663 * We have multiple signs to draw at the same location. Draw the
8664 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8665 */
8666 void
8667netbeans_draw_multisign_indicator(int row)
8668{
8669 int i;
8670 int y;
8671 int x;
8672
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008673 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008674 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008675
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 x = 0;
8677 y = TEXT_Y(row);
8678
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008679# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008680 if (IS_ENABLE_DIRECTX())
8681 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008682# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 for (i = 0; i < gui.char_height - 3; i++)
8685 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8686
8687 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8688 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8689 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8690 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8691 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8692 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8693 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8694}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008695#endif