blob: 4637aa2de5a8e8139c42ea34966f7958ec1e914f [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 Moolenaar7f88b652017-12-14 13:15:19 +010033# ifndef FEAT_MBYTE
34# error FEAT_MBYTE is required for FEAT_DIRECTX.
35# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020036static DWriteContext *s_dwc = NULL;
37static int s_directx_enabled = 0;
38static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010039# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010040static int directx_enabled(void);
41static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020043
Bram Moolenaar065bbac2016-02-20 13:08:46 +010044#ifdef FEAT_MENU
45static int gui_mswin_get_menu_height(int fix_window);
46#endif
47
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020048#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
49 int
50gui_mch_set_rendering_options(char_u *s)
51{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010052# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053 char_u *p, *q;
54
55 int dx_enable = 0;
56 int dx_flags = 0;
57 float dx_gamma = 0.0f;
58 float dx_contrast = 0.0f;
59 float dx_level = 0.0f;
60 int dx_geom = 0;
61 int dx_renmode = 0;
62 int dx_taamode = 0;
63
64 /* parse string as rendering options. */
65 for (p = s; p != NULL && *p != NUL; )
66 {
67 char_u item[256];
68 char_u name[128];
69 char_u value[128];
70
Bram Moolenaarcde88542015-08-11 19:14:00 +020071 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020072 if (p == NULL)
73 break;
74 q = &item[0];
75 copy_option_part(&q, name, sizeof(name), ":");
76 if (q == NULL)
77 return FAIL;
78 copy_option_part(&q, value, sizeof(value), ":");
79
80 if (STRCMP(name, "type") == 0)
81 {
82 if (STRCMP(value, "directx") == 0)
83 dx_enable = 1;
84 else
85 return FAIL;
86 }
87 else if (STRCMP(name, "gamma") == 0)
88 {
89 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010090 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020091 }
92 else if (STRCMP(name, "contrast") == 0)
93 {
94 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "level") == 0)
98 {
99 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "geom") == 0)
103 {
104 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 if (dx_geom < 0 || dx_geom > 2)
107 return FAIL;
108 }
109 else if (STRCMP(name, "renmode") == 0)
110 {
111 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100112 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200113 if (dx_renmode < 0 || dx_renmode > 6)
114 return FAIL;
115 }
116 else if (STRCMP(name, "taamode") == 0)
117 {
118 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100119 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200120 if (dx_taamode < 0 || dx_taamode > 3)
121 return FAIL;
122 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 else if (STRCMP(name, "scrlines") == 0)
124 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100125 /* Deprecated. Simply ignore it. */
Bram Moolenaar92467d32017-12-05 13:22:16 +0100126 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200127 else
128 return FAIL;
129 }
130
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100131 if (!gui.in_use)
132 return OK; /* only checking the syntax of the value */
133
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 /* Enable DirectX/DirectWrite */
135 if (dx_enable)
136 {
137 if (!directx_enabled())
138 return FAIL;
139 DWriteContext_SetRenderingParams(s_dwc, NULL);
140 if (dx_flags)
141 {
142 DWriteRenderingParams param;
143 DWriteContext_GetRenderingParams(s_dwc, &param);
144 if (dx_flags & (1 << 0))
145 param.gamma = dx_gamma;
146 if (dx_flags & (1 << 1))
147 param.enhancedContrast = dx_contrast;
148 if (dx_flags & (1 << 2))
149 param.clearTypeLevel = dx_level;
150 if (dx_flags & (1 << 3))
151 param.pixelGeometry = dx_geom;
152 if (dx_flags & (1 << 4))
153 param.renderingMode = dx_renmode;
154 if (dx_flags & (1 << 5))
155 param.textAntialiasMode = dx_taamode;
156 DWriteContext_SetRenderingParams(s_dwc, &param);
157 }
158 }
159 s_directx_enabled = dx_enable;
160
161 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100162# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200163 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100164# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200165}
166#endif
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * These are new in Windows ME/XP, only defined in recent compilers.
170 */
171#ifndef HANDLE_WM_XBUTTONUP
172# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
173 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
174#endif
175#ifndef HANDLE_WM_XBUTTONDOWN
176# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDBLCLK
180# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100184
185#include "version.h" /* used by dialog box routine for default title */
186#ifdef DEBUG
187# include <tchar.h>
188#endif
189
190/* cproto fails on missing include files */
191#ifndef PROTO
192
193#ifndef __MINGW32__
194# include <shellapi.h>
195#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100196#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197# include <commctrl.h>
198#endif
199#include <windowsx.h>
200
201#ifdef GLOBAL_IME
202# include "glbl_ime.h"
203#endif
204
205#endif /* PROTO */
206
207#ifdef FEAT_MENU
208# define MENUHINTS /* show menu hints in command line */
209#endif
210
211/* Some parameters for dialog boxes. All in pixels. */
212#define DLG_PADDING_X 10
213#define DLG_PADDING_Y 10
214#define DLG_OLD_STYLE_PADDING_X 5
215#define DLG_OLD_STYLE_PADDING_Y 5
216#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
217#define DLG_VERT_PADDING_Y 4
218#define DLG_ICON_WIDTH 34
219#define DLG_ICON_HEIGHT 34
220#define DLG_MIN_WIDTH 150
221#define DLG_FONT_NAME "MS Sans Serif"
222#define DLG_FONT_POINT_SIZE 8
223#define DLG_MIN_MAX_WIDTH 400
224#define DLG_MIN_MAX_HEIGHT 400
225
226#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
227
228#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
229# define WM_XBUTTONDOWN 0x020B
230# define WM_XBUTTONUP 0x020C
231# define WM_XBUTTONDBLCLK 0x020D
232# define MK_XBUTTON1 0x0020
233# define MK_XBUTTON2 0x0040
234#endif
235
236#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238 * Define a few things for generating prototypes. This is just to avoid
239 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241# define APIENTRY
242# define CALLBACK
243# define CONST
244# define FAR
245# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
259typedef int LOGFONT[];
260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200289typedef int COLORREF;
290typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
297static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
301static WORD s_dlgfntheight; /* height of the dialog font */
302static WORD s_dlgfntwidth; /* width of the dialog font */
303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
309static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
310#endif
311
312/* Flag that is set while processing a message that must not be interrupted by
313 * processing another message. */
314static int s_busy_processing = FALSE;
315
316static int destroying = FALSE; /* call DestroyWindow() ourselves */
317
318#ifdef MSWIN_FIND_REPLACE
319static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
320static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200321# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322static FINDREPLACEW s_findrep_struct_w;
323# endif
324static HWND s_findrep_hwnd = NULL;
325static int s_findrep_is_find; /* TRUE for find dialog, FALSE
326 for find/replace dialog */
327#endif
328
329static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100330#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100331static
332#endif
333HWND s_hwnd = NULL;
334static HDC s_hdc = NULL;
335static HBRUSH s_brush = NULL;
336
337#ifdef FEAT_TOOLBAR
338static HWND s_toolbarhwnd = NULL;
339static WNDPROC s_toolbar_wndproc = NULL;
340#endif
341
342#ifdef FEAT_GUI_TABLINE
343static HWND s_tabhwnd = NULL;
344static WNDPROC s_tabline_wndproc = NULL;
345static int showing_tabline = 0;
346#endif
347
348static WPARAM s_wParam = 0;
349static LPARAM s_lParam = 0;
350
351static HWND s_textArea = NULL;
352static UINT s_uMsg = 0;
353
354static char_u *s_textfield; /* Used by dialogs to pass back strings */
355
356static int s_need_activate = FALSE;
357
358/* This variable is set when waiting for an event, which is the only moment
359 * scrollbar dragging can be done directly. It's not allowed while commands
360 * are executed, because it may move the cursor and that may cause unexpected
361 * problems (e.g., while ":s" is working).
362 */
363static int allow_scrollbar = FALSE;
364
365#ifdef GLOBAL_IME
366# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
367#else
368# define MyTranslateMessage(x) TranslateMessage(x)
369#endif
370
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100371#if defined(FEAT_DIRECTX)
372 static int
373directx_enabled(void)
374{
375 if (s_dwc != NULL)
376 return 1;
377 else if (s_directx_load_attempted)
378 return 0;
379 /* load DirectX */
380 DWrite_Init();
381 s_directx_load_attempted = 1;
382 s_dwc = DWriteContext_Open();
383 directx_binddc();
384 return s_dwc != NULL ? 1 : 0;
385}
386
387 static void
388directx_binddc(void)
389{
390 if (s_textArea != NULL)
391 {
392 RECT rect;
393 GetClientRect(s_textArea, &rect);
394 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
395 }
396}
397#endif
398
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200399#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100400 /* use of WindowProc depends on wide_WindowProc */
401# define MyWindowProc vim_WindowProc
402#else
403 /* use ordinary WindowProc */
404# define MyWindowProc DefWindowProc
405#endif
406
407extern int current_font_height; /* this is in os_mswin.c */
408
409static struct
410{
411 UINT key_sym;
412 char_u vim_code0;
413 char_u vim_code1;
414} special_keys[] =
415{
416 {VK_UP, 'k', 'u'},
417 {VK_DOWN, 'k', 'd'},
418 {VK_LEFT, 'k', 'l'},
419 {VK_RIGHT, 'k', 'r'},
420
421 {VK_F1, 'k', '1'},
422 {VK_F2, 'k', '2'},
423 {VK_F3, 'k', '3'},
424 {VK_F4, 'k', '4'},
425 {VK_F5, 'k', '5'},
426 {VK_F6, 'k', '6'},
427 {VK_F7, 'k', '7'},
428 {VK_F8, 'k', '8'},
429 {VK_F9, 'k', '9'},
430 {VK_F10, 'k', ';'},
431
432 {VK_F11, 'F', '1'},
433 {VK_F12, 'F', '2'},
434 {VK_F13, 'F', '3'},
435 {VK_F14, 'F', '4'},
436 {VK_F15, 'F', '5'},
437 {VK_F16, 'F', '6'},
438 {VK_F17, 'F', '7'},
439 {VK_F18, 'F', '8'},
440 {VK_F19, 'F', '9'},
441 {VK_F20, 'F', 'A'},
442
443 {VK_F21, 'F', 'B'},
444#ifdef FEAT_NETBEANS_INTG
445 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
446#endif
447 {VK_F22, 'F', 'C'},
448 {VK_F23, 'F', 'D'},
449 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
450
451 {VK_HELP, '%', '1'},
452 {VK_BACK, 'k', 'b'},
453 {VK_INSERT, 'k', 'I'},
454 {VK_DELETE, 'k', 'D'},
455 {VK_HOME, 'k', 'h'},
456 {VK_END, '@', '7'},
457 {VK_PRIOR, 'k', 'P'},
458 {VK_NEXT, 'k', 'N'},
459 {VK_PRINT, '%', '9'},
460 {VK_ADD, 'K', '6'},
461 {VK_SUBTRACT, 'K', '7'},
462 {VK_DIVIDE, 'K', '8'},
463 {VK_MULTIPLY, 'K', '9'},
464 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
465 {VK_DECIMAL, 'K', 'B'},
466
467 {VK_NUMPAD0, 'K', 'C'},
468 {VK_NUMPAD1, 'K', 'D'},
469 {VK_NUMPAD2, 'K', 'E'},
470 {VK_NUMPAD3, 'K', 'F'},
471 {VK_NUMPAD4, 'K', 'G'},
472 {VK_NUMPAD5, 'K', 'H'},
473 {VK_NUMPAD6, 'K', 'I'},
474 {VK_NUMPAD7, 'K', 'J'},
475 {VK_NUMPAD8, 'K', 'K'},
476 {VK_NUMPAD9, 'K', 'L'},
477
478 /* Keys that we want to be able to use any modifier with: */
479 {VK_SPACE, ' ', NUL},
480 {VK_TAB, TAB, NUL},
481 {VK_ESCAPE, ESC, NUL},
482 {NL, NL, NUL},
483 {CAR, CAR, NUL},
484
485 /* End of list marker: */
486 {0, 0, 0}
487};
488
489/* Local variables */
490static int s_button_pending = -1;
491
492/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
493 * so don't reset s_button_pending. */
494static int s_getting_focus = FALSE;
495
496static int s_x_pending;
497static int s_y_pending;
498static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200499static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200501static int dead_key = 0; // 0: no dead key, 1: dead key pressed
502static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
503 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100505#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100506/* balloon-eval WM_NOTIFY_HANDLER */
507static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
508static void TrackUserActivity(UINT uMsg);
509#endif
510
511/*
512 * For control IME.
513 *
514 * These LOGFONT used for IME.
515 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100516#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100517/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
518static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100519#endif
520#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521/* holds LOGFONT for 'guifont' always. */
522static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523#endif
524
525#ifdef FEAT_MBYTE_IME
526static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
527#endif
528
529#if defined(FEAT_BROWSE)
530static char_u *convert_filter(char_u *s);
531#endif
532
533#ifdef DEBUG_PRINT_ERROR
534/*
535 * Print out the last Windows error message
536 */
537 static void
538print_windows_error(void)
539{
540 LPVOID lpMsgBuf;
541
542 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
543 NULL, GetLastError(),
544 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
545 (LPTSTR) &lpMsgBuf, 0, NULL);
546 TRACE1("Error: %s\n", lpMsgBuf);
547 LocalFree(lpMsgBuf);
548}
549#endif
550
551/*
552 * Cursor blink functions.
553 *
554 * This is a simple state machine:
555 * BLINK_NONE not blinking at all
556 * BLINK_OFF blinking, cursor is not shown
557 * BLINK_ON blinking, cursor is shown
558 */
559
560#define BLINK_NONE 0
561#define BLINK_OFF 1
562#define BLINK_ON 2
563
564static int blink_state = BLINK_NONE;
565static long_u blink_waittime = 700;
566static long_u blink_ontime = 400;
567static long_u blink_offtime = 250;
568static UINT blink_timer = 0;
569
Bram Moolenaar703a8042016-06-04 16:24:32 +0200570 int
571gui_mch_is_blinking(void)
572{
573 return blink_state != BLINK_NONE;
574}
575
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200576 int
577gui_mch_is_blink_off(void)
578{
579 return blink_state == BLINK_OFF;
580}
581
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 void
583gui_mch_set_blinking(long wait, long on, long off)
584{
585 blink_waittime = wait;
586 blink_ontime = on;
587 blink_offtime = off;
588}
589
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100590 static VOID CALLBACK
591_OnBlinkTimer(
592 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100593 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100594 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100595 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100596{
597 MSG msg;
598
599 /*
600 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
601 */
602
603 KillTimer(NULL, idEvent);
604
605 /* Eat spurious WM_TIMER messages */
606 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
607 ;
608
609 if (blink_state == BLINK_ON)
610 {
611 gui_undraw_cursor();
612 blink_state = BLINK_OFF;
613 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
614 (TIMERPROC)_OnBlinkTimer);
615 }
616 else
617 {
618 gui_update_cursor(TRUE, FALSE);
619 blink_state = BLINK_ON;
620 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100621 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100622 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100623 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100624}
625
626 static void
627gui_mswin_rm_blink_timer(void)
628{
629 MSG msg;
630
631 if (blink_timer != 0)
632 {
633 KillTimer(NULL, blink_timer);
634 /* Eat spurious WM_TIMER messages */
635 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
636 ;
637 blink_timer = 0;
638 }
639}
640
641/*
642 * Stop the cursor blinking. Show the cursor if it wasn't shown.
643 */
644 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100645gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646{
647 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100648 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100649 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100650 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100651 gui_mch_flush();
652 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 blink_state = BLINK_NONE;
654}
655
656/*
657 * Start the cursor blinking. If it was already blinking, this restarts the
658 * waiting time and shows the cursor.
659 */
660 void
661gui_mch_start_blink(void)
662{
663 gui_mswin_rm_blink_timer();
664
665 /* Only switch blinking on if none of the times is zero */
666 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
667 {
668 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
669 (TIMERPROC)_OnBlinkTimer);
670 blink_state = BLINK_ON;
671 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100672 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673 }
674}
675
676/*
677 * Call-back routines.
678 */
679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100680 static VOID CALLBACK
681_OnTimer(
682 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100683 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100684 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100685 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686{
687 MSG msg;
688
689 /*
690 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
691 */
692 KillTimer(NULL, idEvent);
693 s_timed_out = TRUE;
694
695 /* Eat spurious WM_TIMER messages */
696 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
697 ;
698 if (idEvent == s_wait_timer)
699 s_wait_timer = 0;
700}
701
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702 static void
703_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100704 HWND hwnd UNUSED,
705 UINT ch UNUSED,
706 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100707{
708 dead_key = 1;
709}
710
711/*
712 * Convert Unicode character "ch" to bytes in "string[slen]".
713 * When "had_alt" is TRUE the ALT key was included in "ch".
714 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 * Because the Windows API uses UTF-16, we have to deal with surrogate
716 * pairs; this is where we choose to deal with them: if "ch" is a high
717 * surrogate, it will be stored, and the length returned will be zero; the next
718 * char_to_string call will then include the high surrogate, decoding the pair
719 * of UTF-16 code units to a single Unicode code point, presuming it is the
720 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 */
722 static int
723char_to_string(int ch, char_u *string, int slen, int had_alt)
724{
725 int len;
726 int i;
727#ifdef FEAT_MBYTE
728 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200729 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200731 if (surrogate_pending_ch != 0)
732 {
733 /* We don't guarantee ch is a low surrogate to match the high surrogate
734 * we already have; it should be, but if it isn't, tough luck. */
735 wstring[0] = surrogate_pending_ch;
736 wstring[1] = ch;
737 surrogate_pending_ch = 0;
738 len = 2;
739 }
740 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
741 {
742 /* We don't have the entire code point yet, only the first UTF-16 code
743 * unit; so just remember it and use it in the next call. */
744 surrogate_pending_ch = ch;
745 return 0;
746 }
747 else
748 {
749 wstring[0] = ch;
750 len = 1;
751 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200752
753 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
754 * "enc_codepage" is non-zero use the standard Win32 function,
755 * otherwise use our own conversion function (e.g., for UTF-8). */
756 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200758 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
759 (LPSTR)string, slen, 0, NULL);
760 /* If we had included the ALT key into the character but now the
761 * upper bit is no longer set, that probably means the conversion
762 * failed. Convert the original character and set the upper bit
763 * afterwards. */
764 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200766 wstring[0] = ch & 0x7f;
767 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
768 (LPSTR)string, slen, 0, NULL);
769 if (len == 1) /* safety check */
770 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773 else
774 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200775 ws = utf16_to_enc(wstring, &len);
776 if (ws == NULL)
777 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100778 else
779 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200780 if (len > slen) /* just in case */
781 len = slen;
782 mch_memmove(string, ws, len);
783 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100784 }
785 }
786
787 if (len == 0)
788#endif
789 {
790 string[0] = ch;
791 len = 1;
792 }
793
794 for (i = 0; i < len; ++i)
795 if (string[i] == CSI && len <= slen - 2)
796 {
797 /* Insert CSI as K_CSI. */
798 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
799 string[++i] = KS_EXTRA;
800 string[++i] = (int)KE_CSI;
801 len += 2;
802 }
803
804 return len;
805}
806
807/*
808 * Key hit, add it to the input buffer.
809 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100810 static void
811_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100812 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100813 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100814 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100815{
816 char_u string[40];
817 int len = 0;
818
819 dead_key = 0;
820
821 len = char_to_string(ch, string, 40, FALSE);
822 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
823 {
824 trash_input_buf();
825 got_int = TRUE;
826 }
827
828 add_to_input_buf(string, len);
829}
830
831/*
832 * Alt-Key hit, add it to the input buffer.
833 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100834 static void
835_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100836 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100837 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100838 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100839{
840 char_u string[40]; /* Enough for multibyte character */
841 int len;
842 int modifiers;
843 int ch = cch; /* special keys are negative */
844
845 dead_key = 0;
846
847 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
848
849 /* OK, we have a character key (given by ch) which was entered with the
850 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
851 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
852 * CAPSLOCK is pressed) at this point.
853 */
854 modifiers = MOD_MASK_ALT;
855 if (GetKeyState(VK_SHIFT) & 0x8000)
856 modifiers |= MOD_MASK_SHIFT;
857 if (GetKeyState(VK_CONTROL) & 0x8000)
858 modifiers |= MOD_MASK_CTRL;
859
860 ch = simplify_key(ch, &modifiers);
861 /* remove the SHIFT modifier for keys where it's already included, e.g.,
862 * '(' and '*' */
863 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
864 modifiers &= ~MOD_MASK_SHIFT;
865
866 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
867 ch = extract_modifiers(ch, &modifiers);
868 if (ch == CSI)
869 ch = K_CSI;
870
871 len = 0;
872 if (modifiers)
873 {
874 string[len++] = CSI;
875 string[len++] = KS_MODIFIER;
876 string[len++] = modifiers;
877 }
878
879 if (IS_SPECIAL((int)ch))
880 {
881 string[len++] = CSI;
882 string[len++] = K_SECOND((int)ch);
883 string[len++] = K_THIRD((int)ch);
884 }
885 else
886 {
887 /* Although the documentation isn't clear about it, we assume "ch" is
888 * a Unicode character. */
889 len += char_to_string(ch, string + len, 40 - len, TRUE);
890 }
891
892 add_to_input_buf(string, len);
893}
894
895 static void
896_OnMouseEvent(
897 int button,
898 int x,
899 int y,
900 int repeated_click,
901 UINT keyFlags)
902{
903 int vim_modifiers = 0x0;
904
905 s_getting_focus = FALSE;
906
907 if (keyFlags & MK_SHIFT)
908 vim_modifiers |= MOUSE_SHIFT;
909 if (keyFlags & MK_CONTROL)
910 vim_modifiers |= MOUSE_CTRL;
911 if (GetKeyState(VK_MENU) & 0x8000)
912 vim_modifiers |= MOUSE_ALT;
913
914 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
915}
916
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100917 static void
918_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100919 HWND hwnd UNUSED,
920 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921 int x,
922 int y,
923 UINT keyFlags)
924{
925 static LONG s_prevTime = 0;
926
927 LONG currentTime = GetMessageTime();
928 int button = -1;
929 int repeated_click;
930
931 /* Give main window the focus: this is so the cursor isn't hollow. */
932 (void)SetFocus(s_hwnd);
933
934 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
935 button = MOUSE_LEFT;
936 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
937 button = MOUSE_MIDDLE;
938 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
939 button = MOUSE_RIGHT;
940 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
941 {
942#ifndef GET_XBUTTON_WPARAM
943# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
944#endif
945 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
946 }
947 else if (s_uMsg == WM_CAPTURECHANGED)
948 {
949 /* on W95/NT4, somehow you get in here with an odd Msg
950 * if you press one button while holding down the other..*/
951 if (s_button_pending == MOUSE_LEFT)
952 button = MOUSE_RIGHT;
953 else
954 button = MOUSE_LEFT;
955 }
956 if (button >= 0)
957 {
958 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
959
960 /*
961 * Holding down the left and right buttons simulates pushing the middle
962 * button.
963 */
964 if (repeated_click
965 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
966 || (button == MOUSE_RIGHT
967 && s_button_pending == MOUSE_LEFT)))
968 {
969 /*
970 * Hmm, gui.c will ignore more than one button down at a time, so
971 * pretend we let go of it first.
972 */
973 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
974 button = MOUSE_MIDDLE;
975 repeated_click = FALSE;
976 s_button_pending = -1;
977 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
978 }
979 else if ((repeated_click)
980 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
981 {
982 if (s_button_pending > -1)
983 {
984 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
985 s_button_pending = -1;
986 }
987 /* TRACE("Button down at x %d, y %d\n", x, y); */
988 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
989 }
990 else
991 {
992 /*
993 * If this is the first press (i.e. not a multiple click) don't
994 * action immediately, but store and wait for:
995 * i) button-up
996 * ii) mouse move
997 * iii) another button press
998 * before using it.
999 * This enables us to make left+right simulate middle button,
1000 * without left or right being actioned first. The side-effect is
1001 * that if you click and hold the mouse without dragging, the
1002 * cursor doesn't move until you release the button. In practice
1003 * this is hardly a problem.
1004 */
1005 s_button_pending = button;
1006 s_x_pending = x;
1007 s_y_pending = y;
1008 s_kFlags_pending = keyFlags;
1009 }
1010
1011 s_prevTime = currentTime;
1012 }
1013}
1014
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001015 static void
1016_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001017 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001018 int x,
1019 int y,
1020 UINT keyFlags)
1021{
1022 int button;
1023
1024 s_getting_focus = FALSE;
1025 if (s_button_pending > -1)
1026 {
1027 /* Delayed action for mouse down event */
1028 _OnMouseEvent(s_button_pending, s_x_pending,
1029 s_y_pending, FALSE, s_kFlags_pending);
1030 s_button_pending = -1;
1031 }
1032 if (s_uMsg == WM_MOUSEMOVE)
1033 {
1034 /*
1035 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1036 * down.
1037 */
1038 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1039 | MK_XBUTTON1 | MK_XBUTTON2)))
1040 {
1041 gui_mouse_moved(x, y);
1042 return;
1043 }
1044
1045 /*
1046 * While button is down, keep grabbing mouse move events when
1047 * the mouse goes outside the window
1048 */
1049 SetCapture(s_textArea);
1050 button = MOUSE_DRAG;
1051 /* TRACE(" move at x %d, y %d\n", x, y); */
1052 }
1053 else
1054 {
1055 ReleaseCapture();
1056 button = MOUSE_RELEASE;
1057 /* TRACE(" up at x %d, y %d\n", x, y); */
1058 }
1059
1060 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1061}
1062
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001063 static void
1064_OnSizeTextArea(
1065 HWND hwnd UNUSED,
1066 UINT state UNUSED,
1067 int cx UNUSED,
1068 int cy UNUSED)
1069{
1070#if defined(FEAT_DIRECTX)
1071 if (IS_ENABLE_DIRECTX())
1072 directx_binddc();
1073#endif
1074}
1075
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001076#ifdef FEAT_MENU
1077/*
1078 * Find the vimmenu_T with the given id
1079 */
1080 static vimmenu_T *
1081gui_mswin_find_menu(
1082 vimmenu_T *pMenu,
1083 int id)
1084{
1085 vimmenu_T *pChildMenu;
1086
1087 while (pMenu)
1088 {
1089 if (pMenu->id == (UINT)id)
1090 break;
1091 if (pMenu->children != NULL)
1092 {
1093 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1094 if (pChildMenu)
1095 {
1096 pMenu = pChildMenu;
1097 break;
1098 }
1099 }
1100 pMenu = pMenu->next;
1101 }
1102 return pMenu;
1103}
1104
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105 static void
1106_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001107 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001109 HWND hwndCtl UNUSED,
1110 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111{
1112 vimmenu_T *pMenu;
1113
1114 pMenu = gui_mswin_find_menu(root_menu, id);
1115 if (pMenu)
1116 gui_menu_cb(pMenu);
1117}
1118#endif
1119
1120#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001121# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001122/*
1123 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1124 */
1125 static void
1126findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1127{
1128 WCHAR *wp;
1129
1130 lpfrw->hwndOwner = lpfr->hwndOwner;
1131 lpfrw->Flags = lpfr->Flags;
1132
1133 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1134 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1135 vim_free(wp);
1136
1137 /* the field "lpstrReplaceWith" doesn't need to be copied */
1138}
1139
1140/*
1141 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1142 */
1143 static void
1144findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1145{
1146 char_u *p;
1147
1148 lpfr->Flags = lpfrw->Flags;
1149
1150 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1151 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1152 vim_free(p);
1153
1154 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1155 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1156 vim_free(p);
1157}
1158# endif
1159
1160/*
1161 * Handle a Find/Replace window message.
1162 */
1163 static void
1164_OnFindRepl(void)
1165{
1166 int flags = 0;
1167 int down;
1168
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001169# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001170 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1171 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001172 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 {
1174 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1175 }
1176# endif
1177
1178 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1179 /* Give main window the focus back. */
1180 (void)SetFocus(s_hwnd);
1181
1182 if (s_findrep_struct.Flags & FR_FINDNEXT)
1183 {
1184 flags = FRD_FINDNEXT;
1185
1186 /* Give main window the focus back: this is so the cursor isn't
1187 * hollow. */
1188 (void)SetFocus(s_hwnd);
1189 }
1190 else if (s_findrep_struct.Flags & FR_REPLACE)
1191 {
1192 flags = FRD_REPLACE;
1193
1194 /* Give main window the focus back: this is so the cursor isn't
1195 * hollow. */
1196 (void)SetFocus(s_hwnd);
1197 }
1198 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1199 {
1200 flags = FRD_REPLACEALL;
1201 }
1202
1203 if (flags != 0)
1204 {
1205 /* Call the generic GUI function to do the actual work. */
1206 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1207 flags |= FRD_WHOLE_WORD;
1208 if (s_findrep_struct.Flags & FR_MATCHCASE)
1209 flags |= FRD_MATCH_CASE;
1210 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1211 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1212 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1213 }
1214}
1215#endif
1216
1217 static void
1218HandleMouseHide(UINT uMsg, LPARAM lParam)
1219{
1220 static LPARAM last_lParam = 0L;
1221
1222 /* We sometimes get a mousemove when the mouse didn't move... */
1223 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1224 {
1225 if (lParam == last_lParam)
1226 return;
1227 last_lParam = lParam;
1228 }
1229
1230 /* Handle specially, to centralise coding. We need to be sure we catch all
1231 * possible events which should cause us to restore the cursor (as it is a
1232 * shared resource, we take full responsibility for it).
1233 */
1234 switch (uMsg)
1235 {
1236 case WM_KEYUP:
1237 case WM_CHAR:
1238 /*
1239 * blank out the pointer if necessary
1240 */
1241 if (p_mh)
1242 gui_mch_mousehide(TRUE);
1243 break;
1244
1245 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1246 case WM_SYSCHAR:
1247 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1248 case WM_LBUTTONDOWN:
1249 case WM_LBUTTONUP:
1250 case WM_MBUTTONDOWN:
1251 case WM_MBUTTONUP:
1252 case WM_RBUTTONDOWN:
1253 case WM_RBUTTONUP:
1254 case WM_XBUTTONDOWN:
1255 case WM_XBUTTONUP:
1256 case WM_NCMOUSEMOVE:
1257 case WM_NCLBUTTONDOWN:
1258 case WM_NCLBUTTONUP:
1259 case WM_NCMBUTTONDOWN:
1260 case WM_NCMBUTTONUP:
1261 case WM_NCRBUTTONDOWN:
1262 case WM_NCRBUTTONUP:
1263 case WM_KILLFOCUS:
1264 /*
1265 * if the pointer is currently hidden, then we should show it.
1266 */
1267 gui_mch_mousehide(FALSE);
1268 break;
1269 }
1270}
1271
1272 static LRESULT CALLBACK
1273_TextAreaWndProc(
1274 HWND hwnd,
1275 UINT uMsg,
1276 WPARAM wParam,
1277 LPARAM lParam)
1278{
1279 /*
1280 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1281 hwnd, uMsg, wParam, lParam);
1282 */
1283
1284 HandleMouseHide(uMsg, lParam);
1285
1286 s_uMsg = uMsg;
1287 s_wParam = wParam;
1288 s_lParam = lParam;
1289
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001290#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001291 TrackUserActivity(uMsg);
1292#endif
1293
1294 switch (uMsg)
1295 {
1296 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1297 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1299 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1300 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1301 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1302 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1303 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1304 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1305 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1307 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1308 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001310 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001312#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001313 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1314 return TRUE;
1315#endif
1316 default:
1317 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1318 }
1319}
1320
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001321#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322 || defined(GLOBAL_IME) \
1323 || defined(PROTO)
1324# ifdef PROTO
1325typedef int WINAPI;
1326# endif
1327
1328 LRESULT WINAPI
1329vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1330{
1331# ifdef GLOBAL_IME
1332 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1333# else
1334 if (wide_WindowProc)
1335 return DefWindowProcW(hwnd, message, wParam, lParam);
1336 return DefWindowProc(hwnd, message, wParam, lParam);
1337#endif
1338}
1339#endif
1340
1341/*
1342 * Called when the foreground or background color has been changed.
1343 */
1344 void
1345gui_mch_new_colors(void)
1346{
1347 /* nothing to do? */
1348}
1349
1350/*
1351 * Set the colors to their default values.
1352 */
1353 void
1354gui_mch_def_colors(void)
1355{
1356 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1357 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1358 gui.def_norm_pixel = gui.norm_pixel;
1359 gui.def_back_pixel = gui.back_pixel;
1360}
1361
1362/*
1363 * Open the GUI window which was created by a call to gui_mch_init().
1364 */
1365 int
1366gui_mch_open(void)
1367{
1368#ifndef SW_SHOWDEFAULT
1369# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1370#endif
1371 /* Actually open the window, if not already visible
1372 * (may be done already in gui_mch_set_shellsize) */
1373 if (!IsWindowVisible(s_hwnd))
1374 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1375
1376#ifdef MSWIN_FIND_REPLACE
1377 /* Init replace string here, so that we keep it when re-opening the
1378 * dialog. */
1379 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1380#endif
1381
1382 return OK;
1383}
1384
1385/*
1386 * Get the position of the top left corner of the window.
1387 */
1388 int
1389gui_mch_get_winpos(int *x, int *y)
1390{
1391 RECT rect;
1392
1393 GetWindowRect(s_hwnd, &rect);
1394 *x = rect.left;
1395 *y = rect.top;
1396 return OK;
1397}
1398
1399/*
1400 * Set the position of the top left corner of the window to the given
1401 * coordinates.
1402 */
1403 void
1404gui_mch_set_winpos(int x, int y)
1405{
1406 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1407 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1408}
1409 void
1410gui_mch_set_text_area_pos(int x, int y, int w, int h)
1411{
1412 static int oldx = 0;
1413 static int oldy = 0;
1414
1415 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1416
1417#ifdef FEAT_TOOLBAR
1418 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1419 SendMessage(s_toolbarhwnd, WM_SIZE,
1420 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1421#endif
1422#if defined(FEAT_GUI_TABLINE)
1423 if (showing_tabline)
1424 {
1425 int top = 0;
1426 RECT rect;
1427
1428# ifdef FEAT_TOOLBAR
1429 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1430 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1431# endif
1432 GetClientRect(s_hwnd, &rect);
1433 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1434 }
1435#endif
1436
1437 /* When side scroll bar is unshown, the size of window will change.
1438 * then, the text area move left or right. thus client rect should be
1439 * forcedly redrawn. (Yasuhiro Matsumoto) */
1440 if (oldx != x || oldy != y)
1441 {
1442 InvalidateRect(s_hwnd, NULL, FALSE);
1443 oldx = x;
1444 oldy = y;
1445 }
1446}
1447
1448
1449/*
1450 * Scrollbar stuff:
1451 */
1452
1453 void
1454gui_mch_enable_scrollbar(
1455 scrollbar_T *sb,
1456 int flag)
1457{
1458 ShowScrollBar(sb->id, SB_CTL, flag);
1459
1460 /* TODO: When the window is maximized, the size of the window stays the
1461 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1462 * NT 4.0 it's not... */
1463}
1464
1465 void
1466gui_mch_set_scrollbar_pos(
1467 scrollbar_T *sb,
1468 int x,
1469 int y,
1470 int w,
1471 int h)
1472{
1473 SetWindowPos(sb->id, NULL, x, y, w, h,
1474 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1475}
1476
1477 void
1478gui_mch_create_scrollbar(
1479 scrollbar_T *sb,
1480 int orient) /* SBAR_VERT or SBAR_HORIZ */
1481{
1482 sb->id = CreateWindow(
1483 "SCROLLBAR", "Scrollbar",
1484 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1485 10, /* Any value will do for now */
1486 10, /* Any value will do for now */
1487 s_hwnd, NULL,
1488 s_hinst, NULL);
1489}
1490
1491/*
1492 * Find the scrollbar with the given hwnd.
1493 */
1494 static scrollbar_T *
1495gui_mswin_find_scrollbar(HWND hwnd)
1496{
1497 win_T *wp;
1498
1499 if (gui.bottom_sbar.id == hwnd)
1500 return &gui.bottom_sbar;
1501 FOR_ALL_WINDOWS(wp)
1502 {
1503 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1504 return &wp->w_scrollbars[SBAR_LEFT];
1505 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1506 return &wp->w_scrollbars[SBAR_RIGHT];
1507 }
1508 return NULL;
1509}
1510
1511/*
1512 * Get the character size of a font.
1513 */
1514 static void
1515GetFontSize(GuiFont font)
1516{
1517 HWND hwnd = GetDesktopWindow();
1518 HDC hdc = GetWindowDC(hwnd);
1519 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1520 TEXTMETRIC tm;
1521
1522 GetTextMetrics(hdc, &tm);
1523 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1524
1525 gui.char_height = tm.tmHeight + p_linespace;
1526
1527 SelectFont(hdc, hfntOld);
1528
1529 ReleaseDC(hwnd, hdc);
1530}
1531
1532/*
1533 * Adjust gui.char_height (after 'linespace' was changed).
1534 */
1535 int
1536gui_mch_adjust_charheight(void)
1537{
1538 GetFontSize(gui.norm_font);
1539 return OK;
1540}
1541
1542 static GuiFont
1543get_font_handle(LOGFONT *lf)
1544{
1545 HFONT font = NULL;
1546
1547 /* Load the font */
1548 font = CreateFontIndirect(lf);
1549
1550 if (font == NULL)
1551 return NOFONT;
1552
1553 return (GuiFont)font;
1554}
1555
1556 static int
1557pixels_to_points(int pixels, int vertical)
1558{
1559 int points;
1560 HWND hwnd;
1561 HDC hdc;
1562
1563 hwnd = GetDesktopWindow();
1564 hdc = GetWindowDC(hwnd);
1565
1566 points = MulDiv(pixels, 72,
1567 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1568
1569 ReleaseDC(hwnd, hdc);
1570
1571 return points;
1572}
1573
1574 GuiFont
1575gui_mch_get_font(
1576 char_u *name,
1577 int giveErrorIfMissing)
1578{
1579 LOGFONT lf;
1580 GuiFont font = NOFONT;
1581
1582 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1583 font = get_font_handle(&lf);
1584 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001585 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001586 return font;
1587}
1588
1589#if defined(FEAT_EVAL) || defined(PROTO)
1590/*
1591 * Return the name of font "font" in allocated memory.
1592 * Don't know how to get the actual name, thus use the provided name.
1593 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001594 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001595gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596{
1597 if (name == NULL)
1598 return NULL;
1599 return vim_strsave(name);
1600}
1601#endif
1602
1603 void
1604gui_mch_free_font(GuiFont font)
1605{
1606 if (font)
1607 DeleteObject((HFONT)font);
1608}
1609
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610/*
1611 * Return the Pixel value (color) for the given color name.
1612 * Return INVALCOLOR for error.
1613 */
1614 guicolor_T
1615gui_mch_get_color(char_u *name)
1616{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001617 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001618
1619 typedef struct SysColorTable
1620 {
1621 char *name;
1622 int color;
1623 } SysColorTable;
1624
1625 static SysColorTable sys_table[] =
1626 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1628 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001629#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1631#endif
1632 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1633 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1634 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1635 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1636 {"SYS_DESKTOP", COLOR_DESKTOP},
1637 {"SYS_INFOBK", COLOR_INFOBK},
1638 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1639 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 {"SYS_BTNFACE", COLOR_BTNFACE},
1641 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1642 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1643 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1644 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1645 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1646 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1647 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1648 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1649 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1650 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1651 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1652 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1653 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1654 {"SYS_MENU", COLOR_MENU},
1655 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1656 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1657 {"SYS_WINDOW", COLOR_WINDOW},
1658 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1659 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1660 };
1661
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001662 /*
1663 * Try to look up a system colour.
1664 */
1665 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1666 if (STRICMP(name, sys_table[i].name) == 0)
1667 return GetSysColor(sys_table[i].color);
1668
Bram Moolenaarab302212016-04-26 20:59:29 +02001669 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001670}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001671
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001672 guicolor_T
1673gui_mch_get_rgb_color(int r, int g, int b)
1674{
1675 return gui_get_rgb_color_cmn(r, g, b);
1676}
1677
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678/*
1679 * Return OK if the key with the termcap name "name" is supported.
1680 */
1681 int
1682gui_mch_haskey(char_u *name)
1683{
1684 int i;
1685
1686 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1687 if (name[0] == special_keys[i].vim_code0 &&
1688 name[1] == special_keys[i].vim_code1)
1689 return OK;
1690 return FAIL;
1691}
1692
1693 void
1694gui_mch_beep(void)
1695{
1696 MessageBeep(MB_OK);
1697}
1698/*
1699 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1700 */
1701 void
1702gui_mch_invert_rectangle(
1703 int r,
1704 int c,
1705 int nr,
1706 int nc)
1707{
1708 RECT rc;
1709
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001710#if defined(FEAT_DIRECTX)
1711 if (IS_ENABLE_DIRECTX())
1712 DWriteContext_Flush(s_dwc);
1713#endif
1714
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001715 /*
1716 * Note: InvertRect() excludes right and bottom of rectangle.
1717 */
1718 rc.left = FILL_X(c);
1719 rc.top = FILL_Y(r);
1720 rc.right = rc.left + nc * gui.char_width;
1721 rc.bottom = rc.top + nr * gui.char_height;
1722 InvertRect(s_hdc, &rc);
1723}
1724
1725/*
1726 * Iconify the GUI window.
1727 */
1728 void
1729gui_mch_iconify(void)
1730{
1731 ShowWindow(s_hwnd, SW_MINIMIZE);
1732}
1733
1734/*
1735 * Draw a cursor without focus.
1736 */
1737 void
1738gui_mch_draw_hollow_cursor(guicolor_T color)
1739{
1740 HBRUSH hbr;
1741 RECT rc;
1742
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001743#if defined(FEAT_DIRECTX)
1744 if (IS_ENABLE_DIRECTX())
1745 DWriteContext_Flush(s_dwc);
1746#endif
1747
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001748 /*
1749 * Note: FrameRect() excludes right and bottom of rectangle.
1750 */
1751 rc.left = FILL_X(gui.col);
1752 rc.top = FILL_Y(gui.row);
1753 rc.right = rc.left + gui.char_width;
1754#ifdef FEAT_MBYTE
1755 if (mb_lefthalve(gui.row, gui.col))
1756 rc.right += gui.char_width;
1757#endif
1758 rc.bottom = rc.top + gui.char_height;
1759 hbr = CreateSolidBrush(color);
1760 FrameRect(s_hdc, &rc, hbr);
1761 DeleteBrush(hbr);
1762}
1763/*
1764 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1765 * color "color".
1766 */
1767 void
1768gui_mch_draw_part_cursor(
1769 int w,
1770 int h,
1771 guicolor_T color)
1772{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001773 RECT rc;
1774
1775 /*
1776 * Note: FillRect() excludes right and bottom of rectangle.
1777 */
1778 rc.left =
1779#ifdef FEAT_RIGHTLEFT
1780 /* vertical line should be on the right of current point */
1781 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1782#endif
1783 FILL_X(gui.col);
1784 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1785 rc.right = rc.left + w;
1786 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001787
Bram Moolenaar92467d32017-12-05 13:22:16 +01001788 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001789}
1790
1791
1792/*
1793 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1794 * dead key's nominal character and re-post the original message.
1795 */
1796 static void
1797outputDeadKey_rePost(MSG originalMsg)
1798{
1799 static MSG deadCharExpel;
1800
1801 if (!dead_key)
1802 return;
1803
1804 dead_key = 0;
1805
1806 /* Make Windows generate the dead key's character */
1807 deadCharExpel.message = originalMsg.message;
1808 deadCharExpel.hwnd = originalMsg.hwnd;
1809 deadCharExpel.wParam = VK_SPACE;
1810
1811 MyTranslateMessage(&deadCharExpel);
1812
1813 /* re-generate the current character free of the dead char influence */
1814 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1815 originalMsg.lParam);
1816}
1817
1818
1819/*
1820 * Process a single Windows message.
1821 * If one is not available we hang until one is.
1822 */
1823 static void
1824process_message(void)
1825{
1826 MSG msg;
1827 UINT vk = 0; /* Virtual key */
1828 char_u string[40];
1829 int i;
1830 int modifiers = 0;
1831 int key;
1832#ifdef FEAT_MENU
1833 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1834#endif
1835
1836 pGetMessage(&msg, NULL, 0, 0);
1837
1838#ifdef FEAT_OLE
1839 /* Look after OLE Automation commands */
1840 if (msg.message == WM_OLE)
1841 {
1842 char_u *str = (char_u *)msg.lParam;
1843 if (str == NULL || *str == NUL)
1844 {
1845 /* Message can't be ours, forward it. Fixes problem with Ultramon
1846 * 3.0.4 */
1847 pDispatchMessage(&msg);
1848 }
1849 else
1850 {
1851 add_to_input_buf(str, (int)STRLEN(str));
1852 vim_free(str); /* was allocated in CVim::SendKeys() */
1853 }
1854 return;
1855 }
1856#endif
1857
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858#ifdef MSWIN_FIND_REPLACE
1859 /* Don't process messages used by the dialog */
1860 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1861 {
1862 HandleMouseHide(msg.message, msg.lParam);
1863 return;
1864 }
1865#endif
1866
1867 /*
1868 * Check if it's a special key that we recognise. If not, call
1869 * TranslateMessage().
1870 */
1871 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1872 {
1873 vk = (int) msg.wParam;
1874
1875 /*
1876 * Handle dead keys in special conditions in other cases we let Windows
1877 * handle them and do not interfere.
1878 *
1879 * The dead_key flag must be reset on several occasions:
1880 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1881 * consumed at that point (This is when we let Windows combine the
1882 * dead character on its own)
1883 *
1884 * - Before doing something special such as regenerating keypresses to
1885 * expel the dead character as this could trigger an infinite loop if
1886 * for some reason MyTranslateMessage() do not trigger a call
1887 * immediately to _OnChar() (or _OnSysChar()).
1888 */
1889 if (dead_key)
1890 {
1891 /*
1892 * If a dead key was pressed and the user presses VK_SPACE,
1893 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1894 * with the dead char now, so do nothing special and let Windows
1895 * handle it.
1896 *
1897 * Note that VK_SPACE combines with the dead_key's character and
1898 * only one WM_CHAR will be generated by TranslateMessage(), in
1899 * the two other cases two WM_CHAR will be generated: the dead
1900 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1901 * user expects.
1902 */
1903 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1904 {
1905 dead_key = 0;
1906 MyTranslateMessage(&msg);
1907 return;
1908 }
1909 /* In modes where we are not typing, dead keys should behave
1910 * normally */
1911 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1912 {
1913 outputDeadKey_rePost(msg);
1914 return;
1915 }
1916 }
1917
1918 /* Check for CTRL-BREAK */
1919 if (vk == VK_CANCEL)
1920 {
1921 trash_input_buf();
1922 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001923 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924 string[0] = Ctrl_C;
1925 add_to_input_buf(string, 1);
1926 }
1927
1928 for (i = 0; special_keys[i].key_sym != 0; i++)
1929 {
1930 /* ignore VK_SPACE when ALT key pressed: system menu */
1931 if (special_keys[i].key_sym == vk
1932 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1933 {
1934 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001935 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001936 * is a key that would normally trigger the dead key nominal
1937 * character output (such as a NUMPAD printable character or
1938 * the TAB key, etc...).
1939 */
1940 if (dead_key && (special_keys[i].vim_code0 == 'K'
1941 || vk == VK_TAB || vk == CAR))
1942 {
1943 outputDeadKey_rePost(msg);
1944 return;
1945 }
1946
1947#ifdef FEAT_MENU
1948 /* Check for <F10>: Windows selects the menu. When <F10> is
1949 * mapped we want to use the mapping instead. */
1950 if (vk == VK_F10
1951 && gui.menu_is_active
1952 && check_map(k10, State, FALSE, TRUE, FALSE,
1953 NULL, NULL) == NULL)
1954 break;
1955#endif
1956 if (GetKeyState(VK_SHIFT) & 0x8000)
1957 modifiers |= MOD_MASK_SHIFT;
1958 /*
1959 * Don't use caps-lock as shift, because these are special keys
1960 * being considered here, and we only want letters to get
1961 * shifted -- webb
1962 */
1963 /*
1964 if (GetKeyState(VK_CAPITAL) & 0x0001)
1965 modifiers ^= MOD_MASK_SHIFT;
1966 */
1967 if (GetKeyState(VK_CONTROL) & 0x8000)
1968 modifiers |= MOD_MASK_CTRL;
1969 if (GetKeyState(VK_MENU) & 0x8000)
1970 modifiers |= MOD_MASK_ALT;
1971
1972 if (special_keys[i].vim_code1 == NUL)
1973 key = special_keys[i].vim_code0;
1974 else
1975 key = TO_SPECIAL(special_keys[i].vim_code0,
1976 special_keys[i].vim_code1);
1977 key = simplify_key(key, &modifiers);
1978 if (key == CSI)
1979 key = K_CSI;
1980
1981 if (modifiers)
1982 {
1983 string[0] = CSI;
1984 string[1] = KS_MODIFIER;
1985 string[2] = modifiers;
1986 add_to_input_buf(string, 3);
1987 }
1988
1989 if (IS_SPECIAL(key))
1990 {
1991 string[0] = CSI;
1992 string[1] = K_SECOND(key);
1993 string[2] = K_THIRD(key);
1994 add_to_input_buf(string, 3);
1995 }
1996 else
1997 {
1998 int len;
1999
2000 /* Handle "key" as a Unicode character. */
2001 len = char_to_string(key, string, 40, FALSE);
2002 add_to_input_buf(string, len);
2003 }
2004 break;
2005 }
2006 }
2007 if (special_keys[i].key_sym == 0)
2008 {
2009 /* Some keys need C-S- where they should only need C-.
2010 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2011 * system startup (Helmut Stiegler, 2003 Oct 3). */
2012 if (vk != 0xff
2013 && (GetKeyState(VK_CONTROL) & 0x8000)
2014 && !(GetKeyState(VK_SHIFT) & 0x8000)
2015 && !(GetKeyState(VK_MENU) & 0x8000))
2016 {
2017 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
2018 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2019 {
2020 string[0] = Ctrl_HAT;
2021 add_to_input_buf(string, 1);
2022 }
2023 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2024 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2025 {
2026 string[0] = Ctrl__;
2027 add_to_input_buf(string, 1);
2028 }
2029 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2030 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2031 {
2032 string[0] = Ctrl_AT;
2033 add_to_input_buf(string, 1);
2034 }
2035 else
2036 MyTranslateMessage(&msg);
2037 }
2038 else
2039 MyTranslateMessage(&msg);
2040 }
2041 }
2042#ifdef FEAT_MBYTE_IME
2043 else if (msg.message == WM_IME_NOTIFY)
2044 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2045 else if (msg.message == WM_KEYUP && im_get_status())
2046 /* added for non-MS IME (Yasuhiro Matsumoto) */
2047 MyTranslateMessage(&msg);
2048#endif
2049#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2050/* GIME_TEST */
2051 else if (msg.message == WM_IME_STARTCOMPOSITION)
2052 {
2053 POINT point;
2054
2055 global_ime_set_font(&norm_logfont);
2056 point.x = FILL_X(gui.col);
2057 point.y = FILL_Y(gui.row);
2058 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2059 global_ime_set_position(&point);
2060 }
2061#endif
2062
2063#ifdef FEAT_MENU
2064 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2065 * mapped we need to stop it here to avoid strange effects (e.g., for the
2066 * key-up event) */
2067 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2068 NULL, NULL) == NULL)
2069#endif
2070 pDispatchMessage(&msg);
2071}
2072
2073/*
2074 * Catch up with any queued events. This may put keyboard input into the
2075 * input buffer, call resize call-backs, trigger timers etc. If there is
2076 * nothing in the event queue (& no timers pending), then we return
2077 * immediately.
2078 */
2079 void
2080gui_mch_update(void)
2081{
2082 MSG msg;
2083
2084 if (!s_busy_processing)
2085 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2086 && !vim_is_input_buf_full())
2087 process_message();
2088}
2089
Bram Moolenaar4231da42016-06-02 14:30:04 +02002090 static void
2091remove_any_timer(void)
2092{
2093 MSG msg;
2094
2095 if (s_wait_timer != 0 && !s_timed_out)
2096 {
2097 KillTimer(NULL, s_wait_timer);
2098
2099 /* Eat spurious WM_TIMER messages */
2100 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2101 ;
2102 s_wait_timer = 0;
2103 }
2104}
2105
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106/*
2107 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2108 * from the keyboard.
2109 * wtime == -1 Wait forever.
2110 * wtime == 0 This should never happen.
2111 * wtime > 0 Wait wtime milliseconds for a character.
2112 * Returns OK if a character was found to be available within the given time,
2113 * or FAIL otherwise.
2114 */
2115 int
2116gui_mch_wait_for_chars(int wtime)
2117{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118 int focus;
2119
2120 s_timed_out = FALSE;
2121
2122 if (wtime > 0)
2123 {
2124 /* Don't do anything while processing a (scroll) message. */
2125 if (s_busy_processing)
2126 return FAIL;
2127 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2128 (TIMERPROC)_OnTimer);
2129 }
2130
2131 allow_scrollbar = TRUE;
2132
2133 focus = gui.in_focus;
2134 while (!s_timed_out)
2135 {
2136 /* Stop or start blinking when focus changes */
2137 if (gui.in_focus != focus)
2138 {
2139 if (gui.in_focus)
2140 gui_mch_start_blink();
2141 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002142 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 focus = gui.in_focus;
2144 }
2145
2146 if (s_need_activate)
2147 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 s_need_activate = FALSE;
2150 }
2151
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152#ifdef FEAT_TIMERS
2153 did_add_timer = FALSE;
2154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002156 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002157 for (;;)
2158 {
2159 MSG msg;
2160
2161 parse_queued_messages();
2162
Bram Moolenaar62426e12017-08-13 15:37:58 +02002163 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2164 {
2165 process_message();
2166 break;
2167 }
2168 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2169 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002170 break;
2171 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002172#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002173 /*
2174 * Don't use gui_mch_update() because then we will spin-lock until a
2175 * char arrives, instead we use GetMessage() to hang until an
2176 * event arrives. No need to check for input_buf_full because we are
2177 * returning as soon as it contains a single char -- webb
2178 */
2179 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002180#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002181
2182 if (input_available())
2183 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002184 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185 allow_scrollbar = FALSE;
2186
2187 /* Clear pending mouse button, the release event may have been
2188 * taken by the dialog window. But don't do this when getting
2189 * focus, we need the mouse-up event then. */
2190 if (!s_getting_focus)
2191 s_button_pending = -1;
2192
2193 return OK;
2194 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002195
2196#ifdef FEAT_TIMERS
2197 if (did_add_timer)
2198 {
2199 /* Need to recompute the waiting time. */
2200 remove_any_timer();
2201 break;
2202 }
2203#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002204 }
2205 allow_scrollbar = FALSE;
2206 return FAIL;
2207}
2208
2209/*
2210 * Clear a rectangular region of the screen from text pos (row1, col1) to
2211 * (row2, col2) inclusive.
2212 */
2213 void
2214gui_mch_clear_block(
2215 int row1,
2216 int col1,
2217 int row2,
2218 int col2)
2219{
2220 RECT rc;
2221
2222 /*
2223 * Clear one extra pixel at the far right, for when bold characters have
2224 * spilled over to the window border.
2225 * Note: FillRect() excludes right and bottom of rectangle.
2226 */
2227 rc.left = FILL_X(col1);
2228 rc.top = FILL_Y(row1);
2229 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2230 rc.bottom = FILL_Y(row2 + 1);
2231 clear_rect(&rc);
2232}
2233
2234/*
2235 * Clear the whole text window.
2236 */
2237 void
2238gui_mch_clear_all(void)
2239{
2240 RECT rc;
2241
2242 rc.left = 0;
2243 rc.top = 0;
2244 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2245 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2246 clear_rect(&rc);
2247}
2248/*
2249 * Menu stuff.
2250 */
2251
2252 void
2253gui_mch_enable_menu(int flag)
2254{
2255#ifdef FEAT_MENU
2256 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2257#endif
2258}
2259
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002260 void
2261gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002262 int x UNUSED,
2263 int y UNUSED,
2264 int w UNUSED,
2265 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002266{
2267 /* It will be in the right place anyway */
2268}
2269
2270#if defined(FEAT_MENU) || defined(PROTO)
2271/*
2272 * Make menu item hidden or not hidden
2273 */
2274 void
2275gui_mch_menu_hidden(
2276 vimmenu_T *menu,
2277 int hidden)
2278{
2279 /*
2280 * This doesn't do what we want. Hmm, just grey the menu items for now.
2281 */
2282 /*
2283 if (hidden)
2284 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2285 else
2286 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2287 */
2288 gui_mch_menu_grey(menu, hidden);
2289}
2290
2291/*
2292 * This is called after setting all the menus to grey/hidden or not.
2293 */
2294 void
2295gui_mch_draw_menubar(void)
2296{
2297 DrawMenuBar(s_hwnd);
2298}
2299#endif /*FEAT_MENU*/
2300
2301#ifndef PROTO
2302void
2303#ifdef VIMDLL
2304_export
2305#endif
2306_cdecl
2307SaveInst(HINSTANCE hInst)
2308{
2309 s_hinst = hInst;
2310}
2311#endif
2312
2313/*
2314 * Return the RGB value of a pixel as a long.
2315 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002316 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002317gui_mch_get_rgb(guicolor_T pixel)
2318{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002319 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2320 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321}
2322
2323#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2324/* Convert pixels in X to dialog units */
2325 static WORD
2326PixelToDialogX(int numPixels)
2327{
2328 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2329}
2330
2331/* Convert pixels in Y to dialog units */
2332 static WORD
2333PixelToDialogY(int numPixels)
2334{
2335 return (WORD)((numPixels * 8) / s_dlgfntheight);
2336}
2337
2338/* Return the width in pixels of the given text in the given DC. */
2339 static int
2340GetTextWidth(HDC hdc, char_u *str, int len)
2341{
2342 SIZE size;
2343
2344 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2345 return size.cx;
2346}
2347
2348#ifdef FEAT_MBYTE
2349/*
2350 * Return the width in pixels of the given text in the given DC, taking care
2351 * of 'encoding' to active codepage conversion.
2352 */
2353 static int
2354GetTextWidthEnc(HDC hdc, char_u *str, int len)
2355{
2356 SIZE size;
2357 WCHAR *wstr;
2358 int n;
2359 int wlen = len;
2360
2361 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2362 {
2363 /* 'encoding' differs from active codepage: convert text and use wide
2364 * function */
2365 wstr = enc_to_utf16(str, &wlen);
2366 if (wstr != NULL)
2367 {
2368 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2369 vim_free(wstr);
2370 if (n)
2371 return size.cx;
2372 }
2373 }
2374
2375 return GetTextWidth(hdc, str, len);
2376}
2377#else
2378# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2379#endif
2380
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002381static void get_work_area(RECT *spi_rect);
2382
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002383/*
2384 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002385 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2386 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 */
2388 static BOOL
2389CenterWindow(
2390 HWND hwndChild,
2391 HWND hwndParent)
2392{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002393 HMONITOR mon;
2394 MONITORINFO moninfo;
2395 RECT rChild, rParent, rScreen;
2396 int wChild, hChild, wParent, hParent;
2397 int xNew, yNew;
2398 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399
2400 GetWindowRect(hwndChild, &rChild);
2401 wChild = rChild.right - rChild.left;
2402 hChild = rChild.bottom - rChild.top;
2403
2404 /* If Vim is minimized put the window in the middle of the screen. */
2405 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002406 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 else
2408 GetWindowRect(hwndParent, &rParent);
2409 wParent = rParent.right - rParent.left;
2410 hParent = rParent.bottom - rParent.top;
2411
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002412 moninfo.cbSize = sizeof(MONITORINFO);
2413 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2414 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002416 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002417 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002418 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002420 hdc = GetDC(hwndChild);
2421 rScreen.left = 0;
2422 rScreen.top = 0;
2423 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2424 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2425 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426 }
2427
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002428 xNew = rParent.left + ((wParent - wChild) / 2);
2429 if (xNew < rScreen.left)
2430 xNew = rScreen.left;
2431 else if ((xNew + wChild) > rScreen.right)
2432 xNew = rScreen.right - wChild;
2433
2434 yNew = rParent.top + ((hParent - hChild) / 2);
2435 if (yNew < rScreen.top)
2436 yNew = rScreen.top;
2437 else if ((yNew + hChild) > rScreen.bottom)
2438 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002439
2440 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2441 SWP_NOSIZE | SWP_NOZORDER);
2442}
2443#endif /* FEAT_GUI_DIALOG */
2444
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445#if defined(FEAT_TOOLBAR) || defined(PROTO)
2446 void
2447gui_mch_show_toolbar(int showit)
2448{
2449 if (s_toolbarhwnd == NULL)
2450 return;
2451
2452 if (showit)
2453 {
2454# ifdef FEAT_MBYTE
2455# ifndef TB_SETUNICODEFORMAT
2456 /* For older compilers. We assume this never changes. */
2457# define TB_SETUNICODEFORMAT 0x2005
2458# endif
2459 /* Enable/disable unicode support */
2460 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2461 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2462# endif
2463 ShowWindow(s_toolbarhwnd, SW_SHOW);
2464 }
2465 else
2466 ShowWindow(s_toolbarhwnd, SW_HIDE);
2467}
2468
2469/* Then number of bitmaps is fixed. Exit is missing! */
2470#define TOOLBAR_BITMAP_COUNT 31
2471
2472#endif
2473
2474#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2475 static void
2476add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2477{
2478#ifdef FEAT_MBYTE
2479 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480
2481 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2482 {
2483 /* 'encoding' differs from active codepage: convert menu name
2484 * and use wide function */
2485 wn = enc_to_utf16(item_text, NULL);
2486 if (wn != NULL)
2487 {
2488 MENUITEMINFOW infow;
2489
2490 infow.cbSize = sizeof(infow);
2491 infow.fMask = MIIM_TYPE | MIIM_ID;
2492 infow.wID = item_id;
2493 infow.fType = MFT_STRING;
2494 infow.dwTypeData = wn;
2495 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002496 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002497 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002498 }
2499 }
2500
2501 if (wn == NULL)
2502#endif
2503 {
2504 MENUITEMINFO info;
2505
2506 info.cbSize = sizeof(info);
2507 info.fMask = MIIM_TYPE | MIIM_ID;
2508 info.wID = item_id;
2509 info.fType = MFT_STRING;
2510 info.dwTypeData = (LPTSTR)item_text;
2511 info.cch = (UINT)STRLEN(item_text);
2512 InsertMenuItem(pmenu, item_id, FALSE, &info);
2513 }
2514}
2515
2516 static void
2517show_tabline_popup_menu(void)
2518{
2519 HMENU tab_pmenu;
2520 long rval;
2521 POINT pt;
2522
2523 /* When ignoring events don't show the menu. */
2524 if (hold_gui_events
2525# ifdef FEAT_CMDWIN
2526 || cmdwin_type != 0
2527# endif
2528 )
2529 return;
2530
2531 tab_pmenu = CreatePopupMenu();
2532 if (tab_pmenu == NULL)
2533 return;
2534
2535 if (first_tabpage->tp_next != NULL)
2536 add_tabline_popup_menu_entry(tab_pmenu,
2537 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2538 add_tabline_popup_menu_entry(tab_pmenu,
2539 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2540 add_tabline_popup_menu_entry(tab_pmenu,
2541 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2542
2543 GetCursorPos(&pt);
2544 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2545 NULL);
2546
2547 DestroyMenu(tab_pmenu);
2548
2549 /* Add the string cmd into input buffer */
2550 if (rval > 0)
2551 {
2552 TCHITTESTINFO htinfo;
2553 int idx;
2554
2555 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2556 return;
2557
2558 htinfo.pt.x = pt.x;
2559 htinfo.pt.y = pt.y;
2560 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2561 if (idx == -1)
2562 idx = 0;
2563 else
2564 idx += 1;
2565
2566 send_tabline_menu_event(idx, (int)rval);
2567 }
2568}
2569
2570/*
2571 * Show or hide the tabline.
2572 */
2573 void
2574gui_mch_show_tabline(int showit)
2575{
2576 if (s_tabhwnd == NULL)
2577 return;
2578
2579 if (!showit != !showing_tabline)
2580 {
2581 if (showit)
2582 ShowWindow(s_tabhwnd, SW_SHOW);
2583 else
2584 ShowWindow(s_tabhwnd, SW_HIDE);
2585 showing_tabline = showit;
2586 }
2587}
2588
2589/*
2590 * Return TRUE when tabline is displayed.
2591 */
2592 int
2593gui_mch_showing_tabline(void)
2594{
2595 return s_tabhwnd != NULL && showing_tabline;
2596}
2597
2598/*
2599 * Update the labels of the tabline.
2600 */
2601 void
2602gui_mch_update_tabline(void)
2603{
2604 tabpage_T *tp;
2605 TCITEM tie;
2606 int nr = 0;
2607 int curtabidx = 0;
2608 int tabadded = 0;
2609#ifdef FEAT_MBYTE
2610 static int use_unicode = FALSE;
2611 int uu;
2612 WCHAR *wstr = NULL;
2613#endif
2614
2615 if (s_tabhwnd == NULL)
2616 return;
2617
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002618#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002619# ifndef CCM_SETUNICODEFORMAT
2620 /* For older compilers. We assume this never changes. */
2621# define CCM_SETUNICODEFORMAT 0x2005
2622# endif
2623 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2624 if (uu != use_unicode)
2625 {
2626 /* Enable/disable unicode support */
2627 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2628 use_unicode = uu;
2629 }
2630#endif
2631
2632 tie.mask = TCIF_TEXT;
2633 tie.iImage = -1;
2634
2635 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2636 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2637
2638 /* Add a label for each tab page. They all contain the same text area. */
2639 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2640 {
2641 if (tp == curtab)
2642 curtabidx = nr;
2643
2644 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2645 {
2646 /* Add the tab */
2647 tie.pszText = "-Empty-";
2648 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2649 tabadded = 1;
2650 }
2651
2652 get_tabline_label(tp, FALSE);
2653 tie.pszText = (LPSTR)NameBuff;
2654#ifdef FEAT_MBYTE
2655 wstr = NULL;
2656 if (use_unicode)
2657 {
2658 /* Need to go through Unicode. */
2659 wstr = enc_to_utf16(NameBuff, NULL);
2660 if (wstr != NULL)
2661 {
2662 TCITEMW tiw;
2663
2664 tiw.mask = TCIF_TEXT;
2665 tiw.iImage = -1;
2666 tiw.pszText = wstr;
2667 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2668 vim_free(wstr);
2669 }
2670 }
2671 if (wstr == NULL)
2672#endif
2673 {
2674 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2675 }
2676 }
2677
2678 /* Remove any old labels. */
2679 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2680 TabCtrl_DeleteItem(s_tabhwnd, nr);
2681
2682 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2683 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2684
2685 /* Re-enable redraw and redraw. */
2686 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2687 RedrawWindow(s_tabhwnd, NULL, NULL,
2688 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2689
2690 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2691 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2692}
2693
2694/*
2695 * Set the current tab to "nr". First tab is 1.
2696 */
2697 void
2698gui_mch_set_curtab(int nr)
2699{
2700 if (s_tabhwnd == NULL)
2701 return;
2702
2703 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2704 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2705}
2706
2707#endif
2708
2709/*
2710 * ":simalt" command.
2711 */
2712 void
2713ex_simalt(exarg_T *eap)
2714{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002715 char_u *keys = eap->arg;
2716 int fill_typebuf = FALSE;
2717 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002718
2719 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2720 while (*keys)
2721 {
2722 if (*keys == '~')
2723 *keys = ' '; /* for showing system menu */
2724 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2725 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002726 fill_typebuf = TRUE;
2727 }
2728 if (fill_typebuf)
2729 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002730 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002731 * processed. */
2732 key_name[0] = K_SPECIAL;
2733 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002734 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002735 key_name[3] = NUL;
2736 typebuf_was_filled = TRUE;
2737 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 }
2739}
2740
2741/*
2742 * Create the find & replace dialogs.
2743 * You can't have both at once: ":find" when replace is showing, destroys
2744 * the replace dialog first, and the other way around.
2745 */
2746#ifdef MSWIN_FIND_REPLACE
2747 static void
2748initialise_findrep(char_u *initial_string)
2749{
2750 int wword = FALSE;
2751 int mcase = !p_ic;
2752 char_u *entry_text;
2753
2754 /* Get the search string to use. */
2755 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2756
2757 s_findrep_struct.hwndOwner = s_hwnd;
2758 s_findrep_struct.Flags = FR_DOWN;
2759 if (mcase)
2760 s_findrep_struct.Flags |= FR_MATCHCASE;
2761 if (wword)
2762 s_findrep_struct.Flags |= FR_WHOLEWORD;
2763 if (entry_text != NULL && *entry_text != NUL)
2764 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2765 s_findrep_struct.wFindWhatLen - 1);
2766 vim_free(entry_text);
2767}
2768#endif
2769
2770 static void
2771set_window_title(HWND hwnd, char *title)
2772{
2773#ifdef FEAT_MBYTE
2774 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2775 {
2776 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777
2778 /* Convert the title from 'encoding' to UTF-16. */
2779 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2780 if (wbuf != NULL)
2781 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002782 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002785 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002786 }
2787#endif
2788 (void)SetWindowText(hwnd, (LPCSTR)title);
2789}
2790
2791 void
2792gui_mch_find_dialog(exarg_T *eap)
2793{
2794#ifdef MSWIN_FIND_REPLACE
2795 if (s_findrep_msg != 0)
2796 {
2797 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2798 DestroyWindow(s_findrep_hwnd);
2799
2800 if (!IsWindow(s_findrep_hwnd))
2801 {
2802 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002803# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002804 /* If the OS is Windows NT, and 'encoding' differs from active
2805 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002806 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002807 {
2808 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2809 s_findrep_hwnd = FindTextW(
2810 (LPFINDREPLACEW) &s_findrep_struct_w);
2811 }
2812 else
2813# endif
2814 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2815 }
2816
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002817 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002818 (void)SetFocus(s_findrep_hwnd);
2819
2820 s_findrep_is_find = TRUE;
2821 }
2822#endif
2823}
2824
2825
2826 void
2827gui_mch_replace_dialog(exarg_T *eap)
2828{
2829#ifdef MSWIN_FIND_REPLACE
2830 if (s_findrep_msg != 0)
2831 {
2832 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2833 DestroyWindow(s_findrep_hwnd);
2834
2835 if (!IsWindow(s_findrep_hwnd))
2836 {
2837 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002838# ifdef FEAT_MBYTE
2839 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840 {
2841 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2842 s_findrep_hwnd = ReplaceTextW(
2843 (LPFINDREPLACEW) &s_findrep_struct_w);
2844 }
2845 else
2846# endif
2847 s_findrep_hwnd = ReplaceText(
2848 (LPFINDREPLACE) &s_findrep_struct);
2849 }
2850
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002851 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 (void)SetFocus(s_findrep_hwnd);
2853
2854 s_findrep_is_find = FALSE;
2855 }
2856#endif
2857}
2858
2859
2860/*
2861 * Set visibility of the pointer.
2862 */
2863 void
2864gui_mch_mousehide(int hide)
2865{
2866 if (hide != gui.pointer_hidden)
2867 {
2868 ShowCursor(!hide);
2869 gui.pointer_hidden = hide;
2870 }
2871}
2872
2873#ifdef FEAT_MENU
2874 static void
2875gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2876{
2877 /* Unhide the mouse, we don't get move events here. */
2878 gui_mch_mousehide(FALSE);
2879
2880 (void)TrackPopupMenu(
2881 (HMENU)menu->submenu_id,
2882 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2883 x, y,
2884 (int)0, /*reserved param*/
2885 s_hwnd,
2886 NULL);
2887 /*
2888 * NOTE: The pop-up menu can eat the mouse up event.
2889 * We deal with this in normal.c.
2890 */
2891}
2892#endif
2893
2894/*
2895 * Got a message when the system will go down.
2896 */
2897 static void
2898_OnEndSession(void)
2899{
2900 getout_preserve_modified(1);
2901}
2902
2903/*
2904 * Get this message when the user clicks on the cross in the top right corner
2905 * of a Windows95 window.
2906 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002907 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002908_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909{
2910 gui_shell_closed();
2911}
2912
2913/*
2914 * Get a message when the window is being destroyed.
2915 */
2916 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002917_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002918{
2919 if (!destroying)
2920 _OnClose(hwnd);
2921}
2922
2923 static void
2924_OnPaint(
2925 HWND hwnd)
2926{
2927 if (!IsMinimized(hwnd))
2928 {
2929 PAINTSTRUCT ps;
2930
2931 out_flush(); /* make sure all output has been processed */
2932 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002933
2934#ifdef FEAT_MBYTE
2935 /* prevent multi-byte characters from misprinting on an invalid
2936 * rectangle */
2937 if (has_mbyte)
2938 {
2939 RECT rect;
2940
2941 GetClientRect(hwnd, &rect);
2942 ps.rcPaint.left = rect.left;
2943 ps.rcPaint.right = rect.right;
2944 }
2945#endif
2946
2947 if (!IsRectEmpty(&ps.rcPaint))
2948 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002949 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2950 ps.rcPaint.right - ps.rcPaint.left + 1,
2951 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2952 }
2953
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 EndPaint(hwnd, &ps);
2955 }
2956}
2957
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002958 static void
2959_OnSize(
2960 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002961 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002962 int cx,
2963 int cy)
2964{
2965 if (!IsMinimized(hwnd))
2966 {
2967 gui_resize_shell(cx, cy);
2968
2969#ifdef FEAT_MENU
2970 /* Menu bar may wrap differently now */
2971 gui_mswin_get_menu_height(TRUE);
2972#endif
2973 }
2974}
2975
2976 static void
2977_OnSetFocus(
2978 HWND hwnd,
2979 HWND hwndOldFocus)
2980{
2981 gui_focus_change(TRUE);
2982 s_getting_focus = TRUE;
2983 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2984}
2985
2986 static void
2987_OnKillFocus(
2988 HWND hwnd,
2989 HWND hwndNewFocus)
2990{
2991 gui_focus_change(FALSE);
2992 s_getting_focus = FALSE;
2993 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2994}
2995
2996/*
2997 * Get a message when the user switches back to vim
2998 */
2999 static LRESULT
3000_OnActivateApp(
3001 HWND hwnd,
3002 BOOL fActivate,
3003 DWORD dwThreadId)
3004{
3005 /* we call gui_focus_change() in _OnSetFocus() */
3006 /* gui_focus_change((int)fActivate); */
3007 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3008}
3009
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003010 void
3011gui_mch_destroy_scrollbar(scrollbar_T *sb)
3012{
3013 DestroyWindow(sb->id);
3014}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015
3016/*
3017 * Get current mouse coordinates in text window.
3018 */
3019 void
3020gui_mch_getmouse(int *x, int *y)
3021{
3022 RECT rct;
3023 POINT mp;
3024
3025 (void)GetWindowRect(s_textArea, &rct);
3026 (void)GetCursorPos((LPPOINT)&mp);
3027 *x = (int)(mp.x - rct.left);
3028 *y = (int)(mp.y - rct.top);
3029}
3030
3031/*
3032 * Move mouse pointer to character at (x, y).
3033 */
3034 void
3035gui_mch_setmouse(int x, int y)
3036{
3037 RECT rct;
3038
3039 (void)GetWindowRect(s_textArea, &rct);
3040 (void)SetCursorPos(x + gui.border_offset + rct.left,
3041 y + gui.border_offset + rct.top);
3042}
3043
3044 static void
3045gui_mswin_get_valid_dimensions(
3046 int w,
3047 int h,
3048 int *valid_w,
3049 int *valid_h)
3050{
3051 int base_width, base_height;
3052
3053 base_width = gui_get_base_width()
3054 + (GetSystemMetrics(SM_CXFRAME) +
3055 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3056 base_height = gui_get_base_height()
3057 + (GetSystemMetrics(SM_CYFRAME) +
3058 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3059 + GetSystemMetrics(SM_CYCAPTION)
3060#ifdef FEAT_MENU
3061 + gui_mswin_get_menu_height(FALSE)
3062#endif
3063 ;
3064 *valid_w = base_width +
3065 ((w - base_width) / gui.char_width) * gui.char_width;
3066 *valid_h = base_height +
3067 ((h - base_height) / gui.char_height) * gui.char_height;
3068}
3069
3070 void
3071gui_mch_flash(int msec)
3072{
3073 RECT rc;
3074
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003075#if defined(FEAT_DIRECTX)
3076 if (IS_ENABLE_DIRECTX())
3077 DWriteContext_Flush(s_dwc);
3078#endif
3079
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003080 /*
3081 * Note: InvertRect() excludes right and bottom of rectangle.
3082 */
3083 rc.left = 0;
3084 rc.top = 0;
3085 rc.right = gui.num_cols * gui.char_width;
3086 rc.bottom = gui.num_rows * gui.char_height;
3087 InvertRect(s_hdc, &rc);
3088 gui_mch_flush(); /* make sure it's displayed */
3089
3090 ui_delay((long)msec, TRUE); /* wait for a few msec */
3091
3092 InvertRect(s_hdc, &rc);
3093}
3094
3095/*
3096 * Return flags used for scrolling.
3097 * The SW_INVALIDATE is required when part of the window is covered or
3098 * off-screen. Refer to MS KB Q75236.
3099 */
3100 static int
3101get_scroll_flags(void)
3102{
3103 HWND hwnd;
3104 RECT rcVim, rcOther, rcDest;
3105
3106 GetWindowRect(s_hwnd, &rcVim);
3107
3108 /* Check if the window is partly above or below the screen. We don't care
3109 * about partly left or right of the screen, it is not relevant when
3110 * scrolling up or down. */
3111 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3112 return SW_INVALIDATE;
3113
3114 /* Check if there is an window (partly) on top of us. */
3115 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3116 if (IsWindowVisible(hwnd))
3117 {
3118 GetWindowRect(hwnd, &rcOther);
3119 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3120 return SW_INVALIDATE;
3121 }
3122 return 0;
3123}
3124
3125/*
3126 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3127 * may not be scrolled out properly.
3128 * For gVim, when _OnScroll() is repeated, the character at the
3129 * previous cursor position may be left drawn after scroll.
3130 * The problem can be avoided by calling GetPixel() to get a pixel in
3131 * the region before ScrollWindowEx().
3132 */
3133 static void
3134intel_gpu_workaround(void)
3135{
3136 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3137}
3138
3139/*
3140 * Delete the given number of lines from the given row, scrolling up any
3141 * text further down within the scroll region.
3142 */
3143 void
3144gui_mch_delete_lines(
3145 int row,
3146 int num_lines)
3147{
3148 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003149
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150 rc.left = FILL_X(gui.scroll_region_left);
3151 rc.right = FILL_X(gui.scroll_region_right + 1);
3152 rc.top = FILL_Y(row);
3153 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3154
Bram Moolenaar92467d32017-12-05 13:22:16 +01003155#if defined(FEAT_DIRECTX)
3156 if (IS_ENABLE_DIRECTX())
3157 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003158 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3159 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003160 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003161 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003162#endif
3163 {
3164 intel_gpu_workaround();
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 /* This seems to be required to avoid the cursor disappearing when
3171 * scrolling such that the cursor ends up in the top-left character on
3172 * the screen... But why? (Webb) */
3173 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3174 /* gui.cursor_is_valid = FALSE; */
3175
3176 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3177 gui.scroll_region_left,
3178 gui.scroll_region_bot, gui.scroll_region_right);
3179}
3180
3181/*
3182 * Insert the given number of lines before the given row, scrolling down any
3183 * following text within the scroll region.
3184 */
3185 void
3186gui_mch_insert_lines(
3187 int row,
3188 int num_lines)
3189{
3190 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003191
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003192 rc.left = FILL_X(gui.scroll_region_left);
3193 rc.right = FILL_X(gui.scroll_region_right + 1);
3194 rc.top = FILL_Y(row);
3195 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003196
3197#if defined(FEAT_DIRECTX)
3198 if (IS_ENABLE_DIRECTX())
3199 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003200 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3201 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003202 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003203 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003204#endif
3205 {
3206 intel_gpu_workaround();
3207 /* The SW_INVALIDATE is required when part of the window is covered or
3208 * off-screen. How do we avoid it when it's not needed? */
3209 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003210 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003211 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003212 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 gui_clear_block(row, gui.scroll_region_left,
3215 row + num_lines - 1, gui.scroll_region_right);
3216}
3217
3218
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003220gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221{
3222#if defined(FEAT_DIRECTX)
3223 DWriteContext_Close(s_dwc);
3224 DWrite_Final();
3225 s_dwc = NULL;
3226#endif
3227
3228 ReleaseDC(s_textArea, s_hdc);
3229 DeleteObject(s_brush);
3230
3231#ifdef FEAT_TEAROFF
3232 /* Unload the tearoff bitmap */
3233 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3234#endif
3235
3236 /* Destroy our window (if we have one). */
3237 if (s_hwnd != NULL)
3238 {
3239 destroying = TRUE; /* ignore WM_DESTROY message now */
3240 DestroyWindow(s_hwnd);
3241 }
3242
3243#ifdef GLOBAL_IME
3244 global_ime_end();
3245#endif
3246}
3247
3248 static char_u *
3249logfont2name(LOGFONT lf)
3250{
3251 char *p;
3252 char *res;
3253 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003254 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003255 char *font_name = lf.lfFaceName;
3256
3257 charset_name = charset_id2name((int)lf.lfCharSet);
3258#ifdef FEAT_MBYTE
3259 /* Convert a font name from the current codepage to 'encoding'.
3260 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3261 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3262 {
3263 int len;
3264 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3265 (char_u **)&font_name, &len);
3266 }
3267#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003268 quality_name = quality_id2name((int)lf.lfQuality);
3269
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003270 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3271 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3272 if (res != NULL)
3273 {
3274 p = res;
3275 /* make a normal font string out of the lf thing:*/
3276 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3277 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3278 while (*p)
3279 {
3280 if (*p == ' ')
3281 *p = '_';
3282 ++p;
3283 }
3284 if (lf.lfItalic)
3285 STRCAT(p, ":i");
3286 if (lf.lfWeight >= FW_BOLD)
3287 STRCAT(p, ":b");
3288 if (lf.lfUnderline)
3289 STRCAT(p, ":u");
3290 if (lf.lfStrikeOut)
3291 STRCAT(p, ":s");
3292 if (charset_name != NULL)
3293 {
3294 STRCAT(p, ":c");
3295 STRCAT(p, charset_name);
3296 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003297 if (quality_name != NULL)
3298 {
3299 STRCAT(p, ":q");
3300 STRCAT(p, quality_name);
3301 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003302 }
3303
3304#ifdef FEAT_MBYTE
3305 if (font_name != lf.lfFaceName)
3306 vim_free(font_name);
3307#endif
3308 return (char_u *)res;
3309}
3310
3311
3312#ifdef FEAT_MBYTE_IME
3313/*
3314 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3315 * 'guifont'
3316 */
3317 static void
3318update_im_font(void)
3319{
3320 LOGFONT lf_wide;
3321
3322 if (p_guifontwide != NULL && *p_guifontwide != NUL
3323 && gui.wide_font != NOFONT
3324 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3325 norm_logfont = lf_wide;
3326 else
3327 norm_logfont = sub_logfont;
3328 im_set_font(&norm_logfont);
3329}
3330#endif
3331
3332#ifdef FEAT_MBYTE
3333/*
3334 * Handler of gui.wide_font (p_guifontwide) changed notification.
3335 */
3336 void
3337gui_mch_wide_font_changed(void)
3338{
3339 LOGFONT lf;
3340
3341# ifdef FEAT_MBYTE_IME
3342 update_im_font();
3343# endif
3344
3345 gui_mch_free_font(gui.wide_ital_font);
3346 gui.wide_ital_font = NOFONT;
3347 gui_mch_free_font(gui.wide_bold_font);
3348 gui.wide_bold_font = NOFONT;
3349 gui_mch_free_font(gui.wide_boldital_font);
3350 gui.wide_boldital_font = NOFONT;
3351
3352 if (gui.wide_font
3353 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3354 {
3355 if (!lf.lfItalic)
3356 {
3357 lf.lfItalic = TRUE;
3358 gui.wide_ital_font = get_font_handle(&lf);
3359 lf.lfItalic = FALSE;
3360 }
3361 if (lf.lfWeight < FW_BOLD)
3362 {
3363 lf.lfWeight = FW_BOLD;
3364 gui.wide_bold_font = get_font_handle(&lf);
3365 if (!lf.lfItalic)
3366 {
3367 lf.lfItalic = TRUE;
3368 gui.wide_boldital_font = get_font_handle(&lf);
3369 }
3370 }
3371 }
3372}
3373#endif
3374
3375/*
3376 * Initialise vim to use the font with the given name.
3377 * Return FAIL if the font could not be loaded, OK otherwise.
3378 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003380gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003381{
3382 LOGFONT lf;
3383 GuiFont font = NOFONT;
3384 char_u *p;
3385
3386 /* Load the font */
3387 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3388 font = get_font_handle(&lf);
3389 if (font == NOFONT)
3390 return FAIL;
3391
3392 if (font_name == NULL)
3393 font_name = (char_u *)lf.lfFaceName;
3394#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3395 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003396#endif
3397#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 sub_logfont = lf;
3399#endif
3400#ifdef FEAT_MBYTE_IME
3401 update_im_font();
3402#endif
3403 gui_mch_free_font(gui.norm_font);
3404 gui.norm_font = font;
3405 current_font_height = lf.lfHeight;
3406 GetFontSize(font);
3407
3408 p = logfont2name(lf);
3409 if (p != NULL)
3410 {
3411 hl_set_font_name(p);
3412
3413 /* When setting 'guifont' to "*" replace it with the actual font name.
3414 * */
3415 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3416 {
3417 vim_free(p_guifont);
3418 p_guifont = p;
3419 }
3420 else
3421 vim_free(p);
3422 }
3423
3424 gui_mch_free_font(gui.ital_font);
3425 gui.ital_font = NOFONT;
3426 gui_mch_free_font(gui.bold_font);
3427 gui.bold_font = NOFONT;
3428 gui_mch_free_font(gui.boldital_font);
3429 gui.boldital_font = NOFONT;
3430
3431 if (!lf.lfItalic)
3432 {
3433 lf.lfItalic = TRUE;
3434 gui.ital_font = get_font_handle(&lf);
3435 lf.lfItalic = FALSE;
3436 }
3437 if (lf.lfWeight < FW_BOLD)
3438 {
3439 lf.lfWeight = FW_BOLD;
3440 gui.bold_font = get_font_handle(&lf);
3441 if (!lf.lfItalic)
3442 {
3443 lf.lfItalic = TRUE;
3444 gui.boldital_font = get_font_handle(&lf);
3445 }
3446 }
3447
3448 return OK;
3449}
3450
3451#ifndef WPF_RESTORETOMAXIMIZED
3452# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3453#endif
3454
3455/*
3456 * Return TRUE if the GUI window is maximized, filling the whole screen.
3457 */
3458 int
3459gui_mch_maximized(void)
3460{
3461 WINDOWPLACEMENT wp;
3462
3463 wp.length = sizeof(WINDOWPLACEMENT);
3464 if (GetWindowPlacement(s_hwnd, &wp))
3465 return wp.showCmd == SW_SHOWMAXIMIZED
3466 || (wp.showCmd == SW_SHOWMINIMIZED
3467 && wp.flags == WPF_RESTORETOMAXIMIZED);
3468
3469 return 0;
3470}
3471
3472/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003473 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3474 * is set. Compute the new Rows and Columns. This is like resizing the
3475 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476 */
3477 void
3478gui_mch_newfont(void)
3479{
3480 RECT rect;
3481
3482 GetWindowRect(s_hwnd, &rect);
3483 if (win_socket_id == 0)
3484 {
3485 gui_resize_shell(rect.right - rect.left
3486 - (GetSystemMetrics(SM_CXFRAME) +
3487 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3488 rect.bottom - rect.top
3489 - (GetSystemMetrics(SM_CYFRAME) +
3490 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3491 - GetSystemMetrics(SM_CYCAPTION)
3492#ifdef FEAT_MENU
3493 - gui_mswin_get_menu_height(FALSE)
3494#endif
3495 );
3496 }
3497 else
3498 {
3499 /* Inside another window, don't use the frame and border. */
3500 gui_resize_shell(rect.right - rect.left,
3501 rect.bottom - rect.top
3502#ifdef FEAT_MENU
3503 - gui_mswin_get_menu_height(FALSE)
3504#endif
3505 );
3506 }
3507}
3508
3509/*
3510 * Set the window title
3511 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512 void
3513gui_mch_settitle(
3514 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003515 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516{
3517 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3518}
3519
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003520#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003521/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3522 * misc2.c! */
3523static LPCSTR mshape_idcs[] =
3524{
3525 IDC_ARROW, /* arrow */
3526 MAKEINTRESOURCE(0), /* blank */
3527 IDC_IBEAM, /* beam */
3528 IDC_SIZENS, /* updown */
3529 IDC_SIZENS, /* udsizing */
3530 IDC_SIZEWE, /* leftright */
3531 IDC_SIZEWE, /* lrsizing */
3532 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003533 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003534 IDC_ARROW, /* crosshair */
3535 IDC_ARROW, /* hand1 */
3536 IDC_ARROW, /* hand2 */
3537 IDC_ARROW, /* pencil */
3538 IDC_ARROW, /* question */
3539 IDC_ARROW, /* right-arrow */
3540 IDC_UPARROW, /* up-arrow */
3541 IDC_ARROW /* last one */
3542};
3543
3544 void
3545mch_set_mouse_shape(int shape)
3546{
3547 LPCSTR idc;
3548
3549 if (shape == MSHAPE_HIDE)
3550 ShowCursor(FALSE);
3551 else
3552 {
3553 if (shape >= MSHAPE_NUMBERED)
3554 idc = IDC_ARROW;
3555 else
3556 idc = mshape_idcs[shape];
3557#ifdef SetClassLongPtr
3558 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3559#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003560 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561#endif
3562 if (!p_mh)
3563 {
3564 POINT mp;
3565
3566 /* Set the position to make it redrawn with the new shape. */
3567 (void)GetCursorPos((LPPOINT)&mp);
3568 (void)SetCursorPos(mp.x, mp.y);
3569 ShowCursor(TRUE);
3570 }
3571 }
3572}
3573#endif
3574
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003575#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576/*
3577 * The file browser exists in two versions: with "W" uses wide characters,
3578 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3579 * Windows NT/2000/XP the "W" functions are used.
3580 */
3581
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003582# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003583/*
3584 * Wide version of convert_filter().
3585 */
3586 static WCHAR *
3587convert_filterW(char_u *s)
3588{
3589 char_u *tmp;
3590 int len;
3591 WCHAR *res;
3592
3593 tmp = convert_filter(s);
3594 if (tmp == NULL)
3595 return NULL;
3596 len = (int)STRLEN(s) + 3;
3597 res = enc_to_utf16(tmp, &len);
3598 vim_free(tmp);
3599 return res;
3600}
3601
3602/*
3603 * Wide version of gui_mch_browse(). Keep in sync!
3604 */
3605 static char_u *
3606gui_mch_browseW(
3607 int saving,
3608 char_u *title,
3609 char_u *dflt,
3610 char_u *ext,
3611 char_u *initdir,
3612 char_u *filter)
3613{
3614 /* We always use the wide function. This means enc_to_utf16() must work,
3615 * otherwise it fails miserably! */
3616 OPENFILENAMEW fileStruct;
3617 WCHAR fileBuf[MAXPATHL];
3618 WCHAR *wp;
3619 int i;
3620 WCHAR *titlep = NULL;
3621 WCHAR *extp = NULL;
3622 WCHAR *initdirp = NULL;
3623 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003624 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003625
3626 if (dflt == NULL)
3627 fileBuf[0] = NUL;
3628 else
3629 {
3630 wp = enc_to_utf16(dflt, NULL);
3631 if (wp == NULL)
3632 fileBuf[0] = NUL;
3633 else
3634 {
3635 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3636 fileBuf[i] = wp[i];
3637 fileBuf[i] = NUL;
3638 vim_free(wp);
3639 }
3640 }
3641
3642 /* Convert the filter to Windows format. */
3643 filterp = convert_filterW(filter);
3644
3645 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003646# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003648 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003649# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003651# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652
3653 if (title != NULL)
3654 titlep = enc_to_utf16(title, NULL);
3655 fileStruct.lpstrTitle = titlep;
3656
3657 if (ext != NULL)
3658 extp = enc_to_utf16(ext, NULL);
3659 fileStruct.lpstrDefExt = extp;
3660
3661 fileStruct.lpstrFile = fileBuf;
3662 fileStruct.nMaxFile = MAXPATHL;
3663 fileStruct.lpstrFilter = filterp;
3664 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3665 /* has an initial dir been specified? */
3666 if (initdir != NULL && *initdir != NUL)
3667 {
3668 /* Must have backslashes here, no matter what 'shellslash' says */
3669 initdirp = enc_to_utf16(initdir, NULL);
3670 if (initdirp != NULL)
3671 {
3672 for (wp = initdirp; *wp != NUL; ++wp)
3673 if (*wp == '/')
3674 *wp = '\\';
3675 }
3676 fileStruct.lpstrInitialDir = initdirp;
3677 }
3678
3679 /*
3680 * TODO: Allow selection of multiple files. Needs another arg to this
3681 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3682 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3683 * files that don't exist yet, so I haven't put it in. What about
3684 * OFN_PATHMUSTEXIST?
3685 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3686 */
3687 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003688# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 if (curbuf->b_p_bin)
3690 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003691# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692 if (saving)
3693 {
3694 if (!GetSaveFileNameW(&fileStruct))
3695 return NULL;
3696 }
3697 else
3698 {
3699 if (!GetOpenFileNameW(&fileStruct))
3700 return NULL;
3701 }
3702
3703 vim_free(filterp);
3704 vim_free(initdirp);
3705 vim_free(titlep);
3706 vim_free(extp);
3707
3708 /* Convert from UCS2 to 'encoding'. */
3709 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003710 if (p == NULL)
3711 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712
3713 /* Give focus back to main window (when using MDI). */
3714 SetFocus(s_hwnd);
3715
3716 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003717 q = vim_strsave(shorten_fname1(p));
3718 vim_free(p);
3719 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720}
3721# endif /* FEAT_MBYTE */
3722
3723
3724/*
3725 * Convert the string s to the proper format for a filter string by replacing
3726 * the \t and \n delimiters with \0.
3727 * Returns the converted string in allocated memory.
3728 *
3729 * Keep in sync with convert_filterW() above!
3730 */
3731 static char_u *
3732convert_filter(char_u *s)
3733{
3734 char_u *res;
3735 unsigned s_len = (unsigned)STRLEN(s);
3736 unsigned i;
3737
3738 res = alloc(s_len + 3);
3739 if (res != NULL)
3740 {
3741 for (i = 0; i < s_len; ++i)
3742 if (s[i] == '\t' || s[i] == '\n')
3743 res[i] = '\0';
3744 else
3745 res[i] = s[i];
3746 res[s_len] = NUL;
3747 /* Add two extra NULs to make sure it's properly terminated. */
3748 res[s_len + 1] = NUL;
3749 res[s_len + 2] = NUL;
3750 }
3751 return res;
3752}
3753
3754/*
3755 * Select a directory.
3756 */
3757 char_u *
3758gui_mch_browsedir(char_u *title, char_u *initdir)
3759{
3760 /* We fake this: Use a filter that doesn't select anything and a default
3761 * file name that won't be used. */
3762 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3763 initdir, (char_u *)_("Directory\t*.nothing\n"));
3764}
3765
3766/*
3767 * Pop open a file browser and return the file selected, in allocated memory,
3768 * or NULL if Cancel is hit.
3769 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3770 * title - Title message for the file browser dialog.
3771 * dflt - Default name of file.
3772 * ext - Default extension to be added to files without extensions.
3773 * initdir - directory in which to open the browser (NULL = current dir)
3774 * filter - Filter for matched files to choose from.
3775 *
3776 * Keep in sync with gui_mch_browseW() above!
3777 */
3778 char_u *
3779gui_mch_browse(
3780 int saving,
3781 char_u *title,
3782 char_u *dflt,
3783 char_u *ext,
3784 char_u *initdir,
3785 char_u *filter)
3786{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003787# ifdef FEAT_MBYTE
3788 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3789# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 OPENFILENAME fileStruct;
3791 char_u fileBuf[MAXPATHL];
3792 char_u *initdirp = NULL;
3793 char_u *filterp;
3794 char_u *p;
3795
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 if (dflt == NULL)
3797 fileBuf[0] = NUL;
3798 else
3799 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3800
3801 /* Convert the filter to Windows format. */
3802 filterp = convert_filter(filter);
3803
3804 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003805# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806 /* be compatible with Windows NT 4.0 */
3807 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003808# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003810# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003811
3812 fileStruct.lpstrTitle = (LPSTR)title;
3813 fileStruct.lpstrDefExt = (LPSTR)ext;
3814
3815 fileStruct.lpstrFile = (LPSTR)fileBuf;
3816 fileStruct.nMaxFile = MAXPATHL;
3817 fileStruct.lpstrFilter = (LPSTR)filterp;
3818 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3819 /* has an initial dir been specified? */
3820 if (initdir != NULL && *initdir != NUL)
3821 {
3822 /* Must have backslashes here, no matter what 'shellslash' says */
3823 initdirp = vim_strsave(initdir);
3824 if (initdirp != NULL)
3825 for (p = initdirp; *p != NUL; ++p)
3826 if (*p == '/')
3827 *p = '\\';
3828 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3829 }
3830
3831 /*
3832 * TODO: Allow selection of multiple files. Needs another arg to this
3833 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3834 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3835 * files that don't exist yet, so I haven't put it in. What about
3836 * OFN_PATHMUSTEXIST?
3837 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3838 */
3839 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003840# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841 if (curbuf->b_p_bin)
3842 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003843# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844 if (saving)
3845 {
3846 if (!GetSaveFileName(&fileStruct))
3847 return NULL;
3848 }
3849 else
3850 {
3851 if (!GetOpenFileName(&fileStruct))
3852 return NULL;
3853 }
3854
3855 vim_free(filterp);
3856 vim_free(initdirp);
3857
3858 /* Give focus back to main window (when using MDI). */
3859 SetFocus(s_hwnd);
3860
3861 /* Shorten the file name if possible */
3862 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003863# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864}
3865#endif /* FEAT_BROWSE */
3866
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003867 static void
3868_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003869 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870 HDROP hDrop)
3871{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003872#define BUFPATHLEN _MAX_PATH
3873#define DRAGQVAL 0xFFFFFFFF
3874#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003876#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877 char szFile[BUFPATHLEN];
3878 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3879 UINT i;
3880 char_u **fnames;
3881 POINT pt;
3882 int_u modifiers = 0;
3883
3884 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3885
3886 /* Obtain dropped position */
3887 DragQueryPoint(hDrop, &pt);
3888 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3889
3890 reset_VIsual();
3891
3892 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3893
3894 if (fnames != NULL)
3895 for (i = 0; i < cFiles; ++i)
3896 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003897#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3899 fnames[i] = utf16_to_enc(wszFile, NULL);
3900 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003901#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003902 {
3903 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3904 fnames[i] = vim_strsave((char_u *)szFile);
3905 }
3906 }
3907
3908 DragFinish(hDrop);
3909
3910 if (fnames != NULL)
3911 {
3912 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3913 modifiers |= MOUSE_SHIFT;
3914 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3915 modifiers |= MOUSE_CTRL;
3916 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3917 modifiers |= MOUSE_ALT;
3918
3919 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3920
3921 s_need_activate = TRUE;
3922 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003923}
3924
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925 static int
3926_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003927 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 HWND hwndCtl,
3929 UINT code,
3930 int pos)
3931{
3932 static UINT prev_code = 0; /* code of previous call */
3933 scrollbar_T *sb, *sb_info;
3934 long val;
3935 int dragging = FALSE;
3936 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003937 SCROLLINFO si;
3938
3939 si.cbSize = sizeof(si);
3940 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003941
3942 sb = gui_mswin_find_scrollbar(hwndCtl);
3943 if (sb == NULL)
3944 return 0;
3945
3946 if (sb->wp != NULL) /* Left or right scrollbar */
3947 {
3948 /*
3949 * Careful: need to get scrollbar info out of first (left) scrollbar
3950 * for window, but keep real scrollbar too because we must pass it to
3951 * gui_drag_scrollbar().
3952 */
3953 sb_info = &sb->wp->w_scrollbars[0];
3954 }
3955 else /* Bottom scrollbar */
3956 sb_info = sb;
3957 val = sb_info->value;
3958
3959 switch (code)
3960 {
3961 case SB_THUMBTRACK:
3962 val = pos;
3963 dragging = TRUE;
3964 if (sb->scroll_shift > 0)
3965 val <<= sb->scroll_shift;
3966 break;
3967 case SB_LINEDOWN:
3968 val++;
3969 break;
3970 case SB_LINEUP:
3971 val--;
3972 break;
3973 case SB_PAGEDOWN:
3974 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3975 break;
3976 case SB_PAGEUP:
3977 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3978 break;
3979 case SB_TOP:
3980 val = 0;
3981 break;
3982 case SB_BOTTOM:
3983 val = sb_info->max;
3984 break;
3985 case SB_ENDSCROLL:
3986 if (prev_code == SB_THUMBTRACK)
3987 {
3988 /*
3989 * "pos" only gives us 16-bit data. In case of large file,
3990 * use GetScrollPos() which returns 32-bit. Unfortunately it
3991 * is not valid while the scrollbar is being dragged.
3992 */
3993 val = GetScrollPos(hwndCtl, SB_CTL);
3994 if (sb->scroll_shift > 0)
3995 val <<= sb->scroll_shift;
3996 }
3997 break;
3998
3999 default:
4000 /* TRACE("Unknown scrollbar event %d\n", code); */
4001 return 0;
4002 }
4003 prev_code = code;
4004
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004005 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4006 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007
4008 /*
4009 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4010 */
4011 if (sb->wp != NULL)
4012 {
4013 scrollbar_T *sba = sb->wp->w_scrollbars;
4014 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004016 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004017 }
4018
4019 /* Don't let us be interrupted here by another message. */
4020 s_busy_processing = TRUE;
4021
4022 /* When "allow_scrollbar" is FALSE still need to remember the new
4023 * position, but don't actually scroll by setting "dont_scroll". */
4024 dont_scroll = !allow_scrollbar;
4025
Bram Moolenaara338adc2018-01-31 20:51:47 +01004026 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004027 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004028 mch_enable_flush();
4029 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004030
4031 s_busy_processing = FALSE;
4032 dont_scroll = dont_scroll_save;
4033
4034 return 0;
4035}
4036
4037
4038/*
4039 * Get command line arguments.
4040 * Use "prog" as the name of the program and "cmdline" as the arguments.
4041 * Copy the arguments to allocated memory.
4042 * Return the number of arguments (including program name).
4043 * Return pointers to the arguments in "argvp". Memory is allocated with
4044 * malloc(), use free() instead of vim_free().
4045 * Return pointer to buffer in "tofree".
4046 * Returns zero when out of memory.
4047 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004048 int
4049get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4050{
4051 int i;
4052 char *p;
4053 char *progp;
4054 char *pnew = NULL;
4055 char *newcmdline;
4056 int inquote;
4057 int argc;
4058 char **argv = NULL;
4059 int round;
4060
4061 *tofree = NULL;
4062
4063#ifdef FEAT_MBYTE
4064 /* Try using the Unicode version first, it takes care of conversion when
4065 * 'encoding' is changed. */
4066 argc = get_cmd_argsW(&argv);
4067 if (argc != 0)
4068 goto done;
4069#endif
4070
4071 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4072 * non-space. */
4073 p = strrchr(prog, '.');
4074 if (p != NULL)
4075 *p = NUL;
4076 for (progp = prog; *progp == ' '; ++progp)
4077 ;
4078
4079 /* The command line is copied to allocated memory, so that we can change
4080 * it. Add the size of the string, the separating NUL and a terminating
4081 * NUL. */
4082 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4083 if (newcmdline == NULL)
4084 return 0;
4085
4086 /*
4087 * First round: count the number of arguments ("pnew" == NULL).
4088 * Second round: produce the arguments.
4089 */
4090 for (round = 1; round <= 2; ++round)
4091 {
4092 /* First argument is the program name. */
4093 if (pnew != NULL)
4094 {
4095 argv[0] = pnew;
4096 strcpy(pnew, progp);
4097 pnew += strlen(pnew);
4098 *pnew++ = NUL;
4099 }
4100
4101 /*
4102 * Isolate each argument and put it in argv[].
4103 */
4104 p = cmdline;
4105 argc = 1;
4106 while (*p != NUL)
4107 {
4108 inquote = FALSE;
4109 if (pnew != NULL)
4110 argv[argc] = pnew;
4111 ++argc;
4112 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4113 {
4114 /* Backslashes are only special when followed by a double
4115 * quote. */
4116 i = (int)strspn(p, "\\");
4117 if (p[i] == '"')
4118 {
4119 /* Halve the number of backslashes. */
4120 if (i > 1 && pnew != NULL)
4121 {
4122 vim_memset(pnew, '\\', i / 2);
4123 pnew += i / 2;
4124 }
4125
4126 /* Even nr of backslashes toggles quoting, uneven copies
4127 * the double quote. */
4128 if ((i & 1) == 0)
4129 inquote = !inquote;
4130 else if (pnew != NULL)
4131 *pnew++ = '"';
4132 p += i + 1;
4133 }
4134 else if (i > 0)
4135 {
4136 /* Copy span of backslashes unmodified. */
4137 if (pnew != NULL)
4138 {
4139 vim_memset(pnew, '\\', i);
4140 pnew += i;
4141 }
4142 p += i;
4143 }
4144 else
4145 {
4146 if (pnew != NULL)
4147 *pnew++ = *p;
4148#ifdef FEAT_MBYTE
4149 /* Can't use mb_* functions, because 'encoding' is not
4150 * initialized yet here. */
4151 if (IsDBCSLeadByte(*p))
4152 {
4153 ++p;
4154 if (pnew != NULL)
4155 *pnew++ = *p;
4156 }
4157#endif
4158 ++p;
4159 }
4160 }
4161
4162 if (pnew != NULL)
4163 *pnew++ = NUL;
4164 while (*p == ' ' || *p == '\t')
4165 ++p; /* advance until a non-space */
4166 }
4167
4168 if (round == 1)
4169 {
4170 argv = (char **)malloc((argc + 1) * sizeof(char *));
4171 if (argv == NULL )
4172 {
4173 free(newcmdline);
4174 return 0; /* malloc error */
4175 }
4176 pnew = newcmdline;
4177 *tofree = newcmdline;
4178 }
4179 }
4180
4181#ifdef FEAT_MBYTE
4182done:
4183#endif
4184 argv[argc] = NULL; /* NULL-terminated list */
4185 *argvp = argv;
4186 return argc;
4187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189#ifdef FEAT_XPM_W32
4190# include "xpm_w32.h"
4191#endif
4192
4193#ifdef PROTO
4194# define WINAPI
4195#endif
4196
4197#ifdef __MINGW32__
4198/*
4199 * Add a lot of missing defines.
4200 * They are not always missing, we need the #ifndef's.
4201 */
4202# ifndef _cdecl
4203# define _cdecl
4204# endif
4205# ifndef IsMinimized
4206# define IsMinimized(hwnd) IsIconic(hwnd)
4207# endif
4208# ifndef IsMaximized
4209# define IsMaximized(hwnd) IsZoomed(hwnd)
4210# endif
4211# ifndef SelectFont
4212# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4213# endif
4214# ifndef GetStockBrush
4215# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4216# endif
4217# ifndef DeleteBrush
4218# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4219# endif
4220
4221# ifndef HANDLE_WM_RBUTTONDBLCLK
4222# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4223 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4224# endif
4225# ifndef HANDLE_WM_MBUTTONUP
4226# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4227 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4228# endif
4229# ifndef HANDLE_WM_MBUTTONDBLCLK
4230# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4231 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4232# endif
4233# ifndef HANDLE_WM_LBUTTONDBLCLK
4234# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4235 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4236# endif
4237# ifndef HANDLE_WM_RBUTTONDOWN
4238# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4239 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4240# endif
4241# ifndef HANDLE_WM_MOUSEMOVE
4242# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4243 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4244# endif
4245# ifndef HANDLE_WM_RBUTTONUP
4246# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4247 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4248# endif
4249# ifndef HANDLE_WM_MBUTTONDOWN
4250# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4251 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4252# endif
4253# ifndef HANDLE_WM_LBUTTONUP
4254# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4255 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4256# endif
4257# ifndef HANDLE_WM_LBUTTONDOWN
4258# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4259 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4260# endif
4261# ifndef HANDLE_WM_SYSCHAR
4262# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4263 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4264# endif
4265# ifndef HANDLE_WM_ACTIVATEAPP
4266# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4267 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4268# endif
4269# ifndef HANDLE_WM_WINDOWPOSCHANGING
4270# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4271 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4272# endif
4273# ifndef HANDLE_WM_VSCROLL
4274# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4275 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4276# endif
4277# ifndef HANDLE_WM_SETFOCUS
4278# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4279 ((fn)((hwnd), (HWND)(wParam)), 0L)
4280# endif
4281# ifndef HANDLE_WM_KILLFOCUS
4282# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4283 ((fn)((hwnd), (HWND)(wParam)), 0L)
4284# endif
4285# ifndef HANDLE_WM_HSCROLL
4286# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4287 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4288# endif
4289# ifndef HANDLE_WM_DROPFILES
4290# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4291 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4292# endif
4293# ifndef HANDLE_WM_CHAR
4294# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4295 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4296# endif
4297# ifndef HANDLE_WM_SYSDEADCHAR
4298# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4299 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4300# endif
4301# ifndef HANDLE_WM_DEADCHAR
4302# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4303 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4304# endif
4305#endif /* __MINGW32__ */
4306
4307
4308/* Some parameters for tearoff menus. All in pixels. */
4309#define TEAROFF_PADDING_X 2
4310#define TEAROFF_BUTTON_PAD_X 8
4311#define TEAROFF_MIN_WIDTH 200
4312#define TEAROFF_SUBMENU_LABEL ">>"
4313#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4314
4315
4316/* For the Intellimouse: */
4317#ifndef WM_MOUSEWHEEL
4318#define WM_MOUSEWHEEL 0x20a
4319#endif
4320
4321
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004322#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323# define ID_BEVAL_TOOLTIP 200
4324# define BEVAL_TEXT_LEN MAXPATHL
4325
Bram Moolenaar167632f2010-05-26 21:42:54 +02004326#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004327/* Work around old versions of basetsd.h which wrongly declares
4328 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004329# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004330# define UINT_PTR UINT
4331#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004334static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004336
Bram Moolenaar82881492012-11-20 16:53:39 +01004337
4338/* cproto fails on missing include files */
4339#ifndef PROTO
4340
Bram Moolenaar45360022005-07-21 21:08:21 +00004341/*
4342 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004343 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004344 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004345# include <pshpack1.h>
4346
4347#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004348
4349typedef struct _DllVersionInfo
4350{
4351 DWORD cbSize;
4352 DWORD dwMajorVersion;
4353 DWORD dwMinorVersion;
4354 DWORD dwBuildNumber;
4355 DWORD dwPlatformID;
4356} DLLVERSIONINFO;
4357
Bram Moolenaar82881492012-11-20 16:53:39 +01004358#ifndef PROTO
4359# include <poppack.h>
4360#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004361
Bram Moolenaar45360022005-07-21 21:08:21 +00004362typedef struct tagTOOLINFOA_NEW
4363{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004364 UINT cbSize;
4365 UINT uFlags;
4366 HWND hwnd;
4367 UINT_PTR uId;
4368 RECT rect;
4369 HINSTANCE hinst;
4370 LPSTR lpszText;
4371 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004372} TOOLINFO_NEW;
4373
4374typedef struct tagNMTTDISPINFO_NEW
4375{
4376 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004377 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004378 char szText[80];
4379 HINSTANCE hinst;
4380 UINT uFlags;
4381 LPARAM lParam;
4382} NMTTDISPINFO_NEW;
4383
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004384#ifdef FEAT_MBYTE
4385typedef struct tagTOOLINFOW_NEW
4386{
4387 UINT cbSize;
4388 UINT uFlags;
4389 HWND hwnd;
4390 UINT_PTR uId;
4391 RECT rect;
4392 HINSTANCE hinst;
4393 LPWSTR lpszText;
4394 LPARAM lParam;
4395 void *lpReserved;
4396} TOOLINFOW_NEW;
4397
4398typedef struct tagNMTTDISPINFOW_NEW
4399{
4400 NMHDR hdr;
4401 LPWSTR lpszText;
4402 WCHAR szText[80];
4403 HINSTANCE hinst;
4404 UINT uFlags;
4405 LPARAM lParam;
4406} NMTTDISPINFOW_NEW;
4407
4408#endif
4409
Bram Moolenaar45360022005-07-21 21:08:21 +00004410typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4411#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004412# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#endif
4414
Bram Moolenaar45360022005-07-21 21:08:21 +00004415#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004416# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004417#endif
4418
4419#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004420# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004421#endif
4422
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004423#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004424
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004425#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4426/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4427 * it here if LPNMTTDISPINFO isn't defined.
4428 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4429 * _MSC_VER. */
4430# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4431typedef struct tagNMTTDISPINFOA {
4432 NMHDR hdr;
4433 LPSTR lpszText;
4434 char szText[80];
4435 HINSTANCE hinst;
4436 UINT uFlags;
4437 LPARAM lParam;
4438} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4439# define LPNMTTDISPINFO LPNMTTDISPINFOA
4440
4441# ifdef FEAT_MBYTE
4442typedef struct tagNMTTDISPINFOW {
4443 NMHDR hdr;
4444 LPWSTR lpszText;
4445 WCHAR szText[80];
4446 HINSTANCE hinst;
4447 UINT uFlags;
4448 LPARAM lParam;
4449} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4450# endif
4451# endif
4452#endif
4453
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004454#ifndef TTN_GETDISPINFOW
4455# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4456#endif
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458/* Local variables: */
4459
4460#ifdef FEAT_MENU
4461static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464/*
4465 * Use the system font for dialogs and tear-off menus. Remove this line to
4466 * use DLG_FONT_NAME.
4467 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004468#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470#define VIM_NAME "vim"
4471#define VIM_CLASS "Vim"
4472#define VIM_CLASSW L"Vim"
4473
4474/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4475 * thus there should be room for every dialog. For tearoffs it's made bigger
4476 * when needed. */
4477#define DLG_ALLOC_SIZE 16 * 1024
4478
4479/*
4480 * stuff for dialogs, menus, tearoffs etc.
4481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482static PWORD
4483add_dialog_element(
4484 PWORD p,
4485 DWORD lStyle,
4486 WORD x,
4487 WORD y,
4488 WORD w,
4489 WORD h,
4490 WORD Id,
4491 WORD clss,
4492 const char *caption);
4493static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004494static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004495#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498static void get_dialog_font_metrics(void);
4499
4500static int dialog_default_button = -1;
4501
4502/* Intellimouse support */
4503static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504
4505static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4506#ifdef FEAT_TOOLBAR
4507static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004508static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509static int get_toolbar_bitmap(vimmenu_T *menu);
4510#endif
4511
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004512#ifdef FEAT_GUI_TABLINE
4513static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004514static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004515#endif
4516
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifdef FEAT_MBYTE_IME
4518static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4519static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4520#endif
4521#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4522# ifdef NOIME
4523typedef struct tagCOMPOSITIONFORM {
4524 DWORD dwStyle;
4525 POINT ptCurrentPos;
4526 RECT rcArea;
4527} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4528typedef HANDLE HIMC;
4529# endif
4530
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004531static HINSTANCE hLibImm = NULL;
4532static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4533static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4534static HIMC (WINAPI *pImmGetContext)(HWND);
4535static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4536static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4537static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4538static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4539static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4540static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4541static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4542static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004543static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544static void dyn_imm_load(void);
4545#else
4546# define pImmGetCompositionStringA ImmGetCompositionStringA
4547# define pImmGetCompositionStringW ImmGetCompositionStringW
4548# define pImmGetContext ImmGetContext
4549# define pImmAssociateContext ImmAssociateContext
4550# define pImmReleaseContext ImmReleaseContext
4551# define pImmGetOpenStatus ImmGetOpenStatus
4552# define pImmSetOpenStatus ImmSetOpenStatus
4553# define pImmGetCompositionFont ImmGetCompositionFontA
4554# define pImmSetCompositionFont ImmSetCompositionFontA
4555# define pImmSetCompositionWindow ImmSetCompositionWindow
4556# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004557# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#endif
4559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#ifdef FEAT_MENU
4561/*
4562 * Figure out how high the menu bar is at the moment.
4563 */
4564 static int
4565gui_mswin_get_menu_height(
4566 int fix_window) /* If TRUE, resize window if menu height changed */
4567{
4568 static int old_menu_height = -1;
4569
4570 RECT rc1, rc2;
4571 int num;
4572 int menu_height;
4573
4574 if (gui.menu_is_active)
4575 num = GetMenuItemCount(s_menuBar);
4576 else
4577 num = 0;
4578
4579 if (num == 0)
4580 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004581 else if (IsMinimized(s_hwnd))
4582 {
4583 /* The height of the menu cannot be determined while the window is
4584 * minimized. Take the previous height if the menu is changed in that
4585 * state, to avoid that Vim's vertical window size accidentally
4586 * increases due to the unaccounted-for menu height. */
4587 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 else
4590 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004591 /*
4592 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4593 * seem to have been set yet, so menu wraps in default window
4594 * width which is very narrow. Instead just return height of a
4595 * single menu item. Will still be wrong when the menu really
4596 * should wrap over more than one line.
4597 */
4598 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4599 if (gui.starting)
4600 menu_height = rc1.bottom - rc1.top + 1;
4601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004603 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4604 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 }
4606 }
4607
4608 if (fix_window && menu_height != old_menu_height)
4609 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004610 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004612 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613
4614 return menu_height;
4615}
4616#endif /*FEAT_MENU*/
4617
4618
4619/*
4620 * Setup for the Intellimouse
4621 */
4622 static void
4623init_mouse_wheel(void)
4624{
4625
4626#ifndef SPI_GETWHEELSCROLLLINES
4627# define SPI_GETWHEELSCROLLLINES 104
4628#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004629#ifndef SPI_SETWHEELSCROLLLINES
4630# define SPI_SETWHEELSCROLLLINES 105
4631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4634#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4635#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4636#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 mouse_scroll_lines = 3; /* reasonable default */
4639
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004640 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4641 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4642 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643}
4644
4645
4646/* Intellimouse wheel handler */
4647 static void
4648_OnMouseWheel(
4649 HWND hwnd,
4650 short zDelta)
4651{
4652/* Treat a mouse wheel event as if it were a scroll request */
4653 int i;
4654 int size;
4655 HWND hwndCtl;
4656
4657 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4658 {
4659 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4660 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4661 }
4662 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4663 {
4664 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4665 size = curwin->w_scrollbars[SBAR_LEFT].size;
4666 }
4667 else
4668 return;
4669
4670 size = curwin->w_height;
4671 if (mouse_scroll_lines == 0)
4672 init_mouse_wheel();
4673
Bram Moolenaara338adc2018-01-31 20:51:47 +01004674 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 if (mouse_scroll_lines > 0
4676 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4677 {
4678 for (i = mouse_scroll_lines; i > 0; --i)
4679 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4680 }
4681 else
4682 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004683 mch_enable_flush();
4684 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685}
4686
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004687#ifdef USE_SYSMENU_FONT
4688/*
4689 * Get Menu Font.
4690 * Return OK or FAIL.
4691 */
4692 static int
4693gui_w32_get_menu_font(LOGFONT *lf)
4694{
4695 NONCLIENTMETRICS nm;
4696
4697 nm.cbSize = sizeof(NONCLIENTMETRICS);
4698 if (!SystemParametersInfo(
4699 SPI_GETNONCLIENTMETRICS,
4700 sizeof(NONCLIENTMETRICS),
4701 &nm,
4702 0))
4703 return FAIL;
4704 *lf = nm.lfMenuFont;
4705 return OK;
4706}
4707#endif
4708
4709
4710#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4711/*
4712 * Set the GUI tabline font to the system menu font
4713 */
4714 static void
4715set_tabline_font(void)
4716{
4717 LOGFONT lfSysmenu;
4718 HFONT font;
4719 HWND hwnd;
4720 HDC hdc;
4721 HFONT hfntOld;
4722 TEXTMETRIC tm;
4723
4724 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4725 return;
4726
4727 font = CreateFontIndirect(&lfSysmenu);
4728
4729 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4730
4731 /*
4732 * Compute the height of the font used for the tab text
4733 */
4734 hwnd = GetDesktopWindow();
4735 hdc = GetWindowDC(hwnd);
4736 hfntOld = SelectFont(hdc, font);
4737
4738 GetTextMetrics(hdc, &tm);
4739
4740 SelectFont(hdc, hfntOld);
4741 ReleaseDC(hwnd, hdc);
4742
4743 /*
4744 * The space used by the tab border and the space between the tab label
4745 * and the tab border is included as 7.
4746 */
4747 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4748}
4749#endif
4750
Bram Moolenaar520470a2005-06-16 21:59:56 +00004751/*
4752 * Invoked when a setting was changed.
4753 */
4754 static LRESULT CALLBACK
4755_OnSettingChange(UINT n)
4756{
4757 if (n == SPI_SETWHEELSCROLLLINES)
4758 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4759 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004760#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4761 if (n == SPI_SETNONCLIENTMETRICS)
4762 set_tabline_font();
4763#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004764 return 0;
4765}
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#ifdef FEAT_NETBEANS_INTG
4768 static void
4769_OnWindowPosChanged(
4770 HWND hwnd,
4771 const LPWINDOWPOS lpwpos)
4772{
4773 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004774 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4777 || lpwpos->cx != cx || lpwpos->cy != cy))
4778 {
4779 x = lpwpos->x;
4780 y = lpwpos->y;
4781 cx = lpwpos->cx;
4782 cy = lpwpos->cy;
4783 netbeans_frame_moved(x, y);
4784 }
4785 /* Allow to send WM_SIZE and WM_MOVE */
4786 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4787}
4788#endif
4789
4790 static int
4791_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 UINT fwSide,
4793 LPRECT lprc)
4794{
4795 int w, h;
4796 int valid_w, valid_h;
4797 int w_offset, h_offset;
4798
4799 w = lprc->right - lprc->left;
4800 h = lprc->bottom - lprc->top;
4801 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4802 w_offset = w - valid_w;
4803 h_offset = h - valid_h;
4804
4805 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4806 || fwSide == WMSZ_BOTTOMLEFT)
4807 lprc->left += w_offset;
4808 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4809 || fwSide == WMSZ_BOTTOMRIGHT)
4810 lprc->right -= w_offset;
4811
4812 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4813 || fwSide == WMSZ_TOPRIGHT)
4814 lprc->top += h_offset;
4815 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4816 || fwSide == WMSZ_BOTTOMRIGHT)
4817 lprc->bottom -= h_offset;
4818 return TRUE;
4819}
4820
4821
4822
4823 static LRESULT CALLBACK
4824_WndProc(
4825 HWND hwnd,
4826 UINT uMsg,
4827 WPARAM wParam,
4828 LPARAM lParam)
4829{
4830 /*
4831 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4832 hwnd, uMsg, wParam, lParam);
4833 */
4834
4835 HandleMouseHide(uMsg, lParam);
4836
4837 s_uMsg = uMsg;
4838 s_wParam = wParam;
4839 s_lParam = lParam;
4840
4841 switch (uMsg)
4842 {
4843 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4844 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4845 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4846 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4847 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4848 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4849 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4850 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4851 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4852#ifdef FEAT_MENU
4853 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4854#endif
4855 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4856 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4857 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4858 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4859 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4860 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4861 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4862 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4863 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4864#ifdef FEAT_NETBEANS_INTG
4865 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4866#endif
4867
Bram Moolenaarafa24992006-03-27 20:58:26 +00004868#ifdef FEAT_GUI_TABLINE
4869 case WM_RBUTTONUP:
4870 {
4871 if (gui_mch_showing_tabline())
4872 {
4873 POINT pt;
4874 RECT rect;
4875
4876 /*
4877 * If the cursor is on the tabline, display the tab menu
4878 */
4879 GetCursorPos((LPPOINT)&pt);
4880 GetWindowRect(s_textArea, &rect);
4881 if (pt.y < rect.top)
4882 {
4883 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004884 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004885 }
4886 }
4887 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4888 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004889 case WM_LBUTTONDBLCLK:
4890 {
4891 /*
4892 * If the user double clicked the tabline, create a new tab
4893 */
4894 if (gui_mch_showing_tabline())
4895 {
4896 POINT pt;
4897 RECT rect;
4898
4899 GetCursorPos((LPPOINT)&pt);
4900 GetWindowRect(s_textArea, &rect);
4901 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004902 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004903 }
4904 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4905 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004906#endif
4907
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 case WM_QUERYENDSESSION: /* System wants to go down. */
4909 gui_shell_closed(); /* Will exit when no changed buffers. */
4910 return FALSE; /* Do NOT allow system to go down. */
4911
4912 case WM_ENDSESSION:
4913 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004916 return 0L;
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 break;
4919
4920 case WM_CHAR:
4921 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4922 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004923 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return 0L;
4925
4926 case WM_SYSCHAR:
4927 /*
4928 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4929 * shortcut key, handle like a typed ALT key, otherwise call Windows
4930 * ALT key handling.
4931 */
4932#ifdef FEAT_MENU
4933 if ( !gui.menu_is_active
4934 || p_wak[0] == 'n'
4935 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4936 )
4937#endif
4938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004939 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 return 0L;
4941 }
4942#ifdef FEAT_MENU
4943 else
4944 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4945#endif
4946
4947 case WM_SYSKEYUP:
4948#ifdef FEAT_MENU
4949 /* This used to be done only when menu is active: ALT key is used for
4950 * that. But that caused problems when menu is disabled and using
4951 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4952 * are received, mouse pointer remains hidden. */
4953 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4954#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004955 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#endif
4957
4958 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004959 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
4961 case WM_MOUSEWHEEL:
4962 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004963 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
Bram Moolenaar520470a2005-06-16 21:59:56 +00004965 /* Notification for change in SystemParametersInfo() */
4966 case WM_SETTINGCHANGE:
4967 return _OnSettingChange((UINT)wParam);
4968
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004969#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 case WM_NOTIFY:
4971 switch (((LPNMHDR) lParam)->code)
4972 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004973# ifdef FEAT_MBYTE
4974 case TTN_GETDISPINFOW:
4975# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004976 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004978 LPNMHDR hdr = (LPNMHDR)lParam;
4979 char_u *str = NULL;
4980 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004981
Bram Moolenaard23a8232018-02-10 18:45:26 +01004982 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004983
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004984# ifdef FEAT_GUI_TABLINE
4985 if (gui_mch_showing_tabline()
4986 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004987 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004988 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004989 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004990 * Mouse is over the GUI tabline. Display the
4991 * tooltip for the tab under the cursor
4992 *
4993 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004994 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004995 GetCursorPos(&pt);
4996 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004997 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004998 TCHITTESTINFO htinfo;
4999 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005000
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005001 /*
5002 * Get the tab under the cursor
5003 */
5004 htinfo.pt.x = pt.x;
5005 htinfo.pt.y = pt.y;
5006 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5007 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005008 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005009 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005010
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005011 tp = find_tabpage(idx + 1);
5012 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005013 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005014 get_tabline_label(tp, TRUE);
5015 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005016 }
5017 }
5018 }
5019 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005020# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005021# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005022# ifdef FEAT_GUI_TABLINE
5023 else
5024# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005026 UINT idButton;
5027 vimmenu_T *pMenu;
5028
5029 idButton = (UINT) hdr->idFrom;
5030 pMenu = gui_mswin_find_menu(root_menu, idButton);
5031 if (pMenu)
5032 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005034# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005035 if (str != NULL)
5036 {
5037# ifdef FEAT_MBYTE
5038 if (hdr->code == TTN_GETDISPINFOW)
5039 {
5040 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5041
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005042 /* Set the maximum width, this also enables using
5043 * \n for line break. */
5044 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5045 0, 500);
5046
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005047 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005048 lpdi->lpszText = tt_text;
5049 /* can't show tooltip if failed */
5050 }
5051 else
5052# endif
5053 {
5054 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5055
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005056 /* Set the maximum width, this also enables using
5057 * \n for line break. */
5058 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5059 0, 500);
5060
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005061 if (STRLEN(str) < sizeof(lpdi->szText)
5062 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005063 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005064 sizeof(lpdi->szText) - 1);
5065 else
5066 lpdi->lpszText = tt_text;
5067 }
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005071# ifdef FEAT_GUI_TABLINE
5072 case TCN_SELCHANGE:
5073 if (gui_mch_showing_tabline()
5074 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005075 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005076 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005077 return 0L;
5078 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005079 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005080
5081 case NM_RCLICK:
5082 if (gui_mch_showing_tabline()
5083 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005084 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005085 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005086 return 0L;
5087 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005088 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005089# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005091# ifdef FEAT_GUI_TABLINE
5092 if (gui_mch_showing_tabline()
5093 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5094 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 break;
5097 }
5098 break;
5099#endif
5100#if defined(MENUHINTS) && defined(FEAT_MENU)
5101 case WM_MENUSELECT:
5102 if (((UINT) HIWORD(wParam)
5103 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5104 == MF_HILITE
5105 && (State & CMDLINE) == 0)
5106 {
5107 UINT idButton;
5108 vimmenu_T *pMenu;
5109 static int did_menu_tip = FALSE;
5110
5111 if (did_menu_tip)
5112 {
5113 msg_clr_cmdline();
5114 setcursor();
5115 out_flush();
5116 did_menu_tip = FALSE;
5117 }
5118
5119 idButton = (UINT)LOWORD(wParam);
5120 pMenu = gui_mswin_find_menu(root_menu, idButton);
5121 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5122 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5123 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005124 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01005125 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005126 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 setcursor();
5128 out_flush();
5129 did_menu_tip = TRUE;
5130 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005131 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 break;
5134#endif
5135 case WM_NCHITTEST:
5136 {
5137 LRESULT result;
5138 int x, y;
5139 int xPos = GET_X_LPARAM(lParam);
5140
5141 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5142 if (result == HTCLIENT)
5143 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005144#ifdef FEAT_GUI_TABLINE
5145 if (gui_mch_showing_tabline())
5146 {
5147 int yPos = GET_Y_LPARAM(lParam);
5148 RECT rct;
5149
5150 /* If the cursor is on the GUI tabline, don't process this
5151 * event */
5152 GetWindowRect(s_textArea, &rct);
5153 if (yPos < rct.top)
5154 return result;
5155 }
5156#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005157 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 xPos -= x;
5159
5160 if (xPos < 48) /* <VN> TODO should use system metric? */
5161 return HTBOTTOMLEFT;
5162 else
5163 return HTBOTTOMRIGHT;
5164 }
5165 else
5166 return result;
5167 }
5168 /* break; notreached */
5169
5170#ifdef FEAT_MBYTE_IME
5171 case WM_IME_NOTIFY:
5172 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5173 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005174 return 1L;
5175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 case WM_IME_COMPOSITION:
5177 if (!_OnImeComposition(hwnd, wParam, lParam))
5178 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005179 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#endif
5181
5182 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005184 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
5186 _OnFindRepl();
5187 }
5188#endif
5189 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5190 }
5191
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005192 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193}
5194
5195/*
5196 * End of call-back routines
5197 */
5198
5199/* parent window, if specified with -P */
5200HWND vim_parent_hwnd = NULL;
5201
5202 static BOOL CALLBACK
5203FindWindowTitle(HWND hwnd, LPARAM lParam)
5204{
5205 char buf[2048];
5206 char *title = (char *)lParam;
5207
5208 if (GetWindowText(hwnd, buf, sizeof(buf)))
5209 {
5210 if (strstr(buf, title) != NULL)
5211 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005212 /* Found it. Store the window ref. and quit searching if MDI
5213 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005215 if (vim_parent_hwnd != NULL)
5216 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218 }
5219 return TRUE; /* continue searching */
5220}
5221
5222/*
5223 * Invoked for '-P "title"' argument: search for parent application to open
5224 * our window in.
5225 */
5226 void
5227gui_mch_set_parent(char *title)
5228{
5229 EnumWindows(FindWindowTitle, (LPARAM)title);
5230 if (vim_parent_hwnd == NULL)
5231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005232 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 mch_exit(2);
5234 }
5235}
5236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 static void
5239ole_error(char *arg)
5240{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005241 char buf[IOSIZE];
5242
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005243 /* Can't use emsg() here, we have not finished initialisation yet. */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005244 vim_snprintf(buf, IOSIZE,
5245 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5246 arg);
5247 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251/*
5252 * Parse the GUI related command-line arguments. Any arguments used are
5253 * deleted from argv, and *argc is decremented accordingly. This is called
5254 * when vim is started, whether or not the GUI has been started.
5255 */
5256 void
5257gui_mch_prepare(int *argc, char **argv)
5258{
5259 int silent = FALSE;
5260 int idx;
5261
5262 /* Check for special OLE command line parameters */
5263 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5264 {
5265 /* Check for a "-silent" argument first. */
5266 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5267 && (argv[2][0] == '-' || argv[2][0] == '/'))
5268 {
5269 silent = TRUE;
5270 idx = 2;
5271 }
5272 else
5273 idx = 1;
5274
5275 /* Register Vim as an OLE Automation server */
5276 if (STRICMP(argv[idx] + 1, "register") == 0)
5277 {
5278#ifdef FEAT_OLE
5279 RegisterMe(silent);
5280 mch_exit(0);
5281#else
5282 if (!silent)
5283 ole_error("register");
5284 mch_exit(2);
5285#endif
5286 }
5287
5288 /* Unregister Vim as an OLE Automation server */
5289 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5290 {
5291#ifdef FEAT_OLE
5292 UnregisterMe(!silent);
5293 mch_exit(0);
5294#else
5295 if (!silent)
5296 ole_error("unregister");
5297 mch_exit(2);
5298#endif
5299 }
5300
5301 /* Ignore an -embedding argument. It is only relevant if the
5302 * application wants to treat the case when it is started manually
5303 * differently from the case where it is started via automation (and
5304 * we don't).
5305 */
5306 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5307 {
5308#ifdef FEAT_OLE
5309 *argc = 1;
5310#else
5311 ole_error("embedding");
5312 mch_exit(2);
5313#endif
5314 }
5315 }
5316
5317#ifdef FEAT_OLE
5318 {
5319 int bDoRestart = FALSE;
5320
5321 InitOLE(&bDoRestart);
5322 /* automatically exit after registering */
5323 if (bDoRestart)
5324 mch_exit(0);
5325 }
5326#endif
5327
5328#ifdef FEAT_NETBEANS_INTG
5329 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005330 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 int arg;
5332
5333 for (arg = 1; arg < *argc; arg++)
5334 if (strncmp("-nb", argv[arg], 3) == 0)
5335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 netbeansArg = argv[arg];
5337 mch_memmove(&argv[arg], &argv[arg + 1],
5338 (--*argc - arg) * sizeof(char *));
5339 argv[*argc] = NULL;
5340 break; /* enough? */
5341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344}
5345
5346/*
5347 * Initialise the GUI. Create all the windows, set up all the call-backs
5348 * etc.
5349 */
5350 int
5351gui_mch_init(void)
5352{
5353 const char szVimWndClass[] = VIM_CLASS;
5354 const char szTextAreaClass[] = "VimTextArea";
5355 WNDCLASS wndclass;
5356#ifdef FEAT_MBYTE
5357 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005358 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 WNDCLASSW wndclassw;
5360#endif
5361#ifdef GLOBAL_IME
5362 ATOM atom;
5363#endif
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 /* Return here if the window was already opened (happens when
5366 * gui_mch_dialog() is called early). */
5367 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005368 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
5370 /*
5371 * Load the tearoff bitmap
5372 */
5373#ifdef FEAT_TEAROFF
5374 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5375#endif
5376
5377 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5378 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5379#ifdef FEAT_MENU
5380 gui.menu_height = 0; /* Windows takes care of this */
5381#endif
5382 gui.border_width = 0;
5383
5384 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5385
5386#ifdef FEAT_MBYTE
5387 /* First try using the wide version, so that we can use any title.
5388 * Otherwise only characters in the active codepage will work. */
5389 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5390 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005391 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 wndclassw.lpfnWndProc = _WndProc;
5393 wndclassw.cbClsExtra = 0;
5394 wndclassw.cbWndExtra = 0;
5395 wndclassw.hInstance = s_hinst;
5396 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5397 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5398 wndclassw.hbrBackground = s_brush;
5399 wndclassw.lpszMenuName = NULL;
5400 wndclassw.lpszClassName = szVimWndClassW;
5401
5402 if ((
5403#ifdef GLOBAL_IME
5404 atom =
5405#endif
5406 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
5409 wide_WindowProc = TRUE;
5410 }
5411
5412 if (!wide_WindowProc)
5413#endif
5414
5415 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5416 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005417 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 wndclass.lpfnWndProc = _WndProc;
5419 wndclass.cbClsExtra = 0;
5420 wndclass.cbWndExtra = 0;
5421 wndclass.hInstance = s_hinst;
5422 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5423 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5424 wndclass.hbrBackground = s_brush;
5425 wndclass.lpszMenuName = NULL;
5426 wndclass.lpszClassName = szVimWndClass;
5427
5428 if ((
5429#ifdef GLOBAL_IME
5430 atom =
5431#endif
5432 RegisterClass(&wndclass)) == 0)
5433 return FAIL;
5434 }
5435
5436 if (vim_parent_hwnd != NULL)
5437 {
5438#ifdef HAVE_TRY_EXCEPT
5439 __try
5440 {
5441#endif
5442 /* Open inside the specified parent window.
5443 * TODO: last argument should point to a CLIENTCREATESTRUCT
5444 * structure. */
5445 s_hwnd = CreateWindowEx(
5446 WS_EX_MDICHILD,
5447 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005448 WS_OVERLAPPEDWINDOW | WS_CHILD
5449 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5451 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5452 100, /* Any value will do */
5453 100, /* Any value will do */
5454 vim_parent_hwnd, NULL,
5455 s_hinst, NULL);
5456#ifdef HAVE_TRY_EXCEPT
5457 }
5458 __except(EXCEPTION_EXECUTE_HANDLER)
5459 {
5460 /* NOP */
5461 }
5462#endif
5463 if (s_hwnd == NULL)
5464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005465 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 mch_exit(2);
5467 }
5468 }
5469 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005470 {
5471 /* If the provided windowid is not valid reset it to zero, so that it
5472 * is ignored and we open our own window. */
5473 if (IsWindow((HWND)win_socket_id) <= 0)
5474 win_socket_id = 0;
5475
5476 /* Create a window. If win_socket_id is not zero without border and
5477 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005479 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005480 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5481 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005482 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5483 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5484 100, /* Any value will do */
5485 100, /* Any value will do */
5486 NULL, NULL,
5487 s_hinst, NULL);
5488 if (s_hwnd != NULL && win_socket_id != 0)
5489 {
5490 SetParent(s_hwnd, (HWND)win_socket_id);
5491 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5492 }
5493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 if (s_hwnd == NULL)
5496 return FAIL;
5497
5498#ifdef GLOBAL_IME
5499 global_ime_init(atom, s_hwnd);
5500#endif
5501#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5502 dyn_imm_load();
5503#endif
5504
5505 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005506#ifdef FEAT_MBYTE
5507 if (wide_WindowProc)
5508 {
5509 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5510 {
5511 wndclassw.style = CS_OWNDC;
5512 wndclassw.lpfnWndProc = _TextAreaWndProc;
5513 wndclassw.cbClsExtra = 0;
5514 wndclassw.cbWndExtra = 0;
5515 wndclassw.hInstance = s_hinst;
5516 wndclassw.hIcon = NULL;
5517 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5518 wndclassw.hbrBackground = NULL;
5519 wndclassw.lpszMenuName = NULL;
5520 wndclassw.lpszClassName = szTextAreaClassW;
5521
5522 if (RegisterClassW(&wndclassw) == 0)
5523 return FAIL;
5524 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005525
5526 s_textArea = CreateWindowExW(
5527 0,
5528 szTextAreaClassW, L"Vim text area",
5529 WS_CHILD | WS_VISIBLE, 0, 0,
5530 100, // Any value will do for now
5531 100, // Any value will do for now
5532 s_hwnd, NULL,
5533 s_hinst, NULL);
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005534 }
5535 else
5536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5538 {
5539 wndclass.style = CS_OWNDC;
5540 wndclass.lpfnWndProc = _TextAreaWndProc;
5541 wndclass.cbClsExtra = 0;
5542 wndclass.cbWndExtra = 0;
5543 wndclass.hInstance = s_hinst;
5544 wndclass.hIcon = NULL;
5545 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5546 wndclass.hbrBackground = NULL;
5547 wndclass.lpszMenuName = NULL;
5548 wndclass.lpszClassName = szTextAreaClass;
5549
5550 if (RegisterClass(&wndclass) == 0)
5551 return FAIL;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005552
5553 s_textArea = CreateWindowEx(
5554 0,
5555 szTextAreaClass, "Vim text area",
5556 WS_CHILD | WS_VISIBLE, 0, 0,
5557 100, // Any value will do for now
5558 100, // Any value will do for now
5559 s_hwnd, NULL,
5560 s_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 if (s_textArea == NULL)
5564 return FAIL;
5565
Bram Moolenaar20321902016-02-17 12:30:17 +01005566#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005567 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5568 {
5569 HANDLE hIcon = NULL;
5570
5571 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005572 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005573 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005574#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576#ifdef FEAT_MENU
5577 s_menuBar = CreateMenu();
5578#endif
5579 s_hdc = GetDC(s_textArea);
5580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 /* Do we need to bother with this? */
5584 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5585
5586 /* Get background/foreground colors from the system */
5587 gui_mch_def_colors();
5588
5589 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5590 * file) */
5591 set_normal_colors();
5592
5593 /*
5594 * Check that none of the colors are the same as the background color.
5595 * Then store the current values as the defaults.
5596 */
5597 gui_check_colors();
5598 gui.def_norm_pixel = gui.norm_pixel;
5599 gui.def_back_pixel = gui.back_pixel;
5600
5601 /* Get the colors for the highlight groups (gui_check_colors() might have
5602 * changed them) */
5603 highlight_gui_started();
5604
5605 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005606 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005608 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
5610 /*
5611 * Set up for Intellimouse processing
5612 */
5613 init_mouse_wheel();
5614
5615 /*
5616 * compute a couple of metrics used for the dialogs
5617 */
5618 get_dialog_font_metrics();
5619#ifdef FEAT_TOOLBAR
5620 /*
5621 * Create the toolbar
5622 */
5623 initialise_toolbar();
5624#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005625#ifdef FEAT_GUI_TABLINE
5626 /*
5627 * Create the tabline
5628 */
5629 initialise_tabline();
5630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631#ifdef MSWIN_FIND_REPLACE
5632 /*
5633 * Initialise the dialog box stuff
5634 */
5635 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5636
5637 /* Initialise the struct */
5638 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005639 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005641 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5643 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5644 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005645# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005646 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5647 s_findrep_struct_w.lpstrFindWhat =
5648 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5649 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5650 s_findrep_struct_w.lpstrReplaceWith =
5651 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5652 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5653 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5654 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005658#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005659# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5660/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5661# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005662# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005663# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005664# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005665 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005666 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005667#endif
5668
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005669#ifdef FEAT_RENDER_OPTIONS
5670 if (p_rop)
5671 (void)gui_mch_set_rendering_options(p_rop);
5672#endif
5673
Bram Moolenaar748bf032005-02-02 23:04:36 +00005674theend:
5675 /* Display any pending error messages */
5676 display_errors();
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 return OK;
5679}
5680
5681/*
5682 * Get the size of the screen, taking position on multiple monitors into
5683 * account (if supported).
5684 */
5685 static void
5686get_work_area(RECT *spi_rect)
5687{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005688 HMONITOR mon;
5689 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005691 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005692 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005693 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005695 moninfo.cbSize = sizeof(MONITORINFO);
5696 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005698 *spi_rect = moninfo.rcWork;
5699 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
5701 }
5702 /* this is the old method... */
5703 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5704}
5705
5706/*
5707 * Set the size of the window to the given width and height in pixels.
5708 */
5709 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005710gui_mch_set_shellsize(
5711 int width,
5712 int height,
5713 int min_width UNUSED,
5714 int min_height UNUSED,
5715 int base_width UNUSED,
5716 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005717 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 RECT workarea_rect;
5720 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 WINDOWPLACEMENT wndpl;
5722
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005723 /* Try to keep window completely on screen. */
5724 /* Get position of the screen work area. This is the part that is not
5725 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 get_work_area(&workarea_rect);
5727
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005728 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005729 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 wndpl.length = sizeof(WINDOWPLACEMENT);
5731 GetWindowPlacement(s_hwnd, &wndpl);
5732
5733 /* Resizing a maximized window looks very strange, unzoom it first.
5734 * But don't do it when still starting up, it may have been requested in
5735 * the shortcut. */
5736 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5737 {
5738 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5739 /* Need to get the settings of the normal window. */
5740 GetWindowPlacement(s_hwnd, &wndpl);
5741 }
5742
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005744 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005745 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005746 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005747 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 + GetSystemMetrics(SM_CYCAPTION)
5749#ifdef FEAT_MENU
5750 + gui_mswin_get_menu_height(FALSE)
5751#endif
5752 ;
5753
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005754 /* The following should take care of keeping Vim on the same monitor, no
5755 * matter if the secondary monitor is left or right of the primary
5756 * monitor. */
5757 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5758 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005759
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005760 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005761 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005762 && wndpl.rcNormalPosition.right > workarea_rect.right)
5763 OffsetRect(&wndpl.rcNormalPosition,
5764 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005766 if ((direction & RESIZE_HOR)
5767 && wndpl.rcNormalPosition.left < workarea_rect.left)
5768 OffsetRect(&wndpl.rcNormalPosition,
5769 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
Bram Moolenaarafa24992006-03-27 20:58:26 +00005771 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005772 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5773 OffsetRect(&wndpl.rcNormalPosition,
5774 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005776 if ((direction & RESIZE_VERT)
5777 && wndpl.rcNormalPosition.top < workarea_rect.top)
5778 OffsetRect(&wndpl.rcNormalPosition,
5779 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 /* set window position - we should use SetWindowPlacement rather than
5782 * SetWindowPos as the MSDN docs say the coord systems returned by
5783 * these two are not compatible. */
5784 SetWindowPlacement(s_hwnd, &wndpl);
5785
5786 SetActiveWindow(s_hwnd);
5787 SetFocus(s_hwnd);
5788
5789#ifdef FEAT_MENU
5790 /* Menu may wrap differently now */
5791 gui_mswin_get_menu_height(!gui.starting);
5792#endif
5793}
5794
5795
5796 void
5797gui_mch_set_scrollbar_thumb(
5798 scrollbar_T *sb,
5799 long val,
5800 long size,
5801 long max)
5802{
5803 SCROLLINFO info;
5804
5805 sb->scroll_shift = 0;
5806 while (max > 32767)
5807 {
5808 max = (max + 1) >> 1;
5809 val >>= 1;
5810 size >>= 1;
5811 ++sb->scroll_shift;
5812 }
5813
5814 if (sb->scroll_shift > 0)
5815 ++size;
5816
5817 info.cbSize = sizeof(info);
5818 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5819 info.nPos = val;
5820 info.nMin = 0;
5821 info.nMax = max;
5822 info.nPage = size;
5823 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5824}
5825
5826
5827/*
5828 * Set the current text font.
5829 */
5830 void
5831gui_mch_set_font(GuiFont font)
5832{
5833 gui.currFont = font;
5834}
5835
5836
5837/*
5838 * Set the current text foreground color.
5839 */
5840 void
5841gui_mch_set_fg_color(guicolor_T color)
5842{
5843 gui.currFgColor = color;
5844}
5845
5846/*
5847 * Set the current text background color.
5848 */
5849 void
5850gui_mch_set_bg_color(guicolor_T color)
5851{
5852 gui.currBgColor = color;
5853}
5854
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005855/*
5856 * Set the current text special color.
5857 */
5858 void
5859gui_mch_set_sp_color(guicolor_T color)
5860{
5861 gui.currSpColor = color;
5862}
5863
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005864#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865/*
5866 * Multi-byte handling, originally by Sung-Hoon Baek.
5867 * First static functions (no prototypes generated).
5868 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005869# ifdef _MSC_VER
5870# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5871# endif
5872# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 * handle WM_IME_NOTIFY message
5876 */
5877 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005878_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
5880 LRESULT lResult = 0;
5881 HIMC hImc;
5882
5883 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5884 return lResult;
5885 switch (dwCommand)
5886 {
5887 case IMN_SETOPENSTATUS:
5888 if (pImmGetOpenStatus(hImc))
5889 {
5890 pImmSetCompositionFont(hImc, &norm_logfont);
5891 im_set_position(gui.row, gui.col);
5892
5893 /* Disable langmap */
5894 State &= ~LANGMAP;
5895 if (State & INSERT)
5896 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005897#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 /* Unshown 'keymap' in status lines */
5899 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5900 {
5901 /* Save cursor position */
5902 int old_row = gui.row;
5903 int old_col = gui.col;
5904
5905 // This must be called here before
5906 // status_redraw_curbuf(), otherwise the mode
5907 // message may appear in the wrong position.
5908 showmode();
5909 status_redraw_curbuf();
5910 update_screen(0);
5911 /* Restore cursor position */
5912 gui.row = old_row;
5913 gui.col = old_col;
5914 }
5915#endif
5916 }
5917 }
5918 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005919 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 lResult = 0;
5921 break;
5922 }
5923 pImmReleaseContext(hWnd, hImc);
5924 return lResult;
5925}
5926
5927 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005928_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 char_u *ret;
5931 int len;
5932
5933 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5934 return 0;
5935
5936 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5937 if (ret != NULL)
5938 {
5939 add_to_input_buf_csi(ret, len);
5940 vim_free(ret);
5941 return 1;
5942 }
5943 return 0;
5944}
5945
5946/*
5947 * get the current composition string, in UCS-2; *lenp is the number of
5948 * *lenp is the number of Unicode characters.
5949 */
5950 static short_u *
5951GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5952{
5953 LONG ret;
5954 LPWSTR wbuf = NULL;
5955 char_u *buf;
5956
5957 if (!pImmGetContext)
5958 return NULL; /* no imm32.dll */
5959
5960 /* Try Unicode; this'll always work on NT regardless of codepage. */
5961 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5962 if (ret == 0)
5963 return NULL; /* empty */
5964
5965 if (ret > 0)
5966 {
5967 /* Allocate the requested buffer plus space for the NUL character. */
5968 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5969 if (wbuf != NULL)
5970 {
5971 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5972 *lenp = ret / sizeof(WCHAR);
5973 }
5974 return (short_u *)wbuf;
5975 }
5976
5977 /* ret < 0; we got an error, so try the ANSI version. This'll work
5978 * on 9x/ME, but only if the codepage happens to be set to whatever
5979 * we're inputting. */
5980 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5981 if (ret <= 0)
5982 return NULL; /* empty or error */
5983
5984 buf = alloc(ret);
5985 if (buf == NULL)
5986 return NULL;
5987 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5988
5989 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005990 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 vim_free(buf);
5992
5993 return (short_u *)wbuf;
5994}
5995
5996/*
5997 * void GetResultStr()
5998 *
5999 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6000 * get complete composition string
6001 */
6002 static char_u *
6003GetResultStr(HWND hwnd, int GCS, int *lenp)
6004{
6005 HIMC hIMC; /* Input context handle. */
6006 short_u *buf = NULL;
6007 char_u *convbuf = NULL;
6008
6009 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6010 return NULL;
6011
6012 /* Reads in the composition string. */
6013 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6014 if (buf == NULL)
6015 return NULL;
6016
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006017 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 pImmReleaseContext(hwnd, hIMC);
6019 vim_free(buf);
6020 return convbuf;
6021}
6022#endif
6023
6024/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006025#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026
6027/*
6028 * set font to IM.
6029 */
6030 void
6031im_set_font(LOGFONT *lf)
6032{
6033 HIMC hImc;
6034
6035 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6036 {
6037 pImmSetCompositionFont(hImc, lf);
6038 pImmReleaseContext(s_hwnd, hImc);
6039 }
6040}
6041
6042/*
6043 * Notify cursor position to IM.
6044 */
6045 void
6046im_set_position(int row, int col)
6047{
6048 HIMC hImc;
6049
6050 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6051 {
6052 COMPOSITIONFORM cfs;
6053
6054 cfs.dwStyle = CFS_POINT;
6055 cfs.ptCurrentPos.x = FILL_X(col);
6056 cfs.ptCurrentPos.y = FILL_Y(row);
6057 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6058 pImmSetCompositionWindow(hImc, &cfs);
6059
6060 pImmReleaseContext(s_hwnd, hImc);
6061 }
6062}
6063
6064/*
6065 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6066 */
6067 void
6068im_set_active(int active)
6069{
6070 HIMC hImc;
6071 static HIMC hImcOld = (HIMC)0;
6072
6073 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6074 {
6075 if (p_imdisable)
6076 {
6077 if (hImcOld == (HIMC)0)
6078 {
6079 hImcOld = pImmGetContext(s_hwnd);
6080 if (hImcOld)
6081 pImmAssociateContext(s_hwnd, (HIMC)0);
6082 }
6083 active = FALSE;
6084 }
6085 else if (hImcOld != (HIMC)0)
6086 {
6087 pImmAssociateContext(s_hwnd, hImcOld);
6088 hImcOld = (HIMC)0;
6089 }
6090
6091 hImc = pImmGetContext(s_hwnd);
6092 if (hImc)
6093 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006094 /*
6095 * for Korean ime
6096 */
6097 HKL hKL = GetKeyboardLayout(0);
6098
6099 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6100 {
6101 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6102 static BOOL bSaved = FALSE;
6103
6104 if (active)
6105 {
6106 /* if we have a saved conversion status, restore it */
6107 if (bSaved)
6108 pImmSetConversionStatus(hImc, dwConversionSaved,
6109 dwSentenceSaved);
6110 bSaved = FALSE;
6111 }
6112 else
6113 {
6114 /* save conversion status and disable korean */
6115 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6116 &dwSentenceSaved))
6117 {
6118 bSaved = TRUE;
6119 pImmSetConversionStatus(hImc,
6120 dwConversionSaved & ~(IME_CMODE_NATIVE
6121 | IME_CMODE_FULLSHAPE),
6122 dwSentenceSaved);
6123 }
6124 }
6125 }
6126
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 pImmSetOpenStatus(hImc, active);
6128 pImmReleaseContext(s_hwnd, hImc);
6129 }
6130 }
6131}
6132
6133/*
6134 * Get IM status. When IM is on, return not 0. Else return 0.
6135 */
6136 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006137im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 int status = 0;
6140 HIMC hImc;
6141
6142 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6143 {
6144 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6145 pImmReleaseContext(s_hwnd, hImc);
6146 }
6147 return status;
6148}
6149
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006150#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6153/* Win32 with GLOBAL IME */
6154
6155/*
6156 * Notify cursor position to IM.
6157 */
6158 void
6159im_set_position(int row, int col)
6160{
6161 /* Win32 with GLOBAL IME */
6162 POINT p;
6163
6164 p.x = FILL_X(col);
6165 p.y = FILL_Y(row);
6166 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6167 global_ime_set_position(&p);
6168}
6169
6170/*
6171 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6172 */
6173 void
6174im_set_active(int active)
6175{
6176 global_ime_set_status(active);
6177}
6178
6179/*
6180 * Get IM status. When IM is on, return not 0. Else return 0.
6181 */
6182 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006183im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 return global_ime_get_status();
6186}
6187#endif
6188
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006189#ifdef FEAT_MBYTE
6190/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006191 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006192 */
6193 static void
6194latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6195{
6196 int c;
6197
Bram Moolenaarca003e12006-03-17 23:19:38 +00006198 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006199 {
6200 c = *text++;
6201 switch (c)
6202 {
6203 case 0xa4: c = 0x20ac; break; /* euro */
6204 case 0xa6: c = 0x0160; break; /* S hat */
6205 case 0xa8: c = 0x0161; break; /* S -hat */
6206 case 0xb4: c = 0x017d; break; /* Z hat */
6207 case 0xb8: c = 0x017e; break; /* Z -hat */
6208 case 0xbc: c = 0x0152; break; /* OE */
6209 case 0xbd: c = 0x0153; break; /* oe */
6210 case 0xbe: c = 0x0178; break; /* Y */
6211 }
6212 *unicodebuf++ = c;
6213 }
6214}
6215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217#ifdef FEAT_RIGHTLEFT
6218/*
6219 * What is this for? In the case where you are using Win98 or Win2K or later,
6220 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6221 * reverses the string sent to the TextOut... family. This sucks, because we
6222 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6223 * way to tell Windblows not to do this!
6224 *
6225 * The short of it is that this 'RevOut' only gets called if you are running
6226 * one of the new, "improved" MS OSes, and only if you are running in
6227 * 'rightleft' mode. It makes display take *slightly* longer, but not
6228 * noticeably so.
6229 */
6230 static void
6231RevOut( HDC s_hdc,
6232 int col,
6233 int row,
6234 UINT foptions,
6235 CONST RECT *pcliprect,
6236 LPCTSTR text,
6237 UINT len,
6238 CONST INT *padding)
6239{
6240 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006242 for (ix = 0; ix < (int)len; ++ix)
6243 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6244 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245}
6246#endif
6247
Bram Moolenaar92467d32017-12-05 13:22:16 +01006248 static void
6249draw_line(
6250 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006251 int y1,
6252 int x2,
6253 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006254 COLORREF color)
6255{
6256#if defined(FEAT_DIRECTX)
6257 if (IS_ENABLE_DIRECTX())
6258 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6259 else
6260#endif
6261 {
6262 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6263 HPEN old_pen = SelectObject(s_hdc, hpen);
6264 MoveToEx(s_hdc, x1, y1, NULL);
6265 /* Note: LineTo() excludes the last pixel in the line. */
6266 LineTo(s_hdc, x2, y2);
6267 DeleteObject(SelectObject(s_hdc, old_pen));
6268 }
6269}
6270
6271 static void
6272set_pixel(
6273 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006274 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006275 COLORREF color)
6276{
6277#if defined(FEAT_DIRECTX)
6278 if (IS_ENABLE_DIRECTX())
6279 DWriteContext_SetPixel(s_dwc, x, y, color);
6280 else
6281#endif
6282 SetPixel(s_hdc, x, y, color);
6283}
6284
6285 static void
6286fill_rect(
6287 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006288 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006289 COLORREF color)
6290{
6291#if defined(FEAT_DIRECTX)
6292 if (IS_ENABLE_DIRECTX())
6293 DWriteContext_FillRect(s_dwc, rcp, color);
6294 else
6295#endif
6296 {
6297 HBRUSH hbr2;
6298
6299 if (hbr == NULL)
6300 hbr2 = CreateSolidBrush(color);
6301 else
6302 hbr2 = hbr;
6303 FillRect(s_hdc, rcp, hbr2);
6304 if (hbr == NULL)
6305 DeleteBrush(hbr2);
6306 }
6307}
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 void
6310gui_mch_draw_string(
6311 int row,
6312 int col,
6313 char_u *text,
6314 int len,
6315 int flags)
6316{
6317 static int *padding = NULL;
6318 static int pad_size = 0;
6319 int i;
6320 const RECT *pcliprect = NULL;
6321 UINT foptions = 0;
6322#ifdef FEAT_MBYTE
6323 static WCHAR *unicodebuf = NULL;
6324 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006325 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 int n = 0;
6327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 int y;
6329
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 /*
6331 * Italic and bold text seems to have an extra row of pixels at the bottom
6332 * (below where the bottom of the character should be). If we draw the
6333 * characters with a solid background, the top row of pixels in the
6334 * character below will be overwritten. We can fix this by filling in the
6335 * background ourselves, to the correct character proportions, and then
6336 * writing the character in transparent mode. Still have a problem when
6337 * the character is "_", which gets written on to the character below.
6338 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6339 * pixel in their slots, which fixes the problem with the bottom row of
6340 * pixels. We still need this code because otherwise the top row of pixels
6341 * becomes a problem. - webb.
6342 */
6343 static HBRUSH hbr_cache[2] = {NULL, NULL};
6344 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6345 static int brush_lru = 0;
6346 HBRUSH hbr;
6347 RECT rc;
6348
6349 if (!(flags & DRAW_TRANSP))
6350 {
6351 /*
6352 * Clear background first.
6353 * Note: FillRect() excludes right and bottom of rectangle.
6354 */
6355 rc.left = FILL_X(col);
6356 rc.top = FILL_Y(row);
6357#ifdef FEAT_MBYTE
6358 if (has_mbyte)
6359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006361 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 }
6363 else
6364#endif
6365 rc.right = FILL_X(col + len);
6366 rc.bottom = FILL_Y(row + 1);
6367
6368 /* Cache the created brush, that saves a lot of time. We need two:
6369 * one for cursor background and one for the normal background. */
6370 if (gui.currBgColor == brush_color[0])
6371 {
6372 hbr = hbr_cache[0];
6373 brush_lru = 1;
6374 }
6375 else if (gui.currBgColor == brush_color[1])
6376 {
6377 hbr = hbr_cache[1];
6378 brush_lru = 0;
6379 }
6380 else
6381 {
6382 if (hbr_cache[brush_lru] != NULL)
6383 DeleteBrush(hbr_cache[brush_lru]);
6384 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6385 brush_color[brush_lru] = gui.currBgColor;
6386 hbr = hbr_cache[brush_lru];
6387 brush_lru = !brush_lru;
6388 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006389
Bram Moolenaar92467d32017-12-05 13:22:16 +01006390 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392 SetBkMode(s_hdc, TRANSPARENT);
6393
6394 /*
6395 * When drawing block cursor, prevent inverted character spilling
6396 * over character cell (can happen with bold/italic)
6397 */
6398 if (flags & DRAW_CURSOR)
6399 {
6400 pcliprect = &rc;
6401 foptions = ETO_CLIPPED;
6402 }
6403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 SetTextColor(s_hdc, gui.currFgColor);
6405 SelectFont(s_hdc, gui.currFont);
6406
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006407#ifdef FEAT_DIRECTX
6408 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006409 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006410#endif
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6413 {
6414 vim_free(padding);
6415 pad_size = Columns;
6416
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006417 /* Don't give an out-of-memory message here, it would call us
6418 * recursively. */
6419 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (padding != NULL)
6421 for (i = 0; i < pad_size; i++)
6422 padding[i] = gui.char_width;
6423 }
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 /*
6426 * We have to provide the padding argument because italic and bold versions
6427 * of fixed-width fonts are often one pixel or so wider than their normal
6428 * versions.
6429 * No check for DRAW_BOLD, Windows will have done it already.
6430 */
6431
6432#ifdef FEAT_MBYTE
6433 /* Check if there are any UTF-8 characters. If not, use normal text
6434 * output to speed up output. */
6435 if (enc_utf8)
6436 for (n = 0; n < len; ++n)
6437 if (text[n] >= 0x80)
6438 break;
6439
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006440# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006441 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6442 * required that unicode drawing routine, currently. So this forces it
6443 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006444 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006445 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006446# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006449 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006451 if ((enc_utf8
6452 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6453 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 && (unicodebuf == NULL || len > unibuflen))
6455 {
6456 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006457 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006460 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 unibuflen = len;
6463 }
6464
6465 if (enc_utf8 && n < len && unicodebuf != NULL)
6466 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006467 /* Output UTF-8 characters. Composing characters should be
6468 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006469 int i;
6470 int wlen; /* string length in words */
6471 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 int cells; /* cell width of string up to composing char */
6473 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006474 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006476 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006477 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006479 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006481 c = utf_ptr2char(text + i);
6482 if (c >= 0x10000)
6483 {
6484 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006485 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6486 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006487 }
6488 else
6489 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006490 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006491 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006492
6493 if (utf_iscomposing(c))
6494 cw = 0;
6495 else
6496 {
6497 cw = utf_char2cells(c);
6498 if (cw > 2) /* don't use 4 for unprintable char */
6499 cw = 1;
6500 }
6501
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 if (unicodepdy != NULL)
6503 {
6504 /* Use unicodepdy to make characters fit as we expect, even
6505 * when the font uses different widths (e.g., bold character
6506 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006507 if (c >= 0x10000)
6508 {
6509 unicodepdy[wlen - 2] = cw * gui.char_width;
6510 unicodepdy[wlen - 1] = 0;
6511 }
6512 else
6513 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 }
6515 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006516 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006517 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006519# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006520 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006521 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006522 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006523 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006524 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006525 gui.char_width, gui.currFgColor,
6526 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006527 }
6528 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006529# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006530 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6531 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 len = cells; /* used for underlining */
6533 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006534 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
6536 /* If we want to display codepage data, and the current CP is not the
6537 * ANSI one, we need to go via Unicode. */
6538 if (unicodebuf != NULL)
6539 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006540 if (enc_latin9)
6541 latin9_to_ucs(text, len, unicodebuf);
6542 else
6543 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 MB_PRECOMPOSED,
6545 (char *)text, len,
6546 (LPWSTR)unicodebuf, unibuflen);
6547 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006548 {
6549 /* Use unicodepdy to make characters fit as we expect, even
6550 * when the font uses different widths (e.g., bold character
6551 * is wider). */
6552 if (unicodepdy != NULL)
6553 {
6554 int i;
6555 int cw;
6556
6557 for (i = 0; i < len; ++i)
6558 {
6559 cw = utf_char2cells(unicodebuf[i]);
6560 if (cw > 2)
6561 cw = 1;
6562 unicodepdy[i] = cw * gui.char_width;
6563 }
6564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006566 foptions, pcliprect, unicodebuf, len, unicodepdy);
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 }
6569 }
6570 else
6571#endif
6572 {
6573#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006574 /* Windows will mess up RL text, so we have to draw it character by
6575 * character. Only do this if RL is on, since it's slow. */
6576 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6578 foptions, pcliprect, (char *)text, len, padding);
6579 else
6580#endif
6581 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6582 foptions, pcliprect, (char *)text, len, padding);
6583 }
6584
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006585 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (flags & DRAW_UNDERL)
6587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 /* When p_linespace is 0, overwrite the bottom row of pixels.
6589 * Otherwise put the line just below the character. */
6590 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (p_linespace > 1)
6592 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006593 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006595
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006596 /* Strikethrough */
6597 if (flags & DRAW_STRIKE)
6598 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006599 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006600 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006601 }
6602
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006603 /* Undercurl */
6604 if (flags & DRAW_UNDERC)
6605 {
6606 int x;
6607 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006608 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006609
6610 y = FILL_Y(row + 1) - 1;
6611 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6612 {
6613 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006614 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006615 }
6616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618
6619
6620/*
6621 * Output routines.
6622 */
6623
6624/* Flush any output to the screen */
6625 void
6626gui_mch_flush(void)
6627{
6628# if defined(__BORLANDC__)
6629 /*
6630 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6631 * prototype declaration.
6632 * The compiler complains if __stdcall is not used in both declarations.
6633 */
6634 BOOL __stdcall GdiFlush(void);
6635# endif
6636
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006637#if defined(FEAT_DIRECTX)
6638 if (IS_ENABLE_DIRECTX())
6639 DWriteContext_Flush(s_dwc);
6640#endif
6641
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 GdiFlush();
6643}
6644
6645 static void
6646clear_rect(RECT *rcp)
6647{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006648 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649}
6650
6651
Bram Moolenaarc716c302006-01-21 22:12:51 +00006652 void
6653gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6654{
6655 RECT workarea_rect;
6656
6657 get_work_area(&workarea_rect);
6658
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006659 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006660 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006661 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006662
6663 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6664 * the menubar for MSwin, we subtract it from the screen height, so that
6665 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006666 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006667 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006668 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006669 - GetSystemMetrics(SM_CYCAPTION)
6670#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006671 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006672#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006673 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006674}
6675
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677#if defined(FEAT_MENU) || defined(PROTO)
6678/*
6679 * Add a sub menu to the menu bar.
6680 */
6681 void
6682gui_mch_add_menu(
6683 vimmenu_T *menu,
6684 int pos)
6685{
6686 vimmenu_T *parent = menu->parent;
6687
6688 menu->submenu_id = CreatePopupMenu();
6689 menu->id = s_menu_id++;
6690
6691 if (menu_is_menubar(menu->name))
6692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006694 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006696 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6697 {
6698 /* 'encoding' differs from active codepage: convert menu name
6699 * and use wide function */
6700 wn = enc_to_utf16(menu->name, NULL);
6701 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006703 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006705 infow.cbSize = sizeof(infow);
6706 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6707 | MIIM_SUBMENU;
6708 infow.dwItemData = (long_u)menu;
6709 infow.wID = menu->id;
6710 infow.fType = MFT_STRING;
6711 infow.dwTypeData = wn;
6712 infow.cch = (UINT)wcslen(wn);
6713 infow.hSubMenu = menu->submenu_id;
6714 InsertMenuItemW((parent == NULL)
6715 ? s_menuBar : parent->submenu_id,
6716 (UINT)pos, TRUE, &infow);
6717 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006721 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006723 {
6724 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006726 info.cbSize = sizeof(info);
6727 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6728 info.dwItemData = (long_u)menu;
6729 info.wID = menu->id;
6730 info.fType = MFT_STRING;
6731 info.dwTypeData = (LPTSTR)menu->name;
6732 info.cch = (UINT)STRLEN(menu->name);
6733 info.hSubMenu = menu->submenu_id;
6734 InsertMenuItem((parent == NULL)
6735 ? s_menuBar : parent->submenu_id,
6736 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738 }
6739
6740 /* Fix window size if menu may have wrapped */
6741 if (parent == NULL)
6742 gui_mswin_get_menu_height(!gui.starting);
6743#ifdef FEAT_TEAROFF
6744 else if (IsWindow(parent->tearoff_handle))
6745 rebuild_tearoff(parent);
6746#endif
6747}
6748
6749 void
6750gui_mch_show_popupmenu(vimmenu_T *menu)
6751{
6752 POINT mp;
6753
6754 (void)GetCursorPos((LPPOINT)&mp);
6755 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6756}
6757
6758 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006759gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 vimmenu_T *menu = gui_find_menu(path_name);
6762
6763 if (menu != NULL)
6764 {
6765 POINT p;
6766
6767 /* Find the position of the current cursor */
6768 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006769 if (mouse_pos)
6770 {
6771 int mx, my;
6772
6773 gui_mch_getmouse(&mx, &my);
6774 p.x += mx;
6775 p.y += my;
6776 }
6777 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006779 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6781 }
6782 msg_scroll = FALSE;
6783 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6784 }
6785}
6786
6787#if defined(FEAT_TEAROFF) || defined(PROTO)
6788/*
6789 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6790 * create it as a pseudo-"tearoff menu".
6791 */
6792 void
6793gui_make_tearoff(char_u *path_name)
6794{
6795 vimmenu_T *menu = gui_find_menu(path_name);
6796
6797 /* Found the menu, so tear it off. */
6798 if (menu != NULL)
6799 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6800}
6801#endif
6802
6803/*
6804 * Add a menu item to a menu
6805 */
6806 void
6807gui_mch_add_menu_item(
6808 vimmenu_T *menu,
6809 int idx)
6810{
6811 vimmenu_T *parent = menu->parent;
6812
6813 menu->id = s_menu_id++;
6814 menu->submenu_id = NULL;
6815
6816#ifdef FEAT_TEAROFF
6817 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6818 {
6819 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6820 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6821 }
6822 else
6823#endif
6824#ifdef FEAT_TOOLBAR
6825 if (menu_is_toolbar(parent->name))
6826 {
6827 TBBUTTON newtb;
6828
6829 vim_memset(&newtb, 0, sizeof(newtb));
6830 if (menu_is_separator(menu->name))
6831 {
6832 newtb.iBitmap = 0;
6833 newtb.fsStyle = TBSTYLE_SEP;
6834 }
6835 else
6836 {
6837 newtb.iBitmap = get_toolbar_bitmap(menu);
6838 newtb.fsStyle = TBSTYLE_BUTTON;
6839 }
6840 newtb.idCommand = menu->id;
6841 newtb.fsState = TBSTATE_ENABLED;
6842 newtb.iString = 0;
6843 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6844 (LPARAM)&newtb);
6845 menu->submenu_id = (HMENU)-1;
6846 }
6847 else
6848#endif
6849 {
6850#ifdef FEAT_MBYTE
6851 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6854 {
6855 /* 'encoding' differs from active codepage: convert menu item name
6856 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006857 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 if (wn != NULL)
6859 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006860 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 (menu_is_separator(menu->name)
6862 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6863 (UINT)menu->id, wn);
6864 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 }
6866 }
6867 if (wn == NULL)
6868#endif
6869 InsertMenu(parent->submenu_id, (UINT)idx,
6870 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6871 | MF_BYPOSITION,
6872 (UINT)menu->id, (LPCTSTR)menu->name);
6873#ifdef FEAT_TEAROFF
6874 if (IsWindow(parent->tearoff_handle))
6875 rebuild_tearoff(parent);
6876#endif
6877 }
6878}
6879
6880/*
6881 * Destroy the machine specific menu widget.
6882 */
6883 void
6884gui_mch_destroy_menu(vimmenu_T *menu)
6885{
6886#ifdef FEAT_TOOLBAR
6887 /*
6888 * is this a toolbar button?
6889 */
6890 if (menu->submenu_id == (HMENU)-1)
6891 {
6892 int iButton;
6893
6894 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6895 (WPARAM)menu->id, 0);
6896 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6897 }
6898 else
6899#endif
6900 {
6901 if (menu->parent != NULL
6902 && menu_is_popup(menu->parent->dname)
6903 && menu->parent->submenu_id != NULL)
6904 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6905 else
6906 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6907 if (menu->submenu_id != NULL)
6908 DestroyMenu(menu->submenu_id);
6909#ifdef FEAT_TEAROFF
6910 if (IsWindow(menu->tearoff_handle))
6911 DestroyWindow(menu->tearoff_handle);
6912 if (menu->parent != NULL
6913 && menu->parent->children != NULL
6914 && IsWindow(menu->parent->tearoff_handle))
6915 {
6916 /* This menu must not show up when rebuilding the tearoff window. */
6917 menu->modes = 0;
6918 rebuild_tearoff(menu->parent);
6919 }
6920#endif
6921 }
6922}
6923
6924#ifdef FEAT_TEAROFF
6925 static void
6926rebuild_tearoff(vimmenu_T *menu)
6927{
6928 /*hackish*/
6929 char_u tbuf[128];
6930 RECT trect;
6931 RECT rct;
6932 RECT roct;
6933 int x, y;
6934
6935 HWND thwnd = menu->tearoff_handle;
6936
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006937 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 if (GetWindowRect(thwnd, &trect)
6939 && GetWindowRect(s_hwnd, &rct)
6940 && GetClientRect(s_hwnd, &roct))
6941 {
6942 x = trect.left - rct.left;
6943 y = (trect.top - rct.bottom + roct.bottom);
6944 }
6945 else
6946 {
6947 x = y = 0xffffL;
6948 }
6949 DestroyWindow(thwnd);
6950 if (menu->children != NULL)
6951 {
6952 gui_mch_tearoff(tbuf, menu, x, y);
6953 if (IsWindow(menu->tearoff_handle))
6954 (void) SetWindowPos(menu->tearoff_handle,
6955 NULL,
6956 (int)trect.left,
6957 (int)trect.top,
6958 0, 0,
6959 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6960 }
6961}
6962#endif /* FEAT_TEAROFF */
6963
6964/*
6965 * Make a menu either grey or not grey.
6966 */
6967 void
6968gui_mch_menu_grey(
6969 vimmenu_T *menu,
6970 int grey)
6971{
6972#ifdef FEAT_TOOLBAR
6973 /*
6974 * is this a toolbar button?
6975 */
6976 if (menu->submenu_id == (HMENU)-1)
6977 {
6978 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6979 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6980 }
6981 else
6982#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006983 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6984 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986#ifdef FEAT_TEAROFF
6987 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6988 {
6989 WORD menuID;
6990 HWND menuHandle;
6991
6992 /*
6993 * A tearoff button has changed state.
6994 */
6995 if (menu->children == NULL)
6996 menuID = (WORD)(menu->id);
6997 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006998 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7000 if (menuHandle)
7001 EnableWindow(menuHandle, !grey);
7002
7003 }
7004#endif
7005}
7006
7007#endif /* FEAT_MENU */
7008
7009
7010/* define some macros used to make the dialogue creation more readable */
7011
7012#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7013#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007014#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7017/*
7018 * stuff for dialogs
7019 */
7020
7021/*
7022 * The callback routine used by all the dialogs. Very simple. First,
7023 * acknowledges the INITDIALOG message so that Windows knows to do standard
7024 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7025 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7026 * number.
7027 */
7028 static LRESULT CALLBACK
7029dialog_callback(
7030 HWND hwnd,
7031 UINT message,
7032 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007033 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 if (message == WM_INITDIALOG)
7036 {
7037 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7038 /* Set focus to the dialog. Set the default button, if specified. */
7039 (void)SetFocus(hwnd);
7040 if (dialog_default_button > IDCANCEL)
7041 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007042 else
7043 /* We don't have a default, set focus on another element of the
7044 * dialog window, probably the icon */
7045 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 return FALSE;
7047 }
7048
7049 if (message == WM_COMMAND)
7050 {
7051 int button = LOWORD(wParam);
7052
7053 /* Don't end the dialog if something was selected that was
7054 * not a button.
7055 */
7056 if (button >= DLG_NONBUTTON_CONTROL)
7057 return TRUE;
7058
7059 /* If the edit box exists, copy the string. */
7060 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007061 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007062# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007063 /* If the OS is Windows NT, and 'encoding' differs from active
7064 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007065 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007066 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007067 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7068 char_u *p;
7069
7070 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7071 p = utf16_to_enc(wp, NULL);
7072 vim_strncpy(s_textfield, p, IOSIZE);
7073 vim_free(p);
7074 vim_free(wp);
7075 }
7076 else
7077# endif
7078 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007079 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081
7082 /*
7083 * Need to check for IDOK because if the user just hits Return to
7084 * accept the default value, some reason this is what we get.
7085 */
7086 if (button == IDOK)
7087 {
7088 if (dialog_default_button > IDCANCEL)
7089 EndDialog(hwnd, dialog_default_button);
7090 }
7091 else
7092 EndDialog(hwnd, button - IDCANCEL);
7093 return TRUE;
7094 }
7095
7096 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7097 {
7098 EndDialog(hwnd, 0);
7099 return TRUE;
7100 }
7101 return FALSE;
7102}
7103
7104/*
7105 * Create a dialog dynamically from the parameter strings.
7106 * type = type of dialog (question, alert, etc.)
7107 * title = dialog title. may be NULL for default title.
7108 * message = text to display. Dialog sizes to accommodate it.
7109 * buttons = '\n' separated list of button captions, default first.
7110 * dfltbutton = number of default button.
7111 *
7112 * This routine returns 1 if the first button is pressed,
7113 * 2 for the second, etc.
7114 *
7115 * 0 indicates Esc was pressed.
7116 * -1 for unexpected error
7117 *
7118 * If stubbing out this fn, return 1.
7119 */
7120
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007121static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122{
7123 "IDR_VIM",
7124 "IDR_VIM_ERROR",
7125 "IDR_VIM_ALERT",
7126 "IDR_VIM_INFO",
7127 "IDR_VIM_QUESTION"
7128};
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 int
7131gui_mch_dialog(
7132 int type,
7133 char_u *title,
7134 char_u *message,
7135 char_u *buttons,
7136 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007137 char_u *textfield,
7138 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007141 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 int numButtons;
7143 int *buttonWidths, *buttonPositions;
7144 int buttonYpos;
7145 int nchar, i;
7146 DWORD lStyle;
7147 int dlgwidth = 0;
7148 int dlgheight;
7149 int editboxheight;
7150 int horizWidth = 0;
7151 int msgheight;
7152 char_u *pstart;
7153 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007154 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 char_u *tbuffer;
7156 RECT rect;
7157 HWND hwnd;
7158 HDC hdc;
7159 HFONT font, oldFont;
7160 TEXTMETRIC fontInfo;
7161 int fontHeight;
7162 int textWidth, minButtonWidth, messageWidth;
7163 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007164 int maxDialogHeight;
7165 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 int vertical;
7167 int dlgPaddingX;
7168 int dlgPaddingY;
7169#ifdef USE_SYSMENU_FONT
7170 LOGFONT lfSysmenu;
7171 int use_lfSysmenu = FALSE;
7172#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007173 garray_T ga;
7174 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
7176#ifndef NO_CONSOLE
7177 /* Don't output anything in silent mode ("ex -s") */
7178 if (silent_mode)
7179 return dfltbutton; /* return default option */
7180#endif
7181
Bram Moolenaar748bf032005-02-02 23:04:36 +00007182 if (s_hwnd == NULL)
7183 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 if ((type < 0) || (type > VIM_LAST_TYPE))
7186 type = 0;
7187
7188 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007189 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007190 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007191 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
7193 if (p == NULL)
7194 return -1;
7195
7196 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007197 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7199 * const.
7200 */
7201 tbuffer = vim_strsave(buttons);
7202 if (tbuffer == NULL)
7203 return -1;
7204
7205 --dfltbutton; /* Change from one-based to zero-based */
7206
7207 /* Count buttons */
7208 numButtons = 1;
7209 for (i = 0; tbuffer[i] != '\0'; i++)
7210 {
7211 if (tbuffer[i] == DLG_BUTTON_SEP)
7212 numButtons++;
7213 }
7214 if (dfltbutton >= numButtons)
7215 dfltbutton = -1;
7216
7217 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007218 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 if (buttonWidths == NULL)
7220 return -1;
7221
7222 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007223 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 if (buttonPositions == NULL)
7225 return -1;
7226
7227 /*
7228 * Calculate how big the dialog must be.
7229 */
7230 hwnd = GetDesktopWindow();
7231 hdc = GetWindowDC(hwnd);
7232#ifdef USE_SYSMENU_FONT
7233 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7234 {
7235 font = CreateFontIndirect(&lfSysmenu);
7236 use_lfSysmenu = TRUE;
7237 }
7238 else
7239#endif
7240 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7241 VARIABLE_PITCH , DLG_FONT_NAME);
7242 if (s_usenewlook)
7243 {
7244 oldFont = SelectFont(hdc, font);
7245 dlgPaddingX = DLG_PADDING_X;
7246 dlgPaddingY = DLG_PADDING_Y;
7247 }
7248 else
7249 {
7250 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7251 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7252 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7253 }
7254 GetTextMetrics(hdc, &fontInfo);
7255 fontHeight = fontInfo.tmHeight;
7256
7257 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007258 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007261 if (s_hwnd == NULL)
7262 {
7263 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
Bram Moolenaarc716c302006-01-21 22:12:51 +00007265 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007266 get_work_area(&workarea_rect);
7267 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7268 if (maxDialogWidth > 600)
7269 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007270 /* Leave some room for the taskbar. */
7271 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007272 }
7273 else
7274 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007275 /* Use our own window for the size, unless it's very small. */
7276 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007277 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007278 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007279 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007280 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7281 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007282
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007283 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007284 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007285 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007286 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007287 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7288 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7289 }
7290
7291 /* Set dlgwidth to width of message.
7292 * Copy the message into "ga", changing NL to CR-NL and inserting line
7293 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 pstart = message;
7295 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007296 msgheight = 0;
7297 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 do
7299 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007300 msgheight += fontHeight; /* at least one line */
7301
7302 /* Need to figure out where to break the string. The system does it
7303 * at a word boundary, which would mean we can't compute the number of
7304 * wrapped lines. */
7305 textWidth = 0;
7306 last_white = NULL;
7307 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007308 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007309#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007310 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007311#else
7312 l = 1;
7313#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007314 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007315 && textWidth > maxDialogWidth * 3 / 4)
7316 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007317 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007318 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007319 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007320 /* Line will wrap. */
7321 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007322 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007323 textWidth = 0;
7324
7325 if (last_white != NULL)
7326 {
7327 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007328 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007329 pend = last_white + 1;
7330 last_white = NULL;
7331 }
7332 ga_append(&ga, '\r');
7333 ga_append(&ga, '\n');
7334 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007335 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007336
7337 while (--l >= 0)
7338 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007339 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007340 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007342
7343 ga_append(&ga, '\r');
7344 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 pstart = pend + 1;
7346 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007347
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007348 if (ga.ga_data != NULL)
7349 message = ga.ga_data;
7350
Bram Moolenaar748bf032005-02-02 23:04:36 +00007351 messageWidth += 10; /* roundoff space */
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007354 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7355 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357 if (msgheight < DLG_ICON_HEIGHT)
7358 msgheight = DLG_ICON_HEIGHT;
7359
7360 /*
7361 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007362 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007364 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 if (!vertical)
7366 {
7367 // Place buttons horizontally if they fit.
7368 horizWidth = dlgPaddingX;
7369 pstart = tbuffer;
7370 i = 0;
7371 do
7372 {
7373 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7374 if (pend == NULL)
7375 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007376 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 if (textWidth < minButtonWidth)
7378 textWidth = minButtonWidth;
7379 textWidth += dlgPaddingX; /* Padding within button */
7380 buttonWidths[i] = textWidth;
7381 buttonPositions[i++] = horizWidth;
7382 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7383 pstart = pend + 1;
7384 } while (*pend != NUL);
7385
7386 if (horizWidth > maxDialogWidth)
7387 vertical = TRUE; // Too wide to fit on the screen.
7388 else if (horizWidth > dlgwidth)
7389 dlgwidth = horizWidth;
7390 }
7391
7392 if (vertical)
7393 {
7394 // Stack buttons vertically.
7395 pstart = tbuffer;
7396 do
7397 {
7398 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7399 if (pend == NULL)
7400 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007401 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 textWidth += dlgPaddingX; /* Padding within button */
7403 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7404 if (textWidth > dlgwidth)
7405 dlgwidth = textWidth;
7406 pstart = pend + 1;
7407 } while (*pend != NUL);
7408 }
7409
7410 if (dlgwidth < DLG_MIN_WIDTH)
7411 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7412
7413 /* start to fill in the dlgtemplate information. addressing by WORDs */
7414 if (s_usenewlook)
7415 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7416 else
7417 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7418
7419 add_long(lStyle);
7420 add_long(0); // (lExtendedStyle)
7421 pnumitems = p; /*save where the number of items must be stored*/
7422 add_word(0); // NumberOfItems(will change later)
7423 add_word(10); // x
7424 add_word(10); // y
7425 add_word(PixelToDialogX(dlgwidth)); // cx
7426
7427 // Dialog height.
7428 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007429 dlgheight = msgheight + 2 * dlgPaddingY
7430 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 else
7432 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7433
7434 // Dialog needs to be taller if contains an edit box.
7435 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7436 if (textfield != NULL)
7437 dlgheight += editboxheight;
7438
Bram Moolenaara95d8232013-08-07 15:27:11 +02007439 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7440 if (dlgheight > maxDialogHeight)
7441 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007442 msgheight = msgheight - (dlgheight - maxDialogHeight);
7443 dlgheight = maxDialogHeight;
7444 scroll_flag = WS_VSCROLL;
7445 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7446 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007447 }
7448
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 add_word(PixelToDialogY(dlgheight));
7450
7451 add_word(0); // Menu
7452 add_word(0); // Class
7453
7454 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007455 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7456 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 p += nchar;
7458
7459 if (s_usenewlook)
7460 {
7461 /* do the font, since DS_3DLOOK doesn't work properly */
7462#ifdef USE_SYSMENU_FONT
7463 if (use_lfSysmenu)
7464 {
7465 /* point size */
7466 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7467 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007468 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 }
7470 else
7471#endif
7472 {
7473 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007474 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 p += nchar;
7477 }
7478
7479 buttonYpos = msgheight + 2 * dlgPaddingY;
7480
7481 if (textfield != NULL)
7482 buttonYpos += editboxheight;
7483
7484 pstart = tbuffer;
7485 if (!vertical)
7486 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7487 for (i = 0; i < numButtons; i++)
7488 {
7489 /* get end of this button. */
7490 for ( pend = pstart;
7491 *pend && (*pend != DLG_BUTTON_SEP);
7492 pend++)
7493 ;
7494
7495 if (*pend)
7496 *pend = '\0';
7497
7498 /*
7499 * old NOTE:
7500 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7501 * the focus to the first tab-able button and in so doing makes that
7502 * the default!! Grrr. Workaround: Make the default button the only
7503 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7504 * he/she can use arrow keys.
7505 *
7506 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007507 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 * dialog. Also needed for when the textfield is the default control.
7509 * It appears to work now (perhaps not on Win95?).
7510 */
7511 if (vertical)
7512 {
7513 p = add_dialog_element(p,
7514 (i == dfltbutton
7515 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7516 PixelToDialogX(DLG_VERT_PADDING_X),
7517 PixelToDialogY(buttonYpos /* TBK */
7518 + 2 * fontHeight * i),
7519 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7520 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007521 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523 else
7524 {
7525 p = add_dialog_element(p,
7526 (i == dfltbutton
7527 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7528 PixelToDialogX(horizWidth + buttonPositions[i]),
7529 PixelToDialogY(buttonYpos), /* TBK */
7530 PixelToDialogX(buttonWidths[i]),
7531 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007532 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 }
7534 pstart = pend + 1; /*next button*/
7535 }
7536 *pnumitems += numButtons;
7537
7538 /* Vim icon */
7539 p = add_dialog_element(p, SS_ICON,
7540 PixelToDialogX(dlgPaddingX),
7541 PixelToDialogY(dlgPaddingY),
7542 PixelToDialogX(DLG_ICON_WIDTH),
7543 PixelToDialogY(DLG_ICON_HEIGHT),
7544 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7545 dlg_icons[type]);
7546
Bram Moolenaar748bf032005-02-02 23:04:36 +00007547 /* Dialog message */
7548 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7549 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7550 PixelToDialogY(dlgPaddingY),
7551 (WORD)(PixelToDialogX(messageWidth) + 1),
7552 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007553 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 /* Edit box */
7556 if (textfield != NULL)
7557 {
7558 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7559 PixelToDialogX(2 * dlgPaddingX),
7560 PixelToDialogY(2 * dlgPaddingY + msgheight),
7561 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7562 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007563 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 *pnumitems += 1;
7565 }
7566
7567 *pnumitems += 2;
7568
7569 SelectFont(hdc, oldFont);
7570 DeleteObject(font);
7571 ReleaseDC(hwnd, hdc);
7572
7573 /* Let the dialog_callback() function know which button to make default
7574 * If we have an edit box, make that the default. We also need to tell
7575 * dialog_callback() if this dialog contains an edit box or not. We do
7576 * this by setting s_textfield if it does.
7577 */
7578 if (textfield != NULL)
7579 {
7580 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7581 s_textfield = textfield;
7582 }
7583 else
7584 {
7585 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7586 s_textfield = NULL;
7587 }
7588
7589 /* show the dialog box modally and get a return value */
7590 nchar = (int)DialogBoxIndirect(
7591 s_hinst,
7592 (LPDLGTEMPLATE)pdlgtemplate,
7593 s_hwnd,
7594 (DLGPROC)dialog_callback);
7595
7596 LocalFree(LocalHandle(pdlgtemplate));
7597 vim_free(tbuffer);
7598 vim_free(buttonWidths);
7599 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007600 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601
7602 /* Focus back to our window (for when MDI is used). */
7603 (void)SetFocus(s_hwnd);
7604
7605 return nchar;
7606}
7607
7608#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007609
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610/*
7611 * Put a simple element (basic class) onto a dialog template in memory.
7612 * return a pointer to where the next item should be added.
7613 *
7614 * parameters:
7615 * lStyle = additional style flags
7616 * (be careful, NT3.51 & Win32s will ignore the new ones)
7617 * x,y = x & y positions IN DIALOG UNITS
7618 * w,h = width and height IN DIALOG UNITS
7619 * Id = ID used in messages
7620 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7621 * caption = usually text or resource name
7622 *
7623 * TODO: use the length information noted here to enable the dialog creation
7624 * routines to work out more exactly how much memory they need to alloc.
7625 */
7626 static PWORD
7627add_dialog_element(
7628 PWORD p,
7629 DWORD lStyle,
7630 WORD x,
7631 WORD y,
7632 WORD w,
7633 WORD h,
7634 WORD Id,
7635 WORD clss,
7636 const char *caption)
7637{
7638 int nchar;
7639
7640 p = lpwAlign(p); /* Align to dword boundary*/
7641 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7642 *p++ = LOWORD(lStyle);
7643 *p++ = HIWORD(lStyle);
7644 *p++ = 0; // LOWORD (lExtendedStyle)
7645 *p++ = 0; // HIWORD (lExtendedStyle)
7646 *p++ = x;
7647 *p++ = y;
7648 *p++ = w;
7649 *p++ = h;
7650 *p++ = Id; //9 or 10 words in all
7651
7652 *p++ = (WORD)0xffff;
7653 *p++ = clss; //2 more here
7654
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007655 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 p += nchar;
7657
7658 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7659
7660 return p; //total = 15+ (strlen(caption)) words
7661 // = 30 + 2(strlen(caption) bytes reqd
7662}
7663
7664
7665/*
7666 * Helper routine. Take an input pointer, return closest pointer that is
7667 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7668 */
7669 static LPWORD
7670lpwAlign(
7671 LPWORD lpIn)
7672{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007673 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007675 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 ul += 3;
7677 ul >>= 2;
7678 ul <<= 2;
7679 return (LPWORD)ul;
7680}
7681
7682/*
7683 * Helper routine. Takes second parameter as Ansi string, copies it to first
7684 * parameter as wide character (16-bits / char) string, and returns integer
7685 * number of wide characters (words) in string (including the trailing wide
7686 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007687 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7688 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 static int
7690nCopyAnsiToWideChar(
7691 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007692 LPSTR lpAnsiIn,
7693 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 int nChar = 0;
7696#ifdef FEAT_MBYTE
7697 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7698 int i;
7699 WCHAR *wn;
7700
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007701 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 {
7703 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007704 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (wn != NULL)
7706 {
7707 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007708 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 vim_free(wn);
7710 }
7711 }
7712 if (nChar == 0)
7713 /* Use Win32 conversion function. */
7714 nChar = MultiByteToWideChar(
7715 enc_codepage > 0 ? enc_codepage : CP_ACP,
7716 MB_PRECOMPOSED,
7717 lpAnsiIn, len,
7718 lpWCStr, len);
7719 for (i = 0; i < nChar; ++i)
7720 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7721 lpWCStr[i] = (WORD)' ';
7722#else
7723 do
7724 {
7725 if (*lpAnsiIn == '\t')
7726 *lpWCStr++ = (WORD)' ';
7727 else
7728 *lpWCStr++ = (WORD)*lpAnsiIn;
7729 nChar++;
7730 } while (*lpAnsiIn++);
7731#endif
7732
7733 return nChar;
7734}
7735
7736
7737#ifdef FEAT_TEAROFF
7738/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007739 * Lookup menu handle from "menu_id".
7740 */
7741 static HMENU
7742tearoff_lookup_menuhandle(
7743 vimmenu_T *menu,
7744 WORD menu_id)
7745{
7746 for ( ; menu != NULL; menu = menu->next)
7747 {
7748 if (menu->modes == 0) /* this menu has just been deleted */
7749 continue;
7750 if (menu_is_separator(menu->dname))
7751 continue;
7752 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7753 return menu->submenu_id;
7754 }
7755 return NULL;
7756}
7757
7758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * The callback function for all the modeless dialogs that make up the
7760 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7761 * thinking its menus have been clicked), and go away when closed.
7762 */
7763 static LRESULT CALLBACK
7764tearoff_callback(
7765 HWND hwnd,
7766 UINT message,
7767 WPARAM wParam,
7768 LPARAM lParam)
7769{
7770 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007771 {
7772 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
7776 /* May show the mouse pointer again. */
7777 HandleMouseHide(message, lParam);
7778
7779 if (message == WM_COMMAND)
7780 {
7781 if ((WORD)(LOWORD(wParam)) & 0x8000)
7782 {
7783 POINT mp;
7784 RECT rect;
7785
7786 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7787 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007788 vimmenu_T *menu;
7789
7790 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007792 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7794 (int)rect.right - 8,
7795 (int)mp.y,
7796 (int)0, /*reserved param*/
7797 s_hwnd,
7798 NULL);
7799 /*
7800 * NOTE: The pop-up menu can eat the mouse up event.
7801 * We deal with this in normal.c.
7802 */
7803 }
7804 }
7805 else
7806 /* Pass on messages to the main Vim window */
7807 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7808 /*
7809 * Give main window the focus back: this is so after
7810 * choosing a tearoff button you can start typing again
7811 * straight away.
7812 */
7813 (void)SetFocus(s_hwnd);
7814 return TRUE;
7815 }
7816 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7817 {
7818 DestroyWindow(hwnd);
7819 return TRUE;
7820 }
7821
7822 /* When moved around, give main window the focus back. */
7823 if (message == WM_EXITSIZEMOVE)
7824 (void)SetActiveWindow(s_hwnd);
7825
7826 return FALSE;
7827}
7828#endif
7829
7830
7831/*
7832 * Decide whether to use the "new look" (small, non-bold font) or the "old
7833 * look" (big, clanky font) for dialogs, and work out a few values for use
7834 * later accordingly.
7835 */
7836 static void
7837get_dialog_font_metrics(void)
7838{
7839 HDC hdc;
7840 HFONT hfontTools = 0;
7841 DWORD dlgFontSize;
7842 SIZE size;
7843#ifdef USE_SYSMENU_FONT
7844 LOGFONT lfSysmenu;
7845#endif
7846
7847 s_usenewlook = FALSE;
7848
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007850 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7851 hfontTools = CreateFontIndirect(&lfSysmenu);
7852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853#endif
7854 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7855 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7856
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007857 if (hfontTools)
7858 {
7859 hdc = GetDC(s_hwnd);
7860 SelectObject(hdc, hfontTools);
7861 /*
7862 * GetTextMetrics() doesn't return the right value in
7863 * tmAveCharWidth, so we have to figure out the dialog base units
7864 * ourselves.
7865 */
7866 GetTextExtentPoint(hdc,
7867 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7868 52, &size);
7869 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007871 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7872 s_dlgfntheight = (WORD)size.cy;
7873 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875
7876 if (!s_usenewlook)
7877 {
7878 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7879 s_dlgfntwidth = LOWORD(dlgFontSize);
7880 s_dlgfntheight = HIWORD(dlgFontSize);
7881 }
7882}
7883
7884#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7885/*
7886 * Create a pseudo-"tearoff menu" based on the child
7887 * items of a given menu pointer.
7888 */
7889 static void
7890gui_mch_tearoff(
7891 char_u *title,
7892 vimmenu_T *menu,
7893 int initX,
7894 int initY)
7895{
7896 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7897 int template_len;
7898 int nchar, textWidth, submenuWidth;
7899 DWORD lStyle;
7900 DWORD lExtendedStyle;
7901 WORD dlgwidth;
7902 WORD menuID;
7903 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007904 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 vimmenu_T *the_menu = menu;
7906 HWND hwnd;
7907 HDC hdc;
7908 HFONT font, oldFont;
7909 int col, spaceWidth, len;
7910 int columnWidths[2];
7911 char_u *label, *text;
7912 int acLen = 0;
7913 int nameLen;
7914 int padding0, padding1, padding2 = 0;
7915 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007916 int x;
7917 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918#ifdef USE_SYSMENU_FONT
7919 LOGFONT lfSysmenu;
7920 int use_lfSysmenu = FALSE;
7921#endif
7922
7923 /*
7924 * If this menu is already torn off, move it to the mouse position.
7925 */
7926 if (IsWindow(menu->tearoff_handle))
7927 {
7928 POINT mp;
7929 if (GetCursorPos((LPPOINT)&mp))
7930 {
7931 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7932 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7933 }
7934 return;
7935 }
7936
7937 /*
7938 * Create a new tearoff.
7939 */
7940 if (*title == MNU_HIDDEN_CHAR)
7941 title++;
7942
7943 /* Allocate memory to store the dialog template. It's made bigger when
7944 * needed. */
7945 template_len = DLG_ALLOC_SIZE;
7946 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7947 if (p == NULL)
7948 return;
7949
7950 hwnd = GetDesktopWindow();
7951 hdc = GetWindowDC(hwnd);
7952#ifdef USE_SYSMENU_FONT
7953 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7954 {
7955 font = CreateFontIndirect(&lfSysmenu);
7956 use_lfSysmenu = TRUE;
7957 }
7958 else
7959#endif
7960 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7961 VARIABLE_PITCH , DLG_FONT_NAME);
7962 if (s_usenewlook)
7963 oldFont = SelectFont(hdc, font);
7964 else
7965 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7966
7967 /* Calculate width of a single space. Used for padding columns to the
7968 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007969 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
7971 /* Figure out max width of the text column, the accelerator column and the
7972 * optional submenu column. */
7973 submenuWidth = 0;
7974 for (col = 0; col < 2; col++)
7975 {
7976 columnWidths[col] = 0;
7977 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7978 {
7979 /* Use "dname" here to compute the width of the visible text. */
7980 text = (col == 0) ? pmenu->dname : pmenu->actext;
7981 if (text != NULL && *text != NUL)
7982 {
7983 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7984 if (textWidth > columnWidths[col])
7985 columnWidths[col] = textWidth;
7986 }
7987 if (pmenu->children != NULL)
7988 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7989 }
7990 }
7991 if (columnWidths[1] == 0)
7992 {
7993 /* no accelerators */
7994 if (submenuWidth != 0)
7995 columnWidths[0] += submenuWidth;
7996 else
7997 columnWidths[0] += spaceWidth;
7998 }
7999 else
8000 {
8001 /* there is an accelerator column */
8002 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8003 columnWidths[1] += submenuWidth;
8004 }
8005
8006 /*
8007 * Now find the total width of our 'menu'.
8008 */
8009 textWidth = columnWidths[0] + columnWidths[1];
8010 if (submenuWidth != 0)
8011 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008012 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8014 textWidth += submenuWidth;
8015 }
8016 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8017 if (textWidth > dlgwidth)
8018 dlgwidth = textWidth;
8019 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8020
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 /* start to fill in the dlgtemplate information. addressing by WORDs */
8022 if (s_usenewlook)
8023 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8024 else
8025 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8026
8027 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8028 *p++ = LOWORD(lStyle);
8029 *p++ = HIWORD(lStyle);
8030 *p++ = LOWORD(lExtendedStyle);
8031 *p++ = HIWORD(lExtendedStyle);
8032 pnumitems = p; /* save where the number of items must be stored */
8033 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008034 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008036 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 else
8038 *p++ = PixelToDialogX(initX); // x
8039 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008040 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 else
8042 *p++ = PixelToDialogY(initY); // y
8043 *p++ = PixelToDialogX(dlgwidth); // cx
8044 ptrueheight = p;
8045 *p++ = 0; // dialog height: changed later anyway
8046 *p++ = 0; // Menu
8047 *p++ = 0; // Class
8048
8049 /* copy the title of the dialog */
8050 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008051 ? (LPSTR)title
8052 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 p += nchar;
8054
8055 if (s_usenewlook)
8056 {
8057 /* do the font, since DS_3DLOOK doesn't work properly */
8058#ifdef USE_SYSMENU_FONT
8059 if (use_lfSysmenu)
8060 {
8061 /* point size */
8062 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8063 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008064 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 }
8066 else
8067#endif
8068 {
8069 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008070 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 }
8072 p += nchar;
8073 }
8074
8075 /*
8076 * Loop over all the items in the menu.
8077 * But skip over the tearbar.
8078 */
8079 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8080 menu = menu->children->next;
8081 else
8082 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008083 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 for ( ; menu != NULL; menu = menu->next)
8085 {
8086 if (menu->modes == 0) /* this menu has just been deleted */
8087 continue;
8088 if (menu_is_separator(menu->dname))
8089 {
8090 sepPadding += 3;
8091 continue;
8092 }
8093
8094 /* Check if there still is plenty of room in the template. Make it
8095 * larger when needed. */
8096 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8097 {
8098 WORD *newp;
8099
8100 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8101 if (newp != NULL)
8102 {
8103 template_len += 4096;
8104 mch_memmove(newp, pdlgtemplate,
8105 (char *)p - (char *)pdlgtemplate);
8106 p = newp + (p - pdlgtemplate);
8107 pnumitems = newp + (pnumitems - pdlgtemplate);
8108 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8109 LocalFree(LocalHandle(pdlgtemplate));
8110 pdlgtemplate = newp;
8111 }
8112 }
8113
8114 /* Figure out minimal length of this menu label. Use "name" for the
8115 * actual text, "dname" for estimating the displayed size. "name"
8116 * has "&a" for mnemonic and includes the accelerator. */
8117 len = nameLen = (int)STRLEN(menu->name);
8118 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8119 (int)STRLEN(menu->dname))) / spaceWidth;
8120 len += padding0;
8121
8122 if (menu->actext != NULL)
8123 {
8124 acLen = (int)STRLEN(menu->actext);
8125 len += acLen;
8126 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8127 }
8128 else
8129 textWidth = 0;
8130 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8131 len += padding1;
8132
8133 if (menu->children == NULL)
8134 {
8135 padding2 = submenuWidth / spaceWidth;
8136 len += padding2;
8137 menuID = (WORD)(menu->id);
8138 }
8139 else
8140 {
8141 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008142 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144
8145 /* Allocate menu label and fill it in */
8146 text = label = alloc((unsigned)len + 1);
8147 if (label == NULL)
8148 break;
8149
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008150 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8152 if (text == NULL)
8153 text = label + nameLen; /* no actext, use whole name */
8154 while (padding0-- > 0)
8155 *text++ = ' ';
8156 if (menu->actext != NULL)
8157 {
8158 STRNCPY(text, menu->actext, acLen);
8159 text += acLen;
8160 }
8161 while (padding1-- > 0)
8162 *text++ = ' ';
8163 if (menu->children != NULL)
8164 {
8165 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8166 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8167 }
8168 else
8169 {
8170 while (padding2-- > 0)
8171 *text++ = ' ';
8172 }
8173 *text = NUL;
8174
8175 /*
8176 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8177 * W95/NT4 it makes the tear-off look more like a menu.
8178 */
8179 p = add_dialog_element(p,
8180 BS_PUSHBUTTON|BS_LEFT,
8181 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8182 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8183 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8184 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008185 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 vim_free(label);
8187 (*pnumitems)++;
8188 }
8189
8190 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8191
8192
8193 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008194 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 s_hinst,
8196 (LPDLGTEMPLATE)pdlgtemplate,
8197 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008198 (DLGPROC)tearoff_callback,
8199 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200
8201 LocalFree(LocalHandle(pdlgtemplate));
8202 SelectFont(hdc, oldFont);
8203 DeleteObject(font);
8204 ReleaseDC(hwnd, hdc);
8205
8206 /*
8207 * Reassert ourselves as the active window. This is so that after creating
8208 * a tearoff, the user doesn't have to click with the mouse just to start
8209 * typing again!
8210 */
8211 (void)SetActiveWindow(s_hwnd);
8212
8213 /* make sure the right buttons are enabled */
8214 force_menu_update = TRUE;
8215}
8216#endif
8217
8218#if defined(FEAT_TOOLBAR) || defined(PROTO)
8219#include "gui_w32_rc.h"
8220
8221/* This not defined in older SDKs */
8222# ifndef TBSTYLE_FLAT
8223# define TBSTYLE_FLAT 0x0800
8224# endif
8225
8226/*
8227 * Create the toolbar, initially unpopulated.
8228 * (just like the menu, there are no defaults, it's all
8229 * set up through menu.vim)
8230 */
8231 static void
8232initialise_toolbar(void)
8233{
8234 InitCommonControls();
8235 s_toolbarhwnd = CreateToolbarEx(
8236 s_hwnd,
8237 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8238 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008239 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 s_hinst,
8241 IDR_TOOLBAR1, // id of initial bitmap
8242 NULL,
8243 0, // initial number of buttons
8244 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8245 TOOLBAR_BUTTON_HEIGHT,
8246 TOOLBAR_BUTTON_WIDTH,
8247 TOOLBAR_BUTTON_HEIGHT,
8248 sizeof(TBBUTTON)
8249 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008250 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8253}
8254
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008255 static LRESULT CALLBACK
8256toolbar_wndproc(
8257 HWND hwnd,
8258 UINT uMsg,
8259 WPARAM wParam,
8260 LPARAM lParam)
8261{
8262 HandleMouseHide(uMsg, lParam);
8263 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8264}
8265
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 static int
8267get_toolbar_bitmap(vimmenu_T *menu)
8268{
8269 int i = -1;
8270
8271 /*
8272 * Check user bitmaps first, unless builtin is specified.
8273 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008274 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 char_u fname[MAXPATHL];
8277 HANDLE hbitmap = NULL;
8278
8279 if (menu->iconfile != NULL)
8280 {
8281 gui_find_iconfile(menu->iconfile, fname, "bmp");
8282 hbitmap = LoadImage(
8283 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008284 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 IMAGE_BITMAP,
8286 TOOLBAR_BUTTON_WIDTH,
8287 TOOLBAR_BUTTON_HEIGHT,
8288 LR_LOADFROMFILE |
8289 LR_LOADMAP3DCOLORS
8290 );
8291 }
8292
8293 /*
8294 * If the LoadImage call failed, or the "icon=" file
8295 * didn't exist or wasn't specified, try the menu name
8296 */
8297 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008298 && (gui_find_bitmap(
8299#ifdef FEAT_MULTI_LANG
8300 menu->en_dname != NULL ? menu->en_dname :
8301#endif
8302 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 hbitmap = LoadImage(
8304 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008305 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 IMAGE_BITMAP,
8307 TOOLBAR_BUTTON_WIDTH,
8308 TOOLBAR_BUTTON_HEIGHT,
8309 LR_LOADFROMFILE |
8310 LR_LOADMAP3DCOLORS
8311 );
8312
8313 if (hbitmap != NULL)
8314 {
8315 TBADDBITMAP tbAddBitmap;
8316
8317 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008318 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319
8320 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8321 (WPARAM)1, (LPARAM)&tbAddBitmap);
8322 /* i will be set to -1 if it fails */
8323 }
8324 }
8325 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8326 i = menu->iconidx;
8327
8328 return i;
8329}
8330#endif
8331
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008332#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8333 static void
8334initialise_tabline(void)
8335{
8336 InitCommonControls();
8337
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008338 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008339 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008340 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8341 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008342 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008343
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008344 gui.tabline_height = TABLINE_HEIGHT;
8345
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008346# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008347 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008348# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008349}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008350
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008351/*
8352 * Get tabpage_T from POINT.
8353 */
8354 static tabpage_T *
8355GetTabFromPoint(
8356 HWND hWnd,
8357 POINT pt)
8358{
8359 tabpage_T *ptp = NULL;
8360
8361 if (gui_mch_showing_tabline())
8362 {
8363 TCHITTESTINFO htinfo;
8364 htinfo.pt = pt;
8365 /* ignore if a window under cusor is not tabcontrol. */
8366 if (s_tabhwnd == hWnd)
8367 {
8368 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8369 if (idx != -1)
8370 ptp = find_tabpage(idx + 1);
8371 }
8372 }
8373 return ptp;
8374}
8375
8376static POINT s_pt = {0, 0};
8377static HCURSOR s_hCursor = NULL;
8378
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008379 static LRESULT CALLBACK
8380tabline_wndproc(
8381 HWND hwnd,
8382 UINT uMsg,
8383 WPARAM wParam,
8384 LPARAM lParam)
8385{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008386 POINT pt;
8387 tabpage_T *tp;
8388 RECT rect;
8389 int nCenter;
8390 int idx0;
8391 int idx1;
8392
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008393 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008394
8395 switch (uMsg)
8396 {
8397 case WM_LBUTTONDOWN:
8398 {
8399 s_pt.x = GET_X_LPARAM(lParam);
8400 s_pt.y = GET_Y_LPARAM(lParam);
8401 SetCapture(hwnd);
8402 s_hCursor = GetCursor(); /* backup default cursor */
8403 break;
8404 }
8405 case WM_MOUSEMOVE:
8406 if (GetCapture() == hwnd
8407 && ((wParam & MK_LBUTTON)) != 0)
8408 {
8409 pt.x = GET_X_LPARAM(lParam);
8410 pt.y = s_pt.y;
8411 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8412 {
8413 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8414
8415 tp = GetTabFromPoint(hwnd, pt);
8416 if (tp != NULL)
8417 {
8418 idx0 = tabpage_index(curtab) - 1;
8419 idx1 = tabpage_index(tp) - 1;
8420
8421 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8422 nCenter = rect.left + (rect.right - rect.left) / 2;
8423
8424 /* Check if the mouse cursor goes over the center of
8425 * the next tab to prevent "flickering". */
8426 if ((idx0 < idx1) && (nCenter < pt.x))
8427 {
8428 tabpage_move(idx1 + 1);
8429 update_screen(0);
8430 }
8431 else if ((idx1 < idx0) && (pt.x < nCenter))
8432 {
8433 tabpage_move(idx1);
8434 update_screen(0);
8435 }
8436 }
8437 }
8438 }
8439 break;
8440 case WM_LBUTTONUP:
8441 {
8442 if (GetCapture() == hwnd)
8443 {
8444 SetCursor(s_hCursor);
8445 ReleaseCapture();
8446 }
8447 break;
8448 }
8449 default:
8450 break;
8451 }
8452
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008453 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8454}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008455#endif
8456
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8458/*
8459 * Make the GUI window come to the foreground.
8460 */
8461 void
8462gui_mch_set_foreground(void)
8463{
8464 if (IsIconic(s_hwnd))
8465 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8466 SetForegroundWindow(s_hwnd);
8467}
8468#endif
8469
8470#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8471 static void
8472dyn_imm_load(void)
8473{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008474 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if (hLibImm == NULL)
8476 return;
8477
8478 pImmGetCompositionStringA
8479 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8480 pImmGetCompositionStringW
8481 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8482 pImmGetContext
8483 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8484 pImmAssociateContext
8485 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8486 pImmReleaseContext
8487 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8488 pImmGetOpenStatus
8489 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8490 pImmSetOpenStatus
8491 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8492 pImmGetCompositionFont
8493 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8494 pImmSetCompositionFont
8495 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8496 pImmSetCompositionWindow
8497 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8498 pImmGetConversionStatus
8499 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008500 pImmSetConversionStatus
8501 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502
8503 if ( pImmGetCompositionStringA == NULL
8504 || pImmGetCompositionStringW == NULL
8505 || pImmGetContext == NULL
8506 || pImmAssociateContext == NULL
8507 || pImmReleaseContext == NULL
8508 || pImmGetOpenStatus == NULL
8509 || pImmSetOpenStatus == NULL
8510 || pImmGetCompositionFont == NULL
8511 || pImmSetCompositionFont == NULL
8512 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008513 || pImmGetConversionStatus == NULL
8514 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {
8516 FreeLibrary(hLibImm);
8517 hLibImm = NULL;
8518 pImmGetContext = NULL;
8519 return;
8520 }
8521
8522 return;
8523}
8524
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#endif
8526
8527#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8528
8529# ifdef FEAT_XPM_W32
8530# define IMAGE_XPM 100
8531# endif
8532
8533typedef struct _signicon_t
8534{
8535 HANDLE hImage;
8536 UINT uType;
8537#ifdef FEAT_XPM_W32
8538 HANDLE hShape; /* Mask bitmap handle */
8539#endif
8540} signicon_t;
8541
8542 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008543gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 signicon_t *sign;
8546 int x, y, w, h;
8547
8548 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8549 return;
8550
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008551#if defined(FEAT_DIRECTX)
8552 if (IS_ENABLE_DIRECTX())
8553 DWriteContext_Flush(s_dwc);
8554#endif
8555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 x = TEXT_X(col);
8557 y = TEXT_Y(row);
8558 w = gui.char_width * 2;
8559 h = gui.char_height;
8560 switch (sign->uType)
8561 {
8562 case IMAGE_BITMAP:
8563 {
8564 HDC hdcMem;
8565 HBITMAP hbmpOld;
8566
8567 hdcMem = CreateCompatibleDC(s_hdc);
8568 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8569 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8570 SelectObject(hdcMem, hbmpOld);
8571 DeleteDC(hdcMem);
8572 }
8573 break;
8574 case IMAGE_ICON:
8575 case IMAGE_CURSOR:
8576 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8577 break;
8578#ifdef FEAT_XPM_W32
8579 case IMAGE_XPM:
8580 {
8581 HDC hdcMem;
8582 HBITMAP hbmpOld;
8583
8584 hdcMem = CreateCompatibleDC(s_hdc);
8585 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8586 /* Make hole */
8587 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8588
8589 SelectObject(hdcMem, sign->hImage);
8590 /* Paint sign */
8591 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8592 SelectObject(hdcMem, hbmpOld);
8593 DeleteDC(hdcMem);
8594 }
8595 break;
8596#endif
8597 }
8598}
8599
8600 static void
8601close_signicon_image(signicon_t *sign)
8602{
8603 if (sign)
8604 switch (sign->uType)
8605 {
8606 case IMAGE_BITMAP:
8607 DeleteObject((HGDIOBJ)sign->hImage);
8608 break;
8609 case IMAGE_CURSOR:
8610 DestroyCursor((HCURSOR)sign->hImage);
8611 break;
8612 case IMAGE_ICON:
8613 DestroyIcon((HICON)sign->hImage);
8614 break;
8615#ifdef FEAT_XPM_W32
8616 case IMAGE_XPM:
8617 DeleteObject((HBITMAP)sign->hImage);
8618 DeleteObject((HBITMAP)sign->hShape);
8619 break;
8620#endif
8621 }
8622}
8623
8624 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008625gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 signicon_t sign, *psign;
8628 char_u *ext;
8629
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008631 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 if (ext > signfile)
8633 {
8634 int do_load = 1;
8635
8636 if (!STRICMP(ext, ".bmp"))
8637 sign.uType = IMAGE_BITMAP;
8638 else if (!STRICMP(ext, ".ico"))
8639 sign.uType = IMAGE_ICON;
8640 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8641 sign.uType = IMAGE_CURSOR;
8642 else
8643 do_load = 0;
8644
8645 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008646 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 gui.char_width * 2, gui.char_height,
8648 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8649#ifdef FEAT_XPM_W32
8650 if (!STRICMP(ext, ".xpm"))
8651 {
8652 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008653 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8654 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 }
8656#endif
8657 }
8658
8659 psign = NULL;
8660 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8661 != NULL)
8662 *psign = sign;
8663
8664 if (!psign)
8665 {
8666 if (sign.hImage)
8667 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008668 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670 return (void *)psign;
8671
8672}
8673
8674 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008675gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 if (sign)
8678 {
8679 close_signicon_image((signicon_t *)sign);
8680 vim_free(sign);
8681 }
8682}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008685#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
8687/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008688 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008690 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8692 * to get current mouse position).
8693 *
8694 * Trying to use as more Windows services as possible, and as less
8695 * IE version as possible :)).
8696 *
8697 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8698 * BalloonEval struct.
8699 * 2) Enable/Disable simply create/kill BalloonEval Timer
8700 * 3) When there was enough inactivity, timer procedure posts
8701 * async request to debugger
8702 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8703 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008704 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 */
8706
Bram Moolenaar45360022005-07-21 21:08:21 +00008707/*
8708 * determine whether installed Common Controls support multiline tooltips
8709 * (i.e. their version is >= 4.70
8710 */
8711 int
8712multiline_balloon_available(void)
8713{
8714 HINSTANCE hDll;
8715 static char comctl_dll[] = "comctl32.dll";
8716 static int multiline_tip = MAYBE;
8717
8718 if (multiline_tip != MAYBE)
8719 return multiline_tip;
8720
8721 hDll = GetModuleHandle(comctl_dll);
8722 if (hDll != NULL)
8723 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008724 DLLGETVERSIONPROC pGetVer;
8725 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008726
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008727 if (pGetVer != NULL)
8728 {
8729 DLLVERSIONINFO dvi;
8730 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008731
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008732 ZeroMemory(&dvi, sizeof(dvi));
8733 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008734
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008735 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008736
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008737 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008738 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008739 || (dvi.dwMajorVersion == 4
8740 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008741 {
8742 multiline_tip = TRUE;
8743 return multiline_tip;
8744 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008745 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008746 else
8747 {
8748 /* there is chance we have ancient CommCtl 4.70
8749 which doesn't export DllGetVersion */
8750 DWORD dwHandle = 0;
8751 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8752 if (len > 0)
8753 {
8754 VS_FIXEDFILEINFO *ver;
8755 UINT vlen = 0;
8756 void *data = alloc(len);
8757
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008758 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008759 && GetFileVersionInfo(comctl_dll, 0, len, data)
8760 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8761 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008762 && HIWORD(ver->dwFileVersionMS) > 4)
8763 || ((HIWORD(ver->dwFileVersionMS) == 4
8764 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008765 {
8766 vim_free(data);
8767 multiline_tip = TRUE;
8768 return multiline_tip;
8769 }
8770 vim_free(data);
8771 }
8772 }
8773 }
8774 multiline_tip = FALSE;
8775 return multiline_tip;
8776}
8777
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008778#ifdef FEAT_MBYTE
8779 static void
8780make_tooltipw(BalloonEval *beval, char *text, POINT pt)
8781{
8782 TOOLINFOW *pti;
8783 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008784
8785 if (multiline_balloon_available() == TRUE)
8786 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8787 else
8788 ToolInfoSize = sizeof(TOOLINFOW);
8789
8790 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8791 if (pti == NULL)
8792 return;
8793
8794 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8795 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8796 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8797 beval->target, NULL, s_hinst, NULL);
8798
8799 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8800 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8801
8802 pti->cbSize = ToolInfoSize;
8803 pti->uFlags = TTF_SUBCLASS;
8804 pti->hwnd = beval->target;
8805 pti->hinst = 0; // Don't use string resources
8806 pti->uId = ID_BEVAL_TOOLTIP;
8807
8808 if (multiline_balloon_available() == TRUE)
8809 {
8810 RECT rect;
8811 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8812 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008813 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8814 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008815 // switch multiline tooltips on
8816 if (GetClientRect(s_textArea, &rect))
8817 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8818 (LPARAM)rect.right);
8819 }
8820 else
8821 {
8822 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008823 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8824 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008825 }
8826
8827 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8828 pti->rect.left = pt.x - 3;
8829 pti->rect.top = pt.y - 3;
8830 pti->rect.right = pt.x + 3;
8831 pti->rect.bottom = pt.y + 3;
8832
8833 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8834 // Make tooltip appear sooner.
8835 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8836 // I've performed some tests and it seems the longest possible life time
8837 // of tooltip is 30 seconds.
8838 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8839 /*
8840 * HACK: force tooltip to appear, because it'll not appear until
8841 * first mouse move. D*mn M$
8842 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8843 */
8844 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8845 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8846 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008847}
8848#endif
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008851make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaar45360022005-07-21 21:08:21 +00008853 TOOLINFO *pti;
8854 int ToolInfoSize;
8855
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008856#ifdef FEAT_MBYTE
8857 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
8858 {
8859 make_tooltipw(beval, text, pt);
8860 return;
8861 }
8862#endif
8863
Bram Moolenaar45360022005-07-21 21:08:21 +00008864 if (multiline_balloon_available() == TRUE)
8865 ToolInfoSize = sizeof(TOOLINFO_NEW);
8866 else
8867 ToolInfoSize = sizeof(TOOLINFO);
8868
8869 pti = (TOOLINFO *)alloc(ToolInfoSize);
8870 if (pti == NULL)
8871 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
8873 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8874 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8875 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8876 beval->target, NULL, s_hinst, NULL);
8877
8878 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8879 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8880
Bram Moolenaar45360022005-07-21 21:08:21 +00008881 pti->cbSize = ToolInfoSize;
8882 pti->uFlags = TTF_SUBCLASS;
8883 pti->hwnd = beval->target;
8884 pti->hinst = 0; /* Don't use string resources */
8885 pti->uId = ID_BEVAL_TOOLTIP;
8886
8887 if (multiline_balloon_available() == TRUE)
8888 {
8889 RECT rect;
8890 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8891 pti->lpszText = LPSTR_TEXTCALLBACK;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008892 beval->tofree = vim_strsave((char_u*)text);
8893 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaar45360022005-07-21 21:08:21 +00008894 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8895 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8896 (LPARAM)rect.right);
8897 }
8898 else
8899 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008902 pti->rect.left = pt.x - 3;
8903 pti->rect.top = pt.y - 3;
8904 pti->rect.right = pt.x + 3;
8905 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906
Bram Moolenaar45360022005-07-21 21:08:21 +00008907 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 /* Make tooltip appear sooner */
8909 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008910 /* I've performed some tests and it seems the longest possible life time
8911 * of tooltip is 30 seconds */
8912 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 /*
8914 * HACK: force tooltip to appear, because it'll not appear until
8915 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008916 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008918 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008920 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921}
8922
8923 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008924delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008926 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927}
8928
8929 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008930BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008931 HWND hwnd UNUSED,
8932 UINT uMsg UNUSED,
8933 UINT_PTR idEvent UNUSED,
8934 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936 POINT pt;
8937 RECT rect;
8938
8939 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8940 return;
8941
8942 GetCursorPos(&pt);
8943 if (WindowFromPoint(pt) != s_textArea)
8944 return;
8945
8946 ScreenToClient(s_textArea, &pt);
8947 GetClientRect(s_textArea, &rect);
8948 if (!PtInRect(&rect, pt))
8949 return;
8950
8951 if (LastActivity > 0
8952 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8953 && (cur_beval->showState != ShS_PENDING
8954 || abs(cur_beval->x - pt.x) > 3
8955 || abs(cur_beval->y - pt.y) > 3))
8956 {
8957 /* Pointer resting in one place long enough, it's time to show
8958 * the tooltip. */
8959 cur_beval->showState = ShS_PENDING;
8960 cur_beval->x = pt.x;
8961 cur_beval->y = pt.y;
8962
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008963 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
8965 if (cur_beval->msgCB != NULL)
8966 (*cur_beval->msgCB)(cur_beval, 0);
8967 }
8968}
8969
8970 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008971gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008973 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008975 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
8978 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008979gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008981 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 if (beval == NULL)
8983 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008984 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008985 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008986 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008990gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
8992 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008993
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008994 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 if (beval->showState == ShS_SHOWING)
8996 return;
8997 GetCursorPos(&pt);
8998 ScreenToClient(s_textArea, &pt);
8999
9000 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01009002 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 gui_mch_disable_beval_area(cur_beval);
9004 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01009005 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009007 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008}
9009
9010 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009011gui_mch_create_beval_area(
9012 void *target, /* ignored, always use s_textArea */
9013 char_u *mesg,
9014 void (*mesgCB)(BalloonEval *, int),
9015 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016{
9017 /* partially stolen from gui_beval.c */
9018 BalloonEval *beval;
9019
9020 if (mesg != NULL && mesgCB != NULL)
9021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009022 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 return NULL;
9024 }
9025
Bram Moolenaarca4b6132018-06-28 12:05:11 +02009026 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 if (beval != NULL)
9028 {
9029 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030
9031 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 beval->msg = mesg;
9033 beval->msgCB = mesgCB;
9034 beval->clientData = clientData;
9035
9036 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 cur_beval = beval;
9038
9039 if (p_beval)
9040 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
9042 return beval;
9043}
9044
9045 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01009046Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
9049 return;
9050
9051 if (cur_beval != NULL)
9052 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009053 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009055 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009056 // TRACE0("TTN_SHOW {{{");
9057 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00009058 break;
9059 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009060 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 delete_tooltip(cur_beval);
9062 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009063 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064
9065 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00009066 break;
9067 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00009068 {
9069 /* if you get there then we have new common controls */
9070 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
9071 info->lpszText = (LPSTR)info->lParam;
9072 info->uFlags |= TTF_DI_SETITEM;
9073 }
Bram Moolenaar45360022005-07-21 21:08:21 +00009074 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01009075#ifdef FEAT_MBYTE
9076 case TTN_GETDISPINFOW:
9077 {
9078 // if we get here then we have new common controls
9079 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
9080 info->lpszText = (LPWSTR)info->lParam;
9081 info->uFlags |= TTF_DI_SETITEM;
9082 }
9083 break;
9084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 }
9086 }
9087}
9088
9089 static void
9090TrackUserActivity(UINT uMsg)
9091{
9092 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9093 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
9094 LastActivity = GetTickCount();
9095}
9096
9097 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009098gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009100#ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009101 vim_free(beval->vts);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009102#endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009103 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 vim_free(beval);
9105}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01009106#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9109/*
9110 * We have multiple signs to draw at the same location. Draw the
9111 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9112 */
9113 void
9114netbeans_draw_multisign_indicator(int row)
9115{
9116 int i;
9117 int y;
9118 int x;
9119
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009120 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009121 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 x = 0;
9124 y = TEXT_Y(row);
9125
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009126#if defined(FEAT_DIRECTX)
9127 if (IS_ENABLE_DIRECTX())
9128 DWriteContext_Flush(s_dwc);
9129#endif
9130
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 for (i = 0; i < gui.char_height - 3; i++)
9132 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9133
9134 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9135 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9136 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9137 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9138 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9139 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9140 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9141}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009142#endif