blob: 1d4e0f74cb5c9ef9bdc2a183e7e2b38bbc1f2d60 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
61 /* parse string as rendering options. */
62 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100122 /* Deprecated. Simply ignore it. */
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
129 return OK; /* only checking the syntax of the value */
130
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200131 /* Enable DirectX/DirectWrite */
132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
182#include "version.h" /* used by dialog box routine for default title */
183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
187/* cproto fails on missing include files */
188#ifndef PROTO
189
190#ifndef __MINGW32__
191# include <shellapi.h>
192#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100193#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100194# include <commctrl.h>
195#endif
196#include <windowsx.h>
197
198#ifdef GLOBAL_IME
199# include "glbl_ime.h"
200#endif
201
202#endif /* PROTO */
203
204#ifdef FEAT_MENU
205# define MENUHINTS /* show menu hints in command line */
206#endif
207
208/* Some parameters for dialog boxes. All in pixels. */
209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
213#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
223#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
224
225#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200243# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100244# define _cdecl
245typedef int BOOL;
246typedef int BYTE;
247typedef int DWORD;
248typedef int WCHAR;
249typedef int ENUMLOGFONT;
250typedef int FINDREPLACE;
251typedef int HANDLE;
252typedef int HBITMAP;
253typedef int HBRUSH;
254typedef int HDROP;
255typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100256typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257typedef int LPARAM;
258typedef int LPCREATESTRUCT;
259typedef int LPCSTR;
260typedef int LPCTSTR;
261typedef int LPRECT;
262typedef int LPSTR;
263typedef int LPWINDOWPOS;
264typedef int LPWORD;
265typedef int LRESULT;
266typedef int HRESULT;
267# undef MSG
268typedef int MSG;
269typedef int NEWTEXTMETRIC;
270typedef int OSVERSIONINFO;
271typedef int PWORD;
272typedef int RECT;
273typedef int UINT;
274typedef int WORD;
275typedef int WPARAM;
276typedef int POINT;
277typedef void *HINSTANCE;
278typedef void *HMENU;
279typedef void *HWND;
280typedef void *HDC;
281typedef void VOID;
282typedef int LPNMHDR;
283typedef int LONG;
284typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200285typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200286typedef int COLORREF;
287typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288#endif
289
290#ifndef GET_X_LPARAM
291# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
292#endif
293
294static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100295static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100296static void clear_rect(RECT *rcp);
297
298static WORD s_dlgfntheight; /* height of the dialog font */
299static WORD s_dlgfntwidth; /* width of the dialog font */
300
301#ifdef FEAT_MENU
302static HMENU s_menuBar = NULL;
303#endif
304#ifdef FEAT_TEAROFF
305static void rebuild_tearoff(vimmenu_T *menu);
306static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
307#endif
308
309/* Flag that is set while processing a message that must not be interrupted by
310 * processing another message. */
311static int s_busy_processing = FALSE;
312
313static int destroying = FALSE; /* call DestroyWindow() ourselves */
314
315#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200316static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
317static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200319static int s_findrep_is_find; // TRUE for find dialog, FALSE
320 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321#endif
322
Bram Moolenaar85b11762016-02-27 18:13:23 +0100323#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324static
325#endif
326HWND s_hwnd = NULL;
327static HDC s_hdc = NULL;
328static HBRUSH s_brush = NULL;
329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
347static char_u *s_textfield; /* Used by dialogs to pass back strings */
348
349static int s_need_activate = FALSE;
350
351/* This variable is set when waiting for an event, which is the only moment
352 * scrollbar dragging can be done directly. It's not allowed while commands
353 * are executed, because it may move the cursor and that may cause unexpected
354 * problems (e.g., while ":s" is working).
355 */
356static int allow_scrollbar = FALSE;
357
358#ifdef GLOBAL_IME
359# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
360#else
361# define MyTranslateMessage(x) TranslateMessage(x)
362#endif
363
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100364#if defined(FEAT_DIRECTX)
365 static int
366directx_enabled(void)
367{
368 if (s_dwc != NULL)
369 return 1;
370 else if (s_directx_load_attempted)
371 return 0;
372 /* load DirectX */
373 DWrite_Init();
374 s_directx_load_attempted = 1;
375 s_dwc = DWriteContext_Open();
376 directx_binddc();
377 return s_dwc != NULL ? 1 : 0;
378}
379
380 static void
381directx_binddc(void)
382{
383 if (s_textArea != NULL)
384 {
385 RECT rect;
386 GetClientRect(s_textArea, &rect);
387 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
388 }
389}
390#endif
391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200392/* use of WindowProc depends on Global IME */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100393#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100394
395extern int current_font_height; /* this is in os_mswin.c */
396
397static struct
398{
399 UINT key_sym;
400 char_u vim_code0;
401 char_u vim_code1;
402} special_keys[] =
403{
404 {VK_UP, 'k', 'u'},
405 {VK_DOWN, 'k', 'd'},
406 {VK_LEFT, 'k', 'l'},
407 {VK_RIGHT, 'k', 'r'},
408
409 {VK_F1, 'k', '1'},
410 {VK_F2, 'k', '2'},
411 {VK_F3, 'k', '3'},
412 {VK_F4, 'k', '4'},
413 {VK_F5, 'k', '5'},
414 {VK_F6, 'k', '6'},
415 {VK_F7, 'k', '7'},
416 {VK_F8, 'k', '8'},
417 {VK_F9, 'k', '9'},
418 {VK_F10, 'k', ';'},
419
420 {VK_F11, 'F', '1'},
421 {VK_F12, 'F', '2'},
422 {VK_F13, 'F', '3'},
423 {VK_F14, 'F', '4'},
424 {VK_F15, 'F', '5'},
425 {VK_F16, 'F', '6'},
426 {VK_F17, 'F', '7'},
427 {VK_F18, 'F', '8'},
428 {VK_F19, 'F', '9'},
429 {VK_F20, 'F', 'A'},
430
431 {VK_F21, 'F', 'B'},
432#ifdef FEAT_NETBEANS_INTG
433 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
434#endif
435 {VK_F22, 'F', 'C'},
436 {VK_F23, 'F', 'D'},
437 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
438
439 {VK_HELP, '%', '1'},
440 {VK_BACK, 'k', 'b'},
441 {VK_INSERT, 'k', 'I'},
442 {VK_DELETE, 'k', 'D'},
443 {VK_HOME, 'k', 'h'},
444 {VK_END, '@', '7'},
445 {VK_PRIOR, 'k', 'P'},
446 {VK_NEXT, 'k', 'N'},
447 {VK_PRINT, '%', '9'},
448 {VK_ADD, 'K', '6'},
449 {VK_SUBTRACT, 'K', '7'},
450 {VK_DIVIDE, 'K', '8'},
451 {VK_MULTIPLY, 'K', '9'},
452 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
453 {VK_DECIMAL, 'K', 'B'},
454
455 {VK_NUMPAD0, 'K', 'C'},
456 {VK_NUMPAD1, 'K', 'D'},
457 {VK_NUMPAD2, 'K', 'E'},
458 {VK_NUMPAD3, 'K', 'F'},
459 {VK_NUMPAD4, 'K', 'G'},
460 {VK_NUMPAD5, 'K', 'H'},
461 {VK_NUMPAD6, 'K', 'I'},
462 {VK_NUMPAD7, 'K', 'J'},
463 {VK_NUMPAD8, 'K', 'K'},
464 {VK_NUMPAD9, 'K', 'L'},
465
466 /* Keys that we want to be able to use any modifier with: */
467 {VK_SPACE, ' ', NUL},
468 {VK_TAB, TAB, NUL},
469 {VK_ESCAPE, ESC, NUL},
470 {NL, NL, NUL},
471 {CAR, CAR, NUL},
472
473 /* End of list marker: */
474 {0, 0, 0}
475};
476
477/* Local variables */
478static int s_button_pending = -1;
479
480/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
481 * so don't reset s_button_pending. */
482static int s_getting_focus = FALSE;
483
484static int s_x_pending;
485static int s_y_pending;
486static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200487static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100488static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200489static int dead_key = 0; // 0: no dead key, 1: dead key pressed
490static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
491 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100493#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494/* balloon-eval WM_NOTIFY_HANDLER */
495static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
496static void TrackUserActivity(UINT uMsg);
497#endif
498
499/*
500 * For control IME.
501 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100502 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100503 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100505/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
506static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100507#endif
508#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100509/* holds LOGFONTW for 'guifont' always. */
510static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511#endif
512
513#ifdef FEAT_MBYTE_IME
514static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
515#endif
516
517#if defined(FEAT_BROWSE)
518static char_u *convert_filter(char_u *s);
519#endif
520
521#ifdef DEBUG_PRINT_ERROR
522/*
523 * Print out the last Windows error message
524 */
525 static void
526print_windows_error(void)
527{
528 LPVOID lpMsgBuf;
529
530 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
531 NULL, GetLastError(),
532 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
533 (LPTSTR) &lpMsgBuf, 0, NULL);
534 TRACE1("Error: %s\n", lpMsgBuf);
535 LocalFree(lpMsgBuf);
536}
537#endif
538
539/*
540 * Cursor blink functions.
541 *
542 * This is a simple state machine:
543 * BLINK_NONE not blinking at all
544 * BLINK_OFF blinking, cursor is not shown
545 * BLINK_ON blinking, cursor is shown
546 */
547
548#define BLINK_NONE 0
549#define BLINK_OFF 1
550#define BLINK_ON 2
551
552static int blink_state = BLINK_NONE;
553static long_u blink_waittime = 700;
554static long_u blink_ontime = 400;
555static long_u blink_offtime = 250;
556static UINT blink_timer = 0;
557
Bram Moolenaar703a8042016-06-04 16:24:32 +0200558 int
559gui_mch_is_blinking(void)
560{
561 return blink_state != BLINK_NONE;
562}
563
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200564 int
565gui_mch_is_blink_off(void)
566{
567 return blink_state == BLINK_OFF;
568}
569
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100570 void
571gui_mch_set_blinking(long wait, long on, long off)
572{
573 blink_waittime = wait;
574 blink_ontime = on;
575 blink_offtime = off;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 static VOID CALLBACK
579_OnBlinkTimer(
580 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100581 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100583 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100584{
585 MSG msg;
586
587 /*
588 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
589 */
590
591 KillTimer(NULL, idEvent);
592
593 /* Eat spurious WM_TIMER messages */
594 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
595 ;
596
597 if (blink_state == BLINK_ON)
598 {
599 gui_undraw_cursor();
600 blink_state = BLINK_OFF;
601 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
602 (TIMERPROC)_OnBlinkTimer);
603 }
604 else
605 {
606 gui_update_cursor(TRUE, FALSE);
607 blink_state = BLINK_ON;
608 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100609 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100610 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100611 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612}
613
614 static void
615gui_mswin_rm_blink_timer(void)
616{
617 MSG msg;
618
619 if (blink_timer != 0)
620 {
621 KillTimer(NULL, blink_timer);
622 /* Eat spurious WM_TIMER messages */
623 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
624 ;
625 blink_timer = 0;
626 }
627}
628
629/*
630 * Stop the cursor blinking. Show the cursor if it wasn't shown.
631 */
632 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100634{
635 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100636 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100637 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100639 gui_mch_flush();
640 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 blink_state = BLINK_NONE;
642}
643
644/*
645 * Start the cursor blinking. If it was already blinking, this restarts the
646 * waiting time and shows the cursor.
647 */
648 void
649gui_mch_start_blink(void)
650{
651 gui_mswin_rm_blink_timer();
652
653 /* Only switch blinking on if none of the times is zero */
654 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
655 {
656 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
657 (TIMERPROC)_OnBlinkTimer);
658 blink_state = BLINK_ON;
659 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100660 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661 }
662}
663
664/*
665 * Call-back routines.
666 */
667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 static VOID CALLBACK
669_OnTimer(
670 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100671 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100673 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674{
675 MSG msg;
676
677 /*
678 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
679 */
680 KillTimer(NULL, idEvent);
681 s_timed_out = TRUE;
682
683 /* Eat spurious WM_TIMER messages */
684 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
685 ;
686 if (idEvent == s_wait_timer)
687 s_wait_timer = 0;
688}
689
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 static void
691_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100692 HWND hwnd UNUSED,
693 UINT ch UNUSED,
694 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695{
696 dead_key = 1;
697}
698
699/*
700 * Convert Unicode character "ch" to bytes in "string[slen]".
701 * When "had_alt" is TRUE the ALT key was included in "ch".
702 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200703 * Because the Windows API uses UTF-16, we have to deal with surrogate
704 * pairs; this is where we choose to deal with them: if "ch" is a high
705 * surrogate, it will be stored, and the length returned will be zero; the next
706 * char_to_string call will then include the high surrogate, decoding the pair
707 * of UTF-16 code units to a single Unicode code point, presuming it is the
708 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 */
710 static int
711char_to_string(int ch, char_u *string, int slen, int had_alt)
712{
713 int len;
714 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200716 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200718 if (surrogate_pending_ch != 0)
719 {
720 /* We don't guarantee ch is a low surrogate to match the high surrogate
721 * we already have; it should be, but if it isn't, tough luck. */
722 wstring[0] = surrogate_pending_ch;
723 wstring[1] = ch;
724 surrogate_pending_ch = 0;
725 len = 2;
726 }
727 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
728 {
729 /* We don't have the entire code point yet, only the first UTF-16 code
730 * unit; so just remember it and use it in the next call. */
731 surrogate_pending_ch = ch;
732 return 0;
733 }
734 else
735 {
736 wstring[0] = ch;
737 len = 1;
738 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200739
740 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
741 * "enc_codepage" is non-zero use the standard Win32 function,
742 * otherwise use our own conversion function (e.g., for UTF-8). */
743 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200745 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
746 (LPSTR)string, slen, 0, NULL);
747 /* If we had included the ALT key into the character but now the
748 * upper bit is no longer set, that probably means the conversion
749 * failed. Convert the original character and set the upper bit
750 * afterwards. */
751 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200753 wstring[0] = ch & 0x7f;
754 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
755 (LPSTR)string, slen, 0, NULL);
756 if (len == 1) /* safety check */
757 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758 }
759 }
760 else
761 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200762 ws = utf16_to_enc(wstring, &len);
763 if (ws == NULL)
764 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 else
766 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767 if (len > slen) /* just in case */
768 len = slen;
769 mch_memmove(string, ws, len);
770 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773
774 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 {
776 string[0] = ch;
777 len = 1;
778 }
779
780 for (i = 0; i < len; ++i)
781 if (string[i] == CSI && len <= slen - 2)
782 {
783 /* Insert CSI as K_CSI. */
784 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
785 string[++i] = KS_EXTRA;
786 string[++i] = (int)KE_CSI;
787 len += 2;
788 }
789
790 return len;
791}
792
793/*
794 * Key hit, add it to the input buffer.
795 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 static void
797_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100798 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801{
802 char_u string[40];
803 int len = 0;
804
805 dead_key = 0;
806
807 len = char_to_string(ch, string, 40, FALSE);
808 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
809 {
810 trash_input_buf();
811 got_int = TRUE;
812 }
813
814 add_to_input_buf(string, len);
815}
816
817/*
818 * Alt-Key hit, add it to the input buffer.
819 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 static void
821_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100822 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100823 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825{
826 char_u string[40]; /* Enough for multibyte character */
827 int len;
828 int modifiers;
829 int ch = cch; /* special keys are negative */
830
831 dead_key = 0;
832
833 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
834
835 /* OK, we have a character key (given by ch) which was entered with the
836 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
837 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
838 * CAPSLOCK is pressed) at this point.
839 */
840 modifiers = MOD_MASK_ALT;
841 if (GetKeyState(VK_SHIFT) & 0x8000)
842 modifiers |= MOD_MASK_SHIFT;
843 if (GetKeyState(VK_CONTROL) & 0x8000)
844 modifiers |= MOD_MASK_CTRL;
845
846 ch = simplify_key(ch, &modifiers);
847 /* remove the SHIFT modifier for keys where it's already included, e.g.,
848 * '(' and '*' */
849 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
850 modifiers &= ~MOD_MASK_SHIFT;
851
852 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
853 ch = extract_modifiers(ch, &modifiers);
854 if (ch == CSI)
855 ch = K_CSI;
856
857 len = 0;
858 if (modifiers)
859 {
860 string[len++] = CSI;
861 string[len++] = KS_MODIFIER;
862 string[len++] = modifiers;
863 }
864
865 if (IS_SPECIAL((int)ch))
866 {
867 string[len++] = CSI;
868 string[len++] = K_SECOND((int)ch);
869 string[len++] = K_THIRD((int)ch);
870 }
871 else
872 {
873 /* Although the documentation isn't clear about it, we assume "ch" is
874 * a Unicode character. */
875 len += char_to_string(ch, string + len, 40 - len, TRUE);
876 }
877
878 add_to_input_buf(string, len);
879}
880
881 static void
882_OnMouseEvent(
883 int button,
884 int x,
885 int y,
886 int repeated_click,
887 UINT keyFlags)
888{
889 int vim_modifiers = 0x0;
890
891 s_getting_focus = FALSE;
892
893 if (keyFlags & MK_SHIFT)
894 vim_modifiers |= MOUSE_SHIFT;
895 if (keyFlags & MK_CONTROL)
896 vim_modifiers |= MOUSE_CTRL;
897 if (GetKeyState(VK_MENU) & 0x8000)
898 vim_modifiers |= MOUSE_ALT;
899
900 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
901}
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 static void
904_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905 HWND hwnd UNUSED,
906 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 int x,
908 int y,
909 UINT keyFlags)
910{
911 static LONG s_prevTime = 0;
912
913 LONG currentTime = GetMessageTime();
914 int button = -1;
915 int repeated_click;
916
917 /* Give main window the focus: this is so the cursor isn't hollow. */
918 (void)SetFocus(s_hwnd);
919
920 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
921 button = MOUSE_LEFT;
922 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
923 button = MOUSE_MIDDLE;
924 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
925 button = MOUSE_RIGHT;
926 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
927 {
928#ifndef GET_XBUTTON_WPARAM
929# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
930#endif
931 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
932 }
933 else if (s_uMsg == WM_CAPTURECHANGED)
934 {
935 /* on W95/NT4, somehow you get in here with an odd Msg
936 * if you press one button while holding down the other..*/
937 if (s_button_pending == MOUSE_LEFT)
938 button = MOUSE_RIGHT;
939 else
940 button = MOUSE_LEFT;
941 }
942 if (button >= 0)
943 {
944 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
945
946 /*
947 * Holding down the left and right buttons simulates pushing the middle
948 * button.
949 */
950 if (repeated_click
951 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
952 || (button == MOUSE_RIGHT
953 && s_button_pending == MOUSE_LEFT)))
954 {
955 /*
956 * Hmm, gui.c will ignore more than one button down at a time, so
957 * pretend we let go of it first.
958 */
959 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
960 button = MOUSE_MIDDLE;
961 repeated_click = FALSE;
962 s_button_pending = -1;
963 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
964 }
965 else if ((repeated_click)
966 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
967 {
968 if (s_button_pending > -1)
969 {
970 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
971 s_button_pending = -1;
972 }
973 /* TRACE("Button down at x %d, y %d\n", x, y); */
974 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
975 }
976 else
977 {
978 /*
979 * If this is the first press (i.e. not a multiple click) don't
980 * action immediately, but store and wait for:
981 * i) button-up
982 * ii) mouse move
983 * iii) another button press
984 * before using it.
985 * This enables us to make left+right simulate middle button,
986 * without left or right being actioned first. The side-effect is
987 * that if you click and hold the mouse without dragging, the
988 * cursor doesn't move until you release the button. In practice
989 * this is hardly a problem.
990 */
991 s_button_pending = button;
992 s_x_pending = x;
993 s_y_pending = y;
994 s_kFlags_pending = keyFlags;
995 }
996
997 s_prevTime = currentTime;
998 }
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001004 int x,
1005 int y,
1006 UINT keyFlags)
1007{
1008 int button;
1009
1010 s_getting_focus = FALSE;
1011 if (s_button_pending > -1)
1012 {
1013 /* Delayed action for mouse down event */
1014 _OnMouseEvent(s_button_pending, s_x_pending,
1015 s_y_pending, FALSE, s_kFlags_pending);
1016 s_button_pending = -1;
1017 }
1018 if (s_uMsg == WM_MOUSEMOVE)
1019 {
1020 /*
1021 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1022 * down.
1023 */
1024 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1025 | MK_XBUTTON1 | MK_XBUTTON2)))
1026 {
1027 gui_mouse_moved(x, y);
1028 return;
1029 }
1030
1031 /*
1032 * While button is down, keep grabbing mouse move events when
1033 * the mouse goes outside the window
1034 */
1035 SetCapture(s_textArea);
1036 button = MOUSE_DRAG;
1037 /* TRACE(" move at x %d, y %d\n", x, y); */
1038 }
1039 else
1040 {
1041 ReleaseCapture();
1042 button = MOUSE_RELEASE;
1043 /* TRACE(" up at x %d, y %d\n", x, y); */
1044 }
1045
1046 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1047}
1048
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001049 static void
1050_OnSizeTextArea(
1051 HWND hwnd UNUSED,
1052 UINT state UNUSED,
1053 int cx UNUSED,
1054 int cy UNUSED)
1055{
1056#if defined(FEAT_DIRECTX)
1057 if (IS_ENABLE_DIRECTX())
1058 directx_binddc();
1059#endif
1060}
1061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062#ifdef FEAT_MENU
1063/*
1064 * Find the vimmenu_T with the given id
1065 */
1066 static vimmenu_T *
1067gui_mswin_find_menu(
1068 vimmenu_T *pMenu,
1069 int id)
1070{
1071 vimmenu_T *pChildMenu;
1072
1073 while (pMenu)
1074 {
1075 if (pMenu->id == (UINT)id)
1076 break;
1077 if (pMenu->children != NULL)
1078 {
1079 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1080 if (pChildMenu)
1081 {
1082 pMenu = pChildMenu;
1083 break;
1084 }
1085 }
1086 pMenu = pMenu->next;
1087 }
1088 return pMenu;
1089}
1090
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091 static void
1092_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwndCtl UNUSED,
1096 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097{
1098 vimmenu_T *pMenu;
1099
1100 pMenu = gui_mswin_find_menu(root_menu, id);
1101 if (pMenu)
1102 gui_menu_cb(pMenu);
1103}
1104#endif
1105
1106#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1117 /* Give main window the focus back. */
1118 (void)SetFocus(s_hwnd);
1119
1120 if (s_findrep_struct.Flags & FR_FINDNEXT)
1121 {
1122 flags = FRD_FINDNEXT;
1123
1124 /* Give main window the focus back: this is so the cursor isn't
1125 * hollow. */
1126 (void)SetFocus(s_hwnd);
1127 }
1128 else if (s_findrep_struct.Flags & FR_REPLACE)
1129 {
1130 flags = FRD_REPLACE;
1131
1132 /* Give main window the focus back: this is so the cursor isn't
1133 * hollow. */
1134 (void)SetFocus(s_hwnd);
1135 }
1136 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1137 {
1138 flags = FRD_REPLACEALL;
1139 }
1140
1141 if (flags != 0)
1142 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001143 char_u *p, *q;
1144
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 /* Call the generic GUI function to do the actual work. */
1146 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1147 flags |= FRD_WHOLE_WORD;
1148 if (s_findrep_struct.Flags & FR_MATCHCASE)
1149 flags |= FRD_MATCH_CASE;
1150 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001151 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1152 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1153 if (p != NULL && q != NULL)
1154 gui_do_findrepl(flags, p, q, down);
1155 vim_free(p);
1156 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 }
1158}
1159#endif
1160
1161 static void
1162HandleMouseHide(UINT uMsg, LPARAM lParam)
1163{
1164 static LPARAM last_lParam = 0L;
1165
1166 /* We sometimes get a mousemove when the mouse didn't move... */
1167 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1168 {
1169 if (lParam == last_lParam)
1170 return;
1171 last_lParam = lParam;
1172 }
1173
1174 /* Handle specially, to centralise coding. We need to be sure we catch all
1175 * possible events which should cause us to restore the cursor (as it is a
1176 * shared resource, we take full responsibility for it).
1177 */
1178 switch (uMsg)
1179 {
1180 case WM_KEYUP:
1181 case WM_CHAR:
1182 /*
1183 * blank out the pointer if necessary
1184 */
1185 if (p_mh)
1186 gui_mch_mousehide(TRUE);
1187 break;
1188
1189 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1190 case WM_SYSCHAR:
1191 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1192 case WM_LBUTTONDOWN:
1193 case WM_LBUTTONUP:
1194 case WM_MBUTTONDOWN:
1195 case WM_MBUTTONUP:
1196 case WM_RBUTTONDOWN:
1197 case WM_RBUTTONUP:
1198 case WM_XBUTTONDOWN:
1199 case WM_XBUTTONUP:
1200 case WM_NCMOUSEMOVE:
1201 case WM_NCLBUTTONDOWN:
1202 case WM_NCLBUTTONUP:
1203 case WM_NCMBUTTONDOWN:
1204 case WM_NCMBUTTONUP:
1205 case WM_NCRBUTTONDOWN:
1206 case WM_NCRBUTTONUP:
1207 case WM_KILLFOCUS:
1208 /*
1209 * if the pointer is currently hidden, then we should show it.
1210 */
1211 gui_mch_mousehide(FALSE);
1212 break;
1213 }
1214}
1215
1216 static LRESULT CALLBACK
1217_TextAreaWndProc(
1218 HWND hwnd,
1219 UINT uMsg,
1220 WPARAM wParam,
1221 LPARAM lParam)
1222{
1223 /*
1224 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1225 hwnd, uMsg, wParam, lParam);
1226 */
1227
1228 HandleMouseHide(uMsg, lParam);
1229
1230 s_uMsg = uMsg;
1231 s_wParam = wParam;
1232 s_lParam = lParam;
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 TrackUserActivity(uMsg);
1236#endif
1237
1238 switch (uMsg)
1239 {
1240 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1248 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001254 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1258 return TRUE;
1259#endif
1260 default:
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263}
1264
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001267#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269 LRESULT WINAPI
1270vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1271{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001272#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001274#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001275 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001276#endif
1277}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278
1279/*
1280 * Called when the foreground or background color has been changed.
1281 */
1282 void
1283gui_mch_new_colors(void)
1284{
1285 /* nothing to do? */
1286}
1287
1288/*
1289 * Set the colors to their default values.
1290 */
1291 void
1292gui_mch_def_colors(void)
1293{
1294 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1295 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1296 gui.def_norm_pixel = gui.norm_pixel;
1297 gui.def_back_pixel = gui.back_pixel;
1298}
1299
1300/*
1301 * Open the GUI window which was created by a call to gui_mch_init().
1302 */
1303 int
1304gui_mch_open(void)
1305{
1306#ifndef SW_SHOWDEFAULT
1307# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1308#endif
1309 /* Actually open the window, if not already visible
1310 * (may be done already in gui_mch_set_shellsize) */
1311 if (!IsWindowVisible(s_hwnd))
1312 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1313
1314#ifdef MSWIN_FIND_REPLACE
1315 /* Init replace string here, so that we keep it when re-opening the
1316 * dialog. */
1317 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1318#endif
1319
1320 return OK;
1321}
1322
1323/*
1324 * Get the position of the top left corner of the window.
1325 */
1326 int
1327gui_mch_get_winpos(int *x, int *y)
1328{
1329 RECT rect;
1330
1331 GetWindowRect(s_hwnd, &rect);
1332 *x = rect.left;
1333 *y = rect.top;
1334 return OK;
1335}
1336
1337/*
1338 * Set the position of the top left corner of the window to the given
1339 * coordinates.
1340 */
1341 void
1342gui_mch_set_winpos(int x, int y)
1343{
1344 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1345 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1346}
1347 void
1348gui_mch_set_text_area_pos(int x, int y, int w, int h)
1349{
1350 static int oldx = 0;
1351 static int oldy = 0;
1352
1353 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1354
1355#ifdef FEAT_TOOLBAR
1356 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1357 SendMessage(s_toolbarhwnd, WM_SIZE,
1358 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1359#endif
1360#if defined(FEAT_GUI_TABLINE)
1361 if (showing_tabline)
1362 {
1363 int top = 0;
1364 RECT rect;
1365
1366# ifdef FEAT_TOOLBAR
1367 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1368 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1369# endif
1370 GetClientRect(s_hwnd, &rect);
1371 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1372 }
1373#endif
1374
1375 /* When side scroll bar is unshown, the size of window will change.
1376 * then, the text area move left or right. thus client rect should be
1377 * forcedly redrawn. (Yasuhiro Matsumoto) */
1378 if (oldx != x || oldy != y)
1379 {
1380 InvalidateRect(s_hwnd, NULL, FALSE);
1381 oldx = x;
1382 oldy = y;
1383 }
1384}
1385
1386
1387/*
1388 * Scrollbar stuff:
1389 */
1390
1391 void
1392gui_mch_enable_scrollbar(
1393 scrollbar_T *sb,
1394 int flag)
1395{
1396 ShowScrollBar(sb->id, SB_CTL, flag);
1397
1398 /* TODO: When the window is maximized, the size of the window stays the
1399 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1400 * NT 4.0 it's not... */
1401}
1402
1403 void
1404gui_mch_set_scrollbar_pos(
1405 scrollbar_T *sb,
1406 int x,
1407 int y,
1408 int w,
1409 int h)
1410{
1411 SetWindowPos(sb->id, NULL, x, y, w, h,
1412 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1413}
1414
1415 void
1416gui_mch_create_scrollbar(
1417 scrollbar_T *sb,
1418 int orient) /* SBAR_VERT or SBAR_HORIZ */
1419{
1420 sb->id = CreateWindow(
1421 "SCROLLBAR", "Scrollbar",
1422 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1423 10, /* Any value will do for now */
1424 10, /* Any value will do for now */
1425 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001426 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001427}
1428
1429/*
1430 * Find the scrollbar with the given hwnd.
1431 */
1432 static scrollbar_T *
1433gui_mswin_find_scrollbar(HWND hwnd)
1434{
1435 win_T *wp;
1436
1437 if (gui.bottom_sbar.id == hwnd)
1438 return &gui.bottom_sbar;
1439 FOR_ALL_WINDOWS(wp)
1440 {
1441 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1442 return &wp->w_scrollbars[SBAR_LEFT];
1443 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1444 return &wp->w_scrollbars[SBAR_RIGHT];
1445 }
1446 return NULL;
1447}
1448
1449/*
1450 * Get the character size of a font.
1451 */
1452 static void
1453GetFontSize(GuiFont font)
1454{
1455 HWND hwnd = GetDesktopWindow();
1456 HDC hdc = GetWindowDC(hwnd);
1457 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1458 TEXTMETRIC tm;
1459
1460 GetTextMetrics(hdc, &tm);
1461 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1462
1463 gui.char_height = tm.tmHeight + p_linespace;
1464
1465 SelectFont(hdc, hfntOld);
1466
1467 ReleaseDC(hwnd, hdc);
1468}
1469
1470/*
1471 * Adjust gui.char_height (after 'linespace' was changed).
1472 */
1473 int
1474gui_mch_adjust_charheight(void)
1475{
1476 GetFontSize(gui.norm_font);
1477 return OK;
1478}
1479
1480 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001481get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001482{
1483 HFONT font = NULL;
1484
1485 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001486 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001487
1488 if (font == NULL)
1489 return NOFONT;
1490
1491 return (GuiFont)font;
1492}
1493
1494 static int
1495pixels_to_points(int pixels, int vertical)
1496{
1497 int points;
1498 HWND hwnd;
1499 HDC hdc;
1500
1501 hwnd = GetDesktopWindow();
1502 hdc = GetWindowDC(hwnd);
1503
1504 points = MulDiv(pixels, 72,
1505 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1506
1507 ReleaseDC(hwnd, hdc);
1508
1509 return points;
1510}
1511
1512 GuiFont
1513gui_mch_get_font(
1514 char_u *name,
1515 int giveErrorIfMissing)
1516{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001517 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001518 GuiFont font = NOFONT;
1519
1520 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1521 font = get_font_handle(&lf);
1522 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001523 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001524 return font;
1525}
1526
1527#if defined(FEAT_EVAL) || defined(PROTO)
1528/*
1529 * Return the name of font "font" in allocated memory.
1530 * Don't know how to get the actual name, thus use the provided name.
1531 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001533gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001534{
1535 if (name == NULL)
1536 return NULL;
1537 return vim_strsave(name);
1538}
1539#endif
1540
1541 void
1542gui_mch_free_font(GuiFont font)
1543{
1544 if (font)
1545 DeleteObject((HFONT)font);
1546}
1547
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001548/*
1549 * Return the Pixel value (color) for the given color name.
1550 * Return INVALCOLOR for error.
1551 */
1552 guicolor_T
1553gui_mch_get_color(char_u *name)
1554{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001555 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001556
1557 typedef struct SysColorTable
1558 {
1559 char *name;
1560 int color;
1561 } SysColorTable;
1562
1563 static SysColorTable sys_table[] =
1564 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001565 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1566 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001567#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1569#endif
1570 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1571 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1572 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1573 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1574 {"SYS_DESKTOP", COLOR_DESKTOP},
1575 {"SYS_INFOBK", COLOR_INFOBK},
1576 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1577 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001578 {"SYS_BTNFACE", COLOR_BTNFACE},
1579 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1580 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1581 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1582 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1583 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1584 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1585 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1586 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1587 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1588 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1589 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1590 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1591 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1592 {"SYS_MENU", COLOR_MENU},
1593 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1594 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1595 {"SYS_WINDOW", COLOR_WINDOW},
1596 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1597 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1598 };
1599
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001600 /*
1601 * Try to look up a system colour.
1602 */
1603 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1604 if (STRICMP(name, sys_table[i].name) == 0)
1605 return GetSysColor(sys_table[i].color);
1606
Bram Moolenaarab302212016-04-26 20:59:29 +02001607 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001608}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001609
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001610 guicolor_T
1611gui_mch_get_rgb_color(int r, int g, int b)
1612{
1613 return gui_get_rgb_color_cmn(r, g, b);
1614}
1615
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001616/*
1617 * Return OK if the key with the termcap name "name" is supported.
1618 */
1619 int
1620gui_mch_haskey(char_u *name)
1621{
1622 int i;
1623
1624 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1625 if (name[0] == special_keys[i].vim_code0 &&
1626 name[1] == special_keys[i].vim_code1)
1627 return OK;
1628 return FAIL;
1629}
1630
1631 void
1632gui_mch_beep(void)
1633{
1634 MessageBeep(MB_OK);
1635}
1636/*
1637 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1638 */
1639 void
1640gui_mch_invert_rectangle(
1641 int r,
1642 int c,
1643 int nr,
1644 int nc)
1645{
1646 RECT rc;
1647
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001648#if defined(FEAT_DIRECTX)
1649 if (IS_ENABLE_DIRECTX())
1650 DWriteContext_Flush(s_dwc);
1651#endif
1652
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 /*
1654 * Note: InvertRect() excludes right and bottom of rectangle.
1655 */
1656 rc.left = FILL_X(c);
1657 rc.top = FILL_Y(r);
1658 rc.right = rc.left + nc * gui.char_width;
1659 rc.bottom = rc.top + nr * gui.char_height;
1660 InvertRect(s_hdc, &rc);
1661}
1662
1663/*
1664 * Iconify the GUI window.
1665 */
1666 void
1667gui_mch_iconify(void)
1668{
1669 ShowWindow(s_hwnd, SW_MINIMIZE);
1670}
1671
1672/*
1673 * Draw a cursor without focus.
1674 */
1675 void
1676gui_mch_draw_hollow_cursor(guicolor_T color)
1677{
1678 HBRUSH hbr;
1679 RECT rc;
1680
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001681#if defined(FEAT_DIRECTX)
1682 if (IS_ENABLE_DIRECTX())
1683 DWriteContext_Flush(s_dwc);
1684#endif
1685
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001686 /*
1687 * Note: FrameRect() excludes right and bottom of rectangle.
1688 */
1689 rc.left = FILL_X(gui.col);
1690 rc.top = FILL_Y(gui.row);
1691 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 if (mb_lefthalve(gui.row, gui.col))
1693 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001694 rc.bottom = rc.top + gui.char_height;
1695 hbr = CreateSolidBrush(color);
1696 FrameRect(s_hdc, &rc, hbr);
1697 DeleteBrush(hbr);
1698}
1699/*
1700 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1701 * color "color".
1702 */
1703 void
1704gui_mch_draw_part_cursor(
1705 int w,
1706 int h,
1707 guicolor_T color)
1708{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001709 RECT rc;
1710
1711 /*
1712 * Note: FillRect() excludes right and bottom of rectangle.
1713 */
1714 rc.left =
1715#ifdef FEAT_RIGHTLEFT
1716 /* vertical line should be on the right of current point */
1717 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1718#endif
1719 FILL_X(gui.col);
1720 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1721 rc.right = rc.left + w;
1722 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001723
Bram Moolenaar92467d32017-12-05 13:22:16 +01001724 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001725}
1726
1727
1728/*
1729 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1730 * dead key's nominal character and re-post the original message.
1731 */
1732 static void
1733outputDeadKey_rePost(MSG originalMsg)
1734{
1735 static MSG deadCharExpel;
1736
1737 if (!dead_key)
1738 return;
1739
1740 dead_key = 0;
1741
1742 /* Make Windows generate the dead key's character */
1743 deadCharExpel.message = originalMsg.message;
1744 deadCharExpel.hwnd = originalMsg.hwnd;
1745 deadCharExpel.wParam = VK_SPACE;
1746
1747 MyTranslateMessage(&deadCharExpel);
1748
1749 /* re-generate the current character free of the dead char influence */
1750 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1751 originalMsg.lParam);
1752}
1753
1754
1755/*
1756 * Process a single Windows message.
1757 * If one is not available we hang until one is.
1758 */
1759 static void
1760process_message(void)
1761{
1762 MSG msg;
1763 UINT vk = 0; /* Virtual key */
1764 char_u string[40];
1765 int i;
1766 int modifiers = 0;
1767 int key;
1768#ifdef FEAT_MENU
1769 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1770#endif
1771
1772 pGetMessage(&msg, NULL, 0, 0);
1773
1774#ifdef FEAT_OLE
1775 /* Look after OLE Automation commands */
1776 if (msg.message == WM_OLE)
1777 {
1778 char_u *str = (char_u *)msg.lParam;
1779 if (str == NULL || *str == NUL)
1780 {
1781 /* Message can't be ours, forward it. Fixes problem with Ultramon
1782 * 3.0.4 */
1783 pDispatchMessage(&msg);
1784 }
1785 else
1786 {
1787 add_to_input_buf(str, (int)STRLEN(str));
1788 vim_free(str); /* was allocated in CVim::SendKeys() */
1789 }
1790 return;
1791 }
1792#endif
1793
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001794#ifdef MSWIN_FIND_REPLACE
1795 /* Don't process messages used by the dialog */
1796 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1797 {
1798 HandleMouseHide(msg.message, msg.lParam);
1799 return;
1800 }
1801#endif
1802
1803 /*
1804 * Check if it's a special key that we recognise. If not, call
1805 * TranslateMessage().
1806 */
1807 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1808 {
1809 vk = (int) msg.wParam;
1810
1811 /*
1812 * Handle dead keys in special conditions in other cases we let Windows
1813 * handle them and do not interfere.
1814 *
1815 * The dead_key flag must be reset on several occasions:
1816 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1817 * consumed at that point (This is when we let Windows combine the
1818 * dead character on its own)
1819 *
1820 * - Before doing something special such as regenerating keypresses to
1821 * expel the dead character as this could trigger an infinite loop if
1822 * for some reason MyTranslateMessage() do not trigger a call
1823 * immediately to _OnChar() (or _OnSysChar()).
1824 */
1825 if (dead_key)
1826 {
1827 /*
1828 * If a dead key was pressed and the user presses VK_SPACE,
1829 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1830 * with the dead char now, so do nothing special and let Windows
1831 * handle it.
1832 *
1833 * Note that VK_SPACE combines with the dead_key's character and
1834 * only one WM_CHAR will be generated by TranslateMessage(), in
1835 * the two other cases two WM_CHAR will be generated: the dead
1836 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1837 * user expects.
1838 */
1839 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1840 {
1841 dead_key = 0;
1842 MyTranslateMessage(&msg);
1843 return;
1844 }
1845 /* In modes where we are not typing, dead keys should behave
1846 * normally */
1847 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1848 {
1849 outputDeadKey_rePost(msg);
1850 return;
1851 }
1852 }
1853
1854 /* Check for CTRL-BREAK */
1855 if (vk == VK_CANCEL)
1856 {
1857 trash_input_buf();
1858 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001859 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001860 string[0] = Ctrl_C;
1861 add_to_input_buf(string, 1);
1862 }
1863
1864 for (i = 0; special_keys[i].key_sym != 0; i++)
1865 {
1866 /* ignore VK_SPACE when ALT key pressed: system menu */
1867 if (special_keys[i].key_sym == vk
1868 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1869 {
1870 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001871 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872 * is a key that would normally trigger the dead key nominal
1873 * character output (such as a NUMPAD printable character or
1874 * the TAB key, etc...).
1875 */
1876 if (dead_key && (special_keys[i].vim_code0 == 'K'
1877 || vk == VK_TAB || vk == CAR))
1878 {
1879 outputDeadKey_rePost(msg);
1880 return;
1881 }
1882
1883#ifdef FEAT_MENU
1884 /* Check for <F10>: Windows selects the menu. When <F10> is
1885 * mapped we want to use the mapping instead. */
1886 if (vk == VK_F10
1887 && gui.menu_is_active
1888 && check_map(k10, State, FALSE, TRUE, FALSE,
1889 NULL, NULL) == NULL)
1890 break;
1891#endif
1892 if (GetKeyState(VK_SHIFT) & 0x8000)
1893 modifiers |= MOD_MASK_SHIFT;
1894 /*
1895 * Don't use caps-lock as shift, because these are special keys
1896 * being considered here, and we only want letters to get
1897 * shifted -- webb
1898 */
1899 /*
1900 if (GetKeyState(VK_CAPITAL) & 0x0001)
1901 modifiers ^= MOD_MASK_SHIFT;
1902 */
1903 if (GetKeyState(VK_CONTROL) & 0x8000)
1904 modifiers |= MOD_MASK_CTRL;
1905 if (GetKeyState(VK_MENU) & 0x8000)
1906 modifiers |= MOD_MASK_ALT;
1907
1908 if (special_keys[i].vim_code1 == NUL)
1909 key = special_keys[i].vim_code0;
1910 else
1911 key = TO_SPECIAL(special_keys[i].vim_code0,
1912 special_keys[i].vim_code1);
1913 key = simplify_key(key, &modifiers);
1914 if (key == CSI)
1915 key = K_CSI;
1916
1917 if (modifiers)
1918 {
1919 string[0] = CSI;
1920 string[1] = KS_MODIFIER;
1921 string[2] = modifiers;
1922 add_to_input_buf(string, 3);
1923 }
1924
1925 if (IS_SPECIAL(key))
1926 {
1927 string[0] = CSI;
1928 string[1] = K_SECOND(key);
1929 string[2] = K_THIRD(key);
1930 add_to_input_buf(string, 3);
1931 }
1932 else
1933 {
1934 int len;
1935
1936 /* Handle "key" as a Unicode character. */
1937 len = char_to_string(key, string, 40, FALSE);
1938 add_to_input_buf(string, len);
1939 }
1940 break;
1941 }
1942 }
1943 if (special_keys[i].key_sym == 0)
1944 {
1945 /* Some keys need C-S- where they should only need C-.
1946 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1947 * system startup (Helmut Stiegler, 2003 Oct 3). */
1948 if (vk != 0xff
1949 && (GetKeyState(VK_CONTROL) & 0x8000)
1950 && !(GetKeyState(VK_SHIFT) & 0x8000)
1951 && !(GetKeyState(VK_MENU) & 0x8000))
1952 {
1953 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1954 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1955 {
1956 string[0] = Ctrl_HAT;
1957 add_to_input_buf(string, 1);
1958 }
1959 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1960 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1961 {
1962 string[0] = Ctrl__;
1963 add_to_input_buf(string, 1);
1964 }
1965 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1966 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1967 {
1968 string[0] = Ctrl_AT;
1969 add_to_input_buf(string, 1);
1970 }
1971 else
1972 MyTranslateMessage(&msg);
1973 }
1974 else
1975 MyTranslateMessage(&msg);
1976 }
1977 }
1978#ifdef FEAT_MBYTE_IME
1979 else if (msg.message == WM_IME_NOTIFY)
1980 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1981 else if (msg.message == WM_KEYUP && im_get_status())
1982 /* added for non-MS IME (Yasuhiro Matsumoto) */
1983 MyTranslateMessage(&msg);
1984#endif
1985#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1986/* GIME_TEST */
1987 else if (msg.message == WM_IME_STARTCOMPOSITION)
1988 {
1989 POINT point;
1990
1991 global_ime_set_font(&norm_logfont);
1992 point.x = FILL_X(gui.col);
1993 point.y = FILL_Y(gui.row);
1994 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1995 global_ime_set_position(&point);
1996 }
1997#endif
1998
1999#ifdef FEAT_MENU
2000 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2001 * mapped we need to stop it here to avoid strange effects (e.g., for the
2002 * key-up event) */
2003 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2004 NULL, NULL) == NULL)
2005#endif
2006 pDispatchMessage(&msg);
2007}
2008
2009/*
2010 * Catch up with any queued events. This may put keyboard input into the
2011 * input buffer, call resize call-backs, trigger timers etc. If there is
2012 * nothing in the event queue (& no timers pending), then we return
2013 * immediately.
2014 */
2015 void
2016gui_mch_update(void)
2017{
2018 MSG msg;
2019
2020 if (!s_busy_processing)
2021 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2022 && !vim_is_input_buf_full())
2023 process_message();
2024}
2025
Bram Moolenaar4231da42016-06-02 14:30:04 +02002026 static void
2027remove_any_timer(void)
2028{
2029 MSG msg;
2030
2031 if (s_wait_timer != 0 && !s_timed_out)
2032 {
2033 KillTimer(NULL, s_wait_timer);
2034
2035 /* Eat spurious WM_TIMER messages */
2036 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2037 ;
2038 s_wait_timer = 0;
2039 }
2040}
2041
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002042/*
2043 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2044 * from the keyboard.
2045 * wtime == -1 Wait forever.
2046 * wtime == 0 This should never happen.
2047 * wtime > 0 Wait wtime milliseconds for a character.
2048 * Returns OK if a character was found to be available within the given time,
2049 * or FAIL otherwise.
2050 */
2051 int
2052gui_mch_wait_for_chars(int wtime)
2053{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002054 int focus;
2055
2056 s_timed_out = FALSE;
2057
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002058 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002060 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002061 if (s_busy_processing)
2062 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002063
2064 // When called with "wtime" zero, just want one msec.
2065 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002066 (TIMERPROC)_OnTimer);
2067 }
2068
2069 allow_scrollbar = TRUE;
2070
2071 focus = gui.in_focus;
2072 while (!s_timed_out)
2073 {
2074 /* Stop or start blinking when focus changes */
2075 if (gui.in_focus != focus)
2076 {
2077 if (gui.in_focus)
2078 gui_mch_start_blink();
2079 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002080 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 focus = gui.in_focus;
2082 }
2083
2084 if (s_need_activate)
2085 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 s_need_activate = FALSE;
2088 }
2089
Bram Moolenaar4231da42016-06-02 14:30:04 +02002090#ifdef FEAT_TIMERS
2091 did_add_timer = FALSE;
2092#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002094 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002095 for (;;)
2096 {
2097 MSG msg;
2098
2099 parse_queued_messages();
2100
Bram Moolenaar62426e12017-08-13 15:37:58 +02002101 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2102 {
2103 process_message();
2104 break;
2105 }
2106 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2107 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002108 break;
2109 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002110#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111 /*
2112 * Don't use gui_mch_update() because then we will spin-lock until a
2113 * char arrives, instead we use GetMessage() to hang until an
2114 * event arrives. No need to check for input_buf_full because we are
2115 * returning as soon as it contains a single char -- webb
2116 */
2117 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002118#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002119
2120 if (input_available())
2121 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002122 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 allow_scrollbar = FALSE;
2124
2125 /* Clear pending mouse button, the release event may have been
2126 * taken by the dialog window. But don't do this when getting
2127 * focus, we need the mouse-up event then. */
2128 if (!s_getting_focus)
2129 s_button_pending = -1;
2130
2131 return OK;
2132 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002133
2134#ifdef FEAT_TIMERS
2135 if (did_add_timer)
2136 {
2137 /* Need to recompute the waiting time. */
2138 remove_any_timer();
2139 break;
2140 }
2141#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002142 }
2143 allow_scrollbar = FALSE;
2144 return FAIL;
2145}
2146
2147/*
2148 * Clear a rectangular region of the screen from text pos (row1, col1) to
2149 * (row2, col2) inclusive.
2150 */
2151 void
2152gui_mch_clear_block(
2153 int row1,
2154 int col1,
2155 int row2,
2156 int col2)
2157{
2158 RECT rc;
2159
2160 /*
2161 * Clear one extra pixel at the far right, for when bold characters have
2162 * spilled over to the window border.
2163 * Note: FillRect() excludes right and bottom of rectangle.
2164 */
2165 rc.left = FILL_X(col1);
2166 rc.top = FILL_Y(row1);
2167 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2168 rc.bottom = FILL_Y(row2 + 1);
2169 clear_rect(&rc);
2170}
2171
2172/*
2173 * Clear the whole text window.
2174 */
2175 void
2176gui_mch_clear_all(void)
2177{
2178 RECT rc;
2179
2180 rc.left = 0;
2181 rc.top = 0;
2182 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2183 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2184 clear_rect(&rc);
2185}
2186/*
2187 * Menu stuff.
2188 */
2189
2190 void
2191gui_mch_enable_menu(int flag)
2192{
2193#ifdef FEAT_MENU
2194 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2195#endif
2196}
2197
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002198 void
2199gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002200 int x UNUSED,
2201 int y UNUSED,
2202 int w UNUSED,
2203 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002204{
2205 /* It will be in the right place anyway */
2206}
2207
2208#if defined(FEAT_MENU) || defined(PROTO)
2209/*
2210 * Make menu item hidden or not hidden
2211 */
2212 void
2213gui_mch_menu_hidden(
2214 vimmenu_T *menu,
2215 int hidden)
2216{
2217 /*
2218 * This doesn't do what we want. Hmm, just grey the menu items for now.
2219 */
2220 /*
2221 if (hidden)
2222 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2223 else
2224 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2225 */
2226 gui_mch_menu_grey(menu, hidden);
2227}
2228
2229/*
2230 * This is called after setting all the menus to grey/hidden or not.
2231 */
2232 void
2233gui_mch_draw_menubar(void)
2234{
2235 DrawMenuBar(s_hwnd);
2236}
2237#endif /*FEAT_MENU*/
2238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002239/*
2240 * Return the RGB value of a pixel as a long.
2241 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002242 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002243gui_mch_get_rgb(guicolor_T pixel)
2244{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002245 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2246 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247}
2248
2249#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2250/* Convert pixels in X to dialog units */
2251 static WORD
2252PixelToDialogX(int numPixels)
2253{
2254 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2255}
2256
2257/* Convert pixels in Y to dialog units */
2258 static WORD
2259PixelToDialogY(int numPixels)
2260{
2261 return (WORD)((numPixels * 8) / s_dlgfntheight);
2262}
2263
2264/* Return the width in pixels of the given text in the given DC. */
2265 static int
2266GetTextWidth(HDC hdc, char_u *str, int len)
2267{
2268 SIZE size;
2269
2270 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2271 return size.cx;
2272}
2273
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002274/*
2275 * Return the width in pixels of the given text in the given DC, taking care
2276 * of 'encoding' to active codepage conversion.
2277 */
2278 static int
2279GetTextWidthEnc(HDC hdc, char_u *str, int len)
2280{
2281 SIZE size;
2282 WCHAR *wstr;
2283 int n;
2284 int wlen = len;
2285
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002286 wstr = enc_to_utf16(str, &wlen);
2287 if (wstr == NULL)
2288 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002289
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002290 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2291 vim_free(wstr);
2292 if (n)
2293 return size.cx;
2294 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002297static void get_work_area(RECT *spi_rect);
2298
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002299/*
2300 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002301 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2302 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 */
2304 static BOOL
2305CenterWindow(
2306 HWND hwndChild,
2307 HWND hwndParent)
2308{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002309 HMONITOR mon;
2310 MONITORINFO moninfo;
2311 RECT rChild, rParent, rScreen;
2312 int wChild, hChild, wParent, hParent;
2313 int xNew, yNew;
2314 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002315
2316 GetWindowRect(hwndChild, &rChild);
2317 wChild = rChild.right - rChild.left;
2318 hChild = rChild.bottom - rChild.top;
2319
2320 /* If Vim is minimized put the window in the middle of the screen. */
2321 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002322 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002323 else
2324 GetWindowRect(hwndParent, &rParent);
2325 wParent = rParent.right - rParent.left;
2326 hParent = rParent.bottom - rParent.top;
2327
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002328 moninfo.cbSize = sizeof(MONITORINFO);
2329 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2330 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002332 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002333 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002334 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002336 hdc = GetDC(hwndChild);
2337 rScreen.left = 0;
2338 rScreen.top = 0;
2339 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2340 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2341 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002342 }
2343
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002344 xNew = rParent.left + ((wParent - wChild) / 2);
2345 if (xNew < rScreen.left)
2346 xNew = rScreen.left;
2347 else if ((xNew + wChild) > rScreen.right)
2348 xNew = rScreen.right - wChild;
2349
2350 yNew = rParent.top + ((hParent - hChild) / 2);
2351 if (yNew < rScreen.top)
2352 yNew = rScreen.top;
2353 else if ((yNew + hChild) > rScreen.bottom)
2354 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355
2356 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2357 SWP_NOSIZE | SWP_NOZORDER);
2358}
2359#endif /* FEAT_GUI_DIALOG */
2360
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361#if defined(FEAT_TOOLBAR) || defined(PROTO)
2362 void
2363gui_mch_show_toolbar(int showit)
2364{
2365 if (s_toolbarhwnd == NULL)
2366 return;
2367
2368 if (showit)
2369 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002370# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002371 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002372# define TB_SETUNICODEFORMAT 0x2005
2373# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002374 // Enable unicode support
2375 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2376 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377 ShowWindow(s_toolbarhwnd, SW_SHOW);
2378 }
2379 else
2380 ShowWindow(s_toolbarhwnd, SW_HIDE);
2381}
2382
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002383/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002384#define TOOLBAR_BITMAP_COUNT 31
2385
2386#endif
2387
2388#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2389 static void
2390add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2391{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002392 WCHAR *wn;
2393 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002394
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002395 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002397 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002399 infow.cbSize = sizeof(infow);
2400 infow.fMask = MIIM_TYPE | MIIM_ID;
2401 infow.wID = item_id;
2402 infow.fType = MFT_STRING;
2403 infow.dwTypeData = wn;
2404 infow.cch = (UINT)wcslen(wn);
2405 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2406 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407}
2408
2409 static void
2410show_tabline_popup_menu(void)
2411{
2412 HMENU tab_pmenu;
2413 long rval;
2414 POINT pt;
2415
2416 /* When ignoring events don't show the menu. */
2417 if (hold_gui_events
2418# ifdef FEAT_CMDWIN
2419 || cmdwin_type != 0
2420# endif
2421 )
2422 return;
2423
2424 tab_pmenu = CreatePopupMenu();
2425 if (tab_pmenu == NULL)
2426 return;
2427
2428 if (first_tabpage->tp_next != NULL)
2429 add_tabline_popup_menu_entry(tab_pmenu,
2430 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2431 add_tabline_popup_menu_entry(tab_pmenu,
2432 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2433 add_tabline_popup_menu_entry(tab_pmenu,
2434 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2435
2436 GetCursorPos(&pt);
2437 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2438 NULL);
2439
2440 DestroyMenu(tab_pmenu);
2441
2442 /* Add the string cmd into input buffer */
2443 if (rval > 0)
2444 {
2445 TCHITTESTINFO htinfo;
2446 int idx;
2447
2448 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2449 return;
2450
2451 htinfo.pt.x = pt.x;
2452 htinfo.pt.y = pt.y;
2453 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2454 if (idx == -1)
2455 idx = 0;
2456 else
2457 idx += 1;
2458
2459 send_tabline_menu_event(idx, (int)rval);
2460 }
2461}
2462
2463/*
2464 * Show or hide the tabline.
2465 */
2466 void
2467gui_mch_show_tabline(int showit)
2468{
2469 if (s_tabhwnd == NULL)
2470 return;
2471
2472 if (!showit != !showing_tabline)
2473 {
2474 if (showit)
2475 ShowWindow(s_tabhwnd, SW_SHOW);
2476 else
2477 ShowWindow(s_tabhwnd, SW_HIDE);
2478 showing_tabline = showit;
2479 }
2480}
2481
2482/*
2483 * Return TRUE when tabline is displayed.
2484 */
2485 int
2486gui_mch_showing_tabline(void)
2487{
2488 return s_tabhwnd != NULL && showing_tabline;
2489}
2490
2491/*
2492 * Update the labels of the tabline.
2493 */
2494 void
2495gui_mch_update_tabline(void)
2496{
2497 tabpage_T *tp;
2498 TCITEM tie;
2499 int nr = 0;
2500 int curtabidx = 0;
2501 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503
2504 if (s_tabhwnd == NULL)
2505 return;
2506
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002507#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002508 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002509# define CCM_SETUNICODEFORMAT 0x2005
2510#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002511 // Enable unicode support
2512 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513
2514 tie.mask = TCIF_TEXT;
2515 tie.iImage = -1;
2516
2517 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2518 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2519
2520 /* Add a label for each tab page. They all contain the same text area. */
2521 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2522 {
2523 if (tp == curtab)
2524 curtabidx = nr;
2525
2526 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2527 {
2528 /* Add the tab */
2529 tie.pszText = "-Empty-";
2530 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2531 tabadded = 1;
2532 }
2533
2534 get_tabline_label(tp, FALSE);
2535 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002537 wstr = enc_to_utf16(NameBuff, NULL);
2538 if (wstr != NULL)
2539 {
2540 TCITEMW tiw;
2541
2542 tiw.mask = TCIF_TEXT;
2543 tiw.iImage = -1;
2544 tiw.pszText = wstr;
2545 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2546 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002547 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548 }
2549
2550 /* Remove any old labels. */
2551 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2552 TabCtrl_DeleteItem(s_tabhwnd, nr);
2553
2554 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2555 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2556
2557 /* Re-enable redraw and redraw. */
2558 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2559 RedrawWindow(s_tabhwnd, NULL, NULL,
2560 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2561
2562 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2563 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2564}
2565
2566/*
2567 * Set the current tab to "nr". First tab is 1.
2568 */
2569 void
2570gui_mch_set_curtab(int nr)
2571{
2572 if (s_tabhwnd == NULL)
2573 return;
2574
2575 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2576 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2577}
2578
2579#endif
2580
2581/*
2582 * ":simalt" command.
2583 */
2584 void
2585ex_simalt(exarg_T *eap)
2586{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002587 char_u *keys = eap->arg;
2588 int fill_typebuf = FALSE;
2589 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590
2591 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2592 while (*keys)
2593 {
2594 if (*keys == '~')
2595 *keys = ' '; /* for showing system menu */
2596 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2597 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002598 fill_typebuf = TRUE;
2599 }
2600 if (fill_typebuf)
2601 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002602 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002603 * processed. */
2604 key_name[0] = K_SPECIAL;
2605 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002606 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002607 key_name[3] = NUL;
2608 typebuf_was_filled = TRUE;
2609 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002610 }
2611}
2612
2613/*
2614 * Create the find & replace dialogs.
2615 * You can't have both at once: ":find" when replace is showing, destroys
2616 * the replace dialog first, and the other way around.
2617 */
2618#ifdef MSWIN_FIND_REPLACE
2619 static void
2620initialise_findrep(char_u *initial_string)
2621{
2622 int wword = FALSE;
2623 int mcase = !p_ic;
2624 char_u *entry_text;
2625
2626 /* Get the search string to use. */
2627 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2628
2629 s_findrep_struct.hwndOwner = s_hwnd;
2630 s_findrep_struct.Flags = FR_DOWN;
2631 if (mcase)
2632 s_findrep_struct.Flags |= FR_MATCHCASE;
2633 if (wword)
2634 s_findrep_struct.Flags |= FR_WHOLEWORD;
2635 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002636 {
2637 WCHAR *p = enc_to_utf16(entry_text, NULL);
2638 if (p != NULL)
2639 {
2640 int len = s_findrep_struct.wFindWhatLen - 1;
2641
2642 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2643 s_findrep_struct.lpstrFindWhat[len] = NUL;
2644 vim_free(p);
2645 }
2646 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002647 vim_free(entry_text);
2648}
2649#endif
2650
2651 static void
2652set_window_title(HWND hwnd, char *title)
2653{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002654 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002655 {
2656 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002657
2658 /* Convert the title from 'encoding' to UTF-16. */
2659 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2660 if (wbuf != NULL)
2661 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002662 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002663 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664 }
2665 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002666 else
2667 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002668}
2669
2670 void
2671gui_mch_find_dialog(exarg_T *eap)
2672{
2673#ifdef MSWIN_FIND_REPLACE
2674 if (s_findrep_msg != 0)
2675 {
2676 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2677 DestroyWindow(s_findrep_hwnd);
2678
2679 if (!IsWindow(s_findrep_hwnd))
2680 {
2681 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002682 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002683 }
2684
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002685 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002686 (void)SetFocus(s_findrep_hwnd);
2687
2688 s_findrep_is_find = TRUE;
2689 }
2690#endif
2691}
2692
2693
2694 void
2695gui_mch_replace_dialog(exarg_T *eap)
2696{
2697#ifdef MSWIN_FIND_REPLACE
2698 if (s_findrep_msg != 0)
2699 {
2700 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2701 DestroyWindow(s_findrep_hwnd);
2702
2703 if (!IsWindow(s_findrep_hwnd))
2704 {
2705 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002706 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707 }
2708
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002709 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710 (void)SetFocus(s_findrep_hwnd);
2711
2712 s_findrep_is_find = FALSE;
2713 }
2714#endif
2715}
2716
2717
2718/*
2719 * Set visibility of the pointer.
2720 */
2721 void
2722gui_mch_mousehide(int hide)
2723{
2724 if (hide != gui.pointer_hidden)
2725 {
2726 ShowCursor(!hide);
2727 gui.pointer_hidden = hide;
2728 }
2729}
2730
2731#ifdef FEAT_MENU
2732 static void
2733gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2734{
2735 /* Unhide the mouse, we don't get move events here. */
2736 gui_mch_mousehide(FALSE);
2737
2738 (void)TrackPopupMenu(
2739 (HMENU)menu->submenu_id,
2740 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2741 x, y,
2742 (int)0, /*reserved param*/
2743 s_hwnd,
2744 NULL);
2745 /*
2746 * NOTE: The pop-up menu can eat the mouse up event.
2747 * We deal with this in normal.c.
2748 */
2749}
2750#endif
2751
2752/*
2753 * Got a message when the system will go down.
2754 */
2755 static void
2756_OnEndSession(void)
2757{
2758 getout_preserve_modified(1);
2759}
2760
2761/*
2762 * Get this message when the user clicks on the cross in the top right corner
2763 * of a Windows95 window.
2764 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002765 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002766_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002767{
2768 gui_shell_closed();
2769}
2770
2771/*
2772 * Get a message when the window is being destroyed.
2773 */
2774 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002775_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002776{
2777 if (!destroying)
2778 _OnClose(hwnd);
2779}
2780
2781 static void
2782_OnPaint(
2783 HWND hwnd)
2784{
2785 if (!IsMinimized(hwnd))
2786 {
2787 PAINTSTRUCT ps;
2788
2789 out_flush(); /* make sure all output has been processed */
2790 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002791
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002792 /* prevent multi-byte characters from misprinting on an invalid
2793 * rectangle */
2794 if (has_mbyte)
2795 {
2796 RECT rect;
2797
2798 GetClientRect(hwnd, &rect);
2799 ps.rcPaint.left = rect.left;
2800 ps.rcPaint.right = rect.right;
2801 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002802
2803 if (!IsRectEmpty(&ps.rcPaint))
2804 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2806 ps.rcPaint.right - ps.rcPaint.left + 1,
2807 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2808 }
2809
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 EndPaint(hwnd, &ps);
2811 }
2812}
2813
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002814 static void
2815_OnSize(
2816 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002817 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 int cx,
2819 int cy)
2820{
2821 if (!IsMinimized(hwnd))
2822 {
2823 gui_resize_shell(cx, cy);
2824
2825#ifdef FEAT_MENU
2826 /* Menu bar may wrap differently now */
2827 gui_mswin_get_menu_height(TRUE);
2828#endif
2829 }
2830}
2831
2832 static void
2833_OnSetFocus(
2834 HWND hwnd,
2835 HWND hwndOldFocus)
2836{
2837 gui_focus_change(TRUE);
2838 s_getting_focus = TRUE;
2839 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2840}
2841
2842 static void
2843_OnKillFocus(
2844 HWND hwnd,
2845 HWND hwndNewFocus)
2846{
2847 gui_focus_change(FALSE);
2848 s_getting_focus = FALSE;
2849 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2850}
2851
2852/*
2853 * Get a message when the user switches back to vim
2854 */
2855 static LRESULT
2856_OnActivateApp(
2857 HWND hwnd,
2858 BOOL fActivate,
2859 DWORD dwThreadId)
2860{
2861 /* we call gui_focus_change() in _OnSetFocus() */
2862 /* gui_focus_change((int)fActivate); */
2863 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2864}
2865
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002866 void
2867gui_mch_destroy_scrollbar(scrollbar_T *sb)
2868{
2869 DestroyWindow(sb->id);
2870}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871
2872/*
2873 * Get current mouse coordinates in text window.
2874 */
2875 void
2876gui_mch_getmouse(int *x, int *y)
2877{
2878 RECT rct;
2879 POINT mp;
2880
2881 (void)GetWindowRect(s_textArea, &rct);
2882 (void)GetCursorPos((LPPOINT)&mp);
2883 *x = (int)(mp.x - rct.left);
2884 *y = (int)(mp.y - rct.top);
2885}
2886
2887/*
2888 * Move mouse pointer to character at (x, y).
2889 */
2890 void
2891gui_mch_setmouse(int x, int y)
2892{
2893 RECT rct;
2894
2895 (void)GetWindowRect(s_textArea, &rct);
2896 (void)SetCursorPos(x + gui.border_offset + rct.left,
2897 y + gui.border_offset + rct.top);
2898}
2899
2900 static void
2901gui_mswin_get_valid_dimensions(
2902 int w,
2903 int h,
2904 int *valid_w,
2905 int *valid_h)
2906{
2907 int base_width, base_height;
2908
2909 base_width = gui_get_base_width()
2910 + (GetSystemMetrics(SM_CXFRAME) +
2911 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2912 base_height = gui_get_base_height()
2913 + (GetSystemMetrics(SM_CYFRAME) +
2914 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2915 + GetSystemMetrics(SM_CYCAPTION)
2916#ifdef FEAT_MENU
2917 + gui_mswin_get_menu_height(FALSE)
2918#endif
2919 ;
2920 *valid_w = base_width +
2921 ((w - base_width) / gui.char_width) * gui.char_width;
2922 *valid_h = base_height +
2923 ((h - base_height) / gui.char_height) * gui.char_height;
2924}
2925
2926 void
2927gui_mch_flash(int msec)
2928{
2929 RECT rc;
2930
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002931#if defined(FEAT_DIRECTX)
2932 if (IS_ENABLE_DIRECTX())
2933 DWriteContext_Flush(s_dwc);
2934#endif
2935
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936 /*
2937 * Note: InvertRect() excludes right and bottom of rectangle.
2938 */
2939 rc.left = 0;
2940 rc.top = 0;
2941 rc.right = gui.num_cols * gui.char_width;
2942 rc.bottom = gui.num_rows * gui.char_height;
2943 InvertRect(s_hdc, &rc);
2944 gui_mch_flush(); /* make sure it's displayed */
2945
2946 ui_delay((long)msec, TRUE); /* wait for a few msec */
2947
2948 InvertRect(s_hdc, &rc);
2949}
2950
2951/*
2952 * Return flags used for scrolling.
2953 * The SW_INVALIDATE is required when part of the window is covered or
2954 * off-screen. Refer to MS KB Q75236.
2955 */
2956 static int
2957get_scroll_flags(void)
2958{
2959 HWND hwnd;
2960 RECT rcVim, rcOther, rcDest;
2961
2962 GetWindowRect(s_hwnd, &rcVim);
2963
2964 /* Check if the window is partly above or below the screen. We don't care
2965 * about partly left or right of the screen, it is not relevant when
2966 * scrolling up or down. */
2967 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2968 return SW_INVALIDATE;
2969
2970 /* Check if there is an window (partly) on top of us. */
2971 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2972 if (IsWindowVisible(hwnd))
2973 {
2974 GetWindowRect(hwnd, &rcOther);
2975 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2976 return SW_INVALIDATE;
2977 }
2978 return 0;
2979}
2980
2981/*
2982 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
2983 * may not be scrolled out properly.
2984 * For gVim, when _OnScroll() is repeated, the character at the
2985 * previous cursor position may be left drawn after scroll.
2986 * The problem can be avoided by calling GetPixel() to get a pixel in
2987 * the region before ScrollWindowEx().
2988 */
2989 static void
2990intel_gpu_workaround(void)
2991{
2992 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
2993}
2994
2995/*
2996 * Delete the given number of lines from the given row, scrolling up any
2997 * text further down within the scroll region.
2998 */
2999 void
3000gui_mch_delete_lines(
3001 int row,
3002 int num_lines)
3003{
3004 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003005
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003006 rc.left = FILL_X(gui.scroll_region_left);
3007 rc.right = FILL_X(gui.scroll_region_right + 1);
3008 rc.top = FILL_Y(row);
3009 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3010
Bram Moolenaar92467d32017-12-05 13:22:16 +01003011#if defined(FEAT_DIRECTX)
3012 if (IS_ENABLE_DIRECTX())
3013 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003014 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3015 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003016 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003017 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003018#endif
3019 {
3020 intel_gpu_workaround();
3021 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003022 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003023 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003024 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003025
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003026 /* This seems to be required to avoid the cursor disappearing when
3027 * scrolling such that the cursor ends up in the top-left character on
3028 * the screen... But why? (Webb) */
3029 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3030 /* gui.cursor_is_valid = FALSE; */
3031
3032 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3033 gui.scroll_region_left,
3034 gui.scroll_region_bot, gui.scroll_region_right);
3035}
3036
3037/*
3038 * Insert the given number of lines before the given row, scrolling down any
3039 * following text within the scroll region.
3040 */
3041 void
3042gui_mch_insert_lines(
3043 int row,
3044 int num_lines)
3045{
3046 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003047
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003048 rc.left = FILL_X(gui.scroll_region_left);
3049 rc.right = FILL_X(gui.scroll_region_right + 1);
3050 rc.top = FILL_Y(row);
3051 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003052
3053#if defined(FEAT_DIRECTX)
3054 if (IS_ENABLE_DIRECTX())
3055 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003056 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3057 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003058 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003059 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003060#endif
3061 {
3062 intel_gpu_workaround();
3063 /* The SW_INVALIDATE is required when part of the window is covered or
3064 * off-screen. How do we avoid it when it's not needed? */
3065 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003067 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003068 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003069
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003070 gui_clear_block(row, gui.scroll_region_left,
3071 row + num_lines - 1, gui.scroll_region_right);
3072}
3073
3074
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003076gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077{
3078#if defined(FEAT_DIRECTX)
3079 DWriteContext_Close(s_dwc);
3080 DWrite_Final();
3081 s_dwc = NULL;
3082#endif
3083
3084 ReleaseDC(s_textArea, s_hdc);
3085 DeleteObject(s_brush);
3086
3087#ifdef FEAT_TEAROFF
3088 /* Unload the tearoff bitmap */
3089 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3090#endif
3091
3092 /* Destroy our window (if we have one). */
3093 if (s_hwnd != NULL)
3094 {
3095 destroying = TRUE; /* ignore WM_DESTROY message now */
3096 DestroyWindow(s_hwnd);
3097 }
3098
3099#ifdef GLOBAL_IME
3100 global_ime_end();
3101#endif
3102}
3103
3104 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003105logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003106{
3107 char *p;
3108 char *res;
3109 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003110 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003111 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003112 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003114 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3115 if (font_name == NULL)
3116 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003117 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003118 quality_name = quality_id2name((int)lf.lfQuality);
3119
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003120 res = (char *)alloc((unsigned)(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003121 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
3122 + (quality_name == NULL ? 0 : strlen(quality_name) + 2)));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003123 if (res != NULL)
3124 {
3125 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003126 // make a normal font string out of the lf thing:
3127 points = pixels_to_points(
3128 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3129 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3130 sprintf((char *)p, "%s:h%d", font_name, points);
3131 else
3132 sprintf((char *)p, "%s:h%d:W%d", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133 while (*p)
3134 {
3135 if (*p == ' ')
3136 *p = '_';
3137 ++p;
3138 }
3139 if (lf.lfItalic)
3140 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003141 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003142 STRCAT(p, ":b");
3143 if (lf.lfUnderline)
3144 STRCAT(p, ":u");
3145 if (lf.lfStrikeOut)
3146 STRCAT(p, ":s");
3147 if (charset_name != NULL)
3148 {
3149 STRCAT(p, ":c");
3150 STRCAT(p, charset_name);
3151 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003152 if (quality_name != NULL)
3153 {
3154 STRCAT(p, ":q");
3155 STRCAT(p, quality_name);
3156 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003157 }
3158
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003159 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003160 return (char_u *)res;
3161}
3162
3163
3164#ifdef FEAT_MBYTE_IME
3165/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003166 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003167 * 'guifont'
3168 */
3169 static void
3170update_im_font(void)
3171{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003172 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003173
3174 if (p_guifontwide != NULL && *p_guifontwide != NUL
3175 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003176 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 norm_logfont = lf_wide;
3178 else
3179 norm_logfont = sub_logfont;
3180 im_set_font(&norm_logfont);
3181}
3182#endif
3183
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003184/*
3185 * Handler of gui.wide_font (p_guifontwide) changed notification.
3186 */
3187 void
3188gui_mch_wide_font_changed(void)
3189{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003190 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003192#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003194#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195
3196 gui_mch_free_font(gui.wide_ital_font);
3197 gui.wide_ital_font = NOFONT;
3198 gui_mch_free_font(gui.wide_bold_font);
3199 gui.wide_bold_font = NOFONT;
3200 gui_mch_free_font(gui.wide_boldital_font);
3201 gui.wide_boldital_font = NOFONT;
3202
3203 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003204 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205 {
3206 if (!lf.lfItalic)
3207 {
3208 lf.lfItalic = TRUE;
3209 gui.wide_ital_font = get_font_handle(&lf);
3210 lf.lfItalic = FALSE;
3211 }
3212 if (lf.lfWeight < FW_BOLD)
3213 {
3214 lf.lfWeight = FW_BOLD;
3215 gui.wide_bold_font = get_font_handle(&lf);
3216 if (!lf.lfItalic)
3217 {
3218 lf.lfItalic = TRUE;
3219 gui.wide_boldital_font = get_font_handle(&lf);
3220 }
3221 }
3222 }
3223}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224
3225/*
3226 * Initialise vim to use the font with the given name.
3227 * Return FAIL if the font could not be loaded, OK otherwise.
3228 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003230gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003232 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233 GuiFont font = NOFONT;
3234 char_u *p;
3235
3236 /* Load the font */
3237 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3238 font = get_font_handle(&lf);
3239 if (font == NOFONT)
3240 return FAIL;
3241
3242 if (font_name == NULL)
3243 font_name = (char_u *)lf.lfFaceName;
3244#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3245 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003246#endif
3247#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003248 sub_logfont = lf;
3249#endif
3250#ifdef FEAT_MBYTE_IME
3251 update_im_font();
3252#endif
3253 gui_mch_free_font(gui.norm_font);
3254 gui.norm_font = font;
3255 current_font_height = lf.lfHeight;
3256 GetFontSize(font);
3257
3258 p = logfont2name(lf);
3259 if (p != NULL)
3260 {
3261 hl_set_font_name(p);
3262
3263 /* When setting 'guifont' to "*" replace it with the actual font name.
3264 * */
3265 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3266 {
3267 vim_free(p_guifont);
3268 p_guifont = p;
3269 }
3270 else
3271 vim_free(p);
3272 }
3273
3274 gui_mch_free_font(gui.ital_font);
3275 gui.ital_font = NOFONT;
3276 gui_mch_free_font(gui.bold_font);
3277 gui.bold_font = NOFONT;
3278 gui_mch_free_font(gui.boldital_font);
3279 gui.boldital_font = NOFONT;
3280
3281 if (!lf.lfItalic)
3282 {
3283 lf.lfItalic = TRUE;
3284 gui.ital_font = get_font_handle(&lf);
3285 lf.lfItalic = FALSE;
3286 }
3287 if (lf.lfWeight < FW_BOLD)
3288 {
3289 lf.lfWeight = FW_BOLD;
3290 gui.bold_font = get_font_handle(&lf);
3291 if (!lf.lfItalic)
3292 {
3293 lf.lfItalic = TRUE;
3294 gui.boldital_font = get_font_handle(&lf);
3295 }
3296 }
3297
3298 return OK;
3299}
3300
3301#ifndef WPF_RESTORETOMAXIMIZED
3302# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3303#endif
3304
3305/*
3306 * Return TRUE if the GUI window is maximized, filling the whole screen.
3307 */
3308 int
3309gui_mch_maximized(void)
3310{
3311 WINDOWPLACEMENT wp;
3312
3313 wp.length = sizeof(WINDOWPLACEMENT);
3314 if (GetWindowPlacement(s_hwnd, &wp))
3315 return wp.showCmd == SW_SHOWMAXIMIZED
3316 || (wp.showCmd == SW_SHOWMINIMIZED
3317 && wp.flags == WPF_RESTORETOMAXIMIZED);
3318
3319 return 0;
3320}
3321
3322/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003323 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3324 * is set. Compute the new Rows and Columns. This is like resizing the
3325 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 */
3327 void
3328gui_mch_newfont(void)
3329{
3330 RECT rect;
3331
3332 GetWindowRect(s_hwnd, &rect);
3333 if (win_socket_id == 0)
3334 {
3335 gui_resize_shell(rect.right - rect.left
3336 - (GetSystemMetrics(SM_CXFRAME) +
3337 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3338 rect.bottom - rect.top
3339 - (GetSystemMetrics(SM_CYFRAME) +
3340 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3341 - GetSystemMetrics(SM_CYCAPTION)
3342#ifdef FEAT_MENU
3343 - gui_mswin_get_menu_height(FALSE)
3344#endif
3345 );
3346 }
3347 else
3348 {
3349 /* Inside another window, don't use the frame and border. */
3350 gui_resize_shell(rect.right - rect.left,
3351 rect.bottom - rect.top
3352#ifdef FEAT_MENU
3353 - gui_mswin_get_menu_height(FALSE)
3354#endif
3355 );
3356 }
3357}
3358
3359/*
3360 * Set the window title
3361 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003362 void
3363gui_mch_settitle(
3364 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003365 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003366{
3367 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3368}
3369
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003370#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3372 * misc2.c! */
3373static LPCSTR mshape_idcs[] =
3374{
3375 IDC_ARROW, /* arrow */
3376 MAKEINTRESOURCE(0), /* blank */
3377 IDC_IBEAM, /* beam */
3378 IDC_SIZENS, /* updown */
3379 IDC_SIZENS, /* udsizing */
3380 IDC_SIZEWE, /* leftright */
3381 IDC_SIZEWE, /* lrsizing */
3382 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003383 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384 IDC_ARROW, /* crosshair */
3385 IDC_ARROW, /* hand1 */
3386 IDC_ARROW, /* hand2 */
3387 IDC_ARROW, /* pencil */
3388 IDC_ARROW, /* question */
3389 IDC_ARROW, /* right-arrow */
3390 IDC_UPARROW, /* up-arrow */
3391 IDC_ARROW /* last one */
3392};
3393
3394 void
3395mch_set_mouse_shape(int shape)
3396{
3397 LPCSTR idc;
3398
3399 if (shape == MSHAPE_HIDE)
3400 ShowCursor(FALSE);
3401 else
3402 {
3403 if (shape >= MSHAPE_NUMBERED)
3404 idc = IDC_ARROW;
3405 else
3406 idc = mshape_idcs[shape];
3407#ifdef SetClassLongPtr
3408 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3409#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003410 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411#endif
3412 if (!p_mh)
3413 {
3414 POINT mp;
3415
3416 /* Set the position to make it redrawn with the new shape. */
3417 (void)GetCursorPos((LPPOINT)&mp);
3418 (void)SetCursorPos(mp.x, mp.y);
3419 ShowCursor(TRUE);
3420 }
3421 }
3422}
3423#endif
3424
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003425#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003427 * Wide version of convert_filter().
3428 */
3429 static WCHAR *
3430convert_filterW(char_u *s)
3431{
3432 char_u *tmp;
3433 int len;
3434 WCHAR *res;
3435
3436 tmp = convert_filter(s);
3437 if (tmp == NULL)
3438 return NULL;
3439 len = (int)STRLEN(s) + 3;
3440 res = enc_to_utf16(tmp, &len);
3441 vim_free(tmp);
3442 return res;
3443}
3444
3445/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003446 * Pop open a file browser and return the file selected, in allocated memory,
3447 * or NULL if Cancel is hit.
3448 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3449 * title - Title message for the file browser dialog.
3450 * dflt - Default name of file.
3451 * ext - Default extension to be added to files without extensions.
3452 * initdir - directory in which to open the browser (NULL = current dir)
3453 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003454 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003455 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003456gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003457 int saving,
3458 char_u *title,
3459 char_u *dflt,
3460 char_u *ext,
3461 char_u *initdir,
3462 char_u *filter)
3463{
3464 /* We always use the wide function. This means enc_to_utf16() must work,
3465 * otherwise it fails miserably! */
3466 OPENFILENAMEW fileStruct;
3467 WCHAR fileBuf[MAXPATHL];
3468 WCHAR *wp;
3469 int i;
3470 WCHAR *titlep = NULL;
3471 WCHAR *extp = NULL;
3472 WCHAR *initdirp = NULL;
3473 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003474 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475
3476 if (dflt == NULL)
3477 fileBuf[0] = NUL;
3478 else
3479 {
3480 wp = enc_to_utf16(dflt, NULL);
3481 if (wp == NULL)
3482 fileBuf[0] = NUL;
3483 else
3484 {
3485 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3486 fileBuf[i] = wp[i];
3487 fileBuf[i] = NUL;
3488 vim_free(wp);
3489 }
3490 }
3491
3492 /* Convert the filter to Windows format. */
3493 filterp = convert_filterW(filter);
3494
3495 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003496# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003497 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003498 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003499# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003501# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003502
3503 if (title != NULL)
3504 titlep = enc_to_utf16(title, NULL);
3505 fileStruct.lpstrTitle = titlep;
3506
3507 if (ext != NULL)
3508 extp = enc_to_utf16(ext, NULL);
3509 fileStruct.lpstrDefExt = extp;
3510
3511 fileStruct.lpstrFile = fileBuf;
3512 fileStruct.nMaxFile = MAXPATHL;
3513 fileStruct.lpstrFilter = filterp;
3514 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3515 /* has an initial dir been specified? */
3516 if (initdir != NULL && *initdir != NUL)
3517 {
3518 /* Must have backslashes here, no matter what 'shellslash' says */
3519 initdirp = enc_to_utf16(initdir, NULL);
3520 if (initdirp != NULL)
3521 {
3522 for (wp = initdirp; *wp != NUL; ++wp)
3523 if (*wp == '/')
3524 *wp = '\\';
3525 }
3526 fileStruct.lpstrInitialDir = initdirp;
3527 }
3528
3529 /*
3530 * TODO: Allow selection of multiple files. Needs another arg to this
3531 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3532 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3533 * files that don't exist yet, so I haven't put it in. What about
3534 * OFN_PATHMUSTEXIST?
3535 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3536 */
3537 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003538# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539 if (curbuf->b_p_bin)
3540 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003541# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 if (saving)
3543 {
3544 if (!GetSaveFileNameW(&fileStruct))
3545 return NULL;
3546 }
3547 else
3548 {
3549 if (!GetOpenFileNameW(&fileStruct))
3550 return NULL;
3551 }
3552
3553 vim_free(filterp);
3554 vim_free(initdirp);
3555 vim_free(titlep);
3556 vim_free(extp);
3557
3558 /* Convert from UCS2 to 'encoding'. */
3559 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003560 if (p == NULL)
3561 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003562
3563 /* Give focus back to main window (when using MDI). */
3564 SetFocus(s_hwnd);
3565
3566 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003567 q = vim_strsave(shorten_fname1(p));
3568 vim_free(p);
3569 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003570}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571
3572
3573/*
3574 * Convert the string s to the proper format for a filter string by replacing
3575 * the \t and \n delimiters with \0.
3576 * Returns the converted string in allocated memory.
3577 *
3578 * Keep in sync with convert_filterW() above!
3579 */
3580 static char_u *
3581convert_filter(char_u *s)
3582{
3583 char_u *res;
3584 unsigned s_len = (unsigned)STRLEN(s);
3585 unsigned i;
3586
3587 res = alloc(s_len + 3);
3588 if (res != NULL)
3589 {
3590 for (i = 0; i < s_len; ++i)
3591 if (s[i] == '\t' || s[i] == '\n')
3592 res[i] = '\0';
3593 else
3594 res[i] = s[i];
3595 res[s_len] = NUL;
3596 /* Add two extra NULs to make sure it's properly terminated. */
3597 res[s_len + 1] = NUL;
3598 res[s_len + 2] = NUL;
3599 }
3600 return res;
3601}
3602
3603/*
3604 * Select a directory.
3605 */
3606 char_u *
3607gui_mch_browsedir(char_u *title, char_u *initdir)
3608{
3609 /* We fake this: Use a filter that doesn't select anything and a default
3610 * file name that won't be used. */
3611 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3612 initdir, (char_u *)_("Directory\t*.nothing\n"));
3613}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003614#endif /* FEAT_BROWSE */
3615
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 static void
3617_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003618 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003619 HDROP hDrop)
3620{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003621#define BUFPATHLEN _MAX_PATH
3622#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003624 char szFile[BUFPATHLEN];
3625 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3626 UINT i;
3627 char_u **fnames;
3628 POINT pt;
3629 int_u modifiers = 0;
3630
3631 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3632
3633 /* Obtain dropped position */
3634 DragQueryPoint(hDrop, &pt);
3635 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3636
3637 reset_VIsual();
3638
3639 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3640
3641 if (fnames != NULL)
3642 for (i = 0; i < cFiles; ++i)
3643 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3645 fnames[i] = utf16_to_enc(wszFile, NULL);
3646 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 {
3648 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3649 fnames[i] = vim_strsave((char_u *)szFile);
3650 }
3651 }
3652
3653 DragFinish(hDrop);
3654
3655 if (fnames != NULL)
3656 {
3657 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3658 modifiers |= MOUSE_SHIFT;
3659 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3660 modifiers |= MOUSE_CTRL;
3661 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3662 modifiers |= MOUSE_ALT;
3663
3664 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3665
3666 s_need_activate = TRUE;
3667 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668}
3669
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670 static int
3671_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003672 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003673 HWND hwndCtl,
3674 UINT code,
3675 int pos)
3676{
3677 static UINT prev_code = 0; /* code of previous call */
3678 scrollbar_T *sb, *sb_info;
3679 long val;
3680 int dragging = FALSE;
3681 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003682 SCROLLINFO si;
3683
3684 si.cbSize = sizeof(si);
3685 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686
3687 sb = gui_mswin_find_scrollbar(hwndCtl);
3688 if (sb == NULL)
3689 return 0;
3690
3691 if (sb->wp != NULL) /* Left or right scrollbar */
3692 {
3693 /*
3694 * Careful: need to get scrollbar info out of first (left) scrollbar
3695 * for window, but keep real scrollbar too because we must pass it to
3696 * gui_drag_scrollbar().
3697 */
3698 sb_info = &sb->wp->w_scrollbars[0];
3699 }
3700 else /* Bottom scrollbar */
3701 sb_info = sb;
3702 val = sb_info->value;
3703
3704 switch (code)
3705 {
3706 case SB_THUMBTRACK:
3707 val = pos;
3708 dragging = TRUE;
3709 if (sb->scroll_shift > 0)
3710 val <<= sb->scroll_shift;
3711 break;
3712 case SB_LINEDOWN:
3713 val++;
3714 break;
3715 case SB_LINEUP:
3716 val--;
3717 break;
3718 case SB_PAGEDOWN:
3719 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3720 break;
3721 case SB_PAGEUP:
3722 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3723 break;
3724 case SB_TOP:
3725 val = 0;
3726 break;
3727 case SB_BOTTOM:
3728 val = sb_info->max;
3729 break;
3730 case SB_ENDSCROLL:
3731 if (prev_code == SB_THUMBTRACK)
3732 {
3733 /*
3734 * "pos" only gives us 16-bit data. In case of large file,
3735 * use GetScrollPos() which returns 32-bit. Unfortunately it
3736 * is not valid while the scrollbar is being dragged.
3737 */
3738 val = GetScrollPos(hwndCtl, SB_CTL);
3739 if (sb->scroll_shift > 0)
3740 val <<= sb->scroll_shift;
3741 }
3742 break;
3743
3744 default:
3745 /* TRACE("Unknown scrollbar event %d\n", code); */
3746 return 0;
3747 }
3748 prev_code = code;
3749
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003750 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3751 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003752
3753 /*
3754 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3755 */
3756 if (sb->wp != NULL)
3757 {
3758 scrollbar_T *sba = sb->wp->w_scrollbars;
3759 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762 }
3763
3764 /* Don't let us be interrupted here by another message. */
3765 s_busy_processing = TRUE;
3766
3767 /* When "allow_scrollbar" is FALSE still need to remember the new
3768 * position, but don't actually scroll by setting "dont_scroll". */
3769 dont_scroll = !allow_scrollbar;
3770
Bram Moolenaara338adc2018-01-31 20:51:47 +01003771 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003772 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003773 mch_enable_flush();
3774 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775
3776 s_busy_processing = FALSE;
3777 dont_scroll = dont_scroll_save;
3778
3779 return 0;
3780}
3781
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783#ifdef FEAT_XPM_W32
3784# include "xpm_w32.h"
3785#endif
3786
3787#ifdef PROTO
3788# define WINAPI
3789#endif
3790
3791#ifdef __MINGW32__
3792/*
3793 * Add a lot of missing defines.
3794 * They are not always missing, we need the #ifndef's.
3795 */
3796# ifndef _cdecl
3797# define _cdecl
3798# endif
3799# ifndef IsMinimized
3800# define IsMinimized(hwnd) IsIconic(hwnd)
3801# endif
3802# ifndef IsMaximized
3803# define IsMaximized(hwnd) IsZoomed(hwnd)
3804# endif
3805# ifndef SelectFont
3806# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3807# endif
3808# ifndef GetStockBrush
3809# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3810# endif
3811# ifndef DeleteBrush
3812# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3813# endif
3814
3815# ifndef HANDLE_WM_RBUTTONDBLCLK
3816# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3817 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3818# endif
3819# ifndef HANDLE_WM_MBUTTONUP
3820# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3821 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3822# endif
3823# ifndef HANDLE_WM_MBUTTONDBLCLK
3824# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3825 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3826# endif
3827# ifndef HANDLE_WM_LBUTTONDBLCLK
3828# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3829 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3830# endif
3831# ifndef HANDLE_WM_RBUTTONDOWN
3832# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3833 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3834# endif
3835# ifndef HANDLE_WM_MOUSEMOVE
3836# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3837 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3838# endif
3839# ifndef HANDLE_WM_RBUTTONUP
3840# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3841 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3842# endif
3843# ifndef HANDLE_WM_MBUTTONDOWN
3844# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3845 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3846# endif
3847# ifndef HANDLE_WM_LBUTTONUP
3848# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3849 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3850# endif
3851# ifndef HANDLE_WM_LBUTTONDOWN
3852# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3853 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3854# endif
3855# ifndef HANDLE_WM_SYSCHAR
3856# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3857 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3858# endif
3859# ifndef HANDLE_WM_ACTIVATEAPP
3860# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3861 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3862# endif
3863# ifndef HANDLE_WM_WINDOWPOSCHANGING
3864# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3865 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3866# endif
3867# ifndef HANDLE_WM_VSCROLL
3868# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3869 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3870# endif
3871# ifndef HANDLE_WM_SETFOCUS
3872# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3873 ((fn)((hwnd), (HWND)(wParam)), 0L)
3874# endif
3875# ifndef HANDLE_WM_KILLFOCUS
3876# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3877 ((fn)((hwnd), (HWND)(wParam)), 0L)
3878# endif
3879# ifndef HANDLE_WM_HSCROLL
3880# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3881 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3882# endif
3883# ifndef HANDLE_WM_DROPFILES
3884# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3885 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3886# endif
3887# ifndef HANDLE_WM_CHAR
3888# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3889 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3890# endif
3891# ifndef HANDLE_WM_SYSDEADCHAR
3892# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3893 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3894# endif
3895# ifndef HANDLE_WM_DEADCHAR
3896# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3897 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3898# endif
3899#endif /* __MINGW32__ */
3900
3901
3902/* Some parameters for tearoff menus. All in pixels. */
3903#define TEAROFF_PADDING_X 2
3904#define TEAROFF_BUTTON_PAD_X 8
3905#define TEAROFF_MIN_WIDTH 200
3906#define TEAROFF_SUBMENU_LABEL ">>"
3907#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3908
3909
3910/* For the Intellimouse: */
3911#ifndef WM_MOUSEWHEEL
3912#define WM_MOUSEWHEEL 0x20a
3913#endif
3914
3915
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003916#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917# define ID_BEVAL_TOOLTIP 200
3918# define BEVAL_TEXT_LEN MAXPATHL
3919
Bram Moolenaar167632f2010-05-26 21:42:54 +02003920#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003921/* Work around old versions of basetsd.h which wrongly declares
3922 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003923# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003924# define UINT_PTR UINT
3925#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003930
Bram Moolenaar82881492012-11-20 16:53:39 +01003931
3932/* cproto fails on missing include files */
3933#ifndef PROTO
3934
Bram Moolenaar45360022005-07-21 21:08:21 +00003935/*
3936 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003937 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003938 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003939# include <pshpack1.h>
3940
3941#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003942
3943typedef struct _DllVersionInfo
3944{
3945 DWORD cbSize;
3946 DWORD dwMajorVersion;
3947 DWORD dwMinorVersion;
3948 DWORD dwBuildNumber;
3949 DWORD dwPlatformID;
3950} DLLVERSIONINFO;
3951
Bram Moolenaar82881492012-11-20 16:53:39 +01003952#ifndef PROTO
3953# include <poppack.h>
3954#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003955
Bram Moolenaar45360022005-07-21 21:08:21 +00003956typedef struct tagTOOLINFOA_NEW
3957{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003958 UINT cbSize;
3959 UINT uFlags;
3960 HWND hwnd;
3961 UINT_PTR uId;
3962 RECT rect;
3963 HINSTANCE hinst;
3964 LPSTR lpszText;
3965 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003966} TOOLINFO_NEW;
3967
3968typedef struct tagNMTTDISPINFO_NEW
3969{
3970 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003971 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003972 char szText[80];
3973 HINSTANCE hinst;
3974 UINT uFlags;
3975 LPARAM lParam;
3976} NMTTDISPINFO_NEW;
3977
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003978typedef struct tagTOOLINFOW_NEW
3979{
3980 UINT cbSize;
3981 UINT uFlags;
3982 HWND hwnd;
3983 UINT_PTR uId;
3984 RECT rect;
3985 HINSTANCE hinst;
3986 LPWSTR lpszText;
3987 LPARAM lParam;
3988 void *lpReserved;
3989} TOOLINFOW_NEW;
3990
3991typedef struct tagNMTTDISPINFOW_NEW
3992{
3993 NMHDR hdr;
3994 LPWSTR lpszText;
3995 WCHAR szText[80];
3996 HINSTANCE hinst;
3997 UINT uFlags;
3998 LPARAM lParam;
3999} NMTTDISPINFOW_NEW;
4000
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004001
Bram Moolenaar45360022005-07-21 21:08:21 +00004002typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4003#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004004# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005#endif
4006
Bram Moolenaar45360022005-07-21 21:08:21 +00004007#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004008# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004009#endif
4010
4011#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004012# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004013#endif
4014
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004015#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004016
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004017#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4018/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4019 * it here if LPNMTTDISPINFO isn't defined.
4020 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4021 * _MSC_VER. */
4022# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4023typedef struct tagNMTTDISPINFOA {
4024 NMHDR hdr;
4025 LPSTR lpszText;
4026 char szText[80];
4027 HINSTANCE hinst;
4028 UINT uFlags;
4029 LPARAM lParam;
4030} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4031# define LPNMTTDISPINFO LPNMTTDISPINFOA
4032
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004033typedef struct tagNMTTDISPINFOW {
4034 NMHDR hdr;
4035 LPWSTR lpszText;
4036 WCHAR szText[80];
4037 HINSTANCE hinst;
4038 UINT uFlags;
4039 LPARAM lParam;
4040} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004041# endif
4042#endif
4043
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004044#ifndef TTN_GETDISPINFOW
4045# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4046#endif
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048/* Local variables: */
4049
4050#ifdef FEAT_MENU
4051static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054/*
4055 * Use the system font for dialogs and tear-off menus. Remove this line to
4056 * use DLG_FONT_NAME.
4057 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004058#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061#define VIM_CLASSW L"Vim"
4062
4063/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4064 * thus there should be room for every dialog. For tearoffs it's made bigger
4065 * when needed. */
4066#define DLG_ALLOC_SIZE 16 * 1024
4067
4068/*
4069 * stuff for dialogs, menus, tearoffs etc.
4070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071static PWORD
4072add_dialog_element(
4073 PWORD p,
4074 DWORD lStyle,
4075 WORD x,
4076 WORD y,
4077 WORD w,
4078 WORD h,
4079 WORD Id,
4080 WORD clss,
4081 const char *caption);
4082static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004083static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004084#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087static void get_dialog_font_metrics(void);
4088
4089static int dialog_default_button = -1;
4090
4091/* Intellimouse support */
4092static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4095#ifdef FEAT_TOOLBAR
4096static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004097static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098static int get_toolbar_bitmap(vimmenu_T *menu);
4099#endif
4100
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004101#ifdef FEAT_GUI_TABLINE
4102static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004103static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004104#endif
4105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#ifdef FEAT_MBYTE_IME
4107static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4108static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4109#endif
4110#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4111# ifdef NOIME
4112typedef struct tagCOMPOSITIONFORM {
4113 DWORD dwStyle;
4114 POINT ptCurrentPos;
4115 RECT rcArea;
4116} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4117typedef HANDLE HIMC;
4118# endif
4119
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004120static HINSTANCE hLibImm = NULL;
4121static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4122static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4123static HIMC (WINAPI *pImmGetContext)(HWND);
4124static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4125static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4126static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4127static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004128static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4129static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004130static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4131static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004132static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133static void dyn_imm_load(void);
4134#else
4135# define pImmGetCompositionStringA ImmGetCompositionStringA
4136# define pImmGetCompositionStringW ImmGetCompositionStringW
4137# define pImmGetContext ImmGetContext
4138# define pImmAssociateContext ImmAssociateContext
4139# define pImmReleaseContext ImmReleaseContext
4140# define pImmGetOpenStatus ImmGetOpenStatus
4141# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004142# define pImmGetCompositionFontW ImmGetCompositionFontW
4143# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144# define pImmSetCompositionWindow ImmSetCompositionWindow
4145# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004146# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147#endif
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149#ifdef FEAT_MENU
4150/*
4151 * Figure out how high the menu bar is at the moment.
4152 */
4153 static int
4154gui_mswin_get_menu_height(
4155 int fix_window) /* If TRUE, resize window if menu height changed */
4156{
4157 static int old_menu_height = -1;
4158
4159 RECT rc1, rc2;
4160 int num;
4161 int menu_height;
4162
4163 if (gui.menu_is_active)
4164 num = GetMenuItemCount(s_menuBar);
4165 else
4166 num = 0;
4167
4168 if (num == 0)
4169 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004170 else if (IsMinimized(s_hwnd))
4171 {
4172 /* The height of the menu cannot be determined while the window is
4173 * minimized. Take the previous height if the menu is changed in that
4174 * state, to avoid that Vim's vertical window size accidentally
4175 * increases due to the unaccounted-for menu height. */
4176 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 else
4179 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004180 /*
4181 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4182 * seem to have been set yet, so menu wraps in default window
4183 * width which is very narrow. Instead just return height of a
4184 * single menu item. Will still be wrong when the menu really
4185 * should wrap over more than one line.
4186 */
4187 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4188 if (gui.starting)
4189 menu_height = rc1.bottom - rc1.top + 1;
4190 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004192 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4193 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 }
4196
4197 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004198 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004199 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 return menu_height;
4202}
4203#endif /*FEAT_MENU*/
4204
4205
4206/*
4207 * Setup for the Intellimouse
4208 */
4209 static void
4210init_mouse_wheel(void)
4211{
4212
4213#ifndef SPI_GETWHEELSCROLLLINES
4214# define SPI_GETWHEELSCROLLLINES 104
4215#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004216#ifndef SPI_SETWHEELSCROLLLINES
4217# define SPI_SETWHEELSCROLLLINES 105
4218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4221#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4222#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4223#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 mouse_scroll_lines = 3; /* reasonable default */
4226
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004227 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4228 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4229 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230}
4231
4232
4233/* Intellimouse wheel handler */
4234 static void
4235_OnMouseWheel(
4236 HWND hwnd,
4237 short zDelta)
4238{
4239/* Treat a mouse wheel event as if it were a scroll request */
4240 int i;
4241 int size;
4242 HWND hwndCtl;
4243
4244 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4245 {
4246 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4247 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4248 }
4249 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4250 {
4251 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4252 size = curwin->w_scrollbars[SBAR_LEFT].size;
4253 }
4254 else
4255 return;
4256
4257 size = curwin->w_height;
4258 if (mouse_scroll_lines == 0)
4259 init_mouse_wheel();
4260
Bram Moolenaara338adc2018-01-31 20:51:47 +01004261 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (mouse_scroll_lines > 0
4263 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4264 {
4265 for (i = mouse_scroll_lines; i > 0; --i)
4266 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4267 }
4268 else
4269 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004270 mch_enable_flush();
4271 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272}
4273
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004274#ifdef USE_SYSMENU_FONT
4275/*
4276 * Get Menu Font.
4277 * Return OK or FAIL.
4278 */
4279 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004280gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004281{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004282 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004283
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004284 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4285 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004286 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004287 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004288 &nm,
4289 0))
4290 return FAIL;
4291 *lf = nm.lfMenuFont;
4292 return OK;
4293}
4294#endif
4295
4296
4297#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4298/*
4299 * Set the GUI tabline font to the system menu font
4300 */
4301 static void
4302set_tabline_font(void)
4303{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004304 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004305 HFONT font;
4306 HWND hwnd;
4307 HDC hdc;
4308 HFONT hfntOld;
4309 TEXTMETRIC tm;
4310
4311 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4312 return;
4313
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004314 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004315
4316 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4317
4318 /*
4319 * Compute the height of the font used for the tab text
4320 */
4321 hwnd = GetDesktopWindow();
4322 hdc = GetWindowDC(hwnd);
4323 hfntOld = SelectFont(hdc, font);
4324
4325 GetTextMetrics(hdc, &tm);
4326
4327 SelectFont(hdc, hfntOld);
4328 ReleaseDC(hwnd, hdc);
4329
4330 /*
4331 * The space used by the tab border and the space between the tab label
4332 * and the tab border is included as 7.
4333 */
4334 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4335}
4336#endif
4337
Bram Moolenaar520470a2005-06-16 21:59:56 +00004338/*
4339 * Invoked when a setting was changed.
4340 */
4341 static LRESULT CALLBACK
4342_OnSettingChange(UINT n)
4343{
4344 if (n == SPI_SETWHEELSCROLLLINES)
4345 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4346 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004347#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4348 if (n == SPI_SETNONCLIENTMETRICS)
4349 set_tabline_font();
4350#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004351 return 0;
4352}
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#ifdef FEAT_NETBEANS_INTG
4355 static void
4356_OnWindowPosChanged(
4357 HWND hwnd,
4358 const LPWINDOWPOS lpwpos)
4359{
4360 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004361 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4364 || lpwpos->cx != cx || lpwpos->cy != cy))
4365 {
4366 x = lpwpos->x;
4367 y = lpwpos->y;
4368 cx = lpwpos->cx;
4369 cy = lpwpos->cy;
4370 netbeans_frame_moved(x, y);
4371 }
4372 /* Allow to send WM_SIZE and WM_MOVE */
4373 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4374}
4375#endif
4376
4377 static int
4378_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 UINT fwSide,
4380 LPRECT lprc)
4381{
4382 int w, h;
4383 int valid_w, valid_h;
4384 int w_offset, h_offset;
4385
4386 w = lprc->right - lprc->left;
4387 h = lprc->bottom - lprc->top;
4388 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4389 w_offset = w - valid_w;
4390 h_offset = h - valid_h;
4391
4392 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4393 || fwSide == WMSZ_BOTTOMLEFT)
4394 lprc->left += w_offset;
4395 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4396 || fwSide == WMSZ_BOTTOMRIGHT)
4397 lprc->right -= w_offset;
4398
4399 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4400 || fwSide == WMSZ_TOPRIGHT)
4401 lprc->top += h_offset;
4402 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4403 || fwSide == WMSZ_BOTTOMRIGHT)
4404 lprc->bottom -= h_offset;
4405 return TRUE;
4406}
4407
4408
4409
4410 static LRESULT CALLBACK
4411_WndProc(
4412 HWND hwnd,
4413 UINT uMsg,
4414 WPARAM wParam,
4415 LPARAM lParam)
4416{
4417 /*
4418 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4419 hwnd, uMsg, wParam, lParam);
4420 */
4421
4422 HandleMouseHide(uMsg, lParam);
4423
4424 s_uMsg = uMsg;
4425 s_wParam = wParam;
4426 s_lParam = lParam;
4427
4428 switch (uMsg)
4429 {
4430 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4431 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4432 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4433 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4434 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4435 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4436 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4437 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4438 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4439#ifdef FEAT_MENU
4440 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4441#endif
4442 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4443 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4444 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4445 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4446 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4447 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4448 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4449 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4450 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4451#ifdef FEAT_NETBEANS_INTG
4452 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4453#endif
4454
Bram Moolenaarafa24992006-03-27 20:58:26 +00004455#ifdef FEAT_GUI_TABLINE
4456 case WM_RBUTTONUP:
4457 {
4458 if (gui_mch_showing_tabline())
4459 {
4460 POINT pt;
4461 RECT rect;
4462
4463 /*
4464 * If the cursor is on the tabline, display the tab menu
4465 */
4466 GetCursorPos((LPPOINT)&pt);
4467 GetWindowRect(s_textArea, &rect);
4468 if (pt.y < rect.top)
4469 {
4470 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004471 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004472 }
4473 }
4474 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4475 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004476 case WM_LBUTTONDBLCLK:
4477 {
4478 /*
4479 * If the user double clicked the tabline, create a new tab
4480 */
4481 if (gui_mch_showing_tabline())
4482 {
4483 POINT pt;
4484 RECT rect;
4485
4486 GetCursorPos((LPPOINT)&pt);
4487 GetWindowRect(s_textArea, &rect);
4488 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004489 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004490 }
4491 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4492 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004493#endif
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 case WM_QUERYENDSESSION: /* System wants to go down. */
4496 gui_shell_closed(); /* Will exit when no changed buffers. */
4497 return FALSE; /* Do NOT allow system to go down. */
4498
4499 case WM_ENDSESSION:
4500 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004503 return 0L;
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 case WM_CHAR:
4508 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4509 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004510 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 return 0L;
4512
4513 case WM_SYSCHAR:
4514 /*
4515 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4516 * shortcut key, handle like a typed ALT key, otherwise call Windows
4517 * ALT key handling.
4518 */
4519#ifdef FEAT_MENU
4520 if ( !gui.menu_is_active
4521 || p_wak[0] == 'n'
4522 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4523 )
4524#endif
4525 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004526 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 return 0L;
4528 }
4529#ifdef FEAT_MENU
4530 else
4531 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4532#endif
4533
4534 case WM_SYSKEYUP:
4535#ifdef FEAT_MENU
4536 /* This used to be done only when menu is active: ALT key is used for
4537 * that. But that caused problems when menu is disabled and using
4538 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4539 * are received, mouse pointer remains hidden. */
4540 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4541#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004542 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543#endif
4544
4545 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004546 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 case WM_MOUSEWHEEL:
4549 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004550 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar520470a2005-06-16 21:59:56 +00004552 /* Notification for change in SystemParametersInfo() */
4553 case WM_SETTINGCHANGE:
4554 return _OnSettingChange((UINT)wParam);
4555
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004556#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 case WM_NOTIFY:
4558 switch (((LPNMHDR) lParam)->code)
4559 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004560 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004561 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004563 LPNMHDR hdr = (LPNMHDR)lParam;
4564 char_u *str = NULL;
4565 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004566
Bram Moolenaard23a8232018-02-10 18:45:26 +01004567 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004568
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004569# ifdef FEAT_GUI_TABLINE
4570 if (gui_mch_showing_tabline()
4571 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004572 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004573 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004574 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004575 * Mouse is over the GUI tabline. Display the
4576 * tooltip for the tab under the cursor
4577 *
4578 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004579 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004580 GetCursorPos(&pt);
4581 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004582 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004583 TCHITTESTINFO htinfo;
4584 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004585
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004586 /*
4587 * Get the tab under the cursor
4588 */
4589 htinfo.pt.x = pt.x;
4590 htinfo.pt.y = pt.y;
4591 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4592 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004593 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004594 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004595
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004596 tp = find_tabpage(idx + 1);
4597 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004598 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004599 get_tabline_label(tp, TRUE);
4600 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004601 }
4602 }
4603 }
4604 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004605# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004606# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004607# ifdef FEAT_GUI_TABLINE
4608 else
4609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004611 UINT idButton;
4612 vimmenu_T *pMenu;
4613
4614 idButton = (UINT) hdr->idFrom;
4615 pMenu = gui_mswin_find_menu(root_menu, idButton);
4616 if (pMenu)
4617 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004619# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004620 if (str != NULL)
4621 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004622 if (hdr->code == TTN_GETDISPINFOW)
4623 {
4624 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4625
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004626 /* Set the maximum width, this also enables using
4627 * \n for line break. */
4628 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4629 0, 500);
4630
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004631 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004632 lpdi->lpszText = tt_text;
4633 /* can't show tooltip if failed */
4634 }
4635 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004636 {
4637 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4638
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004639 /* Set the maximum width, this also enables using
4640 * \n for line break. */
4641 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4642 0, 500);
4643
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004644 if (STRLEN(str) < sizeof(lpdi->szText)
4645 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004646 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004647 sizeof(lpdi->szText) - 1);
4648 else
4649 lpdi->lpszText = tt_text;
4650 }
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
4653 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004654# ifdef FEAT_GUI_TABLINE
4655 case TCN_SELCHANGE:
4656 if (gui_mch_showing_tabline()
4657 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004658 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004659 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004660 return 0L;
4661 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004662 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004663
4664 case NM_RCLICK:
4665 if (gui_mch_showing_tabline()
4666 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004667 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004668 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004669 return 0L;
4670 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004671 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004674# ifdef FEAT_GUI_TABLINE
4675 if (gui_mch_showing_tabline()
4676 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4677 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 break;
4680 }
4681 break;
4682#endif
4683#if defined(MENUHINTS) && defined(FEAT_MENU)
4684 case WM_MENUSELECT:
4685 if (((UINT) HIWORD(wParam)
4686 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4687 == MF_HILITE
4688 && (State & CMDLINE) == 0)
4689 {
4690 UINT idButton;
4691 vimmenu_T *pMenu;
4692 static int did_menu_tip = FALSE;
4693
4694 if (did_menu_tip)
4695 {
4696 msg_clr_cmdline();
4697 setcursor();
4698 out_flush();
4699 did_menu_tip = FALSE;
4700 }
4701
4702 idButton = (UINT)LOWORD(wParam);
4703 pMenu = gui_mswin_find_menu(root_menu, idButton);
4704 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4705 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4706 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004707 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004708 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004709 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 setcursor();
4711 out_flush();
4712 did_menu_tip = TRUE;
4713 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004714 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 }
4716 break;
4717#endif
4718 case WM_NCHITTEST:
4719 {
4720 LRESULT result;
4721 int x, y;
4722 int xPos = GET_X_LPARAM(lParam);
4723
4724 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4725 if (result == HTCLIENT)
4726 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004727#ifdef FEAT_GUI_TABLINE
4728 if (gui_mch_showing_tabline())
4729 {
4730 int yPos = GET_Y_LPARAM(lParam);
4731 RECT rct;
4732
4733 /* If the cursor is on the GUI tabline, don't process this
4734 * event */
4735 GetWindowRect(s_textArea, &rct);
4736 if (yPos < rct.top)
4737 return result;
4738 }
4739#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004740 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 xPos -= x;
4742
4743 if (xPos < 48) /* <VN> TODO should use system metric? */
4744 return HTBOTTOMLEFT;
4745 else
4746 return HTBOTTOMRIGHT;
4747 }
4748 else
4749 return result;
4750 }
4751 /* break; notreached */
4752
4753#ifdef FEAT_MBYTE_IME
4754 case WM_IME_NOTIFY:
4755 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4756 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004757 return 1L;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 case WM_IME_COMPOSITION:
4760 if (!_OnImeComposition(hwnd, wParam, lParam))
4761 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004762 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763#endif
4764
4765 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004767 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#endif
4770 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4771 }
4772
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004773 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
4775
4776/*
4777 * End of call-back routines
4778 */
4779
4780/* parent window, if specified with -P */
4781HWND vim_parent_hwnd = NULL;
4782
4783 static BOOL CALLBACK
4784FindWindowTitle(HWND hwnd, LPARAM lParam)
4785{
4786 char buf[2048];
4787 char *title = (char *)lParam;
4788
4789 if (GetWindowText(hwnd, buf, sizeof(buf)))
4790 {
4791 if (strstr(buf, title) != NULL)
4792 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004793 /* Found it. Store the window ref. and quit searching if MDI
4794 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004796 if (vim_parent_hwnd != NULL)
4797 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 }
4799 }
4800 return TRUE; /* continue searching */
4801}
4802
4803/*
4804 * Invoked for '-P "title"' argument: search for parent application to open
4805 * our window in.
4806 */
4807 void
4808gui_mch_set_parent(char *title)
4809{
4810 EnumWindows(FindWindowTitle, (LPARAM)title);
4811 if (vim_parent_hwnd == NULL)
4812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004813 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 mch_exit(2);
4815 }
4816}
4817
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 static void
4820ole_error(char *arg)
4821{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004822 char buf[IOSIZE];
4823
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004824 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004825 vim_snprintf(buf, IOSIZE,
4826 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4827 arg);
4828 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004832#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4833 static char *
4834gvim_error(void)
4835{
4836 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4837
4838 if (starting)
4839 {
4840 mch_errmsg(msg);
4841 mch_errmsg("\n");
4842 mch_exit(2);
4843 }
4844 return msg;
4845}
4846
4847 char *
4848gui_mch_do_spawn(char_u *arg)
4849{
4850 int len;
4851# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4852 char_u *session = NULL;
4853 LPWSTR tofree1 = NULL;
4854# endif
4855 WCHAR name[MAX_PATH];
4856 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4857 STARTUPINFOW si = {sizeof(si)};
4858 PROCESS_INFORMATION pi;
4859
4860 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4861 goto error;
4862 p = wcsrchr(name, L'\\');
4863 if (p == NULL)
4864 goto error;
4865 // Replace the executable name from vim(d).exe to gvim(d).exe.
4866# ifdef DEBUG
4867 wcscpy(p + 1, L"gvimd.exe");
4868# else
4869 wcscpy(p + 1, L"gvim.exe");
4870# endif
4871
4872# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4873 if (starting)
4874# endif
4875 {
4876 // Pass the command line to the new process.
4877 p = GetCommandLineW();
4878 // Skip 1st argument.
4879 while (*p && *p != L' ' && *p != L'\t')
4880 {
4881 if (*p == L'"')
4882 {
4883 while (*p && *p != L'"')
4884 ++p;
4885 if (*p)
4886 ++p;
4887 }
4888 else
4889 ++p;
4890 }
4891 cmd = p;
4892 }
4893# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4894 else
4895 {
4896 // Create a session file and pass it to the new process.
4897 LPWSTR wsession;
4898 char_u *savebg;
4899 int ret;
4900
4901 session = vim_tempname('s', FALSE);
4902 if (session == NULL)
4903 goto error;
4904 savebg = p_bg;
4905 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4906 ret = write_session_file(session);
4907 vim_free(p_bg);
4908 p_bg = savebg;
4909 if (!ret)
4910 goto error;
4911 wsession = enc_to_utf16(session, NULL);
4912 if (wsession == NULL)
4913 goto error;
4914 len = (int)wcslen(wsession) * 2 + 27 + 1;
4915 cmd = (LPWSTR)alloc(len * (int)sizeof(WCHAR));
4916 if (cmd == NULL)
4917 {
4918 vim_free(wsession);
4919 goto error;
4920 }
4921 tofree1 = cmd;
4922 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4923 wsession, wsession);
4924 vim_free(wsession);
4925 }
4926# endif
4927
4928 // Check additional arguments to the `:gui` command.
4929 if (arg != NULL)
4930 {
4931 warg = enc_to_utf16(arg, NULL);
4932 if (warg == NULL)
4933 goto error;
4934 tofree2 = warg;
4935 }
4936 else
4937 warg = L"";
4938
4939 // Set up the new command line.
4940 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
4941 newcmd = (LPWSTR)alloc(len * (int)sizeof(WCHAR));
4942 if (newcmd == NULL)
4943 goto error;
4944 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4945
4946 // Spawn a new GUI process.
4947 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4948 NULL, NULL, &si, &pi))
4949 goto error;
4950 CloseHandle(pi.hProcess);
4951 CloseHandle(pi.hThread);
4952 mch_exit(0);
4953
4954error:
4955# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4956 if (session)
4957 mch_remove(session);
4958 vim_free(session);
4959 vim_free(tofree1);
4960# endif
4961 vim_free(newcmd);
4962 vim_free(tofree2);
4963 return gvim_error();
4964}
4965#endif
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967/*
4968 * Parse the GUI related command-line arguments. Any arguments used are
4969 * deleted from argv, and *argc is decremented accordingly. This is called
4970 * when vim is started, whether or not the GUI has been started.
4971 */
4972 void
4973gui_mch_prepare(int *argc, char **argv)
4974{
4975 int silent = FALSE;
4976 int idx;
4977
4978 /* Check for special OLE command line parameters */
4979 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4980 {
4981 /* Check for a "-silent" argument first. */
4982 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4983 && (argv[2][0] == '-' || argv[2][0] == '/'))
4984 {
4985 silent = TRUE;
4986 idx = 2;
4987 }
4988 else
4989 idx = 1;
4990
4991 /* Register Vim as an OLE Automation server */
4992 if (STRICMP(argv[idx] + 1, "register") == 0)
4993 {
4994#ifdef FEAT_OLE
4995 RegisterMe(silent);
4996 mch_exit(0);
4997#else
4998 if (!silent)
4999 ole_error("register");
5000 mch_exit(2);
5001#endif
5002 }
5003
5004 /* Unregister Vim as an OLE Automation server */
5005 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5006 {
5007#ifdef FEAT_OLE
5008 UnregisterMe(!silent);
5009 mch_exit(0);
5010#else
5011 if (!silent)
5012 ole_error("unregister");
5013 mch_exit(2);
5014#endif
5015 }
5016
5017 /* Ignore an -embedding argument. It is only relevant if the
5018 * application wants to treat the case when it is started manually
5019 * differently from the case where it is started via automation (and
5020 * we don't).
5021 */
5022 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5023 {
5024#ifdef FEAT_OLE
5025 *argc = 1;
5026#else
5027 ole_error("embedding");
5028 mch_exit(2);
5029#endif
5030 }
5031 }
5032
5033#ifdef FEAT_OLE
5034 {
5035 int bDoRestart = FALSE;
5036
5037 InitOLE(&bDoRestart);
5038 /* automatically exit after registering */
5039 if (bDoRestart)
5040 mch_exit(0);
5041 }
5042#endif
5043
5044#ifdef FEAT_NETBEANS_INTG
5045 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005046 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 int arg;
5048
5049 for (arg = 1; arg < *argc; arg++)
5050 if (strncmp("-nb", argv[arg], 3) == 0)
5051 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 netbeansArg = argv[arg];
5053 mch_memmove(&argv[arg], &argv[arg + 1],
5054 (--*argc - arg) * sizeof(char *));
5055 argv[*argc] = NULL;
5056 break; /* enough? */
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060}
5061
5062/*
5063 * Initialise the GUI. Create all the windows, set up all the call-backs
5064 * etc.
5065 */
5066 int
5067gui_mch_init(void)
5068{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005070 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072#ifdef GLOBAL_IME
5073 ATOM atom;
5074#endif
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 /* Return here if the window was already opened (happens when
5077 * gui_mch_dialog() is called early). */
5078 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005079 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * Load the tearoff bitmap
5083 */
5084#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005085 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#endif
5087
5088 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5089 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5090#ifdef FEAT_MENU
5091 gui.menu_height = 0; /* Windows takes care of this */
5092#endif
5093 gui.border_width = 0;
5094
5095 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /* First try using the wide version, so that we can use any title.
5098 * Otherwise only characters in the active codepage will work. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005099 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005101 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 wndclassw.lpfnWndProc = _WndProc;
5103 wndclassw.cbClsExtra = 0;
5104 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005105 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5107 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5108 wndclassw.hbrBackground = s_brush;
5109 wndclassw.lpszMenuName = NULL;
5110 wndclassw.lpszClassName = szVimWndClassW;
5111
5112 if ((
5113#ifdef GLOBAL_IME
5114 atom =
5115#endif
5116 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005117 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (vim_parent_hwnd != NULL)
5121 {
5122#ifdef HAVE_TRY_EXCEPT
5123 __try
5124 {
5125#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005126 // Open inside the specified parent window.
5127 // TODO: last argument should point to a CLIENTCREATESTRUCT
5128 // structure.
5129 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005131 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005132 WS_OVERLAPPEDWINDOW | WS_CHILD
5133 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5135 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005136 100, // Any value will do
5137 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005139 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#ifdef HAVE_TRY_EXCEPT
5141 }
5142 __except(EXCEPTION_EXECUTE_HANDLER)
5143 {
5144 /* NOP */
5145 }
5146#endif
5147 if (s_hwnd == NULL)
5148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005149 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 mch_exit(2);
5151 }
5152 }
5153 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005154 {
5155 /* If the provided windowid is not valid reset it to zero, so that it
5156 * is ignored and we open our own window. */
5157 if (IsWindow((HWND)win_socket_id) <= 0)
5158 win_socket_id = 0;
5159
5160 /* Create a window. If win_socket_id is not zero without border and
5161 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005162 s_hwnd = CreateWindowW(
5163 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005164 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5165 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005166 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5167 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5168 100, /* Any value will do */
5169 100, /* Any value will do */
5170 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005171 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005172 if (s_hwnd != NULL && win_socket_id != 0)
5173 {
5174 SetParent(s_hwnd, (HWND)win_socket_id);
5175 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5176 }
5177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
5179 if (s_hwnd == NULL)
5180 return FAIL;
5181
5182#ifdef GLOBAL_IME
5183 global_ime_init(atom, s_hwnd);
5184#endif
5185#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5186 dyn_imm_load();
5187#endif
5188
5189 /* Create the text area window */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005190 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005191 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005192 wndclassw.style = CS_OWNDC;
5193 wndclassw.lpfnWndProc = _TextAreaWndProc;
5194 wndclassw.cbClsExtra = 0;
5195 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005196 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005197 wndclassw.hIcon = NULL;
5198 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5199 wndclassw.hbrBackground = NULL;
5200 wndclassw.lpszMenuName = NULL;
5201 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005202
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005203 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return FAIL;
5205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005207 s_textArea = CreateWindowExW(
5208 0,
5209 szTextAreaClassW, L"Vim text area",
5210 WS_CHILD | WS_VISIBLE, 0, 0,
5211 100, // Any value will do for now
5212 100, // Any value will do for now
5213 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005214 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005215
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 if (s_textArea == NULL)
5217 return FAIL;
5218
Bram Moolenaar20321902016-02-17 12:30:17 +01005219#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005220 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5221 {
5222 HANDLE hIcon = NULL;
5223
5224 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005225 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005226 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005227#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#ifdef FEAT_MENU
5230 s_menuBar = CreateMenu();
5231#endif
5232 s_hdc = GetDC(s_textArea);
5233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 /* Do we need to bother with this? */
5237 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5238
5239 /* Get background/foreground colors from the system */
5240 gui_mch_def_colors();
5241
5242 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5243 * file) */
5244 set_normal_colors();
5245
5246 /*
5247 * Check that none of the colors are the same as the background color.
5248 * Then store the current values as the defaults.
5249 */
5250 gui_check_colors();
5251 gui.def_norm_pixel = gui.norm_pixel;
5252 gui.def_back_pixel = gui.back_pixel;
5253
5254 /* Get the colors for the highlight groups (gui_check_colors() might have
5255 * changed them) */
5256 highlight_gui_started();
5257
5258 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005259 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005261 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
5263 /*
5264 * Set up for Intellimouse processing
5265 */
5266 init_mouse_wheel();
5267
5268 /*
5269 * compute a couple of metrics used for the dialogs
5270 */
5271 get_dialog_font_metrics();
5272#ifdef FEAT_TOOLBAR
5273 /*
5274 * Create the toolbar
5275 */
5276 initialise_toolbar();
5277#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005278#ifdef FEAT_GUI_TABLINE
5279 /*
5280 * Create the tabline
5281 */
5282 initialise_tabline();
5283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#ifdef MSWIN_FIND_REPLACE
5285 /*
5286 * Initialise the dialog box stuff
5287 */
5288 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5289
5290 /* Initialise the struct */
5291 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 s_findrep_struct.lpstrFindWhat =
5293 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005295 s_findrep_struct.lpstrReplaceWith =
5296 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5298 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5299 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005302#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005303# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5304/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5305# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005306# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005307# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005308# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005309 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005310 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005311#endif
5312
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005313#ifdef FEAT_RENDER_OPTIONS
5314 if (p_rop)
5315 (void)gui_mch_set_rendering_options(p_rop);
5316#endif
5317
Bram Moolenaar748bf032005-02-02 23:04:36 +00005318theend:
5319 /* Display any pending error messages */
5320 display_errors();
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 return OK;
5323}
5324
5325/*
5326 * Get the size of the screen, taking position on multiple monitors into
5327 * account (if supported).
5328 */
5329 static void
5330get_work_area(RECT *spi_rect)
5331{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005332 HMONITOR mon;
5333 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005335 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005336 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005337 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005339 moninfo.cbSize = sizeof(MONITORINFO);
5340 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005342 *spi_rect = moninfo.rcWork;
5343 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 }
5346 /* this is the old method... */
5347 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5348}
5349
5350/*
5351 * Set the size of the window to the given width and height in pixels.
5352 */
5353 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005354gui_mch_set_shellsize(
5355 int width,
5356 int height,
5357 int min_width UNUSED,
5358 int min_height UNUSED,
5359 int base_width UNUSED,
5360 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005361 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 RECT workarea_rect;
5364 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 WINDOWPLACEMENT wndpl;
5366
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005367 /* Try to keep window completely on screen. */
5368 /* Get position of the screen work area. This is the part that is not
5369 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 get_work_area(&workarea_rect);
5371
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005372 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005373 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 wndpl.length = sizeof(WINDOWPLACEMENT);
5375 GetWindowPlacement(s_hwnd, &wndpl);
5376
5377 /* Resizing a maximized window looks very strange, unzoom it first.
5378 * But don't do it when still starting up, it may have been requested in
5379 * the shortcut. */
5380 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5381 {
5382 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5383 /* Need to get the settings of the normal window. */
5384 GetWindowPlacement(s_hwnd, &wndpl);
5385 }
5386
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005388 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005389 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005390 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005391 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 + GetSystemMetrics(SM_CYCAPTION)
5393#ifdef FEAT_MENU
5394 + gui_mswin_get_menu_height(FALSE)
5395#endif
5396 ;
5397
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005398 /* The following should take care of keeping Vim on the same monitor, no
5399 * matter if the secondary monitor is left or right of the primary
5400 * monitor. */
5401 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5402 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005403
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005404 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005405 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005406 && wndpl.rcNormalPosition.right > workarea_rect.right)
5407 OffsetRect(&wndpl.rcNormalPosition,
5408 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005410 if ((direction & RESIZE_HOR)
5411 && wndpl.rcNormalPosition.left < workarea_rect.left)
5412 OffsetRect(&wndpl.rcNormalPosition,
5413 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
Bram Moolenaarafa24992006-03-27 20:58:26 +00005415 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005416 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5417 OffsetRect(&wndpl.rcNormalPosition,
5418 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005420 if ((direction & RESIZE_VERT)
5421 && wndpl.rcNormalPosition.top < workarea_rect.top)
5422 OffsetRect(&wndpl.rcNormalPosition,
5423 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425 /* set window position - we should use SetWindowPlacement rather than
5426 * SetWindowPos as the MSDN docs say the coord systems returned by
5427 * these two are not compatible. */
5428 SetWindowPlacement(s_hwnd, &wndpl);
5429
5430 SetActiveWindow(s_hwnd);
5431 SetFocus(s_hwnd);
5432
5433#ifdef FEAT_MENU
5434 /* Menu may wrap differently now */
5435 gui_mswin_get_menu_height(!gui.starting);
5436#endif
5437}
5438
5439
5440 void
5441gui_mch_set_scrollbar_thumb(
5442 scrollbar_T *sb,
5443 long val,
5444 long size,
5445 long max)
5446{
5447 SCROLLINFO info;
5448
5449 sb->scroll_shift = 0;
5450 while (max > 32767)
5451 {
5452 max = (max + 1) >> 1;
5453 val >>= 1;
5454 size >>= 1;
5455 ++sb->scroll_shift;
5456 }
5457
5458 if (sb->scroll_shift > 0)
5459 ++size;
5460
5461 info.cbSize = sizeof(info);
5462 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5463 info.nPos = val;
5464 info.nMin = 0;
5465 info.nMax = max;
5466 info.nPage = size;
5467 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5468}
5469
5470
5471/*
5472 * Set the current text font.
5473 */
5474 void
5475gui_mch_set_font(GuiFont font)
5476{
5477 gui.currFont = font;
5478}
5479
5480
5481/*
5482 * Set the current text foreground color.
5483 */
5484 void
5485gui_mch_set_fg_color(guicolor_T color)
5486{
5487 gui.currFgColor = color;
5488}
5489
5490/*
5491 * Set the current text background color.
5492 */
5493 void
5494gui_mch_set_bg_color(guicolor_T color)
5495{
5496 gui.currBgColor = color;
5497}
5498
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005499/*
5500 * Set the current text special color.
5501 */
5502 void
5503gui_mch_set_sp_color(guicolor_T color)
5504{
5505 gui.currSpColor = color;
5506}
5507
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005508#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509/*
5510 * Multi-byte handling, originally by Sung-Hoon Baek.
5511 * First static functions (no prototypes generated).
5512 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005513# ifdef _MSC_VER
5514# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5515# endif
5516# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 * handle WM_IME_NOTIFY message
5520 */
5521 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005522_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 LRESULT lResult = 0;
5525 HIMC hImc;
5526
5527 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5528 return lResult;
5529 switch (dwCommand)
5530 {
5531 case IMN_SETOPENSTATUS:
5532 if (pImmGetOpenStatus(hImc))
5533 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005534 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 im_set_position(gui.row, gui.col);
5536
5537 /* Disable langmap */
5538 State &= ~LANGMAP;
5539 if (State & INSERT)
5540 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005541#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 /* Unshown 'keymap' in status lines */
5543 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5544 {
5545 /* Save cursor position */
5546 int old_row = gui.row;
5547 int old_col = gui.col;
5548
5549 // This must be called here before
5550 // status_redraw_curbuf(), otherwise the mode
5551 // message may appear in the wrong position.
5552 showmode();
5553 status_redraw_curbuf();
5554 update_screen(0);
5555 /* Restore cursor position */
5556 gui.row = old_row;
5557 gui.col = old_col;
5558 }
5559#endif
5560 }
5561 }
5562 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005563 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 lResult = 0;
5565 break;
5566 }
5567 pImmReleaseContext(hWnd, hImc);
5568 return lResult;
5569}
5570
5571 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005572_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
5574 char_u *ret;
5575 int len;
5576
5577 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5578 return 0;
5579
5580 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5581 if (ret != NULL)
5582 {
5583 add_to_input_buf_csi(ret, len);
5584 vim_free(ret);
5585 return 1;
5586 }
5587 return 0;
5588}
5589
5590/*
5591 * get the current composition string, in UCS-2; *lenp is the number of
5592 * *lenp is the number of Unicode characters.
5593 */
5594 static short_u *
5595GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5596{
5597 LONG ret;
5598 LPWSTR wbuf = NULL;
5599 char_u *buf;
5600
5601 if (!pImmGetContext)
5602 return NULL; /* no imm32.dll */
5603
5604 /* Try Unicode; this'll always work on NT regardless of codepage. */
5605 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5606 if (ret == 0)
5607 return NULL; /* empty */
5608
5609 if (ret > 0)
5610 {
5611 /* Allocate the requested buffer plus space for the NUL character. */
5612 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5613 if (wbuf != NULL)
5614 {
5615 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5616 *lenp = ret / sizeof(WCHAR);
5617 }
5618 return (short_u *)wbuf;
5619 }
5620
5621 /* ret < 0; we got an error, so try the ANSI version. This'll work
5622 * on 9x/ME, but only if the codepage happens to be set to whatever
5623 * we're inputting. */
5624 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5625 if (ret <= 0)
5626 return NULL; /* empty or error */
5627
5628 buf = alloc(ret);
5629 if (buf == NULL)
5630 return NULL;
5631 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5632
5633 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005634 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 vim_free(buf);
5636
5637 return (short_u *)wbuf;
5638}
5639
5640/*
5641 * void GetResultStr()
5642 *
5643 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5644 * get complete composition string
5645 */
5646 static char_u *
5647GetResultStr(HWND hwnd, int GCS, int *lenp)
5648{
5649 HIMC hIMC; /* Input context handle. */
5650 short_u *buf = NULL;
5651 char_u *convbuf = NULL;
5652
5653 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5654 return NULL;
5655
5656 /* Reads in the composition string. */
5657 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5658 if (buf == NULL)
5659 return NULL;
5660
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005661 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 pImmReleaseContext(hwnd, hIMC);
5663 vim_free(buf);
5664 return convbuf;
5665}
5666#endif
5667
5668/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005669#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671/*
5672 * set font to IM.
5673 */
5674 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005675im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 HIMC hImc;
5678
5679 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5680 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005681 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 pImmReleaseContext(s_hwnd, hImc);
5683 }
5684}
5685
5686/*
5687 * Notify cursor position to IM.
5688 */
5689 void
5690im_set_position(int row, int col)
5691{
5692 HIMC hImc;
5693
5694 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5695 {
5696 COMPOSITIONFORM cfs;
5697
5698 cfs.dwStyle = CFS_POINT;
5699 cfs.ptCurrentPos.x = FILL_X(col);
5700 cfs.ptCurrentPos.y = FILL_Y(row);
5701 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5702 pImmSetCompositionWindow(hImc, &cfs);
5703
5704 pImmReleaseContext(s_hwnd, hImc);
5705 }
5706}
5707
5708/*
5709 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5710 */
5711 void
5712im_set_active(int active)
5713{
5714 HIMC hImc;
5715 static HIMC hImcOld = (HIMC)0;
5716
5717 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5718 {
5719 if (p_imdisable)
5720 {
5721 if (hImcOld == (HIMC)0)
5722 {
5723 hImcOld = pImmGetContext(s_hwnd);
5724 if (hImcOld)
5725 pImmAssociateContext(s_hwnd, (HIMC)0);
5726 }
5727 active = FALSE;
5728 }
5729 else if (hImcOld != (HIMC)0)
5730 {
5731 pImmAssociateContext(s_hwnd, hImcOld);
5732 hImcOld = (HIMC)0;
5733 }
5734
5735 hImc = pImmGetContext(s_hwnd);
5736 if (hImc)
5737 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005738 /*
5739 * for Korean ime
5740 */
5741 HKL hKL = GetKeyboardLayout(0);
5742
5743 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5744 {
5745 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5746 static BOOL bSaved = FALSE;
5747
5748 if (active)
5749 {
5750 /* if we have a saved conversion status, restore it */
5751 if (bSaved)
5752 pImmSetConversionStatus(hImc, dwConversionSaved,
5753 dwSentenceSaved);
5754 bSaved = FALSE;
5755 }
5756 else
5757 {
5758 /* save conversion status and disable korean */
5759 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5760 &dwSentenceSaved))
5761 {
5762 bSaved = TRUE;
5763 pImmSetConversionStatus(hImc,
5764 dwConversionSaved & ~(IME_CMODE_NATIVE
5765 | IME_CMODE_FULLSHAPE),
5766 dwSentenceSaved);
5767 }
5768 }
5769 }
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 pImmSetOpenStatus(hImc, active);
5772 pImmReleaseContext(s_hwnd, hImc);
5773 }
5774 }
5775}
5776
5777/*
5778 * Get IM status. When IM is on, return not 0. Else return 0.
5779 */
5780 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005781im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 int status = 0;
5784 HIMC hImc;
5785
5786 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5787 {
5788 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5789 pImmReleaseContext(s_hwnd, hImc);
5790 }
5791 return status;
5792}
5793
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005794#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005796#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/* Win32 with GLOBAL IME */
5798
5799/*
5800 * Notify cursor position to IM.
5801 */
5802 void
5803im_set_position(int row, int col)
5804{
5805 /* Win32 with GLOBAL IME */
5806 POINT p;
5807
5808 p.x = FILL_X(col);
5809 p.y = FILL_Y(row);
5810 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5811 global_ime_set_position(&p);
5812}
5813
5814/*
5815 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5816 */
5817 void
5818im_set_active(int active)
5819{
5820 global_ime_set_status(active);
5821}
5822
5823/*
5824 * Get IM status. When IM is on, return not 0. Else return 0.
5825 */
5826 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005827im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 return global_ime_get_status();
5830}
5831#endif
5832
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005833/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005834 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005835 */
5836 static void
5837latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5838{
5839 int c;
5840
Bram Moolenaarca003e12006-03-17 23:19:38 +00005841 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005842 {
5843 c = *text++;
5844 switch (c)
5845 {
5846 case 0xa4: c = 0x20ac; break; /* euro */
5847 case 0xa6: c = 0x0160; break; /* S hat */
5848 case 0xa8: c = 0x0161; break; /* S -hat */
5849 case 0xb4: c = 0x017d; break; /* Z hat */
5850 case 0xb8: c = 0x017e; break; /* Z -hat */
5851 case 0xbc: c = 0x0152; break; /* OE */
5852 case 0xbd: c = 0x0153; break; /* oe */
5853 case 0xbe: c = 0x0178; break; /* Y */
5854 }
5855 *unicodebuf++ = c;
5856 }
5857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
5859#ifdef FEAT_RIGHTLEFT
5860/*
5861 * What is this for? In the case where you are using Win98 or Win2K or later,
5862 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5863 * reverses the string sent to the TextOut... family. This sucks, because we
5864 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5865 * way to tell Windblows not to do this!
5866 *
5867 * The short of it is that this 'RevOut' only gets called if you are running
5868 * one of the new, "improved" MS OSes, and only if you are running in
5869 * 'rightleft' mode. It makes display take *slightly* longer, but not
5870 * noticeably so.
5871 */
5872 static void
5873RevOut( HDC s_hdc,
5874 int col,
5875 int row,
5876 UINT foptions,
5877 CONST RECT *pcliprect,
5878 LPCTSTR text,
5879 UINT len,
5880 CONST INT *padding)
5881{
5882 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005884 for (ix = 0; ix < (int)len; ++ix)
5885 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5886 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887}
5888#endif
5889
Bram Moolenaar92467d32017-12-05 13:22:16 +01005890 static void
5891draw_line(
5892 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005893 int y1,
5894 int x2,
5895 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005896 COLORREF color)
5897{
5898#if defined(FEAT_DIRECTX)
5899 if (IS_ENABLE_DIRECTX())
5900 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5901 else
5902#endif
5903 {
5904 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5905 HPEN old_pen = SelectObject(s_hdc, hpen);
5906 MoveToEx(s_hdc, x1, y1, NULL);
5907 /* Note: LineTo() excludes the last pixel in the line. */
5908 LineTo(s_hdc, x2, y2);
5909 DeleteObject(SelectObject(s_hdc, old_pen));
5910 }
5911}
5912
5913 static void
5914set_pixel(
5915 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005916 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005917 COLORREF color)
5918{
5919#if defined(FEAT_DIRECTX)
5920 if (IS_ENABLE_DIRECTX())
5921 DWriteContext_SetPixel(s_dwc, x, y, color);
5922 else
5923#endif
5924 SetPixel(s_hdc, x, y, color);
5925}
5926
5927 static void
5928fill_rect(
5929 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005930 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005931 COLORREF color)
5932{
5933#if defined(FEAT_DIRECTX)
5934 if (IS_ENABLE_DIRECTX())
5935 DWriteContext_FillRect(s_dwc, rcp, color);
5936 else
5937#endif
5938 {
5939 HBRUSH hbr2;
5940
5941 if (hbr == NULL)
5942 hbr2 = CreateSolidBrush(color);
5943 else
5944 hbr2 = hbr;
5945 FillRect(s_hdc, rcp, hbr2);
5946 if (hbr == NULL)
5947 DeleteBrush(hbr2);
5948 }
5949}
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 void
5952gui_mch_draw_string(
5953 int row,
5954 int col,
5955 char_u *text,
5956 int len,
5957 int flags)
5958{
5959 static int *padding = NULL;
5960 static int pad_size = 0;
5961 int i;
5962 const RECT *pcliprect = NULL;
5963 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 static WCHAR *unicodebuf = NULL;
5965 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005966 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 int y;
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 /*
5971 * Italic and bold text seems to have an extra row of pixels at the bottom
5972 * (below where the bottom of the character should be). If we draw the
5973 * characters with a solid background, the top row of pixels in the
5974 * character below will be overwritten. We can fix this by filling in the
5975 * background ourselves, to the correct character proportions, and then
5976 * writing the character in transparent mode. Still have a problem when
5977 * the character is "_", which gets written on to the character below.
5978 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5979 * pixel in their slots, which fixes the problem with the bottom row of
5980 * pixels. We still need this code because otherwise the top row of pixels
5981 * becomes a problem. - webb.
5982 */
5983 static HBRUSH hbr_cache[2] = {NULL, NULL};
5984 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
5985 static int brush_lru = 0;
5986 HBRUSH hbr;
5987 RECT rc;
5988
5989 if (!(flags & DRAW_TRANSP))
5990 {
5991 /*
5992 * Clear background first.
5993 * Note: FillRect() excludes right and bottom of rectangle.
5994 */
5995 rc.left = FILL_X(col);
5996 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (has_mbyte)
5998 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006000 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 rc.right = FILL_X(col + len);
6004 rc.bottom = FILL_Y(row + 1);
6005
6006 /* Cache the created brush, that saves a lot of time. We need two:
6007 * one for cursor background and one for the normal background. */
6008 if (gui.currBgColor == brush_color[0])
6009 {
6010 hbr = hbr_cache[0];
6011 brush_lru = 1;
6012 }
6013 else if (gui.currBgColor == brush_color[1])
6014 {
6015 hbr = hbr_cache[1];
6016 brush_lru = 0;
6017 }
6018 else
6019 {
6020 if (hbr_cache[brush_lru] != NULL)
6021 DeleteBrush(hbr_cache[brush_lru]);
6022 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6023 brush_color[brush_lru] = gui.currBgColor;
6024 hbr = hbr_cache[brush_lru];
6025 brush_lru = !brush_lru;
6026 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006027
Bram Moolenaar92467d32017-12-05 13:22:16 +01006028 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
6030 SetBkMode(s_hdc, TRANSPARENT);
6031
6032 /*
6033 * When drawing block cursor, prevent inverted character spilling
6034 * over character cell (can happen with bold/italic)
6035 */
6036 if (flags & DRAW_CURSOR)
6037 {
6038 pcliprect = &rc;
6039 foptions = ETO_CLIPPED;
6040 }
6041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 SetTextColor(s_hdc, gui.currFgColor);
6043 SelectFont(s_hdc, gui.currFont);
6044
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006045#ifdef FEAT_DIRECTX
6046 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006047 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006048#endif
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6051 {
6052 vim_free(padding);
6053 pad_size = Columns;
6054
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006055 /* Don't give an out-of-memory message here, it would call us
6056 * recursively. */
6057 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (padding != NULL)
6059 for (i = 0; i < pad_size; i++)
6060 padding[i] = gui.char_width;
6061 }
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 /*
6064 * We have to provide the padding argument because italic and bold versions
6065 * of fixed-width fonts are often one pixel or so wider than their normal
6066 * versions.
6067 * No check for DRAW_BOLD, Windows will have done it already.
6068 */
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 /* Check if there are any UTF-8 characters. If not, use normal text
6071 * output to speed up output. */
6072 if (enc_utf8)
6073 for (n = 0; n < len; ++n)
6074 if (text[n] >= 0x80)
6075 break;
6076
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006077#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006078 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6079 * required that unicode drawing routine, currently. So this forces it
6080 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006081 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006082 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006083#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006086 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006088 if ((enc_utf8
6089 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6090 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 && (unicodebuf == NULL || len > unibuflen))
6092 {
6093 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006094 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095
6096 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006097 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 unibuflen = len;
6100 }
6101
6102 if (enc_utf8 && n < len && unicodebuf != NULL)
6103 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006104 /* Output UTF-8 characters. Composing characters should be
6105 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006106 int i;
6107 int wlen; /* string length in words */
6108 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 int cells; /* cell width of string up to composing char */
6110 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006111 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006113 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006114 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006116 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006118 c = utf_ptr2char(text + i);
6119 if (c >= 0x10000)
6120 {
6121 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006122 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6123 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006124 }
6125 else
6126 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006127 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006128 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006129
6130 if (utf_iscomposing(c))
6131 cw = 0;
6132 else
6133 {
6134 cw = utf_char2cells(c);
6135 if (cw > 2) /* don't use 4 for unprintable char */
6136 cw = 1;
6137 }
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 if (unicodepdy != NULL)
6140 {
6141 /* Use unicodepdy to make characters fit as we expect, even
6142 * when the font uses different widths (e.g., bold character
6143 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006144 if (c >= 0x10000)
6145 {
6146 unicodepdy[wlen - 2] = cw * gui.char_width;
6147 unicodepdy[wlen - 1] = 0;
6148 }
6149 else
6150 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
6152 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006153 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006154 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006156#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006157 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006158 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006159 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006160 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006161 TEXT_X(col), TEXT_Y(row),
6162 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006163 gui.char_width, gui.currFgColor,
6164 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006165 }
6166 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006167#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006168 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6169 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 len = cells; /* used for underlining */
6171 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006172 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
6174 /* If we want to display codepage data, and the current CP is not the
6175 * ANSI one, we need to go via Unicode. */
6176 if (unicodebuf != NULL)
6177 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006178 if (enc_latin9)
6179 latin9_to_ucs(text, len, unicodebuf);
6180 else
6181 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 MB_PRECOMPOSED,
6183 (char *)text, len,
6184 (LPWSTR)unicodebuf, unibuflen);
6185 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006186 {
6187 /* Use unicodepdy to make characters fit as we expect, even
6188 * when the font uses different widths (e.g., bold character
6189 * is wider). */
6190 if (unicodepdy != NULL)
6191 {
6192 int i;
6193 int cw;
6194
6195 for (i = 0; i < len; ++i)
6196 {
6197 cw = utf_char2cells(unicodebuf[i]);
6198 if (cw > 2)
6199 cw = 1;
6200 unicodepdy[i] = cw * gui.char_width;
6201 }
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006204 foptions, pcliprect, unicodebuf, len, unicodepdy);
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 }
6207 }
6208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
6210#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006211 /* Windows will mess up RL text, so we have to draw it character by
6212 * character. Only do this if RL is on, since it's slow. */
6213 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6215 foptions, pcliprect, (char *)text, len, padding);
6216 else
6217#endif
6218 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6219 foptions, pcliprect, (char *)text, len, padding);
6220 }
6221
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006222 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (flags & DRAW_UNDERL)
6224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 /* When p_linespace is 0, overwrite the bottom row of pixels.
6226 * Otherwise put the line just below the character. */
6227 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (p_linespace > 1)
6229 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006230 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006232
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006233 /* Strikethrough */
6234 if (flags & DRAW_STRIKE)
6235 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006236 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006237 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006238 }
6239
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006240 /* Undercurl */
6241 if (flags & DRAW_UNDERC)
6242 {
6243 int x;
6244 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006245 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006246
6247 y = FILL_Y(row + 1) - 1;
6248 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6249 {
6250 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006251 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006252 }
6253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254}
6255
6256
6257/*
6258 * Output routines.
6259 */
6260
6261/* Flush any output to the screen */
6262 void
6263gui_mch_flush(void)
6264{
6265# if defined(__BORLANDC__)
6266 /*
6267 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6268 * prototype declaration.
6269 * The compiler complains if __stdcall is not used in both declarations.
6270 */
6271 BOOL __stdcall GdiFlush(void);
6272# endif
6273
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006274#if defined(FEAT_DIRECTX)
6275 if (IS_ENABLE_DIRECTX())
6276 DWriteContext_Flush(s_dwc);
6277#endif
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 GdiFlush();
6280}
6281
6282 static void
6283clear_rect(RECT *rcp)
6284{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006285 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286}
6287
6288
Bram Moolenaarc716c302006-01-21 22:12:51 +00006289 void
6290gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6291{
6292 RECT workarea_rect;
6293
6294 get_work_area(&workarea_rect);
6295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006296 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006297 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006298 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006299
6300 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6301 * the menubar for MSwin, we subtract it from the screen height, so that
6302 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006303 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006304 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006305 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006306 - GetSystemMetrics(SM_CYCAPTION)
6307#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006308 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006309#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006310 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006311}
6312
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314#if defined(FEAT_MENU) || defined(PROTO)
6315/*
6316 * Add a sub menu to the menu bar.
6317 */
6318 void
6319gui_mch_add_menu(
6320 vimmenu_T *menu,
6321 int pos)
6322{
6323 vimmenu_T *parent = menu->parent;
6324
6325 menu->submenu_id = CreatePopupMenu();
6326 menu->id = s_menu_id++;
6327
6328 if (menu_is_menubar(menu->name))
6329 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006330 WCHAR *wn;
6331 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006333 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006334 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006335 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006337 infow.cbSize = sizeof(infow);
6338 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6339 | MIIM_SUBMENU;
6340 infow.dwItemData = (long_u)menu;
6341 infow.wID = menu->id;
6342 infow.fType = MFT_STRING;
6343 infow.dwTypeData = wn;
6344 infow.cch = (UINT)wcslen(wn);
6345 infow.hSubMenu = menu->submenu_id;
6346 InsertMenuItemW((parent == NULL)
6347 ? s_menuBar : parent->submenu_id,
6348 (UINT)pos, TRUE, &infow);
6349 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351
6352 /* Fix window size if menu may have wrapped */
6353 if (parent == NULL)
6354 gui_mswin_get_menu_height(!gui.starting);
6355#ifdef FEAT_TEAROFF
6356 else if (IsWindow(parent->tearoff_handle))
6357 rebuild_tearoff(parent);
6358#endif
6359}
6360
6361 void
6362gui_mch_show_popupmenu(vimmenu_T *menu)
6363{
6364 POINT mp;
6365
6366 (void)GetCursorPos((LPPOINT)&mp);
6367 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6368}
6369
6370 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006371gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 vimmenu_T *menu = gui_find_menu(path_name);
6374
6375 if (menu != NULL)
6376 {
6377 POINT p;
6378
6379 /* Find the position of the current cursor */
6380 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006381 if (mouse_pos)
6382 {
6383 int mx, my;
6384
6385 gui_mch_getmouse(&mx, &my);
6386 p.x += mx;
6387 p.y += my;
6388 }
6389 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006391 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6393 }
6394 msg_scroll = FALSE;
6395 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6396 }
6397}
6398
6399#if defined(FEAT_TEAROFF) || defined(PROTO)
6400/*
6401 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6402 * create it as a pseudo-"tearoff menu".
6403 */
6404 void
6405gui_make_tearoff(char_u *path_name)
6406{
6407 vimmenu_T *menu = gui_find_menu(path_name);
6408
6409 /* Found the menu, so tear it off. */
6410 if (menu != NULL)
6411 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6412}
6413#endif
6414
6415/*
6416 * Add a menu item to a menu
6417 */
6418 void
6419gui_mch_add_menu_item(
6420 vimmenu_T *menu,
6421 int idx)
6422{
6423 vimmenu_T *parent = menu->parent;
6424
6425 menu->id = s_menu_id++;
6426 menu->submenu_id = NULL;
6427
6428#ifdef FEAT_TEAROFF
6429 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6430 {
6431 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6432 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6433 }
6434 else
6435#endif
6436#ifdef FEAT_TOOLBAR
6437 if (menu_is_toolbar(parent->name))
6438 {
6439 TBBUTTON newtb;
6440
6441 vim_memset(&newtb, 0, sizeof(newtb));
6442 if (menu_is_separator(menu->name))
6443 {
6444 newtb.iBitmap = 0;
6445 newtb.fsStyle = TBSTYLE_SEP;
6446 }
6447 else
6448 {
6449 newtb.iBitmap = get_toolbar_bitmap(menu);
6450 newtb.fsStyle = TBSTYLE_BUTTON;
6451 }
6452 newtb.idCommand = menu->id;
6453 newtb.fsState = TBSTATE_ENABLED;
6454 newtb.iString = 0;
6455 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6456 (LPARAM)&newtb);
6457 menu->submenu_id = (HMENU)-1;
6458 }
6459 else
6460#endif
6461 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006462 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006464 wn = enc_to_utf16(menu->name, NULL);
6465 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006467 InsertMenuW(parent->submenu_id, (UINT)idx,
6468 (menu_is_separator(menu->name)
6469 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6470 (UINT)menu->id, wn);
6471 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473#ifdef FEAT_TEAROFF
6474 if (IsWindow(parent->tearoff_handle))
6475 rebuild_tearoff(parent);
6476#endif
6477 }
6478}
6479
6480/*
6481 * Destroy the machine specific menu widget.
6482 */
6483 void
6484gui_mch_destroy_menu(vimmenu_T *menu)
6485{
6486#ifdef FEAT_TOOLBAR
6487 /*
6488 * is this a toolbar button?
6489 */
6490 if (menu->submenu_id == (HMENU)-1)
6491 {
6492 int iButton;
6493
6494 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6495 (WPARAM)menu->id, 0);
6496 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6497 }
6498 else
6499#endif
6500 {
6501 if (menu->parent != NULL
6502 && menu_is_popup(menu->parent->dname)
6503 && menu->parent->submenu_id != NULL)
6504 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6505 else
6506 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6507 if (menu->submenu_id != NULL)
6508 DestroyMenu(menu->submenu_id);
6509#ifdef FEAT_TEAROFF
6510 if (IsWindow(menu->tearoff_handle))
6511 DestroyWindow(menu->tearoff_handle);
6512 if (menu->parent != NULL
6513 && menu->parent->children != NULL
6514 && IsWindow(menu->parent->tearoff_handle))
6515 {
6516 /* This menu must not show up when rebuilding the tearoff window. */
6517 menu->modes = 0;
6518 rebuild_tearoff(menu->parent);
6519 }
6520#endif
6521 }
6522}
6523
6524#ifdef FEAT_TEAROFF
6525 static void
6526rebuild_tearoff(vimmenu_T *menu)
6527{
6528 /*hackish*/
6529 char_u tbuf[128];
6530 RECT trect;
6531 RECT rct;
6532 RECT roct;
6533 int x, y;
6534
6535 HWND thwnd = menu->tearoff_handle;
6536
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006537 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (GetWindowRect(thwnd, &trect)
6539 && GetWindowRect(s_hwnd, &rct)
6540 && GetClientRect(s_hwnd, &roct))
6541 {
6542 x = trect.left - rct.left;
6543 y = (trect.top - rct.bottom + roct.bottom);
6544 }
6545 else
6546 {
6547 x = y = 0xffffL;
6548 }
6549 DestroyWindow(thwnd);
6550 if (menu->children != NULL)
6551 {
6552 gui_mch_tearoff(tbuf, menu, x, y);
6553 if (IsWindow(menu->tearoff_handle))
6554 (void) SetWindowPos(menu->tearoff_handle,
6555 NULL,
6556 (int)trect.left,
6557 (int)trect.top,
6558 0, 0,
6559 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6560 }
6561}
6562#endif /* FEAT_TEAROFF */
6563
6564/*
6565 * Make a menu either grey or not grey.
6566 */
6567 void
6568gui_mch_menu_grey(
6569 vimmenu_T *menu,
6570 int grey)
6571{
6572#ifdef FEAT_TOOLBAR
6573 /*
6574 * is this a toolbar button?
6575 */
6576 if (menu->submenu_id == (HMENU)-1)
6577 {
6578 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6579 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6580 }
6581 else
6582#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006583 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6584 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586#ifdef FEAT_TEAROFF
6587 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6588 {
6589 WORD menuID;
6590 HWND menuHandle;
6591
6592 /*
6593 * A tearoff button has changed state.
6594 */
6595 if (menu->children == NULL)
6596 menuID = (WORD)(menu->id);
6597 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006598 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6600 if (menuHandle)
6601 EnableWindow(menuHandle, !grey);
6602
6603 }
6604#endif
6605}
6606
6607#endif /* FEAT_MENU */
6608
6609
6610/* define some macros used to make the dialogue creation more readable */
6611
6612#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6613#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006614#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6617/*
6618 * stuff for dialogs
6619 */
6620
6621/*
6622 * The callback routine used by all the dialogs. Very simple. First,
6623 * acknowledges the INITDIALOG message so that Windows knows to do standard
6624 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6625 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6626 * number.
6627 */
6628 static LRESULT CALLBACK
6629dialog_callback(
6630 HWND hwnd,
6631 UINT message,
6632 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006633 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 if (message == WM_INITDIALOG)
6636 {
6637 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6638 /* Set focus to the dialog. Set the default button, if specified. */
6639 (void)SetFocus(hwnd);
6640 if (dialog_default_button > IDCANCEL)
6641 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006642 else
6643 /* We don't have a default, set focus on another element of the
6644 * dialog window, probably the icon */
6645 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 return FALSE;
6647 }
6648
6649 if (message == WM_COMMAND)
6650 {
6651 int button = LOWORD(wParam);
6652
6653 /* Don't end the dialog if something was selected that was
6654 * not a button.
6655 */
6656 if (button >= DLG_NONBUTTON_CONTROL)
6657 return TRUE;
6658
6659 /* If the edit box exists, copy the string. */
6660 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006661 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006662 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6663 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006664
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006665 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6666 p = utf16_to_enc(wp, NULL);
6667 vim_strncpy(s_textfield, p, IOSIZE);
6668 vim_free(p);
6669 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 /*
6673 * Need to check for IDOK because if the user just hits Return to
6674 * accept the default value, some reason this is what we get.
6675 */
6676 if (button == IDOK)
6677 {
6678 if (dialog_default_button > IDCANCEL)
6679 EndDialog(hwnd, dialog_default_button);
6680 }
6681 else
6682 EndDialog(hwnd, button - IDCANCEL);
6683 return TRUE;
6684 }
6685
6686 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6687 {
6688 EndDialog(hwnd, 0);
6689 return TRUE;
6690 }
6691 return FALSE;
6692}
6693
6694/*
6695 * Create a dialog dynamically from the parameter strings.
6696 * type = type of dialog (question, alert, etc.)
6697 * title = dialog title. may be NULL for default title.
6698 * message = text to display. Dialog sizes to accommodate it.
6699 * buttons = '\n' separated list of button captions, default first.
6700 * dfltbutton = number of default button.
6701 *
6702 * This routine returns 1 if the first button is pressed,
6703 * 2 for the second, etc.
6704 *
6705 * 0 indicates Esc was pressed.
6706 * -1 for unexpected error
6707 *
6708 * If stubbing out this fn, return 1.
6709 */
6710
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006711static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 "IDR_VIM",
6714 "IDR_VIM_ERROR",
6715 "IDR_VIM_ALERT",
6716 "IDR_VIM_INFO",
6717 "IDR_VIM_QUESTION"
6718};
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 int
6721gui_mch_dialog(
6722 int type,
6723 char_u *title,
6724 char_u *message,
6725 char_u *buttons,
6726 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006727 char_u *textfield,
6728 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006731 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 int numButtons;
6733 int *buttonWidths, *buttonPositions;
6734 int buttonYpos;
6735 int nchar, i;
6736 DWORD lStyle;
6737 int dlgwidth = 0;
6738 int dlgheight;
6739 int editboxheight;
6740 int horizWidth = 0;
6741 int msgheight;
6742 char_u *pstart;
6743 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006744 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 char_u *tbuffer;
6746 RECT rect;
6747 HWND hwnd;
6748 HDC hdc;
6749 HFONT font, oldFont;
6750 TEXTMETRIC fontInfo;
6751 int fontHeight;
6752 int textWidth, minButtonWidth, messageWidth;
6753 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006754 int maxDialogHeight;
6755 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 int vertical;
6757 int dlgPaddingX;
6758 int dlgPaddingY;
6759#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006760 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 int use_lfSysmenu = FALSE;
6762#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006763 garray_T ga;
6764 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766#ifndef NO_CONSOLE
6767 /* Don't output anything in silent mode ("ex -s") */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006768# ifdef VIMDLL
6769 if (!(gui.in_use || gui.starting))
6770# endif
6771 if (silent_mode)
6772 return dfltbutton; /* return default option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773#endif
6774
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775 if (s_hwnd == NULL)
6776 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778 if ((type < 0) || (type > VIM_LAST_TYPE))
6779 type = 0;
6780
6781 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006782 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006783 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006784 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 if (p == NULL)
6787 return -1;
6788
6789 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006790 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6792 * const.
6793 */
6794 tbuffer = vim_strsave(buttons);
6795 if (tbuffer == NULL)
6796 return -1;
6797
6798 --dfltbutton; /* Change from one-based to zero-based */
6799
6800 /* Count buttons */
6801 numButtons = 1;
6802 for (i = 0; tbuffer[i] != '\0'; i++)
6803 {
6804 if (tbuffer[i] == DLG_BUTTON_SEP)
6805 numButtons++;
6806 }
6807 if (dfltbutton >= numButtons)
6808 dfltbutton = -1;
6809
6810 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006811 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (buttonWidths == NULL)
6813 return -1;
6814
6815 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006816 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (buttonPositions == NULL)
6818 return -1;
6819
6820 /*
6821 * Calculate how big the dialog must be.
6822 */
6823 hwnd = GetDesktopWindow();
6824 hdc = GetWindowDC(hwnd);
6825#ifdef USE_SYSMENU_FONT
6826 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6827 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006828 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 use_lfSysmenu = TRUE;
6830 }
6831 else
6832#endif
6833 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6834 VARIABLE_PITCH , DLG_FONT_NAME);
6835 if (s_usenewlook)
6836 {
6837 oldFont = SelectFont(hdc, font);
6838 dlgPaddingX = DLG_PADDING_X;
6839 dlgPaddingY = DLG_PADDING_Y;
6840 }
6841 else
6842 {
6843 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6844 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6845 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6846 }
6847 GetTextMetrics(hdc, &fontInfo);
6848 fontHeight = fontInfo.tmHeight;
6849
6850 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006851 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006854 if (s_hwnd == NULL)
6855 {
6856 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaarc716c302006-01-21 22:12:51 +00006858 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006859 get_work_area(&workarea_rect);
6860 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6861 if (maxDialogWidth > 600)
6862 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006863 /* Leave some room for the taskbar. */
6864 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006865 }
6866 else
6867 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006868 /* Use our own window for the size, unless it's very small. */
6869 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006870 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006871 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006872 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006873 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6874 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006875
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006876 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006877 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006878 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006879 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006880 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6881 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6882 }
6883
6884 /* Set dlgwidth to width of message.
6885 * Copy the message into "ga", changing NL to CR-NL and inserting line
6886 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 pstart = message;
6888 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006889 msgheight = 0;
6890 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 do
6892 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006893 msgheight += fontHeight; /* at least one line */
6894
6895 /* Need to figure out where to break the string. The system does it
6896 * at a word boundary, which would mean we can't compute the number of
6897 * wrapped lines. */
6898 textWidth = 0;
6899 last_white = NULL;
6900 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006901 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006902 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006903 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006904 && textWidth > maxDialogWidth * 3 / 4)
6905 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006906 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006907 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006908 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006909 /* Line will wrap. */
6910 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006911 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006912 textWidth = 0;
6913
6914 if (last_white != NULL)
6915 {
6916 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006918 pend = last_white + 1;
6919 last_white = NULL;
6920 }
6921 ga_append(&ga, '\r');
6922 ga_append(&ga, '\n');
6923 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006924 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925
6926 while (--l >= 0)
6927 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006928 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006931
6932 ga_append(&ga, '\r');
6933 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 pstart = pend + 1;
6935 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006936
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006937 if (ga.ga_data != NULL)
6938 message = ga.ga_data;
6939
Bram Moolenaar748bf032005-02-02 23:04:36 +00006940 messageWidth += 10; /* roundoff space */
6941
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006943 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6944 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 if (msgheight < DLG_ICON_HEIGHT)
6947 msgheight = DLG_ICON_HEIGHT;
6948
6949 /*
6950 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006951 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006953 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (!vertical)
6955 {
6956 // Place buttons horizontally if they fit.
6957 horizWidth = dlgPaddingX;
6958 pstart = tbuffer;
6959 i = 0;
6960 do
6961 {
6962 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6963 if (pend == NULL)
6964 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006965 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 if (textWidth < minButtonWidth)
6967 textWidth = minButtonWidth;
6968 textWidth += dlgPaddingX; /* Padding within button */
6969 buttonWidths[i] = textWidth;
6970 buttonPositions[i++] = horizWidth;
6971 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6972 pstart = pend + 1;
6973 } while (*pend != NUL);
6974
6975 if (horizWidth > maxDialogWidth)
6976 vertical = TRUE; // Too wide to fit on the screen.
6977 else if (horizWidth > dlgwidth)
6978 dlgwidth = horizWidth;
6979 }
6980
6981 if (vertical)
6982 {
6983 // Stack buttons vertically.
6984 pstart = tbuffer;
6985 do
6986 {
6987 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6988 if (pend == NULL)
6989 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006990 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 textWidth += dlgPaddingX; /* Padding within button */
6992 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
6993 if (textWidth > dlgwidth)
6994 dlgwidth = textWidth;
6995 pstart = pend + 1;
6996 } while (*pend != NUL);
6997 }
6998
6999 if (dlgwidth < DLG_MIN_WIDTH)
7000 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7001
7002 /* start to fill in the dlgtemplate information. addressing by WORDs */
7003 if (s_usenewlook)
7004 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7005 else
7006 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7007
7008 add_long(lStyle);
7009 add_long(0); // (lExtendedStyle)
7010 pnumitems = p; /*save where the number of items must be stored*/
7011 add_word(0); // NumberOfItems(will change later)
7012 add_word(10); // x
7013 add_word(10); // y
7014 add_word(PixelToDialogX(dlgwidth)); // cx
7015
7016 // Dialog height.
7017 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007018 dlgheight = msgheight + 2 * dlgPaddingY
7019 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
7021 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7022
7023 // Dialog needs to be taller if contains an edit box.
7024 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7025 if (textfield != NULL)
7026 dlgheight += editboxheight;
7027
Bram Moolenaara95d8232013-08-07 15:27:11 +02007028 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7029 if (dlgheight > maxDialogHeight)
7030 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007031 msgheight = msgheight - (dlgheight - maxDialogHeight);
7032 dlgheight = maxDialogHeight;
7033 scroll_flag = WS_VSCROLL;
7034 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7035 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007036 }
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 add_word(PixelToDialogY(dlgheight));
7039
7040 add_word(0); // Menu
7041 add_word(0); // Class
7042
7043 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007044 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7045 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 p += nchar;
7047
7048 if (s_usenewlook)
7049 {
7050 /* do the font, since DS_3DLOOK doesn't work properly */
7051#ifdef USE_SYSMENU_FONT
7052 if (use_lfSysmenu)
7053 {
7054 /* point size */
7055 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7056 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007057 wcscpy(p, lfSysmenu.lfFaceName);
7058 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060 else
7061#endif
7062 {
7063 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007064 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066 p += nchar;
7067 }
7068
7069 buttonYpos = msgheight + 2 * dlgPaddingY;
7070
7071 if (textfield != NULL)
7072 buttonYpos += editboxheight;
7073
7074 pstart = tbuffer;
7075 if (!vertical)
7076 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7077 for (i = 0; i < numButtons; i++)
7078 {
7079 /* get end of this button. */
7080 for ( pend = pstart;
7081 *pend && (*pend != DLG_BUTTON_SEP);
7082 pend++)
7083 ;
7084
7085 if (*pend)
7086 *pend = '\0';
7087
7088 /*
7089 * old NOTE:
7090 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7091 * the focus to the first tab-able button and in so doing makes that
7092 * the default!! Grrr. Workaround: Make the default button the only
7093 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7094 * he/she can use arrow keys.
7095 *
7096 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007097 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 * dialog. Also needed for when the textfield is the default control.
7099 * It appears to work now (perhaps not on Win95?).
7100 */
7101 if (vertical)
7102 {
7103 p = add_dialog_element(p,
7104 (i == dfltbutton
7105 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7106 PixelToDialogX(DLG_VERT_PADDING_X),
7107 PixelToDialogY(buttonYpos /* TBK */
7108 + 2 * fontHeight * i),
7109 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7110 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007111 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113 else
7114 {
7115 p = add_dialog_element(p,
7116 (i == dfltbutton
7117 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7118 PixelToDialogX(horizWidth + buttonPositions[i]),
7119 PixelToDialogY(buttonYpos), /* TBK */
7120 PixelToDialogX(buttonWidths[i]),
7121 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007122 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 }
7124 pstart = pend + 1; /*next button*/
7125 }
7126 *pnumitems += numButtons;
7127
7128 /* Vim icon */
7129 p = add_dialog_element(p, SS_ICON,
7130 PixelToDialogX(dlgPaddingX),
7131 PixelToDialogY(dlgPaddingY),
7132 PixelToDialogX(DLG_ICON_WIDTH),
7133 PixelToDialogY(DLG_ICON_HEIGHT),
7134 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7135 dlg_icons[type]);
7136
Bram Moolenaar748bf032005-02-02 23:04:36 +00007137 /* Dialog message */
7138 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7139 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7140 PixelToDialogY(dlgPaddingY),
7141 (WORD)(PixelToDialogX(messageWidth) + 1),
7142 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007143 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 /* Edit box */
7146 if (textfield != NULL)
7147 {
7148 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7149 PixelToDialogX(2 * dlgPaddingX),
7150 PixelToDialogY(2 * dlgPaddingY + msgheight),
7151 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7152 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007153 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 *pnumitems += 1;
7155 }
7156
7157 *pnumitems += 2;
7158
7159 SelectFont(hdc, oldFont);
7160 DeleteObject(font);
7161 ReleaseDC(hwnd, hdc);
7162
7163 /* Let the dialog_callback() function know which button to make default
7164 * If we have an edit box, make that the default. We also need to tell
7165 * dialog_callback() if this dialog contains an edit box or not. We do
7166 * this by setting s_textfield if it does.
7167 */
7168 if (textfield != NULL)
7169 {
7170 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7171 s_textfield = textfield;
7172 }
7173 else
7174 {
7175 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7176 s_textfield = NULL;
7177 }
7178
7179 /* show the dialog box modally and get a return value */
7180 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007181 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 (LPDLGTEMPLATE)pdlgtemplate,
7183 s_hwnd,
7184 (DLGPROC)dialog_callback);
7185
7186 LocalFree(LocalHandle(pdlgtemplate));
7187 vim_free(tbuffer);
7188 vim_free(buttonWidths);
7189 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007190 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191
7192 /* Focus back to our window (for when MDI is used). */
7193 (void)SetFocus(s_hwnd);
7194
7195 return nchar;
7196}
7197
7198#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007199
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200/*
7201 * Put a simple element (basic class) onto a dialog template in memory.
7202 * return a pointer to where the next item should be added.
7203 *
7204 * parameters:
7205 * lStyle = additional style flags
7206 * (be careful, NT3.51 & Win32s will ignore the new ones)
7207 * x,y = x & y positions IN DIALOG UNITS
7208 * w,h = width and height IN DIALOG UNITS
7209 * Id = ID used in messages
7210 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7211 * caption = usually text or resource name
7212 *
7213 * TODO: use the length information noted here to enable the dialog creation
7214 * routines to work out more exactly how much memory they need to alloc.
7215 */
7216 static PWORD
7217add_dialog_element(
7218 PWORD p,
7219 DWORD lStyle,
7220 WORD x,
7221 WORD y,
7222 WORD w,
7223 WORD h,
7224 WORD Id,
7225 WORD clss,
7226 const char *caption)
7227{
7228 int nchar;
7229
7230 p = lpwAlign(p); /* Align to dword boundary*/
7231 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7232 *p++ = LOWORD(lStyle);
7233 *p++ = HIWORD(lStyle);
7234 *p++ = 0; // LOWORD (lExtendedStyle)
7235 *p++ = 0; // HIWORD (lExtendedStyle)
7236 *p++ = x;
7237 *p++ = y;
7238 *p++ = w;
7239 *p++ = h;
7240 *p++ = Id; //9 or 10 words in all
7241
7242 *p++ = (WORD)0xffff;
7243 *p++ = clss; //2 more here
7244
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007245 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 p += nchar;
7247
7248 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7249
7250 return p; //total = 15+ (strlen(caption)) words
7251 // = 30 + 2(strlen(caption) bytes reqd
7252}
7253
7254
7255/*
7256 * Helper routine. Take an input pointer, return closest pointer that is
7257 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7258 */
7259 static LPWORD
7260lpwAlign(
7261 LPWORD lpIn)
7262{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007263 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007265 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 ul += 3;
7267 ul >>= 2;
7268 ul <<= 2;
7269 return (LPWORD)ul;
7270}
7271
7272/*
7273 * Helper routine. Takes second parameter as Ansi string, copies it to first
7274 * parameter as wide character (16-bits / char) string, and returns integer
7275 * number of wide characters (words) in string (including the trailing wide
7276 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007277 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7278 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 static int
7280nCopyAnsiToWideChar(
7281 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007282 LPSTR lpAnsiIn,
7283 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284{
7285 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7287 int i;
7288 WCHAR *wn;
7289
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007290 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 {
7292 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007293 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (wn != NULL)
7295 {
7296 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007297 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 vim_free(wn);
7299 }
7300 }
7301 if (nChar == 0)
7302 /* Use Win32 conversion function. */
7303 nChar = MultiByteToWideChar(
7304 enc_codepage > 0 ? enc_codepage : CP_ACP,
7305 MB_PRECOMPOSED,
7306 lpAnsiIn, len,
7307 lpWCStr, len);
7308 for (i = 0; i < nChar; ++i)
7309 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7310 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 return nChar;
7313}
7314
7315
7316#ifdef FEAT_TEAROFF
7317/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007318 * Lookup menu handle from "menu_id".
7319 */
7320 static HMENU
7321tearoff_lookup_menuhandle(
7322 vimmenu_T *menu,
7323 WORD menu_id)
7324{
7325 for ( ; menu != NULL; menu = menu->next)
7326 {
7327 if (menu->modes == 0) /* this menu has just been deleted */
7328 continue;
7329 if (menu_is_separator(menu->dname))
7330 continue;
7331 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7332 return menu->submenu_id;
7333 }
7334 return NULL;
7335}
7336
7337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 * The callback function for all the modeless dialogs that make up the
7339 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7340 * thinking its menus have been clicked), and go away when closed.
7341 */
7342 static LRESULT CALLBACK
7343tearoff_callback(
7344 HWND hwnd,
7345 UINT message,
7346 WPARAM wParam,
7347 LPARAM lParam)
7348{
7349 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007350 {
7351 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
7355 /* May show the mouse pointer again. */
7356 HandleMouseHide(message, lParam);
7357
7358 if (message == WM_COMMAND)
7359 {
7360 if ((WORD)(LOWORD(wParam)) & 0x8000)
7361 {
7362 POINT mp;
7363 RECT rect;
7364
7365 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7366 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007367 vimmenu_T *menu;
7368
7369 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007371 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7373 (int)rect.right - 8,
7374 (int)mp.y,
7375 (int)0, /*reserved param*/
7376 s_hwnd,
7377 NULL);
7378 /*
7379 * NOTE: The pop-up menu can eat the mouse up event.
7380 * We deal with this in normal.c.
7381 */
7382 }
7383 }
7384 else
7385 /* Pass on messages to the main Vim window */
7386 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7387 /*
7388 * Give main window the focus back: this is so after
7389 * choosing a tearoff button you can start typing again
7390 * straight away.
7391 */
7392 (void)SetFocus(s_hwnd);
7393 return TRUE;
7394 }
7395 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7396 {
7397 DestroyWindow(hwnd);
7398 return TRUE;
7399 }
7400
7401 /* When moved around, give main window the focus back. */
7402 if (message == WM_EXITSIZEMOVE)
7403 (void)SetActiveWindow(s_hwnd);
7404
7405 return FALSE;
7406}
7407#endif
7408
7409
7410/*
7411 * Decide whether to use the "new look" (small, non-bold font) or the "old
7412 * look" (big, clanky font) for dialogs, and work out a few values for use
7413 * later accordingly.
7414 */
7415 static void
7416get_dialog_font_metrics(void)
7417{
7418 HDC hdc;
7419 HFONT hfontTools = 0;
7420 DWORD dlgFontSize;
7421 SIZE size;
7422#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007423 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424#endif
7425
7426 s_usenewlook = FALSE;
7427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007429 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007430 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432#endif
7433 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7434 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7435
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007436 if (hfontTools)
7437 {
7438 hdc = GetDC(s_hwnd);
7439 SelectObject(hdc, hfontTools);
7440 /*
7441 * GetTextMetrics() doesn't return the right value in
7442 * tmAveCharWidth, so we have to figure out the dialog base units
7443 * ourselves.
7444 */
7445 GetTextExtentPoint(hdc,
7446 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7447 52, &size);
7448 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007450 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7451 s_dlgfntheight = (WORD)size.cy;
7452 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454
7455 if (!s_usenewlook)
7456 {
7457 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7458 s_dlgfntwidth = LOWORD(dlgFontSize);
7459 s_dlgfntheight = HIWORD(dlgFontSize);
7460 }
7461}
7462
7463#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7464/*
7465 * Create a pseudo-"tearoff menu" based on the child
7466 * items of a given menu pointer.
7467 */
7468 static void
7469gui_mch_tearoff(
7470 char_u *title,
7471 vimmenu_T *menu,
7472 int initX,
7473 int initY)
7474{
7475 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7476 int template_len;
7477 int nchar, textWidth, submenuWidth;
7478 DWORD lStyle;
7479 DWORD lExtendedStyle;
7480 WORD dlgwidth;
7481 WORD menuID;
7482 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007483 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 vimmenu_T *the_menu = menu;
7485 HWND hwnd;
7486 HDC hdc;
7487 HFONT font, oldFont;
7488 int col, spaceWidth, len;
7489 int columnWidths[2];
7490 char_u *label, *text;
7491 int acLen = 0;
7492 int nameLen;
7493 int padding0, padding1, padding2 = 0;
7494 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007495 int x;
7496 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007498 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 int use_lfSysmenu = FALSE;
7500#endif
7501
7502 /*
7503 * If this menu is already torn off, move it to the mouse position.
7504 */
7505 if (IsWindow(menu->tearoff_handle))
7506 {
7507 POINT mp;
7508 if (GetCursorPos((LPPOINT)&mp))
7509 {
7510 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7511 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7512 }
7513 return;
7514 }
7515
7516 /*
7517 * Create a new tearoff.
7518 */
7519 if (*title == MNU_HIDDEN_CHAR)
7520 title++;
7521
7522 /* Allocate memory to store the dialog template. It's made bigger when
7523 * needed. */
7524 template_len = DLG_ALLOC_SIZE;
7525 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7526 if (p == NULL)
7527 return;
7528
7529 hwnd = GetDesktopWindow();
7530 hdc = GetWindowDC(hwnd);
7531#ifdef USE_SYSMENU_FONT
7532 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7533 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007534 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 use_lfSysmenu = TRUE;
7536 }
7537 else
7538#endif
7539 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7540 VARIABLE_PITCH , DLG_FONT_NAME);
7541 if (s_usenewlook)
7542 oldFont = SelectFont(hdc, font);
7543 else
7544 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7545
7546 /* Calculate width of a single space. Used for padding columns to the
7547 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007548 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 /* Figure out max width of the text column, the accelerator column and the
7551 * optional submenu column. */
7552 submenuWidth = 0;
7553 for (col = 0; col < 2; col++)
7554 {
7555 columnWidths[col] = 0;
7556 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7557 {
7558 /* Use "dname" here to compute the width of the visible text. */
7559 text = (col == 0) ? pmenu->dname : pmenu->actext;
7560 if (text != NULL && *text != NUL)
7561 {
7562 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7563 if (textWidth > columnWidths[col])
7564 columnWidths[col] = textWidth;
7565 }
7566 if (pmenu->children != NULL)
7567 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7568 }
7569 }
7570 if (columnWidths[1] == 0)
7571 {
7572 /* no accelerators */
7573 if (submenuWidth != 0)
7574 columnWidths[0] += submenuWidth;
7575 else
7576 columnWidths[0] += spaceWidth;
7577 }
7578 else
7579 {
7580 /* there is an accelerator column */
7581 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7582 columnWidths[1] += submenuWidth;
7583 }
7584
7585 /*
7586 * Now find the total width of our 'menu'.
7587 */
7588 textWidth = columnWidths[0] + columnWidths[1];
7589 if (submenuWidth != 0)
7590 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007591 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7593 textWidth += submenuWidth;
7594 }
7595 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7596 if (textWidth > dlgwidth)
7597 dlgwidth = textWidth;
7598 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7599
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 /* start to fill in the dlgtemplate information. addressing by WORDs */
7601 if (s_usenewlook)
7602 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7603 else
7604 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7605
7606 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7607 *p++ = LOWORD(lStyle);
7608 *p++ = HIWORD(lStyle);
7609 *p++ = LOWORD(lExtendedStyle);
7610 *p++ = HIWORD(lExtendedStyle);
7611 pnumitems = p; /* save where the number of items must be stored */
7612 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007613 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007615 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 else
7617 *p++ = PixelToDialogX(initX); // x
7618 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007619 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 else
7621 *p++ = PixelToDialogY(initY); // y
7622 *p++ = PixelToDialogX(dlgwidth); // cx
7623 ptrueheight = p;
7624 *p++ = 0; // dialog height: changed later anyway
7625 *p++ = 0; // Menu
7626 *p++ = 0; // Class
7627
7628 /* copy the title of the dialog */
7629 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007630 ? (LPSTR)title
7631 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 p += nchar;
7633
7634 if (s_usenewlook)
7635 {
7636 /* do the font, since DS_3DLOOK doesn't work properly */
7637#ifdef USE_SYSMENU_FONT
7638 if (use_lfSysmenu)
7639 {
7640 /* point size */
7641 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7642 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007643 wcscpy(p, lfSysmenu.lfFaceName);
7644 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 }
7646 else
7647#endif
7648 {
7649 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007650 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652 p += nchar;
7653 }
7654
7655 /*
7656 * Loop over all the items in the menu.
7657 * But skip over the tearbar.
7658 */
7659 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7660 menu = menu->children->next;
7661 else
7662 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007663 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 for ( ; menu != NULL; menu = menu->next)
7665 {
7666 if (menu->modes == 0) /* this menu has just been deleted */
7667 continue;
7668 if (menu_is_separator(menu->dname))
7669 {
7670 sepPadding += 3;
7671 continue;
7672 }
7673
7674 /* Check if there still is plenty of room in the template. Make it
7675 * larger when needed. */
7676 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7677 {
7678 WORD *newp;
7679
7680 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7681 if (newp != NULL)
7682 {
7683 template_len += 4096;
7684 mch_memmove(newp, pdlgtemplate,
7685 (char *)p - (char *)pdlgtemplate);
7686 p = newp + (p - pdlgtemplate);
7687 pnumitems = newp + (pnumitems - pdlgtemplate);
7688 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7689 LocalFree(LocalHandle(pdlgtemplate));
7690 pdlgtemplate = newp;
7691 }
7692 }
7693
7694 /* Figure out minimal length of this menu label. Use "name" for the
7695 * actual text, "dname" for estimating the displayed size. "name"
7696 * has "&a" for mnemonic and includes the accelerator. */
7697 len = nameLen = (int)STRLEN(menu->name);
7698 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7699 (int)STRLEN(menu->dname))) / spaceWidth;
7700 len += padding0;
7701
7702 if (menu->actext != NULL)
7703 {
7704 acLen = (int)STRLEN(menu->actext);
7705 len += acLen;
7706 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7707 }
7708 else
7709 textWidth = 0;
7710 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7711 len += padding1;
7712
7713 if (menu->children == NULL)
7714 {
7715 padding2 = submenuWidth / spaceWidth;
7716 len += padding2;
7717 menuID = (WORD)(menu->id);
7718 }
7719 else
7720 {
7721 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007722 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
7724
7725 /* Allocate menu label and fill it in */
7726 text = label = alloc((unsigned)len + 1);
7727 if (label == NULL)
7728 break;
7729
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007730 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7732 if (text == NULL)
7733 text = label + nameLen; /* no actext, use whole name */
7734 while (padding0-- > 0)
7735 *text++ = ' ';
7736 if (menu->actext != NULL)
7737 {
7738 STRNCPY(text, menu->actext, acLen);
7739 text += acLen;
7740 }
7741 while (padding1-- > 0)
7742 *text++ = ' ';
7743 if (menu->children != NULL)
7744 {
7745 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7746 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7747 }
7748 else
7749 {
7750 while (padding2-- > 0)
7751 *text++ = ' ';
7752 }
7753 *text = NUL;
7754
7755 /*
7756 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7757 * W95/NT4 it makes the tear-off look more like a menu.
7758 */
7759 p = add_dialog_element(p,
7760 BS_PUSHBUTTON|BS_LEFT,
7761 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7762 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7763 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7764 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007765 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 vim_free(label);
7767 (*pnumitems)++;
7768 }
7769
7770 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7771
7772
7773 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007774 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007775 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 (LPDLGTEMPLATE)pdlgtemplate,
7777 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007778 (DLGPROC)tearoff_callback,
7779 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780
7781 LocalFree(LocalHandle(pdlgtemplate));
7782 SelectFont(hdc, oldFont);
7783 DeleteObject(font);
7784 ReleaseDC(hwnd, hdc);
7785
7786 /*
7787 * Reassert ourselves as the active window. This is so that after creating
7788 * a tearoff, the user doesn't have to click with the mouse just to start
7789 * typing again!
7790 */
7791 (void)SetActiveWindow(s_hwnd);
7792
7793 /* make sure the right buttons are enabled */
7794 force_menu_update = TRUE;
7795}
7796#endif
7797
7798#if defined(FEAT_TOOLBAR) || defined(PROTO)
7799#include "gui_w32_rc.h"
7800
7801/* This not defined in older SDKs */
7802# ifndef TBSTYLE_FLAT
7803# define TBSTYLE_FLAT 0x0800
7804# endif
7805
7806/*
7807 * Create the toolbar, initially unpopulated.
7808 * (just like the menu, there are no defaults, it's all
7809 * set up through menu.vim)
7810 */
7811 static void
7812initialise_toolbar(void)
7813{
7814 InitCommonControls();
7815 s_toolbarhwnd = CreateToolbarEx(
7816 s_hwnd,
7817 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7818 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007819 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007820 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 IDR_TOOLBAR1, // id of initial bitmap
7822 NULL,
7823 0, // initial number of buttons
7824 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7825 TOOLBAR_BUTTON_HEIGHT,
7826 TOOLBAR_BUTTON_WIDTH,
7827 TOOLBAR_BUTTON_HEIGHT,
7828 sizeof(TBBUTTON)
7829 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007830 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831
7832 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7833}
7834
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007835 static LRESULT CALLBACK
7836toolbar_wndproc(
7837 HWND hwnd,
7838 UINT uMsg,
7839 WPARAM wParam,
7840 LPARAM lParam)
7841{
7842 HandleMouseHide(uMsg, lParam);
7843 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7844}
7845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 static int
7847get_toolbar_bitmap(vimmenu_T *menu)
7848{
7849 int i = -1;
7850
7851 /*
7852 * Check user bitmaps first, unless builtin is specified.
7853 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007854 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
7856 char_u fname[MAXPATHL];
7857 HANDLE hbitmap = NULL;
7858
7859 if (menu->iconfile != NULL)
7860 {
7861 gui_find_iconfile(menu->iconfile, fname, "bmp");
7862 hbitmap = LoadImage(
7863 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007864 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 IMAGE_BITMAP,
7866 TOOLBAR_BUTTON_WIDTH,
7867 TOOLBAR_BUTTON_HEIGHT,
7868 LR_LOADFROMFILE |
7869 LR_LOADMAP3DCOLORS
7870 );
7871 }
7872
7873 /*
7874 * If the LoadImage call failed, or the "icon=" file
7875 * didn't exist or wasn't specified, try the menu name
7876 */
7877 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007878 && (gui_find_bitmap(
7879#ifdef FEAT_MULTI_LANG
7880 menu->en_dname != NULL ? menu->en_dname :
7881#endif
7882 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 hbitmap = LoadImage(
7884 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007885 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 IMAGE_BITMAP,
7887 TOOLBAR_BUTTON_WIDTH,
7888 TOOLBAR_BUTTON_HEIGHT,
7889 LR_LOADFROMFILE |
7890 LR_LOADMAP3DCOLORS
7891 );
7892
7893 if (hbitmap != NULL)
7894 {
7895 TBADDBITMAP tbAddBitmap;
7896
7897 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007898 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899
7900 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7901 (WPARAM)1, (LPARAM)&tbAddBitmap);
7902 /* i will be set to -1 if it fails */
7903 }
7904 }
7905 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7906 i = menu->iconidx;
7907
7908 return i;
7909}
7910#endif
7911
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007912#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7913 static void
7914initialise_tabline(void)
7915{
7916 InitCommonControls();
7917
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007918 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007919 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007920 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007921 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007922 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007923
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007924 gui.tabline_height = TABLINE_HEIGHT;
7925
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007926# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007927 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007928# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007929}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007930
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007931/*
7932 * Get tabpage_T from POINT.
7933 */
7934 static tabpage_T *
7935GetTabFromPoint(
7936 HWND hWnd,
7937 POINT pt)
7938{
7939 tabpage_T *ptp = NULL;
7940
7941 if (gui_mch_showing_tabline())
7942 {
7943 TCHITTESTINFO htinfo;
7944 htinfo.pt = pt;
7945 /* ignore if a window under cusor is not tabcontrol. */
7946 if (s_tabhwnd == hWnd)
7947 {
7948 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7949 if (idx != -1)
7950 ptp = find_tabpage(idx + 1);
7951 }
7952 }
7953 return ptp;
7954}
7955
7956static POINT s_pt = {0, 0};
7957static HCURSOR s_hCursor = NULL;
7958
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007959 static LRESULT CALLBACK
7960tabline_wndproc(
7961 HWND hwnd,
7962 UINT uMsg,
7963 WPARAM wParam,
7964 LPARAM lParam)
7965{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007966 POINT pt;
7967 tabpage_T *tp;
7968 RECT rect;
7969 int nCenter;
7970 int idx0;
7971 int idx1;
7972
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007973 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007974
7975 switch (uMsg)
7976 {
7977 case WM_LBUTTONDOWN:
7978 {
7979 s_pt.x = GET_X_LPARAM(lParam);
7980 s_pt.y = GET_Y_LPARAM(lParam);
7981 SetCapture(hwnd);
7982 s_hCursor = GetCursor(); /* backup default cursor */
7983 break;
7984 }
7985 case WM_MOUSEMOVE:
7986 if (GetCapture() == hwnd
7987 && ((wParam & MK_LBUTTON)) != 0)
7988 {
7989 pt.x = GET_X_LPARAM(lParam);
7990 pt.y = s_pt.y;
7991 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
7992 {
7993 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
7994
7995 tp = GetTabFromPoint(hwnd, pt);
7996 if (tp != NULL)
7997 {
7998 idx0 = tabpage_index(curtab) - 1;
7999 idx1 = tabpage_index(tp) - 1;
8000
8001 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8002 nCenter = rect.left + (rect.right - rect.left) / 2;
8003
8004 /* Check if the mouse cursor goes over the center of
8005 * the next tab to prevent "flickering". */
8006 if ((idx0 < idx1) && (nCenter < pt.x))
8007 {
8008 tabpage_move(idx1 + 1);
8009 update_screen(0);
8010 }
8011 else if ((idx1 < idx0) && (pt.x < nCenter))
8012 {
8013 tabpage_move(idx1);
8014 update_screen(0);
8015 }
8016 }
8017 }
8018 }
8019 break;
8020 case WM_LBUTTONUP:
8021 {
8022 if (GetCapture() == hwnd)
8023 {
8024 SetCursor(s_hCursor);
8025 ReleaseCapture();
8026 }
8027 break;
8028 }
8029 default:
8030 break;
8031 }
8032
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008033 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8034}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008035#endif
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8038/*
8039 * Make the GUI window come to the foreground.
8040 */
8041 void
8042gui_mch_set_foreground(void)
8043{
8044 if (IsIconic(s_hwnd))
8045 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8046 SetForegroundWindow(s_hwnd);
8047}
8048#endif
8049
8050#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8051 static void
8052dyn_imm_load(void)
8053{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008054 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 if (hLibImm == NULL)
8056 return;
8057
8058 pImmGetCompositionStringA
8059 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8060 pImmGetCompositionStringW
8061 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8062 pImmGetContext
8063 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8064 pImmAssociateContext
8065 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8066 pImmReleaseContext
8067 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8068 pImmGetOpenStatus
8069 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8070 pImmSetOpenStatus
8071 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008072 pImmGetCompositionFontW
8073 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8074 pImmSetCompositionFontW
8075 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 pImmSetCompositionWindow
8077 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8078 pImmGetConversionStatus
8079 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008080 pImmSetConversionStatus
8081 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082
8083 if ( pImmGetCompositionStringA == NULL
8084 || pImmGetCompositionStringW == NULL
8085 || pImmGetContext == NULL
8086 || pImmAssociateContext == NULL
8087 || pImmReleaseContext == NULL
8088 || pImmGetOpenStatus == NULL
8089 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008090 || pImmGetCompositionFontW == NULL
8091 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008093 || pImmGetConversionStatus == NULL
8094 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {
8096 FreeLibrary(hLibImm);
8097 hLibImm = NULL;
8098 pImmGetContext = NULL;
8099 return;
8100 }
8101
8102 return;
8103}
8104
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105#endif
8106
8107#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8108
8109# ifdef FEAT_XPM_W32
8110# define IMAGE_XPM 100
8111# endif
8112
8113typedef struct _signicon_t
8114{
8115 HANDLE hImage;
8116 UINT uType;
8117#ifdef FEAT_XPM_W32
8118 HANDLE hShape; /* Mask bitmap handle */
8119#endif
8120} signicon_t;
8121
8122 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008123gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 signicon_t *sign;
8126 int x, y, w, h;
8127
8128 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8129 return;
8130
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008131#if defined(FEAT_DIRECTX)
8132 if (IS_ENABLE_DIRECTX())
8133 DWriteContext_Flush(s_dwc);
8134#endif
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 x = TEXT_X(col);
8137 y = TEXT_Y(row);
8138 w = gui.char_width * 2;
8139 h = gui.char_height;
8140 switch (sign->uType)
8141 {
8142 case IMAGE_BITMAP:
8143 {
8144 HDC hdcMem;
8145 HBITMAP hbmpOld;
8146
8147 hdcMem = CreateCompatibleDC(s_hdc);
8148 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8149 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8150 SelectObject(hdcMem, hbmpOld);
8151 DeleteDC(hdcMem);
8152 }
8153 break;
8154 case IMAGE_ICON:
8155 case IMAGE_CURSOR:
8156 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8157 break;
8158#ifdef FEAT_XPM_W32
8159 case IMAGE_XPM:
8160 {
8161 HDC hdcMem;
8162 HBITMAP hbmpOld;
8163
8164 hdcMem = CreateCompatibleDC(s_hdc);
8165 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8166 /* Make hole */
8167 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8168
8169 SelectObject(hdcMem, sign->hImage);
8170 /* Paint sign */
8171 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8172 SelectObject(hdcMem, hbmpOld);
8173 DeleteDC(hdcMem);
8174 }
8175 break;
8176#endif
8177 }
8178}
8179
8180 static void
8181close_signicon_image(signicon_t *sign)
8182{
8183 if (sign)
8184 switch (sign->uType)
8185 {
8186 case IMAGE_BITMAP:
8187 DeleteObject((HGDIOBJ)sign->hImage);
8188 break;
8189 case IMAGE_CURSOR:
8190 DestroyCursor((HCURSOR)sign->hImage);
8191 break;
8192 case IMAGE_ICON:
8193 DestroyIcon((HICON)sign->hImage);
8194 break;
8195#ifdef FEAT_XPM_W32
8196 case IMAGE_XPM:
8197 DeleteObject((HBITMAP)sign->hImage);
8198 DeleteObject((HBITMAP)sign->hShape);
8199 break;
8200#endif
8201 }
8202}
8203
8204 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008205gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206{
8207 signicon_t sign, *psign;
8208 char_u *ext;
8209
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008211 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 if (ext > signfile)
8213 {
8214 int do_load = 1;
8215
8216 if (!STRICMP(ext, ".bmp"))
8217 sign.uType = IMAGE_BITMAP;
8218 else if (!STRICMP(ext, ".ico"))
8219 sign.uType = IMAGE_ICON;
8220 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8221 sign.uType = IMAGE_CURSOR;
8222 else
8223 do_load = 0;
8224
8225 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008226 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 gui.char_width * 2, gui.char_height,
8228 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8229#ifdef FEAT_XPM_W32
8230 if (!STRICMP(ext, ".xpm"))
8231 {
8232 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008233 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8234 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
8236#endif
8237 }
8238
8239 psign = NULL;
8240 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8241 != NULL)
8242 *psign = sign;
8243
8244 if (!psign)
8245 {
8246 if (sign.hImage)
8247 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008248 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 }
8250 return (void *)psign;
8251
8252}
8253
8254 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008255gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 if (sign)
8258 {
8259 close_signicon_image((signicon_t *)sign);
8260 vim_free(sign);
8261 }
8262}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008265#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266
8267/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008268 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008270 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8272 * to get current mouse position).
8273 *
8274 * Trying to use as more Windows services as possible, and as less
8275 * IE version as possible :)).
8276 *
8277 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8278 * BalloonEval struct.
8279 * 2) Enable/Disable simply create/kill BalloonEval Timer
8280 * 3) When there was enough inactivity, timer procedure posts
8281 * async request to debugger
8282 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8283 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008284 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 */
8286
Bram Moolenaar45360022005-07-21 21:08:21 +00008287/*
8288 * determine whether installed Common Controls support multiline tooltips
8289 * (i.e. their version is >= 4.70
8290 */
8291 int
8292multiline_balloon_available(void)
8293{
8294 HINSTANCE hDll;
8295 static char comctl_dll[] = "comctl32.dll";
8296 static int multiline_tip = MAYBE;
8297
8298 if (multiline_tip != MAYBE)
8299 return multiline_tip;
8300
8301 hDll = GetModuleHandle(comctl_dll);
8302 if (hDll != NULL)
8303 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008304 DLLGETVERSIONPROC pGetVer;
8305 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008306
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008307 if (pGetVer != NULL)
8308 {
8309 DLLVERSIONINFO dvi;
8310 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008311
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008312 ZeroMemory(&dvi, sizeof(dvi));
8313 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008314
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008315 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008316
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008317 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008318 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008319 || (dvi.dwMajorVersion == 4
8320 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008321 {
8322 multiline_tip = TRUE;
8323 return multiline_tip;
8324 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008325 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008326 else
8327 {
8328 /* there is chance we have ancient CommCtl 4.70
8329 which doesn't export DllGetVersion */
8330 DWORD dwHandle = 0;
8331 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8332 if (len > 0)
8333 {
8334 VS_FIXEDFILEINFO *ver;
8335 UINT vlen = 0;
8336 void *data = alloc(len);
8337
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008338 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008339 && GetFileVersionInfo(comctl_dll, 0, len, data)
8340 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8341 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008342 && HIWORD(ver->dwFileVersionMS) > 4)
8343 || ((HIWORD(ver->dwFileVersionMS) == 4
8344 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008345 {
8346 vim_free(data);
8347 multiline_tip = TRUE;
8348 return multiline_tip;
8349 }
8350 vim_free(data);
8351 }
8352 }
8353 }
8354 multiline_tip = FALSE;
8355 return multiline_tip;
8356}
8357
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008358 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008359make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008360{
8361 TOOLINFOW *pti;
8362 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008363
8364 if (multiline_balloon_available() == TRUE)
8365 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8366 else
8367 ToolInfoSize = sizeof(TOOLINFOW);
8368
8369 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8370 if (pti == NULL)
8371 return;
8372
8373 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8374 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8375 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008376 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008377
8378 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8379 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8380
8381 pti->cbSize = ToolInfoSize;
8382 pti->uFlags = TTF_SUBCLASS;
8383 pti->hwnd = beval->target;
8384 pti->hinst = 0; // Don't use string resources
8385 pti->uId = ID_BEVAL_TOOLTIP;
8386
8387 if (multiline_balloon_available() == TRUE)
8388 {
8389 RECT rect;
8390 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8391 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008392 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8393 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008394 // switch multiline tooltips on
8395 if (GetClientRect(s_textArea, &rect))
8396 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8397 (LPARAM)rect.right);
8398 }
8399 else
8400 {
8401 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008402 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8403 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008404 }
8405
8406 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8407 pti->rect.left = pt.x - 3;
8408 pti->rect.top = pt.y - 3;
8409 pti->rect.right = pt.x + 3;
8410 pti->rect.bottom = pt.y + 3;
8411
8412 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8413 // Make tooltip appear sooner.
8414 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8415 // I've performed some tests and it seems the longest possible life time
8416 // of tooltip is 30 seconds.
8417 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8418 /*
8419 * HACK: force tooltip to appear, because it'll not appear until
8420 * first mouse move. D*mn M$
8421 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8422 */
8423 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8424 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8425 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008426}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008429delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008431 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432}
8433
8434 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008435BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008436 HWND hwnd UNUSED,
8437 UINT uMsg UNUSED,
8438 UINT_PTR idEvent UNUSED,
8439 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440{
8441 POINT pt;
8442 RECT rect;
8443
8444 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8445 return;
8446
8447 GetCursorPos(&pt);
8448 if (WindowFromPoint(pt) != s_textArea)
8449 return;
8450
8451 ScreenToClient(s_textArea, &pt);
8452 GetClientRect(s_textArea, &rect);
8453 if (!PtInRect(&rect, pt))
8454 return;
8455
8456 if (LastActivity > 0
8457 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8458 && (cur_beval->showState != ShS_PENDING
8459 || abs(cur_beval->x - pt.x) > 3
8460 || abs(cur_beval->y - pt.y) > 3))
8461 {
8462 /* Pointer resting in one place long enough, it's time to show
8463 * the tooltip. */
8464 cur_beval->showState = ShS_PENDING;
8465 cur_beval->x = pt.x;
8466 cur_beval->y = pt.y;
8467
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008468 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469
8470 if (cur_beval->msgCB != NULL)
8471 (*cur_beval->msgCB)(cur_beval, 0);
8472 }
8473}
8474
8475 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008476gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008478 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008480 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481}
8482
8483 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008484gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008486 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 if (beval == NULL)
8488 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008489 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008490 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008491 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492}
8493
8494 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008495gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008498
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008499 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (beval->showState == ShS_SHOWING)
8501 return;
8502 GetCursorPos(&pt);
8503 ScreenToClient(s_textArea, &pt);
8504
8505 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008507 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 gui_mch_disable_beval_area(cur_beval);
8509 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008510 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008512 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513}
8514
8515 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008516gui_mch_create_beval_area(
8517 void *target, /* ignored, always use s_textArea */
8518 char_u *mesg,
8519 void (*mesgCB)(BalloonEval *, int),
8520 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522 /* partially stolen from gui_beval.c */
8523 BalloonEval *beval;
8524
8525 if (mesg != NULL && mesgCB != NULL)
8526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008527 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 return NULL;
8529 }
8530
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008531 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 if (beval != NULL)
8533 {
8534 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
8536 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 beval->msg = mesg;
8538 beval->msgCB = mesgCB;
8539 beval->clientData = clientData;
8540
8541 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 cur_beval = beval;
8543
8544 if (p_beval)
8545 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 }
8547 return beval;
8548}
8549
8550 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008551Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552{
8553 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8554 return;
8555
8556 if (cur_beval != NULL)
8557 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008558 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008560 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008561 // TRACE0("TTN_SHOW {{{");
8562 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008563 break;
8564 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008565 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 delete_tooltip(cur_beval);
8567 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008568 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008571 break;
8572 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008573 {
8574 /* if you get there then we have new common controls */
8575 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8576 info->lpszText = (LPSTR)info->lParam;
8577 info->uFlags |= TTF_DI_SETITEM;
8578 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008579 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008580 case TTN_GETDISPINFOW:
8581 {
8582 // if we get here then we have new common controls
8583 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8584 info->lpszText = (LPWSTR)info->lParam;
8585 info->uFlags |= TTF_DI_SETITEM;
8586 }
8587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589 }
8590}
8591
8592 static void
8593TrackUserActivity(UINT uMsg)
8594{
8595 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8596 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8597 LastActivity = GetTickCount();
8598}
8599
8600 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008601gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008603#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008604 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008605#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008606 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 vim_free(beval);
8608}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008609#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
8611#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8612/*
8613 * We have multiple signs to draw at the same location. Draw the
8614 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8615 */
8616 void
8617netbeans_draw_multisign_indicator(int row)
8618{
8619 int i;
8620 int y;
8621 int x;
8622
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008623 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008624 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 x = 0;
8627 y = TEXT_Y(row);
8628
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008629#if defined(FEAT_DIRECTX)
8630 if (IS_ENABLE_DIRECTX())
8631 DWriteContext_Flush(s_dwc);
8632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 for (i = 0; i < gui.char_height - 3; i++)
8635 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8636
8637 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8638 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8639 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8640 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8641 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8642 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8643 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8644}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008645#endif