blob: 02d2e096b565dc1a4a28a961b3ca098ab2c54985 [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. */
Bram Moolenaar459fd782019-10-13 16:43:39 +0200853 ch = extract_modifiers(ch, &modifiers, TRUE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100854 if (ch == CSI)
855 ch = K_CSI;
856
857 len = 0;
858 if (modifiers)
859 {
860 string[len++] = CSI;
861 string[len++] = KS_MODIFIER;
862 string[len++] = modifiers;
863 }
864
865 if (IS_SPECIAL((int)ch))
866 {
867 string[len++] = CSI;
868 string[len++] = K_SECOND((int)ch);
869 string[len++] = K_THIRD((int)ch);
870 }
871 else
872 {
873 /* Although the documentation isn't clear about it, we assume "ch" is
874 * a Unicode character. */
875 len += char_to_string(ch, string + len, 40 - len, TRUE);
876 }
877
878 add_to_input_buf(string, len);
879}
880
881 static void
882_OnMouseEvent(
883 int button,
884 int x,
885 int y,
886 int repeated_click,
887 UINT keyFlags)
888{
889 int vim_modifiers = 0x0;
890
891 s_getting_focus = FALSE;
892
893 if (keyFlags & MK_SHIFT)
894 vim_modifiers |= MOUSE_SHIFT;
895 if (keyFlags & MK_CONTROL)
896 vim_modifiers |= MOUSE_CTRL;
897 if (GetKeyState(VK_MENU) & 0x8000)
898 vim_modifiers |= MOUSE_ALT;
899
900 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
901}
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 static void
904_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905 HWND hwnd UNUSED,
906 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 int x,
908 int y,
909 UINT keyFlags)
910{
911 static LONG s_prevTime = 0;
912
913 LONG currentTime = GetMessageTime();
914 int button = -1;
915 int repeated_click;
916
917 /* Give main window the focus: this is so the cursor isn't hollow. */
918 (void)SetFocus(s_hwnd);
919
920 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
921 button = MOUSE_LEFT;
922 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
923 button = MOUSE_MIDDLE;
924 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
925 button = MOUSE_RIGHT;
926 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
927 {
928#ifndef GET_XBUTTON_WPARAM
929# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
930#endif
931 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
932 }
933 else if (s_uMsg == WM_CAPTURECHANGED)
934 {
935 /* on W95/NT4, somehow you get in here with an odd Msg
936 * if you press one button while holding down the other..*/
937 if (s_button_pending == MOUSE_LEFT)
938 button = MOUSE_RIGHT;
939 else
940 button = MOUSE_LEFT;
941 }
942 if (button >= 0)
943 {
944 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
945
946 /*
947 * Holding down the left and right buttons simulates pushing the middle
948 * button.
949 */
950 if (repeated_click
951 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
952 || (button == MOUSE_RIGHT
953 && s_button_pending == MOUSE_LEFT)))
954 {
955 /*
956 * Hmm, gui.c will ignore more than one button down at a time, so
957 * pretend we let go of it first.
958 */
959 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
960 button = MOUSE_MIDDLE;
961 repeated_click = FALSE;
962 s_button_pending = -1;
963 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
964 }
965 else if ((repeated_click)
966 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
967 {
968 if (s_button_pending > -1)
969 {
970 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
971 s_button_pending = -1;
972 }
973 /* TRACE("Button down at x %d, y %d\n", x, y); */
974 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
975 }
976 else
977 {
978 /*
979 * If this is the first press (i.e. not a multiple click) don't
980 * action immediately, but store and wait for:
981 * i) button-up
982 * ii) mouse move
983 * iii) another button press
984 * before using it.
985 * This enables us to make left+right simulate middle button,
986 * without left or right being actioned first. The side-effect is
987 * that if you click and hold the mouse without dragging, the
988 * cursor doesn't move until you release the button. In practice
989 * this is hardly a problem.
990 */
991 s_button_pending = button;
992 s_x_pending = x;
993 s_y_pending = y;
994 s_kFlags_pending = keyFlags;
995 }
996
997 s_prevTime = currentTime;
998 }
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001004 int x,
1005 int y,
1006 UINT keyFlags)
1007{
1008 int button;
1009
1010 s_getting_focus = FALSE;
1011 if (s_button_pending > -1)
1012 {
1013 /* Delayed action for mouse down event */
1014 _OnMouseEvent(s_button_pending, s_x_pending,
1015 s_y_pending, FALSE, s_kFlags_pending);
1016 s_button_pending = -1;
1017 }
1018 if (s_uMsg == WM_MOUSEMOVE)
1019 {
1020 /*
1021 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1022 * down.
1023 */
1024 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1025 | MK_XBUTTON1 | MK_XBUTTON2)))
1026 {
1027 gui_mouse_moved(x, y);
1028 return;
1029 }
1030
1031 /*
1032 * While button is down, keep grabbing mouse move events when
1033 * the mouse goes outside the window
1034 */
1035 SetCapture(s_textArea);
1036 button = MOUSE_DRAG;
1037 /* TRACE(" move at x %d, y %d\n", x, y); */
1038 }
1039 else
1040 {
1041 ReleaseCapture();
1042 button = MOUSE_RELEASE;
1043 /* TRACE(" up at x %d, y %d\n", x, y); */
1044 }
1045
1046 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1047}
1048
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001049 static void
1050_OnSizeTextArea(
1051 HWND hwnd UNUSED,
1052 UINT state UNUSED,
1053 int cx UNUSED,
1054 int cy UNUSED)
1055{
1056#if defined(FEAT_DIRECTX)
1057 if (IS_ENABLE_DIRECTX())
1058 directx_binddc();
1059#endif
1060}
1061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062#ifdef FEAT_MENU
1063/*
1064 * Find the vimmenu_T with the given id
1065 */
1066 static vimmenu_T *
1067gui_mswin_find_menu(
1068 vimmenu_T *pMenu,
1069 int id)
1070{
1071 vimmenu_T *pChildMenu;
1072
1073 while (pMenu)
1074 {
1075 if (pMenu->id == (UINT)id)
1076 break;
1077 if (pMenu->children != NULL)
1078 {
1079 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1080 if (pChildMenu)
1081 {
1082 pMenu = pChildMenu;
1083 break;
1084 }
1085 }
1086 pMenu = pMenu->next;
1087 }
1088 return pMenu;
1089}
1090
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091 static void
1092_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwndCtl UNUSED,
1096 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097{
1098 vimmenu_T *pMenu;
1099
1100 pMenu = gui_mswin_find_menu(root_menu, id);
1101 if (pMenu)
1102 gui_menu_cb(pMenu);
1103}
1104#endif
1105
1106#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1117 /* Give main window the focus back. */
1118 (void)SetFocus(s_hwnd);
1119
1120 if (s_findrep_struct.Flags & FR_FINDNEXT)
1121 {
1122 flags = FRD_FINDNEXT;
1123
1124 /* Give main window the focus back: this is so the cursor isn't
1125 * hollow. */
1126 (void)SetFocus(s_hwnd);
1127 }
1128 else if (s_findrep_struct.Flags & FR_REPLACE)
1129 {
1130 flags = FRD_REPLACE;
1131
1132 /* Give main window the focus back: this is so the cursor isn't
1133 * hollow. */
1134 (void)SetFocus(s_hwnd);
1135 }
1136 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1137 {
1138 flags = FRD_REPLACEALL;
1139 }
1140
1141 if (flags != 0)
1142 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001143 char_u *p, *q;
1144
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 /* Call the generic GUI function to do the actual work. */
1146 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1147 flags |= FRD_WHOLE_WORD;
1148 if (s_findrep_struct.Flags & FR_MATCHCASE)
1149 flags |= FRD_MATCH_CASE;
1150 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001151 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1152 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1153 if (p != NULL && q != NULL)
1154 gui_do_findrepl(flags, p, q, down);
1155 vim_free(p);
1156 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 }
1158}
1159#endif
1160
1161 static void
1162HandleMouseHide(UINT uMsg, LPARAM lParam)
1163{
1164 static LPARAM last_lParam = 0L;
1165
1166 /* We sometimes get a mousemove when the mouse didn't move... */
1167 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1168 {
1169 if (lParam == last_lParam)
1170 return;
1171 last_lParam = lParam;
1172 }
1173
1174 /* Handle specially, to centralise coding. We need to be sure we catch all
1175 * possible events which should cause us to restore the cursor (as it is a
1176 * shared resource, we take full responsibility for it).
1177 */
1178 switch (uMsg)
1179 {
1180 case WM_KEYUP:
1181 case WM_CHAR:
1182 /*
1183 * blank out the pointer if necessary
1184 */
1185 if (p_mh)
1186 gui_mch_mousehide(TRUE);
1187 break;
1188
1189 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1190 case WM_SYSCHAR:
1191 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1192 case WM_LBUTTONDOWN:
1193 case WM_LBUTTONUP:
1194 case WM_MBUTTONDOWN:
1195 case WM_MBUTTONUP:
1196 case WM_RBUTTONDOWN:
1197 case WM_RBUTTONUP:
1198 case WM_XBUTTONDOWN:
1199 case WM_XBUTTONUP:
1200 case WM_NCMOUSEMOVE:
1201 case WM_NCLBUTTONDOWN:
1202 case WM_NCLBUTTONUP:
1203 case WM_NCMBUTTONDOWN:
1204 case WM_NCMBUTTONUP:
1205 case WM_NCRBUTTONDOWN:
1206 case WM_NCRBUTTONUP:
1207 case WM_KILLFOCUS:
1208 /*
1209 * if the pointer is currently hidden, then we should show it.
1210 */
1211 gui_mch_mousehide(FALSE);
1212 break;
1213 }
1214}
1215
1216 static LRESULT CALLBACK
1217_TextAreaWndProc(
1218 HWND hwnd,
1219 UINT uMsg,
1220 WPARAM wParam,
1221 LPARAM lParam)
1222{
1223 /*
1224 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1225 hwnd, uMsg, wParam, lParam);
1226 */
1227
1228 HandleMouseHide(uMsg, lParam);
1229
1230 s_uMsg = uMsg;
1231 s_wParam = wParam;
1232 s_lParam = lParam;
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 TrackUserActivity(uMsg);
1236#endif
1237
1238 switch (uMsg)
1239 {
1240 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1248 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001254 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1258 return TRUE;
1259#endif
1260 default:
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263}
1264
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001267#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269 LRESULT WINAPI
1270vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1271{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001272#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001274#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001275 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001276#endif
1277}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278
1279/*
1280 * Called when the foreground or background color has been changed.
1281 */
1282 void
1283gui_mch_new_colors(void)
1284{
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{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001306 /* Actually open the window, if not already visible
1307 * (may be done already in gui_mch_set_shellsize) */
1308 if (!IsWindowVisible(s_hwnd))
1309 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1310
1311#ifdef MSWIN_FIND_REPLACE
1312 /* Init replace string here, so that we keep it when re-opening the
1313 * dialog. */
1314 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1315#endif
1316
1317 return OK;
1318}
1319
1320/*
1321 * Get the position of the top left corner of the window.
1322 */
1323 int
1324gui_mch_get_winpos(int *x, int *y)
1325{
1326 RECT rect;
1327
1328 GetWindowRect(s_hwnd, &rect);
1329 *x = rect.left;
1330 *y = rect.top;
1331 return OK;
1332}
1333
1334/*
1335 * Set the position of the top left corner of the window to the given
1336 * coordinates.
1337 */
1338 void
1339gui_mch_set_winpos(int x, int y)
1340{
1341 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1342 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1343}
1344 void
1345gui_mch_set_text_area_pos(int x, int y, int w, int h)
1346{
1347 static int oldx = 0;
1348 static int oldy = 0;
1349
1350 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1351
1352#ifdef FEAT_TOOLBAR
1353 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1354 SendMessage(s_toolbarhwnd, WM_SIZE,
1355 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1356#endif
1357#if defined(FEAT_GUI_TABLINE)
1358 if (showing_tabline)
1359 {
1360 int top = 0;
1361 RECT rect;
1362
1363# ifdef FEAT_TOOLBAR
1364 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1365 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1366# endif
1367 GetClientRect(s_hwnd, &rect);
1368 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1369 }
1370#endif
1371
1372 /* When side scroll bar is unshown, the size of window will change.
1373 * then, the text area move left or right. thus client rect should be
1374 * forcedly redrawn. (Yasuhiro Matsumoto) */
1375 if (oldx != x || oldy != y)
1376 {
1377 InvalidateRect(s_hwnd, NULL, FALSE);
1378 oldx = x;
1379 oldy = y;
1380 }
1381}
1382
1383
1384/*
1385 * Scrollbar stuff:
1386 */
1387
1388 void
1389gui_mch_enable_scrollbar(
1390 scrollbar_T *sb,
1391 int flag)
1392{
1393 ShowScrollBar(sb->id, SB_CTL, flag);
1394
1395 /* TODO: When the window is maximized, the size of the window stays the
1396 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1397 * NT 4.0 it's not... */
1398}
1399
1400 void
1401gui_mch_set_scrollbar_pos(
1402 scrollbar_T *sb,
1403 int x,
1404 int y,
1405 int w,
1406 int h)
1407{
1408 SetWindowPos(sb->id, NULL, x, y, w, h,
1409 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1410}
1411
1412 void
1413gui_mch_create_scrollbar(
1414 scrollbar_T *sb,
1415 int orient) /* SBAR_VERT or SBAR_HORIZ */
1416{
1417 sb->id = CreateWindow(
1418 "SCROLLBAR", "Scrollbar",
1419 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1420 10, /* Any value will do for now */
1421 10, /* Any value will do for now */
1422 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001423 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001424}
1425
1426/*
1427 * Find the scrollbar with the given hwnd.
1428 */
1429 static scrollbar_T *
1430gui_mswin_find_scrollbar(HWND hwnd)
1431{
1432 win_T *wp;
1433
1434 if (gui.bottom_sbar.id == hwnd)
1435 return &gui.bottom_sbar;
1436 FOR_ALL_WINDOWS(wp)
1437 {
1438 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1439 return &wp->w_scrollbars[SBAR_LEFT];
1440 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1441 return &wp->w_scrollbars[SBAR_RIGHT];
1442 }
1443 return NULL;
1444}
1445
1446/*
1447 * Get the character size of a font.
1448 */
1449 static void
1450GetFontSize(GuiFont font)
1451{
1452 HWND hwnd = GetDesktopWindow();
1453 HDC hdc = GetWindowDC(hwnd);
1454 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001455 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001456 TEXTMETRIC tm;
1457
1458 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001459 // GetTextMetrics() may not return the right value in tmAveCharWidth
1460 // for some fonts. Do our own average computation.
1461 GetTextExtentPoint(hdc,
1462 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1463 52, &size);
1464 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001465
1466 gui.char_height = tm.tmHeight + p_linespace;
1467
1468 SelectFont(hdc, hfntOld);
1469
1470 ReleaseDC(hwnd, hdc);
1471}
1472
1473/*
1474 * Adjust gui.char_height (after 'linespace' was changed).
1475 */
1476 int
1477gui_mch_adjust_charheight(void)
1478{
1479 GetFontSize(gui.norm_font);
1480 return OK;
1481}
1482
1483 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001484get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001485{
1486 HFONT font = NULL;
1487
1488 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001489 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490
1491 if (font == NULL)
1492 return NOFONT;
1493
1494 return (GuiFont)font;
1495}
1496
1497 static int
1498pixels_to_points(int pixels, int vertical)
1499{
1500 int points;
1501 HWND hwnd;
1502 HDC hdc;
1503
1504 hwnd = GetDesktopWindow();
1505 hdc = GetWindowDC(hwnd);
1506
1507 points = MulDiv(pixels, 72,
1508 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1509
1510 ReleaseDC(hwnd, hdc);
1511
1512 return points;
1513}
1514
1515 GuiFont
1516gui_mch_get_font(
1517 char_u *name,
1518 int giveErrorIfMissing)
1519{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001520 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001521 GuiFont font = NOFONT;
1522
1523 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1524 font = get_font_handle(&lf);
1525 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001526 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001527 return font;
1528}
1529
1530#if defined(FEAT_EVAL) || defined(PROTO)
1531/*
1532 * Return the name of font "font" in allocated memory.
1533 * Don't know how to get the actual name, thus use the provided name.
1534 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001535 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001536gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001537{
1538 if (name == NULL)
1539 return NULL;
1540 return vim_strsave(name);
1541}
1542#endif
1543
1544 void
1545gui_mch_free_font(GuiFont font)
1546{
1547 if (font)
1548 DeleteObject((HFONT)font);
1549}
1550
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001551/*
1552 * Return the Pixel value (color) for the given color name.
1553 * Return INVALCOLOR for error.
1554 */
1555 guicolor_T
1556gui_mch_get_color(char_u *name)
1557{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001558 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001559
1560 typedef struct SysColorTable
1561 {
1562 char *name;
1563 int color;
1564 } SysColorTable;
1565
1566 static SysColorTable sys_table[] =
1567 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1569 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001570#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001571 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1572#endif
1573 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1574 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1575 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1576 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1577 {"SYS_DESKTOP", COLOR_DESKTOP},
1578 {"SYS_INFOBK", COLOR_INFOBK},
1579 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1580 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001581 {"SYS_BTNFACE", COLOR_BTNFACE},
1582 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1583 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1584 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1585 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1586 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1587 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1588 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1589 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1590 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1591 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1592 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1593 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1594 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1595 {"SYS_MENU", COLOR_MENU},
1596 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1597 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1598 {"SYS_WINDOW", COLOR_WINDOW},
1599 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1600 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1601 };
1602
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603 /*
1604 * Try to look up a system colour.
1605 */
1606 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1607 if (STRICMP(name, sys_table[i].name) == 0)
1608 return GetSysColor(sys_table[i].color);
1609
Bram Moolenaarab302212016-04-26 20:59:29 +02001610 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001611}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001612
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001613 guicolor_T
1614gui_mch_get_rgb_color(int r, int g, int b)
1615{
1616 return gui_get_rgb_color_cmn(r, g, b);
1617}
1618
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001619/*
1620 * Return OK if the key with the termcap name "name" is supported.
1621 */
1622 int
1623gui_mch_haskey(char_u *name)
1624{
1625 int i;
1626
1627 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1628 if (name[0] == special_keys[i].vim_code0 &&
1629 name[1] == special_keys[i].vim_code1)
1630 return OK;
1631 return FAIL;
1632}
1633
1634 void
1635gui_mch_beep(void)
1636{
1637 MessageBeep(MB_OK);
1638}
1639/*
1640 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1641 */
1642 void
1643gui_mch_invert_rectangle(
1644 int r,
1645 int c,
1646 int nr,
1647 int nc)
1648{
1649 RECT rc;
1650
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001651#if defined(FEAT_DIRECTX)
1652 if (IS_ENABLE_DIRECTX())
1653 DWriteContext_Flush(s_dwc);
1654#endif
1655
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656 /*
1657 * Note: InvertRect() excludes right and bottom of rectangle.
1658 */
1659 rc.left = FILL_X(c);
1660 rc.top = FILL_Y(r);
1661 rc.right = rc.left + nc * gui.char_width;
1662 rc.bottom = rc.top + nr * gui.char_height;
1663 InvertRect(s_hdc, &rc);
1664}
1665
1666/*
1667 * Iconify the GUI window.
1668 */
1669 void
1670gui_mch_iconify(void)
1671{
1672 ShowWindow(s_hwnd, SW_MINIMIZE);
1673}
1674
1675/*
1676 * Draw a cursor without focus.
1677 */
1678 void
1679gui_mch_draw_hollow_cursor(guicolor_T color)
1680{
1681 HBRUSH hbr;
1682 RECT rc;
1683
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001684#if defined(FEAT_DIRECTX)
1685 if (IS_ENABLE_DIRECTX())
1686 DWriteContext_Flush(s_dwc);
1687#endif
1688
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001689 /*
1690 * Note: FrameRect() excludes right and bottom of rectangle.
1691 */
1692 rc.left = FILL_X(gui.col);
1693 rc.top = FILL_Y(gui.row);
1694 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001695 if (mb_lefthalve(gui.row, gui.col))
1696 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001697 rc.bottom = rc.top + gui.char_height;
1698 hbr = CreateSolidBrush(color);
1699 FrameRect(s_hdc, &rc, hbr);
1700 DeleteBrush(hbr);
1701}
1702/*
1703 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1704 * color "color".
1705 */
1706 void
1707gui_mch_draw_part_cursor(
1708 int w,
1709 int h,
1710 guicolor_T color)
1711{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001712 RECT rc;
1713
1714 /*
1715 * Note: FillRect() excludes right and bottom of rectangle.
1716 */
1717 rc.left =
1718#ifdef FEAT_RIGHTLEFT
1719 /* vertical line should be on the right of current point */
1720 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1721#endif
1722 FILL_X(gui.col);
1723 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1724 rc.right = rc.left + w;
1725 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001726
Bram Moolenaar92467d32017-12-05 13:22:16 +01001727 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728}
1729
1730
1731/*
1732 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1733 * dead key's nominal character and re-post the original message.
1734 */
1735 static void
1736outputDeadKey_rePost(MSG originalMsg)
1737{
1738 static MSG deadCharExpel;
1739
1740 if (!dead_key)
1741 return;
1742
1743 dead_key = 0;
1744
1745 /* Make Windows generate the dead key's character */
1746 deadCharExpel.message = originalMsg.message;
1747 deadCharExpel.hwnd = originalMsg.hwnd;
1748 deadCharExpel.wParam = VK_SPACE;
1749
1750 MyTranslateMessage(&deadCharExpel);
1751
1752 /* re-generate the current character free of the dead char influence */
1753 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1754 originalMsg.lParam);
1755}
1756
1757
1758/*
1759 * Process a single Windows message.
1760 * If one is not available we hang until one is.
1761 */
1762 static void
1763process_message(void)
1764{
1765 MSG msg;
1766 UINT vk = 0; /* Virtual key */
1767 char_u string[40];
1768 int i;
1769 int modifiers = 0;
1770 int key;
1771#ifdef FEAT_MENU
1772 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1773#endif
1774
1775 pGetMessage(&msg, NULL, 0, 0);
1776
1777#ifdef FEAT_OLE
1778 /* Look after OLE Automation commands */
1779 if (msg.message == WM_OLE)
1780 {
1781 char_u *str = (char_u *)msg.lParam;
1782 if (str == NULL || *str == NUL)
1783 {
1784 /* Message can't be ours, forward it. Fixes problem with Ultramon
1785 * 3.0.4 */
1786 pDispatchMessage(&msg);
1787 }
1788 else
1789 {
1790 add_to_input_buf(str, (int)STRLEN(str));
1791 vim_free(str); /* was allocated in CVim::SendKeys() */
1792 }
1793 return;
1794 }
1795#endif
1796
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001797#ifdef MSWIN_FIND_REPLACE
1798 /* Don't process messages used by the dialog */
1799 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1800 {
1801 HandleMouseHide(msg.message, msg.lParam);
1802 return;
1803 }
1804#endif
1805
1806 /*
1807 * Check if it's a special key that we recognise. If not, call
1808 * TranslateMessage().
1809 */
1810 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1811 {
1812 vk = (int) msg.wParam;
1813
1814 /*
1815 * Handle dead keys in special conditions in other cases we let Windows
1816 * handle them and do not interfere.
1817 *
1818 * The dead_key flag must be reset on several occasions:
1819 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1820 * consumed at that point (This is when we let Windows combine the
1821 * dead character on its own)
1822 *
1823 * - Before doing something special such as regenerating keypresses to
1824 * expel the dead character as this could trigger an infinite loop if
1825 * for some reason MyTranslateMessage() do not trigger a call
1826 * immediately to _OnChar() (or _OnSysChar()).
1827 */
1828 if (dead_key)
1829 {
1830 /*
1831 * If a dead key was pressed and the user presses VK_SPACE,
1832 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1833 * with the dead char now, so do nothing special and let Windows
1834 * handle it.
1835 *
1836 * Note that VK_SPACE combines with the dead_key's character and
1837 * only one WM_CHAR will be generated by TranslateMessage(), in
1838 * the two other cases two WM_CHAR will be generated: the dead
1839 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1840 * user expects.
1841 */
1842 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1843 {
1844 dead_key = 0;
1845 MyTranslateMessage(&msg);
1846 return;
1847 }
1848 /* In modes where we are not typing, dead keys should behave
1849 * normally */
1850 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1851 {
1852 outputDeadKey_rePost(msg);
1853 return;
1854 }
1855 }
1856
1857 /* Check for CTRL-BREAK */
1858 if (vk == VK_CANCEL)
1859 {
1860 trash_input_buf();
1861 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001862 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001863 string[0] = Ctrl_C;
1864 add_to_input_buf(string, 1);
1865 }
1866
1867 for (i = 0; special_keys[i].key_sym != 0; i++)
1868 {
1869 /* ignore VK_SPACE when ALT key pressed: system menu */
1870 if (special_keys[i].key_sym == vk
1871 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1872 {
1873 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001874 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 * is a key that would normally trigger the dead key nominal
1876 * character output (such as a NUMPAD printable character or
1877 * the TAB key, etc...).
1878 */
1879 if (dead_key && (special_keys[i].vim_code0 == 'K'
1880 || vk == VK_TAB || vk == CAR))
1881 {
1882 outputDeadKey_rePost(msg);
1883 return;
1884 }
1885
1886#ifdef FEAT_MENU
1887 /* Check for <F10>: Windows selects the menu. When <F10> is
1888 * mapped we want to use the mapping instead. */
1889 if (vk == VK_F10
1890 && gui.menu_is_active
1891 && check_map(k10, State, FALSE, TRUE, FALSE,
1892 NULL, NULL) == NULL)
1893 break;
1894#endif
1895 if (GetKeyState(VK_SHIFT) & 0x8000)
1896 modifiers |= MOD_MASK_SHIFT;
1897 /*
1898 * Don't use caps-lock as shift, because these are special keys
1899 * being considered here, and we only want letters to get
1900 * shifted -- webb
1901 */
1902 /*
1903 if (GetKeyState(VK_CAPITAL) & 0x0001)
1904 modifiers ^= MOD_MASK_SHIFT;
1905 */
1906 if (GetKeyState(VK_CONTROL) & 0x8000)
1907 modifiers |= MOD_MASK_CTRL;
1908 if (GetKeyState(VK_MENU) & 0x8000)
1909 modifiers |= MOD_MASK_ALT;
1910
1911 if (special_keys[i].vim_code1 == NUL)
1912 key = special_keys[i].vim_code0;
1913 else
1914 key = TO_SPECIAL(special_keys[i].vim_code0,
1915 special_keys[i].vim_code1);
1916 key = simplify_key(key, &modifiers);
1917 if (key == CSI)
1918 key = K_CSI;
1919
1920 if (modifiers)
1921 {
1922 string[0] = CSI;
1923 string[1] = KS_MODIFIER;
1924 string[2] = modifiers;
1925 add_to_input_buf(string, 3);
1926 }
1927
1928 if (IS_SPECIAL(key))
1929 {
1930 string[0] = CSI;
1931 string[1] = K_SECOND(key);
1932 string[2] = K_THIRD(key);
1933 add_to_input_buf(string, 3);
1934 }
1935 else
1936 {
1937 int len;
1938
1939 /* Handle "key" as a Unicode character. */
1940 len = char_to_string(key, string, 40, FALSE);
1941 add_to_input_buf(string, len);
1942 }
1943 break;
1944 }
1945 }
1946 if (special_keys[i].key_sym == 0)
1947 {
1948 /* Some keys need C-S- where they should only need C-.
1949 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1950 * system startup (Helmut Stiegler, 2003 Oct 3). */
1951 if (vk != 0xff
1952 && (GetKeyState(VK_CONTROL) & 0x8000)
1953 && !(GetKeyState(VK_SHIFT) & 0x8000)
1954 && !(GetKeyState(VK_MENU) & 0x8000))
1955 {
1956 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1957 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1958 {
1959 string[0] = Ctrl_HAT;
1960 add_to_input_buf(string, 1);
1961 }
1962 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1963 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1964 {
1965 string[0] = Ctrl__;
1966 add_to_input_buf(string, 1);
1967 }
1968 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1969 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1970 {
1971 string[0] = Ctrl_AT;
1972 add_to_input_buf(string, 1);
1973 }
1974 else
1975 MyTranslateMessage(&msg);
1976 }
1977 else
1978 MyTranslateMessage(&msg);
1979 }
1980 }
1981#ifdef FEAT_MBYTE_IME
1982 else if (msg.message == WM_IME_NOTIFY)
1983 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1984 else if (msg.message == WM_KEYUP && im_get_status())
1985 /* added for non-MS IME (Yasuhiro Matsumoto) */
1986 MyTranslateMessage(&msg);
1987#endif
1988#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
1989/* GIME_TEST */
1990 else if (msg.message == WM_IME_STARTCOMPOSITION)
1991 {
1992 POINT point;
1993
1994 global_ime_set_font(&norm_logfont);
1995 point.x = FILL_X(gui.col);
1996 point.y = FILL_Y(gui.row);
1997 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
1998 global_ime_set_position(&point);
1999 }
2000#endif
2001
2002#ifdef FEAT_MENU
2003 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2004 * mapped we need to stop it here to avoid strange effects (e.g., for the
2005 * key-up event) */
2006 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2007 NULL, NULL) == NULL)
2008#endif
2009 pDispatchMessage(&msg);
2010}
2011
2012/*
2013 * Catch up with any queued events. This may put keyboard input into the
2014 * input buffer, call resize call-backs, trigger timers etc. If there is
2015 * nothing in the event queue (& no timers pending), then we return
2016 * immediately.
2017 */
2018 void
2019gui_mch_update(void)
2020{
2021 MSG msg;
2022
2023 if (!s_busy_processing)
2024 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2025 && !vim_is_input_buf_full())
2026 process_message();
2027}
2028
Bram Moolenaar4231da42016-06-02 14:30:04 +02002029 static void
2030remove_any_timer(void)
2031{
2032 MSG msg;
2033
2034 if (s_wait_timer != 0 && !s_timed_out)
2035 {
2036 KillTimer(NULL, s_wait_timer);
2037
2038 /* Eat spurious WM_TIMER messages */
2039 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2040 ;
2041 s_wait_timer = 0;
2042 }
2043}
2044
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002045/*
2046 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2047 * from the keyboard.
2048 * wtime == -1 Wait forever.
2049 * wtime == 0 This should never happen.
2050 * wtime > 0 Wait wtime milliseconds for a character.
2051 * Returns OK if a character was found to be available within the given time,
2052 * or FAIL otherwise.
2053 */
2054 int
2055gui_mch_wait_for_chars(int wtime)
2056{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002057 int focus;
2058
2059 s_timed_out = FALSE;
2060
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002061 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002062 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002063 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002064 if (s_busy_processing)
2065 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002066
2067 // When called with "wtime" zero, just want one msec.
2068 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 (TIMERPROC)_OnTimer);
2070 }
2071
2072 allow_scrollbar = TRUE;
2073
2074 focus = gui.in_focus;
2075 while (!s_timed_out)
2076 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002077 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002078 if (gui.in_focus != focus)
2079 {
2080 if (gui.in_focus)
2081 gui_mch_start_blink();
2082 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002083 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002084 focus = gui.in_focus;
2085 }
2086
2087 if (s_need_activate)
2088 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002089 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002090 s_need_activate = FALSE;
2091 }
2092
Bram Moolenaar4231da42016-06-02 14:30:04 +02002093#ifdef FEAT_TIMERS
2094 did_add_timer = FALSE;
2095#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002096#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002097 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002098 for (;;)
2099 {
2100 MSG msg;
2101
2102 parse_queued_messages();
Bram Moolenaar89c00032019-09-04 13:53:21 +02002103#ifdef FEAT_TIMERS
2104 if (did_add_timer)
2105 break;
2106#endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002107 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2108 {
2109 process_message();
2110 break;
2111 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002112 else if (input_available()
2113 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2114 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002115 break;
2116 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002117#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002118 // Don't use gui_mch_update() because then we will spin-lock until a
2119 // char arrives, instead we use GetMessage() to hang until an
2120 // event arrives. No need to check for input_buf_full because we are
2121 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002122 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002123#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124
2125 if (input_available())
2126 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002127 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128 allow_scrollbar = FALSE;
2129
Bram Moolenaar89c00032019-09-04 13:53:21 +02002130 // Clear pending mouse button, the release event may have been
2131 // taken by the dialog window. But don't do this when getting
2132 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 if (!s_getting_focus)
2134 s_button_pending = -1;
2135
2136 return OK;
2137 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002138
2139#ifdef FEAT_TIMERS
2140 if (did_add_timer)
2141 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002142 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002143 remove_any_timer();
2144 break;
2145 }
2146#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002147 }
2148 allow_scrollbar = FALSE;
2149 return FAIL;
2150}
2151
2152/*
2153 * Clear a rectangular region of the screen from text pos (row1, col1) to
2154 * (row2, col2) inclusive.
2155 */
2156 void
2157gui_mch_clear_block(
2158 int row1,
2159 int col1,
2160 int row2,
2161 int col2)
2162{
2163 RECT rc;
2164
2165 /*
2166 * Clear one extra pixel at the far right, for when bold characters have
2167 * spilled over to the window border.
2168 * Note: FillRect() excludes right and bottom of rectangle.
2169 */
2170 rc.left = FILL_X(col1);
2171 rc.top = FILL_Y(row1);
2172 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2173 rc.bottom = FILL_Y(row2 + 1);
2174 clear_rect(&rc);
2175}
2176
2177/*
2178 * Clear the whole text window.
2179 */
2180 void
2181gui_mch_clear_all(void)
2182{
2183 RECT rc;
2184
2185 rc.left = 0;
2186 rc.top = 0;
2187 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2188 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2189 clear_rect(&rc);
2190}
2191/*
2192 * Menu stuff.
2193 */
2194
2195 void
2196gui_mch_enable_menu(int flag)
2197{
2198#ifdef FEAT_MENU
2199 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2200#endif
2201}
2202
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002203 void
2204gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002205 int x UNUSED,
2206 int y UNUSED,
2207 int w UNUSED,
2208 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002209{
2210 /* It will be in the right place anyway */
2211}
2212
2213#if defined(FEAT_MENU) || defined(PROTO)
2214/*
2215 * Make menu item hidden or not hidden
2216 */
2217 void
2218gui_mch_menu_hidden(
2219 vimmenu_T *menu,
2220 int hidden)
2221{
2222 /*
2223 * This doesn't do what we want. Hmm, just grey the menu items for now.
2224 */
2225 /*
2226 if (hidden)
2227 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2228 else
2229 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2230 */
2231 gui_mch_menu_grey(menu, hidden);
2232}
2233
2234/*
2235 * This is called after setting all the menus to grey/hidden or not.
2236 */
2237 void
2238gui_mch_draw_menubar(void)
2239{
2240 DrawMenuBar(s_hwnd);
2241}
2242#endif /*FEAT_MENU*/
2243
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002244/*
2245 * Return the RGB value of a pixel as a long.
2246 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002247 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002248gui_mch_get_rgb(guicolor_T pixel)
2249{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002250 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2251 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002252}
2253
2254#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2255/* Convert pixels in X to dialog units */
2256 static WORD
2257PixelToDialogX(int numPixels)
2258{
2259 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2260}
2261
2262/* Convert pixels in Y to dialog units */
2263 static WORD
2264PixelToDialogY(int numPixels)
2265{
2266 return (WORD)((numPixels * 8) / s_dlgfntheight);
2267}
2268
2269/* Return the width in pixels of the given text in the given DC. */
2270 static int
2271GetTextWidth(HDC hdc, char_u *str, int len)
2272{
2273 SIZE size;
2274
2275 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2276 return size.cx;
2277}
2278
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279/*
2280 * Return the width in pixels of the given text in the given DC, taking care
2281 * of 'encoding' to active codepage conversion.
2282 */
2283 static int
2284GetTextWidthEnc(HDC hdc, char_u *str, int len)
2285{
2286 SIZE size;
2287 WCHAR *wstr;
2288 int n;
2289 int wlen = len;
2290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002291 wstr = enc_to_utf16(str, &wlen);
2292 if (wstr == NULL)
2293 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002294
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002295 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2296 vim_free(wstr);
2297 if (n)
2298 return size.cx;
2299 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002300}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002302static void get_work_area(RECT *spi_rect);
2303
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002304/*
2305 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002306 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2307 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002308 */
2309 static BOOL
2310CenterWindow(
2311 HWND hwndChild,
2312 HWND hwndParent)
2313{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002314 HMONITOR mon;
2315 MONITORINFO moninfo;
2316 RECT rChild, rParent, rScreen;
2317 int wChild, hChild, wParent, hParent;
2318 int xNew, yNew;
2319 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002320
2321 GetWindowRect(hwndChild, &rChild);
2322 wChild = rChild.right - rChild.left;
2323 hChild = rChild.bottom - rChild.top;
2324
2325 /* If Vim is minimized put the window in the middle of the screen. */
2326 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002327 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 else
2329 GetWindowRect(hwndParent, &rParent);
2330 wParent = rParent.right - rParent.left;
2331 hParent = rParent.bottom - rParent.top;
2332
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002333 moninfo.cbSize = sizeof(MONITORINFO);
2334 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2335 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002336 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002337 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002339 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002340 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002341 hdc = GetDC(hwndChild);
2342 rScreen.left = 0;
2343 rScreen.top = 0;
2344 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2345 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2346 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347 }
2348
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002349 xNew = rParent.left + ((wParent - wChild) / 2);
2350 if (xNew < rScreen.left)
2351 xNew = rScreen.left;
2352 else if ((xNew + wChild) > rScreen.right)
2353 xNew = rScreen.right - wChild;
2354
2355 yNew = rParent.top + ((hParent - hChild) / 2);
2356 if (yNew < rScreen.top)
2357 yNew = rScreen.top;
2358 else if ((yNew + hChild) > rScreen.bottom)
2359 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002360
2361 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2362 SWP_NOSIZE | SWP_NOZORDER);
2363}
2364#endif /* FEAT_GUI_DIALOG */
2365
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002366#if defined(FEAT_TOOLBAR) || defined(PROTO)
2367 void
2368gui_mch_show_toolbar(int showit)
2369{
2370 if (s_toolbarhwnd == NULL)
2371 return;
2372
2373 if (showit)
2374 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002375# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002376 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002377# define TB_SETUNICODEFORMAT 0x2005
2378# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002379 // Enable unicode support
2380 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2381 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002382 ShowWindow(s_toolbarhwnd, SW_SHOW);
2383 }
2384 else
2385 ShowWindow(s_toolbarhwnd, SW_HIDE);
2386}
2387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002388/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389#define TOOLBAR_BITMAP_COUNT 31
2390
2391#endif
2392
2393#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2394 static void
2395add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2396{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002397 WCHAR *wn;
2398 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002400 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002401 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002402 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002403
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002404 infow.cbSize = sizeof(infow);
2405 infow.fMask = MIIM_TYPE | MIIM_ID;
2406 infow.wID = item_id;
2407 infow.fType = MFT_STRING;
2408 infow.dwTypeData = wn;
2409 infow.cch = (UINT)wcslen(wn);
2410 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2411 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412}
2413
2414 static void
2415show_tabline_popup_menu(void)
2416{
2417 HMENU tab_pmenu;
2418 long rval;
2419 POINT pt;
2420
2421 /* When ignoring events don't show the menu. */
2422 if (hold_gui_events
2423# ifdef FEAT_CMDWIN
2424 || cmdwin_type != 0
2425# endif
2426 )
2427 return;
2428
2429 tab_pmenu = CreatePopupMenu();
2430 if (tab_pmenu == NULL)
2431 return;
2432
2433 if (first_tabpage->tp_next != NULL)
2434 add_tabline_popup_menu_entry(tab_pmenu,
2435 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2436 add_tabline_popup_menu_entry(tab_pmenu,
2437 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2438 add_tabline_popup_menu_entry(tab_pmenu,
2439 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2440
2441 GetCursorPos(&pt);
2442 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2443 NULL);
2444
2445 DestroyMenu(tab_pmenu);
2446
2447 /* Add the string cmd into input buffer */
2448 if (rval > 0)
2449 {
2450 TCHITTESTINFO htinfo;
2451 int idx;
2452
2453 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2454 return;
2455
2456 htinfo.pt.x = pt.x;
2457 htinfo.pt.y = pt.y;
2458 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2459 if (idx == -1)
2460 idx = 0;
2461 else
2462 idx += 1;
2463
2464 send_tabline_menu_event(idx, (int)rval);
2465 }
2466}
2467
2468/*
2469 * Show or hide the tabline.
2470 */
2471 void
2472gui_mch_show_tabline(int showit)
2473{
2474 if (s_tabhwnd == NULL)
2475 return;
2476
2477 if (!showit != !showing_tabline)
2478 {
2479 if (showit)
2480 ShowWindow(s_tabhwnd, SW_SHOW);
2481 else
2482 ShowWindow(s_tabhwnd, SW_HIDE);
2483 showing_tabline = showit;
2484 }
2485}
2486
2487/*
2488 * Return TRUE when tabline is displayed.
2489 */
2490 int
2491gui_mch_showing_tabline(void)
2492{
2493 return s_tabhwnd != NULL && showing_tabline;
2494}
2495
2496/*
2497 * Update the labels of the tabline.
2498 */
2499 void
2500gui_mch_update_tabline(void)
2501{
2502 tabpage_T *tp;
2503 TCITEM tie;
2504 int nr = 0;
2505 int curtabidx = 0;
2506 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002508
2509 if (s_tabhwnd == NULL)
2510 return;
2511
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002512#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002514# define CCM_SETUNICODEFORMAT 0x2005
2515#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002516 // Enable unicode support
2517 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518
2519 tie.mask = TCIF_TEXT;
2520 tie.iImage = -1;
2521
2522 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2523 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2524
2525 /* Add a label for each tab page. They all contain the same text area. */
2526 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2527 {
2528 if (tp == curtab)
2529 curtabidx = nr;
2530
2531 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2532 {
2533 /* Add the tab */
2534 tie.pszText = "-Empty-";
2535 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2536 tabadded = 1;
2537 }
2538
2539 get_tabline_label(tp, FALSE);
2540 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002542 wstr = enc_to_utf16(NameBuff, NULL);
2543 if (wstr != NULL)
2544 {
2545 TCITEMW tiw;
2546
2547 tiw.mask = TCIF_TEXT;
2548 tiw.iImage = -1;
2549 tiw.pszText = wstr;
2550 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2551 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002552 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 }
2554
2555 /* Remove any old labels. */
2556 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2557 TabCtrl_DeleteItem(s_tabhwnd, nr);
2558
2559 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2560 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2561
2562 /* Re-enable redraw and redraw. */
2563 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2564 RedrawWindow(s_tabhwnd, NULL, NULL,
2565 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2566
2567 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2568 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2569}
2570
2571/*
2572 * Set the current tab to "nr". First tab is 1.
2573 */
2574 void
2575gui_mch_set_curtab(int nr)
2576{
2577 if (s_tabhwnd == NULL)
2578 return;
2579
2580 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2581 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2582}
2583
2584#endif
2585
2586/*
2587 * ":simalt" command.
2588 */
2589 void
2590ex_simalt(exarg_T *eap)
2591{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002592 char_u *keys = eap->arg;
2593 int fill_typebuf = FALSE;
2594 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002595
2596 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2597 while (*keys)
2598 {
2599 if (*keys == '~')
2600 *keys = ' '; /* for showing system menu */
2601 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2602 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002603 fill_typebuf = TRUE;
2604 }
2605 if (fill_typebuf)
2606 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002607 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002608 * processed. */
2609 key_name[0] = K_SPECIAL;
2610 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002611 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002612 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002613#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002614 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002615#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002616 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 }
2618}
2619
2620/*
2621 * Create the find & replace dialogs.
2622 * You can't have both at once: ":find" when replace is showing, destroys
2623 * the replace dialog first, and the other way around.
2624 */
2625#ifdef MSWIN_FIND_REPLACE
2626 static void
2627initialise_findrep(char_u *initial_string)
2628{
2629 int wword = FALSE;
2630 int mcase = !p_ic;
2631 char_u *entry_text;
2632
2633 /* Get the search string to use. */
2634 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2635
2636 s_findrep_struct.hwndOwner = s_hwnd;
2637 s_findrep_struct.Flags = FR_DOWN;
2638 if (mcase)
2639 s_findrep_struct.Flags |= FR_MATCHCASE;
2640 if (wword)
2641 s_findrep_struct.Flags |= FR_WHOLEWORD;
2642 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002643 {
2644 WCHAR *p = enc_to_utf16(entry_text, NULL);
2645 if (p != NULL)
2646 {
2647 int len = s_findrep_struct.wFindWhatLen - 1;
2648
2649 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2650 s_findrep_struct.lpstrFindWhat[len] = NUL;
2651 vim_free(p);
2652 }
2653 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002654 vim_free(entry_text);
2655}
2656#endif
2657
2658 static void
2659set_window_title(HWND hwnd, char *title)
2660{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002661 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 {
2663 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664
2665 /* Convert the title from 'encoding' to UTF-16. */
2666 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2667 if (wbuf != NULL)
2668 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002669 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002670 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002671 }
2672 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002673 else
2674 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002675}
2676
2677 void
2678gui_mch_find_dialog(exarg_T *eap)
2679{
2680#ifdef MSWIN_FIND_REPLACE
2681 if (s_findrep_msg != 0)
2682 {
2683 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2684 DestroyWindow(s_findrep_hwnd);
2685
2686 if (!IsWindow(s_findrep_hwnd))
2687 {
2688 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002689 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002690 }
2691
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002692 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 (void)SetFocus(s_findrep_hwnd);
2694
2695 s_findrep_is_find = TRUE;
2696 }
2697#endif
2698}
2699
2700
2701 void
2702gui_mch_replace_dialog(exarg_T *eap)
2703{
2704#ifdef MSWIN_FIND_REPLACE
2705 if (s_findrep_msg != 0)
2706 {
2707 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2708 DestroyWindow(s_findrep_hwnd);
2709
2710 if (!IsWindow(s_findrep_hwnd))
2711 {
2712 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002713 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002714 }
2715
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002716 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002717 (void)SetFocus(s_findrep_hwnd);
2718
2719 s_findrep_is_find = FALSE;
2720 }
2721#endif
2722}
2723
2724
2725/*
2726 * Set visibility of the pointer.
2727 */
2728 void
2729gui_mch_mousehide(int hide)
2730{
2731 if (hide != gui.pointer_hidden)
2732 {
2733 ShowCursor(!hide);
2734 gui.pointer_hidden = hide;
2735 }
2736}
2737
2738#ifdef FEAT_MENU
2739 static void
2740gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2741{
2742 /* Unhide the mouse, we don't get move events here. */
2743 gui_mch_mousehide(FALSE);
2744
2745 (void)TrackPopupMenu(
2746 (HMENU)menu->submenu_id,
2747 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2748 x, y,
2749 (int)0, /*reserved param*/
2750 s_hwnd,
2751 NULL);
2752 /*
2753 * NOTE: The pop-up menu can eat the mouse up event.
2754 * We deal with this in normal.c.
2755 */
2756}
2757#endif
2758
2759/*
2760 * Got a message when the system will go down.
2761 */
2762 static void
2763_OnEndSession(void)
2764{
2765 getout_preserve_modified(1);
2766}
2767
2768/*
2769 * Get this message when the user clicks on the cross in the top right corner
2770 * of a Windows95 window.
2771 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002773_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774{
2775 gui_shell_closed();
2776}
2777
2778/*
2779 * Get a message when the window is being destroyed.
2780 */
2781 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002782_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783{
2784 if (!destroying)
2785 _OnClose(hwnd);
2786}
2787
2788 static void
2789_OnPaint(
2790 HWND hwnd)
2791{
2792 if (!IsMinimized(hwnd))
2793 {
2794 PAINTSTRUCT ps;
2795
2796 out_flush(); /* make sure all output has been processed */
2797 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002798
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 /* prevent multi-byte characters from misprinting on an invalid
2800 * rectangle */
2801 if (has_mbyte)
2802 {
2803 RECT rect;
2804
2805 GetClientRect(hwnd, &rect);
2806 ps.rcPaint.left = rect.left;
2807 ps.rcPaint.right = rect.right;
2808 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002809
2810 if (!IsRectEmpty(&ps.rcPaint))
2811 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002812 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2813 ps.rcPaint.right - ps.rcPaint.left + 1,
2814 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2815 }
2816
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002817 EndPaint(hwnd, &ps);
2818 }
2819}
2820
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002821 static void
2822_OnSize(
2823 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002824 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002825 int cx,
2826 int cy)
2827{
2828 if (!IsMinimized(hwnd))
2829 {
2830 gui_resize_shell(cx, cy);
2831
2832#ifdef FEAT_MENU
2833 /* Menu bar may wrap differently now */
2834 gui_mswin_get_menu_height(TRUE);
2835#endif
2836 }
2837}
2838
2839 static void
2840_OnSetFocus(
2841 HWND hwnd,
2842 HWND hwndOldFocus)
2843{
2844 gui_focus_change(TRUE);
2845 s_getting_focus = TRUE;
2846 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2847}
2848
2849 static void
2850_OnKillFocus(
2851 HWND hwnd,
2852 HWND hwndNewFocus)
2853{
2854 gui_focus_change(FALSE);
2855 s_getting_focus = FALSE;
2856 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2857}
2858
2859/*
2860 * Get a message when the user switches back to vim
2861 */
2862 static LRESULT
2863_OnActivateApp(
2864 HWND hwnd,
2865 BOOL fActivate,
2866 DWORD dwThreadId)
2867{
2868 /* we call gui_focus_change() in _OnSetFocus() */
2869 /* gui_focus_change((int)fActivate); */
2870 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2871}
2872
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 void
2874gui_mch_destroy_scrollbar(scrollbar_T *sb)
2875{
2876 DestroyWindow(sb->id);
2877}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002878
2879/*
2880 * Get current mouse coordinates in text window.
2881 */
2882 void
2883gui_mch_getmouse(int *x, int *y)
2884{
2885 RECT rct;
2886 POINT mp;
2887
2888 (void)GetWindowRect(s_textArea, &rct);
2889 (void)GetCursorPos((LPPOINT)&mp);
2890 *x = (int)(mp.x - rct.left);
2891 *y = (int)(mp.y - rct.top);
2892}
2893
2894/*
2895 * Move mouse pointer to character at (x, y).
2896 */
2897 void
2898gui_mch_setmouse(int x, int y)
2899{
2900 RECT rct;
2901
2902 (void)GetWindowRect(s_textArea, &rct);
2903 (void)SetCursorPos(x + gui.border_offset + rct.left,
2904 y + gui.border_offset + rct.top);
2905}
2906
2907 static void
2908gui_mswin_get_valid_dimensions(
2909 int w,
2910 int h,
2911 int *valid_w,
2912 int *valid_h)
2913{
2914 int base_width, base_height;
2915
2916 base_width = gui_get_base_width()
2917 + (GetSystemMetrics(SM_CXFRAME) +
2918 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2919 base_height = gui_get_base_height()
2920 + (GetSystemMetrics(SM_CYFRAME) +
2921 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2922 + GetSystemMetrics(SM_CYCAPTION)
2923#ifdef FEAT_MENU
2924 + gui_mswin_get_menu_height(FALSE)
2925#endif
2926 ;
2927 *valid_w = base_width +
2928 ((w - base_width) / gui.char_width) * gui.char_width;
2929 *valid_h = base_height +
2930 ((h - base_height) / gui.char_height) * gui.char_height;
2931}
2932
2933 void
2934gui_mch_flash(int msec)
2935{
2936 RECT rc;
2937
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002938#if defined(FEAT_DIRECTX)
2939 if (IS_ENABLE_DIRECTX())
2940 DWriteContext_Flush(s_dwc);
2941#endif
2942
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002943 /*
2944 * Note: InvertRect() excludes right and bottom of rectangle.
2945 */
2946 rc.left = 0;
2947 rc.top = 0;
2948 rc.right = gui.num_cols * gui.char_width;
2949 rc.bottom = gui.num_rows * gui.char_height;
2950 InvertRect(s_hdc, &rc);
2951 gui_mch_flush(); /* make sure it's displayed */
2952
2953 ui_delay((long)msec, TRUE); /* wait for a few msec */
2954
2955 InvertRect(s_hdc, &rc);
2956}
2957
2958/*
2959 * Return flags used for scrolling.
2960 * The SW_INVALIDATE is required when part of the window is covered or
2961 * off-screen. Refer to MS KB Q75236.
2962 */
2963 static int
2964get_scroll_flags(void)
2965{
2966 HWND hwnd;
2967 RECT rcVim, rcOther, rcDest;
2968
2969 GetWindowRect(s_hwnd, &rcVim);
2970
2971 /* Check if the window is partly above or below the screen. We don't care
2972 * about partly left or right of the screen, it is not relevant when
2973 * scrolling up or down. */
2974 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2975 return SW_INVALIDATE;
2976
2977 /* Check if there is an window (partly) on top of us. */
2978 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2979 if (IsWindowVisible(hwnd))
2980 {
2981 GetWindowRect(hwnd, &rcOther);
2982 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2983 return SW_INVALIDATE;
2984 }
2985 return 0;
2986}
2987
2988/*
2989 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
2990 * may not be scrolled out properly.
2991 * For gVim, when _OnScroll() is repeated, the character at the
2992 * previous cursor position may be left drawn after scroll.
2993 * The problem can be avoided by calling GetPixel() to get a pixel in
2994 * the region before ScrollWindowEx().
2995 */
2996 static void
2997intel_gpu_workaround(void)
2998{
2999 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3000}
3001
3002/*
3003 * Delete the given number of lines from the given row, scrolling up any
3004 * text further down within the scroll region.
3005 */
3006 void
3007gui_mch_delete_lines(
3008 int row,
3009 int num_lines)
3010{
3011 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003012
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003013 rc.left = FILL_X(gui.scroll_region_left);
3014 rc.right = FILL_X(gui.scroll_region_right + 1);
3015 rc.top = FILL_Y(row);
3016 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3017
Bram Moolenaar92467d32017-12-05 13:22:16 +01003018#if defined(FEAT_DIRECTX)
3019 if (IS_ENABLE_DIRECTX())
3020 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003021 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3022 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003023 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003024 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003025#endif
3026 {
3027 intel_gpu_workaround();
3028 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003029 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003030 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003031 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003033 /* This seems to be required to avoid the cursor disappearing when
3034 * scrolling such that the cursor ends up in the top-left character on
3035 * the screen... But why? (Webb) */
3036 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3037 /* gui.cursor_is_valid = FALSE; */
3038
3039 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3040 gui.scroll_region_left,
3041 gui.scroll_region_bot, gui.scroll_region_right);
3042}
3043
3044/*
3045 * Insert the given number of lines before the given row, scrolling down any
3046 * following text within the scroll region.
3047 */
3048 void
3049gui_mch_insert_lines(
3050 int row,
3051 int num_lines)
3052{
3053 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003054
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055 rc.left = FILL_X(gui.scroll_region_left);
3056 rc.right = FILL_X(gui.scroll_region_right + 1);
3057 rc.top = FILL_Y(row);
3058 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003059
3060#if defined(FEAT_DIRECTX)
3061 if (IS_ENABLE_DIRECTX())
3062 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003063 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3064 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003065 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003066 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003067#endif
3068 {
3069 intel_gpu_workaround();
3070 /* The SW_INVALIDATE is required when part of the window is covered or
3071 * off-screen. How do we avoid it when it's not needed? */
3072 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003073 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003074 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003075 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077 gui_clear_block(row, gui.scroll_region_left,
3078 row + num_lines - 1, gui.scroll_region_right);
3079}
3080
3081
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003082 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003083gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003084{
3085#if defined(FEAT_DIRECTX)
3086 DWriteContext_Close(s_dwc);
3087 DWrite_Final();
3088 s_dwc = NULL;
3089#endif
3090
3091 ReleaseDC(s_textArea, s_hdc);
3092 DeleteObject(s_brush);
3093
3094#ifdef FEAT_TEAROFF
3095 /* Unload the tearoff bitmap */
3096 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3097#endif
3098
3099 /* Destroy our window (if we have one). */
3100 if (s_hwnd != NULL)
3101 {
3102 destroying = TRUE; /* ignore WM_DESTROY message now */
3103 DestroyWindow(s_hwnd);
3104 }
3105
3106#ifdef GLOBAL_IME
3107 global_ime_end();
3108#endif
3109}
3110
3111 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003112logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113{
3114 char *p;
3115 char *res;
3116 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003117 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003118 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003119 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003121 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3122 if (font_name == NULL)
3123 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003125 quality_name = quality_id2name((int)lf.lfQuality);
3126
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003127 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003128 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003129 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 if (res != NULL)
3131 {
3132 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003133 // make a normal font string out of the lf thing:
3134 points = pixels_to_points(
3135 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3136 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3137 sprintf((char *)p, "%s:h%d", font_name, points);
3138 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003139 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140 while (*p)
3141 {
3142 if (*p == ' ')
3143 *p = '_';
3144 ++p;
3145 }
3146 if (lf.lfItalic)
3147 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003148 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149 STRCAT(p, ":b");
3150 if (lf.lfUnderline)
3151 STRCAT(p, ":u");
3152 if (lf.lfStrikeOut)
3153 STRCAT(p, ":s");
3154 if (charset_name != NULL)
3155 {
3156 STRCAT(p, ":c");
3157 STRCAT(p, charset_name);
3158 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003159 if (quality_name != NULL)
3160 {
3161 STRCAT(p, ":q");
3162 STRCAT(p, quality_name);
3163 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164 }
3165
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003166 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003167 return (char_u *)res;
3168}
3169
3170
3171#ifdef FEAT_MBYTE_IME
3172/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003173 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003174 * 'guifont'
3175 */
3176 static void
3177update_im_font(void)
3178{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003179 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180
3181 if (p_guifontwide != NULL && *p_guifontwide != NUL
3182 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003183 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003184 norm_logfont = lf_wide;
3185 else
3186 norm_logfont = sub_logfont;
3187 im_set_font(&norm_logfont);
3188}
3189#endif
3190
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191/*
3192 * Handler of gui.wide_font (p_guifontwide) changed notification.
3193 */
3194 void
3195gui_mch_wide_font_changed(void)
3196{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003197 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003199#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003201#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003202
3203 gui_mch_free_font(gui.wide_ital_font);
3204 gui.wide_ital_font = NOFONT;
3205 gui_mch_free_font(gui.wide_bold_font);
3206 gui.wide_bold_font = NOFONT;
3207 gui_mch_free_font(gui.wide_boldital_font);
3208 gui.wide_boldital_font = NOFONT;
3209
3210 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003211 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003212 {
3213 if (!lf.lfItalic)
3214 {
3215 lf.lfItalic = TRUE;
3216 gui.wide_ital_font = get_font_handle(&lf);
3217 lf.lfItalic = FALSE;
3218 }
3219 if (lf.lfWeight < FW_BOLD)
3220 {
3221 lf.lfWeight = FW_BOLD;
3222 gui.wide_bold_font = get_font_handle(&lf);
3223 if (!lf.lfItalic)
3224 {
3225 lf.lfItalic = TRUE;
3226 gui.wide_boldital_font = get_font_handle(&lf);
3227 }
3228 }
3229 }
3230}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231
3232/*
3233 * Initialise vim to use the font with the given name.
3234 * Return FAIL if the font could not be loaded, OK otherwise.
3235 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003236 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003237gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003239 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 GuiFont font = NOFONT;
3241 char_u *p;
3242
3243 /* Load the font */
3244 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3245 font = get_font_handle(&lf);
3246 if (font == NOFONT)
3247 return FAIL;
3248
3249 if (font_name == NULL)
3250 font_name = (char_u *)lf.lfFaceName;
3251#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3252 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003253#endif
3254#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255 sub_logfont = lf;
3256#endif
3257#ifdef FEAT_MBYTE_IME
3258 update_im_font();
3259#endif
3260 gui_mch_free_font(gui.norm_font);
3261 gui.norm_font = font;
3262 current_font_height = lf.lfHeight;
3263 GetFontSize(font);
3264
3265 p = logfont2name(lf);
3266 if (p != NULL)
3267 {
3268 hl_set_font_name(p);
3269
3270 /* When setting 'guifont' to "*" replace it with the actual font name.
3271 * */
3272 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3273 {
3274 vim_free(p_guifont);
3275 p_guifont = p;
3276 }
3277 else
3278 vim_free(p);
3279 }
3280
3281 gui_mch_free_font(gui.ital_font);
3282 gui.ital_font = NOFONT;
3283 gui_mch_free_font(gui.bold_font);
3284 gui.bold_font = NOFONT;
3285 gui_mch_free_font(gui.boldital_font);
3286 gui.boldital_font = NOFONT;
3287
3288 if (!lf.lfItalic)
3289 {
3290 lf.lfItalic = TRUE;
3291 gui.ital_font = get_font_handle(&lf);
3292 lf.lfItalic = FALSE;
3293 }
3294 if (lf.lfWeight < FW_BOLD)
3295 {
3296 lf.lfWeight = FW_BOLD;
3297 gui.bold_font = get_font_handle(&lf);
3298 if (!lf.lfItalic)
3299 {
3300 lf.lfItalic = TRUE;
3301 gui.boldital_font = get_font_handle(&lf);
3302 }
3303 }
3304
3305 return OK;
3306}
3307
3308#ifndef WPF_RESTORETOMAXIMIZED
3309# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3310#endif
3311
3312/*
3313 * Return TRUE if the GUI window is maximized, filling the whole screen.
3314 */
3315 int
3316gui_mch_maximized(void)
3317{
3318 WINDOWPLACEMENT wp;
3319
3320 wp.length = sizeof(WINDOWPLACEMENT);
3321 if (GetWindowPlacement(s_hwnd, &wp))
3322 return wp.showCmd == SW_SHOWMAXIMIZED
3323 || (wp.showCmd == SW_SHOWMINIMIZED
3324 && wp.flags == WPF_RESTORETOMAXIMIZED);
3325
3326 return 0;
3327}
3328
3329/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003330 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3331 * is set. Compute the new Rows and Columns. This is like resizing the
3332 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003333 */
3334 void
3335gui_mch_newfont(void)
3336{
3337 RECT rect;
3338
3339 GetWindowRect(s_hwnd, &rect);
3340 if (win_socket_id == 0)
3341 {
3342 gui_resize_shell(rect.right - rect.left
3343 - (GetSystemMetrics(SM_CXFRAME) +
3344 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3345 rect.bottom - rect.top
3346 - (GetSystemMetrics(SM_CYFRAME) +
3347 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3348 - GetSystemMetrics(SM_CYCAPTION)
3349#ifdef FEAT_MENU
3350 - gui_mswin_get_menu_height(FALSE)
3351#endif
3352 );
3353 }
3354 else
3355 {
3356 /* Inside another window, don't use the frame and border. */
3357 gui_resize_shell(rect.right - rect.left,
3358 rect.bottom - rect.top
3359#ifdef FEAT_MENU
3360 - gui_mswin_get_menu_height(FALSE)
3361#endif
3362 );
3363 }
3364}
3365
3366/*
3367 * Set the window title
3368 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 void
3370gui_mch_settitle(
3371 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003372 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003373{
3374 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3375}
3376
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003377#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3379 * misc2.c! */
3380static LPCSTR mshape_idcs[] =
3381{
3382 IDC_ARROW, /* arrow */
3383 MAKEINTRESOURCE(0), /* blank */
3384 IDC_IBEAM, /* beam */
3385 IDC_SIZENS, /* updown */
3386 IDC_SIZENS, /* udsizing */
3387 IDC_SIZEWE, /* leftright */
3388 IDC_SIZEWE, /* lrsizing */
3389 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 IDC_ARROW, /* crosshair */
3392 IDC_ARROW, /* hand1 */
3393 IDC_ARROW, /* hand2 */
3394 IDC_ARROW, /* pencil */
3395 IDC_ARROW, /* question */
3396 IDC_ARROW, /* right-arrow */
3397 IDC_UPARROW, /* up-arrow */
3398 IDC_ARROW /* last one */
3399};
3400
3401 void
3402mch_set_mouse_shape(int shape)
3403{
3404 LPCSTR idc;
3405
3406 if (shape == MSHAPE_HIDE)
3407 ShowCursor(FALSE);
3408 else
3409 {
3410 if (shape >= MSHAPE_NUMBERED)
3411 idc = IDC_ARROW;
3412 else
3413 idc = mshape_idcs[shape];
3414#ifdef SetClassLongPtr
3415 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3416#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003417 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418#endif
3419 if (!p_mh)
3420 {
3421 POINT mp;
3422
3423 /* Set the position to make it redrawn with the new shape. */
3424 (void)GetCursorPos((LPPOINT)&mp);
3425 (void)SetCursorPos(mp.x, mp.y);
3426 ShowCursor(TRUE);
3427 }
3428 }
3429}
3430#endif
3431
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003432#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003434 * Wide version of convert_filter().
3435 */
3436 static WCHAR *
3437convert_filterW(char_u *s)
3438{
3439 char_u *tmp;
3440 int len;
3441 WCHAR *res;
3442
3443 tmp = convert_filter(s);
3444 if (tmp == NULL)
3445 return NULL;
3446 len = (int)STRLEN(s) + 3;
3447 res = enc_to_utf16(tmp, &len);
3448 vim_free(tmp);
3449 return res;
3450}
3451
3452/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003453 * Pop open a file browser and return the file selected, in allocated memory,
3454 * or NULL if Cancel is hit.
3455 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3456 * title - Title message for the file browser dialog.
3457 * dflt - Default name of file.
3458 * ext - Default extension to be added to files without extensions.
3459 * initdir - directory in which to open the browser (NULL = current dir)
3460 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003462 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003463gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 int saving,
3465 char_u *title,
3466 char_u *dflt,
3467 char_u *ext,
3468 char_u *initdir,
3469 char_u *filter)
3470{
3471 /* We always use the wide function. This means enc_to_utf16() must work,
3472 * otherwise it fails miserably! */
3473 OPENFILENAMEW fileStruct;
3474 WCHAR fileBuf[MAXPATHL];
3475 WCHAR *wp;
3476 int i;
3477 WCHAR *titlep = NULL;
3478 WCHAR *extp = NULL;
3479 WCHAR *initdirp = NULL;
3480 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003481 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482
3483 if (dflt == NULL)
3484 fileBuf[0] = NUL;
3485 else
3486 {
3487 wp = enc_to_utf16(dflt, NULL);
3488 if (wp == NULL)
3489 fileBuf[0] = NUL;
3490 else
3491 {
3492 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3493 fileBuf[i] = wp[i];
3494 fileBuf[i] = NUL;
3495 vim_free(wp);
3496 }
3497 }
3498
3499 /* Convert the filter to Windows format. */
3500 filterp = convert_filterW(filter);
3501
3502 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003503# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003505 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003506# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003508# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003509
3510 if (title != NULL)
3511 titlep = enc_to_utf16(title, NULL);
3512 fileStruct.lpstrTitle = titlep;
3513
3514 if (ext != NULL)
3515 extp = enc_to_utf16(ext, NULL);
3516 fileStruct.lpstrDefExt = extp;
3517
3518 fileStruct.lpstrFile = fileBuf;
3519 fileStruct.nMaxFile = MAXPATHL;
3520 fileStruct.lpstrFilter = filterp;
3521 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3522 /* has an initial dir been specified? */
3523 if (initdir != NULL && *initdir != NUL)
3524 {
3525 /* Must have backslashes here, no matter what 'shellslash' says */
3526 initdirp = enc_to_utf16(initdir, NULL);
3527 if (initdirp != NULL)
3528 {
3529 for (wp = initdirp; *wp != NUL; ++wp)
3530 if (*wp == '/')
3531 *wp = '\\';
3532 }
3533 fileStruct.lpstrInitialDir = initdirp;
3534 }
3535
3536 /*
3537 * TODO: Allow selection of multiple files. Needs another arg to this
3538 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3539 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3540 * files that don't exist yet, so I haven't put it in. What about
3541 * OFN_PATHMUSTEXIST?
3542 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3543 */
3544 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003545# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546 if (curbuf->b_p_bin)
3547 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003548# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549 if (saving)
3550 {
3551 if (!GetSaveFileNameW(&fileStruct))
3552 return NULL;
3553 }
3554 else
3555 {
3556 if (!GetOpenFileNameW(&fileStruct))
3557 return NULL;
3558 }
3559
3560 vim_free(filterp);
3561 vim_free(initdirp);
3562 vim_free(titlep);
3563 vim_free(extp);
3564
3565 /* Convert from UCS2 to 'encoding'. */
3566 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003567 if (p == NULL)
3568 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569
3570 /* Give focus back to main window (when using MDI). */
3571 SetFocus(s_hwnd);
3572
3573 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003574 q = vim_strsave(shorten_fname1(p));
3575 vim_free(p);
3576 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578
3579
3580/*
3581 * Convert the string s to the proper format for a filter string by replacing
3582 * the \t and \n delimiters with \0.
3583 * Returns the converted string in allocated memory.
3584 *
3585 * Keep in sync with convert_filterW() above!
3586 */
3587 static char_u *
3588convert_filter(char_u *s)
3589{
3590 char_u *res;
3591 unsigned s_len = (unsigned)STRLEN(s);
3592 unsigned i;
3593
3594 res = alloc(s_len + 3);
3595 if (res != NULL)
3596 {
3597 for (i = 0; i < s_len; ++i)
3598 if (s[i] == '\t' || s[i] == '\n')
3599 res[i] = '\0';
3600 else
3601 res[i] = s[i];
3602 res[s_len] = NUL;
3603 /* Add two extra NULs to make sure it's properly terminated. */
3604 res[s_len + 1] = NUL;
3605 res[s_len + 2] = NUL;
3606 }
3607 return res;
3608}
3609
3610/*
3611 * Select a directory.
3612 */
3613 char_u *
3614gui_mch_browsedir(char_u *title, char_u *initdir)
3615{
3616 /* We fake this: Use a filter that doesn't select anything and a default
3617 * file name that won't be used. */
3618 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3619 initdir, (char_u *)_("Directory\t*.nothing\n"));
3620}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003621#endif /* FEAT_BROWSE */
3622
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003623 static void
3624_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003625 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003626 HDROP hDrop)
3627{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003628#define BUFPATHLEN _MAX_PATH
3629#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 char szFile[BUFPATHLEN];
3632 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3633 UINT i;
3634 char_u **fnames;
3635 POINT pt;
3636 int_u modifiers = 0;
3637
3638 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3639
3640 /* Obtain dropped position */
3641 DragQueryPoint(hDrop, &pt);
3642 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3643
3644 reset_VIsual();
3645
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003646 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647
3648 if (fnames != NULL)
3649 for (i = 0; i < cFiles; ++i)
3650 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3652 fnames[i] = utf16_to_enc(wszFile, NULL);
3653 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654 {
3655 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3656 fnames[i] = vim_strsave((char_u *)szFile);
3657 }
3658 }
3659
3660 DragFinish(hDrop);
3661
3662 if (fnames != NULL)
3663 {
3664 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3665 modifiers |= MOUSE_SHIFT;
3666 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3667 modifiers |= MOUSE_CTRL;
3668 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3669 modifiers |= MOUSE_ALT;
3670
3671 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3672
3673 s_need_activate = TRUE;
3674 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675}
3676
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 static int
3678_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003679 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 HWND hwndCtl,
3681 UINT code,
3682 int pos)
3683{
3684 static UINT prev_code = 0; /* code of previous call */
3685 scrollbar_T *sb, *sb_info;
3686 long val;
3687 int dragging = FALSE;
3688 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 SCROLLINFO si;
3690
3691 si.cbSize = sizeof(si);
3692 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003693
3694 sb = gui_mswin_find_scrollbar(hwndCtl);
3695 if (sb == NULL)
3696 return 0;
3697
3698 if (sb->wp != NULL) /* Left or right scrollbar */
3699 {
3700 /*
3701 * Careful: need to get scrollbar info out of first (left) scrollbar
3702 * for window, but keep real scrollbar too because we must pass it to
3703 * gui_drag_scrollbar().
3704 */
3705 sb_info = &sb->wp->w_scrollbars[0];
3706 }
3707 else /* Bottom scrollbar */
3708 sb_info = sb;
3709 val = sb_info->value;
3710
3711 switch (code)
3712 {
3713 case SB_THUMBTRACK:
3714 val = pos;
3715 dragging = TRUE;
3716 if (sb->scroll_shift > 0)
3717 val <<= sb->scroll_shift;
3718 break;
3719 case SB_LINEDOWN:
3720 val++;
3721 break;
3722 case SB_LINEUP:
3723 val--;
3724 break;
3725 case SB_PAGEDOWN:
3726 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3727 break;
3728 case SB_PAGEUP:
3729 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3730 break;
3731 case SB_TOP:
3732 val = 0;
3733 break;
3734 case SB_BOTTOM:
3735 val = sb_info->max;
3736 break;
3737 case SB_ENDSCROLL:
3738 if (prev_code == SB_THUMBTRACK)
3739 {
3740 /*
3741 * "pos" only gives us 16-bit data. In case of large file,
3742 * use GetScrollPos() which returns 32-bit. Unfortunately it
3743 * is not valid while the scrollbar is being dragged.
3744 */
3745 val = GetScrollPos(hwndCtl, SB_CTL);
3746 if (sb->scroll_shift > 0)
3747 val <<= sb->scroll_shift;
3748 }
3749 break;
3750
3751 default:
3752 /* TRACE("Unknown scrollbar event %d\n", code); */
3753 return 0;
3754 }
3755 prev_code = code;
3756
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3758 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759
3760 /*
3761 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3762 */
3763 if (sb->wp != NULL)
3764 {
3765 scrollbar_T *sba = sb->wp->w_scrollbars;
3766 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3767
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769 }
3770
3771 /* Don't let us be interrupted here by another message. */
3772 s_busy_processing = TRUE;
3773
3774 /* When "allow_scrollbar" is FALSE still need to remember the new
3775 * position, but don't actually scroll by setting "dont_scroll". */
3776 dont_scroll = !allow_scrollbar;
3777
Bram Moolenaara338adc2018-01-31 20:51:47 +01003778 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003780 mch_enable_flush();
3781 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782
3783 s_busy_processing = FALSE;
3784 dont_scroll = dont_scroll_save;
3785
3786 return 0;
3787}
3788
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790#ifdef FEAT_XPM_W32
3791# include "xpm_w32.h"
3792#endif
3793
3794#ifdef PROTO
3795# define WINAPI
3796#endif
3797
3798#ifdef __MINGW32__
3799/*
3800 * Add a lot of missing defines.
3801 * They are not always missing, we need the #ifndef's.
3802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803# ifndef IsMinimized
3804# define IsMinimized(hwnd) IsIconic(hwnd)
3805# endif
3806# ifndef IsMaximized
3807# define IsMaximized(hwnd) IsZoomed(hwnd)
3808# endif
3809# ifndef SelectFont
3810# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3811# endif
3812# ifndef GetStockBrush
3813# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3814# endif
3815# ifndef DeleteBrush
3816# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3817# endif
3818
3819# ifndef HANDLE_WM_RBUTTONDBLCLK
3820# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3821 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3822# endif
3823# ifndef HANDLE_WM_MBUTTONUP
3824# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3825 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3826# endif
3827# ifndef HANDLE_WM_MBUTTONDBLCLK
3828# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3829 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3830# endif
3831# ifndef HANDLE_WM_LBUTTONDBLCLK
3832# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3833 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3834# endif
3835# ifndef HANDLE_WM_RBUTTONDOWN
3836# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3837 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3838# endif
3839# ifndef HANDLE_WM_MOUSEMOVE
3840# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3841 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3842# endif
3843# ifndef HANDLE_WM_RBUTTONUP
3844# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3845 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3846# endif
3847# ifndef HANDLE_WM_MBUTTONDOWN
3848# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3849 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3850# endif
3851# ifndef HANDLE_WM_LBUTTONUP
3852# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3853 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3854# endif
3855# ifndef HANDLE_WM_LBUTTONDOWN
3856# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3857 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3858# endif
3859# ifndef HANDLE_WM_SYSCHAR
3860# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3861 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3862# endif
3863# ifndef HANDLE_WM_ACTIVATEAPP
3864# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3865 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3866# endif
3867# ifndef HANDLE_WM_WINDOWPOSCHANGING
3868# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3869 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3870# endif
3871# ifndef HANDLE_WM_VSCROLL
3872# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3873 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3874# endif
3875# ifndef HANDLE_WM_SETFOCUS
3876# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3877 ((fn)((hwnd), (HWND)(wParam)), 0L)
3878# endif
3879# ifndef HANDLE_WM_KILLFOCUS
3880# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3881 ((fn)((hwnd), (HWND)(wParam)), 0L)
3882# endif
3883# ifndef HANDLE_WM_HSCROLL
3884# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3885 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3886# endif
3887# ifndef HANDLE_WM_DROPFILES
3888# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3889 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3890# endif
3891# ifndef HANDLE_WM_CHAR
3892# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3893 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3894# endif
3895# ifndef HANDLE_WM_SYSDEADCHAR
3896# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3897 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3898# endif
3899# ifndef HANDLE_WM_DEADCHAR
3900# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3901 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3902# endif
3903#endif /* __MINGW32__ */
3904
3905
3906/* Some parameters for tearoff menus. All in pixels. */
3907#define TEAROFF_PADDING_X 2
3908#define TEAROFF_BUTTON_PAD_X 8
3909#define TEAROFF_MIN_WIDTH 200
3910#define TEAROFF_SUBMENU_LABEL ">>"
3911#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3912
3913
3914/* For the Intellimouse: */
3915#ifndef WM_MOUSEWHEEL
3916#define WM_MOUSEWHEEL 0x20a
3917#endif
3918
3919
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003920#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921# define ID_BEVAL_TOOLTIP 200
3922# define BEVAL_TEXT_LEN MAXPATHL
3923
Bram Moolenaar167632f2010-05-26 21:42:54 +02003924#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003925/* Work around old versions of basetsd.h which wrongly declares
3926 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003927# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003928# define UINT_PTR UINT
3929#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003932static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003934
Bram Moolenaar82881492012-11-20 16:53:39 +01003935
3936/* cproto fails on missing include files */
3937#ifndef PROTO
3938
Bram Moolenaar45360022005-07-21 21:08:21 +00003939/*
3940 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003941 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003942 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003943# include <pshpack1.h>
3944
3945#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003946
3947typedef struct _DllVersionInfo
3948{
3949 DWORD cbSize;
3950 DWORD dwMajorVersion;
3951 DWORD dwMinorVersion;
3952 DWORD dwBuildNumber;
3953 DWORD dwPlatformID;
3954} DLLVERSIONINFO;
3955
Bram Moolenaar82881492012-11-20 16:53:39 +01003956#ifndef PROTO
3957# include <poppack.h>
3958#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003959
Bram Moolenaar45360022005-07-21 21:08:21 +00003960typedef struct tagTOOLINFOA_NEW
3961{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003962 UINT cbSize;
3963 UINT uFlags;
3964 HWND hwnd;
3965 UINT_PTR uId;
3966 RECT rect;
3967 HINSTANCE hinst;
3968 LPSTR lpszText;
3969 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003970} TOOLINFO_NEW;
3971
3972typedef struct tagNMTTDISPINFO_NEW
3973{
3974 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003975 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003976 char szText[80];
3977 HINSTANCE hinst;
3978 UINT uFlags;
3979 LPARAM lParam;
3980} NMTTDISPINFO_NEW;
3981
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003982typedef struct tagTOOLINFOW_NEW
3983{
3984 UINT cbSize;
3985 UINT uFlags;
3986 HWND hwnd;
3987 UINT_PTR uId;
3988 RECT rect;
3989 HINSTANCE hinst;
3990 LPWSTR lpszText;
3991 LPARAM lParam;
3992 void *lpReserved;
3993} TOOLINFOW_NEW;
3994
3995typedef struct tagNMTTDISPINFOW_NEW
3996{
3997 NMHDR hdr;
3998 LPWSTR lpszText;
3999 WCHAR szText[80];
4000 HINSTANCE hinst;
4001 UINT uFlags;
4002 LPARAM lParam;
4003} NMTTDISPINFOW_NEW;
4004
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004005
Bram Moolenaar45360022005-07-21 21:08:21 +00004006typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4007#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004008# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009#endif
4010
Bram Moolenaar45360022005-07-21 21:08:21 +00004011#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004012# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004013#endif
4014
4015#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004016# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004017#endif
4018
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004019#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004020
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004021#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4022/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4023 * it here if LPNMTTDISPINFO isn't defined.
4024 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4025 * _MSC_VER. */
4026# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4027typedef struct tagNMTTDISPINFOA {
4028 NMHDR hdr;
4029 LPSTR lpszText;
4030 char szText[80];
4031 HINSTANCE hinst;
4032 UINT uFlags;
4033 LPARAM lParam;
4034} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4035# define LPNMTTDISPINFO LPNMTTDISPINFOA
4036
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004037typedef struct tagNMTTDISPINFOW {
4038 NMHDR hdr;
4039 LPWSTR lpszText;
4040 WCHAR szText[80];
4041 HINSTANCE hinst;
4042 UINT uFlags;
4043 LPARAM lParam;
4044} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004045# endif
4046#endif
4047
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004048#ifndef TTN_GETDISPINFOW
4049# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4050#endif
4051
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052/* Local variables: */
4053
4054#ifdef FEAT_MENU
4055static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058/*
4059 * Use the system font for dialogs and tear-off menus. Remove this line to
4060 * use DLG_FONT_NAME.
4061 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004062#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#define VIM_CLASSW L"Vim"
4066
4067/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4068 * thus there should be room for every dialog. For tearoffs it's made bigger
4069 * when needed. */
4070#define DLG_ALLOC_SIZE 16 * 1024
4071
4072/*
4073 * stuff for dialogs, menus, tearoffs etc.
4074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075static PWORD
4076add_dialog_element(
4077 PWORD p,
4078 DWORD lStyle,
4079 WORD x,
4080 WORD y,
4081 WORD w,
4082 WORD h,
4083 WORD Id,
4084 WORD clss,
4085 const char *caption);
4086static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004087static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004088#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static void get_dialog_font_metrics(void);
4092
4093static int dialog_default_button = -1;
4094
4095/* Intellimouse support */
4096static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4099#ifdef FEAT_TOOLBAR
4100static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004101static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102static int get_toolbar_bitmap(vimmenu_T *menu);
4103#endif
4104
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004105#ifdef FEAT_GUI_TABLINE
4106static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004107static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004108#endif
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#ifdef FEAT_MBYTE_IME
4111static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4112static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4113#endif
4114#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4115# ifdef NOIME
4116typedef struct tagCOMPOSITIONFORM {
4117 DWORD dwStyle;
4118 POINT ptCurrentPos;
4119 RECT rcArea;
4120} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4121typedef HANDLE HIMC;
4122# endif
4123
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004124static HINSTANCE hLibImm = NULL;
4125static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4126static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4127static HIMC (WINAPI *pImmGetContext)(HWND);
4128static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4129static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4130static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4131static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004132static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4133static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004134static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4135static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004136static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137static void dyn_imm_load(void);
4138#else
4139# define pImmGetCompositionStringA ImmGetCompositionStringA
4140# define pImmGetCompositionStringW ImmGetCompositionStringW
4141# define pImmGetContext ImmGetContext
4142# define pImmAssociateContext ImmAssociateContext
4143# define pImmReleaseContext ImmReleaseContext
4144# define pImmGetOpenStatus ImmGetOpenStatus
4145# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004146# define pImmGetCompositionFontW ImmGetCompositionFontW
4147# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148# define pImmSetCompositionWindow ImmSetCompositionWindow
4149# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004150# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151#endif
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#ifdef FEAT_MENU
4154/*
4155 * Figure out how high the menu bar is at the moment.
4156 */
4157 static int
4158gui_mswin_get_menu_height(
4159 int fix_window) /* If TRUE, resize window if menu height changed */
4160{
4161 static int old_menu_height = -1;
4162
4163 RECT rc1, rc2;
4164 int num;
4165 int menu_height;
4166
4167 if (gui.menu_is_active)
4168 num = GetMenuItemCount(s_menuBar);
4169 else
4170 num = 0;
4171
4172 if (num == 0)
4173 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004174 else if (IsMinimized(s_hwnd))
4175 {
4176 /* The height of the menu cannot be determined while the window is
4177 * minimized. Take the previous height if the menu is changed in that
4178 * state, to avoid that Vim's vertical window size accidentally
4179 * increases due to the unaccounted-for menu height. */
4180 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 else
4183 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004184 /*
4185 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4186 * seem to have been set yet, so menu wraps in default window
4187 * width which is very narrow. Instead just return height of a
4188 * single menu item. Will still be wrong when the menu really
4189 * should wrap over more than one line.
4190 */
4191 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4192 if (gui.starting)
4193 menu_height = rc1.bottom - rc1.top + 1;
4194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004196 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4197 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200
4201 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004202 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004203 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 return menu_height;
4206}
4207#endif /*FEAT_MENU*/
4208
4209
4210/*
4211 * Setup for the Intellimouse
4212 */
4213 static void
4214init_mouse_wheel(void)
4215{
4216
4217#ifndef SPI_GETWHEELSCROLLLINES
4218# define SPI_GETWHEELSCROLLLINES 104
4219#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004220#ifndef SPI_SETWHEELSCROLLLINES
4221# define SPI_SETWHEELSCROLLLINES 105
4222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4225#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4226#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4227#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 mouse_scroll_lines = 3; /* reasonable default */
4230
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004231 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4232 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4233 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234}
4235
4236
Bram Moolenaarae20f342019-11-05 21:09:23 +01004237/*
4238 * Intellimouse wheel handler.
4239 * Treat a mouse wheel event as if it were a scroll request.
4240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 static void
4242_OnMouseWheel(
4243 HWND hwnd,
4244 short zDelta)
4245{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int i;
4247 int size;
4248 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004249 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 if (mouse_scroll_lines == 0)
4252 init_mouse_wheel();
4253
Bram Moolenaarae20f342019-11-05 21:09:23 +01004254 wp = gui_mouse_window(FIND_POPUP);
4255
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004256#ifdef FEAT_TEXT_PROP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004257 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004258 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004259 cmdarg_T cap;
4260 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004261
Bram Moolenaarae20f342019-11-05 21:09:23 +01004262 // Mouse hovers over popup window, scroll it if possible.
4263 mouse_row = wp->w_winrow;
4264 mouse_col = wp->w_wincol;
4265 vim_memset(&cap, 0, sizeof(cap));
4266 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4267 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4268 clear_oparg(&oa);
4269 cap.oap = &oa;
4270 nv_mousescroll(&cap);
4271 update_screen(0);
4272 setcursor();
4273 out_flush();
4274 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004275 }
4276#endif
4277
Bram Moolenaarae20f342019-11-05 21:09:23 +01004278 if (wp == NULL || !p_scf)
4279 wp = curwin;
4280
4281 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4282 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4283 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4284 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4285 else
4286 return;
4287 size = wp->w_height;
4288
Bram Moolenaara338adc2018-01-31 20:51:47 +01004289 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (mouse_scroll_lines > 0
4291 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4292 {
4293 for (i = mouse_scroll_lines; i > 0; --i)
4294 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4295 }
4296 else
4297 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004298 mch_enable_flush();
4299 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004302#ifdef USE_SYSMENU_FONT
4303/*
4304 * Get Menu Font.
4305 * Return OK or FAIL.
4306 */
4307 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004308gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004309{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004310 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004312 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4313 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004314 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004315 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004316 &nm,
4317 0))
4318 return FAIL;
4319 *lf = nm.lfMenuFont;
4320 return OK;
4321}
4322#endif
4323
4324
4325#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4326/*
4327 * Set the GUI tabline font to the system menu font
4328 */
4329 static void
4330set_tabline_font(void)
4331{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004332 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004333 HFONT font;
4334 HWND hwnd;
4335 HDC hdc;
4336 HFONT hfntOld;
4337 TEXTMETRIC tm;
4338
4339 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4340 return;
4341
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004342 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004343
4344 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4345
4346 /*
4347 * Compute the height of the font used for the tab text
4348 */
4349 hwnd = GetDesktopWindow();
4350 hdc = GetWindowDC(hwnd);
4351 hfntOld = SelectFont(hdc, font);
4352
4353 GetTextMetrics(hdc, &tm);
4354
4355 SelectFont(hdc, hfntOld);
4356 ReleaseDC(hwnd, hdc);
4357
4358 /*
4359 * The space used by the tab border and the space between the tab label
4360 * and the tab border is included as 7.
4361 */
4362 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4363}
4364#endif
4365
Bram Moolenaar520470a2005-06-16 21:59:56 +00004366/*
4367 * Invoked when a setting was changed.
4368 */
4369 static LRESULT CALLBACK
4370_OnSettingChange(UINT n)
4371{
4372 if (n == SPI_SETWHEELSCROLLLINES)
4373 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4374 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004375#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4376 if (n == SPI_SETNONCLIENTMETRICS)
4377 set_tabline_font();
4378#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004379 return 0;
4380}
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382#ifdef FEAT_NETBEANS_INTG
4383 static void
4384_OnWindowPosChanged(
4385 HWND hwnd,
4386 const LPWINDOWPOS lpwpos)
4387{
4388 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004389 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4392 || lpwpos->cx != cx || lpwpos->cy != cy))
4393 {
4394 x = lpwpos->x;
4395 y = lpwpos->y;
4396 cx = lpwpos->cx;
4397 cy = lpwpos->cy;
4398 netbeans_frame_moved(x, y);
4399 }
4400 /* Allow to send WM_SIZE and WM_MOVE */
4401 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4402}
4403#endif
4404
4405 static int
4406_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 UINT fwSide,
4408 LPRECT lprc)
4409{
4410 int w, h;
4411 int valid_w, valid_h;
4412 int w_offset, h_offset;
4413
4414 w = lprc->right - lprc->left;
4415 h = lprc->bottom - lprc->top;
4416 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4417 w_offset = w - valid_w;
4418 h_offset = h - valid_h;
4419
4420 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4421 || fwSide == WMSZ_BOTTOMLEFT)
4422 lprc->left += w_offset;
4423 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4424 || fwSide == WMSZ_BOTTOMRIGHT)
4425 lprc->right -= w_offset;
4426
4427 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4428 || fwSide == WMSZ_TOPRIGHT)
4429 lprc->top += h_offset;
4430 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4431 || fwSide == WMSZ_BOTTOMRIGHT)
4432 lprc->bottom -= h_offset;
4433 return TRUE;
4434}
4435
4436
4437
4438 static LRESULT CALLBACK
4439_WndProc(
4440 HWND hwnd,
4441 UINT uMsg,
4442 WPARAM wParam,
4443 LPARAM lParam)
4444{
4445 /*
4446 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4447 hwnd, uMsg, wParam, lParam);
4448 */
4449
4450 HandleMouseHide(uMsg, lParam);
4451
4452 s_uMsg = uMsg;
4453 s_wParam = wParam;
4454 s_lParam = lParam;
4455
4456 switch (uMsg)
4457 {
4458 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4459 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4460 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4461 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4462 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4463 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4464 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4465 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4466 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4467#ifdef FEAT_MENU
4468 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4469#endif
4470 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4471 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4472 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4473 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4474 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4475 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4476 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4477 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4478 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4479#ifdef FEAT_NETBEANS_INTG
4480 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4481#endif
4482
Bram Moolenaarafa24992006-03-27 20:58:26 +00004483#ifdef FEAT_GUI_TABLINE
4484 case WM_RBUTTONUP:
4485 {
4486 if (gui_mch_showing_tabline())
4487 {
4488 POINT pt;
4489 RECT rect;
4490
4491 /*
4492 * If the cursor is on the tabline, display the tab menu
4493 */
4494 GetCursorPos((LPPOINT)&pt);
4495 GetWindowRect(s_textArea, &rect);
4496 if (pt.y < rect.top)
4497 {
4498 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004499 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004500 }
4501 }
4502 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4503 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004504 case WM_LBUTTONDBLCLK:
4505 {
4506 /*
4507 * If the user double clicked the tabline, create a new tab
4508 */
4509 if (gui_mch_showing_tabline())
4510 {
4511 POINT pt;
4512 RECT rect;
4513
4514 GetCursorPos((LPPOINT)&pt);
4515 GetWindowRect(s_textArea, &rect);
4516 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004517 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004518 }
4519 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4520 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 case WM_QUERYENDSESSION: /* System wants to go down. */
4524 gui_shell_closed(); /* Will exit when no changed buffers. */
4525 return FALSE; /* Do NOT allow system to go down. */
4526
4527 case WM_ENDSESSION:
4528 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004531 return 0L;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 break;
4534
4535 case WM_CHAR:
4536 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4537 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004538 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 return 0L;
4540
4541 case WM_SYSCHAR:
4542 /*
4543 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4544 * shortcut key, handle like a typed ALT key, otherwise call Windows
4545 * ALT key handling.
4546 */
4547#ifdef FEAT_MENU
4548 if ( !gui.menu_is_active
4549 || p_wak[0] == 'n'
4550 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4551 )
4552#endif
4553 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004554 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 return 0L;
4556 }
4557#ifdef FEAT_MENU
4558 else
4559 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4560#endif
4561
4562 case WM_SYSKEYUP:
4563#ifdef FEAT_MENU
4564 /* This used to be done only when menu is active: ALT key is used for
4565 * that. But that caused problems when menu is disabled and using
4566 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4567 * are received, mouse pointer remains hidden. */
4568 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4569#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004570 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571#endif
4572
4573 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004574 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 case WM_MOUSEWHEEL:
4577 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004578 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaar520470a2005-06-16 21:59:56 +00004580 /* Notification for change in SystemParametersInfo() */
4581 case WM_SETTINGCHANGE:
4582 return _OnSettingChange((UINT)wParam);
4583
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004584#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 case WM_NOTIFY:
4586 switch (((LPNMHDR) lParam)->code)
4587 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004588 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004589 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004591 LPNMHDR hdr = (LPNMHDR)lParam;
4592 char_u *str = NULL;
4593 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004594
Bram Moolenaard23a8232018-02-10 18:45:26 +01004595 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004596
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004597# ifdef FEAT_GUI_TABLINE
4598 if (gui_mch_showing_tabline()
4599 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004600 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004601 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004602 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004603 * Mouse is over the GUI tabline. Display the
4604 * tooltip for the tab under the cursor
4605 *
4606 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004607 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004608 GetCursorPos(&pt);
4609 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004610 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004611 TCHITTESTINFO htinfo;
4612 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004613
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004614 /*
4615 * Get the tab under the cursor
4616 */
4617 htinfo.pt.x = pt.x;
4618 htinfo.pt.y = pt.y;
4619 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4620 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004621 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004622 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004623
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004624 tp = find_tabpage(idx + 1);
4625 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004626 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004627 get_tabline_label(tp, TRUE);
4628 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004629 }
4630 }
4631 }
4632 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004633# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004634# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004635# ifdef FEAT_GUI_TABLINE
4636 else
4637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004639 UINT idButton;
4640 vimmenu_T *pMenu;
4641
4642 idButton = (UINT) hdr->idFrom;
4643 pMenu = gui_mswin_find_menu(root_menu, idButton);
4644 if (pMenu)
4645 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004647# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004648 if (str != NULL)
4649 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 if (hdr->code == TTN_GETDISPINFOW)
4651 {
4652 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4653
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004654 /* Set the maximum width, this also enables using
4655 * \n for line break. */
4656 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4657 0, 500);
4658
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004659 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004660 lpdi->lpszText = tt_text;
4661 /* can't show tooltip if failed */
4662 }
4663 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004664 {
4665 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4666
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004667 /* Set the maximum width, this also enables using
4668 * \n for line break. */
4669 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4670 0, 500);
4671
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004672 if (STRLEN(str) < sizeof(lpdi->szText)
4673 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004674 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004675 sizeof(lpdi->szText) - 1);
4676 else
4677 lpdi->lpszText = tt_text;
4678 }
4679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004682# ifdef FEAT_GUI_TABLINE
4683 case TCN_SELCHANGE:
4684 if (gui_mch_showing_tabline()
4685 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004686 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004687 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004688 return 0L;
4689 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004690 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004691
4692 case NM_RCLICK:
4693 if (gui_mch_showing_tabline()
4694 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004695 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004696 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004697 return 0L;
4698 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004699 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004702# ifdef FEAT_GUI_TABLINE
4703 if (gui_mch_showing_tabline()
4704 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4705 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4706# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708 }
4709 break;
4710#endif
4711#if defined(MENUHINTS) && defined(FEAT_MENU)
4712 case WM_MENUSELECT:
4713 if (((UINT) HIWORD(wParam)
4714 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4715 == MF_HILITE
4716 && (State & CMDLINE) == 0)
4717 {
4718 UINT idButton;
4719 vimmenu_T *pMenu;
4720 static int did_menu_tip = FALSE;
4721
4722 if (did_menu_tip)
4723 {
4724 msg_clr_cmdline();
4725 setcursor();
4726 out_flush();
4727 did_menu_tip = FALSE;
4728 }
4729
4730 idButton = (UINT)LOWORD(wParam);
4731 pMenu = gui_mswin_find_menu(root_menu, idButton);
4732 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4733 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4734 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004735 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004736 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004737 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 setcursor();
4739 out_flush();
4740 did_menu_tip = TRUE;
4741 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004742 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744 break;
4745#endif
4746 case WM_NCHITTEST:
4747 {
4748 LRESULT result;
4749 int x, y;
4750 int xPos = GET_X_LPARAM(lParam);
4751
4752 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4753 if (result == HTCLIENT)
4754 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004755#ifdef FEAT_GUI_TABLINE
4756 if (gui_mch_showing_tabline())
4757 {
4758 int yPos = GET_Y_LPARAM(lParam);
4759 RECT rct;
4760
4761 /* If the cursor is on the GUI tabline, don't process this
4762 * event */
4763 GetWindowRect(s_textArea, &rct);
4764 if (yPos < rct.top)
4765 return result;
4766 }
4767#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004768 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 xPos -= x;
4770
4771 if (xPos < 48) /* <VN> TODO should use system metric? */
4772 return HTBOTTOMLEFT;
4773 else
4774 return HTBOTTOMRIGHT;
4775 }
4776 else
4777 return result;
4778 }
4779 /* break; notreached */
4780
4781#ifdef FEAT_MBYTE_IME
4782 case WM_IME_NOTIFY:
4783 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4784 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004785 return 1L;
4786
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 case WM_IME_COMPOSITION:
4788 if (!_OnImeComposition(hwnd, wParam, lParam))
4789 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004790 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791#endif
4792
4793 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004795 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797#endif
4798 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4799 }
4800
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004801 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802}
4803
4804/*
4805 * End of call-back routines
4806 */
4807
4808/* parent window, if specified with -P */
4809HWND vim_parent_hwnd = NULL;
4810
4811 static BOOL CALLBACK
4812FindWindowTitle(HWND hwnd, LPARAM lParam)
4813{
4814 char buf[2048];
4815 char *title = (char *)lParam;
4816
4817 if (GetWindowText(hwnd, buf, sizeof(buf)))
4818 {
4819 if (strstr(buf, title) != NULL)
4820 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004821 /* Found it. Store the window ref. and quit searching if MDI
4822 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004824 if (vim_parent_hwnd != NULL)
4825 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 }
4828 return TRUE; /* continue searching */
4829}
4830
4831/*
4832 * Invoked for '-P "title"' argument: search for parent application to open
4833 * our window in.
4834 */
4835 void
4836gui_mch_set_parent(char *title)
4837{
4838 EnumWindows(FindWindowTitle, (LPARAM)title);
4839 if (vim_parent_hwnd == NULL)
4840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004841 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 mch_exit(2);
4843 }
4844}
4845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 static void
4848ole_error(char *arg)
4849{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004850 char buf[IOSIZE];
4851
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004852# ifdef VIMDLL
4853 gui.in_use = mch_is_gui_executable();
4854# endif
4855
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004856 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004857 vim_snprintf(buf, IOSIZE,
4858 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4859 arg);
4860 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004864#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4865 static char *
4866gvim_error(void)
4867{
4868 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4869
4870 if (starting)
4871 {
4872 mch_errmsg(msg);
4873 mch_errmsg("\n");
4874 mch_exit(2);
4875 }
4876 return msg;
4877}
4878
4879 char *
4880gui_mch_do_spawn(char_u *arg)
4881{
4882 int len;
4883# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4884 char_u *session = NULL;
4885 LPWSTR tofree1 = NULL;
4886# endif
4887 WCHAR name[MAX_PATH];
4888 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4889 STARTUPINFOW si = {sizeof(si)};
4890 PROCESS_INFORMATION pi;
4891
4892 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4893 goto error;
4894 p = wcsrchr(name, L'\\');
4895 if (p == NULL)
4896 goto error;
4897 // Replace the executable name from vim(d).exe to gvim(d).exe.
4898# ifdef DEBUG
4899 wcscpy(p + 1, L"gvimd.exe");
4900# else
4901 wcscpy(p + 1, L"gvim.exe");
4902# endif
4903
4904# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4905 if (starting)
4906# endif
4907 {
4908 // Pass the command line to the new process.
4909 p = GetCommandLineW();
4910 // Skip 1st argument.
4911 while (*p && *p != L' ' && *p != L'\t')
4912 {
4913 if (*p == L'"')
4914 {
4915 while (*p && *p != L'"')
4916 ++p;
4917 if (*p)
4918 ++p;
4919 }
4920 else
4921 ++p;
4922 }
4923 cmd = p;
4924 }
4925# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4926 else
4927 {
4928 // Create a session file and pass it to the new process.
4929 LPWSTR wsession;
4930 char_u *savebg;
4931 int ret;
4932
4933 session = vim_tempname('s', FALSE);
4934 if (session == NULL)
4935 goto error;
4936 savebg = p_bg;
4937 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4938 ret = write_session_file(session);
4939 vim_free(p_bg);
4940 p_bg = savebg;
4941 if (!ret)
4942 goto error;
4943 wsession = enc_to_utf16(session, NULL);
4944 if (wsession == NULL)
4945 goto error;
4946 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004947 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004948 if (cmd == NULL)
4949 {
4950 vim_free(wsession);
4951 goto error;
4952 }
4953 tofree1 = cmd;
4954 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4955 wsession, wsession);
4956 vim_free(wsession);
4957 }
4958# endif
4959
4960 // Check additional arguments to the `:gui` command.
4961 if (arg != NULL)
4962 {
4963 warg = enc_to_utf16(arg, NULL);
4964 if (warg == NULL)
4965 goto error;
4966 tofree2 = warg;
4967 }
4968 else
4969 warg = L"";
4970
4971 // Set up the new command line.
4972 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004973 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004974 if (newcmd == NULL)
4975 goto error;
4976 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4977
4978 // Spawn a new GUI process.
4979 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4980 NULL, NULL, &si, &pi))
4981 goto error;
4982 CloseHandle(pi.hProcess);
4983 CloseHandle(pi.hThread);
4984 mch_exit(0);
4985
4986error:
4987# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4988 if (session)
4989 mch_remove(session);
4990 vim_free(session);
4991 vim_free(tofree1);
4992# endif
4993 vim_free(newcmd);
4994 vim_free(tofree2);
4995 return gvim_error();
4996}
4997#endif
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999/*
5000 * Parse the GUI related command-line arguments. Any arguments used are
5001 * deleted from argv, and *argc is decremented accordingly. This is called
5002 * when vim is started, whether or not the GUI has been started.
5003 */
5004 void
5005gui_mch_prepare(int *argc, char **argv)
5006{
5007 int silent = FALSE;
5008 int idx;
5009
5010 /* Check for special OLE command line parameters */
5011 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5012 {
5013 /* Check for a "-silent" argument first. */
5014 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5015 && (argv[2][0] == '-' || argv[2][0] == '/'))
5016 {
5017 silent = TRUE;
5018 idx = 2;
5019 }
5020 else
5021 idx = 1;
5022
5023 /* Register Vim as an OLE Automation server */
5024 if (STRICMP(argv[idx] + 1, "register") == 0)
5025 {
5026#ifdef FEAT_OLE
5027 RegisterMe(silent);
5028 mch_exit(0);
5029#else
5030 if (!silent)
5031 ole_error("register");
5032 mch_exit(2);
5033#endif
5034 }
5035
5036 /* Unregister Vim as an OLE Automation server */
5037 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5038 {
5039#ifdef FEAT_OLE
5040 UnregisterMe(!silent);
5041 mch_exit(0);
5042#else
5043 if (!silent)
5044 ole_error("unregister");
5045 mch_exit(2);
5046#endif
5047 }
5048
5049 /* Ignore an -embedding argument. It is only relevant if the
5050 * application wants to treat the case when it is started manually
5051 * differently from the case where it is started via automation (and
5052 * we don't).
5053 */
5054 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5055 {
5056#ifdef FEAT_OLE
5057 *argc = 1;
5058#else
5059 ole_error("embedding");
5060 mch_exit(2);
5061#endif
5062 }
5063 }
5064
5065#ifdef FEAT_OLE
5066 {
5067 int bDoRestart = FALSE;
5068
5069 InitOLE(&bDoRestart);
5070 /* automatically exit after registering */
5071 if (bDoRestart)
5072 mch_exit(0);
5073 }
5074#endif
5075
5076#ifdef FEAT_NETBEANS_INTG
5077 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005078 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 int arg;
5080
5081 for (arg = 1; arg < *argc; arg++)
5082 if (strncmp("-nb", argv[arg], 3) == 0)
5083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 netbeansArg = argv[arg];
5085 mch_memmove(&argv[arg], &argv[arg + 1],
5086 (--*argc - arg) * sizeof(char *));
5087 argv[*argc] = NULL;
5088 break; /* enough? */
5089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092}
5093
5094/*
5095 * Initialise the GUI. Create all the windows, set up all the call-backs
5096 * etc.
5097 */
5098 int
5099gui_mch_init(void)
5100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005102 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#ifdef GLOBAL_IME
5105 ATOM atom;
5106#endif
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 /* Return here if the window was already opened (happens when
5109 * gui_mch_dialog() is called early). */
5110 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005111 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 /*
5114 * Load the tearoff bitmap
5115 */
5116#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005117 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#endif
5119
5120 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5121 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5122#ifdef FEAT_MENU
5123 gui.menu_height = 0; /* Windows takes care of this */
5124#endif
5125 gui.border_width = 0;
5126
5127 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 /* First try using the wide version, so that we can use any title.
5130 * Otherwise only characters in the active codepage will work. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005131 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005133 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 wndclassw.lpfnWndProc = _WndProc;
5135 wndclassw.cbClsExtra = 0;
5136 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005137 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5139 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5140 wndclassw.hbrBackground = s_brush;
5141 wndclassw.lpszMenuName = NULL;
5142 wndclassw.lpszClassName = szVimWndClassW;
5143
5144 if ((
5145#ifdef GLOBAL_IME
5146 atom =
5147#endif
5148 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005149 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (vim_parent_hwnd != NULL)
5153 {
5154#ifdef HAVE_TRY_EXCEPT
5155 __try
5156 {
5157#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005158 // Open inside the specified parent window.
5159 // TODO: last argument should point to a CLIENTCREATESTRUCT
5160 // structure.
5161 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005163 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005164 WS_OVERLAPPEDWINDOW | WS_CHILD
5165 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5167 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005168 100, // Any value will do
5169 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005171 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#ifdef HAVE_TRY_EXCEPT
5173 }
5174 __except(EXCEPTION_EXECUTE_HANDLER)
5175 {
5176 /* NOP */
5177 }
5178#endif
5179 if (s_hwnd == NULL)
5180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005181 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 mch_exit(2);
5183 }
5184 }
5185 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005186 {
5187 /* If the provided windowid is not valid reset it to zero, so that it
5188 * is ignored and we open our own window. */
5189 if (IsWindow((HWND)win_socket_id) <= 0)
5190 win_socket_id = 0;
5191
5192 /* Create a window. If win_socket_id is not zero without border and
5193 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005194 s_hwnd = CreateWindowW(
5195 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005196 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5197 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005198 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5199 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5200 100, /* Any value will do */
5201 100, /* Any value will do */
5202 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005203 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005204 if (s_hwnd != NULL && win_socket_id != 0)
5205 {
5206 SetParent(s_hwnd, (HWND)win_socket_id);
5207 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5208 }
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 if (s_hwnd == NULL)
5212 return FAIL;
5213
5214#ifdef GLOBAL_IME
5215 global_ime_init(atom, s_hwnd);
5216#endif
5217#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5218 dyn_imm_load();
5219#endif
5220
5221 /* Create the text area window */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005222 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005223 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005224 wndclassw.style = CS_OWNDC;
5225 wndclassw.lpfnWndProc = _TextAreaWndProc;
5226 wndclassw.cbClsExtra = 0;
5227 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005228 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005229 wndclassw.hIcon = NULL;
5230 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5231 wndclassw.hbrBackground = NULL;
5232 wndclassw.lpszMenuName = NULL;
5233 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005234
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005235 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 return FAIL;
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005239 s_textArea = CreateWindowExW(
5240 0,
5241 szTextAreaClassW, L"Vim text area",
5242 WS_CHILD | WS_VISIBLE, 0, 0,
5243 100, // Any value will do for now
5244 100, // Any value will do for now
5245 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005246 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (s_textArea == NULL)
5249 return FAIL;
5250
Bram Moolenaar20321902016-02-17 12:30:17 +01005251#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005252 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5253 {
5254 HANDLE hIcon = NULL;
5255
5256 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005257 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005258 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005259#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261#ifdef FEAT_MENU
5262 s_menuBar = CreateMenu();
5263#endif
5264 s_hdc = GetDC(s_textArea);
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267
5268 /* Do we need to bother with this? */
5269 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5270
5271 /* Get background/foreground colors from the system */
5272 gui_mch_def_colors();
5273
5274 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5275 * file) */
5276 set_normal_colors();
5277
5278 /*
5279 * Check that none of the colors are the same as the background color.
5280 * Then store the current values as the defaults.
5281 */
5282 gui_check_colors();
5283 gui.def_norm_pixel = gui.norm_pixel;
5284 gui.def_back_pixel = gui.back_pixel;
5285
5286 /* Get the colors for the highlight groups (gui_check_colors() might have
5287 * changed them) */
5288 highlight_gui_started();
5289
5290 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005291 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005293 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /*
5296 * Set up for Intellimouse processing
5297 */
5298 init_mouse_wheel();
5299
5300 /*
5301 * compute a couple of metrics used for the dialogs
5302 */
5303 get_dialog_font_metrics();
5304#ifdef FEAT_TOOLBAR
5305 /*
5306 * Create the toolbar
5307 */
5308 initialise_toolbar();
5309#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005310#ifdef FEAT_GUI_TABLINE
5311 /*
5312 * Create the tabline
5313 */
5314 initialise_tabline();
5315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316#ifdef MSWIN_FIND_REPLACE
5317 /*
5318 * Initialise the dialog box stuff
5319 */
5320 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5321
5322 /* Initialise the struct */
5323 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005324 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005326 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5328 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5329 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005332#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005333# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5334/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5335# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005336# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005337# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005338# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005339 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005340 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005341#endif
5342
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005343#ifdef FEAT_RENDER_OPTIONS
5344 if (p_rop)
5345 (void)gui_mch_set_rendering_options(p_rop);
5346#endif
5347
Bram Moolenaar748bf032005-02-02 23:04:36 +00005348theend:
5349 /* Display any pending error messages */
5350 display_errors();
5351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 return OK;
5353}
5354
5355/*
5356 * Get the size of the screen, taking position on multiple monitors into
5357 * account (if supported).
5358 */
5359 static void
5360get_work_area(RECT *spi_rect)
5361{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005362 HMONITOR mon;
5363 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005365 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005366 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005367 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005369 moninfo.cbSize = sizeof(MONITORINFO);
5370 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005372 *spi_rect = moninfo.rcWork;
5373 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375 }
5376 /* this is the old method... */
5377 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5378}
5379
5380/*
5381 * Set the size of the window to the given width and height in pixels.
5382 */
5383 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005384gui_mch_set_shellsize(
5385 int width,
5386 int height,
5387 int min_width UNUSED,
5388 int min_height UNUSED,
5389 int base_width UNUSED,
5390 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005391 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 RECT workarea_rect;
5394 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 WINDOWPLACEMENT wndpl;
5396
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005397 /* Try to keep window completely on screen. */
5398 /* Get position of the screen work area. This is the part that is not
5399 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 get_work_area(&workarea_rect);
5401
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005402 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005403 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 wndpl.length = sizeof(WINDOWPLACEMENT);
5405 GetWindowPlacement(s_hwnd, &wndpl);
5406
5407 /* Resizing a maximized window looks very strange, unzoom it first.
5408 * But don't do it when still starting up, it may have been requested in
5409 * the shortcut. */
5410 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5411 {
5412 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5413 /* Need to get the settings of the normal window. */
5414 GetWindowPlacement(s_hwnd, &wndpl);
5415 }
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005418 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005419 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005420 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005421 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 + GetSystemMetrics(SM_CYCAPTION)
5423#ifdef FEAT_MENU
5424 + gui_mswin_get_menu_height(FALSE)
5425#endif
5426 ;
5427
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005428 /* The following should take care of keeping Vim on the same monitor, no
5429 * matter if the secondary monitor is left or right of the primary
5430 * monitor. */
5431 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5432 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005433
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005434 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005435 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005436 && wndpl.rcNormalPosition.right > workarea_rect.right)
5437 OffsetRect(&wndpl.rcNormalPosition,
5438 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005440 if ((direction & RESIZE_HOR)
5441 && wndpl.rcNormalPosition.left < workarea_rect.left)
5442 OffsetRect(&wndpl.rcNormalPosition,
5443 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaarafa24992006-03-27 20:58:26 +00005445 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005446 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5447 OffsetRect(&wndpl.rcNormalPosition,
5448 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005450 if ((direction & RESIZE_VERT)
5451 && wndpl.rcNormalPosition.top < workarea_rect.top)
5452 OffsetRect(&wndpl.rcNormalPosition,
5453 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
5455 /* set window position - we should use SetWindowPlacement rather than
5456 * SetWindowPos as the MSDN docs say the coord systems returned by
5457 * these two are not compatible. */
5458 SetWindowPlacement(s_hwnd, &wndpl);
5459
5460 SetActiveWindow(s_hwnd);
5461 SetFocus(s_hwnd);
5462
5463#ifdef FEAT_MENU
5464 /* Menu may wrap differently now */
5465 gui_mswin_get_menu_height(!gui.starting);
5466#endif
5467}
5468
5469
5470 void
5471gui_mch_set_scrollbar_thumb(
5472 scrollbar_T *sb,
5473 long val,
5474 long size,
5475 long max)
5476{
5477 SCROLLINFO info;
5478
5479 sb->scroll_shift = 0;
5480 while (max > 32767)
5481 {
5482 max = (max + 1) >> 1;
5483 val >>= 1;
5484 size >>= 1;
5485 ++sb->scroll_shift;
5486 }
5487
5488 if (sb->scroll_shift > 0)
5489 ++size;
5490
5491 info.cbSize = sizeof(info);
5492 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5493 info.nPos = val;
5494 info.nMin = 0;
5495 info.nMax = max;
5496 info.nPage = size;
5497 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5498}
5499
5500
5501/*
5502 * Set the current text font.
5503 */
5504 void
5505gui_mch_set_font(GuiFont font)
5506{
5507 gui.currFont = font;
5508}
5509
5510
5511/*
5512 * Set the current text foreground color.
5513 */
5514 void
5515gui_mch_set_fg_color(guicolor_T color)
5516{
5517 gui.currFgColor = color;
5518}
5519
5520/*
5521 * Set the current text background color.
5522 */
5523 void
5524gui_mch_set_bg_color(guicolor_T color)
5525{
5526 gui.currBgColor = color;
5527}
5528
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005529/*
5530 * Set the current text special color.
5531 */
5532 void
5533gui_mch_set_sp_color(guicolor_T color)
5534{
5535 gui.currSpColor = color;
5536}
5537
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005538#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539/*
5540 * Multi-byte handling, originally by Sung-Hoon Baek.
5541 * First static functions (no prototypes generated).
5542 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005543# ifdef _MSC_VER
Bram Moolenaareae1b912019-05-09 15:12:55 +02005544# include <ime.h> /* Apparently not needed for Cygwin or MinGW. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005545# endif
5546# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
5548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 * handle WM_IME_NOTIFY message
5550 */
5551 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005552_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 LRESULT lResult = 0;
5555 HIMC hImc;
5556
5557 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5558 return lResult;
5559 switch (dwCommand)
5560 {
5561 case IMN_SETOPENSTATUS:
5562 if (pImmGetOpenStatus(hImc))
5563 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005564 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 im_set_position(gui.row, gui.col);
5566
5567 /* Disable langmap */
5568 State &= ~LANGMAP;
5569 if (State & INSERT)
5570 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005571#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /* Unshown 'keymap' in status lines */
5573 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5574 {
5575 /* Save cursor position */
5576 int old_row = gui.row;
5577 int old_col = gui.col;
5578
5579 // This must be called here before
5580 // status_redraw_curbuf(), otherwise the mode
5581 // message may appear in the wrong position.
5582 showmode();
5583 status_redraw_curbuf();
5584 update_screen(0);
5585 /* Restore cursor position */
5586 gui.row = old_row;
5587 gui.col = old_col;
5588 }
5589#endif
5590 }
5591 }
5592 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005593 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 lResult = 0;
5595 break;
5596 }
5597 pImmReleaseContext(hWnd, hImc);
5598 return lResult;
5599}
5600
5601 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005602_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603{
5604 char_u *ret;
5605 int len;
5606
5607 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5608 return 0;
5609
5610 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5611 if (ret != NULL)
5612 {
5613 add_to_input_buf_csi(ret, len);
5614 vim_free(ret);
5615 return 1;
5616 }
5617 return 0;
5618}
5619
5620/*
5621 * get the current composition string, in UCS-2; *lenp is the number of
5622 * *lenp is the number of Unicode characters.
5623 */
5624 static short_u *
5625GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5626{
5627 LONG ret;
5628 LPWSTR wbuf = NULL;
5629 char_u *buf;
5630
5631 if (!pImmGetContext)
5632 return NULL; /* no imm32.dll */
5633
5634 /* Try Unicode; this'll always work on NT regardless of codepage. */
5635 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5636 if (ret == 0)
5637 return NULL; /* empty */
5638
5639 if (ret > 0)
5640 {
5641 /* Allocate the requested buffer plus space for the NUL character. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005642 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 if (wbuf != NULL)
5644 {
5645 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5646 *lenp = ret / sizeof(WCHAR);
5647 }
5648 return (short_u *)wbuf;
5649 }
5650
5651 /* ret < 0; we got an error, so try the ANSI version. This'll work
5652 * on 9x/ME, but only if the codepage happens to be set to whatever
5653 * we're inputting. */
5654 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5655 if (ret <= 0)
5656 return NULL; /* empty or error */
5657
5658 buf = alloc(ret);
5659 if (buf == NULL)
5660 return NULL;
5661 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5662
5663 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005664 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 vim_free(buf);
5666
5667 return (short_u *)wbuf;
5668}
5669
5670/*
5671 * void GetResultStr()
5672 *
5673 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5674 * get complete composition string
5675 */
5676 static char_u *
5677GetResultStr(HWND hwnd, int GCS, int *lenp)
5678{
5679 HIMC hIMC; /* Input context handle. */
5680 short_u *buf = NULL;
5681 char_u *convbuf = NULL;
5682
5683 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5684 return NULL;
5685
5686 /* Reads in the composition string. */
5687 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5688 if (buf == NULL)
5689 return NULL;
5690
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005691 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 pImmReleaseContext(hwnd, hIMC);
5693 vim_free(buf);
5694 return convbuf;
5695}
5696#endif
5697
5698/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005699#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701/*
5702 * set font to IM.
5703 */
5704 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005705im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706{
5707 HIMC hImc;
5708
5709 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5710 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005711 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 pImmReleaseContext(s_hwnd, hImc);
5713 }
5714}
5715
5716/*
5717 * Notify cursor position to IM.
5718 */
5719 void
5720im_set_position(int row, int col)
5721{
5722 HIMC hImc;
5723
5724 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5725 {
5726 COMPOSITIONFORM cfs;
5727
5728 cfs.dwStyle = CFS_POINT;
5729 cfs.ptCurrentPos.x = FILL_X(col);
5730 cfs.ptCurrentPos.y = FILL_Y(row);
5731 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5732 pImmSetCompositionWindow(hImc, &cfs);
5733
5734 pImmReleaseContext(s_hwnd, hImc);
5735 }
5736}
5737
5738/*
5739 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5740 */
5741 void
5742im_set_active(int active)
5743{
5744 HIMC hImc;
5745 static HIMC hImcOld = (HIMC)0;
5746
5747 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5748 {
5749 if (p_imdisable)
5750 {
5751 if (hImcOld == (HIMC)0)
5752 {
5753 hImcOld = pImmGetContext(s_hwnd);
5754 if (hImcOld)
5755 pImmAssociateContext(s_hwnd, (HIMC)0);
5756 }
5757 active = FALSE;
5758 }
5759 else if (hImcOld != (HIMC)0)
5760 {
5761 pImmAssociateContext(s_hwnd, hImcOld);
5762 hImcOld = (HIMC)0;
5763 }
5764
5765 hImc = pImmGetContext(s_hwnd);
5766 if (hImc)
5767 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005768 /*
5769 * for Korean ime
5770 */
5771 HKL hKL = GetKeyboardLayout(0);
5772
5773 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5774 {
5775 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5776 static BOOL bSaved = FALSE;
5777
5778 if (active)
5779 {
5780 /* if we have a saved conversion status, restore it */
5781 if (bSaved)
5782 pImmSetConversionStatus(hImc, dwConversionSaved,
5783 dwSentenceSaved);
5784 bSaved = FALSE;
5785 }
5786 else
5787 {
5788 /* save conversion status and disable korean */
5789 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5790 &dwSentenceSaved))
5791 {
5792 bSaved = TRUE;
5793 pImmSetConversionStatus(hImc,
5794 dwConversionSaved & ~(IME_CMODE_NATIVE
5795 | IME_CMODE_FULLSHAPE),
5796 dwSentenceSaved);
5797 }
5798 }
5799 }
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 pImmSetOpenStatus(hImc, active);
5802 pImmReleaseContext(s_hwnd, hImc);
5803 }
5804 }
5805}
5806
5807/*
5808 * Get IM status. When IM is on, return not 0. Else return 0.
5809 */
5810 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005811im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 int status = 0;
5814 HIMC hImc;
5815
5816 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5817 {
5818 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5819 pImmReleaseContext(s_hwnd, hImc);
5820 }
5821 return status;
5822}
5823
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005824#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005826#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827/* Win32 with GLOBAL IME */
5828
5829/*
5830 * Notify cursor position to IM.
5831 */
5832 void
5833im_set_position(int row, int col)
5834{
5835 /* Win32 with GLOBAL IME */
5836 POINT p;
5837
5838 p.x = FILL_X(col);
5839 p.y = FILL_Y(row);
5840 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5841 global_ime_set_position(&p);
5842}
5843
5844/*
5845 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5846 */
5847 void
5848im_set_active(int active)
5849{
5850 global_ime_set_status(active);
5851}
5852
5853/*
5854 * Get IM status. When IM is on, return not 0. Else return 0.
5855 */
5856 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005857im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 return global_ime_get_status();
5860}
5861#endif
5862
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005863/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005864 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005865 */
5866 static void
5867latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5868{
5869 int c;
5870
Bram Moolenaarca003e12006-03-17 23:19:38 +00005871 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005872 {
5873 c = *text++;
5874 switch (c)
5875 {
5876 case 0xa4: c = 0x20ac; break; /* euro */
5877 case 0xa6: c = 0x0160; break; /* S hat */
5878 case 0xa8: c = 0x0161; break; /* S -hat */
5879 case 0xb4: c = 0x017d; break; /* Z hat */
5880 case 0xb8: c = 0x017e; break; /* Z -hat */
5881 case 0xbc: c = 0x0152; break; /* OE */
5882 case 0xbd: c = 0x0153; break; /* oe */
5883 case 0xbe: c = 0x0178; break; /* Y */
5884 }
5885 *unicodebuf++ = c;
5886 }
5887}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
5889#ifdef FEAT_RIGHTLEFT
5890/*
5891 * What is this for? In the case where you are using Win98 or Win2K or later,
5892 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5893 * reverses the string sent to the TextOut... family. This sucks, because we
5894 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5895 * way to tell Windblows not to do this!
5896 *
5897 * The short of it is that this 'RevOut' only gets called if you are running
5898 * one of the new, "improved" MS OSes, and only if you are running in
5899 * 'rightleft' mode. It makes display take *slightly* longer, but not
5900 * noticeably so.
5901 */
5902 static void
5903RevOut( HDC s_hdc,
5904 int col,
5905 int row,
5906 UINT foptions,
5907 CONST RECT *pcliprect,
5908 LPCTSTR text,
5909 UINT len,
5910 CONST INT *padding)
5911{
5912 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005914 for (ix = 0; ix < (int)len; ++ix)
5915 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5916 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917}
5918#endif
5919
Bram Moolenaar92467d32017-12-05 13:22:16 +01005920 static void
5921draw_line(
5922 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005923 int y1,
5924 int x2,
5925 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005926 COLORREF color)
5927{
5928#if defined(FEAT_DIRECTX)
5929 if (IS_ENABLE_DIRECTX())
5930 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5931 else
5932#endif
5933 {
5934 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5935 HPEN old_pen = SelectObject(s_hdc, hpen);
5936 MoveToEx(s_hdc, x1, y1, NULL);
5937 /* Note: LineTo() excludes the last pixel in the line. */
5938 LineTo(s_hdc, x2, y2);
5939 DeleteObject(SelectObject(s_hdc, old_pen));
5940 }
5941}
5942
5943 static void
5944set_pixel(
5945 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005946 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005947 COLORREF color)
5948{
5949#if defined(FEAT_DIRECTX)
5950 if (IS_ENABLE_DIRECTX())
5951 DWriteContext_SetPixel(s_dwc, x, y, color);
5952 else
5953#endif
5954 SetPixel(s_hdc, x, y, color);
5955}
5956
5957 static void
5958fill_rect(
5959 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005960 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005961 COLORREF color)
5962{
5963#if defined(FEAT_DIRECTX)
5964 if (IS_ENABLE_DIRECTX())
5965 DWriteContext_FillRect(s_dwc, rcp, color);
5966 else
5967#endif
5968 {
5969 HBRUSH hbr2;
5970
5971 if (hbr == NULL)
5972 hbr2 = CreateSolidBrush(color);
5973 else
5974 hbr2 = hbr;
5975 FillRect(s_hdc, rcp, hbr2);
5976 if (hbr == NULL)
5977 DeleteBrush(hbr2);
5978 }
5979}
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 void
5982gui_mch_draw_string(
5983 int row,
5984 int col,
5985 char_u *text,
5986 int len,
5987 int flags)
5988{
5989 static int *padding = NULL;
5990 static int pad_size = 0;
5991 int i;
5992 const RECT *pcliprect = NULL;
5993 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 static WCHAR *unicodebuf = NULL;
5995 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005996 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 int y;
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 /*
6001 * Italic and bold text seems to have an extra row of pixels at the bottom
6002 * (below where the bottom of the character should be). If we draw the
6003 * characters with a solid background, the top row of pixels in the
6004 * character below will be overwritten. We can fix this by filling in the
6005 * background ourselves, to the correct character proportions, and then
6006 * writing the character in transparent mode. Still have a problem when
6007 * the character is "_", which gets written on to the character below.
6008 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6009 * pixel in their slots, which fixes the problem with the bottom row of
6010 * pixels. We still need this code because otherwise the top row of pixels
6011 * becomes a problem. - webb.
6012 */
6013 static HBRUSH hbr_cache[2] = {NULL, NULL};
6014 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6015 static int brush_lru = 0;
6016 HBRUSH hbr;
6017 RECT rc;
6018
6019 if (!(flags & DRAW_TRANSP))
6020 {
6021 /*
6022 * Clear background first.
6023 * Note: FillRect() excludes right and bottom of rectangle.
6024 */
6025 rc.left = FILL_X(col);
6026 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 if (has_mbyte)
6028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006030 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 rc.right = FILL_X(col + len);
6034 rc.bottom = FILL_Y(row + 1);
6035
6036 /* Cache the created brush, that saves a lot of time. We need two:
6037 * one for cursor background and one for the normal background. */
6038 if (gui.currBgColor == brush_color[0])
6039 {
6040 hbr = hbr_cache[0];
6041 brush_lru = 1;
6042 }
6043 else if (gui.currBgColor == brush_color[1])
6044 {
6045 hbr = hbr_cache[1];
6046 brush_lru = 0;
6047 }
6048 else
6049 {
6050 if (hbr_cache[brush_lru] != NULL)
6051 DeleteBrush(hbr_cache[brush_lru]);
6052 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6053 brush_color[brush_lru] = gui.currBgColor;
6054 hbr = hbr_cache[brush_lru];
6055 brush_lru = !brush_lru;
6056 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006057
Bram Moolenaar92467d32017-12-05 13:22:16 +01006058 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 SetBkMode(s_hdc, TRANSPARENT);
6061
6062 /*
6063 * When drawing block cursor, prevent inverted character spilling
6064 * over character cell (can happen with bold/italic)
6065 */
6066 if (flags & DRAW_CURSOR)
6067 {
6068 pcliprect = &rc;
6069 foptions = ETO_CLIPPED;
6070 }
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 SetTextColor(s_hdc, gui.currFgColor);
6073 SelectFont(s_hdc, gui.currFont);
6074
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006075#ifdef FEAT_DIRECTX
6076 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006077 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006078#endif
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6081 {
6082 vim_free(padding);
6083 pad_size = Columns;
6084
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006085 /* Don't give an out-of-memory message here, it would call us
6086 * recursively. */
Bram Moolenaar59edb002019-05-28 23:32:47 +02006087 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (padding != NULL)
6089 for (i = 0; i < pad_size; i++)
6090 padding[i] = gui.char_width;
6091 }
6092
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 /*
6094 * We have to provide the padding argument because italic and bold versions
6095 * of fixed-width fonts are often one pixel or so wider than their normal
6096 * versions.
6097 * No check for DRAW_BOLD, Windows will have done it already.
6098 */
6099
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 /* Check if there are any UTF-8 characters. If not, use normal text
6101 * output to speed up output. */
6102 if (enc_utf8)
6103 for (n = 0; n < len; ++n)
6104 if (text[n] >= 0x80)
6105 break;
6106
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006107#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006108 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6109 * required that unicode drawing routine, currently. So this forces it
6110 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006111 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006112 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006113#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006116 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006118 if ((enc_utf8
6119 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6120 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 && (unicodebuf == NULL || len > unibuflen))
6122 {
6123 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006124 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006127 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129 unibuflen = len;
6130 }
6131
6132 if (enc_utf8 && n < len && unicodebuf != NULL)
6133 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006134 /* Output UTF-8 characters. Composing characters should be
6135 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006136 int i;
6137 int wlen; /* string length in words */
6138 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 int cells; /* cell width of string up to composing char */
6140 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006141 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006143 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006144 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006146 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006148 c = utf_ptr2char(text + i);
6149 if (c >= 0x10000)
6150 {
6151 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006152 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6153 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006154 }
6155 else
6156 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006157 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006158 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006159
6160 if (utf_iscomposing(c))
6161 cw = 0;
6162 else
6163 {
6164 cw = utf_char2cells(c);
6165 if (cw > 2) /* don't use 4 for unprintable char */
6166 cw = 1;
6167 }
6168
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (unicodepdy != NULL)
6170 {
6171 /* Use unicodepdy to make characters fit as we expect, even
6172 * when the font uses different widths (e.g., bold character
6173 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006174 if (c >= 0x10000)
6175 {
6176 unicodepdy[wlen - 2] = cw * gui.char_width;
6177 unicodepdy[wlen - 1] = 0;
6178 }
6179 else
6180 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006183 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006184 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006186#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006187 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006188 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006189 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006190 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006191 TEXT_X(col), TEXT_Y(row),
6192 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006193 gui.char_width, gui.currFgColor,
6194 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006195 }
6196 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006197#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006198 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6199 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 len = cells; /* used for underlining */
6201 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006202 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /* If we want to display codepage data, and the current CP is not the
6205 * ANSI one, we need to go via Unicode. */
6206 if (unicodebuf != NULL)
6207 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006208 if (enc_latin9)
6209 latin9_to_ucs(text, len, unicodebuf);
6210 else
6211 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 MB_PRECOMPOSED,
6213 (char *)text, len,
6214 (LPWSTR)unicodebuf, unibuflen);
6215 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006216 {
6217 /* Use unicodepdy to make characters fit as we expect, even
6218 * when the font uses different widths (e.g., bold character
6219 * is wider). */
6220 if (unicodepdy != NULL)
6221 {
6222 int i;
6223 int cw;
6224
6225 for (i = 0; i < len; ++i)
6226 {
6227 cw = utf_char2cells(unicodebuf[i]);
6228 if (cw > 2)
6229 cw = 1;
6230 unicodepdy[i] = cw * gui.char_width;
6231 }
6232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006234 foptions, pcliprect, unicodebuf, len, unicodepdy);
6235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
6237 }
6238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 {
6240#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006241 /* Windows will mess up RL text, so we have to draw it character by
6242 * character. Only do this if RL is on, since it's slow. */
6243 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6245 foptions, pcliprect, (char *)text, len, padding);
6246 else
6247#endif
6248 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6249 foptions, pcliprect, (char *)text, len, padding);
6250 }
6251
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006252 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 if (flags & DRAW_UNDERL)
6254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 /* When p_linespace is 0, overwrite the bottom row of pixels.
6256 * Otherwise put the line just below the character. */
6257 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (p_linespace > 1)
6259 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006260 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006262
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006263 /* Strikethrough */
6264 if (flags & DRAW_STRIKE)
6265 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006266 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006267 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006268 }
6269
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006270 /* Undercurl */
6271 if (flags & DRAW_UNDERC)
6272 {
6273 int x;
6274 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006275 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006276
6277 y = FILL_Y(row + 1) - 1;
6278 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6279 {
6280 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006281 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006282 }
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284}
6285
6286
6287/*
6288 * Output routines.
6289 */
6290
6291/* Flush any output to the screen */
6292 void
6293gui_mch_flush(void)
6294{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006295#if defined(FEAT_DIRECTX)
6296 if (IS_ENABLE_DIRECTX())
6297 DWriteContext_Flush(s_dwc);
6298#endif
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 GdiFlush();
6301}
6302
6303 static void
6304clear_rect(RECT *rcp)
6305{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006306 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307}
6308
6309
Bram Moolenaarc716c302006-01-21 22:12:51 +00006310 void
6311gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6312{
6313 RECT workarea_rect;
6314
6315 get_work_area(&workarea_rect);
6316
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006317 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006318 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006319 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006320
6321 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6322 * the menubar for MSwin, we subtract it from the screen height, so that
6323 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006324 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006325 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006326 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006327 - GetSystemMetrics(SM_CYCAPTION)
6328#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006329 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006330#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006331 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006332}
6333
6334
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335#if defined(FEAT_MENU) || defined(PROTO)
6336/*
6337 * Add a sub menu to the menu bar.
6338 */
6339 void
6340gui_mch_add_menu(
6341 vimmenu_T *menu,
6342 int pos)
6343{
6344 vimmenu_T *parent = menu->parent;
6345
6346 menu->submenu_id = CreatePopupMenu();
6347 menu->id = s_menu_id++;
6348
6349 if (menu_is_menubar(menu->name))
6350 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006351 WCHAR *wn;
6352 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006354 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006355 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006356 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006358 infow.cbSize = sizeof(infow);
6359 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6360 | MIIM_SUBMENU;
6361 infow.dwItemData = (long_u)menu;
6362 infow.wID = menu->id;
6363 infow.fType = MFT_STRING;
6364 infow.dwTypeData = wn;
6365 infow.cch = (UINT)wcslen(wn);
6366 infow.hSubMenu = menu->submenu_id;
6367 InsertMenuItemW((parent == NULL)
6368 ? s_menuBar : parent->submenu_id,
6369 (UINT)pos, TRUE, &infow);
6370 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
6372
6373 /* Fix window size if menu may have wrapped */
6374 if (parent == NULL)
6375 gui_mswin_get_menu_height(!gui.starting);
6376#ifdef FEAT_TEAROFF
6377 else if (IsWindow(parent->tearoff_handle))
6378 rebuild_tearoff(parent);
6379#endif
6380}
6381
6382 void
6383gui_mch_show_popupmenu(vimmenu_T *menu)
6384{
6385 POINT mp;
6386
6387 (void)GetCursorPos((LPPOINT)&mp);
6388 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6389}
6390
6391 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006392gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 vimmenu_T *menu = gui_find_menu(path_name);
6395
6396 if (menu != NULL)
6397 {
6398 POINT p;
6399
6400 /* Find the position of the current cursor */
6401 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006402 if (mouse_pos)
6403 {
6404 int mx, my;
6405
6406 gui_mch_getmouse(&mx, &my);
6407 p.x += mx;
6408 p.y += my;
6409 }
6410 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006412 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6414 }
6415 msg_scroll = FALSE;
6416 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6417 }
6418}
6419
6420#if defined(FEAT_TEAROFF) || defined(PROTO)
6421/*
6422 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6423 * create it as a pseudo-"tearoff menu".
6424 */
6425 void
6426gui_make_tearoff(char_u *path_name)
6427{
6428 vimmenu_T *menu = gui_find_menu(path_name);
6429
6430 /* Found the menu, so tear it off. */
6431 if (menu != NULL)
6432 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6433}
6434#endif
6435
6436/*
6437 * Add a menu item to a menu
6438 */
6439 void
6440gui_mch_add_menu_item(
6441 vimmenu_T *menu,
6442 int idx)
6443{
6444 vimmenu_T *parent = menu->parent;
6445
6446 menu->id = s_menu_id++;
6447 menu->submenu_id = NULL;
6448
6449#ifdef FEAT_TEAROFF
6450 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6451 {
6452 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6453 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6454 }
6455 else
6456#endif
6457#ifdef FEAT_TOOLBAR
6458 if (menu_is_toolbar(parent->name))
6459 {
6460 TBBUTTON newtb;
6461
6462 vim_memset(&newtb, 0, sizeof(newtb));
6463 if (menu_is_separator(menu->name))
6464 {
6465 newtb.iBitmap = 0;
6466 newtb.fsStyle = TBSTYLE_SEP;
6467 }
6468 else
6469 {
6470 newtb.iBitmap = get_toolbar_bitmap(menu);
6471 newtb.fsStyle = TBSTYLE_BUTTON;
6472 }
6473 newtb.idCommand = menu->id;
6474 newtb.fsState = TBSTATE_ENABLED;
6475 newtb.iString = 0;
6476 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6477 (LPARAM)&newtb);
6478 menu->submenu_id = (HMENU)-1;
6479 }
6480 else
6481#endif
6482 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006483 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006485 wn = enc_to_utf16(menu->name, NULL);
6486 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006488 InsertMenuW(parent->submenu_id, (UINT)idx,
6489 (menu_is_separator(menu->name)
6490 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6491 (UINT)menu->id, wn);
6492 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#ifdef FEAT_TEAROFF
6495 if (IsWindow(parent->tearoff_handle))
6496 rebuild_tearoff(parent);
6497#endif
6498 }
6499}
6500
6501/*
6502 * Destroy the machine specific menu widget.
6503 */
6504 void
6505gui_mch_destroy_menu(vimmenu_T *menu)
6506{
6507#ifdef FEAT_TOOLBAR
6508 /*
6509 * is this a toolbar button?
6510 */
6511 if (menu->submenu_id == (HMENU)-1)
6512 {
6513 int iButton;
6514
6515 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6516 (WPARAM)menu->id, 0);
6517 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6518 }
6519 else
6520#endif
6521 {
6522 if (menu->parent != NULL
6523 && menu_is_popup(menu->parent->dname)
6524 && menu->parent->submenu_id != NULL)
6525 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6526 else
6527 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6528 if (menu->submenu_id != NULL)
6529 DestroyMenu(menu->submenu_id);
6530#ifdef FEAT_TEAROFF
6531 if (IsWindow(menu->tearoff_handle))
6532 DestroyWindow(menu->tearoff_handle);
6533 if (menu->parent != NULL
6534 && menu->parent->children != NULL
6535 && IsWindow(menu->parent->tearoff_handle))
6536 {
6537 /* This menu must not show up when rebuilding the tearoff window. */
6538 menu->modes = 0;
6539 rebuild_tearoff(menu->parent);
6540 }
6541#endif
6542 }
6543}
6544
6545#ifdef FEAT_TEAROFF
6546 static void
6547rebuild_tearoff(vimmenu_T *menu)
6548{
6549 /*hackish*/
6550 char_u tbuf[128];
6551 RECT trect;
6552 RECT rct;
6553 RECT roct;
6554 int x, y;
6555
6556 HWND thwnd = menu->tearoff_handle;
6557
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006558 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 if (GetWindowRect(thwnd, &trect)
6560 && GetWindowRect(s_hwnd, &rct)
6561 && GetClientRect(s_hwnd, &roct))
6562 {
6563 x = trect.left - rct.left;
6564 y = (trect.top - rct.bottom + roct.bottom);
6565 }
6566 else
6567 {
6568 x = y = 0xffffL;
6569 }
6570 DestroyWindow(thwnd);
6571 if (menu->children != NULL)
6572 {
6573 gui_mch_tearoff(tbuf, menu, x, y);
6574 if (IsWindow(menu->tearoff_handle))
6575 (void) SetWindowPos(menu->tearoff_handle,
6576 NULL,
6577 (int)trect.left,
6578 (int)trect.top,
6579 0, 0,
6580 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6581 }
6582}
6583#endif /* FEAT_TEAROFF */
6584
6585/*
6586 * Make a menu either grey or not grey.
6587 */
6588 void
6589gui_mch_menu_grey(
6590 vimmenu_T *menu,
6591 int grey)
6592{
6593#ifdef FEAT_TOOLBAR
6594 /*
6595 * is this a toolbar button?
6596 */
6597 if (menu->submenu_id == (HMENU)-1)
6598 {
6599 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6600 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6601 }
6602 else
6603#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006604 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6605 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
6607#ifdef FEAT_TEAROFF
6608 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6609 {
6610 WORD menuID;
6611 HWND menuHandle;
6612
6613 /*
6614 * A tearoff button has changed state.
6615 */
6616 if (menu->children == NULL)
6617 menuID = (WORD)(menu->id);
6618 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006619 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6621 if (menuHandle)
6622 EnableWindow(menuHandle, !grey);
6623
6624 }
6625#endif
6626}
6627
6628#endif /* FEAT_MENU */
6629
6630
6631/* define some macros used to make the dialogue creation more readable */
6632
6633#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6634#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006635#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6638/*
6639 * stuff for dialogs
6640 */
6641
6642/*
6643 * The callback routine used by all the dialogs. Very simple. First,
6644 * acknowledges the INITDIALOG message so that Windows knows to do standard
6645 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6646 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6647 * number.
6648 */
6649 static LRESULT CALLBACK
6650dialog_callback(
6651 HWND hwnd,
6652 UINT message,
6653 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006654 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 if (message == WM_INITDIALOG)
6657 {
6658 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6659 /* Set focus to the dialog. Set the default button, if specified. */
6660 (void)SetFocus(hwnd);
6661 if (dialog_default_button > IDCANCEL)
6662 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006663 else
6664 /* We don't have a default, set focus on another element of the
6665 * dialog window, probably the icon */
6666 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 return FALSE;
6668 }
6669
6670 if (message == WM_COMMAND)
6671 {
6672 int button = LOWORD(wParam);
6673
6674 /* Don't end the dialog if something was selected that was
6675 * not a button.
6676 */
6677 if (button >= DLG_NONBUTTON_CONTROL)
6678 return TRUE;
6679
6680 /* If the edit box exists, copy the string. */
6681 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006682 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006683 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006684 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006685
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006686 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6687 p = utf16_to_enc(wp, NULL);
6688 vim_strncpy(s_textfield, p, IOSIZE);
6689 vim_free(p);
6690 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
6693 /*
6694 * Need to check for IDOK because if the user just hits Return to
6695 * accept the default value, some reason this is what we get.
6696 */
6697 if (button == IDOK)
6698 {
6699 if (dialog_default_button > IDCANCEL)
6700 EndDialog(hwnd, dialog_default_button);
6701 }
6702 else
6703 EndDialog(hwnd, button - IDCANCEL);
6704 return TRUE;
6705 }
6706
6707 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6708 {
6709 EndDialog(hwnd, 0);
6710 return TRUE;
6711 }
6712 return FALSE;
6713}
6714
6715/*
6716 * Create a dialog dynamically from the parameter strings.
6717 * type = type of dialog (question, alert, etc.)
6718 * title = dialog title. may be NULL for default title.
6719 * message = text to display. Dialog sizes to accommodate it.
6720 * buttons = '\n' separated list of button captions, default first.
6721 * dfltbutton = number of default button.
6722 *
6723 * This routine returns 1 if the first button is pressed,
6724 * 2 for the second, etc.
6725 *
6726 * 0 indicates Esc was pressed.
6727 * -1 for unexpected error
6728 *
6729 * If stubbing out this fn, return 1.
6730 */
6731
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006732static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 "IDR_VIM",
6735 "IDR_VIM_ERROR",
6736 "IDR_VIM_ALERT",
6737 "IDR_VIM_INFO",
6738 "IDR_VIM_QUESTION"
6739};
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 int
6742gui_mch_dialog(
6743 int type,
6744 char_u *title,
6745 char_u *message,
6746 char_u *buttons,
6747 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006748 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006749 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006752 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 int numButtons;
6754 int *buttonWidths, *buttonPositions;
6755 int buttonYpos;
6756 int nchar, i;
6757 DWORD lStyle;
6758 int dlgwidth = 0;
6759 int dlgheight;
6760 int editboxheight;
6761 int horizWidth = 0;
6762 int msgheight;
6763 char_u *pstart;
6764 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006765 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 char_u *tbuffer;
6767 RECT rect;
6768 HWND hwnd;
6769 HDC hdc;
6770 HFONT font, oldFont;
6771 TEXTMETRIC fontInfo;
6772 int fontHeight;
6773 int textWidth, minButtonWidth, messageWidth;
6774 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006775 int maxDialogHeight;
6776 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 int vertical;
6778 int dlgPaddingX;
6779 int dlgPaddingY;
6780#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006781 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 int use_lfSysmenu = FALSE;
6783#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006784 garray_T ga;
6785 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
6787#ifndef NO_CONSOLE
6788 /* Don't output anything in silent mode ("ex -s") */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006789# ifdef VIMDLL
6790 if (!(gui.in_use || gui.starting))
6791# endif
6792 if (silent_mode)
6793 return dfltbutton; /* return default option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794#endif
6795
Bram Moolenaar748bf032005-02-02 23:04:36 +00006796 if (s_hwnd == NULL)
6797 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
6799 if ((type < 0) || (type > VIM_LAST_TYPE))
6800 type = 0;
6801
6802 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006803 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006804 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006805 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 if (p == NULL)
6808 return -1;
6809
6810 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006811 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6813 * const.
6814 */
6815 tbuffer = vim_strsave(buttons);
6816 if (tbuffer == NULL)
6817 return -1;
6818
6819 --dfltbutton; /* Change from one-based to zero-based */
6820
6821 /* Count buttons */
6822 numButtons = 1;
6823 for (i = 0; tbuffer[i] != '\0'; i++)
6824 {
6825 if (tbuffer[i] == DLG_BUTTON_SEP)
6826 numButtons++;
6827 }
6828 if (dfltbutton >= numButtons)
6829 dfltbutton = -1;
6830
6831 /* Allocate array to hold the width of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006832 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (buttonWidths == NULL)
6834 return -1;
6835
6836 /* Allocate array to hold the X position of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006837 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (buttonPositions == NULL)
6839 return -1;
6840
6841 /*
6842 * Calculate how big the dialog must be.
6843 */
6844 hwnd = GetDesktopWindow();
6845 hdc = GetWindowDC(hwnd);
6846#ifdef USE_SYSMENU_FONT
6847 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6848 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006849 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 use_lfSysmenu = TRUE;
6851 }
6852 else
6853#endif
6854 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6855 VARIABLE_PITCH , DLG_FONT_NAME);
6856 if (s_usenewlook)
6857 {
6858 oldFont = SelectFont(hdc, font);
6859 dlgPaddingX = DLG_PADDING_X;
6860 dlgPaddingY = DLG_PADDING_Y;
6861 }
6862 else
6863 {
6864 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6865 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6866 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6867 }
6868 GetTextMetrics(hdc, &fontInfo);
6869 fontHeight = fontInfo.tmHeight;
6870
6871 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006872 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006875 if (s_hwnd == NULL)
6876 {
6877 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaarc716c302006-01-21 22:12:51 +00006879 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006880 get_work_area(&workarea_rect);
6881 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6882 if (maxDialogWidth > 600)
6883 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006884 /* Leave some room for the taskbar. */
6885 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886 }
6887 else
6888 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006889 /* Use our own window for the size, unless it's very small. */
6890 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006891 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006892 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006893 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006894 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6895 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006896
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006897 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006898 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006899 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006900 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006901 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6902 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6903 }
6904
6905 /* Set dlgwidth to width of message.
6906 * Copy the message into "ga", changing NL to CR-NL and inserting line
6907 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 pstart = message;
6909 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006910 msgheight = 0;
6911 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 do
6913 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006914 msgheight += fontHeight; /* at least one line */
6915
6916 /* Need to figure out where to break the string. The system does it
6917 * at a word boundary, which would mean we can't compute the number of
6918 * wrapped lines. */
6919 textWidth = 0;
6920 last_white = NULL;
6921 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006922 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006923 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006924 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925 && textWidth > maxDialogWidth * 3 / 4)
6926 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006927 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006928 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006929 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006930 /* Line will wrap. */
6931 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006932 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 textWidth = 0;
6934
6935 if (last_white != NULL)
6936 {
6937 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006938 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006939 pend = last_white + 1;
6940 last_white = NULL;
6941 }
6942 ga_append(&ga, '\r');
6943 ga_append(&ga, '\n');
6944 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006945 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006946
6947 while (--l >= 0)
6948 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006949 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006950 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006952
6953 ga_append(&ga, '\r');
6954 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 pstart = pend + 1;
6956 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006957
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006958 if (ga.ga_data != NULL)
6959 message = ga.ga_data;
6960
Bram Moolenaar748bf032005-02-02 23:04:36 +00006961 messageWidth += 10; /* roundoff space */
6962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006964 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6965 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966
6967 if (msgheight < DLG_ICON_HEIGHT)
6968 msgheight = DLG_ICON_HEIGHT;
6969
6970 /*
6971 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006972 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006974 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (!vertical)
6976 {
6977 // Place buttons horizontally if they fit.
6978 horizWidth = dlgPaddingX;
6979 pstart = tbuffer;
6980 i = 0;
6981 do
6982 {
6983 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6984 if (pend == NULL)
6985 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006986 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (textWidth < minButtonWidth)
6988 textWidth = minButtonWidth;
6989 textWidth += dlgPaddingX; /* Padding within button */
6990 buttonWidths[i] = textWidth;
6991 buttonPositions[i++] = horizWidth;
6992 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
6993 pstart = pend + 1;
6994 } while (*pend != NUL);
6995
6996 if (horizWidth > maxDialogWidth)
6997 vertical = TRUE; // Too wide to fit on the screen.
6998 else if (horizWidth > dlgwidth)
6999 dlgwidth = horizWidth;
7000 }
7001
7002 if (vertical)
7003 {
7004 // Stack buttons vertically.
7005 pstart = tbuffer;
7006 do
7007 {
7008 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7009 if (pend == NULL)
7010 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007011 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 textWidth += dlgPaddingX; /* Padding within button */
7013 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7014 if (textWidth > dlgwidth)
7015 dlgwidth = textWidth;
7016 pstart = pend + 1;
7017 } while (*pend != NUL);
7018 }
7019
7020 if (dlgwidth < DLG_MIN_WIDTH)
7021 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7022
7023 /* start to fill in the dlgtemplate information. addressing by WORDs */
7024 if (s_usenewlook)
7025 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7026 else
7027 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7028
7029 add_long(lStyle);
7030 add_long(0); // (lExtendedStyle)
7031 pnumitems = p; /*save where the number of items must be stored*/
7032 add_word(0); // NumberOfItems(will change later)
7033 add_word(10); // x
7034 add_word(10); // y
7035 add_word(PixelToDialogX(dlgwidth)); // cx
7036
7037 // Dialog height.
7038 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007039 dlgheight = msgheight + 2 * dlgPaddingY
7040 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 else
7042 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7043
7044 // Dialog needs to be taller if contains an edit box.
7045 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7046 if (textfield != NULL)
7047 dlgheight += editboxheight;
7048
Bram Moolenaara95d8232013-08-07 15:27:11 +02007049 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7050 if (dlgheight > maxDialogHeight)
7051 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007052 msgheight = msgheight - (dlgheight - maxDialogHeight);
7053 dlgheight = maxDialogHeight;
7054 scroll_flag = WS_VSCROLL;
7055 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7056 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007057 }
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 add_word(PixelToDialogY(dlgheight));
7060
7061 add_word(0); // Menu
7062 add_word(0); // Class
7063
7064 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007065 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7066 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 p += nchar;
7068
7069 if (s_usenewlook)
7070 {
7071 /* do the font, since DS_3DLOOK doesn't work properly */
7072#ifdef USE_SYSMENU_FONT
7073 if (use_lfSysmenu)
7074 {
7075 /* point size */
7076 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7077 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007078 wcscpy(p, lfSysmenu.lfFaceName);
7079 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081 else
7082#endif
7083 {
7084 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007085 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 p += nchar;
7088 }
7089
7090 buttonYpos = msgheight + 2 * dlgPaddingY;
7091
7092 if (textfield != NULL)
7093 buttonYpos += editboxheight;
7094
7095 pstart = tbuffer;
7096 if (!vertical)
7097 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7098 for (i = 0; i < numButtons; i++)
7099 {
7100 /* get end of this button. */
7101 for ( pend = pstart;
7102 *pend && (*pend != DLG_BUTTON_SEP);
7103 pend++)
7104 ;
7105
7106 if (*pend)
7107 *pend = '\0';
7108
7109 /*
7110 * old NOTE:
7111 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7112 * the focus to the first tab-able button and in so doing makes that
7113 * the default!! Grrr. Workaround: Make the default button the only
7114 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7115 * he/she can use arrow keys.
7116 *
7117 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007118 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 * dialog. Also needed for when the textfield is the default control.
7120 * It appears to work now (perhaps not on Win95?).
7121 */
7122 if (vertical)
7123 {
7124 p = add_dialog_element(p,
7125 (i == dfltbutton
7126 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7127 PixelToDialogX(DLG_VERT_PADDING_X),
7128 PixelToDialogY(buttonYpos /* TBK */
7129 + 2 * fontHeight * i),
7130 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7131 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007132 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134 else
7135 {
7136 p = add_dialog_element(p,
7137 (i == dfltbutton
7138 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7139 PixelToDialogX(horizWidth + buttonPositions[i]),
7140 PixelToDialogY(buttonYpos), /* TBK */
7141 PixelToDialogX(buttonWidths[i]),
7142 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007143 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
7145 pstart = pend + 1; /*next button*/
7146 }
7147 *pnumitems += numButtons;
7148
7149 /* Vim icon */
7150 p = add_dialog_element(p, SS_ICON,
7151 PixelToDialogX(dlgPaddingX),
7152 PixelToDialogY(dlgPaddingY),
7153 PixelToDialogX(DLG_ICON_WIDTH),
7154 PixelToDialogY(DLG_ICON_HEIGHT),
7155 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7156 dlg_icons[type]);
7157
Bram Moolenaar748bf032005-02-02 23:04:36 +00007158 /* Dialog message */
7159 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7160 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7161 PixelToDialogY(dlgPaddingY),
7162 (WORD)(PixelToDialogX(messageWidth) + 1),
7163 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007164 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
7166 /* Edit box */
7167 if (textfield != NULL)
7168 {
7169 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7170 PixelToDialogX(2 * dlgPaddingX),
7171 PixelToDialogY(2 * dlgPaddingY + msgheight),
7172 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7173 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007174 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 *pnumitems += 1;
7176 }
7177
7178 *pnumitems += 2;
7179
7180 SelectFont(hdc, oldFont);
7181 DeleteObject(font);
7182 ReleaseDC(hwnd, hdc);
7183
7184 /* Let the dialog_callback() function know which button to make default
7185 * If we have an edit box, make that the default. We also need to tell
7186 * dialog_callback() if this dialog contains an edit box or not. We do
7187 * this by setting s_textfield if it does.
7188 */
7189 if (textfield != NULL)
7190 {
7191 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7192 s_textfield = textfield;
7193 }
7194 else
7195 {
7196 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7197 s_textfield = NULL;
7198 }
7199
7200 /* show the dialog box modally and get a return value */
7201 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007202 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 (LPDLGTEMPLATE)pdlgtemplate,
7204 s_hwnd,
7205 (DLGPROC)dialog_callback);
7206
7207 LocalFree(LocalHandle(pdlgtemplate));
7208 vim_free(tbuffer);
7209 vim_free(buttonWidths);
7210 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007211 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 /* Focus back to our window (for when MDI is used). */
7214 (void)SetFocus(s_hwnd);
7215
7216 return nchar;
7217}
7218
7219#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221/*
7222 * Put a simple element (basic class) onto a dialog template in memory.
7223 * return a pointer to where the next item should be added.
7224 *
7225 * parameters:
7226 * lStyle = additional style flags
7227 * (be careful, NT3.51 & Win32s will ignore the new ones)
7228 * x,y = x & y positions IN DIALOG UNITS
7229 * w,h = width and height IN DIALOG UNITS
7230 * Id = ID used in messages
7231 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7232 * caption = usually text or resource name
7233 *
7234 * TODO: use the length information noted here to enable the dialog creation
7235 * routines to work out more exactly how much memory they need to alloc.
7236 */
7237 static PWORD
7238add_dialog_element(
7239 PWORD p,
7240 DWORD lStyle,
7241 WORD x,
7242 WORD y,
7243 WORD w,
7244 WORD h,
7245 WORD Id,
7246 WORD clss,
7247 const char *caption)
7248{
7249 int nchar;
7250
7251 p = lpwAlign(p); /* Align to dword boundary*/
7252 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7253 *p++ = LOWORD(lStyle);
7254 *p++ = HIWORD(lStyle);
7255 *p++ = 0; // LOWORD (lExtendedStyle)
7256 *p++ = 0; // HIWORD (lExtendedStyle)
7257 *p++ = x;
7258 *p++ = y;
7259 *p++ = w;
7260 *p++ = h;
7261 *p++ = Id; //9 or 10 words in all
7262
7263 *p++ = (WORD)0xffff;
7264 *p++ = clss; //2 more here
7265
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007266 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 p += nchar;
7268
7269 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7270
7271 return p; //total = 15+ (strlen(caption)) words
7272 // = 30 + 2(strlen(caption) bytes reqd
7273}
7274
7275
7276/*
7277 * Helper routine. Take an input pointer, return closest pointer that is
7278 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7279 */
7280 static LPWORD
7281lpwAlign(
7282 LPWORD lpIn)
7283{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007284 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007286 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 ul += 3;
7288 ul >>= 2;
7289 ul <<= 2;
7290 return (LPWORD)ul;
7291}
7292
7293/*
7294 * Helper routine. Takes second parameter as Ansi string, copies it to first
7295 * parameter as wide character (16-bits / char) string, and returns integer
7296 * number of wide characters (words) in string (including the trailing wide
7297 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007298 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7299 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 static int
7301nCopyAnsiToWideChar(
7302 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007303 LPSTR lpAnsiIn,
7304 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
7306 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7308 int i;
7309 WCHAR *wn;
7310
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007311 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 {
7313 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007314 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (wn != NULL)
7316 {
7317 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007318 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 vim_free(wn);
7320 }
7321 }
7322 if (nChar == 0)
7323 /* Use Win32 conversion function. */
7324 nChar = MultiByteToWideChar(
7325 enc_codepage > 0 ? enc_codepage : CP_ACP,
7326 MB_PRECOMPOSED,
7327 lpAnsiIn, len,
7328 lpWCStr, len);
7329 for (i = 0; i < nChar; ++i)
7330 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7331 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
7333 return nChar;
7334}
7335
7336
7337#ifdef FEAT_TEAROFF
7338/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007339 * Lookup menu handle from "menu_id".
7340 */
7341 static HMENU
7342tearoff_lookup_menuhandle(
7343 vimmenu_T *menu,
7344 WORD menu_id)
7345{
7346 for ( ; menu != NULL; menu = menu->next)
7347 {
7348 if (menu->modes == 0) /* this menu has just been deleted */
7349 continue;
7350 if (menu_is_separator(menu->dname))
7351 continue;
7352 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7353 return menu->submenu_id;
7354 }
7355 return NULL;
7356}
7357
7358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 * The callback function for all the modeless dialogs that make up the
7360 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7361 * thinking its menus have been clicked), and go away when closed.
7362 */
7363 static LRESULT CALLBACK
7364tearoff_callback(
7365 HWND hwnd,
7366 UINT message,
7367 WPARAM wParam,
7368 LPARAM lParam)
7369{
7370 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007371 {
7372 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 /* May show the mouse pointer again. */
7377 HandleMouseHide(message, lParam);
7378
7379 if (message == WM_COMMAND)
7380 {
7381 if ((WORD)(LOWORD(wParam)) & 0x8000)
7382 {
7383 POINT mp;
7384 RECT rect;
7385
7386 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7387 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007388 vimmenu_T *menu;
7389
7390 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007392 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7394 (int)rect.right - 8,
7395 (int)mp.y,
7396 (int)0, /*reserved param*/
7397 s_hwnd,
7398 NULL);
7399 /*
7400 * NOTE: The pop-up menu can eat the mouse up event.
7401 * We deal with this in normal.c.
7402 */
7403 }
7404 }
7405 else
7406 /* Pass on messages to the main Vim window */
7407 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7408 /*
7409 * Give main window the focus back: this is so after
7410 * choosing a tearoff button you can start typing again
7411 * straight away.
7412 */
7413 (void)SetFocus(s_hwnd);
7414 return TRUE;
7415 }
7416 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7417 {
7418 DestroyWindow(hwnd);
7419 return TRUE;
7420 }
7421
7422 /* When moved around, give main window the focus back. */
7423 if (message == WM_EXITSIZEMOVE)
7424 (void)SetActiveWindow(s_hwnd);
7425
7426 return FALSE;
7427}
7428#endif
7429
7430
7431/*
7432 * Decide whether to use the "new look" (small, non-bold font) or the "old
7433 * look" (big, clanky font) for dialogs, and work out a few values for use
7434 * later accordingly.
7435 */
7436 static void
7437get_dialog_font_metrics(void)
7438{
7439 HDC hdc;
7440 HFONT hfontTools = 0;
7441 DWORD dlgFontSize;
7442 SIZE size;
7443#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007444 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445#endif
7446
7447 s_usenewlook = FALSE;
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007450 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007451 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453#endif
7454 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7455 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7456
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007457 if (hfontTools)
7458 {
7459 hdc = GetDC(s_hwnd);
7460 SelectObject(hdc, hfontTools);
7461 /*
7462 * GetTextMetrics() doesn't return the right value in
7463 * tmAveCharWidth, so we have to figure out the dialog base units
7464 * ourselves.
7465 */
7466 GetTextExtentPoint(hdc,
7467 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7468 52, &size);
7469 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007471 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7472 s_dlgfntheight = (WORD)size.cy;
7473 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475
7476 if (!s_usenewlook)
7477 {
7478 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7479 s_dlgfntwidth = LOWORD(dlgFontSize);
7480 s_dlgfntheight = HIWORD(dlgFontSize);
7481 }
7482}
7483
7484#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7485/*
7486 * Create a pseudo-"tearoff menu" based on the child
7487 * items of a given menu pointer.
7488 */
7489 static void
7490gui_mch_tearoff(
7491 char_u *title,
7492 vimmenu_T *menu,
7493 int initX,
7494 int initY)
7495{
7496 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7497 int template_len;
7498 int nchar, textWidth, submenuWidth;
7499 DWORD lStyle;
7500 DWORD lExtendedStyle;
7501 WORD dlgwidth;
7502 WORD menuID;
7503 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007504 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 vimmenu_T *the_menu = menu;
7506 HWND hwnd;
7507 HDC hdc;
7508 HFONT font, oldFont;
7509 int col, spaceWidth, len;
7510 int columnWidths[2];
7511 char_u *label, *text;
7512 int acLen = 0;
7513 int nameLen;
7514 int padding0, padding1, padding2 = 0;
7515 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007516 int x;
7517 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007519 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 int use_lfSysmenu = FALSE;
7521#endif
7522
7523 /*
7524 * If this menu is already torn off, move it to the mouse position.
7525 */
7526 if (IsWindow(menu->tearoff_handle))
7527 {
7528 POINT mp;
7529 if (GetCursorPos((LPPOINT)&mp))
7530 {
7531 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7532 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7533 }
7534 return;
7535 }
7536
7537 /*
7538 * Create a new tearoff.
7539 */
7540 if (*title == MNU_HIDDEN_CHAR)
7541 title++;
7542
7543 /* Allocate memory to store the dialog template. It's made bigger when
7544 * needed. */
7545 template_len = DLG_ALLOC_SIZE;
7546 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7547 if (p == NULL)
7548 return;
7549
7550 hwnd = GetDesktopWindow();
7551 hdc = GetWindowDC(hwnd);
7552#ifdef USE_SYSMENU_FONT
7553 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7554 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007555 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 use_lfSysmenu = TRUE;
7557 }
7558 else
7559#endif
7560 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7561 VARIABLE_PITCH , DLG_FONT_NAME);
7562 if (s_usenewlook)
7563 oldFont = SelectFont(hdc, font);
7564 else
7565 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7566
7567 /* Calculate width of a single space. Used for padding columns to the
7568 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007569 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
7571 /* Figure out max width of the text column, the accelerator column and the
7572 * optional submenu column. */
7573 submenuWidth = 0;
7574 for (col = 0; col < 2; col++)
7575 {
7576 columnWidths[col] = 0;
7577 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7578 {
7579 /* Use "dname" here to compute the width of the visible text. */
7580 text = (col == 0) ? pmenu->dname : pmenu->actext;
7581 if (text != NULL && *text != NUL)
7582 {
7583 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7584 if (textWidth > columnWidths[col])
7585 columnWidths[col] = textWidth;
7586 }
7587 if (pmenu->children != NULL)
7588 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7589 }
7590 }
7591 if (columnWidths[1] == 0)
7592 {
7593 /* no accelerators */
7594 if (submenuWidth != 0)
7595 columnWidths[0] += submenuWidth;
7596 else
7597 columnWidths[0] += spaceWidth;
7598 }
7599 else
7600 {
7601 /* there is an accelerator column */
7602 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7603 columnWidths[1] += submenuWidth;
7604 }
7605
7606 /*
7607 * Now find the total width of our 'menu'.
7608 */
7609 textWidth = columnWidths[0] + columnWidths[1];
7610 if (submenuWidth != 0)
7611 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007612 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7614 textWidth += submenuWidth;
7615 }
7616 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7617 if (textWidth > dlgwidth)
7618 dlgwidth = textWidth;
7619 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7620
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 /* start to fill in the dlgtemplate information. addressing by WORDs */
7622 if (s_usenewlook)
7623 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7624 else
7625 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7626
7627 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7628 *p++ = LOWORD(lStyle);
7629 *p++ = HIWORD(lStyle);
7630 *p++ = LOWORD(lExtendedStyle);
7631 *p++ = HIWORD(lExtendedStyle);
7632 pnumitems = p; /* save where the number of items must be stored */
7633 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007634 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007636 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else
7638 *p++ = PixelToDialogX(initX); // x
7639 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007640 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 else
7642 *p++ = PixelToDialogY(initY); // y
7643 *p++ = PixelToDialogX(dlgwidth); // cx
7644 ptrueheight = p;
7645 *p++ = 0; // dialog height: changed later anyway
7646 *p++ = 0; // Menu
7647 *p++ = 0; // Class
7648
7649 /* copy the title of the dialog */
7650 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007651 ? (LPSTR)title
7652 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 p += nchar;
7654
7655 if (s_usenewlook)
7656 {
7657 /* do the font, since DS_3DLOOK doesn't work properly */
7658#ifdef USE_SYSMENU_FONT
7659 if (use_lfSysmenu)
7660 {
7661 /* point size */
7662 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7663 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007664 wcscpy(p, lfSysmenu.lfFaceName);
7665 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 }
7667 else
7668#endif
7669 {
7670 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007671 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 }
7673 p += nchar;
7674 }
7675
7676 /*
7677 * Loop over all the items in the menu.
7678 * But skip over the tearbar.
7679 */
7680 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7681 menu = menu->children->next;
7682 else
7683 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007684 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 for ( ; menu != NULL; menu = menu->next)
7686 {
7687 if (menu->modes == 0) /* this menu has just been deleted */
7688 continue;
7689 if (menu_is_separator(menu->dname))
7690 {
7691 sepPadding += 3;
7692 continue;
7693 }
7694
7695 /* Check if there still is plenty of room in the template. Make it
7696 * larger when needed. */
7697 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7698 {
7699 WORD *newp;
7700
7701 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7702 if (newp != NULL)
7703 {
7704 template_len += 4096;
7705 mch_memmove(newp, pdlgtemplate,
7706 (char *)p - (char *)pdlgtemplate);
7707 p = newp + (p - pdlgtemplate);
7708 pnumitems = newp + (pnumitems - pdlgtemplate);
7709 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7710 LocalFree(LocalHandle(pdlgtemplate));
7711 pdlgtemplate = newp;
7712 }
7713 }
7714
7715 /* Figure out minimal length of this menu label. Use "name" for the
7716 * actual text, "dname" for estimating the displayed size. "name"
7717 * has "&a" for mnemonic and includes the accelerator. */
7718 len = nameLen = (int)STRLEN(menu->name);
7719 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7720 (int)STRLEN(menu->dname))) / spaceWidth;
7721 len += padding0;
7722
7723 if (menu->actext != NULL)
7724 {
7725 acLen = (int)STRLEN(menu->actext);
7726 len += acLen;
7727 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7728 }
7729 else
7730 textWidth = 0;
7731 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7732 len += padding1;
7733
7734 if (menu->children == NULL)
7735 {
7736 padding2 = submenuWidth / spaceWidth;
7737 len += padding2;
7738 menuID = (WORD)(menu->id);
7739 }
7740 else
7741 {
7742 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007743 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 }
7745
7746 /* Allocate menu label and fill it in */
Bram Moolenaar964b3742019-05-24 18:54:09 +02007747 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 if (label == NULL)
7749 break;
7750
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007751 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7753 if (text == NULL)
7754 text = label + nameLen; /* no actext, use whole name */
7755 while (padding0-- > 0)
7756 *text++ = ' ';
7757 if (menu->actext != NULL)
7758 {
7759 STRNCPY(text, menu->actext, acLen);
7760 text += acLen;
7761 }
7762 while (padding1-- > 0)
7763 *text++ = ' ';
7764 if (menu->children != NULL)
7765 {
7766 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7767 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7768 }
7769 else
7770 {
7771 while (padding2-- > 0)
7772 *text++ = ' ';
7773 }
7774 *text = NUL;
7775
7776 /*
7777 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7778 * W95/NT4 it makes the tear-off look more like a menu.
7779 */
7780 p = add_dialog_element(p,
7781 BS_PUSHBUTTON|BS_LEFT,
7782 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7783 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7784 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7785 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007786 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 vim_free(label);
7788 (*pnumitems)++;
7789 }
7790
7791 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7792
7793
7794 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007795 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007796 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 (LPDLGTEMPLATE)pdlgtemplate,
7798 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007799 (DLGPROC)tearoff_callback,
7800 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
7802 LocalFree(LocalHandle(pdlgtemplate));
7803 SelectFont(hdc, oldFont);
7804 DeleteObject(font);
7805 ReleaseDC(hwnd, hdc);
7806
7807 /*
7808 * Reassert ourselves as the active window. This is so that after creating
7809 * a tearoff, the user doesn't have to click with the mouse just to start
7810 * typing again!
7811 */
7812 (void)SetActiveWindow(s_hwnd);
7813
7814 /* make sure the right buttons are enabled */
7815 force_menu_update = TRUE;
7816}
7817#endif
7818
7819#if defined(FEAT_TOOLBAR) || defined(PROTO)
7820#include "gui_w32_rc.h"
7821
7822/* This not defined in older SDKs */
7823# ifndef TBSTYLE_FLAT
7824# define TBSTYLE_FLAT 0x0800
7825# endif
7826
7827/*
7828 * Create the toolbar, initially unpopulated.
7829 * (just like the menu, there are no defaults, it's all
7830 * set up through menu.vim)
7831 */
7832 static void
7833initialise_toolbar(void)
7834{
7835 InitCommonControls();
7836 s_toolbarhwnd = CreateToolbarEx(
7837 s_hwnd,
7838 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7839 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007840 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007841 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 IDR_TOOLBAR1, // id of initial bitmap
7843 NULL,
7844 0, // initial number of buttons
7845 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7846 TOOLBAR_BUTTON_HEIGHT,
7847 TOOLBAR_BUTTON_WIDTH,
7848 TOOLBAR_BUTTON_HEIGHT,
7849 sizeof(TBBUTTON)
7850 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007851 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852
7853 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7854}
7855
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007856 static LRESULT CALLBACK
7857toolbar_wndproc(
7858 HWND hwnd,
7859 UINT uMsg,
7860 WPARAM wParam,
7861 LPARAM lParam)
7862{
7863 HandleMouseHide(uMsg, lParam);
7864 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7865}
7866
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 static int
7868get_toolbar_bitmap(vimmenu_T *menu)
7869{
7870 int i = -1;
7871
7872 /*
7873 * Check user bitmaps first, unless builtin is specified.
7874 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007875 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 {
7877 char_u fname[MAXPATHL];
7878 HANDLE hbitmap = NULL;
7879
7880 if (menu->iconfile != NULL)
7881 {
7882 gui_find_iconfile(menu->iconfile, fname, "bmp");
7883 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
7894 /*
7895 * If the LoadImage call failed, or the "icon=" file
7896 * didn't exist or wasn't specified, try the menu name
7897 */
7898 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007899 && (gui_find_bitmap(
7900#ifdef FEAT_MULTI_LANG
7901 menu->en_dname != NULL ? menu->en_dname :
7902#endif
7903 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 hbitmap = LoadImage(
7905 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007906 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 IMAGE_BITMAP,
7908 TOOLBAR_BUTTON_WIDTH,
7909 TOOLBAR_BUTTON_HEIGHT,
7910 LR_LOADFROMFILE |
7911 LR_LOADMAP3DCOLORS
7912 );
7913
7914 if (hbitmap != NULL)
7915 {
7916 TBADDBITMAP tbAddBitmap;
7917
7918 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007919 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920
7921 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7922 (WPARAM)1, (LPARAM)&tbAddBitmap);
7923 /* i will be set to -1 if it fails */
7924 }
7925 }
7926 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7927 i = menu->iconidx;
7928
7929 return i;
7930}
7931#endif
7932
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007933#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7934 static void
7935initialise_tabline(void)
7936{
7937 InitCommonControls();
7938
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007939 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007940 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007941 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007942 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007943 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007944
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007945 gui.tabline_height = TABLINE_HEIGHT;
7946
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007947# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007948 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007949# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007950}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007951
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007952/*
7953 * Get tabpage_T from POINT.
7954 */
7955 static tabpage_T *
7956GetTabFromPoint(
7957 HWND hWnd,
7958 POINT pt)
7959{
7960 tabpage_T *ptp = NULL;
7961
7962 if (gui_mch_showing_tabline())
7963 {
7964 TCHITTESTINFO htinfo;
7965 htinfo.pt = pt;
7966 /* ignore if a window under cusor is not tabcontrol. */
7967 if (s_tabhwnd == hWnd)
7968 {
7969 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7970 if (idx != -1)
7971 ptp = find_tabpage(idx + 1);
7972 }
7973 }
7974 return ptp;
7975}
7976
7977static POINT s_pt = {0, 0};
7978static HCURSOR s_hCursor = NULL;
7979
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007980 static LRESULT CALLBACK
7981tabline_wndproc(
7982 HWND hwnd,
7983 UINT uMsg,
7984 WPARAM wParam,
7985 LPARAM lParam)
7986{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007987 POINT pt;
7988 tabpage_T *tp;
7989 RECT rect;
7990 int nCenter;
7991 int idx0;
7992 int idx1;
7993
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007994 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007995
7996 switch (uMsg)
7997 {
7998 case WM_LBUTTONDOWN:
7999 {
8000 s_pt.x = GET_X_LPARAM(lParam);
8001 s_pt.y = GET_Y_LPARAM(lParam);
8002 SetCapture(hwnd);
8003 s_hCursor = GetCursor(); /* backup default cursor */
8004 break;
8005 }
8006 case WM_MOUSEMOVE:
8007 if (GetCapture() == hwnd
8008 && ((wParam & MK_LBUTTON)) != 0)
8009 {
8010 pt.x = GET_X_LPARAM(lParam);
8011 pt.y = s_pt.y;
8012 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8013 {
8014 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8015
8016 tp = GetTabFromPoint(hwnd, pt);
8017 if (tp != NULL)
8018 {
8019 idx0 = tabpage_index(curtab) - 1;
8020 idx1 = tabpage_index(tp) - 1;
8021
8022 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8023 nCenter = rect.left + (rect.right - rect.left) / 2;
8024
8025 /* Check if the mouse cursor goes over the center of
8026 * the next tab to prevent "flickering". */
8027 if ((idx0 < idx1) && (nCenter < pt.x))
8028 {
8029 tabpage_move(idx1 + 1);
8030 update_screen(0);
8031 }
8032 else if ((idx1 < idx0) && (pt.x < nCenter))
8033 {
8034 tabpage_move(idx1);
8035 update_screen(0);
8036 }
8037 }
8038 }
8039 }
8040 break;
8041 case WM_LBUTTONUP:
8042 {
8043 if (GetCapture() == hwnd)
8044 {
8045 SetCursor(s_hCursor);
8046 ReleaseCapture();
8047 }
8048 break;
8049 }
8050 default:
8051 break;
8052 }
8053
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008054 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8055}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008056#endif
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8059/*
8060 * Make the GUI window come to the foreground.
8061 */
8062 void
8063gui_mch_set_foreground(void)
8064{
8065 if (IsIconic(s_hwnd))
8066 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8067 SetForegroundWindow(s_hwnd);
8068}
8069#endif
8070
8071#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8072 static void
8073dyn_imm_load(void)
8074{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008075 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 if (hLibImm == NULL)
8077 return;
8078
8079 pImmGetCompositionStringA
8080 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8081 pImmGetCompositionStringW
8082 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8083 pImmGetContext
8084 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8085 pImmAssociateContext
8086 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8087 pImmReleaseContext
8088 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8089 pImmGetOpenStatus
8090 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8091 pImmSetOpenStatus
8092 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008093 pImmGetCompositionFontW
8094 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8095 pImmSetCompositionFontW
8096 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 pImmSetCompositionWindow
8098 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8099 pImmGetConversionStatus
8100 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008101 pImmSetConversionStatus
8102 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
8104 if ( pImmGetCompositionStringA == NULL
8105 || pImmGetCompositionStringW == NULL
8106 || pImmGetContext == NULL
8107 || pImmAssociateContext == NULL
8108 || pImmReleaseContext == NULL
8109 || pImmGetOpenStatus == NULL
8110 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008111 || pImmGetCompositionFontW == NULL
8112 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008114 || pImmGetConversionStatus == NULL
8115 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 {
8117 FreeLibrary(hLibImm);
8118 hLibImm = NULL;
8119 pImmGetContext = NULL;
8120 return;
8121 }
8122
8123 return;
8124}
8125
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126#endif
8127
8128#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8129
8130# ifdef FEAT_XPM_W32
8131# define IMAGE_XPM 100
8132# endif
8133
8134typedef struct _signicon_t
8135{
8136 HANDLE hImage;
8137 UINT uType;
8138#ifdef FEAT_XPM_W32
8139 HANDLE hShape; /* Mask bitmap handle */
8140#endif
8141} signicon_t;
8142
8143 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008144gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145{
8146 signicon_t *sign;
8147 int x, y, w, h;
8148
8149 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8150 return;
8151
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008152#if defined(FEAT_DIRECTX)
8153 if (IS_ENABLE_DIRECTX())
8154 DWriteContext_Flush(s_dwc);
8155#endif
8156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 x = TEXT_X(col);
8158 y = TEXT_Y(row);
8159 w = gui.char_width * 2;
8160 h = gui.char_height;
8161 switch (sign->uType)
8162 {
8163 case IMAGE_BITMAP:
8164 {
8165 HDC hdcMem;
8166 HBITMAP hbmpOld;
8167
8168 hdcMem = CreateCompatibleDC(s_hdc);
8169 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8170 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8171 SelectObject(hdcMem, hbmpOld);
8172 DeleteDC(hdcMem);
8173 }
8174 break;
8175 case IMAGE_ICON:
8176 case IMAGE_CURSOR:
8177 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8178 break;
8179#ifdef FEAT_XPM_W32
8180 case IMAGE_XPM:
8181 {
8182 HDC hdcMem;
8183 HBITMAP hbmpOld;
8184
8185 hdcMem = CreateCompatibleDC(s_hdc);
8186 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8187 /* Make hole */
8188 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8189
8190 SelectObject(hdcMem, sign->hImage);
8191 /* Paint sign */
8192 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8193 SelectObject(hdcMem, hbmpOld);
8194 DeleteDC(hdcMem);
8195 }
8196 break;
8197#endif
8198 }
8199}
8200
8201 static void
8202close_signicon_image(signicon_t *sign)
8203{
8204 if (sign)
8205 switch (sign->uType)
8206 {
8207 case IMAGE_BITMAP:
8208 DeleteObject((HGDIOBJ)sign->hImage);
8209 break;
8210 case IMAGE_CURSOR:
8211 DestroyCursor((HCURSOR)sign->hImage);
8212 break;
8213 case IMAGE_ICON:
8214 DestroyIcon((HICON)sign->hImage);
8215 break;
8216#ifdef FEAT_XPM_W32
8217 case IMAGE_XPM:
8218 DeleteObject((HBITMAP)sign->hImage);
8219 DeleteObject((HBITMAP)sign->hShape);
8220 break;
8221#endif
8222 }
8223}
8224
8225 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008226gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227{
8228 signicon_t sign, *psign;
8229 char_u *ext;
8230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008232 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 if (ext > signfile)
8234 {
8235 int do_load = 1;
8236
8237 if (!STRICMP(ext, ".bmp"))
8238 sign.uType = IMAGE_BITMAP;
8239 else if (!STRICMP(ext, ".ico"))
8240 sign.uType = IMAGE_ICON;
8241 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8242 sign.uType = IMAGE_CURSOR;
8243 else
8244 do_load = 0;
8245
8246 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008247 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 gui.char_width * 2, gui.char_height,
8249 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8250#ifdef FEAT_XPM_W32
8251 if (!STRICMP(ext, ".xpm"))
8252 {
8253 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008254 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8255 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 }
8257#endif
8258 }
8259
8260 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008261 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 *psign = sign;
8263
8264 if (!psign)
8265 {
8266 if (sign.hImage)
8267 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008268 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 }
8270 return (void *)psign;
8271
8272}
8273
8274 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008275gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
8277 if (sign)
8278 {
8279 close_signicon_image((signicon_t *)sign);
8280 vim_free(sign);
8281 }
8282}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008285#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286
8287/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008288 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008290 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8292 * to get current mouse position).
8293 *
8294 * Trying to use as more Windows services as possible, and as less
8295 * IE version as possible :)).
8296 *
8297 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8298 * BalloonEval struct.
8299 * 2) Enable/Disable simply create/kill BalloonEval Timer
8300 * 3) When there was enough inactivity, timer procedure posts
8301 * async request to debugger
8302 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8303 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008304 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 */
8306
Bram Moolenaar45360022005-07-21 21:08:21 +00008307/*
8308 * determine whether installed Common Controls support multiline tooltips
8309 * (i.e. their version is >= 4.70
8310 */
8311 int
8312multiline_balloon_available(void)
8313{
8314 HINSTANCE hDll;
8315 static char comctl_dll[] = "comctl32.dll";
8316 static int multiline_tip = MAYBE;
8317
8318 if (multiline_tip != MAYBE)
8319 return multiline_tip;
8320
8321 hDll = GetModuleHandle(comctl_dll);
8322 if (hDll != NULL)
8323 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008324 DLLGETVERSIONPROC pGetVer;
8325 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008326
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008327 if (pGetVer != NULL)
8328 {
8329 DLLVERSIONINFO dvi;
8330 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008331
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008332 ZeroMemory(&dvi, sizeof(dvi));
8333 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008334
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008335 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008336
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008337 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008338 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008339 || (dvi.dwMajorVersion == 4
8340 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008341 {
8342 multiline_tip = TRUE;
8343 return multiline_tip;
8344 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008345 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008346 else
8347 {
8348 /* there is chance we have ancient CommCtl 4.70
8349 which doesn't export DllGetVersion */
8350 DWORD dwHandle = 0;
8351 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8352 if (len > 0)
8353 {
8354 VS_FIXEDFILEINFO *ver;
8355 UINT vlen = 0;
8356 void *data = alloc(len);
8357
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008358 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008359 && GetFileVersionInfo(comctl_dll, 0, len, data)
8360 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8361 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008362 && HIWORD(ver->dwFileVersionMS) > 4)
8363 || ((HIWORD(ver->dwFileVersionMS) == 4
8364 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008365 {
8366 vim_free(data);
8367 multiline_tip = TRUE;
8368 return multiline_tip;
8369 }
8370 vim_free(data);
8371 }
8372 }
8373 }
8374 multiline_tip = FALSE;
8375 return multiline_tip;
8376}
8377
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008378 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008379make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008380{
8381 TOOLINFOW *pti;
8382 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008383
8384 if (multiline_balloon_available() == TRUE)
8385 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8386 else
8387 ToolInfoSize = sizeof(TOOLINFOW);
8388
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008389 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008390 if (pti == NULL)
8391 return;
8392
8393 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8394 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8395 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008396 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008397
8398 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8399 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8400
8401 pti->cbSize = ToolInfoSize;
8402 pti->uFlags = TTF_SUBCLASS;
8403 pti->hwnd = beval->target;
8404 pti->hinst = 0; // Don't use string resources
8405 pti->uId = ID_BEVAL_TOOLTIP;
8406
8407 if (multiline_balloon_available() == TRUE)
8408 {
8409 RECT rect;
8410 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8411 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008412 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8413 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008414 // switch multiline tooltips on
8415 if (GetClientRect(s_textArea, &rect))
8416 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8417 (LPARAM)rect.right);
8418 }
8419 else
8420 {
8421 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008422 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8423 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008424 }
8425
8426 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8427 pti->rect.left = pt.x - 3;
8428 pti->rect.top = pt.y - 3;
8429 pti->rect.right = pt.x + 3;
8430 pti->rect.bottom = pt.y + 3;
8431
8432 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8433 // Make tooltip appear sooner.
8434 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8435 // I've performed some tests and it seems the longest possible life time
8436 // of tooltip is 30 seconds.
8437 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8438 /*
8439 * HACK: force tooltip to appear, because it'll not appear until
8440 * first mouse move. D*mn M$
8441 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8442 */
8443 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8444 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8445 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008446}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008449delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008451 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452}
8453
8454 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008455BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008456 HWND hwnd UNUSED,
8457 UINT uMsg UNUSED,
8458 UINT_PTR idEvent UNUSED,
8459 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 POINT pt;
8462 RECT rect;
8463
8464 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8465 return;
8466
8467 GetCursorPos(&pt);
8468 if (WindowFromPoint(pt) != s_textArea)
8469 return;
8470
8471 ScreenToClient(s_textArea, &pt);
8472 GetClientRect(s_textArea, &rect);
8473 if (!PtInRect(&rect, pt))
8474 return;
8475
8476 if (LastActivity > 0
8477 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8478 && (cur_beval->showState != ShS_PENDING
8479 || abs(cur_beval->x - pt.x) > 3
8480 || abs(cur_beval->y - pt.y) > 3))
8481 {
8482 /* Pointer resting in one place long enough, it's time to show
8483 * the tooltip. */
8484 cur_beval->showState = ShS_PENDING;
8485 cur_beval->x = pt.x;
8486 cur_beval->y = pt.y;
8487
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008488 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 if (cur_beval->msgCB != NULL)
8491 (*cur_beval->msgCB)(cur_beval, 0);
8492 }
8493}
8494
8495 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008496gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008498 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008500 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501}
8502
8503 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008504gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008506 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 if (beval == NULL)
8508 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008509 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008510 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008511 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512}
8513
8514 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008515gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516{
8517 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008518
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008519 vim_free(beval->msg);
8520 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8521 if (beval->msg == NULL)
8522 {
8523 delete_tooltip(beval);
8524 beval->showState = ShS_NEUTRAL;
8525 return;
8526 }
8527
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008528 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 if (beval->showState == ShS_SHOWING)
8530 return;
8531 GetCursorPos(&pt);
8532 ScreenToClient(s_textArea, &pt);
8533
8534 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008536 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 gui_mch_disable_beval_area(cur_beval);
8538 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008539 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008541 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008545gui_mch_create_beval_area(
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008546 void *target UNUSED, /* ignored, always use s_textArea */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008547 char_u *mesg,
8548 void (*mesgCB)(BalloonEval *, int),
8549 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 /* partially stolen from gui_beval.c */
8552 BalloonEval *beval;
8553
8554 if (mesg != NULL && mesgCB != NULL)
8555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008556 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 return NULL;
8558 }
8559
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008560 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (beval != NULL)
8562 {
8563 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
8565 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 beval->msg = mesg;
8567 beval->msgCB = mesgCB;
8568 beval->clientData = clientData;
8569
8570 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 cur_beval = beval;
8572
8573 if (p_beval)
8574 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576 return beval;
8577}
8578
8579 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008580Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
8582 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8583 return;
8584
8585 if (cur_beval != NULL)
8586 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008587 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008589 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008590 // TRACE0("TTN_SHOW {{{");
8591 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008592 break;
8593 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008594 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 delete_tooltip(cur_beval);
8596 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008597 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598
8599 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008600 break;
8601 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008602 {
8603 /* if you get there then we have new common controls */
8604 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8605 info->lpszText = (LPSTR)info->lParam;
8606 info->uFlags |= TTF_DI_SETITEM;
8607 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008608 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008609 case TTN_GETDISPINFOW:
8610 {
8611 // if we get here then we have new common controls
8612 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8613 info->lpszText = (LPWSTR)info->lParam;
8614 info->uFlags |= TTF_DI_SETITEM;
8615 }
8616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 }
8618 }
8619}
8620
8621 static void
8622TrackUserActivity(UINT uMsg)
8623{
8624 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8625 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8626 LastActivity = GetTickCount();
8627}
8628
8629 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008630gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008632#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008633 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008634#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008635 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 vim_free(beval);
8637}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008638#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8641/*
8642 * We have multiple signs to draw at the same location. Draw the
8643 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8644 */
8645 void
8646netbeans_draw_multisign_indicator(int row)
8647{
8648 int i;
8649 int y;
8650 int x;
8651
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008652 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008653 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008654
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 x = 0;
8656 y = TEXT_Y(row);
8657
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008658#if defined(FEAT_DIRECTX)
8659 if (IS_ENABLE_DIRECTX())
8660 DWriteContext_Flush(s_dwc);
8661#endif
8662
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 for (i = 0; i < gui.char_height - 3; i++)
8664 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8665
8666 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8667 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8668 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8669 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8670 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8671 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8672 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8673}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008674#endif