blob: cdde3c2bcb2ae1d0a3eeca8bc0c14973a00347a8 [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;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100328static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329
330#ifdef FEAT_TOOLBAR
331static HWND s_toolbarhwnd = NULL;
332static WNDPROC s_toolbar_wndproc = NULL;
333#endif
334
335#ifdef FEAT_GUI_TABLINE
336static HWND s_tabhwnd = NULL;
337static WNDPROC s_tabline_wndproc = NULL;
338static int showing_tabline = 0;
339#endif
340
341static WPARAM s_wParam = 0;
342static LPARAM s_lParam = 0;
343
344static HWND s_textArea = NULL;
345static UINT s_uMsg = 0;
346
347static char_u *s_textfield; /* Used by dialogs to pass back strings */
348
349static int s_need_activate = FALSE;
350
351/* This variable is set when waiting for an event, which is the only moment
352 * scrollbar dragging can be done directly. It's not allowed while commands
353 * are executed, because it may move the cursor and that may cause unexpected
354 * problems (e.g., while ":s" is working).
355 */
356static int allow_scrollbar = FALSE;
357
358#ifdef GLOBAL_IME
359# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
360#else
361# define MyTranslateMessage(x) TranslateMessage(x)
362#endif
363
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100364#if defined(FEAT_DIRECTX)
365 static int
366directx_enabled(void)
367{
368 if (s_dwc != NULL)
369 return 1;
370 else if (s_directx_load_attempted)
371 return 0;
372 /* load DirectX */
373 DWrite_Init();
374 s_directx_load_attempted = 1;
375 s_dwc = DWriteContext_Open();
376 directx_binddc();
377 return s_dwc != NULL ? 1 : 0;
378}
379
380 static void
381directx_binddc(void)
382{
383 if (s_textArea != NULL)
384 {
385 RECT rect;
386 GetClientRect(s_textArea, &rect);
387 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
388 }
389}
390#endif
391
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200392/* use of WindowProc depends on Global IME */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100393#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100394
395extern int current_font_height; /* this is in os_mswin.c */
396
397static struct
398{
399 UINT key_sym;
400 char_u vim_code0;
401 char_u vim_code1;
402} special_keys[] =
403{
404 {VK_UP, 'k', 'u'},
405 {VK_DOWN, 'k', 'd'},
406 {VK_LEFT, 'k', 'l'},
407 {VK_RIGHT, 'k', 'r'},
408
409 {VK_F1, 'k', '1'},
410 {VK_F2, 'k', '2'},
411 {VK_F3, 'k', '3'},
412 {VK_F4, 'k', '4'},
413 {VK_F5, 'k', '5'},
414 {VK_F6, 'k', '6'},
415 {VK_F7, 'k', '7'},
416 {VK_F8, 'k', '8'},
417 {VK_F9, 'k', '9'},
418 {VK_F10, 'k', ';'},
419
420 {VK_F11, 'F', '1'},
421 {VK_F12, 'F', '2'},
422 {VK_F13, 'F', '3'},
423 {VK_F14, 'F', '4'},
424 {VK_F15, 'F', '5'},
425 {VK_F16, 'F', '6'},
426 {VK_F17, 'F', '7'},
427 {VK_F18, 'F', '8'},
428 {VK_F19, 'F', '9'},
429 {VK_F20, 'F', 'A'},
430
431 {VK_F21, 'F', 'B'},
432#ifdef FEAT_NETBEANS_INTG
433 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
434#endif
435 {VK_F22, 'F', 'C'},
436 {VK_F23, 'F', 'D'},
437 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
438
439 {VK_HELP, '%', '1'},
440 {VK_BACK, 'k', 'b'},
441 {VK_INSERT, 'k', 'I'},
442 {VK_DELETE, 'k', 'D'},
443 {VK_HOME, 'k', 'h'},
444 {VK_END, '@', '7'},
445 {VK_PRIOR, 'k', 'P'},
446 {VK_NEXT, 'k', 'N'},
447 {VK_PRINT, '%', '9'},
448 {VK_ADD, 'K', '6'},
449 {VK_SUBTRACT, 'K', '7'},
450 {VK_DIVIDE, 'K', '8'},
451 {VK_MULTIPLY, 'K', '9'},
452 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
453 {VK_DECIMAL, 'K', 'B'},
454
455 {VK_NUMPAD0, 'K', 'C'},
456 {VK_NUMPAD1, 'K', 'D'},
457 {VK_NUMPAD2, 'K', 'E'},
458 {VK_NUMPAD3, 'K', 'F'},
459 {VK_NUMPAD4, 'K', 'G'},
460 {VK_NUMPAD5, 'K', 'H'},
461 {VK_NUMPAD6, 'K', 'I'},
462 {VK_NUMPAD7, 'K', 'J'},
463 {VK_NUMPAD8, 'K', 'K'},
464 {VK_NUMPAD9, 'K', 'L'},
465
466 /* Keys that we want to be able to use any modifier with: */
467 {VK_SPACE, ' ', NUL},
468 {VK_TAB, TAB, NUL},
469 {VK_ESCAPE, ESC, NUL},
470 {NL, NL, NUL},
471 {CAR, CAR, NUL},
472
473 /* End of list marker: */
474 {0, 0, 0}
475};
476
477/* Local variables */
478static int s_button_pending = -1;
479
480/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
481 * so don't reset s_button_pending. */
482static int s_getting_focus = FALSE;
483
484static int s_x_pending;
485static int s_y_pending;
486static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200487static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100488static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200489static int dead_key = 0; // 0: no dead key, 1: dead key pressed
490static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
491 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100493#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494/* balloon-eval WM_NOTIFY_HANDLER */
495static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
496static void TrackUserActivity(UINT uMsg);
497#endif
498
499/*
500 * For control IME.
501 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100502 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100503 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100505/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
506static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100507#endif
508#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100509/* holds LOGFONTW for 'guifont' always. */
510static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511#endif
512
513#ifdef FEAT_MBYTE_IME
514static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
515#endif
516
517#if defined(FEAT_BROWSE)
518static char_u *convert_filter(char_u *s);
519#endif
520
521#ifdef DEBUG_PRINT_ERROR
522/*
523 * Print out the last Windows error message
524 */
525 static void
526print_windows_error(void)
527{
528 LPVOID lpMsgBuf;
529
530 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
531 NULL, GetLastError(),
532 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
533 (LPTSTR) &lpMsgBuf, 0, NULL);
534 TRACE1("Error: %s\n", lpMsgBuf);
535 LocalFree(lpMsgBuf);
536}
537#endif
538
539/*
540 * Cursor blink functions.
541 *
542 * This is a simple state machine:
543 * BLINK_NONE not blinking at all
544 * BLINK_OFF blinking, cursor is not shown
545 * BLINK_ON blinking, cursor is shown
546 */
547
548#define BLINK_NONE 0
549#define BLINK_OFF 1
550#define BLINK_ON 2
551
552static int blink_state = BLINK_NONE;
553static long_u blink_waittime = 700;
554static long_u blink_ontime = 400;
555static long_u blink_offtime = 250;
556static UINT blink_timer = 0;
557
Bram Moolenaar703a8042016-06-04 16:24:32 +0200558 int
559gui_mch_is_blinking(void)
560{
561 return blink_state != BLINK_NONE;
562}
563
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200564 int
565gui_mch_is_blink_off(void)
566{
567 return blink_state == BLINK_OFF;
568}
569
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100570 void
571gui_mch_set_blinking(long wait, long on, long off)
572{
573 blink_waittime = wait;
574 blink_ontime = on;
575 blink_offtime = off;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 static VOID CALLBACK
579_OnBlinkTimer(
580 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100581 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100583 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100584{
585 MSG msg;
586
587 /*
588 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
589 */
590
591 KillTimer(NULL, idEvent);
592
593 /* Eat spurious WM_TIMER messages */
594 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
595 ;
596
597 if (blink_state == BLINK_ON)
598 {
599 gui_undraw_cursor();
600 blink_state = BLINK_OFF;
601 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
602 (TIMERPROC)_OnBlinkTimer);
603 }
604 else
605 {
606 gui_update_cursor(TRUE, FALSE);
607 blink_state = BLINK_ON;
608 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100609 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100610 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100611 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612}
613
614 static void
615gui_mswin_rm_blink_timer(void)
616{
617 MSG msg;
618
619 if (blink_timer != 0)
620 {
621 KillTimer(NULL, blink_timer);
622 /* Eat spurious WM_TIMER messages */
623 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
624 ;
625 blink_timer = 0;
626 }
627}
628
629/*
630 * Stop the cursor blinking. Show the cursor if it wasn't shown.
631 */
632 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100634{
635 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100636 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100637 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100639 gui_mch_flush();
640 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100641 blink_state = BLINK_NONE;
642}
643
644/*
645 * Start the cursor blinking. If it was already blinking, this restarts the
646 * waiting time and shows the cursor.
647 */
648 void
649gui_mch_start_blink(void)
650{
651 gui_mswin_rm_blink_timer();
652
653 /* Only switch blinking on if none of the times is zero */
654 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
655 {
656 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
657 (TIMERPROC)_OnBlinkTimer);
658 blink_state = BLINK_ON;
659 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100660 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100661 }
662}
663
664/*
665 * Call-back routines.
666 */
667
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100668 static VOID CALLBACK
669_OnTimer(
670 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100671 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100672 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100673 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674{
675 MSG msg;
676
677 /*
678 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
679 */
680 KillTimer(NULL, idEvent);
681 s_timed_out = TRUE;
682
683 /* Eat spurious WM_TIMER messages */
684 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
685 ;
686 if (idEvent == s_wait_timer)
687 s_wait_timer = 0;
688}
689
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 static void
691_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100692 HWND hwnd UNUSED,
693 UINT ch UNUSED,
694 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100695{
696 dead_key = 1;
697}
698
699/*
700 * Convert Unicode character "ch" to bytes in "string[slen]".
701 * When "had_alt" is TRUE the ALT key was included in "ch".
702 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200703 * Because the Windows API uses UTF-16, we have to deal with surrogate
704 * pairs; this is where we choose to deal with them: if "ch" is a high
705 * surrogate, it will be stored, and the length returned will be zero; the next
706 * char_to_string call will then include the high surrogate, decoding the pair
707 * of UTF-16 code units to a single Unicode code point, presuming it is the
708 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 */
710 static int
711char_to_string(int ch, char_u *string, int slen, int had_alt)
712{
713 int len;
714 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200716 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200718 if (surrogate_pending_ch != 0)
719 {
720 /* We don't guarantee ch is a low surrogate to match the high surrogate
721 * we already have; it should be, but if it isn't, tough luck. */
722 wstring[0] = surrogate_pending_ch;
723 wstring[1] = ch;
724 surrogate_pending_ch = 0;
725 len = 2;
726 }
727 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
728 {
729 /* We don't have the entire code point yet, only the first UTF-16 code
730 * unit; so just remember it and use it in the next call. */
731 surrogate_pending_ch = ch;
732 return 0;
733 }
734 else
735 {
736 wstring[0] = ch;
737 len = 1;
738 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200739
740 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
741 * "enc_codepage" is non-zero use the standard Win32 function,
742 * otherwise use our own conversion function (e.g., for UTF-8). */
743 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200745 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
746 (LPSTR)string, slen, 0, NULL);
747 /* If we had included the ALT key into the character but now the
748 * upper bit is no longer set, that probably means the conversion
749 * failed. Convert the original character and set the upper bit
750 * afterwards. */
751 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200753 wstring[0] = ch & 0x7f;
754 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
755 (LPSTR)string, slen, 0, NULL);
756 if (len == 1) /* safety check */
757 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100758 }
759 }
760 else
761 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200762 ws = utf16_to_enc(wstring, &len);
763 if (ws == NULL)
764 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 else
766 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200767 if (len > slen) /* just in case */
768 len = slen;
769 mch_memmove(string, ws, len);
770 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773
774 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 {
776 string[0] = ch;
777 len = 1;
778 }
779
780 for (i = 0; i < len; ++i)
781 if (string[i] == CSI && len <= slen - 2)
782 {
783 /* Insert CSI as K_CSI. */
784 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
785 string[++i] = KS_EXTRA;
786 string[++i] = (int)KE_CSI;
787 len += 2;
788 }
789
790 return len;
791}
792
793/*
794 * Key hit, add it to the input buffer.
795 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 static void
797_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100798 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100799 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801{
802 char_u string[40];
803 int len = 0;
804
805 dead_key = 0;
806
807 len = char_to_string(ch, string, 40, FALSE);
808 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
809 {
810 trash_input_buf();
811 got_int = TRUE;
812 }
813
814 add_to_input_buf(string, len);
815}
816
817/*
818 * Alt-Key hit, add it to the input buffer.
819 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 static void
821_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100822 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100823 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825{
826 char_u string[40]; /* Enough for multibyte character */
827 int len;
828 int modifiers;
829 int ch = cch; /* special keys are negative */
830
831 dead_key = 0;
832
833 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
834
835 /* OK, we have a character key (given by ch) which was entered with the
836 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
837 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
838 * CAPSLOCK is pressed) at this point.
839 */
840 modifiers = MOD_MASK_ALT;
841 if (GetKeyState(VK_SHIFT) & 0x8000)
842 modifiers |= MOD_MASK_SHIFT;
843 if (GetKeyState(VK_CONTROL) & 0x8000)
844 modifiers |= MOD_MASK_CTRL;
845
846 ch = simplify_key(ch, &modifiers);
847 /* remove the SHIFT modifier for keys where it's already included, e.g.,
848 * '(' and '*' */
849 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
850 modifiers &= ~MOD_MASK_SHIFT;
851
852 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
Bram Moolenaar459fd782019-10-13 16:43:39 +0200853 ch = extract_modifiers(ch, &modifiers, TRUE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100854 if (ch == CSI)
855 ch = K_CSI;
856
857 len = 0;
858 if (modifiers)
859 {
860 string[len++] = CSI;
861 string[len++] = KS_MODIFIER;
862 string[len++] = modifiers;
863 }
864
865 if (IS_SPECIAL((int)ch))
866 {
867 string[len++] = CSI;
868 string[len++] = K_SECOND((int)ch);
869 string[len++] = K_THIRD((int)ch);
870 }
871 else
872 {
873 /* Although the documentation isn't clear about it, we assume "ch" is
874 * a Unicode character. */
875 len += char_to_string(ch, string + len, 40 - len, TRUE);
876 }
877
878 add_to_input_buf(string, len);
879}
880
881 static void
882_OnMouseEvent(
883 int button,
884 int x,
885 int y,
886 int repeated_click,
887 UINT keyFlags)
888{
889 int vim_modifiers = 0x0;
890
891 s_getting_focus = FALSE;
892
893 if (keyFlags & MK_SHIFT)
894 vim_modifiers |= MOUSE_SHIFT;
895 if (keyFlags & MK_CONTROL)
896 vim_modifiers |= MOUSE_CTRL;
897 if (GetKeyState(VK_MENU) & 0x8000)
898 vim_modifiers |= MOUSE_ALT;
899
900 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
901}
902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 static void
904_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905 HWND hwnd UNUSED,
906 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100907 int x,
908 int y,
909 UINT keyFlags)
910{
911 static LONG s_prevTime = 0;
912
913 LONG currentTime = GetMessageTime();
914 int button = -1;
915 int repeated_click;
916
917 /* Give main window the focus: this is so the cursor isn't hollow. */
918 (void)SetFocus(s_hwnd);
919
920 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
921 button = MOUSE_LEFT;
922 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
923 button = MOUSE_MIDDLE;
924 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
925 button = MOUSE_RIGHT;
926 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
927 {
928#ifndef GET_XBUTTON_WPARAM
929# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
930#endif
931 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
932 }
933 else if (s_uMsg == WM_CAPTURECHANGED)
934 {
935 /* on W95/NT4, somehow you get in here with an odd Msg
936 * if you press one button while holding down the other..*/
937 if (s_button_pending == MOUSE_LEFT)
938 button = MOUSE_RIGHT;
939 else
940 button = MOUSE_LEFT;
941 }
942 if (button >= 0)
943 {
944 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
945
946 /*
947 * Holding down the left and right buttons simulates pushing the middle
948 * button.
949 */
950 if (repeated_click
951 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
952 || (button == MOUSE_RIGHT
953 && s_button_pending == MOUSE_LEFT)))
954 {
955 /*
956 * Hmm, gui.c will ignore more than one button down at a time, so
957 * pretend we let go of it first.
958 */
959 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
960 button = MOUSE_MIDDLE;
961 repeated_click = FALSE;
962 s_button_pending = -1;
963 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
964 }
965 else if ((repeated_click)
966 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
967 {
968 if (s_button_pending > -1)
969 {
970 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
971 s_button_pending = -1;
972 }
973 /* TRACE("Button down at x %d, y %d\n", x, y); */
974 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
975 }
976 else
977 {
978 /*
979 * If this is the first press (i.e. not a multiple click) don't
980 * action immediately, but store and wait for:
981 * i) button-up
982 * ii) mouse move
983 * iii) another button press
984 * before using it.
985 * This enables us to make left+right simulate middle button,
986 * without left or right being actioned first. The side-effect is
987 * that if you click and hold the mouse without dragging, the
988 * cursor doesn't move until you release the button. In practice
989 * this is hardly a problem.
990 */
991 s_button_pending = button;
992 s_x_pending = x;
993 s_y_pending = y;
994 s_kFlags_pending = keyFlags;
995 }
996
997 s_prevTime = currentTime;
998 }
999}
1000
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 static void
1002_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001003 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001004 int x,
1005 int y,
1006 UINT keyFlags)
1007{
1008 int button;
1009
1010 s_getting_focus = FALSE;
1011 if (s_button_pending > -1)
1012 {
1013 /* Delayed action for mouse down event */
1014 _OnMouseEvent(s_button_pending, s_x_pending,
1015 s_y_pending, FALSE, s_kFlags_pending);
1016 s_button_pending = -1;
1017 }
1018 if (s_uMsg == WM_MOUSEMOVE)
1019 {
1020 /*
1021 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1022 * down.
1023 */
1024 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1025 | MK_XBUTTON1 | MK_XBUTTON2)))
1026 {
1027 gui_mouse_moved(x, y);
1028 return;
1029 }
1030
1031 /*
1032 * While button is down, keep grabbing mouse move events when
1033 * the mouse goes outside the window
1034 */
1035 SetCapture(s_textArea);
1036 button = MOUSE_DRAG;
1037 /* TRACE(" move at x %d, y %d\n", x, y); */
1038 }
1039 else
1040 {
1041 ReleaseCapture();
1042 button = MOUSE_RELEASE;
1043 /* TRACE(" up at x %d, y %d\n", x, y); */
1044 }
1045
1046 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1047}
1048
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001049 static void
1050_OnSizeTextArea(
1051 HWND hwnd UNUSED,
1052 UINT state UNUSED,
1053 int cx UNUSED,
1054 int cy UNUSED)
1055{
1056#if defined(FEAT_DIRECTX)
1057 if (IS_ENABLE_DIRECTX())
1058 directx_binddc();
1059#endif
1060}
1061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001062#ifdef FEAT_MENU
1063/*
1064 * Find the vimmenu_T with the given id
1065 */
1066 static vimmenu_T *
1067gui_mswin_find_menu(
1068 vimmenu_T *pMenu,
1069 int id)
1070{
1071 vimmenu_T *pChildMenu;
1072
1073 while (pMenu)
1074 {
1075 if (pMenu->id == (UINT)id)
1076 break;
1077 if (pMenu->children != NULL)
1078 {
1079 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1080 if (pChildMenu)
1081 {
1082 pMenu = pChildMenu;
1083 break;
1084 }
1085 }
1086 pMenu = pMenu->next;
1087 }
1088 return pMenu;
1089}
1090
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001091 static void
1092_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwndCtl UNUSED,
1096 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001097{
1098 vimmenu_T *pMenu;
1099
1100 pMenu = gui_mswin_find_menu(root_menu, id);
1101 if (pMenu)
1102 gui_menu_cb(pMenu);
1103}
1104#endif
1105
1106#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001107/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 * Handle a Find/Replace window message.
1109 */
1110 static void
1111_OnFindRepl(void)
1112{
1113 int flags = 0;
1114 int down;
1115
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001116 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1117 /* Give main window the focus back. */
1118 (void)SetFocus(s_hwnd);
1119
1120 if (s_findrep_struct.Flags & FR_FINDNEXT)
1121 {
1122 flags = FRD_FINDNEXT;
1123
1124 /* Give main window the focus back: this is so the cursor isn't
1125 * hollow. */
1126 (void)SetFocus(s_hwnd);
1127 }
1128 else if (s_findrep_struct.Flags & FR_REPLACE)
1129 {
1130 flags = FRD_REPLACE;
1131
1132 /* Give main window the focus back: this is so the cursor isn't
1133 * hollow. */
1134 (void)SetFocus(s_hwnd);
1135 }
1136 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1137 {
1138 flags = FRD_REPLACEALL;
1139 }
1140
1141 if (flags != 0)
1142 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001143 char_u *p, *q;
1144
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 /* Call the generic GUI function to do the actual work. */
1146 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1147 flags |= FRD_WHOLE_WORD;
1148 if (s_findrep_struct.Flags & FR_MATCHCASE)
1149 flags |= FRD_MATCH_CASE;
1150 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001151 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1152 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1153 if (p != NULL && q != NULL)
1154 gui_do_findrepl(flags, p, q, down);
1155 vim_free(p);
1156 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001157 }
1158}
1159#endif
1160
1161 static void
1162HandleMouseHide(UINT uMsg, LPARAM lParam)
1163{
1164 static LPARAM last_lParam = 0L;
1165
1166 /* We sometimes get a mousemove when the mouse didn't move... */
1167 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1168 {
1169 if (lParam == last_lParam)
1170 return;
1171 last_lParam = lParam;
1172 }
1173
1174 /* Handle specially, to centralise coding. We need to be sure we catch all
1175 * possible events which should cause us to restore the cursor (as it is a
1176 * shared resource, we take full responsibility for it).
1177 */
1178 switch (uMsg)
1179 {
1180 case WM_KEYUP:
1181 case WM_CHAR:
1182 /*
1183 * blank out the pointer if necessary
1184 */
1185 if (p_mh)
1186 gui_mch_mousehide(TRUE);
1187 break;
1188
1189 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1190 case WM_SYSCHAR:
1191 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1192 case WM_LBUTTONDOWN:
1193 case WM_LBUTTONUP:
1194 case WM_MBUTTONDOWN:
1195 case WM_MBUTTONUP:
1196 case WM_RBUTTONDOWN:
1197 case WM_RBUTTONUP:
1198 case WM_XBUTTONDOWN:
1199 case WM_XBUTTONUP:
1200 case WM_NCMOUSEMOVE:
1201 case WM_NCLBUTTONDOWN:
1202 case WM_NCLBUTTONUP:
1203 case WM_NCMBUTTONDOWN:
1204 case WM_NCMBUTTONUP:
1205 case WM_NCRBUTTONDOWN:
1206 case WM_NCRBUTTONUP:
1207 case WM_KILLFOCUS:
1208 /*
1209 * if the pointer is currently hidden, then we should show it.
1210 */
1211 gui_mch_mousehide(FALSE);
1212 break;
1213 }
1214}
1215
1216 static LRESULT CALLBACK
1217_TextAreaWndProc(
1218 HWND hwnd,
1219 UINT uMsg,
1220 WPARAM wParam,
1221 LPARAM lParam)
1222{
1223 /*
1224 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1225 hwnd, uMsg, wParam, lParam);
1226 */
1227
1228 HandleMouseHide(uMsg, lParam);
1229
1230 s_uMsg = uMsg;
1231 s_wParam = wParam;
1232 s_lParam = lParam;
1233
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001234#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 TrackUserActivity(uMsg);
1236#endif
1237
1238 switch (uMsg)
1239 {
1240 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1241 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1242 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1243 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1247 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1248 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1249 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1250 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1251 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1252 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1253 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001254 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001255
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001256#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001257 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1258 return TRUE;
1259#endif
1260 default:
1261 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1262 }
1263}
1264
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001267#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268
1269 LRESULT WINAPI
1270vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1271{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001272#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001274#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001275 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001276#endif
1277}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278
1279/*
1280 * Called when the foreground or background color has been changed.
1281 */
1282 void
1283gui_mch_new_colors(void)
1284{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001285 HBRUSH prevBrush;
1286
1287 s_brush = CreateSolidBrush(gui.back_pixel);
1288#ifdef SetClassLongPtr
1289 prevBrush = (HBRUSH)SetClassLongPtr(
1290 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
1291#else
1292 prevBrush = (HBRUSH)SetClassLong(
1293 s_hwnd, GCL_HBRBACKGROUND, (long_u)s_brush);
1294#endif
1295 InvalidateRect(s_hwnd, NULL, TRUE);
1296 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297}
1298
1299/*
1300 * Set the colors to their default values.
1301 */
1302 void
1303gui_mch_def_colors(void)
1304{
1305 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1306 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1307 gui.def_norm_pixel = gui.norm_pixel;
1308 gui.def_back_pixel = gui.back_pixel;
1309}
1310
1311/*
1312 * Open the GUI window which was created by a call to gui_mch_init().
1313 */
1314 int
1315gui_mch_open(void)
1316{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001317 /* Actually open the window, if not already visible
1318 * (may be done already in gui_mch_set_shellsize) */
1319 if (!IsWindowVisible(s_hwnd))
1320 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1321
1322#ifdef MSWIN_FIND_REPLACE
1323 /* Init replace string here, so that we keep it when re-opening the
1324 * dialog. */
1325 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1326#endif
1327
1328 return OK;
1329}
1330
1331/*
1332 * Get the position of the top left corner of the window.
1333 */
1334 int
1335gui_mch_get_winpos(int *x, int *y)
1336{
1337 RECT rect;
1338
1339 GetWindowRect(s_hwnd, &rect);
1340 *x = rect.left;
1341 *y = rect.top;
1342 return OK;
1343}
1344
1345/*
1346 * Set the position of the top left corner of the window to the given
1347 * coordinates.
1348 */
1349 void
1350gui_mch_set_winpos(int x, int y)
1351{
1352 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1353 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1354}
1355 void
1356gui_mch_set_text_area_pos(int x, int y, int w, int h)
1357{
1358 static int oldx = 0;
1359 static int oldy = 0;
1360
1361 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1362
1363#ifdef FEAT_TOOLBAR
1364 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1365 SendMessage(s_toolbarhwnd, WM_SIZE,
1366 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1367#endif
1368#if defined(FEAT_GUI_TABLINE)
1369 if (showing_tabline)
1370 {
1371 int top = 0;
1372 RECT rect;
1373
1374# ifdef FEAT_TOOLBAR
1375 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1376 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1377# endif
1378 GetClientRect(s_hwnd, &rect);
1379 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1380 }
1381#endif
1382
1383 /* When side scroll bar is unshown, the size of window will change.
1384 * then, the text area move left or right. thus client rect should be
1385 * forcedly redrawn. (Yasuhiro Matsumoto) */
1386 if (oldx != x || oldy != y)
1387 {
1388 InvalidateRect(s_hwnd, NULL, FALSE);
1389 oldx = x;
1390 oldy = y;
1391 }
1392}
1393
1394
1395/*
1396 * Scrollbar stuff:
1397 */
1398
1399 void
1400gui_mch_enable_scrollbar(
1401 scrollbar_T *sb,
1402 int flag)
1403{
1404 ShowScrollBar(sb->id, SB_CTL, flag);
1405
1406 /* TODO: When the window is maximized, the size of the window stays the
1407 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1408 * NT 4.0 it's not... */
1409}
1410
1411 void
1412gui_mch_set_scrollbar_pos(
1413 scrollbar_T *sb,
1414 int x,
1415 int y,
1416 int w,
1417 int h)
1418{
1419 SetWindowPos(sb->id, NULL, x, y, w, h,
1420 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1421}
1422
1423 void
1424gui_mch_create_scrollbar(
1425 scrollbar_T *sb,
1426 int orient) /* SBAR_VERT or SBAR_HORIZ */
1427{
1428 sb->id = CreateWindow(
1429 "SCROLLBAR", "Scrollbar",
1430 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1431 10, /* Any value will do for now */
1432 10, /* Any value will do for now */
1433 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001434 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001435}
1436
1437/*
1438 * Find the scrollbar with the given hwnd.
1439 */
1440 static scrollbar_T *
1441gui_mswin_find_scrollbar(HWND hwnd)
1442{
1443 win_T *wp;
1444
1445 if (gui.bottom_sbar.id == hwnd)
1446 return &gui.bottom_sbar;
1447 FOR_ALL_WINDOWS(wp)
1448 {
1449 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1450 return &wp->w_scrollbars[SBAR_LEFT];
1451 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1452 return &wp->w_scrollbars[SBAR_RIGHT];
1453 }
1454 return NULL;
1455}
1456
1457/*
1458 * Get the character size of a font.
1459 */
1460 static void
1461GetFontSize(GuiFont font)
1462{
1463 HWND hwnd = GetDesktopWindow();
1464 HDC hdc = GetWindowDC(hwnd);
1465 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001466 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001467 TEXTMETRIC tm;
1468
1469 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001470 // GetTextMetrics() may not return the right value in tmAveCharWidth
1471 // for some fonts. Do our own average computation.
1472 GetTextExtentPoint(hdc,
1473 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1474 52, &size);
1475 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001476
1477 gui.char_height = tm.tmHeight + p_linespace;
1478
1479 SelectFont(hdc, hfntOld);
1480
1481 ReleaseDC(hwnd, hdc);
1482}
1483
1484/*
1485 * Adjust gui.char_height (after 'linespace' was changed).
1486 */
1487 int
1488gui_mch_adjust_charheight(void)
1489{
1490 GetFontSize(gui.norm_font);
1491 return OK;
1492}
1493
1494 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001495get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001496{
1497 HFONT font = NULL;
1498
1499 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001500 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501
1502 if (font == NULL)
1503 return NOFONT;
1504
1505 return (GuiFont)font;
1506}
1507
1508 static int
1509pixels_to_points(int pixels, int vertical)
1510{
1511 int points;
1512 HWND hwnd;
1513 HDC hdc;
1514
1515 hwnd = GetDesktopWindow();
1516 hdc = GetWindowDC(hwnd);
1517
1518 points = MulDiv(pixels, 72,
1519 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1520
1521 ReleaseDC(hwnd, hdc);
1522
1523 return points;
1524}
1525
1526 GuiFont
1527gui_mch_get_font(
1528 char_u *name,
1529 int giveErrorIfMissing)
1530{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001531 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532 GuiFont font = NOFONT;
1533
1534 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1535 font = get_font_handle(&lf);
1536 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001537 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538 return font;
1539}
1540
1541#if defined(FEAT_EVAL) || defined(PROTO)
1542/*
1543 * Return the name of font "font" in allocated memory.
1544 * Don't know how to get the actual name, thus use the provided name.
1545 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001546 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001547gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001548{
1549 if (name == NULL)
1550 return NULL;
1551 return vim_strsave(name);
1552}
1553#endif
1554
1555 void
1556gui_mch_free_font(GuiFont font)
1557{
1558 if (font)
1559 DeleteObject((HFONT)font);
1560}
1561
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562/*
1563 * Return the Pixel value (color) for the given color name.
1564 * Return INVALCOLOR for error.
1565 */
1566 guicolor_T
1567gui_mch_get_color(char_u *name)
1568{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001569 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001570
1571 typedef struct SysColorTable
1572 {
1573 char *name;
1574 int color;
1575 } SysColorTable;
1576
1577 static SysColorTable sys_table[] =
1578 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001579 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1580 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001581#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1583#endif
1584 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1585 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1586 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1587 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1588 {"SYS_DESKTOP", COLOR_DESKTOP},
1589 {"SYS_INFOBK", COLOR_INFOBK},
1590 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1591 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001592 {"SYS_BTNFACE", COLOR_BTNFACE},
1593 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1594 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1595 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1596 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1597 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1598 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1599 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1600 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1601 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1602 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1603 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1604 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1605 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1606 {"SYS_MENU", COLOR_MENU},
1607 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1608 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1609 {"SYS_WINDOW", COLOR_WINDOW},
1610 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1611 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1612 };
1613
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001614 /*
1615 * Try to look up a system colour.
1616 */
1617 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1618 if (STRICMP(name, sys_table[i].name) == 0)
1619 return GetSysColor(sys_table[i].color);
1620
Bram Moolenaarab302212016-04-26 20:59:29 +02001621 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001623
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001624 guicolor_T
1625gui_mch_get_rgb_color(int r, int g, int b)
1626{
1627 return gui_get_rgb_color_cmn(r, g, b);
1628}
1629
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630/*
1631 * Return OK if the key with the termcap name "name" is supported.
1632 */
1633 int
1634gui_mch_haskey(char_u *name)
1635{
1636 int i;
1637
1638 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1639 if (name[0] == special_keys[i].vim_code0 &&
1640 name[1] == special_keys[i].vim_code1)
1641 return OK;
1642 return FAIL;
1643}
1644
1645 void
1646gui_mch_beep(void)
1647{
1648 MessageBeep(MB_OK);
1649}
1650/*
1651 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1652 */
1653 void
1654gui_mch_invert_rectangle(
1655 int r,
1656 int c,
1657 int nr,
1658 int nc)
1659{
1660 RECT rc;
1661
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001662#if defined(FEAT_DIRECTX)
1663 if (IS_ENABLE_DIRECTX())
1664 DWriteContext_Flush(s_dwc);
1665#endif
1666
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001667 /*
1668 * Note: InvertRect() excludes right and bottom of rectangle.
1669 */
1670 rc.left = FILL_X(c);
1671 rc.top = FILL_Y(r);
1672 rc.right = rc.left + nc * gui.char_width;
1673 rc.bottom = rc.top + nr * gui.char_height;
1674 InvertRect(s_hdc, &rc);
1675}
1676
1677/*
1678 * Iconify the GUI window.
1679 */
1680 void
1681gui_mch_iconify(void)
1682{
1683 ShowWindow(s_hwnd, SW_MINIMIZE);
1684}
1685
1686/*
1687 * Draw a cursor without focus.
1688 */
1689 void
1690gui_mch_draw_hollow_cursor(guicolor_T color)
1691{
1692 HBRUSH hbr;
1693 RECT rc;
1694
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001695#if defined(FEAT_DIRECTX)
1696 if (IS_ENABLE_DIRECTX())
1697 DWriteContext_Flush(s_dwc);
1698#endif
1699
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 /*
1701 * Note: FrameRect() excludes right and bottom of rectangle.
1702 */
1703 rc.left = FILL_X(gui.col);
1704 rc.top = FILL_Y(gui.row);
1705 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001706 if (mb_lefthalve(gui.row, gui.col))
1707 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708 rc.bottom = rc.top + gui.char_height;
1709 hbr = CreateSolidBrush(color);
1710 FrameRect(s_hdc, &rc, hbr);
1711 DeleteBrush(hbr);
1712}
1713/*
1714 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1715 * color "color".
1716 */
1717 void
1718gui_mch_draw_part_cursor(
1719 int w,
1720 int h,
1721 guicolor_T color)
1722{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001723 RECT rc;
1724
1725 /*
1726 * Note: FillRect() excludes right and bottom of rectangle.
1727 */
1728 rc.left =
1729#ifdef FEAT_RIGHTLEFT
1730 /* vertical line should be on the right of current point */
1731 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1732#endif
1733 FILL_X(gui.col);
1734 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1735 rc.right = rc.left + w;
1736 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001737
Bram Moolenaar92467d32017-12-05 13:22:16 +01001738 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001739}
1740
1741
1742/*
1743 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1744 * dead key's nominal character and re-post the original message.
1745 */
1746 static void
1747outputDeadKey_rePost(MSG originalMsg)
1748{
1749 static MSG deadCharExpel;
1750
1751 if (!dead_key)
1752 return;
1753
1754 dead_key = 0;
1755
1756 /* Make Windows generate the dead key's character */
1757 deadCharExpel.message = originalMsg.message;
1758 deadCharExpel.hwnd = originalMsg.hwnd;
1759 deadCharExpel.wParam = VK_SPACE;
1760
1761 MyTranslateMessage(&deadCharExpel);
1762
1763 /* re-generate the current character free of the dead char influence */
1764 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1765 originalMsg.lParam);
1766}
1767
1768
1769/*
1770 * Process a single Windows message.
1771 * If one is not available we hang until one is.
1772 */
1773 static void
1774process_message(void)
1775{
1776 MSG msg;
1777 UINT vk = 0; /* Virtual key */
1778 char_u string[40];
1779 int i;
1780 int modifiers = 0;
1781 int key;
1782#ifdef FEAT_MENU
1783 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1784#endif
1785
1786 pGetMessage(&msg, NULL, 0, 0);
1787
1788#ifdef FEAT_OLE
1789 /* Look after OLE Automation commands */
1790 if (msg.message == WM_OLE)
1791 {
1792 char_u *str = (char_u *)msg.lParam;
1793 if (str == NULL || *str == NUL)
1794 {
1795 /* Message can't be ours, forward it. Fixes problem with Ultramon
1796 * 3.0.4 */
1797 pDispatchMessage(&msg);
1798 }
1799 else
1800 {
1801 add_to_input_buf(str, (int)STRLEN(str));
1802 vim_free(str); /* was allocated in CVim::SendKeys() */
1803 }
1804 return;
1805 }
1806#endif
1807
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001808#ifdef MSWIN_FIND_REPLACE
1809 /* Don't process messages used by the dialog */
1810 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1811 {
1812 HandleMouseHide(msg.message, msg.lParam);
1813 return;
1814 }
1815#endif
1816
1817 /*
1818 * Check if it's a special key that we recognise. If not, call
1819 * TranslateMessage().
1820 */
1821 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1822 {
1823 vk = (int) msg.wParam;
1824
1825 /*
1826 * Handle dead keys in special conditions in other cases we let Windows
1827 * handle them and do not interfere.
1828 *
1829 * The dead_key flag must be reset on several occasions:
1830 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1831 * consumed at that point (This is when we let Windows combine the
1832 * dead character on its own)
1833 *
1834 * - Before doing something special such as regenerating keypresses to
1835 * expel the dead character as this could trigger an infinite loop if
1836 * for some reason MyTranslateMessage() do not trigger a call
1837 * immediately to _OnChar() (or _OnSysChar()).
1838 */
1839 if (dead_key)
1840 {
1841 /*
1842 * If a dead key was pressed and the user presses VK_SPACE,
1843 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1844 * with the dead char now, so do nothing special and let Windows
1845 * handle it.
1846 *
1847 * Note that VK_SPACE combines with the dead_key's character and
1848 * only one WM_CHAR will be generated by TranslateMessage(), in
1849 * the two other cases two WM_CHAR will be generated: the dead
1850 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1851 * user expects.
1852 */
1853 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1854 {
1855 dead_key = 0;
1856 MyTranslateMessage(&msg);
1857 return;
1858 }
1859 /* In modes where we are not typing, dead keys should behave
1860 * normally */
1861 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1862 {
1863 outputDeadKey_rePost(msg);
1864 return;
1865 }
1866 }
1867
1868 /* Check for CTRL-BREAK */
1869 if (vk == VK_CANCEL)
1870 {
1871 trash_input_buf();
1872 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001873 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 string[0] = Ctrl_C;
1875 add_to_input_buf(string, 1);
1876 }
1877
1878 for (i = 0; special_keys[i].key_sym != 0; i++)
1879 {
1880 /* ignore VK_SPACE when ALT key pressed: system menu */
1881 if (special_keys[i].key_sym == vk
1882 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1883 {
1884 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001885 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001886 * is a key that would normally trigger the dead key nominal
1887 * character output (such as a NUMPAD printable character or
1888 * the TAB key, etc...).
1889 */
1890 if (dead_key && (special_keys[i].vim_code0 == 'K'
1891 || vk == VK_TAB || vk == CAR))
1892 {
1893 outputDeadKey_rePost(msg);
1894 return;
1895 }
1896
1897#ifdef FEAT_MENU
1898 /* Check for <F10>: Windows selects the menu. When <F10> is
1899 * mapped we want to use the mapping instead. */
1900 if (vk == VK_F10
1901 && gui.menu_is_active
1902 && check_map(k10, State, FALSE, TRUE, FALSE,
1903 NULL, NULL) == NULL)
1904 break;
1905#endif
1906 if (GetKeyState(VK_SHIFT) & 0x8000)
1907 modifiers |= MOD_MASK_SHIFT;
1908 /*
1909 * Don't use caps-lock as shift, because these are special keys
1910 * being considered here, and we only want letters to get
1911 * shifted -- webb
1912 */
1913 /*
1914 if (GetKeyState(VK_CAPITAL) & 0x0001)
1915 modifiers ^= MOD_MASK_SHIFT;
1916 */
1917 if (GetKeyState(VK_CONTROL) & 0x8000)
1918 modifiers |= MOD_MASK_CTRL;
1919 if (GetKeyState(VK_MENU) & 0x8000)
1920 modifiers |= MOD_MASK_ALT;
1921
1922 if (special_keys[i].vim_code1 == NUL)
1923 key = special_keys[i].vim_code0;
1924 else
1925 key = TO_SPECIAL(special_keys[i].vim_code0,
1926 special_keys[i].vim_code1);
1927 key = simplify_key(key, &modifiers);
1928 if (key == CSI)
1929 key = K_CSI;
1930
1931 if (modifiers)
1932 {
1933 string[0] = CSI;
1934 string[1] = KS_MODIFIER;
1935 string[2] = modifiers;
1936 add_to_input_buf(string, 3);
1937 }
1938
1939 if (IS_SPECIAL(key))
1940 {
1941 string[0] = CSI;
1942 string[1] = K_SECOND(key);
1943 string[2] = K_THIRD(key);
1944 add_to_input_buf(string, 3);
1945 }
1946 else
1947 {
1948 int len;
1949
1950 /* Handle "key" as a Unicode character. */
1951 len = char_to_string(key, string, 40, FALSE);
1952 add_to_input_buf(string, len);
1953 }
1954 break;
1955 }
1956 }
1957 if (special_keys[i].key_sym == 0)
1958 {
1959 /* Some keys need C-S- where they should only need C-.
1960 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1961 * system startup (Helmut Stiegler, 2003 Oct 3). */
1962 if (vk != 0xff
1963 && (GetKeyState(VK_CONTROL) & 0x8000)
1964 && !(GetKeyState(VK_SHIFT) & 0x8000)
1965 && !(GetKeyState(VK_MENU) & 0x8000))
1966 {
1967 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1968 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1969 {
1970 string[0] = Ctrl_HAT;
1971 add_to_input_buf(string, 1);
1972 }
1973 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1974 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1975 {
1976 string[0] = Ctrl__;
1977 add_to_input_buf(string, 1);
1978 }
1979 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
1980 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1981 {
1982 string[0] = Ctrl_AT;
1983 add_to_input_buf(string, 1);
1984 }
1985 else
1986 MyTranslateMessage(&msg);
1987 }
1988 else
1989 MyTranslateMessage(&msg);
1990 }
1991 }
1992#ifdef FEAT_MBYTE_IME
1993 else if (msg.message == WM_IME_NOTIFY)
1994 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
1995 else if (msg.message == WM_KEYUP && im_get_status())
1996 /* added for non-MS IME (Yasuhiro Matsumoto) */
1997 MyTranslateMessage(&msg);
1998#endif
1999#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2000/* GIME_TEST */
2001 else if (msg.message == WM_IME_STARTCOMPOSITION)
2002 {
2003 POINT point;
2004
2005 global_ime_set_font(&norm_logfont);
2006 point.x = FILL_X(gui.col);
2007 point.y = FILL_Y(gui.row);
2008 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2009 global_ime_set_position(&point);
2010 }
2011#endif
2012
2013#ifdef FEAT_MENU
2014 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2015 * mapped we need to stop it here to avoid strange effects (e.g., for the
2016 * key-up event) */
2017 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2018 NULL, NULL) == NULL)
2019#endif
2020 pDispatchMessage(&msg);
2021}
2022
2023/*
2024 * Catch up with any queued events. This may put keyboard input into the
2025 * input buffer, call resize call-backs, trigger timers etc. If there is
2026 * nothing in the event queue (& no timers pending), then we return
2027 * immediately.
2028 */
2029 void
2030gui_mch_update(void)
2031{
2032 MSG msg;
2033
2034 if (!s_busy_processing)
2035 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2036 && !vim_is_input_buf_full())
2037 process_message();
2038}
2039
Bram Moolenaar4231da42016-06-02 14:30:04 +02002040 static void
2041remove_any_timer(void)
2042{
2043 MSG msg;
2044
2045 if (s_wait_timer != 0 && !s_timed_out)
2046 {
2047 KillTimer(NULL, s_wait_timer);
2048
2049 /* Eat spurious WM_TIMER messages */
2050 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2051 ;
2052 s_wait_timer = 0;
2053 }
2054}
2055
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002056/*
2057 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2058 * from the keyboard.
2059 * wtime == -1 Wait forever.
2060 * wtime == 0 This should never happen.
2061 * wtime > 0 Wait wtime milliseconds for a character.
2062 * Returns OK if a character was found to be available within the given time,
2063 * or FAIL otherwise.
2064 */
2065 int
2066gui_mch_wait_for_chars(int wtime)
2067{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 int focus;
2069
2070 s_timed_out = FALSE;
2071
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002072 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002073 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002074 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075 if (s_busy_processing)
2076 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002077
2078 // When called with "wtime" zero, just want one msec.
2079 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002080 (TIMERPROC)_OnTimer);
2081 }
2082
2083 allow_scrollbar = TRUE;
2084
2085 focus = gui.in_focus;
2086 while (!s_timed_out)
2087 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002088 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002089 if (gui.in_focus != focus)
2090 {
2091 if (gui.in_focus)
2092 gui_mch_start_blink();
2093 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002094 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002095 focus = gui.in_focus;
2096 }
2097
2098 if (s_need_activate)
2099 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002100 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002101 s_need_activate = FALSE;
2102 }
2103
Bram Moolenaar4231da42016-06-02 14:30:04 +02002104#ifdef FEAT_TIMERS
2105 did_add_timer = FALSE;
2106#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002107#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002108 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002109 for (;;)
2110 {
2111 MSG msg;
2112
2113 parse_queued_messages();
Bram Moolenaar89c00032019-09-04 13:53:21 +02002114#ifdef FEAT_TIMERS
2115 if (did_add_timer)
2116 break;
2117#endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002118 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2119 {
2120 process_message();
2121 break;
2122 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002123 else if (input_available()
2124 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2125 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002126 break;
2127 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002128#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002129 // Don't use gui_mch_update() because then we will spin-lock until a
2130 // char arrives, instead we use GetMessage() to hang until an
2131 // event arrives. No need to check for input_buf_full because we are
2132 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002134#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002135
2136 if (input_available())
2137 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002138 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002139 allow_scrollbar = FALSE;
2140
Bram Moolenaar89c00032019-09-04 13:53:21 +02002141 // Clear pending mouse button, the release event may have been
2142 // taken by the dialog window. But don't do this when getting
2143 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002144 if (!s_getting_focus)
2145 s_button_pending = -1;
2146
2147 return OK;
2148 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002149
2150#ifdef FEAT_TIMERS
2151 if (did_add_timer)
2152 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002153 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002154 remove_any_timer();
2155 break;
2156 }
2157#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002158 }
2159 allow_scrollbar = FALSE;
2160 return FAIL;
2161}
2162
2163/*
2164 * Clear a rectangular region of the screen from text pos (row1, col1) to
2165 * (row2, col2) inclusive.
2166 */
2167 void
2168gui_mch_clear_block(
2169 int row1,
2170 int col1,
2171 int row2,
2172 int col2)
2173{
2174 RECT rc;
2175
2176 /*
2177 * Clear one extra pixel at the far right, for when bold characters have
2178 * spilled over to the window border.
2179 * Note: FillRect() excludes right and bottom of rectangle.
2180 */
2181 rc.left = FILL_X(col1);
2182 rc.top = FILL_Y(row1);
2183 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2184 rc.bottom = FILL_Y(row2 + 1);
2185 clear_rect(&rc);
2186}
2187
2188/*
2189 * Clear the whole text window.
2190 */
2191 void
2192gui_mch_clear_all(void)
2193{
2194 RECT rc;
2195
2196 rc.left = 0;
2197 rc.top = 0;
2198 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2199 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2200 clear_rect(&rc);
2201}
2202/*
2203 * Menu stuff.
2204 */
2205
2206 void
2207gui_mch_enable_menu(int flag)
2208{
2209#ifdef FEAT_MENU
2210 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2211#endif
2212}
2213
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002214 void
2215gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002216 int x UNUSED,
2217 int y UNUSED,
2218 int w UNUSED,
2219 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002220{
2221 /* It will be in the right place anyway */
2222}
2223
2224#if defined(FEAT_MENU) || defined(PROTO)
2225/*
2226 * Make menu item hidden or not hidden
2227 */
2228 void
2229gui_mch_menu_hidden(
2230 vimmenu_T *menu,
2231 int hidden)
2232{
2233 /*
2234 * This doesn't do what we want. Hmm, just grey the menu items for now.
2235 */
2236 /*
2237 if (hidden)
2238 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2239 else
2240 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2241 */
2242 gui_mch_menu_grey(menu, hidden);
2243}
2244
2245/*
2246 * This is called after setting all the menus to grey/hidden or not.
2247 */
2248 void
2249gui_mch_draw_menubar(void)
2250{
2251 DrawMenuBar(s_hwnd);
2252}
2253#endif /*FEAT_MENU*/
2254
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255/*
2256 * Return the RGB value of a pixel as a long.
2257 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002258 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002259gui_mch_get_rgb(guicolor_T pixel)
2260{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002261 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2262 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263}
2264
2265#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2266/* Convert pixels in X to dialog units */
2267 static WORD
2268PixelToDialogX(int numPixels)
2269{
2270 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2271}
2272
2273/* Convert pixels in Y to dialog units */
2274 static WORD
2275PixelToDialogY(int numPixels)
2276{
2277 return (WORD)((numPixels * 8) / s_dlgfntheight);
2278}
2279
2280/* Return the width in pixels of the given text in the given DC. */
2281 static int
2282GetTextWidth(HDC hdc, char_u *str, int len)
2283{
2284 SIZE size;
2285
2286 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2287 return size.cx;
2288}
2289
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002290/*
2291 * Return the width in pixels of the given text in the given DC, taking care
2292 * of 'encoding' to active codepage conversion.
2293 */
2294 static int
2295GetTextWidthEnc(HDC hdc, char_u *str, int len)
2296{
2297 SIZE size;
2298 WCHAR *wstr;
2299 int n;
2300 int wlen = len;
2301
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002302 wstr = enc_to_utf16(str, &wlen);
2303 if (wstr == NULL)
2304 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002306 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2307 vim_free(wstr);
2308 if (n)
2309 return size.cx;
2310 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002311}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002313static void get_work_area(RECT *spi_rect);
2314
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002315/*
2316 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002317 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2318 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319 */
2320 static BOOL
2321CenterWindow(
2322 HWND hwndChild,
2323 HWND hwndParent)
2324{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002325 HMONITOR mon;
2326 MONITORINFO moninfo;
2327 RECT rChild, rParent, rScreen;
2328 int wChild, hChild, wParent, hParent;
2329 int xNew, yNew;
2330 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331
2332 GetWindowRect(hwndChild, &rChild);
2333 wChild = rChild.right - rChild.left;
2334 hChild = rChild.bottom - rChild.top;
2335
2336 /* If Vim is minimized put the window in the middle of the screen. */
2337 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002338 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 else
2340 GetWindowRect(hwndParent, &rParent);
2341 wParent = rParent.right - rParent.left;
2342 hParent = rParent.bottom - rParent.top;
2343
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002344 moninfo.cbSize = sizeof(MONITORINFO);
2345 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2346 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002347 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002348 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002349 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002350 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002352 hdc = GetDC(hwndChild);
2353 rScreen.left = 0;
2354 rScreen.top = 0;
2355 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2356 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2357 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 }
2359
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002360 xNew = rParent.left + ((wParent - wChild) / 2);
2361 if (xNew < rScreen.left)
2362 xNew = rScreen.left;
2363 else if ((xNew + wChild) > rScreen.right)
2364 xNew = rScreen.right - wChild;
2365
2366 yNew = rParent.top + ((hParent - hChild) / 2);
2367 if (yNew < rScreen.top)
2368 yNew = rScreen.top;
2369 else if ((yNew + hChild) > rScreen.bottom)
2370 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371
2372 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2373 SWP_NOSIZE | SWP_NOZORDER);
2374}
2375#endif /* FEAT_GUI_DIALOG */
2376
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002377#if defined(FEAT_TOOLBAR) || defined(PROTO)
2378 void
2379gui_mch_show_toolbar(int showit)
2380{
2381 if (s_toolbarhwnd == NULL)
2382 return;
2383
2384 if (showit)
2385 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002386# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002387 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002388# define TB_SETUNICODEFORMAT 0x2005
2389# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002390 // Enable unicode support
2391 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2392 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393 ShowWindow(s_toolbarhwnd, SW_SHOW);
2394 }
2395 else
2396 ShowWindow(s_toolbarhwnd, SW_HIDE);
2397}
2398
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002399/* The number of bitmaps is fixed. Exit is missing! */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400#define TOOLBAR_BITMAP_COUNT 31
2401
2402#endif
2403
2404#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2405 static void
2406add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2407{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002408 WCHAR *wn;
2409 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002411 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002412 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002413 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002414
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002415 infow.cbSize = sizeof(infow);
2416 infow.fMask = MIIM_TYPE | MIIM_ID;
2417 infow.wID = item_id;
2418 infow.fType = MFT_STRING;
2419 infow.dwTypeData = wn;
2420 infow.cch = (UINT)wcslen(wn);
2421 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2422 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002423}
2424
2425 static void
2426show_tabline_popup_menu(void)
2427{
2428 HMENU tab_pmenu;
2429 long rval;
2430 POINT pt;
2431
2432 /* When ignoring events don't show the menu. */
2433 if (hold_gui_events
2434# ifdef FEAT_CMDWIN
2435 || cmdwin_type != 0
2436# endif
2437 )
2438 return;
2439
2440 tab_pmenu = CreatePopupMenu();
2441 if (tab_pmenu == NULL)
2442 return;
2443
2444 if (first_tabpage->tp_next != NULL)
2445 add_tabline_popup_menu_entry(tab_pmenu,
2446 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2447 add_tabline_popup_menu_entry(tab_pmenu,
2448 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2449 add_tabline_popup_menu_entry(tab_pmenu,
2450 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2451
2452 GetCursorPos(&pt);
2453 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2454 NULL);
2455
2456 DestroyMenu(tab_pmenu);
2457
2458 /* Add the string cmd into input buffer */
2459 if (rval > 0)
2460 {
2461 TCHITTESTINFO htinfo;
2462 int idx;
2463
2464 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2465 return;
2466
2467 htinfo.pt.x = pt.x;
2468 htinfo.pt.y = pt.y;
2469 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2470 if (idx == -1)
2471 idx = 0;
2472 else
2473 idx += 1;
2474
2475 send_tabline_menu_event(idx, (int)rval);
2476 }
2477}
2478
2479/*
2480 * Show or hide the tabline.
2481 */
2482 void
2483gui_mch_show_tabline(int showit)
2484{
2485 if (s_tabhwnd == NULL)
2486 return;
2487
2488 if (!showit != !showing_tabline)
2489 {
2490 if (showit)
2491 ShowWindow(s_tabhwnd, SW_SHOW);
2492 else
2493 ShowWindow(s_tabhwnd, SW_HIDE);
2494 showing_tabline = showit;
2495 }
2496}
2497
2498/*
2499 * Return TRUE when tabline is displayed.
2500 */
2501 int
2502gui_mch_showing_tabline(void)
2503{
2504 return s_tabhwnd != NULL && showing_tabline;
2505}
2506
2507/*
2508 * Update the labels of the tabline.
2509 */
2510 void
2511gui_mch_update_tabline(void)
2512{
2513 tabpage_T *tp;
2514 TCITEM tie;
2515 int nr = 0;
2516 int curtabidx = 0;
2517 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002519
2520 if (s_tabhwnd == NULL)
2521 return;
2522
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002523#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002525# define CCM_SETUNICODEFORMAT 0x2005
2526#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002527 // Enable unicode support
2528 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002529
2530 tie.mask = TCIF_TEXT;
2531 tie.iImage = -1;
2532
2533 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2534 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2535
2536 /* Add a label for each tab page. They all contain the same text area. */
2537 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2538 {
2539 if (tp == curtab)
2540 curtabidx = nr;
2541
2542 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2543 {
2544 /* Add the tab */
2545 tie.pszText = "-Empty-";
2546 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2547 tabadded = 1;
2548 }
2549
2550 get_tabline_label(tp, FALSE);
2551 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002552
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002553 wstr = enc_to_utf16(NameBuff, NULL);
2554 if (wstr != NULL)
2555 {
2556 TCITEMW tiw;
2557
2558 tiw.mask = TCIF_TEXT;
2559 tiw.iImage = -1;
2560 tiw.pszText = wstr;
2561 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2562 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002563 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002564 }
2565
2566 /* Remove any old labels. */
2567 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2568 TabCtrl_DeleteItem(s_tabhwnd, nr);
2569
2570 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2571 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2572
2573 /* Re-enable redraw and redraw. */
2574 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2575 RedrawWindow(s_tabhwnd, NULL, NULL,
2576 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2577
2578 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2579 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2580}
2581
2582/*
2583 * Set the current tab to "nr". First tab is 1.
2584 */
2585 void
2586gui_mch_set_curtab(int nr)
2587{
2588 if (s_tabhwnd == NULL)
2589 return;
2590
2591 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2592 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2593}
2594
2595#endif
2596
2597/*
2598 * ":simalt" command.
2599 */
2600 void
2601ex_simalt(exarg_T *eap)
2602{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002603 char_u *keys = eap->arg;
2604 int fill_typebuf = FALSE;
2605 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606
2607 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2608 while (*keys)
2609 {
2610 if (*keys == '~')
2611 *keys = ' '; /* for showing system menu */
2612 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2613 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002614 fill_typebuf = TRUE;
2615 }
2616 if (fill_typebuf)
2617 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002618 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002619 * processed. */
2620 key_name[0] = K_SPECIAL;
2621 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002622 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002623 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002624#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002625 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002626#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002627 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002628 }
2629}
2630
2631/*
2632 * Create the find & replace dialogs.
2633 * You can't have both at once: ":find" when replace is showing, destroys
2634 * the replace dialog first, and the other way around.
2635 */
2636#ifdef MSWIN_FIND_REPLACE
2637 static void
2638initialise_findrep(char_u *initial_string)
2639{
2640 int wword = FALSE;
2641 int mcase = !p_ic;
2642 char_u *entry_text;
2643
2644 /* Get the search string to use. */
2645 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2646
2647 s_findrep_struct.hwndOwner = s_hwnd;
2648 s_findrep_struct.Flags = FR_DOWN;
2649 if (mcase)
2650 s_findrep_struct.Flags |= FR_MATCHCASE;
2651 if (wword)
2652 s_findrep_struct.Flags |= FR_WHOLEWORD;
2653 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002654 {
2655 WCHAR *p = enc_to_utf16(entry_text, NULL);
2656 if (p != NULL)
2657 {
2658 int len = s_findrep_struct.wFindWhatLen - 1;
2659
2660 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2661 s_findrep_struct.lpstrFindWhat[len] = NUL;
2662 vim_free(p);
2663 }
2664 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 vim_free(entry_text);
2666}
2667#endif
2668
2669 static void
2670set_window_title(HWND hwnd, char *title)
2671{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002672 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002673 {
2674 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002675
2676 /* Convert the title from 'encoding' to UTF-16. */
2677 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2678 if (wbuf != NULL)
2679 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002680 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002681 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002682 }
2683 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002684 else
2685 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002686}
2687
2688 void
2689gui_mch_find_dialog(exarg_T *eap)
2690{
2691#ifdef MSWIN_FIND_REPLACE
2692 if (s_findrep_msg != 0)
2693 {
2694 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2695 DestroyWindow(s_findrep_hwnd);
2696
2697 if (!IsWindow(s_findrep_hwnd))
2698 {
2699 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002700 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 }
2702
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002703 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704 (void)SetFocus(s_findrep_hwnd);
2705
2706 s_findrep_is_find = TRUE;
2707 }
2708#endif
2709}
2710
2711
2712 void
2713gui_mch_replace_dialog(exarg_T *eap)
2714{
2715#ifdef MSWIN_FIND_REPLACE
2716 if (s_findrep_msg != 0)
2717 {
2718 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2719 DestroyWindow(s_findrep_hwnd);
2720
2721 if (!IsWindow(s_findrep_hwnd))
2722 {
2723 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002724 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002725 }
2726
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002727 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728 (void)SetFocus(s_findrep_hwnd);
2729
2730 s_findrep_is_find = FALSE;
2731 }
2732#endif
2733}
2734
2735
2736/*
2737 * Set visibility of the pointer.
2738 */
2739 void
2740gui_mch_mousehide(int hide)
2741{
2742 if (hide != gui.pointer_hidden)
2743 {
2744 ShowCursor(!hide);
2745 gui.pointer_hidden = hide;
2746 }
2747}
2748
2749#ifdef FEAT_MENU
2750 static void
2751gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2752{
2753 /* Unhide the mouse, we don't get move events here. */
2754 gui_mch_mousehide(FALSE);
2755
2756 (void)TrackPopupMenu(
2757 (HMENU)menu->submenu_id,
2758 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2759 x, y,
2760 (int)0, /*reserved param*/
2761 s_hwnd,
2762 NULL);
2763 /*
2764 * NOTE: The pop-up menu can eat the mouse up event.
2765 * We deal with this in normal.c.
2766 */
2767}
2768#endif
2769
2770/*
2771 * Got a message when the system will go down.
2772 */
2773 static void
2774_OnEndSession(void)
2775{
2776 getout_preserve_modified(1);
2777}
2778
2779/*
2780 * Get this message when the user clicks on the cross in the top right corner
2781 * of a Windows95 window.
2782 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002784_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785{
2786 gui_shell_closed();
2787}
2788
2789/*
2790 * Get a message when the window is being destroyed.
2791 */
2792 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002793_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002794{
2795 if (!destroying)
2796 _OnClose(hwnd);
2797}
2798
2799 static void
2800_OnPaint(
2801 HWND hwnd)
2802{
2803 if (!IsMinimized(hwnd))
2804 {
2805 PAINTSTRUCT ps;
2806
2807 out_flush(); /* make sure all output has been processed */
2808 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002809
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 /* prevent multi-byte characters from misprinting on an invalid
2811 * rectangle */
2812 if (has_mbyte)
2813 {
2814 RECT rect;
2815
2816 GetClientRect(hwnd, &rect);
2817 ps.rcPaint.left = rect.left;
2818 ps.rcPaint.right = rect.right;
2819 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820
2821 if (!IsRectEmpty(&ps.rcPaint))
2822 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2824 ps.rcPaint.right - ps.rcPaint.left + 1,
2825 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2826 }
2827
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828 EndPaint(hwnd, &ps);
2829 }
2830}
2831
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002832 static void
2833_OnSize(
2834 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002835 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002836 int cx,
2837 int cy)
2838{
2839 if (!IsMinimized(hwnd))
2840 {
2841 gui_resize_shell(cx, cy);
2842
2843#ifdef FEAT_MENU
2844 /* Menu bar may wrap differently now */
2845 gui_mswin_get_menu_height(TRUE);
2846#endif
2847 }
2848}
2849
2850 static void
2851_OnSetFocus(
2852 HWND hwnd,
2853 HWND hwndOldFocus)
2854{
2855 gui_focus_change(TRUE);
2856 s_getting_focus = TRUE;
2857 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2858}
2859
2860 static void
2861_OnKillFocus(
2862 HWND hwnd,
2863 HWND hwndNewFocus)
2864{
2865 gui_focus_change(FALSE);
2866 s_getting_focus = FALSE;
2867 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2868}
2869
2870/*
2871 * Get a message when the user switches back to vim
2872 */
2873 static LRESULT
2874_OnActivateApp(
2875 HWND hwnd,
2876 BOOL fActivate,
2877 DWORD dwThreadId)
2878{
2879 /* we call gui_focus_change() in _OnSetFocus() */
2880 /* gui_focus_change((int)fActivate); */
2881 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2882}
2883
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884 void
2885gui_mch_destroy_scrollbar(scrollbar_T *sb)
2886{
2887 DestroyWindow(sb->id);
2888}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889
2890/*
2891 * Get current mouse coordinates in text window.
2892 */
2893 void
2894gui_mch_getmouse(int *x, int *y)
2895{
2896 RECT rct;
2897 POINT mp;
2898
2899 (void)GetWindowRect(s_textArea, &rct);
2900 (void)GetCursorPos((LPPOINT)&mp);
2901 *x = (int)(mp.x - rct.left);
2902 *y = (int)(mp.y - rct.top);
2903}
2904
2905/*
2906 * Move mouse pointer to character at (x, y).
2907 */
2908 void
2909gui_mch_setmouse(int x, int y)
2910{
2911 RECT rct;
2912
2913 (void)GetWindowRect(s_textArea, &rct);
2914 (void)SetCursorPos(x + gui.border_offset + rct.left,
2915 y + gui.border_offset + rct.top);
2916}
2917
2918 static void
2919gui_mswin_get_valid_dimensions(
2920 int w,
2921 int h,
2922 int *valid_w,
2923 int *valid_h)
2924{
2925 int base_width, base_height;
2926
2927 base_width = gui_get_base_width()
2928 + (GetSystemMetrics(SM_CXFRAME) +
2929 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2930 base_height = gui_get_base_height()
2931 + (GetSystemMetrics(SM_CYFRAME) +
2932 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2933 + GetSystemMetrics(SM_CYCAPTION)
2934#ifdef FEAT_MENU
2935 + gui_mswin_get_menu_height(FALSE)
2936#endif
2937 ;
2938 *valid_w = base_width +
2939 ((w - base_width) / gui.char_width) * gui.char_width;
2940 *valid_h = base_height +
2941 ((h - base_height) / gui.char_height) * gui.char_height;
2942}
2943
2944 void
2945gui_mch_flash(int msec)
2946{
2947 RECT rc;
2948
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002949#if defined(FEAT_DIRECTX)
2950 if (IS_ENABLE_DIRECTX())
2951 DWriteContext_Flush(s_dwc);
2952#endif
2953
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 /*
2955 * Note: InvertRect() excludes right and bottom of rectangle.
2956 */
2957 rc.left = 0;
2958 rc.top = 0;
2959 rc.right = gui.num_cols * gui.char_width;
2960 rc.bottom = gui.num_rows * gui.char_height;
2961 InvertRect(s_hdc, &rc);
2962 gui_mch_flush(); /* make sure it's displayed */
2963
2964 ui_delay((long)msec, TRUE); /* wait for a few msec */
2965
2966 InvertRect(s_hdc, &rc);
2967}
2968
2969/*
2970 * Return flags used for scrolling.
2971 * The SW_INVALIDATE is required when part of the window is covered or
2972 * off-screen. Refer to MS KB Q75236.
2973 */
2974 static int
2975get_scroll_flags(void)
2976{
2977 HWND hwnd;
2978 RECT rcVim, rcOther, rcDest;
2979
2980 GetWindowRect(s_hwnd, &rcVim);
2981
2982 /* Check if the window is partly above or below the screen. We don't care
2983 * about partly left or right of the screen, it is not relevant when
2984 * scrolling up or down. */
2985 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
2986 return SW_INVALIDATE;
2987
2988 /* Check if there is an window (partly) on top of us. */
2989 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
2990 if (IsWindowVisible(hwnd))
2991 {
2992 GetWindowRect(hwnd, &rcOther);
2993 if (IntersectRect(&rcDest, &rcVim, &rcOther))
2994 return SW_INVALIDATE;
2995 }
2996 return 0;
2997}
2998
2999/*
3000 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3001 * may not be scrolled out properly.
3002 * For gVim, when _OnScroll() is repeated, the character at the
3003 * previous cursor position may be left drawn after scroll.
3004 * The problem can be avoided by calling GetPixel() to get a pixel in
3005 * the region before ScrollWindowEx().
3006 */
3007 static void
3008intel_gpu_workaround(void)
3009{
3010 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3011}
3012
3013/*
3014 * Delete the given number of lines from the given row, scrolling up any
3015 * text further down within the scroll region.
3016 */
3017 void
3018gui_mch_delete_lines(
3019 int row,
3020 int num_lines)
3021{
3022 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003023
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003024 rc.left = FILL_X(gui.scroll_region_left);
3025 rc.right = FILL_X(gui.scroll_region_right + 1);
3026 rc.top = FILL_Y(row);
3027 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3028
Bram Moolenaar92467d32017-12-05 13:22:16 +01003029#if defined(FEAT_DIRECTX)
3030 if (IS_ENABLE_DIRECTX())
3031 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003032 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3033 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003034 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003035 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003036#endif
3037 {
3038 intel_gpu_workaround();
3039 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003040 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003041 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003042 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 /* This seems to be required to avoid the cursor disappearing when
3045 * scrolling such that the cursor ends up in the top-left character on
3046 * the screen... But why? (Webb) */
3047 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3048 /* gui.cursor_is_valid = FALSE; */
3049
3050 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3051 gui.scroll_region_left,
3052 gui.scroll_region_bot, gui.scroll_region_right);
3053}
3054
3055/*
3056 * Insert the given number of lines before the given row, scrolling down any
3057 * following text within the scroll region.
3058 */
3059 void
3060gui_mch_insert_lines(
3061 int row,
3062 int num_lines)
3063{
3064 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003065
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066 rc.left = FILL_X(gui.scroll_region_left);
3067 rc.right = FILL_X(gui.scroll_region_right + 1);
3068 rc.top = FILL_Y(row);
3069 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003070
3071#if defined(FEAT_DIRECTX)
3072 if (IS_ENABLE_DIRECTX())
3073 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003074 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3075 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003076 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003077 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003078#endif
3079 {
3080 intel_gpu_workaround();
3081 /* The SW_INVALIDATE is required when part of the window is covered or
3082 * off-screen. How do we avoid it when it's not needed? */
3083 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003084 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003085 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003086 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003087
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088 gui_clear_block(row, gui.scroll_region_left,
3089 row + num_lines - 1, gui.scroll_region_right);
3090}
3091
3092
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003093 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003094gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003095{
3096#if defined(FEAT_DIRECTX)
3097 DWriteContext_Close(s_dwc);
3098 DWrite_Final();
3099 s_dwc = NULL;
3100#endif
3101
3102 ReleaseDC(s_textArea, s_hdc);
3103 DeleteObject(s_brush);
3104
3105#ifdef FEAT_TEAROFF
3106 /* Unload the tearoff bitmap */
3107 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3108#endif
3109
3110 /* Destroy our window (if we have one). */
3111 if (s_hwnd != NULL)
3112 {
3113 destroying = TRUE; /* ignore WM_DESTROY message now */
3114 DestroyWindow(s_hwnd);
3115 }
3116
3117#ifdef GLOBAL_IME
3118 global_ime_end();
3119#endif
3120}
3121
3122 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003123logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124{
3125 char *p;
3126 char *res;
3127 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003128 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003129 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003130 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003131
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003132 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3133 if (font_name == NULL)
3134 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003135 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003136 quality_name = quality_id2name((int)lf.lfQuality);
3137
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003138 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003139 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003140 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003141 if (res != NULL)
3142 {
3143 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003144 // make a normal font string out of the lf thing:
3145 points = pixels_to_points(
3146 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3147 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3148 sprintf((char *)p, "%s:h%d", font_name, points);
3149 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003150 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151 while (*p)
3152 {
3153 if (*p == ' ')
3154 *p = '_';
3155 ++p;
3156 }
3157 if (lf.lfItalic)
3158 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003159 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003160 STRCAT(p, ":b");
3161 if (lf.lfUnderline)
3162 STRCAT(p, ":u");
3163 if (lf.lfStrikeOut)
3164 STRCAT(p, ":s");
3165 if (charset_name != NULL)
3166 {
3167 STRCAT(p, ":c");
3168 STRCAT(p, charset_name);
3169 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003170 if (quality_name != NULL)
3171 {
3172 STRCAT(p, ":q");
3173 STRCAT(p, quality_name);
3174 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175 }
3176
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003177 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 return (char_u *)res;
3179}
3180
3181
3182#ifdef FEAT_MBYTE_IME
3183/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003184 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185 * 'guifont'
3186 */
3187 static void
3188update_im_font(void)
3189{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003190 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191
3192 if (p_guifontwide != NULL && *p_guifontwide != NUL
3193 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003194 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 norm_logfont = lf_wide;
3196 else
3197 norm_logfont = sub_logfont;
3198 im_set_font(&norm_logfont);
3199}
3200#endif
3201
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003202/*
3203 * Handler of gui.wide_font (p_guifontwide) changed notification.
3204 */
3205 void
3206gui_mch_wide_font_changed(void)
3207{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003208 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003209
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003210#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003212#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213
3214 gui_mch_free_font(gui.wide_ital_font);
3215 gui.wide_ital_font = NOFONT;
3216 gui_mch_free_font(gui.wide_bold_font);
3217 gui.wide_bold_font = NOFONT;
3218 gui_mch_free_font(gui.wide_boldital_font);
3219 gui.wide_boldital_font = NOFONT;
3220
3221 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003222 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003223 {
3224 if (!lf.lfItalic)
3225 {
3226 lf.lfItalic = TRUE;
3227 gui.wide_ital_font = get_font_handle(&lf);
3228 lf.lfItalic = FALSE;
3229 }
3230 if (lf.lfWeight < FW_BOLD)
3231 {
3232 lf.lfWeight = FW_BOLD;
3233 gui.wide_bold_font = get_font_handle(&lf);
3234 if (!lf.lfItalic)
3235 {
3236 lf.lfItalic = TRUE;
3237 gui.wide_boldital_font = get_font_handle(&lf);
3238 }
3239 }
3240 }
3241}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242
3243/*
3244 * Initialise vim to use the font with the given name.
3245 * Return FAIL if the font could not be loaded, OK otherwise.
3246 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003247 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003248gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003250 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003251 GuiFont font = NOFONT;
3252 char_u *p;
3253
3254 /* Load the font */
3255 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3256 font = get_font_handle(&lf);
3257 if (font == NOFONT)
3258 return FAIL;
3259
3260 if (font_name == NULL)
3261 font_name = (char_u *)lf.lfFaceName;
3262#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3263 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003264#endif
3265#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 sub_logfont = lf;
3267#endif
3268#ifdef FEAT_MBYTE_IME
3269 update_im_font();
3270#endif
3271 gui_mch_free_font(gui.norm_font);
3272 gui.norm_font = font;
3273 current_font_height = lf.lfHeight;
3274 GetFontSize(font);
3275
3276 p = logfont2name(lf);
3277 if (p != NULL)
3278 {
3279 hl_set_font_name(p);
3280
3281 /* When setting 'guifont' to "*" replace it with the actual font name.
3282 * */
3283 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3284 {
3285 vim_free(p_guifont);
3286 p_guifont = p;
3287 }
3288 else
3289 vim_free(p);
3290 }
3291
3292 gui_mch_free_font(gui.ital_font);
3293 gui.ital_font = NOFONT;
3294 gui_mch_free_font(gui.bold_font);
3295 gui.bold_font = NOFONT;
3296 gui_mch_free_font(gui.boldital_font);
3297 gui.boldital_font = NOFONT;
3298
3299 if (!lf.lfItalic)
3300 {
3301 lf.lfItalic = TRUE;
3302 gui.ital_font = get_font_handle(&lf);
3303 lf.lfItalic = FALSE;
3304 }
3305 if (lf.lfWeight < FW_BOLD)
3306 {
3307 lf.lfWeight = FW_BOLD;
3308 gui.bold_font = get_font_handle(&lf);
3309 if (!lf.lfItalic)
3310 {
3311 lf.lfItalic = TRUE;
3312 gui.boldital_font = get_font_handle(&lf);
3313 }
3314 }
3315
3316 return OK;
3317}
3318
3319#ifndef WPF_RESTORETOMAXIMIZED
3320# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3321#endif
3322
3323/*
3324 * Return TRUE if the GUI window is maximized, filling the whole screen.
3325 */
3326 int
3327gui_mch_maximized(void)
3328{
3329 WINDOWPLACEMENT wp;
3330
3331 wp.length = sizeof(WINDOWPLACEMENT);
3332 if (GetWindowPlacement(s_hwnd, &wp))
3333 return wp.showCmd == SW_SHOWMAXIMIZED
3334 || (wp.showCmd == SW_SHOWMINIMIZED
3335 && wp.flags == WPF_RESTORETOMAXIMIZED);
3336
3337 return 0;
3338}
3339
3340/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003341 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3342 * is set. Compute the new Rows and Columns. This is like resizing the
3343 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344 */
3345 void
3346gui_mch_newfont(void)
3347{
3348 RECT rect;
3349
3350 GetWindowRect(s_hwnd, &rect);
3351 if (win_socket_id == 0)
3352 {
3353 gui_resize_shell(rect.right - rect.left
3354 - (GetSystemMetrics(SM_CXFRAME) +
3355 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3356 rect.bottom - rect.top
3357 - (GetSystemMetrics(SM_CYFRAME) +
3358 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3359 - GetSystemMetrics(SM_CYCAPTION)
3360#ifdef FEAT_MENU
3361 - gui_mswin_get_menu_height(FALSE)
3362#endif
3363 );
3364 }
3365 else
3366 {
3367 /* Inside another window, don't use the frame and border. */
3368 gui_resize_shell(rect.right - rect.left,
3369 rect.bottom - rect.top
3370#ifdef FEAT_MENU
3371 - gui_mswin_get_menu_height(FALSE)
3372#endif
3373 );
3374 }
3375}
3376
3377/*
3378 * Set the window title
3379 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003380 void
3381gui_mch_settitle(
3382 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003383 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003384{
3385 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3386}
3387
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003388#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3390 * misc2.c! */
3391static LPCSTR mshape_idcs[] =
3392{
3393 IDC_ARROW, /* arrow */
3394 MAKEINTRESOURCE(0), /* blank */
3395 IDC_IBEAM, /* beam */
3396 IDC_SIZENS, /* updown */
3397 IDC_SIZENS, /* udsizing */
3398 IDC_SIZEWE, /* leftright */
3399 IDC_SIZEWE, /* lrsizing */
3400 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003402 IDC_ARROW, /* crosshair */
3403 IDC_ARROW, /* hand1 */
3404 IDC_ARROW, /* hand2 */
3405 IDC_ARROW, /* pencil */
3406 IDC_ARROW, /* question */
3407 IDC_ARROW, /* right-arrow */
3408 IDC_UPARROW, /* up-arrow */
3409 IDC_ARROW /* last one */
3410};
3411
3412 void
3413mch_set_mouse_shape(int shape)
3414{
3415 LPCSTR idc;
3416
3417 if (shape == MSHAPE_HIDE)
3418 ShowCursor(FALSE);
3419 else
3420 {
3421 if (shape >= MSHAPE_NUMBERED)
3422 idc = IDC_ARROW;
3423 else
3424 idc = mshape_idcs[shape];
3425#ifdef SetClassLongPtr
3426 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3427#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003428 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003429#endif
3430 if (!p_mh)
3431 {
3432 POINT mp;
3433
3434 /* Set the position to make it redrawn with the new shape. */
3435 (void)GetCursorPos((LPPOINT)&mp);
3436 (void)SetCursorPos(mp.x, mp.y);
3437 ShowCursor(TRUE);
3438 }
3439 }
3440}
3441#endif
3442
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003443#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003445 * Wide version of convert_filter().
3446 */
3447 static WCHAR *
3448convert_filterW(char_u *s)
3449{
3450 char_u *tmp;
3451 int len;
3452 WCHAR *res;
3453
3454 tmp = convert_filter(s);
3455 if (tmp == NULL)
3456 return NULL;
3457 len = (int)STRLEN(s) + 3;
3458 res = enc_to_utf16(tmp, &len);
3459 vim_free(tmp);
3460 return res;
3461}
3462
3463/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003464 * Pop open a file browser and return the file selected, in allocated memory,
3465 * or NULL if Cancel is hit.
3466 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3467 * title - Title message for the file browser dialog.
3468 * dflt - Default name of file.
3469 * ext - Default extension to be added to files without extensions.
3470 * initdir - directory in which to open the browser (NULL = current dir)
3471 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003473 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003474gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 int saving,
3476 char_u *title,
3477 char_u *dflt,
3478 char_u *ext,
3479 char_u *initdir,
3480 char_u *filter)
3481{
3482 /* We always use the wide function. This means enc_to_utf16() must work,
3483 * otherwise it fails miserably! */
3484 OPENFILENAMEW fileStruct;
3485 WCHAR fileBuf[MAXPATHL];
3486 WCHAR *wp;
3487 int i;
3488 WCHAR *titlep = NULL;
3489 WCHAR *extp = NULL;
3490 WCHAR *initdirp = NULL;
3491 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003492 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003493
3494 if (dflt == NULL)
3495 fileBuf[0] = NUL;
3496 else
3497 {
3498 wp = enc_to_utf16(dflt, NULL);
3499 if (wp == NULL)
3500 fileBuf[0] = NUL;
3501 else
3502 {
3503 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3504 fileBuf[i] = wp[i];
3505 fileBuf[i] = NUL;
3506 vim_free(wp);
3507 }
3508 }
3509
3510 /* Convert the filter to Windows format. */
3511 filterp = convert_filterW(filter);
3512
3513 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003514# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003516 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003517# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003519# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520
3521 if (title != NULL)
3522 titlep = enc_to_utf16(title, NULL);
3523 fileStruct.lpstrTitle = titlep;
3524
3525 if (ext != NULL)
3526 extp = enc_to_utf16(ext, NULL);
3527 fileStruct.lpstrDefExt = extp;
3528
3529 fileStruct.lpstrFile = fileBuf;
3530 fileStruct.nMaxFile = MAXPATHL;
3531 fileStruct.lpstrFilter = filterp;
3532 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3533 /* has an initial dir been specified? */
3534 if (initdir != NULL && *initdir != NUL)
3535 {
3536 /* Must have backslashes here, no matter what 'shellslash' says */
3537 initdirp = enc_to_utf16(initdir, NULL);
3538 if (initdirp != NULL)
3539 {
3540 for (wp = initdirp; *wp != NUL; ++wp)
3541 if (*wp == '/')
3542 *wp = '\\';
3543 }
3544 fileStruct.lpstrInitialDir = initdirp;
3545 }
3546
3547 /*
3548 * TODO: Allow selection of multiple files. Needs another arg to this
3549 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3550 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3551 * files that don't exist yet, so I haven't put it in. What about
3552 * OFN_PATHMUSTEXIST?
3553 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3554 */
3555 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003556# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557 if (curbuf->b_p_bin)
3558 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003559# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003560 if (saving)
3561 {
3562 if (!GetSaveFileNameW(&fileStruct))
3563 return NULL;
3564 }
3565 else
3566 {
3567 if (!GetOpenFileNameW(&fileStruct))
3568 return NULL;
3569 }
3570
3571 vim_free(filterp);
3572 vim_free(initdirp);
3573 vim_free(titlep);
3574 vim_free(extp);
3575
3576 /* Convert from UCS2 to 'encoding'. */
3577 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003578 if (p == NULL)
3579 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580
3581 /* Give focus back to main window (when using MDI). */
3582 SetFocus(s_hwnd);
3583
3584 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003585 q = vim_strsave(shorten_fname1(p));
3586 vim_free(p);
3587 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589
3590
3591/*
3592 * Convert the string s to the proper format for a filter string by replacing
3593 * the \t and \n delimiters with \0.
3594 * Returns the converted string in allocated memory.
3595 *
3596 * Keep in sync with convert_filterW() above!
3597 */
3598 static char_u *
3599convert_filter(char_u *s)
3600{
3601 char_u *res;
3602 unsigned s_len = (unsigned)STRLEN(s);
3603 unsigned i;
3604
3605 res = alloc(s_len + 3);
3606 if (res != NULL)
3607 {
3608 for (i = 0; i < s_len; ++i)
3609 if (s[i] == '\t' || s[i] == '\n')
3610 res[i] = '\0';
3611 else
3612 res[i] = s[i];
3613 res[s_len] = NUL;
3614 /* Add two extra NULs to make sure it's properly terminated. */
3615 res[s_len + 1] = NUL;
3616 res[s_len + 2] = NUL;
3617 }
3618 return res;
3619}
3620
3621/*
3622 * Select a directory.
3623 */
3624 char_u *
3625gui_mch_browsedir(char_u *title, char_u *initdir)
3626{
3627 /* We fake this: Use a filter that doesn't select anything and a default
3628 * file name that won't be used. */
3629 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3630 initdir, (char_u *)_("Directory\t*.nothing\n"));
3631}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632#endif /* FEAT_BROWSE */
3633
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634 static void
3635_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003636 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003637 HDROP hDrop)
3638{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003639#define BUFPATHLEN _MAX_PATH
3640#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003641 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003642 char szFile[BUFPATHLEN];
3643 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3644 UINT i;
3645 char_u **fnames;
3646 POINT pt;
3647 int_u modifiers = 0;
3648
3649 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3650
3651 /* Obtain dropped position */
3652 DragQueryPoint(hDrop, &pt);
3653 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3654
3655 reset_VIsual();
3656
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003657 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658
3659 if (fnames != NULL)
3660 for (i = 0; i < cFiles; ++i)
3661 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3663 fnames[i] = utf16_to_enc(wszFile, NULL);
3664 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003665 {
3666 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3667 fnames[i] = vim_strsave((char_u *)szFile);
3668 }
3669 }
3670
3671 DragFinish(hDrop);
3672
3673 if (fnames != NULL)
3674 {
3675 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3676 modifiers |= MOUSE_SHIFT;
3677 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3678 modifiers |= MOUSE_CTRL;
3679 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3680 modifiers |= MOUSE_ALT;
3681
3682 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3683
3684 s_need_activate = TRUE;
3685 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686}
3687
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 static int
3689_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003690 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 HWND hwndCtl,
3692 UINT code,
3693 int pos)
3694{
3695 static UINT prev_code = 0; /* code of previous call */
3696 scrollbar_T *sb, *sb_info;
3697 long val;
3698 int dragging = FALSE;
3699 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 SCROLLINFO si;
3701
3702 si.cbSize = sizeof(si);
3703 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704
3705 sb = gui_mswin_find_scrollbar(hwndCtl);
3706 if (sb == NULL)
3707 return 0;
3708
3709 if (sb->wp != NULL) /* Left or right scrollbar */
3710 {
3711 /*
3712 * Careful: need to get scrollbar info out of first (left) scrollbar
3713 * for window, but keep real scrollbar too because we must pass it to
3714 * gui_drag_scrollbar().
3715 */
3716 sb_info = &sb->wp->w_scrollbars[0];
3717 }
3718 else /* Bottom scrollbar */
3719 sb_info = sb;
3720 val = sb_info->value;
3721
3722 switch (code)
3723 {
3724 case SB_THUMBTRACK:
3725 val = pos;
3726 dragging = TRUE;
3727 if (sb->scroll_shift > 0)
3728 val <<= sb->scroll_shift;
3729 break;
3730 case SB_LINEDOWN:
3731 val++;
3732 break;
3733 case SB_LINEUP:
3734 val--;
3735 break;
3736 case SB_PAGEDOWN:
3737 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3738 break;
3739 case SB_PAGEUP:
3740 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3741 break;
3742 case SB_TOP:
3743 val = 0;
3744 break;
3745 case SB_BOTTOM:
3746 val = sb_info->max;
3747 break;
3748 case SB_ENDSCROLL:
3749 if (prev_code == SB_THUMBTRACK)
3750 {
3751 /*
3752 * "pos" only gives us 16-bit data. In case of large file,
3753 * use GetScrollPos() which returns 32-bit. Unfortunately it
3754 * is not valid while the scrollbar is being dragged.
3755 */
3756 val = GetScrollPos(hwndCtl, SB_CTL);
3757 if (sb->scroll_shift > 0)
3758 val <<= sb->scroll_shift;
3759 }
3760 break;
3761
3762 default:
3763 /* TRACE("Unknown scrollbar event %d\n", code); */
3764 return 0;
3765 }
3766 prev_code = code;
3767
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3769 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770
3771 /*
3772 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3773 */
3774 if (sb->wp != NULL)
3775 {
3776 scrollbar_T *sba = sb->wp->w_scrollbars;
3777 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3778
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 }
3781
3782 /* Don't let us be interrupted here by another message. */
3783 s_busy_processing = TRUE;
3784
3785 /* When "allow_scrollbar" is FALSE still need to remember the new
3786 * position, but don't actually scroll by setting "dont_scroll". */
3787 dont_scroll = !allow_scrollbar;
3788
Bram Moolenaara338adc2018-01-31 20:51:47 +01003789 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003791 mch_enable_flush();
3792 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793
3794 s_busy_processing = FALSE;
3795 dont_scroll = dont_scroll_save;
3796
3797 return 0;
3798}
3799
3800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801#ifdef FEAT_XPM_W32
3802# include "xpm_w32.h"
3803#endif
3804
3805#ifdef PROTO
3806# define WINAPI
3807#endif
3808
3809#ifdef __MINGW32__
3810/*
3811 * Add a lot of missing defines.
3812 * They are not always missing, we need the #ifndef's.
3813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814# ifndef IsMinimized
3815# define IsMinimized(hwnd) IsIconic(hwnd)
3816# endif
3817# ifndef IsMaximized
3818# define IsMaximized(hwnd) IsZoomed(hwnd)
3819# endif
3820# ifndef SelectFont
3821# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3822# endif
3823# ifndef GetStockBrush
3824# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3825# endif
3826# ifndef DeleteBrush
3827# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3828# endif
3829
3830# ifndef HANDLE_WM_RBUTTONDBLCLK
3831# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3832 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3833# endif
3834# ifndef HANDLE_WM_MBUTTONUP
3835# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3836 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3837# endif
3838# ifndef HANDLE_WM_MBUTTONDBLCLK
3839# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3840 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3841# endif
3842# ifndef HANDLE_WM_LBUTTONDBLCLK
3843# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3844 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3845# endif
3846# ifndef HANDLE_WM_RBUTTONDOWN
3847# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3848 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3849# endif
3850# ifndef HANDLE_WM_MOUSEMOVE
3851# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3852 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3853# endif
3854# ifndef HANDLE_WM_RBUTTONUP
3855# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3856 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3857# endif
3858# ifndef HANDLE_WM_MBUTTONDOWN
3859# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3860 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3861# endif
3862# ifndef HANDLE_WM_LBUTTONUP
3863# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3864 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3865# endif
3866# ifndef HANDLE_WM_LBUTTONDOWN
3867# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3868 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3869# endif
3870# ifndef HANDLE_WM_SYSCHAR
3871# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3872 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3873# endif
3874# ifndef HANDLE_WM_ACTIVATEAPP
3875# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3876 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3877# endif
3878# ifndef HANDLE_WM_WINDOWPOSCHANGING
3879# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3880 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3881# endif
3882# ifndef HANDLE_WM_VSCROLL
3883# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3884 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3885# endif
3886# ifndef HANDLE_WM_SETFOCUS
3887# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3888 ((fn)((hwnd), (HWND)(wParam)), 0L)
3889# endif
3890# ifndef HANDLE_WM_KILLFOCUS
3891# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3892 ((fn)((hwnd), (HWND)(wParam)), 0L)
3893# endif
3894# ifndef HANDLE_WM_HSCROLL
3895# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3896 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3897# endif
3898# ifndef HANDLE_WM_DROPFILES
3899# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3900 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3901# endif
3902# ifndef HANDLE_WM_CHAR
3903# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3904 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3905# endif
3906# ifndef HANDLE_WM_SYSDEADCHAR
3907# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3908 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3909# endif
3910# ifndef HANDLE_WM_DEADCHAR
3911# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3912 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3913# endif
3914#endif /* __MINGW32__ */
3915
3916
3917/* Some parameters for tearoff menus. All in pixels. */
3918#define TEAROFF_PADDING_X 2
3919#define TEAROFF_BUTTON_PAD_X 8
3920#define TEAROFF_MIN_WIDTH 200
3921#define TEAROFF_SUBMENU_LABEL ">>"
3922#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3923
3924
3925/* For the Intellimouse: */
3926#ifndef WM_MOUSEWHEEL
3927#define WM_MOUSEWHEEL 0x20a
3928#endif
3929
3930
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003931#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932# define ID_BEVAL_TOOLTIP 200
3933# define BEVAL_TEXT_LEN MAXPATHL
3934
Bram Moolenaar167632f2010-05-26 21:42:54 +02003935#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00003936/* Work around old versions of basetsd.h which wrongly declares
3937 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02003938# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00003939# define UINT_PTR UINT
3940#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003943static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003945
Bram Moolenaar82881492012-11-20 16:53:39 +01003946
3947/* cproto fails on missing include files */
3948#ifndef PROTO
3949
Bram Moolenaar45360022005-07-21 21:08:21 +00003950/*
3951 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003952 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003953 */
Bram Moolenaar82881492012-11-20 16:53:39 +01003954# include <pshpack1.h>
3955
3956#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003957
3958typedef struct _DllVersionInfo
3959{
3960 DWORD cbSize;
3961 DWORD dwMajorVersion;
3962 DWORD dwMinorVersion;
3963 DWORD dwBuildNumber;
3964 DWORD dwPlatformID;
3965} DLLVERSIONINFO;
3966
Bram Moolenaar82881492012-11-20 16:53:39 +01003967#ifndef PROTO
3968# include <poppack.h>
3969#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003970
Bram Moolenaar45360022005-07-21 21:08:21 +00003971typedef struct tagTOOLINFOA_NEW
3972{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003973 UINT cbSize;
3974 UINT uFlags;
3975 HWND hwnd;
3976 UINT_PTR uId;
3977 RECT rect;
3978 HINSTANCE hinst;
3979 LPSTR lpszText;
3980 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003981} TOOLINFO_NEW;
3982
3983typedef struct tagNMTTDISPINFO_NEW
3984{
3985 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003986 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003987 char szText[80];
3988 HINSTANCE hinst;
3989 UINT uFlags;
3990 LPARAM lParam;
3991} NMTTDISPINFO_NEW;
3992
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003993typedef struct tagTOOLINFOW_NEW
3994{
3995 UINT cbSize;
3996 UINT uFlags;
3997 HWND hwnd;
3998 UINT_PTR uId;
3999 RECT rect;
4000 HINSTANCE hinst;
4001 LPWSTR lpszText;
4002 LPARAM lParam;
4003 void *lpReserved;
4004} TOOLINFOW_NEW;
4005
4006typedef struct tagNMTTDISPINFOW_NEW
4007{
4008 NMHDR hdr;
4009 LPWSTR lpszText;
4010 WCHAR szText[80];
4011 HINSTANCE hinst;
4012 UINT uFlags;
4013 LPARAM lParam;
4014} NMTTDISPINFOW_NEW;
4015
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004016
Bram Moolenaar45360022005-07-21 21:08:21 +00004017typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4018#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004019# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020#endif
4021
Bram Moolenaar45360022005-07-21 21:08:21 +00004022#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004023# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004024#endif
4025
4026#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004027# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004028#endif
4029
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004030#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004031
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004032#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4033/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4034 * it here if LPNMTTDISPINFO isn't defined.
4035 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4036 * _MSC_VER. */
4037# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4038typedef struct tagNMTTDISPINFOA {
4039 NMHDR hdr;
4040 LPSTR lpszText;
4041 char szText[80];
4042 HINSTANCE hinst;
4043 UINT uFlags;
4044 LPARAM lParam;
4045} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4046# define LPNMTTDISPINFO LPNMTTDISPINFOA
4047
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004048typedef struct tagNMTTDISPINFOW {
4049 NMHDR hdr;
4050 LPWSTR lpszText;
4051 WCHAR szText[80];
4052 HINSTANCE hinst;
4053 UINT uFlags;
4054 LPARAM lParam;
4055} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004056# endif
4057#endif
4058
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004059#ifndef TTN_GETDISPINFOW
4060# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4061#endif
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063/* Local variables: */
4064
4065#ifdef FEAT_MENU
4066static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068
4069/*
4070 * Use the system font for dialogs and tear-off menus. Remove this line to
4071 * use DLG_FONT_NAME.
4072 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004073#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074
4075#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076#define VIM_CLASSW L"Vim"
4077
4078/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4079 * thus there should be room for every dialog. For tearoffs it's made bigger
4080 * when needed. */
4081#define DLG_ALLOC_SIZE 16 * 1024
4082
4083/*
4084 * stuff for dialogs, menus, tearoffs etc.
4085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086static PWORD
4087add_dialog_element(
4088 PWORD p,
4089 DWORD lStyle,
4090 WORD x,
4091 WORD y,
4092 WORD w,
4093 WORD h,
4094 WORD Id,
4095 WORD clss,
4096 const char *caption);
4097static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004098static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004099#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102static void get_dialog_font_metrics(void);
4103
4104static int dialog_default_button = -1;
4105
4106/* Intellimouse support */
4107static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
4109static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4110#ifdef FEAT_TOOLBAR
4111static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004112static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113static int get_toolbar_bitmap(vimmenu_T *menu);
4114#endif
4115
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004116#ifdef FEAT_GUI_TABLINE
4117static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004118static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004119#endif
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121#ifdef FEAT_MBYTE_IME
4122static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4123static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4124#endif
4125#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4126# ifdef NOIME
4127typedef struct tagCOMPOSITIONFORM {
4128 DWORD dwStyle;
4129 POINT ptCurrentPos;
4130 RECT rcArea;
4131} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4132typedef HANDLE HIMC;
4133# endif
4134
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004135static HINSTANCE hLibImm = NULL;
4136static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4137static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4138static HIMC (WINAPI *pImmGetContext)(HWND);
4139static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4140static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4141static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4142static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004143static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4144static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004145static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4146static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004147static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148static void dyn_imm_load(void);
4149#else
4150# define pImmGetCompositionStringA ImmGetCompositionStringA
4151# define pImmGetCompositionStringW ImmGetCompositionStringW
4152# define pImmGetContext ImmGetContext
4153# define pImmAssociateContext ImmAssociateContext
4154# define pImmReleaseContext ImmReleaseContext
4155# define pImmGetOpenStatus ImmGetOpenStatus
4156# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004157# define pImmGetCompositionFontW ImmGetCompositionFontW
4158# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159# define pImmSetCompositionWindow ImmSetCompositionWindow
4160# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004161# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162#endif
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#ifdef FEAT_MENU
4165/*
4166 * Figure out how high the menu bar is at the moment.
4167 */
4168 static int
4169gui_mswin_get_menu_height(
4170 int fix_window) /* If TRUE, resize window if menu height changed */
4171{
4172 static int old_menu_height = -1;
4173
4174 RECT rc1, rc2;
4175 int num;
4176 int menu_height;
4177
4178 if (gui.menu_is_active)
4179 num = GetMenuItemCount(s_menuBar);
4180 else
4181 num = 0;
4182
4183 if (num == 0)
4184 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004185 else if (IsMinimized(s_hwnd))
4186 {
4187 /* The height of the menu cannot be determined while the window is
4188 * minimized. Take the previous height if the menu is changed in that
4189 * state, to avoid that Vim's vertical window size accidentally
4190 * increases due to the unaccounted-for menu height. */
4191 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
4194 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004195 /*
4196 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4197 * seem to have been set yet, so menu wraps in default window
4198 * width which is very narrow. Instead just return height of a
4199 * single menu item. Will still be wrong when the menu really
4200 * should wrap over more than one line.
4201 */
4202 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4203 if (gui.starting)
4204 menu_height = rc1.bottom - rc1.top + 1;
4205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004207 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4208 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004213 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004214 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 return menu_height;
4217}
4218#endif /*FEAT_MENU*/
4219
4220
4221/*
4222 * Setup for the Intellimouse
4223 */
4224 static void
4225init_mouse_wheel(void)
4226{
4227
4228#ifndef SPI_GETWHEELSCROLLLINES
4229# define SPI_GETWHEELSCROLLLINES 104
4230#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004231#ifndef SPI_SETWHEELSCROLLLINES
4232# define SPI_SETWHEELSCROLLLINES 105
4233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4236#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4237#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4238#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4239
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 mouse_scroll_lines = 3; /* reasonable default */
4241
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004242 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4243 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4244 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245}
4246
4247
Bram Moolenaarae20f342019-11-05 21:09:23 +01004248/*
4249 * Intellimouse wheel handler.
4250 * Treat a mouse wheel event as if it were a scroll request.
4251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 static void
4253_OnMouseWheel(
4254 HWND hwnd,
4255 short zDelta)
4256{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int i;
4258 int size;
4259 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004260 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (mouse_scroll_lines == 0)
4263 init_mouse_wheel();
4264
Bram Moolenaarae20f342019-11-05 21:09:23 +01004265 wp = gui_mouse_window(FIND_POPUP);
4266
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004267#ifdef FEAT_TEXT_PROP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004268 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004269 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004270 cmdarg_T cap;
4271 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004272
Bram Moolenaarae20f342019-11-05 21:09:23 +01004273 // Mouse hovers over popup window, scroll it if possible.
4274 mouse_row = wp->w_winrow;
4275 mouse_col = wp->w_wincol;
4276 vim_memset(&cap, 0, sizeof(cap));
4277 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4278 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4279 clear_oparg(&oa);
4280 cap.oap = &oa;
4281 nv_mousescroll(&cap);
4282 update_screen(0);
4283 setcursor();
4284 out_flush();
4285 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004286 }
4287#endif
4288
Bram Moolenaarae20f342019-11-05 21:09:23 +01004289 if (wp == NULL || !p_scf)
4290 wp = curwin;
4291
4292 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4293 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4294 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4295 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4296 else
4297 return;
4298 size = wp->w_height;
4299
Bram Moolenaara338adc2018-01-31 20:51:47 +01004300 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 if (mouse_scroll_lines > 0
4302 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4303 {
4304 for (i = mouse_scroll_lines; i > 0; --i)
4305 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4306 }
4307 else
4308 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004309 mch_enable_flush();
4310 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311}
4312
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004313#ifdef USE_SYSMENU_FONT
4314/*
4315 * Get Menu Font.
4316 * Return OK or FAIL.
4317 */
4318 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004319gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004320{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004321 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004322
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004323 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4324 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004325 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004326 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004327 &nm,
4328 0))
4329 return FAIL;
4330 *lf = nm.lfMenuFont;
4331 return OK;
4332}
4333#endif
4334
4335
4336#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4337/*
4338 * Set the GUI tabline font to the system menu font
4339 */
4340 static void
4341set_tabline_font(void)
4342{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004343 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004344 HFONT font;
4345 HWND hwnd;
4346 HDC hdc;
4347 HFONT hfntOld;
4348 TEXTMETRIC tm;
4349
4350 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4351 return;
4352
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004353 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004354
4355 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4356
4357 /*
4358 * Compute the height of the font used for the tab text
4359 */
4360 hwnd = GetDesktopWindow();
4361 hdc = GetWindowDC(hwnd);
4362 hfntOld = SelectFont(hdc, font);
4363
4364 GetTextMetrics(hdc, &tm);
4365
4366 SelectFont(hdc, hfntOld);
4367 ReleaseDC(hwnd, hdc);
4368
4369 /*
4370 * The space used by the tab border and the space between the tab label
4371 * and the tab border is included as 7.
4372 */
4373 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4374}
4375#endif
4376
Bram Moolenaar520470a2005-06-16 21:59:56 +00004377/*
4378 * Invoked when a setting was changed.
4379 */
4380 static LRESULT CALLBACK
4381_OnSettingChange(UINT n)
4382{
4383 if (n == SPI_SETWHEELSCROLLLINES)
4384 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4385 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004386#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4387 if (n == SPI_SETNONCLIENTMETRICS)
4388 set_tabline_font();
4389#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004390 return 0;
4391}
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#ifdef FEAT_NETBEANS_INTG
4394 static void
4395_OnWindowPosChanged(
4396 HWND hwnd,
4397 const LPWINDOWPOS lpwpos)
4398{
4399 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004400 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401
4402 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4403 || lpwpos->cx != cx || lpwpos->cy != cy))
4404 {
4405 x = lpwpos->x;
4406 y = lpwpos->y;
4407 cx = lpwpos->cx;
4408 cy = lpwpos->cy;
4409 netbeans_frame_moved(x, y);
4410 }
4411 /* Allow to send WM_SIZE and WM_MOVE */
4412 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4413}
4414#endif
4415
4416 static int
4417_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 UINT fwSide,
4419 LPRECT lprc)
4420{
4421 int w, h;
4422 int valid_w, valid_h;
4423 int w_offset, h_offset;
4424
4425 w = lprc->right - lprc->left;
4426 h = lprc->bottom - lprc->top;
4427 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4428 w_offset = w - valid_w;
4429 h_offset = h - valid_h;
4430
4431 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4432 || fwSide == WMSZ_BOTTOMLEFT)
4433 lprc->left += w_offset;
4434 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4435 || fwSide == WMSZ_BOTTOMRIGHT)
4436 lprc->right -= w_offset;
4437
4438 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4439 || fwSide == WMSZ_TOPRIGHT)
4440 lprc->top += h_offset;
4441 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4442 || fwSide == WMSZ_BOTTOMRIGHT)
4443 lprc->bottom -= h_offset;
4444 return TRUE;
4445}
4446
4447
4448
4449 static LRESULT CALLBACK
4450_WndProc(
4451 HWND hwnd,
4452 UINT uMsg,
4453 WPARAM wParam,
4454 LPARAM lParam)
4455{
4456 /*
4457 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4458 hwnd, uMsg, wParam, lParam);
4459 */
4460
4461 HandleMouseHide(uMsg, lParam);
4462
4463 s_uMsg = uMsg;
4464 s_wParam = wParam;
4465 s_lParam = lParam;
4466
4467 switch (uMsg)
4468 {
4469 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4470 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4471 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4472 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4473 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4474 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4475 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4476 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4477 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4478#ifdef FEAT_MENU
4479 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4480#endif
4481 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4482 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4483 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4484 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4485 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4486 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4487 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4488 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4489 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4490#ifdef FEAT_NETBEANS_INTG
4491 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4492#endif
4493
Bram Moolenaarafa24992006-03-27 20:58:26 +00004494#ifdef FEAT_GUI_TABLINE
4495 case WM_RBUTTONUP:
4496 {
4497 if (gui_mch_showing_tabline())
4498 {
4499 POINT pt;
4500 RECT rect;
4501
4502 /*
4503 * If the cursor is on the tabline, display the tab menu
4504 */
4505 GetCursorPos((LPPOINT)&pt);
4506 GetWindowRect(s_textArea, &rect);
4507 if (pt.y < rect.top)
4508 {
4509 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004510 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004511 }
4512 }
4513 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4514 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004515 case WM_LBUTTONDBLCLK:
4516 {
4517 /*
4518 * If the user double clicked the tabline, create a new tab
4519 */
4520 if (gui_mch_showing_tabline())
4521 {
4522 POINT pt;
4523 RECT rect;
4524
4525 GetCursorPos((LPPOINT)&pt);
4526 GetWindowRect(s_textArea, &rect);
4527 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004528 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004529 }
4530 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4531 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004532#endif
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 case WM_QUERYENDSESSION: /* System wants to go down. */
4535 gui_shell_closed(); /* Will exit when no changed buffers. */
4536 return FALSE; /* Do NOT allow system to go down. */
4537
4538 case WM_ENDSESSION:
4539 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004542 return 0L;
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 break;
4545
4546 case WM_CHAR:
4547 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4548 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004549 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return 0L;
4551
4552 case WM_SYSCHAR:
4553 /*
4554 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4555 * shortcut key, handle like a typed ALT key, otherwise call Windows
4556 * ALT key handling.
4557 */
4558#ifdef FEAT_MENU
4559 if ( !gui.menu_is_active
4560 || p_wak[0] == 'n'
4561 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4562 )
4563#endif
4564 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004565 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 return 0L;
4567 }
4568#ifdef FEAT_MENU
4569 else
4570 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4571#endif
4572
4573 case WM_SYSKEYUP:
4574#ifdef FEAT_MENU
4575 /* This used to be done only when menu is active: ALT key is used for
4576 * that. But that caused problems when menu is disabled and using
4577 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4578 * are received, mouse pointer remains hidden. */
4579 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4580#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004581 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#endif
4583
4584 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004585 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 case WM_MOUSEWHEEL:
4588 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004589 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar520470a2005-06-16 21:59:56 +00004591 /* Notification for change in SystemParametersInfo() */
4592 case WM_SETTINGCHANGE:
4593 return _OnSettingChange((UINT)wParam);
4594
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004595#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 case WM_NOTIFY:
4597 switch (((LPNMHDR) lParam)->code)
4598 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004599 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004600 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004602 LPNMHDR hdr = (LPNMHDR)lParam;
4603 char_u *str = NULL;
4604 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004605
Bram Moolenaard23a8232018-02-10 18:45:26 +01004606 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004607
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004608# ifdef FEAT_GUI_TABLINE
4609 if (gui_mch_showing_tabline()
4610 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004611 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004612 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004613 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004614 * Mouse is over the GUI tabline. Display the
4615 * tooltip for the tab under the cursor
4616 *
4617 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004618 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004619 GetCursorPos(&pt);
4620 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004621 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004622 TCHITTESTINFO htinfo;
4623 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004624
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004625 /*
4626 * Get the tab under the cursor
4627 */
4628 htinfo.pt.x = pt.x;
4629 htinfo.pt.y = pt.y;
4630 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4631 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004632 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004633 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004634
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004635 tp = find_tabpage(idx + 1);
4636 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004637 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004638 get_tabline_label(tp, TRUE);
4639 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004640 }
4641 }
4642 }
4643 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004645# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004646# ifdef FEAT_GUI_TABLINE
4647 else
4648# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 UINT idButton;
4651 vimmenu_T *pMenu;
4652
4653 idButton = (UINT) hdr->idFrom;
4654 pMenu = gui_mswin_find_menu(root_menu, idButton);
4655 if (pMenu)
4656 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004658# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004659 if (str != NULL)
4660 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004661 if (hdr->code == TTN_GETDISPINFOW)
4662 {
4663 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4664
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004665 /* Set the maximum width, this also enables using
4666 * \n for line break. */
4667 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4668 0, 500);
4669
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004670 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004671 lpdi->lpszText = tt_text;
4672 /* can't show tooltip if failed */
4673 }
4674 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004675 {
4676 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4677
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004678 /* Set the maximum width, this also enables using
4679 * \n for line break. */
4680 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4681 0, 500);
4682
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004683 if (STRLEN(str) < sizeof(lpdi->szText)
4684 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004685 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004686 sizeof(lpdi->szText) - 1);
4687 else
4688 lpdi->lpszText = tt_text;
4689 }
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004693# ifdef FEAT_GUI_TABLINE
4694 case TCN_SELCHANGE:
4695 if (gui_mch_showing_tabline()
4696 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004697 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004698 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004699 return 0L;
4700 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004701 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004702
4703 case NM_RCLICK:
4704 if (gui_mch_showing_tabline()
4705 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004706 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004707 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004708 return 0L;
4709 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004710 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004713# ifdef FEAT_GUI_TABLINE
4714 if (gui_mch_showing_tabline()
4715 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4716 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4717# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 break;
4719 }
4720 break;
4721#endif
4722#if defined(MENUHINTS) && defined(FEAT_MENU)
4723 case WM_MENUSELECT:
4724 if (((UINT) HIWORD(wParam)
4725 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4726 == MF_HILITE
4727 && (State & CMDLINE) == 0)
4728 {
4729 UINT idButton;
4730 vimmenu_T *pMenu;
4731 static int did_menu_tip = FALSE;
4732
4733 if (did_menu_tip)
4734 {
4735 msg_clr_cmdline();
4736 setcursor();
4737 out_flush();
4738 did_menu_tip = FALSE;
4739 }
4740
4741 idButton = (UINT)LOWORD(wParam);
4742 pMenu = gui_mswin_find_menu(root_menu, idButton);
4743 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4744 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4745 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004746 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004747 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004748 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 setcursor();
4750 out_flush();
4751 did_menu_tip = TRUE;
4752 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004753 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755 break;
4756#endif
4757 case WM_NCHITTEST:
4758 {
4759 LRESULT result;
4760 int x, y;
4761 int xPos = GET_X_LPARAM(lParam);
4762
4763 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4764 if (result == HTCLIENT)
4765 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004766#ifdef FEAT_GUI_TABLINE
4767 if (gui_mch_showing_tabline())
4768 {
4769 int yPos = GET_Y_LPARAM(lParam);
4770 RECT rct;
4771
4772 /* If the cursor is on the GUI tabline, don't process this
4773 * event */
4774 GetWindowRect(s_textArea, &rct);
4775 if (yPos < rct.top)
4776 return result;
4777 }
4778#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004779 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 xPos -= x;
4781
4782 if (xPos < 48) /* <VN> TODO should use system metric? */
4783 return HTBOTTOMLEFT;
4784 else
4785 return HTBOTTOMRIGHT;
4786 }
4787 else
4788 return result;
4789 }
4790 /* break; notreached */
4791
4792#ifdef FEAT_MBYTE_IME
4793 case WM_IME_NOTIFY:
4794 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4795 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004796 return 1L;
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 case WM_IME_COMPOSITION:
4799 if (!_OnImeComposition(hwnd, wParam, lParam))
4800 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004801 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802#endif
4803
4804 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004806 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808#endif
4809 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4810 }
4811
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004812 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813}
4814
4815/*
4816 * End of call-back routines
4817 */
4818
4819/* parent window, if specified with -P */
4820HWND vim_parent_hwnd = NULL;
4821
4822 static BOOL CALLBACK
4823FindWindowTitle(HWND hwnd, LPARAM lParam)
4824{
4825 char buf[2048];
4826 char *title = (char *)lParam;
4827
4828 if (GetWindowText(hwnd, buf, sizeof(buf)))
4829 {
4830 if (strstr(buf, title) != NULL)
4831 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004832 /* Found it. Store the window ref. and quit searching if MDI
4833 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004835 if (vim_parent_hwnd != NULL)
4836 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 }
4839 return TRUE; /* continue searching */
4840}
4841
4842/*
4843 * Invoked for '-P "title"' argument: search for parent application to open
4844 * our window in.
4845 */
4846 void
4847gui_mch_set_parent(char *title)
4848{
4849 EnumWindows(FindWindowTitle, (LPARAM)title);
4850 if (vim_parent_hwnd == NULL)
4851 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004852 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 mch_exit(2);
4854 }
4855}
4856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 static void
4859ole_error(char *arg)
4860{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004861 char buf[IOSIZE];
4862
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004863# ifdef VIMDLL
4864 gui.in_use = mch_is_gui_executable();
4865# endif
4866
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004867 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004868 vim_snprintf(buf, IOSIZE,
4869 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4870 arg);
4871 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004875#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4876 static char *
4877gvim_error(void)
4878{
4879 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4880
4881 if (starting)
4882 {
4883 mch_errmsg(msg);
4884 mch_errmsg("\n");
4885 mch_exit(2);
4886 }
4887 return msg;
4888}
4889
4890 char *
4891gui_mch_do_spawn(char_u *arg)
4892{
4893 int len;
4894# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4895 char_u *session = NULL;
4896 LPWSTR tofree1 = NULL;
4897# endif
4898 WCHAR name[MAX_PATH];
4899 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4900 STARTUPINFOW si = {sizeof(si)};
4901 PROCESS_INFORMATION pi;
4902
4903 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4904 goto error;
4905 p = wcsrchr(name, L'\\');
4906 if (p == NULL)
4907 goto error;
4908 // Replace the executable name from vim(d).exe to gvim(d).exe.
4909# ifdef DEBUG
4910 wcscpy(p + 1, L"gvimd.exe");
4911# else
4912 wcscpy(p + 1, L"gvim.exe");
4913# endif
4914
4915# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4916 if (starting)
4917# endif
4918 {
4919 // Pass the command line to the new process.
4920 p = GetCommandLineW();
4921 // Skip 1st argument.
4922 while (*p && *p != L' ' && *p != L'\t')
4923 {
4924 if (*p == L'"')
4925 {
4926 while (*p && *p != L'"')
4927 ++p;
4928 if (*p)
4929 ++p;
4930 }
4931 else
4932 ++p;
4933 }
4934 cmd = p;
4935 }
4936# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4937 else
4938 {
4939 // Create a session file and pass it to the new process.
4940 LPWSTR wsession;
4941 char_u *savebg;
4942 int ret;
4943
4944 session = vim_tempname('s', FALSE);
4945 if (session == NULL)
4946 goto error;
4947 savebg = p_bg;
4948 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4949 ret = write_session_file(session);
4950 vim_free(p_bg);
4951 p_bg = savebg;
4952 if (!ret)
4953 goto error;
4954 wsession = enc_to_utf16(session, NULL);
4955 if (wsession == NULL)
4956 goto error;
4957 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004958 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004959 if (cmd == NULL)
4960 {
4961 vim_free(wsession);
4962 goto error;
4963 }
4964 tofree1 = cmd;
4965 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4966 wsession, wsession);
4967 vim_free(wsession);
4968 }
4969# endif
4970
4971 // Check additional arguments to the `:gui` command.
4972 if (arg != NULL)
4973 {
4974 warg = enc_to_utf16(arg, NULL);
4975 if (warg == NULL)
4976 goto error;
4977 tofree2 = warg;
4978 }
4979 else
4980 warg = L"";
4981
4982 // Set up the new command line.
4983 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004984 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004985 if (newcmd == NULL)
4986 goto error;
4987 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
4988
4989 // Spawn a new GUI process.
4990 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
4991 NULL, NULL, &si, &pi))
4992 goto error;
4993 CloseHandle(pi.hProcess);
4994 CloseHandle(pi.hThread);
4995 mch_exit(0);
4996
4997error:
4998# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4999 if (session)
5000 mch_remove(session);
5001 vim_free(session);
5002 vim_free(tofree1);
5003# endif
5004 vim_free(newcmd);
5005 vim_free(tofree2);
5006 return gvim_error();
5007}
5008#endif
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * Parse the GUI related command-line arguments. Any arguments used are
5012 * deleted from argv, and *argc is decremented accordingly. This is called
5013 * when vim is started, whether or not the GUI has been started.
5014 */
5015 void
5016gui_mch_prepare(int *argc, char **argv)
5017{
5018 int silent = FALSE;
5019 int idx;
5020
5021 /* Check for special OLE command line parameters */
5022 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5023 {
5024 /* Check for a "-silent" argument first. */
5025 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5026 && (argv[2][0] == '-' || argv[2][0] == '/'))
5027 {
5028 silent = TRUE;
5029 idx = 2;
5030 }
5031 else
5032 idx = 1;
5033
5034 /* Register Vim as an OLE Automation server */
5035 if (STRICMP(argv[idx] + 1, "register") == 0)
5036 {
5037#ifdef FEAT_OLE
5038 RegisterMe(silent);
5039 mch_exit(0);
5040#else
5041 if (!silent)
5042 ole_error("register");
5043 mch_exit(2);
5044#endif
5045 }
5046
5047 /* Unregister Vim as an OLE Automation server */
5048 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5049 {
5050#ifdef FEAT_OLE
5051 UnregisterMe(!silent);
5052 mch_exit(0);
5053#else
5054 if (!silent)
5055 ole_error("unregister");
5056 mch_exit(2);
5057#endif
5058 }
5059
5060 /* Ignore an -embedding argument. It is only relevant if the
5061 * application wants to treat the case when it is started manually
5062 * differently from the case where it is started via automation (and
5063 * we don't).
5064 */
5065 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5066 {
5067#ifdef FEAT_OLE
5068 *argc = 1;
5069#else
5070 ole_error("embedding");
5071 mch_exit(2);
5072#endif
5073 }
5074 }
5075
5076#ifdef FEAT_OLE
5077 {
5078 int bDoRestart = FALSE;
5079
5080 InitOLE(&bDoRestart);
5081 /* automatically exit after registering */
5082 if (bDoRestart)
5083 mch_exit(0);
5084 }
5085#endif
5086
5087#ifdef FEAT_NETBEANS_INTG
5088 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005089 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 int arg;
5091
5092 for (arg = 1; arg < *argc; arg++)
5093 if (strncmp("-nb", argv[arg], 3) == 0)
5094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 netbeansArg = argv[arg];
5096 mch_memmove(&argv[arg], &argv[arg + 1],
5097 (--*argc - arg) * sizeof(char *));
5098 argv[*argc] = NULL;
5099 break; /* enough? */
5100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103}
5104
5105/*
5106 * Initialise the GUI. Create all the windows, set up all the call-backs
5107 * etc.
5108 */
5109 int
5110gui_mch_init(void)
5111{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005113 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#ifdef GLOBAL_IME
5116 ATOM atom;
5117#endif
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 /* Return here if the window was already opened (happens when
5120 * gui_mch_dialog() is called early). */
5121 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005122 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 /*
5125 * Load the tearoff bitmap
5126 */
5127#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005128 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#endif
5130
5131 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5132 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5133#ifdef FEAT_MENU
5134 gui.menu_height = 0; /* Windows takes care of this */
5135#endif
5136 gui.border_width = 0;
5137
5138 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 /* First try using the wide version, so that we can use any title.
5141 * Otherwise only characters in the active codepage will work. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005142 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005144 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 wndclassw.lpfnWndProc = _WndProc;
5146 wndclassw.cbClsExtra = 0;
5147 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005148 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5150 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5151 wndclassw.hbrBackground = s_brush;
5152 wndclassw.lpszMenuName = NULL;
5153 wndclassw.lpszClassName = szVimWndClassW;
5154
5155 if ((
5156#ifdef GLOBAL_IME
5157 atom =
5158#endif
5159 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005160 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (vim_parent_hwnd != NULL)
5164 {
5165#ifdef HAVE_TRY_EXCEPT
5166 __try
5167 {
5168#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005169 // Open inside the specified parent window.
5170 // TODO: last argument should point to a CLIENTCREATESTRUCT
5171 // structure.
5172 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005174 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005175 WS_OVERLAPPEDWINDOW | WS_CHILD
5176 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5178 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005179 100, // Any value will do
5180 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005182 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#ifdef HAVE_TRY_EXCEPT
5184 }
5185 __except(EXCEPTION_EXECUTE_HANDLER)
5186 {
5187 /* NOP */
5188 }
5189#endif
5190 if (s_hwnd == NULL)
5191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005192 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 mch_exit(2);
5194 }
5195 }
5196 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005197 {
5198 /* If the provided windowid is not valid reset it to zero, so that it
5199 * is ignored and we open our own window. */
5200 if (IsWindow((HWND)win_socket_id) <= 0)
5201 win_socket_id = 0;
5202
5203 /* Create a window. If win_socket_id is not zero without border and
5204 * titlebar, it will be reparented below. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005205 s_hwnd = CreateWindowW(
5206 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005207 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5208 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005209 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5210 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5211 100, /* Any value will do */
5212 100, /* Any value will do */
5213 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005214 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005215 if (s_hwnd != NULL && win_socket_id != 0)
5216 {
5217 SetParent(s_hwnd, (HWND)win_socket_id);
5218 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5219 }
5220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 if (s_hwnd == NULL)
5223 return FAIL;
5224
5225#ifdef GLOBAL_IME
5226 global_ime_init(atom, s_hwnd);
5227#endif
5228#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5229 dyn_imm_load();
5230#endif
5231
5232 /* Create the text area window */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005233 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005234 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005235 wndclassw.style = CS_OWNDC;
5236 wndclassw.lpfnWndProc = _TextAreaWndProc;
5237 wndclassw.cbClsExtra = 0;
5238 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005239 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005240 wndclassw.hIcon = NULL;
5241 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5242 wndclassw.hbrBackground = NULL;
5243 wndclassw.lpszMenuName = NULL;
5244 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005245
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005246 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return FAIL;
5248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005250 s_textArea = CreateWindowExW(
5251 0,
5252 szTextAreaClassW, L"Vim text area",
5253 WS_CHILD | WS_VISIBLE, 0, 0,
5254 100, // Any value will do for now
5255 100, // Any value will do for now
5256 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005257 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 if (s_textArea == NULL)
5260 return FAIL;
5261
Bram Moolenaar20321902016-02-17 12:30:17 +01005262#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005263 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5264 {
5265 HANDLE hIcon = NULL;
5266
5267 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005268 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005269 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005270#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#ifdef FEAT_MENU
5273 s_menuBar = CreateMenu();
5274#endif
5275 s_hdc = GetDC(s_textArea);
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279 /* Do we need to bother with this? */
5280 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5281
5282 /* Get background/foreground colors from the system */
5283 gui_mch_def_colors();
5284
5285 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5286 * file) */
5287 set_normal_colors();
5288
5289 /*
5290 * Check that none of the colors are the same as the background color.
5291 * Then store the current values as the defaults.
5292 */
5293 gui_check_colors();
5294 gui.def_norm_pixel = gui.norm_pixel;
5295 gui.def_back_pixel = gui.back_pixel;
5296
5297 /* Get the colors for the highlight groups (gui_check_colors() might have
5298 * changed them) */
5299 highlight_gui_started();
5300
5301 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005302 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005304 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 /*
5307 * Set up for Intellimouse processing
5308 */
5309 init_mouse_wheel();
5310
5311 /*
5312 * compute a couple of metrics used for the dialogs
5313 */
5314 get_dialog_font_metrics();
5315#ifdef FEAT_TOOLBAR
5316 /*
5317 * Create the toolbar
5318 */
5319 initialise_toolbar();
5320#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005321#ifdef FEAT_GUI_TABLINE
5322 /*
5323 * Create the tabline
5324 */
5325 initialise_tabline();
5326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327#ifdef MSWIN_FIND_REPLACE
5328 /*
5329 * Initialise the dialog box stuff
5330 */
5331 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5332
5333 /* Initialise the struct */
5334 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005335 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005337 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5339 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5340 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005343#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005344# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5345/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5346# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005347# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005348# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005349# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005350 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005351 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005352#endif
5353
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005354#ifdef FEAT_RENDER_OPTIONS
5355 if (p_rop)
5356 (void)gui_mch_set_rendering_options(p_rop);
5357#endif
5358
Bram Moolenaar748bf032005-02-02 23:04:36 +00005359theend:
5360 /* Display any pending error messages */
5361 display_errors();
5362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return OK;
5364}
5365
5366/*
5367 * Get the size of the screen, taking position on multiple monitors into
5368 * account (if supported).
5369 */
5370 static void
5371get_work_area(RECT *spi_rect)
5372{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005373 HMONITOR mon;
5374 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005376 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005377 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005378 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005380 moninfo.cbSize = sizeof(MONITORINFO);
5381 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005383 *spi_rect = moninfo.rcWork;
5384 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 }
5386 }
5387 /* this is the old method... */
5388 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5389}
5390
5391/*
5392 * Set the size of the window to the given width and height in pixels.
5393 */
5394 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005395gui_mch_set_shellsize(
5396 int width,
5397 int height,
5398 int min_width UNUSED,
5399 int min_height UNUSED,
5400 int base_width UNUSED,
5401 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005402 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 RECT workarea_rect;
5405 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 WINDOWPLACEMENT wndpl;
5407
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005408 /* Try to keep window completely on screen. */
5409 /* Get position of the screen work area. This is the part that is not
5410 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 get_work_area(&workarea_rect);
5412
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005413 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005414 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 wndpl.length = sizeof(WINDOWPLACEMENT);
5416 GetWindowPlacement(s_hwnd, &wndpl);
5417
5418 /* Resizing a maximized window looks very strange, unzoom it first.
5419 * But don't do it when still starting up, it may have been requested in
5420 * the shortcut. */
5421 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5422 {
5423 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5424 /* Need to get the settings of the normal window. */
5425 GetWindowPlacement(s_hwnd, &wndpl);
5426 }
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005429 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005430 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005431 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005432 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 + GetSystemMetrics(SM_CYCAPTION)
5434#ifdef FEAT_MENU
5435 + gui_mswin_get_menu_height(FALSE)
5436#endif
5437 ;
5438
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005439 /* The following should take care of keeping Vim on the same monitor, no
5440 * matter if the secondary monitor is left or right of the primary
5441 * monitor. */
5442 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5443 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005444
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005445 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005446 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005447 && wndpl.rcNormalPosition.right > workarea_rect.right)
5448 OffsetRect(&wndpl.rcNormalPosition,
5449 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005451 if ((direction & RESIZE_HOR)
5452 && wndpl.rcNormalPosition.left < workarea_rect.left)
5453 OffsetRect(&wndpl.rcNormalPosition,
5454 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaarafa24992006-03-27 20:58:26 +00005456 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005457 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5458 OffsetRect(&wndpl.rcNormalPosition,
5459 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005461 if ((direction & RESIZE_VERT)
5462 && wndpl.rcNormalPosition.top < workarea_rect.top)
5463 OffsetRect(&wndpl.rcNormalPosition,
5464 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 /* set window position - we should use SetWindowPlacement rather than
5467 * SetWindowPos as the MSDN docs say the coord systems returned by
5468 * these two are not compatible. */
5469 SetWindowPlacement(s_hwnd, &wndpl);
5470
5471 SetActiveWindow(s_hwnd);
5472 SetFocus(s_hwnd);
5473
5474#ifdef FEAT_MENU
5475 /* Menu may wrap differently now */
5476 gui_mswin_get_menu_height(!gui.starting);
5477#endif
5478}
5479
5480
5481 void
5482gui_mch_set_scrollbar_thumb(
5483 scrollbar_T *sb,
5484 long val,
5485 long size,
5486 long max)
5487{
5488 SCROLLINFO info;
5489
5490 sb->scroll_shift = 0;
5491 while (max > 32767)
5492 {
5493 max = (max + 1) >> 1;
5494 val >>= 1;
5495 size >>= 1;
5496 ++sb->scroll_shift;
5497 }
5498
5499 if (sb->scroll_shift > 0)
5500 ++size;
5501
5502 info.cbSize = sizeof(info);
5503 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5504 info.nPos = val;
5505 info.nMin = 0;
5506 info.nMax = max;
5507 info.nPage = size;
5508 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5509}
5510
5511
5512/*
5513 * Set the current text font.
5514 */
5515 void
5516gui_mch_set_font(GuiFont font)
5517{
5518 gui.currFont = font;
5519}
5520
5521
5522/*
5523 * Set the current text foreground color.
5524 */
5525 void
5526gui_mch_set_fg_color(guicolor_T color)
5527{
5528 gui.currFgColor = color;
5529}
5530
5531/*
5532 * Set the current text background color.
5533 */
5534 void
5535gui_mch_set_bg_color(guicolor_T color)
5536{
5537 gui.currBgColor = color;
5538}
5539
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005540/*
5541 * Set the current text special color.
5542 */
5543 void
5544gui_mch_set_sp_color(guicolor_T color)
5545{
5546 gui.currSpColor = color;
5547}
5548
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005549#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550/*
5551 * Multi-byte handling, originally by Sung-Hoon Baek.
5552 * First static functions (no prototypes generated).
5553 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005554# ifdef _MSC_VER
Bram Moolenaareae1b912019-05-09 15:12:55 +02005555# include <ime.h> /* Apparently not needed for Cygwin or MinGW. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005556# endif
5557# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 * handle WM_IME_NOTIFY message
5561 */
5562 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005563_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 LRESULT lResult = 0;
5566 HIMC hImc;
5567
5568 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5569 return lResult;
5570 switch (dwCommand)
5571 {
5572 case IMN_SETOPENSTATUS:
5573 if (pImmGetOpenStatus(hImc))
5574 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005575 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 im_set_position(gui.row, gui.col);
5577
5578 /* Disable langmap */
5579 State &= ~LANGMAP;
5580 if (State & INSERT)
5581 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005582#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 /* Unshown 'keymap' in status lines */
5584 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5585 {
5586 /* Save cursor position */
5587 int old_row = gui.row;
5588 int old_col = gui.col;
5589
5590 // This must be called here before
5591 // status_redraw_curbuf(), otherwise the mode
5592 // message may appear in the wrong position.
5593 showmode();
5594 status_redraw_curbuf();
5595 update_screen(0);
5596 /* Restore cursor position */
5597 gui.row = old_row;
5598 gui.col = old_col;
5599 }
5600#endif
5601 }
5602 }
5603 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005604 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 lResult = 0;
5606 break;
5607 }
5608 pImmReleaseContext(hWnd, hImc);
5609 return lResult;
5610}
5611
5612 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005613_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 char_u *ret;
5616 int len;
5617
5618 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5619 return 0;
5620
5621 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5622 if (ret != NULL)
5623 {
5624 add_to_input_buf_csi(ret, len);
5625 vim_free(ret);
5626 return 1;
5627 }
5628 return 0;
5629}
5630
5631/*
5632 * get the current composition string, in UCS-2; *lenp is the number of
5633 * *lenp is the number of Unicode characters.
5634 */
5635 static short_u *
5636GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5637{
5638 LONG ret;
5639 LPWSTR wbuf = NULL;
5640 char_u *buf;
5641
5642 if (!pImmGetContext)
5643 return NULL; /* no imm32.dll */
5644
5645 /* Try Unicode; this'll always work on NT regardless of codepage. */
5646 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5647 if (ret == 0)
5648 return NULL; /* empty */
5649
5650 if (ret > 0)
5651 {
5652 /* Allocate the requested buffer plus space for the NUL character. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005653 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (wbuf != NULL)
5655 {
5656 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5657 *lenp = ret / sizeof(WCHAR);
5658 }
5659 return (short_u *)wbuf;
5660 }
5661
5662 /* ret < 0; we got an error, so try the ANSI version. This'll work
5663 * on 9x/ME, but only if the codepage happens to be set to whatever
5664 * we're inputting. */
5665 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5666 if (ret <= 0)
5667 return NULL; /* empty or error */
5668
5669 buf = alloc(ret);
5670 if (buf == NULL)
5671 return NULL;
5672 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5673
5674 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005675 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 vim_free(buf);
5677
5678 return (short_u *)wbuf;
5679}
5680
5681/*
5682 * void GetResultStr()
5683 *
5684 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5685 * get complete composition string
5686 */
5687 static char_u *
5688GetResultStr(HWND hwnd, int GCS, int *lenp)
5689{
5690 HIMC hIMC; /* Input context handle. */
5691 short_u *buf = NULL;
5692 char_u *convbuf = NULL;
5693
5694 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5695 return NULL;
5696
5697 /* Reads in the composition string. */
5698 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5699 if (buf == NULL)
5700 return NULL;
5701
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005702 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 pImmReleaseContext(hwnd, hIMC);
5704 vim_free(buf);
5705 return convbuf;
5706}
5707#endif
5708
5709/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005710#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712/*
5713 * set font to IM.
5714 */
5715 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005716im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 HIMC hImc;
5719
5720 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5721 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005722 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 pImmReleaseContext(s_hwnd, hImc);
5724 }
5725}
5726
5727/*
5728 * Notify cursor position to IM.
5729 */
5730 void
5731im_set_position(int row, int col)
5732{
5733 HIMC hImc;
5734
5735 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5736 {
5737 COMPOSITIONFORM cfs;
5738
5739 cfs.dwStyle = CFS_POINT;
5740 cfs.ptCurrentPos.x = FILL_X(col);
5741 cfs.ptCurrentPos.y = FILL_Y(row);
5742 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5743 pImmSetCompositionWindow(hImc, &cfs);
5744
5745 pImmReleaseContext(s_hwnd, hImc);
5746 }
5747}
5748
5749/*
5750 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5751 */
5752 void
5753im_set_active(int active)
5754{
5755 HIMC hImc;
5756 static HIMC hImcOld = (HIMC)0;
5757
5758 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5759 {
5760 if (p_imdisable)
5761 {
5762 if (hImcOld == (HIMC)0)
5763 {
5764 hImcOld = pImmGetContext(s_hwnd);
5765 if (hImcOld)
5766 pImmAssociateContext(s_hwnd, (HIMC)0);
5767 }
5768 active = FALSE;
5769 }
5770 else if (hImcOld != (HIMC)0)
5771 {
5772 pImmAssociateContext(s_hwnd, hImcOld);
5773 hImcOld = (HIMC)0;
5774 }
5775
5776 hImc = pImmGetContext(s_hwnd);
5777 if (hImc)
5778 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005779 /*
5780 * for Korean ime
5781 */
5782 HKL hKL = GetKeyboardLayout(0);
5783
5784 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5785 {
5786 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5787 static BOOL bSaved = FALSE;
5788
5789 if (active)
5790 {
5791 /* if we have a saved conversion status, restore it */
5792 if (bSaved)
5793 pImmSetConversionStatus(hImc, dwConversionSaved,
5794 dwSentenceSaved);
5795 bSaved = FALSE;
5796 }
5797 else
5798 {
5799 /* save conversion status and disable korean */
5800 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5801 &dwSentenceSaved))
5802 {
5803 bSaved = TRUE;
5804 pImmSetConversionStatus(hImc,
5805 dwConversionSaved & ~(IME_CMODE_NATIVE
5806 | IME_CMODE_FULLSHAPE),
5807 dwSentenceSaved);
5808 }
5809 }
5810 }
5811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 pImmSetOpenStatus(hImc, active);
5813 pImmReleaseContext(s_hwnd, hImc);
5814 }
5815 }
5816}
5817
5818/*
5819 * Get IM status. When IM is on, return not 0. Else return 0.
5820 */
5821 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005822im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 int status = 0;
5825 HIMC hImc;
5826
5827 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5828 {
5829 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5830 pImmReleaseContext(s_hwnd, hImc);
5831 }
5832 return status;
5833}
5834
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005835#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005837#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838/* Win32 with GLOBAL IME */
5839
5840/*
5841 * Notify cursor position to IM.
5842 */
5843 void
5844im_set_position(int row, int col)
5845{
5846 /* Win32 with GLOBAL IME */
5847 POINT p;
5848
5849 p.x = FILL_X(col);
5850 p.y = FILL_Y(row);
5851 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5852 global_ime_set_position(&p);
5853}
5854
5855/*
5856 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5857 */
5858 void
5859im_set_active(int active)
5860{
5861 global_ime_set_status(active);
5862}
5863
5864/*
5865 * Get IM status. When IM is on, return not 0. Else return 0.
5866 */
5867 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005868im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 return global_ime_get_status();
5871}
5872#endif
5873
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005874/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005875 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005876 */
5877 static void
5878latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5879{
5880 int c;
5881
Bram Moolenaarca003e12006-03-17 23:19:38 +00005882 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005883 {
5884 c = *text++;
5885 switch (c)
5886 {
5887 case 0xa4: c = 0x20ac; break; /* euro */
5888 case 0xa6: c = 0x0160; break; /* S hat */
5889 case 0xa8: c = 0x0161; break; /* S -hat */
5890 case 0xb4: c = 0x017d; break; /* Z hat */
5891 case 0xb8: c = 0x017e; break; /* Z -hat */
5892 case 0xbc: c = 0x0152; break; /* OE */
5893 case 0xbd: c = 0x0153; break; /* oe */
5894 case 0xbe: c = 0x0178; break; /* Y */
5895 }
5896 *unicodebuf++ = c;
5897 }
5898}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900#ifdef FEAT_RIGHTLEFT
5901/*
5902 * What is this for? In the case where you are using Win98 or Win2K or later,
5903 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5904 * reverses the string sent to the TextOut... family. This sucks, because we
5905 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5906 * way to tell Windblows not to do this!
5907 *
5908 * The short of it is that this 'RevOut' only gets called if you are running
5909 * one of the new, "improved" MS OSes, and only if you are running in
5910 * 'rightleft' mode. It makes display take *slightly* longer, but not
5911 * noticeably so.
5912 */
5913 static void
5914RevOut( HDC s_hdc,
5915 int col,
5916 int row,
5917 UINT foptions,
5918 CONST RECT *pcliprect,
5919 LPCTSTR text,
5920 UINT len,
5921 CONST INT *padding)
5922{
5923 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005925 for (ix = 0; ix < (int)len; ++ix)
5926 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5927 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928}
5929#endif
5930
Bram Moolenaar92467d32017-12-05 13:22:16 +01005931 static void
5932draw_line(
5933 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005934 int y1,
5935 int x2,
5936 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005937 COLORREF color)
5938{
5939#if defined(FEAT_DIRECTX)
5940 if (IS_ENABLE_DIRECTX())
5941 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5942 else
5943#endif
5944 {
5945 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5946 HPEN old_pen = SelectObject(s_hdc, hpen);
5947 MoveToEx(s_hdc, x1, y1, NULL);
5948 /* Note: LineTo() excludes the last pixel in the line. */
5949 LineTo(s_hdc, x2, y2);
5950 DeleteObject(SelectObject(s_hdc, old_pen));
5951 }
5952}
5953
5954 static void
5955set_pixel(
5956 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005957 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005958 COLORREF color)
5959{
5960#if defined(FEAT_DIRECTX)
5961 if (IS_ENABLE_DIRECTX())
5962 DWriteContext_SetPixel(s_dwc, x, y, color);
5963 else
5964#endif
5965 SetPixel(s_hdc, x, y, color);
5966}
5967
5968 static void
5969fill_rect(
5970 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005971 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005972 COLORREF color)
5973{
5974#if defined(FEAT_DIRECTX)
5975 if (IS_ENABLE_DIRECTX())
5976 DWriteContext_FillRect(s_dwc, rcp, color);
5977 else
5978#endif
5979 {
5980 HBRUSH hbr2;
5981
5982 if (hbr == NULL)
5983 hbr2 = CreateSolidBrush(color);
5984 else
5985 hbr2 = hbr;
5986 FillRect(s_hdc, rcp, hbr2);
5987 if (hbr == NULL)
5988 DeleteBrush(hbr2);
5989 }
5990}
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 void
5993gui_mch_draw_string(
5994 int row,
5995 int col,
5996 char_u *text,
5997 int len,
5998 int flags)
5999{
6000 static int *padding = NULL;
6001 static int pad_size = 0;
6002 int i;
6003 const RECT *pcliprect = NULL;
6004 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 static WCHAR *unicodebuf = NULL;
6006 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006007 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 int y;
6010
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 /*
6012 * Italic and bold text seems to have an extra row of pixels at the bottom
6013 * (below where the bottom of the character should be). If we draw the
6014 * characters with a solid background, the top row of pixels in the
6015 * character below will be overwritten. We can fix this by filling in the
6016 * background ourselves, to the correct character proportions, and then
6017 * writing the character in transparent mode. Still have a problem when
6018 * the character is "_", which gets written on to the character below.
6019 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6020 * pixel in their slots, which fixes the problem with the bottom row of
6021 * pixels. We still need this code because otherwise the top row of pixels
6022 * becomes a problem. - webb.
6023 */
6024 static HBRUSH hbr_cache[2] = {NULL, NULL};
6025 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6026 static int brush_lru = 0;
6027 HBRUSH hbr;
6028 RECT rc;
6029
6030 if (!(flags & DRAW_TRANSP))
6031 {
6032 /*
6033 * Clear background first.
6034 * Note: FillRect() excludes right and bottom of rectangle.
6035 */
6036 rc.left = FILL_X(col);
6037 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (has_mbyte)
6039 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006041 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 }
6043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 rc.right = FILL_X(col + len);
6045 rc.bottom = FILL_Y(row + 1);
6046
6047 /* Cache the created brush, that saves a lot of time. We need two:
6048 * one for cursor background and one for the normal background. */
6049 if (gui.currBgColor == brush_color[0])
6050 {
6051 hbr = hbr_cache[0];
6052 brush_lru = 1;
6053 }
6054 else if (gui.currBgColor == brush_color[1])
6055 {
6056 hbr = hbr_cache[1];
6057 brush_lru = 0;
6058 }
6059 else
6060 {
6061 if (hbr_cache[brush_lru] != NULL)
6062 DeleteBrush(hbr_cache[brush_lru]);
6063 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6064 brush_color[brush_lru] = gui.currBgColor;
6065 hbr = hbr_cache[brush_lru];
6066 brush_lru = !brush_lru;
6067 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006068
Bram Moolenaar92467d32017-12-05 13:22:16 +01006069 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 SetBkMode(s_hdc, TRANSPARENT);
6072
6073 /*
6074 * When drawing block cursor, prevent inverted character spilling
6075 * over character cell (can happen with bold/italic)
6076 */
6077 if (flags & DRAW_CURSOR)
6078 {
6079 pcliprect = &rc;
6080 foptions = ETO_CLIPPED;
6081 }
6082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 SetTextColor(s_hdc, gui.currFgColor);
6084 SelectFont(s_hdc, gui.currFont);
6085
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006086#ifdef FEAT_DIRECTX
6087 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006088 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006089#endif
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6092 {
6093 vim_free(padding);
6094 pad_size = Columns;
6095
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006096 /* Don't give an out-of-memory message here, it would call us
6097 * recursively. */
Bram Moolenaar59edb002019-05-28 23:32:47 +02006098 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (padding != NULL)
6100 for (i = 0; i < pad_size; i++)
6101 padding[i] = gui.char_width;
6102 }
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 /*
6105 * We have to provide the padding argument because italic and bold versions
6106 * of fixed-width fonts are often one pixel or so wider than their normal
6107 * versions.
6108 * No check for DRAW_BOLD, Windows will have done it already.
6109 */
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 /* Check if there are any UTF-8 characters. If not, use normal text
6112 * output to speed up output. */
6113 if (enc_utf8)
6114 for (n = 0; n < len; ++n)
6115 if (text[n] >= 0x80)
6116 break;
6117
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006118#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006119 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6120 * required that unicode drawing routine, currently. So this forces it
6121 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006122 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006123 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006124#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006127 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006129 if ((enc_utf8
6130 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6131 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 && (unicodebuf == NULL || len > unibuflen))
6133 {
6134 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006135 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006138 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 unibuflen = len;
6141 }
6142
6143 if (enc_utf8 && n < len && unicodebuf != NULL)
6144 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006145 /* Output UTF-8 characters. Composing characters should be
6146 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006147 int i;
6148 int wlen; /* string length in words */
6149 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 int cells; /* cell width of string up to composing char */
6151 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006152 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006154 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006155 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006157 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006159 c = utf_ptr2char(text + i);
6160 if (c >= 0x10000)
6161 {
6162 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006163 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6164 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006165 }
6166 else
6167 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006168 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006169 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006170
6171 if (utf_iscomposing(c))
6172 cw = 0;
6173 else
6174 {
6175 cw = utf_char2cells(c);
6176 if (cw > 2) /* don't use 4 for unprintable char */
6177 cw = 1;
6178 }
6179
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (unicodepdy != NULL)
6181 {
6182 /* Use unicodepdy to make characters fit as we expect, even
6183 * when the font uses different widths (e.g., bold character
6184 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006185 if (c >= 0x10000)
6186 {
6187 unicodepdy[wlen - 2] = cw * gui.char_width;
6188 unicodepdy[wlen - 1] = 0;
6189 }
6190 else
6191 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 }
6193 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006194 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006195 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006197#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006198 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006199 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006200 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006201 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006202 TEXT_X(col), TEXT_Y(row),
6203 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006204 gui.char_width, gui.currFgColor,
6205 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006206 }
6207 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006208#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006209 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6210 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 len = cells; /* used for underlining */
6212 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006213 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 /* If we want to display codepage data, and the current CP is not the
6216 * ANSI one, we need to go via Unicode. */
6217 if (unicodebuf != NULL)
6218 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006219 if (enc_latin9)
6220 latin9_to_ucs(text, len, unicodebuf);
6221 else
6222 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 MB_PRECOMPOSED,
6224 (char *)text, len,
6225 (LPWSTR)unicodebuf, unibuflen);
6226 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006227 {
6228 /* Use unicodepdy to make characters fit as we expect, even
6229 * when the font uses different widths (e.g., bold character
6230 * is wider). */
6231 if (unicodepdy != NULL)
6232 {
6233 int i;
6234 int cw;
6235
6236 for (i = 0; i < len; ++i)
6237 {
6238 cw = utf_char2cells(unicodebuf[i]);
6239 if (cw > 2)
6240 cw = 1;
6241 unicodepdy[i] = cw * gui.char_width;
6242 }
6243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006245 foptions, pcliprect, unicodebuf, len, unicodepdy);
6246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248 }
6249 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006252 /* Windows will mess up RL text, so we have to draw it character by
6253 * character. Only do this if RL is on, since it's slow. */
6254 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6256 foptions, pcliprect, (char *)text, len, padding);
6257 else
6258#endif
6259 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6260 foptions, pcliprect, (char *)text, len, padding);
6261 }
6262
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006263 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (flags & DRAW_UNDERL)
6265 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 /* When p_linespace is 0, overwrite the bottom row of pixels.
6267 * Otherwise put the line just below the character. */
6268 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (p_linespace > 1)
6270 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006271 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006273
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006274 /* Strikethrough */
6275 if (flags & DRAW_STRIKE)
6276 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006277 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006278 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006279 }
6280
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006281 /* Undercurl */
6282 if (flags & DRAW_UNDERC)
6283 {
6284 int x;
6285 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006286 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006287
6288 y = FILL_Y(row + 1) - 1;
6289 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6290 {
6291 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006292 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006293 }
6294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295}
6296
6297
6298/*
6299 * Output routines.
6300 */
6301
6302/* Flush any output to the screen */
6303 void
6304gui_mch_flush(void)
6305{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006306#if defined(FEAT_DIRECTX)
6307 if (IS_ENABLE_DIRECTX())
6308 DWriteContext_Flush(s_dwc);
6309#endif
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 GdiFlush();
6312}
6313
6314 static void
6315clear_rect(RECT *rcp)
6316{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006317 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318}
6319
6320
Bram Moolenaarc716c302006-01-21 22:12:51 +00006321 void
6322gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6323{
6324 RECT workarea_rect;
6325
6326 get_work_area(&workarea_rect);
6327
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006328 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006329 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006330 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006331
6332 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6333 * the menubar for MSwin, we subtract it from the screen height, so that
6334 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006335 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006336 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006337 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006338 - GetSystemMetrics(SM_CYCAPTION)
6339#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006340 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006341#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006342 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006343}
6344
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346#if defined(FEAT_MENU) || defined(PROTO)
6347/*
6348 * Add a sub menu to the menu bar.
6349 */
6350 void
6351gui_mch_add_menu(
6352 vimmenu_T *menu,
6353 int pos)
6354{
6355 vimmenu_T *parent = menu->parent;
6356
6357 menu->submenu_id = CreatePopupMenu();
6358 menu->id = s_menu_id++;
6359
6360 if (menu_is_menubar(menu->name))
6361 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006362 WCHAR *wn;
6363 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006365 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006366 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006367 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006369 infow.cbSize = sizeof(infow);
6370 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6371 | MIIM_SUBMENU;
6372 infow.dwItemData = (long_u)menu;
6373 infow.wID = menu->id;
6374 infow.fType = MFT_STRING;
6375 infow.dwTypeData = wn;
6376 infow.cch = (UINT)wcslen(wn);
6377 infow.hSubMenu = menu->submenu_id;
6378 InsertMenuItemW((parent == NULL)
6379 ? s_menuBar : parent->submenu_id,
6380 (UINT)pos, TRUE, &infow);
6381 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383
6384 /* Fix window size if menu may have wrapped */
6385 if (parent == NULL)
6386 gui_mswin_get_menu_height(!gui.starting);
6387#ifdef FEAT_TEAROFF
6388 else if (IsWindow(parent->tearoff_handle))
6389 rebuild_tearoff(parent);
6390#endif
6391}
6392
6393 void
6394gui_mch_show_popupmenu(vimmenu_T *menu)
6395{
6396 POINT mp;
6397
6398 (void)GetCursorPos((LPPOINT)&mp);
6399 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6400}
6401
6402 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006403gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 vimmenu_T *menu = gui_find_menu(path_name);
6406
6407 if (menu != NULL)
6408 {
6409 POINT p;
6410
6411 /* Find the position of the current cursor */
6412 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006413 if (mouse_pos)
6414 {
6415 int mx, my;
6416
6417 gui_mch_getmouse(&mx, &my);
6418 p.x += mx;
6419 p.y += my;
6420 }
6421 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006423 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6425 }
6426 msg_scroll = FALSE;
6427 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6428 }
6429}
6430
6431#if defined(FEAT_TEAROFF) || defined(PROTO)
6432/*
6433 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6434 * create it as a pseudo-"tearoff menu".
6435 */
6436 void
6437gui_make_tearoff(char_u *path_name)
6438{
6439 vimmenu_T *menu = gui_find_menu(path_name);
6440
6441 /* Found the menu, so tear it off. */
6442 if (menu != NULL)
6443 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6444}
6445#endif
6446
6447/*
6448 * Add a menu item to a menu
6449 */
6450 void
6451gui_mch_add_menu_item(
6452 vimmenu_T *menu,
6453 int idx)
6454{
6455 vimmenu_T *parent = menu->parent;
6456
6457 menu->id = s_menu_id++;
6458 menu->submenu_id = NULL;
6459
6460#ifdef FEAT_TEAROFF
6461 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6462 {
6463 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6464 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6465 }
6466 else
6467#endif
6468#ifdef FEAT_TOOLBAR
6469 if (menu_is_toolbar(parent->name))
6470 {
6471 TBBUTTON newtb;
6472
6473 vim_memset(&newtb, 0, sizeof(newtb));
6474 if (menu_is_separator(menu->name))
6475 {
6476 newtb.iBitmap = 0;
6477 newtb.fsStyle = TBSTYLE_SEP;
6478 }
6479 else
6480 {
6481 newtb.iBitmap = get_toolbar_bitmap(menu);
6482 newtb.fsStyle = TBSTYLE_BUTTON;
6483 }
6484 newtb.idCommand = menu->id;
6485 newtb.fsState = TBSTATE_ENABLED;
6486 newtb.iString = 0;
6487 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6488 (LPARAM)&newtb);
6489 menu->submenu_id = (HMENU)-1;
6490 }
6491 else
6492#endif
6493 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006494 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006496 wn = enc_to_utf16(menu->name, NULL);
6497 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006499 InsertMenuW(parent->submenu_id, (UINT)idx,
6500 (menu_is_separator(menu->name)
6501 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6502 (UINT)menu->id, wn);
6503 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505#ifdef FEAT_TEAROFF
6506 if (IsWindow(parent->tearoff_handle))
6507 rebuild_tearoff(parent);
6508#endif
6509 }
6510}
6511
6512/*
6513 * Destroy the machine specific menu widget.
6514 */
6515 void
6516gui_mch_destroy_menu(vimmenu_T *menu)
6517{
6518#ifdef FEAT_TOOLBAR
6519 /*
6520 * is this a toolbar button?
6521 */
6522 if (menu->submenu_id == (HMENU)-1)
6523 {
6524 int iButton;
6525
6526 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6527 (WPARAM)menu->id, 0);
6528 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6529 }
6530 else
6531#endif
6532 {
6533 if (menu->parent != NULL
6534 && menu_is_popup(menu->parent->dname)
6535 && menu->parent->submenu_id != NULL)
6536 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6537 else
6538 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6539 if (menu->submenu_id != NULL)
6540 DestroyMenu(menu->submenu_id);
6541#ifdef FEAT_TEAROFF
6542 if (IsWindow(menu->tearoff_handle))
6543 DestroyWindow(menu->tearoff_handle);
6544 if (menu->parent != NULL
6545 && menu->parent->children != NULL
6546 && IsWindow(menu->parent->tearoff_handle))
6547 {
6548 /* This menu must not show up when rebuilding the tearoff window. */
6549 menu->modes = 0;
6550 rebuild_tearoff(menu->parent);
6551 }
6552#endif
6553 }
6554}
6555
6556#ifdef FEAT_TEAROFF
6557 static void
6558rebuild_tearoff(vimmenu_T *menu)
6559{
6560 /*hackish*/
6561 char_u tbuf[128];
6562 RECT trect;
6563 RECT rct;
6564 RECT roct;
6565 int x, y;
6566
6567 HWND thwnd = menu->tearoff_handle;
6568
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006569 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 if (GetWindowRect(thwnd, &trect)
6571 && GetWindowRect(s_hwnd, &rct)
6572 && GetClientRect(s_hwnd, &roct))
6573 {
6574 x = trect.left - rct.left;
6575 y = (trect.top - rct.bottom + roct.bottom);
6576 }
6577 else
6578 {
6579 x = y = 0xffffL;
6580 }
6581 DestroyWindow(thwnd);
6582 if (menu->children != NULL)
6583 {
6584 gui_mch_tearoff(tbuf, menu, x, y);
6585 if (IsWindow(menu->tearoff_handle))
6586 (void) SetWindowPos(menu->tearoff_handle,
6587 NULL,
6588 (int)trect.left,
6589 (int)trect.top,
6590 0, 0,
6591 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6592 }
6593}
6594#endif /* FEAT_TEAROFF */
6595
6596/*
6597 * Make a menu either grey or not grey.
6598 */
6599 void
6600gui_mch_menu_grey(
6601 vimmenu_T *menu,
6602 int grey)
6603{
6604#ifdef FEAT_TOOLBAR
6605 /*
6606 * is this a toolbar button?
6607 */
6608 if (menu->submenu_id == (HMENU)-1)
6609 {
6610 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6611 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6612 }
6613 else
6614#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006615 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6616 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618#ifdef FEAT_TEAROFF
6619 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6620 {
6621 WORD menuID;
6622 HWND menuHandle;
6623
6624 /*
6625 * A tearoff button has changed state.
6626 */
6627 if (menu->children == NULL)
6628 menuID = (WORD)(menu->id);
6629 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006630 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6632 if (menuHandle)
6633 EnableWindow(menuHandle, !grey);
6634
6635 }
6636#endif
6637}
6638
6639#endif /* FEAT_MENU */
6640
6641
6642/* define some macros used to make the dialogue creation more readable */
6643
6644#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6645#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006646#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6649/*
6650 * stuff for dialogs
6651 */
6652
6653/*
6654 * The callback routine used by all the dialogs. Very simple. First,
6655 * acknowledges the INITDIALOG message so that Windows knows to do standard
6656 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6657 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6658 * number.
6659 */
6660 static LRESULT CALLBACK
6661dialog_callback(
6662 HWND hwnd,
6663 UINT message,
6664 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006665 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 if (message == WM_INITDIALOG)
6668 {
6669 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6670 /* Set focus to the dialog. Set the default button, if specified. */
6671 (void)SetFocus(hwnd);
6672 if (dialog_default_button > IDCANCEL)
6673 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006674 else
6675 /* We don't have a default, set focus on another element of the
6676 * dialog window, probably the icon */
6677 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 return FALSE;
6679 }
6680
6681 if (message == WM_COMMAND)
6682 {
6683 int button = LOWORD(wParam);
6684
6685 /* Don't end the dialog if something was selected that was
6686 * not a button.
6687 */
6688 if (button >= DLG_NONBUTTON_CONTROL)
6689 return TRUE;
6690
6691 /* If the edit box exists, copy the string. */
6692 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006693 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006694 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006695 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006696
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006697 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6698 p = utf16_to_enc(wp, NULL);
6699 vim_strncpy(s_textfield, p, IOSIZE);
6700 vim_free(p);
6701 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 /*
6705 * Need to check for IDOK because if the user just hits Return to
6706 * accept the default value, some reason this is what we get.
6707 */
6708 if (button == IDOK)
6709 {
6710 if (dialog_default_button > IDCANCEL)
6711 EndDialog(hwnd, dialog_default_button);
6712 }
6713 else
6714 EndDialog(hwnd, button - IDCANCEL);
6715 return TRUE;
6716 }
6717
6718 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6719 {
6720 EndDialog(hwnd, 0);
6721 return TRUE;
6722 }
6723 return FALSE;
6724}
6725
6726/*
6727 * Create a dialog dynamically from the parameter strings.
6728 * type = type of dialog (question, alert, etc.)
6729 * title = dialog title. may be NULL for default title.
6730 * message = text to display. Dialog sizes to accommodate it.
6731 * buttons = '\n' separated list of button captions, default first.
6732 * dfltbutton = number of default button.
6733 *
6734 * This routine returns 1 if the first button is pressed,
6735 * 2 for the second, etc.
6736 *
6737 * 0 indicates Esc was pressed.
6738 * -1 for unexpected error
6739 *
6740 * If stubbing out this fn, return 1.
6741 */
6742
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006743static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 "IDR_VIM",
6746 "IDR_VIM_ERROR",
6747 "IDR_VIM_ALERT",
6748 "IDR_VIM_INFO",
6749 "IDR_VIM_QUESTION"
6750};
6751
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 int
6753gui_mch_dialog(
6754 int type,
6755 char_u *title,
6756 char_u *message,
6757 char_u *buttons,
6758 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006759 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006760 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006763 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 int numButtons;
6765 int *buttonWidths, *buttonPositions;
6766 int buttonYpos;
6767 int nchar, i;
6768 DWORD lStyle;
6769 int dlgwidth = 0;
6770 int dlgheight;
6771 int editboxheight;
6772 int horizWidth = 0;
6773 int msgheight;
6774 char_u *pstart;
6775 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006776 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 char_u *tbuffer;
6778 RECT rect;
6779 HWND hwnd;
6780 HDC hdc;
6781 HFONT font, oldFont;
6782 TEXTMETRIC fontInfo;
6783 int fontHeight;
6784 int textWidth, minButtonWidth, messageWidth;
6785 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006786 int maxDialogHeight;
6787 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 int vertical;
6789 int dlgPaddingX;
6790 int dlgPaddingY;
6791#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006792 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 int use_lfSysmenu = FALSE;
6794#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006795 garray_T ga;
6796 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
6798#ifndef NO_CONSOLE
6799 /* Don't output anything in silent mode ("ex -s") */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006800# ifdef VIMDLL
6801 if (!(gui.in_use || gui.starting))
6802# endif
6803 if (silent_mode)
6804 return dfltbutton; /* return default option */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805#endif
6806
Bram Moolenaar748bf032005-02-02 23:04:36 +00006807 if (s_hwnd == NULL)
6808 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 if ((type < 0) || (type > VIM_LAST_TYPE))
6811 type = 0;
6812
6813 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006814 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006815 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006816 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 if (p == NULL)
6819 return -1;
6820
6821 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006822 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6824 * const.
6825 */
6826 tbuffer = vim_strsave(buttons);
6827 if (tbuffer == NULL)
6828 return -1;
6829
6830 --dfltbutton; /* Change from one-based to zero-based */
6831
6832 /* Count buttons */
6833 numButtons = 1;
6834 for (i = 0; tbuffer[i] != '\0'; i++)
6835 {
6836 if (tbuffer[i] == DLG_BUTTON_SEP)
6837 numButtons++;
6838 }
6839 if (dfltbutton >= numButtons)
6840 dfltbutton = -1;
6841
6842 /* Allocate array to hold the width of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006843 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 if (buttonWidths == NULL)
6845 return -1;
6846
6847 /* Allocate array to hold the X position of each button */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006848 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 if (buttonPositions == NULL)
6850 return -1;
6851
6852 /*
6853 * Calculate how big the dialog must be.
6854 */
6855 hwnd = GetDesktopWindow();
6856 hdc = GetWindowDC(hwnd);
6857#ifdef USE_SYSMENU_FONT
6858 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6859 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006860 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 use_lfSysmenu = TRUE;
6862 }
6863 else
6864#endif
6865 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6866 VARIABLE_PITCH , DLG_FONT_NAME);
6867 if (s_usenewlook)
6868 {
6869 oldFont = SelectFont(hdc, font);
6870 dlgPaddingX = DLG_PADDING_X;
6871 dlgPaddingY = DLG_PADDING_Y;
6872 }
6873 else
6874 {
6875 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6876 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6877 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6878 }
6879 GetTextMetrics(hdc, &fontInfo);
6880 fontHeight = fontInfo.tmHeight;
6881
6882 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006883 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884
6885 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006886 if (s_hwnd == NULL)
6887 {
6888 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
Bram Moolenaarc716c302006-01-21 22:12:51 +00006890 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006891 get_work_area(&workarea_rect);
6892 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6893 if (maxDialogWidth > 600)
6894 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006895 /* Leave some room for the taskbar. */
6896 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006897 }
6898 else
6899 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006900 /* Use our own window for the size, unless it's very small. */
6901 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006902 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006903 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006904 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006905 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6906 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006907
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006908 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006909 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006910 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006911 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006912 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6913 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6914 }
6915
6916 /* Set dlgwidth to width of message.
6917 * Copy the message into "ga", changing NL to CR-NL and inserting line
6918 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 pstart = message;
6920 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006921 msgheight = 0;
6922 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 do
6924 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925 msgheight += fontHeight; /* at least one line */
6926
6927 /* Need to figure out where to break the string. The system does it
6928 * at a word boundary, which would mean we can't compute the number of
6929 * wrapped lines. */
6930 textWidth = 0;
6931 last_white = NULL;
6932 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006933 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006934 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006935 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006936 && textWidth > maxDialogWidth * 3 / 4)
6937 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006938 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006939 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006940 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006941 /* Line will wrap. */
6942 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006943 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944 textWidth = 0;
6945
6946 if (last_white != NULL)
6947 {
6948 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006949 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006950 pend = last_white + 1;
6951 last_white = NULL;
6952 }
6953 ga_append(&ga, '\r');
6954 ga_append(&ga, '\n');
6955 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006956 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957
6958 while (--l >= 0)
6959 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006960 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006963
6964 ga_append(&ga, '\r');
6965 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 pstart = pend + 1;
6967 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006968
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006969 if (ga.ga_data != NULL)
6970 message = ga.ga_data;
6971
Bram Moolenaar748bf032005-02-02 23:04:36 +00006972 messageWidth += 10; /* roundoff space */
6973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006975 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6976 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 if (msgheight < DLG_ICON_HEIGHT)
6979 msgheight = DLG_ICON_HEIGHT;
6980
6981 /*
6982 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006983 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006985 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 if (!vertical)
6987 {
6988 // Place buttons horizontally if they fit.
6989 horizWidth = dlgPaddingX;
6990 pstart = tbuffer;
6991 i = 0;
6992 do
6993 {
6994 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
6995 if (pend == NULL)
6996 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02006997 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 if (textWidth < minButtonWidth)
6999 textWidth = minButtonWidth;
7000 textWidth += dlgPaddingX; /* Padding within button */
7001 buttonWidths[i] = textWidth;
7002 buttonPositions[i++] = horizWidth;
7003 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7004 pstart = pend + 1;
7005 } while (*pend != NUL);
7006
7007 if (horizWidth > maxDialogWidth)
7008 vertical = TRUE; // Too wide to fit on the screen.
7009 else if (horizWidth > dlgwidth)
7010 dlgwidth = horizWidth;
7011 }
7012
7013 if (vertical)
7014 {
7015 // Stack buttons vertically.
7016 pstart = tbuffer;
7017 do
7018 {
7019 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7020 if (pend == NULL)
7021 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007022 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 textWidth += dlgPaddingX; /* Padding within button */
7024 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7025 if (textWidth > dlgwidth)
7026 dlgwidth = textWidth;
7027 pstart = pend + 1;
7028 } while (*pend != NUL);
7029 }
7030
7031 if (dlgwidth < DLG_MIN_WIDTH)
7032 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7033
7034 /* start to fill in the dlgtemplate information. addressing by WORDs */
7035 if (s_usenewlook)
7036 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7037 else
7038 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7039
7040 add_long(lStyle);
7041 add_long(0); // (lExtendedStyle)
7042 pnumitems = p; /*save where the number of items must be stored*/
7043 add_word(0); // NumberOfItems(will change later)
7044 add_word(10); // x
7045 add_word(10); // y
7046 add_word(PixelToDialogX(dlgwidth)); // cx
7047
7048 // Dialog height.
7049 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007050 dlgheight = msgheight + 2 * dlgPaddingY
7051 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 else
7053 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7054
7055 // Dialog needs to be taller if contains an edit box.
7056 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7057 if (textfield != NULL)
7058 dlgheight += editboxheight;
7059
Bram Moolenaara95d8232013-08-07 15:27:11 +02007060 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7061 if (dlgheight > maxDialogHeight)
7062 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007063 msgheight = msgheight - (dlgheight - maxDialogHeight);
7064 dlgheight = maxDialogHeight;
7065 scroll_flag = WS_VSCROLL;
7066 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7067 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007068 }
7069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 add_word(PixelToDialogY(dlgheight));
7071
7072 add_word(0); // Menu
7073 add_word(0); // Class
7074
7075 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007076 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7077 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 p += nchar;
7079
7080 if (s_usenewlook)
7081 {
7082 /* do the font, since DS_3DLOOK doesn't work properly */
7083#ifdef USE_SYSMENU_FONT
7084 if (use_lfSysmenu)
7085 {
7086 /* point size */
7087 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7088 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007089 wcscpy(p, lfSysmenu.lfFaceName);
7090 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 }
7092 else
7093#endif
7094 {
7095 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007096 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 }
7098 p += nchar;
7099 }
7100
7101 buttonYpos = msgheight + 2 * dlgPaddingY;
7102
7103 if (textfield != NULL)
7104 buttonYpos += editboxheight;
7105
7106 pstart = tbuffer;
7107 if (!vertical)
7108 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7109 for (i = 0; i < numButtons; i++)
7110 {
7111 /* get end of this button. */
7112 for ( pend = pstart;
7113 *pend && (*pend != DLG_BUTTON_SEP);
7114 pend++)
7115 ;
7116
7117 if (*pend)
7118 *pend = '\0';
7119
7120 /*
7121 * old NOTE:
7122 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7123 * the focus to the first tab-able button and in so doing makes that
7124 * the default!! Grrr. Workaround: Make the default button the only
7125 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7126 * he/she can use arrow keys.
7127 *
7128 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007129 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 * dialog. Also needed for when the textfield is the default control.
7131 * It appears to work now (perhaps not on Win95?).
7132 */
7133 if (vertical)
7134 {
7135 p = add_dialog_element(p,
7136 (i == dfltbutton
7137 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7138 PixelToDialogX(DLG_VERT_PADDING_X),
7139 PixelToDialogY(buttonYpos /* TBK */
7140 + 2 * fontHeight * i),
7141 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
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 else
7146 {
7147 p = add_dialog_element(p,
7148 (i == dfltbutton
7149 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7150 PixelToDialogX(horizWidth + buttonPositions[i]),
7151 PixelToDialogY(buttonYpos), /* TBK */
7152 PixelToDialogX(buttonWidths[i]),
7153 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007154 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
7156 pstart = pend + 1; /*next button*/
7157 }
7158 *pnumitems += numButtons;
7159
7160 /* Vim icon */
7161 p = add_dialog_element(p, SS_ICON,
7162 PixelToDialogX(dlgPaddingX),
7163 PixelToDialogY(dlgPaddingY),
7164 PixelToDialogX(DLG_ICON_WIDTH),
7165 PixelToDialogY(DLG_ICON_HEIGHT),
7166 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7167 dlg_icons[type]);
7168
Bram Moolenaar748bf032005-02-02 23:04:36 +00007169 /* Dialog message */
7170 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7171 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7172 PixelToDialogY(dlgPaddingY),
7173 (WORD)(PixelToDialogX(messageWidth) + 1),
7174 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007175 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
7177 /* Edit box */
7178 if (textfield != NULL)
7179 {
7180 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7181 PixelToDialogX(2 * dlgPaddingX),
7182 PixelToDialogY(2 * dlgPaddingY + msgheight),
7183 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7184 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007185 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 *pnumitems += 1;
7187 }
7188
7189 *pnumitems += 2;
7190
7191 SelectFont(hdc, oldFont);
7192 DeleteObject(font);
7193 ReleaseDC(hwnd, hdc);
7194
7195 /* Let the dialog_callback() function know which button to make default
7196 * If we have an edit box, make that the default. We also need to tell
7197 * dialog_callback() if this dialog contains an edit box or not. We do
7198 * this by setting s_textfield if it does.
7199 */
7200 if (textfield != NULL)
7201 {
7202 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7203 s_textfield = textfield;
7204 }
7205 else
7206 {
7207 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7208 s_textfield = NULL;
7209 }
7210
7211 /* show the dialog box modally and get a return value */
7212 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007213 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 (LPDLGTEMPLATE)pdlgtemplate,
7215 s_hwnd,
7216 (DLGPROC)dialog_callback);
7217
7218 LocalFree(LocalHandle(pdlgtemplate));
7219 vim_free(tbuffer);
7220 vim_free(buttonWidths);
7221 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007222 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
7224 /* Focus back to our window (for when MDI is used). */
7225 (void)SetFocus(s_hwnd);
7226
7227 return nchar;
7228}
7229
7230#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232/*
7233 * Put a simple element (basic class) onto a dialog template in memory.
7234 * return a pointer to where the next item should be added.
7235 *
7236 * parameters:
7237 * lStyle = additional style flags
7238 * (be careful, NT3.51 & Win32s will ignore the new ones)
7239 * x,y = x & y positions IN DIALOG UNITS
7240 * w,h = width and height IN DIALOG UNITS
7241 * Id = ID used in messages
7242 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7243 * caption = usually text or resource name
7244 *
7245 * TODO: use the length information noted here to enable the dialog creation
7246 * routines to work out more exactly how much memory they need to alloc.
7247 */
7248 static PWORD
7249add_dialog_element(
7250 PWORD p,
7251 DWORD lStyle,
7252 WORD x,
7253 WORD y,
7254 WORD w,
7255 WORD h,
7256 WORD Id,
7257 WORD clss,
7258 const char *caption)
7259{
7260 int nchar;
7261
7262 p = lpwAlign(p); /* Align to dword boundary*/
7263 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7264 *p++ = LOWORD(lStyle);
7265 *p++ = HIWORD(lStyle);
7266 *p++ = 0; // LOWORD (lExtendedStyle)
7267 *p++ = 0; // HIWORD (lExtendedStyle)
7268 *p++ = x;
7269 *p++ = y;
7270 *p++ = w;
7271 *p++ = h;
7272 *p++ = Id; //9 or 10 words in all
7273
7274 *p++ = (WORD)0xffff;
7275 *p++ = clss; //2 more here
7276
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007277 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 p += nchar;
7279
7280 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7281
7282 return p; //total = 15+ (strlen(caption)) words
7283 // = 30 + 2(strlen(caption) bytes reqd
7284}
7285
7286
7287/*
7288 * Helper routine. Take an input pointer, return closest pointer that is
7289 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7290 */
7291 static LPWORD
7292lpwAlign(
7293 LPWORD lpIn)
7294{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007295 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007297 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 ul += 3;
7299 ul >>= 2;
7300 ul <<= 2;
7301 return (LPWORD)ul;
7302}
7303
7304/*
7305 * Helper routine. Takes second parameter as Ansi string, copies it to first
7306 * parameter as wide character (16-bits / char) string, and returns integer
7307 * number of wide characters (words) in string (including the trailing wide
7308 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007309 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7310 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 static int
7312nCopyAnsiToWideChar(
7313 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007314 LPSTR lpAnsiIn,
7315 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
7317 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7319 int i;
7320 WCHAR *wn;
7321
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007322 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 {
7324 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007325 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (wn != NULL)
7327 {
7328 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007329 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 vim_free(wn);
7331 }
7332 }
7333 if (nChar == 0)
7334 /* Use Win32 conversion function. */
7335 nChar = MultiByteToWideChar(
7336 enc_codepage > 0 ? enc_codepage : CP_ACP,
7337 MB_PRECOMPOSED,
7338 lpAnsiIn, len,
7339 lpWCStr, len);
7340 for (i = 0; i < nChar; ++i)
7341 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7342 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344 return nChar;
7345}
7346
7347
7348#ifdef FEAT_TEAROFF
7349/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007350 * Lookup menu handle from "menu_id".
7351 */
7352 static HMENU
7353tearoff_lookup_menuhandle(
7354 vimmenu_T *menu,
7355 WORD menu_id)
7356{
7357 for ( ; menu != NULL; menu = menu->next)
7358 {
7359 if (menu->modes == 0) /* this menu has just been deleted */
7360 continue;
7361 if (menu_is_separator(menu->dname))
7362 continue;
7363 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7364 return menu->submenu_id;
7365 }
7366 return NULL;
7367}
7368
7369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 * The callback function for all the modeless dialogs that make up the
7371 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7372 * thinking its menus have been clicked), and go away when closed.
7373 */
7374 static LRESULT CALLBACK
7375tearoff_callback(
7376 HWND hwnd,
7377 UINT message,
7378 WPARAM wParam,
7379 LPARAM lParam)
7380{
7381 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007382 {
7383 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
7387 /* May show the mouse pointer again. */
7388 HandleMouseHide(message, lParam);
7389
7390 if (message == WM_COMMAND)
7391 {
7392 if ((WORD)(LOWORD(wParam)) & 0x8000)
7393 {
7394 POINT mp;
7395 RECT rect;
7396
7397 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7398 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007399 vimmenu_T *menu;
7400
7401 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007403 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7405 (int)rect.right - 8,
7406 (int)mp.y,
7407 (int)0, /*reserved param*/
7408 s_hwnd,
7409 NULL);
7410 /*
7411 * NOTE: The pop-up menu can eat the mouse up event.
7412 * We deal with this in normal.c.
7413 */
7414 }
7415 }
7416 else
7417 /* Pass on messages to the main Vim window */
7418 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7419 /*
7420 * Give main window the focus back: this is so after
7421 * choosing a tearoff button you can start typing again
7422 * straight away.
7423 */
7424 (void)SetFocus(s_hwnd);
7425 return TRUE;
7426 }
7427 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7428 {
7429 DestroyWindow(hwnd);
7430 return TRUE;
7431 }
7432
7433 /* When moved around, give main window the focus back. */
7434 if (message == WM_EXITSIZEMOVE)
7435 (void)SetActiveWindow(s_hwnd);
7436
7437 return FALSE;
7438}
7439#endif
7440
7441
7442/*
7443 * Decide whether to use the "new look" (small, non-bold font) or the "old
7444 * look" (big, clanky font) for dialogs, and work out a few values for use
7445 * later accordingly.
7446 */
7447 static void
7448get_dialog_font_metrics(void)
7449{
7450 HDC hdc;
7451 HFONT hfontTools = 0;
7452 DWORD dlgFontSize;
7453 SIZE size;
7454#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007455 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456#endif
7457
7458 s_usenewlook = FALSE;
7459
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007461 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007462 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464#endif
7465 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7466 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7467
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007468 if (hfontTools)
7469 {
7470 hdc = GetDC(s_hwnd);
7471 SelectObject(hdc, hfontTools);
7472 /*
7473 * GetTextMetrics() doesn't return the right value in
7474 * tmAveCharWidth, so we have to figure out the dialog base units
7475 * ourselves.
7476 */
7477 GetTextExtentPoint(hdc,
7478 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7479 52, &size);
7480 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007482 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7483 s_dlgfntheight = (WORD)size.cy;
7484 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486
7487 if (!s_usenewlook)
7488 {
7489 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7490 s_dlgfntwidth = LOWORD(dlgFontSize);
7491 s_dlgfntheight = HIWORD(dlgFontSize);
7492 }
7493}
7494
7495#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7496/*
7497 * Create a pseudo-"tearoff menu" based on the child
7498 * items of a given menu pointer.
7499 */
7500 static void
7501gui_mch_tearoff(
7502 char_u *title,
7503 vimmenu_T *menu,
7504 int initX,
7505 int initY)
7506{
7507 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7508 int template_len;
7509 int nchar, textWidth, submenuWidth;
7510 DWORD lStyle;
7511 DWORD lExtendedStyle;
7512 WORD dlgwidth;
7513 WORD menuID;
7514 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007515 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 vimmenu_T *the_menu = menu;
7517 HWND hwnd;
7518 HDC hdc;
7519 HFONT font, oldFont;
7520 int col, spaceWidth, len;
7521 int columnWidths[2];
7522 char_u *label, *text;
7523 int acLen = 0;
7524 int nameLen;
7525 int padding0, padding1, padding2 = 0;
7526 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007527 int x;
7528 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007530 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 int use_lfSysmenu = FALSE;
7532#endif
7533
7534 /*
7535 * If this menu is already torn off, move it to the mouse position.
7536 */
7537 if (IsWindow(menu->tearoff_handle))
7538 {
7539 POINT mp;
7540 if (GetCursorPos((LPPOINT)&mp))
7541 {
7542 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7543 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7544 }
7545 return;
7546 }
7547
7548 /*
7549 * Create a new tearoff.
7550 */
7551 if (*title == MNU_HIDDEN_CHAR)
7552 title++;
7553
7554 /* Allocate memory to store the dialog template. It's made bigger when
7555 * needed. */
7556 template_len = DLG_ALLOC_SIZE;
7557 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7558 if (p == NULL)
7559 return;
7560
7561 hwnd = GetDesktopWindow();
7562 hdc = GetWindowDC(hwnd);
7563#ifdef USE_SYSMENU_FONT
7564 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7565 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007566 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 use_lfSysmenu = TRUE;
7568 }
7569 else
7570#endif
7571 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7572 VARIABLE_PITCH , DLG_FONT_NAME);
7573 if (s_usenewlook)
7574 oldFont = SelectFont(hdc, font);
7575 else
7576 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7577
7578 /* Calculate width of a single space. Used for padding columns to the
7579 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007580 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 /* Figure out max width of the text column, the accelerator column and the
7583 * optional submenu column. */
7584 submenuWidth = 0;
7585 for (col = 0; col < 2; col++)
7586 {
7587 columnWidths[col] = 0;
7588 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7589 {
7590 /* Use "dname" here to compute the width of the visible text. */
7591 text = (col == 0) ? pmenu->dname : pmenu->actext;
7592 if (text != NULL && *text != NUL)
7593 {
7594 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7595 if (textWidth > columnWidths[col])
7596 columnWidths[col] = textWidth;
7597 }
7598 if (pmenu->children != NULL)
7599 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7600 }
7601 }
7602 if (columnWidths[1] == 0)
7603 {
7604 /* no accelerators */
7605 if (submenuWidth != 0)
7606 columnWidths[0] += submenuWidth;
7607 else
7608 columnWidths[0] += spaceWidth;
7609 }
7610 else
7611 {
7612 /* there is an accelerator column */
7613 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7614 columnWidths[1] += submenuWidth;
7615 }
7616
7617 /*
7618 * Now find the total width of our 'menu'.
7619 */
7620 textWidth = columnWidths[0] + columnWidths[1];
7621 if (submenuWidth != 0)
7622 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007623 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7625 textWidth += submenuWidth;
7626 }
7627 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7628 if (textWidth > dlgwidth)
7629 dlgwidth = textWidth;
7630 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7631
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 /* start to fill in the dlgtemplate information. addressing by WORDs */
7633 if (s_usenewlook)
7634 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7635 else
7636 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7637
7638 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7639 *p++ = LOWORD(lStyle);
7640 *p++ = HIWORD(lStyle);
7641 *p++ = LOWORD(lExtendedStyle);
7642 *p++ = HIWORD(lExtendedStyle);
7643 pnumitems = p; /* save where the number of items must be stored */
7644 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007645 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007647 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 else
7649 *p++ = PixelToDialogX(initX); // x
7650 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007651 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 else
7653 *p++ = PixelToDialogY(initY); // y
7654 *p++ = PixelToDialogX(dlgwidth); // cx
7655 ptrueheight = p;
7656 *p++ = 0; // dialog height: changed later anyway
7657 *p++ = 0; // Menu
7658 *p++ = 0; // Class
7659
7660 /* copy the title of the dialog */
7661 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007662 ? (LPSTR)title
7663 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 p += nchar;
7665
7666 if (s_usenewlook)
7667 {
7668 /* do the font, since DS_3DLOOK doesn't work properly */
7669#ifdef USE_SYSMENU_FONT
7670 if (use_lfSysmenu)
7671 {
7672 /* point size */
7673 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7674 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007675 wcscpy(p, lfSysmenu.lfFaceName);
7676 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678 else
7679#endif
7680 {
7681 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007682 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 }
7684 p += nchar;
7685 }
7686
7687 /*
7688 * Loop over all the items in the menu.
7689 * But skip over the tearbar.
7690 */
7691 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7692 menu = menu->children->next;
7693 else
7694 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007695 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 for ( ; menu != NULL; menu = menu->next)
7697 {
7698 if (menu->modes == 0) /* this menu has just been deleted */
7699 continue;
7700 if (menu_is_separator(menu->dname))
7701 {
7702 sepPadding += 3;
7703 continue;
7704 }
7705
7706 /* Check if there still is plenty of room in the template. Make it
7707 * larger when needed. */
7708 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7709 {
7710 WORD *newp;
7711
7712 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7713 if (newp != NULL)
7714 {
7715 template_len += 4096;
7716 mch_memmove(newp, pdlgtemplate,
7717 (char *)p - (char *)pdlgtemplate);
7718 p = newp + (p - pdlgtemplate);
7719 pnumitems = newp + (pnumitems - pdlgtemplate);
7720 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7721 LocalFree(LocalHandle(pdlgtemplate));
7722 pdlgtemplate = newp;
7723 }
7724 }
7725
7726 /* Figure out minimal length of this menu label. Use "name" for the
7727 * actual text, "dname" for estimating the displayed size. "name"
7728 * has "&a" for mnemonic and includes the accelerator. */
7729 len = nameLen = (int)STRLEN(menu->name);
7730 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7731 (int)STRLEN(menu->dname))) / spaceWidth;
7732 len += padding0;
7733
7734 if (menu->actext != NULL)
7735 {
7736 acLen = (int)STRLEN(menu->actext);
7737 len += acLen;
7738 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7739 }
7740 else
7741 textWidth = 0;
7742 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7743 len += padding1;
7744
7745 if (menu->children == NULL)
7746 {
7747 padding2 = submenuWidth / spaceWidth;
7748 len += padding2;
7749 menuID = (WORD)(menu->id);
7750 }
7751 else
7752 {
7753 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007754 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 }
7756
7757 /* Allocate menu label and fill it in */
Bram Moolenaar964b3742019-05-24 18:54:09 +02007758 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 if (label == NULL)
7760 break;
7761
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007762 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7764 if (text == NULL)
7765 text = label + nameLen; /* no actext, use whole name */
7766 while (padding0-- > 0)
7767 *text++ = ' ';
7768 if (menu->actext != NULL)
7769 {
7770 STRNCPY(text, menu->actext, acLen);
7771 text += acLen;
7772 }
7773 while (padding1-- > 0)
7774 *text++ = ' ';
7775 if (menu->children != NULL)
7776 {
7777 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7778 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7779 }
7780 else
7781 {
7782 while (padding2-- > 0)
7783 *text++ = ' ';
7784 }
7785 *text = NUL;
7786
7787 /*
7788 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7789 * W95/NT4 it makes the tear-off look more like a menu.
7790 */
7791 p = add_dialog_element(p,
7792 BS_PUSHBUTTON|BS_LEFT,
7793 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7794 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7795 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7796 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007797 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 vim_free(label);
7799 (*pnumitems)++;
7800 }
7801
7802 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7803
7804
7805 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007806 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007807 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 (LPDLGTEMPLATE)pdlgtemplate,
7809 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007810 (DLGPROC)tearoff_callback,
7811 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
7813 LocalFree(LocalHandle(pdlgtemplate));
7814 SelectFont(hdc, oldFont);
7815 DeleteObject(font);
7816 ReleaseDC(hwnd, hdc);
7817
7818 /*
7819 * Reassert ourselves as the active window. This is so that after creating
7820 * a tearoff, the user doesn't have to click with the mouse just to start
7821 * typing again!
7822 */
7823 (void)SetActiveWindow(s_hwnd);
7824
7825 /* make sure the right buttons are enabled */
7826 force_menu_update = TRUE;
7827}
7828#endif
7829
7830#if defined(FEAT_TOOLBAR) || defined(PROTO)
7831#include "gui_w32_rc.h"
7832
7833/* This not defined in older SDKs */
7834# ifndef TBSTYLE_FLAT
7835# define TBSTYLE_FLAT 0x0800
7836# endif
7837
7838/*
7839 * Create the toolbar, initially unpopulated.
7840 * (just like the menu, there are no defaults, it's all
7841 * set up through menu.vim)
7842 */
7843 static void
7844initialise_toolbar(void)
7845{
7846 InitCommonControls();
7847 s_toolbarhwnd = CreateToolbarEx(
7848 s_hwnd,
7849 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7850 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007851 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007852 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 IDR_TOOLBAR1, // id of initial bitmap
7854 NULL,
7855 0, // initial number of buttons
7856 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7857 TOOLBAR_BUTTON_HEIGHT,
7858 TOOLBAR_BUTTON_WIDTH,
7859 TOOLBAR_BUTTON_HEIGHT,
7860 sizeof(TBBUTTON)
7861 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007862
7863 // Remove transparency from the toolbar to prevent the main window
7864 // background colour showing through
7865 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7866 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7867
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007868 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869
7870 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7871}
7872
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007873 static LRESULT CALLBACK
7874toolbar_wndproc(
7875 HWND hwnd,
7876 UINT uMsg,
7877 WPARAM wParam,
7878 LPARAM lParam)
7879{
7880 HandleMouseHide(uMsg, lParam);
7881 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7882}
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 static int
7885get_toolbar_bitmap(vimmenu_T *menu)
7886{
7887 int i = -1;
7888
7889 /*
7890 * Check user bitmaps first, unless builtin is specified.
7891 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007892 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {
7894 char_u fname[MAXPATHL];
7895 HANDLE hbitmap = NULL;
7896
7897 if (menu->iconfile != NULL)
7898 {
7899 gui_find_iconfile(menu->iconfile, fname, "bmp");
7900 hbitmap = LoadImage(
7901 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007902 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 IMAGE_BITMAP,
7904 TOOLBAR_BUTTON_WIDTH,
7905 TOOLBAR_BUTTON_HEIGHT,
7906 LR_LOADFROMFILE |
7907 LR_LOADMAP3DCOLORS
7908 );
7909 }
7910
7911 /*
7912 * If the LoadImage call failed, or the "icon=" file
7913 * didn't exist or wasn't specified, try the menu name
7914 */
7915 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007916 && (gui_find_bitmap(
7917#ifdef FEAT_MULTI_LANG
7918 menu->en_dname != NULL ? menu->en_dname :
7919#endif
7920 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 hbitmap = LoadImage(
7922 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007923 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 IMAGE_BITMAP,
7925 TOOLBAR_BUTTON_WIDTH,
7926 TOOLBAR_BUTTON_HEIGHT,
7927 LR_LOADFROMFILE |
7928 LR_LOADMAP3DCOLORS
7929 );
7930
7931 if (hbitmap != NULL)
7932 {
7933 TBADDBITMAP tbAddBitmap;
7934
7935 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007936 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937
7938 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7939 (WPARAM)1, (LPARAM)&tbAddBitmap);
7940 /* i will be set to -1 if it fails */
7941 }
7942 }
7943 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7944 i = menu->iconidx;
7945
7946 return i;
7947}
7948#endif
7949
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007950#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7951 static void
7952initialise_tabline(void)
7953{
7954 InitCommonControls();
7955
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007956 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007957 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007958 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007959 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007960 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007961
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007962 gui.tabline_height = TABLINE_HEIGHT;
7963
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007964# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007965 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007966# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007967}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007968
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007969/*
7970 * Get tabpage_T from POINT.
7971 */
7972 static tabpage_T *
7973GetTabFromPoint(
7974 HWND hWnd,
7975 POINT pt)
7976{
7977 tabpage_T *ptp = NULL;
7978
7979 if (gui_mch_showing_tabline())
7980 {
7981 TCHITTESTINFO htinfo;
7982 htinfo.pt = pt;
7983 /* ignore if a window under cusor is not tabcontrol. */
7984 if (s_tabhwnd == hWnd)
7985 {
7986 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7987 if (idx != -1)
7988 ptp = find_tabpage(idx + 1);
7989 }
7990 }
7991 return ptp;
7992}
7993
7994static POINT s_pt = {0, 0};
7995static HCURSOR s_hCursor = NULL;
7996
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007997 static LRESULT CALLBACK
7998tabline_wndproc(
7999 HWND hwnd,
8000 UINT uMsg,
8001 WPARAM wParam,
8002 LPARAM lParam)
8003{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008004 POINT pt;
8005 tabpage_T *tp;
8006 RECT rect;
8007 int nCenter;
8008 int idx0;
8009 int idx1;
8010
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008011 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008012
8013 switch (uMsg)
8014 {
8015 case WM_LBUTTONDOWN:
8016 {
8017 s_pt.x = GET_X_LPARAM(lParam);
8018 s_pt.y = GET_Y_LPARAM(lParam);
8019 SetCapture(hwnd);
8020 s_hCursor = GetCursor(); /* backup default cursor */
8021 break;
8022 }
8023 case WM_MOUSEMOVE:
8024 if (GetCapture() == hwnd
8025 && ((wParam & MK_LBUTTON)) != 0)
8026 {
8027 pt.x = GET_X_LPARAM(lParam);
8028 pt.y = s_pt.y;
8029 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8030 {
8031 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8032
8033 tp = GetTabFromPoint(hwnd, pt);
8034 if (tp != NULL)
8035 {
8036 idx0 = tabpage_index(curtab) - 1;
8037 idx1 = tabpage_index(tp) - 1;
8038
8039 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8040 nCenter = rect.left + (rect.right - rect.left) / 2;
8041
8042 /* Check if the mouse cursor goes over the center of
8043 * the next tab to prevent "flickering". */
8044 if ((idx0 < idx1) && (nCenter < pt.x))
8045 {
8046 tabpage_move(idx1 + 1);
8047 update_screen(0);
8048 }
8049 else if ((idx1 < idx0) && (pt.x < nCenter))
8050 {
8051 tabpage_move(idx1);
8052 update_screen(0);
8053 }
8054 }
8055 }
8056 }
8057 break;
8058 case WM_LBUTTONUP:
8059 {
8060 if (GetCapture() == hwnd)
8061 {
8062 SetCursor(s_hCursor);
8063 ReleaseCapture();
8064 }
8065 break;
8066 }
8067 default:
8068 break;
8069 }
8070
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008071 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8072}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008073#endif
8074
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8076/*
8077 * Make the GUI window come to the foreground.
8078 */
8079 void
8080gui_mch_set_foreground(void)
8081{
8082 if (IsIconic(s_hwnd))
8083 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8084 SetForegroundWindow(s_hwnd);
8085}
8086#endif
8087
8088#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8089 static void
8090dyn_imm_load(void)
8091{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008092 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 if (hLibImm == NULL)
8094 return;
8095
8096 pImmGetCompositionStringA
8097 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8098 pImmGetCompositionStringW
8099 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8100 pImmGetContext
8101 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8102 pImmAssociateContext
8103 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8104 pImmReleaseContext
8105 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8106 pImmGetOpenStatus
8107 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8108 pImmSetOpenStatus
8109 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008110 pImmGetCompositionFontW
8111 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8112 pImmSetCompositionFontW
8113 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 pImmSetCompositionWindow
8115 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8116 pImmGetConversionStatus
8117 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008118 pImmSetConversionStatus
8119 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120
8121 if ( pImmGetCompositionStringA == NULL
8122 || pImmGetCompositionStringW == NULL
8123 || pImmGetContext == NULL
8124 || pImmAssociateContext == NULL
8125 || pImmReleaseContext == NULL
8126 || pImmGetOpenStatus == NULL
8127 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008128 || pImmGetCompositionFontW == NULL
8129 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008131 || pImmGetConversionStatus == NULL
8132 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {
8134 FreeLibrary(hLibImm);
8135 hLibImm = NULL;
8136 pImmGetContext = NULL;
8137 return;
8138 }
8139
8140 return;
8141}
8142
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143#endif
8144
8145#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8146
8147# ifdef FEAT_XPM_W32
8148# define IMAGE_XPM 100
8149# endif
8150
8151typedef struct _signicon_t
8152{
8153 HANDLE hImage;
8154 UINT uType;
8155#ifdef FEAT_XPM_W32
8156 HANDLE hShape; /* Mask bitmap handle */
8157#endif
8158} signicon_t;
8159
8160 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008161gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 signicon_t *sign;
8164 int x, y, w, h;
8165
8166 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8167 return;
8168
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008169#if defined(FEAT_DIRECTX)
8170 if (IS_ENABLE_DIRECTX())
8171 DWriteContext_Flush(s_dwc);
8172#endif
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 x = TEXT_X(col);
8175 y = TEXT_Y(row);
8176 w = gui.char_width * 2;
8177 h = gui.char_height;
8178 switch (sign->uType)
8179 {
8180 case IMAGE_BITMAP:
8181 {
8182 HDC hdcMem;
8183 HBITMAP hbmpOld;
8184
8185 hdcMem = CreateCompatibleDC(s_hdc);
8186 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8187 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8188 SelectObject(hdcMem, hbmpOld);
8189 DeleteDC(hdcMem);
8190 }
8191 break;
8192 case IMAGE_ICON:
8193 case IMAGE_CURSOR:
8194 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8195 break;
8196#ifdef FEAT_XPM_W32
8197 case IMAGE_XPM:
8198 {
8199 HDC hdcMem;
8200 HBITMAP hbmpOld;
8201
8202 hdcMem = CreateCompatibleDC(s_hdc);
8203 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8204 /* Make hole */
8205 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8206
8207 SelectObject(hdcMem, sign->hImage);
8208 /* Paint sign */
8209 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8210 SelectObject(hdcMem, hbmpOld);
8211 DeleteDC(hdcMem);
8212 }
8213 break;
8214#endif
8215 }
8216}
8217
8218 static void
8219close_signicon_image(signicon_t *sign)
8220{
8221 if (sign)
8222 switch (sign->uType)
8223 {
8224 case IMAGE_BITMAP:
8225 DeleteObject((HGDIOBJ)sign->hImage);
8226 break;
8227 case IMAGE_CURSOR:
8228 DestroyCursor((HCURSOR)sign->hImage);
8229 break;
8230 case IMAGE_ICON:
8231 DestroyIcon((HICON)sign->hImage);
8232 break;
8233#ifdef FEAT_XPM_W32
8234 case IMAGE_XPM:
8235 DeleteObject((HBITMAP)sign->hImage);
8236 DeleteObject((HBITMAP)sign->hShape);
8237 break;
8238#endif
8239 }
8240}
8241
8242 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008243gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244{
8245 signicon_t sign, *psign;
8246 char_u *ext;
8247
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008249 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (ext > signfile)
8251 {
8252 int do_load = 1;
8253
8254 if (!STRICMP(ext, ".bmp"))
8255 sign.uType = IMAGE_BITMAP;
8256 else if (!STRICMP(ext, ".ico"))
8257 sign.uType = IMAGE_ICON;
8258 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8259 sign.uType = IMAGE_CURSOR;
8260 else
8261 do_load = 0;
8262
8263 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008264 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 gui.char_width * 2, gui.char_height,
8266 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8267#ifdef FEAT_XPM_W32
8268 if (!STRICMP(ext, ".xpm"))
8269 {
8270 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008271 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8272 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 }
8274#endif
8275 }
8276
8277 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008278 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 *psign = sign;
8280
8281 if (!psign)
8282 {
8283 if (sign.hImage)
8284 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008285 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
8287 return (void *)psign;
8288
8289}
8290
8291 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008292gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293{
8294 if (sign)
8295 {
8296 close_signicon_image((signicon_t *)sign);
8297 vim_free(sign);
8298 }
8299}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008302#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303
8304/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008305 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008307 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8309 * to get current mouse position).
8310 *
8311 * Trying to use as more Windows services as possible, and as less
8312 * IE version as possible :)).
8313 *
8314 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8315 * BalloonEval struct.
8316 * 2) Enable/Disable simply create/kill BalloonEval Timer
8317 * 3) When there was enough inactivity, timer procedure posts
8318 * async request to debugger
8319 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8320 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008321 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 */
8323
Bram Moolenaar45360022005-07-21 21:08:21 +00008324/*
8325 * determine whether installed Common Controls support multiline tooltips
8326 * (i.e. their version is >= 4.70
8327 */
8328 int
8329multiline_balloon_available(void)
8330{
8331 HINSTANCE hDll;
8332 static char comctl_dll[] = "comctl32.dll";
8333 static int multiline_tip = MAYBE;
8334
8335 if (multiline_tip != MAYBE)
8336 return multiline_tip;
8337
8338 hDll = GetModuleHandle(comctl_dll);
8339 if (hDll != NULL)
8340 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008341 DLLGETVERSIONPROC pGetVer;
8342 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008343
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008344 if (pGetVer != NULL)
8345 {
8346 DLLVERSIONINFO dvi;
8347 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008348
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008349 ZeroMemory(&dvi, sizeof(dvi));
8350 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008351
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008352 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008353
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008354 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008355 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008356 || (dvi.dwMajorVersion == 4
8357 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008358 {
8359 multiline_tip = TRUE;
8360 return multiline_tip;
8361 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008363 else
8364 {
8365 /* there is chance we have ancient CommCtl 4.70
8366 which doesn't export DllGetVersion */
8367 DWORD dwHandle = 0;
8368 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8369 if (len > 0)
8370 {
8371 VS_FIXEDFILEINFO *ver;
8372 UINT vlen = 0;
8373 void *data = alloc(len);
8374
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008375 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008376 && GetFileVersionInfo(comctl_dll, 0, len, data)
8377 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8378 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008379 && HIWORD(ver->dwFileVersionMS) > 4)
8380 || ((HIWORD(ver->dwFileVersionMS) == 4
8381 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008382 {
8383 vim_free(data);
8384 multiline_tip = TRUE;
8385 return multiline_tip;
8386 }
8387 vim_free(data);
8388 }
8389 }
8390 }
8391 multiline_tip = FALSE;
8392 return multiline_tip;
8393}
8394
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008395 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008396make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008397{
8398 TOOLINFOW *pti;
8399 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008400
8401 if (multiline_balloon_available() == TRUE)
8402 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8403 else
8404 ToolInfoSize = sizeof(TOOLINFOW);
8405
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008406 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008407 if (pti == NULL)
8408 return;
8409
8410 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8411 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8412 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008413 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008414
8415 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8416 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8417
8418 pti->cbSize = ToolInfoSize;
8419 pti->uFlags = TTF_SUBCLASS;
8420 pti->hwnd = beval->target;
8421 pti->hinst = 0; // Don't use string resources
8422 pti->uId = ID_BEVAL_TOOLTIP;
8423
8424 if (multiline_balloon_available() == TRUE)
8425 {
8426 RECT rect;
8427 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8428 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008429 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8430 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008431 // switch multiline tooltips on
8432 if (GetClientRect(s_textArea, &rect))
8433 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8434 (LPARAM)rect.right);
8435 }
8436 else
8437 {
8438 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008439 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8440 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008441 }
8442
8443 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8444 pti->rect.left = pt.x - 3;
8445 pti->rect.top = pt.y - 3;
8446 pti->rect.right = pt.x + 3;
8447 pti->rect.bottom = pt.y + 3;
8448
8449 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8450 // Make tooltip appear sooner.
8451 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8452 // I've performed some tests and it seems the longest possible life time
8453 // of tooltip is 30 seconds.
8454 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8455 /*
8456 * HACK: force tooltip to appear, because it'll not appear until
8457 * first mouse move. D*mn M$
8458 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8459 */
8460 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8461 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8462 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008463}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008466delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008468 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
8470
8471 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008472BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008473 HWND hwnd UNUSED,
8474 UINT uMsg UNUSED,
8475 UINT_PTR idEvent UNUSED,
8476 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 POINT pt;
8479 RECT rect;
8480
8481 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8482 return;
8483
8484 GetCursorPos(&pt);
8485 if (WindowFromPoint(pt) != s_textArea)
8486 return;
8487
8488 ScreenToClient(s_textArea, &pt);
8489 GetClientRect(s_textArea, &rect);
8490 if (!PtInRect(&rect, pt))
8491 return;
8492
8493 if (LastActivity > 0
8494 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8495 && (cur_beval->showState != ShS_PENDING
8496 || abs(cur_beval->x - pt.x) > 3
8497 || abs(cur_beval->y - pt.y) > 3))
8498 {
8499 /* Pointer resting in one place long enough, it's time to show
8500 * the tooltip. */
8501 cur_beval->showState = ShS_PENDING;
8502 cur_beval->x = pt.x;
8503 cur_beval->y = pt.y;
8504
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008505 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506
8507 if (cur_beval->msgCB != NULL)
8508 (*cur_beval->msgCB)(cur_beval, 0);
8509 }
8510}
8511
8512 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008513gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008515 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008517 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518}
8519
8520 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008521gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008523 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (beval == NULL)
8525 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008526 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008527 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008528 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008532gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008535
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008536 vim_free(beval->msg);
8537 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8538 if (beval->msg == NULL)
8539 {
8540 delete_tooltip(beval);
8541 beval->showState = ShS_NEUTRAL;
8542 return;
8543 }
8544
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008545 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 if (beval->showState == ShS_SHOWING)
8547 return;
8548 GetCursorPos(&pt);
8549 ScreenToClient(s_textArea, &pt);
8550
8551 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008553 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 gui_mch_disable_beval_area(cur_beval);
8555 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008556 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008558 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559}
8560
8561 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008562gui_mch_create_beval_area(
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02008563 void *target UNUSED, /* ignored, always use s_textArea */
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008564 char_u *mesg,
8565 void (*mesgCB)(BalloonEval *, int),
8566 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 /* partially stolen from gui_beval.c */
8569 BalloonEval *beval;
8570
8571 if (mesg != NULL && mesgCB != NULL)
8572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008573 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 return NULL;
8575 }
8576
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008577 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 if (beval != NULL)
8579 {
8580 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
8582 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 beval->msg = mesg;
8584 beval->msgCB = mesgCB;
8585 beval->clientData = clientData;
8586
8587 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 cur_beval = beval;
8589
8590 if (p_beval)
8591 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 return beval;
8594}
8595
8596 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008597Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598{
8599 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8600 return;
8601
8602 if (cur_beval != NULL)
8603 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008604 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008606 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008607 // TRACE0("TTN_SHOW {{{");
8608 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008609 break;
8610 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008611 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 delete_tooltip(cur_beval);
8613 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008614 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615
8616 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008617 break;
8618 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008619 {
8620 /* if you get there then we have new common controls */
8621 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8622 info->lpszText = (LPSTR)info->lParam;
8623 info->uFlags |= TTF_DI_SETITEM;
8624 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008625 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008626 case TTN_GETDISPINFOW:
8627 {
8628 // if we get here then we have new common controls
8629 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8630 info->lpszText = (LPWSTR)info->lParam;
8631 info->uFlags |= TTF_DI_SETITEM;
8632 }
8633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 }
8635 }
8636}
8637
8638 static void
8639TrackUserActivity(UINT uMsg)
8640{
8641 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8642 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8643 LastActivity = GetTickCount();
8644}
8645
8646 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008647gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008649#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008650 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008651#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008652 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 vim_free(beval);
8654}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008655#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8658/*
8659 * We have multiple signs to draw at the same location. Draw the
8660 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8661 */
8662 void
8663netbeans_draw_multisign_indicator(int row)
8664{
8665 int i;
8666 int y;
8667 int x;
8668
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008669 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008670 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 x = 0;
8673 y = TEXT_Y(row);
8674
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008675#if defined(FEAT_DIRECTX)
8676 if (IS_ENABLE_DIRECTX())
8677 DWriteContext_Flush(s_dwc);
8678#endif
8679
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 for (i = 0; i < gui.char_height - 3; i++)
8681 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8682
8683 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8684 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8685 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8686 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8687 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8688 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8689 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8690}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008691#endif