blob: 292fbdd40c29f8e100616cd947358d6666e3bec5 [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
316static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
317static FINDREPLACE s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static FINDREPLACEW s_findrep_struct_w;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
320static int s_findrep_is_find; /* TRUE for find dialog, FALSE
321 for find/replace dialog */
322#endif
323
324static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100325#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326static
327#endif
328HWND s_hwnd = NULL;
329static HDC s_hdc = NULL;
330static HBRUSH s_brush = NULL;
331
332#ifdef FEAT_TOOLBAR
333static HWND s_toolbarhwnd = NULL;
334static WNDPROC s_toolbar_wndproc = NULL;
335#endif
336
337#ifdef FEAT_GUI_TABLINE
338static HWND s_tabhwnd = NULL;
339static WNDPROC s_tabline_wndproc = NULL;
340static int showing_tabline = 0;
341#endif
342
343static WPARAM s_wParam = 0;
344static LPARAM s_lParam = 0;
345
346static HWND s_textArea = NULL;
347static UINT s_uMsg = 0;
348
349static char_u *s_textfield; /* Used by dialogs to pass back strings */
350
351static int s_need_activate = FALSE;
352
353/* This variable is set when waiting for an event, which is the only moment
354 * scrollbar dragging can be done directly. It's not allowed while commands
355 * are executed, because it may move the cursor and that may cause unexpected
356 * problems (e.g., while ":s" is working).
357 */
358static int allow_scrollbar = FALSE;
359
360#ifdef GLOBAL_IME
361# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
362#else
363# define MyTranslateMessage(x) TranslateMessage(x)
364#endif
365
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100366#if defined(FEAT_DIRECTX)
367 static int
368directx_enabled(void)
369{
370 if (s_dwc != NULL)
371 return 1;
372 else if (s_directx_load_attempted)
373 return 0;
374 /* load DirectX */
375 DWrite_Init();
376 s_directx_load_attempted = 1;
377 s_dwc = DWriteContext_Open();
378 directx_binddc();
379 return s_dwc != NULL ? 1 : 0;
380}
381
382 static void
383directx_binddc(void)
384{
385 if (s_textArea != NULL)
386 {
387 RECT rect;
388 GetClientRect(s_textArea, &rect);
389 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
390 }
391}
392#endif
393
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100394/* use of WindowProc depends on wide_WindowProc */
395#define MyWindowProc vim_WindowProc
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100396
397extern int current_font_height; /* this is in os_mswin.c */
398
399static struct
400{
401 UINT key_sym;
402 char_u vim_code0;
403 char_u vim_code1;
404} special_keys[] =
405{
406 {VK_UP, 'k', 'u'},
407 {VK_DOWN, 'k', 'd'},
408 {VK_LEFT, 'k', 'l'},
409 {VK_RIGHT, 'k', 'r'},
410
411 {VK_F1, 'k', '1'},
412 {VK_F2, 'k', '2'},
413 {VK_F3, 'k', '3'},
414 {VK_F4, 'k', '4'},
415 {VK_F5, 'k', '5'},
416 {VK_F6, 'k', '6'},
417 {VK_F7, 'k', '7'},
418 {VK_F8, 'k', '8'},
419 {VK_F9, 'k', '9'},
420 {VK_F10, 'k', ';'},
421
422 {VK_F11, 'F', '1'},
423 {VK_F12, 'F', '2'},
424 {VK_F13, 'F', '3'},
425 {VK_F14, 'F', '4'},
426 {VK_F15, 'F', '5'},
427 {VK_F16, 'F', '6'},
428 {VK_F17, 'F', '7'},
429 {VK_F18, 'F', '8'},
430 {VK_F19, 'F', '9'},
431 {VK_F20, 'F', 'A'},
432
433 {VK_F21, 'F', 'B'},
434#ifdef FEAT_NETBEANS_INTG
435 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
436#endif
437 {VK_F22, 'F', 'C'},
438 {VK_F23, 'F', 'D'},
439 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
440
441 {VK_HELP, '%', '1'},
442 {VK_BACK, 'k', 'b'},
443 {VK_INSERT, 'k', 'I'},
444 {VK_DELETE, 'k', 'D'},
445 {VK_HOME, 'k', 'h'},
446 {VK_END, '@', '7'},
447 {VK_PRIOR, 'k', 'P'},
448 {VK_NEXT, 'k', 'N'},
449 {VK_PRINT, '%', '9'},
450 {VK_ADD, 'K', '6'},
451 {VK_SUBTRACT, 'K', '7'},
452 {VK_DIVIDE, 'K', '8'},
453 {VK_MULTIPLY, 'K', '9'},
454 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
455 {VK_DECIMAL, 'K', 'B'},
456
457 {VK_NUMPAD0, 'K', 'C'},
458 {VK_NUMPAD1, 'K', 'D'},
459 {VK_NUMPAD2, 'K', 'E'},
460 {VK_NUMPAD3, 'K', 'F'},
461 {VK_NUMPAD4, 'K', 'G'},
462 {VK_NUMPAD5, 'K', 'H'},
463 {VK_NUMPAD6, 'K', 'I'},
464 {VK_NUMPAD7, 'K', 'J'},
465 {VK_NUMPAD8, 'K', 'K'},
466 {VK_NUMPAD9, 'K', 'L'},
467
468 /* Keys that we want to be able to use any modifier with: */
469 {VK_SPACE, ' ', NUL},
470 {VK_TAB, TAB, NUL},
471 {VK_ESCAPE, ESC, NUL},
472 {NL, NL, NUL},
473 {CAR, CAR, NUL},
474
475 /* End of list marker: */
476 {0, 0, 0}
477};
478
479/* Local variables */
480static int s_button_pending = -1;
481
482/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
483 * so don't reset s_button_pending. */
484static int s_getting_focus = FALSE;
485
486static int s_x_pending;
487static int s_y_pending;
488static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200489static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100490static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200491static int dead_key = 0; // 0: no dead key, 1: dead key pressed
492static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
493 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100495#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100496/* balloon-eval WM_NOTIFY_HANDLER */
497static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
498static void TrackUserActivity(UINT uMsg);
499#endif
500
501/*
502 * For control IME.
503 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100504 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100505 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100506#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100507/* holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont' */
508static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100509#endif
510#ifdef FEAT_MBYTE_IME
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100511/* holds LOGFONTW for 'guifont' always. */
512static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513#endif
514
515#ifdef FEAT_MBYTE_IME
516static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
517#endif
518
519#if defined(FEAT_BROWSE)
520static char_u *convert_filter(char_u *s);
521#endif
522
523#ifdef DEBUG_PRINT_ERROR
524/*
525 * Print out the last Windows error message
526 */
527 static void
528print_windows_error(void)
529{
530 LPVOID lpMsgBuf;
531
532 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
533 NULL, GetLastError(),
534 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
535 (LPTSTR) &lpMsgBuf, 0, NULL);
536 TRACE1("Error: %s\n", lpMsgBuf);
537 LocalFree(lpMsgBuf);
538}
539#endif
540
541/*
542 * Cursor blink functions.
543 *
544 * This is a simple state machine:
545 * BLINK_NONE not blinking at all
546 * BLINK_OFF blinking, cursor is not shown
547 * BLINK_ON blinking, cursor is shown
548 */
549
550#define BLINK_NONE 0
551#define BLINK_OFF 1
552#define BLINK_ON 2
553
554static int blink_state = BLINK_NONE;
555static long_u blink_waittime = 700;
556static long_u blink_ontime = 400;
557static long_u blink_offtime = 250;
558static UINT blink_timer = 0;
559
Bram Moolenaar703a8042016-06-04 16:24:32 +0200560 int
561gui_mch_is_blinking(void)
562{
563 return blink_state != BLINK_NONE;
564}
565
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200566 int
567gui_mch_is_blink_off(void)
568{
569 return blink_state == BLINK_OFF;
570}
571
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100572 void
573gui_mch_set_blinking(long wait, long on, long off)
574{
575 blink_waittime = wait;
576 blink_ontime = on;
577 blink_offtime = off;
578}
579
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100580 static VOID CALLBACK
581_OnBlinkTimer(
582 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100583 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100584 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100585 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100586{
587 MSG msg;
588
589 /*
590 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
591 */
592
593 KillTimer(NULL, idEvent);
594
595 /* Eat spurious WM_TIMER messages */
596 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
597 ;
598
599 if (blink_state == BLINK_ON)
600 {
601 gui_undraw_cursor();
602 blink_state = BLINK_OFF;
603 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
604 (TIMERPROC)_OnBlinkTimer);
605 }
606 else
607 {
608 gui_update_cursor(TRUE, FALSE);
609 blink_state = BLINK_ON;
610 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100611 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100612 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100613 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100614}
615
616 static void
617gui_mswin_rm_blink_timer(void)
618{
619 MSG msg;
620
621 if (blink_timer != 0)
622 {
623 KillTimer(NULL, blink_timer);
624 /* Eat spurious WM_TIMER messages */
625 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
626 ;
627 blink_timer = 0;
628 }
629}
630
631/*
632 * Stop the cursor blinking. Show the cursor if it wasn't shown.
633 */
634 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100635gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100636{
637 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100638 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100639 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100640 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100641 gui_mch_flush();
642 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100643 blink_state = BLINK_NONE;
644}
645
646/*
647 * Start the cursor blinking. If it was already blinking, this restarts the
648 * waiting time and shows the cursor.
649 */
650 void
651gui_mch_start_blink(void)
652{
653 gui_mswin_rm_blink_timer();
654
655 /* Only switch blinking on if none of the times is zero */
656 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
657 {
658 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
659 (TIMERPROC)_OnBlinkTimer);
660 blink_state = BLINK_ON;
661 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100662 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100663 }
664}
665
666/*
667 * Call-back routines.
668 */
669
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100670 static VOID CALLBACK
671_OnTimer(
672 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100673 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100674 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100675 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100676{
677 MSG msg;
678
679 /*
680 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
681 */
682 KillTimer(NULL, idEvent);
683 s_timed_out = TRUE;
684
685 /* Eat spurious WM_TIMER messages */
686 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
687 ;
688 if (idEvent == s_wait_timer)
689 s_wait_timer = 0;
690}
691
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692 static void
693_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100694 HWND hwnd UNUSED,
695 UINT ch UNUSED,
696 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100697{
698 dead_key = 1;
699}
700
701/*
702 * Convert Unicode character "ch" to bytes in "string[slen]".
703 * When "had_alt" is TRUE the ALT key was included in "ch".
704 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200705 * Because the Windows API uses UTF-16, we have to deal with surrogate
706 * pairs; this is where we choose to deal with them: if "ch" is a high
707 * surrogate, it will be stored, and the length returned will be zero; the next
708 * char_to_string call will then include the high surrogate, decoding the pair
709 * of UTF-16 code units to a single Unicode code point, presuming it is the
710 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100711 */
712 static int
713char_to_string(int ch, char_u *string, int slen, int had_alt)
714{
715 int len;
716 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100717 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200718 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200720 if (surrogate_pending_ch != 0)
721 {
722 /* We don't guarantee ch is a low surrogate to match the high surrogate
723 * we already have; it should be, but if it isn't, tough luck. */
724 wstring[0] = surrogate_pending_ch;
725 wstring[1] = ch;
726 surrogate_pending_ch = 0;
727 len = 2;
728 }
729 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
730 {
731 /* We don't have the entire code point yet, only the first UTF-16 code
732 * unit; so just remember it and use it in the next call. */
733 surrogate_pending_ch = ch;
734 return 0;
735 }
736 else
737 {
738 wstring[0] = ch;
739 len = 1;
740 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200741
742 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
743 * "enc_codepage" is non-zero use the standard Win32 function,
744 * otherwise use our own conversion function (e.g., for UTF-8). */
745 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100746 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200747 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
748 (LPSTR)string, slen, 0, NULL);
749 /* If we had included the ALT key into the character but now the
750 * upper bit is no longer set, that probably means the conversion
751 * failed. Convert the original character and set the upper bit
752 * afterwards. */
753 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100754 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200755 wstring[0] = ch & 0x7f;
756 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
757 (LPSTR)string, slen, 0, NULL);
758 if (len == 1) /* safety check */
759 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 }
761 }
762 else
763 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200764 ws = utf16_to_enc(wstring, &len);
765 if (ws == NULL)
766 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100767 else
768 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200769 if (len > slen) /* just in case */
770 len = slen;
771 mch_memmove(string, ws, len);
772 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100773 }
774 }
775
776 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100777 {
778 string[0] = ch;
779 len = 1;
780 }
781
782 for (i = 0; i < len; ++i)
783 if (string[i] == CSI && len <= slen - 2)
784 {
785 /* Insert CSI as K_CSI. */
786 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
787 string[++i] = KS_EXTRA;
788 string[++i] = (int)KE_CSI;
789 len += 2;
790 }
791
792 return len;
793}
794
795/*
796 * Key hit, add it to the input buffer.
797 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 static void
799_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100800 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100801 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100802 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803{
804 char_u string[40];
805 int len = 0;
806
807 dead_key = 0;
808
809 len = char_to_string(ch, string, 40, FALSE);
810 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
811 {
812 trash_input_buf();
813 got_int = TRUE;
814 }
815
816 add_to_input_buf(string, len);
817}
818
819/*
820 * Alt-Key hit, add it to the input buffer.
821 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822 static void
823_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100825 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100826 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827{
828 char_u string[40]; /* Enough for multibyte character */
829 int len;
830 int modifiers;
831 int ch = cch; /* special keys are negative */
832
833 dead_key = 0;
834
835 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
836
837 /* OK, we have a character key (given by ch) which was entered with the
838 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
839 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
840 * CAPSLOCK is pressed) at this point.
841 */
842 modifiers = MOD_MASK_ALT;
843 if (GetKeyState(VK_SHIFT) & 0x8000)
844 modifiers |= MOD_MASK_SHIFT;
845 if (GetKeyState(VK_CONTROL) & 0x8000)
846 modifiers |= MOD_MASK_CTRL;
847
848 ch = simplify_key(ch, &modifiers);
849 /* remove the SHIFT modifier for keys where it's already included, e.g.,
850 * '(' and '*' */
851 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
852 modifiers &= ~MOD_MASK_SHIFT;
853
854 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
855 ch = extract_modifiers(ch, &modifiers);
856 if (ch == CSI)
857 ch = K_CSI;
858
859 len = 0;
860 if (modifiers)
861 {
862 string[len++] = CSI;
863 string[len++] = KS_MODIFIER;
864 string[len++] = modifiers;
865 }
866
867 if (IS_SPECIAL((int)ch))
868 {
869 string[len++] = CSI;
870 string[len++] = K_SECOND((int)ch);
871 string[len++] = K_THIRD((int)ch);
872 }
873 else
874 {
875 /* Although the documentation isn't clear about it, we assume "ch" is
876 * a Unicode character. */
877 len += char_to_string(ch, string + len, 40 - len, TRUE);
878 }
879
880 add_to_input_buf(string, len);
881}
882
883 static void
884_OnMouseEvent(
885 int button,
886 int x,
887 int y,
888 int repeated_click,
889 UINT keyFlags)
890{
891 int vim_modifiers = 0x0;
892
893 s_getting_focus = FALSE;
894
895 if (keyFlags & MK_SHIFT)
896 vim_modifiers |= MOUSE_SHIFT;
897 if (keyFlags & MK_CONTROL)
898 vim_modifiers |= MOUSE_CTRL;
899 if (GetKeyState(VK_MENU) & 0x8000)
900 vim_modifiers |= MOUSE_ALT;
901
902 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
903}
904
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 static void
906_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100907 HWND hwnd UNUSED,
908 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100909 int x,
910 int y,
911 UINT keyFlags)
912{
913 static LONG s_prevTime = 0;
914
915 LONG currentTime = GetMessageTime();
916 int button = -1;
917 int repeated_click;
918
919 /* Give main window the focus: this is so the cursor isn't hollow. */
920 (void)SetFocus(s_hwnd);
921
922 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
923 button = MOUSE_LEFT;
924 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
925 button = MOUSE_MIDDLE;
926 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
927 button = MOUSE_RIGHT;
928 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
929 {
930#ifndef GET_XBUTTON_WPARAM
931# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
932#endif
933 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
934 }
935 else if (s_uMsg == WM_CAPTURECHANGED)
936 {
937 /* on W95/NT4, somehow you get in here with an odd Msg
938 * if you press one button while holding down the other..*/
939 if (s_button_pending == MOUSE_LEFT)
940 button = MOUSE_RIGHT;
941 else
942 button = MOUSE_LEFT;
943 }
944 if (button >= 0)
945 {
946 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
947
948 /*
949 * Holding down the left and right buttons simulates pushing the middle
950 * button.
951 */
952 if (repeated_click
953 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
954 || (button == MOUSE_RIGHT
955 && s_button_pending == MOUSE_LEFT)))
956 {
957 /*
958 * Hmm, gui.c will ignore more than one button down at a time, so
959 * pretend we let go of it first.
960 */
961 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
962 button = MOUSE_MIDDLE;
963 repeated_click = FALSE;
964 s_button_pending = -1;
965 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
966 }
967 else if ((repeated_click)
968 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
969 {
970 if (s_button_pending > -1)
971 {
972 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
973 s_button_pending = -1;
974 }
975 /* TRACE("Button down at x %d, y %d\n", x, y); */
976 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
977 }
978 else
979 {
980 /*
981 * If this is the first press (i.e. not a multiple click) don't
982 * action immediately, but store and wait for:
983 * i) button-up
984 * ii) mouse move
985 * iii) another button press
986 * before using it.
987 * This enables us to make left+right simulate middle button,
988 * without left or right being actioned first. The side-effect is
989 * that if you click and hold the mouse without dragging, the
990 * cursor doesn't move until you release the button. In practice
991 * this is hardly a problem.
992 */
993 s_button_pending = button;
994 s_x_pending = x;
995 s_y_pending = y;
996 s_kFlags_pending = keyFlags;
997 }
998
999 s_prevTime = currentTime;
1000 }
1001}
1002
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001003 static void
1004_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001005 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001006 int x,
1007 int y,
1008 UINT keyFlags)
1009{
1010 int button;
1011
1012 s_getting_focus = FALSE;
1013 if (s_button_pending > -1)
1014 {
1015 /* Delayed action for mouse down event */
1016 _OnMouseEvent(s_button_pending, s_x_pending,
1017 s_y_pending, FALSE, s_kFlags_pending);
1018 s_button_pending = -1;
1019 }
1020 if (s_uMsg == WM_MOUSEMOVE)
1021 {
1022 /*
1023 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1024 * down.
1025 */
1026 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1027 | MK_XBUTTON1 | MK_XBUTTON2)))
1028 {
1029 gui_mouse_moved(x, y);
1030 return;
1031 }
1032
1033 /*
1034 * While button is down, keep grabbing mouse move events when
1035 * the mouse goes outside the window
1036 */
1037 SetCapture(s_textArea);
1038 button = MOUSE_DRAG;
1039 /* TRACE(" move at x %d, y %d\n", x, y); */
1040 }
1041 else
1042 {
1043 ReleaseCapture();
1044 button = MOUSE_RELEASE;
1045 /* TRACE(" up at x %d, y %d\n", x, y); */
1046 }
1047
1048 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1049}
1050
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001051 static void
1052_OnSizeTextArea(
1053 HWND hwnd UNUSED,
1054 UINT state UNUSED,
1055 int cx UNUSED,
1056 int cy UNUSED)
1057{
1058#if defined(FEAT_DIRECTX)
1059 if (IS_ENABLE_DIRECTX())
1060 directx_binddc();
1061#endif
1062}
1063
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001064#ifdef FEAT_MENU
1065/*
1066 * Find the vimmenu_T with the given id
1067 */
1068 static vimmenu_T *
1069gui_mswin_find_menu(
1070 vimmenu_T *pMenu,
1071 int id)
1072{
1073 vimmenu_T *pChildMenu;
1074
1075 while (pMenu)
1076 {
1077 if (pMenu->id == (UINT)id)
1078 break;
1079 if (pMenu->children != NULL)
1080 {
1081 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1082 if (pChildMenu)
1083 {
1084 pMenu = pChildMenu;
1085 break;
1086 }
1087 }
1088 pMenu = pMenu->next;
1089 }
1090 return pMenu;
1091}
1092
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001093 static void
1094_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001095 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001097 HWND hwndCtl UNUSED,
1098 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001099{
1100 vimmenu_T *pMenu;
1101
1102 pMenu = gui_mswin_find_menu(root_menu, id);
1103 if (pMenu)
1104 gui_menu_cb(pMenu);
1105}
1106#endif
1107
1108#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001109/*
1110 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1111 */
1112 static void
1113findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1114{
1115 WCHAR *wp;
1116
1117 lpfrw->hwndOwner = lpfr->hwndOwner;
1118 lpfrw->Flags = lpfr->Flags;
1119
1120 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1121 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1122 vim_free(wp);
1123
1124 /* the field "lpstrReplaceWith" doesn't need to be copied */
1125}
1126
1127/*
1128 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1129 */
1130 static void
1131findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1132{
1133 char_u *p;
1134
1135 lpfr->Flags = lpfrw->Flags;
1136
1137 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1138 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1139 vim_free(p);
1140
1141 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1142 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1143 vim_free(p);
1144}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145
1146/*
1147 * Handle a Find/Replace window message.
1148 */
1149 static void
1150_OnFindRepl(void)
1151{
1152 int flags = 0;
1153 int down;
1154
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001155 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1156 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001157 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001158 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001159
1160 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1161 /* Give main window the focus back. */
1162 (void)SetFocus(s_hwnd);
1163
1164 if (s_findrep_struct.Flags & FR_FINDNEXT)
1165 {
1166 flags = FRD_FINDNEXT;
1167
1168 /* Give main window the focus back: this is so the cursor isn't
1169 * hollow. */
1170 (void)SetFocus(s_hwnd);
1171 }
1172 else if (s_findrep_struct.Flags & FR_REPLACE)
1173 {
1174 flags = FRD_REPLACE;
1175
1176 /* Give main window the focus back: this is so the cursor isn't
1177 * hollow. */
1178 (void)SetFocus(s_hwnd);
1179 }
1180 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1181 {
1182 flags = FRD_REPLACEALL;
1183 }
1184
1185 if (flags != 0)
1186 {
1187 /* Call the generic GUI function to do the actual work. */
1188 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1189 flags |= FRD_WHOLE_WORD;
1190 if (s_findrep_struct.Flags & FR_MATCHCASE)
1191 flags |= FRD_MATCH_CASE;
1192 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1193 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1194 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1195 }
1196}
1197#endif
1198
1199 static void
1200HandleMouseHide(UINT uMsg, LPARAM lParam)
1201{
1202 static LPARAM last_lParam = 0L;
1203
1204 /* We sometimes get a mousemove when the mouse didn't move... */
1205 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1206 {
1207 if (lParam == last_lParam)
1208 return;
1209 last_lParam = lParam;
1210 }
1211
1212 /* Handle specially, to centralise coding. We need to be sure we catch all
1213 * possible events which should cause us to restore the cursor (as it is a
1214 * shared resource, we take full responsibility for it).
1215 */
1216 switch (uMsg)
1217 {
1218 case WM_KEYUP:
1219 case WM_CHAR:
1220 /*
1221 * blank out the pointer if necessary
1222 */
1223 if (p_mh)
1224 gui_mch_mousehide(TRUE);
1225 break;
1226
1227 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1228 case WM_SYSCHAR:
1229 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1230 case WM_LBUTTONDOWN:
1231 case WM_LBUTTONUP:
1232 case WM_MBUTTONDOWN:
1233 case WM_MBUTTONUP:
1234 case WM_RBUTTONDOWN:
1235 case WM_RBUTTONUP:
1236 case WM_XBUTTONDOWN:
1237 case WM_XBUTTONUP:
1238 case WM_NCMOUSEMOVE:
1239 case WM_NCLBUTTONDOWN:
1240 case WM_NCLBUTTONUP:
1241 case WM_NCMBUTTONDOWN:
1242 case WM_NCMBUTTONUP:
1243 case WM_NCRBUTTONDOWN:
1244 case WM_NCRBUTTONUP:
1245 case WM_KILLFOCUS:
1246 /*
1247 * if the pointer is currently hidden, then we should show it.
1248 */
1249 gui_mch_mousehide(FALSE);
1250 break;
1251 }
1252}
1253
1254 static LRESULT CALLBACK
1255_TextAreaWndProc(
1256 HWND hwnd,
1257 UINT uMsg,
1258 WPARAM wParam,
1259 LPARAM lParam)
1260{
1261 /*
1262 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1263 hwnd, uMsg, wParam, lParam);
1264 */
1265
1266 HandleMouseHide(uMsg, lParam);
1267
1268 s_uMsg = uMsg;
1269 s_wParam = wParam;
1270 s_lParam = lParam;
1271
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001272#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 TrackUserActivity(uMsg);
1274#endif
1275
1276 switch (uMsg)
1277 {
1278 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1279 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1280 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1281 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1282 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1283 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1284 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1285 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1286 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1287 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1288 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1289 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1290 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1291 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001292 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001293
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001294#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001295 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1296 return TRUE;
1297#endif
1298 default:
1299 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1300 }
1301}
1302
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001303#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001304typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001305#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001306
1307 LRESULT WINAPI
1308vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1309{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001310#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001312#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001313 if (wide_WindowProc)
1314 return DefWindowProcW(hwnd, message, wParam, lParam);
1315 return DefWindowProc(hwnd, message, wParam, lParam);
1316#endif
1317}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001318
1319/*
1320 * Called when the foreground or background color has been changed.
1321 */
1322 void
1323gui_mch_new_colors(void)
1324{
1325 /* nothing to do? */
1326}
1327
1328/*
1329 * Set the colors to their default values.
1330 */
1331 void
1332gui_mch_def_colors(void)
1333{
1334 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1335 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1336 gui.def_norm_pixel = gui.norm_pixel;
1337 gui.def_back_pixel = gui.back_pixel;
1338}
1339
1340/*
1341 * Open the GUI window which was created by a call to gui_mch_init().
1342 */
1343 int
1344gui_mch_open(void)
1345{
1346#ifndef SW_SHOWDEFAULT
1347# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1348#endif
1349 /* Actually open the window, if not already visible
1350 * (may be done already in gui_mch_set_shellsize) */
1351 if (!IsWindowVisible(s_hwnd))
1352 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1353
1354#ifdef MSWIN_FIND_REPLACE
1355 /* Init replace string here, so that we keep it when re-opening the
1356 * dialog. */
1357 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1358#endif
1359
1360 return OK;
1361}
1362
1363/*
1364 * Get the position of the top left corner of the window.
1365 */
1366 int
1367gui_mch_get_winpos(int *x, int *y)
1368{
1369 RECT rect;
1370
1371 GetWindowRect(s_hwnd, &rect);
1372 *x = rect.left;
1373 *y = rect.top;
1374 return OK;
1375}
1376
1377/*
1378 * Set the position of the top left corner of the window to the given
1379 * coordinates.
1380 */
1381 void
1382gui_mch_set_winpos(int x, int y)
1383{
1384 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1385 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1386}
1387 void
1388gui_mch_set_text_area_pos(int x, int y, int w, int h)
1389{
1390 static int oldx = 0;
1391 static int oldy = 0;
1392
1393 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1394
1395#ifdef FEAT_TOOLBAR
1396 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1397 SendMessage(s_toolbarhwnd, WM_SIZE,
1398 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1399#endif
1400#if defined(FEAT_GUI_TABLINE)
1401 if (showing_tabline)
1402 {
1403 int top = 0;
1404 RECT rect;
1405
1406# ifdef FEAT_TOOLBAR
1407 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1408 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1409# endif
1410 GetClientRect(s_hwnd, &rect);
1411 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1412 }
1413#endif
1414
1415 /* When side scroll bar is unshown, the size of window will change.
1416 * then, the text area move left or right. thus client rect should be
1417 * forcedly redrawn. (Yasuhiro Matsumoto) */
1418 if (oldx != x || oldy != y)
1419 {
1420 InvalidateRect(s_hwnd, NULL, FALSE);
1421 oldx = x;
1422 oldy = y;
1423 }
1424}
1425
1426
1427/*
1428 * Scrollbar stuff:
1429 */
1430
1431 void
1432gui_mch_enable_scrollbar(
1433 scrollbar_T *sb,
1434 int flag)
1435{
1436 ShowScrollBar(sb->id, SB_CTL, flag);
1437
1438 /* TODO: When the window is maximized, the size of the window stays the
1439 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1440 * NT 4.0 it's not... */
1441}
1442
1443 void
1444gui_mch_set_scrollbar_pos(
1445 scrollbar_T *sb,
1446 int x,
1447 int y,
1448 int w,
1449 int h)
1450{
1451 SetWindowPos(sb->id, NULL, x, y, w, h,
1452 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1453}
1454
1455 void
1456gui_mch_create_scrollbar(
1457 scrollbar_T *sb,
1458 int orient) /* SBAR_VERT or SBAR_HORIZ */
1459{
1460 sb->id = CreateWindow(
1461 "SCROLLBAR", "Scrollbar",
1462 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1463 10, /* Any value will do for now */
1464 10, /* Any value will do for now */
1465 s_hwnd, NULL,
1466 s_hinst, NULL);
1467}
1468
1469/*
1470 * Find the scrollbar with the given hwnd.
1471 */
1472 static scrollbar_T *
1473gui_mswin_find_scrollbar(HWND hwnd)
1474{
1475 win_T *wp;
1476
1477 if (gui.bottom_sbar.id == hwnd)
1478 return &gui.bottom_sbar;
1479 FOR_ALL_WINDOWS(wp)
1480 {
1481 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1482 return &wp->w_scrollbars[SBAR_LEFT];
1483 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1484 return &wp->w_scrollbars[SBAR_RIGHT];
1485 }
1486 return NULL;
1487}
1488
1489/*
1490 * Get the character size of a font.
1491 */
1492 static void
1493GetFontSize(GuiFont font)
1494{
1495 HWND hwnd = GetDesktopWindow();
1496 HDC hdc = GetWindowDC(hwnd);
1497 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1498 TEXTMETRIC tm;
1499
1500 GetTextMetrics(hdc, &tm);
1501 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1502
1503 gui.char_height = tm.tmHeight + p_linespace;
1504
1505 SelectFont(hdc, hfntOld);
1506
1507 ReleaseDC(hwnd, hdc);
1508}
1509
1510/*
1511 * Adjust gui.char_height (after 'linespace' was changed).
1512 */
1513 int
1514gui_mch_adjust_charheight(void)
1515{
1516 GetFontSize(gui.norm_font);
1517 return OK;
1518}
1519
1520 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001521get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001522{
1523 HFONT font = NULL;
1524
1525 /* Load the font */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001526 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001527
1528 if (font == NULL)
1529 return NOFONT;
1530
1531 return (GuiFont)font;
1532}
1533
1534 static int
1535pixels_to_points(int pixels, int vertical)
1536{
1537 int points;
1538 HWND hwnd;
1539 HDC hdc;
1540
1541 hwnd = GetDesktopWindow();
1542 hdc = GetWindowDC(hwnd);
1543
1544 points = MulDiv(pixels, 72,
1545 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1546
1547 ReleaseDC(hwnd, hdc);
1548
1549 return points;
1550}
1551
1552 GuiFont
1553gui_mch_get_font(
1554 char_u *name,
1555 int giveErrorIfMissing)
1556{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001557 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001558 GuiFont font = NOFONT;
1559
1560 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1561 font = get_font_handle(&lf);
1562 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001563 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001564 return font;
1565}
1566
1567#if defined(FEAT_EVAL) || defined(PROTO)
1568/*
1569 * Return the name of font "font" in allocated memory.
1570 * Don't know how to get the actual name, thus use the provided name.
1571 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001572 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001573gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001574{
1575 if (name == NULL)
1576 return NULL;
1577 return vim_strsave(name);
1578}
1579#endif
1580
1581 void
1582gui_mch_free_font(GuiFont font)
1583{
1584 if (font)
1585 DeleteObject((HFONT)font);
1586}
1587
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001588/*
1589 * Return the Pixel value (color) for the given color name.
1590 * Return INVALCOLOR for error.
1591 */
1592 guicolor_T
1593gui_mch_get_color(char_u *name)
1594{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001595 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596
1597 typedef struct SysColorTable
1598 {
1599 char *name;
1600 int color;
1601 } SysColorTable;
1602
1603 static SysColorTable sys_table[] =
1604 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001605 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1606 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001607#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001608 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1609#endif
1610 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1611 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1612 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1613 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1614 {"SYS_DESKTOP", COLOR_DESKTOP},
1615 {"SYS_INFOBK", COLOR_INFOBK},
1616 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1617 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001618 {"SYS_BTNFACE", COLOR_BTNFACE},
1619 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1620 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1621 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1622 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1623 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1624 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1625 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1626 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1627 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1628 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1629 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1630 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1631 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1632 {"SYS_MENU", COLOR_MENU},
1633 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1634 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1635 {"SYS_WINDOW", COLOR_WINDOW},
1636 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1637 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1638 };
1639
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 /*
1641 * Try to look up a system colour.
1642 */
1643 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1644 if (STRICMP(name, sys_table[i].name) == 0)
1645 return GetSysColor(sys_table[i].color);
1646
Bram Moolenaarab302212016-04-26 20:59:29 +02001647 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001649
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001650 guicolor_T
1651gui_mch_get_rgb_color(int r, int g, int b)
1652{
1653 return gui_get_rgb_color_cmn(r, g, b);
1654}
1655
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656/*
1657 * Return OK if the key with the termcap name "name" is supported.
1658 */
1659 int
1660gui_mch_haskey(char_u *name)
1661{
1662 int i;
1663
1664 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1665 if (name[0] == special_keys[i].vim_code0 &&
1666 name[1] == special_keys[i].vim_code1)
1667 return OK;
1668 return FAIL;
1669}
1670
1671 void
1672gui_mch_beep(void)
1673{
1674 MessageBeep(MB_OK);
1675}
1676/*
1677 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1678 */
1679 void
1680gui_mch_invert_rectangle(
1681 int r,
1682 int c,
1683 int nr,
1684 int nc)
1685{
1686 RECT rc;
1687
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001688#if defined(FEAT_DIRECTX)
1689 if (IS_ENABLE_DIRECTX())
1690 DWriteContext_Flush(s_dwc);
1691#endif
1692
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001693 /*
1694 * Note: InvertRect() excludes right and bottom of rectangle.
1695 */
1696 rc.left = FILL_X(c);
1697 rc.top = FILL_Y(r);
1698 rc.right = rc.left + nc * gui.char_width;
1699 rc.bottom = rc.top + nr * gui.char_height;
1700 InvertRect(s_hdc, &rc);
1701}
1702
1703/*
1704 * Iconify the GUI window.
1705 */
1706 void
1707gui_mch_iconify(void)
1708{
1709 ShowWindow(s_hwnd, SW_MINIMIZE);
1710}
1711
1712/*
1713 * Draw a cursor without focus.
1714 */
1715 void
1716gui_mch_draw_hollow_cursor(guicolor_T color)
1717{
1718 HBRUSH hbr;
1719 RECT rc;
1720
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001721#if defined(FEAT_DIRECTX)
1722 if (IS_ENABLE_DIRECTX())
1723 DWriteContext_Flush(s_dwc);
1724#endif
1725
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001726 /*
1727 * Note: FrameRect() excludes right and bottom of rectangle.
1728 */
1729 rc.left = FILL_X(gui.col);
1730 rc.top = FILL_Y(gui.row);
1731 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001732 if (mb_lefthalve(gui.row, gui.col))
1733 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001734 rc.bottom = rc.top + gui.char_height;
1735 hbr = CreateSolidBrush(color);
1736 FrameRect(s_hdc, &rc, hbr);
1737 DeleteBrush(hbr);
1738}
1739/*
1740 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1741 * color "color".
1742 */
1743 void
1744gui_mch_draw_part_cursor(
1745 int w,
1746 int h,
1747 guicolor_T color)
1748{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001749 RECT rc;
1750
1751 /*
1752 * Note: FillRect() excludes right and bottom of rectangle.
1753 */
1754 rc.left =
1755#ifdef FEAT_RIGHTLEFT
1756 /* vertical line should be on the right of current point */
1757 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1758#endif
1759 FILL_X(gui.col);
1760 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1761 rc.right = rc.left + w;
1762 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001763
Bram Moolenaar92467d32017-12-05 13:22:16 +01001764 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765}
1766
1767
1768/*
1769 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1770 * dead key's nominal character and re-post the original message.
1771 */
1772 static void
1773outputDeadKey_rePost(MSG originalMsg)
1774{
1775 static MSG deadCharExpel;
1776
1777 if (!dead_key)
1778 return;
1779
1780 dead_key = 0;
1781
1782 /* Make Windows generate the dead key's character */
1783 deadCharExpel.message = originalMsg.message;
1784 deadCharExpel.hwnd = originalMsg.hwnd;
1785 deadCharExpel.wParam = VK_SPACE;
1786
1787 MyTranslateMessage(&deadCharExpel);
1788
1789 /* re-generate the current character free of the dead char influence */
1790 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1791 originalMsg.lParam);
1792}
1793
1794
1795/*
1796 * Process a single Windows message.
1797 * If one is not available we hang until one is.
1798 */
1799 static void
1800process_message(void)
1801{
1802 MSG msg;
1803 UINT vk = 0; /* Virtual key */
1804 char_u string[40];
1805 int i;
1806 int modifiers = 0;
1807 int key;
1808#ifdef FEAT_MENU
1809 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1810#endif
1811
1812 pGetMessage(&msg, NULL, 0, 0);
1813
1814#ifdef FEAT_OLE
1815 /* Look after OLE Automation commands */
1816 if (msg.message == WM_OLE)
1817 {
1818 char_u *str = (char_u *)msg.lParam;
1819 if (str == NULL || *str == NUL)
1820 {
1821 /* Message can't be ours, forward it. Fixes problem with Ultramon
1822 * 3.0.4 */
1823 pDispatchMessage(&msg);
1824 }
1825 else
1826 {
1827 add_to_input_buf(str, (int)STRLEN(str));
1828 vim_free(str); /* was allocated in CVim::SendKeys() */
1829 }
1830 return;
1831 }
1832#endif
1833
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001834#ifdef MSWIN_FIND_REPLACE
1835 /* Don't process messages used by the dialog */
1836 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1837 {
1838 HandleMouseHide(msg.message, msg.lParam);
1839 return;
1840 }
1841#endif
1842
1843 /*
1844 * Check if it's a special key that we recognise. If not, call
1845 * TranslateMessage().
1846 */
1847 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1848 {
1849 vk = (int) msg.wParam;
1850
1851 /*
1852 * Handle dead keys in special conditions in other cases we let Windows
1853 * handle them and do not interfere.
1854 *
1855 * The dead_key flag must be reset on several occasions:
1856 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1857 * consumed at that point (This is when we let Windows combine the
1858 * dead character on its own)
1859 *
1860 * - Before doing something special such as regenerating keypresses to
1861 * expel the dead character as this could trigger an infinite loop if
1862 * for some reason MyTranslateMessage() do not trigger a call
1863 * immediately to _OnChar() (or _OnSysChar()).
1864 */
1865 if (dead_key)
1866 {
1867 /*
1868 * If a dead key was pressed and the user presses VK_SPACE,
1869 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1870 * with the dead char now, so do nothing special and let Windows
1871 * handle it.
1872 *
1873 * Note that VK_SPACE combines with the dead_key's character and
1874 * only one WM_CHAR will be generated by TranslateMessage(), in
1875 * the two other cases two WM_CHAR will be generated: the dead
1876 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1877 * user expects.
1878 */
1879 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1880 {
1881 dead_key = 0;
1882 MyTranslateMessage(&msg);
1883 return;
1884 }
1885 /* In modes where we are not typing, dead keys should behave
1886 * normally */
1887 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1888 {
1889 outputDeadKey_rePost(msg);
1890 return;
1891 }
1892 }
1893
1894 /* Check for CTRL-BREAK */
1895 if (vk == VK_CANCEL)
1896 {
1897 trash_input_buf();
1898 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001899 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001900 string[0] = Ctrl_C;
1901 add_to_input_buf(string, 1);
1902 }
1903
1904 for (i = 0; special_keys[i].key_sym != 0; i++)
1905 {
1906 /* ignore VK_SPACE when ALT key pressed: system menu */
1907 if (special_keys[i].key_sym == vk
1908 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1909 {
1910 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001911 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001912 * is a key that would normally trigger the dead key nominal
1913 * character output (such as a NUMPAD printable character or
1914 * the TAB key, etc...).
1915 */
1916 if (dead_key && (special_keys[i].vim_code0 == 'K'
1917 || vk == VK_TAB || vk == CAR))
1918 {
1919 outputDeadKey_rePost(msg);
1920 return;
1921 }
1922
1923#ifdef FEAT_MENU
1924 /* Check for <F10>: Windows selects the menu. When <F10> is
1925 * mapped we want to use the mapping instead. */
1926 if (vk == VK_F10
1927 && gui.menu_is_active
1928 && check_map(k10, State, FALSE, TRUE, FALSE,
1929 NULL, NULL) == NULL)
1930 break;
1931#endif
1932 if (GetKeyState(VK_SHIFT) & 0x8000)
1933 modifiers |= MOD_MASK_SHIFT;
1934 /*
1935 * Don't use caps-lock as shift, because these are special keys
1936 * being considered here, and we only want letters to get
1937 * shifted -- webb
1938 */
1939 /*
1940 if (GetKeyState(VK_CAPITAL) & 0x0001)
1941 modifiers ^= MOD_MASK_SHIFT;
1942 */
1943 if (GetKeyState(VK_CONTROL) & 0x8000)
1944 modifiers |= MOD_MASK_CTRL;
1945 if (GetKeyState(VK_MENU) & 0x8000)
1946 modifiers |= MOD_MASK_ALT;
1947
1948 if (special_keys[i].vim_code1 == NUL)
1949 key = special_keys[i].vim_code0;
1950 else
1951 key = TO_SPECIAL(special_keys[i].vim_code0,
1952 special_keys[i].vim_code1);
1953 key = simplify_key(key, &modifiers);
1954 if (key == CSI)
1955 key = K_CSI;
1956
1957 if (modifiers)
1958 {
1959 string[0] = CSI;
1960 string[1] = KS_MODIFIER;
1961 string[2] = modifiers;
1962 add_to_input_buf(string, 3);
1963 }
1964
1965 if (IS_SPECIAL(key))
1966 {
1967 string[0] = CSI;
1968 string[1] = K_SECOND(key);
1969 string[2] = K_THIRD(key);
1970 add_to_input_buf(string, 3);
1971 }
1972 else
1973 {
1974 int len;
1975
1976 /* Handle "key" as a Unicode character. */
1977 len = char_to_string(key, string, 40, FALSE);
1978 add_to_input_buf(string, len);
1979 }
1980 break;
1981 }
1982 }
1983 if (special_keys[i].key_sym == 0)
1984 {
1985 /* Some keys need C-S- where they should only need C-.
1986 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1987 * system startup (Helmut Stiegler, 2003 Oct 3). */
1988 if (vk != 0xff
1989 && (GetKeyState(VK_CONTROL) & 0x8000)
1990 && !(GetKeyState(VK_SHIFT) & 0x8000)
1991 && !(GetKeyState(VK_MENU) & 0x8000))
1992 {
1993 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1994 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1995 {
1996 string[0] = Ctrl_HAT;
1997 add_to_input_buf(string, 1);
1998 }
1999 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2000 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2001 {
2002 string[0] = Ctrl__;
2003 add_to_input_buf(string, 1);
2004 }
2005 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2006 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2007 {
2008 string[0] = Ctrl_AT;
2009 add_to_input_buf(string, 1);
2010 }
2011 else
2012 MyTranslateMessage(&msg);
2013 }
2014 else
2015 MyTranslateMessage(&msg);
2016 }
2017 }
2018#ifdef FEAT_MBYTE_IME
2019 else if (msg.message == WM_IME_NOTIFY)
2020 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2021 else if (msg.message == WM_KEYUP && im_get_status())
2022 /* added for non-MS IME (Yasuhiro Matsumoto) */
2023 MyTranslateMessage(&msg);
2024#endif
2025#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2026/* GIME_TEST */
2027 else if (msg.message == WM_IME_STARTCOMPOSITION)
2028 {
2029 POINT point;
2030
2031 global_ime_set_font(&norm_logfont);
2032 point.x = FILL_X(gui.col);
2033 point.y = FILL_Y(gui.row);
2034 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2035 global_ime_set_position(&point);
2036 }
2037#endif
2038
2039#ifdef FEAT_MENU
2040 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2041 * mapped we need to stop it here to avoid strange effects (e.g., for the
2042 * key-up event) */
2043 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2044 NULL, NULL) == NULL)
2045#endif
2046 pDispatchMessage(&msg);
2047}
2048
2049/*
2050 * Catch up with any queued events. This may put keyboard input into the
2051 * input buffer, call resize call-backs, trigger timers etc. If there is
2052 * nothing in the event queue (& no timers pending), then we return
2053 * immediately.
2054 */
2055 void
2056gui_mch_update(void)
2057{
2058 MSG msg;
2059
2060 if (!s_busy_processing)
2061 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2062 && !vim_is_input_buf_full())
2063 process_message();
2064}
2065
Bram Moolenaar4231da42016-06-02 14:30:04 +02002066 static void
2067remove_any_timer(void)
2068{
2069 MSG msg;
2070
2071 if (s_wait_timer != 0 && !s_timed_out)
2072 {
2073 KillTimer(NULL, s_wait_timer);
2074
2075 /* Eat spurious WM_TIMER messages */
2076 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2077 ;
2078 s_wait_timer = 0;
2079 }
2080}
2081
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082/*
2083 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2084 * from the keyboard.
2085 * wtime == -1 Wait forever.
2086 * wtime == 0 This should never happen.
2087 * wtime > 0 Wait wtime milliseconds for a character.
2088 * Returns OK if a character was found to be available within the given time,
2089 * or FAIL otherwise.
2090 */
2091 int
2092gui_mch_wait_for_chars(int wtime)
2093{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094 int focus;
2095
2096 s_timed_out = FALSE;
2097
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002098 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002099 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002100 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002101 if (s_busy_processing)
2102 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002103
2104 // When called with "wtime" zero, just want one msec.
2105 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106 (TIMERPROC)_OnTimer);
2107 }
2108
2109 allow_scrollbar = TRUE;
2110
2111 focus = gui.in_focus;
2112 while (!s_timed_out)
2113 {
2114 /* Stop or start blinking when focus changes */
2115 if (gui.in_focus != focus)
2116 {
2117 if (gui.in_focus)
2118 gui_mch_start_blink();
2119 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002120 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 focus = gui.in_focus;
2122 }
2123
2124 if (s_need_activate)
2125 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002126 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002127 s_need_activate = FALSE;
2128 }
2129
Bram Moolenaar4231da42016-06-02 14:30:04 +02002130#ifdef FEAT_TIMERS
2131 did_add_timer = FALSE;
2132#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002134 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002135 for (;;)
2136 {
2137 MSG msg;
2138
2139 parse_queued_messages();
2140
Bram Moolenaar62426e12017-08-13 15:37:58 +02002141 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2142 {
2143 process_message();
2144 break;
2145 }
2146 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2147 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002148 break;
2149 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002150#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002151 /*
2152 * Don't use gui_mch_update() because then we will spin-lock until a
2153 * char arrives, instead we use GetMessage() to hang until an
2154 * event arrives. No need to check for input_buf_full because we are
2155 * returning as soon as it contains a single char -- webb
2156 */
2157 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002158#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002159
2160 if (input_available())
2161 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002162 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002163 allow_scrollbar = FALSE;
2164
2165 /* Clear pending mouse button, the release event may have been
2166 * taken by the dialog window. But don't do this when getting
2167 * focus, we need the mouse-up event then. */
2168 if (!s_getting_focus)
2169 s_button_pending = -1;
2170
2171 return OK;
2172 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002173
2174#ifdef FEAT_TIMERS
2175 if (did_add_timer)
2176 {
2177 /* Need to recompute the waiting time. */
2178 remove_any_timer();
2179 break;
2180 }
2181#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002182 }
2183 allow_scrollbar = FALSE;
2184 return FAIL;
2185}
2186
2187/*
2188 * Clear a rectangular region of the screen from text pos (row1, col1) to
2189 * (row2, col2) inclusive.
2190 */
2191 void
2192gui_mch_clear_block(
2193 int row1,
2194 int col1,
2195 int row2,
2196 int col2)
2197{
2198 RECT rc;
2199
2200 /*
2201 * Clear one extra pixel at the far right, for when bold characters have
2202 * spilled over to the window border.
2203 * Note: FillRect() excludes right and bottom of rectangle.
2204 */
2205 rc.left = FILL_X(col1);
2206 rc.top = FILL_Y(row1);
2207 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2208 rc.bottom = FILL_Y(row2 + 1);
2209 clear_rect(&rc);
2210}
2211
2212/*
2213 * Clear the whole text window.
2214 */
2215 void
2216gui_mch_clear_all(void)
2217{
2218 RECT rc;
2219
2220 rc.left = 0;
2221 rc.top = 0;
2222 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2223 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2224 clear_rect(&rc);
2225}
2226/*
2227 * Menu stuff.
2228 */
2229
2230 void
2231gui_mch_enable_menu(int flag)
2232{
2233#ifdef FEAT_MENU
2234 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2235#endif
2236}
2237
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238 void
2239gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002240 int x UNUSED,
2241 int y UNUSED,
2242 int w UNUSED,
2243 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002244{
2245 /* It will be in the right place anyway */
2246}
2247
2248#if defined(FEAT_MENU) || defined(PROTO)
2249/*
2250 * Make menu item hidden or not hidden
2251 */
2252 void
2253gui_mch_menu_hidden(
2254 vimmenu_T *menu,
2255 int hidden)
2256{
2257 /*
2258 * This doesn't do what we want. Hmm, just grey the menu items for now.
2259 */
2260 /*
2261 if (hidden)
2262 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2263 else
2264 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2265 */
2266 gui_mch_menu_grey(menu, hidden);
2267}
2268
2269/*
2270 * This is called after setting all the menus to grey/hidden or not.
2271 */
2272 void
2273gui_mch_draw_menubar(void)
2274{
2275 DrawMenuBar(s_hwnd);
2276}
2277#endif /*FEAT_MENU*/
2278
2279#ifndef PROTO
2280void
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281_cdecl
2282SaveInst(HINSTANCE hInst)
2283{
2284 s_hinst = hInst;
2285}
2286#endif
2287
2288/*
2289 * Return the RGB value of a pixel as a long.
2290 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002291 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292gui_mch_get_rgb(guicolor_T pixel)
2293{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002294 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2295 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002296}
2297
2298#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2299/* Convert pixels in X to dialog units */
2300 static WORD
2301PixelToDialogX(int numPixels)
2302{
2303 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2304}
2305
2306/* Convert pixels in Y to dialog units */
2307 static WORD
2308PixelToDialogY(int numPixels)
2309{
2310 return (WORD)((numPixels * 8) / s_dlgfntheight);
2311}
2312
2313/* Return the width in pixels of the given text in the given DC. */
2314 static int
2315GetTextWidth(HDC hdc, char_u *str, int len)
2316{
2317 SIZE size;
2318
2319 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2320 return size.cx;
2321}
2322
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002323/*
2324 * Return the width in pixels of the given text in the given DC, taking care
2325 * of 'encoding' to active codepage conversion.
2326 */
2327 static int
2328GetTextWidthEnc(HDC hdc, char_u *str, int len)
2329{
2330 SIZE size;
2331 WCHAR *wstr;
2332 int n;
2333 int wlen = len;
2334
2335 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2336 {
2337 /* 'encoding' differs from active codepage: convert text and use wide
2338 * function */
2339 wstr = enc_to_utf16(str, &wlen);
2340 if (wstr != NULL)
2341 {
2342 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2343 vim_free(wstr);
2344 if (n)
2345 return size.cx;
2346 }
2347 }
2348
2349 return GetTextWidth(hdc, str, len);
2350}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002352static void get_work_area(RECT *spi_rect);
2353
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002354/*
2355 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002356 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2357 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002358 */
2359 static BOOL
2360CenterWindow(
2361 HWND hwndChild,
2362 HWND hwndParent)
2363{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 HMONITOR mon;
2365 MONITORINFO moninfo;
2366 RECT rChild, rParent, rScreen;
2367 int wChild, hChild, wParent, hParent;
2368 int xNew, yNew;
2369 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002370
2371 GetWindowRect(hwndChild, &rChild);
2372 wChild = rChild.right - rChild.left;
2373 hChild = rChild.bottom - rChild.top;
2374
2375 /* If Vim is minimized put the window in the middle of the screen. */
2376 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002377 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378 else
2379 GetWindowRect(hwndParent, &rParent);
2380 wParent = rParent.right - rParent.left;
2381 hParent = rParent.bottom - rParent.top;
2382
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002383 moninfo.cbSize = sizeof(MONITORINFO);
2384 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2385 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002386 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002387 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002388 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002389 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002390 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002391 hdc = GetDC(hwndChild);
2392 rScreen.left = 0;
2393 rScreen.top = 0;
2394 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2395 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2396 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397 }
2398
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002399 xNew = rParent.left + ((wParent - wChild) / 2);
2400 if (xNew < rScreen.left)
2401 xNew = rScreen.left;
2402 else if ((xNew + wChild) > rScreen.right)
2403 xNew = rScreen.right - wChild;
2404
2405 yNew = rParent.top + ((hParent - hChild) / 2);
2406 if (yNew < rScreen.top)
2407 yNew = rScreen.top;
2408 else if ((yNew + hChild) > rScreen.bottom)
2409 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002410
2411 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2412 SWP_NOSIZE | SWP_NOZORDER);
2413}
2414#endif /* FEAT_GUI_DIALOG */
2415
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002416#if defined(FEAT_TOOLBAR) || defined(PROTO)
2417 void
2418gui_mch_show_toolbar(int showit)
2419{
2420 if (s_toolbarhwnd == NULL)
2421 return;
2422
2423 if (showit)
2424 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002425# ifndef TB_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002427# define TB_SETUNICODEFORMAT 0x2005
2428# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429 /* Enable/disable unicode support */
2430 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2431 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 ShowWindow(s_toolbarhwnd, SW_SHOW);
2433 }
2434 else
2435 ShowWindow(s_toolbarhwnd, SW_HIDE);
2436}
2437
2438/* Then number of bitmaps is fixed. Exit is missing! */
2439#define TOOLBAR_BITMAP_COUNT 31
2440
2441#endif
2442
2443#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2444 static void
2445add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2446{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002448
2449 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2450 {
2451 /* 'encoding' differs from active codepage: convert menu name
2452 * and use wide function */
2453 wn = enc_to_utf16(item_text, NULL);
2454 if (wn != NULL)
2455 {
2456 MENUITEMINFOW infow;
2457
2458 infow.cbSize = sizeof(infow);
2459 infow.fMask = MIIM_TYPE | MIIM_ID;
2460 infow.wID = item_id;
2461 infow.fType = MFT_STRING;
2462 infow.dwTypeData = wn;
2463 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002464 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002465 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002466 }
2467 }
2468
2469 if (wn == NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002470 {
2471 MENUITEMINFO info;
2472
2473 info.cbSize = sizeof(info);
2474 info.fMask = MIIM_TYPE | MIIM_ID;
2475 info.wID = item_id;
2476 info.fType = MFT_STRING;
2477 info.dwTypeData = (LPTSTR)item_text;
2478 info.cch = (UINT)STRLEN(item_text);
2479 InsertMenuItem(pmenu, item_id, FALSE, &info);
2480 }
2481}
2482
2483 static void
2484show_tabline_popup_menu(void)
2485{
2486 HMENU tab_pmenu;
2487 long rval;
2488 POINT pt;
2489
2490 /* When ignoring events don't show the menu. */
2491 if (hold_gui_events
2492# ifdef FEAT_CMDWIN
2493 || cmdwin_type != 0
2494# endif
2495 )
2496 return;
2497
2498 tab_pmenu = CreatePopupMenu();
2499 if (tab_pmenu == NULL)
2500 return;
2501
2502 if (first_tabpage->tp_next != NULL)
2503 add_tabline_popup_menu_entry(tab_pmenu,
2504 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2505 add_tabline_popup_menu_entry(tab_pmenu,
2506 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2507 add_tabline_popup_menu_entry(tab_pmenu,
2508 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2509
2510 GetCursorPos(&pt);
2511 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2512 NULL);
2513
2514 DestroyMenu(tab_pmenu);
2515
2516 /* Add the string cmd into input buffer */
2517 if (rval > 0)
2518 {
2519 TCHITTESTINFO htinfo;
2520 int idx;
2521
2522 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2523 return;
2524
2525 htinfo.pt.x = pt.x;
2526 htinfo.pt.y = pt.y;
2527 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2528 if (idx == -1)
2529 idx = 0;
2530 else
2531 idx += 1;
2532
2533 send_tabline_menu_event(idx, (int)rval);
2534 }
2535}
2536
2537/*
2538 * Show or hide the tabline.
2539 */
2540 void
2541gui_mch_show_tabline(int showit)
2542{
2543 if (s_tabhwnd == NULL)
2544 return;
2545
2546 if (!showit != !showing_tabline)
2547 {
2548 if (showit)
2549 ShowWindow(s_tabhwnd, SW_SHOW);
2550 else
2551 ShowWindow(s_tabhwnd, SW_HIDE);
2552 showing_tabline = showit;
2553 }
2554}
2555
2556/*
2557 * Return TRUE when tabline is displayed.
2558 */
2559 int
2560gui_mch_showing_tabline(void)
2561{
2562 return s_tabhwnd != NULL && showing_tabline;
2563}
2564
2565/*
2566 * Update the labels of the tabline.
2567 */
2568 void
2569gui_mch_update_tabline(void)
2570{
2571 tabpage_T *tp;
2572 TCITEM tie;
2573 int nr = 0;
2574 int curtabidx = 0;
2575 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576 static int use_unicode = FALSE;
2577 int uu;
2578 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002579
2580 if (s_tabhwnd == NULL)
2581 return;
2582
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002583#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002585# define CCM_SETUNICODEFORMAT 0x2005
2586#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2588 if (uu != use_unicode)
2589 {
2590 /* Enable/disable unicode support */
2591 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2592 use_unicode = uu;
2593 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594
2595 tie.mask = TCIF_TEXT;
2596 tie.iImage = -1;
2597
2598 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2599 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2600
2601 /* Add a label for each tab page. They all contain the same text area. */
2602 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2603 {
2604 if (tp == curtab)
2605 curtabidx = nr;
2606
2607 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2608 {
2609 /* Add the tab */
2610 tie.pszText = "-Empty-";
2611 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2612 tabadded = 1;
2613 }
2614
2615 get_tabline_label(tp, FALSE);
2616 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 wstr = NULL;
2618 if (use_unicode)
2619 {
2620 /* Need to go through Unicode. */
2621 wstr = enc_to_utf16(NameBuff, NULL);
2622 if (wstr != NULL)
2623 {
2624 TCITEMW tiw;
2625
2626 tiw.mask = TCIF_TEXT;
2627 tiw.iImage = -1;
2628 tiw.pszText = wstr;
2629 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2630 vim_free(wstr);
2631 }
2632 }
2633 if (wstr == NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002634 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002635 }
2636
2637 /* Remove any old labels. */
2638 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2639 TabCtrl_DeleteItem(s_tabhwnd, nr);
2640
2641 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2642 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2643
2644 /* Re-enable redraw and redraw. */
2645 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2646 RedrawWindow(s_tabhwnd, NULL, NULL,
2647 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2648
2649 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2650 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2651}
2652
2653/*
2654 * Set the current tab to "nr". First tab is 1.
2655 */
2656 void
2657gui_mch_set_curtab(int nr)
2658{
2659 if (s_tabhwnd == NULL)
2660 return;
2661
2662 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2663 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2664}
2665
2666#endif
2667
2668/*
2669 * ":simalt" command.
2670 */
2671 void
2672ex_simalt(exarg_T *eap)
2673{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002674 char_u *keys = eap->arg;
2675 int fill_typebuf = FALSE;
2676 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002677
2678 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2679 while (*keys)
2680 {
2681 if (*keys == '~')
2682 *keys = ' '; /* for showing system menu */
2683 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2684 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002685 fill_typebuf = TRUE;
2686 }
2687 if (fill_typebuf)
2688 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002689 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002690 * processed. */
2691 key_name[0] = K_SPECIAL;
2692 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002693 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002694 key_name[3] = NUL;
2695 typebuf_was_filled = TRUE;
2696 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697 }
2698}
2699
2700/*
2701 * Create the find & replace dialogs.
2702 * You can't have both at once: ":find" when replace is showing, destroys
2703 * the replace dialog first, and the other way around.
2704 */
2705#ifdef MSWIN_FIND_REPLACE
2706 static void
2707initialise_findrep(char_u *initial_string)
2708{
2709 int wword = FALSE;
2710 int mcase = !p_ic;
2711 char_u *entry_text;
2712
2713 /* Get the search string to use. */
2714 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2715
2716 s_findrep_struct.hwndOwner = s_hwnd;
2717 s_findrep_struct.Flags = FR_DOWN;
2718 if (mcase)
2719 s_findrep_struct.Flags |= FR_MATCHCASE;
2720 if (wword)
2721 s_findrep_struct.Flags |= FR_WHOLEWORD;
2722 if (entry_text != NULL && *entry_text != NUL)
2723 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2724 s_findrep_struct.wFindWhatLen - 1);
2725 vim_free(entry_text);
2726}
2727#endif
2728
2729 static void
2730set_window_title(HWND hwnd, char *title)
2731{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2733 {
2734 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002735
2736 /* Convert the title from 'encoding' to UTF-16. */
2737 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2738 if (wbuf != NULL)
2739 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002740 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002741 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002743 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 (void)SetWindowText(hwnd, (LPCSTR)title);
2746}
2747
2748 void
2749gui_mch_find_dialog(exarg_T *eap)
2750{
2751#ifdef MSWIN_FIND_REPLACE
2752 if (s_findrep_msg != 0)
2753 {
2754 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2755 DestroyWindow(s_findrep_hwnd);
2756
2757 if (!IsWindow(s_findrep_hwnd))
2758 {
2759 initialise_findrep(eap->arg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002760 /* If the OS is Windows NT, and 'encoding' differs from active
2761 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002762 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002763 {
2764 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2765 s_findrep_hwnd = FindTextW(
2766 (LPFINDREPLACEW) &s_findrep_struct_w);
2767 }
2768 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002769 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2770 }
2771
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002772 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 (void)SetFocus(s_findrep_hwnd);
2774
2775 s_findrep_is_find = TRUE;
2776 }
2777#endif
2778}
2779
2780
2781 void
2782gui_mch_replace_dialog(exarg_T *eap)
2783{
2784#ifdef MSWIN_FIND_REPLACE
2785 if (s_findrep_msg != 0)
2786 {
2787 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2788 DestroyWindow(s_findrep_hwnd);
2789
2790 if (!IsWindow(s_findrep_hwnd))
2791 {
2792 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002793 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002794 {
2795 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2796 s_findrep_hwnd = ReplaceTextW(
2797 (LPFINDREPLACEW) &s_findrep_struct_w);
2798 }
2799 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002800 s_findrep_hwnd = ReplaceText(
2801 (LPFINDREPLACE) &s_findrep_struct);
2802 }
2803
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002804 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 (void)SetFocus(s_findrep_hwnd);
2806
2807 s_findrep_is_find = FALSE;
2808 }
2809#endif
2810}
2811
2812
2813/*
2814 * Set visibility of the pointer.
2815 */
2816 void
2817gui_mch_mousehide(int hide)
2818{
2819 if (hide != gui.pointer_hidden)
2820 {
2821 ShowCursor(!hide);
2822 gui.pointer_hidden = hide;
2823 }
2824}
2825
2826#ifdef FEAT_MENU
2827 static void
2828gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2829{
2830 /* Unhide the mouse, we don't get move events here. */
2831 gui_mch_mousehide(FALSE);
2832
2833 (void)TrackPopupMenu(
2834 (HMENU)menu->submenu_id,
2835 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2836 x, y,
2837 (int)0, /*reserved param*/
2838 s_hwnd,
2839 NULL);
2840 /*
2841 * NOTE: The pop-up menu can eat the mouse up event.
2842 * We deal with this in normal.c.
2843 */
2844}
2845#endif
2846
2847/*
2848 * Got a message when the system will go down.
2849 */
2850 static void
2851_OnEndSession(void)
2852{
2853 getout_preserve_modified(1);
2854}
2855
2856/*
2857 * Get this message when the user clicks on the cross in the top right corner
2858 * of a Windows95 window.
2859 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002860 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002861_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862{
2863 gui_shell_closed();
2864}
2865
2866/*
2867 * Get a message when the window is being destroyed.
2868 */
2869 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002870_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871{
2872 if (!destroying)
2873 _OnClose(hwnd);
2874}
2875
2876 static void
2877_OnPaint(
2878 HWND hwnd)
2879{
2880 if (!IsMinimized(hwnd))
2881 {
2882 PAINTSTRUCT ps;
2883
2884 out_flush(); /* make sure all output has been processed */
2885 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002886
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002887 /* prevent multi-byte characters from misprinting on an invalid
2888 * rectangle */
2889 if (has_mbyte)
2890 {
2891 RECT rect;
2892
2893 GetClientRect(hwnd, &rect);
2894 ps.rcPaint.left = rect.left;
2895 ps.rcPaint.right = rect.right;
2896 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897
2898 if (!IsRectEmpty(&ps.rcPaint))
2899 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2901 ps.rcPaint.right - ps.rcPaint.left + 1,
2902 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2903 }
2904
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905 EndPaint(hwnd, &ps);
2906 }
2907}
2908
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909 static void
2910_OnSize(
2911 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002912 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913 int cx,
2914 int cy)
2915{
2916 if (!IsMinimized(hwnd))
2917 {
2918 gui_resize_shell(cx, cy);
2919
2920#ifdef FEAT_MENU
2921 /* Menu bar may wrap differently now */
2922 gui_mswin_get_menu_height(TRUE);
2923#endif
2924 }
2925}
2926
2927 static void
2928_OnSetFocus(
2929 HWND hwnd,
2930 HWND hwndOldFocus)
2931{
2932 gui_focus_change(TRUE);
2933 s_getting_focus = TRUE;
2934 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2935}
2936
2937 static void
2938_OnKillFocus(
2939 HWND hwnd,
2940 HWND hwndNewFocus)
2941{
2942 gui_focus_change(FALSE);
2943 s_getting_focus = FALSE;
2944 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2945}
2946
2947/*
2948 * Get a message when the user switches back to vim
2949 */
2950 static LRESULT
2951_OnActivateApp(
2952 HWND hwnd,
2953 BOOL fActivate,
2954 DWORD dwThreadId)
2955{
2956 /* we call gui_focus_change() in _OnSetFocus() */
2957 /* gui_focus_change((int)fActivate); */
2958 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2959}
2960
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961 void
2962gui_mch_destroy_scrollbar(scrollbar_T *sb)
2963{
2964 DestroyWindow(sb->id);
2965}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966
2967/*
2968 * Get current mouse coordinates in text window.
2969 */
2970 void
2971gui_mch_getmouse(int *x, int *y)
2972{
2973 RECT rct;
2974 POINT mp;
2975
2976 (void)GetWindowRect(s_textArea, &rct);
2977 (void)GetCursorPos((LPPOINT)&mp);
2978 *x = (int)(mp.x - rct.left);
2979 *y = (int)(mp.y - rct.top);
2980}
2981
2982/*
2983 * Move mouse pointer to character at (x, y).
2984 */
2985 void
2986gui_mch_setmouse(int x, int y)
2987{
2988 RECT rct;
2989
2990 (void)GetWindowRect(s_textArea, &rct);
2991 (void)SetCursorPos(x + gui.border_offset + rct.left,
2992 y + gui.border_offset + rct.top);
2993}
2994
2995 static void
2996gui_mswin_get_valid_dimensions(
2997 int w,
2998 int h,
2999 int *valid_w,
3000 int *valid_h)
3001{
3002 int base_width, base_height;
3003
3004 base_width = gui_get_base_width()
3005 + (GetSystemMetrics(SM_CXFRAME) +
3006 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3007 base_height = gui_get_base_height()
3008 + (GetSystemMetrics(SM_CYFRAME) +
3009 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3010 + GetSystemMetrics(SM_CYCAPTION)
3011#ifdef FEAT_MENU
3012 + gui_mswin_get_menu_height(FALSE)
3013#endif
3014 ;
3015 *valid_w = base_width +
3016 ((w - base_width) / gui.char_width) * gui.char_width;
3017 *valid_h = base_height +
3018 ((h - base_height) / gui.char_height) * gui.char_height;
3019}
3020
3021 void
3022gui_mch_flash(int msec)
3023{
3024 RECT rc;
3025
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003026#if defined(FEAT_DIRECTX)
3027 if (IS_ENABLE_DIRECTX())
3028 DWriteContext_Flush(s_dwc);
3029#endif
3030
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003031 /*
3032 * Note: InvertRect() excludes right and bottom of rectangle.
3033 */
3034 rc.left = 0;
3035 rc.top = 0;
3036 rc.right = gui.num_cols * gui.char_width;
3037 rc.bottom = gui.num_rows * gui.char_height;
3038 InvertRect(s_hdc, &rc);
3039 gui_mch_flush(); /* make sure it's displayed */
3040
3041 ui_delay((long)msec, TRUE); /* wait for a few msec */
3042
3043 InvertRect(s_hdc, &rc);
3044}
3045
3046/*
3047 * Return flags used for scrolling.
3048 * The SW_INVALIDATE is required when part of the window is covered or
3049 * off-screen. Refer to MS KB Q75236.
3050 */
3051 static int
3052get_scroll_flags(void)
3053{
3054 HWND hwnd;
3055 RECT rcVim, rcOther, rcDest;
3056
3057 GetWindowRect(s_hwnd, &rcVim);
3058
3059 /* Check if the window is partly above or below the screen. We don't care
3060 * about partly left or right of the screen, it is not relevant when
3061 * scrolling up or down. */
3062 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3063 return SW_INVALIDATE;
3064
3065 /* Check if there is an window (partly) on top of us. */
3066 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3067 if (IsWindowVisible(hwnd))
3068 {
3069 GetWindowRect(hwnd, &rcOther);
3070 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3071 return SW_INVALIDATE;
3072 }
3073 return 0;
3074}
3075
3076/*
3077 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3078 * may not be scrolled out properly.
3079 * For gVim, when _OnScroll() is repeated, the character at the
3080 * previous cursor position may be left drawn after scroll.
3081 * The problem can be avoided by calling GetPixel() to get a pixel in
3082 * the region before ScrollWindowEx().
3083 */
3084 static void
3085intel_gpu_workaround(void)
3086{
3087 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3088}
3089
3090/*
3091 * Delete the given number of lines from the given row, scrolling up any
3092 * text further down within the scroll region.
3093 */
3094 void
3095gui_mch_delete_lines(
3096 int row,
3097 int num_lines)
3098{
3099 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003100
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003101 rc.left = FILL_X(gui.scroll_region_left);
3102 rc.right = FILL_X(gui.scroll_region_right + 1);
3103 rc.top = FILL_Y(row);
3104 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3105
Bram Moolenaar92467d32017-12-05 13:22:16 +01003106#if defined(FEAT_DIRECTX)
3107 if (IS_ENABLE_DIRECTX())
3108 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003109 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3110 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003111 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003112 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003113#endif
3114 {
3115 intel_gpu_workaround();
3116 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003117 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003118 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003119 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003121 /* This seems to be required to avoid the cursor disappearing when
3122 * scrolling such that the cursor ends up in the top-left character on
3123 * the screen... But why? (Webb) */
3124 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3125 /* gui.cursor_is_valid = FALSE; */
3126
3127 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3128 gui.scroll_region_left,
3129 gui.scroll_region_bot, gui.scroll_region_right);
3130}
3131
3132/*
3133 * Insert the given number of lines before the given row, scrolling down any
3134 * following text within the scroll region.
3135 */
3136 void
3137gui_mch_insert_lines(
3138 int row,
3139 int num_lines)
3140{
3141 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003142
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003143 rc.left = FILL_X(gui.scroll_region_left);
3144 rc.right = FILL_X(gui.scroll_region_right + 1);
3145 rc.top = FILL_Y(row);
3146 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003147
3148#if defined(FEAT_DIRECTX)
3149 if (IS_ENABLE_DIRECTX())
3150 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003151 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3152 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003153 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003154 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003155#endif
3156 {
3157 intel_gpu_workaround();
3158 /* The SW_INVALIDATE is required when part of the window is covered or
3159 * off-screen. How do we avoid it when it's not needed? */
3160 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003162 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003164
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003165 gui_clear_block(row, gui.scroll_region_left,
3166 row + num_lines - 1, gui.scroll_region_right);
3167}
3168
3169
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003171gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172{
3173#if defined(FEAT_DIRECTX)
3174 DWriteContext_Close(s_dwc);
3175 DWrite_Final();
3176 s_dwc = NULL;
3177#endif
3178
3179 ReleaseDC(s_textArea, s_hdc);
3180 DeleteObject(s_brush);
3181
3182#ifdef FEAT_TEAROFF
3183 /* Unload the tearoff bitmap */
3184 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3185#endif
3186
3187 /* Destroy our window (if we have one). */
3188 if (s_hwnd != NULL)
3189 {
3190 destroying = TRUE; /* ignore WM_DESTROY message now */
3191 DestroyWindow(s_hwnd);
3192 }
3193
3194#ifdef GLOBAL_IME
3195 global_ime_end();
3196#endif
3197}
3198
3199 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003200logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201{
3202 char *p;
3203 char *res;
3204 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003205 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003206 char *font_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003207
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003208 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3209 if (font_name == NULL)
3210 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003212 quality_name = quality_id2name((int)lf.lfQuality);
3213
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3215 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3216 if (res != NULL)
3217 {
3218 p = res;
3219 /* make a normal font string out of the lf thing:*/
3220 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3221 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3222 while (*p)
3223 {
3224 if (*p == ' ')
3225 *p = '_';
3226 ++p;
3227 }
3228 if (lf.lfItalic)
3229 STRCAT(p, ":i");
3230 if (lf.lfWeight >= FW_BOLD)
3231 STRCAT(p, ":b");
3232 if (lf.lfUnderline)
3233 STRCAT(p, ":u");
3234 if (lf.lfStrikeOut)
3235 STRCAT(p, ":s");
3236 if (charset_name != NULL)
3237 {
3238 STRCAT(p, ":c");
3239 STRCAT(p, charset_name);
3240 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003241 if (quality_name != NULL)
3242 {
3243 STRCAT(p, ":q");
3244 STRCAT(p, quality_name);
3245 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003246 }
3247
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003248 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003249 return (char_u *)res;
3250}
3251
3252
3253#ifdef FEAT_MBYTE_IME
3254/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003255 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256 * 'guifont'
3257 */
3258 static void
3259update_im_font(void)
3260{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003261 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
3263 if (p_guifontwide != NULL && *p_guifontwide != NUL
3264 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003265 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 norm_logfont = lf_wide;
3267 else
3268 norm_logfont = sub_logfont;
3269 im_set_font(&norm_logfont);
3270}
3271#endif
3272
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273/*
3274 * Handler of gui.wide_font (p_guifontwide) changed notification.
3275 */
3276 void
3277gui_mch_wide_font_changed(void)
3278{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003279 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003280
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003281#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003283#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284
3285 gui_mch_free_font(gui.wide_ital_font);
3286 gui.wide_ital_font = NOFONT;
3287 gui_mch_free_font(gui.wide_bold_font);
3288 gui.wide_bold_font = NOFONT;
3289 gui_mch_free_font(gui.wide_boldital_font);
3290 gui.wide_boldital_font = NOFONT;
3291
3292 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003293 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003294 {
3295 if (!lf.lfItalic)
3296 {
3297 lf.lfItalic = TRUE;
3298 gui.wide_ital_font = get_font_handle(&lf);
3299 lf.lfItalic = FALSE;
3300 }
3301 if (lf.lfWeight < FW_BOLD)
3302 {
3303 lf.lfWeight = FW_BOLD;
3304 gui.wide_bold_font = get_font_handle(&lf);
3305 if (!lf.lfItalic)
3306 {
3307 lf.lfItalic = TRUE;
3308 gui.wide_boldital_font = get_font_handle(&lf);
3309 }
3310 }
3311 }
3312}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003313
3314/*
3315 * Initialise vim to use the font with the given name.
3316 * Return FAIL if the font could not be loaded, OK otherwise.
3317 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003319gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003320{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003321 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003322 GuiFont font = NOFONT;
3323 char_u *p;
3324
3325 /* Load the font */
3326 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3327 font = get_font_handle(&lf);
3328 if (font == NOFONT)
3329 return FAIL;
3330
3331 if (font_name == NULL)
3332 font_name = (char_u *)lf.lfFaceName;
3333#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3334 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003335#endif
3336#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 sub_logfont = lf;
3338#endif
3339#ifdef FEAT_MBYTE_IME
3340 update_im_font();
3341#endif
3342 gui_mch_free_font(gui.norm_font);
3343 gui.norm_font = font;
3344 current_font_height = lf.lfHeight;
3345 GetFontSize(font);
3346
3347 p = logfont2name(lf);
3348 if (p != NULL)
3349 {
3350 hl_set_font_name(p);
3351
3352 /* When setting 'guifont' to "*" replace it with the actual font name.
3353 * */
3354 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3355 {
3356 vim_free(p_guifont);
3357 p_guifont = p;
3358 }
3359 else
3360 vim_free(p);
3361 }
3362
3363 gui_mch_free_font(gui.ital_font);
3364 gui.ital_font = NOFONT;
3365 gui_mch_free_font(gui.bold_font);
3366 gui.bold_font = NOFONT;
3367 gui_mch_free_font(gui.boldital_font);
3368 gui.boldital_font = NOFONT;
3369
3370 if (!lf.lfItalic)
3371 {
3372 lf.lfItalic = TRUE;
3373 gui.ital_font = get_font_handle(&lf);
3374 lf.lfItalic = FALSE;
3375 }
3376 if (lf.lfWeight < FW_BOLD)
3377 {
3378 lf.lfWeight = FW_BOLD;
3379 gui.bold_font = get_font_handle(&lf);
3380 if (!lf.lfItalic)
3381 {
3382 lf.lfItalic = TRUE;
3383 gui.boldital_font = get_font_handle(&lf);
3384 }
3385 }
3386
3387 return OK;
3388}
3389
3390#ifndef WPF_RESTORETOMAXIMIZED
3391# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3392#endif
3393
3394/*
3395 * Return TRUE if the GUI window is maximized, filling the whole screen.
3396 */
3397 int
3398gui_mch_maximized(void)
3399{
3400 WINDOWPLACEMENT wp;
3401
3402 wp.length = sizeof(WINDOWPLACEMENT);
3403 if (GetWindowPlacement(s_hwnd, &wp))
3404 return wp.showCmd == SW_SHOWMAXIMIZED
3405 || (wp.showCmd == SW_SHOWMINIMIZED
3406 && wp.flags == WPF_RESTORETOMAXIMIZED);
3407
3408 return 0;
3409}
3410
3411/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003412 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3413 * is set. Compute the new Rows and Columns. This is like resizing the
3414 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 */
3416 void
3417gui_mch_newfont(void)
3418{
3419 RECT rect;
3420
3421 GetWindowRect(s_hwnd, &rect);
3422 if (win_socket_id == 0)
3423 {
3424 gui_resize_shell(rect.right - rect.left
3425 - (GetSystemMetrics(SM_CXFRAME) +
3426 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3427 rect.bottom - rect.top
3428 - (GetSystemMetrics(SM_CYFRAME) +
3429 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3430 - GetSystemMetrics(SM_CYCAPTION)
3431#ifdef FEAT_MENU
3432 - gui_mswin_get_menu_height(FALSE)
3433#endif
3434 );
3435 }
3436 else
3437 {
3438 /* Inside another window, don't use the frame and border. */
3439 gui_resize_shell(rect.right - rect.left,
3440 rect.bottom - rect.top
3441#ifdef FEAT_MENU
3442 - gui_mswin_get_menu_height(FALSE)
3443#endif
3444 );
3445 }
3446}
3447
3448/*
3449 * Set the window title
3450 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 void
3452gui_mch_settitle(
3453 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003454 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455{
3456 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3457}
3458
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003459#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3461 * misc2.c! */
3462static LPCSTR mshape_idcs[] =
3463{
3464 IDC_ARROW, /* arrow */
3465 MAKEINTRESOURCE(0), /* blank */
3466 IDC_IBEAM, /* beam */
3467 IDC_SIZENS, /* updown */
3468 IDC_SIZENS, /* udsizing */
3469 IDC_SIZEWE, /* leftright */
3470 IDC_SIZEWE, /* lrsizing */
3471 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 IDC_ARROW, /* crosshair */
3474 IDC_ARROW, /* hand1 */
3475 IDC_ARROW, /* hand2 */
3476 IDC_ARROW, /* pencil */
3477 IDC_ARROW, /* question */
3478 IDC_ARROW, /* right-arrow */
3479 IDC_UPARROW, /* up-arrow */
3480 IDC_ARROW /* last one */
3481};
3482
3483 void
3484mch_set_mouse_shape(int shape)
3485{
3486 LPCSTR idc;
3487
3488 if (shape == MSHAPE_HIDE)
3489 ShowCursor(FALSE);
3490 else
3491 {
3492 if (shape >= MSHAPE_NUMBERED)
3493 idc = IDC_ARROW;
3494 else
3495 idc = mshape_idcs[shape];
3496#ifdef SetClassLongPtr
3497 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3498#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003499 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500#endif
3501 if (!p_mh)
3502 {
3503 POINT mp;
3504
3505 /* Set the position to make it redrawn with the new shape. */
3506 (void)GetCursorPos((LPPOINT)&mp);
3507 (void)SetCursorPos(mp.x, mp.y);
3508 ShowCursor(TRUE);
3509 }
3510 }
3511}
3512#endif
3513
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003514#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003515/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516 * Wide version of convert_filter().
3517 */
3518 static WCHAR *
3519convert_filterW(char_u *s)
3520{
3521 char_u *tmp;
3522 int len;
3523 WCHAR *res;
3524
3525 tmp = convert_filter(s);
3526 if (tmp == NULL)
3527 return NULL;
3528 len = (int)STRLEN(s) + 3;
3529 res = enc_to_utf16(tmp, &len);
3530 vim_free(tmp);
3531 return res;
3532}
3533
3534/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003535 * Pop open a file browser and return the file selected, in allocated memory,
3536 * or NULL if Cancel is hit.
3537 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3538 * title - Title message for the file browser dialog.
3539 * dflt - Default name of file.
3540 * ext - Default extension to be added to files without extensions.
3541 * initdir - directory in which to open the browser (NULL = current dir)
3542 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003544 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003545gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546 int saving,
3547 char_u *title,
3548 char_u *dflt,
3549 char_u *ext,
3550 char_u *initdir,
3551 char_u *filter)
3552{
3553 /* We always use the wide function. This means enc_to_utf16() must work,
3554 * otherwise it fails miserably! */
3555 OPENFILENAMEW fileStruct;
3556 WCHAR fileBuf[MAXPATHL];
3557 WCHAR *wp;
3558 int i;
3559 WCHAR *titlep = NULL;
3560 WCHAR *extp = NULL;
3561 WCHAR *initdirp = NULL;
3562 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003563 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564
3565 if (dflt == NULL)
3566 fileBuf[0] = NUL;
3567 else
3568 {
3569 wp = enc_to_utf16(dflt, NULL);
3570 if (wp == NULL)
3571 fileBuf[0] = NUL;
3572 else
3573 {
3574 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3575 fileBuf[i] = wp[i];
3576 fileBuf[i] = NUL;
3577 vim_free(wp);
3578 }
3579 }
3580
3581 /* Convert the filter to Windows format. */
3582 filterp = convert_filterW(filter);
3583
3584 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003585# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003586 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003587 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003588# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003590# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591
3592 if (title != NULL)
3593 titlep = enc_to_utf16(title, NULL);
3594 fileStruct.lpstrTitle = titlep;
3595
3596 if (ext != NULL)
3597 extp = enc_to_utf16(ext, NULL);
3598 fileStruct.lpstrDefExt = extp;
3599
3600 fileStruct.lpstrFile = fileBuf;
3601 fileStruct.nMaxFile = MAXPATHL;
3602 fileStruct.lpstrFilter = filterp;
3603 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3604 /* has an initial dir been specified? */
3605 if (initdir != NULL && *initdir != NUL)
3606 {
3607 /* Must have backslashes here, no matter what 'shellslash' says */
3608 initdirp = enc_to_utf16(initdir, NULL);
3609 if (initdirp != NULL)
3610 {
3611 for (wp = initdirp; *wp != NUL; ++wp)
3612 if (*wp == '/')
3613 *wp = '\\';
3614 }
3615 fileStruct.lpstrInitialDir = initdirp;
3616 }
3617
3618 /*
3619 * TODO: Allow selection of multiple files. Needs another arg to this
3620 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3621 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3622 * files that don't exist yet, so I haven't put it in. What about
3623 * OFN_PATHMUSTEXIST?
3624 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3625 */
3626 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003627# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 if (curbuf->b_p_bin)
3629 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003630# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 if (saving)
3632 {
3633 if (!GetSaveFileNameW(&fileStruct))
3634 return NULL;
3635 }
3636 else
3637 {
3638 if (!GetOpenFileNameW(&fileStruct))
3639 return NULL;
3640 }
3641
3642 vim_free(filterp);
3643 vim_free(initdirp);
3644 vim_free(titlep);
3645 vim_free(extp);
3646
3647 /* Convert from UCS2 to 'encoding'. */
3648 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003649 if (p == NULL)
3650 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003651
3652 /* Give focus back to main window (when using MDI). */
3653 SetFocus(s_hwnd);
3654
3655 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003656 q = vim_strsave(shorten_fname1(p));
3657 vim_free(p);
3658 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660
3661
3662/*
3663 * Convert the string s to the proper format for a filter string by replacing
3664 * the \t and \n delimiters with \0.
3665 * Returns the converted string in allocated memory.
3666 *
3667 * Keep in sync with convert_filterW() above!
3668 */
3669 static char_u *
3670convert_filter(char_u *s)
3671{
3672 char_u *res;
3673 unsigned s_len = (unsigned)STRLEN(s);
3674 unsigned i;
3675
3676 res = alloc(s_len + 3);
3677 if (res != NULL)
3678 {
3679 for (i = 0; i < s_len; ++i)
3680 if (s[i] == '\t' || s[i] == '\n')
3681 res[i] = '\0';
3682 else
3683 res[i] = s[i];
3684 res[s_len] = NUL;
3685 /* Add two extra NULs to make sure it's properly terminated. */
3686 res[s_len + 1] = NUL;
3687 res[s_len + 2] = NUL;
3688 }
3689 return res;
3690}
3691
3692/*
3693 * Select a directory.
3694 */
3695 char_u *
3696gui_mch_browsedir(char_u *title, char_u *initdir)
3697{
3698 /* We fake this: Use a filter that doesn't select anything and a default
3699 * file name that won't be used. */
3700 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3701 initdir, (char_u *)_("Directory\t*.nothing\n"));
3702}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003703#endif /* FEAT_BROWSE */
3704
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003705 static void
3706_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003707 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003708 HDROP hDrop)
3709{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003710#define BUFPATHLEN _MAX_PATH
3711#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003713 char szFile[BUFPATHLEN];
3714 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3715 UINT i;
3716 char_u **fnames;
3717 POINT pt;
3718 int_u modifiers = 0;
3719
3720 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3721
3722 /* Obtain dropped position */
3723 DragQueryPoint(hDrop, &pt);
3724 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3725
3726 reset_VIsual();
3727
3728 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3729
3730 if (fnames != NULL)
3731 for (i = 0; i < cFiles; ++i)
3732 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3734 fnames[i] = utf16_to_enc(wszFile, NULL);
3735 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003736 {
3737 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3738 fnames[i] = vim_strsave((char_u *)szFile);
3739 }
3740 }
3741
3742 DragFinish(hDrop);
3743
3744 if (fnames != NULL)
3745 {
3746 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3747 modifiers |= MOUSE_SHIFT;
3748 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3749 modifiers |= MOUSE_CTRL;
3750 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3751 modifiers |= MOUSE_ALT;
3752
3753 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3754
3755 s_need_activate = TRUE;
3756 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003757}
3758
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759 static int
3760_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003761 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762 HWND hwndCtl,
3763 UINT code,
3764 int pos)
3765{
3766 static UINT prev_code = 0; /* code of previous call */
3767 scrollbar_T *sb, *sb_info;
3768 long val;
3769 int dragging = FALSE;
3770 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 SCROLLINFO si;
3772
3773 si.cbSize = sizeof(si);
3774 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775
3776 sb = gui_mswin_find_scrollbar(hwndCtl);
3777 if (sb == NULL)
3778 return 0;
3779
3780 if (sb->wp != NULL) /* Left or right scrollbar */
3781 {
3782 /*
3783 * Careful: need to get scrollbar info out of first (left) scrollbar
3784 * for window, but keep real scrollbar too because we must pass it to
3785 * gui_drag_scrollbar().
3786 */
3787 sb_info = &sb->wp->w_scrollbars[0];
3788 }
3789 else /* Bottom scrollbar */
3790 sb_info = sb;
3791 val = sb_info->value;
3792
3793 switch (code)
3794 {
3795 case SB_THUMBTRACK:
3796 val = pos;
3797 dragging = TRUE;
3798 if (sb->scroll_shift > 0)
3799 val <<= sb->scroll_shift;
3800 break;
3801 case SB_LINEDOWN:
3802 val++;
3803 break;
3804 case SB_LINEUP:
3805 val--;
3806 break;
3807 case SB_PAGEDOWN:
3808 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3809 break;
3810 case SB_PAGEUP:
3811 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3812 break;
3813 case SB_TOP:
3814 val = 0;
3815 break;
3816 case SB_BOTTOM:
3817 val = sb_info->max;
3818 break;
3819 case SB_ENDSCROLL:
3820 if (prev_code == SB_THUMBTRACK)
3821 {
3822 /*
3823 * "pos" only gives us 16-bit data. In case of large file,
3824 * use GetScrollPos() which returns 32-bit. Unfortunately it
3825 * is not valid while the scrollbar is being dragged.
3826 */
3827 val = GetScrollPos(hwndCtl, SB_CTL);
3828 if (sb->scroll_shift > 0)
3829 val <<= sb->scroll_shift;
3830 }
3831 break;
3832
3833 default:
3834 /* TRACE("Unknown scrollbar event %d\n", code); */
3835 return 0;
3836 }
3837 prev_code = code;
3838
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3840 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841
3842 /*
3843 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3844 */
3845 if (sb->wp != NULL)
3846 {
3847 scrollbar_T *sba = sb->wp->w_scrollbars;
3848 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 }
3852
3853 /* Don't let us be interrupted here by another message. */
3854 s_busy_processing = TRUE;
3855
3856 /* When "allow_scrollbar" is FALSE still need to remember the new
3857 * position, but don't actually scroll by setting "dont_scroll". */
3858 dont_scroll = !allow_scrollbar;
3859
Bram Moolenaara338adc2018-01-31 20:51:47 +01003860 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003862 mch_enable_flush();
3863 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864
3865 s_busy_processing = FALSE;
3866 dont_scroll = dont_scroll_save;
3867
3868 return 0;
3869}
3870
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#ifdef FEAT_XPM_W32
3873# include "xpm_w32.h"
3874#endif
3875
3876#ifdef PROTO
3877# define WINAPI
3878#endif
3879
3880#ifdef __MINGW32__
3881/*
3882 * Add a lot of missing defines.
3883 * They are not always missing, we need the #ifndef's.
3884 */
3885# ifndef _cdecl
3886# define _cdecl
3887# endif
3888# ifndef IsMinimized
3889# define IsMinimized(hwnd) IsIconic(hwnd)
3890# endif
3891# ifndef IsMaximized
3892# define IsMaximized(hwnd) IsZoomed(hwnd)
3893# endif
3894# ifndef SelectFont
3895# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3896# endif
3897# ifndef GetStockBrush
3898# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3899# endif
3900# ifndef DeleteBrush
3901# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3902# endif
3903
3904# ifndef HANDLE_WM_RBUTTONDBLCLK
3905# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3906 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3907# endif
3908# ifndef HANDLE_WM_MBUTTONUP
3909# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3910 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3911# endif
3912# ifndef HANDLE_WM_MBUTTONDBLCLK
3913# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3914 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3915# endif
3916# ifndef HANDLE_WM_LBUTTONDBLCLK
3917# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3918 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3919# endif
3920# ifndef HANDLE_WM_RBUTTONDOWN
3921# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3922 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3923# endif
3924# ifndef HANDLE_WM_MOUSEMOVE
3925# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3926 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3927# endif
3928# ifndef HANDLE_WM_RBUTTONUP
3929# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3930 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3931# endif
3932# ifndef HANDLE_WM_MBUTTONDOWN
3933# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3934 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3935# endif
3936# ifndef HANDLE_WM_LBUTTONUP
3937# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3938 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3939# endif
3940# ifndef HANDLE_WM_LBUTTONDOWN
3941# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3942 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3943# endif
3944# ifndef HANDLE_WM_SYSCHAR
3945# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3946 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3947# endif
3948# ifndef HANDLE_WM_ACTIVATEAPP
3949# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3950 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3951# endif
3952# ifndef HANDLE_WM_WINDOWPOSCHANGING
3953# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3954 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3955# endif
3956# ifndef HANDLE_WM_VSCROLL
3957# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3958 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3959# endif
3960# ifndef HANDLE_WM_SETFOCUS
3961# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3962 ((fn)((hwnd), (HWND)(wParam)), 0L)
3963# endif
3964# ifndef HANDLE_WM_KILLFOCUS
3965# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3966 ((fn)((hwnd), (HWND)(wParam)), 0L)
3967# endif
3968# ifndef HANDLE_WM_HSCROLL
3969# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3970 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3971# endif
3972# ifndef HANDLE_WM_DROPFILES
3973# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3974 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3975# endif
3976# ifndef HANDLE_WM_CHAR
3977# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3978 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3979# endif
3980# ifndef HANDLE_WM_SYSDEADCHAR
3981# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3982 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3983# endif
3984# ifndef HANDLE_WM_DEADCHAR
3985# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3986 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3987# endif
3988#endif /* __MINGW32__ */
3989
3990
3991/* Some parameters for tearoff menus. All in pixels. */
3992#define TEAROFF_PADDING_X 2
3993#define TEAROFF_BUTTON_PAD_X 8
3994#define TEAROFF_MIN_WIDTH 200
3995#define TEAROFF_SUBMENU_LABEL ">>"
3996#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3997
3998
3999/* For the Intellimouse: */
4000#ifndef WM_MOUSEWHEEL
4001#define WM_MOUSEWHEEL 0x20a
4002#endif
4003
4004
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004005#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006# define ID_BEVAL_TOOLTIP 200
4007# define BEVAL_TEXT_LEN MAXPATHL
4008
Bram Moolenaar167632f2010-05-26 21:42:54 +02004009#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004010/* Work around old versions of basetsd.h which wrongly declares
4011 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004012# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004013# define UINT_PTR UINT
4014#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004017static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004019
Bram Moolenaar82881492012-11-20 16:53:39 +01004020
4021/* cproto fails on missing include files */
4022#ifndef PROTO
4023
Bram Moolenaar45360022005-07-21 21:08:21 +00004024/*
4025 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004026 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004027 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004028# include <pshpack1.h>
4029
4030#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004031
4032typedef struct _DllVersionInfo
4033{
4034 DWORD cbSize;
4035 DWORD dwMajorVersion;
4036 DWORD dwMinorVersion;
4037 DWORD dwBuildNumber;
4038 DWORD dwPlatformID;
4039} DLLVERSIONINFO;
4040
Bram Moolenaar82881492012-11-20 16:53:39 +01004041#ifndef PROTO
4042# include <poppack.h>
4043#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004044
Bram Moolenaar45360022005-07-21 21:08:21 +00004045typedef struct tagTOOLINFOA_NEW
4046{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004047 UINT cbSize;
4048 UINT uFlags;
4049 HWND hwnd;
4050 UINT_PTR uId;
4051 RECT rect;
4052 HINSTANCE hinst;
4053 LPSTR lpszText;
4054 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004055} TOOLINFO_NEW;
4056
4057typedef struct tagNMTTDISPINFO_NEW
4058{
4059 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004060 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004061 char szText[80];
4062 HINSTANCE hinst;
4063 UINT uFlags;
4064 LPARAM lParam;
4065} NMTTDISPINFO_NEW;
4066
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004067typedef struct tagTOOLINFOW_NEW
4068{
4069 UINT cbSize;
4070 UINT uFlags;
4071 HWND hwnd;
4072 UINT_PTR uId;
4073 RECT rect;
4074 HINSTANCE hinst;
4075 LPWSTR lpszText;
4076 LPARAM lParam;
4077 void *lpReserved;
4078} TOOLINFOW_NEW;
4079
4080typedef struct tagNMTTDISPINFOW_NEW
4081{
4082 NMHDR hdr;
4083 LPWSTR lpszText;
4084 WCHAR szText[80];
4085 HINSTANCE hinst;
4086 UINT uFlags;
4087 LPARAM lParam;
4088} NMTTDISPINFOW_NEW;
4089
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004090
Bram Moolenaar45360022005-07-21 21:08:21 +00004091typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4092#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004093# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#endif
4095
Bram Moolenaar45360022005-07-21 21:08:21 +00004096#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004097# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004098#endif
4099
4100#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004101# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004102#endif
4103
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004104#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004105
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004106#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4107/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4108 * it here if LPNMTTDISPINFO isn't defined.
4109 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4110 * _MSC_VER. */
4111# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4112typedef struct tagNMTTDISPINFOA {
4113 NMHDR hdr;
4114 LPSTR lpszText;
4115 char szText[80];
4116 HINSTANCE hinst;
4117 UINT uFlags;
4118 LPARAM lParam;
4119} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4120# define LPNMTTDISPINFO LPNMTTDISPINFOA
4121
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004122typedef struct tagNMTTDISPINFOW {
4123 NMHDR hdr;
4124 LPWSTR lpszText;
4125 WCHAR szText[80];
4126 HINSTANCE hinst;
4127 UINT uFlags;
4128 LPARAM lParam;
4129} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004130# endif
4131#endif
4132
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004133#ifndef TTN_GETDISPINFOW
4134# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4135#endif
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137/* Local variables: */
4138
4139#ifdef FEAT_MENU
4140static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
4143/*
4144 * Use the system font for dialogs and tear-off menus. Remove this line to
4145 * use DLG_FONT_NAME.
4146 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004147#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149#define VIM_NAME "vim"
4150#define VIM_CLASS "Vim"
4151#define VIM_CLASSW L"Vim"
4152
4153/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4154 * thus there should be room for every dialog. For tearoffs it's made bigger
4155 * when needed. */
4156#define DLG_ALLOC_SIZE 16 * 1024
4157
4158/*
4159 * stuff for dialogs, menus, tearoffs etc.
4160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161static PWORD
4162add_dialog_element(
4163 PWORD p,
4164 DWORD lStyle,
4165 WORD x,
4166 WORD y,
4167 WORD w,
4168 WORD h,
4169 WORD Id,
4170 WORD clss,
4171 const char *caption);
4172static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004173static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004174#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177static void get_dialog_font_metrics(void);
4178
4179static int dialog_default_button = -1;
4180
4181/* Intellimouse support */
4182static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4185#ifdef FEAT_TOOLBAR
4186static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004187static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188static int get_toolbar_bitmap(vimmenu_T *menu);
4189#endif
4190
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004191#ifdef FEAT_GUI_TABLINE
4192static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004193static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004194#endif
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196#ifdef FEAT_MBYTE_IME
4197static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4198static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4199#endif
4200#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4201# ifdef NOIME
4202typedef struct tagCOMPOSITIONFORM {
4203 DWORD dwStyle;
4204 POINT ptCurrentPos;
4205 RECT rcArea;
4206} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4207typedef HANDLE HIMC;
4208# endif
4209
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004210static HINSTANCE hLibImm = NULL;
4211static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4212static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4213static HIMC (WINAPI *pImmGetContext)(HWND);
4214static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4215static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4216static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4217static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004218static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4219static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004220static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4221static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004222static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223static void dyn_imm_load(void);
4224#else
4225# define pImmGetCompositionStringA ImmGetCompositionStringA
4226# define pImmGetCompositionStringW ImmGetCompositionStringW
4227# define pImmGetContext ImmGetContext
4228# define pImmAssociateContext ImmAssociateContext
4229# define pImmReleaseContext ImmReleaseContext
4230# define pImmGetOpenStatus ImmGetOpenStatus
4231# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004232# define pImmGetCompositionFontW ImmGetCompositionFontW
4233# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234# define pImmSetCompositionWindow ImmSetCompositionWindow
4235# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004236# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237#endif
4238
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239#ifdef FEAT_MENU
4240/*
4241 * Figure out how high the menu bar is at the moment.
4242 */
4243 static int
4244gui_mswin_get_menu_height(
4245 int fix_window) /* If TRUE, resize window if menu height changed */
4246{
4247 static int old_menu_height = -1;
4248
4249 RECT rc1, rc2;
4250 int num;
4251 int menu_height;
4252
4253 if (gui.menu_is_active)
4254 num = GetMenuItemCount(s_menuBar);
4255 else
4256 num = 0;
4257
4258 if (num == 0)
4259 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004260 else if (IsMinimized(s_hwnd))
4261 {
4262 /* The height of the menu cannot be determined while the window is
4263 * minimized. Take the previous height if the menu is changed in that
4264 * state, to avoid that Vim's vertical window size accidentally
4265 * increases due to the unaccounted-for menu height. */
4266 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 else
4269 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004270 /*
4271 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4272 * seem to have been set yet, so menu wraps in default window
4273 * width which is very narrow. Instead just return height of a
4274 * single menu item. Will still be wrong when the menu really
4275 * should wrap over more than one line.
4276 */
4277 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4278 if (gui.starting)
4279 menu_height = rc1.bottom - rc1.top + 1;
4280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004282 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4283 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285 }
4286
4287 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004288 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004289 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290
4291 return menu_height;
4292}
4293#endif /*FEAT_MENU*/
4294
4295
4296/*
4297 * Setup for the Intellimouse
4298 */
4299 static void
4300init_mouse_wheel(void)
4301{
4302
4303#ifndef SPI_GETWHEELSCROLLLINES
4304# define SPI_GETWHEELSCROLLLINES 104
4305#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004306#ifndef SPI_SETWHEELSCROLLLINES
4307# define SPI_SETWHEELSCROLLLINES 105
4308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4311#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4312#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4313#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 mouse_scroll_lines = 3; /* reasonable default */
4316
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004317 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4318 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4319 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322
4323/* Intellimouse wheel handler */
4324 static void
4325_OnMouseWheel(
4326 HWND hwnd,
4327 short zDelta)
4328{
4329/* Treat a mouse wheel event as if it were a scroll request */
4330 int i;
4331 int size;
4332 HWND hwndCtl;
4333
4334 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4335 {
4336 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4337 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4338 }
4339 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4340 {
4341 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4342 size = curwin->w_scrollbars[SBAR_LEFT].size;
4343 }
4344 else
4345 return;
4346
4347 size = curwin->w_height;
4348 if (mouse_scroll_lines == 0)
4349 init_mouse_wheel();
4350
Bram Moolenaara338adc2018-01-31 20:51:47 +01004351 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if (mouse_scroll_lines > 0
4353 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4354 {
4355 for (i = mouse_scroll_lines; i > 0; --i)
4356 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4357 }
4358 else
4359 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004360 mch_enable_flush();
4361 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362}
4363
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004364#ifdef USE_SYSMENU_FONT
4365/*
4366 * Get Menu Font.
4367 * Return OK or FAIL.
4368 */
4369 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004370gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004371{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004372 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004373
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004374 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4375 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004376 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004377 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004378 &nm,
4379 0))
4380 return FAIL;
4381 *lf = nm.lfMenuFont;
4382 return OK;
4383}
4384#endif
4385
4386
4387#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4388/*
4389 * Set the GUI tabline font to the system menu font
4390 */
4391 static void
4392set_tabline_font(void)
4393{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004394 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004395 HFONT font;
4396 HWND hwnd;
4397 HDC hdc;
4398 HFONT hfntOld;
4399 TEXTMETRIC tm;
4400
4401 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4402 return;
4403
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004404 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004405
4406 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4407
4408 /*
4409 * Compute the height of the font used for the tab text
4410 */
4411 hwnd = GetDesktopWindow();
4412 hdc = GetWindowDC(hwnd);
4413 hfntOld = SelectFont(hdc, font);
4414
4415 GetTextMetrics(hdc, &tm);
4416
4417 SelectFont(hdc, hfntOld);
4418 ReleaseDC(hwnd, hdc);
4419
4420 /*
4421 * The space used by the tab border and the space between the tab label
4422 * and the tab border is included as 7.
4423 */
4424 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4425}
4426#endif
4427
Bram Moolenaar520470a2005-06-16 21:59:56 +00004428/*
4429 * Invoked when a setting was changed.
4430 */
4431 static LRESULT CALLBACK
4432_OnSettingChange(UINT n)
4433{
4434 if (n == SPI_SETWHEELSCROLLLINES)
4435 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4436 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004437#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4438 if (n == SPI_SETNONCLIENTMETRICS)
4439 set_tabline_font();
4440#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004441 return 0;
4442}
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#ifdef FEAT_NETBEANS_INTG
4445 static void
4446_OnWindowPosChanged(
4447 HWND hwnd,
4448 const LPWINDOWPOS lpwpos)
4449{
4450 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004451 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
4453 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4454 || lpwpos->cx != cx || lpwpos->cy != cy))
4455 {
4456 x = lpwpos->x;
4457 y = lpwpos->y;
4458 cx = lpwpos->cx;
4459 cy = lpwpos->cy;
4460 netbeans_frame_moved(x, y);
4461 }
4462 /* Allow to send WM_SIZE and WM_MOVE */
4463 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4464}
4465#endif
4466
4467 static int
4468_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 UINT fwSide,
4470 LPRECT lprc)
4471{
4472 int w, h;
4473 int valid_w, valid_h;
4474 int w_offset, h_offset;
4475
4476 w = lprc->right - lprc->left;
4477 h = lprc->bottom - lprc->top;
4478 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4479 w_offset = w - valid_w;
4480 h_offset = h - valid_h;
4481
4482 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4483 || fwSide == WMSZ_BOTTOMLEFT)
4484 lprc->left += w_offset;
4485 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4486 || fwSide == WMSZ_BOTTOMRIGHT)
4487 lprc->right -= w_offset;
4488
4489 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4490 || fwSide == WMSZ_TOPRIGHT)
4491 lprc->top += h_offset;
4492 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4493 || fwSide == WMSZ_BOTTOMRIGHT)
4494 lprc->bottom -= h_offset;
4495 return TRUE;
4496}
4497
4498
4499
4500 static LRESULT CALLBACK
4501_WndProc(
4502 HWND hwnd,
4503 UINT uMsg,
4504 WPARAM wParam,
4505 LPARAM lParam)
4506{
4507 /*
4508 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4509 hwnd, uMsg, wParam, lParam);
4510 */
4511
4512 HandleMouseHide(uMsg, lParam);
4513
4514 s_uMsg = uMsg;
4515 s_wParam = wParam;
4516 s_lParam = lParam;
4517
4518 switch (uMsg)
4519 {
4520 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4521 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4522 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4523 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4524 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4525 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4526 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4527 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4528 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4529#ifdef FEAT_MENU
4530 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4531#endif
4532 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4533 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4534 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4535 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4536 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4537 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4538 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4539 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4540 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4541#ifdef FEAT_NETBEANS_INTG
4542 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4543#endif
4544
Bram Moolenaarafa24992006-03-27 20:58:26 +00004545#ifdef FEAT_GUI_TABLINE
4546 case WM_RBUTTONUP:
4547 {
4548 if (gui_mch_showing_tabline())
4549 {
4550 POINT pt;
4551 RECT rect;
4552
4553 /*
4554 * If the cursor is on the tabline, display the tab menu
4555 */
4556 GetCursorPos((LPPOINT)&pt);
4557 GetWindowRect(s_textArea, &rect);
4558 if (pt.y < rect.top)
4559 {
4560 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004561 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004562 }
4563 }
4564 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4565 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004566 case WM_LBUTTONDBLCLK:
4567 {
4568 /*
4569 * If the user double clicked the tabline, create a new tab
4570 */
4571 if (gui_mch_showing_tabline())
4572 {
4573 POINT pt;
4574 RECT rect;
4575
4576 GetCursorPos((LPPOINT)&pt);
4577 GetWindowRect(s_textArea, &rect);
4578 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004579 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004580 }
4581 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4582 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004583#endif
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 case WM_QUERYENDSESSION: /* System wants to go down. */
4586 gui_shell_closed(); /* Will exit when no changed buffers. */
4587 return FALSE; /* Do NOT allow system to go down. */
4588
4589 case WM_ENDSESSION:
4590 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004591 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004593 return 0L;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 break;
4596
4597 case WM_CHAR:
4598 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4599 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004600 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 return 0L;
4602
4603 case WM_SYSCHAR:
4604 /*
4605 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4606 * shortcut key, handle like a typed ALT key, otherwise call Windows
4607 * ALT key handling.
4608 */
4609#ifdef FEAT_MENU
4610 if ( !gui.menu_is_active
4611 || p_wak[0] == 'n'
4612 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4613 )
4614#endif
4615 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004616 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 return 0L;
4618 }
4619#ifdef FEAT_MENU
4620 else
4621 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4622#endif
4623
4624 case WM_SYSKEYUP:
4625#ifdef FEAT_MENU
4626 /* This used to be done only when menu is active: ALT key is used for
4627 * that. But that caused problems when menu is disabled and using
4628 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4629 * are received, mouse pointer remains hidden. */
4630 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4631#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004632 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
4634
4635 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
4638 case WM_MOUSEWHEEL:
4639 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004640 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar520470a2005-06-16 21:59:56 +00004642 /* Notification for change in SystemParametersInfo() */
4643 case WM_SETTINGCHANGE:
4644 return _OnSettingChange((UINT)wParam);
4645
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004646#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 case WM_NOTIFY:
4648 switch (((LPNMHDR) lParam)->code)
4649 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004650 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004651 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004653 LPNMHDR hdr = (LPNMHDR)lParam;
4654 char_u *str = NULL;
4655 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004656
Bram Moolenaard23a8232018-02-10 18:45:26 +01004657 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004658
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004659# ifdef FEAT_GUI_TABLINE
4660 if (gui_mch_showing_tabline()
4661 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004662 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004663 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004664 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004665 * Mouse is over the GUI tabline. Display the
4666 * tooltip for the tab under the cursor
4667 *
4668 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004669 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004670 GetCursorPos(&pt);
4671 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004672 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004673 TCHITTESTINFO htinfo;
4674 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004675
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004676 /*
4677 * Get the tab under the cursor
4678 */
4679 htinfo.pt.x = pt.x;
4680 htinfo.pt.y = pt.y;
4681 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4682 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004683 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004684 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004685
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004686 tp = find_tabpage(idx + 1);
4687 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004688 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004689 get_tabline_label(tp, TRUE);
4690 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004691 }
4692 }
4693 }
4694 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004695# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004696# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004697# ifdef FEAT_GUI_TABLINE
4698 else
4699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004701 UINT idButton;
4702 vimmenu_T *pMenu;
4703
4704 idButton = (UINT) hdr->idFrom;
4705 pMenu = gui_mswin_find_menu(root_menu, idButton);
4706 if (pMenu)
4707 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004709# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004710 if (str != NULL)
4711 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004712 if (hdr->code == TTN_GETDISPINFOW)
4713 {
4714 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4715
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004716 /* Set the maximum width, this also enables using
4717 * \n for line break. */
4718 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4719 0, 500);
4720
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004721 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004722 lpdi->lpszText = tt_text;
4723 /* can't show tooltip if failed */
4724 }
4725 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004726 {
4727 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4728
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004729 /* Set the maximum width, this also enables using
4730 * \n for line break. */
4731 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4732 0, 500);
4733
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004734 if (STRLEN(str) < sizeof(lpdi->szText)
4735 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004736 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004737 sizeof(lpdi->szText) - 1);
4738 else
4739 lpdi->lpszText = tt_text;
4740 }
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004744# ifdef FEAT_GUI_TABLINE
4745 case TCN_SELCHANGE:
4746 if (gui_mch_showing_tabline()
4747 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004748 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004749 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004750 return 0L;
4751 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004752 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004753
4754 case NM_RCLICK:
4755 if (gui_mch_showing_tabline()
4756 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004757 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004758 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004759 return 0L;
4760 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004761 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004764# ifdef FEAT_GUI_TABLINE
4765 if (gui_mch_showing_tabline()
4766 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4767 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 break;
4770 }
4771 break;
4772#endif
4773#if defined(MENUHINTS) && defined(FEAT_MENU)
4774 case WM_MENUSELECT:
4775 if (((UINT) HIWORD(wParam)
4776 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4777 == MF_HILITE
4778 && (State & CMDLINE) == 0)
4779 {
4780 UINT idButton;
4781 vimmenu_T *pMenu;
4782 static int did_menu_tip = FALSE;
4783
4784 if (did_menu_tip)
4785 {
4786 msg_clr_cmdline();
4787 setcursor();
4788 out_flush();
4789 did_menu_tip = FALSE;
4790 }
4791
4792 idButton = (UINT)LOWORD(wParam);
4793 pMenu = gui_mswin_find_menu(root_menu, idButton);
4794 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4795 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4796 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004797 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004798 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004799 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 setcursor();
4801 out_flush();
4802 did_menu_tip = TRUE;
4803 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004804 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
4806 break;
4807#endif
4808 case WM_NCHITTEST:
4809 {
4810 LRESULT result;
4811 int x, y;
4812 int xPos = GET_X_LPARAM(lParam);
4813
4814 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4815 if (result == HTCLIENT)
4816 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004817#ifdef FEAT_GUI_TABLINE
4818 if (gui_mch_showing_tabline())
4819 {
4820 int yPos = GET_Y_LPARAM(lParam);
4821 RECT rct;
4822
4823 /* If the cursor is on the GUI tabline, don't process this
4824 * event */
4825 GetWindowRect(s_textArea, &rct);
4826 if (yPos < rct.top)
4827 return result;
4828 }
4829#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004830 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 xPos -= x;
4832
4833 if (xPos < 48) /* <VN> TODO should use system metric? */
4834 return HTBOTTOMLEFT;
4835 else
4836 return HTBOTTOMRIGHT;
4837 }
4838 else
4839 return result;
4840 }
4841 /* break; notreached */
4842
4843#ifdef FEAT_MBYTE_IME
4844 case WM_IME_NOTIFY:
4845 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4846 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004847 return 1L;
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 case WM_IME_COMPOSITION:
4850 if (!_OnImeComposition(hwnd, wParam, lParam))
4851 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004852 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853#endif
4854
4855 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004857 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
4860 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4861 }
4862
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004863 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864}
4865
4866/*
4867 * End of call-back routines
4868 */
4869
4870/* parent window, if specified with -P */
4871HWND vim_parent_hwnd = NULL;
4872
4873 static BOOL CALLBACK
4874FindWindowTitle(HWND hwnd, LPARAM lParam)
4875{
4876 char buf[2048];
4877 char *title = (char *)lParam;
4878
4879 if (GetWindowText(hwnd, buf, sizeof(buf)))
4880 {
4881 if (strstr(buf, title) != NULL)
4882 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004883 /* Found it. Store the window ref. and quit searching if MDI
4884 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004886 if (vim_parent_hwnd != NULL)
4887 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 }
4890 return TRUE; /* continue searching */
4891}
4892
4893/*
4894 * Invoked for '-P "title"' argument: search for parent application to open
4895 * our window in.
4896 */
4897 void
4898gui_mch_set_parent(char *title)
4899{
4900 EnumWindows(FindWindowTitle, (LPARAM)title);
4901 if (vim_parent_hwnd == NULL)
4902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004903 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 mch_exit(2);
4905 }
4906}
4907
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004908#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 static void
4910ole_error(char *arg)
4911{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004912 char buf[IOSIZE];
4913
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004914 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004915 vim_snprintf(buf, IOSIZE,
4916 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4917 arg);
4918 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922/*
4923 * Parse the GUI related command-line arguments. Any arguments used are
4924 * deleted from argv, and *argc is decremented accordingly. This is called
4925 * when vim is started, whether or not the GUI has been started.
4926 */
4927 void
4928gui_mch_prepare(int *argc, char **argv)
4929{
4930 int silent = FALSE;
4931 int idx;
4932
4933 /* Check for special OLE command line parameters */
4934 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
4935 {
4936 /* Check for a "-silent" argument first. */
4937 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
4938 && (argv[2][0] == '-' || argv[2][0] == '/'))
4939 {
4940 silent = TRUE;
4941 idx = 2;
4942 }
4943 else
4944 idx = 1;
4945
4946 /* Register Vim as an OLE Automation server */
4947 if (STRICMP(argv[idx] + 1, "register") == 0)
4948 {
4949#ifdef FEAT_OLE
4950 RegisterMe(silent);
4951 mch_exit(0);
4952#else
4953 if (!silent)
4954 ole_error("register");
4955 mch_exit(2);
4956#endif
4957 }
4958
4959 /* Unregister Vim as an OLE Automation server */
4960 if (STRICMP(argv[idx] + 1, "unregister") == 0)
4961 {
4962#ifdef FEAT_OLE
4963 UnregisterMe(!silent);
4964 mch_exit(0);
4965#else
4966 if (!silent)
4967 ole_error("unregister");
4968 mch_exit(2);
4969#endif
4970 }
4971
4972 /* Ignore an -embedding argument. It is only relevant if the
4973 * application wants to treat the case when it is started manually
4974 * differently from the case where it is started via automation (and
4975 * we don't).
4976 */
4977 if (STRICMP(argv[idx] + 1, "embedding") == 0)
4978 {
4979#ifdef FEAT_OLE
4980 *argc = 1;
4981#else
4982 ole_error("embedding");
4983 mch_exit(2);
4984#endif
4985 }
4986 }
4987
4988#ifdef FEAT_OLE
4989 {
4990 int bDoRestart = FALSE;
4991
4992 InitOLE(&bDoRestart);
4993 /* automatically exit after registering */
4994 if (bDoRestart)
4995 mch_exit(0);
4996 }
4997#endif
4998
4999#ifdef FEAT_NETBEANS_INTG
5000 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005001 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 int arg;
5003
5004 for (arg = 1; arg < *argc; arg++)
5005 if (strncmp("-nb", argv[arg], 3) == 0)
5006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 netbeansArg = argv[arg];
5008 mch_memmove(&argv[arg], &argv[arg + 1],
5009 (--*argc - arg) * sizeof(char *));
5010 argv[*argc] = NULL;
5011 break; /* enough? */
5012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015}
5016
5017/*
5018 * Initialise the GUI. Create all the windows, set up all the call-backs
5019 * etc.
5020 */
5021 int
5022gui_mch_init(void)
5023{
5024 const char szVimWndClass[] = VIM_CLASS;
5025 const char szTextAreaClass[] = "VimTextArea";
5026 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005028 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030#ifdef GLOBAL_IME
5031 ATOM atom;
5032#endif
5033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 /* Return here if the window was already opened (happens when
5035 * gui_mch_dialog() is called early). */
5036 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005037 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
5039 /*
5040 * Load the tearoff bitmap
5041 */
5042#ifdef FEAT_TEAROFF
5043 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5044#endif
5045
5046 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5047 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5048#ifdef FEAT_MENU
5049 gui.menu_height = 0; /* Windows takes care of this */
5050#endif
5051 gui.border_width = 0;
5052
5053 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /* First try using the wide version, so that we can use any title.
5056 * Otherwise only characters in the active codepage will work. */
5057 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5058 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 wndclassw.lpfnWndProc = _WndProc;
5061 wndclassw.cbClsExtra = 0;
5062 wndclassw.cbWndExtra = 0;
5063 wndclassw.hInstance = s_hinst;
5064 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5065 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5066 wndclassw.hbrBackground = s_brush;
5067 wndclassw.lpszMenuName = NULL;
5068 wndclassw.lpszClassName = szVimWndClassW;
5069
5070 if ((
5071#ifdef GLOBAL_IME
5072 atom =
5073#endif
5074 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005075 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 else
5077 wide_WindowProc = TRUE;
5078 }
5079
5080 if (!wide_WindowProc)
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005081 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5082 {
5083 wndclass.style = CS_DBLCLKS;
5084 wndclass.lpfnWndProc = _WndProc;
5085 wndclass.cbClsExtra = 0;
5086 wndclass.cbWndExtra = 0;
5087 wndclass.hInstance = s_hinst;
5088 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5089 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5090 wndclass.hbrBackground = s_brush;
5091 wndclass.lpszMenuName = NULL;
5092 wndclass.lpszClassName = szVimWndClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005094 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095#ifdef GLOBAL_IME
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005096 atom =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005098 RegisterClass(&wndclass)) == 0)
5099 return FAIL;
5100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 if (vim_parent_hwnd != NULL)
5103 {
5104#ifdef HAVE_TRY_EXCEPT
5105 __try
5106 {
5107#endif
5108 /* Open inside the specified parent window.
5109 * TODO: last argument should point to a CLIENTCREATESTRUCT
5110 * structure. */
5111 s_hwnd = CreateWindowEx(
5112 WS_EX_MDICHILD,
5113 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005114 WS_OVERLAPPEDWINDOW | WS_CHILD
5115 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5117 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5118 100, /* Any value will do */
5119 100, /* Any value will do */
5120 vim_parent_hwnd, NULL,
5121 s_hinst, NULL);
5122#ifdef HAVE_TRY_EXCEPT
5123 }
5124 __except(EXCEPTION_EXECUTE_HANDLER)
5125 {
5126 /* NOP */
5127 }
5128#endif
5129 if (s_hwnd == NULL)
5130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005131 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 mch_exit(2);
5133 }
5134 }
5135 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005136 {
5137 /* If the provided windowid is not valid reset it to zero, so that it
5138 * is ignored and we open our own window. */
5139 if (IsWindow((HWND)win_socket_id) <= 0)
5140 win_socket_id = 0;
5141
5142 /* Create a window. If win_socket_id is not zero without border and
5143 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005145 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005146 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5147 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005148 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5149 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5150 100, /* Any value will do */
5151 100, /* Any value will do */
5152 NULL, NULL,
5153 s_hinst, NULL);
5154 if (s_hwnd != NULL && win_socket_id != 0)
5155 {
5156 SetParent(s_hwnd, (HWND)win_socket_id);
5157 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5158 }
5159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160
5161 if (s_hwnd == NULL)
5162 return FAIL;
5163
5164#ifdef GLOBAL_IME
5165 global_ime_init(atom, s_hwnd);
5166#endif
5167#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5168 dyn_imm_load();
5169#endif
5170
5171 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005172 if (wide_WindowProc)
5173 {
5174 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5175 {
5176 wndclassw.style = CS_OWNDC;
5177 wndclassw.lpfnWndProc = _TextAreaWndProc;
5178 wndclassw.cbClsExtra = 0;
5179 wndclassw.cbWndExtra = 0;
5180 wndclassw.hInstance = s_hinst;
5181 wndclassw.hIcon = NULL;
5182 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5183 wndclassw.hbrBackground = NULL;
5184 wndclassw.lpszMenuName = NULL;
5185 wndclassw.lpszClassName = szTextAreaClassW;
5186
5187 if (RegisterClassW(&wndclassw) == 0)
5188 return FAIL;
5189 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005190
5191 s_textArea = CreateWindowExW(
5192 0,
5193 szTextAreaClassW, L"Vim text area",
5194 WS_CHILD | WS_VISIBLE, 0, 0,
5195 100, // Any value will do for now
5196 100, // Any value will do for now
5197 s_hwnd, NULL,
5198 s_hinst, NULL);
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005199 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005200 else if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 {
5202 wndclass.style = CS_OWNDC;
5203 wndclass.lpfnWndProc = _TextAreaWndProc;
5204 wndclass.cbClsExtra = 0;
5205 wndclass.cbWndExtra = 0;
5206 wndclass.hInstance = s_hinst;
5207 wndclass.hIcon = NULL;
5208 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5209 wndclass.hbrBackground = NULL;
5210 wndclass.lpszMenuName = NULL;
5211 wndclass.lpszClassName = szTextAreaClass;
5212
5213 if (RegisterClass(&wndclass) == 0)
5214 return FAIL;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005215
5216 s_textArea = CreateWindowEx(
5217 0,
5218 szTextAreaClass, "Vim text area",
5219 WS_CHILD | WS_VISIBLE, 0, 0,
5220 100, // Any value will do for now
5221 100, // Any value will do for now
5222 s_hwnd, NULL,
5223 s_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225
5226 if (s_textArea == NULL)
5227 return FAIL;
5228
Bram Moolenaar20321902016-02-17 12:30:17 +01005229#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005230 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5231 {
5232 HANDLE hIcon = NULL;
5233
5234 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005235 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005236 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005237#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#ifdef FEAT_MENU
5240 s_menuBar = CreateMenu();
5241#endif
5242 s_hdc = GetDC(s_textArea);
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 /* Do we need to bother with this? */
5247 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5248
5249 /* Get background/foreground colors from the system */
5250 gui_mch_def_colors();
5251
5252 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5253 * file) */
5254 set_normal_colors();
5255
5256 /*
5257 * Check that none of the colors are the same as the background color.
5258 * Then store the current values as the defaults.
5259 */
5260 gui_check_colors();
5261 gui.def_norm_pixel = gui.norm_pixel;
5262 gui.def_back_pixel = gui.back_pixel;
5263
5264 /* Get the colors for the highlight groups (gui_check_colors() might have
5265 * changed them) */
5266 highlight_gui_started();
5267
5268 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005269 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005271 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 /*
5274 * Set up for Intellimouse processing
5275 */
5276 init_mouse_wheel();
5277
5278 /*
5279 * compute a couple of metrics used for the dialogs
5280 */
5281 get_dialog_font_metrics();
5282#ifdef FEAT_TOOLBAR
5283 /*
5284 * Create the toolbar
5285 */
5286 initialise_toolbar();
5287#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005288#ifdef FEAT_GUI_TABLINE
5289 /*
5290 * Create the tabline
5291 */
5292 initialise_tabline();
5293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#ifdef MSWIN_FIND_REPLACE
5295 /*
5296 * Initialise the dialog box stuff
5297 */
5298 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5299
5300 /* Initialise the struct */
5301 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005302 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005304 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5306 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5307 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005308 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5309 s_findrep_struct_w.lpstrFindWhat =
5310 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5311 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5312 s_findrep_struct_w.lpstrReplaceWith =
5313 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5314 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5315 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5316 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005319#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005320# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5321/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5322# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005323# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005324# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005325# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005326 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005327 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005328#endif
5329
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005330#ifdef FEAT_RENDER_OPTIONS
5331 if (p_rop)
5332 (void)gui_mch_set_rendering_options(p_rop);
5333#endif
5334
Bram Moolenaar748bf032005-02-02 23:04:36 +00005335theend:
5336 /* Display any pending error messages */
5337 display_errors();
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 return OK;
5340}
5341
5342/*
5343 * Get the size of the screen, taking position on multiple monitors into
5344 * account (if supported).
5345 */
5346 static void
5347get_work_area(RECT *spi_rect)
5348{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005349 HMONITOR mon;
5350 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005352 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005353 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005354 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005356 moninfo.cbSize = sizeof(MONITORINFO);
5357 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005359 *spi_rect = moninfo.rcWork;
5360 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362 }
5363 /* this is the old method... */
5364 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5365}
5366
5367/*
5368 * Set the size of the window to the given width and height in pixels.
5369 */
5370 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005371gui_mch_set_shellsize(
5372 int width,
5373 int height,
5374 int min_width UNUSED,
5375 int min_height UNUSED,
5376 int base_width UNUSED,
5377 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005378 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380 RECT workarea_rect;
5381 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 WINDOWPLACEMENT wndpl;
5383
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005384 /* Try to keep window completely on screen. */
5385 /* Get position of the screen work area. This is the part that is not
5386 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 get_work_area(&workarea_rect);
5388
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005389 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005390 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 wndpl.length = sizeof(WINDOWPLACEMENT);
5392 GetWindowPlacement(s_hwnd, &wndpl);
5393
5394 /* Resizing a maximized window looks very strange, unzoom it first.
5395 * But don't do it when still starting up, it may have been requested in
5396 * the shortcut. */
5397 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5398 {
5399 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5400 /* Need to get the settings of the normal window. */
5401 GetWindowPlacement(s_hwnd, &wndpl);
5402 }
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005405 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005406 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005407 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005408 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 + GetSystemMetrics(SM_CYCAPTION)
5410#ifdef FEAT_MENU
5411 + gui_mswin_get_menu_height(FALSE)
5412#endif
5413 ;
5414
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005415 /* The following should take care of keeping Vim on the same monitor, no
5416 * matter if the secondary monitor is left or right of the primary
5417 * monitor. */
5418 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5419 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005420
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005421 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005422 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005423 && wndpl.rcNormalPosition.right > workarea_rect.right)
5424 OffsetRect(&wndpl.rcNormalPosition,
5425 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005427 if ((direction & RESIZE_HOR)
5428 && wndpl.rcNormalPosition.left < workarea_rect.left)
5429 OffsetRect(&wndpl.rcNormalPosition,
5430 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaarafa24992006-03-27 20:58:26 +00005432 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005433 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5434 OffsetRect(&wndpl.rcNormalPosition,
5435 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005437 if ((direction & RESIZE_VERT)
5438 && wndpl.rcNormalPosition.top < workarea_rect.top)
5439 OffsetRect(&wndpl.rcNormalPosition,
5440 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441
5442 /* set window position - we should use SetWindowPlacement rather than
5443 * SetWindowPos as the MSDN docs say the coord systems returned by
5444 * these two are not compatible. */
5445 SetWindowPlacement(s_hwnd, &wndpl);
5446
5447 SetActiveWindow(s_hwnd);
5448 SetFocus(s_hwnd);
5449
5450#ifdef FEAT_MENU
5451 /* Menu may wrap differently now */
5452 gui_mswin_get_menu_height(!gui.starting);
5453#endif
5454}
5455
5456
5457 void
5458gui_mch_set_scrollbar_thumb(
5459 scrollbar_T *sb,
5460 long val,
5461 long size,
5462 long max)
5463{
5464 SCROLLINFO info;
5465
5466 sb->scroll_shift = 0;
5467 while (max > 32767)
5468 {
5469 max = (max + 1) >> 1;
5470 val >>= 1;
5471 size >>= 1;
5472 ++sb->scroll_shift;
5473 }
5474
5475 if (sb->scroll_shift > 0)
5476 ++size;
5477
5478 info.cbSize = sizeof(info);
5479 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5480 info.nPos = val;
5481 info.nMin = 0;
5482 info.nMax = max;
5483 info.nPage = size;
5484 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5485}
5486
5487
5488/*
5489 * Set the current text font.
5490 */
5491 void
5492gui_mch_set_font(GuiFont font)
5493{
5494 gui.currFont = font;
5495}
5496
5497
5498/*
5499 * Set the current text foreground color.
5500 */
5501 void
5502gui_mch_set_fg_color(guicolor_T color)
5503{
5504 gui.currFgColor = color;
5505}
5506
5507/*
5508 * Set the current text background color.
5509 */
5510 void
5511gui_mch_set_bg_color(guicolor_T color)
5512{
5513 gui.currBgColor = color;
5514}
5515
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005516/*
5517 * Set the current text special color.
5518 */
5519 void
5520gui_mch_set_sp_color(guicolor_T color)
5521{
5522 gui.currSpColor = color;
5523}
5524
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005525#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526/*
5527 * Multi-byte handling, originally by Sung-Hoon Baek.
5528 * First static functions (no prototypes generated).
5529 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005530# ifdef _MSC_VER
5531# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5532# endif
5533# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 * handle WM_IME_NOTIFY message
5537 */
5538 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005539_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 LRESULT lResult = 0;
5542 HIMC hImc;
5543
5544 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5545 return lResult;
5546 switch (dwCommand)
5547 {
5548 case IMN_SETOPENSTATUS:
5549 if (pImmGetOpenStatus(hImc))
5550 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005551 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 im_set_position(gui.row, gui.col);
5553
5554 /* Disable langmap */
5555 State &= ~LANGMAP;
5556 if (State & INSERT)
5557 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005558#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 /* Unshown 'keymap' in status lines */
5560 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5561 {
5562 /* Save cursor position */
5563 int old_row = gui.row;
5564 int old_col = gui.col;
5565
5566 // This must be called here before
5567 // status_redraw_curbuf(), otherwise the mode
5568 // message may appear in the wrong position.
5569 showmode();
5570 status_redraw_curbuf();
5571 update_screen(0);
5572 /* Restore cursor position */
5573 gui.row = old_row;
5574 gui.col = old_col;
5575 }
5576#endif
5577 }
5578 }
5579 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005580 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 lResult = 0;
5582 break;
5583 }
5584 pImmReleaseContext(hWnd, hImc);
5585 return lResult;
5586}
5587
5588 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005589_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
5591 char_u *ret;
5592 int len;
5593
5594 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5595 return 0;
5596
5597 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5598 if (ret != NULL)
5599 {
5600 add_to_input_buf_csi(ret, len);
5601 vim_free(ret);
5602 return 1;
5603 }
5604 return 0;
5605}
5606
5607/*
5608 * get the current composition string, in UCS-2; *lenp is the number of
5609 * *lenp is the number of Unicode characters.
5610 */
5611 static short_u *
5612GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5613{
5614 LONG ret;
5615 LPWSTR wbuf = NULL;
5616 char_u *buf;
5617
5618 if (!pImmGetContext)
5619 return NULL; /* no imm32.dll */
5620
5621 /* Try Unicode; this'll always work on NT regardless of codepage. */
5622 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5623 if (ret == 0)
5624 return NULL; /* empty */
5625
5626 if (ret > 0)
5627 {
5628 /* Allocate the requested buffer plus space for the NUL character. */
5629 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5630 if (wbuf != NULL)
5631 {
5632 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5633 *lenp = ret / sizeof(WCHAR);
5634 }
5635 return (short_u *)wbuf;
5636 }
5637
5638 /* ret < 0; we got an error, so try the ANSI version. This'll work
5639 * on 9x/ME, but only if the codepage happens to be set to whatever
5640 * we're inputting. */
5641 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5642 if (ret <= 0)
5643 return NULL; /* empty or error */
5644
5645 buf = alloc(ret);
5646 if (buf == NULL)
5647 return NULL;
5648 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5649
5650 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005651 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 vim_free(buf);
5653
5654 return (short_u *)wbuf;
5655}
5656
5657/*
5658 * void GetResultStr()
5659 *
5660 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5661 * get complete composition string
5662 */
5663 static char_u *
5664GetResultStr(HWND hwnd, int GCS, int *lenp)
5665{
5666 HIMC hIMC; /* Input context handle. */
5667 short_u *buf = NULL;
5668 char_u *convbuf = NULL;
5669
5670 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5671 return NULL;
5672
5673 /* Reads in the composition string. */
5674 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5675 if (buf == NULL)
5676 return NULL;
5677
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005678 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 pImmReleaseContext(hwnd, hIMC);
5680 vim_free(buf);
5681 return convbuf;
5682}
5683#endif
5684
5685/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005686#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
5688/*
5689 * set font to IM.
5690 */
5691 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005692im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 HIMC hImc;
5695
5696 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5697 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005698 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 pImmReleaseContext(s_hwnd, hImc);
5700 }
5701}
5702
5703/*
5704 * Notify cursor position to IM.
5705 */
5706 void
5707im_set_position(int row, int col)
5708{
5709 HIMC hImc;
5710
5711 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5712 {
5713 COMPOSITIONFORM cfs;
5714
5715 cfs.dwStyle = CFS_POINT;
5716 cfs.ptCurrentPos.x = FILL_X(col);
5717 cfs.ptCurrentPos.y = FILL_Y(row);
5718 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5719 pImmSetCompositionWindow(hImc, &cfs);
5720
5721 pImmReleaseContext(s_hwnd, hImc);
5722 }
5723}
5724
5725/*
5726 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5727 */
5728 void
5729im_set_active(int active)
5730{
5731 HIMC hImc;
5732 static HIMC hImcOld = (HIMC)0;
5733
5734 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5735 {
5736 if (p_imdisable)
5737 {
5738 if (hImcOld == (HIMC)0)
5739 {
5740 hImcOld = pImmGetContext(s_hwnd);
5741 if (hImcOld)
5742 pImmAssociateContext(s_hwnd, (HIMC)0);
5743 }
5744 active = FALSE;
5745 }
5746 else if (hImcOld != (HIMC)0)
5747 {
5748 pImmAssociateContext(s_hwnd, hImcOld);
5749 hImcOld = (HIMC)0;
5750 }
5751
5752 hImc = pImmGetContext(s_hwnd);
5753 if (hImc)
5754 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005755 /*
5756 * for Korean ime
5757 */
5758 HKL hKL = GetKeyboardLayout(0);
5759
5760 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5761 {
5762 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5763 static BOOL bSaved = FALSE;
5764
5765 if (active)
5766 {
5767 /* if we have a saved conversion status, restore it */
5768 if (bSaved)
5769 pImmSetConversionStatus(hImc, dwConversionSaved,
5770 dwSentenceSaved);
5771 bSaved = FALSE;
5772 }
5773 else
5774 {
5775 /* save conversion status and disable korean */
5776 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5777 &dwSentenceSaved))
5778 {
5779 bSaved = TRUE;
5780 pImmSetConversionStatus(hImc,
5781 dwConversionSaved & ~(IME_CMODE_NATIVE
5782 | IME_CMODE_FULLSHAPE),
5783 dwSentenceSaved);
5784 }
5785 }
5786 }
5787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 pImmSetOpenStatus(hImc, active);
5789 pImmReleaseContext(s_hwnd, hImc);
5790 }
5791 }
5792}
5793
5794/*
5795 * Get IM status. When IM is on, return not 0. Else return 0.
5796 */
5797 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005798im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
5800 int status = 0;
5801 HIMC hImc;
5802
5803 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5804 {
5805 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5806 pImmReleaseContext(s_hwnd, hImc);
5807 }
5808 return status;
5809}
5810
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005811#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005813#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814/* Win32 with GLOBAL IME */
5815
5816/*
5817 * Notify cursor position to IM.
5818 */
5819 void
5820im_set_position(int row, int col)
5821{
5822 /* Win32 with GLOBAL IME */
5823 POINT p;
5824
5825 p.x = FILL_X(col);
5826 p.y = FILL_Y(row);
5827 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5828 global_ime_set_position(&p);
5829}
5830
5831/*
5832 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5833 */
5834 void
5835im_set_active(int active)
5836{
5837 global_ime_set_status(active);
5838}
5839
5840/*
5841 * Get IM status. When IM is on, return not 0. Else return 0.
5842 */
5843 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005844im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845{
5846 return global_ime_get_status();
5847}
5848#endif
5849
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005850/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005851 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005852 */
5853 static void
5854latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5855{
5856 int c;
5857
Bram Moolenaarca003e12006-03-17 23:19:38 +00005858 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005859 {
5860 c = *text++;
5861 switch (c)
5862 {
5863 case 0xa4: c = 0x20ac; break; /* euro */
5864 case 0xa6: c = 0x0160; break; /* S hat */
5865 case 0xa8: c = 0x0161; break; /* S -hat */
5866 case 0xb4: c = 0x017d; break; /* Z hat */
5867 case 0xb8: c = 0x017e; break; /* Z -hat */
5868 case 0xbc: c = 0x0152; break; /* OE */
5869 case 0xbd: c = 0x0153; break; /* oe */
5870 case 0xbe: c = 0x0178; break; /* Y */
5871 }
5872 *unicodebuf++ = c;
5873 }
5874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876#ifdef FEAT_RIGHTLEFT
5877/*
5878 * What is this for? In the case where you are using Win98 or Win2K or later,
5879 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5880 * reverses the string sent to the TextOut... family. This sucks, because we
5881 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5882 * way to tell Windblows not to do this!
5883 *
5884 * The short of it is that this 'RevOut' only gets called if you are running
5885 * one of the new, "improved" MS OSes, and only if you are running in
5886 * 'rightleft' mode. It makes display take *slightly* longer, but not
5887 * noticeably so.
5888 */
5889 static void
5890RevOut( HDC s_hdc,
5891 int col,
5892 int row,
5893 UINT foptions,
5894 CONST RECT *pcliprect,
5895 LPCTSTR text,
5896 UINT len,
5897 CONST INT *padding)
5898{
5899 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005901 for (ix = 0; ix < (int)len; ++ix)
5902 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5903 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904}
5905#endif
5906
Bram Moolenaar92467d32017-12-05 13:22:16 +01005907 static void
5908draw_line(
5909 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005910 int y1,
5911 int x2,
5912 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005913 COLORREF color)
5914{
5915#if defined(FEAT_DIRECTX)
5916 if (IS_ENABLE_DIRECTX())
5917 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5918 else
5919#endif
5920 {
5921 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5922 HPEN old_pen = SelectObject(s_hdc, hpen);
5923 MoveToEx(s_hdc, x1, y1, NULL);
5924 /* Note: LineTo() excludes the last pixel in the line. */
5925 LineTo(s_hdc, x2, y2);
5926 DeleteObject(SelectObject(s_hdc, old_pen));
5927 }
5928}
5929
5930 static void
5931set_pixel(
5932 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005933 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005934 COLORREF color)
5935{
5936#if defined(FEAT_DIRECTX)
5937 if (IS_ENABLE_DIRECTX())
5938 DWriteContext_SetPixel(s_dwc, x, y, color);
5939 else
5940#endif
5941 SetPixel(s_hdc, x, y, color);
5942}
5943
5944 static void
5945fill_rect(
5946 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005947 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005948 COLORREF color)
5949{
5950#if defined(FEAT_DIRECTX)
5951 if (IS_ENABLE_DIRECTX())
5952 DWriteContext_FillRect(s_dwc, rcp, color);
5953 else
5954#endif
5955 {
5956 HBRUSH hbr2;
5957
5958 if (hbr == NULL)
5959 hbr2 = CreateSolidBrush(color);
5960 else
5961 hbr2 = hbr;
5962 FillRect(s_hdc, rcp, hbr2);
5963 if (hbr == NULL)
5964 DeleteBrush(hbr2);
5965 }
5966}
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 void
5969gui_mch_draw_string(
5970 int row,
5971 int col,
5972 char_u *text,
5973 int len,
5974 int flags)
5975{
5976 static int *padding = NULL;
5977 static int pad_size = 0;
5978 int i;
5979 const RECT *pcliprect = NULL;
5980 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 static WCHAR *unicodebuf = NULL;
5982 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00005983 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 int y;
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 /*
5988 * Italic and bold text seems to have an extra row of pixels at the bottom
5989 * (below where the bottom of the character should be). If we draw the
5990 * characters with a solid background, the top row of pixels in the
5991 * character below will be overwritten. We can fix this by filling in the
5992 * background ourselves, to the correct character proportions, and then
5993 * writing the character in transparent mode. Still have a problem when
5994 * the character is "_", which gets written on to the character below.
5995 * New fix: set gui.char_ascent to -1. This shifts all characters up one
5996 * pixel in their slots, which fixes the problem with the bottom row of
5997 * pixels. We still need this code because otherwise the top row of pixels
5998 * becomes a problem. - webb.
5999 */
6000 static HBRUSH hbr_cache[2] = {NULL, NULL};
6001 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6002 static int brush_lru = 0;
6003 HBRUSH hbr;
6004 RECT rc;
6005
6006 if (!(flags & DRAW_TRANSP))
6007 {
6008 /*
6009 * Clear background first.
6010 * Note: FillRect() excludes right and bottom of rectangle.
6011 */
6012 rc.left = FILL_X(col);
6013 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 if (has_mbyte)
6015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006017 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 }
6019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 rc.right = FILL_X(col + len);
6021 rc.bottom = FILL_Y(row + 1);
6022
6023 /* Cache the created brush, that saves a lot of time. We need two:
6024 * one for cursor background and one for the normal background. */
6025 if (gui.currBgColor == brush_color[0])
6026 {
6027 hbr = hbr_cache[0];
6028 brush_lru = 1;
6029 }
6030 else if (gui.currBgColor == brush_color[1])
6031 {
6032 hbr = hbr_cache[1];
6033 brush_lru = 0;
6034 }
6035 else
6036 {
6037 if (hbr_cache[brush_lru] != NULL)
6038 DeleteBrush(hbr_cache[brush_lru]);
6039 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6040 brush_color[brush_lru] = gui.currBgColor;
6041 hbr = hbr_cache[brush_lru];
6042 brush_lru = !brush_lru;
6043 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006044
Bram Moolenaar92467d32017-12-05 13:22:16 +01006045 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047 SetBkMode(s_hdc, TRANSPARENT);
6048
6049 /*
6050 * When drawing block cursor, prevent inverted character spilling
6051 * over character cell (can happen with bold/italic)
6052 */
6053 if (flags & DRAW_CURSOR)
6054 {
6055 pcliprect = &rc;
6056 foptions = ETO_CLIPPED;
6057 }
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 SetTextColor(s_hdc, gui.currFgColor);
6060 SelectFont(s_hdc, gui.currFont);
6061
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006062#ifdef FEAT_DIRECTX
6063 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006064 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006065#endif
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6068 {
6069 vim_free(padding);
6070 pad_size = Columns;
6071
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006072 /* Don't give an out-of-memory message here, it would call us
6073 * recursively. */
6074 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 if (padding != NULL)
6076 for (i = 0; i < pad_size; i++)
6077 padding[i] = gui.char_width;
6078 }
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 /*
6081 * We have to provide the padding argument because italic and bold versions
6082 * of fixed-width fonts are often one pixel or so wider than their normal
6083 * versions.
6084 * No check for DRAW_BOLD, Windows will have done it already.
6085 */
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /* Check if there are any UTF-8 characters. If not, use normal text
6088 * output to speed up output. */
6089 if (enc_utf8)
6090 for (n = 0; n < len; ++n)
6091 if (text[n] >= 0x80)
6092 break;
6093
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006094#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006095 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6096 * required that unicode drawing routine, currently. So this forces it
6097 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006098 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006099 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006100#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006101
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006103 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006105 if ((enc_utf8
6106 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6107 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 && (unicodebuf == NULL || len > unibuflen))
6109 {
6110 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006111 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006114 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116 unibuflen = len;
6117 }
6118
6119 if (enc_utf8 && n < len && unicodebuf != NULL)
6120 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006121 /* Output UTF-8 characters. Composing characters should be
6122 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006123 int i;
6124 int wlen; /* string length in words */
6125 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 int cells; /* cell width of string up to composing char */
6127 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006128 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006130 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006131 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006133 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006135 c = utf_ptr2char(text + i);
6136 if (c >= 0x10000)
6137 {
6138 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006139 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6140 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006141 }
6142 else
6143 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006144 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006145 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006146
6147 if (utf_iscomposing(c))
6148 cw = 0;
6149 else
6150 {
6151 cw = utf_char2cells(c);
6152 if (cw > 2) /* don't use 4 for unprintable char */
6153 cw = 1;
6154 }
6155
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 if (unicodepdy != NULL)
6157 {
6158 /* Use unicodepdy to make characters fit as we expect, even
6159 * when the font uses different widths (e.g., bold character
6160 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006161 if (c >= 0x10000)
6162 {
6163 unicodepdy[wlen - 2] = cw * gui.char_width;
6164 unicodepdy[wlen - 1] = 0;
6165 }
6166 else
6167 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 }
6169 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006170 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006171 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006173#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006174 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006175 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006176 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006177 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006178 TEXT_X(col), TEXT_Y(row),
6179 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006180 gui.char_width, gui.currFgColor,
6181 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006182 }
6183 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006184#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006185 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6186 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 len = cells; /* used for underlining */
6188 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006189 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191 /* If we want to display codepage data, and the current CP is not the
6192 * ANSI one, we need to go via Unicode. */
6193 if (unicodebuf != NULL)
6194 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006195 if (enc_latin9)
6196 latin9_to_ucs(text, len, unicodebuf);
6197 else
6198 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 MB_PRECOMPOSED,
6200 (char *)text, len,
6201 (LPWSTR)unicodebuf, unibuflen);
6202 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006203 {
6204 /* Use unicodepdy to make characters fit as we expect, even
6205 * when the font uses different widths (e.g., bold character
6206 * is wider). */
6207 if (unicodepdy != NULL)
6208 {
6209 int i;
6210 int cw;
6211
6212 for (i = 0; i < len; ++i)
6213 {
6214 cw = utf_char2cells(unicodebuf[i]);
6215 if (cw > 2)
6216 cw = 1;
6217 unicodepdy[i] = cw * gui.char_width;
6218 }
6219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006221 foptions, pcliprect, unicodebuf, len, unicodepdy);
6222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224 }
6225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006228 /* Windows will mess up RL text, so we have to draw it character by
6229 * character. Only do this if RL is on, since it's slow. */
6230 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6232 foptions, pcliprect, (char *)text, len, padding);
6233 else
6234#endif
6235 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6236 foptions, pcliprect, (char *)text, len, padding);
6237 }
6238
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006239 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 if (flags & DRAW_UNDERL)
6241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 /* When p_linespace is 0, overwrite the bottom row of pixels.
6243 * Otherwise put the line just below the character. */
6244 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 if (p_linespace > 1)
6246 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006247 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006249
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006250 /* Strikethrough */
6251 if (flags & DRAW_STRIKE)
6252 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006253 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006254 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006255 }
6256
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006257 /* Undercurl */
6258 if (flags & DRAW_UNDERC)
6259 {
6260 int x;
6261 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006262 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006263
6264 y = FILL_Y(row + 1) - 1;
6265 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6266 {
6267 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006268 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006269 }
6270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272
6273
6274/*
6275 * Output routines.
6276 */
6277
6278/* Flush any output to the screen */
6279 void
6280gui_mch_flush(void)
6281{
6282# if defined(__BORLANDC__)
6283 /*
6284 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6285 * prototype declaration.
6286 * The compiler complains if __stdcall is not used in both declarations.
6287 */
6288 BOOL __stdcall GdiFlush(void);
6289# endif
6290
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006291#if defined(FEAT_DIRECTX)
6292 if (IS_ENABLE_DIRECTX())
6293 DWriteContext_Flush(s_dwc);
6294#endif
6295
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 GdiFlush();
6297}
6298
6299 static void
6300clear_rect(RECT *rcp)
6301{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006302 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303}
6304
6305
Bram Moolenaarc716c302006-01-21 22:12:51 +00006306 void
6307gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6308{
6309 RECT workarea_rect;
6310
6311 get_work_area(&workarea_rect);
6312
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006313 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006314 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006315 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006316
6317 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6318 * the menubar for MSwin, we subtract it from the screen height, so that
6319 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006320 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006321 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006322 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006323 - GetSystemMetrics(SM_CYCAPTION)
6324#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006325 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006326#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006327 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006328}
6329
6330
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331#if defined(FEAT_MENU) || defined(PROTO)
6332/*
6333 * Add a sub menu to the menu bar.
6334 */
6335 void
6336gui_mch_add_menu(
6337 vimmenu_T *menu,
6338 int pos)
6339{
6340 vimmenu_T *parent = menu->parent;
6341
6342 menu->submenu_id = CreatePopupMenu();
6343 menu->id = s_menu_id++;
6344
6345 if (menu_is_menubar(menu->name))
6346 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006347 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006349 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6350 {
6351 /* 'encoding' differs from active codepage: convert menu name
6352 * and use wide function */
6353 wn = enc_to_utf16(menu->name, NULL);
6354 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006356 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006358 infow.cbSize = sizeof(infow);
6359 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6360 | MIIM_SUBMENU;
6361 infow.dwItemData = (long_u)menu;
6362 infow.wID = menu->id;
6363 infow.fType = MFT_STRING;
6364 infow.dwTypeData = wn;
6365 infow.cch = (UINT)wcslen(wn);
6366 infow.hSubMenu = menu->submenu_id;
6367 InsertMenuItemW((parent == NULL)
6368 ? s_menuBar : parent->submenu_id,
6369 (UINT)pos, TRUE, &infow);
6370 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006374 if (wn == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006375 {
6376 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006378 info.cbSize = sizeof(info);
6379 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6380 info.dwItemData = (long_u)menu;
6381 info.wID = menu->id;
6382 info.fType = MFT_STRING;
6383 info.dwTypeData = (LPTSTR)menu->name;
6384 info.cch = (UINT)STRLEN(menu->name);
6385 info.hSubMenu = menu->submenu_id;
6386 InsertMenuItem((parent == NULL)
6387 ? s_menuBar : parent->submenu_id,
6388 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 }
6390 }
6391
6392 /* Fix window size if menu may have wrapped */
6393 if (parent == NULL)
6394 gui_mswin_get_menu_height(!gui.starting);
6395#ifdef FEAT_TEAROFF
6396 else if (IsWindow(parent->tearoff_handle))
6397 rebuild_tearoff(parent);
6398#endif
6399}
6400
6401 void
6402gui_mch_show_popupmenu(vimmenu_T *menu)
6403{
6404 POINT mp;
6405
6406 (void)GetCursorPos((LPPOINT)&mp);
6407 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6408}
6409
6410 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006411gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 vimmenu_T *menu = gui_find_menu(path_name);
6414
6415 if (menu != NULL)
6416 {
6417 POINT p;
6418
6419 /* Find the position of the current cursor */
6420 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006421 if (mouse_pos)
6422 {
6423 int mx, my;
6424
6425 gui_mch_getmouse(&mx, &my);
6426 p.x += mx;
6427 p.y += my;
6428 }
6429 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006431 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6433 }
6434 msg_scroll = FALSE;
6435 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6436 }
6437}
6438
6439#if defined(FEAT_TEAROFF) || defined(PROTO)
6440/*
6441 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6442 * create it as a pseudo-"tearoff menu".
6443 */
6444 void
6445gui_make_tearoff(char_u *path_name)
6446{
6447 vimmenu_T *menu = gui_find_menu(path_name);
6448
6449 /* Found the menu, so tear it off. */
6450 if (menu != NULL)
6451 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6452}
6453#endif
6454
6455/*
6456 * Add a menu item to a menu
6457 */
6458 void
6459gui_mch_add_menu_item(
6460 vimmenu_T *menu,
6461 int idx)
6462{
6463 vimmenu_T *parent = menu->parent;
6464
6465 menu->id = s_menu_id++;
6466 menu->submenu_id = NULL;
6467
6468#ifdef FEAT_TEAROFF
6469 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6470 {
6471 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6472 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6473 }
6474 else
6475#endif
6476#ifdef FEAT_TOOLBAR
6477 if (menu_is_toolbar(parent->name))
6478 {
6479 TBBUTTON newtb;
6480
6481 vim_memset(&newtb, 0, sizeof(newtb));
6482 if (menu_is_separator(menu->name))
6483 {
6484 newtb.iBitmap = 0;
6485 newtb.fsStyle = TBSTYLE_SEP;
6486 }
6487 else
6488 {
6489 newtb.iBitmap = get_toolbar_bitmap(menu);
6490 newtb.fsStyle = TBSTYLE_BUTTON;
6491 }
6492 newtb.idCommand = menu->id;
6493 newtb.fsState = TBSTATE_ENABLED;
6494 newtb.iString = 0;
6495 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6496 (LPARAM)&newtb);
6497 menu->submenu_id = (HMENU)-1;
6498 }
6499 else
6500#endif
6501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6505 {
6506 /* 'encoding' differs from active codepage: convert menu item name
6507 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006508 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (wn != NULL)
6510 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006511 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 (menu_is_separator(menu->name)
6513 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6514 (UINT)menu->id, wn);
6515 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 }
6517 }
6518 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 InsertMenu(parent->submenu_id, (UINT)idx,
6520 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6521 | MF_BYPOSITION,
6522 (UINT)menu->id, (LPCTSTR)menu->name);
6523#ifdef FEAT_TEAROFF
6524 if (IsWindow(parent->tearoff_handle))
6525 rebuild_tearoff(parent);
6526#endif
6527 }
6528}
6529
6530/*
6531 * Destroy the machine specific menu widget.
6532 */
6533 void
6534gui_mch_destroy_menu(vimmenu_T *menu)
6535{
6536#ifdef FEAT_TOOLBAR
6537 /*
6538 * is this a toolbar button?
6539 */
6540 if (menu->submenu_id == (HMENU)-1)
6541 {
6542 int iButton;
6543
6544 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6545 (WPARAM)menu->id, 0);
6546 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6547 }
6548 else
6549#endif
6550 {
6551 if (menu->parent != NULL
6552 && menu_is_popup(menu->parent->dname)
6553 && menu->parent->submenu_id != NULL)
6554 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6555 else
6556 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6557 if (menu->submenu_id != NULL)
6558 DestroyMenu(menu->submenu_id);
6559#ifdef FEAT_TEAROFF
6560 if (IsWindow(menu->tearoff_handle))
6561 DestroyWindow(menu->tearoff_handle);
6562 if (menu->parent != NULL
6563 && menu->parent->children != NULL
6564 && IsWindow(menu->parent->tearoff_handle))
6565 {
6566 /* This menu must not show up when rebuilding the tearoff window. */
6567 menu->modes = 0;
6568 rebuild_tearoff(menu->parent);
6569 }
6570#endif
6571 }
6572}
6573
6574#ifdef FEAT_TEAROFF
6575 static void
6576rebuild_tearoff(vimmenu_T *menu)
6577{
6578 /*hackish*/
6579 char_u tbuf[128];
6580 RECT trect;
6581 RECT rct;
6582 RECT roct;
6583 int x, y;
6584
6585 HWND thwnd = menu->tearoff_handle;
6586
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006587 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 if (GetWindowRect(thwnd, &trect)
6589 && GetWindowRect(s_hwnd, &rct)
6590 && GetClientRect(s_hwnd, &roct))
6591 {
6592 x = trect.left - rct.left;
6593 y = (trect.top - rct.bottom + roct.bottom);
6594 }
6595 else
6596 {
6597 x = y = 0xffffL;
6598 }
6599 DestroyWindow(thwnd);
6600 if (menu->children != NULL)
6601 {
6602 gui_mch_tearoff(tbuf, menu, x, y);
6603 if (IsWindow(menu->tearoff_handle))
6604 (void) SetWindowPos(menu->tearoff_handle,
6605 NULL,
6606 (int)trect.left,
6607 (int)trect.top,
6608 0, 0,
6609 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6610 }
6611}
6612#endif /* FEAT_TEAROFF */
6613
6614/*
6615 * Make a menu either grey or not grey.
6616 */
6617 void
6618gui_mch_menu_grey(
6619 vimmenu_T *menu,
6620 int grey)
6621{
6622#ifdef FEAT_TOOLBAR
6623 /*
6624 * is this a toolbar button?
6625 */
6626 if (menu->submenu_id == (HMENU)-1)
6627 {
6628 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6629 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6630 }
6631 else
6632#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006633 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6634 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636#ifdef FEAT_TEAROFF
6637 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6638 {
6639 WORD menuID;
6640 HWND menuHandle;
6641
6642 /*
6643 * A tearoff button has changed state.
6644 */
6645 if (menu->children == NULL)
6646 menuID = (WORD)(menu->id);
6647 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006648 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6650 if (menuHandle)
6651 EnableWindow(menuHandle, !grey);
6652
6653 }
6654#endif
6655}
6656
6657#endif /* FEAT_MENU */
6658
6659
6660/* define some macros used to make the dialogue creation more readable */
6661
6662#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6663#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006664#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6667/*
6668 * stuff for dialogs
6669 */
6670
6671/*
6672 * The callback routine used by all the dialogs. Very simple. First,
6673 * acknowledges the INITDIALOG message so that Windows knows to do standard
6674 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6675 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6676 * number.
6677 */
6678 static LRESULT CALLBACK
6679dialog_callback(
6680 HWND hwnd,
6681 UINT message,
6682 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006683 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 if (message == WM_INITDIALOG)
6686 {
6687 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6688 /* Set focus to the dialog. Set the default button, if specified. */
6689 (void)SetFocus(hwnd);
6690 if (dialog_default_button > IDCANCEL)
6691 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006692 else
6693 /* We don't have a default, set focus on another element of the
6694 * dialog window, probably the icon */
6695 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 return FALSE;
6697 }
6698
6699 if (message == WM_COMMAND)
6700 {
6701 int button = LOWORD(wParam);
6702
6703 /* Don't end the dialog if something was selected that was
6704 * not a button.
6705 */
6706 if (button >= DLG_NONBUTTON_CONTROL)
6707 return TRUE;
6708
6709 /* If the edit box exists, copy the string. */
6710 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006711 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006712 /* If the OS is Windows NT, and 'encoding' differs from active
6713 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006714 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006715 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006716 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6717 char_u *p;
6718
6719 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6720 p = utf16_to_enc(wp, NULL);
6721 vim_strncpy(s_textfield, p, IOSIZE);
6722 vim_free(p);
6723 vim_free(wp);
6724 }
6725 else
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006726 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006727 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
6730 /*
6731 * Need to check for IDOK because if the user just hits Return to
6732 * accept the default value, some reason this is what we get.
6733 */
6734 if (button == IDOK)
6735 {
6736 if (dialog_default_button > IDCANCEL)
6737 EndDialog(hwnd, dialog_default_button);
6738 }
6739 else
6740 EndDialog(hwnd, button - IDCANCEL);
6741 return TRUE;
6742 }
6743
6744 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6745 {
6746 EndDialog(hwnd, 0);
6747 return TRUE;
6748 }
6749 return FALSE;
6750}
6751
6752/*
6753 * Create a dialog dynamically from the parameter strings.
6754 * type = type of dialog (question, alert, etc.)
6755 * title = dialog title. may be NULL for default title.
6756 * message = text to display. Dialog sizes to accommodate it.
6757 * buttons = '\n' separated list of button captions, default first.
6758 * dfltbutton = number of default button.
6759 *
6760 * This routine returns 1 if the first button is pressed,
6761 * 2 for the second, etc.
6762 *
6763 * 0 indicates Esc was pressed.
6764 * -1 for unexpected error
6765 *
6766 * If stubbing out this fn, return 1.
6767 */
6768
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006769static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
6771 "IDR_VIM",
6772 "IDR_VIM_ERROR",
6773 "IDR_VIM_ALERT",
6774 "IDR_VIM_INFO",
6775 "IDR_VIM_QUESTION"
6776};
6777
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 int
6779gui_mch_dialog(
6780 int type,
6781 char_u *title,
6782 char_u *message,
6783 char_u *buttons,
6784 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006785 char_u *textfield,
6786 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787{
6788 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006789 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int numButtons;
6791 int *buttonWidths, *buttonPositions;
6792 int buttonYpos;
6793 int nchar, i;
6794 DWORD lStyle;
6795 int dlgwidth = 0;
6796 int dlgheight;
6797 int editboxheight;
6798 int horizWidth = 0;
6799 int msgheight;
6800 char_u *pstart;
6801 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006802 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 char_u *tbuffer;
6804 RECT rect;
6805 HWND hwnd;
6806 HDC hdc;
6807 HFONT font, oldFont;
6808 TEXTMETRIC fontInfo;
6809 int fontHeight;
6810 int textWidth, minButtonWidth, messageWidth;
6811 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006812 int maxDialogHeight;
6813 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 int vertical;
6815 int dlgPaddingX;
6816 int dlgPaddingY;
6817#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006818 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 int use_lfSysmenu = FALSE;
6820#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006821 garray_T ga;
6822 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
6824#ifndef NO_CONSOLE
6825 /* Don't output anything in silent mode ("ex -s") */
6826 if (silent_mode)
6827 return dfltbutton; /* return default option */
6828#endif
6829
Bram Moolenaar748bf032005-02-02 23:04:36 +00006830 if (s_hwnd == NULL)
6831 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 if ((type < 0) || (type > VIM_LAST_TYPE))
6834 type = 0;
6835
6836 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006837 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006838 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006839 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
6841 if (p == NULL)
6842 return -1;
6843
6844 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006845 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6847 * const.
6848 */
6849 tbuffer = vim_strsave(buttons);
6850 if (tbuffer == NULL)
6851 return -1;
6852
6853 --dfltbutton; /* Change from one-based to zero-based */
6854
6855 /* Count buttons */
6856 numButtons = 1;
6857 for (i = 0; tbuffer[i] != '\0'; i++)
6858 {
6859 if (tbuffer[i] == DLG_BUTTON_SEP)
6860 numButtons++;
6861 }
6862 if (dfltbutton >= numButtons)
6863 dfltbutton = -1;
6864
6865 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006866 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 if (buttonWidths == NULL)
6868 return -1;
6869
6870 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006871 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (buttonPositions == NULL)
6873 return -1;
6874
6875 /*
6876 * Calculate how big the dialog must be.
6877 */
6878 hwnd = GetDesktopWindow();
6879 hdc = GetWindowDC(hwnd);
6880#ifdef USE_SYSMENU_FONT
6881 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6882 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006883 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 use_lfSysmenu = TRUE;
6885 }
6886 else
6887#endif
6888 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6889 VARIABLE_PITCH , DLG_FONT_NAME);
6890 if (s_usenewlook)
6891 {
6892 oldFont = SelectFont(hdc, font);
6893 dlgPaddingX = DLG_PADDING_X;
6894 dlgPaddingY = DLG_PADDING_Y;
6895 }
6896 else
6897 {
6898 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6899 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6900 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6901 }
6902 GetTextMetrics(hdc, &fontInfo);
6903 fontHeight = fontInfo.tmHeight;
6904
6905 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006906 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006909 if (s_hwnd == NULL)
6910 {
6911 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
Bram Moolenaarc716c302006-01-21 22:12:51 +00006913 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006914 get_work_area(&workarea_rect);
6915 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6916 if (maxDialogWidth > 600)
6917 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006918 /* Leave some room for the taskbar. */
6919 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006920 }
6921 else
6922 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02006923 /* Use our own window for the size, unless it's very small. */
6924 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006925 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006926 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006927 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006928 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6929 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006930
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006931 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006932 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006933 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006934 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6936 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6937 }
6938
6939 /* Set dlgwidth to width of message.
6940 * Copy the message into "ga", changing NL to CR-NL and inserting line
6941 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 pstart = message;
6943 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944 msgheight = 0;
6945 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 do
6947 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948 msgheight += fontHeight; /* at least one line */
6949
6950 /* Need to figure out where to break the string. The system does it
6951 * at a word boundary, which would mean we can't compute the number of
6952 * wrapped lines. */
6953 textWidth = 0;
6954 last_white = NULL;
6955 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006956 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006957 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006958 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006959 && textWidth > maxDialogWidth * 3 / 4)
6960 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006961 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006962 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006963 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006964 /* Line will wrap. */
6965 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006966 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006967 textWidth = 0;
6968
6969 if (last_white != NULL)
6970 {
6971 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006972 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006973 pend = last_white + 1;
6974 last_white = NULL;
6975 }
6976 ga_append(&ga, '\r');
6977 ga_append(&ga, '\n');
6978 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006979 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006980
6981 while (--l >= 0)
6982 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006983 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006986
6987 ga_append(&ga, '\r');
6988 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 pstart = pend + 1;
6990 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006991
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006992 if (ga.ga_data != NULL)
6993 message = ga.ga_data;
6994
Bram Moolenaar748bf032005-02-02 23:04:36 +00006995 messageWidth += 10; /* roundoff space */
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02006998 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6999 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 if (msgheight < DLG_ICON_HEIGHT)
7002 msgheight = DLG_ICON_HEIGHT;
7003
7004 /*
7005 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007006 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007008 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 if (!vertical)
7010 {
7011 // Place buttons horizontally if they fit.
7012 horizWidth = dlgPaddingX;
7013 pstart = tbuffer;
7014 i = 0;
7015 do
7016 {
7017 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7018 if (pend == NULL)
7019 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007020 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 if (textWidth < minButtonWidth)
7022 textWidth = minButtonWidth;
7023 textWidth += dlgPaddingX; /* Padding within button */
7024 buttonWidths[i] = textWidth;
7025 buttonPositions[i++] = horizWidth;
7026 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7027 pstart = pend + 1;
7028 } while (*pend != NUL);
7029
7030 if (horizWidth > maxDialogWidth)
7031 vertical = TRUE; // Too wide to fit on the screen.
7032 else if (horizWidth > dlgwidth)
7033 dlgwidth = horizWidth;
7034 }
7035
7036 if (vertical)
7037 {
7038 // Stack buttons vertically.
7039 pstart = tbuffer;
7040 do
7041 {
7042 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7043 if (pend == NULL)
7044 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007045 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 textWidth += dlgPaddingX; /* Padding within button */
7047 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7048 if (textWidth > dlgwidth)
7049 dlgwidth = textWidth;
7050 pstart = pend + 1;
7051 } while (*pend != NUL);
7052 }
7053
7054 if (dlgwidth < DLG_MIN_WIDTH)
7055 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7056
7057 /* start to fill in the dlgtemplate information. addressing by WORDs */
7058 if (s_usenewlook)
7059 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7060 else
7061 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7062
7063 add_long(lStyle);
7064 add_long(0); // (lExtendedStyle)
7065 pnumitems = p; /*save where the number of items must be stored*/
7066 add_word(0); // NumberOfItems(will change later)
7067 add_word(10); // x
7068 add_word(10); // y
7069 add_word(PixelToDialogX(dlgwidth)); // cx
7070
7071 // Dialog height.
7072 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007073 dlgheight = msgheight + 2 * dlgPaddingY
7074 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 else
7076 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7077
7078 // Dialog needs to be taller if contains an edit box.
7079 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7080 if (textfield != NULL)
7081 dlgheight += editboxheight;
7082
Bram Moolenaara95d8232013-08-07 15:27:11 +02007083 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7084 if (dlgheight > maxDialogHeight)
7085 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007086 msgheight = msgheight - (dlgheight - maxDialogHeight);
7087 dlgheight = maxDialogHeight;
7088 scroll_flag = WS_VSCROLL;
7089 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7090 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007091 }
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 add_word(PixelToDialogY(dlgheight));
7094
7095 add_word(0); // Menu
7096 add_word(0); // Class
7097
7098 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007099 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7100 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 p += nchar;
7102
7103 if (s_usenewlook)
7104 {
7105 /* do the font, since DS_3DLOOK doesn't work properly */
7106#ifdef USE_SYSMENU_FONT
7107 if (use_lfSysmenu)
7108 {
7109 /* point size */
7110 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7111 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007112 wcscpy(p, lfSysmenu.lfFaceName);
7113 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115 else
7116#endif
7117 {
7118 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007119 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
7121 p += nchar;
7122 }
7123
7124 buttonYpos = msgheight + 2 * dlgPaddingY;
7125
7126 if (textfield != NULL)
7127 buttonYpos += editboxheight;
7128
7129 pstart = tbuffer;
7130 if (!vertical)
7131 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7132 for (i = 0; i < numButtons; i++)
7133 {
7134 /* get end of this button. */
7135 for ( pend = pstart;
7136 *pend && (*pend != DLG_BUTTON_SEP);
7137 pend++)
7138 ;
7139
7140 if (*pend)
7141 *pend = '\0';
7142
7143 /*
7144 * old NOTE:
7145 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7146 * the focus to the first tab-able button and in so doing makes that
7147 * the default!! Grrr. Workaround: Make the default button the only
7148 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7149 * he/she can use arrow keys.
7150 *
7151 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007152 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 * dialog. Also needed for when the textfield is the default control.
7154 * It appears to work now (perhaps not on Win95?).
7155 */
7156 if (vertical)
7157 {
7158 p = add_dialog_element(p,
7159 (i == dfltbutton
7160 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7161 PixelToDialogX(DLG_VERT_PADDING_X),
7162 PixelToDialogY(buttonYpos /* TBK */
7163 + 2 * fontHeight * i),
7164 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7165 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007166 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 else
7169 {
7170 p = add_dialog_element(p,
7171 (i == dfltbutton
7172 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7173 PixelToDialogX(horizWidth + buttonPositions[i]),
7174 PixelToDialogY(buttonYpos), /* TBK */
7175 PixelToDialogX(buttonWidths[i]),
7176 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007177 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 pstart = pend + 1; /*next button*/
7180 }
7181 *pnumitems += numButtons;
7182
7183 /* Vim icon */
7184 p = add_dialog_element(p, SS_ICON,
7185 PixelToDialogX(dlgPaddingX),
7186 PixelToDialogY(dlgPaddingY),
7187 PixelToDialogX(DLG_ICON_WIDTH),
7188 PixelToDialogY(DLG_ICON_HEIGHT),
7189 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7190 dlg_icons[type]);
7191
Bram Moolenaar748bf032005-02-02 23:04:36 +00007192 /* Dialog message */
7193 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7194 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7195 PixelToDialogY(dlgPaddingY),
7196 (WORD)(PixelToDialogX(messageWidth) + 1),
7197 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007198 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200 /* Edit box */
7201 if (textfield != NULL)
7202 {
7203 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7204 PixelToDialogX(2 * dlgPaddingX),
7205 PixelToDialogY(2 * dlgPaddingY + msgheight),
7206 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7207 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007208 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 *pnumitems += 1;
7210 }
7211
7212 *pnumitems += 2;
7213
7214 SelectFont(hdc, oldFont);
7215 DeleteObject(font);
7216 ReleaseDC(hwnd, hdc);
7217
7218 /* Let the dialog_callback() function know which button to make default
7219 * If we have an edit box, make that the default. We also need to tell
7220 * dialog_callback() if this dialog contains an edit box or not. We do
7221 * this by setting s_textfield if it does.
7222 */
7223 if (textfield != NULL)
7224 {
7225 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7226 s_textfield = textfield;
7227 }
7228 else
7229 {
7230 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7231 s_textfield = NULL;
7232 }
7233
7234 /* show the dialog box modally and get a return value */
7235 nchar = (int)DialogBoxIndirect(
7236 s_hinst,
7237 (LPDLGTEMPLATE)pdlgtemplate,
7238 s_hwnd,
7239 (DLGPROC)dialog_callback);
7240
7241 LocalFree(LocalHandle(pdlgtemplate));
7242 vim_free(tbuffer);
7243 vim_free(buttonWidths);
7244 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007245 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 /* Focus back to our window (for when MDI is used). */
7248 (void)SetFocus(s_hwnd);
7249
7250 return nchar;
7251}
7252
7253#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255/*
7256 * Put a simple element (basic class) onto a dialog template in memory.
7257 * return a pointer to where the next item should be added.
7258 *
7259 * parameters:
7260 * lStyle = additional style flags
7261 * (be careful, NT3.51 & Win32s will ignore the new ones)
7262 * x,y = x & y positions IN DIALOG UNITS
7263 * w,h = width and height IN DIALOG UNITS
7264 * Id = ID used in messages
7265 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7266 * caption = usually text or resource name
7267 *
7268 * TODO: use the length information noted here to enable the dialog creation
7269 * routines to work out more exactly how much memory they need to alloc.
7270 */
7271 static PWORD
7272add_dialog_element(
7273 PWORD p,
7274 DWORD lStyle,
7275 WORD x,
7276 WORD y,
7277 WORD w,
7278 WORD h,
7279 WORD Id,
7280 WORD clss,
7281 const char *caption)
7282{
7283 int nchar;
7284
7285 p = lpwAlign(p); /* Align to dword boundary*/
7286 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7287 *p++ = LOWORD(lStyle);
7288 *p++ = HIWORD(lStyle);
7289 *p++ = 0; // LOWORD (lExtendedStyle)
7290 *p++ = 0; // HIWORD (lExtendedStyle)
7291 *p++ = x;
7292 *p++ = y;
7293 *p++ = w;
7294 *p++ = h;
7295 *p++ = Id; //9 or 10 words in all
7296
7297 *p++ = (WORD)0xffff;
7298 *p++ = clss; //2 more here
7299
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007300 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 p += nchar;
7302
7303 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7304
7305 return p; //total = 15+ (strlen(caption)) words
7306 // = 30 + 2(strlen(caption) bytes reqd
7307}
7308
7309
7310/*
7311 * Helper routine. Take an input pointer, return closest pointer that is
7312 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7313 */
7314 static LPWORD
7315lpwAlign(
7316 LPWORD lpIn)
7317{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007318 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007320 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 ul += 3;
7322 ul >>= 2;
7323 ul <<= 2;
7324 return (LPWORD)ul;
7325}
7326
7327/*
7328 * Helper routine. Takes second parameter as Ansi string, copies it to first
7329 * parameter as wide character (16-bits / char) string, and returns integer
7330 * number of wide characters (words) in string (including the trailing wide
7331 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007332 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7333 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 static int
7335nCopyAnsiToWideChar(
7336 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007337 LPSTR lpAnsiIn,
7338 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7342 int i;
7343 WCHAR *wn;
7344
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007345 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007348 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 if (wn != NULL)
7350 {
7351 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007352 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 vim_free(wn);
7354 }
7355 }
7356 if (nChar == 0)
7357 /* Use Win32 conversion function. */
7358 nChar = MultiByteToWideChar(
7359 enc_codepage > 0 ? enc_codepage : CP_ACP,
7360 MB_PRECOMPOSED,
7361 lpAnsiIn, len,
7362 lpWCStr, len);
7363 for (i = 0; i < nChar; ++i)
7364 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7365 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367 return nChar;
7368}
7369
7370
7371#ifdef FEAT_TEAROFF
7372/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007373 * Lookup menu handle from "menu_id".
7374 */
7375 static HMENU
7376tearoff_lookup_menuhandle(
7377 vimmenu_T *menu,
7378 WORD menu_id)
7379{
7380 for ( ; menu != NULL; menu = menu->next)
7381 {
7382 if (menu->modes == 0) /* this menu has just been deleted */
7383 continue;
7384 if (menu_is_separator(menu->dname))
7385 continue;
7386 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7387 return menu->submenu_id;
7388 }
7389 return NULL;
7390}
7391
7392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 * The callback function for all the modeless dialogs that make up the
7394 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7395 * thinking its menus have been clicked), and go away when closed.
7396 */
7397 static LRESULT CALLBACK
7398tearoff_callback(
7399 HWND hwnd,
7400 UINT message,
7401 WPARAM wParam,
7402 LPARAM lParam)
7403{
7404 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007405 {
7406 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 /* May show the mouse pointer again. */
7411 HandleMouseHide(message, lParam);
7412
7413 if (message == WM_COMMAND)
7414 {
7415 if ((WORD)(LOWORD(wParam)) & 0x8000)
7416 {
7417 POINT mp;
7418 RECT rect;
7419
7420 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7421 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007422 vimmenu_T *menu;
7423
7424 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007426 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7428 (int)rect.right - 8,
7429 (int)mp.y,
7430 (int)0, /*reserved param*/
7431 s_hwnd,
7432 NULL);
7433 /*
7434 * NOTE: The pop-up menu can eat the mouse up event.
7435 * We deal with this in normal.c.
7436 */
7437 }
7438 }
7439 else
7440 /* Pass on messages to the main Vim window */
7441 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7442 /*
7443 * Give main window the focus back: this is so after
7444 * choosing a tearoff button you can start typing again
7445 * straight away.
7446 */
7447 (void)SetFocus(s_hwnd);
7448 return TRUE;
7449 }
7450 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7451 {
7452 DestroyWindow(hwnd);
7453 return TRUE;
7454 }
7455
7456 /* When moved around, give main window the focus back. */
7457 if (message == WM_EXITSIZEMOVE)
7458 (void)SetActiveWindow(s_hwnd);
7459
7460 return FALSE;
7461}
7462#endif
7463
7464
7465/*
7466 * Decide whether to use the "new look" (small, non-bold font) or the "old
7467 * look" (big, clanky font) for dialogs, and work out a few values for use
7468 * later accordingly.
7469 */
7470 static void
7471get_dialog_font_metrics(void)
7472{
7473 HDC hdc;
7474 HFONT hfontTools = 0;
7475 DWORD dlgFontSize;
7476 SIZE size;
7477#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007478 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479#endif
7480
7481 s_usenewlook = FALSE;
7482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007484 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007485 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487#endif
7488 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7489 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7490
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007491 if (hfontTools)
7492 {
7493 hdc = GetDC(s_hwnd);
7494 SelectObject(hdc, hfontTools);
7495 /*
7496 * GetTextMetrics() doesn't return the right value in
7497 * tmAveCharWidth, so we have to figure out the dialog base units
7498 * ourselves.
7499 */
7500 GetTextExtentPoint(hdc,
7501 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7502 52, &size);
7503 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007505 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7506 s_dlgfntheight = (WORD)size.cy;
7507 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
7509
7510 if (!s_usenewlook)
7511 {
7512 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7513 s_dlgfntwidth = LOWORD(dlgFontSize);
7514 s_dlgfntheight = HIWORD(dlgFontSize);
7515 }
7516}
7517
7518#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7519/*
7520 * Create a pseudo-"tearoff menu" based on the child
7521 * items of a given menu pointer.
7522 */
7523 static void
7524gui_mch_tearoff(
7525 char_u *title,
7526 vimmenu_T *menu,
7527 int initX,
7528 int initY)
7529{
7530 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7531 int template_len;
7532 int nchar, textWidth, submenuWidth;
7533 DWORD lStyle;
7534 DWORD lExtendedStyle;
7535 WORD dlgwidth;
7536 WORD menuID;
7537 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007538 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 vimmenu_T *the_menu = menu;
7540 HWND hwnd;
7541 HDC hdc;
7542 HFONT font, oldFont;
7543 int col, spaceWidth, len;
7544 int columnWidths[2];
7545 char_u *label, *text;
7546 int acLen = 0;
7547 int nameLen;
7548 int padding0, padding1, padding2 = 0;
7549 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007550 int x;
7551 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007553 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 int use_lfSysmenu = FALSE;
7555#endif
7556
7557 /*
7558 * If this menu is already torn off, move it to the mouse position.
7559 */
7560 if (IsWindow(menu->tearoff_handle))
7561 {
7562 POINT mp;
7563 if (GetCursorPos((LPPOINT)&mp))
7564 {
7565 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7566 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7567 }
7568 return;
7569 }
7570
7571 /*
7572 * Create a new tearoff.
7573 */
7574 if (*title == MNU_HIDDEN_CHAR)
7575 title++;
7576
7577 /* Allocate memory to store the dialog template. It's made bigger when
7578 * needed. */
7579 template_len = DLG_ALLOC_SIZE;
7580 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7581 if (p == NULL)
7582 return;
7583
7584 hwnd = GetDesktopWindow();
7585 hdc = GetWindowDC(hwnd);
7586#ifdef USE_SYSMENU_FONT
7587 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7588 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007589 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 use_lfSysmenu = TRUE;
7591 }
7592 else
7593#endif
7594 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7595 VARIABLE_PITCH , DLG_FONT_NAME);
7596 if (s_usenewlook)
7597 oldFont = SelectFont(hdc, font);
7598 else
7599 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7600
7601 /* Calculate width of a single space. Used for padding columns to the
7602 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007603 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
7605 /* Figure out max width of the text column, the accelerator column and the
7606 * optional submenu column. */
7607 submenuWidth = 0;
7608 for (col = 0; col < 2; col++)
7609 {
7610 columnWidths[col] = 0;
7611 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7612 {
7613 /* Use "dname" here to compute the width of the visible text. */
7614 text = (col == 0) ? pmenu->dname : pmenu->actext;
7615 if (text != NULL && *text != NUL)
7616 {
7617 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7618 if (textWidth > columnWidths[col])
7619 columnWidths[col] = textWidth;
7620 }
7621 if (pmenu->children != NULL)
7622 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7623 }
7624 }
7625 if (columnWidths[1] == 0)
7626 {
7627 /* no accelerators */
7628 if (submenuWidth != 0)
7629 columnWidths[0] += submenuWidth;
7630 else
7631 columnWidths[0] += spaceWidth;
7632 }
7633 else
7634 {
7635 /* there is an accelerator column */
7636 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7637 columnWidths[1] += submenuWidth;
7638 }
7639
7640 /*
7641 * Now find the total width of our 'menu'.
7642 */
7643 textWidth = columnWidths[0] + columnWidths[1];
7644 if (submenuWidth != 0)
7645 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007646 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7648 textWidth += submenuWidth;
7649 }
7650 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7651 if (textWidth > dlgwidth)
7652 dlgwidth = textWidth;
7653 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7654
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 /* start to fill in the dlgtemplate information. addressing by WORDs */
7656 if (s_usenewlook)
7657 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7658 else
7659 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7660
7661 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7662 *p++ = LOWORD(lStyle);
7663 *p++ = HIWORD(lStyle);
7664 *p++ = LOWORD(lExtendedStyle);
7665 *p++ = HIWORD(lExtendedStyle);
7666 pnumitems = p; /* save where the number of items must be stored */
7667 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007668 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007670 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 else
7672 *p++ = PixelToDialogX(initX); // x
7673 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007674 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 else
7676 *p++ = PixelToDialogY(initY); // y
7677 *p++ = PixelToDialogX(dlgwidth); // cx
7678 ptrueheight = p;
7679 *p++ = 0; // dialog height: changed later anyway
7680 *p++ = 0; // Menu
7681 *p++ = 0; // Class
7682
7683 /* copy the title of the dialog */
7684 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007685 ? (LPSTR)title
7686 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 p += nchar;
7688
7689 if (s_usenewlook)
7690 {
7691 /* do the font, since DS_3DLOOK doesn't work properly */
7692#ifdef USE_SYSMENU_FONT
7693 if (use_lfSysmenu)
7694 {
7695 /* point size */
7696 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7697 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007698 wcscpy(p, lfSysmenu.lfFaceName);
7699 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 else
7702#endif
7703 {
7704 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007705 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 }
7707 p += nchar;
7708 }
7709
7710 /*
7711 * Loop over all the items in the menu.
7712 * But skip over the tearbar.
7713 */
7714 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7715 menu = menu->children->next;
7716 else
7717 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007718 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 for ( ; menu != NULL; menu = menu->next)
7720 {
7721 if (menu->modes == 0) /* this menu has just been deleted */
7722 continue;
7723 if (menu_is_separator(menu->dname))
7724 {
7725 sepPadding += 3;
7726 continue;
7727 }
7728
7729 /* Check if there still is plenty of room in the template. Make it
7730 * larger when needed. */
7731 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7732 {
7733 WORD *newp;
7734
7735 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7736 if (newp != NULL)
7737 {
7738 template_len += 4096;
7739 mch_memmove(newp, pdlgtemplate,
7740 (char *)p - (char *)pdlgtemplate);
7741 p = newp + (p - pdlgtemplate);
7742 pnumitems = newp + (pnumitems - pdlgtemplate);
7743 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7744 LocalFree(LocalHandle(pdlgtemplate));
7745 pdlgtemplate = newp;
7746 }
7747 }
7748
7749 /* Figure out minimal length of this menu label. Use "name" for the
7750 * actual text, "dname" for estimating the displayed size. "name"
7751 * has "&a" for mnemonic and includes the accelerator. */
7752 len = nameLen = (int)STRLEN(menu->name);
7753 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7754 (int)STRLEN(menu->dname))) / spaceWidth;
7755 len += padding0;
7756
7757 if (menu->actext != NULL)
7758 {
7759 acLen = (int)STRLEN(menu->actext);
7760 len += acLen;
7761 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7762 }
7763 else
7764 textWidth = 0;
7765 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7766 len += padding1;
7767
7768 if (menu->children == NULL)
7769 {
7770 padding2 = submenuWidth / spaceWidth;
7771 len += padding2;
7772 menuID = (WORD)(menu->id);
7773 }
7774 else
7775 {
7776 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007777 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
7779
7780 /* Allocate menu label and fill it in */
7781 text = label = alloc((unsigned)len + 1);
7782 if (label == NULL)
7783 break;
7784
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007785 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7787 if (text == NULL)
7788 text = label + nameLen; /* no actext, use whole name */
7789 while (padding0-- > 0)
7790 *text++ = ' ';
7791 if (menu->actext != NULL)
7792 {
7793 STRNCPY(text, menu->actext, acLen);
7794 text += acLen;
7795 }
7796 while (padding1-- > 0)
7797 *text++ = ' ';
7798 if (menu->children != NULL)
7799 {
7800 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7801 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7802 }
7803 else
7804 {
7805 while (padding2-- > 0)
7806 *text++ = ' ';
7807 }
7808 *text = NUL;
7809
7810 /*
7811 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7812 * W95/NT4 it makes the tear-off look more like a menu.
7813 */
7814 p = add_dialog_element(p,
7815 BS_PUSHBUTTON|BS_LEFT,
7816 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7817 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7818 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7819 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007820 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 vim_free(label);
7822 (*pnumitems)++;
7823 }
7824
7825 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7826
7827
7828 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007829 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 s_hinst,
7831 (LPDLGTEMPLATE)pdlgtemplate,
7832 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007833 (DLGPROC)tearoff_callback,
7834 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835
7836 LocalFree(LocalHandle(pdlgtemplate));
7837 SelectFont(hdc, oldFont);
7838 DeleteObject(font);
7839 ReleaseDC(hwnd, hdc);
7840
7841 /*
7842 * Reassert ourselves as the active window. This is so that after creating
7843 * a tearoff, the user doesn't have to click with the mouse just to start
7844 * typing again!
7845 */
7846 (void)SetActiveWindow(s_hwnd);
7847
7848 /* make sure the right buttons are enabled */
7849 force_menu_update = TRUE;
7850}
7851#endif
7852
7853#if defined(FEAT_TOOLBAR) || defined(PROTO)
7854#include "gui_w32_rc.h"
7855
7856/* This not defined in older SDKs */
7857# ifndef TBSTYLE_FLAT
7858# define TBSTYLE_FLAT 0x0800
7859# endif
7860
7861/*
7862 * Create the toolbar, initially unpopulated.
7863 * (just like the menu, there are no defaults, it's all
7864 * set up through menu.vim)
7865 */
7866 static void
7867initialise_toolbar(void)
7868{
7869 InitCommonControls();
7870 s_toolbarhwnd = CreateToolbarEx(
7871 s_hwnd,
7872 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7873 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007874 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 s_hinst,
7876 IDR_TOOLBAR1, // id of initial bitmap
7877 NULL,
7878 0, // initial number of buttons
7879 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7880 TOOLBAR_BUTTON_HEIGHT,
7881 TOOLBAR_BUTTON_WIDTH,
7882 TOOLBAR_BUTTON_HEIGHT,
7883 sizeof(TBBUTTON)
7884 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007885 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886
7887 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7888}
7889
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007890 static LRESULT CALLBACK
7891toolbar_wndproc(
7892 HWND hwnd,
7893 UINT uMsg,
7894 WPARAM wParam,
7895 LPARAM lParam)
7896{
7897 HandleMouseHide(uMsg, lParam);
7898 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7899}
7900
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 static int
7902get_toolbar_bitmap(vimmenu_T *menu)
7903{
7904 int i = -1;
7905
7906 /*
7907 * Check user bitmaps first, unless builtin is specified.
7908 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007909 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 {
7911 char_u fname[MAXPATHL];
7912 HANDLE hbitmap = NULL;
7913
7914 if (menu->iconfile != NULL)
7915 {
7916 gui_find_iconfile(menu->iconfile, fname, "bmp");
7917 hbitmap = LoadImage(
7918 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007919 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 IMAGE_BITMAP,
7921 TOOLBAR_BUTTON_WIDTH,
7922 TOOLBAR_BUTTON_HEIGHT,
7923 LR_LOADFROMFILE |
7924 LR_LOADMAP3DCOLORS
7925 );
7926 }
7927
7928 /*
7929 * If the LoadImage call failed, or the "icon=" file
7930 * didn't exist or wasn't specified, try the menu name
7931 */
7932 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007933 && (gui_find_bitmap(
7934#ifdef FEAT_MULTI_LANG
7935 menu->en_dname != NULL ? menu->en_dname :
7936#endif
7937 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 hbitmap = LoadImage(
7939 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007940 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 IMAGE_BITMAP,
7942 TOOLBAR_BUTTON_WIDTH,
7943 TOOLBAR_BUTTON_HEIGHT,
7944 LR_LOADFROMFILE |
7945 LR_LOADMAP3DCOLORS
7946 );
7947
7948 if (hbitmap != NULL)
7949 {
7950 TBADDBITMAP tbAddBitmap;
7951
7952 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007953 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7956 (WPARAM)1, (LPARAM)&tbAddBitmap);
7957 /* i will be set to -1 if it fails */
7958 }
7959 }
7960 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7961 i = menu->iconidx;
7962
7963 return i;
7964}
7965#endif
7966
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007967#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7968 static void
7969initialise_tabline(void)
7970{
7971 InitCommonControls();
7972
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007973 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007974 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007975 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
7976 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007977 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007978
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007979 gui.tabline_height = TABLINE_HEIGHT;
7980
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007981# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007982 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007983# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007984}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007985
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007986/*
7987 * Get tabpage_T from POINT.
7988 */
7989 static tabpage_T *
7990GetTabFromPoint(
7991 HWND hWnd,
7992 POINT pt)
7993{
7994 tabpage_T *ptp = NULL;
7995
7996 if (gui_mch_showing_tabline())
7997 {
7998 TCHITTESTINFO htinfo;
7999 htinfo.pt = pt;
8000 /* ignore if a window under cusor is not tabcontrol. */
8001 if (s_tabhwnd == hWnd)
8002 {
8003 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8004 if (idx != -1)
8005 ptp = find_tabpage(idx + 1);
8006 }
8007 }
8008 return ptp;
8009}
8010
8011static POINT s_pt = {0, 0};
8012static HCURSOR s_hCursor = NULL;
8013
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008014 static LRESULT CALLBACK
8015tabline_wndproc(
8016 HWND hwnd,
8017 UINT uMsg,
8018 WPARAM wParam,
8019 LPARAM lParam)
8020{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008021 POINT pt;
8022 tabpage_T *tp;
8023 RECT rect;
8024 int nCenter;
8025 int idx0;
8026 int idx1;
8027
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008028 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008029
8030 switch (uMsg)
8031 {
8032 case WM_LBUTTONDOWN:
8033 {
8034 s_pt.x = GET_X_LPARAM(lParam);
8035 s_pt.y = GET_Y_LPARAM(lParam);
8036 SetCapture(hwnd);
8037 s_hCursor = GetCursor(); /* backup default cursor */
8038 break;
8039 }
8040 case WM_MOUSEMOVE:
8041 if (GetCapture() == hwnd
8042 && ((wParam & MK_LBUTTON)) != 0)
8043 {
8044 pt.x = GET_X_LPARAM(lParam);
8045 pt.y = s_pt.y;
8046 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8047 {
8048 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8049
8050 tp = GetTabFromPoint(hwnd, pt);
8051 if (tp != NULL)
8052 {
8053 idx0 = tabpage_index(curtab) - 1;
8054 idx1 = tabpage_index(tp) - 1;
8055
8056 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8057 nCenter = rect.left + (rect.right - rect.left) / 2;
8058
8059 /* Check if the mouse cursor goes over the center of
8060 * the next tab to prevent "flickering". */
8061 if ((idx0 < idx1) && (nCenter < pt.x))
8062 {
8063 tabpage_move(idx1 + 1);
8064 update_screen(0);
8065 }
8066 else if ((idx1 < idx0) && (pt.x < nCenter))
8067 {
8068 tabpage_move(idx1);
8069 update_screen(0);
8070 }
8071 }
8072 }
8073 }
8074 break;
8075 case WM_LBUTTONUP:
8076 {
8077 if (GetCapture() == hwnd)
8078 {
8079 SetCursor(s_hCursor);
8080 ReleaseCapture();
8081 }
8082 break;
8083 }
8084 default:
8085 break;
8086 }
8087
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008088 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8089}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008090#endif
8091
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8093/*
8094 * Make the GUI window come to the foreground.
8095 */
8096 void
8097gui_mch_set_foreground(void)
8098{
8099 if (IsIconic(s_hwnd))
8100 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8101 SetForegroundWindow(s_hwnd);
8102}
8103#endif
8104
8105#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8106 static void
8107dyn_imm_load(void)
8108{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008109 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (hLibImm == NULL)
8111 return;
8112
8113 pImmGetCompositionStringA
8114 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8115 pImmGetCompositionStringW
8116 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8117 pImmGetContext
8118 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8119 pImmAssociateContext
8120 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8121 pImmReleaseContext
8122 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8123 pImmGetOpenStatus
8124 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8125 pImmSetOpenStatus
8126 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008127 pImmGetCompositionFontW
8128 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8129 pImmSetCompositionFontW
8130 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 pImmSetCompositionWindow
8132 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8133 pImmGetConversionStatus
8134 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008135 pImmSetConversionStatus
8136 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137
8138 if ( pImmGetCompositionStringA == NULL
8139 || pImmGetCompositionStringW == NULL
8140 || pImmGetContext == NULL
8141 || pImmAssociateContext == NULL
8142 || pImmReleaseContext == NULL
8143 || pImmGetOpenStatus == NULL
8144 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008145 || pImmGetCompositionFontW == NULL
8146 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008148 || pImmGetConversionStatus == NULL
8149 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 FreeLibrary(hLibImm);
8152 hLibImm = NULL;
8153 pImmGetContext = NULL;
8154 return;
8155 }
8156
8157 return;
8158}
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160#endif
8161
8162#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8163
8164# ifdef FEAT_XPM_W32
8165# define IMAGE_XPM 100
8166# endif
8167
8168typedef struct _signicon_t
8169{
8170 HANDLE hImage;
8171 UINT uType;
8172#ifdef FEAT_XPM_W32
8173 HANDLE hShape; /* Mask bitmap handle */
8174#endif
8175} signicon_t;
8176
8177 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008178gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
8180 signicon_t *sign;
8181 int x, y, w, h;
8182
8183 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8184 return;
8185
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008186#if defined(FEAT_DIRECTX)
8187 if (IS_ENABLE_DIRECTX())
8188 DWriteContext_Flush(s_dwc);
8189#endif
8190
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 x = TEXT_X(col);
8192 y = TEXT_Y(row);
8193 w = gui.char_width * 2;
8194 h = gui.char_height;
8195 switch (sign->uType)
8196 {
8197 case IMAGE_BITMAP:
8198 {
8199 HDC hdcMem;
8200 HBITMAP hbmpOld;
8201
8202 hdcMem = CreateCompatibleDC(s_hdc);
8203 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8204 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8205 SelectObject(hdcMem, hbmpOld);
8206 DeleteDC(hdcMem);
8207 }
8208 break;
8209 case IMAGE_ICON:
8210 case IMAGE_CURSOR:
8211 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8212 break;
8213#ifdef FEAT_XPM_W32
8214 case IMAGE_XPM:
8215 {
8216 HDC hdcMem;
8217 HBITMAP hbmpOld;
8218
8219 hdcMem = CreateCompatibleDC(s_hdc);
8220 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8221 /* Make hole */
8222 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8223
8224 SelectObject(hdcMem, sign->hImage);
8225 /* Paint sign */
8226 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8227 SelectObject(hdcMem, hbmpOld);
8228 DeleteDC(hdcMem);
8229 }
8230 break;
8231#endif
8232 }
8233}
8234
8235 static void
8236close_signicon_image(signicon_t *sign)
8237{
8238 if (sign)
8239 switch (sign->uType)
8240 {
8241 case IMAGE_BITMAP:
8242 DeleteObject((HGDIOBJ)sign->hImage);
8243 break;
8244 case IMAGE_CURSOR:
8245 DestroyCursor((HCURSOR)sign->hImage);
8246 break;
8247 case IMAGE_ICON:
8248 DestroyIcon((HICON)sign->hImage);
8249 break;
8250#ifdef FEAT_XPM_W32
8251 case IMAGE_XPM:
8252 DeleteObject((HBITMAP)sign->hImage);
8253 DeleteObject((HBITMAP)sign->hShape);
8254 break;
8255#endif
8256 }
8257}
8258
8259 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008260gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
8262 signicon_t sign, *psign;
8263 char_u *ext;
8264
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008266 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (ext > signfile)
8268 {
8269 int do_load = 1;
8270
8271 if (!STRICMP(ext, ".bmp"))
8272 sign.uType = IMAGE_BITMAP;
8273 else if (!STRICMP(ext, ".ico"))
8274 sign.uType = IMAGE_ICON;
8275 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8276 sign.uType = IMAGE_CURSOR;
8277 else
8278 do_load = 0;
8279
8280 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008281 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 gui.char_width * 2, gui.char_height,
8283 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8284#ifdef FEAT_XPM_W32
8285 if (!STRICMP(ext, ".xpm"))
8286 {
8287 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008288 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8289 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 }
8291#endif
8292 }
8293
8294 psign = NULL;
8295 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8296 != NULL)
8297 *psign = sign;
8298
8299 if (!psign)
8300 {
8301 if (sign.hImage)
8302 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008303 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305 return (void *)psign;
8306
8307}
8308
8309 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008310gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 if (sign)
8313 {
8314 close_signicon_image((signicon_t *)sign);
8315 vim_free(sign);
8316 }
8317}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008320#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008323 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008325 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8327 * to get current mouse position).
8328 *
8329 * Trying to use as more Windows services as possible, and as less
8330 * IE version as possible :)).
8331 *
8332 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8333 * BalloonEval struct.
8334 * 2) Enable/Disable simply create/kill BalloonEval Timer
8335 * 3) When there was enough inactivity, timer procedure posts
8336 * async request to debugger
8337 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8338 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008339 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 */
8341
Bram Moolenaar45360022005-07-21 21:08:21 +00008342/*
8343 * determine whether installed Common Controls support multiline tooltips
8344 * (i.e. their version is >= 4.70
8345 */
8346 int
8347multiline_balloon_available(void)
8348{
8349 HINSTANCE hDll;
8350 static char comctl_dll[] = "comctl32.dll";
8351 static int multiline_tip = MAYBE;
8352
8353 if (multiline_tip != MAYBE)
8354 return multiline_tip;
8355
8356 hDll = GetModuleHandle(comctl_dll);
8357 if (hDll != NULL)
8358 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008359 DLLGETVERSIONPROC pGetVer;
8360 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008361
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 if (pGetVer != NULL)
8363 {
8364 DLLVERSIONINFO dvi;
8365 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008366
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008367 ZeroMemory(&dvi, sizeof(dvi));
8368 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008369
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008370 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008371
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008372 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008373 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008374 || (dvi.dwMajorVersion == 4
8375 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008376 {
8377 multiline_tip = TRUE;
8378 return multiline_tip;
8379 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008380 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008381 else
8382 {
8383 /* there is chance we have ancient CommCtl 4.70
8384 which doesn't export DllGetVersion */
8385 DWORD dwHandle = 0;
8386 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8387 if (len > 0)
8388 {
8389 VS_FIXEDFILEINFO *ver;
8390 UINT vlen = 0;
8391 void *data = alloc(len);
8392
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008393 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008394 && GetFileVersionInfo(comctl_dll, 0, len, data)
8395 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8396 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008397 && HIWORD(ver->dwFileVersionMS) > 4)
8398 || ((HIWORD(ver->dwFileVersionMS) == 4
8399 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008400 {
8401 vim_free(data);
8402 multiline_tip = TRUE;
8403 return multiline_tip;
8404 }
8405 vim_free(data);
8406 }
8407 }
8408 }
8409 multiline_tip = FALSE;
8410 return multiline_tip;
8411}
8412
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008413 static void
8414make_tooltipw(BalloonEval *beval, char *text, POINT pt)
8415{
8416 TOOLINFOW *pti;
8417 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418
8419 if (multiline_balloon_available() == TRUE)
8420 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8421 else
8422 ToolInfoSize = sizeof(TOOLINFOW);
8423
8424 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8425 if (pti == NULL)
8426 return;
8427
8428 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8429 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8430 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8431 beval->target, NULL, s_hinst, NULL);
8432
8433 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8434 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8435
8436 pti->cbSize = ToolInfoSize;
8437 pti->uFlags = TTF_SUBCLASS;
8438 pti->hwnd = beval->target;
8439 pti->hinst = 0; // Don't use string resources
8440 pti->uId = ID_BEVAL_TOOLTIP;
8441
8442 if (multiline_balloon_available() == TRUE)
8443 {
8444 RECT rect;
8445 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8446 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008447 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8448 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008449 // switch multiline tooltips on
8450 if (GetClientRect(s_textArea, &rect))
8451 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8452 (LPARAM)rect.right);
8453 }
8454 else
8455 {
8456 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008457 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8458 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008459 }
8460
8461 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8462 pti->rect.left = pt.x - 3;
8463 pti->rect.top = pt.y - 3;
8464 pti->rect.right = pt.x + 3;
8465 pti->rect.bottom = pt.y + 3;
8466
8467 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8468 // Make tooltip appear sooner.
8469 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8470 // I've performed some tests and it seems the longest possible life time
8471 // of tooltip is 30 seconds.
8472 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8473 /*
8474 * HACK: force tooltip to appear, because it'll not appear until
8475 * first mouse move. D*mn M$
8476 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8477 */
8478 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8479 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8480 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008481}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008484make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
Bram Moolenaar45360022005-07-21 21:08:21 +00008486 TOOLINFO *pti;
8487 int ToolInfoSize;
8488
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008489 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
8490 {
8491 make_tooltipw(beval, text, pt);
8492 return;
8493 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008494
Bram Moolenaar45360022005-07-21 21:08:21 +00008495 if (multiline_balloon_available() == TRUE)
8496 ToolInfoSize = sizeof(TOOLINFO_NEW);
8497 else
8498 ToolInfoSize = sizeof(TOOLINFO);
8499
8500 pti = (TOOLINFO *)alloc(ToolInfoSize);
8501 if (pti == NULL)
8502 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
8504 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8505 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8506 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8507 beval->target, NULL, s_hinst, NULL);
8508
8509 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8510 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8511
Bram Moolenaar45360022005-07-21 21:08:21 +00008512 pti->cbSize = ToolInfoSize;
8513 pti->uFlags = TTF_SUBCLASS;
8514 pti->hwnd = beval->target;
8515 pti->hinst = 0; /* Don't use string resources */
8516 pti->uId = ID_BEVAL_TOOLTIP;
8517
8518 if (multiline_balloon_available() == TRUE)
8519 {
8520 RECT rect;
8521 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8522 pti->lpszText = LPSTR_TEXTCALLBACK;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008523 beval->tofree = vim_strsave((char_u*)text);
8524 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaar45360022005-07-21 21:08:21 +00008525 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8526 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8527 (LPARAM)rect.right);
8528 }
8529 else
8530 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531
8532 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008533 pti->rect.left = pt.x - 3;
8534 pti->rect.top = pt.y - 3;
8535 pti->rect.right = pt.x + 3;
8536 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
Bram Moolenaar45360022005-07-21 21:08:21 +00008538 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 /* Make tooltip appear sooner */
8540 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008541 /* I've performed some tests and it seems the longest possible life time
8542 * of tooltip is 30 seconds */
8543 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 /*
8545 * HACK: force tooltip to appear, because it'll not appear until
8546 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008547 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008549 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008551 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552}
8553
8554 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008555delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008557 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559
8560 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008561BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008562 HWND hwnd UNUSED,
8563 UINT uMsg UNUSED,
8564 UINT_PTR idEvent UNUSED,
8565 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
8567 POINT pt;
8568 RECT rect;
8569
8570 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8571 return;
8572
8573 GetCursorPos(&pt);
8574 if (WindowFromPoint(pt) != s_textArea)
8575 return;
8576
8577 ScreenToClient(s_textArea, &pt);
8578 GetClientRect(s_textArea, &rect);
8579 if (!PtInRect(&rect, pt))
8580 return;
8581
8582 if (LastActivity > 0
8583 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8584 && (cur_beval->showState != ShS_PENDING
8585 || abs(cur_beval->x - pt.x) > 3
8586 || abs(cur_beval->y - pt.y) > 3))
8587 {
8588 /* Pointer resting in one place long enough, it's time to show
8589 * the tooltip. */
8590 cur_beval->showState = ShS_PENDING;
8591 cur_beval->x = pt.x;
8592 cur_beval->y = pt.y;
8593
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008594 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 if (cur_beval->msgCB != NULL)
8597 (*cur_beval->msgCB)(cur_beval, 0);
8598 }
8599}
8600
8601 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008602gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008604 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008606 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607}
8608
8609 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008610gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008612 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 if (beval == NULL)
8614 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008615 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008616 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008617 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618}
8619
8620 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008621gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622{
8623 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008624
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008625 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 if (beval->showState == ShS_SHOWING)
8627 return;
8628 GetCursorPos(&pt);
8629 ScreenToClient(s_textArea, &pt);
8630
8631 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008633 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 gui_mch_disable_beval_area(cur_beval);
8635 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008636 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008638 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639}
8640
8641 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008642gui_mch_create_beval_area(
8643 void *target, /* ignored, always use s_textArea */
8644 char_u *mesg,
8645 void (*mesgCB)(BalloonEval *, int),
8646 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 /* partially stolen from gui_beval.c */
8649 BalloonEval *beval;
8650
8651 if (mesg != NULL && mesgCB != NULL)
8652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 return NULL;
8655 }
8656
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008657 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 if (beval != NULL)
8659 {
8660 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
8662 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 beval->msg = mesg;
8664 beval->msgCB = mesgCB;
8665 beval->clientData = clientData;
8666
8667 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 cur_beval = beval;
8669
8670 if (p_beval)
8671 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 }
8673 return beval;
8674}
8675
8676 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008677Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
8679 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8680 return;
8681
8682 if (cur_beval != NULL)
8683 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008684 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008686 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008687 // TRACE0("TTN_SHOW {{{");
8688 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008689 break;
8690 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008691 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 delete_tooltip(cur_beval);
8693 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008694 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
8696 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008697 break;
8698 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008699 {
8700 /* if you get there then we have new common controls */
8701 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8702 info->lpszText = (LPSTR)info->lParam;
8703 info->uFlags |= TTF_DI_SETITEM;
8704 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008705 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008706 case TTN_GETDISPINFOW:
8707 {
8708 // if we get here then we have new common controls
8709 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8710 info->lpszText = (LPWSTR)info->lParam;
8711 info->uFlags |= TTF_DI_SETITEM;
8712 }
8713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715 }
8716}
8717
8718 static void
8719TrackUserActivity(UINT uMsg)
8720{
8721 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8722 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8723 LastActivity = GetTickCount();
8724}
8725
8726 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008727gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008729#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008730 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008731#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008732 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 vim_free(beval);
8734}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008735#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
8737#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8738/*
8739 * We have multiple signs to draw at the same location. Draw the
8740 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8741 */
8742 void
8743netbeans_draw_multisign_indicator(int row)
8744{
8745 int i;
8746 int y;
8747 int x;
8748
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008749 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008750 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008751
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 x = 0;
8753 y = TEXT_Y(row);
8754
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008755#if defined(FEAT_DIRECTX)
8756 if (IS_ENABLE_DIRECTX())
8757 DWriteContext_Flush(s_dwc);
8758#endif
8759
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 for (i = 0; i < gui.char_height - 3; i++)
8761 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8762
8763 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8764 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8765 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8766 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8767 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8768 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8769 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8770}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008771#endif