blob: c5e58790d263aea65c7a01b1bf5912a24046805e [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;
256typedef int LOGFONT[];
257typedef 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 *
504 * These LOGFONT used for IME.
505 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100506#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100507/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
508static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100509#endif
510#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100511/* holds LOGFONT for 'guifont' always. */
512static LOGFONT 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 {
1159 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1160 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001161
1162 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1163 /* Give main window the focus back. */
1164 (void)SetFocus(s_hwnd);
1165
1166 if (s_findrep_struct.Flags & FR_FINDNEXT)
1167 {
1168 flags = FRD_FINDNEXT;
1169
1170 /* Give main window the focus back: this is so the cursor isn't
1171 * hollow. */
1172 (void)SetFocus(s_hwnd);
1173 }
1174 else if (s_findrep_struct.Flags & FR_REPLACE)
1175 {
1176 flags = FRD_REPLACE;
1177
1178 /* Give main window the focus back: this is so the cursor isn't
1179 * hollow. */
1180 (void)SetFocus(s_hwnd);
1181 }
1182 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1183 {
1184 flags = FRD_REPLACEALL;
1185 }
1186
1187 if (flags != 0)
1188 {
1189 /* Call the generic GUI function to do the actual work. */
1190 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1191 flags |= FRD_WHOLE_WORD;
1192 if (s_findrep_struct.Flags & FR_MATCHCASE)
1193 flags |= FRD_MATCH_CASE;
1194 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1195 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1196 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1197 }
1198}
1199#endif
1200
1201 static void
1202HandleMouseHide(UINT uMsg, LPARAM lParam)
1203{
1204 static LPARAM last_lParam = 0L;
1205
1206 /* We sometimes get a mousemove when the mouse didn't move... */
1207 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1208 {
1209 if (lParam == last_lParam)
1210 return;
1211 last_lParam = lParam;
1212 }
1213
1214 /* Handle specially, to centralise coding. We need to be sure we catch all
1215 * possible events which should cause us to restore the cursor (as it is a
1216 * shared resource, we take full responsibility for it).
1217 */
1218 switch (uMsg)
1219 {
1220 case WM_KEYUP:
1221 case WM_CHAR:
1222 /*
1223 * blank out the pointer if necessary
1224 */
1225 if (p_mh)
1226 gui_mch_mousehide(TRUE);
1227 break;
1228
1229 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1230 case WM_SYSCHAR:
1231 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1232 case WM_LBUTTONDOWN:
1233 case WM_LBUTTONUP:
1234 case WM_MBUTTONDOWN:
1235 case WM_MBUTTONUP:
1236 case WM_RBUTTONDOWN:
1237 case WM_RBUTTONUP:
1238 case WM_XBUTTONDOWN:
1239 case WM_XBUTTONUP:
1240 case WM_NCMOUSEMOVE:
1241 case WM_NCLBUTTONDOWN:
1242 case WM_NCLBUTTONUP:
1243 case WM_NCMBUTTONDOWN:
1244 case WM_NCMBUTTONUP:
1245 case WM_NCRBUTTONDOWN:
1246 case WM_NCRBUTTONUP:
1247 case WM_KILLFOCUS:
1248 /*
1249 * if the pointer is currently hidden, then we should show it.
1250 */
1251 gui_mch_mousehide(FALSE);
1252 break;
1253 }
1254}
1255
1256 static LRESULT CALLBACK
1257_TextAreaWndProc(
1258 HWND hwnd,
1259 UINT uMsg,
1260 WPARAM wParam,
1261 LPARAM lParam)
1262{
1263 /*
1264 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1265 hwnd, uMsg, wParam, lParam);
1266 */
1267
1268 HandleMouseHide(uMsg, lParam);
1269
1270 s_uMsg = uMsg;
1271 s_wParam = wParam;
1272 s_lParam = lParam;
1273
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001274#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001275 TrackUserActivity(uMsg);
1276#endif
1277
1278 switch (uMsg)
1279 {
1280 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1281 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1282 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1283 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1284 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1285 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1286 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1287 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1288 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1289 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1290 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1291 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1292 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1293 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001294 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001295
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001296#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001297 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1298 return TRUE;
1299#endif
1300 default:
1301 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1302 }
1303}
1304
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001305#ifdef PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001306typedef int WINAPI;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001307#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001308
1309 LRESULT WINAPI
1310vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1311{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001312#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001313 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001314#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001315 if (wide_WindowProc)
1316 return DefWindowProcW(hwnd, message, wParam, lParam);
1317 return DefWindowProc(hwnd, message, wParam, lParam);
1318#endif
1319}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001320
1321/*
1322 * Called when the foreground or background color has been changed.
1323 */
1324 void
1325gui_mch_new_colors(void)
1326{
1327 /* nothing to do? */
1328}
1329
1330/*
1331 * Set the colors to their default values.
1332 */
1333 void
1334gui_mch_def_colors(void)
1335{
1336 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1337 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1338 gui.def_norm_pixel = gui.norm_pixel;
1339 gui.def_back_pixel = gui.back_pixel;
1340}
1341
1342/*
1343 * Open the GUI window which was created by a call to gui_mch_init().
1344 */
1345 int
1346gui_mch_open(void)
1347{
1348#ifndef SW_SHOWDEFAULT
1349# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1350#endif
1351 /* Actually open the window, if not already visible
1352 * (may be done already in gui_mch_set_shellsize) */
1353 if (!IsWindowVisible(s_hwnd))
1354 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1355
1356#ifdef MSWIN_FIND_REPLACE
1357 /* Init replace string here, so that we keep it when re-opening the
1358 * dialog. */
1359 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1360#endif
1361
1362 return OK;
1363}
1364
1365/*
1366 * Get the position of the top left corner of the window.
1367 */
1368 int
1369gui_mch_get_winpos(int *x, int *y)
1370{
1371 RECT rect;
1372
1373 GetWindowRect(s_hwnd, &rect);
1374 *x = rect.left;
1375 *y = rect.top;
1376 return OK;
1377}
1378
1379/*
1380 * Set the position of the top left corner of the window to the given
1381 * coordinates.
1382 */
1383 void
1384gui_mch_set_winpos(int x, int y)
1385{
1386 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1387 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1388}
1389 void
1390gui_mch_set_text_area_pos(int x, int y, int w, int h)
1391{
1392 static int oldx = 0;
1393 static int oldy = 0;
1394
1395 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1396
1397#ifdef FEAT_TOOLBAR
1398 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1399 SendMessage(s_toolbarhwnd, WM_SIZE,
1400 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1401#endif
1402#if defined(FEAT_GUI_TABLINE)
1403 if (showing_tabline)
1404 {
1405 int top = 0;
1406 RECT rect;
1407
1408# ifdef FEAT_TOOLBAR
1409 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1410 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1411# endif
1412 GetClientRect(s_hwnd, &rect);
1413 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1414 }
1415#endif
1416
1417 /* When side scroll bar is unshown, the size of window will change.
1418 * then, the text area move left or right. thus client rect should be
1419 * forcedly redrawn. (Yasuhiro Matsumoto) */
1420 if (oldx != x || oldy != y)
1421 {
1422 InvalidateRect(s_hwnd, NULL, FALSE);
1423 oldx = x;
1424 oldy = y;
1425 }
1426}
1427
1428
1429/*
1430 * Scrollbar stuff:
1431 */
1432
1433 void
1434gui_mch_enable_scrollbar(
1435 scrollbar_T *sb,
1436 int flag)
1437{
1438 ShowScrollBar(sb->id, SB_CTL, flag);
1439
1440 /* TODO: When the window is maximized, the size of the window stays the
1441 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1442 * NT 4.0 it's not... */
1443}
1444
1445 void
1446gui_mch_set_scrollbar_pos(
1447 scrollbar_T *sb,
1448 int x,
1449 int y,
1450 int w,
1451 int h)
1452{
1453 SetWindowPos(sb->id, NULL, x, y, w, h,
1454 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1455}
1456
1457 void
1458gui_mch_create_scrollbar(
1459 scrollbar_T *sb,
1460 int orient) /* SBAR_VERT or SBAR_HORIZ */
1461{
1462 sb->id = CreateWindow(
1463 "SCROLLBAR", "Scrollbar",
1464 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1465 10, /* Any value will do for now */
1466 10, /* Any value will do for now */
1467 s_hwnd, NULL,
1468 s_hinst, NULL);
1469}
1470
1471/*
1472 * Find the scrollbar with the given hwnd.
1473 */
1474 static scrollbar_T *
1475gui_mswin_find_scrollbar(HWND hwnd)
1476{
1477 win_T *wp;
1478
1479 if (gui.bottom_sbar.id == hwnd)
1480 return &gui.bottom_sbar;
1481 FOR_ALL_WINDOWS(wp)
1482 {
1483 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1484 return &wp->w_scrollbars[SBAR_LEFT];
1485 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1486 return &wp->w_scrollbars[SBAR_RIGHT];
1487 }
1488 return NULL;
1489}
1490
1491/*
1492 * Get the character size of a font.
1493 */
1494 static void
1495GetFontSize(GuiFont font)
1496{
1497 HWND hwnd = GetDesktopWindow();
1498 HDC hdc = GetWindowDC(hwnd);
1499 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1500 TEXTMETRIC tm;
1501
1502 GetTextMetrics(hdc, &tm);
1503 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1504
1505 gui.char_height = tm.tmHeight + p_linespace;
1506
1507 SelectFont(hdc, hfntOld);
1508
1509 ReleaseDC(hwnd, hdc);
1510}
1511
1512/*
1513 * Adjust gui.char_height (after 'linespace' was changed).
1514 */
1515 int
1516gui_mch_adjust_charheight(void)
1517{
1518 GetFontSize(gui.norm_font);
1519 return OK;
1520}
1521
1522 static GuiFont
1523get_font_handle(LOGFONT *lf)
1524{
1525 HFONT font = NULL;
1526
1527 /* Load the font */
1528 font = CreateFontIndirect(lf);
1529
1530 if (font == NULL)
1531 return NOFONT;
1532
1533 return (GuiFont)font;
1534}
1535
1536 static int
1537pixels_to_points(int pixels, int vertical)
1538{
1539 int points;
1540 HWND hwnd;
1541 HDC hdc;
1542
1543 hwnd = GetDesktopWindow();
1544 hdc = GetWindowDC(hwnd);
1545
1546 points = MulDiv(pixels, 72,
1547 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1548
1549 ReleaseDC(hwnd, hdc);
1550
1551 return points;
1552}
1553
1554 GuiFont
1555gui_mch_get_font(
1556 char_u *name,
1557 int giveErrorIfMissing)
1558{
1559 LOGFONT lf;
1560 GuiFont font = NOFONT;
1561
1562 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1563 font = get_font_handle(&lf);
1564 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001565 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 return font;
1567}
1568
1569#if defined(FEAT_EVAL) || defined(PROTO)
1570/*
1571 * Return the name of font "font" in allocated memory.
1572 * Don't know how to get the actual name, thus use the provided name.
1573 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001574 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001575gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001576{
1577 if (name == NULL)
1578 return NULL;
1579 return vim_strsave(name);
1580}
1581#endif
1582
1583 void
1584gui_mch_free_font(GuiFont font)
1585{
1586 if (font)
1587 DeleteObject((HFONT)font);
1588}
1589
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590/*
1591 * Return the Pixel value (color) for the given color name.
1592 * Return INVALCOLOR for error.
1593 */
1594 guicolor_T
1595gui_mch_get_color(char_u *name)
1596{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001597 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598
1599 typedef struct SysColorTable
1600 {
1601 char *name;
1602 int color;
1603 } SysColorTable;
1604
1605 static SysColorTable sys_table[] =
1606 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1608 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001609#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1611#endif
1612 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1613 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1614 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1615 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1616 {"SYS_DESKTOP", COLOR_DESKTOP},
1617 {"SYS_INFOBK", COLOR_INFOBK},
1618 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1619 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001620 {"SYS_BTNFACE", COLOR_BTNFACE},
1621 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1622 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1623 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1624 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1625 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1626 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1627 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1628 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1629 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1630 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1631 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1632 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1633 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1634 {"SYS_MENU", COLOR_MENU},
1635 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1636 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1637 {"SYS_WINDOW", COLOR_WINDOW},
1638 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1639 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1640 };
1641
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001642 /*
1643 * Try to look up a system colour.
1644 */
1645 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1646 if (STRICMP(name, sys_table[i].name) == 0)
1647 return GetSysColor(sys_table[i].color);
1648
Bram Moolenaarab302212016-04-26 20:59:29 +02001649 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001650}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001651
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001652 guicolor_T
1653gui_mch_get_rgb_color(int r, int g, int b)
1654{
1655 return gui_get_rgb_color_cmn(r, g, b);
1656}
1657
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658/*
1659 * Return OK if the key with the termcap name "name" is supported.
1660 */
1661 int
1662gui_mch_haskey(char_u *name)
1663{
1664 int i;
1665
1666 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1667 if (name[0] == special_keys[i].vim_code0 &&
1668 name[1] == special_keys[i].vim_code1)
1669 return OK;
1670 return FAIL;
1671}
1672
1673 void
1674gui_mch_beep(void)
1675{
1676 MessageBeep(MB_OK);
1677}
1678/*
1679 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1680 */
1681 void
1682gui_mch_invert_rectangle(
1683 int r,
1684 int c,
1685 int nr,
1686 int nc)
1687{
1688 RECT rc;
1689
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001690#if defined(FEAT_DIRECTX)
1691 if (IS_ENABLE_DIRECTX())
1692 DWriteContext_Flush(s_dwc);
1693#endif
1694
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001695 /*
1696 * Note: InvertRect() excludes right and bottom of rectangle.
1697 */
1698 rc.left = FILL_X(c);
1699 rc.top = FILL_Y(r);
1700 rc.right = rc.left + nc * gui.char_width;
1701 rc.bottom = rc.top + nr * gui.char_height;
1702 InvertRect(s_hdc, &rc);
1703}
1704
1705/*
1706 * Iconify the GUI window.
1707 */
1708 void
1709gui_mch_iconify(void)
1710{
1711 ShowWindow(s_hwnd, SW_MINIMIZE);
1712}
1713
1714/*
1715 * Draw a cursor without focus.
1716 */
1717 void
1718gui_mch_draw_hollow_cursor(guicolor_T color)
1719{
1720 HBRUSH hbr;
1721 RECT rc;
1722
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001723#if defined(FEAT_DIRECTX)
1724 if (IS_ENABLE_DIRECTX())
1725 DWriteContext_Flush(s_dwc);
1726#endif
1727
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 /*
1729 * Note: FrameRect() excludes right and bottom of rectangle.
1730 */
1731 rc.left = FILL_X(gui.col);
1732 rc.top = FILL_Y(gui.row);
1733 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001734 if (mb_lefthalve(gui.row, gui.col))
1735 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001736 rc.bottom = rc.top + gui.char_height;
1737 hbr = CreateSolidBrush(color);
1738 FrameRect(s_hdc, &rc, hbr);
1739 DeleteBrush(hbr);
1740}
1741/*
1742 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1743 * color "color".
1744 */
1745 void
1746gui_mch_draw_part_cursor(
1747 int w,
1748 int h,
1749 guicolor_T color)
1750{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 RECT rc;
1752
1753 /*
1754 * Note: FillRect() excludes right and bottom of rectangle.
1755 */
1756 rc.left =
1757#ifdef FEAT_RIGHTLEFT
1758 /* vertical line should be on the right of current point */
1759 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1760#endif
1761 FILL_X(gui.col);
1762 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1763 rc.right = rc.left + w;
1764 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001765
Bram Moolenaar92467d32017-12-05 13:22:16 +01001766 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001767}
1768
1769
1770/*
1771 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1772 * dead key's nominal character and re-post the original message.
1773 */
1774 static void
1775outputDeadKey_rePost(MSG originalMsg)
1776{
1777 static MSG deadCharExpel;
1778
1779 if (!dead_key)
1780 return;
1781
1782 dead_key = 0;
1783
1784 /* Make Windows generate the dead key's character */
1785 deadCharExpel.message = originalMsg.message;
1786 deadCharExpel.hwnd = originalMsg.hwnd;
1787 deadCharExpel.wParam = VK_SPACE;
1788
1789 MyTranslateMessage(&deadCharExpel);
1790
1791 /* re-generate the current character free of the dead char influence */
1792 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1793 originalMsg.lParam);
1794}
1795
1796
1797/*
1798 * Process a single Windows message.
1799 * If one is not available we hang until one is.
1800 */
1801 static void
1802process_message(void)
1803{
1804 MSG msg;
1805 UINT vk = 0; /* Virtual key */
1806 char_u string[40];
1807 int i;
1808 int modifiers = 0;
1809 int key;
1810#ifdef FEAT_MENU
1811 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1812#endif
1813
1814 pGetMessage(&msg, NULL, 0, 0);
1815
1816#ifdef FEAT_OLE
1817 /* Look after OLE Automation commands */
1818 if (msg.message == WM_OLE)
1819 {
1820 char_u *str = (char_u *)msg.lParam;
1821 if (str == NULL || *str == NUL)
1822 {
1823 /* Message can't be ours, forward it. Fixes problem with Ultramon
1824 * 3.0.4 */
1825 pDispatchMessage(&msg);
1826 }
1827 else
1828 {
1829 add_to_input_buf(str, (int)STRLEN(str));
1830 vim_free(str); /* was allocated in CVim::SendKeys() */
1831 }
1832 return;
1833 }
1834#endif
1835
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001836#ifdef MSWIN_FIND_REPLACE
1837 /* Don't process messages used by the dialog */
1838 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1839 {
1840 HandleMouseHide(msg.message, msg.lParam);
1841 return;
1842 }
1843#endif
1844
1845 /*
1846 * Check if it's a special key that we recognise. If not, call
1847 * TranslateMessage().
1848 */
1849 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1850 {
1851 vk = (int) msg.wParam;
1852
1853 /*
1854 * Handle dead keys in special conditions in other cases we let Windows
1855 * handle them and do not interfere.
1856 *
1857 * The dead_key flag must be reset on several occasions:
1858 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1859 * consumed at that point (This is when we let Windows combine the
1860 * dead character on its own)
1861 *
1862 * - Before doing something special such as regenerating keypresses to
1863 * expel the dead character as this could trigger an infinite loop if
1864 * for some reason MyTranslateMessage() do not trigger a call
1865 * immediately to _OnChar() (or _OnSysChar()).
1866 */
1867 if (dead_key)
1868 {
1869 /*
1870 * If a dead key was pressed and the user presses VK_SPACE,
1871 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1872 * with the dead char now, so do nothing special and let Windows
1873 * handle it.
1874 *
1875 * Note that VK_SPACE combines with the dead_key's character and
1876 * only one WM_CHAR will be generated by TranslateMessage(), in
1877 * the two other cases two WM_CHAR will be generated: the dead
1878 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1879 * user expects.
1880 */
1881 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1882 {
1883 dead_key = 0;
1884 MyTranslateMessage(&msg);
1885 return;
1886 }
1887 /* In modes where we are not typing, dead keys should behave
1888 * normally */
1889 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1890 {
1891 outputDeadKey_rePost(msg);
1892 return;
1893 }
1894 }
1895
1896 /* Check for CTRL-BREAK */
1897 if (vk == VK_CANCEL)
1898 {
1899 trash_input_buf();
1900 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001901 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001902 string[0] = Ctrl_C;
1903 add_to_input_buf(string, 1);
1904 }
1905
1906 for (i = 0; special_keys[i].key_sym != 0; i++)
1907 {
1908 /* ignore VK_SPACE when ALT key pressed: system menu */
1909 if (special_keys[i].key_sym == vk
1910 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1911 {
1912 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001913 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001914 * is a key that would normally trigger the dead key nominal
1915 * character output (such as a NUMPAD printable character or
1916 * the TAB key, etc...).
1917 */
1918 if (dead_key && (special_keys[i].vim_code0 == 'K'
1919 || vk == VK_TAB || vk == CAR))
1920 {
1921 outputDeadKey_rePost(msg);
1922 return;
1923 }
1924
1925#ifdef FEAT_MENU
1926 /* Check for <F10>: Windows selects the menu. When <F10> is
1927 * mapped we want to use the mapping instead. */
1928 if (vk == VK_F10
1929 && gui.menu_is_active
1930 && check_map(k10, State, FALSE, TRUE, FALSE,
1931 NULL, NULL) == NULL)
1932 break;
1933#endif
1934 if (GetKeyState(VK_SHIFT) & 0x8000)
1935 modifiers |= MOD_MASK_SHIFT;
1936 /*
1937 * Don't use caps-lock as shift, because these are special keys
1938 * being considered here, and we only want letters to get
1939 * shifted -- webb
1940 */
1941 /*
1942 if (GetKeyState(VK_CAPITAL) & 0x0001)
1943 modifiers ^= MOD_MASK_SHIFT;
1944 */
1945 if (GetKeyState(VK_CONTROL) & 0x8000)
1946 modifiers |= MOD_MASK_CTRL;
1947 if (GetKeyState(VK_MENU) & 0x8000)
1948 modifiers |= MOD_MASK_ALT;
1949
1950 if (special_keys[i].vim_code1 == NUL)
1951 key = special_keys[i].vim_code0;
1952 else
1953 key = TO_SPECIAL(special_keys[i].vim_code0,
1954 special_keys[i].vim_code1);
1955 key = simplify_key(key, &modifiers);
1956 if (key == CSI)
1957 key = K_CSI;
1958
1959 if (modifiers)
1960 {
1961 string[0] = CSI;
1962 string[1] = KS_MODIFIER;
1963 string[2] = modifiers;
1964 add_to_input_buf(string, 3);
1965 }
1966
1967 if (IS_SPECIAL(key))
1968 {
1969 string[0] = CSI;
1970 string[1] = K_SECOND(key);
1971 string[2] = K_THIRD(key);
1972 add_to_input_buf(string, 3);
1973 }
1974 else
1975 {
1976 int len;
1977
1978 /* Handle "key" as a Unicode character. */
1979 len = char_to_string(key, string, 40, FALSE);
1980 add_to_input_buf(string, len);
1981 }
1982 break;
1983 }
1984 }
1985 if (special_keys[i].key_sym == 0)
1986 {
1987 /* Some keys need C-S- where they should only need C-.
1988 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1989 * system startup (Helmut Stiegler, 2003 Oct 3). */
1990 if (vk != 0xff
1991 && (GetKeyState(VK_CONTROL) & 0x8000)
1992 && !(GetKeyState(VK_SHIFT) & 0x8000)
1993 && !(GetKeyState(VK_MENU) & 0x8000))
1994 {
1995 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1996 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1997 {
1998 string[0] = Ctrl_HAT;
1999 add_to_input_buf(string, 1);
2000 }
2001 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2002 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2003 {
2004 string[0] = Ctrl__;
2005 add_to_input_buf(string, 1);
2006 }
2007 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2008 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2009 {
2010 string[0] = Ctrl_AT;
2011 add_to_input_buf(string, 1);
2012 }
2013 else
2014 MyTranslateMessage(&msg);
2015 }
2016 else
2017 MyTranslateMessage(&msg);
2018 }
2019 }
2020#ifdef FEAT_MBYTE_IME
2021 else if (msg.message == WM_IME_NOTIFY)
2022 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2023 else if (msg.message == WM_KEYUP && im_get_status())
2024 /* added for non-MS IME (Yasuhiro Matsumoto) */
2025 MyTranslateMessage(&msg);
2026#endif
2027#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2028/* GIME_TEST */
2029 else if (msg.message == WM_IME_STARTCOMPOSITION)
2030 {
2031 POINT point;
2032
2033 global_ime_set_font(&norm_logfont);
2034 point.x = FILL_X(gui.col);
2035 point.y = FILL_Y(gui.row);
2036 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2037 global_ime_set_position(&point);
2038 }
2039#endif
2040
2041#ifdef FEAT_MENU
2042 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2043 * mapped we need to stop it here to avoid strange effects (e.g., for the
2044 * key-up event) */
2045 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2046 NULL, NULL) == NULL)
2047#endif
2048 pDispatchMessage(&msg);
2049}
2050
2051/*
2052 * Catch up with any queued events. This may put keyboard input into the
2053 * input buffer, call resize call-backs, trigger timers etc. If there is
2054 * nothing in the event queue (& no timers pending), then we return
2055 * immediately.
2056 */
2057 void
2058gui_mch_update(void)
2059{
2060 MSG msg;
2061
2062 if (!s_busy_processing)
2063 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2064 && !vim_is_input_buf_full())
2065 process_message();
2066}
2067
Bram Moolenaar4231da42016-06-02 14:30:04 +02002068 static void
2069remove_any_timer(void)
2070{
2071 MSG msg;
2072
2073 if (s_wait_timer != 0 && !s_timed_out)
2074 {
2075 KillTimer(NULL, s_wait_timer);
2076
2077 /* Eat spurious WM_TIMER messages */
2078 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2079 ;
2080 s_wait_timer = 0;
2081 }
2082}
2083
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002084/*
2085 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2086 * from the keyboard.
2087 * wtime == -1 Wait forever.
2088 * wtime == 0 This should never happen.
2089 * wtime > 0 Wait wtime milliseconds for a character.
2090 * Returns OK if a character was found to be available within the given time,
2091 * or FAIL otherwise.
2092 */
2093 int
2094gui_mch_wait_for_chars(int wtime)
2095{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002096 int focus;
2097
2098 s_timed_out = FALSE;
2099
2100 if (wtime > 0)
2101 {
2102 /* Don't do anything while processing a (scroll) message. */
2103 if (s_busy_processing)
2104 return FAIL;
2105 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2106 (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
2281#ifdef VIMDLL
2282_export
2283#endif
2284_cdecl
2285SaveInst(HINSTANCE hInst)
2286{
2287 s_hinst = hInst;
2288}
2289#endif
2290
2291/*
2292 * Return the RGB value of a pixel as a long.
2293 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002294 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002295gui_mch_get_rgb(guicolor_T pixel)
2296{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002297 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2298 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002299}
2300
2301#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2302/* Convert pixels in X to dialog units */
2303 static WORD
2304PixelToDialogX(int numPixels)
2305{
2306 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2307}
2308
2309/* Convert pixels in Y to dialog units */
2310 static WORD
2311PixelToDialogY(int numPixels)
2312{
2313 return (WORD)((numPixels * 8) / s_dlgfntheight);
2314}
2315
2316/* Return the width in pixels of the given text in the given DC. */
2317 static int
2318GetTextWidth(HDC hdc, char_u *str, int len)
2319{
2320 SIZE size;
2321
2322 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2323 return size.cx;
2324}
2325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002326/*
2327 * Return the width in pixels of the given text in the given DC, taking care
2328 * of 'encoding' to active codepage conversion.
2329 */
2330 static int
2331GetTextWidthEnc(HDC hdc, char_u *str, int len)
2332{
2333 SIZE size;
2334 WCHAR *wstr;
2335 int n;
2336 int wlen = len;
2337
2338 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2339 {
2340 /* 'encoding' differs from active codepage: convert text and use wide
2341 * function */
2342 wstr = enc_to_utf16(str, &wlen);
2343 if (wstr != NULL)
2344 {
2345 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2346 vim_free(wstr);
2347 if (n)
2348 return size.cx;
2349 }
2350 }
2351
2352 return GetTextWidth(hdc, str, len);
2353}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002354
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002355static void get_work_area(RECT *spi_rect);
2356
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357/*
2358 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002359 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2360 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002361 */
2362 static BOOL
2363CenterWindow(
2364 HWND hwndChild,
2365 HWND hwndParent)
2366{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002367 HMONITOR mon;
2368 MONITORINFO moninfo;
2369 RECT rChild, rParent, rScreen;
2370 int wChild, hChild, wParent, hParent;
2371 int xNew, yNew;
2372 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002373
2374 GetWindowRect(hwndChild, &rChild);
2375 wChild = rChild.right - rChild.left;
2376 hChild = rChild.bottom - rChild.top;
2377
2378 /* If Vim is minimized put the window in the middle of the screen. */
2379 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002380 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002381 else
2382 GetWindowRect(hwndParent, &rParent);
2383 wParent = rParent.right - rParent.left;
2384 hParent = rParent.bottom - rParent.top;
2385
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002386 moninfo.cbSize = sizeof(MONITORINFO);
2387 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2388 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002390 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002392 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002393 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002394 hdc = GetDC(hwndChild);
2395 rScreen.left = 0;
2396 rScreen.top = 0;
2397 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2398 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2399 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002400 }
2401
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002402 xNew = rParent.left + ((wParent - wChild) / 2);
2403 if (xNew < rScreen.left)
2404 xNew = rScreen.left;
2405 else if ((xNew + wChild) > rScreen.right)
2406 xNew = rScreen.right - wChild;
2407
2408 yNew = rParent.top + ((hParent - hChild) / 2);
2409 if (yNew < rScreen.top)
2410 yNew = rScreen.top;
2411 else if ((yNew + hChild) > rScreen.bottom)
2412 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413
2414 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2415 SWP_NOSIZE | SWP_NOZORDER);
2416}
2417#endif /* FEAT_GUI_DIALOG */
2418
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419#if defined(FEAT_TOOLBAR) || defined(PROTO)
2420 void
2421gui_mch_show_toolbar(int showit)
2422{
2423 if (s_toolbarhwnd == NULL)
2424 return;
2425
2426 if (showit)
2427 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002428# ifndef TB_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002430# define TB_SETUNICODEFORMAT 0x2005
2431# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 /* Enable/disable unicode support */
2433 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2434 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002435 ShowWindow(s_toolbarhwnd, SW_SHOW);
2436 }
2437 else
2438 ShowWindow(s_toolbarhwnd, SW_HIDE);
2439}
2440
2441/* Then number of bitmaps is fixed. Exit is missing! */
2442#define TOOLBAR_BITMAP_COUNT 31
2443
2444#endif
2445
2446#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2447 static void
2448add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2449{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002451
2452 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2453 {
2454 /* 'encoding' differs from active codepage: convert menu name
2455 * and use wide function */
2456 wn = enc_to_utf16(item_text, NULL);
2457 if (wn != NULL)
2458 {
2459 MENUITEMINFOW infow;
2460
2461 infow.cbSize = sizeof(infow);
2462 infow.fMask = MIIM_TYPE | MIIM_ID;
2463 infow.wID = item_id;
2464 infow.fType = MFT_STRING;
2465 infow.dwTypeData = wn;
2466 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002467 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002468 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002469 }
2470 }
2471
2472 if (wn == NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002473 {
2474 MENUITEMINFO info;
2475
2476 info.cbSize = sizeof(info);
2477 info.fMask = MIIM_TYPE | MIIM_ID;
2478 info.wID = item_id;
2479 info.fType = MFT_STRING;
2480 info.dwTypeData = (LPTSTR)item_text;
2481 info.cch = (UINT)STRLEN(item_text);
2482 InsertMenuItem(pmenu, item_id, FALSE, &info);
2483 }
2484}
2485
2486 static void
2487show_tabline_popup_menu(void)
2488{
2489 HMENU tab_pmenu;
2490 long rval;
2491 POINT pt;
2492
2493 /* When ignoring events don't show the menu. */
2494 if (hold_gui_events
2495# ifdef FEAT_CMDWIN
2496 || cmdwin_type != 0
2497# endif
2498 )
2499 return;
2500
2501 tab_pmenu = CreatePopupMenu();
2502 if (tab_pmenu == NULL)
2503 return;
2504
2505 if (first_tabpage->tp_next != NULL)
2506 add_tabline_popup_menu_entry(tab_pmenu,
2507 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2508 add_tabline_popup_menu_entry(tab_pmenu,
2509 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2510 add_tabline_popup_menu_entry(tab_pmenu,
2511 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2512
2513 GetCursorPos(&pt);
2514 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2515 NULL);
2516
2517 DestroyMenu(tab_pmenu);
2518
2519 /* Add the string cmd into input buffer */
2520 if (rval > 0)
2521 {
2522 TCHITTESTINFO htinfo;
2523 int idx;
2524
2525 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2526 return;
2527
2528 htinfo.pt.x = pt.x;
2529 htinfo.pt.y = pt.y;
2530 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2531 if (idx == -1)
2532 idx = 0;
2533 else
2534 idx += 1;
2535
2536 send_tabline_menu_event(idx, (int)rval);
2537 }
2538}
2539
2540/*
2541 * Show or hide the tabline.
2542 */
2543 void
2544gui_mch_show_tabline(int showit)
2545{
2546 if (s_tabhwnd == NULL)
2547 return;
2548
2549 if (!showit != !showing_tabline)
2550 {
2551 if (showit)
2552 ShowWindow(s_tabhwnd, SW_SHOW);
2553 else
2554 ShowWindow(s_tabhwnd, SW_HIDE);
2555 showing_tabline = showit;
2556 }
2557}
2558
2559/*
2560 * Return TRUE when tabline is displayed.
2561 */
2562 int
2563gui_mch_showing_tabline(void)
2564{
2565 return s_tabhwnd != NULL && showing_tabline;
2566}
2567
2568/*
2569 * Update the labels of the tabline.
2570 */
2571 void
2572gui_mch_update_tabline(void)
2573{
2574 tabpage_T *tp;
2575 TCITEM tie;
2576 int nr = 0;
2577 int curtabidx = 0;
2578 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002579 static int use_unicode = FALSE;
2580 int uu;
2581 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582
2583 if (s_tabhwnd == NULL)
2584 return;
2585
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002586#ifndef CCM_SETUNICODEFORMAT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587 /* For older compilers. We assume this never changes. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002588# define CCM_SETUNICODEFORMAT 0x2005
2589#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2591 if (uu != use_unicode)
2592 {
2593 /* Enable/disable unicode support */
2594 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2595 use_unicode = uu;
2596 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002597
2598 tie.mask = TCIF_TEXT;
2599 tie.iImage = -1;
2600
2601 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2602 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2603
2604 /* Add a label for each tab page. They all contain the same text area. */
2605 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2606 {
2607 if (tp == curtab)
2608 curtabidx = nr;
2609
2610 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2611 {
2612 /* Add the tab */
2613 tie.pszText = "-Empty-";
2614 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2615 tabadded = 1;
2616 }
2617
2618 get_tabline_label(tp, FALSE);
2619 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 wstr = NULL;
2621 if (use_unicode)
2622 {
2623 /* Need to go through Unicode. */
2624 wstr = enc_to_utf16(NameBuff, NULL);
2625 if (wstr != NULL)
2626 {
2627 TCITEMW tiw;
2628
2629 tiw.mask = TCIF_TEXT;
2630 tiw.iImage = -1;
2631 tiw.pszText = wstr;
2632 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2633 vim_free(wstr);
2634 }
2635 }
2636 if (wstr == NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002637 {
2638 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2639 }
2640 }
2641
2642 /* Remove any old labels. */
2643 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2644 TabCtrl_DeleteItem(s_tabhwnd, nr);
2645
2646 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2647 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2648
2649 /* Re-enable redraw and redraw. */
2650 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2651 RedrawWindow(s_tabhwnd, NULL, NULL,
2652 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2653
2654 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2655 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2656}
2657
2658/*
2659 * Set the current tab to "nr". First tab is 1.
2660 */
2661 void
2662gui_mch_set_curtab(int nr)
2663{
2664 if (s_tabhwnd == NULL)
2665 return;
2666
2667 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2668 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2669}
2670
2671#endif
2672
2673/*
2674 * ":simalt" command.
2675 */
2676 void
2677ex_simalt(exarg_T *eap)
2678{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002679 char_u *keys = eap->arg;
2680 int fill_typebuf = FALSE;
2681 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002682
2683 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2684 while (*keys)
2685 {
2686 if (*keys == '~')
2687 *keys = ' '; /* for showing system menu */
2688 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2689 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002690 fill_typebuf = TRUE;
2691 }
2692 if (fill_typebuf)
2693 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002694 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002695 * processed. */
2696 key_name[0] = K_SPECIAL;
2697 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002698 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002699 key_name[3] = NUL;
2700 typebuf_was_filled = TRUE;
2701 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 }
2703}
2704
2705/*
2706 * Create the find & replace dialogs.
2707 * You can't have both at once: ":find" when replace is showing, destroys
2708 * the replace dialog first, and the other way around.
2709 */
2710#ifdef MSWIN_FIND_REPLACE
2711 static void
2712initialise_findrep(char_u *initial_string)
2713{
2714 int wword = FALSE;
2715 int mcase = !p_ic;
2716 char_u *entry_text;
2717
2718 /* Get the search string to use. */
2719 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2720
2721 s_findrep_struct.hwndOwner = s_hwnd;
2722 s_findrep_struct.Flags = FR_DOWN;
2723 if (mcase)
2724 s_findrep_struct.Flags |= FR_MATCHCASE;
2725 if (wword)
2726 s_findrep_struct.Flags |= FR_WHOLEWORD;
2727 if (entry_text != NULL && *entry_text != NUL)
2728 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2729 s_findrep_struct.wFindWhatLen - 1);
2730 vim_free(entry_text);
2731}
2732#endif
2733
2734 static void
2735set_window_title(HWND hwnd, char *title)
2736{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002737 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2738 {
2739 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740
2741 /* Convert the title from 'encoding' to UTF-16. */
2742 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2743 if (wbuf != NULL)
2744 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002745 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002746 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002747 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002748 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002749 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002750 (void)SetWindowText(hwnd, (LPCSTR)title);
2751}
2752
2753 void
2754gui_mch_find_dialog(exarg_T *eap)
2755{
2756#ifdef MSWIN_FIND_REPLACE
2757 if (s_findrep_msg != 0)
2758 {
2759 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2760 DestroyWindow(s_findrep_hwnd);
2761
2762 if (!IsWindow(s_findrep_hwnd))
2763 {
2764 initialise_findrep(eap->arg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002765 /* If the OS is Windows NT, and 'encoding' differs from active
2766 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002767 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002768 {
2769 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2770 s_findrep_hwnd = FindTextW(
2771 (LPFINDREPLACEW) &s_findrep_struct_w);
2772 }
2773 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2775 }
2776
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002777 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002778 (void)SetFocus(s_findrep_hwnd);
2779
2780 s_findrep_is_find = TRUE;
2781 }
2782#endif
2783}
2784
2785
2786 void
2787gui_mch_replace_dialog(exarg_T *eap)
2788{
2789#ifdef MSWIN_FIND_REPLACE
2790 if (s_findrep_msg != 0)
2791 {
2792 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2793 DestroyWindow(s_findrep_hwnd);
2794
2795 if (!IsWindow(s_findrep_hwnd))
2796 {
2797 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002798 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 {
2800 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2801 s_findrep_hwnd = ReplaceTextW(
2802 (LPFINDREPLACEW) &s_findrep_struct_w);
2803 }
2804 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 s_findrep_hwnd = ReplaceText(
2806 (LPFINDREPLACE) &s_findrep_struct);
2807 }
2808
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002809 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 (void)SetFocus(s_findrep_hwnd);
2811
2812 s_findrep_is_find = FALSE;
2813 }
2814#endif
2815}
2816
2817
2818/*
2819 * Set visibility of the pointer.
2820 */
2821 void
2822gui_mch_mousehide(int hide)
2823{
2824 if (hide != gui.pointer_hidden)
2825 {
2826 ShowCursor(!hide);
2827 gui.pointer_hidden = hide;
2828 }
2829}
2830
2831#ifdef FEAT_MENU
2832 static void
2833gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2834{
2835 /* Unhide the mouse, we don't get move events here. */
2836 gui_mch_mousehide(FALSE);
2837
2838 (void)TrackPopupMenu(
2839 (HMENU)menu->submenu_id,
2840 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2841 x, y,
2842 (int)0, /*reserved param*/
2843 s_hwnd,
2844 NULL);
2845 /*
2846 * NOTE: The pop-up menu can eat the mouse up event.
2847 * We deal with this in normal.c.
2848 */
2849}
2850#endif
2851
2852/*
2853 * Got a message when the system will go down.
2854 */
2855 static void
2856_OnEndSession(void)
2857{
2858 getout_preserve_modified(1);
2859}
2860
2861/*
2862 * Get this message when the user clicks on the cross in the top right corner
2863 * of a Windows95 window.
2864 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002866_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002867{
2868 gui_shell_closed();
2869}
2870
2871/*
2872 * Get a message when the window is being destroyed.
2873 */
2874 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002875_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876{
2877 if (!destroying)
2878 _OnClose(hwnd);
2879}
2880
2881 static void
2882_OnPaint(
2883 HWND hwnd)
2884{
2885 if (!IsMinimized(hwnd))
2886 {
2887 PAINTSTRUCT ps;
2888
2889 out_flush(); /* make sure all output has been processed */
2890 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002892 /* prevent multi-byte characters from misprinting on an invalid
2893 * rectangle */
2894 if (has_mbyte)
2895 {
2896 RECT rect;
2897
2898 GetClientRect(hwnd, &rect);
2899 ps.rcPaint.left = rect.left;
2900 ps.rcPaint.right = rect.right;
2901 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902
2903 if (!IsRectEmpty(&ps.rcPaint))
2904 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002905 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2906 ps.rcPaint.right - ps.rcPaint.left + 1,
2907 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2908 }
2909
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002910 EndPaint(hwnd, &ps);
2911 }
2912}
2913
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002914 static void
2915_OnSize(
2916 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002917 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918 int cx,
2919 int cy)
2920{
2921 if (!IsMinimized(hwnd))
2922 {
2923 gui_resize_shell(cx, cy);
2924
2925#ifdef FEAT_MENU
2926 /* Menu bar may wrap differently now */
2927 gui_mswin_get_menu_height(TRUE);
2928#endif
2929 }
2930}
2931
2932 static void
2933_OnSetFocus(
2934 HWND hwnd,
2935 HWND hwndOldFocus)
2936{
2937 gui_focus_change(TRUE);
2938 s_getting_focus = TRUE;
2939 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2940}
2941
2942 static void
2943_OnKillFocus(
2944 HWND hwnd,
2945 HWND hwndNewFocus)
2946{
2947 gui_focus_change(FALSE);
2948 s_getting_focus = FALSE;
2949 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2950}
2951
2952/*
2953 * Get a message when the user switches back to vim
2954 */
2955 static LRESULT
2956_OnActivateApp(
2957 HWND hwnd,
2958 BOOL fActivate,
2959 DWORD dwThreadId)
2960{
2961 /* we call gui_focus_change() in _OnSetFocus() */
2962 /* gui_focus_change((int)fActivate); */
2963 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2964}
2965
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966 void
2967gui_mch_destroy_scrollbar(scrollbar_T *sb)
2968{
2969 DestroyWindow(sb->id);
2970}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002971
2972/*
2973 * Get current mouse coordinates in text window.
2974 */
2975 void
2976gui_mch_getmouse(int *x, int *y)
2977{
2978 RECT rct;
2979 POINT mp;
2980
2981 (void)GetWindowRect(s_textArea, &rct);
2982 (void)GetCursorPos((LPPOINT)&mp);
2983 *x = (int)(mp.x - rct.left);
2984 *y = (int)(mp.y - rct.top);
2985}
2986
2987/*
2988 * Move mouse pointer to character at (x, y).
2989 */
2990 void
2991gui_mch_setmouse(int x, int y)
2992{
2993 RECT rct;
2994
2995 (void)GetWindowRect(s_textArea, &rct);
2996 (void)SetCursorPos(x + gui.border_offset + rct.left,
2997 y + gui.border_offset + rct.top);
2998}
2999
3000 static void
3001gui_mswin_get_valid_dimensions(
3002 int w,
3003 int h,
3004 int *valid_w,
3005 int *valid_h)
3006{
3007 int base_width, base_height;
3008
3009 base_width = gui_get_base_width()
3010 + (GetSystemMetrics(SM_CXFRAME) +
3011 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3012 base_height = gui_get_base_height()
3013 + (GetSystemMetrics(SM_CYFRAME) +
3014 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3015 + GetSystemMetrics(SM_CYCAPTION)
3016#ifdef FEAT_MENU
3017 + gui_mswin_get_menu_height(FALSE)
3018#endif
3019 ;
3020 *valid_w = base_width +
3021 ((w - base_width) / gui.char_width) * gui.char_width;
3022 *valid_h = base_height +
3023 ((h - base_height) / gui.char_height) * gui.char_height;
3024}
3025
3026 void
3027gui_mch_flash(int msec)
3028{
3029 RECT rc;
3030
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003031#if defined(FEAT_DIRECTX)
3032 if (IS_ENABLE_DIRECTX())
3033 DWriteContext_Flush(s_dwc);
3034#endif
3035
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 /*
3037 * Note: InvertRect() excludes right and bottom of rectangle.
3038 */
3039 rc.left = 0;
3040 rc.top = 0;
3041 rc.right = gui.num_cols * gui.char_width;
3042 rc.bottom = gui.num_rows * gui.char_height;
3043 InvertRect(s_hdc, &rc);
3044 gui_mch_flush(); /* make sure it's displayed */
3045
3046 ui_delay((long)msec, TRUE); /* wait for a few msec */
3047
3048 InvertRect(s_hdc, &rc);
3049}
3050
3051/*
3052 * Return flags used for scrolling.
3053 * The SW_INVALIDATE is required when part of the window is covered or
3054 * off-screen. Refer to MS KB Q75236.
3055 */
3056 static int
3057get_scroll_flags(void)
3058{
3059 HWND hwnd;
3060 RECT rcVim, rcOther, rcDest;
3061
3062 GetWindowRect(s_hwnd, &rcVim);
3063
3064 /* Check if the window is partly above or below the screen. We don't care
3065 * about partly left or right of the screen, it is not relevant when
3066 * scrolling up or down. */
3067 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3068 return SW_INVALIDATE;
3069
3070 /* Check if there is an window (partly) on top of us. */
3071 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3072 if (IsWindowVisible(hwnd))
3073 {
3074 GetWindowRect(hwnd, &rcOther);
3075 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3076 return SW_INVALIDATE;
3077 }
3078 return 0;
3079}
3080
3081/*
3082 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3083 * may not be scrolled out properly.
3084 * For gVim, when _OnScroll() is repeated, the character at the
3085 * previous cursor position may be left drawn after scroll.
3086 * The problem can be avoided by calling GetPixel() to get a pixel in
3087 * the region before ScrollWindowEx().
3088 */
3089 static void
3090intel_gpu_workaround(void)
3091{
3092 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3093}
3094
3095/*
3096 * Delete the given number of lines from the given row, scrolling up any
3097 * text further down within the scroll region.
3098 */
3099 void
3100gui_mch_delete_lines(
3101 int row,
3102 int num_lines)
3103{
3104 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003105
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003106 rc.left = FILL_X(gui.scroll_region_left);
3107 rc.right = FILL_X(gui.scroll_region_right + 1);
3108 rc.top = FILL_Y(row);
3109 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3110
Bram Moolenaar92467d32017-12-05 13:22:16 +01003111#if defined(FEAT_DIRECTX)
3112 if (IS_ENABLE_DIRECTX())
3113 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003114 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3115 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003116 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003117 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003118#endif
3119 {
3120 intel_gpu_workaround();
3121 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003122 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003123 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003124 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003125
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 /* This seems to be required to avoid the cursor disappearing when
3127 * scrolling such that the cursor ends up in the top-left character on
3128 * the screen... But why? (Webb) */
3129 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3130 /* gui.cursor_is_valid = FALSE; */
3131
3132 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3133 gui.scroll_region_left,
3134 gui.scroll_region_bot, gui.scroll_region_right);
3135}
3136
3137/*
3138 * Insert the given number of lines before the given row, scrolling down any
3139 * following text within the scroll region.
3140 */
3141 void
3142gui_mch_insert_lines(
3143 int row,
3144 int num_lines)
3145{
3146 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003147
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003148 rc.left = FILL_X(gui.scroll_region_left);
3149 rc.right = FILL_X(gui.scroll_region_right + 1);
3150 rc.top = FILL_Y(row);
3151 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003152
3153#if defined(FEAT_DIRECTX)
3154 if (IS_ENABLE_DIRECTX())
3155 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003156 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3157 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003158 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003159 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160#endif
3161 {
3162 intel_gpu_workaround();
3163 /* The SW_INVALIDATE is required when part of the window is covered or
3164 * off-screen. How do we avoid it when it's not needed? */
3165 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003166 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003167 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003169
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003170 gui_clear_block(row, gui.scroll_region_left,
3171 row + num_lines - 1, gui.scroll_region_right);
3172}
3173
3174
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003176gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177{
3178#if defined(FEAT_DIRECTX)
3179 DWriteContext_Close(s_dwc);
3180 DWrite_Final();
3181 s_dwc = NULL;
3182#endif
3183
3184 ReleaseDC(s_textArea, s_hdc);
3185 DeleteObject(s_brush);
3186
3187#ifdef FEAT_TEAROFF
3188 /* Unload the tearoff bitmap */
3189 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3190#endif
3191
3192 /* Destroy our window (if we have one). */
3193 if (s_hwnd != NULL)
3194 {
3195 destroying = TRUE; /* ignore WM_DESTROY message now */
3196 DestroyWindow(s_hwnd);
3197 }
3198
3199#ifdef GLOBAL_IME
3200 global_ime_end();
3201#endif
3202}
3203
3204 static char_u *
3205logfont2name(LOGFONT lf)
3206{
3207 char *p;
3208 char *res;
3209 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003210 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 char *font_name = lf.lfFaceName;
3212
3213 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 /* Convert a font name from the current codepage to 'encoding'.
3215 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3216 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3217 {
3218 int len;
3219 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3220 (char_u **)&font_name, &len);
3221 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003222 quality_name = quality_id2name((int)lf.lfQuality);
3223
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003224 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3225 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3226 if (res != NULL)
3227 {
3228 p = res;
3229 /* make a normal font string out of the lf thing:*/
3230 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3231 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3232 while (*p)
3233 {
3234 if (*p == ' ')
3235 *p = '_';
3236 ++p;
3237 }
3238 if (lf.lfItalic)
3239 STRCAT(p, ":i");
3240 if (lf.lfWeight >= FW_BOLD)
3241 STRCAT(p, ":b");
3242 if (lf.lfUnderline)
3243 STRCAT(p, ":u");
3244 if (lf.lfStrikeOut)
3245 STRCAT(p, ":s");
3246 if (charset_name != NULL)
3247 {
3248 STRCAT(p, ":c");
3249 STRCAT(p, charset_name);
3250 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003251 if (quality_name != NULL)
3252 {
3253 STRCAT(p, ":q");
3254 STRCAT(p, quality_name);
3255 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256 }
3257
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003258 if (font_name != lf.lfFaceName)
3259 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260 return (char_u *)res;
3261}
3262
3263
3264#ifdef FEAT_MBYTE_IME
3265/*
3266 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3267 * 'guifont'
3268 */
3269 static void
3270update_im_font(void)
3271{
3272 LOGFONT lf_wide;
3273
3274 if (p_guifontwide != NULL && *p_guifontwide != NUL
3275 && gui.wide_font != NOFONT
3276 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3277 norm_logfont = lf_wide;
3278 else
3279 norm_logfont = sub_logfont;
3280 im_set_font(&norm_logfont);
3281}
3282#endif
3283
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284/*
3285 * Handler of gui.wide_font (p_guifontwide) changed notification.
3286 */
3287 void
3288gui_mch_wide_font_changed(void)
3289{
3290 LOGFONT lf;
3291
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003292#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003293 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003294#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295
3296 gui_mch_free_font(gui.wide_ital_font);
3297 gui.wide_ital_font = NOFONT;
3298 gui_mch_free_font(gui.wide_bold_font);
3299 gui.wide_bold_font = NOFONT;
3300 gui_mch_free_font(gui.wide_boldital_font);
3301 gui.wide_boldital_font = NOFONT;
3302
3303 if (gui.wide_font
3304 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3305 {
3306 if (!lf.lfItalic)
3307 {
3308 lf.lfItalic = TRUE;
3309 gui.wide_ital_font = get_font_handle(&lf);
3310 lf.lfItalic = FALSE;
3311 }
3312 if (lf.lfWeight < FW_BOLD)
3313 {
3314 lf.lfWeight = FW_BOLD;
3315 gui.wide_bold_font = get_font_handle(&lf);
3316 if (!lf.lfItalic)
3317 {
3318 lf.lfItalic = TRUE;
3319 gui.wide_boldital_font = get_font_handle(&lf);
3320 }
3321 }
3322 }
3323}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003324
3325/*
3326 * Initialise vim to use the font with the given name.
3327 * Return FAIL if the font could not be loaded, OK otherwise.
3328 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003329 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003330gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331{
3332 LOGFONT lf;
3333 GuiFont font = NOFONT;
3334 char_u *p;
3335
3336 /* Load the font */
3337 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3338 font = get_font_handle(&lf);
3339 if (font == NOFONT)
3340 return FAIL;
3341
3342 if (font_name == NULL)
3343 font_name = (char_u *)lf.lfFaceName;
3344#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3345 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003346#endif
3347#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348 sub_logfont = lf;
3349#endif
3350#ifdef FEAT_MBYTE_IME
3351 update_im_font();
3352#endif
3353 gui_mch_free_font(gui.norm_font);
3354 gui.norm_font = font;
3355 current_font_height = lf.lfHeight;
3356 GetFontSize(font);
3357
3358 p = logfont2name(lf);
3359 if (p != NULL)
3360 {
3361 hl_set_font_name(p);
3362
3363 /* When setting 'guifont' to "*" replace it with the actual font name.
3364 * */
3365 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3366 {
3367 vim_free(p_guifont);
3368 p_guifont = p;
3369 }
3370 else
3371 vim_free(p);
3372 }
3373
3374 gui_mch_free_font(gui.ital_font);
3375 gui.ital_font = NOFONT;
3376 gui_mch_free_font(gui.bold_font);
3377 gui.bold_font = NOFONT;
3378 gui_mch_free_font(gui.boldital_font);
3379 gui.boldital_font = NOFONT;
3380
3381 if (!lf.lfItalic)
3382 {
3383 lf.lfItalic = TRUE;
3384 gui.ital_font = get_font_handle(&lf);
3385 lf.lfItalic = FALSE;
3386 }
3387 if (lf.lfWeight < FW_BOLD)
3388 {
3389 lf.lfWeight = FW_BOLD;
3390 gui.bold_font = get_font_handle(&lf);
3391 if (!lf.lfItalic)
3392 {
3393 lf.lfItalic = TRUE;
3394 gui.boldital_font = get_font_handle(&lf);
3395 }
3396 }
3397
3398 return OK;
3399}
3400
3401#ifndef WPF_RESTORETOMAXIMIZED
3402# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3403#endif
3404
3405/*
3406 * Return TRUE if the GUI window is maximized, filling the whole screen.
3407 */
3408 int
3409gui_mch_maximized(void)
3410{
3411 WINDOWPLACEMENT wp;
3412
3413 wp.length = sizeof(WINDOWPLACEMENT);
3414 if (GetWindowPlacement(s_hwnd, &wp))
3415 return wp.showCmd == SW_SHOWMAXIMIZED
3416 || (wp.showCmd == SW_SHOWMINIMIZED
3417 && wp.flags == WPF_RESTORETOMAXIMIZED);
3418
3419 return 0;
3420}
3421
3422/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003423 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3424 * is set. Compute the new Rows and Columns. This is like resizing the
3425 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426 */
3427 void
3428gui_mch_newfont(void)
3429{
3430 RECT rect;
3431
3432 GetWindowRect(s_hwnd, &rect);
3433 if (win_socket_id == 0)
3434 {
3435 gui_resize_shell(rect.right - rect.left
3436 - (GetSystemMetrics(SM_CXFRAME) +
3437 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3438 rect.bottom - rect.top
3439 - (GetSystemMetrics(SM_CYFRAME) +
3440 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3441 - GetSystemMetrics(SM_CYCAPTION)
3442#ifdef FEAT_MENU
3443 - gui_mswin_get_menu_height(FALSE)
3444#endif
3445 );
3446 }
3447 else
3448 {
3449 /* Inside another window, don't use the frame and border. */
3450 gui_resize_shell(rect.right - rect.left,
3451 rect.bottom - rect.top
3452#ifdef FEAT_MENU
3453 - gui_mswin_get_menu_height(FALSE)
3454#endif
3455 );
3456 }
3457}
3458
3459/*
3460 * Set the window title
3461 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003462 void
3463gui_mch_settitle(
3464 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003465 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466{
3467 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3468}
3469
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003470#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003471/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3472 * misc2.c! */
3473static LPCSTR mshape_idcs[] =
3474{
3475 IDC_ARROW, /* arrow */
3476 MAKEINTRESOURCE(0), /* blank */
3477 IDC_IBEAM, /* beam */
3478 IDC_SIZENS, /* updown */
3479 IDC_SIZENS, /* udsizing */
3480 IDC_SIZEWE, /* leftright */
3481 IDC_SIZEWE, /* lrsizing */
3482 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484 IDC_ARROW, /* crosshair */
3485 IDC_ARROW, /* hand1 */
3486 IDC_ARROW, /* hand2 */
3487 IDC_ARROW, /* pencil */
3488 IDC_ARROW, /* question */
3489 IDC_ARROW, /* right-arrow */
3490 IDC_UPARROW, /* up-arrow */
3491 IDC_ARROW /* last one */
3492};
3493
3494 void
3495mch_set_mouse_shape(int shape)
3496{
3497 LPCSTR idc;
3498
3499 if (shape == MSHAPE_HIDE)
3500 ShowCursor(FALSE);
3501 else
3502 {
3503 if (shape >= MSHAPE_NUMBERED)
3504 idc = IDC_ARROW;
3505 else
3506 idc = mshape_idcs[shape];
3507#ifdef SetClassLongPtr
3508 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3509#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003510 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511#endif
3512 if (!p_mh)
3513 {
3514 POINT mp;
3515
3516 /* Set the position to make it redrawn with the new shape. */
3517 (void)GetCursorPos((LPPOINT)&mp);
3518 (void)SetCursorPos(mp.x, mp.y);
3519 ShowCursor(TRUE);
3520 }
3521 }
3522}
3523#endif
3524
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003525#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003526/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 * Wide version of convert_filter().
3528 */
3529 static WCHAR *
3530convert_filterW(char_u *s)
3531{
3532 char_u *tmp;
3533 int len;
3534 WCHAR *res;
3535
3536 tmp = convert_filter(s);
3537 if (tmp == NULL)
3538 return NULL;
3539 len = (int)STRLEN(s) + 3;
3540 res = enc_to_utf16(tmp, &len);
3541 vim_free(tmp);
3542 return res;
3543}
3544
3545/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003546 * Pop open a file browser and return the file selected, in allocated memory,
3547 * or NULL if Cancel is hit.
3548 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3549 * title - Title message for the file browser dialog.
3550 * dflt - Default name of file.
3551 * ext - Default extension to be added to files without extensions.
3552 * initdir - directory in which to open the browser (NULL = current dir)
3553 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003554 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003555 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003556gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557 int saving,
3558 char_u *title,
3559 char_u *dflt,
3560 char_u *ext,
3561 char_u *initdir,
3562 char_u *filter)
3563{
3564 /* We always use the wide function. This means enc_to_utf16() must work,
3565 * otherwise it fails miserably! */
3566 OPENFILENAMEW fileStruct;
3567 WCHAR fileBuf[MAXPATHL];
3568 WCHAR *wp;
3569 int i;
3570 WCHAR *titlep = NULL;
3571 WCHAR *extp = NULL;
3572 WCHAR *initdirp = NULL;
3573 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003574 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575
3576 if (dflt == NULL)
3577 fileBuf[0] = NUL;
3578 else
3579 {
3580 wp = enc_to_utf16(dflt, NULL);
3581 if (wp == NULL)
3582 fileBuf[0] = NUL;
3583 else
3584 {
3585 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3586 fileBuf[i] = wp[i];
3587 fileBuf[i] = NUL;
3588 vim_free(wp);
3589 }
3590 }
3591
3592 /* Convert the filter to Windows format. */
3593 filterp = convert_filterW(filter);
3594
3595 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003596# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003598 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003599# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003601# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003602
3603 if (title != NULL)
3604 titlep = enc_to_utf16(title, NULL);
3605 fileStruct.lpstrTitle = titlep;
3606
3607 if (ext != NULL)
3608 extp = enc_to_utf16(ext, NULL);
3609 fileStruct.lpstrDefExt = extp;
3610
3611 fileStruct.lpstrFile = fileBuf;
3612 fileStruct.nMaxFile = MAXPATHL;
3613 fileStruct.lpstrFilter = filterp;
3614 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3615 /* has an initial dir been specified? */
3616 if (initdir != NULL && *initdir != NUL)
3617 {
3618 /* Must have backslashes here, no matter what 'shellslash' says */
3619 initdirp = enc_to_utf16(initdir, NULL);
3620 if (initdirp != NULL)
3621 {
3622 for (wp = initdirp; *wp != NUL; ++wp)
3623 if (*wp == '/')
3624 *wp = '\\';
3625 }
3626 fileStruct.lpstrInitialDir = initdirp;
3627 }
3628
3629 /*
3630 * TODO: Allow selection of multiple files. Needs another arg to this
3631 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3632 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3633 * files that don't exist yet, so I haven't put it in. What about
3634 * OFN_PATHMUSTEXIST?
3635 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3636 */
3637 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003638# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639 if (curbuf->b_p_bin)
3640 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003641# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003642 if (saving)
3643 {
3644 if (!GetSaveFileNameW(&fileStruct))
3645 return NULL;
3646 }
3647 else
3648 {
3649 if (!GetOpenFileNameW(&fileStruct))
3650 return NULL;
3651 }
3652
3653 vim_free(filterp);
3654 vim_free(initdirp);
3655 vim_free(titlep);
3656 vim_free(extp);
3657
3658 /* Convert from UCS2 to 'encoding'. */
3659 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003660 if (p == NULL)
3661 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662
3663 /* Give focus back to main window (when using MDI). */
3664 SetFocus(s_hwnd);
3665
3666 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003667 q = vim_strsave(shorten_fname1(p));
3668 vim_free(p);
3669 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671
3672
3673/*
3674 * Convert the string s to the proper format for a filter string by replacing
3675 * the \t and \n delimiters with \0.
3676 * Returns the converted string in allocated memory.
3677 *
3678 * Keep in sync with convert_filterW() above!
3679 */
3680 static char_u *
3681convert_filter(char_u *s)
3682{
3683 char_u *res;
3684 unsigned s_len = (unsigned)STRLEN(s);
3685 unsigned i;
3686
3687 res = alloc(s_len + 3);
3688 if (res != NULL)
3689 {
3690 for (i = 0; i < s_len; ++i)
3691 if (s[i] == '\t' || s[i] == '\n')
3692 res[i] = '\0';
3693 else
3694 res[i] = s[i];
3695 res[s_len] = NUL;
3696 /* Add two extra NULs to make sure it's properly terminated. */
3697 res[s_len + 1] = NUL;
3698 res[s_len + 2] = NUL;
3699 }
3700 return res;
3701}
3702
3703/*
3704 * Select a directory.
3705 */
3706 char_u *
3707gui_mch_browsedir(char_u *title, char_u *initdir)
3708{
3709 /* We fake this: Use a filter that doesn't select anything and a default
3710 * file name that won't be used. */
3711 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3712 initdir, (char_u *)_("Directory\t*.nothing\n"));
3713}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714#endif /* FEAT_BROWSE */
3715
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 static void
3717_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003718 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003719 HDROP hDrop)
3720{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003721#define BUFPATHLEN _MAX_PATH
3722#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003723 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003724 char szFile[BUFPATHLEN];
3725 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3726 UINT i;
3727 char_u **fnames;
3728 POINT pt;
3729 int_u modifiers = 0;
3730
3731 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3732
3733 /* Obtain dropped position */
3734 DragQueryPoint(hDrop, &pt);
3735 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3736
3737 reset_VIsual();
3738
3739 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3740
3741 if (fnames != NULL)
3742 for (i = 0; i < cFiles; ++i)
3743 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003744 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3745 fnames[i] = utf16_to_enc(wszFile, NULL);
3746 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003747 {
3748 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3749 fnames[i] = vim_strsave((char_u *)szFile);
3750 }
3751 }
3752
3753 DragFinish(hDrop);
3754
3755 if (fnames != NULL)
3756 {
3757 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3758 modifiers |= MOUSE_SHIFT;
3759 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3760 modifiers |= MOUSE_CTRL;
3761 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3762 modifiers |= MOUSE_ALT;
3763
3764 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3765
3766 s_need_activate = TRUE;
3767 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003768}
3769
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 static int
3771_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003772 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 HWND hwndCtl,
3774 UINT code,
3775 int pos)
3776{
3777 static UINT prev_code = 0; /* code of previous call */
3778 scrollbar_T *sb, *sb_info;
3779 long val;
3780 int dragging = FALSE;
3781 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 SCROLLINFO si;
3783
3784 si.cbSize = sizeof(si);
3785 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786
3787 sb = gui_mswin_find_scrollbar(hwndCtl);
3788 if (sb == NULL)
3789 return 0;
3790
3791 if (sb->wp != NULL) /* Left or right scrollbar */
3792 {
3793 /*
3794 * Careful: need to get scrollbar info out of first (left) scrollbar
3795 * for window, but keep real scrollbar too because we must pass it to
3796 * gui_drag_scrollbar().
3797 */
3798 sb_info = &sb->wp->w_scrollbars[0];
3799 }
3800 else /* Bottom scrollbar */
3801 sb_info = sb;
3802 val = sb_info->value;
3803
3804 switch (code)
3805 {
3806 case SB_THUMBTRACK:
3807 val = pos;
3808 dragging = TRUE;
3809 if (sb->scroll_shift > 0)
3810 val <<= sb->scroll_shift;
3811 break;
3812 case SB_LINEDOWN:
3813 val++;
3814 break;
3815 case SB_LINEUP:
3816 val--;
3817 break;
3818 case SB_PAGEDOWN:
3819 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3820 break;
3821 case SB_PAGEUP:
3822 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3823 break;
3824 case SB_TOP:
3825 val = 0;
3826 break;
3827 case SB_BOTTOM:
3828 val = sb_info->max;
3829 break;
3830 case SB_ENDSCROLL:
3831 if (prev_code == SB_THUMBTRACK)
3832 {
3833 /*
3834 * "pos" only gives us 16-bit data. In case of large file,
3835 * use GetScrollPos() which returns 32-bit. Unfortunately it
3836 * is not valid while the scrollbar is being dragged.
3837 */
3838 val = GetScrollPos(hwndCtl, SB_CTL);
3839 if (sb->scroll_shift > 0)
3840 val <<= sb->scroll_shift;
3841 }
3842 break;
3843
3844 default:
3845 /* TRACE("Unknown scrollbar event %d\n", code); */
3846 return 0;
3847 }
3848 prev_code = code;
3849
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3851 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852
3853 /*
3854 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3855 */
3856 if (sb->wp != NULL)
3857 {
3858 scrollbar_T *sba = sb->wp->w_scrollbars;
3859 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3860
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 }
3863
3864 /* Don't let us be interrupted here by another message. */
3865 s_busy_processing = TRUE;
3866
3867 /* When "allow_scrollbar" is FALSE still need to remember the new
3868 * position, but don't actually scroll by setting "dont_scroll". */
3869 dont_scroll = !allow_scrollbar;
3870
Bram Moolenaara338adc2018-01-31 20:51:47 +01003871 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003873 mch_enable_flush();
3874 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875
3876 s_busy_processing = FALSE;
3877 dont_scroll = dont_scroll_save;
3878
3879 return 0;
3880}
3881
3882
3883/*
3884 * Get command line arguments.
3885 * Use "prog" as the name of the program and "cmdline" as the arguments.
3886 * Copy the arguments to allocated memory.
3887 * Return the number of arguments (including program name).
3888 * Return pointers to the arguments in "argvp". Memory is allocated with
3889 * malloc(), use free() instead of vim_free().
3890 * Return pointer to buffer in "tofree".
3891 * Returns zero when out of memory.
3892 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893 int
3894get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
3895{
3896 int i;
3897 char *p;
3898 char *progp;
3899 char *pnew = NULL;
3900 char *newcmdline;
3901 int inquote;
3902 int argc;
3903 char **argv = NULL;
3904 int round;
3905
3906 *tofree = NULL;
3907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 /* Try using the Unicode version first, it takes care of conversion when
3909 * 'encoding' is changed. */
3910 argc = get_cmd_argsW(&argv);
3911 if (argc != 0)
3912 goto done;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003913
3914 /* Handle the program name. Remove the ".exe" extension, and find the 1st
3915 * non-space. */
3916 p = strrchr(prog, '.');
3917 if (p != NULL)
3918 *p = NUL;
3919 for (progp = prog; *progp == ' '; ++progp)
3920 ;
3921
3922 /* The command line is copied to allocated memory, so that we can change
3923 * it. Add the size of the string, the separating NUL and a terminating
3924 * NUL. */
3925 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
3926 if (newcmdline == NULL)
3927 return 0;
3928
3929 /*
3930 * First round: count the number of arguments ("pnew" == NULL).
3931 * Second round: produce the arguments.
3932 */
3933 for (round = 1; round <= 2; ++round)
3934 {
3935 /* First argument is the program name. */
3936 if (pnew != NULL)
3937 {
3938 argv[0] = pnew;
3939 strcpy(pnew, progp);
3940 pnew += strlen(pnew);
3941 *pnew++ = NUL;
3942 }
3943
3944 /*
3945 * Isolate each argument and put it in argv[].
3946 */
3947 p = cmdline;
3948 argc = 1;
3949 while (*p != NUL)
3950 {
3951 inquote = FALSE;
3952 if (pnew != NULL)
3953 argv[argc] = pnew;
3954 ++argc;
3955 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
3956 {
3957 /* Backslashes are only special when followed by a double
3958 * quote. */
3959 i = (int)strspn(p, "\\");
3960 if (p[i] == '"')
3961 {
3962 /* Halve the number of backslashes. */
3963 if (i > 1 && pnew != NULL)
3964 {
3965 vim_memset(pnew, '\\', i / 2);
3966 pnew += i / 2;
3967 }
3968
3969 /* Even nr of backslashes toggles quoting, uneven copies
3970 * the double quote. */
3971 if ((i & 1) == 0)
3972 inquote = !inquote;
3973 else if (pnew != NULL)
3974 *pnew++ = '"';
3975 p += i + 1;
3976 }
3977 else if (i > 0)
3978 {
3979 /* Copy span of backslashes unmodified. */
3980 if (pnew != NULL)
3981 {
3982 vim_memset(pnew, '\\', i);
3983 pnew += i;
3984 }
3985 p += i;
3986 }
3987 else
3988 {
3989 if (pnew != NULL)
3990 *pnew++ = *p;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003991 /* Can't use mb_* functions, because 'encoding' is not
3992 * initialized yet here. */
3993 if (IsDBCSLeadByte(*p))
3994 {
3995 ++p;
3996 if (pnew != NULL)
3997 *pnew++ = *p;
3998 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003999 ++p;
4000 }
4001 }
4002
4003 if (pnew != NULL)
4004 *pnew++ = NUL;
4005 while (*p == ' ' || *p == '\t')
4006 ++p; /* advance until a non-space */
4007 }
4008
4009 if (round == 1)
4010 {
4011 argv = (char **)malloc((argc + 1) * sizeof(char *));
4012 if (argv == NULL )
4013 {
4014 free(newcmdline);
4015 return 0; /* malloc error */
4016 }
4017 pnew = newcmdline;
4018 *tofree = newcmdline;
4019 }
4020 }
4021
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004022done:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023 argv[argc] = NULL; /* NULL-terminated list */
4024 *argvp = argv;
4025 return argc;
4026}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028#ifdef FEAT_XPM_W32
4029# include "xpm_w32.h"
4030#endif
4031
4032#ifdef PROTO
4033# define WINAPI
4034#endif
4035
4036#ifdef __MINGW32__
4037/*
4038 * Add a lot of missing defines.
4039 * They are not always missing, we need the #ifndef's.
4040 */
4041# ifndef _cdecl
4042# define _cdecl
4043# endif
4044# ifndef IsMinimized
4045# define IsMinimized(hwnd) IsIconic(hwnd)
4046# endif
4047# ifndef IsMaximized
4048# define IsMaximized(hwnd) IsZoomed(hwnd)
4049# endif
4050# ifndef SelectFont
4051# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4052# endif
4053# ifndef GetStockBrush
4054# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4055# endif
4056# ifndef DeleteBrush
4057# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4058# endif
4059
4060# ifndef HANDLE_WM_RBUTTONDBLCLK
4061# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4062 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4063# endif
4064# ifndef HANDLE_WM_MBUTTONUP
4065# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4066 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4067# endif
4068# ifndef HANDLE_WM_MBUTTONDBLCLK
4069# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4070 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4071# endif
4072# ifndef HANDLE_WM_LBUTTONDBLCLK
4073# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4074 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4075# endif
4076# ifndef HANDLE_WM_RBUTTONDOWN
4077# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4078 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4079# endif
4080# ifndef HANDLE_WM_MOUSEMOVE
4081# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4082 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4083# endif
4084# ifndef HANDLE_WM_RBUTTONUP
4085# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4086 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4087# endif
4088# ifndef HANDLE_WM_MBUTTONDOWN
4089# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4090 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4091# endif
4092# ifndef HANDLE_WM_LBUTTONUP
4093# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4094 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4095# endif
4096# ifndef HANDLE_WM_LBUTTONDOWN
4097# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4098 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4099# endif
4100# ifndef HANDLE_WM_SYSCHAR
4101# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4102 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4103# endif
4104# ifndef HANDLE_WM_ACTIVATEAPP
4105# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4106 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4107# endif
4108# ifndef HANDLE_WM_WINDOWPOSCHANGING
4109# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4110 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4111# endif
4112# ifndef HANDLE_WM_VSCROLL
4113# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4114 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4115# endif
4116# ifndef HANDLE_WM_SETFOCUS
4117# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4118 ((fn)((hwnd), (HWND)(wParam)), 0L)
4119# endif
4120# ifndef HANDLE_WM_KILLFOCUS
4121# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4122 ((fn)((hwnd), (HWND)(wParam)), 0L)
4123# endif
4124# ifndef HANDLE_WM_HSCROLL
4125# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4126 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4127# endif
4128# ifndef HANDLE_WM_DROPFILES
4129# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4130 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4131# endif
4132# ifndef HANDLE_WM_CHAR
4133# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4134 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4135# endif
4136# ifndef HANDLE_WM_SYSDEADCHAR
4137# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4138 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4139# endif
4140# ifndef HANDLE_WM_DEADCHAR
4141# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4142 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4143# endif
4144#endif /* __MINGW32__ */
4145
4146
4147/* Some parameters for tearoff menus. All in pixels. */
4148#define TEAROFF_PADDING_X 2
4149#define TEAROFF_BUTTON_PAD_X 8
4150#define TEAROFF_MIN_WIDTH 200
4151#define TEAROFF_SUBMENU_LABEL ">>"
4152#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4153
4154
4155/* For the Intellimouse: */
4156#ifndef WM_MOUSEWHEEL
4157#define WM_MOUSEWHEEL 0x20a
4158#endif
4159
4160
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004161#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162# define ID_BEVAL_TOOLTIP 200
4163# define BEVAL_TEXT_LEN MAXPATHL
4164
Bram Moolenaar167632f2010-05-26 21:42:54 +02004165#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004166/* Work around old versions of basetsd.h which wrongly declares
4167 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004168# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004169# define UINT_PTR UINT
4170#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004173static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004175
Bram Moolenaar82881492012-11-20 16:53:39 +01004176
4177/* cproto fails on missing include files */
4178#ifndef PROTO
4179
Bram Moolenaar45360022005-07-21 21:08:21 +00004180/*
4181 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004182 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004183 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004184# include <pshpack1.h>
4185
4186#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004187
4188typedef struct _DllVersionInfo
4189{
4190 DWORD cbSize;
4191 DWORD dwMajorVersion;
4192 DWORD dwMinorVersion;
4193 DWORD dwBuildNumber;
4194 DWORD dwPlatformID;
4195} DLLVERSIONINFO;
4196
Bram Moolenaar82881492012-11-20 16:53:39 +01004197#ifndef PROTO
4198# include <poppack.h>
4199#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004200
Bram Moolenaar45360022005-07-21 21:08:21 +00004201typedef struct tagTOOLINFOA_NEW
4202{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004203 UINT cbSize;
4204 UINT uFlags;
4205 HWND hwnd;
4206 UINT_PTR uId;
4207 RECT rect;
4208 HINSTANCE hinst;
4209 LPSTR lpszText;
4210 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004211} TOOLINFO_NEW;
4212
4213typedef struct tagNMTTDISPINFO_NEW
4214{
4215 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004216 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004217 char szText[80];
4218 HINSTANCE hinst;
4219 UINT uFlags;
4220 LPARAM lParam;
4221} NMTTDISPINFO_NEW;
4222
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004223typedef struct tagTOOLINFOW_NEW
4224{
4225 UINT cbSize;
4226 UINT uFlags;
4227 HWND hwnd;
4228 UINT_PTR uId;
4229 RECT rect;
4230 HINSTANCE hinst;
4231 LPWSTR lpszText;
4232 LPARAM lParam;
4233 void *lpReserved;
4234} TOOLINFOW_NEW;
4235
4236typedef struct tagNMTTDISPINFOW_NEW
4237{
4238 NMHDR hdr;
4239 LPWSTR lpszText;
4240 WCHAR szText[80];
4241 HINSTANCE hinst;
4242 UINT uFlags;
4243 LPARAM lParam;
4244} NMTTDISPINFOW_NEW;
4245
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004246
Bram Moolenaar45360022005-07-21 21:08:21 +00004247typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4248#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004249# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250#endif
4251
Bram Moolenaar45360022005-07-21 21:08:21 +00004252#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004253# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004254#endif
4255
4256#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004257# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004258#endif
4259
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004260#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004261
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004262#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4263/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4264 * it here if LPNMTTDISPINFO isn't defined.
4265 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4266 * _MSC_VER. */
4267# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4268typedef struct tagNMTTDISPINFOA {
4269 NMHDR hdr;
4270 LPSTR lpszText;
4271 char szText[80];
4272 HINSTANCE hinst;
4273 UINT uFlags;
4274 LPARAM lParam;
4275} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4276# define LPNMTTDISPINFO LPNMTTDISPINFOA
4277
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004278typedef struct tagNMTTDISPINFOW {
4279 NMHDR hdr;
4280 LPWSTR lpszText;
4281 WCHAR szText[80];
4282 HINSTANCE hinst;
4283 UINT uFlags;
4284 LPARAM lParam;
4285} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004286# endif
4287#endif
4288
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004289#ifndef TTN_GETDISPINFOW
4290# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4291#endif
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293/* Local variables: */
4294
4295#ifdef FEAT_MENU
4296static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299/*
4300 * Use the system font for dialogs and tear-off menus. Remove this line to
4301 * use DLG_FONT_NAME.
4302 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004303#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
4305#define VIM_NAME "vim"
4306#define VIM_CLASS "Vim"
4307#define VIM_CLASSW L"Vim"
4308
4309/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4310 * thus there should be room for every dialog. For tearoffs it's made bigger
4311 * when needed. */
4312#define DLG_ALLOC_SIZE 16 * 1024
4313
4314/*
4315 * stuff for dialogs, menus, tearoffs etc.
4316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317static PWORD
4318add_dialog_element(
4319 PWORD p,
4320 DWORD lStyle,
4321 WORD x,
4322 WORD y,
4323 WORD w,
4324 WORD h,
4325 WORD Id,
4326 WORD clss,
4327 const char *caption);
4328static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004329static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004330#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333static void get_dialog_font_metrics(void);
4334
4335static int dialog_default_button = -1;
4336
4337/* Intellimouse support */
4338static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4341#ifdef FEAT_TOOLBAR
4342static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004343static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344static int get_toolbar_bitmap(vimmenu_T *menu);
4345#endif
4346
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004347#ifdef FEAT_GUI_TABLINE
4348static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004349static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004350#endif
4351
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352#ifdef FEAT_MBYTE_IME
4353static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4354static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4355#endif
4356#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4357# ifdef NOIME
4358typedef struct tagCOMPOSITIONFORM {
4359 DWORD dwStyle;
4360 POINT ptCurrentPos;
4361 RECT rcArea;
4362} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4363typedef HANDLE HIMC;
4364# endif
4365
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004366static HINSTANCE hLibImm = NULL;
4367static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4368static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4369static HIMC (WINAPI *pImmGetContext)(HWND);
4370static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4371static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4372static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4373static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4374static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4375static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4376static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4377static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004378static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379static void dyn_imm_load(void);
4380#else
4381# define pImmGetCompositionStringA ImmGetCompositionStringA
4382# define pImmGetCompositionStringW ImmGetCompositionStringW
4383# define pImmGetContext ImmGetContext
4384# define pImmAssociateContext ImmAssociateContext
4385# define pImmReleaseContext ImmReleaseContext
4386# define pImmGetOpenStatus ImmGetOpenStatus
4387# define pImmSetOpenStatus ImmSetOpenStatus
4388# define pImmGetCompositionFont ImmGetCompositionFontA
4389# define pImmSetCompositionFont ImmSetCompositionFontA
4390# define pImmSetCompositionWindow ImmSetCompositionWindow
4391# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004392# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#endif
4394
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#ifdef FEAT_MENU
4396/*
4397 * Figure out how high the menu bar is at the moment.
4398 */
4399 static int
4400gui_mswin_get_menu_height(
4401 int fix_window) /* If TRUE, resize window if menu height changed */
4402{
4403 static int old_menu_height = -1;
4404
4405 RECT rc1, rc2;
4406 int num;
4407 int menu_height;
4408
4409 if (gui.menu_is_active)
4410 num = GetMenuItemCount(s_menuBar);
4411 else
4412 num = 0;
4413
4414 if (num == 0)
4415 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004416 else if (IsMinimized(s_hwnd))
4417 {
4418 /* The height of the menu cannot be determined while the window is
4419 * minimized. Take the previous height if the menu is changed in that
4420 * state, to avoid that Vim's vertical window size accidentally
4421 * increases due to the unaccounted-for menu height. */
4422 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 else
4425 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004426 /*
4427 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4428 * seem to have been set yet, so menu wraps in default window
4429 * width which is very narrow. Instead just return height of a
4430 * single menu item. Will still be wrong when the menu really
4431 * should wrap over more than one line.
4432 */
4433 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4434 if (gui.starting)
4435 menu_height = rc1.bottom - rc1.top + 1;
4436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004438 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4439 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
4442
4443 if (fix_window && menu_height != old_menu_height)
4444 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004445 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004447 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 return menu_height;
4450}
4451#endif /*FEAT_MENU*/
4452
4453
4454/*
4455 * Setup for the Intellimouse
4456 */
4457 static void
4458init_mouse_wheel(void)
4459{
4460
4461#ifndef SPI_GETWHEELSCROLLLINES
4462# define SPI_GETWHEELSCROLLLINES 104
4463#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004464#ifndef SPI_SETWHEELSCROLLLINES
4465# define SPI_SETWHEELSCROLLLINES 105
4466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4469#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4470#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4471#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4472
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 mouse_scroll_lines = 3; /* reasonable default */
4474
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004475 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4476 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4477 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478}
4479
4480
4481/* Intellimouse wheel handler */
4482 static void
4483_OnMouseWheel(
4484 HWND hwnd,
4485 short zDelta)
4486{
4487/* Treat a mouse wheel event as if it were a scroll request */
4488 int i;
4489 int size;
4490 HWND hwndCtl;
4491
4492 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4493 {
4494 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4495 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4496 }
4497 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4498 {
4499 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4500 size = curwin->w_scrollbars[SBAR_LEFT].size;
4501 }
4502 else
4503 return;
4504
4505 size = curwin->w_height;
4506 if (mouse_scroll_lines == 0)
4507 init_mouse_wheel();
4508
Bram Moolenaara338adc2018-01-31 20:51:47 +01004509 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (mouse_scroll_lines > 0
4511 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4512 {
4513 for (i = mouse_scroll_lines; i > 0; --i)
4514 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4515 }
4516 else
4517 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004518 mch_enable_flush();
4519 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520}
4521
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004522#ifdef USE_SYSMENU_FONT
4523/*
4524 * Get Menu Font.
4525 * Return OK or FAIL.
4526 */
4527 static int
4528gui_w32_get_menu_font(LOGFONT *lf)
4529{
4530 NONCLIENTMETRICS nm;
4531
4532 nm.cbSize = sizeof(NONCLIENTMETRICS);
4533 if (!SystemParametersInfo(
4534 SPI_GETNONCLIENTMETRICS,
4535 sizeof(NONCLIENTMETRICS),
4536 &nm,
4537 0))
4538 return FAIL;
4539 *lf = nm.lfMenuFont;
4540 return OK;
4541}
4542#endif
4543
4544
4545#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4546/*
4547 * Set the GUI tabline font to the system menu font
4548 */
4549 static void
4550set_tabline_font(void)
4551{
4552 LOGFONT lfSysmenu;
4553 HFONT font;
4554 HWND hwnd;
4555 HDC hdc;
4556 HFONT hfntOld;
4557 TEXTMETRIC tm;
4558
4559 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4560 return;
4561
4562 font = CreateFontIndirect(&lfSysmenu);
4563
4564 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4565
4566 /*
4567 * Compute the height of the font used for the tab text
4568 */
4569 hwnd = GetDesktopWindow();
4570 hdc = GetWindowDC(hwnd);
4571 hfntOld = SelectFont(hdc, font);
4572
4573 GetTextMetrics(hdc, &tm);
4574
4575 SelectFont(hdc, hfntOld);
4576 ReleaseDC(hwnd, hdc);
4577
4578 /*
4579 * The space used by the tab border and the space between the tab label
4580 * and the tab border is included as 7.
4581 */
4582 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4583}
4584#endif
4585
Bram Moolenaar520470a2005-06-16 21:59:56 +00004586/*
4587 * Invoked when a setting was changed.
4588 */
4589 static LRESULT CALLBACK
4590_OnSettingChange(UINT n)
4591{
4592 if (n == SPI_SETWHEELSCROLLLINES)
4593 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4594 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004595#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4596 if (n == SPI_SETNONCLIENTMETRICS)
4597 set_tabline_font();
4598#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004599 return 0;
4600}
4601
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_NETBEANS_INTG
4603 static void
4604_OnWindowPosChanged(
4605 HWND hwnd,
4606 const LPWINDOWPOS lpwpos)
4607{
4608 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004609 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4612 || lpwpos->cx != cx || lpwpos->cy != cy))
4613 {
4614 x = lpwpos->x;
4615 y = lpwpos->y;
4616 cx = lpwpos->cx;
4617 cy = lpwpos->cy;
4618 netbeans_frame_moved(x, y);
4619 }
4620 /* Allow to send WM_SIZE and WM_MOVE */
4621 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4622}
4623#endif
4624
4625 static int
4626_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 UINT fwSide,
4628 LPRECT lprc)
4629{
4630 int w, h;
4631 int valid_w, valid_h;
4632 int w_offset, h_offset;
4633
4634 w = lprc->right - lprc->left;
4635 h = lprc->bottom - lprc->top;
4636 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4637 w_offset = w - valid_w;
4638 h_offset = h - valid_h;
4639
4640 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4641 || fwSide == WMSZ_BOTTOMLEFT)
4642 lprc->left += w_offset;
4643 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4644 || fwSide == WMSZ_BOTTOMRIGHT)
4645 lprc->right -= w_offset;
4646
4647 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4648 || fwSide == WMSZ_TOPRIGHT)
4649 lprc->top += h_offset;
4650 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4651 || fwSide == WMSZ_BOTTOMRIGHT)
4652 lprc->bottom -= h_offset;
4653 return TRUE;
4654}
4655
4656
4657
4658 static LRESULT CALLBACK
4659_WndProc(
4660 HWND hwnd,
4661 UINT uMsg,
4662 WPARAM wParam,
4663 LPARAM lParam)
4664{
4665 /*
4666 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4667 hwnd, uMsg, wParam, lParam);
4668 */
4669
4670 HandleMouseHide(uMsg, lParam);
4671
4672 s_uMsg = uMsg;
4673 s_wParam = wParam;
4674 s_lParam = lParam;
4675
4676 switch (uMsg)
4677 {
4678 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4679 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4680 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4681 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4682 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4683 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4684 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4685 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4686 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4687#ifdef FEAT_MENU
4688 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4689#endif
4690 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4691 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4692 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4693 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4694 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4695 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4696 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4697 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4698 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4699#ifdef FEAT_NETBEANS_INTG
4700 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4701#endif
4702
Bram Moolenaarafa24992006-03-27 20:58:26 +00004703#ifdef FEAT_GUI_TABLINE
4704 case WM_RBUTTONUP:
4705 {
4706 if (gui_mch_showing_tabline())
4707 {
4708 POINT pt;
4709 RECT rect;
4710
4711 /*
4712 * If the cursor is on the tabline, display the tab menu
4713 */
4714 GetCursorPos((LPPOINT)&pt);
4715 GetWindowRect(s_textArea, &rect);
4716 if (pt.y < rect.top)
4717 {
4718 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004719 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004720 }
4721 }
4722 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4723 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004724 case WM_LBUTTONDBLCLK:
4725 {
4726 /*
4727 * If the user double clicked the tabline, create a new tab
4728 */
4729 if (gui_mch_showing_tabline())
4730 {
4731 POINT pt;
4732 RECT rect;
4733
4734 GetCursorPos((LPPOINT)&pt);
4735 GetWindowRect(s_textArea, &rect);
4736 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004737 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004738 }
4739 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4740 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004741#endif
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 case WM_QUERYENDSESSION: /* System wants to go down. */
4744 gui_shell_closed(); /* Will exit when no changed buffers. */
4745 return FALSE; /* Do NOT allow system to go down. */
4746
4747 case WM_ENDSESSION:
4748 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004751 return 0L;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 break;
4754
4755 case WM_CHAR:
4756 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4757 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004758 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 return 0L;
4760
4761 case WM_SYSCHAR:
4762 /*
4763 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4764 * shortcut key, handle like a typed ALT key, otherwise call Windows
4765 * ALT key handling.
4766 */
4767#ifdef FEAT_MENU
4768 if ( !gui.menu_is_active
4769 || p_wak[0] == 'n'
4770 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4771 )
4772#endif
4773 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004774 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 return 0L;
4776 }
4777#ifdef FEAT_MENU
4778 else
4779 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4780#endif
4781
4782 case WM_SYSKEYUP:
4783#ifdef FEAT_MENU
4784 /* This used to be done only when menu is active: ALT key is used for
4785 * that. But that caused problems when menu is disabled and using
4786 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4787 * are received, mouse pointer remains hidden. */
4788 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4789#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004790 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791#endif
4792
4793 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 case WM_MOUSEWHEEL:
4797 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004798 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
Bram Moolenaar520470a2005-06-16 21:59:56 +00004800 /* Notification for change in SystemParametersInfo() */
4801 case WM_SETTINGCHANGE:
4802 return _OnSettingChange((UINT)wParam);
4803
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004804#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 case WM_NOTIFY:
4806 switch (((LPNMHDR) lParam)->code)
4807 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004808 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004809 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004811 LPNMHDR hdr = (LPNMHDR)lParam;
4812 char_u *str = NULL;
4813 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004814
Bram Moolenaard23a8232018-02-10 18:45:26 +01004815 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004816
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004817# ifdef FEAT_GUI_TABLINE
4818 if (gui_mch_showing_tabline()
4819 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004820 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004821 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004822 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004823 * Mouse is over the GUI tabline. Display the
4824 * tooltip for the tab under the cursor
4825 *
4826 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004827 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004828 GetCursorPos(&pt);
4829 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004830 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004831 TCHITTESTINFO htinfo;
4832 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004833
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004834 /*
4835 * Get the tab under the cursor
4836 */
4837 htinfo.pt.x = pt.x;
4838 htinfo.pt.y = pt.y;
4839 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4840 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004841 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004842 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004843
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004844 tp = find_tabpage(idx + 1);
4845 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004846 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004847 get_tabline_label(tp, TRUE);
4848 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004849 }
4850 }
4851 }
4852 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004853# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004854# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004855# ifdef FEAT_GUI_TABLINE
4856 else
4857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004859 UINT idButton;
4860 vimmenu_T *pMenu;
4861
4862 idButton = (UINT) hdr->idFrom;
4863 pMenu = gui_mswin_find_menu(root_menu, idButton);
4864 if (pMenu)
4865 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004867# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004868 if (str != NULL)
4869 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004870 if (hdr->code == TTN_GETDISPINFOW)
4871 {
4872 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4873
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004874 /* Set the maximum width, this also enables using
4875 * \n for line break. */
4876 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4877 0, 500);
4878
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004879 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004880 lpdi->lpszText = tt_text;
4881 /* can't show tooltip if failed */
4882 }
4883 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004884 {
4885 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4886
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004887 /* Set the maximum width, this also enables using
4888 * \n for line break. */
4889 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4890 0, 500);
4891
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004892 if (STRLEN(str) < sizeof(lpdi->szText)
4893 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004894 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004895 sizeof(lpdi->szText) - 1);
4896 else
4897 lpdi->lpszText = tt_text;
4898 }
4899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
4901 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004902# ifdef FEAT_GUI_TABLINE
4903 case TCN_SELCHANGE:
4904 if (gui_mch_showing_tabline()
4905 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004906 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004907 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004908 return 0L;
4909 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004910 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004911
4912 case NM_RCLICK:
4913 if (gui_mch_showing_tabline()
4914 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004915 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004916 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 0L;
4918 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004919 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004922# ifdef FEAT_GUI_TABLINE
4923 if (gui_mch_showing_tabline()
4924 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4925 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 break;
4928 }
4929 break;
4930#endif
4931#if defined(MENUHINTS) && defined(FEAT_MENU)
4932 case WM_MENUSELECT:
4933 if (((UINT) HIWORD(wParam)
4934 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4935 == MF_HILITE
4936 && (State & CMDLINE) == 0)
4937 {
4938 UINT idButton;
4939 vimmenu_T *pMenu;
4940 static int did_menu_tip = FALSE;
4941
4942 if (did_menu_tip)
4943 {
4944 msg_clr_cmdline();
4945 setcursor();
4946 out_flush();
4947 did_menu_tip = FALSE;
4948 }
4949
4950 idButton = (UINT)LOWORD(wParam);
4951 pMenu = gui_mswin_find_menu(root_menu, idButton);
4952 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4953 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4954 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004955 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004956 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004957 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 setcursor();
4959 out_flush();
4960 did_menu_tip = TRUE;
4961 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004962 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 break;
4965#endif
4966 case WM_NCHITTEST:
4967 {
4968 LRESULT result;
4969 int x, y;
4970 int xPos = GET_X_LPARAM(lParam);
4971
4972 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4973 if (result == HTCLIENT)
4974 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004975#ifdef FEAT_GUI_TABLINE
4976 if (gui_mch_showing_tabline())
4977 {
4978 int yPos = GET_Y_LPARAM(lParam);
4979 RECT rct;
4980
4981 /* If the cursor is on the GUI tabline, don't process this
4982 * event */
4983 GetWindowRect(s_textArea, &rct);
4984 if (yPos < rct.top)
4985 return result;
4986 }
4987#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004988 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 xPos -= x;
4990
4991 if (xPos < 48) /* <VN> TODO should use system metric? */
4992 return HTBOTTOMLEFT;
4993 else
4994 return HTBOTTOMRIGHT;
4995 }
4996 else
4997 return result;
4998 }
4999 /* break; notreached */
5000
5001#ifdef FEAT_MBYTE_IME
5002 case WM_IME_NOTIFY:
5003 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5004 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005005 return 1L;
5006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 case WM_IME_COMPOSITION:
5008 if (!_OnImeComposition(hwnd, wParam, lParam))
5009 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005010 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#endif
5012
5013 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005015 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 _OnFindRepl();
5018 }
5019#endif
5020 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5021 }
5022
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005023 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024}
5025
5026/*
5027 * End of call-back routines
5028 */
5029
5030/* parent window, if specified with -P */
5031HWND vim_parent_hwnd = NULL;
5032
5033 static BOOL CALLBACK
5034FindWindowTitle(HWND hwnd, LPARAM lParam)
5035{
5036 char buf[2048];
5037 char *title = (char *)lParam;
5038
5039 if (GetWindowText(hwnd, buf, sizeof(buf)))
5040 {
5041 if (strstr(buf, title) != NULL)
5042 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005043 /* Found it. Store the window ref. and quit searching if MDI
5044 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005046 if (vim_parent_hwnd != NULL)
5047 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 }
5050 return TRUE; /* continue searching */
5051}
5052
5053/*
5054 * Invoked for '-P "title"' argument: search for parent application to open
5055 * our window in.
5056 */
5057 void
5058gui_mch_set_parent(char *title)
5059{
5060 EnumWindows(FindWindowTitle, (LPARAM)title);
5061 if (vim_parent_hwnd == NULL)
5062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005063 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 mch_exit(2);
5065 }
5066}
5067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005068#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 static void
5070ole_error(char *arg)
5071{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005072 char buf[IOSIZE];
5073
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005074 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005075 vim_snprintf(buf, IOSIZE,
5076 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5077 arg);
5078 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081
5082/*
5083 * Parse the GUI related command-line arguments. Any arguments used are
5084 * deleted from argv, and *argc is decremented accordingly. This is called
5085 * when vim is started, whether or not the GUI has been started.
5086 */
5087 void
5088gui_mch_prepare(int *argc, char **argv)
5089{
5090 int silent = FALSE;
5091 int idx;
5092
5093 /* Check for special OLE command line parameters */
5094 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5095 {
5096 /* Check for a "-silent" argument first. */
5097 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5098 && (argv[2][0] == '-' || argv[2][0] == '/'))
5099 {
5100 silent = TRUE;
5101 idx = 2;
5102 }
5103 else
5104 idx = 1;
5105
5106 /* Register Vim as an OLE Automation server */
5107 if (STRICMP(argv[idx] + 1, "register") == 0)
5108 {
5109#ifdef FEAT_OLE
5110 RegisterMe(silent);
5111 mch_exit(0);
5112#else
5113 if (!silent)
5114 ole_error("register");
5115 mch_exit(2);
5116#endif
5117 }
5118
5119 /* Unregister Vim as an OLE Automation server */
5120 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5121 {
5122#ifdef FEAT_OLE
5123 UnregisterMe(!silent);
5124 mch_exit(0);
5125#else
5126 if (!silent)
5127 ole_error("unregister");
5128 mch_exit(2);
5129#endif
5130 }
5131
5132 /* Ignore an -embedding argument. It is only relevant if the
5133 * application wants to treat the case when it is started manually
5134 * differently from the case where it is started via automation (and
5135 * we don't).
5136 */
5137 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5138 {
5139#ifdef FEAT_OLE
5140 *argc = 1;
5141#else
5142 ole_error("embedding");
5143 mch_exit(2);
5144#endif
5145 }
5146 }
5147
5148#ifdef FEAT_OLE
5149 {
5150 int bDoRestart = FALSE;
5151
5152 InitOLE(&bDoRestart);
5153 /* automatically exit after registering */
5154 if (bDoRestart)
5155 mch_exit(0);
5156 }
5157#endif
5158
5159#ifdef FEAT_NETBEANS_INTG
5160 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005161 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 int arg;
5163
5164 for (arg = 1; arg < *argc; arg++)
5165 if (strncmp("-nb", argv[arg], 3) == 0)
5166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 netbeansArg = argv[arg];
5168 mch_memmove(&argv[arg], &argv[arg + 1],
5169 (--*argc - arg) * sizeof(char *));
5170 argv[*argc] = NULL;
5171 break; /* enough? */
5172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175}
5176
5177/*
5178 * Initialise the GUI. Create all the windows, set up all the call-backs
5179 * etc.
5180 */
5181 int
5182gui_mch_init(void)
5183{
5184 const char szVimWndClass[] = VIM_CLASS;
5185 const char szTextAreaClass[] = "VimTextArea";
5186 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005188 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#ifdef GLOBAL_IME
5191 ATOM atom;
5192#endif
5193
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 /* Return here if the window was already opened (happens when
5195 * gui_mch_dialog() is called early). */
5196 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005197 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 /*
5200 * Load the tearoff bitmap
5201 */
5202#ifdef FEAT_TEAROFF
5203 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5204#endif
5205
5206 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5207 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5208#ifdef FEAT_MENU
5209 gui.menu_height = 0; /* Windows takes care of this */
5210#endif
5211 gui.border_width = 0;
5212
5213 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 /* First try using the wide version, so that we can use any title.
5216 * Otherwise only characters in the active codepage will work. */
5217 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5218 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005219 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 wndclassw.lpfnWndProc = _WndProc;
5221 wndclassw.cbClsExtra = 0;
5222 wndclassw.cbWndExtra = 0;
5223 wndclassw.hInstance = s_hinst;
5224 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5225 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5226 wndclassw.hbrBackground = s_brush;
5227 wndclassw.lpszMenuName = NULL;
5228 wndclassw.lpszClassName = szVimWndClassW;
5229
5230 if ((
5231#ifdef GLOBAL_IME
5232 atom =
5233#endif
5234 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005235 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 else
5237 wide_WindowProc = TRUE;
5238 }
5239
5240 if (!wide_WindowProc)
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005241 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5242 {
5243 wndclass.style = CS_DBLCLKS;
5244 wndclass.lpfnWndProc = _WndProc;
5245 wndclass.cbClsExtra = 0;
5246 wndclass.cbWndExtra = 0;
5247 wndclass.hInstance = s_hinst;
5248 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5249 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5250 wndclass.hbrBackground = s_brush;
5251 wndclass.lpszMenuName = NULL;
5252 wndclass.lpszClassName = szVimWndClass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005254 if ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#ifdef GLOBAL_IME
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005256 atom =
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005258 RegisterClass(&wndclass)) == 0)
5259 return FAIL;
5260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 if (vim_parent_hwnd != NULL)
5263 {
5264#ifdef HAVE_TRY_EXCEPT
5265 __try
5266 {
5267#endif
5268 /* Open inside the specified parent window.
5269 * TODO: last argument should point to a CLIENTCREATESTRUCT
5270 * structure. */
5271 s_hwnd = CreateWindowEx(
5272 WS_EX_MDICHILD,
5273 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005274 WS_OVERLAPPEDWINDOW | WS_CHILD
5275 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5277 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5278 100, /* Any value will do */
5279 100, /* Any value will do */
5280 vim_parent_hwnd, NULL,
5281 s_hinst, NULL);
5282#ifdef HAVE_TRY_EXCEPT
5283 }
5284 __except(EXCEPTION_EXECUTE_HANDLER)
5285 {
5286 /* NOP */
5287 }
5288#endif
5289 if (s_hwnd == NULL)
5290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005291 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 mch_exit(2);
5293 }
5294 }
5295 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005296 {
5297 /* If the provided windowid is not valid reset it to zero, so that it
5298 * is ignored and we open our own window. */
5299 if (IsWindow((HWND)win_socket_id) <= 0)
5300 win_socket_id = 0;
5301
5302 /* Create a window. If win_socket_id is not zero without border and
5303 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005305 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005306 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5307 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005308 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5309 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5310 100, /* Any value will do */
5311 100, /* Any value will do */
5312 NULL, NULL,
5313 s_hinst, NULL);
5314 if (s_hwnd != NULL && win_socket_id != 0)
5315 {
5316 SetParent(s_hwnd, (HWND)win_socket_id);
5317 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5318 }
5319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
5321 if (s_hwnd == NULL)
5322 return FAIL;
5323
5324#ifdef GLOBAL_IME
5325 global_ime_init(atom, s_hwnd);
5326#endif
5327#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5328 dyn_imm_load();
5329#endif
5330
5331 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005332 if (wide_WindowProc)
5333 {
5334 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5335 {
5336 wndclassw.style = CS_OWNDC;
5337 wndclassw.lpfnWndProc = _TextAreaWndProc;
5338 wndclassw.cbClsExtra = 0;
5339 wndclassw.cbWndExtra = 0;
5340 wndclassw.hInstance = s_hinst;
5341 wndclassw.hIcon = NULL;
5342 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5343 wndclassw.hbrBackground = NULL;
5344 wndclassw.lpszMenuName = NULL;
5345 wndclassw.lpszClassName = szTextAreaClassW;
5346
5347 if (RegisterClassW(&wndclassw) == 0)
5348 return FAIL;
5349 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005350
5351 s_textArea = CreateWindowExW(
5352 0,
5353 szTextAreaClassW, L"Vim text area",
5354 WS_CHILD | WS_VISIBLE, 0, 0,
5355 100, // Any value will do for now
5356 100, // Any value will do for now
5357 s_hwnd, NULL,
5358 s_hinst, NULL);
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005359 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005360 else if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
5362 wndclass.style = CS_OWNDC;
5363 wndclass.lpfnWndProc = _TextAreaWndProc;
5364 wndclass.cbClsExtra = 0;
5365 wndclass.cbWndExtra = 0;
5366 wndclass.hInstance = s_hinst;
5367 wndclass.hIcon = NULL;
5368 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5369 wndclass.hbrBackground = NULL;
5370 wndclass.lpszMenuName = NULL;
5371 wndclass.lpszClassName = szTextAreaClass;
5372
5373 if (RegisterClass(&wndclass) == 0)
5374 return FAIL;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005375
5376 s_textArea = CreateWindowEx(
5377 0,
5378 szTextAreaClass, "Vim text area",
5379 WS_CHILD | WS_VISIBLE, 0, 0,
5380 100, // Any value will do for now
5381 100, // Any value will do for now
5382 s_hwnd, NULL,
5383 s_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
5386 if (s_textArea == NULL)
5387 return FAIL;
5388
Bram Moolenaar20321902016-02-17 12:30:17 +01005389#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005390 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5391 {
5392 HANDLE hIcon = NULL;
5393
5394 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005395 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005396 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005397#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005398
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399#ifdef FEAT_MENU
5400 s_menuBar = CreateMenu();
5401#endif
5402 s_hdc = GetDC(s_textArea);
5403
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 /* Do we need to bother with this? */
5407 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5408
5409 /* Get background/foreground colors from the system */
5410 gui_mch_def_colors();
5411
5412 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5413 * file) */
5414 set_normal_colors();
5415
5416 /*
5417 * Check that none of the colors are the same as the background color.
5418 * Then store the current values as the defaults.
5419 */
5420 gui_check_colors();
5421 gui.def_norm_pixel = gui.norm_pixel;
5422 gui.def_back_pixel = gui.back_pixel;
5423
5424 /* Get the colors for the highlight groups (gui_check_colors() might have
5425 * changed them) */
5426 highlight_gui_started();
5427
5428 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005429 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005431 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432
5433 /*
5434 * Set up for Intellimouse processing
5435 */
5436 init_mouse_wheel();
5437
5438 /*
5439 * compute a couple of metrics used for the dialogs
5440 */
5441 get_dialog_font_metrics();
5442#ifdef FEAT_TOOLBAR
5443 /*
5444 * Create the toolbar
5445 */
5446 initialise_toolbar();
5447#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005448#ifdef FEAT_GUI_TABLINE
5449 /*
5450 * Create the tabline
5451 */
5452 initialise_tabline();
5453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454#ifdef MSWIN_FIND_REPLACE
5455 /*
5456 * Initialise the dialog box stuff
5457 */
5458 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5459
5460 /* Initialise the struct */
5461 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005462 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005464 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5466 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5467 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005468 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5469 s_findrep_struct_w.lpstrFindWhat =
5470 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5471 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5472 s_findrep_struct_w.lpstrReplaceWith =
5473 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5474 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5475 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5476 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005479#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005480# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5481/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5482# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005483# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005484# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005485# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005486 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005487 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005488#endif
5489
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005490#ifdef FEAT_RENDER_OPTIONS
5491 if (p_rop)
5492 (void)gui_mch_set_rendering_options(p_rop);
5493#endif
5494
Bram Moolenaar748bf032005-02-02 23:04:36 +00005495theend:
5496 /* Display any pending error messages */
5497 display_errors();
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 return OK;
5500}
5501
5502/*
5503 * Get the size of the screen, taking position on multiple monitors into
5504 * account (if supported).
5505 */
5506 static void
5507get_work_area(RECT *spi_rect)
5508{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005509 HMONITOR mon;
5510 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005512 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005513 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005514 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005516 moninfo.cbSize = sizeof(MONITORINFO);
5517 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005519 *spi_rect = moninfo.rcWork;
5520 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
5522 }
5523 /* this is the old method... */
5524 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5525}
5526
5527/*
5528 * Set the size of the window to the given width and height in pixels.
5529 */
5530 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005531gui_mch_set_shellsize(
5532 int width,
5533 int height,
5534 int min_width UNUSED,
5535 int min_height UNUSED,
5536 int base_width UNUSED,
5537 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005538 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 RECT workarea_rect;
5541 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 WINDOWPLACEMENT wndpl;
5543
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005544 /* Try to keep window completely on screen. */
5545 /* Get position of the screen work area. This is the part that is not
5546 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 get_work_area(&workarea_rect);
5548
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005549 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005550 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 wndpl.length = sizeof(WINDOWPLACEMENT);
5552 GetWindowPlacement(s_hwnd, &wndpl);
5553
5554 /* Resizing a maximized window looks very strange, unzoom it first.
5555 * But don't do it when still starting up, it may have been requested in
5556 * the shortcut. */
5557 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5558 {
5559 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5560 /* Need to get the settings of the normal window. */
5561 GetWindowPlacement(s_hwnd, &wndpl);
5562 }
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005565 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005566 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005567 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005568 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 + GetSystemMetrics(SM_CYCAPTION)
5570#ifdef FEAT_MENU
5571 + gui_mswin_get_menu_height(FALSE)
5572#endif
5573 ;
5574
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005575 /* The following should take care of keeping Vim on the same monitor, no
5576 * matter if the secondary monitor is left or right of the primary
5577 * monitor. */
5578 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5579 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005580
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005581 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005582 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005583 && wndpl.rcNormalPosition.right > workarea_rect.right)
5584 OffsetRect(&wndpl.rcNormalPosition,
5585 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005587 if ((direction & RESIZE_HOR)
5588 && wndpl.rcNormalPosition.left < workarea_rect.left)
5589 OffsetRect(&wndpl.rcNormalPosition,
5590 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaarafa24992006-03-27 20:58:26 +00005592 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005593 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5594 OffsetRect(&wndpl.rcNormalPosition,
5595 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005597 if ((direction & RESIZE_VERT)
5598 && wndpl.rcNormalPosition.top < workarea_rect.top)
5599 OffsetRect(&wndpl.rcNormalPosition,
5600 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
5602 /* set window position - we should use SetWindowPlacement rather than
5603 * SetWindowPos as the MSDN docs say the coord systems returned by
5604 * these two are not compatible. */
5605 SetWindowPlacement(s_hwnd, &wndpl);
5606
5607 SetActiveWindow(s_hwnd);
5608 SetFocus(s_hwnd);
5609
5610#ifdef FEAT_MENU
5611 /* Menu may wrap differently now */
5612 gui_mswin_get_menu_height(!gui.starting);
5613#endif
5614}
5615
5616
5617 void
5618gui_mch_set_scrollbar_thumb(
5619 scrollbar_T *sb,
5620 long val,
5621 long size,
5622 long max)
5623{
5624 SCROLLINFO info;
5625
5626 sb->scroll_shift = 0;
5627 while (max > 32767)
5628 {
5629 max = (max + 1) >> 1;
5630 val >>= 1;
5631 size >>= 1;
5632 ++sb->scroll_shift;
5633 }
5634
5635 if (sb->scroll_shift > 0)
5636 ++size;
5637
5638 info.cbSize = sizeof(info);
5639 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5640 info.nPos = val;
5641 info.nMin = 0;
5642 info.nMax = max;
5643 info.nPage = size;
5644 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5645}
5646
5647
5648/*
5649 * Set the current text font.
5650 */
5651 void
5652gui_mch_set_font(GuiFont font)
5653{
5654 gui.currFont = font;
5655}
5656
5657
5658/*
5659 * Set the current text foreground color.
5660 */
5661 void
5662gui_mch_set_fg_color(guicolor_T color)
5663{
5664 gui.currFgColor = color;
5665}
5666
5667/*
5668 * Set the current text background color.
5669 */
5670 void
5671gui_mch_set_bg_color(guicolor_T color)
5672{
5673 gui.currBgColor = color;
5674}
5675
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005676/*
5677 * Set the current text special color.
5678 */
5679 void
5680gui_mch_set_sp_color(guicolor_T color)
5681{
5682 gui.currSpColor = color;
5683}
5684
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005685#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686/*
5687 * Multi-byte handling, originally by Sung-Hoon Baek.
5688 * First static functions (no prototypes generated).
5689 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005690# ifdef _MSC_VER
5691# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5692# endif
5693# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 * handle WM_IME_NOTIFY message
5697 */
5698 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005699_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 LRESULT lResult = 0;
5702 HIMC hImc;
5703
5704 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5705 return lResult;
5706 switch (dwCommand)
5707 {
5708 case IMN_SETOPENSTATUS:
5709 if (pImmGetOpenStatus(hImc))
5710 {
5711 pImmSetCompositionFont(hImc, &norm_logfont);
5712 im_set_position(gui.row, gui.col);
5713
5714 /* Disable langmap */
5715 State &= ~LANGMAP;
5716 if (State & INSERT)
5717 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005718#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 /* Unshown 'keymap' in status lines */
5720 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5721 {
5722 /* Save cursor position */
5723 int old_row = gui.row;
5724 int old_col = gui.col;
5725
5726 // This must be called here before
5727 // status_redraw_curbuf(), otherwise the mode
5728 // message may appear in the wrong position.
5729 showmode();
5730 status_redraw_curbuf();
5731 update_screen(0);
5732 /* Restore cursor position */
5733 gui.row = old_row;
5734 gui.col = old_col;
5735 }
5736#endif
5737 }
5738 }
5739 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005740 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 lResult = 0;
5742 break;
5743 }
5744 pImmReleaseContext(hWnd, hImc);
5745 return lResult;
5746}
5747
5748 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005749_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 char_u *ret;
5752 int len;
5753
5754 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5755 return 0;
5756
5757 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5758 if (ret != NULL)
5759 {
5760 add_to_input_buf_csi(ret, len);
5761 vim_free(ret);
5762 return 1;
5763 }
5764 return 0;
5765}
5766
5767/*
5768 * get the current composition string, in UCS-2; *lenp is the number of
5769 * *lenp is the number of Unicode characters.
5770 */
5771 static short_u *
5772GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5773{
5774 LONG ret;
5775 LPWSTR wbuf = NULL;
5776 char_u *buf;
5777
5778 if (!pImmGetContext)
5779 return NULL; /* no imm32.dll */
5780
5781 /* Try Unicode; this'll always work on NT regardless of codepage. */
5782 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5783 if (ret == 0)
5784 return NULL; /* empty */
5785
5786 if (ret > 0)
5787 {
5788 /* Allocate the requested buffer plus space for the NUL character. */
5789 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5790 if (wbuf != NULL)
5791 {
5792 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5793 *lenp = ret / sizeof(WCHAR);
5794 }
5795 return (short_u *)wbuf;
5796 }
5797
5798 /* ret < 0; we got an error, so try the ANSI version. This'll work
5799 * on 9x/ME, but only if the codepage happens to be set to whatever
5800 * we're inputting. */
5801 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5802 if (ret <= 0)
5803 return NULL; /* empty or error */
5804
5805 buf = alloc(ret);
5806 if (buf == NULL)
5807 return NULL;
5808 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5809
5810 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005811 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 vim_free(buf);
5813
5814 return (short_u *)wbuf;
5815}
5816
5817/*
5818 * void GetResultStr()
5819 *
5820 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5821 * get complete composition string
5822 */
5823 static char_u *
5824GetResultStr(HWND hwnd, int GCS, int *lenp)
5825{
5826 HIMC hIMC; /* Input context handle. */
5827 short_u *buf = NULL;
5828 char_u *convbuf = NULL;
5829
5830 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5831 return NULL;
5832
5833 /* Reads in the composition string. */
5834 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5835 if (buf == NULL)
5836 return NULL;
5837
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005838 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 pImmReleaseContext(hwnd, hIMC);
5840 vim_free(buf);
5841 return convbuf;
5842}
5843#endif
5844
5845/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005846#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848/*
5849 * set font to IM.
5850 */
5851 void
5852im_set_font(LOGFONT *lf)
5853{
5854 HIMC hImc;
5855
5856 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5857 {
5858 pImmSetCompositionFont(hImc, lf);
5859 pImmReleaseContext(s_hwnd, hImc);
5860 }
5861}
5862
5863/*
5864 * Notify cursor position to IM.
5865 */
5866 void
5867im_set_position(int row, int col)
5868{
5869 HIMC hImc;
5870
5871 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5872 {
5873 COMPOSITIONFORM cfs;
5874
5875 cfs.dwStyle = CFS_POINT;
5876 cfs.ptCurrentPos.x = FILL_X(col);
5877 cfs.ptCurrentPos.y = FILL_Y(row);
5878 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5879 pImmSetCompositionWindow(hImc, &cfs);
5880
5881 pImmReleaseContext(s_hwnd, hImc);
5882 }
5883}
5884
5885/*
5886 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5887 */
5888 void
5889im_set_active(int active)
5890{
5891 HIMC hImc;
5892 static HIMC hImcOld = (HIMC)0;
5893
5894 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
5895 {
5896 if (p_imdisable)
5897 {
5898 if (hImcOld == (HIMC)0)
5899 {
5900 hImcOld = pImmGetContext(s_hwnd);
5901 if (hImcOld)
5902 pImmAssociateContext(s_hwnd, (HIMC)0);
5903 }
5904 active = FALSE;
5905 }
5906 else if (hImcOld != (HIMC)0)
5907 {
5908 pImmAssociateContext(s_hwnd, hImcOld);
5909 hImcOld = (HIMC)0;
5910 }
5911
5912 hImc = pImmGetContext(s_hwnd);
5913 if (hImc)
5914 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005915 /*
5916 * for Korean ime
5917 */
5918 HKL hKL = GetKeyboardLayout(0);
5919
5920 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5921 {
5922 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5923 static BOOL bSaved = FALSE;
5924
5925 if (active)
5926 {
5927 /* if we have a saved conversion status, restore it */
5928 if (bSaved)
5929 pImmSetConversionStatus(hImc, dwConversionSaved,
5930 dwSentenceSaved);
5931 bSaved = FALSE;
5932 }
5933 else
5934 {
5935 /* save conversion status and disable korean */
5936 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5937 &dwSentenceSaved))
5938 {
5939 bSaved = TRUE;
5940 pImmSetConversionStatus(hImc,
5941 dwConversionSaved & ~(IME_CMODE_NATIVE
5942 | IME_CMODE_FULLSHAPE),
5943 dwSentenceSaved);
5944 }
5945 }
5946 }
5947
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 pImmSetOpenStatus(hImc, active);
5949 pImmReleaseContext(s_hwnd, hImc);
5950 }
5951 }
5952}
5953
5954/*
5955 * Get IM status. When IM is on, return not 0. Else return 0.
5956 */
5957 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005958im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 int status = 0;
5961 HIMC hImc;
5962
5963 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5964 {
5965 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5966 pImmReleaseContext(s_hwnd, hImc);
5967 }
5968 return status;
5969}
5970
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005971#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005973#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974/* Win32 with GLOBAL IME */
5975
5976/*
5977 * Notify cursor position to IM.
5978 */
5979 void
5980im_set_position(int row, int col)
5981{
5982 /* Win32 with GLOBAL IME */
5983 POINT p;
5984
5985 p.x = FILL_X(col);
5986 p.y = FILL_Y(row);
5987 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5988 global_ime_set_position(&p);
5989}
5990
5991/*
5992 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5993 */
5994 void
5995im_set_active(int active)
5996{
5997 global_ime_set_status(active);
5998}
5999
6000/*
6001 * Get IM status. When IM is on, return not 0. Else return 0.
6002 */
6003 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006004im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 return global_ime_get_status();
6007}
6008#endif
6009
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006010/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006011 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006012 */
6013 static void
6014latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6015{
6016 int c;
6017
Bram Moolenaarca003e12006-03-17 23:19:38 +00006018 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006019 {
6020 c = *text++;
6021 switch (c)
6022 {
6023 case 0xa4: c = 0x20ac; break; /* euro */
6024 case 0xa6: c = 0x0160; break; /* S hat */
6025 case 0xa8: c = 0x0161; break; /* S -hat */
6026 case 0xb4: c = 0x017d; break; /* Z hat */
6027 case 0xb8: c = 0x017e; break; /* Z -hat */
6028 case 0xbc: c = 0x0152; break; /* OE */
6029 case 0xbd: c = 0x0153; break; /* oe */
6030 case 0xbe: c = 0x0178; break; /* Y */
6031 }
6032 *unicodebuf++ = c;
6033 }
6034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036#ifdef FEAT_RIGHTLEFT
6037/*
6038 * What is this for? In the case where you are using Win98 or Win2K or later,
6039 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6040 * reverses the string sent to the TextOut... family. This sucks, because we
6041 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6042 * way to tell Windblows not to do this!
6043 *
6044 * The short of it is that this 'RevOut' only gets called if you are running
6045 * one of the new, "improved" MS OSes, and only if you are running in
6046 * 'rightleft' mode. It makes display take *slightly* longer, but not
6047 * noticeably so.
6048 */
6049 static void
6050RevOut( HDC s_hdc,
6051 int col,
6052 int row,
6053 UINT foptions,
6054 CONST RECT *pcliprect,
6055 LPCTSTR text,
6056 UINT len,
6057 CONST INT *padding)
6058{
6059 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006061 for (ix = 0; ix < (int)len; ++ix)
6062 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6063 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064}
6065#endif
6066
Bram Moolenaar92467d32017-12-05 13:22:16 +01006067 static void
6068draw_line(
6069 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006070 int y1,
6071 int x2,
6072 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006073 COLORREF color)
6074{
6075#if defined(FEAT_DIRECTX)
6076 if (IS_ENABLE_DIRECTX())
6077 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6078 else
6079#endif
6080 {
6081 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6082 HPEN old_pen = SelectObject(s_hdc, hpen);
6083 MoveToEx(s_hdc, x1, y1, NULL);
6084 /* Note: LineTo() excludes the last pixel in the line. */
6085 LineTo(s_hdc, x2, y2);
6086 DeleteObject(SelectObject(s_hdc, old_pen));
6087 }
6088}
6089
6090 static void
6091set_pixel(
6092 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006093 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006094 COLORREF color)
6095{
6096#if defined(FEAT_DIRECTX)
6097 if (IS_ENABLE_DIRECTX())
6098 DWriteContext_SetPixel(s_dwc, x, y, color);
6099 else
6100#endif
6101 SetPixel(s_hdc, x, y, color);
6102}
6103
6104 static void
6105fill_rect(
6106 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006107 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006108 COLORREF color)
6109{
6110#if defined(FEAT_DIRECTX)
6111 if (IS_ENABLE_DIRECTX())
6112 DWriteContext_FillRect(s_dwc, rcp, color);
6113 else
6114#endif
6115 {
6116 HBRUSH hbr2;
6117
6118 if (hbr == NULL)
6119 hbr2 = CreateSolidBrush(color);
6120 else
6121 hbr2 = hbr;
6122 FillRect(s_hdc, rcp, hbr2);
6123 if (hbr == NULL)
6124 DeleteBrush(hbr2);
6125 }
6126}
6127
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 void
6129gui_mch_draw_string(
6130 int row,
6131 int col,
6132 char_u *text,
6133 int len,
6134 int flags)
6135{
6136 static int *padding = NULL;
6137 static int pad_size = 0;
6138 int i;
6139 const RECT *pcliprect = NULL;
6140 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 static WCHAR *unicodebuf = NULL;
6142 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006143 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 int y;
6146
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 /*
6148 * Italic and bold text seems to have an extra row of pixels at the bottom
6149 * (below where the bottom of the character should be). If we draw the
6150 * characters with a solid background, the top row of pixels in the
6151 * character below will be overwritten. We can fix this by filling in the
6152 * background ourselves, to the correct character proportions, and then
6153 * writing the character in transparent mode. Still have a problem when
6154 * the character is "_", which gets written on to the character below.
6155 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6156 * pixel in their slots, which fixes the problem with the bottom row of
6157 * pixels. We still need this code because otherwise the top row of pixels
6158 * becomes a problem. - webb.
6159 */
6160 static HBRUSH hbr_cache[2] = {NULL, NULL};
6161 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6162 static int brush_lru = 0;
6163 HBRUSH hbr;
6164 RECT rc;
6165
6166 if (!(flags & DRAW_TRANSP))
6167 {
6168 /*
6169 * Clear background first.
6170 * Note: FillRect() excludes right and bottom of rectangle.
6171 */
6172 rc.left = FILL_X(col);
6173 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (has_mbyte)
6175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006177 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 }
6179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 rc.right = FILL_X(col + len);
6181 rc.bottom = FILL_Y(row + 1);
6182
6183 /* Cache the created brush, that saves a lot of time. We need two:
6184 * one for cursor background and one for the normal background. */
6185 if (gui.currBgColor == brush_color[0])
6186 {
6187 hbr = hbr_cache[0];
6188 brush_lru = 1;
6189 }
6190 else if (gui.currBgColor == brush_color[1])
6191 {
6192 hbr = hbr_cache[1];
6193 brush_lru = 0;
6194 }
6195 else
6196 {
6197 if (hbr_cache[brush_lru] != NULL)
6198 DeleteBrush(hbr_cache[brush_lru]);
6199 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6200 brush_color[brush_lru] = gui.currBgColor;
6201 hbr = hbr_cache[brush_lru];
6202 brush_lru = !brush_lru;
6203 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006204
Bram Moolenaar92467d32017-12-05 13:22:16 +01006205 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 SetBkMode(s_hdc, TRANSPARENT);
6208
6209 /*
6210 * When drawing block cursor, prevent inverted character spilling
6211 * over character cell (can happen with bold/italic)
6212 */
6213 if (flags & DRAW_CURSOR)
6214 {
6215 pcliprect = &rc;
6216 foptions = ETO_CLIPPED;
6217 }
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 SetTextColor(s_hdc, gui.currFgColor);
6220 SelectFont(s_hdc, gui.currFont);
6221
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006222#ifdef FEAT_DIRECTX
6223 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006224 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006225#endif
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6228 {
6229 vim_free(padding);
6230 pad_size = Columns;
6231
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006232 /* Don't give an out-of-memory message here, it would call us
6233 * recursively. */
6234 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (padding != NULL)
6236 for (i = 0; i < pad_size; i++)
6237 padding[i] = gui.char_width;
6238 }
6239
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 /*
6241 * We have to provide the padding argument because italic and bold versions
6242 * of fixed-width fonts are often one pixel or so wider than their normal
6243 * versions.
6244 * No check for DRAW_BOLD, Windows will have done it already.
6245 */
6246
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 /* Check if there are any UTF-8 characters. If not, use normal text
6248 * output to speed up output. */
6249 if (enc_utf8)
6250 for (n = 0; n < len; ++n)
6251 if (text[n] >= 0x80)
6252 break;
6253
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006254#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006255 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6256 * required that unicode drawing routine, currently. So this forces it
6257 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006258 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006259 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006260#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006263 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006265 if ((enc_utf8
6266 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6267 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 && (unicodebuf == NULL || len > unibuflen))
6269 {
6270 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006271 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006274 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 unibuflen = len;
6277 }
6278
6279 if (enc_utf8 && n < len && unicodebuf != NULL)
6280 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006281 /* Output UTF-8 characters. Composing characters should be
6282 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006283 int i;
6284 int wlen; /* string length in words */
6285 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 int cells; /* cell width of string up to composing char */
6287 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006288 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006290 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006291 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006293 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006295 c = utf_ptr2char(text + i);
6296 if (c >= 0x10000)
6297 {
6298 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006299 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6300 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006301 }
6302 else
6303 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006304 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006305 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006306
6307 if (utf_iscomposing(c))
6308 cw = 0;
6309 else
6310 {
6311 cw = utf_char2cells(c);
6312 if (cw > 2) /* don't use 4 for unprintable char */
6313 cw = 1;
6314 }
6315
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 if (unicodepdy != NULL)
6317 {
6318 /* Use unicodepdy to make characters fit as we expect, even
6319 * when the font uses different widths (e.g., bold character
6320 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006321 if (c >= 0x10000)
6322 {
6323 unicodepdy[wlen - 2] = cw * gui.char_width;
6324 unicodepdy[wlen - 1] = 0;
6325 }
6326 else
6327 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
6329 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006330 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006331 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006333#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006334 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006335 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006336 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006337 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006338 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006339 gui.char_width, gui.currFgColor,
6340 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006341 }
6342 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006343#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006344 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6345 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 len = cells; /* used for underlining */
6347 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006348 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
6350 /* If we want to display codepage data, and the current CP is not the
6351 * ANSI one, we need to go via Unicode. */
6352 if (unicodebuf != NULL)
6353 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006354 if (enc_latin9)
6355 latin9_to_ucs(text, len, unicodebuf);
6356 else
6357 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 MB_PRECOMPOSED,
6359 (char *)text, len,
6360 (LPWSTR)unicodebuf, unibuflen);
6361 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006362 {
6363 /* Use unicodepdy to make characters fit as we expect, even
6364 * when the font uses different widths (e.g., bold character
6365 * is wider). */
6366 if (unicodepdy != NULL)
6367 {
6368 int i;
6369 int cw;
6370
6371 for (i = 0; i < len; ++i)
6372 {
6373 cw = utf_char2cells(unicodebuf[i]);
6374 if (cw > 2)
6375 cw = 1;
6376 unicodepdy[i] = cw * gui.char_width;
6377 }
6378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006380 foptions, pcliprect, unicodebuf, len, unicodepdy);
6381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383 }
6384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 {
6386#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006387 /* Windows will mess up RL text, so we have to draw it character by
6388 * character. Only do this if RL is on, since it's slow. */
6389 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6391 foptions, pcliprect, (char *)text, len, padding);
6392 else
6393#endif
6394 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6395 foptions, pcliprect, (char *)text, len, padding);
6396 }
6397
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006398 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (flags & DRAW_UNDERL)
6400 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 /* When p_linespace is 0, overwrite the bottom row of pixels.
6402 * Otherwise put the line just below the character. */
6403 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (p_linespace > 1)
6405 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006406 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006408
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006409 /* Strikethrough */
6410 if (flags & DRAW_STRIKE)
6411 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006412 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006413 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006414 }
6415
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006416 /* Undercurl */
6417 if (flags & DRAW_UNDERC)
6418 {
6419 int x;
6420 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006421 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006422
6423 y = FILL_Y(row + 1) - 1;
6424 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6425 {
6426 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006427 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006428 }
6429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430}
6431
6432
6433/*
6434 * Output routines.
6435 */
6436
6437/* Flush any output to the screen */
6438 void
6439gui_mch_flush(void)
6440{
6441# if defined(__BORLANDC__)
6442 /*
6443 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6444 * prototype declaration.
6445 * The compiler complains if __stdcall is not used in both declarations.
6446 */
6447 BOOL __stdcall GdiFlush(void);
6448# endif
6449
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006450#if defined(FEAT_DIRECTX)
6451 if (IS_ENABLE_DIRECTX())
6452 DWriteContext_Flush(s_dwc);
6453#endif
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 GdiFlush();
6456}
6457
6458 static void
6459clear_rect(RECT *rcp)
6460{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006461 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462}
6463
6464
Bram Moolenaarc716c302006-01-21 22:12:51 +00006465 void
6466gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6467{
6468 RECT workarea_rect;
6469
6470 get_work_area(&workarea_rect);
6471
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006472 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006473 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006474 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006475
6476 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6477 * the menubar for MSwin, we subtract it from the screen height, so that
6478 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006479 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006480 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006481 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006482 - GetSystemMetrics(SM_CYCAPTION)
6483#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006484 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006485#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006486 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006487}
6488
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490#if defined(FEAT_MENU) || defined(PROTO)
6491/*
6492 * Add a sub menu to the menu bar.
6493 */
6494 void
6495gui_mch_add_menu(
6496 vimmenu_T *menu,
6497 int pos)
6498{
6499 vimmenu_T *parent = menu->parent;
6500
6501 menu->submenu_id = CreatePopupMenu();
6502 menu->id = s_menu_id++;
6503
6504 if (menu_is_menubar(menu->name))
6505 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006506 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006508 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6509 {
6510 /* 'encoding' differs from active codepage: convert menu name
6511 * and use wide function */
6512 wn = enc_to_utf16(menu->name, NULL);
6513 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006515 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006517 infow.cbSize = sizeof(infow);
6518 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6519 | MIIM_SUBMENU;
6520 infow.dwItemData = (long_u)menu;
6521 infow.wID = menu->id;
6522 infow.fType = MFT_STRING;
6523 infow.dwTypeData = wn;
6524 infow.cch = (UINT)wcslen(wn);
6525 infow.hSubMenu = menu->submenu_id;
6526 InsertMenuItemW((parent == NULL)
6527 ? s_menuBar : parent->submenu_id,
6528 (UINT)pos, TRUE, &infow);
6529 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006533 if (wn == NULL)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006534 {
6535 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006537 info.cbSize = sizeof(info);
6538 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6539 info.dwItemData = (long_u)menu;
6540 info.wID = menu->id;
6541 info.fType = MFT_STRING;
6542 info.dwTypeData = (LPTSTR)menu->name;
6543 info.cch = (UINT)STRLEN(menu->name);
6544 info.hSubMenu = menu->submenu_id;
6545 InsertMenuItem((parent == NULL)
6546 ? s_menuBar : parent->submenu_id,
6547 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549 }
6550
6551 /* Fix window size if menu may have wrapped */
6552 if (parent == NULL)
6553 gui_mswin_get_menu_height(!gui.starting);
6554#ifdef FEAT_TEAROFF
6555 else if (IsWindow(parent->tearoff_handle))
6556 rebuild_tearoff(parent);
6557#endif
6558}
6559
6560 void
6561gui_mch_show_popupmenu(vimmenu_T *menu)
6562{
6563 POINT mp;
6564
6565 (void)GetCursorPos((LPPOINT)&mp);
6566 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6567}
6568
6569 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006570gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 vimmenu_T *menu = gui_find_menu(path_name);
6573
6574 if (menu != NULL)
6575 {
6576 POINT p;
6577
6578 /* Find the position of the current cursor */
6579 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006580 if (mouse_pos)
6581 {
6582 int mx, my;
6583
6584 gui_mch_getmouse(&mx, &my);
6585 p.x += mx;
6586 p.y += my;
6587 }
6588 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006590 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6592 }
6593 msg_scroll = FALSE;
6594 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6595 }
6596}
6597
6598#if defined(FEAT_TEAROFF) || defined(PROTO)
6599/*
6600 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6601 * create it as a pseudo-"tearoff menu".
6602 */
6603 void
6604gui_make_tearoff(char_u *path_name)
6605{
6606 vimmenu_T *menu = gui_find_menu(path_name);
6607
6608 /* Found the menu, so tear it off. */
6609 if (menu != NULL)
6610 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6611}
6612#endif
6613
6614/*
6615 * Add a menu item to a menu
6616 */
6617 void
6618gui_mch_add_menu_item(
6619 vimmenu_T *menu,
6620 int idx)
6621{
6622 vimmenu_T *parent = menu->parent;
6623
6624 menu->id = s_menu_id++;
6625 menu->submenu_id = NULL;
6626
6627#ifdef FEAT_TEAROFF
6628 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6629 {
6630 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6631 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6632 }
6633 else
6634#endif
6635#ifdef FEAT_TOOLBAR
6636 if (menu_is_toolbar(parent->name))
6637 {
6638 TBBUTTON newtb;
6639
6640 vim_memset(&newtb, 0, sizeof(newtb));
6641 if (menu_is_separator(menu->name))
6642 {
6643 newtb.iBitmap = 0;
6644 newtb.fsStyle = TBSTYLE_SEP;
6645 }
6646 else
6647 {
6648 newtb.iBitmap = get_toolbar_bitmap(menu);
6649 newtb.fsStyle = TBSTYLE_BUTTON;
6650 }
6651 newtb.idCommand = menu->id;
6652 newtb.fsState = TBSTATE_ENABLED;
6653 newtb.iString = 0;
6654 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6655 (LPARAM)&newtb);
6656 menu->submenu_id = (HMENU)-1;
6657 }
6658 else
6659#endif
6660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6664 {
6665 /* 'encoding' differs from active codepage: convert menu item name
6666 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006667 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (wn != NULL)
6669 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006670 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 (menu_is_separator(menu->name)
6672 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6673 (UINT)menu->id, wn);
6674 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 }
6676 }
6677 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 InsertMenu(parent->submenu_id, (UINT)idx,
6679 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6680 | MF_BYPOSITION,
6681 (UINT)menu->id, (LPCTSTR)menu->name);
6682#ifdef FEAT_TEAROFF
6683 if (IsWindow(parent->tearoff_handle))
6684 rebuild_tearoff(parent);
6685#endif
6686 }
6687}
6688
6689/*
6690 * Destroy the machine specific menu widget.
6691 */
6692 void
6693gui_mch_destroy_menu(vimmenu_T *menu)
6694{
6695#ifdef FEAT_TOOLBAR
6696 /*
6697 * is this a toolbar button?
6698 */
6699 if (menu->submenu_id == (HMENU)-1)
6700 {
6701 int iButton;
6702
6703 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6704 (WPARAM)menu->id, 0);
6705 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6706 }
6707 else
6708#endif
6709 {
6710 if (menu->parent != NULL
6711 && menu_is_popup(menu->parent->dname)
6712 && menu->parent->submenu_id != NULL)
6713 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6714 else
6715 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6716 if (menu->submenu_id != NULL)
6717 DestroyMenu(menu->submenu_id);
6718#ifdef FEAT_TEAROFF
6719 if (IsWindow(menu->tearoff_handle))
6720 DestroyWindow(menu->tearoff_handle);
6721 if (menu->parent != NULL
6722 && menu->parent->children != NULL
6723 && IsWindow(menu->parent->tearoff_handle))
6724 {
6725 /* This menu must not show up when rebuilding the tearoff window. */
6726 menu->modes = 0;
6727 rebuild_tearoff(menu->parent);
6728 }
6729#endif
6730 }
6731}
6732
6733#ifdef FEAT_TEAROFF
6734 static void
6735rebuild_tearoff(vimmenu_T *menu)
6736{
6737 /*hackish*/
6738 char_u tbuf[128];
6739 RECT trect;
6740 RECT rct;
6741 RECT roct;
6742 int x, y;
6743
6744 HWND thwnd = menu->tearoff_handle;
6745
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006746 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (GetWindowRect(thwnd, &trect)
6748 && GetWindowRect(s_hwnd, &rct)
6749 && GetClientRect(s_hwnd, &roct))
6750 {
6751 x = trect.left - rct.left;
6752 y = (trect.top - rct.bottom + roct.bottom);
6753 }
6754 else
6755 {
6756 x = y = 0xffffL;
6757 }
6758 DestroyWindow(thwnd);
6759 if (menu->children != NULL)
6760 {
6761 gui_mch_tearoff(tbuf, menu, x, y);
6762 if (IsWindow(menu->tearoff_handle))
6763 (void) SetWindowPos(menu->tearoff_handle,
6764 NULL,
6765 (int)trect.left,
6766 (int)trect.top,
6767 0, 0,
6768 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6769 }
6770}
6771#endif /* FEAT_TEAROFF */
6772
6773/*
6774 * Make a menu either grey or not grey.
6775 */
6776 void
6777gui_mch_menu_grey(
6778 vimmenu_T *menu,
6779 int grey)
6780{
6781#ifdef FEAT_TOOLBAR
6782 /*
6783 * is this a toolbar button?
6784 */
6785 if (menu->submenu_id == (HMENU)-1)
6786 {
6787 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6788 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6789 }
6790 else
6791#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006792 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6793 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
6795#ifdef FEAT_TEAROFF
6796 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6797 {
6798 WORD menuID;
6799 HWND menuHandle;
6800
6801 /*
6802 * A tearoff button has changed state.
6803 */
6804 if (menu->children == NULL)
6805 menuID = (WORD)(menu->id);
6806 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006807 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6809 if (menuHandle)
6810 EnableWindow(menuHandle, !grey);
6811
6812 }
6813#endif
6814}
6815
6816#endif /* FEAT_MENU */
6817
6818
6819/* define some macros used to make the dialogue creation more readable */
6820
6821#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6822#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006823#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824
6825#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6826/*
6827 * stuff for dialogs
6828 */
6829
6830/*
6831 * The callback routine used by all the dialogs. Very simple. First,
6832 * acknowledges the INITDIALOG message so that Windows knows to do standard
6833 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6834 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6835 * number.
6836 */
6837 static LRESULT CALLBACK
6838dialog_callback(
6839 HWND hwnd,
6840 UINT message,
6841 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006842 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
6844 if (message == WM_INITDIALOG)
6845 {
6846 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6847 /* Set focus to the dialog. Set the default button, if specified. */
6848 (void)SetFocus(hwnd);
6849 if (dialog_default_button > IDCANCEL)
6850 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006851 else
6852 /* We don't have a default, set focus on another element of the
6853 * dialog window, probably the icon */
6854 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 return FALSE;
6856 }
6857
6858 if (message == WM_COMMAND)
6859 {
6860 int button = LOWORD(wParam);
6861
6862 /* Don't end the dialog if something was selected that was
6863 * not a button.
6864 */
6865 if (button >= DLG_NONBUTTON_CONTROL)
6866 return TRUE;
6867
6868 /* If the edit box exists, copy the string. */
6869 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006870 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006871 /* If the OS is Windows NT, and 'encoding' differs from active
6872 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006873 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02006874 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006875 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
6876 char_u *p;
6877
6878 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6879 p = utf16_to_enc(wp, NULL);
6880 vim_strncpy(s_textfield, p, IOSIZE);
6881 vim_free(p);
6882 vim_free(wp);
6883 }
6884 else
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006885 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006886 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
6889 /*
6890 * Need to check for IDOK because if the user just hits Return to
6891 * accept the default value, some reason this is what we get.
6892 */
6893 if (button == IDOK)
6894 {
6895 if (dialog_default_button > IDCANCEL)
6896 EndDialog(hwnd, dialog_default_button);
6897 }
6898 else
6899 EndDialog(hwnd, button - IDCANCEL);
6900 return TRUE;
6901 }
6902
6903 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6904 {
6905 EndDialog(hwnd, 0);
6906 return TRUE;
6907 }
6908 return FALSE;
6909}
6910
6911/*
6912 * Create a dialog dynamically from the parameter strings.
6913 * type = type of dialog (question, alert, etc.)
6914 * title = dialog title. may be NULL for default title.
6915 * message = text to display. Dialog sizes to accommodate it.
6916 * buttons = '\n' separated list of button captions, default first.
6917 * dfltbutton = number of default button.
6918 *
6919 * This routine returns 1 if the first button is pressed,
6920 * 2 for the second, etc.
6921 *
6922 * 0 indicates Esc was pressed.
6923 * -1 for unexpected error
6924 *
6925 * If stubbing out this fn, return 1.
6926 */
6927
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006928static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 "IDR_VIM",
6931 "IDR_VIM_ERROR",
6932 "IDR_VIM_ALERT",
6933 "IDR_VIM_INFO",
6934 "IDR_VIM_QUESTION"
6935};
6936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 int
6938gui_mch_dialog(
6939 int type,
6940 char_u *title,
6941 char_u *message,
6942 char_u *buttons,
6943 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006944 char_u *textfield,
6945 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006948 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 int numButtons;
6950 int *buttonWidths, *buttonPositions;
6951 int buttonYpos;
6952 int nchar, i;
6953 DWORD lStyle;
6954 int dlgwidth = 0;
6955 int dlgheight;
6956 int editboxheight;
6957 int horizWidth = 0;
6958 int msgheight;
6959 char_u *pstart;
6960 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 char_u *tbuffer;
6963 RECT rect;
6964 HWND hwnd;
6965 HDC hdc;
6966 HFONT font, oldFont;
6967 TEXTMETRIC fontInfo;
6968 int fontHeight;
6969 int textWidth, minButtonWidth, messageWidth;
6970 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006971 int maxDialogHeight;
6972 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 int vertical;
6974 int dlgPaddingX;
6975 int dlgPaddingY;
6976#ifdef USE_SYSMENU_FONT
6977 LOGFONT lfSysmenu;
6978 int use_lfSysmenu = FALSE;
6979#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006980 garray_T ga;
6981 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982
6983#ifndef NO_CONSOLE
6984 /* Don't output anything in silent mode ("ex -s") */
6985 if (silent_mode)
6986 return dfltbutton; /* return default option */
6987#endif
6988
Bram Moolenaar748bf032005-02-02 23:04:36 +00006989 if (s_hwnd == NULL)
6990 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992 if ((type < 0) || (type > VIM_LAST_TYPE))
6993 type = 0;
6994
6995 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006996 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006997 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006998 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000 if (p == NULL)
7001 return -1;
7002
7003 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007004 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7006 * const.
7007 */
7008 tbuffer = vim_strsave(buttons);
7009 if (tbuffer == NULL)
7010 return -1;
7011
7012 --dfltbutton; /* Change from one-based to zero-based */
7013
7014 /* Count buttons */
7015 numButtons = 1;
7016 for (i = 0; tbuffer[i] != '\0'; i++)
7017 {
7018 if (tbuffer[i] == DLG_BUTTON_SEP)
7019 numButtons++;
7020 }
7021 if (dfltbutton >= numButtons)
7022 dfltbutton = -1;
7023
7024 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007025 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (buttonWidths == NULL)
7027 return -1;
7028
7029 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007030 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (buttonPositions == NULL)
7032 return -1;
7033
7034 /*
7035 * Calculate how big the dialog must be.
7036 */
7037 hwnd = GetDesktopWindow();
7038 hdc = GetWindowDC(hwnd);
7039#ifdef USE_SYSMENU_FONT
7040 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7041 {
7042 font = CreateFontIndirect(&lfSysmenu);
7043 use_lfSysmenu = TRUE;
7044 }
7045 else
7046#endif
7047 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7048 VARIABLE_PITCH , DLG_FONT_NAME);
7049 if (s_usenewlook)
7050 {
7051 oldFont = SelectFont(hdc, font);
7052 dlgPaddingX = DLG_PADDING_X;
7053 dlgPaddingY = DLG_PADDING_Y;
7054 }
7055 else
7056 {
7057 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7058 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7059 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7060 }
7061 GetTextMetrics(hdc, &fontInfo);
7062 fontHeight = fontInfo.tmHeight;
7063
7064 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007065 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
7067 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007068 if (s_hwnd == NULL)
7069 {
7070 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
Bram Moolenaarc716c302006-01-21 22:12:51 +00007072 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007073 get_work_area(&workarea_rect);
7074 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7075 if (maxDialogWidth > 600)
7076 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007077 /* Leave some room for the taskbar. */
7078 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007079 }
7080 else
7081 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007082 /* Use our own window for the size, unless it's very small. */
7083 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007084 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007085 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007086 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007087 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7088 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007089
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007090 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007091 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007092 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007093 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007094 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7095 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7096 }
7097
7098 /* Set dlgwidth to width of message.
7099 * Copy the message into "ga", changing NL to CR-NL and inserting line
7100 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 pstart = message;
7102 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007103 msgheight = 0;
7104 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 do
7106 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007107 msgheight += fontHeight; /* at least one line */
7108
7109 /* Need to figure out where to break the string. The system does it
7110 * at a word boundary, which would mean we can't compute the number of
7111 * wrapped lines. */
7112 textWidth = 0;
7113 last_white = NULL;
7114 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007115 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007116 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007117 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007118 && textWidth > maxDialogWidth * 3 / 4)
7119 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007120 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007121 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007122 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007123 /* Line will wrap. */
7124 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007125 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007126 textWidth = 0;
7127
7128 if (last_white != NULL)
7129 {
7130 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007131 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007132 pend = last_white + 1;
7133 last_white = NULL;
7134 }
7135 ga_append(&ga, '\r');
7136 ga_append(&ga, '\n');
7137 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007138 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007139
7140 while (--l >= 0)
7141 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007142 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007143 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007145
7146 ga_append(&ga, '\r');
7147 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 pstart = pend + 1;
7149 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007150
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007151 if (ga.ga_data != NULL)
7152 message = ga.ga_data;
7153
Bram Moolenaar748bf032005-02-02 23:04:36 +00007154 messageWidth += 10; /* roundoff space */
7155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007157 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7158 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
7160 if (msgheight < DLG_ICON_HEIGHT)
7161 msgheight = DLG_ICON_HEIGHT;
7162
7163 /*
7164 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007165 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007167 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (!vertical)
7169 {
7170 // Place buttons horizontally if they fit.
7171 horizWidth = dlgPaddingX;
7172 pstart = tbuffer;
7173 i = 0;
7174 do
7175 {
7176 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7177 if (pend == NULL)
7178 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007179 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (textWidth < minButtonWidth)
7181 textWidth = minButtonWidth;
7182 textWidth += dlgPaddingX; /* Padding within button */
7183 buttonWidths[i] = textWidth;
7184 buttonPositions[i++] = horizWidth;
7185 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7186 pstart = pend + 1;
7187 } while (*pend != NUL);
7188
7189 if (horizWidth > maxDialogWidth)
7190 vertical = TRUE; // Too wide to fit on the screen.
7191 else if (horizWidth > dlgwidth)
7192 dlgwidth = horizWidth;
7193 }
7194
7195 if (vertical)
7196 {
7197 // Stack buttons vertically.
7198 pstart = tbuffer;
7199 do
7200 {
7201 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7202 if (pend == NULL)
7203 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007204 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 textWidth += dlgPaddingX; /* Padding within button */
7206 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7207 if (textWidth > dlgwidth)
7208 dlgwidth = textWidth;
7209 pstart = pend + 1;
7210 } while (*pend != NUL);
7211 }
7212
7213 if (dlgwidth < DLG_MIN_WIDTH)
7214 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7215
7216 /* start to fill in the dlgtemplate information. addressing by WORDs */
7217 if (s_usenewlook)
7218 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7219 else
7220 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7221
7222 add_long(lStyle);
7223 add_long(0); // (lExtendedStyle)
7224 pnumitems = p; /*save where the number of items must be stored*/
7225 add_word(0); // NumberOfItems(will change later)
7226 add_word(10); // x
7227 add_word(10); // y
7228 add_word(PixelToDialogX(dlgwidth)); // cx
7229
7230 // Dialog height.
7231 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007232 dlgheight = msgheight + 2 * dlgPaddingY
7233 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 else
7235 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7236
7237 // Dialog needs to be taller if contains an edit box.
7238 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7239 if (textfield != NULL)
7240 dlgheight += editboxheight;
7241
Bram Moolenaara95d8232013-08-07 15:27:11 +02007242 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7243 if (dlgheight > maxDialogHeight)
7244 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007245 msgheight = msgheight - (dlgheight - maxDialogHeight);
7246 dlgheight = maxDialogHeight;
7247 scroll_flag = WS_VSCROLL;
7248 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7249 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007250 }
7251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 add_word(PixelToDialogY(dlgheight));
7253
7254 add_word(0); // Menu
7255 add_word(0); // Class
7256
7257 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007258 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7259 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 p += nchar;
7261
7262 if (s_usenewlook)
7263 {
7264 /* do the font, since DS_3DLOOK doesn't work properly */
7265#ifdef USE_SYSMENU_FONT
7266 if (use_lfSysmenu)
7267 {
7268 /* point size */
7269 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7270 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007271 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 }
7273 else
7274#endif
7275 {
7276 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007277 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 p += nchar;
7280 }
7281
7282 buttonYpos = msgheight + 2 * dlgPaddingY;
7283
7284 if (textfield != NULL)
7285 buttonYpos += editboxheight;
7286
7287 pstart = tbuffer;
7288 if (!vertical)
7289 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7290 for (i = 0; i < numButtons; i++)
7291 {
7292 /* get end of this button. */
7293 for ( pend = pstart;
7294 *pend && (*pend != DLG_BUTTON_SEP);
7295 pend++)
7296 ;
7297
7298 if (*pend)
7299 *pend = '\0';
7300
7301 /*
7302 * old NOTE:
7303 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7304 * the focus to the first tab-able button and in so doing makes that
7305 * the default!! Grrr. Workaround: Make the default button the only
7306 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7307 * he/she can use arrow keys.
7308 *
7309 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007310 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 * dialog. Also needed for when the textfield is the default control.
7312 * It appears to work now (perhaps not on Win95?).
7313 */
7314 if (vertical)
7315 {
7316 p = add_dialog_element(p,
7317 (i == dfltbutton
7318 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7319 PixelToDialogX(DLG_VERT_PADDING_X),
7320 PixelToDialogY(buttonYpos /* TBK */
7321 + 2 * fontHeight * i),
7322 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7323 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007324 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
7326 else
7327 {
7328 p = add_dialog_element(p,
7329 (i == dfltbutton
7330 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7331 PixelToDialogX(horizWidth + buttonPositions[i]),
7332 PixelToDialogY(buttonYpos), /* TBK */
7333 PixelToDialogX(buttonWidths[i]),
7334 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007335 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 }
7337 pstart = pend + 1; /*next button*/
7338 }
7339 *pnumitems += numButtons;
7340
7341 /* Vim icon */
7342 p = add_dialog_element(p, SS_ICON,
7343 PixelToDialogX(dlgPaddingX),
7344 PixelToDialogY(dlgPaddingY),
7345 PixelToDialogX(DLG_ICON_WIDTH),
7346 PixelToDialogY(DLG_ICON_HEIGHT),
7347 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7348 dlg_icons[type]);
7349
Bram Moolenaar748bf032005-02-02 23:04:36 +00007350 /* Dialog message */
7351 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7352 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7353 PixelToDialogY(dlgPaddingY),
7354 (WORD)(PixelToDialogX(messageWidth) + 1),
7355 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007356 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 /* Edit box */
7359 if (textfield != NULL)
7360 {
7361 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7362 PixelToDialogX(2 * dlgPaddingX),
7363 PixelToDialogY(2 * dlgPaddingY + msgheight),
7364 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7365 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007366 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 *pnumitems += 1;
7368 }
7369
7370 *pnumitems += 2;
7371
7372 SelectFont(hdc, oldFont);
7373 DeleteObject(font);
7374 ReleaseDC(hwnd, hdc);
7375
7376 /* Let the dialog_callback() function know which button to make default
7377 * If we have an edit box, make that the default. We also need to tell
7378 * dialog_callback() if this dialog contains an edit box or not. We do
7379 * this by setting s_textfield if it does.
7380 */
7381 if (textfield != NULL)
7382 {
7383 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7384 s_textfield = textfield;
7385 }
7386 else
7387 {
7388 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7389 s_textfield = NULL;
7390 }
7391
7392 /* show the dialog box modally and get a return value */
7393 nchar = (int)DialogBoxIndirect(
7394 s_hinst,
7395 (LPDLGTEMPLATE)pdlgtemplate,
7396 s_hwnd,
7397 (DLGPROC)dialog_callback);
7398
7399 LocalFree(LocalHandle(pdlgtemplate));
7400 vim_free(tbuffer);
7401 vim_free(buttonWidths);
7402 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007403 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
7405 /* Focus back to our window (for when MDI is used). */
7406 (void)SetFocus(s_hwnd);
7407
7408 return nchar;
7409}
7410
7411#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413/*
7414 * Put a simple element (basic class) onto a dialog template in memory.
7415 * return a pointer to where the next item should be added.
7416 *
7417 * parameters:
7418 * lStyle = additional style flags
7419 * (be careful, NT3.51 & Win32s will ignore the new ones)
7420 * x,y = x & y positions IN DIALOG UNITS
7421 * w,h = width and height IN DIALOG UNITS
7422 * Id = ID used in messages
7423 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7424 * caption = usually text or resource name
7425 *
7426 * TODO: use the length information noted here to enable the dialog creation
7427 * routines to work out more exactly how much memory they need to alloc.
7428 */
7429 static PWORD
7430add_dialog_element(
7431 PWORD p,
7432 DWORD lStyle,
7433 WORD x,
7434 WORD y,
7435 WORD w,
7436 WORD h,
7437 WORD Id,
7438 WORD clss,
7439 const char *caption)
7440{
7441 int nchar;
7442
7443 p = lpwAlign(p); /* Align to dword boundary*/
7444 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7445 *p++ = LOWORD(lStyle);
7446 *p++ = HIWORD(lStyle);
7447 *p++ = 0; // LOWORD (lExtendedStyle)
7448 *p++ = 0; // HIWORD (lExtendedStyle)
7449 *p++ = x;
7450 *p++ = y;
7451 *p++ = w;
7452 *p++ = h;
7453 *p++ = Id; //9 or 10 words in all
7454
7455 *p++ = (WORD)0xffff;
7456 *p++ = clss; //2 more here
7457
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007458 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 p += nchar;
7460
7461 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7462
7463 return p; //total = 15+ (strlen(caption)) words
7464 // = 30 + 2(strlen(caption) bytes reqd
7465}
7466
7467
7468/*
7469 * Helper routine. Take an input pointer, return closest pointer that is
7470 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7471 */
7472 static LPWORD
7473lpwAlign(
7474 LPWORD lpIn)
7475{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007476 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007478 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 ul += 3;
7480 ul >>= 2;
7481 ul <<= 2;
7482 return (LPWORD)ul;
7483}
7484
7485/*
7486 * Helper routine. Takes second parameter as Ansi string, copies it to first
7487 * parameter as wide character (16-bits / char) string, and returns integer
7488 * number of wide characters (words) in string (including the trailing wide
7489 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007490 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7491 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 static int
7493nCopyAnsiToWideChar(
7494 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007495 LPSTR lpAnsiIn,
7496 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 int nChar = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7500 int i;
7501 WCHAR *wn;
7502
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007503 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 {
7505 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007506 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (wn != NULL)
7508 {
7509 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007510 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 vim_free(wn);
7512 }
7513 }
7514 if (nChar == 0)
7515 /* Use Win32 conversion function. */
7516 nChar = MultiByteToWideChar(
7517 enc_codepage > 0 ? enc_codepage : CP_ACP,
7518 MB_PRECOMPOSED,
7519 lpAnsiIn, len,
7520 lpWCStr, len);
7521 for (i = 0; i < nChar; ++i)
7522 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7523 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
7525 return nChar;
7526}
7527
7528
7529#ifdef FEAT_TEAROFF
7530/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 * Lookup menu handle from "menu_id".
7532 */
7533 static HMENU
7534tearoff_lookup_menuhandle(
7535 vimmenu_T *menu,
7536 WORD menu_id)
7537{
7538 for ( ; menu != NULL; menu = menu->next)
7539 {
7540 if (menu->modes == 0) /* this menu has just been deleted */
7541 continue;
7542 if (menu_is_separator(menu->dname))
7543 continue;
7544 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7545 return menu->submenu_id;
7546 }
7547 return NULL;
7548}
7549
7550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 * The callback function for all the modeless dialogs that make up the
7552 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7553 * thinking its menus have been clicked), and go away when closed.
7554 */
7555 static LRESULT CALLBACK
7556tearoff_callback(
7557 HWND hwnd,
7558 UINT message,
7559 WPARAM wParam,
7560 LPARAM lParam)
7561{
7562 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007563 {
7564 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 /* May show the mouse pointer again. */
7569 HandleMouseHide(message, lParam);
7570
7571 if (message == WM_COMMAND)
7572 {
7573 if ((WORD)(LOWORD(wParam)) & 0x8000)
7574 {
7575 POINT mp;
7576 RECT rect;
7577
7578 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7579 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007580 vimmenu_T *menu;
7581
7582 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007584 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7586 (int)rect.right - 8,
7587 (int)mp.y,
7588 (int)0, /*reserved param*/
7589 s_hwnd,
7590 NULL);
7591 /*
7592 * NOTE: The pop-up menu can eat the mouse up event.
7593 * We deal with this in normal.c.
7594 */
7595 }
7596 }
7597 else
7598 /* Pass on messages to the main Vim window */
7599 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7600 /*
7601 * Give main window the focus back: this is so after
7602 * choosing a tearoff button you can start typing again
7603 * straight away.
7604 */
7605 (void)SetFocus(s_hwnd);
7606 return TRUE;
7607 }
7608 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7609 {
7610 DestroyWindow(hwnd);
7611 return TRUE;
7612 }
7613
7614 /* When moved around, give main window the focus back. */
7615 if (message == WM_EXITSIZEMOVE)
7616 (void)SetActiveWindow(s_hwnd);
7617
7618 return FALSE;
7619}
7620#endif
7621
7622
7623/*
7624 * Decide whether to use the "new look" (small, non-bold font) or the "old
7625 * look" (big, clanky font) for dialogs, and work out a few values for use
7626 * later accordingly.
7627 */
7628 static void
7629get_dialog_font_metrics(void)
7630{
7631 HDC hdc;
7632 HFONT hfontTools = 0;
7633 DWORD dlgFontSize;
7634 SIZE size;
7635#ifdef USE_SYSMENU_FONT
7636 LOGFONT lfSysmenu;
7637#endif
7638
7639 s_usenewlook = FALSE;
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007642 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7643 hfontTools = CreateFontIndirect(&lfSysmenu);
7644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645#endif
7646 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7647 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7648
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007649 if (hfontTools)
7650 {
7651 hdc = GetDC(s_hwnd);
7652 SelectObject(hdc, hfontTools);
7653 /*
7654 * GetTextMetrics() doesn't return the right value in
7655 * tmAveCharWidth, so we have to figure out the dialog base units
7656 * ourselves.
7657 */
7658 GetTextExtentPoint(hdc,
7659 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7660 52, &size);
7661 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007663 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7664 s_dlgfntheight = (WORD)size.cy;
7665 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 }
7667
7668 if (!s_usenewlook)
7669 {
7670 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7671 s_dlgfntwidth = LOWORD(dlgFontSize);
7672 s_dlgfntheight = HIWORD(dlgFontSize);
7673 }
7674}
7675
7676#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7677/*
7678 * Create a pseudo-"tearoff menu" based on the child
7679 * items of a given menu pointer.
7680 */
7681 static void
7682gui_mch_tearoff(
7683 char_u *title,
7684 vimmenu_T *menu,
7685 int initX,
7686 int initY)
7687{
7688 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7689 int template_len;
7690 int nchar, textWidth, submenuWidth;
7691 DWORD lStyle;
7692 DWORD lExtendedStyle;
7693 WORD dlgwidth;
7694 WORD menuID;
7695 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007696 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 vimmenu_T *the_menu = menu;
7698 HWND hwnd;
7699 HDC hdc;
7700 HFONT font, oldFont;
7701 int col, spaceWidth, len;
7702 int columnWidths[2];
7703 char_u *label, *text;
7704 int acLen = 0;
7705 int nameLen;
7706 int padding0, padding1, padding2 = 0;
7707 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007708 int x;
7709 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#ifdef USE_SYSMENU_FONT
7711 LOGFONT lfSysmenu;
7712 int use_lfSysmenu = FALSE;
7713#endif
7714
7715 /*
7716 * If this menu is already torn off, move it to the mouse position.
7717 */
7718 if (IsWindow(menu->tearoff_handle))
7719 {
7720 POINT mp;
7721 if (GetCursorPos((LPPOINT)&mp))
7722 {
7723 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7724 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7725 }
7726 return;
7727 }
7728
7729 /*
7730 * Create a new tearoff.
7731 */
7732 if (*title == MNU_HIDDEN_CHAR)
7733 title++;
7734
7735 /* Allocate memory to store the dialog template. It's made bigger when
7736 * needed. */
7737 template_len = DLG_ALLOC_SIZE;
7738 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7739 if (p == NULL)
7740 return;
7741
7742 hwnd = GetDesktopWindow();
7743 hdc = GetWindowDC(hwnd);
7744#ifdef USE_SYSMENU_FONT
7745 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7746 {
7747 font = CreateFontIndirect(&lfSysmenu);
7748 use_lfSysmenu = TRUE;
7749 }
7750 else
7751#endif
7752 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7753 VARIABLE_PITCH , DLG_FONT_NAME);
7754 if (s_usenewlook)
7755 oldFont = SelectFont(hdc, font);
7756 else
7757 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7758
7759 /* Calculate width of a single space. Used for padding columns to the
7760 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007761 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 /* Figure out max width of the text column, the accelerator column and the
7764 * optional submenu column. */
7765 submenuWidth = 0;
7766 for (col = 0; col < 2; col++)
7767 {
7768 columnWidths[col] = 0;
7769 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7770 {
7771 /* Use "dname" here to compute the width of the visible text. */
7772 text = (col == 0) ? pmenu->dname : pmenu->actext;
7773 if (text != NULL && *text != NUL)
7774 {
7775 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7776 if (textWidth > columnWidths[col])
7777 columnWidths[col] = textWidth;
7778 }
7779 if (pmenu->children != NULL)
7780 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7781 }
7782 }
7783 if (columnWidths[1] == 0)
7784 {
7785 /* no accelerators */
7786 if (submenuWidth != 0)
7787 columnWidths[0] += submenuWidth;
7788 else
7789 columnWidths[0] += spaceWidth;
7790 }
7791 else
7792 {
7793 /* there is an accelerator column */
7794 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7795 columnWidths[1] += submenuWidth;
7796 }
7797
7798 /*
7799 * Now find the total width of our 'menu'.
7800 */
7801 textWidth = columnWidths[0] + columnWidths[1];
7802 if (submenuWidth != 0)
7803 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007804 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7806 textWidth += submenuWidth;
7807 }
7808 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7809 if (textWidth > dlgwidth)
7810 dlgwidth = textWidth;
7811 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 /* start to fill in the dlgtemplate information. addressing by WORDs */
7814 if (s_usenewlook)
7815 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7816 else
7817 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7818
7819 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7820 *p++ = LOWORD(lStyle);
7821 *p++ = HIWORD(lStyle);
7822 *p++ = LOWORD(lExtendedStyle);
7823 *p++ = HIWORD(lExtendedStyle);
7824 pnumitems = p; /* save where the number of items must be stored */
7825 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007826 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007828 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 else
7830 *p++ = PixelToDialogX(initX); // x
7831 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007832 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 else
7834 *p++ = PixelToDialogY(initY); // y
7835 *p++ = PixelToDialogX(dlgwidth); // cx
7836 ptrueheight = p;
7837 *p++ = 0; // dialog height: changed later anyway
7838 *p++ = 0; // Menu
7839 *p++ = 0; // Class
7840
7841 /* copy the title of the dialog */
7842 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007843 ? (LPSTR)title
7844 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 p += nchar;
7846
7847 if (s_usenewlook)
7848 {
7849 /* do the font, since DS_3DLOOK doesn't work properly */
7850#ifdef USE_SYSMENU_FONT
7851 if (use_lfSysmenu)
7852 {
7853 /* point size */
7854 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7855 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007856 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858 else
7859#endif
7860 {
7861 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007862 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864 p += nchar;
7865 }
7866
7867 /*
7868 * Loop over all the items in the menu.
7869 * But skip over the tearbar.
7870 */
7871 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7872 menu = menu->children->next;
7873 else
7874 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007875 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 for ( ; menu != NULL; menu = menu->next)
7877 {
7878 if (menu->modes == 0) /* this menu has just been deleted */
7879 continue;
7880 if (menu_is_separator(menu->dname))
7881 {
7882 sepPadding += 3;
7883 continue;
7884 }
7885
7886 /* Check if there still is plenty of room in the template. Make it
7887 * larger when needed. */
7888 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7889 {
7890 WORD *newp;
7891
7892 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7893 if (newp != NULL)
7894 {
7895 template_len += 4096;
7896 mch_memmove(newp, pdlgtemplate,
7897 (char *)p - (char *)pdlgtemplate);
7898 p = newp + (p - pdlgtemplate);
7899 pnumitems = newp + (pnumitems - pdlgtemplate);
7900 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7901 LocalFree(LocalHandle(pdlgtemplate));
7902 pdlgtemplate = newp;
7903 }
7904 }
7905
7906 /* Figure out minimal length of this menu label. Use "name" for the
7907 * actual text, "dname" for estimating the displayed size. "name"
7908 * has "&a" for mnemonic and includes the accelerator. */
7909 len = nameLen = (int)STRLEN(menu->name);
7910 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7911 (int)STRLEN(menu->dname))) / spaceWidth;
7912 len += padding0;
7913
7914 if (menu->actext != NULL)
7915 {
7916 acLen = (int)STRLEN(menu->actext);
7917 len += acLen;
7918 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7919 }
7920 else
7921 textWidth = 0;
7922 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7923 len += padding1;
7924
7925 if (menu->children == NULL)
7926 {
7927 padding2 = submenuWidth / spaceWidth;
7928 len += padding2;
7929 menuID = (WORD)(menu->id);
7930 }
7931 else
7932 {
7933 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007934 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 }
7936
7937 /* Allocate menu label and fill it in */
7938 text = label = alloc((unsigned)len + 1);
7939 if (label == NULL)
7940 break;
7941
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007942 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 text = vim_strchr(text, TAB); /* stop at TAB before actext */
7944 if (text == NULL)
7945 text = label + nameLen; /* no actext, use whole name */
7946 while (padding0-- > 0)
7947 *text++ = ' ';
7948 if (menu->actext != NULL)
7949 {
7950 STRNCPY(text, menu->actext, acLen);
7951 text += acLen;
7952 }
7953 while (padding1-- > 0)
7954 *text++ = ' ';
7955 if (menu->children != NULL)
7956 {
7957 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7958 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7959 }
7960 else
7961 {
7962 while (padding2-- > 0)
7963 *text++ = ' ';
7964 }
7965 *text = NUL;
7966
7967 /*
7968 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7969 * W95/NT4 it makes the tear-off look more like a menu.
7970 */
7971 p = add_dialog_element(p,
7972 BS_PUSHBUTTON|BS_LEFT,
7973 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7974 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7975 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7976 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007977 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 vim_free(label);
7979 (*pnumitems)++;
7980 }
7981
7982 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7983
7984
7985 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02007986 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 s_hinst,
7988 (LPDLGTEMPLATE)pdlgtemplate,
7989 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007990 (DLGPROC)tearoff_callback,
7991 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
7993 LocalFree(LocalHandle(pdlgtemplate));
7994 SelectFont(hdc, oldFont);
7995 DeleteObject(font);
7996 ReleaseDC(hwnd, hdc);
7997
7998 /*
7999 * Reassert ourselves as the active window. This is so that after creating
8000 * a tearoff, the user doesn't have to click with the mouse just to start
8001 * typing again!
8002 */
8003 (void)SetActiveWindow(s_hwnd);
8004
8005 /* make sure the right buttons are enabled */
8006 force_menu_update = TRUE;
8007}
8008#endif
8009
8010#if defined(FEAT_TOOLBAR) || defined(PROTO)
8011#include "gui_w32_rc.h"
8012
8013/* This not defined in older SDKs */
8014# ifndef TBSTYLE_FLAT
8015# define TBSTYLE_FLAT 0x0800
8016# endif
8017
8018/*
8019 * Create the toolbar, initially unpopulated.
8020 * (just like the menu, there are no defaults, it's all
8021 * set up through menu.vim)
8022 */
8023 static void
8024initialise_toolbar(void)
8025{
8026 InitCommonControls();
8027 s_toolbarhwnd = CreateToolbarEx(
8028 s_hwnd,
8029 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8030 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008031 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 s_hinst,
8033 IDR_TOOLBAR1, // id of initial bitmap
8034 NULL,
8035 0, // initial number of buttons
8036 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8037 TOOLBAR_BUTTON_HEIGHT,
8038 TOOLBAR_BUTTON_WIDTH,
8039 TOOLBAR_BUTTON_HEIGHT,
8040 sizeof(TBBUTTON)
8041 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008042 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043
8044 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8045}
8046
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008047 static LRESULT CALLBACK
8048toolbar_wndproc(
8049 HWND hwnd,
8050 UINT uMsg,
8051 WPARAM wParam,
8052 LPARAM lParam)
8053{
8054 HandleMouseHide(uMsg, lParam);
8055 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8056}
8057
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 static int
8059get_toolbar_bitmap(vimmenu_T *menu)
8060{
8061 int i = -1;
8062
8063 /*
8064 * Check user bitmaps first, unless builtin is specified.
8065 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008066 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {
8068 char_u fname[MAXPATHL];
8069 HANDLE hbitmap = NULL;
8070
8071 if (menu->iconfile != NULL)
8072 {
8073 gui_find_iconfile(menu->iconfile, fname, "bmp");
8074 hbitmap = LoadImage(
8075 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008076 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 IMAGE_BITMAP,
8078 TOOLBAR_BUTTON_WIDTH,
8079 TOOLBAR_BUTTON_HEIGHT,
8080 LR_LOADFROMFILE |
8081 LR_LOADMAP3DCOLORS
8082 );
8083 }
8084
8085 /*
8086 * If the LoadImage call failed, or the "icon=" file
8087 * didn't exist or wasn't specified, try the menu name
8088 */
8089 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008090 && (gui_find_bitmap(
8091#ifdef FEAT_MULTI_LANG
8092 menu->en_dname != NULL ? menu->en_dname :
8093#endif
8094 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 hbitmap = LoadImage(
8096 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008097 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 IMAGE_BITMAP,
8099 TOOLBAR_BUTTON_WIDTH,
8100 TOOLBAR_BUTTON_HEIGHT,
8101 LR_LOADFROMFILE |
8102 LR_LOADMAP3DCOLORS
8103 );
8104
8105 if (hbitmap != NULL)
8106 {
8107 TBADDBITMAP tbAddBitmap;
8108
8109 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008110 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8113 (WPARAM)1, (LPARAM)&tbAddBitmap);
8114 /* i will be set to -1 if it fails */
8115 }
8116 }
8117 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8118 i = menu->iconidx;
8119
8120 return i;
8121}
8122#endif
8123
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008124#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8125 static void
8126initialise_tabline(void)
8127{
8128 InitCommonControls();
8129
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008130 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008131 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008132 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8133 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008134 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008135
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008136 gui.tabline_height = TABLINE_HEIGHT;
8137
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008138# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008139 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008140# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008141}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008142
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008143/*
8144 * Get tabpage_T from POINT.
8145 */
8146 static tabpage_T *
8147GetTabFromPoint(
8148 HWND hWnd,
8149 POINT pt)
8150{
8151 tabpage_T *ptp = NULL;
8152
8153 if (gui_mch_showing_tabline())
8154 {
8155 TCHITTESTINFO htinfo;
8156 htinfo.pt = pt;
8157 /* ignore if a window under cusor is not tabcontrol. */
8158 if (s_tabhwnd == hWnd)
8159 {
8160 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8161 if (idx != -1)
8162 ptp = find_tabpage(idx + 1);
8163 }
8164 }
8165 return ptp;
8166}
8167
8168static POINT s_pt = {0, 0};
8169static HCURSOR s_hCursor = NULL;
8170
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008171 static LRESULT CALLBACK
8172tabline_wndproc(
8173 HWND hwnd,
8174 UINT uMsg,
8175 WPARAM wParam,
8176 LPARAM lParam)
8177{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008178 POINT pt;
8179 tabpage_T *tp;
8180 RECT rect;
8181 int nCenter;
8182 int idx0;
8183 int idx1;
8184
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008185 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008186
8187 switch (uMsg)
8188 {
8189 case WM_LBUTTONDOWN:
8190 {
8191 s_pt.x = GET_X_LPARAM(lParam);
8192 s_pt.y = GET_Y_LPARAM(lParam);
8193 SetCapture(hwnd);
8194 s_hCursor = GetCursor(); /* backup default cursor */
8195 break;
8196 }
8197 case WM_MOUSEMOVE:
8198 if (GetCapture() == hwnd
8199 && ((wParam & MK_LBUTTON)) != 0)
8200 {
8201 pt.x = GET_X_LPARAM(lParam);
8202 pt.y = s_pt.y;
8203 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8204 {
8205 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8206
8207 tp = GetTabFromPoint(hwnd, pt);
8208 if (tp != NULL)
8209 {
8210 idx0 = tabpage_index(curtab) - 1;
8211 idx1 = tabpage_index(tp) - 1;
8212
8213 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8214 nCenter = rect.left + (rect.right - rect.left) / 2;
8215
8216 /* Check if the mouse cursor goes over the center of
8217 * the next tab to prevent "flickering". */
8218 if ((idx0 < idx1) && (nCenter < pt.x))
8219 {
8220 tabpage_move(idx1 + 1);
8221 update_screen(0);
8222 }
8223 else if ((idx1 < idx0) && (pt.x < nCenter))
8224 {
8225 tabpage_move(idx1);
8226 update_screen(0);
8227 }
8228 }
8229 }
8230 }
8231 break;
8232 case WM_LBUTTONUP:
8233 {
8234 if (GetCapture() == hwnd)
8235 {
8236 SetCursor(s_hCursor);
8237 ReleaseCapture();
8238 }
8239 break;
8240 }
8241 default:
8242 break;
8243 }
8244
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008245 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8246}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008247#endif
8248
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8250/*
8251 * Make the GUI window come to the foreground.
8252 */
8253 void
8254gui_mch_set_foreground(void)
8255{
8256 if (IsIconic(s_hwnd))
8257 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8258 SetForegroundWindow(s_hwnd);
8259}
8260#endif
8261
8262#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8263 static void
8264dyn_imm_load(void)
8265{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008266 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (hLibImm == NULL)
8268 return;
8269
8270 pImmGetCompositionStringA
8271 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8272 pImmGetCompositionStringW
8273 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8274 pImmGetContext
8275 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8276 pImmAssociateContext
8277 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8278 pImmReleaseContext
8279 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8280 pImmGetOpenStatus
8281 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8282 pImmSetOpenStatus
8283 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8284 pImmGetCompositionFont
8285 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8286 pImmSetCompositionFont
8287 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8288 pImmSetCompositionWindow
8289 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8290 pImmGetConversionStatus
8291 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008292 pImmSetConversionStatus
8293 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294
8295 if ( pImmGetCompositionStringA == NULL
8296 || pImmGetCompositionStringW == NULL
8297 || pImmGetContext == NULL
8298 || pImmAssociateContext == NULL
8299 || pImmReleaseContext == NULL
8300 || pImmGetOpenStatus == NULL
8301 || pImmSetOpenStatus == NULL
8302 || pImmGetCompositionFont == NULL
8303 || pImmSetCompositionFont == NULL
8304 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008305 || pImmGetConversionStatus == NULL
8306 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {
8308 FreeLibrary(hLibImm);
8309 hLibImm = NULL;
8310 pImmGetContext = NULL;
8311 return;
8312 }
8313
8314 return;
8315}
8316
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#endif
8318
8319#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8320
8321# ifdef FEAT_XPM_W32
8322# define IMAGE_XPM 100
8323# endif
8324
8325typedef struct _signicon_t
8326{
8327 HANDLE hImage;
8328 UINT uType;
8329#ifdef FEAT_XPM_W32
8330 HANDLE hShape; /* Mask bitmap handle */
8331#endif
8332} signicon_t;
8333
8334 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008335gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 signicon_t *sign;
8338 int x, y, w, h;
8339
8340 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8341 return;
8342
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008343#if defined(FEAT_DIRECTX)
8344 if (IS_ENABLE_DIRECTX())
8345 DWriteContext_Flush(s_dwc);
8346#endif
8347
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 x = TEXT_X(col);
8349 y = TEXT_Y(row);
8350 w = gui.char_width * 2;
8351 h = gui.char_height;
8352 switch (sign->uType)
8353 {
8354 case IMAGE_BITMAP:
8355 {
8356 HDC hdcMem;
8357 HBITMAP hbmpOld;
8358
8359 hdcMem = CreateCompatibleDC(s_hdc);
8360 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8361 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8362 SelectObject(hdcMem, hbmpOld);
8363 DeleteDC(hdcMem);
8364 }
8365 break;
8366 case IMAGE_ICON:
8367 case IMAGE_CURSOR:
8368 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8369 break;
8370#ifdef FEAT_XPM_W32
8371 case IMAGE_XPM:
8372 {
8373 HDC hdcMem;
8374 HBITMAP hbmpOld;
8375
8376 hdcMem = CreateCompatibleDC(s_hdc);
8377 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8378 /* Make hole */
8379 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8380
8381 SelectObject(hdcMem, sign->hImage);
8382 /* Paint sign */
8383 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8384 SelectObject(hdcMem, hbmpOld);
8385 DeleteDC(hdcMem);
8386 }
8387 break;
8388#endif
8389 }
8390}
8391
8392 static void
8393close_signicon_image(signicon_t *sign)
8394{
8395 if (sign)
8396 switch (sign->uType)
8397 {
8398 case IMAGE_BITMAP:
8399 DeleteObject((HGDIOBJ)sign->hImage);
8400 break;
8401 case IMAGE_CURSOR:
8402 DestroyCursor((HCURSOR)sign->hImage);
8403 break;
8404 case IMAGE_ICON:
8405 DestroyIcon((HICON)sign->hImage);
8406 break;
8407#ifdef FEAT_XPM_W32
8408 case IMAGE_XPM:
8409 DeleteObject((HBITMAP)sign->hImage);
8410 DeleteObject((HBITMAP)sign->hShape);
8411 break;
8412#endif
8413 }
8414}
8415
8416 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008417gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 signicon_t sign, *psign;
8420 char_u *ext;
8421
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008423 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 if (ext > signfile)
8425 {
8426 int do_load = 1;
8427
8428 if (!STRICMP(ext, ".bmp"))
8429 sign.uType = IMAGE_BITMAP;
8430 else if (!STRICMP(ext, ".ico"))
8431 sign.uType = IMAGE_ICON;
8432 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8433 sign.uType = IMAGE_CURSOR;
8434 else
8435 do_load = 0;
8436
8437 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008438 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 gui.char_width * 2, gui.char_height,
8440 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8441#ifdef FEAT_XPM_W32
8442 if (!STRICMP(ext, ".xpm"))
8443 {
8444 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008445 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8446 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 }
8448#endif
8449 }
8450
8451 psign = NULL;
8452 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8453 != NULL)
8454 *psign = sign;
8455
8456 if (!psign)
8457 {
8458 if (sign.hImage)
8459 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008460 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462 return (void *)psign;
8463
8464}
8465
8466 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008467gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469 if (sign)
8470 {
8471 close_signicon_image((signicon_t *)sign);
8472 vim_free(sign);
8473 }
8474}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008477#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008480 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008482 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8484 * to get current mouse position).
8485 *
8486 * Trying to use as more Windows services as possible, and as less
8487 * IE version as possible :)).
8488 *
8489 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8490 * BalloonEval struct.
8491 * 2) Enable/Disable simply create/kill BalloonEval Timer
8492 * 3) When there was enough inactivity, timer procedure posts
8493 * async request to debugger
8494 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8495 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008496 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 */
8498
Bram Moolenaar45360022005-07-21 21:08:21 +00008499/*
8500 * determine whether installed Common Controls support multiline tooltips
8501 * (i.e. their version is >= 4.70
8502 */
8503 int
8504multiline_balloon_available(void)
8505{
8506 HINSTANCE hDll;
8507 static char comctl_dll[] = "comctl32.dll";
8508 static int multiline_tip = MAYBE;
8509
8510 if (multiline_tip != MAYBE)
8511 return multiline_tip;
8512
8513 hDll = GetModuleHandle(comctl_dll);
8514 if (hDll != NULL)
8515 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008516 DLLGETVERSIONPROC pGetVer;
8517 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008518
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008519 if (pGetVer != NULL)
8520 {
8521 DLLVERSIONINFO dvi;
8522 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008523
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008524 ZeroMemory(&dvi, sizeof(dvi));
8525 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008526
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008527 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008528
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008529 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008530 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008531 || (dvi.dwMajorVersion == 4
8532 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008533 {
8534 multiline_tip = TRUE;
8535 return multiline_tip;
8536 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008537 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008538 else
8539 {
8540 /* there is chance we have ancient CommCtl 4.70
8541 which doesn't export DllGetVersion */
8542 DWORD dwHandle = 0;
8543 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8544 if (len > 0)
8545 {
8546 VS_FIXEDFILEINFO *ver;
8547 UINT vlen = 0;
8548 void *data = alloc(len);
8549
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008550 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008551 && GetFileVersionInfo(comctl_dll, 0, len, data)
8552 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8553 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008554 && HIWORD(ver->dwFileVersionMS) > 4)
8555 || ((HIWORD(ver->dwFileVersionMS) == 4
8556 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008557 {
8558 vim_free(data);
8559 multiline_tip = TRUE;
8560 return multiline_tip;
8561 }
8562 vim_free(data);
8563 }
8564 }
8565 }
8566 multiline_tip = FALSE;
8567 return multiline_tip;
8568}
8569
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008570 static void
8571make_tooltipw(BalloonEval *beval, char *text, POINT pt)
8572{
8573 TOOLINFOW *pti;
8574 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008575
8576 if (multiline_balloon_available() == TRUE)
8577 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8578 else
8579 ToolInfoSize = sizeof(TOOLINFOW);
8580
8581 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8582 if (pti == NULL)
8583 return;
8584
8585 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8586 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8587 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8588 beval->target, NULL, s_hinst, NULL);
8589
8590 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8591 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8592
8593 pti->cbSize = ToolInfoSize;
8594 pti->uFlags = TTF_SUBCLASS;
8595 pti->hwnd = beval->target;
8596 pti->hinst = 0; // Don't use string resources
8597 pti->uId = ID_BEVAL_TOOLTIP;
8598
8599 if (multiline_balloon_available() == TRUE)
8600 {
8601 RECT rect;
8602 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8603 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008604 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8605 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008606 // switch multiline tooltips on
8607 if (GetClientRect(s_textArea, &rect))
8608 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8609 (LPARAM)rect.right);
8610 }
8611 else
8612 {
8613 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008614 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8615 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008616 }
8617
8618 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8619 pti->rect.left = pt.x - 3;
8620 pti->rect.top = pt.y - 3;
8621 pti->rect.right = pt.x + 3;
8622 pti->rect.bottom = pt.y + 3;
8623
8624 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8625 // Make tooltip appear sooner.
8626 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8627 // I've performed some tests and it seems the longest possible life time
8628 // of tooltip is 30 seconds.
8629 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8630 /*
8631 * HACK: force tooltip to appear, because it'll not appear until
8632 * first mouse move. D*mn M$
8633 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8634 */
8635 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8636 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8637 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008638}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008639
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008641make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
Bram Moolenaar45360022005-07-21 21:08:21 +00008643 TOOLINFO *pti;
8644 int ToolInfoSize;
8645
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008646 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
8647 {
8648 make_tooltipw(beval, text, pt);
8649 return;
8650 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008651
Bram Moolenaar45360022005-07-21 21:08:21 +00008652 if (multiline_balloon_available() == TRUE)
8653 ToolInfoSize = sizeof(TOOLINFO_NEW);
8654 else
8655 ToolInfoSize = sizeof(TOOLINFO);
8656
8657 pti = (TOOLINFO *)alloc(ToolInfoSize);
8658 if (pti == NULL)
8659 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660
8661 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8662 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8663 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8664 beval->target, NULL, s_hinst, NULL);
8665
8666 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8667 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8668
Bram Moolenaar45360022005-07-21 21:08:21 +00008669 pti->cbSize = ToolInfoSize;
8670 pti->uFlags = TTF_SUBCLASS;
8671 pti->hwnd = beval->target;
8672 pti->hinst = 0; /* Don't use string resources */
8673 pti->uId = ID_BEVAL_TOOLTIP;
8674
8675 if (multiline_balloon_available() == TRUE)
8676 {
8677 RECT rect;
8678 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8679 pti->lpszText = LPSTR_TEXTCALLBACK;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008680 beval->tofree = vim_strsave((char_u*)text);
8681 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaar45360022005-07-21 21:08:21 +00008682 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8683 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8684 (LPARAM)rect.right);
8685 }
8686 else
8687 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008690 pti->rect.left = pt.x - 3;
8691 pti->rect.top = pt.y - 3;
8692 pti->rect.right = pt.x + 3;
8693 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694
Bram Moolenaar45360022005-07-21 21:08:21 +00008695 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 /* Make tooltip appear sooner */
8697 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008698 /* I've performed some tests and it seems the longest possible life time
8699 * of tooltip is 30 seconds */
8700 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 /*
8702 * HACK: force tooltip to appear, because it'll not appear until
8703 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008704 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008706 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008708 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709}
8710
8711 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008712delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008714 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715}
8716
8717 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008718BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008719 HWND hwnd UNUSED,
8720 UINT uMsg UNUSED,
8721 UINT_PTR idEvent UNUSED,
8722 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 POINT pt;
8725 RECT rect;
8726
8727 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8728 return;
8729
8730 GetCursorPos(&pt);
8731 if (WindowFromPoint(pt) != s_textArea)
8732 return;
8733
8734 ScreenToClient(s_textArea, &pt);
8735 GetClientRect(s_textArea, &rect);
8736 if (!PtInRect(&rect, pt))
8737 return;
8738
8739 if (LastActivity > 0
8740 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8741 && (cur_beval->showState != ShS_PENDING
8742 || abs(cur_beval->x - pt.x) > 3
8743 || abs(cur_beval->y - pt.y) > 3))
8744 {
8745 /* Pointer resting in one place long enough, it's time to show
8746 * the tooltip. */
8747 cur_beval->showState = ShS_PENDING;
8748 cur_beval->x = pt.x;
8749 cur_beval->y = pt.y;
8750
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008751 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752
8753 if (cur_beval->msgCB != NULL)
8754 (*cur_beval->msgCB)(cur_beval, 0);
8755 }
8756}
8757
8758 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008759gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008761 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008763 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764}
8765
8766 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008767gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008769 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (beval == NULL)
8771 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008772 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008773 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008774 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775}
8776
8777 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008778gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008781
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008782 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 if (beval->showState == ShS_SHOWING)
8784 return;
8785 GetCursorPos(&pt);
8786 ScreenToClient(s_textArea, &pt);
8787
8788 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008790 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 gui_mch_disable_beval_area(cur_beval);
8792 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008793 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008795 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008799gui_mch_create_beval_area(
8800 void *target, /* ignored, always use s_textArea */
8801 char_u *mesg,
8802 void (*mesgCB)(BalloonEval *, int),
8803 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 /* partially stolen from gui_beval.c */
8806 BalloonEval *beval;
8807
8808 if (mesg != NULL && mesgCB != NULL)
8809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008810 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 return NULL;
8812 }
8813
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008814 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (beval != NULL)
8816 {
8817 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
8819 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 beval->msg = mesg;
8821 beval->msgCB = mesgCB;
8822 beval->clientData = clientData;
8823
8824 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 cur_beval = beval;
8826
8827 if (p_beval)
8828 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 }
8830 return beval;
8831}
8832
8833 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008834Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8837 return;
8838
8839 if (cur_beval != NULL)
8840 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008841 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008843 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008844 // TRACE0("TTN_SHOW {{{");
8845 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008846 break;
8847 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008848 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 delete_tooltip(cur_beval);
8850 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008851 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852
8853 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008854 break;
8855 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008856 {
8857 /* if you get there then we have new common controls */
8858 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8859 info->lpszText = (LPSTR)info->lParam;
8860 info->uFlags |= TTF_DI_SETITEM;
8861 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008862 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008863 case TTN_GETDISPINFOW:
8864 {
8865 // if we get here then we have new common controls
8866 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8867 info->lpszText = (LPWSTR)info->lParam;
8868 info->uFlags |= TTF_DI_SETITEM;
8869 }
8870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 }
8873}
8874
8875 static void
8876TrackUserActivity(UINT uMsg)
8877{
8878 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8879 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8880 LastActivity = GetTickCount();
8881}
8882
8883 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008884gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008886#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008887 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008888#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008889 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 vim_free(beval);
8891}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008892#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8895/*
8896 * We have multiple signs to draw at the same location. Draw the
8897 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8898 */
8899 void
8900netbeans_draw_multisign_indicator(int row)
8901{
8902 int i;
8903 int y;
8904 int x;
8905
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008906 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008907 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 x = 0;
8910 y = TEXT_Y(row);
8911
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008912#if defined(FEAT_DIRECTX)
8913 if (IS_ENABLE_DIRECTX())
8914 DWriteContext_Flush(s_dwc);
8915#endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 for (i = 0; i < gui.char_height - 3; i++)
8918 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8919
8920 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8921 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8922 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8923 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8924 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8925 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8926 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8927}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008928#endif