blob: 147d98b3913774717572947419c76093906c9ff6 [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)
1585 EMSG2(_(e_font), name);
1586 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
2445void
2446gui_mch_activate_window(void)
2447{
2448 (void)SetActiveWindow(s_hwnd);
2449}
2450
2451#if defined(FEAT_TOOLBAR) || defined(PROTO)
2452 void
2453gui_mch_show_toolbar(int showit)
2454{
2455 if (s_toolbarhwnd == NULL)
2456 return;
2457
2458 if (showit)
2459 {
2460# ifdef FEAT_MBYTE
2461# ifndef TB_SETUNICODEFORMAT
2462 /* For older compilers. We assume this never changes. */
2463# define TB_SETUNICODEFORMAT 0x2005
2464# endif
2465 /* Enable/disable unicode support */
2466 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2467 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2468# endif
2469 ShowWindow(s_toolbarhwnd, SW_SHOW);
2470 }
2471 else
2472 ShowWindow(s_toolbarhwnd, SW_HIDE);
2473}
2474
2475/* Then number of bitmaps is fixed. Exit is missing! */
2476#define TOOLBAR_BITMAP_COUNT 31
2477
2478#endif
2479
2480#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2481 static void
2482add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2483{
2484#ifdef FEAT_MBYTE
2485 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002486
2487 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2488 {
2489 /* 'encoding' differs from active codepage: convert menu name
2490 * and use wide function */
2491 wn = enc_to_utf16(item_text, NULL);
2492 if (wn != NULL)
2493 {
2494 MENUITEMINFOW infow;
2495
2496 infow.cbSize = sizeof(infow);
2497 infow.fMask = MIIM_TYPE | MIIM_ID;
2498 infow.wID = item_id;
2499 infow.fType = MFT_STRING;
2500 infow.dwTypeData = wn;
2501 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002502 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002504 }
2505 }
2506
2507 if (wn == NULL)
2508#endif
2509 {
2510 MENUITEMINFO info;
2511
2512 info.cbSize = sizeof(info);
2513 info.fMask = MIIM_TYPE | MIIM_ID;
2514 info.wID = item_id;
2515 info.fType = MFT_STRING;
2516 info.dwTypeData = (LPTSTR)item_text;
2517 info.cch = (UINT)STRLEN(item_text);
2518 InsertMenuItem(pmenu, item_id, FALSE, &info);
2519 }
2520}
2521
2522 static void
2523show_tabline_popup_menu(void)
2524{
2525 HMENU tab_pmenu;
2526 long rval;
2527 POINT pt;
2528
2529 /* When ignoring events don't show the menu. */
2530 if (hold_gui_events
2531# ifdef FEAT_CMDWIN
2532 || cmdwin_type != 0
2533# endif
2534 )
2535 return;
2536
2537 tab_pmenu = CreatePopupMenu();
2538 if (tab_pmenu == NULL)
2539 return;
2540
2541 if (first_tabpage->tp_next != NULL)
2542 add_tabline_popup_menu_entry(tab_pmenu,
2543 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2546 add_tabline_popup_menu_entry(tab_pmenu,
2547 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2548
2549 GetCursorPos(&pt);
2550 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2551 NULL);
2552
2553 DestroyMenu(tab_pmenu);
2554
2555 /* Add the string cmd into input buffer */
2556 if (rval > 0)
2557 {
2558 TCHITTESTINFO htinfo;
2559 int idx;
2560
2561 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2562 return;
2563
2564 htinfo.pt.x = pt.x;
2565 htinfo.pt.y = pt.y;
2566 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2567 if (idx == -1)
2568 idx = 0;
2569 else
2570 idx += 1;
2571
2572 send_tabline_menu_event(idx, (int)rval);
2573 }
2574}
2575
2576/*
2577 * Show or hide the tabline.
2578 */
2579 void
2580gui_mch_show_tabline(int showit)
2581{
2582 if (s_tabhwnd == NULL)
2583 return;
2584
2585 if (!showit != !showing_tabline)
2586 {
2587 if (showit)
2588 ShowWindow(s_tabhwnd, SW_SHOW);
2589 else
2590 ShowWindow(s_tabhwnd, SW_HIDE);
2591 showing_tabline = showit;
2592 }
2593}
2594
2595/*
2596 * Return TRUE when tabline is displayed.
2597 */
2598 int
2599gui_mch_showing_tabline(void)
2600{
2601 return s_tabhwnd != NULL && showing_tabline;
2602}
2603
2604/*
2605 * Update the labels of the tabline.
2606 */
2607 void
2608gui_mch_update_tabline(void)
2609{
2610 tabpage_T *tp;
2611 TCITEM tie;
2612 int nr = 0;
2613 int curtabidx = 0;
2614 int tabadded = 0;
2615#ifdef FEAT_MBYTE
2616 static int use_unicode = FALSE;
2617 int uu;
2618 WCHAR *wstr = NULL;
2619#endif
2620
2621 if (s_tabhwnd == NULL)
2622 return;
2623
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002624#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625# ifndef CCM_SETUNICODEFORMAT
2626 /* For older compilers. We assume this never changes. */
2627# define CCM_SETUNICODEFORMAT 0x2005
2628# endif
2629 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2630 if (uu != use_unicode)
2631 {
2632 /* Enable/disable unicode support */
2633 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2634 use_unicode = uu;
2635 }
2636#endif
2637
2638 tie.mask = TCIF_TEXT;
2639 tie.iImage = -1;
2640
2641 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2642 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2643
2644 /* Add a label for each tab page. They all contain the same text area. */
2645 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2646 {
2647 if (tp == curtab)
2648 curtabidx = nr;
2649
2650 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2651 {
2652 /* Add the tab */
2653 tie.pszText = "-Empty-";
2654 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2655 tabadded = 1;
2656 }
2657
2658 get_tabline_label(tp, FALSE);
2659 tie.pszText = (LPSTR)NameBuff;
2660#ifdef FEAT_MBYTE
2661 wstr = NULL;
2662 if (use_unicode)
2663 {
2664 /* Need to go through Unicode. */
2665 wstr = enc_to_utf16(NameBuff, NULL);
2666 if (wstr != NULL)
2667 {
2668 TCITEMW tiw;
2669
2670 tiw.mask = TCIF_TEXT;
2671 tiw.iImage = -1;
2672 tiw.pszText = wstr;
2673 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2674 vim_free(wstr);
2675 }
2676 }
2677 if (wstr == NULL)
2678#endif
2679 {
2680 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2681 }
2682 }
2683
2684 /* Remove any old labels. */
2685 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2686 TabCtrl_DeleteItem(s_tabhwnd, nr);
2687
2688 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2689 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2690
2691 /* Re-enable redraw and redraw. */
2692 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2693 RedrawWindow(s_tabhwnd, NULL, NULL,
2694 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2695
2696 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2697 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2698}
2699
2700/*
2701 * Set the current tab to "nr". First tab is 1.
2702 */
2703 void
2704gui_mch_set_curtab(int nr)
2705{
2706 if (s_tabhwnd == NULL)
2707 return;
2708
2709 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2710 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2711}
2712
2713#endif
2714
2715/*
2716 * ":simalt" command.
2717 */
2718 void
2719ex_simalt(exarg_T *eap)
2720{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002721 char_u *keys = eap->arg;
2722 int fill_typebuf = FALSE;
2723 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724
2725 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2726 while (*keys)
2727 {
2728 if (*keys == '~')
2729 *keys = ' '; /* for showing system menu */
2730 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2731 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002732 fill_typebuf = TRUE;
2733 }
2734 if (fill_typebuf)
2735 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002736 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002737 * processed. */
2738 key_name[0] = K_SPECIAL;
2739 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002740 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002741 key_name[3] = NUL;
2742 typebuf_was_filled = TRUE;
2743 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 }
2745}
2746
2747/*
2748 * Create the find & replace dialogs.
2749 * You can't have both at once: ":find" when replace is showing, destroys
2750 * the replace dialog first, and the other way around.
2751 */
2752#ifdef MSWIN_FIND_REPLACE
2753 static void
2754initialise_findrep(char_u *initial_string)
2755{
2756 int wword = FALSE;
2757 int mcase = !p_ic;
2758 char_u *entry_text;
2759
2760 /* Get the search string to use. */
2761 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2762
2763 s_findrep_struct.hwndOwner = s_hwnd;
2764 s_findrep_struct.Flags = FR_DOWN;
2765 if (mcase)
2766 s_findrep_struct.Flags |= FR_MATCHCASE;
2767 if (wword)
2768 s_findrep_struct.Flags |= FR_WHOLEWORD;
2769 if (entry_text != NULL && *entry_text != NUL)
2770 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2771 s_findrep_struct.wFindWhatLen - 1);
2772 vim_free(entry_text);
2773}
2774#endif
2775
2776 static void
2777set_window_title(HWND hwnd, char *title)
2778{
2779#ifdef FEAT_MBYTE
2780 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2781 {
2782 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783
2784 /* Convert the title from 'encoding' to UTF-16. */
2785 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2786 if (wbuf != NULL)
2787 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002788 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002789 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002790 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002791 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002792 }
2793#endif
2794 (void)SetWindowText(hwnd, (LPCSTR)title);
2795}
2796
2797 void
2798gui_mch_find_dialog(exarg_T *eap)
2799{
2800#ifdef MSWIN_FIND_REPLACE
2801 if (s_findrep_msg != 0)
2802 {
2803 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2804 DestroyWindow(s_findrep_hwnd);
2805
2806 if (!IsWindow(s_findrep_hwnd))
2807 {
2808 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002809# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 /* If the OS is Windows NT, and 'encoding' differs from active
2811 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002812 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002813 {
2814 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2815 s_findrep_hwnd = FindTextW(
2816 (LPFINDREPLACEW) &s_findrep_struct_w);
2817 }
2818 else
2819# endif
2820 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2821 }
2822
2823 set_window_title(s_findrep_hwnd,
Bram Moolenaar8df6e5d2018-06-19 14:45:37 +02002824 _("Find string (use '\\\\' to find a '\\')"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002825 (void)SetFocus(s_findrep_hwnd);
2826
2827 s_findrep_is_find = TRUE;
2828 }
2829#endif
2830}
2831
2832
2833 void
2834gui_mch_replace_dialog(exarg_T *eap)
2835{
2836#ifdef MSWIN_FIND_REPLACE
2837 if (s_findrep_msg != 0)
2838 {
2839 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2840 DestroyWindow(s_findrep_hwnd);
2841
2842 if (!IsWindow(s_findrep_hwnd))
2843 {
2844 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002845# ifdef FEAT_MBYTE
2846 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002847 {
2848 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2849 s_findrep_hwnd = ReplaceTextW(
2850 (LPFINDREPLACEW) &s_findrep_struct_w);
2851 }
2852 else
2853# endif
2854 s_findrep_hwnd = ReplaceText(
2855 (LPFINDREPLACE) &s_findrep_struct);
2856 }
2857
2858 set_window_title(s_findrep_hwnd,
Bram Moolenaar8df6e5d2018-06-19 14:45:37 +02002859 _("Find & Replace (use '\\\\' to find a '\\')"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002860 (void)SetFocus(s_findrep_hwnd);
2861
2862 s_findrep_is_find = FALSE;
2863 }
2864#endif
2865}
2866
2867
2868/*
2869 * Set visibility of the pointer.
2870 */
2871 void
2872gui_mch_mousehide(int hide)
2873{
2874 if (hide != gui.pointer_hidden)
2875 {
2876 ShowCursor(!hide);
2877 gui.pointer_hidden = hide;
2878 }
2879}
2880
2881#ifdef FEAT_MENU
2882 static void
2883gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2884{
2885 /* Unhide the mouse, we don't get move events here. */
2886 gui_mch_mousehide(FALSE);
2887
2888 (void)TrackPopupMenu(
2889 (HMENU)menu->submenu_id,
2890 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2891 x, y,
2892 (int)0, /*reserved param*/
2893 s_hwnd,
2894 NULL);
2895 /*
2896 * NOTE: The pop-up menu can eat the mouse up event.
2897 * We deal with this in normal.c.
2898 */
2899}
2900#endif
2901
2902/*
2903 * Got a message when the system will go down.
2904 */
2905 static void
2906_OnEndSession(void)
2907{
2908 getout_preserve_modified(1);
2909}
2910
2911/*
2912 * Get this message when the user clicks on the cross in the top right corner
2913 * of a Windows95 window.
2914 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002916_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002917{
2918 gui_shell_closed();
2919}
2920
2921/*
2922 * Get a message when the window is being destroyed.
2923 */
2924 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002925_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002926{
2927 if (!destroying)
2928 _OnClose(hwnd);
2929}
2930
2931 static void
2932_OnPaint(
2933 HWND hwnd)
2934{
2935 if (!IsMinimized(hwnd))
2936 {
2937 PAINTSTRUCT ps;
2938
2939 out_flush(); /* make sure all output has been processed */
2940 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941
2942#ifdef FEAT_MBYTE
2943 /* prevent multi-byte characters from misprinting on an invalid
2944 * rectangle */
2945 if (has_mbyte)
2946 {
2947 RECT rect;
2948
2949 GetClientRect(hwnd, &rect);
2950 ps.rcPaint.left = rect.left;
2951 ps.rcPaint.right = rect.right;
2952 }
2953#endif
2954
2955 if (!IsRectEmpty(&ps.rcPaint))
2956 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002957 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2958 ps.rcPaint.right - ps.rcPaint.left + 1,
2959 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2960 }
2961
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002962 EndPaint(hwnd, &ps);
2963 }
2964}
2965
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002966 static void
2967_OnSize(
2968 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002969 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002970 int cx,
2971 int cy)
2972{
2973 if (!IsMinimized(hwnd))
2974 {
2975 gui_resize_shell(cx, cy);
2976
2977#ifdef FEAT_MENU
2978 /* Menu bar may wrap differently now */
2979 gui_mswin_get_menu_height(TRUE);
2980#endif
2981 }
2982}
2983
2984 static void
2985_OnSetFocus(
2986 HWND hwnd,
2987 HWND hwndOldFocus)
2988{
2989 gui_focus_change(TRUE);
2990 s_getting_focus = TRUE;
2991 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2992}
2993
2994 static void
2995_OnKillFocus(
2996 HWND hwnd,
2997 HWND hwndNewFocus)
2998{
2999 gui_focus_change(FALSE);
3000 s_getting_focus = FALSE;
3001 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3002}
3003
3004/*
3005 * Get a message when the user switches back to vim
3006 */
3007 static LRESULT
3008_OnActivateApp(
3009 HWND hwnd,
3010 BOOL fActivate,
3011 DWORD dwThreadId)
3012{
3013 /* we call gui_focus_change() in _OnSetFocus() */
3014 /* gui_focus_change((int)fActivate); */
3015 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3016}
3017
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003018 void
3019gui_mch_destroy_scrollbar(scrollbar_T *sb)
3020{
3021 DestroyWindow(sb->id);
3022}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003023
3024/*
3025 * Get current mouse coordinates in text window.
3026 */
3027 void
3028gui_mch_getmouse(int *x, int *y)
3029{
3030 RECT rct;
3031 POINT mp;
3032
3033 (void)GetWindowRect(s_textArea, &rct);
3034 (void)GetCursorPos((LPPOINT)&mp);
3035 *x = (int)(mp.x - rct.left);
3036 *y = (int)(mp.y - rct.top);
3037}
3038
3039/*
3040 * Move mouse pointer to character at (x, y).
3041 */
3042 void
3043gui_mch_setmouse(int x, int y)
3044{
3045 RECT rct;
3046
3047 (void)GetWindowRect(s_textArea, &rct);
3048 (void)SetCursorPos(x + gui.border_offset + rct.left,
3049 y + gui.border_offset + rct.top);
3050}
3051
3052 static void
3053gui_mswin_get_valid_dimensions(
3054 int w,
3055 int h,
3056 int *valid_w,
3057 int *valid_h)
3058{
3059 int base_width, base_height;
3060
3061 base_width = gui_get_base_width()
3062 + (GetSystemMetrics(SM_CXFRAME) +
3063 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3064 base_height = gui_get_base_height()
3065 + (GetSystemMetrics(SM_CYFRAME) +
3066 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3067 + GetSystemMetrics(SM_CYCAPTION)
3068#ifdef FEAT_MENU
3069 + gui_mswin_get_menu_height(FALSE)
3070#endif
3071 ;
3072 *valid_w = base_width +
3073 ((w - base_width) / gui.char_width) * gui.char_width;
3074 *valid_h = base_height +
3075 ((h - base_height) / gui.char_height) * gui.char_height;
3076}
3077
3078 void
3079gui_mch_flash(int msec)
3080{
3081 RECT rc;
3082
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003083#if defined(FEAT_DIRECTX)
3084 if (IS_ENABLE_DIRECTX())
3085 DWriteContext_Flush(s_dwc);
3086#endif
3087
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088 /*
3089 * Note: InvertRect() excludes right and bottom of rectangle.
3090 */
3091 rc.left = 0;
3092 rc.top = 0;
3093 rc.right = gui.num_cols * gui.char_width;
3094 rc.bottom = gui.num_rows * gui.char_height;
3095 InvertRect(s_hdc, &rc);
3096 gui_mch_flush(); /* make sure it's displayed */
3097
3098 ui_delay((long)msec, TRUE); /* wait for a few msec */
3099
3100 InvertRect(s_hdc, &rc);
3101}
3102
3103/*
3104 * Return flags used for scrolling.
3105 * The SW_INVALIDATE is required when part of the window is covered or
3106 * off-screen. Refer to MS KB Q75236.
3107 */
3108 static int
3109get_scroll_flags(void)
3110{
3111 HWND hwnd;
3112 RECT rcVim, rcOther, rcDest;
3113
3114 GetWindowRect(s_hwnd, &rcVim);
3115
3116 /* Check if the window is partly above or below the screen. We don't care
3117 * about partly left or right of the screen, it is not relevant when
3118 * scrolling up or down. */
3119 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3120 return SW_INVALIDATE;
3121
3122 /* Check if there is an window (partly) on top of us. */
3123 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3124 if (IsWindowVisible(hwnd))
3125 {
3126 GetWindowRect(hwnd, &rcOther);
3127 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3128 return SW_INVALIDATE;
3129 }
3130 return 0;
3131}
3132
3133/*
3134 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3135 * may not be scrolled out properly.
3136 * For gVim, when _OnScroll() is repeated, the character at the
3137 * previous cursor position may be left drawn after scroll.
3138 * The problem can be avoided by calling GetPixel() to get a pixel in
3139 * the region before ScrollWindowEx().
3140 */
3141 static void
3142intel_gpu_workaround(void)
3143{
3144 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3145}
3146
3147/*
3148 * Delete the given number of lines from the given row, scrolling up any
3149 * text further down within the scroll region.
3150 */
3151 void
3152gui_mch_delete_lines(
3153 int row,
3154 int num_lines)
3155{
3156 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003157
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003158 rc.left = FILL_X(gui.scroll_region_left);
3159 rc.right = FILL_X(gui.scroll_region_right + 1);
3160 rc.top = FILL_Y(row);
3161 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3162
Bram Moolenaar92467d32017-12-05 13:22:16 +01003163#if defined(FEAT_DIRECTX)
3164 if (IS_ENABLE_DIRECTX())
3165 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003166 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3167 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003169 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003170#endif
3171 {
3172 intel_gpu_workaround();
3173 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003174 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003175 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003178 /* This seems to be required to avoid the cursor disappearing when
3179 * scrolling such that the cursor ends up in the top-left character on
3180 * the screen... But why? (Webb) */
3181 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3182 /* gui.cursor_is_valid = FALSE; */
3183
3184 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3185 gui.scroll_region_left,
3186 gui.scroll_region_bot, gui.scroll_region_right);
3187}
3188
3189/*
3190 * Insert the given number of lines before the given row, scrolling down any
3191 * following text within the scroll region.
3192 */
3193 void
3194gui_mch_insert_lines(
3195 int row,
3196 int num_lines)
3197{
3198 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003199
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200 rc.left = FILL_X(gui.scroll_region_left);
3201 rc.right = FILL_X(gui.scroll_region_right + 1);
3202 rc.top = FILL_Y(row);
3203 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003204
3205#if defined(FEAT_DIRECTX)
3206 if (IS_ENABLE_DIRECTX())
3207 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003208 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3209 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003211 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003212#endif
3213 {
3214 intel_gpu_workaround();
3215 /* The SW_INVALIDATE is required when part of the window is covered or
3216 * off-screen. How do we avoid it when it's not needed? */
3217 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003219 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003220 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003221
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222 gui_clear_block(row, gui.scroll_region_left,
3223 row + num_lines - 1, gui.scroll_region_right);
3224}
3225
3226
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003228gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229{
3230#if defined(FEAT_DIRECTX)
3231 DWriteContext_Close(s_dwc);
3232 DWrite_Final();
3233 s_dwc = NULL;
3234#endif
3235
3236 ReleaseDC(s_textArea, s_hdc);
3237 DeleteObject(s_brush);
3238
3239#ifdef FEAT_TEAROFF
3240 /* Unload the tearoff bitmap */
3241 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3242#endif
3243
3244 /* Destroy our window (if we have one). */
3245 if (s_hwnd != NULL)
3246 {
3247 destroying = TRUE; /* ignore WM_DESTROY message now */
3248 DestroyWindow(s_hwnd);
3249 }
3250
3251#ifdef GLOBAL_IME
3252 global_ime_end();
3253#endif
3254}
3255
3256 static char_u *
3257logfont2name(LOGFONT lf)
3258{
3259 char *p;
3260 char *res;
3261 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003262 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263 char *font_name = lf.lfFaceName;
3264
3265 charset_name = charset_id2name((int)lf.lfCharSet);
3266#ifdef FEAT_MBYTE
3267 /* Convert a font name from the current codepage to 'encoding'.
3268 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3269 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3270 {
3271 int len;
3272 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3273 (char_u **)&font_name, &len);
3274 }
3275#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003276 quality_name = quality_id2name((int)lf.lfQuality);
3277
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3279 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3280 if (res != NULL)
3281 {
3282 p = res;
3283 /* make a normal font string out of the lf thing:*/
3284 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3285 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3286 while (*p)
3287 {
3288 if (*p == ' ')
3289 *p = '_';
3290 ++p;
3291 }
3292 if (lf.lfItalic)
3293 STRCAT(p, ":i");
3294 if (lf.lfWeight >= FW_BOLD)
3295 STRCAT(p, ":b");
3296 if (lf.lfUnderline)
3297 STRCAT(p, ":u");
3298 if (lf.lfStrikeOut)
3299 STRCAT(p, ":s");
3300 if (charset_name != NULL)
3301 {
3302 STRCAT(p, ":c");
3303 STRCAT(p, charset_name);
3304 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003305 if (quality_name != NULL)
3306 {
3307 STRCAT(p, ":q");
3308 STRCAT(p, quality_name);
3309 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310 }
3311
3312#ifdef FEAT_MBYTE
3313 if (font_name != lf.lfFaceName)
3314 vim_free(font_name);
3315#endif
3316 return (char_u *)res;
3317}
3318
3319
3320#ifdef FEAT_MBYTE_IME
3321/*
3322 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3323 * 'guifont'
3324 */
3325 static void
3326update_im_font(void)
3327{
3328 LOGFONT lf_wide;
3329
3330 if (p_guifontwide != NULL && *p_guifontwide != NUL
3331 && gui.wide_font != NOFONT
3332 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3333 norm_logfont = lf_wide;
3334 else
3335 norm_logfont = sub_logfont;
3336 im_set_font(&norm_logfont);
3337}
3338#endif
3339
3340#ifdef FEAT_MBYTE
3341/*
3342 * Handler of gui.wide_font (p_guifontwide) changed notification.
3343 */
3344 void
3345gui_mch_wide_font_changed(void)
3346{
3347 LOGFONT lf;
3348
3349# ifdef FEAT_MBYTE_IME
3350 update_im_font();
3351# endif
3352
3353 gui_mch_free_font(gui.wide_ital_font);
3354 gui.wide_ital_font = NOFONT;
3355 gui_mch_free_font(gui.wide_bold_font);
3356 gui.wide_bold_font = NOFONT;
3357 gui_mch_free_font(gui.wide_boldital_font);
3358 gui.wide_boldital_font = NOFONT;
3359
3360 if (gui.wide_font
3361 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3362 {
3363 if (!lf.lfItalic)
3364 {
3365 lf.lfItalic = TRUE;
3366 gui.wide_ital_font = get_font_handle(&lf);
3367 lf.lfItalic = FALSE;
3368 }
3369 if (lf.lfWeight < FW_BOLD)
3370 {
3371 lf.lfWeight = FW_BOLD;
3372 gui.wide_bold_font = get_font_handle(&lf);
3373 if (!lf.lfItalic)
3374 {
3375 lf.lfItalic = TRUE;
3376 gui.wide_boldital_font = get_font_handle(&lf);
3377 }
3378 }
3379 }
3380}
3381#endif
3382
3383/*
3384 * Initialise vim to use the font with the given name.
3385 * Return FAIL if the font could not be loaded, OK otherwise.
3386 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003388gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003389{
3390 LOGFONT lf;
3391 GuiFont font = NOFONT;
3392 char_u *p;
3393
3394 /* Load the font */
3395 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3396 font = get_font_handle(&lf);
3397 if (font == NOFONT)
3398 return FAIL;
3399
3400 if (font_name == NULL)
3401 font_name = (char_u *)lf.lfFaceName;
3402#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3403 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003404#endif
3405#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406 sub_logfont = lf;
3407#endif
3408#ifdef FEAT_MBYTE_IME
3409 update_im_font();
3410#endif
3411 gui_mch_free_font(gui.norm_font);
3412 gui.norm_font = font;
3413 current_font_height = lf.lfHeight;
3414 GetFontSize(font);
3415
3416 p = logfont2name(lf);
3417 if (p != NULL)
3418 {
3419 hl_set_font_name(p);
3420
3421 /* When setting 'guifont' to "*" replace it with the actual font name.
3422 * */
3423 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3424 {
3425 vim_free(p_guifont);
3426 p_guifont = p;
3427 }
3428 else
3429 vim_free(p);
3430 }
3431
3432 gui_mch_free_font(gui.ital_font);
3433 gui.ital_font = NOFONT;
3434 gui_mch_free_font(gui.bold_font);
3435 gui.bold_font = NOFONT;
3436 gui_mch_free_font(gui.boldital_font);
3437 gui.boldital_font = NOFONT;
3438
3439 if (!lf.lfItalic)
3440 {
3441 lf.lfItalic = TRUE;
3442 gui.ital_font = get_font_handle(&lf);
3443 lf.lfItalic = FALSE;
3444 }
3445 if (lf.lfWeight < FW_BOLD)
3446 {
3447 lf.lfWeight = FW_BOLD;
3448 gui.bold_font = get_font_handle(&lf);
3449 if (!lf.lfItalic)
3450 {
3451 lf.lfItalic = TRUE;
3452 gui.boldital_font = get_font_handle(&lf);
3453 }
3454 }
3455
3456 return OK;
3457}
3458
3459#ifndef WPF_RESTORETOMAXIMIZED
3460# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3461#endif
3462
3463/*
3464 * Return TRUE if the GUI window is maximized, filling the whole screen.
3465 */
3466 int
3467gui_mch_maximized(void)
3468{
3469 WINDOWPLACEMENT wp;
3470
3471 wp.length = sizeof(WINDOWPLACEMENT);
3472 if (GetWindowPlacement(s_hwnd, &wp))
3473 return wp.showCmd == SW_SHOWMAXIMIZED
3474 || (wp.showCmd == SW_SHOWMINIMIZED
3475 && wp.flags == WPF_RESTORETOMAXIMIZED);
3476
3477 return 0;
3478}
3479
3480/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003481 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3482 * is set. Compute the new Rows and Columns. This is like resizing the
3483 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484 */
3485 void
3486gui_mch_newfont(void)
3487{
3488 RECT rect;
3489
3490 GetWindowRect(s_hwnd, &rect);
3491 if (win_socket_id == 0)
3492 {
3493 gui_resize_shell(rect.right - rect.left
3494 - (GetSystemMetrics(SM_CXFRAME) +
3495 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3496 rect.bottom - rect.top
3497 - (GetSystemMetrics(SM_CYFRAME) +
3498 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3499 - GetSystemMetrics(SM_CYCAPTION)
3500#ifdef FEAT_MENU
3501 - gui_mswin_get_menu_height(FALSE)
3502#endif
3503 );
3504 }
3505 else
3506 {
3507 /* Inside another window, don't use the frame and border. */
3508 gui_resize_shell(rect.right - rect.left,
3509 rect.bottom - rect.top
3510#ifdef FEAT_MENU
3511 - gui_mswin_get_menu_height(FALSE)
3512#endif
3513 );
3514 }
3515}
3516
3517/*
3518 * Set the window title
3519 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520 void
3521gui_mch_settitle(
3522 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003523 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003524{
3525 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3526}
3527
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003528#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003529/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3530 * misc2.c! */
3531static LPCSTR mshape_idcs[] =
3532{
3533 IDC_ARROW, /* arrow */
3534 MAKEINTRESOURCE(0), /* blank */
3535 IDC_IBEAM, /* beam */
3536 IDC_SIZENS, /* updown */
3537 IDC_SIZENS, /* udsizing */
3538 IDC_SIZEWE, /* leftright */
3539 IDC_SIZEWE, /* lrsizing */
3540 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 IDC_ARROW, /* crosshair */
3543 IDC_ARROW, /* hand1 */
3544 IDC_ARROW, /* hand2 */
3545 IDC_ARROW, /* pencil */
3546 IDC_ARROW, /* question */
3547 IDC_ARROW, /* right-arrow */
3548 IDC_UPARROW, /* up-arrow */
3549 IDC_ARROW /* last one */
3550};
3551
3552 void
3553mch_set_mouse_shape(int shape)
3554{
3555 LPCSTR idc;
3556
3557 if (shape == MSHAPE_HIDE)
3558 ShowCursor(FALSE);
3559 else
3560 {
3561 if (shape >= MSHAPE_NUMBERED)
3562 idc = IDC_ARROW;
3563 else
3564 idc = mshape_idcs[shape];
3565#ifdef SetClassLongPtr
3566 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3567#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003568 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569#endif
3570 if (!p_mh)
3571 {
3572 POINT mp;
3573
3574 /* Set the position to make it redrawn with the new shape. */
3575 (void)GetCursorPos((LPPOINT)&mp);
3576 (void)SetCursorPos(mp.x, mp.y);
3577 ShowCursor(TRUE);
3578 }
3579 }
3580}
3581#endif
3582
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003583#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003584/*
3585 * The file browser exists in two versions: with "W" uses wide characters,
3586 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3587 * Windows NT/2000/XP the "W" functions are used.
3588 */
3589
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003590# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003591/*
3592 * Wide version of convert_filter().
3593 */
3594 static WCHAR *
3595convert_filterW(char_u *s)
3596{
3597 char_u *tmp;
3598 int len;
3599 WCHAR *res;
3600
3601 tmp = convert_filter(s);
3602 if (tmp == NULL)
3603 return NULL;
3604 len = (int)STRLEN(s) + 3;
3605 res = enc_to_utf16(tmp, &len);
3606 vim_free(tmp);
3607 return res;
3608}
3609
3610/*
3611 * Wide version of gui_mch_browse(). Keep in sync!
3612 */
3613 static char_u *
3614gui_mch_browseW(
3615 int saving,
3616 char_u *title,
3617 char_u *dflt,
3618 char_u *ext,
3619 char_u *initdir,
3620 char_u *filter)
3621{
3622 /* We always use the wide function. This means enc_to_utf16() must work,
3623 * otherwise it fails miserably! */
3624 OPENFILENAMEW fileStruct;
3625 WCHAR fileBuf[MAXPATHL];
3626 WCHAR *wp;
3627 int i;
3628 WCHAR *titlep = NULL;
3629 WCHAR *extp = NULL;
3630 WCHAR *initdirp = NULL;
3631 WCHAR *filterp;
3632 char_u *p;
3633
3634 if (dflt == NULL)
3635 fileBuf[0] = NUL;
3636 else
3637 {
3638 wp = enc_to_utf16(dflt, NULL);
3639 if (wp == NULL)
3640 fileBuf[0] = NUL;
3641 else
3642 {
3643 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3644 fileBuf[i] = wp[i];
3645 fileBuf[i] = NUL;
3646 vim_free(wp);
3647 }
3648 }
3649
3650 /* Convert the filter to Windows format. */
3651 filterp = convert_filterW(filter);
3652
3653 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003654# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003655 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003656 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003657# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003659# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660
3661 if (title != NULL)
3662 titlep = enc_to_utf16(title, NULL);
3663 fileStruct.lpstrTitle = titlep;
3664
3665 if (ext != NULL)
3666 extp = enc_to_utf16(ext, NULL);
3667 fileStruct.lpstrDefExt = extp;
3668
3669 fileStruct.lpstrFile = fileBuf;
3670 fileStruct.nMaxFile = MAXPATHL;
3671 fileStruct.lpstrFilter = filterp;
3672 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3673 /* has an initial dir been specified? */
3674 if (initdir != NULL && *initdir != NUL)
3675 {
3676 /* Must have backslashes here, no matter what 'shellslash' says */
3677 initdirp = enc_to_utf16(initdir, NULL);
3678 if (initdirp != NULL)
3679 {
3680 for (wp = initdirp; *wp != NUL; ++wp)
3681 if (*wp == '/')
3682 *wp = '\\';
3683 }
3684 fileStruct.lpstrInitialDir = initdirp;
3685 }
3686
3687 /*
3688 * TODO: Allow selection of multiple files. Needs another arg to this
3689 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3690 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3691 * files that don't exist yet, so I haven't put it in. What about
3692 * OFN_PATHMUSTEXIST?
3693 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3694 */
3695 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003696# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003697 if (curbuf->b_p_bin)
3698 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003699# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 if (saving)
3701 {
3702 if (!GetSaveFileNameW(&fileStruct))
3703 return NULL;
3704 }
3705 else
3706 {
3707 if (!GetOpenFileNameW(&fileStruct))
3708 return NULL;
3709 }
3710
3711 vim_free(filterp);
3712 vim_free(initdirp);
3713 vim_free(titlep);
3714 vim_free(extp);
3715
3716 /* Convert from UCS2 to 'encoding'. */
3717 p = utf16_to_enc(fileBuf, NULL);
3718 if (p != NULL)
3719 /* when out of memory we get garbage for non-ASCII chars */
3720 STRCPY(fileBuf, p);
3721 vim_free(p);
3722
3723 /* Give focus back to main window (when using MDI). */
3724 SetFocus(s_hwnd);
3725
3726 /* Shorten the file name if possible */
3727 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3728}
3729# endif /* FEAT_MBYTE */
3730
3731
3732/*
3733 * Convert the string s to the proper format for a filter string by replacing
3734 * the \t and \n delimiters with \0.
3735 * Returns the converted string in allocated memory.
3736 *
3737 * Keep in sync with convert_filterW() above!
3738 */
3739 static char_u *
3740convert_filter(char_u *s)
3741{
3742 char_u *res;
3743 unsigned s_len = (unsigned)STRLEN(s);
3744 unsigned i;
3745
3746 res = alloc(s_len + 3);
3747 if (res != NULL)
3748 {
3749 for (i = 0; i < s_len; ++i)
3750 if (s[i] == '\t' || s[i] == '\n')
3751 res[i] = '\0';
3752 else
3753 res[i] = s[i];
3754 res[s_len] = NUL;
3755 /* Add two extra NULs to make sure it's properly terminated. */
3756 res[s_len + 1] = NUL;
3757 res[s_len + 2] = NUL;
3758 }
3759 return res;
3760}
3761
3762/*
3763 * Select a directory.
3764 */
3765 char_u *
3766gui_mch_browsedir(char_u *title, char_u *initdir)
3767{
3768 /* We fake this: Use a filter that doesn't select anything and a default
3769 * file name that won't be used. */
3770 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3771 initdir, (char_u *)_("Directory\t*.nothing\n"));
3772}
3773
3774/*
3775 * Pop open a file browser and return the file selected, in allocated memory,
3776 * or NULL if Cancel is hit.
3777 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3778 * title - Title message for the file browser dialog.
3779 * dflt - Default name of file.
3780 * ext - Default extension to be added to files without extensions.
3781 * initdir - directory in which to open the browser (NULL = current dir)
3782 * filter - Filter for matched files to choose from.
3783 *
3784 * Keep in sync with gui_mch_browseW() above!
3785 */
3786 char_u *
3787gui_mch_browse(
3788 int saving,
3789 char_u *title,
3790 char_u *dflt,
3791 char_u *ext,
3792 char_u *initdir,
3793 char_u *filter)
3794{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003795# ifdef FEAT_MBYTE
3796 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3797# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798 OPENFILENAME fileStruct;
3799 char_u fileBuf[MAXPATHL];
3800 char_u *initdirp = NULL;
3801 char_u *filterp;
3802 char_u *p;
3803
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804 if (dflt == NULL)
3805 fileBuf[0] = NUL;
3806 else
3807 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3808
3809 /* Convert the filter to Windows format. */
3810 filterp = convert_filter(filter);
3811
3812 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003813# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003814 /* be compatible with Windows NT 4.0 */
3815 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003816# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003818# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819
3820 fileStruct.lpstrTitle = (LPSTR)title;
3821 fileStruct.lpstrDefExt = (LPSTR)ext;
3822
3823 fileStruct.lpstrFile = (LPSTR)fileBuf;
3824 fileStruct.nMaxFile = MAXPATHL;
3825 fileStruct.lpstrFilter = (LPSTR)filterp;
3826 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3827 /* has an initial dir been specified? */
3828 if (initdir != NULL && *initdir != NUL)
3829 {
3830 /* Must have backslashes here, no matter what 'shellslash' says */
3831 initdirp = vim_strsave(initdir);
3832 if (initdirp != NULL)
3833 for (p = initdirp; *p != NUL; ++p)
3834 if (*p == '/')
3835 *p = '\\';
3836 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3837 }
3838
3839 /*
3840 * TODO: Allow selection of multiple files. Needs another arg to this
3841 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3842 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3843 * files that don't exist yet, so I haven't put it in. What about
3844 * OFN_PATHMUSTEXIST?
3845 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3846 */
3847 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003848# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003849 if (curbuf->b_p_bin)
3850 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003851# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852 if (saving)
3853 {
3854 if (!GetSaveFileName(&fileStruct))
3855 return NULL;
3856 }
3857 else
3858 {
3859 if (!GetOpenFileName(&fileStruct))
3860 return NULL;
3861 }
3862
3863 vim_free(filterp);
3864 vim_free(initdirp);
3865
3866 /* Give focus back to main window (when using MDI). */
3867 SetFocus(s_hwnd);
3868
3869 /* Shorten the file name if possible */
3870 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003871# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872}
3873#endif /* FEAT_BROWSE */
3874
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875 static void
3876_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003877 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 HDROP hDrop)
3879{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003880#define BUFPATHLEN _MAX_PATH
3881#define DRAGQVAL 0xFFFFFFFF
3882#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003884#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003885 char szFile[BUFPATHLEN];
3886 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3887 UINT i;
3888 char_u **fnames;
3889 POINT pt;
3890 int_u modifiers = 0;
3891
3892 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3893
3894 /* Obtain dropped position */
3895 DragQueryPoint(hDrop, &pt);
3896 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3897
3898 reset_VIsual();
3899
3900 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3901
3902 if (fnames != NULL)
3903 for (i = 0; i < cFiles; ++i)
3904 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003905#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3907 fnames[i] = utf16_to_enc(wszFile, NULL);
3908 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003909#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 {
3911 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3912 fnames[i] = vim_strsave((char_u *)szFile);
3913 }
3914 }
3915
3916 DragFinish(hDrop);
3917
3918 if (fnames != NULL)
3919 {
3920 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3921 modifiers |= MOUSE_SHIFT;
3922 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3923 modifiers |= MOUSE_CTRL;
3924 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3925 modifiers |= MOUSE_ALT;
3926
3927 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3928
3929 s_need_activate = TRUE;
3930 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931}
3932
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 static int
3934_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003935 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003936 HWND hwndCtl,
3937 UINT code,
3938 int pos)
3939{
3940 static UINT prev_code = 0; /* code of previous call */
3941 scrollbar_T *sb, *sb_info;
3942 long val;
3943 int dragging = FALSE;
3944 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003945 SCROLLINFO si;
3946
3947 si.cbSize = sizeof(si);
3948 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003949
3950 sb = gui_mswin_find_scrollbar(hwndCtl);
3951 if (sb == NULL)
3952 return 0;
3953
3954 if (sb->wp != NULL) /* Left or right scrollbar */
3955 {
3956 /*
3957 * Careful: need to get scrollbar info out of first (left) scrollbar
3958 * for window, but keep real scrollbar too because we must pass it to
3959 * gui_drag_scrollbar().
3960 */
3961 sb_info = &sb->wp->w_scrollbars[0];
3962 }
3963 else /* Bottom scrollbar */
3964 sb_info = sb;
3965 val = sb_info->value;
3966
3967 switch (code)
3968 {
3969 case SB_THUMBTRACK:
3970 val = pos;
3971 dragging = TRUE;
3972 if (sb->scroll_shift > 0)
3973 val <<= sb->scroll_shift;
3974 break;
3975 case SB_LINEDOWN:
3976 val++;
3977 break;
3978 case SB_LINEUP:
3979 val--;
3980 break;
3981 case SB_PAGEDOWN:
3982 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3983 break;
3984 case SB_PAGEUP:
3985 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3986 break;
3987 case SB_TOP:
3988 val = 0;
3989 break;
3990 case SB_BOTTOM:
3991 val = sb_info->max;
3992 break;
3993 case SB_ENDSCROLL:
3994 if (prev_code == SB_THUMBTRACK)
3995 {
3996 /*
3997 * "pos" only gives us 16-bit data. In case of large file,
3998 * use GetScrollPos() which returns 32-bit. Unfortunately it
3999 * is not valid while the scrollbar is being dragged.
4000 */
4001 val = GetScrollPos(hwndCtl, SB_CTL);
4002 if (sb->scroll_shift > 0)
4003 val <<= sb->scroll_shift;
4004 }
4005 break;
4006
4007 default:
4008 /* TRACE("Unknown scrollbar event %d\n", code); */
4009 return 0;
4010 }
4011 prev_code = code;
4012
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4014 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004015
4016 /*
4017 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4018 */
4019 if (sb->wp != NULL)
4020 {
4021 scrollbar_T *sba = sb->wp->w_scrollbars;
4022 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4023
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004024 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004025 }
4026
4027 /* Don't let us be interrupted here by another message. */
4028 s_busy_processing = TRUE;
4029
4030 /* When "allow_scrollbar" is FALSE still need to remember the new
4031 * position, but don't actually scroll by setting "dont_scroll". */
4032 dont_scroll = !allow_scrollbar;
4033
Bram Moolenaara338adc2018-01-31 20:51:47 +01004034 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004035 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004036 mch_enable_flush();
4037 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004038
4039 s_busy_processing = FALSE;
4040 dont_scroll = dont_scroll_save;
4041
4042 return 0;
4043}
4044
4045
4046/*
4047 * Get command line arguments.
4048 * Use "prog" as the name of the program and "cmdline" as the arguments.
4049 * Copy the arguments to allocated memory.
4050 * Return the number of arguments (including program name).
4051 * Return pointers to the arguments in "argvp". Memory is allocated with
4052 * malloc(), use free() instead of vim_free().
4053 * Return pointer to buffer in "tofree".
4054 * Returns zero when out of memory.
4055 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004056 int
4057get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4058{
4059 int i;
4060 char *p;
4061 char *progp;
4062 char *pnew = NULL;
4063 char *newcmdline;
4064 int inquote;
4065 int argc;
4066 char **argv = NULL;
4067 int round;
4068
4069 *tofree = NULL;
4070
4071#ifdef FEAT_MBYTE
4072 /* Try using the Unicode version first, it takes care of conversion when
4073 * 'encoding' is changed. */
4074 argc = get_cmd_argsW(&argv);
4075 if (argc != 0)
4076 goto done;
4077#endif
4078
4079 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4080 * non-space. */
4081 p = strrchr(prog, '.');
4082 if (p != NULL)
4083 *p = NUL;
4084 for (progp = prog; *progp == ' '; ++progp)
4085 ;
4086
4087 /* The command line is copied to allocated memory, so that we can change
4088 * it. Add the size of the string, the separating NUL and a terminating
4089 * NUL. */
4090 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4091 if (newcmdline == NULL)
4092 return 0;
4093
4094 /*
4095 * First round: count the number of arguments ("pnew" == NULL).
4096 * Second round: produce the arguments.
4097 */
4098 for (round = 1; round <= 2; ++round)
4099 {
4100 /* First argument is the program name. */
4101 if (pnew != NULL)
4102 {
4103 argv[0] = pnew;
4104 strcpy(pnew, progp);
4105 pnew += strlen(pnew);
4106 *pnew++ = NUL;
4107 }
4108
4109 /*
4110 * Isolate each argument and put it in argv[].
4111 */
4112 p = cmdline;
4113 argc = 1;
4114 while (*p != NUL)
4115 {
4116 inquote = FALSE;
4117 if (pnew != NULL)
4118 argv[argc] = pnew;
4119 ++argc;
4120 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4121 {
4122 /* Backslashes are only special when followed by a double
4123 * quote. */
4124 i = (int)strspn(p, "\\");
4125 if (p[i] == '"')
4126 {
4127 /* Halve the number of backslashes. */
4128 if (i > 1 && pnew != NULL)
4129 {
4130 vim_memset(pnew, '\\', i / 2);
4131 pnew += i / 2;
4132 }
4133
4134 /* Even nr of backslashes toggles quoting, uneven copies
4135 * the double quote. */
4136 if ((i & 1) == 0)
4137 inquote = !inquote;
4138 else if (pnew != NULL)
4139 *pnew++ = '"';
4140 p += i + 1;
4141 }
4142 else if (i > 0)
4143 {
4144 /* Copy span of backslashes unmodified. */
4145 if (pnew != NULL)
4146 {
4147 vim_memset(pnew, '\\', i);
4148 pnew += i;
4149 }
4150 p += i;
4151 }
4152 else
4153 {
4154 if (pnew != NULL)
4155 *pnew++ = *p;
4156#ifdef FEAT_MBYTE
4157 /* Can't use mb_* functions, because 'encoding' is not
4158 * initialized yet here. */
4159 if (IsDBCSLeadByte(*p))
4160 {
4161 ++p;
4162 if (pnew != NULL)
4163 *pnew++ = *p;
4164 }
4165#endif
4166 ++p;
4167 }
4168 }
4169
4170 if (pnew != NULL)
4171 *pnew++ = NUL;
4172 while (*p == ' ' || *p == '\t')
4173 ++p; /* advance until a non-space */
4174 }
4175
4176 if (round == 1)
4177 {
4178 argv = (char **)malloc((argc + 1) * sizeof(char *));
4179 if (argv == NULL )
4180 {
4181 free(newcmdline);
4182 return 0; /* malloc error */
4183 }
4184 pnew = newcmdline;
4185 *tofree = newcmdline;
4186 }
4187 }
4188
4189#ifdef FEAT_MBYTE
4190done:
4191#endif
4192 argv[argc] = NULL; /* NULL-terminated list */
4193 *argvp = argv;
4194 return argc;
4195}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
4197#ifdef FEAT_XPM_W32
4198# include "xpm_w32.h"
4199#endif
4200
4201#ifdef PROTO
4202# define WINAPI
4203#endif
4204
4205#ifdef __MINGW32__
4206/*
4207 * Add a lot of missing defines.
4208 * They are not always missing, we need the #ifndef's.
4209 */
4210# ifndef _cdecl
4211# define _cdecl
4212# endif
4213# ifndef IsMinimized
4214# define IsMinimized(hwnd) IsIconic(hwnd)
4215# endif
4216# ifndef IsMaximized
4217# define IsMaximized(hwnd) IsZoomed(hwnd)
4218# endif
4219# ifndef SelectFont
4220# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4221# endif
4222# ifndef GetStockBrush
4223# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4224# endif
4225# ifndef DeleteBrush
4226# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4227# endif
4228
4229# ifndef HANDLE_WM_RBUTTONDBLCLK
4230# define HANDLE_WM_RBUTTONDBLCLK(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_MBUTTONUP
4234# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4235 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4236# endif
4237# ifndef HANDLE_WM_MBUTTONDBLCLK
4238# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4239 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4240# endif
4241# ifndef HANDLE_WM_LBUTTONDBLCLK
4242# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4243 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4244# endif
4245# ifndef HANDLE_WM_RBUTTONDOWN
4246# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4247 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4248# endif
4249# ifndef HANDLE_WM_MOUSEMOVE
4250# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4251 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4252# endif
4253# ifndef HANDLE_WM_RBUTTONUP
4254# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4255 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4256# endif
4257# ifndef HANDLE_WM_MBUTTONDOWN
4258# define HANDLE_WM_MBUTTONDOWN(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_LBUTTONUP
4262# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4263 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4264# endif
4265# ifndef HANDLE_WM_LBUTTONDOWN
4266# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4267 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4268# endif
4269# ifndef HANDLE_WM_SYSCHAR
4270# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4271 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4272# endif
4273# ifndef HANDLE_WM_ACTIVATEAPP
4274# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4275 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4276# endif
4277# ifndef HANDLE_WM_WINDOWPOSCHANGING
4278# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4279 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4280# endif
4281# ifndef HANDLE_WM_VSCROLL
4282# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4283 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4284# endif
4285# ifndef HANDLE_WM_SETFOCUS
4286# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4287 ((fn)((hwnd), (HWND)(wParam)), 0L)
4288# endif
4289# ifndef HANDLE_WM_KILLFOCUS
4290# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4291 ((fn)((hwnd), (HWND)(wParam)), 0L)
4292# endif
4293# ifndef HANDLE_WM_HSCROLL
4294# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4295 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4296# endif
4297# ifndef HANDLE_WM_DROPFILES
4298# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4299 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4300# endif
4301# ifndef HANDLE_WM_CHAR
4302# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4303 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4304# endif
4305# ifndef HANDLE_WM_SYSDEADCHAR
4306# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4307 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4308# endif
4309# ifndef HANDLE_WM_DEADCHAR
4310# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4311 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4312# endif
4313#endif /* __MINGW32__ */
4314
4315
4316/* Some parameters for tearoff menus. All in pixels. */
4317#define TEAROFF_PADDING_X 2
4318#define TEAROFF_BUTTON_PAD_X 8
4319#define TEAROFF_MIN_WIDTH 200
4320#define TEAROFF_SUBMENU_LABEL ">>"
4321#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4322
4323
4324/* For the Intellimouse: */
4325#ifndef WM_MOUSEWHEEL
4326#define WM_MOUSEWHEEL 0x20a
4327#endif
4328
4329
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004330#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331# define ID_BEVAL_TOOLTIP 200
4332# define BEVAL_TEXT_LEN MAXPATHL
4333
Bram Moolenaar167632f2010-05-26 21:42:54 +02004334#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004335/* Work around old versions of basetsd.h which wrongly declares
4336 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004337# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004338# define UINT_PTR UINT
4339#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004340
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004341static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4342static void delete_tooltip(BalloonEval *beval);
4343static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004346static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004348
Bram Moolenaar82881492012-11-20 16:53:39 +01004349
4350/* cproto fails on missing include files */
4351#ifndef PROTO
4352
Bram Moolenaar45360022005-07-21 21:08:21 +00004353/*
4354 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004355 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004356 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004357# include <pshpack1.h>
4358
4359#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004360
4361typedef struct _DllVersionInfo
4362{
4363 DWORD cbSize;
4364 DWORD dwMajorVersion;
4365 DWORD dwMinorVersion;
4366 DWORD dwBuildNumber;
4367 DWORD dwPlatformID;
4368} DLLVERSIONINFO;
4369
Bram Moolenaar82881492012-11-20 16:53:39 +01004370#ifndef PROTO
4371# include <poppack.h>
4372#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004373
Bram Moolenaar45360022005-07-21 21:08:21 +00004374typedef struct tagTOOLINFOA_NEW
4375{
4376 UINT cbSize;
4377 UINT uFlags;
4378 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004379 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004380 RECT rect;
4381 HINSTANCE hinst;
4382 LPSTR lpszText;
4383 LPARAM lParam;
4384} TOOLINFO_NEW;
4385
4386typedef struct tagNMTTDISPINFO_NEW
4387{
4388 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004389 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004390 char szText[80];
4391 HINSTANCE hinst;
4392 UINT uFlags;
4393 LPARAM lParam;
4394} NMTTDISPINFO_NEW;
4395
Bram Moolenaar45360022005-07-21 21:08:21 +00004396typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4397#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004398# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#endif
4400
Bram Moolenaar45360022005-07-21 21:08:21 +00004401#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004402# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004403#endif
4404
4405#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004406# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004407#endif
4408
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004409#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004410
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004411#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4412/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4413 * it here if LPNMTTDISPINFO isn't defined.
4414 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4415 * _MSC_VER. */
4416# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4417typedef struct tagNMTTDISPINFOA {
4418 NMHDR hdr;
4419 LPSTR lpszText;
4420 char szText[80];
4421 HINSTANCE hinst;
4422 UINT uFlags;
4423 LPARAM lParam;
4424} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4425# define LPNMTTDISPINFO LPNMTTDISPINFOA
4426
4427# ifdef FEAT_MBYTE
4428typedef struct tagNMTTDISPINFOW {
4429 NMHDR hdr;
4430 LPWSTR lpszText;
4431 WCHAR szText[80];
4432 HINSTANCE hinst;
4433 UINT uFlags;
4434 LPARAM lParam;
4435} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4436# endif
4437# endif
4438#endif
4439
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004440#ifndef TTN_GETDISPINFOW
4441# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444/* Local variables: */
4445
4446#ifdef FEAT_MENU
4447static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450/*
4451 * Use the system font for dialogs and tear-off menus. Remove this line to
4452 * use DLG_FONT_NAME.
4453 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004454#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455
4456#define VIM_NAME "vim"
4457#define VIM_CLASS "Vim"
4458#define VIM_CLASSW L"Vim"
4459
4460/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4461 * thus there should be room for every dialog. For tearoffs it's made bigger
4462 * when needed. */
4463#define DLG_ALLOC_SIZE 16 * 1024
4464
4465/*
4466 * stuff for dialogs, menus, tearoffs etc.
4467 */
4468static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004469#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472static PWORD
4473add_dialog_element(
4474 PWORD p,
4475 DWORD lStyle,
4476 WORD x,
4477 WORD y,
4478 WORD w,
4479 WORD h,
4480 WORD Id,
4481 WORD clss,
4482 const char *caption);
4483static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004484static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004485#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488static void get_dialog_font_metrics(void);
4489
4490static int dialog_default_button = -1;
4491
4492/* Intellimouse support */
4493static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4496#ifdef FEAT_TOOLBAR
4497static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004498static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499static int get_toolbar_bitmap(vimmenu_T *menu);
4500#endif
4501
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004502#ifdef FEAT_GUI_TABLINE
4503static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004504static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004505#endif
4506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507#ifdef FEAT_MBYTE_IME
4508static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4509static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4510#endif
4511#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4512# ifdef NOIME
4513typedef struct tagCOMPOSITIONFORM {
4514 DWORD dwStyle;
4515 POINT ptCurrentPos;
4516 RECT rcArea;
4517} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4518typedef HANDLE HIMC;
4519# endif
4520
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004521static HINSTANCE hLibImm = NULL;
4522static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4523static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4524static HIMC (WINAPI *pImmGetContext)(HWND);
4525static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4526static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4527static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4528static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4529static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4530static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4531static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4532static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004533static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534static void dyn_imm_load(void);
4535#else
4536# define pImmGetCompositionStringA ImmGetCompositionStringA
4537# define pImmGetCompositionStringW ImmGetCompositionStringW
4538# define pImmGetContext ImmGetContext
4539# define pImmAssociateContext ImmAssociateContext
4540# define pImmReleaseContext ImmReleaseContext
4541# define pImmGetOpenStatus ImmGetOpenStatus
4542# define pImmSetOpenStatus ImmSetOpenStatus
4543# define pImmGetCompositionFont ImmGetCompositionFontA
4544# define pImmSetCompositionFont ImmSetCompositionFontA
4545# define pImmSetCompositionWindow ImmSetCompositionWindow
4546# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004547# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#endif
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550#ifdef FEAT_MENU
4551/*
4552 * Figure out how high the menu bar is at the moment.
4553 */
4554 static int
4555gui_mswin_get_menu_height(
4556 int fix_window) /* If TRUE, resize window if menu height changed */
4557{
4558 static int old_menu_height = -1;
4559
4560 RECT rc1, rc2;
4561 int num;
4562 int menu_height;
4563
4564 if (gui.menu_is_active)
4565 num = GetMenuItemCount(s_menuBar);
4566 else
4567 num = 0;
4568
4569 if (num == 0)
4570 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004571 else if (IsMinimized(s_hwnd))
4572 {
4573 /* The height of the menu cannot be determined while the window is
4574 * minimized. Take the previous height if the menu is changed in that
4575 * state, to avoid that Vim's vertical window size accidentally
4576 * increases due to the unaccounted-for menu height. */
4577 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 else
4580 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004581 /*
4582 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4583 * seem to have been set yet, so menu wraps in default window
4584 * width which is very narrow. Instead just return height of a
4585 * single menu item. Will still be wrong when the menu really
4586 * should wrap over more than one line.
4587 */
4588 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4589 if (gui.starting)
4590 menu_height = rc1.bottom - rc1.top + 1;
4591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004593 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4594 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596 }
4597
4598 if (fix_window && menu_height != old_menu_height)
4599 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004600 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004602 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 return menu_height;
4605}
4606#endif /*FEAT_MENU*/
4607
4608
4609/*
4610 * Setup for the Intellimouse
4611 */
4612 static void
4613init_mouse_wheel(void)
4614{
4615
4616#ifndef SPI_GETWHEELSCROLLLINES
4617# define SPI_GETWHEELSCROLLLINES 104
4618#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004619#ifndef SPI_SETWHEELSCROLLLINES
4620# define SPI_SETWHEELSCROLLLINES 105
4621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4624#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4625#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4626#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 mouse_scroll_lines = 3; /* reasonable default */
4629
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004630 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4631 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4632 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633}
4634
4635
4636/* Intellimouse wheel handler */
4637 static void
4638_OnMouseWheel(
4639 HWND hwnd,
4640 short zDelta)
4641{
4642/* Treat a mouse wheel event as if it were a scroll request */
4643 int i;
4644 int size;
4645 HWND hwndCtl;
4646
4647 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4648 {
4649 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4650 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4651 }
4652 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4653 {
4654 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4655 size = curwin->w_scrollbars[SBAR_LEFT].size;
4656 }
4657 else
4658 return;
4659
4660 size = curwin->w_height;
4661 if (mouse_scroll_lines == 0)
4662 init_mouse_wheel();
4663
Bram Moolenaara338adc2018-01-31 20:51:47 +01004664 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (mouse_scroll_lines > 0
4666 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4667 {
4668 for (i = mouse_scroll_lines; i > 0; --i)
4669 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4670 }
4671 else
4672 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004673 mch_enable_flush();
4674 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675}
4676
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004677#ifdef USE_SYSMENU_FONT
4678/*
4679 * Get Menu Font.
4680 * Return OK or FAIL.
4681 */
4682 static int
4683gui_w32_get_menu_font(LOGFONT *lf)
4684{
4685 NONCLIENTMETRICS nm;
4686
4687 nm.cbSize = sizeof(NONCLIENTMETRICS);
4688 if (!SystemParametersInfo(
4689 SPI_GETNONCLIENTMETRICS,
4690 sizeof(NONCLIENTMETRICS),
4691 &nm,
4692 0))
4693 return FAIL;
4694 *lf = nm.lfMenuFont;
4695 return OK;
4696}
4697#endif
4698
4699
4700#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4701/*
4702 * Set the GUI tabline font to the system menu font
4703 */
4704 static void
4705set_tabline_font(void)
4706{
4707 LOGFONT lfSysmenu;
4708 HFONT font;
4709 HWND hwnd;
4710 HDC hdc;
4711 HFONT hfntOld;
4712 TEXTMETRIC tm;
4713
4714 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4715 return;
4716
4717 font = CreateFontIndirect(&lfSysmenu);
4718
4719 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4720
4721 /*
4722 * Compute the height of the font used for the tab text
4723 */
4724 hwnd = GetDesktopWindow();
4725 hdc = GetWindowDC(hwnd);
4726 hfntOld = SelectFont(hdc, font);
4727
4728 GetTextMetrics(hdc, &tm);
4729
4730 SelectFont(hdc, hfntOld);
4731 ReleaseDC(hwnd, hdc);
4732
4733 /*
4734 * The space used by the tab border and the space between the tab label
4735 * and the tab border is included as 7.
4736 */
4737 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4738}
4739#endif
4740
Bram Moolenaar520470a2005-06-16 21:59:56 +00004741/*
4742 * Invoked when a setting was changed.
4743 */
4744 static LRESULT CALLBACK
4745_OnSettingChange(UINT n)
4746{
4747 if (n == SPI_SETWHEELSCROLLLINES)
4748 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4749 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004750#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4751 if (n == SPI_SETNONCLIENTMETRICS)
4752 set_tabline_font();
4753#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004754 return 0;
4755}
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#ifdef FEAT_NETBEANS_INTG
4758 static void
4759_OnWindowPosChanged(
4760 HWND hwnd,
4761 const LPWINDOWPOS lpwpos)
4762{
4763 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004764 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4767 || lpwpos->cx != cx || lpwpos->cy != cy))
4768 {
4769 x = lpwpos->x;
4770 y = lpwpos->y;
4771 cx = lpwpos->cx;
4772 cy = lpwpos->cy;
4773 netbeans_frame_moved(x, y);
4774 }
4775 /* Allow to send WM_SIZE and WM_MOVE */
4776 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4777}
4778#endif
4779
4780 static int
4781_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 UINT fwSide,
4783 LPRECT lprc)
4784{
4785 int w, h;
4786 int valid_w, valid_h;
4787 int w_offset, h_offset;
4788
4789 w = lprc->right - lprc->left;
4790 h = lprc->bottom - lprc->top;
4791 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4792 w_offset = w - valid_w;
4793 h_offset = h - valid_h;
4794
4795 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4796 || fwSide == WMSZ_BOTTOMLEFT)
4797 lprc->left += w_offset;
4798 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4799 || fwSide == WMSZ_BOTTOMRIGHT)
4800 lprc->right -= w_offset;
4801
4802 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4803 || fwSide == WMSZ_TOPRIGHT)
4804 lprc->top += h_offset;
4805 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4806 || fwSide == WMSZ_BOTTOMRIGHT)
4807 lprc->bottom -= h_offset;
4808 return TRUE;
4809}
4810
4811
4812
4813 static LRESULT CALLBACK
4814_WndProc(
4815 HWND hwnd,
4816 UINT uMsg,
4817 WPARAM wParam,
4818 LPARAM lParam)
4819{
4820 /*
4821 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4822 hwnd, uMsg, wParam, lParam);
4823 */
4824
4825 HandleMouseHide(uMsg, lParam);
4826
4827 s_uMsg = uMsg;
4828 s_wParam = wParam;
4829 s_lParam = lParam;
4830
4831 switch (uMsg)
4832 {
4833 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4834 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4835 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4836 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4837 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4838 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4839 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4840 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4841 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4842#ifdef FEAT_MENU
4843 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4844#endif
4845 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4846 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4847 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4848 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4849 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4850 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4851 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4852 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4853 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4854#ifdef FEAT_NETBEANS_INTG
4855 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4856#endif
4857
Bram Moolenaarafa24992006-03-27 20:58:26 +00004858#ifdef FEAT_GUI_TABLINE
4859 case WM_RBUTTONUP:
4860 {
4861 if (gui_mch_showing_tabline())
4862 {
4863 POINT pt;
4864 RECT rect;
4865
4866 /*
4867 * If the cursor is on the tabline, display the tab menu
4868 */
4869 GetCursorPos((LPPOINT)&pt);
4870 GetWindowRect(s_textArea, &rect);
4871 if (pt.y < rect.top)
4872 {
4873 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004874 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004875 }
4876 }
4877 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4878 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004879 case WM_LBUTTONDBLCLK:
4880 {
4881 /*
4882 * If the user double clicked the tabline, create a new tab
4883 */
4884 if (gui_mch_showing_tabline())
4885 {
4886 POINT pt;
4887 RECT rect;
4888
4889 GetCursorPos((LPPOINT)&pt);
4890 GetWindowRect(s_textArea, &rect);
4891 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004892 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004893 }
4894 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4895 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004896#endif
4897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 case WM_QUERYENDSESSION: /* System wants to go down. */
4899 gui_shell_closed(); /* Will exit when no changed buffers. */
4900 return FALSE; /* Do NOT allow system to go down. */
4901
4902 case WM_ENDSESSION:
4903 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004906 return 0L;
4907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 break;
4909
4910 case WM_CHAR:
4911 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4912 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004913 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return 0L;
4915
4916 case WM_SYSCHAR:
4917 /*
4918 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4919 * shortcut key, handle like a typed ALT key, otherwise call Windows
4920 * ALT key handling.
4921 */
4922#ifdef FEAT_MENU
4923 if ( !gui.menu_is_active
4924 || p_wak[0] == 'n'
4925 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4926 )
4927#endif
4928 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004929 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return 0L;
4931 }
4932#ifdef FEAT_MENU
4933 else
4934 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4935#endif
4936
4937 case WM_SYSKEYUP:
4938#ifdef FEAT_MENU
4939 /* This used to be done only when menu is active: ALT key is used for
4940 * that. But that caused problems when menu is disabled and using
4941 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4942 * are received, mouse pointer remains hidden. */
4943 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4944#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004945 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#endif
4947
4948 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004949 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 case WM_MOUSEWHEEL:
4952 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004953 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
Bram Moolenaar520470a2005-06-16 21:59:56 +00004955 /* Notification for change in SystemParametersInfo() */
4956 case WM_SETTINGCHANGE:
4957 return _OnSettingChange((UINT)wParam);
4958
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004959#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 case WM_NOTIFY:
4961 switch (((LPNMHDR) lParam)->code)
4962 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004963# ifdef FEAT_MBYTE
4964 case TTN_GETDISPINFOW:
4965# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004966 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004968 LPNMHDR hdr = (LPNMHDR)lParam;
4969 char_u *str = NULL;
4970 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004971
Bram Moolenaard23a8232018-02-10 18:45:26 +01004972 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004973
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004974# ifdef FEAT_GUI_TABLINE
4975 if (gui_mch_showing_tabline()
4976 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004977 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004978 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004979 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004980 * Mouse is over the GUI tabline. Display the
4981 * tooltip for the tab under the cursor
4982 *
4983 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004984 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004985 GetCursorPos(&pt);
4986 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004987 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004988 TCHITTESTINFO htinfo;
4989 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004990
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004991 /*
4992 * Get the tab under the cursor
4993 */
4994 htinfo.pt.x = pt.x;
4995 htinfo.pt.y = pt.y;
4996 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4997 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004998 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004999 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005000
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005001 tp = find_tabpage(idx + 1);
5002 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005003 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005004 get_tabline_label(tp, TRUE);
5005 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005006 }
5007 }
5008 }
5009 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005010# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005011# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005012# ifdef FEAT_GUI_TABLINE
5013 else
5014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005016 UINT idButton;
5017 vimmenu_T *pMenu;
5018
5019 idButton = (UINT) hdr->idFrom;
5020 pMenu = gui_mswin_find_menu(root_menu, idButton);
5021 if (pMenu)
5022 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005024# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005025 if (str != NULL)
5026 {
5027# ifdef FEAT_MBYTE
5028 if (hdr->code == TTN_GETDISPINFOW)
5029 {
5030 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5031
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005032 /* Set the maximum width, this also enables using
5033 * \n for line break. */
5034 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5035 0, 500);
5036
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005037 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005038 lpdi->lpszText = tt_text;
5039 /* can't show tooltip if failed */
5040 }
5041 else
5042# endif
5043 {
5044 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5045
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005046 /* Set the maximum width, this also enables using
5047 * \n for line break. */
5048 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5049 0, 500);
5050
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005051 if (STRLEN(str) < sizeof(lpdi->szText)
5052 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005053 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005054 sizeof(lpdi->szText) - 1);
5055 else
5056 lpdi->lpszText = tt_text;
5057 }
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005061# ifdef FEAT_GUI_TABLINE
5062 case TCN_SELCHANGE:
5063 if (gui_mch_showing_tabline()
5064 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005065 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005066 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005067 return 0L;
5068 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005069 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005070
5071 case NM_RCLICK:
5072 if (gui_mch_showing_tabline()
5073 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005074 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005075 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005076 return 0L;
5077 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005078 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005081# ifdef FEAT_GUI_TABLINE
5082 if (gui_mch_showing_tabline()
5083 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5084 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 break;
5087 }
5088 break;
5089#endif
5090#if defined(MENUHINTS) && defined(FEAT_MENU)
5091 case WM_MENUSELECT:
5092 if (((UINT) HIWORD(wParam)
5093 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5094 == MF_HILITE
5095 && (State & CMDLINE) == 0)
5096 {
5097 UINT idButton;
5098 vimmenu_T *pMenu;
5099 static int did_menu_tip = FALSE;
5100
5101 if (did_menu_tip)
5102 {
5103 msg_clr_cmdline();
5104 setcursor();
5105 out_flush();
5106 did_menu_tip = FALSE;
5107 }
5108
5109 idButton = (UINT)LOWORD(wParam);
5110 pMenu = gui_mswin_find_menu(root_menu, idButton);
5111 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5112 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5113 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005114 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005116 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 setcursor();
5118 out_flush();
5119 did_menu_tip = TRUE;
5120 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005121 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123 break;
5124#endif
5125 case WM_NCHITTEST:
5126 {
5127 LRESULT result;
5128 int x, y;
5129 int xPos = GET_X_LPARAM(lParam);
5130
5131 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5132 if (result == HTCLIENT)
5133 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005134#ifdef FEAT_GUI_TABLINE
5135 if (gui_mch_showing_tabline())
5136 {
5137 int yPos = GET_Y_LPARAM(lParam);
5138 RECT rct;
5139
5140 /* If the cursor is on the GUI tabline, don't process this
5141 * event */
5142 GetWindowRect(s_textArea, &rct);
5143 if (yPos < rct.top)
5144 return result;
5145 }
5146#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005147 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 xPos -= x;
5149
5150 if (xPos < 48) /* <VN> TODO should use system metric? */
5151 return HTBOTTOMLEFT;
5152 else
5153 return HTBOTTOMRIGHT;
5154 }
5155 else
5156 return result;
5157 }
5158 /* break; notreached */
5159
5160#ifdef FEAT_MBYTE_IME
5161 case WM_IME_NOTIFY:
5162 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5163 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005164 return 1L;
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 case WM_IME_COMPOSITION:
5167 if (!_OnImeComposition(hwnd, wParam, lParam))
5168 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005169 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
5171
5172 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005174 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 _OnFindRepl();
5177 }
5178#endif
5179 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5180 }
5181
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005182 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183}
5184
5185/*
5186 * End of call-back routines
5187 */
5188
5189/* parent window, if specified with -P */
5190HWND vim_parent_hwnd = NULL;
5191
5192 static BOOL CALLBACK
5193FindWindowTitle(HWND hwnd, LPARAM lParam)
5194{
5195 char buf[2048];
5196 char *title = (char *)lParam;
5197
5198 if (GetWindowText(hwnd, buf, sizeof(buf)))
5199 {
5200 if (strstr(buf, title) != NULL)
5201 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005202 /* Found it. Store the window ref. and quit searching if MDI
5203 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005205 if (vim_parent_hwnd != NULL)
5206 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 }
5209 return TRUE; /* continue searching */
5210}
5211
5212/*
5213 * Invoked for '-P "title"' argument: search for parent application to open
5214 * our window in.
5215 */
5216 void
5217gui_mch_set_parent(char *title)
5218{
5219 EnumWindows(FindWindowTitle, (LPARAM)title);
5220 if (vim_parent_hwnd == NULL)
5221 {
5222 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5223 mch_exit(2);
5224 }
5225}
5226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 static void
5229ole_error(char *arg)
5230{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005231 char buf[IOSIZE];
5232
5233 /* Can't use EMSG() here, we have not finished initialisation yet. */
5234 vim_snprintf(buf, IOSIZE,
5235 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5236 arg);
5237 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241/*
5242 * Parse the GUI related command-line arguments. Any arguments used are
5243 * deleted from argv, and *argc is decremented accordingly. This is called
5244 * when vim is started, whether or not the GUI has been started.
5245 */
5246 void
5247gui_mch_prepare(int *argc, char **argv)
5248{
5249 int silent = FALSE;
5250 int idx;
5251
5252 /* Check for special OLE command line parameters */
5253 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5254 {
5255 /* Check for a "-silent" argument first. */
5256 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5257 && (argv[2][0] == '-' || argv[2][0] == '/'))
5258 {
5259 silent = TRUE;
5260 idx = 2;
5261 }
5262 else
5263 idx = 1;
5264
5265 /* Register Vim as an OLE Automation server */
5266 if (STRICMP(argv[idx] + 1, "register") == 0)
5267 {
5268#ifdef FEAT_OLE
5269 RegisterMe(silent);
5270 mch_exit(0);
5271#else
5272 if (!silent)
5273 ole_error("register");
5274 mch_exit(2);
5275#endif
5276 }
5277
5278 /* Unregister Vim as an OLE Automation server */
5279 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5280 {
5281#ifdef FEAT_OLE
5282 UnregisterMe(!silent);
5283 mch_exit(0);
5284#else
5285 if (!silent)
5286 ole_error("unregister");
5287 mch_exit(2);
5288#endif
5289 }
5290
5291 /* Ignore an -embedding argument. It is only relevant if the
5292 * application wants to treat the case when it is started manually
5293 * differently from the case where it is started via automation (and
5294 * we don't).
5295 */
5296 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5297 {
5298#ifdef FEAT_OLE
5299 *argc = 1;
5300#else
5301 ole_error("embedding");
5302 mch_exit(2);
5303#endif
5304 }
5305 }
5306
5307#ifdef FEAT_OLE
5308 {
5309 int bDoRestart = FALSE;
5310
5311 InitOLE(&bDoRestart);
5312 /* automatically exit after registering */
5313 if (bDoRestart)
5314 mch_exit(0);
5315 }
5316#endif
5317
5318#ifdef FEAT_NETBEANS_INTG
5319 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005320 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 int arg;
5322
5323 for (arg = 1; arg < *argc; arg++)
5324 if (strncmp("-nb", argv[arg], 3) == 0)
5325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 netbeansArg = argv[arg];
5327 mch_memmove(&argv[arg], &argv[arg + 1],
5328 (--*argc - arg) * sizeof(char *));
5329 argv[*argc] = NULL;
5330 break; /* enough? */
5331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
5333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334}
5335
5336/*
5337 * Initialise the GUI. Create all the windows, set up all the call-backs
5338 * etc.
5339 */
5340 int
5341gui_mch_init(void)
5342{
5343 const char szVimWndClass[] = VIM_CLASS;
5344 const char szTextAreaClass[] = "VimTextArea";
5345 WNDCLASS wndclass;
5346#ifdef FEAT_MBYTE
5347 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005348 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 WNDCLASSW wndclassw;
5350#endif
5351#ifdef GLOBAL_IME
5352 ATOM atom;
5353#endif
5354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 /* Return here if the window was already opened (happens when
5356 * gui_mch_dialog() is called early). */
5357 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005358 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 /*
5361 * Load the tearoff bitmap
5362 */
5363#ifdef FEAT_TEAROFF
5364 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5365#endif
5366
5367 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5368 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5369#ifdef FEAT_MENU
5370 gui.menu_height = 0; /* Windows takes care of this */
5371#endif
5372 gui.border_width = 0;
5373
5374 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5375
5376#ifdef FEAT_MBYTE
5377 /* First try using the wide version, so that we can use any title.
5378 * Otherwise only characters in the active codepage will work. */
5379 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5380 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005381 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 wndclassw.lpfnWndProc = _WndProc;
5383 wndclassw.cbClsExtra = 0;
5384 wndclassw.cbWndExtra = 0;
5385 wndclassw.hInstance = s_hinst;
5386 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5387 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5388 wndclassw.hbrBackground = s_brush;
5389 wndclassw.lpszMenuName = NULL;
5390 wndclassw.lpszClassName = szVimWndClassW;
5391
5392 if ((
5393#ifdef GLOBAL_IME
5394 atom =
5395#endif
5396 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005397 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 else
5399 wide_WindowProc = TRUE;
5400 }
5401
5402 if (!wide_WindowProc)
5403#endif
5404
5405 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5406 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 wndclass.lpfnWndProc = _WndProc;
5409 wndclass.cbClsExtra = 0;
5410 wndclass.cbWndExtra = 0;
5411 wndclass.hInstance = s_hinst;
5412 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5413 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5414 wndclass.hbrBackground = s_brush;
5415 wndclass.lpszMenuName = NULL;
5416 wndclass.lpszClassName = szVimWndClass;
5417
5418 if ((
5419#ifdef GLOBAL_IME
5420 atom =
5421#endif
5422 RegisterClass(&wndclass)) == 0)
5423 return FAIL;
5424 }
5425
5426 if (vim_parent_hwnd != NULL)
5427 {
5428#ifdef HAVE_TRY_EXCEPT
5429 __try
5430 {
5431#endif
5432 /* Open inside the specified parent window.
5433 * TODO: last argument should point to a CLIENTCREATESTRUCT
5434 * structure. */
5435 s_hwnd = CreateWindowEx(
5436 WS_EX_MDICHILD,
5437 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005438 WS_OVERLAPPEDWINDOW | WS_CHILD
5439 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5441 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5442 100, /* Any value will do */
5443 100, /* Any value will do */
5444 vim_parent_hwnd, NULL,
5445 s_hinst, NULL);
5446#ifdef HAVE_TRY_EXCEPT
5447 }
5448 __except(EXCEPTION_EXECUTE_HANDLER)
5449 {
5450 /* NOP */
5451 }
5452#endif
5453 if (s_hwnd == NULL)
5454 {
5455 EMSG(_("E672: Unable to open window inside MDI application"));
5456 mch_exit(2);
5457 }
5458 }
5459 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005460 {
5461 /* If the provided windowid is not valid reset it to zero, so that it
5462 * is ignored and we open our own window. */
5463 if (IsWindow((HWND)win_socket_id) <= 0)
5464 win_socket_id = 0;
5465
5466 /* Create a window. If win_socket_id is not zero without border and
5467 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005469 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005470 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5471 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005472 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5473 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5474 100, /* Any value will do */
5475 100, /* Any value will do */
5476 NULL, NULL,
5477 s_hinst, NULL);
5478 if (s_hwnd != NULL && win_socket_id != 0)
5479 {
5480 SetParent(s_hwnd, (HWND)win_socket_id);
5481 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5482 }
5483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
5485 if (s_hwnd == NULL)
5486 return FAIL;
5487
5488#ifdef GLOBAL_IME
5489 global_ime_init(atom, s_hwnd);
5490#endif
5491#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5492 dyn_imm_load();
5493#endif
5494
5495 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005496#ifdef FEAT_MBYTE
5497 if (wide_WindowProc)
5498 {
5499 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5500 {
5501 wndclassw.style = CS_OWNDC;
5502 wndclassw.lpfnWndProc = _TextAreaWndProc;
5503 wndclassw.cbClsExtra = 0;
5504 wndclassw.cbWndExtra = 0;
5505 wndclassw.hInstance = s_hinst;
5506 wndclassw.hIcon = NULL;
5507 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5508 wndclassw.hbrBackground = NULL;
5509 wndclassw.lpszMenuName = NULL;
5510 wndclassw.lpszClassName = szTextAreaClassW;
5511
5512 if (RegisterClassW(&wndclassw) == 0)
5513 return FAIL;
5514 }
5515 }
5516 else
5517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5519 {
5520 wndclass.style = CS_OWNDC;
5521 wndclass.lpfnWndProc = _TextAreaWndProc;
5522 wndclass.cbClsExtra = 0;
5523 wndclass.cbWndExtra = 0;
5524 wndclass.hInstance = s_hinst;
5525 wndclass.hIcon = NULL;
5526 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5527 wndclass.hbrBackground = NULL;
5528 wndclass.lpszMenuName = NULL;
5529 wndclass.lpszClassName = szTextAreaClass;
5530
5531 if (RegisterClass(&wndclass) == 0)
5532 return FAIL;
5533 }
5534 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005535 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 szTextAreaClass, "Vim text area",
5537 WS_CHILD | WS_VISIBLE, 0, 0,
5538 100, /* Any value will do for now */
5539 100, /* Any value will do for now */
5540 s_hwnd, NULL,
5541 s_hinst, NULL);
5542
5543 if (s_textArea == NULL)
5544 return FAIL;
5545
Bram Moolenaar20321902016-02-17 12:30:17 +01005546#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005547 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5548 {
5549 HANDLE hIcon = NULL;
5550
5551 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005552 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005553 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005554#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556#ifdef FEAT_MENU
5557 s_menuBar = CreateMenu();
5558#endif
5559 s_hdc = GetDC(s_textArea);
5560
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 /* Do we need to bother with this? */
5564 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5565
5566 /* Get background/foreground colors from the system */
5567 gui_mch_def_colors();
5568
5569 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5570 * file) */
5571 set_normal_colors();
5572
5573 /*
5574 * Check that none of the colors are the same as the background color.
5575 * Then store the current values as the defaults.
5576 */
5577 gui_check_colors();
5578 gui.def_norm_pixel = gui.norm_pixel;
5579 gui.def_back_pixel = gui.back_pixel;
5580
5581 /* Get the colors for the highlight groups (gui_check_colors() might have
5582 * changed them) */
5583 highlight_gui_started();
5584
5585 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005586 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005588 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 /*
5591 * Set up for Intellimouse processing
5592 */
5593 init_mouse_wheel();
5594
5595 /*
5596 * compute a couple of metrics used for the dialogs
5597 */
5598 get_dialog_font_metrics();
5599#ifdef FEAT_TOOLBAR
5600 /*
5601 * Create the toolbar
5602 */
5603 initialise_toolbar();
5604#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005605#ifdef FEAT_GUI_TABLINE
5606 /*
5607 * Create the tabline
5608 */
5609 initialise_tabline();
5610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#ifdef MSWIN_FIND_REPLACE
5612 /*
5613 * Initialise the dialog box stuff
5614 */
5615 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5616
5617 /* Initialise the struct */
5618 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005619 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005621 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5623 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5624 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005625# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005626 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5627 s_findrep_struct_w.lpstrFindWhat =
5628 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5629 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5630 s_findrep_struct_w.lpstrReplaceWith =
5631 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5632 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5633 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5634 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005638#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005639# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5640/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5641# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005642# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005643# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005644# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005645 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005646 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005647#endif
5648
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005649#ifdef FEAT_RENDER_OPTIONS
5650 if (p_rop)
5651 (void)gui_mch_set_rendering_options(p_rop);
5652#endif
5653
Bram Moolenaar748bf032005-02-02 23:04:36 +00005654theend:
5655 /* Display any pending error messages */
5656 display_errors();
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 return OK;
5659}
5660
5661/*
5662 * Get the size of the screen, taking position on multiple monitors into
5663 * account (if supported).
5664 */
5665 static void
5666get_work_area(RECT *spi_rect)
5667{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005668 HMONITOR mon;
5669 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005671 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005672 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005673 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005675 moninfo.cbSize = sizeof(MONITORINFO);
5676 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005678 *spi_rect = moninfo.rcWork;
5679 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 }
5682 /* this is the old method... */
5683 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5684}
5685
5686/*
5687 * Set the size of the window to the given width and height in pixels.
5688 */
5689 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005690gui_mch_set_shellsize(
5691 int width,
5692 int height,
5693 int min_width UNUSED,
5694 int min_height UNUSED,
5695 int base_width UNUSED,
5696 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005697 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
5699 RECT workarea_rect;
5700 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 WINDOWPLACEMENT wndpl;
5702
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005703 /* Try to keep window completely on screen. */
5704 /* Get position of the screen work area. This is the part that is not
5705 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 get_work_area(&workarea_rect);
5707
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005708 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005709 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 wndpl.length = sizeof(WINDOWPLACEMENT);
5711 GetWindowPlacement(s_hwnd, &wndpl);
5712
5713 /* Resizing a maximized window looks very strange, unzoom it first.
5714 * But don't do it when still starting up, it may have been requested in
5715 * the shortcut. */
5716 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5717 {
5718 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5719 /* Need to get the settings of the normal window. */
5720 GetWindowPlacement(s_hwnd, &wndpl);
5721 }
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005724 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005725 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005726 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005727 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 + GetSystemMetrics(SM_CYCAPTION)
5729#ifdef FEAT_MENU
5730 + gui_mswin_get_menu_height(FALSE)
5731#endif
5732 ;
5733
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005734 /* The following should take care of keeping Vim on the same monitor, no
5735 * matter if the secondary monitor is left or right of the primary
5736 * monitor. */
5737 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5738 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005739
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005740 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005741 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005742 && wndpl.rcNormalPosition.right > workarea_rect.right)
5743 OffsetRect(&wndpl.rcNormalPosition,
5744 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005746 if ((direction & RESIZE_HOR)
5747 && wndpl.rcNormalPosition.left < workarea_rect.left)
5748 OffsetRect(&wndpl.rcNormalPosition,
5749 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
Bram Moolenaarafa24992006-03-27 20:58:26 +00005751 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005752 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5753 OffsetRect(&wndpl.rcNormalPosition,
5754 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005756 if ((direction & RESIZE_VERT)
5757 && wndpl.rcNormalPosition.top < workarea_rect.top)
5758 OffsetRect(&wndpl.rcNormalPosition,
5759 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 /* set window position - we should use SetWindowPlacement rather than
5762 * SetWindowPos as the MSDN docs say the coord systems returned by
5763 * these two are not compatible. */
5764 SetWindowPlacement(s_hwnd, &wndpl);
5765
5766 SetActiveWindow(s_hwnd);
5767 SetFocus(s_hwnd);
5768
5769#ifdef FEAT_MENU
5770 /* Menu may wrap differently now */
5771 gui_mswin_get_menu_height(!gui.starting);
5772#endif
5773}
5774
5775
5776 void
5777gui_mch_set_scrollbar_thumb(
5778 scrollbar_T *sb,
5779 long val,
5780 long size,
5781 long max)
5782{
5783 SCROLLINFO info;
5784
5785 sb->scroll_shift = 0;
5786 while (max > 32767)
5787 {
5788 max = (max + 1) >> 1;
5789 val >>= 1;
5790 size >>= 1;
5791 ++sb->scroll_shift;
5792 }
5793
5794 if (sb->scroll_shift > 0)
5795 ++size;
5796
5797 info.cbSize = sizeof(info);
5798 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5799 info.nPos = val;
5800 info.nMin = 0;
5801 info.nMax = max;
5802 info.nPage = size;
5803 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5804}
5805
5806
5807/*
5808 * Set the current text font.
5809 */
5810 void
5811gui_mch_set_font(GuiFont font)
5812{
5813 gui.currFont = font;
5814}
5815
5816
5817/*
5818 * Set the current text foreground color.
5819 */
5820 void
5821gui_mch_set_fg_color(guicolor_T color)
5822{
5823 gui.currFgColor = color;
5824}
5825
5826/*
5827 * Set the current text background color.
5828 */
5829 void
5830gui_mch_set_bg_color(guicolor_T color)
5831{
5832 gui.currBgColor = color;
5833}
5834
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005835/*
5836 * Set the current text special color.
5837 */
5838 void
5839gui_mch_set_sp_color(guicolor_T color)
5840{
5841 gui.currSpColor = color;
5842}
5843
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005844#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845/*
5846 * Multi-byte handling, originally by Sung-Hoon Baek.
5847 * First static functions (no prototypes generated).
5848 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005849# ifdef _MSC_VER
5850# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5851# endif
5852# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853
5854/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 * handle WM_IME_NOTIFY message
5856 */
5857 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005858_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 LRESULT lResult = 0;
5861 HIMC hImc;
5862
5863 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5864 return lResult;
5865 switch (dwCommand)
5866 {
5867 case IMN_SETOPENSTATUS:
5868 if (pImmGetOpenStatus(hImc))
5869 {
5870 pImmSetCompositionFont(hImc, &norm_logfont);
5871 im_set_position(gui.row, gui.col);
5872
5873 /* Disable langmap */
5874 State &= ~LANGMAP;
5875 if (State & INSERT)
5876 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005877#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /* Unshown 'keymap' in status lines */
5879 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5880 {
5881 /* Save cursor position */
5882 int old_row = gui.row;
5883 int old_col = gui.col;
5884
5885 // This must be called here before
5886 // status_redraw_curbuf(), otherwise the mode
5887 // message may appear in the wrong position.
5888 showmode();
5889 status_redraw_curbuf();
5890 update_screen(0);
5891 /* Restore cursor position */
5892 gui.row = old_row;
5893 gui.col = old_col;
5894 }
5895#endif
5896 }
5897 }
5898 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005899 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 lResult = 0;
5901 break;
5902 }
5903 pImmReleaseContext(hWnd, hImc);
5904 return lResult;
5905}
5906
5907 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005908_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 char_u *ret;
5911 int len;
5912
5913 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5914 return 0;
5915
5916 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5917 if (ret != NULL)
5918 {
5919 add_to_input_buf_csi(ret, len);
5920 vim_free(ret);
5921 return 1;
5922 }
5923 return 0;
5924}
5925
5926/*
5927 * get the current composition string, in UCS-2; *lenp is the number of
5928 * *lenp is the number of Unicode characters.
5929 */
5930 static short_u *
5931GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5932{
5933 LONG ret;
5934 LPWSTR wbuf = NULL;
5935 char_u *buf;
5936
5937 if (!pImmGetContext)
5938 return NULL; /* no imm32.dll */
5939
5940 /* Try Unicode; this'll always work on NT regardless of codepage. */
5941 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5942 if (ret == 0)
5943 return NULL; /* empty */
5944
5945 if (ret > 0)
5946 {
5947 /* Allocate the requested buffer plus space for the NUL character. */
5948 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5949 if (wbuf != NULL)
5950 {
5951 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5952 *lenp = ret / sizeof(WCHAR);
5953 }
5954 return (short_u *)wbuf;
5955 }
5956
5957 /* ret < 0; we got an error, so try the ANSI version. This'll work
5958 * on 9x/ME, but only if the codepage happens to be set to whatever
5959 * we're inputting. */
5960 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5961 if (ret <= 0)
5962 return NULL; /* empty or error */
5963
5964 buf = alloc(ret);
5965 if (buf == NULL)
5966 return NULL;
5967 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5968
5969 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005970 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 vim_free(buf);
5972
5973 return (short_u *)wbuf;
5974}
5975
5976/*
5977 * void GetResultStr()
5978 *
5979 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5980 * get complete composition string
5981 */
5982 static char_u *
5983GetResultStr(HWND hwnd, int GCS, int *lenp)
5984{
5985 HIMC hIMC; /* Input context handle. */
5986 short_u *buf = NULL;
5987 char_u *convbuf = NULL;
5988
5989 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5990 return NULL;
5991
5992 /* Reads in the composition string. */
5993 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5994 if (buf == NULL)
5995 return NULL;
5996
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005997 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 pImmReleaseContext(hwnd, hIMC);
5999 vim_free(buf);
6000 return convbuf;
6001}
6002#endif
6003
6004/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006005#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006
6007/*
6008 * set font to IM.
6009 */
6010 void
6011im_set_font(LOGFONT *lf)
6012{
6013 HIMC hImc;
6014
6015 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6016 {
6017 pImmSetCompositionFont(hImc, lf);
6018 pImmReleaseContext(s_hwnd, hImc);
6019 }
6020}
6021
6022/*
6023 * Notify cursor position to IM.
6024 */
6025 void
6026im_set_position(int row, int col)
6027{
6028 HIMC hImc;
6029
6030 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6031 {
6032 COMPOSITIONFORM cfs;
6033
6034 cfs.dwStyle = CFS_POINT;
6035 cfs.ptCurrentPos.x = FILL_X(col);
6036 cfs.ptCurrentPos.y = FILL_Y(row);
6037 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6038 pImmSetCompositionWindow(hImc, &cfs);
6039
6040 pImmReleaseContext(s_hwnd, hImc);
6041 }
6042}
6043
6044/*
6045 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6046 */
6047 void
6048im_set_active(int active)
6049{
6050 HIMC hImc;
6051 static HIMC hImcOld = (HIMC)0;
6052
6053 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6054 {
6055 if (p_imdisable)
6056 {
6057 if (hImcOld == (HIMC)0)
6058 {
6059 hImcOld = pImmGetContext(s_hwnd);
6060 if (hImcOld)
6061 pImmAssociateContext(s_hwnd, (HIMC)0);
6062 }
6063 active = FALSE;
6064 }
6065 else if (hImcOld != (HIMC)0)
6066 {
6067 pImmAssociateContext(s_hwnd, hImcOld);
6068 hImcOld = (HIMC)0;
6069 }
6070
6071 hImc = pImmGetContext(s_hwnd);
6072 if (hImc)
6073 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006074 /*
6075 * for Korean ime
6076 */
6077 HKL hKL = GetKeyboardLayout(0);
6078
6079 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6080 {
6081 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6082 static BOOL bSaved = FALSE;
6083
6084 if (active)
6085 {
6086 /* if we have a saved conversion status, restore it */
6087 if (bSaved)
6088 pImmSetConversionStatus(hImc, dwConversionSaved,
6089 dwSentenceSaved);
6090 bSaved = FALSE;
6091 }
6092 else
6093 {
6094 /* save conversion status and disable korean */
6095 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6096 &dwSentenceSaved))
6097 {
6098 bSaved = TRUE;
6099 pImmSetConversionStatus(hImc,
6100 dwConversionSaved & ~(IME_CMODE_NATIVE
6101 | IME_CMODE_FULLSHAPE),
6102 dwSentenceSaved);
6103 }
6104 }
6105 }
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 pImmSetOpenStatus(hImc, active);
6108 pImmReleaseContext(s_hwnd, hImc);
6109 }
6110 }
6111}
6112
6113/*
6114 * Get IM status. When IM is on, return not 0. Else return 0.
6115 */
6116 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006117im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119 int status = 0;
6120 HIMC hImc;
6121
6122 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6123 {
6124 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6125 pImmReleaseContext(s_hwnd, hImc);
6126 }
6127 return status;
6128}
6129
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006130#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131
6132#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6133/* Win32 with GLOBAL IME */
6134
6135/*
6136 * Notify cursor position to IM.
6137 */
6138 void
6139im_set_position(int row, int col)
6140{
6141 /* Win32 with GLOBAL IME */
6142 POINT p;
6143
6144 p.x = FILL_X(col);
6145 p.y = FILL_Y(row);
6146 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6147 global_ime_set_position(&p);
6148}
6149
6150/*
6151 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6152 */
6153 void
6154im_set_active(int active)
6155{
6156 global_ime_set_status(active);
6157}
6158
6159/*
6160 * Get IM status. When IM is on, return not 0. Else return 0.
6161 */
6162 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006163im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 return global_ime_get_status();
6166}
6167#endif
6168
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006169#ifdef FEAT_MBYTE
6170/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006171 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006172 */
6173 static void
6174latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6175{
6176 int c;
6177
Bram Moolenaarca003e12006-03-17 23:19:38 +00006178 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006179 {
6180 c = *text++;
6181 switch (c)
6182 {
6183 case 0xa4: c = 0x20ac; break; /* euro */
6184 case 0xa6: c = 0x0160; break; /* S hat */
6185 case 0xa8: c = 0x0161; break; /* S -hat */
6186 case 0xb4: c = 0x017d; break; /* Z hat */
6187 case 0xb8: c = 0x017e; break; /* Z -hat */
6188 case 0xbc: c = 0x0152; break; /* OE */
6189 case 0xbd: c = 0x0153; break; /* oe */
6190 case 0xbe: c = 0x0178; break; /* Y */
6191 }
6192 *unicodebuf++ = c;
6193 }
6194}
6195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
6197#ifdef FEAT_RIGHTLEFT
6198/*
6199 * What is this for? In the case where you are using Win98 or Win2K or later,
6200 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6201 * reverses the string sent to the TextOut... family. This sucks, because we
6202 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6203 * way to tell Windblows not to do this!
6204 *
6205 * The short of it is that this 'RevOut' only gets called if you are running
6206 * one of the new, "improved" MS OSes, and only if you are running in
6207 * 'rightleft' mode. It makes display take *slightly* longer, but not
6208 * noticeably so.
6209 */
6210 static void
6211RevOut( HDC s_hdc,
6212 int col,
6213 int row,
6214 UINT foptions,
6215 CONST RECT *pcliprect,
6216 LPCTSTR text,
6217 UINT len,
6218 CONST INT *padding)
6219{
6220 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006222 for (ix = 0; ix < (int)len; ++ix)
6223 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6224 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225}
6226#endif
6227
Bram Moolenaar92467d32017-12-05 13:22:16 +01006228 static void
6229draw_line(
6230 int x1,
6231 int y1,
6232 int x2,
6233 int y2,
6234 COLORREF color)
6235{
6236#if defined(FEAT_DIRECTX)
6237 if (IS_ENABLE_DIRECTX())
6238 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6239 else
6240#endif
6241 {
6242 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6243 HPEN old_pen = SelectObject(s_hdc, hpen);
6244 MoveToEx(s_hdc, x1, y1, NULL);
6245 /* Note: LineTo() excludes the last pixel in the line. */
6246 LineTo(s_hdc, x2, y2);
6247 DeleteObject(SelectObject(s_hdc, old_pen));
6248 }
6249}
6250
6251 static void
6252set_pixel(
6253 int x,
6254 int y,
6255 COLORREF color)
6256{
6257#if defined(FEAT_DIRECTX)
6258 if (IS_ENABLE_DIRECTX())
6259 DWriteContext_SetPixel(s_dwc, x, y, color);
6260 else
6261#endif
6262 SetPixel(s_hdc, x, y, color);
6263}
6264
6265 static void
6266fill_rect(
6267 const RECT *rcp,
6268 HBRUSH hbr,
6269 COLORREF color)
6270{
6271#if defined(FEAT_DIRECTX)
6272 if (IS_ENABLE_DIRECTX())
6273 DWriteContext_FillRect(s_dwc, rcp, color);
6274 else
6275#endif
6276 {
6277 HBRUSH hbr2;
6278
6279 if (hbr == NULL)
6280 hbr2 = CreateSolidBrush(color);
6281 else
6282 hbr2 = hbr;
6283 FillRect(s_hdc, rcp, hbr2);
6284 if (hbr == NULL)
6285 DeleteBrush(hbr2);
6286 }
6287}
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 void
6290gui_mch_draw_string(
6291 int row,
6292 int col,
6293 char_u *text,
6294 int len,
6295 int flags)
6296{
6297 static int *padding = NULL;
6298 static int pad_size = 0;
6299 int i;
6300 const RECT *pcliprect = NULL;
6301 UINT foptions = 0;
6302#ifdef FEAT_MBYTE
6303 static WCHAR *unicodebuf = NULL;
6304 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006305 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 int n = 0;
6307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 int y;
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 /*
6311 * Italic and bold text seems to have an extra row of pixels at the bottom
6312 * (below where the bottom of the character should be). If we draw the
6313 * characters with a solid background, the top row of pixels in the
6314 * character below will be overwritten. We can fix this by filling in the
6315 * background ourselves, to the correct character proportions, and then
6316 * writing the character in transparent mode. Still have a problem when
6317 * the character is "_", which gets written on to the character below.
6318 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6319 * pixel in their slots, which fixes the problem with the bottom row of
6320 * pixels. We still need this code because otherwise the top row of pixels
6321 * becomes a problem. - webb.
6322 */
6323 static HBRUSH hbr_cache[2] = {NULL, NULL};
6324 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6325 static int brush_lru = 0;
6326 HBRUSH hbr;
6327 RECT rc;
6328
6329 if (!(flags & DRAW_TRANSP))
6330 {
6331 /*
6332 * Clear background first.
6333 * Note: FillRect() excludes right and bottom of rectangle.
6334 */
6335 rc.left = FILL_X(col);
6336 rc.top = FILL_Y(row);
6337#ifdef FEAT_MBYTE
6338 if (has_mbyte)
6339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006341 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 }
6343 else
6344#endif
6345 rc.right = FILL_X(col + len);
6346 rc.bottom = FILL_Y(row + 1);
6347
6348 /* Cache the created brush, that saves a lot of time. We need two:
6349 * one for cursor background and one for the normal background. */
6350 if (gui.currBgColor == brush_color[0])
6351 {
6352 hbr = hbr_cache[0];
6353 brush_lru = 1;
6354 }
6355 else if (gui.currBgColor == brush_color[1])
6356 {
6357 hbr = hbr_cache[1];
6358 brush_lru = 0;
6359 }
6360 else
6361 {
6362 if (hbr_cache[brush_lru] != NULL)
6363 DeleteBrush(hbr_cache[brush_lru]);
6364 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6365 brush_color[brush_lru] = gui.currBgColor;
6366 hbr = hbr_cache[brush_lru];
6367 brush_lru = !brush_lru;
6368 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006369
Bram Moolenaar92467d32017-12-05 13:22:16 +01006370 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372 SetBkMode(s_hdc, TRANSPARENT);
6373
6374 /*
6375 * When drawing block cursor, prevent inverted character spilling
6376 * over character cell (can happen with bold/italic)
6377 */
6378 if (flags & DRAW_CURSOR)
6379 {
6380 pcliprect = &rc;
6381 foptions = ETO_CLIPPED;
6382 }
6383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 SetTextColor(s_hdc, gui.currFgColor);
6385 SelectFont(s_hdc, gui.currFont);
6386
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006387#ifdef FEAT_DIRECTX
6388 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006389 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006390#endif
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6393 {
6394 vim_free(padding);
6395 pad_size = Columns;
6396
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006397 /* Don't give an out-of-memory message here, it would call us
6398 * recursively. */
6399 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (padding != NULL)
6401 for (i = 0; i < pad_size; i++)
6402 padding[i] = gui.char_width;
6403 }
6404
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 /*
6406 * We have to provide the padding argument because italic and bold versions
6407 * of fixed-width fonts are often one pixel or so wider than their normal
6408 * versions.
6409 * No check for DRAW_BOLD, Windows will have done it already.
6410 */
6411
6412#ifdef FEAT_MBYTE
6413 /* Check if there are any UTF-8 characters. If not, use normal text
6414 * output to speed up output. */
6415 if (enc_utf8)
6416 for (n = 0; n < len; ++n)
6417 if (text[n] >= 0x80)
6418 break;
6419
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006420# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006421 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6422 * required that unicode drawing routine, currently. So this forces it
6423 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006424 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006425 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006426# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006429 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006431 if ((enc_utf8
6432 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6433 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 && (unicodebuf == NULL || len > unibuflen))
6435 {
6436 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006437 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438
6439 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006440 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441
6442 unibuflen = len;
6443 }
6444
6445 if (enc_utf8 && n < len && unicodebuf != NULL)
6446 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006447 /* Output UTF-8 characters. Composing characters should be
6448 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006449 int i;
6450 int wlen; /* string length in words */
6451 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 int cells; /* cell width of string up to composing char */
6453 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006454 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006456 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006457 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006459 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006461 c = utf_ptr2char(text + i);
6462 if (c >= 0x10000)
6463 {
6464 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006465 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6466 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006467 }
6468 else
6469 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006470 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006471 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006472
6473 if (utf_iscomposing(c))
6474 cw = 0;
6475 else
6476 {
6477 cw = utf_char2cells(c);
6478 if (cw > 2) /* don't use 4 for unprintable char */
6479 cw = 1;
6480 }
6481
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (unicodepdy != NULL)
6483 {
6484 /* Use unicodepdy to make characters fit as we expect, even
6485 * when the font uses different widths (e.g., bold character
6486 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006487 if (c >= 0x10000)
6488 {
6489 unicodepdy[wlen - 2] = cw * gui.char_width;
6490 unicodepdy[wlen - 1] = 0;
6491 }
6492 else
6493 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006496 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006497 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006499# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006500 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006501 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006502 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006503 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006504 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006505 gui.char_width, gui.currFgColor,
6506 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006507 }
6508 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006509# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006510 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6511 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 len = cells; /* used for underlining */
6513 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006514 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 {
6516 /* If we want to display codepage data, and the current CP is not the
6517 * ANSI one, we need to go via Unicode. */
6518 if (unicodebuf != NULL)
6519 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006520 if (enc_latin9)
6521 latin9_to_ucs(text, len, unicodebuf);
6522 else
6523 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 MB_PRECOMPOSED,
6525 (char *)text, len,
6526 (LPWSTR)unicodebuf, unibuflen);
6527 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006528 {
6529 /* Use unicodepdy to make characters fit as we expect, even
6530 * when the font uses different widths (e.g., bold character
6531 * is wider). */
6532 if (unicodepdy != NULL)
6533 {
6534 int i;
6535 int cw;
6536
6537 for (i = 0; i < len; ++i)
6538 {
6539 cw = utf_char2cells(unicodebuf[i]);
6540 if (cw > 2)
6541 cw = 1;
6542 unicodepdy[i] = cw * gui.char_width;
6543 }
6544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006546 foptions, pcliprect, unicodebuf, len, unicodepdy);
6547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 }
6549 }
6550 else
6551#endif
6552 {
6553#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006554 /* Windows will mess up RL text, so we have to draw it character by
6555 * character. Only do this if RL is on, since it's slow. */
6556 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6558 foptions, pcliprect, (char *)text, len, padding);
6559 else
6560#endif
6561 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6562 foptions, pcliprect, (char *)text, len, padding);
6563 }
6564
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006565 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 if (flags & DRAW_UNDERL)
6567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 /* When p_linespace is 0, overwrite the bottom row of pixels.
6569 * Otherwise put the line just below the character. */
6570 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (p_linespace > 1)
6572 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006573 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006575
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006576 /* Strikethrough */
6577 if (flags & DRAW_STRIKE)
6578 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006579 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006580 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006581 }
6582
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006583 /* Undercurl */
6584 if (flags & DRAW_UNDERC)
6585 {
6586 int x;
6587 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006588 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006589
6590 y = FILL_Y(row + 1) - 1;
6591 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6592 {
6593 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006594 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006595 }
6596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
6599
6600/*
6601 * Output routines.
6602 */
6603
6604/* Flush any output to the screen */
6605 void
6606gui_mch_flush(void)
6607{
6608# if defined(__BORLANDC__)
6609 /*
6610 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6611 * prototype declaration.
6612 * The compiler complains if __stdcall is not used in both declarations.
6613 */
6614 BOOL __stdcall GdiFlush(void);
6615# endif
6616
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006617#if defined(FEAT_DIRECTX)
6618 if (IS_ENABLE_DIRECTX())
6619 DWriteContext_Flush(s_dwc);
6620#endif
6621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 GdiFlush();
6623}
6624
6625 static void
6626clear_rect(RECT *rcp)
6627{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006628 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630
6631
Bram Moolenaarc716c302006-01-21 22:12:51 +00006632 void
6633gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6634{
6635 RECT workarea_rect;
6636
6637 get_work_area(&workarea_rect);
6638
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006639 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006640 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006641 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006642
6643 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6644 * the menubar for MSwin, we subtract it from the screen height, so that
6645 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006646 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006647 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006648 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006649 - GetSystemMetrics(SM_CYCAPTION)
6650#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006651 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006652#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006653 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006654}
6655
6656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657#if defined(FEAT_MENU) || defined(PROTO)
6658/*
6659 * Add a sub menu to the menu bar.
6660 */
6661 void
6662gui_mch_add_menu(
6663 vimmenu_T *menu,
6664 int pos)
6665{
6666 vimmenu_T *parent = menu->parent;
6667
6668 menu->submenu_id = CreatePopupMenu();
6669 menu->id = s_menu_id++;
6670
6671 if (menu_is_menubar(menu->name))
6672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006674 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006676 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6677 {
6678 /* 'encoding' differs from active codepage: convert menu name
6679 * and use wide function */
6680 wn = enc_to_utf16(menu->name, NULL);
6681 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006683 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006685 infow.cbSize = sizeof(infow);
6686 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6687 | MIIM_SUBMENU;
6688 infow.dwItemData = (long_u)menu;
6689 infow.wID = menu->id;
6690 infow.fType = MFT_STRING;
6691 infow.dwTypeData = wn;
6692 infow.cch = (UINT)wcslen(wn);
6693 infow.hSubMenu = menu->submenu_id;
6694 InsertMenuItemW((parent == NULL)
6695 ? s_menuBar : parent->submenu_id,
6696 (UINT)pos, TRUE, &infow);
6697 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006701 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006703 {
6704 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006706 info.cbSize = sizeof(info);
6707 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6708 info.dwItemData = (long_u)menu;
6709 info.wID = menu->id;
6710 info.fType = MFT_STRING;
6711 info.dwTypeData = (LPTSTR)menu->name;
6712 info.cch = (UINT)STRLEN(menu->name);
6713 info.hSubMenu = menu->submenu_id;
6714 InsertMenuItem((parent == NULL)
6715 ? s_menuBar : parent->submenu_id,
6716 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 }
6718 }
6719
6720 /* Fix window size if menu may have wrapped */
6721 if (parent == NULL)
6722 gui_mswin_get_menu_height(!gui.starting);
6723#ifdef FEAT_TEAROFF
6724 else if (IsWindow(parent->tearoff_handle))
6725 rebuild_tearoff(parent);
6726#endif
6727}
6728
6729 void
6730gui_mch_show_popupmenu(vimmenu_T *menu)
6731{
6732 POINT mp;
6733
6734 (void)GetCursorPos((LPPOINT)&mp);
6735 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6736}
6737
6738 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006739gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 vimmenu_T *menu = gui_find_menu(path_name);
6742
6743 if (menu != NULL)
6744 {
6745 POINT p;
6746
6747 /* Find the position of the current cursor */
6748 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006749 if (mouse_pos)
6750 {
6751 int mx, my;
6752
6753 gui_mch_getmouse(&mx, &my);
6754 p.x += mx;
6755 p.y += my;
6756 }
6757 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006759 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6761 }
6762 msg_scroll = FALSE;
6763 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6764 }
6765}
6766
6767#if defined(FEAT_TEAROFF) || defined(PROTO)
6768/*
6769 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6770 * create it as a pseudo-"tearoff menu".
6771 */
6772 void
6773gui_make_tearoff(char_u *path_name)
6774{
6775 vimmenu_T *menu = gui_find_menu(path_name);
6776
6777 /* Found the menu, so tear it off. */
6778 if (menu != NULL)
6779 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6780}
6781#endif
6782
6783/*
6784 * Add a menu item to a menu
6785 */
6786 void
6787gui_mch_add_menu_item(
6788 vimmenu_T *menu,
6789 int idx)
6790{
6791 vimmenu_T *parent = menu->parent;
6792
6793 menu->id = s_menu_id++;
6794 menu->submenu_id = NULL;
6795
6796#ifdef FEAT_TEAROFF
6797 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6798 {
6799 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6800 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6801 }
6802 else
6803#endif
6804#ifdef FEAT_TOOLBAR
6805 if (menu_is_toolbar(parent->name))
6806 {
6807 TBBUTTON newtb;
6808
6809 vim_memset(&newtb, 0, sizeof(newtb));
6810 if (menu_is_separator(menu->name))
6811 {
6812 newtb.iBitmap = 0;
6813 newtb.fsStyle = TBSTYLE_SEP;
6814 }
6815 else
6816 {
6817 newtb.iBitmap = get_toolbar_bitmap(menu);
6818 newtb.fsStyle = TBSTYLE_BUTTON;
6819 }
6820 newtb.idCommand = menu->id;
6821 newtb.fsState = TBSTATE_ENABLED;
6822 newtb.iString = 0;
6823 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6824 (LPARAM)&newtb);
6825 menu->submenu_id = (HMENU)-1;
6826 }
6827 else
6828#endif
6829 {
6830#ifdef FEAT_MBYTE
6831 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6834 {
6835 /* 'encoding' differs from active codepage: convert menu item name
6836 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006837 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 if (wn != NULL)
6839 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006840 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 (menu_is_separator(menu->name)
6842 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6843 (UINT)menu->id, wn);
6844 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846 }
6847 if (wn == NULL)
6848#endif
6849 InsertMenu(parent->submenu_id, (UINT)idx,
6850 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6851 | MF_BYPOSITION,
6852 (UINT)menu->id, (LPCTSTR)menu->name);
6853#ifdef FEAT_TEAROFF
6854 if (IsWindow(parent->tearoff_handle))
6855 rebuild_tearoff(parent);
6856#endif
6857 }
6858}
6859
6860/*
6861 * Destroy the machine specific menu widget.
6862 */
6863 void
6864gui_mch_destroy_menu(vimmenu_T *menu)
6865{
6866#ifdef FEAT_TOOLBAR
6867 /*
6868 * is this a toolbar button?
6869 */
6870 if (menu->submenu_id == (HMENU)-1)
6871 {
6872 int iButton;
6873
6874 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6875 (WPARAM)menu->id, 0);
6876 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6877 }
6878 else
6879#endif
6880 {
6881 if (menu->parent != NULL
6882 && menu_is_popup(menu->parent->dname)
6883 && menu->parent->submenu_id != NULL)
6884 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6885 else
6886 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6887 if (menu->submenu_id != NULL)
6888 DestroyMenu(menu->submenu_id);
6889#ifdef FEAT_TEAROFF
6890 if (IsWindow(menu->tearoff_handle))
6891 DestroyWindow(menu->tearoff_handle);
6892 if (menu->parent != NULL
6893 && menu->parent->children != NULL
6894 && IsWindow(menu->parent->tearoff_handle))
6895 {
6896 /* This menu must not show up when rebuilding the tearoff window. */
6897 menu->modes = 0;
6898 rebuild_tearoff(menu->parent);
6899 }
6900#endif
6901 }
6902}
6903
6904#ifdef FEAT_TEAROFF
6905 static void
6906rebuild_tearoff(vimmenu_T *menu)
6907{
6908 /*hackish*/
6909 char_u tbuf[128];
6910 RECT trect;
6911 RECT rct;
6912 RECT roct;
6913 int x, y;
6914
6915 HWND thwnd = menu->tearoff_handle;
6916
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006917 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (GetWindowRect(thwnd, &trect)
6919 && GetWindowRect(s_hwnd, &rct)
6920 && GetClientRect(s_hwnd, &roct))
6921 {
6922 x = trect.left - rct.left;
6923 y = (trect.top - rct.bottom + roct.bottom);
6924 }
6925 else
6926 {
6927 x = y = 0xffffL;
6928 }
6929 DestroyWindow(thwnd);
6930 if (menu->children != NULL)
6931 {
6932 gui_mch_tearoff(tbuf, menu, x, y);
6933 if (IsWindow(menu->tearoff_handle))
6934 (void) SetWindowPos(menu->tearoff_handle,
6935 NULL,
6936 (int)trect.left,
6937 (int)trect.top,
6938 0, 0,
6939 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6940 }
6941}
6942#endif /* FEAT_TEAROFF */
6943
6944/*
6945 * Make a menu either grey or not grey.
6946 */
6947 void
6948gui_mch_menu_grey(
6949 vimmenu_T *menu,
6950 int grey)
6951{
6952#ifdef FEAT_TOOLBAR
6953 /*
6954 * is this a toolbar button?
6955 */
6956 if (menu->submenu_id == (HMENU)-1)
6957 {
6958 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6959 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6960 }
6961 else
6962#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006963 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6964 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966#ifdef FEAT_TEAROFF
6967 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6968 {
6969 WORD menuID;
6970 HWND menuHandle;
6971
6972 /*
6973 * A tearoff button has changed state.
6974 */
6975 if (menu->children == NULL)
6976 menuID = (WORD)(menu->id);
6977 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006978 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6980 if (menuHandle)
6981 EnableWindow(menuHandle, !grey);
6982
6983 }
6984#endif
6985}
6986
6987#endif /* FEAT_MENU */
6988
6989
6990/* define some macros used to make the dialogue creation more readable */
6991
6992#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6993#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006994#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6997/*
6998 * stuff for dialogs
6999 */
7000
7001/*
7002 * The callback routine used by all the dialogs. Very simple. First,
7003 * acknowledges the INITDIALOG message so that Windows knows to do standard
7004 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7005 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7006 * number.
7007 */
7008 static LRESULT CALLBACK
7009dialog_callback(
7010 HWND hwnd,
7011 UINT message,
7012 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007013 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014{
7015 if (message == WM_INITDIALOG)
7016 {
7017 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7018 /* Set focus to the dialog. Set the default button, if specified. */
7019 (void)SetFocus(hwnd);
7020 if (dialog_default_button > IDCANCEL)
7021 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007022 else
7023 /* We don't have a default, set focus on another element of the
7024 * dialog window, probably the icon */
7025 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 return FALSE;
7027 }
7028
7029 if (message == WM_COMMAND)
7030 {
7031 int button = LOWORD(wParam);
7032
7033 /* Don't end the dialog if something was selected that was
7034 * not a button.
7035 */
7036 if (button >= DLG_NONBUTTON_CONTROL)
7037 return TRUE;
7038
7039 /* If the edit box exists, copy the string. */
7040 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007041 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007042# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007043 /* If the OS is Windows NT, and 'encoding' differs from active
7044 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007045 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007046 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007047 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7048 char_u *p;
7049
7050 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7051 p = utf16_to_enc(wp, NULL);
7052 vim_strncpy(s_textfield, p, IOSIZE);
7053 vim_free(p);
7054 vim_free(wp);
7055 }
7056 else
7057# endif
7058 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007059 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
7062 /*
7063 * Need to check for IDOK because if the user just hits Return to
7064 * accept the default value, some reason this is what we get.
7065 */
7066 if (button == IDOK)
7067 {
7068 if (dialog_default_button > IDCANCEL)
7069 EndDialog(hwnd, dialog_default_button);
7070 }
7071 else
7072 EndDialog(hwnd, button - IDCANCEL);
7073 return TRUE;
7074 }
7075
7076 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7077 {
7078 EndDialog(hwnd, 0);
7079 return TRUE;
7080 }
7081 return FALSE;
7082}
7083
7084/*
7085 * Create a dialog dynamically from the parameter strings.
7086 * type = type of dialog (question, alert, etc.)
7087 * title = dialog title. may be NULL for default title.
7088 * message = text to display. Dialog sizes to accommodate it.
7089 * buttons = '\n' separated list of button captions, default first.
7090 * dfltbutton = number of default button.
7091 *
7092 * This routine returns 1 if the first button is pressed,
7093 * 2 for the second, etc.
7094 *
7095 * 0 indicates Esc was pressed.
7096 * -1 for unexpected error
7097 *
7098 * If stubbing out this fn, return 1.
7099 */
7100
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007101static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 "IDR_VIM",
7104 "IDR_VIM_ERROR",
7105 "IDR_VIM_ALERT",
7106 "IDR_VIM_INFO",
7107 "IDR_VIM_QUESTION"
7108};
7109
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 int
7111gui_mch_dialog(
7112 int type,
7113 char_u *title,
7114 char_u *message,
7115 char_u *buttons,
7116 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007117 char_u *textfield,
7118 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007121 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 int numButtons;
7123 int *buttonWidths, *buttonPositions;
7124 int buttonYpos;
7125 int nchar, i;
7126 DWORD lStyle;
7127 int dlgwidth = 0;
7128 int dlgheight;
7129 int editboxheight;
7130 int horizWidth = 0;
7131 int msgheight;
7132 char_u *pstart;
7133 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007134 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 char_u *tbuffer;
7136 RECT rect;
7137 HWND hwnd;
7138 HDC hdc;
7139 HFONT font, oldFont;
7140 TEXTMETRIC fontInfo;
7141 int fontHeight;
7142 int textWidth, minButtonWidth, messageWidth;
7143 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007144 int maxDialogHeight;
7145 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 int vertical;
7147 int dlgPaddingX;
7148 int dlgPaddingY;
7149#ifdef USE_SYSMENU_FONT
7150 LOGFONT lfSysmenu;
7151 int use_lfSysmenu = FALSE;
7152#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007153 garray_T ga;
7154 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156#ifndef NO_CONSOLE
7157 /* Don't output anything in silent mode ("ex -s") */
7158 if (silent_mode)
7159 return dfltbutton; /* return default option */
7160#endif
7161
Bram Moolenaar748bf032005-02-02 23:04:36 +00007162 if (s_hwnd == NULL)
7163 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
7165 if ((type < 0) || (type > VIM_LAST_TYPE))
7166 type = 0;
7167
7168 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007169 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007170 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007171 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
7173 if (p == NULL)
7174 return -1;
7175
7176 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007177 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7179 * const.
7180 */
7181 tbuffer = vim_strsave(buttons);
7182 if (tbuffer == NULL)
7183 return -1;
7184
7185 --dfltbutton; /* Change from one-based to zero-based */
7186
7187 /* Count buttons */
7188 numButtons = 1;
7189 for (i = 0; tbuffer[i] != '\0'; i++)
7190 {
7191 if (tbuffer[i] == DLG_BUTTON_SEP)
7192 numButtons++;
7193 }
7194 if (dfltbutton >= numButtons)
7195 dfltbutton = -1;
7196
7197 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007198 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (buttonWidths == NULL)
7200 return -1;
7201
7202 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007203 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 if (buttonPositions == NULL)
7205 return -1;
7206
7207 /*
7208 * Calculate how big the dialog must be.
7209 */
7210 hwnd = GetDesktopWindow();
7211 hdc = GetWindowDC(hwnd);
7212#ifdef USE_SYSMENU_FONT
7213 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7214 {
7215 font = CreateFontIndirect(&lfSysmenu);
7216 use_lfSysmenu = TRUE;
7217 }
7218 else
7219#endif
7220 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7221 VARIABLE_PITCH , DLG_FONT_NAME);
7222 if (s_usenewlook)
7223 {
7224 oldFont = SelectFont(hdc, font);
7225 dlgPaddingX = DLG_PADDING_X;
7226 dlgPaddingY = DLG_PADDING_Y;
7227 }
7228 else
7229 {
7230 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7231 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7232 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7233 }
7234 GetTextMetrics(hdc, &fontInfo);
7235 fontHeight = fontInfo.tmHeight;
7236
7237 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007238 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007241 if (s_hwnd == NULL)
7242 {
7243 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244
Bram Moolenaarc716c302006-01-21 22:12:51 +00007245 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007246 get_work_area(&workarea_rect);
7247 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7248 if (maxDialogWidth > 600)
7249 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007250 /* Leave some room for the taskbar. */
7251 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007252 }
7253 else
7254 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007255 /* Use our own window for the size, unless it's very small. */
7256 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007257 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007258 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007259 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007260 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7261 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007262
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007263 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007264 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007265 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007266 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007267 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7268 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7269 }
7270
7271 /* Set dlgwidth to width of message.
7272 * Copy the message into "ga", changing NL to CR-NL and inserting line
7273 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 pstart = message;
7275 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007276 msgheight = 0;
7277 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 do
7279 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007280 msgheight += fontHeight; /* at least one line */
7281
7282 /* Need to figure out where to break the string. The system does it
7283 * at a word boundary, which would mean we can't compute the number of
7284 * wrapped lines. */
7285 textWidth = 0;
7286 last_white = NULL;
7287 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007288 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007289#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007290 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007291#else
7292 l = 1;
7293#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007294 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007295 && textWidth > maxDialogWidth * 3 / 4)
7296 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007297 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007298 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007299 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007300 /* Line will wrap. */
7301 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007302 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007303 textWidth = 0;
7304
7305 if (last_white != NULL)
7306 {
7307 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007308 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007309 pend = last_white + 1;
7310 last_white = NULL;
7311 }
7312 ga_append(&ga, '\r');
7313 ga_append(&ga, '\n');
7314 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007315 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007316
7317 while (--l >= 0)
7318 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007319 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007320 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007322
7323 ga_append(&ga, '\r');
7324 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 pstart = pend + 1;
7326 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007327
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007328 if (ga.ga_data != NULL)
7329 message = ga.ga_data;
7330
Bram Moolenaar748bf032005-02-02 23:04:36 +00007331 messageWidth += 10; /* roundoff space */
7332
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007334 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7335 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
7337 if (msgheight < DLG_ICON_HEIGHT)
7338 msgheight = DLG_ICON_HEIGHT;
7339
7340 /*
7341 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007342 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007344 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 if (!vertical)
7346 {
7347 // Place buttons horizontally if they fit.
7348 horizWidth = dlgPaddingX;
7349 pstart = tbuffer;
7350 i = 0;
7351 do
7352 {
7353 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7354 if (pend == NULL)
7355 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007356 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 if (textWidth < minButtonWidth)
7358 textWidth = minButtonWidth;
7359 textWidth += dlgPaddingX; /* Padding within button */
7360 buttonWidths[i] = textWidth;
7361 buttonPositions[i++] = horizWidth;
7362 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7363 pstart = pend + 1;
7364 } while (*pend != NUL);
7365
7366 if (horizWidth > maxDialogWidth)
7367 vertical = TRUE; // Too wide to fit on the screen.
7368 else if (horizWidth > dlgwidth)
7369 dlgwidth = horizWidth;
7370 }
7371
7372 if (vertical)
7373 {
7374 // Stack buttons vertically.
7375 pstart = tbuffer;
7376 do
7377 {
7378 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7379 if (pend == NULL)
7380 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007381 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 textWidth += dlgPaddingX; /* Padding within button */
7383 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7384 if (textWidth > dlgwidth)
7385 dlgwidth = textWidth;
7386 pstart = pend + 1;
7387 } while (*pend != NUL);
7388 }
7389
7390 if (dlgwidth < DLG_MIN_WIDTH)
7391 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7392
7393 /* start to fill in the dlgtemplate information. addressing by WORDs */
7394 if (s_usenewlook)
7395 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7396 else
7397 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7398
7399 add_long(lStyle);
7400 add_long(0); // (lExtendedStyle)
7401 pnumitems = p; /*save where the number of items must be stored*/
7402 add_word(0); // NumberOfItems(will change later)
7403 add_word(10); // x
7404 add_word(10); // y
7405 add_word(PixelToDialogX(dlgwidth)); // cx
7406
7407 // Dialog height.
7408 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007409 dlgheight = msgheight + 2 * dlgPaddingY
7410 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 else
7412 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7413
7414 // Dialog needs to be taller if contains an edit box.
7415 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7416 if (textfield != NULL)
7417 dlgheight += editboxheight;
7418
Bram Moolenaara95d8232013-08-07 15:27:11 +02007419 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7420 if (dlgheight > maxDialogHeight)
7421 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007422 msgheight = msgheight - (dlgheight - maxDialogHeight);
7423 dlgheight = maxDialogHeight;
7424 scroll_flag = WS_VSCROLL;
7425 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7426 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007427 }
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 add_word(PixelToDialogY(dlgheight));
7430
7431 add_word(0); // Menu
7432 add_word(0); // Class
7433
7434 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007435 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7436 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 p += nchar;
7438
7439 if (s_usenewlook)
7440 {
7441 /* do the font, since DS_3DLOOK doesn't work properly */
7442#ifdef USE_SYSMENU_FONT
7443 if (use_lfSysmenu)
7444 {
7445 /* point size */
7446 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7447 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007448 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 }
7450 else
7451#endif
7452 {
7453 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007454 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
7456 p += nchar;
7457 }
7458
7459 buttonYpos = msgheight + 2 * dlgPaddingY;
7460
7461 if (textfield != NULL)
7462 buttonYpos += editboxheight;
7463
7464 pstart = tbuffer;
7465 if (!vertical)
7466 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7467 for (i = 0; i < numButtons; i++)
7468 {
7469 /* get end of this button. */
7470 for ( pend = pstart;
7471 *pend && (*pend != DLG_BUTTON_SEP);
7472 pend++)
7473 ;
7474
7475 if (*pend)
7476 *pend = '\0';
7477
7478 /*
7479 * old NOTE:
7480 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7481 * the focus to the first tab-able button and in so doing makes that
7482 * the default!! Grrr. Workaround: Make the default button the only
7483 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7484 * he/she can use arrow keys.
7485 *
7486 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007487 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 * dialog. Also needed for when the textfield is the default control.
7489 * It appears to work now (perhaps not on Win95?).
7490 */
7491 if (vertical)
7492 {
7493 p = add_dialog_element(p,
7494 (i == dfltbutton
7495 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7496 PixelToDialogX(DLG_VERT_PADDING_X),
7497 PixelToDialogY(buttonYpos /* TBK */
7498 + 2 * fontHeight * i),
7499 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7500 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007501 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 else
7504 {
7505 p = add_dialog_element(p,
7506 (i == dfltbutton
7507 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7508 PixelToDialogX(horizWidth + buttonPositions[i]),
7509 PixelToDialogY(buttonYpos), /* TBK */
7510 PixelToDialogX(buttonWidths[i]),
7511 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007512 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
7514 pstart = pend + 1; /*next button*/
7515 }
7516 *pnumitems += numButtons;
7517
7518 /* Vim icon */
7519 p = add_dialog_element(p, SS_ICON,
7520 PixelToDialogX(dlgPaddingX),
7521 PixelToDialogY(dlgPaddingY),
7522 PixelToDialogX(DLG_ICON_WIDTH),
7523 PixelToDialogY(DLG_ICON_HEIGHT),
7524 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7525 dlg_icons[type]);
7526
Bram Moolenaar748bf032005-02-02 23:04:36 +00007527 /* Dialog message */
7528 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7529 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7530 PixelToDialogY(dlgPaddingY),
7531 (WORD)(PixelToDialogX(messageWidth) + 1),
7532 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007533 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
7535 /* Edit box */
7536 if (textfield != NULL)
7537 {
7538 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7539 PixelToDialogX(2 * dlgPaddingX),
7540 PixelToDialogY(2 * dlgPaddingY + msgheight),
7541 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7542 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007543 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 *pnumitems += 1;
7545 }
7546
7547 *pnumitems += 2;
7548
7549 SelectFont(hdc, oldFont);
7550 DeleteObject(font);
7551 ReleaseDC(hwnd, hdc);
7552
7553 /* Let the dialog_callback() function know which button to make default
7554 * If we have an edit box, make that the default. We also need to tell
7555 * dialog_callback() if this dialog contains an edit box or not. We do
7556 * this by setting s_textfield if it does.
7557 */
7558 if (textfield != NULL)
7559 {
7560 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7561 s_textfield = textfield;
7562 }
7563 else
7564 {
7565 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7566 s_textfield = NULL;
7567 }
7568
7569 /* show the dialog box modally and get a return value */
7570 nchar = (int)DialogBoxIndirect(
7571 s_hinst,
7572 (LPDLGTEMPLATE)pdlgtemplate,
7573 s_hwnd,
7574 (DLGPROC)dialog_callback);
7575
7576 LocalFree(LocalHandle(pdlgtemplate));
7577 vim_free(tbuffer);
7578 vim_free(buttonWidths);
7579 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007580 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581
7582 /* Focus back to our window (for when MDI is used). */
7583 (void)SetFocus(s_hwnd);
7584
7585 return nchar;
7586}
7587
7588#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007589
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590/*
7591 * Put a simple element (basic class) onto a dialog template in memory.
7592 * return a pointer to where the next item should be added.
7593 *
7594 * parameters:
7595 * lStyle = additional style flags
7596 * (be careful, NT3.51 & Win32s will ignore the new ones)
7597 * x,y = x & y positions IN DIALOG UNITS
7598 * w,h = width and height IN DIALOG UNITS
7599 * Id = ID used in messages
7600 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7601 * caption = usually text or resource name
7602 *
7603 * TODO: use the length information noted here to enable the dialog creation
7604 * routines to work out more exactly how much memory they need to alloc.
7605 */
7606 static PWORD
7607add_dialog_element(
7608 PWORD p,
7609 DWORD lStyle,
7610 WORD x,
7611 WORD y,
7612 WORD w,
7613 WORD h,
7614 WORD Id,
7615 WORD clss,
7616 const char *caption)
7617{
7618 int nchar;
7619
7620 p = lpwAlign(p); /* Align to dword boundary*/
7621 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7622 *p++ = LOWORD(lStyle);
7623 *p++ = HIWORD(lStyle);
7624 *p++ = 0; // LOWORD (lExtendedStyle)
7625 *p++ = 0; // HIWORD (lExtendedStyle)
7626 *p++ = x;
7627 *p++ = y;
7628 *p++ = w;
7629 *p++ = h;
7630 *p++ = Id; //9 or 10 words in all
7631
7632 *p++ = (WORD)0xffff;
7633 *p++ = clss; //2 more here
7634
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007635 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 p += nchar;
7637
7638 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7639
7640 return p; //total = 15+ (strlen(caption)) words
7641 // = 30 + 2(strlen(caption) bytes reqd
7642}
7643
7644
7645/*
7646 * Helper routine. Take an input pointer, return closest pointer that is
7647 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7648 */
7649 static LPWORD
7650lpwAlign(
7651 LPWORD lpIn)
7652{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007653 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007655 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 ul += 3;
7657 ul >>= 2;
7658 ul <<= 2;
7659 return (LPWORD)ul;
7660}
7661
7662/*
7663 * Helper routine. Takes second parameter as Ansi string, copies it to first
7664 * parameter as wide character (16-bits / char) string, and returns integer
7665 * number of wide characters (words) in string (including the trailing wide
7666 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007667 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7668 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 static int
7670nCopyAnsiToWideChar(
7671 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007672 LPSTR lpAnsiIn,
7673 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 int nChar = 0;
7676#ifdef FEAT_MBYTE
7677 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7678 int i;
7679 WCHAR *wn;
7680
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007681 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {
7683 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007684 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (wn != NULL)
7686 {
7687 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007688 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 vim_free(wn);
7690 }
7691 }
7692 if (nChar == 0)
7693 /* Use Win32 conversion function. */
7694 nChar = MultiByteToWideChar(
7695 enc_codepage > 0 ? enc_codepage : CP_ACP,
7696 MB_PRECOMPOSED,
7697 lpAnsiIn, len,
7698 lpWCStr, len);
7699 for (i = 0; i < nChar; ++i)
7700 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7701 lpWCStr[i] = (WORD)' ';
7702#else
7703 do
7704 {
7705 if (*lpAnsiIn == '\t')
7706 *lpWCStr++ = (WORD)' ';
7707 else
7708 *lpWCStr++ = (WORD)*lpAnsiIn;
7709 nChar++;
7710 } while (*lpAnsiIn++);
7711#endif
7712
7713 return nChar;
7714}
7715
7716
7717#ifdef FEAT_TEAROFF
7718/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007719 * Lookup menu handle from "menu_id".
7720 */
7721 static HMENU
7722tearoff_lookup_menuhandle(
7723 vimmenu_T *menu,
7724 WORD menu_id)
7725{
7726 for ( ; menu != NULL; menu = menu->next)
7727 {
7728 if (menu->modes == 0) /* this menu has just been deleted */
7729 continue;
7730 if (menu_is_separator(menu->dname))
7731 continue;
7732 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7733 return menu->submenu_id;
7734 }
7735 return NULL;
7736}
7737
7738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 * The callback function for all the modeless dialogs that make up the
7740 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7741 * thinking its menus have been clicked), and go away when closed.
7742 */
7743 static LRESULT CALLBACK
7744tearoff_callback(
7745 HWND hwnd,
7746 UINT message,
7747 WPARAM wParam,
7748 LPARAM lParam)
7749{
7750 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007751 {
7752 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755
7756 /* May show the mouse pointer again. */
7757 HandleMouseHide(message, lParam);
7758
7759 if (message == WM_COMMAND)
7760 {
7761 if ((WORD)(LOWORD(wParam)) & 0x8000)
7762 {
7763 POINT mp;
7764 RECT rect;
7765
7766 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7767 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007768 vimmenu_T *menu;
7769
7770 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007772 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7774 (int)rect.right - 8,
7775 (int)mp.y,
7776 (int)0, /*reserved param*/
7777 s_hwnd,
7778 NULL);
7779 /*
7780 * NOTE: The pop-up menu can eat the mouse up event.
7781 * We deal with this in normal.c.
7782 */
7783 }
7784 }
7785 else
7786 /* Pass on messages to the main Vim window */
7787 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7788 /*
7789 * Give main window the focus back: this is so after
7790 * choosing a tearoff button you can start typing again
7791 * straight away.
7792 */
7793 (void)SetFocus(s_hwnd);
7794 return TRUE;
7795 }
7796 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7797 {
7798 DestroyWindow(hwnd);
7799 return TRUE;
7800 }
7801
7802 /* When moved around, give main window the focus back. */
7803 if (message == WM_EXITSIZEMOVE)
7804 (void)SetActiveWindow(s_hwnd);
7805
7806 return FALSE;
7807}
7808#endif
7809
7810
7811/*
7812 * Decide whether to use the "new look" (small, non-bold font) or the "old
7813 * look" (big, clanky font) for dialogs, and work out a few values for use
7814 * later accordingly.
7815 */
7816 static void
7817get_dialog_font_metrics(void)
7818{
7819 HDC hdc;
7820 HFONT hfontTools = 0;
7821 DWORD dlgFontSize;
7822 SIZE size;
7823#ifdef USE_SYSMENU_FONT
7824 LOGFONT lfSysmenu;
7825#endif
7826
7827 s_usenewlook = FALSE;
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007830 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7831 hfontTools = CreateFontIndirect(&lfSysmenu);
7832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833#endif
7834 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7835 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7836
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007837 if (hfontTools)
7838 {
7839 hdc = GetDC(s_hwnd);
7840 SelectObject(hdc, hfontTools);
7841 /*
7842 * GetTextMetrics() doesn't return the right value in
7843 * tmAveCharWidth, so we have to figure out the dialog base units
7844 * ourselves.
7845 */
7846 GetTextExtentPoint(hdc,
7847 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7848 52, &size);
7849 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007851 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7852 s_dlgfntheight = (WORD)size.cy;
7853 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 }
7855
7856 if (!s_usenewlook)
7857 {
7858 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7859 s_dlgfntwidth = LOWORD(dlgFontSize);
7860 s_dlgfntheight = HIWORD(dlgFontSize);
7861 }
7862}
7863
7864#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7865/*
7866 * Create a pseudo-"tearoff menu" based on the child
7867 * items of a given menu pointer.
7868 */
7869 static void
7870gui_mch_tearoff(
7871 char_u *title,
7872 vimmenu_T *menu,
7873 int initX,
7874 int initY)
7875{
7876 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7877 int template_len;
7878 int nchar, textWidth, submenuWidth;
7879 DWORD lStyle;
7880 DWORD lExtendedStyle;
7881 WORD dlgwidth;
7882 WORD menuID;
7883 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007884 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 vimmenu_T *the_menu = menu;
7886 HWND hwnd;
7887 HDC hdc;
7888 HFONT font, oldFont;
7889 int col, spaceWidth, len;
7890 int columnWidths[2];
7891 char_u *label, *text;
7892 int acLen = 0;
7893 int nameLen;
7894 int padding0, padding1, padding2 = 0;
7895 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007896 int x;
7897 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#ifdef USE_SYSMENU_FONT
7899 LOGFONT lfSysmenu;
7900 int use_lfSysmenu = FALSE;
7901#endif
7902
7903 /*
7904 * If this menu is already torn off, move it to the mouse position.
7905 */
7906 if (IsWindow(menu->tearoff_handle))
7907 {
7908 POINT mp;
7909 if (GetCursorPos((LPPOINT)&mp))
7910 {
7911 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7912 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7913 }
7914 return;
7915 }
7916
7917 /*
7918 * Create a new tearoff.
7919 */
7920 if (*title == MNU_HIDDEN_CHAR)
7921 title++;
7922
7923 /* Allocate memory to store the dialog template. It's made bigger when
7924 * needed. */
7925 template_len = DLG_ALLOC_SIZE;
7926 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7927 if (p == NULL)
7928 return;
7929
7930 hwnd = GetDesktopWindow();
7931 hdc = GetWindowDC(hwnd);
7932#ifdef USE_SYSMENU_FONT
7933 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7934 {
7935 font = CreateFontIndirect(&lfSysmenu);
7936 use_lfSysmenu = TRUE;
7937 }
7938 else
7939#endif
7940 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7941 VARIABLE_PITCH , DLG_FONT_NAME);
7942 if (s_usenewlook)
7943 oldFont = SelectFont(hdc, font);
7944 else
7945 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7946
7947 /* Calculate width of a single space. Used for padding columns to the
7948 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007949 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950
7951 /* Figure out max width of the text column, the accelerator column and the
7952 * optional submenu column. */
7953 submenuWidth = 0;
7954 for (col = 0; col < 2; col++)
7955 {
7956 columnWidths[col] = 0;
7957 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7958 {
7959 /* Use "dname" here to compute the width of the visible text. */
7960 text = (col == 0) ? pmenu->dname : pmenu->actext;
7961 if (text != NULL && *text != NUL)
7962 {
7963 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7964 if (textWidth > columnWidths[col])
7965 columnWidths[col] = textWidth;
7966 }
7967 if (pmenu->children != NULL)
7968 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7969 }
7970 }
7971 if (columnWidths[1] == 0)
7972 {
7973 /* no accelerators */
7974 if (submenuWidth != 0)
7975 columnWidths[0] += submenuWidth;
7976 else
7977 columnWidths[0] += spaceWidth;
7978 }
7979 else
7980 {
7981 /* there is an accelerator column */
7982 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7983 columnWidths[1] += submenuWidth;
7984 }
7985
7986 /*
7987 * Now find the total width of our 'menu'.
7988 */
7989 textWidth = columnWidths[0] + columnWidths[1];
7990 if (submenuWidth != 0)
7991 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007992 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7994 textWidth += submenuWidth;
7995 }
7996 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7997 if (textWidth > dlgwidth)
7998 dlgwidth = textWidth;
7999 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 /* start to fill in the dlgtemplate information. addressing by WORDs */
8002 if (s_usenewlook)
8003 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8004 else
8005 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8006
8007 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8008 *p++ = LOWORD(lStyle);
8009 *p++ = HIWORD(lStyle);
8010 *p++ = LOWORD(lExtendedStyle);
8011 *p++ = HIWORD(lExtendedStyle);
8012 pnumitems = p; /* save where the number of items must be stored */
8013 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008014 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008016 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 else
8018 *p++ = PixelToDialogX(initX); // x
8019 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008020 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 else
8022 *p++ = PixelToDialogY(initY); // y
8023 *p++ = PixelToDialogX(dlgwidth); // cx
8024 ptrueheight = p;
8025 *p++ = 0; // dialog height: changed later anyway
8026 *p++ = 0; // Menu
8027 *p++ = 0; // Class
8028
8029 /* copy the title of the dialog */
8030 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008031 ? (LPSTR)title
8032 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 p += nchar;
8034
8035 if (s_usenewlook)
8036 {
8037 /* do the font, since DS_3DLOOK doesn't work properly */
8038#ifdef USE_SYSMENU_FONT
8039 if (use_lfSysmenu)
8040 {
8041 /* point size */
8042 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8043 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008044 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
8046 else
8047#endif
8048 {
8049 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008050 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 }
8052 p += nchar;
8053 }
8054
8055 /*
8056 * Loop over all the items in the menu.
8057 * But skip over the tearbar.
8058 */
8059 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8060 menu = menu->children->next;
8061 else
8062 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008063 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 for ( ; menu != NULL; menu = menu->next)
8065 {
8066 if (menu->modes == 0) /* this menu has just been deleted */
8067 continue;
8068 if (menu_is_separator(menu->dname))
8069 {
8070 sepPadding += 3;
8071 continue;
8072 }
8073
8074 /* Check if there still is plenty of room in the template. Make it
8075 * larger when needed. */
8076 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8077 {
8078 WORD *newp;
8079
8080 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8081 if (newp != NULL)
8082 {
8083 template_len += 4096;
8084 mch_memmove(newp, pdlgtemplate,
8085 (char *)p - (char *)pdlgtemplate);
8086 p = newp + (p - pdlgtemplate);
8087 pnumitems = newp + (pnumitems - pdlgtemplate);
8088 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8089 LocalFree(LocalHandle(pdlgtemplate));
8090 pdlgtemplate = newp;
8091 }
8092 }
8093
8094 /* Figure out minimal length of this menu label. Use "name" for the
8095 * actual text, "dname" for estimating the displayed size. "name"
8096 * has "&a" for mnemonic and includes the accelerator. */
8097 len = nameLen = (int)STRLEN(menu->name);
8098 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8099 (int)STRLEN(menu->dname))) / spaceWidth;
8100 len += padding0;
8101
8102 if (menu->actext != NULL)
8103 {
8104 acLen = (int)STRLEN(menu->actext);
8105 len += acLen;
8106 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8107 }
8108 else
8109 textWidth = 0;
8110 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8111 len += padding1;
8112
8113 if (menu->children == NULL)
8114 {
8115 padding2 = submenuWidth / spaceWidth;
8116 len += padding2;
8117 menuID = (WORD)(menu->id);
8118 }
8119 else
8120 {
8121 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008122 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124
8125 /* Allocate menu label and fill it in */
8126 text = label = alloc((unsigned)len + 1);
8127 if (label == NULL)
8128 break;
8129
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008130 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8132 if (text == NULL)
8133 text = label + nameLen; /* no actext, use whole name */
8134 while (padding0-- > 0)
8135 *text++ = ' ';
8136 if (menu->actext != NULL)
8137 {
8138 STRNCPY(text, menu->actext, acLen);
8139 text += acLen;
8140 }
8141 while (padding1-- > 0)
8142 *text++ = ' ';
8143 if (menu->children != NULL)
8144 {
8145 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8146 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8147 }
8148 else
8149 {
8150 while (padding2-- > 0)
8151 *text++ = ' ';
8152 }
8153 *text = NUL;
8154
8155 /*
8156 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8157 * W95/NT4 it makes the tear-off look more like a menu.
8158 */
8159 p = add_dialog_element(p,
8160 BS_PUSHBUTTON|BS_LEFT,
8161 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8162 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8163 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8164 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008165 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 vim_free(label);
8167 (*pnumitems)++;
8168 }
8169
8170 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8171
8172
8173 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008174 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 s_hinst,
8176 (LPDLGTEMPLATE)pdlgtemplate,
8177 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008178 (DLGPROC)tearoff_callback,
8179 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180
8181 LocalFree(LocalHandle(pdlgtemplate));
8182 SelectFont(hdc, oldFont);
8183 DeleteObject(font);
8184 ReleaseDC(hwnd, hdc);
8185
8186 /*
8187 * Reassert ourselves as the active window. This is so that after creating
8188 * a tearoff, the user doesn't have to click with the mouse just to start
8189 * typing again!
8190 */
8191 (void)SetActiveWindow(s_hwnd);
8192
8193 /* make sure the right buttons are enabled */
8194 force_menu_update = TRUE;
8195}
8196#endif
8197
8198#if defined(FEAT_TOOLBAR) || defined(PROTO)
8199#include "gui_w32_rc.h"
8200
8201/* This not defined in older SDKs */
8202# ifndef TBSTYLE_FLAT
8203# define TBSTYLE_FLAT 0x0800
8204# endif
8205
8206/*
8207 * Create the toolbar, initially unpopulated.
8208 * (just like the menu, there are no defaults, it's all
8209 * set up through menu.vim)
8210 */
8211 static void
8212initialise_toolbar(void)
8213{
8214 InitCommonControls();
8215 s_toolbarhwnd = CreateToolbarEx(
8216 s_hwnd,
8217 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8218 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008219 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 s_hinst,
8221 IDR_TOOLBAR1, // id of initial bitmap
8222 NULL,
8223 0, // initial number of buttons
8224 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8225 TOOLBAR_BUTTON_HEIGHT,
8226 TOOLBAR_BUTTON_WIDTH,
8227 TOOLBAR_BUTTON_HEIGHT,
8228 sizeof(TBBUTTON)
8229 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008230 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8233}
8234
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008235 static LRESULT CALLBACK
8236toolbar_wndproc(
8237 HWND hwnd,
8238 UINT uMsg,
8239 WPARAM wParam,
8240 LPARAM lParam)
8241{
8242 HandleMouseHide(uMsg, lParam);
8243 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8244}
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 static int
8247get_toolbar_bitmap(vimmenu_T *menu)
8248{
8249 int i = -1;
8250
8251 /*
8252 * Check user bitmaps first, unless builtin is specified.
8253 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008254 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {
8256 char_u fname[MAXPATHL];
8257 HANDLE hbitmap = NULL;
8258
8259 if (menu->iconfile != NULL)
8260 {
8261 gui_find_iconfile(menu->iconfile, fname, "bmp");
8262 hbitmap = LoadImage(
8263 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008264 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 IMAGE_BITMAP,
8266 TOOLBAR_BUTTON_WIDTH,
8267 TOOLBAR_BUTTON_HEIGHT,
8268 LR_LOADFROMFILE |
8269 LR_LOADMAP3DCOLORS
8270 );
8271 }
8272
8273 /*
8274 * If the LoadImage call failed, or the "icon=" file
8275 * didn't exist or wasn't specified, try the menu name
8276 */
8277 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008278 && (gui_find_bitmap(
8279#ifdef FEAT_MULTI_LANG
8280 menu->en_dname != NULL ? menu->en_dname :
8281#endif
8282 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 hbitmap = LoadImage(
8284 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008285 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 IMAGE_BITMAP,
8287 TOOLBAR_BUTTON_WIDTH,
8288 TOOLBAR_BUTTON_HEIGHT,
8289 LR_LOADFROMFILE |
8290 LR_LOADMAP3DCOLORS
8291 );
8292
8293 if (hbitmap != NULL)
8294 {
8295 TBADDBITMAP tbAddBitmap;
8296
8297 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008298 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
8300 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8301 (WPARAM)1, (LPARAM)&tbAddBitmap);
8302 /* i will be set to -1 if it fails */
8303 }
8304 }
8305 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8306 i = menu->iconidx;
8307
8308 return i;
8309}
8310#endif
8311
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008312#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8313 static void
8314initialise_tabline(void)
8315{
8316 InitCommonControls();
8317
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008318 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008319 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008320 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8321 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008322 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008323
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008324 gui.tabline_height = TABLINE_HEIGHT;
8325
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008326# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008327 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008328# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008329}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008330
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008331/*
8332 * Get tabpage_T from POINT.
8333 */
8334 static tabpage_T *
8335GetTabFromPoint(
8336 HWND hWnd,
8337 POINT pt)
8338{
8339 tabpage_T *ptp = NULL;
8340
8341 if (gui_mch_showing_tabline())
8342 {
8343 TCHITTESTINFO htinfo;
8344 htinfo.pt = pt;
8345 /* ignore if a window under cusor is not tabcontrol. */
8346 if (s_tabhwnd == hWnd)
8347 {
8348 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8349 if (idx != -1)
8350 ptp = find_tabpage(idx + 1);
8351 }
8352 }
8353 return ptp;
8354}
8355
8356static POINT s_pt = {0, 0};
8357static HCURSOR s_hCursor = NULL;
8358
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008359 static LRESULT CALLBACK
8360tabline_wndproc(
8361 HWND hwnd,
8362 UINT uMsg,
8363 WPARAM wParam,
8364 LPARAM lParam)
8365{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008366 POINT pt;
8367 tabpage_T *tp;
8368 RECT rect;
8369 int nCenter;
8370 int idx0;
8371 int idx1;
8372
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008373 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008374
8375 switch (uMsg)
8376 {
8377 case WM_LBUTTONDOWN:
8378 {
8379 s_pt.x = GET_X_LPARAM(lParam);
8380 s_pt.y = GET_Y_LPARAM(lParam);
8381 SetCapture(hwnd);
8382 s_hCursor = GetCursor(); /* backup default cursor */
8383 break;
8384 }
8385 case WM_MOUSEMOVE:
8386 if (GetCapture() == hwnd
8387 && ((wParam & MK_LBUTTON)) != 0)
8388 {
8389 pt.x = GET_X_LPARAM(lParam);
8390 pt.y = s_pt.y;
8391 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8392 {
8393 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8394
8395 tp = GetTabFromPoint(hwnd, pt);
8396 if (tp != NULL)
8397 {
8398 idx0 = tabpage_index(curtab) - 1;
8399 idx1 = tabpage_index(tp) - 1;
8400
8401 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8402 nCenter = rect.left + (rect.right - rect.left) / 2;
8403
8404 /* Check if the mouse cursor goes over the center of
8405 * the next tab to prevent "flickering". */
8406 if ((idx0 < idx1) && (nCenter < pt.x))
8407 {
8408 tabpage_move(idx1 + 1);
8409 update_screen(0);
8410 }
8411 else if ((idx1 < idx0) && (pt.x < nCenter))
8412 {
8413 tabpage_move(idx1);
8414 update_screen(0);
8415 }
8416 }
8417 }
8418 }
8419 break;
8420 case WM_LBUTTONUP:
8421 {
8422 if (GetCapture() == hwnd)
8423 {
8424 SetCursor(s_hCursor);
8425 ReleaseCapture();
8426 }
8427 break;
8428 }
8429 default:
8430 break;
8431 }
8432
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008433 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8434}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008435#endif
8436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8438/*
8439 * Make the GUI window come to the foreground.
8440 */
8441 void
8442gui_mch_set_foreground(void)
8443{
8444 if (IsIconic(s_hwnd))
8445 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8446 SetForegroundWindow(s_hwnd);
8447}
8448#endif
8449
8450#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8451 static void
8452dyn_imm_load(void)
8453{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008454 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 if (hLibImm == NULL)
8456 return;
8457
8458 pImmGetCompositionStringA
8459 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8460 pImmGetCompositionStringW
8461 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8462 pImmGetContext
8463 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8464 pImmAssociateContext
8465 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8466 pImmReleaseContext
8467 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8468 pImmGetOpenStatus
8469 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8470 pImmSetOpenStatus
8471 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8472 pImmGetCompositionFont
8473 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8474 pImmSetCompositionFont
8475 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8476 pImmSetCompositionWindow
8477 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8478 pImmGetConversionStatus
8479 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008480 pImmSetConversionStatus
8481 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483 if ( pImmGetCompositionStringA == NULL
8484 || pImmGetCompositionStringW == NULL
8485 || pImmGetContext == NULL
8486 || pImmAssociateContext == NULL
8487 || pImmReleaseContext == NULL
8488 || pImmGetOpenStatus == NULL
8489 || pImmSetOpenStatus == NULL
8490 || pImmGetCompositionFont == NULL
8491 || pImmSetCompositionFont == NULL
8492 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008493 || pImmGetConversionStatus == NULL
8494 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 {
8496 FreeLibrary(hLibImm);
8497 hLibImm = NULL;
8498 pImmGetContext = NULL;
8499 return;
8500 }
8501
8502 return;
8503}
8504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505#endif
8506
8507#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8508
8509# ifdef FEAT_XPM_W32
8510# define IMAGE_XPM 100
8511# endif
8512
8513typedef struct _signicon_t
8514{
8515 HANDLE hImage;
8516 UINT uType;
8517#ifdef FEAT_XPM_W32
8518 HANDLE hShape; /* Mask bitmap handle */
8519#endif
8520} signicon_t;
8521
8522 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008523gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 signicon_t *sign;
8526 int x, y, w, h;
8527
8528 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8529 return;
8530
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008531#if defined(FEAT_DIRECTX)
8532 if (IS_ENABLE_DIRECTX())
8533 DWriteContext_Flush(s_dwc);
8534#endif
8535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 x = TEXT_X(col);
8537 y = TEXT_Y(row);
8538 w = gui.char_width * 2;
8539 h = gui.char_height;
8540 switch (sign->uType)
8541 {
8542 case IMAGE_BITMAP:
8543 {
8544 HDC hdcMem;
8545 HBITMAP hbmpOld;
8546
8547 hdcMem = CreateCompatibleDC(s_hdc);
8548 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8549 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8550 SelectObject(hdcMem, hbmpOld);
8551 DeleteDC(hdcMem);
8552 }
8553 break;
8554 case IMAGE_ICON:
8555 case IMAGE_CURSOR:
8556 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8557 break;
8558#ifdef FEAT_XPM_W32
8559 case IMAGE_XPM:
8560 {
8561 HDC hdcMem;
8562 HBITMAP hbmpOld;
8563
8564 hdcMem = CreateCompatibleDC(s_hdc);
8565 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8566 /* Make hole */
8567 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8568
8569 SelectObject(hdcMem, sign->hImage);
8570 /* Paint sign */
8571 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8572 SelectObject(hdcMem, hbmpOld);
8573 DeleteDC(hdcMem);
8574 }
8575 break;
8576#endif
8577 }
8578}
8579
8580 static void
8581close_signicon_image(signicon_t *sign)
8582{
8583 if (sign)
8584 switch (sign->uType)
8585 {
8586 case IMAGE_BITMAP:
8587 DeleteObject((HGDIOBJ)sign->hImage);
8588 break;
8589 case IMAGE_CURSOR:
8590 DestroyCursor((HCURSOR)sign->hImage);
8591 break;
8592 case IMAGE_ICON:
8593 DestroyIcon((HICON)sign->hImage);
8594 break;
8595#ifdef FEAT_XPM_W32
8596 case IMAGE_XPM:
8597 DeleteObject((HBITMAP)sign->hImage);
8598 DeleteObject((HBITMAP)sign->hShape);
8599 break;
8600#endif
8601 }
8602}
8603
8604 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008605gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 signicon_t sign, *psign;
8608 char_u *ext;
8609
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008611 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (ext > signfile)
8613 {
8614 int do_load = 1;
8615
8616 if (!STRICMP(ext, ".bmp"))
8617 sign.uType = IMAGE_BITMAP;
8618 else if (!STRICMP(ext, ".ico"))
8619 sign.uType = IMAGE_ICON;
8620 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8621 sign.uType = IMAGE_CURSOR;
8622 else
8623 do_load = 0;
8624
8625 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008626 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 gui.char_width * 2, gui.char_height,
8628 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8629#ifdef FEAT_XPM_W32
8630 if (!STRICMP(ext, ".xpm"))
8631 {
8632 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008633 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8634 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636#endif
8637 }
8638
8639 psign = NULL;
8640 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8641 != NULL)
8642 *psign = sign;
8643
8644 if (!psign)
8645 {
8646 if (sign.hImage)
8647 close_signicon_image(&sign);
8648 EMSG(_(e_signdata));
8649 }
8650 return (void *)psign;
8651
8652}
8653
8654 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008655gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 if (sign)
8658 {
8659 close_signicon_image((signicon_t *)sign);
8660 vim_free(sign);
8661 }
8662}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008665#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666
8667/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008668 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008670 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8672 * to get current mouse position).
8673 *
8674 * Trying to use as more Windows services as possible, and as less
8675 * IE version as possible :)).
8676 *
8677 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8678 * BalloonEval struct.
8679 * 2) Enable/Disable simply create/kill BalloonEval Timer
8680 * 3) When there was enough inactivity, timer procedure posts
8681 * async request to debugger
8682 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8683 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008684 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 */
8686
Bram Moolenaar45360022005-07-21 21:08:21 +00008687/*
8688 * determine whether installed Common Controls support multiline tooltips
8689 * (i.e. their version is >= 4.70
8690 */
8691 int
8692multiline_balloon_available(void)
8693{
8694 HINSTANCE hDll;
8695 static char comctl_dll[] = "comctl32.dll";
8696 static int multiline_tip = MAYBE;
8697
8698 if (multiline_tip != MAYBE)
8699 return multiline_tip;
8700
8701 hDll = GetModuleHandle(comctl_dll);
8702 if (hDll != NULL)
8703 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008704 DLLGETVERSIONPROC pGetVer;
8705 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008706
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008707 if (pGetVer != NULL)
8708 {
8709 DLLVERSIONINFO dvi;
8710 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008711
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008712 ZeroMemory(&dvi, sizeof(dvi));
8713 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008714
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008715 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008716
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008717 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008718 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008719 || (dvi.dwMajorVersion == 4
8720 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008721 {
8722 multiline_tip = TRUE;
8723 return multiline_tip;
8724 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008725 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008726 else
8727 {
8728 /* there is chance we have ancient CommCtl 4.70
8729 which doesn't export DllGetVersion */
8730 DWORD dwHandle = 0;
8731 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8732 if (len > 0)
8733 {
8734 VS_FIXEDFILEINFO *ver;
8735 UINT vlen = 0;
8736 void *data = alloc(len);
8737
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008738 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008739 && GetFileVersionInfo(comctl_dll, 0, len, data)
8740 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8741 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008742 && HIWORD(ver->dwFileVersionMS) > 4)
8743 || ((HIWORD(ver->dwFileVersionMS) == 4
8744 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008745 {
8746 vim_free(data);
8747 multiline_tip = TRUE;
8748 return multiline_tip;
8749 }
8750 vim_free(data);
8751 }
8752 }
8753 }
8754 multiline_tip = FALSE;
8755 return multiline_tip;
8756}
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008759make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaar45360022005-07-21 21:08:21 +00008761 TOOLINFO *pti;
8762 int ToolInfoSize;
8763
8764 if (multiline_balloon_available() == TRUE)
8765 ToolInfoSize = sizeof(TOOLINFO_NEW);
8766 else
8767 ToolInfoSize = sizeof(TOOLINFO);
8768
8769 pti = (TOOLINFO *)alloc(ToolInfoSize);
8770 if (pti == NULL)
8771 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8774 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8775 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8776 beval->target, NULL, s_hinst, NULL);
8777
8778 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8779 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8780
Bram Moolenaar45360022005-07-21 21:08:21 +00008781 pti->cbSize = ToolInfoSize;
8782 pti->uFlags = TTF_SUBCLASS;
8783 pti->hwnd = beval->target;
8784 pti->hinst = 0; /* Don't use string resources */
8785 pti->uId = ID_BEVAL_TOOLTIP;
8786
8787 if (multiline_balloon_available() == TRUE)
8788 {
8789 RECT rect;
8790 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8791 pti->lpszText = LPSTR_TEXTCALLBACK;
8792 ptin->lParam = (LPARAM)text;
8793 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8794 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8795 (LPARAM)rect.right);
8796 }
8797 else
8798 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799
8800 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008801 pti->rect.left = pt.x - 3;
8802 pti->rect.top = pt.y - 3;
8803 pti->rect.right = pt.x + 3;
8804 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805
Bram Moolenaar45360022005-07-21 21:08:21 +00008806 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 /* Make tooltip appear sooner */
8808 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008809 /* I've performed some tests and it seems the longest possible life time
8810 * of tooltip is 30 seconds */
8811 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 /*
8813 * HACK: force tooltip to appear, because it'll not appear until
8814 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008815 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008817 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008819 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
8822 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008823delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008825 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826}
8827
8828 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008829BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008830 HWND hwnd UNUSED,
8831 UINT uMsg UNUSED,
8832 UINT_PTR idEvent UNUSED,
8833 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 POINT pt;
8836 RECT rect;
8837
8838 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8839 return;
8840
8841 GetCursorPos(&pt);
8842 if (WindowFromPoint(pt) != s_textArea)
8843 return;
8844
8845 ScreenToClient(s_textArea, &pt);
8846 GetClientRect(s_textArea, &rect);
8847 if (!PtInRect(&rect, pt))
8848 return;
8849
8850 if (LastActivity > 0
8851 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8852 && (cur_beval->showState != ShS_PENDING
8853 || abs(cur_beval->x - pt.x) > 3
8854 || abs(cur_beval->y - pt.y) > 3))
8855 {
8856 /* Pointer resting in one place long enough, it's time to show
8857 * the tooltip. */
8858 cur_beval->showState = ShS_PENDING;
8859 cur_beval->x = pt.x;
8860 cur_beval->y = pt.y;
8861
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008862 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863
8864 if (cur_beval->msgCB != NULL)
8865 (*cur_beval->msgCB)(cur_beval, 0);
8866 }
8867}
8868
8869 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008870gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008872 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008874 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875}
8876
8877 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008878gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008880 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (beval == NULL)
8882 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008883 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008884 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008885 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886}
8887
8888 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008889gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008892
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008893 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 if (beval->showState == ShS_SHOWING)
8895 return;
8896 GetCursorPos(&pt);
8897 ScreenToClient(s_textArea, &pt);
8898
8899 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008901 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 gui_mch_disable_beval_area(cur_beval);
8903 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008904 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008906 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907}
8908
8909 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008910gui_mch_create_beval_area(
8911 void *target, /* ignored, always use s_textArea */
8912 char_u *mesg,
8913 void (*mesgCB)(BalloonEval *, int),
8914 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915{
8916 /* partially stolen from gui_beval.c */
8917 BalloonEval *beval;
8918
8919 if (mesg != NULL && mesgCB != NULL)
8920 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008921 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 return NULL;
8923 }
8924
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008925 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 if (beval != NULL)
8927 {
8928 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929
8930 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 beval->msg = mesg;
8932 beval->msgCB = mesgCB;
8933 beval->clientData = clientData;
8934
8935 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 cur_beval = beval;
8937
8938 if (p_beval)
8939 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 }
8941 return beval;
8942}
8943
8944 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008945Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8948 return;
8949
8950 if (cur_beval != NULL)
8951 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008952 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008954 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008955 // TRACE0("TTN_SHOW {{{");
8956 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008957 break;
8958 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008959 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 delete_tooltip(cur_beval);
8961 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008962 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
8964 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008965 break;
8966 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008967 {
8968 /* if you get there then we have new common controls */
8969 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8970 info->lpszText = (LPSTR)info->lParam;
8971 info->uFlags |= TTF_DI_SETITEM;
8972 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
8975 }
8976}
8977
8978 static void
8979TrackUserActivity(UINT uMsg)
8980{
8981 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8982 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8983 LastActivity = GetTickCount();
8984}
8985
8986 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008987gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008989#ifdef FEAT_VARTABS
8990 if (beval->vts)
8991 vim_free(beval->vts);
8992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 vim_free(beval);
8994}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008995#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996
8997#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8998/*
8999 * We have multiple signs to draw at the same location. Draw the
9000 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9001 */
9002 void
9003netbeans_draw_multisign_indicator(int row)
9004{
9005 int i;
9006 int y;
9007 int x;
9008
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009009 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009010 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009011
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 x = 0;
9013 y = TEXT_Y(row);
9014
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009015#if defined(FEAT_DIRECTX)
9016 if (IS_ENABLE_DIRECTX())
9017 DWriteContext_Flush(s_dwc);
9018#endif
9019
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 for (i = 0; i < gui.char_height - 3; i++)
9021 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9022
9023 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9024 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9025 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9026 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9027 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9028 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9029 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9030}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009031#endif