blob: c919243161751bb8e8ddc4e8bae61a2c96b99d76 [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
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002823 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 (void)SetFocus(s_findrep_hwnd);
2825
2826 s_findrep_is_find = TRUE;
2827 }
2828#endif
2829}
2830
2831
2832 void
2833gui_mch_replace_dialog(exarg_T *eap)
2834{
2835#ifdef MSWIN_FIND_REPLACE
2836 if (s_findrep_msg != 0)
2837 {
2838 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2839 DestroyWindow(s_findrep_hwnd);
2840
2841 if (!IsWindow(s_findrep_hwnd))
2842 {
2843 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002844# ifdef FEAT_MBYTE
2845 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002846 {
2847 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2848 s_findrep_hwnd = ReplaceTextW(
2849 (LPFINDREPLACEW) &s_findrep_struct_w);
2850 }
2851 else
2852# endif
2853 s_findrep_hwnd = ReplaceText(
2854 (LPFINDREPLACE) &s_findrep_struct);
2855 }
2856
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002857 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 (void)SetFocus(s_findrep_hwnd);
2859
2860 s_findrep_is_find = FALSE;
2861 }
2862#endif
2863}
2864
2865
2866/*
2867 * Set visibility of the pointer.
2868 */
2869 void
2870gui_mch_mousehide(int hide)
2871{
2872 if (hide != gui.pointer_hidden)
2873 {
2874 ShowCursor(!hide);
2875 gui.pointer_hidden = hide;
2876 }
2877}
2878
2879#ifdef FEAT_MENU
2880 static void
2881gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2882{
2883 /* Unhide the mouse, we don't get move events here. */
2884 gui_mch_mousehide(FALSE);
2885
2886 (void)TrackPopupMenu(
2887 (HMENU)menu->submenu_id,
2888 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2889 x, y,
2890 (int)0, /*reserved param*/
2891 s_hwnd,
2892 NULL);
2893 /*
2894 * NOTE: The pop-up menu can eat the mouse up event.
2895 * We deal with this in normal.c.
2896 */
2897}
2898#endif
2899
2900/*
2901 * Got a message when the system will go down.
2902 */
2903 static void
2904_OnEndSession(void)
2905{
2906 getout_preserve_modified(1);
2907}
2908
2909/*
2910 * Get this message when the user clicks on the cross in the top right corner
2911 * of a Windows95 window.
2912 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002914_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915{
2916 gui_shell_closed();
2917}
2918
2919/*
2920 * Get a message when the window is being destroyed.
2921 */
2922 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002923_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924{
2925 if (!destroying)
2926 _OnClose(hwnd);
2927}
2928
2929 static void
2930_OnPaint(
2931 HWND hwnd)
2932{
2933 if (!IsMinimized(hwnd))
2934 {
2935 PAINTSTRUCT ps;
2936
2937 out_flush(); /* make sure all output has been processed */
2938 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939
2940#ifdef FEAT_MBYTE
2941 /* prevent multi-byte characters from misprinting on an invalid
2942 * rectangle */
2943 if (has_mbyte)
2944 {
2945 RECT rect;
2946
2947 GetClientRect(hwnd, &rect);
2948 ps.rcPaint.left = rect.left;
2949 ps.rcPaint.right = rect.right;
2950 }
2951#endif
2952
2953 if (!IsRectEmpty(&ps.rcPaint))
2954 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002955 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2956 ps.rcPaint.right - ps.rcPaint.left + 1,
2957 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2958 }
2959
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960 EndPaint(hwnd, &ps);
2961 }
2962}
2963
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964 static void
2965_OnSize(
2966 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002967 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002968 int cx,
2969 int cy)
2970{
2971 if (!IsMinimized(hwnd))
2972 {
2973 gui_resize_shell(cx, cy);
2974
2975#ifdef FEAT_MENU
2976 /* Menu bar may wrap differently now */
2977 gui_mswin_get_menu_height(TRUE);
2978#endif
2979 }
2980}
2981
2982 static void
2983_OnSetFocus(
2984 HWND hwnd,
2985 HWND hwndOldFocus)
2986{
2987 gui_focus_change(TRUE);
2988 s_getting_focus = TRUE;
2989 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2990}
2991
2992 static void
2993_OnKillFocus(
2994 HWND hwnd,
2995 HWND hwndNewFocus)
2996{
2997 gui_focus_change(FALSE);
2998 s_getting_focus = FALSE;
2999 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3000}
3001
3002/*
3003 * Get a message when the user switches back to vim
3004 */
3005 static LRESULT
3006_OnActivateApp(
3007 HWND hwnd,
3008 BOOL fActivate,
3009 DWORD dwThreadId)
3010{
3011 /* we call gui_focus_change() in _OnSetFocus() */
3012 /* gui_focus_change((int)fActivate); */
3013 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3014}
3015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 void
3017gui_mch_destroy_scrollbar(scrollbar_T *sb)
3018{
3019 DestroyWindow(sb->id);
3020}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021
3022/*
3023 * Get current mouse coordinates in text window.
3024 */
3025 void
3026gui_mch_getmouse(int *x, int *y)
3027{
3028 RECT rct;
3029 POINT mp;
3030
3031 (void)GetWindowRect(s_textArea, &rct);
3032 (void)GetCursorPos((LPPOINT)&mp);
3033 *x = (int)(mp.x - rct.left);
3034 *y = (int)(mp.y - rct.top);
3035}
3036
3037/*
3038 * Move mouse pointer to character at (x, y).
3039 */
3040 void
3041gui_mch_setmouse(int x, int y)
3042{
3043 RECT rct;
3044
3045 (void)GetWindowRect(s_textArea, &rct);
3046 (void)SetCursorPos(x + gui.border_offset + rct.left,
3047 y + gui.border_offset + rct.top);
3048}
3049
3050 static void
3051gui_mswin_get_valid_dimensions(
3052 int w,
3053 int h,
3054 int *valid_w,
3055 int *valid_h)
3056{
3057 int base_width, base_height;
3058
3059 base_width = gui_get_base_width()
3060 + (GetSystemMetrics(SM_CXFRAME) +
3061 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3062 base_height = gui_get_base_height()
3063 + (GetSystemMetrics(SM_CYFRAME) +
3064 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3065 + GetSystemMetrics(SM_CYCAPTION)
3066#ifdef FEAT_MENU
3067 + gui_mswin_get_menu_height(FALSE)
3068#endif
3069 ;
3070 *valid_w = base_width +
3071 ((w - base_width) / gui.char_width) * gui.char_width;
3072 *valid_h = base_height +
3073 ((h - base_height) / gui.char_height) * gui.char_height;
3074}
3075
3076 void
3077gui_mch_flash(int msec)
3078{
3079 RECT rc;
3080
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003081#if defined(FEAT_DIRECTX)
3082 if (IS_ENABLE_DIRECTX())
3083 DWriteContext_Flush(s_dwc);
3084#endif
3085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003086 /*
3087 * Note: InvertRect() excludes right and bottom of rectangle.
3088 */
3089 rc.left = 0;
3090 rc.top = 0;
3091 rc.right = gui.num_cols * gui.char_width;
3092 rc.bottom = gui.num_rows * gui.char_height;
3093 InvertRect(s_hdc, &rc);
3094 gui_mch_flush(); /* make sure it's displayed */
3095
3096 ui_delay((long)msec, TRUE); /* wait for a few msec */
3097
3098 InvertRect(s_hdc, &rc);
3099}
3100
3101/*
3102 * Return flags used for scrolling.
3103 * The SW_INVALIDATE is required when part of the window is covered or
3104 * off-screen. Refer to MS KB Q75236.
3105 */
3106 static int
3107get_scroll_flags(void)
3108{
3109 HWND hwnd;
3110 RECT rcVim, rcOther, rcDest;
3111
3112 GetWindowRect(s_hwnd, &rcVim);
3113
3114 /* Check if the window is partly above or below the screen. We don't care
3115 * about partly left or right of the screen, it is not relevant when
3116 * scrolling up or down. */
3117 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3118 return SW_INVALIDATE;
3119
3120 /* Check if there is an window (partly) on top of us. */
3121 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3122 if (IsWindowVisible(hwnd))
3123 {
3124 GetWindowRect(hwnd, &rcOther);
3125 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3126 return SW_INVALIDATE;
3127 }
3128 return 0;
3129}
3130
3131/*
3132 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3133 * may not be scrolled out properly.
3134 * For gVim, when _OnScroll() is repeated, the character at the
3135 * previous cursor position may be left drawn after scroll.
3136 * The problem can be avoided by calling GetPixel() to get a pixel in
3137 * the region before ScrollWindowEx().
3138 */
3139 static void
3140intel_gpu_workaround(void)
3141{
3142 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3143}
3144
3145/*
3146 * Delete the given number of lines from the given row, scrolling up any
3147 * text further down within the scroll region.
3148 */
3149 void
3150gui_mch_delete_lines(
3151 int row,
3152 int num_lines)
3153{
3154 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 rc.left = FILL_X(gui.scroll_region_left);
3157 rc.right = FILL_X(gui.scroll_region_right + 1);
3158 rc.top = FILL_Y(row);
3159 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3160
Bram Moolenaar92467d32017-12-05 13:22:16 +01003161#if defined(FEAT_DIRECTX)
3162 if (IS_ENABLE_DIRECTX())
3163 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3165 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003167 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168#endif
3169 {
3170 intel_gpu_workaround();
3171 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003173 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003174 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003176 /* This seems to be required to avoid the cursor disappearing when
3177 * scrolling such that the cursor ends up in the top-left character on
3178 * the screen... But why? (Webb) */
3179 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3180 /* gui.cursor_is_valid = FALSE; */
3181
3182 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3183 gui.scroll_region_left,
3184 gui.scroll_region_bot, gui.scroll_region_right);
3185}
3186
3187/*
3188 * Insert the given number of lines before the given row, scrolling down any
3189 * following text within the scroll region.
3190 */
3191 void
3192gui_mch_insert_lines(
3193 int row,
3194 int num_lines)
3195{
3196 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003197
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 rc.left = FILL_X(gui.scroll_region_left);
3199 rc.right = FILL_X(gui.scroll_region_right + 1);
3200 rc.top = FILL_Y(row);
3201 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003202
3203#if defined(FEAT_DIRECTX)
3204 if (IS_ENABLE_DIRECTX())
3205 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003206 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3207 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003209 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210#endif
3211 {
3212 intel_gpu_workaround();
3213 /* The SW_INVALIDATE is required when part of the window is covered or
3214 * off-screen. How do we avoid it when it's not needed? */
3215 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003216 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003217 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003218 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220 gui_clear_block(row, gui.scroll_region_left,
3221 row + num_lines - 1, gui.scroll_region_right);
3222}
3223
3224
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003226gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227{
3228#if defined(FEAT_DIRECTX)
3229 DWriteContext_Close(s_dwc);
3230 DWrite_Final();
3231 s_dwc = NULL;
3232#endif
3233
3234 ReleaseDC(s_textArea, s_hdc);
3235 DeleteObject(s_brush);
3236
3237#ifdef FEAT_TEAROFF
3238 /* Unload the tearoff bitmap */
3239 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3240#endif
3241
3242 /* Destroy our window (if we have one). */
3243 if (s_hwnd != NULL)
3244 {
3245 destroying = TRUE; /* ignore WM_DESTROY message now */
3246 DestroyWindow(s_hwnd);
3247 }
3248
3249#ifdef GLOBAL_IME
3250 global_ime_end();
3251#endif
3252}
3253
3254 static char_u *
3255logfont2name(LOGFONT lf)
3256{
3257 char *p;
3258 char *res;
3259 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003260 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261 char *font_name = lf.lfFaceName;
3262
3263 charset_name = charset_id2name((int)lf.lfCharSet);
3264#ifdef FEAT_MBYTE
3265 /* Convert a font name from the current codepage to 'encoding'.
3266 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3267 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3268 {
3269 int len;
3270 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3271 (char_u **)&font_name, &len);
3272 }
3273#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003274 quality_name = quality_id2name((int)lf.lfQuality);
3275
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003276 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3277 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3278 if (res != NULL)
3279 {
3280 p = res;
3281 /* make a normal font string out of the lf thing:*/
3282 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3283 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3284 while (*p)
3285 {
3286 if (*p == ' ')
3287 *p = '_';
3288 ++p;
3289 }
3290 if (lf.lfItalic)
3291 STRCAT(p, ":i");
3292 if (lf.lfWeight >= FW_BOLD)
3293 STRCAT(p, ":b");
3294 if (lf.lfUnderline)
3295 STRCAT(p, ":u");
3296 if (lf.lfStrikeOut)
3297 STRCAT(p, ":s");
3298 if (charset_name != NULL)
3299 {
3300 STRCAT(p, ":c");
3301 STRCAT(p, charset_name);
3302 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003303 if (quality_name != NULL)
3304 {
3305 STRCAT(p, ":q");
3306 STRCAT(p, quality_name);
3307 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 }
3309
3310#ifdef FEAT_MBYTE
3311 if (font_name != lf.lfFaceName)
3312 vim_free(font_name);
3313#endif
3314 return (char_u *)res;
3315}
3316
3317
3318#ifdef FEAT_MBYTE_IME
3319/*
3320 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3321 * 'guifont'
3322 */
3323 static void
3324update_im_font(void)
3325{
3326 LOGFONT lf_wide;
3327
3328 if (p_guifontwide != NULL && *p_guifontwide != NUL
3329 && gui.wide_font != NOFONT
3330 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3331 norm_logfont = lf_wide;
3332 else
3333 norm_logfont = sub_logfont;
3334 im_set_font(&norm_logfont);
3335}
3336#endif
3337
3338#ifdef FEAT_MBYTE
3339/*
3340 * Handler of gui.wide_font (p_guifontwide) changed notification.
3341 */
3342 void
3343gui_mch_wide_font_changed(void)
3344{
3345 LOGFONT lf;
3346
3347# ifdef FEAT_MBYTE_IME
3348 update_im_font();
3349# endif
3350
3351 gui_mch_free_font(gui.wide_ital_font);
3352 gui.wide_ital_font = NOFONT;
3353 gui_mch_free_font(gui.wide_bold_font);
3354 gui.wide_bold_font = NOFONT;
3355 gui_mch_free_font(gui.wide_boldital_font);
3356 gui.wide_boldital_font = NOFONT;
3357
3358 if (gui.wide_font
3359 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3360 {
3361 if (!lf.lfItalic)
3362 {
3363 lf.lfItalic = TRUE;
3364 gui.wide_ital_font = get_font_handle(&lf);
3365 lf.lfItalic = FALSE;
3366 }
3367 if (lf.lfWeight < FW_BOLD)
3368 {
3369 lf.lfWeight = FW_BOLD;
3370 gui.wide_bold_font = get_font_handle(&lf);
3371 if (!lf.lfItalic)
3372 {
3373 lf.lfItalic = TRUE;
3374 gui.wide_boldital_font = get_font_handle(&lf);
3375 }
3376 }
3377 }
3378}
3379#endif
3380
3381/*
3382 * Initialise vim to use the font with the given name.
3383 * Return FAIL if the font could not be loaded, OK otherwise.
3384 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003386gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387{
3388 LOGFONT lf;
3389 GuiFont font = NOFONT;
3390 char_u *p;
3391
3392 /* Load the font */
3393 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3394 font = get_font_handle(&lf);
3395 if (font == NOFONT)
3396 return FAIL;
3397
3398 if (font_name == NULL)
3399 font_name = (char_u *)lf.lfFaceName;
3400#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3401 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003402#endif
3403#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 sub_logfont = lf;
3405#endif
3406#ifdef FEAT_MBYTE_IME
3407 update_im_font();
3408#endif
3409 gui_mch_free_font(gui.norm_font);
3410 gui.norm_font = font;
3411 current_font_height = lf.lfHeight;
3412 GetFontSize(font);
3413
3414 p = logfont2name(lf);
3415 if (p != NULL)
3416 {
3417 hl_set_font_name(p);
3418
3419 /* When setting 'guifont' to "*" replace it with the actual font name.
3420 * */
3421 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3422 {
3423 vim_free(p_guifont);
3424 p_guifont = p;
3425 }
3426 else
3427 vim_free(p);
3428 }
3429
3430 gui_mch_free_font(gui.ital_font);
3431 gui.ital_font = NOFONT;
3432 gui_mch_free_font(gui.bold_font);
3433 gui.bold_font = NOFONT;
3434 gui_mch_free_font(gui.boldital_font);
3435 gui.boldital_font = NOFONT;
3436
3437 if (!lf.lfItalic)
3438 {
3439 lf.lfItalic = TRUE;
3440 gui.ital_font = get_font_handle(&lf);
3441 lf.lfItalic = FALSE;
3442 }
3443 if (lf.lfWeight < FW_BOLD)
3444 {
3445 lf.lfWeight = FW_BOLD;
3446 gui.bold_font = get_font_handle(&lf);
3447 if (!lf.lfItalic)
3448 {
3449 lf.lfItalic = TRUE;
3450 gui.boldital_font = get_font_handle(&lf);
3451 }
3452 }
3453
3454 return OK;
3455}
3456
3457#ifndef WPF_RESTORETOMAXIMIZED
3458# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3459#endif
3460
3461/*
3462 * Return TRUE if the GUI window is maximized, filling the whole screen.
3463 */
3464 int
3465gui_mch_maximized(void)
3466{
3467 WINDOWPLACEMENT wp;
3468
3469 wp.length = sizeof(WINDOWPLACEMENT);
3470 if (GetWindowPlacement(s_hwnd, &wp))
3471 return wp.showCmd == SW_SHOWMAXIMIZED
3472 || (wp.showCmd == SW_SHOWMINIMIZED
3473 && wp.flags == WPF_RESTORETOMAXIMIZED);
3474
3475 return 0;
3476}
3477
3478/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003479 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3480 * is set. Compute the new Rows and Columns. This is like resizing the
3481 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482 */
3483 void
3484gui_mch_newfont(void)
3485{
3486 RECT rect;
3487
3488 GetWindowRect(s_hwnd, &rect);
3489 if (win_socket_id == 0)
3490 {
3491 gui_resize_shell(rect.right - rect.left
3492 - (GetSystemMetrics(SM_CXFRAME) +
3493 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3494 rect.bottom - rect.top
3495 - (GetSystemMetrics(SM_CYFRAME) +
3496 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3497 - GetSystemMetrics(SM_CYCAPTION)
3498#ifdef FEAT_MENU
3499 - gui_mswin_get_menu_height(FALSE)
3500#endif
3501 );
3502 }
3503 else
3504 {
3505 /* Inside another window, don't use the frame and border. */
3506 gui_resize_shell(rect.right - rect.left,
3507 rect.bottom - rect.top
3508#ifdef FEAT_MENU
3509 - gui_mswin_get_menu_height(FALSE)
3510#endif
3511 );
3512 }
3513}
3514
3515/*
3516 * Set the window title
3517 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 void
3519gui_mch_settitle(
3520 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003521 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522{
3523 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3524}
3525
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003526#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3528 * misc2.c! */
3529static LPCSTR mshape_idcs[] =
3530{
3531 IDC_ARROW, /* arrow */
3532 MAKEINTRESOURCE(0), /* blank */
3533 IDC_IBEAM, /* beam */
3534 IDC_SIZENS, /* updown */
3535 IDC_SIZENS, /* udsizing */
3536 IDC_SIZEWE, /* leftright */
3537 IDC_SIZEWE, /* lrsizing */
3538 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 IDC_ARROW, /* crosshair */
3541 IDC_ARROW, /* hand1 */
3542 IDC_ARROW, /* hand2 */
3543 IDC_ARROW, /* pencil */
3544 IDC_ARROW, /* question */
3545 IDC_ARROW, /* right-arrow */
3546 IDC_UPARROW, /* up-arrow */
3547 IDC_ARROW /* last one */
3548};
3549
3550 void
3551mch_set_mouse_shape(int shape)
3552{
3553 LPCSTR idc;
3554
3555 if (shape == MSHAPE_HIDE)
3556 ShowCursor(FALSE);
3557 else
3558 {
3559 if (shape >= MSHAPE_NUMBERED)
3560 idc = IDC_ARROW;
3561 else
3562 idc = mshape_idcs[shape];
3563#ifdef SetClassLongPtr
3564 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3565#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567#endif
3568 if (!p_mh)
3569 {
3570 POINT mp;
3571
3572 /* Set the position to make it redrawn with the new shape. */
3573 (void)GetCursorPos((LPPOINT)&mp);
3574 (void)SetCursorPos(mp.x, mp.y);
3575 ShowCursor(TRUE);
3576 }
3577 }
3578}
3579#endif
3580
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003581#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003582/*
3583 * The file browser exists in two versions: with "W" uses wide characters,
3584 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3585 * Windows NT/2000/XP the "W" functions are used.
3586 */
3587
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003588# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589/*
3590 * Wide version of convert_filter().
3591 */
3592 static WCHAR *
3593convert_filterW(char_u *s)
3594{
3595 char_u *tmp;
3596 int len;
3597 WCHAR *res;
3598
3599 tmp = convert_filter(s);
3600 if (tmp == NULL)
3601 return NULL;
3602 len = (int)STRLEN(s) + 3;
3603 res = enc_to_utf16(tmp, &len);
3604 vim_free(tmp);
3605 return res;
3606}
3607
3608/*
3609 * Wide version of gui_mch_browse(). Keep in sync!
3610 */
3611 static char_u *
3612gui_mch_browseW(
3613 int saving,
3614 char_u *title,
3615 char_u *dflt,
3616 char_u *ext,
3617 char_u *initdir,
3618 char_u *filter)
3619{
3620 /* We always use the wide function. This means enc_to_utf16() must work,
3621 * otherwise it fails miserably! */
3622 OPENFILENAMEW fileStruct;
3623 WCHAR fileBuf[MAXPATHL];
3624 WCHAR *wp;
3625 int i;
3626 WCHAR *titlep = NULL;
3627 WCHAR *extp = NULL;
3628 WCHAR *initdirp = NULL;
3629 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003630 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631
3632 if (dflt == NULL)
3633 fileBuf[0] = NUL;
3634 else
3635 {
3636 wp = enc_to_utf16(dflt, NULL);
3637 if (wp == NULL)
3638 fileBuf[0] = NUL;
3639 else
3640 {
3641 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3642 fileBuf[i] = wp[i];
3643 fileBuf[i] = NUL;
3644 vim_free(wp);
3645 }
3646 }
3647
3648 /* Convert the filter to Windows format. */
3649 filterp = convert_filterW(filter);
3650
3651 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003652# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003654 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003655# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003657# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658
3659 if (title != NULL)
3660 titlep = enc_to_utf16(title, NULL);
3661 fileStruct.lpstrTitle = titlep;
3662
3663 if (ext != NULL)
3664 extp = enc_to_utf16(ext, NULL);
3665 fileStruct.lpstrDefExt = extp;
3666
3667 fileStruct.lpstrFile = fileBuf;
3668 fileStruct.nMaxFile = MAXPATHL;
3669 fileStruct.lpstrFilter = filterp;
3670 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3671 /* has an initial dir been specified? */
3672 if (initdir != NULL && *initdir != NUL)
3673 {
3674 /* Must have backslashes here, no matter what 'shellslash' says */
3675 initdirp = enc_to_utf16(initdir, NULL);
3676 if (initdirp != NULL)
3677 {
3678 for (wp = initdirp; *wp != NUL; ++wp)
3679 if (*wp == '/')
3680 *wp = '\\';
3681 }
3682 fileStruct.lpstrInitialDir = initdirp;
3683 }
3684
3685 /*
3686 * TODO: Allow selection of multiple files. Needs another arg to this
3687 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3688 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3689 * files that don't exist yet, so I haven't put it in. What about
3690 * OFN_PATHMUSTEXIST?
3691 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3692 */
3693 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003694# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 if (curbuf->b_p_bin)
3696 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003697# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003698 if (saving)
3699 {
3700 if (!GetSaveFileNameW(&fileStruct))
3701 return NULL;
3702 }
3703 else
3704 {
3705 if (!GetOpenFileNameW(&fileStruct))
3706 return NULL;
3707 }
3708
3709 vim_free(filterp);
3710 vim_free(initdirp);
3711 vim_free(titlep);
3712 vim_free(extp);
3713
3714 /* Convert from UCS2 to 'encoding'. */
3715 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003716 if (p == NULL)
3717 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718
3719 /* Give focus back to main window (when using MDI). */
3720 SetFocus(s_hwnd);
3721
3722 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003723 q = vim_strsave(shorten_fname1(p));
3724 vim_free(p);
3725 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726}
3727# endif /* FEAT_MBYTE */
3728
3729
3730/*
3731 * Convert the string s to the proper format for a filter string by replacing
3732 * the \t and \n delimiters with \0.
3733 * Returns the converted string in allocated memory.
3734 *
3735 * Keep in sync with convert_filterW() above!
3736 */
3737 static char_u *
3738convert_filter(char_u *s)
3739{
3740 char_u *res;
3741 unsigned s_len = (unsigned)STRLEN(s);
3742 unsigned i;
3743
3744 res = alloc(s_len + 3);
3745 if (res != NULL)
3746 {
3747 for (i = 0; i < s_len; ++i)
3748 if (s[i] == '\t' || s[i] == '\n')
3749 res[i] = '\0';
3750 else
3751 res[i] = s[i];
3752 res[s_len] = NUL;
3753 /* Add two extra NULs to make sure it's properly terminated. */
3754 res[s_len + 1] = NUL;
3755 res[s_len + 2] = NUL;
3756 }
3757 return res;
3758}
3759
3760/*
3761 * Select a directory.
3762 */
3763 char_u *
3764gui_mch_browsedir(char_u *title, char_u *initdir)
3765{
3766 /* We fake this: Use a filter that doesn't select anything and a default
3767 * file name that won't be used. */
3768 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3769 initdir, (char_u *)_("Directory\t*.nothing\n"));
3770}
3771
3772/*
3773 * Pop open a file browser and return the file selected, in allocated memory,
3774 * or NULL if Cancel is hit.
3775 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3776 * title - Title message for the file browser dialog.
3777 * dflt - Default name of file.
3778 * ext - Default extension to be added to files without extensions.
3779 * initdir - directory in which to open the browser (NULL = current dir)
3780 * filter - Filter for matched files to choose from.
3781 *
3782 * Keep in sync with gui_mch_browseW() above!
3783 */
3784 char_u *
3785gui_mch_browse(
3786 int saving,
3787 char_u *title,
3788 char_u *dflt,
3789 char_u *ext,
3790 char_u *initdir,
3791 char_u *filter)
3792{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003793# ifdef FEAT_MBYTE
3794 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3795# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 OPENFILENAME fileStruct;
3797 char_u fileBuf[MAXPATHL];
3798 char_u *initdirp = NULL;
3799 char_u *filterp;
3800 char_u *p;
3801
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 if (dflt == NULL)
3803 fileBuf[0] = NUL;
3804 else
3805 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3806
3807 /* Convert the filter to Windows format. */
3808 filterp = convert_filter(filter);
3809
3810 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003811# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 /* be compatible with Windows NT 4.0 */
3813 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003814# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003816# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817
3818 fileStruct.lpstrTitle = (LPSTR)title;
3819 fileStruct.lpstrDefExt = (LPSTR)ext;
3820
3821 fileStruct.lpstrFile = (LPSTR)fileBuf;
3822 fileStruct.nMaxFile = MAXPATHL;
3823 fileStruct.lpstrFilter = (LPSTR)filterp;
3824 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3825 /* has an initial dir been specified? */
3826 if (initdir != NULL && *initdir != NUL)
3827 {
3828 /* Must have backslashes here, no matter what 'shellslash' says */
3829 initdirp = vim_strsave(initdir);
3830 if (initdirp != NULL)
3831 for (p = initdirp; *p != NUL; ++p)
3832 if (*p == '/')
3833 *p = '\\';
3834 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3835 }
3836
3837 /*
3838 * TODO: Allow selection of multiple files. Needs another arg to this
3839 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3840 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3841 * files that don't exist yet, so I haven't put it in. What about
3842 * OFN_PATHMUSTEXIST?
3843 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3844 */
3845 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003846# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 if (curbuf->b_p_bin)
3848 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003849# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 if (saving)
3851 {
3852 if (!GetSaveFileName(&fileStruct))
3853 return NULL;
3854 }
3855 else
3856 {
3857 if (!GetOpenFileName(&fileStruct))
3858 return NULL;
3859 }
3860
3861 vim_free(filterp);
3862 vim_free(initdirp);
3863
3864 /* Give focus back to main window (when using MDI). */
3865 SetFocus(s_hwnd);
3866
3867 /* Shorten the file name if possible */
3868 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003869# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870}
3871#endif /* FEAT_BROWSE */
3872
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003873 static void
3874_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003875 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876 HDROP hDrop)
3877{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003878#define BUFPATHLEN _MAX_PATH
3879#define DRAGQVAL 0xFFFFFFFF
3880#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003881 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003882#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 char szFile[BUFPATHLEN];
3884 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3885 UINT i;
3886 char_u **fnames;
3887 POINT pt;
3888 int_u modifiers = 0;
3889
3890 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3891
3892 /* Obtain dropped position */
3893 DragQueryPoint(hDrop, &pt);
3894 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3895
3896 reset_VIsual();
3897
3898 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3899
3900 if (fnames != NULL)
3901 for (i = 0; i < cFiles; ++i)
3902 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003903#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3905 fnames[i] = utf16_to_enc(wszFile, NULL);
3906 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003907#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 {
3909 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3910 fnames[i] = vim_strsave((char_u *)szFile);
3911 }
3912 }
3913
3914 DragFinish(hDrop);
3915
3916 if (fnames != NULL)
3917 {
3918 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3919 modifiers |= MOUSE_SHIFT;
3920 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3921 modifiers |= MOUSE_CTRL;
3922 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3923 modifiers |= MOUSE_ALT;
3924
3925 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3926
3927 s_need_activate = TRUE;
3928 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929}
3930
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 static int
3932_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003933 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934 HWND hwndCtl,
3935 UINT code,
3936 int pos)
3937{
3938 static UINT prev_code = 0; /* code of previous call */
3939 scrollbar_T *sb, *sb_info;
3940 long val;
3941 int dragging = FALSE;
3942 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003943 SCROLLINFO si;
3944
3945 si.cbSize = sizeof(si);
3946 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003947
3948 sb = gui_mswin_find_scrollbar(hwndCtl);
3949 if (sb == NULL)
3950 return 0;
3951
3952 if (sb->wp != NULL) /* Left or right scrollbar */
3953 {
3954 /*
3955 * Careful: need to get scrollbar info out of first (left) scrollbar
3956 * for window, but keep real scrollbar too because we must pass it to
3957 * gui_drag_scrollbar().
3958 */
3959 sb_info = &sb->wp->w_scrollbars[0];
3960 }
3961 else /* Bottom scrollbar */
3962 sb_info = sb;
3963 val = sb_info->value;
3964
3965 switch (code)
3966 {
3967 case SB_THUMBTRACK:
3968 val = pos;
3969 dragging = TRUE;
3970 if (sb->scroll_shift > 0)
3971 val <<= sb->scroll_shift;
3972 break;
3973 case SB_LINEDOWN:
3974 val++;
3975 break;
3976 case SB_LINEUP:
3977 val--;
3978 break;
3979 case SB_PAGEDOWN:
3980 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3981 break;
3982 case SB_PAGEUP:
3983 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3984 break;
3985 case SB_TOP:
3986 val = 0;
3987 break;
3988 case SB_BOTTOM:
3989 val = sb_info->max;
3990 break;
3991 case SB_ENDSCROLL:
3992 if (prev_code == SB_THUMBTRACK)
3993 {
3994 /*
3995 * "pos" only gives us 16-bit data. In case of large file,
3996 * use GetScrollPos() which returns 32-bit. Unfortunately it
3997 * is not valid while the scrollbar is being dragged.
3998 */
3999 val = GetScrollPos(hwndCtl, SB_CTL);
4000 if (sb->scroll_shift > 0)
4001 val <<= sb->scroll_shift;
4002 }
4003 break;
4004
4005 default:
4006 /* TRACE("Unknown scrollbar event %d\n", code); */
4007 return 0;
4008 }
4009 prev_code = code;
4010
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4012 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013
4014 /*
4015 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4016 */
4017 if (sb->wp != NULL)
4018 {
4019 scrollbar_T *sba = sb->wp->w_scrollbars;
4020 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4021
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004022 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023 }
4024
4025 /* Don't let us be interrupted here by another message. */
4026 s_busy_processing = TRUE;
4027
4028 /* When "allow_scrollbar" is FALSE still need to remember the new
4029 * position, but don't actually scroll by setting "dont_scroll". */
4030 dont_scroll = !allow_scrollbar;
4031
Bram Moolenaara338adc2018-01-31 20:51:47 +01004032 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004033 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004034 mch_enable_flush();
4035 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004036
4037 s_busy_processing = FALSE;
4038 dont_scroll = dont_scroll_save;
4039
4040 return 0;
4041}
4042
4043
4044/*
4045 * Get command line arguments.
4046 * Use "prog" as the name of the program and "cmdline" as the arguments.
4047 * Copy the arguments to allocated memory.
4048 * Return the number of arguments (including program name).
4049 * Return pointers to the arguments in "argvp". Memory is allocated with
4050 * malloc(), use free() instead of vim_free().
4051 * Return pointer to buffer in "tofree".
4052 * Returns zero when out of memory.
4053 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004054 int
4055get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4056{
4057 int i;
4058 char *p;
4059 char *progp;
4060 char *pnew = NULL;
4061 char *newcmdline;
4062 int inquote;
4063 int argc;
4064 char **argv = NULL;
4065 int round;
4066
4067 *tofree = NULL;
4068
4069#ifdef FEAT_MBYTE
4070 /* Try using the Unicode version first, it takes care of conversion when
4071 * 'encoding' is changed. */
4072 argc = get_cmd_argsW(&argv);
4073 if (argc != 0)
4074 goto done;
4075#endif
4076
4077 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4078 * non-space. */
4079 p = strrchr(prog, '.');
4080 if (p != NULL)
4081 *p = NUL;
4082 for (progp = prog; *progp == ' '; ++progp)
4083 ;
4084
4085 /* The command line is copied to allocated memory, so that we can change
4086 * it. Add the size of the string, the separating NUL and a terminating
4087 * NUL. */
4088 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4089 if (newcmdline == NULL)
4090 return 0;
4091
4092 /*
4093 * First round: count the number of arguments ("pnew" == NULL).
4094 * Second round: produce the arguments.
4095 */
4096 for (round = 1; round <= 2; ++round)
4097 {
4098 /* First argument is the program name. */
4099 if (pnew != NULL)
4100 {
4101 argv[0] = pnew;
4102 strcpy(pnew, progp);
4103 pnew += strlen(pnew);
4104 *pnew++ = NUL;
4105 }
4106
4107 /*
4108 * Isolate each argument and put it in argv[].
4109 */
4110 p = cmdline;
4111 argc = 1;
4112 while (*p != NUL)
4113 {
4114 inquote = FALSE;
4115 if (pnew != NULL)
4116 argv[argc] = pnew;
4117 ++argc;
4118 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4119 {
4120 /* Backslashes are only special when followed by a double
4121 * quote. */
4122 i = (int)strspn(p, "\\");
4123 if (p[i] == '"')
4124 {
4125 /* Halve the number of backslashes. */
4126 if (i > 1 && pnew != NULL)
4127 {
4128 vim_memset(pnew, '\\', i / 2);
4129 pnew += i / 2;
4130 }
4131
4132 /* Even nr of backslashes toggles quoting, uneven copies
4133 * the double quote. */
4134 if ((i & 1) == 0)
4135 inquote = !inquote;
4136 else if (pnew != NULL)
4137 *pnew++ = '"';
4138 p += i + 1;
4139 }
4140 else if (i > 0)
4141 {
4142 /* Copy span of backslashes unmodified. */
4143 if (pnew != NULL)
4144 {
4145 vim_memset(pnew, '\\', i);
4146 pnew += i;
4147 }
4148 p += i;
4149 }
4150 else
4151 {
4152 if (pnew != NULL)
4153 *pnew++ = *p;
4154#ifdef FEAT_MBYTE
4155 /* Can't use mb_* functions, because 'encoding' is not
4156 * initialized yet here. */
4157 if (IsDBCSLeadByte(*p))
4158 {
4159 ++p;
4160 if (pnew != NULL)
4161 *pnew++ = *p;
4162 }
4163#endif
4164 ++p;
4165 }
4166 }
4167
4168 if (pnew != NULL)
4169 *pnew++ = NUL;
4170 while (*p == ' ' || *p == '\t')
4171 ++p; /* advance until a non-space */
4172 }
4173
4174 if (round == 1)
4175 {
4176 argv = (char **)malloc((argc + 1) * sizeof(char *));
4177 if (argv == NULL )
4178 {
4179 free(newcmdline);
4180 return 0; /* malloc error */
4181 }
4182 pnew = newcmdline;
4183 *tofree = newcmdline;
4184 }
4185 }
4186
4187#ifdef FEAT_MBYTE
4188done:
4189#endif
4190 argv[argc] = NULL; /* NULL-terminated list */
4191 *argvp = argv;
4192 return argc;
4193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195#ifdef FEAT_XPM_W32
4196# include "xpm_w32.h"
4197#endif
4198
4199#ifdef PROTO
4200# define WINAPI
4201#endif
4202
4203#ifdef __MINGW32__
4204/*
4205 * Add a lot of missing defines.
4206 * They are not always missing, we need the #ifndef's.
4207 */
4208# ifndef _cdecl
4209# define _cdecl
4210# endif
4211# ifndef IsMinimized
4212# define IsMinimized(hwnd) IsIconic(hwnd)
4213# endif
4214# ifndef IsMaximized
4215# define IsMaximized(hwnd) IsZoomed(hwnd)
4216# endif
4217# ifndef SelectFont
4218# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4219# endif
4220# ifndef GetStockBrush
4221# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4222# endif
4223# ifndef DeleteBrush
4224# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4225# endif
4226
4227# ifndef HANDLE_WM_RBUTTONDBLCLK
4228# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4229 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4230# endif
4231# ifndef HANDLE_WM_MBUTTONUP
4232# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4233 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4234# endif
4235# ifndef HANDLE_WM_MBUTTONDBLCLK
4236# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4237 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4238# endif
4239# ifndef HANDLE_WM_LBUTTONDBLCLK
4240# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4241 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4242# endif
4243# ifndef HANDLE_WM_RBUTTONDOWN
4244# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4245 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4246# endif
4247# ifndef HANDLE_WM_MOUSEMOVE
4248# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4249 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4250# endif
4251# ifndef HANDLE_WM_RBUTTONUP
4252# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4253 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4254# endif
4255# ifndef HANDLE_WM_MBUTTONDOWN
4256# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4257 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4258# endif
4259# ifndef HANDLE_WM_LBUTTONUP
4260# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4261 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4262# endif
4263# ifndef HANDLE_WM_LBUTTONDOWN
4264# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4265 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4266# endif
4267# ifndef HANDLE_WM_SYSCHAR
4268# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4269 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4270# endif
4271# ifndef HANDLE_WM_ACTIVATEAPP
4272# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4273 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4274# endif
4275# ifndef HANDLE_WM_WINDOWPOSCHANGING
4276# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4277 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4278# endif
4279# ifndef HANDLE_WM_VSCROLL
4280# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4281 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4282# endif
4283# ifndef HANDLE_WM_SETFOCUS
4284# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4285 ((fn)((hwnd), (HWND)(wParam)), 0L)
4286# endif
4287# ifndef HANDLE_WM_KILLFOCUS
4288# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4289 ((fn)((hwnd), (HWND)(wParam)), 0L)
4290# endif
4291# ifndef HANDLE_WM_HSCROLL
4292# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4293 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4294# endif
4295# ifndef HANDLE_WM_DROPFILES
4296# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4297 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4298# endif
4299# ifndef HANDLE_WM_CHAR
4300# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4301 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4302# endif
4303# ifndef HANDLE_WM_SYSDEADCHAR
4304# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4305 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4306# endif
4307# ifndef HANDLE_WM_DEADCHAR
4308# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4309 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4310# endif
4311#endif /* __MINGW32__ */
4312
4313
4314/* Some parameters for tearoff menus. All in pixels. */
4315#define TEAROFF_PADDING_X 2
4316#define TEAROFF_BUTTON_PAD_X 8
4317#define TEAROFF_MIN_WIDTH 200
4318#define TEAROFF_SUBMENU_LABEL ">>"
4319#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4320
4321
4322/* For the Intellimouse: */
4323#ifndef WM_MOUSEWHEEL
4324#define WM_MOUSEWHEEL 0x20a
4325#endif
4326
4327
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004328#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329# define ID_BEVAL_TOOLTIP 200
4330# define BEVAL_TEXT_LEN MAXPATHL
4331
Bram Moolenaar167632f2010-05-26 21:42:54 +02004332#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004333/* Work around old versions of basetsd.h which wrongly declares
4334 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004335# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004336# define UINT_PTR UINT
4337#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004340static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004342
Bram Moolenaar82881492012-11-20 16:53:39 +01004343
4344/* cproto fails on missing include files */
4345#ifndef PROTO
4346
Bram Moolenaar45360022005-07-21 21:08:21 +00004347/*
4348 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004349 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004350 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004351# include <pshpack1.h>
4352
4353#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004354
4355typedef struct _DllVersionInfo
4356{
4357 DWORD cbSize;
4358 DWORD dwMajorVersion;
4359 DWORD dwMinorVersion;
4360 DWORD dwBuildNumber;
4361 DWORD dwPlatformID;
4362} DLLVERSIONINFO;
4363
Bram Moolenaar82881492012-11-20 16:53:39 +01004364#ifndef PROTO
4365# include <poppack.h>
4366#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004367
Bram Moolenaar45360022005-07-21 21:08:21 +00004368typedef struct tagTOOLINFOA_NEW
4369{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004370 UINT cbSize;
4371 UINT uFlags;
4372 HWND hwnd;
4373 UINT_PTR uId;
4374 RECT rect;
4375 HINSTANCE hinst;
4376 LPSTR lpszText;
4377 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00004378} TOOLINFO_NEW;
4379
4380typedef struct tagNMTTDISPINFO_NEW
4381{
4382 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004383 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004384 char szText[80];
4385 HINSTANCE hinst;
4386 UINT uFlags;
4387 LPARAM lParam;
4388} NMTTDISPINFO_NEW;
4389
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004390#ifdef FEAT_MBYTE
4391typedef struct tagTOOLINFOW_NEW
4392{
4393 UINT cbSize;
4394 UINT uFlags;
4395 HWND hwnd;
4396 UINT_PTR uId;
4397 RECT rect;
4398 HINSTANCE hinst;
4399 LPWSTR lpszText;
4400 LPARAM lParam;
4401 void *lpReserved;
4402} TOOLINFOW_NEW;
4403
4404typedef struct tagNMTTDISPINFOW_NEW
4405{
4406 NMHDR hdr;
4407 LPWSTR lpszText;
4408 WCHAR szText[80];
4409 HINSTANCE hinst;
4410 UINT uFlags;
4411 LPARAM lParam;
4412} NMTTDISPINFOW_NEW;
4413
4414#endif
4415
Bram Moolenaar45360022005-07-21 21:08:21 +00004416typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4417#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004418# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420
Bram Moolenaar45360022005-07-21 21:08:21 +00004421#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004422# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004423#endif
4424
4425#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004426# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004427#endif
4428
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004429#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004430
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004431#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4432/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4433 * it here if LPNMTTDISPINFO isn't defined.
4434 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4435 * _MSC_VER. */
4436# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4437typedef struct tagNMTTDISPINFOA {
4438 NMHDR hdr;
4439 LPSTR lpszText;
4440 char szText[80];
4441 HINSTANCE hinst;
4442 UINT uFlags;
4443 LPARAM lParam;
4444} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4445# define LPNMTTDISPINFO LPNMTTDISPINFOA
4446
4447# ifdef FEAT_MBYTE
4448typedef struct tagNMTTDISPINFOW {
4449 NMHDR hdr;
4450 LPWSTR lpszText;
4451 WCHAR szText[80];
4452 HINSTANCE hinst;
4453 UINT uFlags;
4454 LPARAM lParam;
4455} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4456# endif
4457# endif
4458#endif
4459
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004460#ifndef TTN_GETDISPINFOW
4461# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4462#endif
4463
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464/* Local variables: */
4465
4466#ifdef FEAT_MENU
4467static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004468#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470/*
4471 * Use the system font for dialogs and tear-off menus. Remove this line to
4472 * use DLG_FONT_NAME.
4473 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004474#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
4476#define VIM_NAME "vim"
4477#define VIM_CLASS "Vim"
4478#define VIM_CLASSW L"Vim"
4479
4480/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4481 * thus there should be room for every dialog. For tearoffs it's made bigger
4482 * when needed. */
4483#define DLG_ALLOC_SIZE 16 * 1024
4484
4485/*
4486 * stuff for dialogs, menus, tearoffs etc.
4487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488static PWORD
4489add_dialog_element(
4490 PWORD p,
4491 DWORD lStyle,
4492 WORD x,
4493 WORD y,
4494 WORD w,
4495 WORD h,
4496 WORD Id,
4497 WORD clss,
4498 const char *caption);
4499static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004500static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004501#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504static void get_dialog_font_metrics(void);
4505
4506static int dialog_default_button = -1;
4507
4508/* Intellimouse support */
4509static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4512#ifdef FEAT_TOOLBAR
4513static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004514static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515static int get_toolbar_bitmap(vimmenu_T *menu);
4516#endif
4517
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004518#ifdef FEAT_GUI_TABLINE
4519static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004520static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004521#endif
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#ifdef FEAT_MBYTE_IME
4524static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4525static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4526#endif
4527#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4528# ifdef NOIME
4529typedef struct tagCOMPOSITIONFORM {
4530 DWORD dwStyle;
4531 POINT ptCurrentPos;
4532 RECT rcArea;
4533} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4534typedef HANDLE HIMC;
4535# endif
4536
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004537static HINSTANCE hLibImm = NULL;
4538static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4539static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4540static HIMC (WINAPI *pImmGetContext)(HWND);
4541static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4542static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4543static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4544static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4545static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4546static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4547static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4548static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004549static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550static void dyn_imm_load(void);
4551#else
4552# define pImmGetCompositionStringA ImmGetCompositionStringA
4553# define pImmGetCompositionStringW ImmGetCompositionStringW
4554# define pImmGetContext ImmGetContext
4555# define pImmAssociateContext ImmAssociateContext
4556# define pImmReleaseContext ImmReleaseContext
4557# define pImmGetOpenStatus ImmGetOpenStatus
4558# define pImmSetOpenStatus ImmSetOpenStatus
4559# define pImmGetCompositionFont ImmGetCompositionFontA
4560# define pImmSetCompositionFont ImmSetCompositionFontA
4561# define pImmSetCompositionWindow ImmSetCompositionWindow
4562# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004563# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564#endif
4565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566#ifdef FEAT_MENU
4567/*
4568 * Figure out how high the menu bar is at the moment.
4569 */
4570 static int
4571gui_mswin_get_menu_height(
4572 int fix_window) /* If TRUE, resize window if menu height changed */
4573{
4574 static int old_menu_height = -1;
4575
4576 RECT rc1, rc2;
4577 int num;
4578 int menu_height;
4579
4580 if (gui.menu_is_active)
4581 num = GetMenuItemCount(s_menuBar);
4582 else
4583 num = 0;
4584
4585 if (num == 0)
4586 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004587 else if (IsMinimized(s_hwnd))
4588 {
4589 /* The height of the menu cannot be determined while the window is
4590 * minimized. Take the previous height if the menu is changed in that
4591 * state, to avoid that Vim's vertical window size accidentally
4592 * increases due to the unaccounted-for menu height. */
4593 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 else
4596 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004597 /*
4598 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4599 * seem to have been set yet, so menu wraps in default window
4600 * width which is very narrow. Instead just return height of a
4601 * single menu item. Will still be wrong when the menu really
4602 * should wrap over more than one line.
4603 */
4604 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4605 if (gui.starting)
4606 menu_height = rc1.bottom - rc1.top + 1;
4607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004609 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4610 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612 }
4613
4614 if (fix_window && menu_height != old_menu_height)
4615 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004616 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004618 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619
4620 return menu_height;
4621}
4622#endif /*FEAT_MENU*/
4623
4624
4625/*
4626 * Setup for the Intellimouse
4627 */
4628 static void
4629init_mouse_wheel(void)
4630{
4631
4632#ifndef SPI_GETWHEELSCROLLLINES
4633# define SPI_GETWHEELSCROLLLINES 104
4634#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004635#ifndef SPI_SETWHEELSCROLLLINES
4636# define SPI_SETWHEELSCROLLLINES 105
4637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
4639#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4640#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4641#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4642#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 mouse_scroll_lines = 3; /* reasonable default */
4645
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004646 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4647 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4648 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649}
4650
4651
4652/* Intellimouse wheel handler */
4653 static void
4654_OnMouseWheel(
4655 HWND hwnd,
4656 short zDelta)
4657{
4658/* Treat a mouse wheel event as if it were a scroll request */
4659 int i;
4660 int size;
4661 HWND hwndCtl;
4662
4663 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4664 {
4665 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4666 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4667 }
4668 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4669 {
4670 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4671 size = curwin->w_scrollbars[SBAR_LEFT].size;
4672 }
4673 else
4674 return;
4675
4676 size = curwin->w_height;
4677 if (mouse_scroll_lines == 0)
4678 init_mouse_wheel();
4679
Bram Moolenaara338adc2018-01-31 20:51:47 +01004680 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (mouse_scroll_lines > 0
4682 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4683 {
4684 for (i = mouse_scroll_lines; i > 0; --i)
4685 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4686 }
4687 else
4688 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004689 mch_enable_flush();
4690 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004693#ifdef USE_SYSMENU_FONT
4694/*
4695 * Get Menu Font.
4696 * Return OK or FAIL.
4697 */
4698 static int
4699gui_w32_get_menu_font(LOGFONT *lf)
4700{
4701 NONCLIENTMETRICS nm;
4702
4703 nm.cbSize = sizeof(NONCLIENTMETRICS);
4704 if (!SystemParametersInfo(
4705 SPI_GETNONCLIENTMETRICS,
4706 sizeof(NONCLIENTMETRICS),
4707 &nm,
4708 0))
4709 return FAIL;
4710 *lf = nm.lfMenuFont;
4711 return OK;
4712}
4713#endif
4714
4715
4716#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4717/*
4718 * Set the GUI tabline font to the system menu font
4719 */
4720 static void
4721set_tabline_font(void)
4722{
4723 LOGFONT lfSysmenu;
4724 HFONT font;
4725 HWND hwnd;
4726 HDC hdc;
4727 HFONT hfntOld;
4728 TEXTMETRIC tm;
4729
4730 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4731 return;
4732
4733 font = CreateFontIndirect(&lfSysmenu);
4734
4735 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4736
4737 /*
4738 * Compute the height of the font used for the tab text
4739 */
4740 hwnd = GetDesktopWindow();
4741 hdc = GetWindowDC(hwnd);
4742 hfntOld = SelectFont(hdc, font);
4743
4744 GetTextMetrics(hdc, &tm);
4745
4746 SelectFont(hdc, hfntOld);
4747 ReleaseDC(hwnd, hdc);
4748
4749 /*
4750 * The space used by the tab border and the space between the tab label
4751 * and the tab border is included as 7.
4752 */
4753 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4754}
4755#endif
4756
Bram Moolenaar520470a2005-06-16 21:59:56 +00004757/*
4758 * Invoked when a setting was changed.
4759 */
4760 static LRESULT CALLBACK
4761_OnSettingChange(UINT n)
4762{
4763 if (n == SPI_SETWHEELSCROLLLINES)
4764 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4765 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004766#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4767 if (n == SPI_SETNONCLIENTMETRICS)
4768 set_tabline_font();
4769#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004770 return 0;
4771}
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773#ifdef FEAT_NETBEANS_INTG
4774 static void
4775_OnWindowPosChanged(
4776 HWND hwnd,
4777 const LPWINDOWPOS lpwpos)
4778{
4779 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004780 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4783 || lpwpos->cx != cx || lpwpos->cy != cy))
4784 {
4785 x = lpwpos->x;
4786 y = lpwpos->y;
4787 cx = lpwpos->cx;
4788 cy = lpwpos->cy;
4789 netbeans_frame_moved(x, y);
4790 }
4791 /* Allow to send WM_SIZE and WM_MOVE */
4792 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4793}
4794#endif
4795
4796 static int
4797_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 UINT fwSide,
4799 LPRECT lprc)
4800{
4801 int w, h;
4802 int valid_w, valid_h;
4803 int w_offset, h_offset;
4804
4805 w = lprc->right - lprc->left;
4806 h = lprc->bottom - lprc->top;
4807 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4808 w_offset = w - valid_w;
4809 h_offset = h - valid_h;
4810
4811 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4812 || fwSide == WMSZ_BOTTOMLEFT)
4813 lprc->left += w_offset;
4814 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4815 || fwSide == WMSZ_BOTTOMRIGHT)
4816 lprc->right -= w_offset;
4817
4818 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4819 || fwSide == WMSZ_TOPRIGHT)
4820 lprc->top += h_offset;
4821 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4822 || fwSide == WMSZ_BOTTOMRIGHT)
4823 lprc->bottom -= h_offset;
4824 return TRUE;
4825}
4826
4827
4828
4829 static LRESULT CALLBACK
4830_WndProc(
4831 HWND hwnd,
4832 UINT uMsg,
4833 WPARAM wParam,
4834 LPARAM lParam)
4835{
4836 /*
4837 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4838 hwnd, uMsg, wParam, lParam);
4839 */
4840
4841 HandleMouseHide(uMsg, lParam);
4842
4843 s_uMsg = uMsg;
4844 s_wParam = wParam;
4845 s_lParam = lParam;
4846
4847 switch (uMsg)
4848 {
4849 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4850 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4851 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4852 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4853 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4854 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4855 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4856 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4857 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4858#ifdef FEAT_MENU
4859 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4860#endif
4861 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4862 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4863 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4864 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4865 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4866 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4867 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4868 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4869 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4870#ifdef FEAT_NETBEANS_INTG
4871 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4872#endif
4873
Bram Moolenaarafa24992006-03-27 20:58:26 +00004874#ifdef FEAT_GUI_TABLINE
4875 case WM_RBUTTONUP:
4876 {
4877 if (gui_mch_showing_tabline())
4878 {
4879 POINT pt;
4880 RECT rect;
4881
4882 /*
4883 * If the cursor is on the tabline, display the tab menu
4884 */
4885 GetCursorPos((LPPOINT)&pt);
4886 GetWindowRect(s_textArea, &rect);
4887 if (pt.y < rect.top)
4888 {
4889 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004890 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004891 }
4892 }
4893 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4894 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004895 case WM_LBUTTONDBLCLK:
4896 {
4897 /*
4898 * If the user double clicked the tabline, create a new tab
4899 */
4900 if (gui_mch_showing_tabline())
4901 {
4902 POINT pt;
4903 RECT rect;
4904
4905 GetCursorPos((LPPOINT)&pt);
4906 GetWindowRect(s_textArea, &rect);
4907 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004908 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004909 }
4910 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4911 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004912#endif
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 case WM_QUERYENDSESSION: /* System wants to go down. */
4915 gui_shell_closed(); /* Will exit when no changed buffers. */
4916 return FALSE; /* Do NOT allow system to go down. */
4917
4918 case WM_ENDSESSION:
4919 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004922 return 0L;
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 break;
4925
4926 case WM_CHAR:
4927 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4928 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004929 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return 0L;
4931
4932 case WM_SYSCHAR:
4933 /*
4934 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4935 * shortcut key, handle like a typed ALT key, otherwise call Windows
4936 * ALT key handling.
4937 */
4938#ifdef FEAT_MENU
4939 if ( !gui.menu_is_active
4940 || p_wak[0] == 'n'
4941 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4942 )
4943#endif
4944 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004945 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return 0L;
4947 }
4948#ifdef FEAT_MENU
4949 else
4950 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4951#endif
4952
4953 case WM_SYSKEYUP:
4954#ifdef FEAT_MENU
4955 /* This used to be done only when menu is active: ALT key is used for
4956 * that. But that caused problems when menu is disabled and using
4957 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4958 * are received, mouse pointer remains hidden. */
4959 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4960#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004961 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#endif
4963
4964 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 case WM_MOUSEWHEEL:
4968 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004969 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
Bram Moolenaar520470a2005-06-16 21:59:56 +00004971 /* Notification for change in SystemParametersInfo() */
4972 case WM_SETTINGCHANGE:
4973 return _OnSettingChange((UINT)wParam);
4974
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004975#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 case WM_NOTIFY:
4977 switch (((LPNMHDR) lParam)->code)
4978 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004979# ifdef FEAT_MBYTE
4980 case TTN_GETDISPINFOW:
4981# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004982 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004984 LPNMHDR hdr = (LPNMHDR)lParam;
4985 char_u *str = NULL;
4986 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004987
Bram Moolenaard23a8232018-02-10 18:45:26 +01004988 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004989
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004990# ifdef FEAT_GUI_TABLINE
4991 if (gui_mch_showing_tabline()
4992 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004993 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004994 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004995 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004996 * Mouse is over the GUI tabline. Display the
4997 * tooltip for the tab under the cursor
4998 *
4999 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005000 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005001 GetCursorPos(&pt);
5002 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005003 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005004 TCHITTESTINFO htinfo;
5005 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005006
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005007 /*
5008 * Get the tab under the cursor
5009 */
5010 htinfo.pt.x = pt.x;
5011 htinfo.pt.y = pt.y;
5012 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
5013 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005014 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005015 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005016
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005017 tp = find_tabpage(idx + 1);
5018 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005019 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005020 get_tabline_label(tp, TRUE);
5021 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005022 }
5023 }
5024 }
5025 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005026# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005027# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005028# ifdef FEAT_GUI_TABLINE
5029 else
5030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005032 UINT idButton;
5033 vimmenu_T *pMenu;
5034
5035 idButton = (UINT) hdr->idFrom;
5036 pMenu = gui_mswin_find_menu(root_menu, idButton);
5037 if (pMenu)
5038 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005040# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005041 if (str != NULL)
5042 {
5043# ifdef FEAT_MBYTE
5044 if (hdr->code == TTN_GETDISPINFOW)
5045 {
5046 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5047
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005048 /* Set the maximum width, this also enables using
5049 * \n for line break. */
5050 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5051 0, 500);
5052
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005053 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005054 lpdi->lpszText = tt_text;
5055 /* can't show tooltip if failed */
5056 }
5057 else
5058# endif
5059 {
5060 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5061
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005062 /* Set the maximum width, this also enables using
5063 * \n for line break. */
5064 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5065 0, 500);
5066
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005067 if (STRLEN(str) < sizeof(lpdi->szText)
5068 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005069 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005070 sizeof(lpdi->szText) - 1);
5071 else
5072 lpdi->lpszText = tt_text;
5073 }
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005077# ifdef FEAT_GUI_TABLINE
5078 case TCN_SELCHANGE:
5079 if (gui_mch_showing_tabline()
5080 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005081 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005082 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005083 return 0L;
5084 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005085 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005086
5087 case NM_RCLICK:
5088 if (gui_mch_showing_tabline()
5089 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005090 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005091 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005092 return 0L;
5093 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005094 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005097# ifdef FEAT_GUI_TABLINE
5098 if (gui_mch_showing_tabline()
5099 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5100 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 break;
5103 }
5104 break;
5105#endif
5106#if defined(MENUHINTS) && defined(FEAT_MENU)
5107 case WM_MENUSELECT:
5108 if (((UINT) HIWORD(wParam)
5109 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5110 == MF_HILITE
5111 && (State & CMDLINE) == 0)
5112 {
5113 UINT idButton;
5114 vimmenu_T *pMenu;
5115 static int did_menu_tip = FALSE;
5116
5117 if (did_menu_tip)
5118 {
5119 msg_clr_cmdline();
5120 setcursor();
5121 out_flush();
5122 did_menu_tip = FALSE;
5123 }
5124
5125 idButton = (UINT)LOWORD(wParam);
5126 pMenu = gui_mswin_find_menu(root_menu, idButton);
5127 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5128 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5129 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005130 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005132 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 setcursor();
5134 out_flush();
5135 did_menu_tip = TRUE;
5136 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005137 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 break;
5140#endif
5141 case WM_NCHITTEST:
5142 {
5143 LRESULT result;
5144 int x, y;
5145 int xPos = GET_X_LPARAM(lParam);
5146
5147 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5148 if (result == HTCLIENT)
5149 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005150#ifdef FEAT_GUI_TABLINE
5151 if (gui_mch_showing_tabline())
5152 {
5153 int yPos = GET_Y_LPARAM(lParam);
5154 RECT rct;
5155
5156 /* If the cursor is on the GUI tabline, don't process this
5157 * event */
5158 GetWindowRect(s_textArea, &rct);
5159 if (yPos < rct.top)
5160 return result;
5161 }
5162#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005163 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 xPos -= x;
5165
5166 if (xPos < 48) /* <VN> TODO should use system metric? */
5167 return HTBOTTOMLEFT;
5168 else
5169 return HTBOTTOMRIGHT;
5170 }
5171 else
5172 return result;
5173 }
5174 /* break; notreached */
5175
5176#ifdef FEAT_MBYTE_IME
5177 case WM_IME_NOTIFY:
5178 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5179 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005180 return 1L;
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 case WM_IME_COMPOSITION:
5183 if (!_OnImeComposition(hwnd, wParam, lParam))
5184 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005185 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#endif
5187
5188 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005190 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 _OnFindRepl();
5193 }
5194#endif
5195 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5196 }
5197
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005198 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199}
5200
5201/*
5202 * End of call-back routines
5203 */
5204
5205/* parent window, if specified with -P */
5206HWND vim_parent_hwnd = NULL;
5207
5208 static BOOL CALLBACK
5209FindWindowTitle(HWND hwnd, LPARAM lParam)
5210{
5211 char buf[2048];
5212 char *title = (char *)lParam;
5213
5214 if (GetWindowText(hwnd, buf, sizeof(buf)))
5215 {
5216 if (strstr(buf, title) != NULL)
5217 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005218 /* Found it. Store the window ref. and quit searching if MDI
5219 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005221 if (vim_parent_hwnd != NULL)
5222 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224 }
5225 return TRUE; /* continue searching */
5226}
5227
5228/*
5229 * Invoked for '-P "title"' argument: search for parent application to open
5230 * our window in.
5231 */
5232 void
5233gui_mch_set_parent(char *title)
5234{
5235 EnumWindows(FindWindowTitle, (LPARAM)title);
5236 if (vim_parent_hwnd == NULL)
5237 {
5238 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5239 mch_exit(2);
5240 }
5241}
5242
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005243#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 static void
5245ole_error(char *arg)
5246{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005247 char buf[IOSIZE];
5248
5249 /* Can't use EMSG() here, we have not finished initialisation yet. */
5250 vim_snprintf(buf, IOSIZE,
5251 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5252 arg);
5253 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256
5257/*
5258 * Parse the GUI related command-line arguments. Any arguments used are
5259 * deleted from argv, and *argc is decremented accordingly. This is called
5260 * when vim is started, whether or not the GUI has been started.
5261 */
5262 void
5263gui_mch_prepare(int *argc, char **argv)
5264{
5265 int silent = FALSE;
5266 int idx;
5267
5268 /* Check for special OLE command line parameters */
5269 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5270 {
5271 /* Check for a "-silent" argument first. */
5272 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5273 && (argv[2][0] == '-' || argv[2][0] == '/'))
5274 {
5275 silent = TRUE;
5276 idx = 2;
5277 }
5278 else
5279 idx = 1;
5280
5281 /* Register Vim as an OLE Automation server */
5282 if (STRICMP(argv[idx] + 1, "register") == 0)
5283 {
5284#ifdef FEAT_OLE
5285 RegisterMe(silent);
5286 mch_exit(0);
5287#else
5288 if (!silent)
5289 ole_error("register");
5290 mch_exit(2);
5291#endif
5292 }
5293
5294 /* Unregister Vim as an OLE Automation server */
5295 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5296 {
5297#ifdef FEAT_OLE
5298 UnregisterMe(!silent);
5299 mch_exit(0);
5300#else
5301 if (!silent)
5302 ole_error("unregister");
5303 mch_exit(2);
5304#endif
5305 }
5306
5307 /* Ignore an -embedding argument. It is only relevant if the
5308 * application wants to treat the case when it is started manually
5309 * differently from the case where it is started via automation (and
5310 * we don't).
5311 */
5312 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5313 {
5314#ifdef FEAT_OLE
5315 *argc = 1;
5316#else
5317 ole_error("embedding");
5318 mch_exit(2);
5319#endif
5320 }
5321 }
5322
5323#ifdef FEAT_OLE
5324 {
5325 int bDoRestart = FALSE;
5326
5327 InitOLE(&bDoRestart);
5328 /* automatically exit after registering */
5329 if (bDoRestart)
5330 mch_exit(0);
5331 }
5332#endif
5333
5334#ifdef FEAT_NETBEANS_INTG
5335 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005336 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 int arg;
5338
5339 for (arg = 1; arg < *argc; arg++)
5340 if (strncmp("-nb", argv[arg], 3) == 0)
5341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 netbeansArg = argv[arg];
5343 mch_memmove(&argv[arg], &argv[arg + 1],
5344 (--*argc - arg) * sizeof(char *));
5345 argv[*argc] = NULL;
5346 break; /* enough? */
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
5351
5352/*
5353 * Initialise the GUI. Create all the windows, set up all the call-backs
5354 * etc.
5355 */
5356 int
5357gui_mch_init(void)
5358{
5359 const char szVimWndClass[] = VIM_CLASS;
5360 const char szTextAreaClass[] = "VimTextArea";
5361 WNDCLASS wndclass;
5362#ifdef FEAT_MBYTE
5363 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005364 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 WNDCLASSW wndclassw;
5366#endif
5367#ifdef GLOBAL_IME
5368 ATOM atom;
5369#endif
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 /* Return here if the window was already opened (happens when
5372 * gui_mch_dialog() is called early). */
5373 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005374 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 /*
5377 * Load the tearoff bitmap
5378 */
5379#ifdef FEAT_TEAROFF
5380 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5381#endif
5382
5383 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5384 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5385#ifdef FEAT_MENU
5386 gui.menu_height = 0; /* Windows takes care of this */
5387#endif
5388 gui.border_width = 0;
5389
5390 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5391
5392#ifdef FEAT_MBYTE
5393 /* First try using the wide version, so that we can use any title.
5394 * Otherwise only characters in the active codepage will work. */
5395 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5396 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005397 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 wndclassw.lpfnWndProc = _WndProc;
5399 wndclassw.cbClsExtra = 0;
5400 wndclassw.cbWndExtra = 0;
5401 wndclassw.hInstance = s_hinst;
5402 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5403 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5404 wndclassw.hbrBackground = s_brush;
5405 wndclassw.lpszMenuName = NULL;
5406 wndclassw.lpszClassName = szVimWndClassW;
5407
5408 if ((
5409#ifdef GLOBAL_IME
5410 atom =
5411#endif
5412 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 else
5415 wide_WindowProc = TRUE;
5416 }
5417
5418 if (!wide_WindowProc)
5419#endif
5420
5421 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5422 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005423 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 wndclass.lpfnWndProc = _WndProc;
5425 wndclass.cbClsExtra = 0;
5426 wndclass.cbWndExtra = 0;
5427 wndclass.hInstance = s_hinst;
5428 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5429 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5430 wndclass.hbrBackground = s_brush;
5431 wndclass.lpszMenuName = NULL;
5432 wndclass.lpszClassName = szVimWndClass;
5433
5434 if ((
5435#ifdef GLOBAL_IME
5436 atom =
5437#endif
5438 RegisterClass(&wndclass)) == 0)
5439 return FAIL;
5440 }
5441
5442 if (vim_parent_hwnd != NULL)
5443 {
5444#ifdef HAVE_TRY_EXCEPT
5445 __try
5446 {
5447#endif
5448 /* Open inside the specified parent window.
5449 * TODO: last argument should point to a CLIENTCREATESTRUCT
5450 * structure. */
5451 s_hwnd = CreateWindowEx(
5452 WS_EX_MDICHILD,
5453 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005454 WS_OVERLAPPEDWINDOW | WS_CHILD
5455 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5457 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5458 100, /* Any value will do */
5459 100, /* Any value will do */
5460 vim_parent_hwnd, NULL,
5461 s_hinst, NULL);
5462#ifdef HAVE_TRY_EXCEPT
5463 }
5464 __except(EXCEPTION_EXECUTE_HANDLER)
5465 {
5466 /* NOP */
5467 }
5468#endif
5469 if (s_hwnd == NULL)
5470 {
5471 EMSG(_("E672: Unable to open window inside MDI application"));
5472 mch_exit(2);
5473 }
5474 }
5475 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005476 {
5477 /* If the provided windowid is not valid reset it to zero, so that it
5478 * is ignored and we open our own window. */
5479 if (IsWindow((HWND)win_socket_id) <= 0)
5480 win_socket_id = 0;
5481
5482 /* Create a window. If win_socket_id is not zero without border and
5483 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005485 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005486 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5487 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005488 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5489 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5490 100, /* Any value will do */
5491 100, /* Any value will do */
5492 NULL, NULL,
5493 s_hinst, NULL);
5494 if (s_hwnd != NULL && win_socket_id != 0)
5495 {
5496 SetParent(s_hwnd, (HWND)win_socket_id);
5497 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5498 }
5499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501 if (s_hwnd == NULL)
5502 return FAIL;
5503
5504#ifdef GLOBAL_IME
5505 global_ime_init(atom, s_hwnd);
5506#endif
5507#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5508 dyn_imm_load();
5509#endif
5510
5511 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005512#ifdef FEAT_MBYTE
5513 if (wide_WindowProc)
5514 {
5515 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5516 {
5517 wndclassw.style = CS_OWNDC;
5518 wndclassw.lpfnWndProc = _TextAreaWndProc;
5519 wndclassw.cbClsExtra = 0;
5520 wndclassw.cbWndExtra = 0;
5521 wndclassw.hInstance = s_hinst;
5522 wndclassw.hIcon = NULL;
5523 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5524 wndclassw.hbrBackground = NULL;
5525 wndclassw.lpszMenuName = NULL;
5526 wndclassw.lpszClassName = szTextAreaClassW;
5527
5528 if (RegisterClassW(&wndclassw) == 0)
5529 return FAIL;
5530 }
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005531
5532 s_textArea = CreateWindowExW(
5533 0,
5534 szTextAreaClassW, L"Vim text area",
5535 WS_CHILD | WS_VISIBLE, 0, 0,
5536 100, // Any value will do for now
5537 100, // Any value will do for now
5538 s_hwnd, NULL,
5539 s_hinst, NULL);
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005540 }
5541 else
5542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5544 {
5545 wndclass.style = CS_OWNDC;
5546 wndclass.lpfnWndProc = _TextAreaWndProc;
5547 wndclass.cbClsExtra = 0;
5548 wndclass.cbWndExtra = 0;
5549 wndclass.hInstance = s_hinst;
5550 wndclass.hIcon = NULL;
5551 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5552 wndclass.hbrBackground = NULL;
5553 wndclass.lpszMenuName = NULL;
5554 wndclass.lpszClassName = szTextAreaClass;
5555
5556 if (RegisterClass(&wndclass) == 0)
5557 return FAIL;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005558
5559 s_textArea = CreateWindowEx(
5560 0,
5561 szTextAreaClass, "Vim text area",
5562 WS_CHILD | WS_VISIBLE, 0, 0,
5563 100, // Any value will do for now
5564 100, // Any value will do for now
5565 s_hwnd, NULL,
5566 s_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (s_textArea == NULL)
5570 return FAIL;
5571
Bram Moolenaar20321902016-02-17 12:30:17 +01005572#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005573 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5574 {
5575 HANDLE hIcon = NULL;
5576
5577 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005578 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005579 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005580#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582#ifdef FEAT_MENU
5583 s_menuBar = CreateMenu();
5584#endif
5585 s_hdc = GetDC(s_textArea);
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589 /* Do we need to bother with this? */
5590 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5591
5592 /* Get background/foreground colors from the system */
5593 gui_mch_def_colors();
5594
5595 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5596 * file) */
5597 set_normal_colors();
5598
5599 /*
5600 * Check that none of the colors are the same as the background color.
5601 * Then store the current values as the defaults.
5602 */
5603 gui_check_colors();
5604 gui.def_norm_pixel = gui.norm_pixel;
5605 gui.def_back_pixel = gui.back_pixel;
5606
5607 /* Get the colors for the highlight groups (gui_check_colors() might have
5608 * changed them) */
5609 highlight_gui_started();
5610
5611 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005612 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005614 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 /*
5617 * Set up for Intellimouse processing
5618 */
5619 init_mouse_wheel();
5620
5621 /*
5622 * compute a couple of metrics used for the dialogs
5623 */
5624 get_dialog_font_metrics();
5625#ifdef FEAT_TOOLBAR
5626 /*
5627 * Create the toolbar
5628 */
5629 initialise_toolbar();
5630#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005631#ifdef FEAT_GUI_TABLINE
5632 /*
5633 * Create the tabline
5634 */
5635 initialise_tabline();
5636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#ifdef MSWIN_FIND_REPLACE
5638 /*
5639 * Initialise the dialog box stuff
5640 */
5641 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5642
5643 /* Initialise the struct */
5644 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005645 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005647 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5649 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5650 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005651# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005652 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5653 s_findrep_struct_w.lpstrFindWhat =
5654 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5655 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5656 s_findrep_struct_w.lpstrReplaceWith =
5657 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5658 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5659 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5660 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5661# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005664#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005665# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5666/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5667# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005668# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005669# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005670# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005671 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005672 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005673#endif
5674
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005675#ifdef FEAT_RENDER_OPTIONS
5676 if (p_rop)
5677 (void)gui_mch_set_rendering_options(p_rop);
5678#endif
5679
Bram Moolenaar748bf032005-02-02 23:04:36 +00005680theend:
5681 /* Display any pending error messages */
5682 display_errors();
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 return OK;
5685}
5686
5687/*
5688 * Get the size of the screen, taking position on multiple monitors into
5689 * account (if supported).
5690 */
5691 static void
5692get_work_area(RECT *spi_rect)
5693{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005694 HMONITOR mon;
5695 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01005697 /* work out which monitor the window is on, and get *its* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005698 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005699 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005701 moninfo.cbSize = sizeof(MONITORINFO);
5702 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005704 *spi_rect = moninfo.rcWork;
5705 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 }
5708 /* this is the old method... */
5709 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5710}
5711
5712/*
5713 * Set the size of the window to the given width and height in pixels.
5714 */
5715 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005716gui_mch_set_shellsize(
5717 int width,
5718 int height,
5719 int min_width UNUSED,
5720 int min_height UNUSED,
5721 int base_width UNUSED,
5722 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005723 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 RECT workarea_rect;
5726 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 WINDOWPLACEMENT wndpl;
5728
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005729 /* Try to keep window completely on screen. */
5730 /* Get position of the screen work area. This is the part that is not
5731 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 get_work_area(&workarea_rect);
5733
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005734 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005735 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 wndpl.length = sizeof(WINDOWPLACEMENT);
5737 GetWindowPlacement(s_hwnd, &wndpl);
5738
5739 /* Resizing a maximized window looks very strange, unzoom it first.
5740 * But don't do it when still starting up, it may have been requested in
5741 * the shortcut. */
5742 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5743 {
5744 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5745 /* Need to get the settings of the normal window. */
5746 GetWindowPlacement(s_hwnd, &wndpl);
5747 }
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005750 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005751 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005752 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005753 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 + GetSystemMetrics(SM_CYCAPTION)
5755#ifdef FEAT_MENU
5756 + gui_mswin_get_menu_height(FALSE)
5757#endif
5758 ;
5759
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005760 /* The following should take care of keeping Vim on the same monitor, no
5761 * matter if the secondary monitor is left or right of the primary
5762 * monitor. */
5763 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5764 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005765
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005766 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005767 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005768 && wndpl.rcNormalPosition.right > workarea_rect.right)
5769 OffsetRect(&wndpl.rcNormalPosition,
5770 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005772 if ((direction & RESIZE_HOR)
5773 && wndpl.rcNormalPosition.left < workarea_rect.left)
5774 OffsetRect(&wndpl.rcNormalPosition,
5775 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
Bram Moolenaarafa24992006-03-27 20:58:26 +00005777 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005778 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5779 OffsetRect(&wndpl.rcNormalPosition,
5780 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005782 if ((direction & RESIZE_VERT)
5783 && wndpl.rcNormalPosition.top < workarea_rect.top)
5784 OffsetRect(&wndpl.rcNormalPosition,
5785 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
5787 /* set window position - we should use SetWindowPlacement rather than
5788 * SetWindowPos as the MSDN docs say the coord systems returned by
5789 * these two are not compatible. */
5790 SetWindowPlacement(s_hwnd, &wndpl);
5791
5792 SetActiveWindow(s_hwnd);
5793 SetFocus(s_hwnd);
5794
5795#ifdef FEAT_MENU
5796 /* Menu may wrap differently now */
5797 gui_mswin_get_menu_height(!gui.starting);
5798#endif
5799}
5800
5801
5802 void
5803gui_mch_set_scrollbar_thumb(
5804 scrollbar_T *sb,
5805 long val,
5806 long size,
5807 long max)
5808{
5809 SCROLLINFO info;
5810
5811 sb->scroll_shift = 0;
5812 while (max > 32767)
5813 {
5814 max = (max + 1) >> 1;
5815 val >>= 1;
5816 size >>= 1;
5817 ++sb->scroll_shift;
5818 }
5819
5820 if (sb->scroll_shift > 0)
5821 ++size;
5822
5823 info.cbSize = sizeof(info);
5824 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5825 info.nPos = val;
5826 info.nMin = 0;
5827 info.nMax = max;
5828 info.nPage = size;
5829 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5830}
5831
5832
5833/*
5834 * Set the current text font.
5835 */
5836 void
5837gui_mch_set_font(GuiFont font)
5838{
5839 gui.currFont = font;
5840}
5841
5842
5843/*
5844 * Set the current text foreground color.
5845 */
5846 void
5847gui_mch_set_fg_color(guicolor_T color)
5848{
5849 gui.currFgColor = color;
5850}
5851
5852/*
5853 * Set the current text background color.
5854 */
5855 void
5856gui_mch_set_bg_color(guicolor_T color)
5857{
5858 gui.currBgColor = color;
5859}
5860
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005861/*
5862 * Set the current text special color.
5863 */
5864 void
5865gui_mch_set_sp_color(guicolor_T color)
5866{
5867 gui.currSpColor = color;
5868}
5869
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005870#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871/*
5872 * Multi-byte handling, originally by Sung-Hoon Baek.
5873 * First static functions (no prototypes generated).
5874 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005875# ifdef _MSC_VER
5876# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5877# endif
5878# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 * handle WM_IME_NOTIFY message
5882 */
5883 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005884_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 LRESULT lResult = 0;
5887 HIMC hImc;
5888
5889 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5890 return lResult;
5891 switch (dwCommand)
5892 {
5893 case IMN_SETOPENSTATUS:
5894 if (pImmGetOpenStatus(hImc))
5895 {
5896 pImmSetCompositionFont(hImc, &norm_logfont);
5897 im_set_position(gui.row, gui.col);
5898
5899 /* Disable langmap */
5900 State &= ~LANGMAP;
5901 if (State & INSERT)
5902 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005903#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 /* Unshown 'keymap' in status lines */
5905 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5906 {
5907 /* Save cursor position */
5908 int old_row = gui.row;
5909 int old_col = gui.col;
5910
5911 // This must be called here before
5912 // status_redraw_curbuf(), otherwise the mode
5913 // message may appear in the wrong position.
5914 showmode();
5915 status_redraw_curbuf();
5916 update_screen(0);
5917 /* Restore cursor position */
5918 gui.row = old_row;
5919 gui.col = old_col;
5920 }
5921#endif
5922 }
5923 }
5924 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005925 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 lResult = 0;
5927 break;
5928 }
5929 pImmReleaseContext(hWnd, hImc);
5930 return lResult;
5931}
5932
5933 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005934_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 char_u *ret;
5937 int len;
5938
5939 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5940 return 0;
5941
5942 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5943 if (ret != NULL)
5944 {
5945 add_to_input_buf_csi(ret, len);
5946 vim_free(ret);
5947 return 1;
5948 }
5949 return 0;
5950}
5951
5952/*
5953 * get the current composition string, in UCS-2; *lenp is the number of
5954 * *lenp is the number of Unicode characters.
5955 */
5956 static short_u *
5957GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5958{
5959 LONG ret;
5960 LPWSTR wbuf = NULL;
5961 char_u *buf;
5962
5963 if (!pImmGetContext)
5964 return NULL; /* no imm32.dll */
5965
5966 /* Try Unicode; this'll always work on NT regardless of codepage. */
5967 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5968 if (ret == 0)
5969 return NULL; /* empty */
5970
5971 if (ret > 0)
5972 {
5973 /* Allocate the requested buffer plus space for the NUL character. */
5974 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5975 if (wbuf != NULL)
5976 {
5977 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5978 *lenp = ret / sizeof(WCHAR);
5979 }
5980 return (short_u *)wbuf;
5981 }
5982
5983 /* ret < 0; we got an error, so try the ANSI version. This'll work
5984 * on 9x/ME, but only if the codepage happens to be set to whatever
5985 * we're inputting. */
5986 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5987 if (ret <= 0)
5988 return NULL; /* empty or error */
5989
5990 buf = alloc(ret);
5991 if (buf == NULL)
5992 return NULL;
5993 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5994
5995 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005996 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 vim_free(buf);
5998
5999 return (short_u *)wbuf;
6000}
6001
6002/*
6003 * void GetResultStr()
6004 *
6005 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6006 * get complete composition string
6007 */
6008 static char_u *
6009GetResultStr(HWND hwnd, int GCS, int *lenp)
6010{
6011 HIMC hIMC; /* Input context handle. */
6012 short_u *buf = NULL;
6013 char_u *convbuf = NULL;
6014
6015 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6016 return NULL;
6017
6018 /* Reads in the composition string. */
6019 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
6020 if (buf == NULL)
6021 return NULL;
6022
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006023 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 pImmReleaseContext(hwnd, hIMC);
6025 vim_free(buf);
6026 return convbuf;
6027}
6028#endif
6029
6030/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006031#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
6033/*
6034 * set font to IM.
6035 */
6036 void
6037im_set_font(LOGFONT *lf)
6038{
6039 HIMC hImc;
6040
6041 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6042 {
6043 pImmSetCompositionFont(hImc, lf);
6044 pImmReleaseContext(s_hwnd, hImc);
6045 }
6046}
6047
6048/*
6049 * Notify cursor position to IM.
6050 */
6051 void
6052im_set_position(int row, int col)
6053{
6054 HIMC hImc;
6055
6056 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6057 {
6058 COMPOSITIONFORM cfs;
6059
6060 cfs.dwStyle = CFS_POINT;
6061 cfs.ptCurrentPos.x = FILL_X(col);
6062 cfs.ptCurrentPos.y = FILL_Y(row);
6063 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6064 pImmSetCompositionWindow(hImc, &cfs);
6065
6066 pImmReleaseContext(s_hwnd, hImc);
6067 }
6068}
6069
6070/*
6071 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6072 */
6073 void
6074im_set_active(int active)
6075{
6076 HIMC hImc;
6077 static HIMC hImcOld = (HIMC)0;
6078
6079 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6080 {
6081 if (p_imdisable)
6082 {
6083 if (hImcOld == (HIMC)0)
6084 {
6085 hImcOld = pImmGetContext(s_hwnd);
6086 if (hImcOld)
6087 pImmAssociateContext(s_hwnd, (HIMC)0);
6088 }
6089 active = FALSE;
6090 }
6091 else if (hImcOld != (HIMC)0)
6092 {
6093 pImmAssociateContext(s_hwnd, hImcOld);
6094 hImcOld = (HIMC)0;
6095 }
6096
6097 hImc = pImmGetContext(s_hwnd);
6098 if (hImc)
6099 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006100 /*
6101 * for Korean ime
6102 */
6103 HKL hKL = GetKeyboardLayout(0);
6104
6105 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6106 {
6107 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6108 static BOOL bSaved = FALSE;
6109
6110 if (active)
6111 {
6112 /* if we have a saved conversion status, restore it */
6113 if (bSaved)
6114 pImmSetConversionStatus(hImc, dwConversionSaved,
6115 dwSentenceSaved);
6116 bSaved = FALSE;
6117 }
6118 else
6119 {
6120 /* save conversion status and disable korean */
6121 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6122 &dwSentenceSaved))
6123 {
6124 bSaved = TRUE;
6125 pImmSetConversionStatus(hImc,
6126 dwConversionSaved & ~(IME_CMODE_NATIVE
6127 | IME_CMODE_FULLSHAPE),
6128 dwSentenceSaved);
6129 }
6130 }
6131 }
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 pImmSetOpenStatus(hImc, active);
6134 pImmReleaseContext(s_hwnd, hImc);
6135 }
6136 }
6137}
6138
6139/*
6140 * Get IM status. When IM is on, return not 0. Else return 0.
6141 */
6142 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006143im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 int status = 0;
6146 HIMC hImc;
6147
6148 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6149 {
6150 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6151 pImmReleaseContext(s_hwnd, hImc);
6152 }
6153 return status;
6154}
6155
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006156#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6159/* Win32 with GLOBAL IME */
6160
6161/*
6162 * Notify cursor position to IM.
6163 */
6164 void
6165im_set_position(int row, int col)
6166{
6167 /* Win32 with GLOBAL IME */
6168 POINT p;
6169
6170 p.x = FILL_X(col);
6171 p.y = FILL_Y(row);
6172 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6173 global_ime_set_position(&p);
6174}
6175
6176/*
6177 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6178 */
6179 void
6180im_set_active(int active)
6181{
6182 global_ime_set_status(active);
6183}
6184
6185/*
6186 * Get IM status. When IM is on, return not 0. Else return 0.
6187 */
6188 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006189im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 return global_ime_get_status();
6192}
6193#endif
6194
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006195#ifdef FEAT_MBYTE
6196/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006197 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006198 */
6199 static void
6200latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6201{
6202 int c;
6203
Bram Moolenaarca003e12006-03-17 23:19:38 +00006204 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006205 {
6206 c = *text++;
6207 switch (c)
6208 {
6209 case 0xa4: c = 0x20ac; break; /* euro */
6210 case 0xa6: c = 0x0160; break; /* S hat */
6211 case 0xa8: c = 0x0161; break; /* S -hat */
6212 case 0xb4: c = 0x017d; break; /* Z hat */
6213 case 0xb8: c = 0x017e; break; /* Z -hat */
6214 case 0xbc: c = 0x0152; break; /* OE */
6215 case 0xbd: c = 0x0153; break; /* oe */
6216 case 0xbe: c = 0x0178; break; /* Y */
6217 }
6218 *unicodebuf++ = c;
6219 }
6220}
6221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223#ifdef FEAT_RIGHTLEFT
6224/*
6225 * What is this for? In the case where you are using Win98 or Win2K or later,
6226 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6227 * reverses the string sent to the TextOut... family. This sucks, because we
6228 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6229 * way to tell Windblows not to do this!
6230 *
6231 * The short of it is that this 'RevOut' only gets called if you are running
6232 * one of the new, "improved" MS OSes, and only if you are running in
6233 * 'rightleft' mode. It makes display take *slightly* longer, but not
6234 * noticeably so.
6235 */
6236 static void
6237RevOut( HDC s_hdc,
6238 int col,
6239 int row,
6240 UINT foptions,
6241 CONST RECT *pcliprect,
6242 LPCTSTR text,
6243 UINT len,
6244 CONST INT *padding)
6245{
6246 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006248 for (ix = 0; ix < (int)len; ++ix)
6249 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6250 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251}
6252#endif
6253
Bram Moolenaar92467d32017-12-05 13:22:16 +01006254 static void
6255draw_line(
6256 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006257 int y1,
6258 int x2,
6259 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006260 COLORREF color)
6261{
6262#if defined(FEAT_DIRECTX)
6263 if (IS_ENABLE_DIRECTX())
6264 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6265 else
6266#endif
6267 {
6268 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6269 HPEN old_pen = SelectObject(s_hdc, hpen);
6270 MoveToEx(s_hdc, x1, y1, NULL);
6271 /* Note: LineTo() excludes the last pixel in the line. */
6272 LineTo(s_hdc, x2, y2);
6273 DeleteObject(SelectObject(s_hdc, old_pen));
6274 }
6275}
6276
6277 static void
6278set_pixel(
6279 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006280 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006281 COLORREF color)
6282{
6283#if defined(FEAT_DIRECTX)
6284 if (IS_ENABLE_DIRECTX())
6285 DWriteContext_SetPixel(s_dwc, x, y, color);
6286 else
6287#endif
6288 SetPixel(s_hdc, x, y, color);
6289}
6290
6291 static void
6292fill_rect(
6293 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006294 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006295 COLORREF color)
6296{
6297#if defined(FEAT_DIRECTX)
6298 if (IS_ENABLE_DIRECTX())
6299 DWriteContext_FillRect(s_dwc, rcp, color);
6300 else
6301#endif
6302 {
6303 HBRUSH hbr2;
6304
6305 if (hbr == NULL)
6306 hbr2 = CreateSolidBrush(color);
6307 else
6308 hbr2 = hbr;
6309 FillRect(s_hdc, rcp, hbr2);
6310 if (hbr == NULL)
6311 DeleteBrush(hbr2);
6312 }
6313}
6314
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 void
6316gui_mch_draw_string(
6317 int row,
6318 int col,
6319 char_u *text,
6320 int len,
6321 int flags)
6322{
6323 static int *padding = NULL;
6324 static int pad_size = 0;
6325 int i;
6326 const RECT *pcliprect = NULL;
6327 UINT foptions = 0;
6328#ifdef FEAT_MBYTE
6329 static WCHAR *unicodebuf = NULL;
6330 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006331 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 int n = 0;
6333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 int y;
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 /*
6337 * Italic and bold text seems to have an extra row of pixels at the bottom
6338 * (below where the bottom of the character should be). If we draw the
6339 * characters with a solid background, the top row of pixels in the
6340 * character below will be overwritten. We can fix this by filling in the
6341 * background ourselves, to the correct character proportions, and then
6342 * writing the character in transparent mode. Still have a problem when
6343 * the character is "_", which gets written on to the character below.
6344 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6345 * pixel in their slots, which fixes the problem with the bottom row of
6346 * pixels. We still need this code because otherwise the top row of pixels
6347 * becomes a problem. - webb.
6348 */
6349 static HBRUSH hbr_cache[2] = {NULL, NULL};
6350 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6351 static int brush_lru = 0;
6352 HBRUSH hbr;
6353 RECT rc;
6354
6355 if (!(flags & DRAW_TRANSP))
6356 {
6357 /*
6358 * Clear background first.
6359 * Note: FillRect() excludes right and bottom of rectangle.
6360 */
6361 rc.left = FILL_X(col);
6362 rc.top = FILL_Y(row);
6363#ifdef FEAT_MBYTE
6364 if (has_mbyte)
6365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006367 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 else
6370#endif
6371 rc.right = FILL_X(col + len);
6372 rc.bottom = FILL_Y(row + 1);
6373
6374 /* Cache the created brush, that saves a lot of time. We need two:
6375 * one for cursor background and one for the normal background. */
6376 if (gui.currBgColor == brush_color[0])
6377 {
6378 hbr = hbr_cache[0];
6379 brush_lru = 1;
6380 }
6381 else if (gui.currBgColor == brush_color[1])
6382 {
6383 hbr = hbr_cache[1];
6384 brush_lru = 0;
6385 }
6386 else
6387 {
6388 if (hbr_cache[brush_lru] != NULL)
6389 DeleteBrush(hbr_cache[brush_lru]);
6390 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6391 brush_color[brush_lru] = gui.currBgColor;
6392 hbr = hbr_cache[brush_lru];
6393 brush_lru = !brush_lru;
6394 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006395
Bram Moolenaar92467d32017-12-05 13:22:16 +01006396 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397
6398 SetBkMode(s_hdc, TRANSPARENT);
6399
6400 /*
6401 * When drawing block cursor, prevent inverted character spilling
6402 * over character cell (can happen with bold/italic)
6403 */
6404 if (flags & DRAW_CURSOR)
6405 {
6406 pcliprect = &rc;
6407 foptions = ETO_CLIPPED;
6408 }
6409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 SetTextColor(s_hdc, gui.currFgColor);
6411 SelectFont(s_hdc, gui.currFont);
6412
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006413#ifdef FEAT_DIRECTX
6414 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006415 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006416#endif
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6419 {
6420 vim_free(padding);
6421 pad_size = Columns;
6422
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006423 /* Don't give an out-of-memory message here, it would call us
6424 * recursively. */
6425 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (padding != NULL)
6427 for (i = 0; i < pad_size; i++)
6428 padding[i] = gui.char_width;
6429 }
6430
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 /*
6432 * We have to provide the padding argument because italic and bold versions
6433 * of fixed-width fonts are often one pixel or so wider than their normal
6434 * versions.
6435 * No check for DRAW_BOLD, Windows will have done it already.
6436 */
6437
6438#ifdef FEAT_MBYTE
6439 /* Check if there are any UTF-8 characters. If not, use normal text
6440 * output to speed up output. */
6441 if (enc_utf8)
6442 for (n = 0; n < len; ++n)
6443 if (text[n] >= 0x80)
6444 break;
6445
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006446# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006447 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6448 * required that unicode drawing routine, currently. So this forces it
6449 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006450 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006451 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006452# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006455 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006457 if ((enc_utf8
6458 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6459 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 && (unicodebuf == NULL || len > unibuflen))
6461 {
6462 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006463 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464
6465 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006466 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
6468 unibuflen = len;
6469 }
6470
6471 if (enc_utf8 && n < len && unicodebuf != NULL)
6472 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006473 /* Output UTF-8 characters. Composing characters should be
6474 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006475 int i;
6476 int wlen; /* string length in words */
6477 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 int cells; /* cell width of string up to composing char */
6479 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006480 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006482 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006483 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006485 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006487 c = utf_ptr2char(text + i);
6488 if (c >= 0x10000)
6489 {
6490 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006491 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6492 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006493 }
6494 else
6495 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006496 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006497 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006498
6499 if (utf_iscomposing(c))
6500 cw = 0;
6501 else
6502 {
6503 cw = utf_char2cells(c);
6504 if (cw > 2) /* don't use 4 for unprintable char */
6505 cw = 1;
6506 }
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 if (unicodepdy != NULL)
6509 {
6510 /* Use unicodepdy to make characters fit as we expect, even
6511 * when the font uses different widths (e.g., bold character
6512 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006513 if (c >= 0x10000)
6514 {
6515 unicodepdy[wlen - 2] = cw * gui.char_width;
6516 unicodepdy[wlen - 1] = 0;
6517 }
6518 else
6519 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006522 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006523 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006525# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006526 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006527 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006528 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006529 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006530 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006531 gui.char_width, gui.currFgColor,
6532 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006533 }
6534 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006535# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006536 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6537 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 len = cells; /* used for underlining */
6539 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006540 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
6542 /* If we want to display codepage data, and the current CP is not the
6543 * ANSI one, we need to go via Unicode. */
6544 if (unicodebuf != NULL)
6545 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006546 if (enc_latin9)
6547 latin9_to_ucs(text, len, unicodebuf);
6548 else
6549 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 MB_PRECOMPOSED,
6551 (char *)text, len,
6552 (LPWSTR)unicodebuf, unibuflen);
6553 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006554 {
6555 /* Use unicodepdy to make characters fit as we expect, even
6556 * when the font uses different widths (e.g., bold character
6557 * is wider). */
6558 if (unicodepdy != NULL)
6559 {
6560 int i;
6561 int cw;
6562
6563 for (i = 0; i < len; ++i)
6564 {
6565 cw = utf_char2cells(unicodebuf[i]);
6566 if (cw > 2)
6567 cw = 1;
6568 unicodepdy[i] = cw * gui.char_width;
6569 }
6570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006572 foptions, pcliprect, unicodebuf, len, unicodepdy);
6573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
6575 }
6576 else
6577#endif
6578 {
6579#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006580 /* Windows will mess up RL text, so we have to draw it character by
6581 * character. Only do this if RL is on, since it's slow. */
6582 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6584 foptions, pcliprect, (char *)text, len, padding);
6585 else
6586#endif
6587 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6588 foptions, pcliprect, (char *)text, len, padding);
6589 }
6590
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006591 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (flags & DRAW_UNDERL)
6593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 /* When p_linespace is 0, overwrite the bottom row of pixels.
6595 * Otherwise put the line just below the character. */
6596 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (p_linespace > 1)
6598 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006599 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006601
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006602 /* Strikethrough */
6603 if (flags & DRAW_STRIKE)
6604 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006605 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006606 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006607 }
6608
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006609 /* Undercurl */
6610 if (flags & DRAW_UNDERC)
6611 {
6612 int x;
6613 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006614 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006615
6616 y = FILL_Y(row + 1) - 1;
6617 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6618 {
6619 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006620 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006621 }
6622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625
6626/*
6627 * Output routines.
6628 */
6629
6630/* Flush any output to the screen */
6631 void
6632gui_mch_flush(void)
6633{
6634# if defined(__BORLANDC__)
6635 /*
6636 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6637 * prototype declaration.
6638 * The compiler complains if __stdcall is not used in both declarations.
6639 */
6640 BOOL __stdcall GdiFlush(void);
6641# endif
6642
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006643#if defined(FEAT_DIRECTX)
6644 if (IS_ENABLE_DIRECTX())
6645 DWriteContext_Flush(s_dwc);
6646#endif
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 GdiFlush();
6649}
6650
6651 static void
6652clear_rect(RECT *rcp)
6653{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006654 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655}
6656
6657
Bram Moolenaarc716c302006-01-21 22:12:51 +00006658 void
6659gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6660{
6661 RECT workarea_rect;
6662
6663 get_work_area(&workarea_rect);
6664
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006665 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006666 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006667 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006668
6669 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6670 * the menubar for MSwin, we subtract it from the screen height, so that
6671 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006672 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006673 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006674 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006675 - GetSystemMetrics(SM_CYCAPTION)
6676#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006677 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006678#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006679 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006680}
6681
6682
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683#if defined(FEAT_MENU) || defined(PROTO)
6684/*
6685 * Add a sub menu to the menu bar.
6686 */
6687 void
6688gui_mch_add_menu(
6689 vimmenu_T *menu,
6690 int pos)
6691{
6692 vimmenu_T *parent = menu->parent;
6693
6694 menu->submenu_id = CreatePopupMenu();
6695 menu->id = s_menu_id++;
6696
6697 if (menu_is_menubar(menu->name))
6698 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006700 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006702 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6703 {
6704 /* 'encoding' differs from active codepage: convert menu name
6705 * and use wide function */
6706 wn = enc_to_utf16(menu->name, NULL);
6707 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006709 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006711 infow.cbSize = sizeof(infow);
6712 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6713 | MIIM_SUBMENU;
6714 infow.dwItemData = (long_u)menu;
6715 infow.wID = menu->id;
6716 infow.fType = MFT_STRING;
6717 infow.dwTypeData = wn;
6718 infow.cch = (UINT)wcslen(wn);
6719 infow.hSubMenu = menu->submenu_id;
6720 InsertMenuItemW((parent == NULL)
6721 ? s_menuBar : parent->submenu_id,
6722 (UINT)pos, TRUE, &infow);
6723 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006727 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006729 {
6730 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006732 info.cbSize = sizeof(info);
6733 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6734 info.dwItemData = (long_u)menu;
6735 info.wID = menu->id;
6736 info.fType = MFT_STRING;
6737 info.dwTypeData = (LPTSTR)menu->name;
6738 info.cch = (UINT)STRLEN(menu->name);
6739 info.hSubMenu = menu->submenu_id;
6740 InsertMenuItem((parent == NULL)
6741 ? s_menuBar : parent->submenu_id,
6742 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744 }
6745
6746 /* Fix window size if menu may have wrapped */
6747 if (parent == NULL)
6748 gui_mswin_get_menu_height(!gui.starting);
6749#ifdef FEAT_TEAROFF
6750 else if (IsWindow(parent->tearoff_handle))
6751 rebuild_tearoff(parent);
6752#endif
6753}
6754
6755 void
6756gui_mch_show_popupmenu(vimmenu_T *menu)
6757{
6758 POINT mp;
6759
6760 (void)GetCursorPos((LPPOINT)&mp);
6761 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6762}
6763
6764 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006765gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 vimmenu_T *menu = gui_find_menu(path_name);
6768
6769 if (menu != NULL)
6770 {
6771 POINT p;
6772
6773 /* Find the position of the current cursor */
6774 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006775 if (mouse_pos)
6776 {
6777 int mx, my;
6778
6779 gui_mch_getmouse(&mx, &my);
6780 p.x += mx;
6781 p.y += my;
6782 }
6783 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006785 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6787 }
6788 msg_scroll = FALSE;
6789 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6790 }
6791}
6792
6793#if defined(FEAT_TEAROFF) || defined(PROTO)
6794/*
6795 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6796 * create it as a pseudo-"tearoff menu".
6797 */
6798 void
6799gui_make_tearoff(char_u *path_name)
6800{
6801 vimmenu_T *menu = gui_find_menu(path_name);
6802
6803 /* Found the menu, so tear it off. */
6804 if (menu != NULL)
6805 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6806}
6807#endif
6808
6809/*
6810 * Add a menu item to a menu
6811 */
6812 void
6813gui_mch_add_menu_item(
6814 vimmenu_T *menu,
6815 int idx)
6816{
6817 vimmenu_T *parent = menu->parent;
6818
6819 menu->id = s_menu_id++;
6820 menu->submenu_id = NULL;
6821
6822#ifdef FEAT_TEAROFF
6823 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6824 {
6825 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6826 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6827 }
6828 else
6829#endif
6830#ifdef FEAT_TOOLBAR
6831 if (menu_is_toolbar(parent->name))
6832 {
6833 TBBUTTON newtb;
6834
6835 vim_memset(&newtb, 0, sizeof(newtb));
6836 if (menu_is_separator(menu->name))
6837 {
6838 newtb.iBitmap = 0;
6839 newtb.fsStyle = TBSTYLE_SEP;
6840 }
6841 else
6842 {
6843 newtb.iBitmap = get_toolbar_bitmap(menu);
6844 newtb.fsStyle = TBSTYLE_BUTTON;
6845 }
6846 newtb.idCommand = menu->id;
6847 newtb.fsState = TBSTATE_ENABLED;
6848 newtb.iString = 0;
6849 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6850 (LPARAM)&newtb);
6851 menu->submenu_id = (HMENU)-1;
6852 }
6853 else
6854#endif
6855 {
6856#ifdef FEAT_MBYTE
6857 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6860 {
6861 /* 'encoding' differs from active codepage: convert menu item name
6862 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006863 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 if (wn != NULL)
6865 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006866 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 (menu_is_separator(menu->name)
6868 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6869 (UINT)menu->id, wn);
6870 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872 }
6873 if (wn == NULL)
6874#endif
6875 InsertMenu(parent->submenu_id, (UINT)idx,
6876 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6877 | MF_BYPOSITION,
6878 (UINT)menu->id, (LPCTSTR)menu->name);
6879#ifdef FEAT_TEAROFF
6880 if (IsWindow(parent->tearoff_handle))
6881 rebuild_tearoff(parent);
6882#endif
6883 }
6884}
6885
6886/*
6887 * Destroy the machine specific menu widget.
6888 */
6889 void
6890gui_mch_destroy_menu(vimmenu_T *menu)
6891{
6892#ifdef FEAT_TOOLBAR
6893 /*
6894 * is this a toolbar button?
6895 */
6896 if (menu->submenu_id == (HMENU)-1)
6897 {
6898 int iButton;
6899
6900 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6901 (WPARAM)menu->id, 0);
6902 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6903 }
6904 else
6905#endif
6906 {
6907 if (menu->parent != NULL
6908 && menu_is_popup(menu->parent->dname)
6909 && menu->parent->submenu_id != NULL)
6910 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6911 else
6912 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6913 if (menu->submenu_id != NULL)
6914 DestroyMenu(menu->submenu_id);
6915#ifdef FEAT_TEAROFF
6916 if (IsWindow(menu->tearoff_handle))
6917 DestroyWindow(menu->tearoff_handle);
6918 if (menu->parent != NULL
6919 && menu->parent->children != NULL
6920 && IsWindow(menu->parent->tearoff_handle))
6921 {
6922 /* This menu must not show up when rebuilding the tearoff window. */
6923 menu->modes = 0;
6924 rebuild_tearoff(menu->parent);
6925 }
6926#endif
6927 }
6928}
6929
6930#ifdef FEAT_TEAROFF
6931 static void
6932rebuild_tearoff(vimmenu_T *menu)
6933{
6934 /*hackish*/
6935 char_u tbuf[128];
6936 RECT trect;
6937 RECT rct;
6938 RECT roct;
6939 int x, y;
6940
6941 HWND thwnd = menu->tearoff_handle;
6942
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006943 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 if (GetWindowRect(thwnd, &trect)
6945 && GetWindowRect(s_hwnd, &rct)
6946 && GetClientRect(s_hwnd, &roct))
6947 {
6948 x = trect.left - rct.left;
6949 y = (trect.top - rct.bottom + roct.bottom);
6950 }
6951 else
6952 {
6953 x = y = 0xffffL;
6954 }
6955 DestroyWindow(thwnd);
6956 if (menu->children != NULL)
6957 {
6958 gui_mch_tearoff(tbuf, menu, x, y);
6959 if (IsWindow(menu->tearoff_handle))
6960 (void) SetWindowPos(menu->tearoff_handle,
6961 NULL,
6962 (int)trect.left,
6963 (int)trect.top,
6964 0, 0,
6965 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6966 }
6967}
6968#endif /* FEAT_TEAROFF */
6969
6970/*
6971 * Make a menu either grey or not grey.
6972 */
6973 void
6974gui_mch_menu_grey(
6975 vimmenu_T *menu,
6976 int grey)
6977{
6978#ifdef FEAT_TOOLBAR
6979 /*
6980 * is this a toolbar button?
6981 */
6982 if (menu->submenu_id == (HMENU)-1)
6983 {
6984 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6985 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6986 }
6987 else
6988#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006989 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6990 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992#ifdef FEAT_TEAROFF
6993 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6994 {
6995 WORD menuID;
6996 HWND menuHandle;
6997
6998 /*
6999 * A tearoff button has changed state.
7000 */
7001 if (menu->children == NULL)
7002 menuID = (WORD)(menu->id);
7003 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007004 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7006 if (menuHandle)
7007 EnableWindow(menuHandle, !grey);
7008
7009 }
7010#endif
7011}
7012
7013#endif /* FEAT_MENU */
7014
7015
7016/* define some macros used to make the dialogue creation more readable */
7017
7018#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
7019#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007020#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7023/*
7024 * stuff for dialogs
7025 */
7026
7027/*
7028 * The callback routine used by all the dialogs. Very simple. First,
7029 * acknowledges the INITDIALOG message so that Windows knows to do standard
7030 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7031 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7032 * number.
7033 */
7034 static LRESULT CALLBACK
7035dialog_callback(
7036 HWND hwnd,
7037 UINT message,
7038 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007039 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 if (message == WM_INITDIALOG)
7042 {
7043 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7044 /* Set focus to the dialog. Set the default button, if specified. */
7045 (void)SetFocus(hwnd);
7046 if (dialog_default_button > IDCANCEL)
7047 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007048 else
7049 /* We don't have a default, set focus on another element of the
7050 * dialog window, probably the icon */
7051 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 return FALSE;
7053 }
7054
7055 if (message == WM_COMMAND)
7056 {
7057 int button = LOWORD(wParam);
7058
7059 /* Don't end the dialog if something was selected that was
7060 * not a button.
7061 */
7062 if (button >= DLG_NONBUTTON_CONTROL)
7063 return TRUE;
7064
7065 /* If the edit box exists, copy the string. */
7066 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007067 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007068# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007069 /* If the OS is Windows NT, and 'encoding' differs from active
7070 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007071 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007072 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007073 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7074 char_u *p;
7075
7076 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7077 p = utf16_to_enc(wp, NULL);
7078 vim_strncpy(s_textfield, p, IOSIZE);
7079 vim_free(p);
7080 vim_free(wp);
7081 }
7082 else
7083# endif
7084 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007085 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087
7088 /*
7089 * Need to check for IDOK because if the user just hits Return to
7090 * accept the default value, some reason this is what we get.
7091 */
7092 if (button == IDOK)
7093 {
7094 if (dialog_default_button > IDCANCEL)
7095 EndDialog(hwnd, dialog_default_button);
7096 }
7097 else
7098 EndDialog(hwnd, button - IDCANCEL);
7099 return TRUE;
7100 }
7101
7102 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7103 {
7104 EndDialog(hwnd, 0);
7105 return TRUE;
7106 }
7107 return FALSE;
7108}
7109
7110/*
7111 * Create a dialog dynamically from the parameter strings.
7112 * type = type of dialog (question, alert, etc.)
7113 * title = dialog title. may be NULL for default title.
7114 * message = text to display. Dialog sizes to accommodate it.
7115 * buttons = '\n' separated list of button captions, default first.
7116 * dfltbutton = number of default button.
7117 *
7118 * This routine returns 1 if the first button is pressed,
7119 * 2 for the second, etc.
7120 *
7121 * 0 indicates Esc was pressed.
7122 * -1 for unexpected error
7123 *
7124 * If stubbing out this fn, return 1.
7125 */
7126
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007127static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 "IDR_VIM",
7130 "IDR_VIM_ERROR",
7131 "IDR_VIM_ALERT",
7132 "IDR_VIM_INFO",
7133 "IDR_VIM_QUESTION"
7134};
7135
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int
7137gui_mch_dialog(
7138 int type,
7139 char_u *title,
7140 char_u *message,
7141 char_u *buttons,
7142 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007143 char_u *textfield,
7144 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145{
7146 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007147 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 int numButtons;
7149 int *buttonWidths, *buttonPositions;
7150 int buttonYpos;
7151 int nchar, i;
7152 DWORD lStyle;
7153 int dlgwidth = 0;
7154 int dlgheight;
7155 int editboxheight;
7156 int horizWidth = 0;
7157 int msgheight;
7158 char_u *pstart;
7159 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007160 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 char_u *tbuffer;
7162 RECT rect;
7163 HWND hwnd;
7164 HDC hdc;
7165 HFONT font, oldFont;
7166 TEXTMETRIC fontInfo;
7167 int fontHeight;
7168 int textWidth, minButtonWidth, messageWidth;
7169 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007170 int maxDialogHeight;
7171 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 int vertical;
7173 int dlgPaddingX;
7174 int dlgPaddingY;
7175#ifdef USE_SYSMENU_FONT
7176 LOGFONT lfSysmenu;
7177 int use_lfSysmenu = FALSE;
7178#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007179 garray_T ga;
7180 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
7182#ifndef NO_CONSOLE
7183 /* Don't output anything in silent mode ("ex -s") */
7184 if (silent_mode)
7185 return dfltbutton; /* return default option */
7186#endif
7187
Bram Moolenaar748bf032005-02-02 23:04:36 +00007188 if (s_hwnd == NULL)
7189 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
7191 if ((type < 0) || (type > VIM_LAST_TYPE))
7192 type = 0;
7193
7194 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007195 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007196 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007197 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
7199 if (p == NULL)
7200 return -1;
7201
7202 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007203 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7205 * const.
7206 */
7207 tbuffer = vim_strsave(buttons);
7208 if (tbuffer == NULL)
7209 return -1;
7210
7211 --dfltbutton; /* Change from one-based to zero-based */
7212
7213 /* Count buttons */
7214 numButtons = 1;
7215 for (i = 0; tbuffer[i] != '\0'; i++)
7216 {
7217 if (tbuffer[i] == DLG_BUTTON_SEP)
7218 numButtons++;
7219 }
7220 if (dfltbutton >= numButtons)
7221 dfltbutton = -1;
7222
7223 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007224 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (buttonWidths == NULL)
7226 return -1;
7227
7228 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007229 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 if (buttonPositions == NULL)
7231 return -1;
7232
7233 /*
7234 * Calculate how big the dialog must be.
7235 */
7236 hwnd = GetDesktopWindow();
7237 hdc = GetWindowDC(hwnd);
7238#ifdef USE_SYSMENU_FONT
7239 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7240 {
7241 font = CreateFontIndirect(&lfSysmenu);
7242 use_lfSysmenu = TRUE;
7243 }
7244 else
7245#endif
7246 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7247 VARIABLE_PITCH , DLG_FONT_NAME);
7248 if (s_usenewlook)
7249 {
7250 oldFont = SelectFont(hdc, font);
7251 dlgPaddingX = DLG_PADDING_X;
7252 dlgPaddingY = DLG_PADDING_Y;
7253 }
7254 else
7255 {
7256 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7257 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7258 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7259 }
7260 GetTextMetrics(hdc, &fontInfo);
7261 fontHeight = fontInfo.tmHeight;
7262
7263 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007264 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265
7266 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007267 if (s_hwnd == NULL)
7268 {
7269 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaarc716c302006-01-21 22:12:51 +00007271 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007272 get_work_area(&workarea_rect);
7273 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7274 if (maxDialogWidth > 600)
7275 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007276 /* Leave some room for the taskbar. */
7277 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007278 }
7279 else
7280 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007281 /* Use our own window for the size, unless it's very small. */
7282 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007283 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007284 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007285 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007286 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7287 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007288
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007289 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007290 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007291 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007292 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007293 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7294 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7295 }
7296
7297 /* Set dlgwidth to width of message.
7298 * Copy the message into "ga", changing NL to CR-NL and inserting line
7299 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 pstart = message;
7301 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007302 msgheight = 0;
7303 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 do
7305 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007306 msgheight += fontHeight; /* at least one line */
7307
7308 /* Need to figure out where to break the string. The system does it
7309 * at a word boundary, which would mean we can't compute the number of
7310 * wrapped lines. */
7311 textWidth = 0;
7312 last_white = NULL;
7313 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007314 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007315#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007316 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007317#else
7318 l = 1;
7319#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007320 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007321 && textWidth > maxDialogWidth * 3 / 4)
7322 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007323 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007324 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007325 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007326 /* Line will wrap. */
7327 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007328 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007329 textWidth = 0;
7330
7331 if (last_white != NULL)
7332 {
7333 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007334 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007335 pend = last_white + 1;
7336 last_white = NULL;
7337 }
7338 ga_append(&ga, '\r');
7339 ga_append(&ga, '\n');
7340 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007341 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007342
7343 while (--l >= 0)
7344 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007345 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007346 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007348
7349 ga_append(&ga, '\r');
7350 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 pstart = pend + 1;
7352 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007353
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007354 if (ga.ga_data != NULL)
7355 message = ga.ga_data;
7356
Bram Moolenaar748bf032005-02-02 23:04:36 +00007357 messageWidth += 10; /* roundoff space */
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007360 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7361 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362
7363 if (msgheight < DLG_ICON_HEIGHT)
7364 msgheight = DLG_ICON_HEIGHT;
7365
7366 /*
7367 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007368 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007370 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (!vertical)
7372 {
7373 // Place buttons horizontally if they fit.
7374 horizWidth = dlgPaddingX;
7375 pstart = tbuffer;
7376 i = 0;
7377 do
7378 {
7379 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7380 if (pend == NULL)
7381 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007382 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 if (textWidth < minButtonWidth)
7384 textWidth = minButtonWidth;
7385 textWidth += dlgPaddingX; /* Padding within button */
7386 buttonWidths[i] = textWidth;
7387 buttonPositions[i++] = horizWidth;
7388 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7389 pstart = pend + 1;
7390 } while (*pend != NUL);
7391
7392 if (horizWidth > maxDialogWidth)
7393 vertical = TRUE; // Too wide to fit on the screen.
7394 else if (horizWidth > dlgwidth)
7395 dlgwidth = horizWidth;
7396 }
7397
7398 if (vertical)
7399 {
7400 // Stack buttons vertically.
7401 pstart = tbuffer;
7402 do
7403 {
7404 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7405 if (pend == NULL)
7406 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007407 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 textWidth += dlgPaddingX; /* Padding within button */
7409 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7410 if (textWidth > dlgwidth)
7411 dlgwidth = textWidth;
7412 pstart = pend + 1;
7413 } while (*pend != NUL);
7414 }
7415
7416 if (dlgwidth < DLG_MIN_WIDTH)
7417 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7418
7419 /* start to fill in the dlgtemplate information. addressing by WORDs */
7420 if (s_usenewlook)
7421 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7422 else
7423 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7424
7425 add_long(lStyle);
7426 add_long(0); // (lExtendedStyle)
7427 pnumitems = p; /*save where the number of items must be stored*/
7428 add_word(0); // NumberOfItems(will change later)
7429 add_word(10); // x
7430 add_word(10); // y
7431 add_word(PixelToDialogX(dlgwidth)); // cx
7432
7433 // Dialog height.
7434 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007435 dlgheight = msgheight + 2 * dlgPaddingY
7436 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 else
7438 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7439
7440 // Dialog needs to be taller if contains an edit box.
7441 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7442 if (textfield != NULL)
7443 dlgheight += editboxheight;
7444
Bram Moolenaara95d8232013-08-07 15:27:11 +02007445 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7446 if (dlgheight > maxDialogHeight)
7447 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007448 msgheight = msgheight - (dlgheight - maxDialogHeight);
7449 dlgheight = maxDialogHeight;
7450 scroll_flag = WS_VSCROLL;
7451 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7452 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007453 }
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 add_word(PixelToDialogY(dlgheight));
7456
7457 add_word(0); // Menu
7458 add_word(0); // Class
7459
7460 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007461 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7462 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 p += nchar;
7464
7465 if (s_usenewlook)
7466 {
7467 /* do the font, since DS_3DLOOK doesn't work properly */
7468#ifdef USE_SYSMENU_FONT
7469 if (use_lfSysmenu)
7470 {
7471 /* point size */
7472 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7473 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007474 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 else
7477#endif
7478 {
7479 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007480 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 p += nchar;
7483 }
7484
7485 buttonYpos = msgheight + 2 * dlgPaddingY;
7486
7487 if (textfield != NULL)
7488 buttonYpos += editboxheight;
7489
7490 pstart = tbuffer;
7491 if (!vertical)
7492 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7493 for (i = 0; i < numButtons; i++)
7494 {
7495 /* get end of this button. */
7496 for ( pend = pstart;
7497 *pend && (*pend != DLG_BUTTON_SEP);
7498 pend++)
7499 ;
7500
7501 if (*pend)
7502 *pend = '\0';
7503
7504 /*
7505 * old NOTE:
7506 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7507 * the focus to the first tab-able button and in so doing makes that
7508 * the default!! Grrr. Workaround: Make the default button the only
7509 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7510 * he/she can use arrow keys.
7511 *
7512 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007513 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 * dialog. Also needed for when the textfield is the default control.
7515 * It appears to work now (perhaps not on Win95?).
7516 */
7517 if (vertical)
7518 {
7519 p = add_dialog_element(p,
7520 (i == dfltbutton
7521 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7522 PixelToDialogX(DLG_VERT_PADDING_X),
7523 PixelToDialogY(buttonYpos /* TBK */
7524 + 2 * fontHeight * i),
7525 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7526 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007527 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 }
7529 else
7530 {
7531 p = add_dialog_element(p,
7532 (i == dfltbutton
7533 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7534 PixelToDialogX(horizWidth + buttonPositions[i]),
7535 PixelToDialogY(buttonYpos), /* TBK */
7536 PixelToDialogX(buttonWidths[i]),
7537 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007538 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 }
7540 pstart = pend + 1; /*next button*/
7541 }
7542 *pnumitems += numButtons;
7543
7544 /* Vim icon */
7545 p = add_dialog_element(p, SS_ICON,
7546 PixelToDialogX(dlgPaddingX),
7547 PixelToDialogY(dlgPaddingY),
7548 PixelToDialogX(DLG_ICON_WIDTH),
7549 PixelToDialogY(DLG_ICON_HEIGHT),
7550 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7551 dlg_icons[type]);
7552
Bram Moolenaar748bf032005-02-02 23:04:36 +00007553 /* Dialog message */
7554 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7555 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7556 PixelToDialogY(dlgPaddingY),
7557 (WORD)(PixelToDialogX(messageWidth) + 1),
7558 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007559 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560
7561 /* Edit box */
7562 if (textfield != NULL)
7563 {
7564 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7565 PixelToDialogX(2 * dlgPaddingX),
7566 PixelToDialogY(2 * dlgPaddingY + msgheight),
7567 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7568 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007569 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 *pnumitems += 1;
7571 }
7572
7573 *pnumitems += 2;
7574
7575 SelectFont(hdc, oldFont);
7576 DeleteObject(font);
7577 ReleaseDC(hwnd, hdc);
7578
7579 /* Let the dialog_callback() function know which button to make default
7580 * If we have an edit box, make that the default. We also need to tell
7581 * dialog_callback() if this dialog contains an edit box or not. We do
7582 * this by setting s_textfield if it does.
7583 */
7584 if (textfield != NULL)
7585 {
7586 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7587 s_textfield = textfield;
7588 }
7589 else
7590 {
7591 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7592 s_textfield = NULL;
7593 }
7594
7595 /* show the dialog box modally and get a return value */
7596 nchar = (int)DialogBoxIndirect(
7597 s_hinst,
7598 (LPDLGTEMPLATE)pdlgtemplate,
7599 s_hwnd,
7600 (DLGPROC)dialog_callback);
7601
7602 LocalFree(LocalHandle(pdlgtemplate));
7603 vim_free(tbuffer);
7604 vim_free(buttonWidths);
7605 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007606 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
7608 /* Focus back to our window (for when MDI is used). */
7609 (void)SetFocus(s_hwnd);
7610
7611 return nchar;
7612}
7613
7614#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007615
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616/*
7617 * Put a simple element (basic class) onto a dialog template in memory.
7618 * return a pointer to where the next item should be added.
7619 *
7620 * parameters:
7621 * lStyle = additional style flags
7622 * (be careful, NT3.51 & Win32s will ignore the new ones)
7623 * x,y = x & y positions IN DIALOG UNITS
7624 * w,h = width and height IN DIALOG UNITS
7625 * Id = ID used in messages
7626 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7627 * caption = usually text or resource name
7628 *
7629 * TODO: use the length information noted here to enable the dialog creation
7630 * routines to work out more exactly how much memory they need to alloc.
7631 */
7632 static PWORD
7633add_dialog_element(
7634 PWORD p,
7635 DWORD lStyle,
7636 WORD x,
7637 WORD y,
7638 WORD w,
7639 WORD h,
7640 WORD Id,
7641 WORD clss,
7642 const char *caption)
7643{
7644 int nchar;
7645
7646 p = lpwAlign(p); /* Align to dword boundary*/
7647 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7648 *p++ = LOWORD(lStyle);
7649 *p++ = HIWORD(lStyle);
7650 *p++ = 0; // LOWORD (lExtendedStyle)
7651 *p++ = 0; // HIWORD (lExtendedStyle)
7652 *p++ = x;
7653 *p++ = y;
7654 *p++ = w;
7655 *p++ = h;
7656 *p++ = Id; //9 or 10 words in all
7657
7658 *p++ = (WORD)0xffff;
7659 *p++ = clss; //2 more here
7660
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007661 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 p += nchar;
7663
7664 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7665
7666 return p; //total = 15+ (strlen(caption)) words
7667 // = 30 + 2(strlen(caption) bytes reqd
7668}
7669
7670
7671/*
7672 * Helper routine. Take an input pointer, return closest pointer that is
7673 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7674 */
7675 static LPWORD
7676lpwAlign(
7677 LPWORD lpIn)
7678{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007679 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007681 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 ul += 3;
7683 ul >>= 2;
7684 ul <<= 2;
7685 return (LPWORD)ul;
7686}
7687
7688/*
7689 * Helper routine. Takes second parameter as Ansi string, copies it to first
7690 * parameter as wide character (16-bits / char) string, and returns integer
7691 * number of wide characters (words) in string (including the trailing wide
7692 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007693 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7694 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 static int
7696nCopyAnsiToWideChar(
7697 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007698 LPSTR lpAnsiIn,
7699 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 int nChar = 0;
7702#ifdef FEAT_MBYTE
7703 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7704 int i;
7705 WCHAR *wn;
7706
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007707 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 {
7709 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007710 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 if (wn != NULL)
7712 {
7713 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007714 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 vim_free(wn);
7716 }
7717 }
7718 if (nChar == 0)
7719 /* Use Win32 conversion function. */
7720 nChar = MultiByteToWideChar(
7721 enc_codepage > 0 ? enc_codepage : CP_ACP,
7722 MB_PRECOMPOSED,
7723 lpAnsiIn, len,
7724 lpWCStr, len);
7725 for (i = 0; i < nChar; ++i)
7726 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7727 lpWCStr[i] = (WORD)' ';
7728#else
7729 do
7730 {
7731 if (*lpAnsiIn == '\t')
7732 *lpWCStr++ = (WORD)' ';
7733 else
7734 *lpWCStr++ = (WORD)*lpAnsiIn;
7735 nChar++;
7736 } while (*lpAnsiIn++);
7737#endif
7738
7739 return nChar;
7740}
7741
7742
7743#ifdef FEAT_TEAROFF
7744/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007745 * Lookup menu handle from "menu_id".
7746 */
7747 static HMENU
7748tearoff_lookup_menuhandle(
7749 vimmenu_T *menu,
7750 WORD menu_id)
7751{
7752 for ( ; menu != NULL; menu = menu->next)
7753 {
7754 if (menu->modes == 0) /* this menu has just been deleted */
7755 continue;
7756 if (menu_is_separator(menu->dname))
7757 continue;
7758 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7759 return menu->submenu_id;
7760 }
7761 return NULL;
7762}
7763
7764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 * The callback function for all the modeless dialogs that make up the
7766 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7767 * thinking its menus have been clicked), and go away when closed.
7768 */
7769 static LRESULT CALLBACK
7770tearoff_callback(
7771 HWND hwnd,
7772 UINT message,
7773 WPARAM wParam,
7774 LPARAM lParam)
7775{
7776 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007777 {
7778 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781
7782 /* May show the mouse pointer again. */
7783 HandleMouseHide(message, lParam);
7784
7785 if (message == WM_COMMAND)
7786 {
7787 if ((WORD)(LOWORD(wParam)) & 0x8000)
7788 {
7789 POINT mp;
7790 RECT rect;
7791
7792 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7793 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007794 vimmenu_T *menu;
7795
7796 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007798 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7800 (int)rect.right - 8,
7801 (int)mp.y,
7802 (int)0, /*reserved param*/
7803 s_hwnd,
7804 NULL);
7805 /*
7806 * NOTE: The pop-up menu can eat the mouse up event.
7807 * We deal with this in normal.c.
7808 */
7809 }
7810 }
7811 else
7812 /* Pass on messages to the main Vim window */
7813 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7814 /*
7815 * Give main window the focus back: this is so after
7816 * choosing a tearoff button you can start typing again
7817 * straight away.
7818 */
7819 (void)SetFocus(s_hwnd);
7820 return TRUE;
7821 }
7822 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7823 {
7824 DestroyWindow(hwnd);
7825 return TRUE;
7826 }
7827
7828 /* When moved around, give main window the focus back. */
7829 if (message == WM_EXITSIZEMOVE)
7830 (void)SetActiveWindow(s_hwnd);
7831
7832 return FALSE;
7833}
7834#endif
7835
7836
7837/*
7838 * Decide whether to use the "new look" (small, non-bold font) or the "old
7839 * look" (big, clanky font) for dialogs, and work out a few values for use
7840 * later accordingly.
7841 */
7842 static void
7843get_dialog_font_metrics(void)
7844{
7845 HDC hdc;
7846 HFONT hfontTools = 0;
7847 DWORD dlgFontSize;
7848 SIZE size;
7849#ifdef USE_SYSMENU_FONT
7850 LOGFONT lfSysmenu;
7851#endif
7852
7853 s_usenewlook = FALSE;
7854
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007856 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7857 hfontTools = CreateFontIndirect(&lfSysmenu);
7858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859#endif
7860 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7861 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7862
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007863 if (hfontTools)
7864 {
7865 hdc = GetDC(s_hwnd);
7866 SelectObject(hdc, hfontTools);
7867 /*
7868 * GetTextMetrics() doesn't return the right value in
7869 * tmAveCharWidth, so we have to figure out the dialog base units
7870 * ourselves.
7871 */
7872 GetTextExtentPoint(hdc,
7873 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7874 52, &size);
7875 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007877 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7878 s_dlgfntheight = (WORD)size.cy;
7879 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881
7882 if (!s_usenewlook)
7883 {
7884 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7885 s_dlgfntwidth = LOWORD(dlgFontSize);
7886 s_dlgfntheight = HIWORD(dlgFontSize);
7887 }
7888}
7889
7890#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7891/*
7892 * Create a pseudo-"tearoff menu" based on the child
7893 * items of a given menu pointer.
7894 */
7895 static void
7896gui_mch_tearoff(
7897 char_u *title,
7898 vimmenu_T *menu,
7899 int initX,
7900 int initY)
7901{
7902 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7903 int template_len;
7904 int nchar, textWidth, submenuWidth;
7905 DWORD lStyle;
7906 DWORD lExtendedStyle;
7907 WORD dlgwidth;
7908 WORD menuID;
7909 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007910 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 vimmenu_T *the_menu = menu;
7912 HWND hwnd;
7913 HDC hdc;
7914 HFONT font, oldFont;
7915 int col, spaceWidth, len;
7916 int columnWidths[2];
7917 char_u *label, *text;
7918 int acLen = 0;
7919 int nameLen;
7920 int padding0, padding1, padding2 = 0;
7921 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007922 int x;
7923 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924#ifdef USE_SYSMENU_FONT
7925 LOGFONT lfSysmenu;
7926 int use_lfSysmenu = FALSE;
7927#endif
7928
7929 /*
7930 * If this menu is already torn off, move it to the mouse position.
7931 */
7932 if (IsWindow(menu->tearoff_handle))
7933 {
7934 POINT mp;
7935 if (GetCursorPos((LPPOINT)&mp))
7936 {
7937 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7938 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7939 }
7940 return;
7941 }
7942
7943 /*
7944 * Create a new tearoff.
7945 */
7946 if (*title == MNU_HIDDEN_CHAR)
7947 title++;
7948
7949 /* Allocate memory to store the dialog template. It's made bigger when
7950 * needed. */
7951 template_len = DLG_ALLOC_SIZE;
7952 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7953 if (p == NULL)
7954 return;
7955
7956 hwnd = GetDesktopWindow();
7957 hdc = GetWindowDC(hwnd);
7958#ifdef USE_SYSMENU_FONT
7959 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7960 {
7961 font = CreateFontIndirect(&lfSysmenu);
7962 use_lfSysmenu = TRUE;
7963 }
7964 else
7965#endif
7966 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7967 VARIABLE_PITCH , DLG_FONT_NAME);
7968 if (s_usenewlook)
7969 oldFont = SelectFont(hdc, font);
7970 else
7971 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7972
7973 /* Calculate width of a single space. Used for padding columns to the
7974 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007975 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976
7977 /* Figure out max width of the text column, the accelerator column and the
7978 * optional submenu column. */
7979 submenuWidth = 0;
7980 for (col = 0; col < 2; col++)
7981 {
7982 columnWidths[col] = 0;
7983 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7984 {
7985 /* Use "dname" here to compute the width of the visible text. */
7986 text = (col == 0) ? pmenu->dname : pmenu->actext;
7987 if (text != NULL && *text != NUL)
7988 {
7989 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7990 if (textWidth > columnWidths[col])
7991 columnWidths[col] = textWidth;
7992 }
7993 if (pmenu->children != NULL)
7994 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7995 }
7996 }
7997 if (columnWidths[1] == 0)
7998 {
7999 /* no accelerators */
8000 if (submenuWidth != 0)
8001 columnWidths[0] += submenuWidth;
8002 else
8003 columnWidths[0] += spaceWidth;
8004 }
8005 else
8006 {
8007 /* there is an accelerator column */
8008 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8009 columnWidths[1] += submenuWidth;
8010 }
8011
8012 /*
8013 * Now find the total width of our 'menu'.
8014 */
8015 textWidth = columnWidths[0] + columnWidths[1];
8016 if (submenuWidth != 0)
8017 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008018 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8020 textWidth += submenuWidth;
8021 }
8022 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8023 if (textWidth > dlgwidth)
8024 dlgwidth = textWidth;
8025 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 /* start to fill in the dlgtemplate information. addressing by WORDs */
8028 if (s_usenewlook)
8029 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8030 else
8031 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8032
8033 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8034 *p++ = LOWORD(lStyle);
8035 *p++ = HIWORD(lStyle);
8036 *p++ = LOWORD(lExtendedStyle);
8037 *p++ = HIWORD(lExtendedStyle);
8038 pnumitems = p; /* save where the number of items must be stored */
8039 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008040 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008042 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 else
8044 *p++ = PixelToDialogX(initX); // x
8045 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008046 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 else
8048 *p++ = PixelToDialogY(initY); // y
8049 *p++ = PixelToDialogX(dlgwidth); // cx
8050 ptrueheight = p;
8051 *p++ = 0; // dialog height: changed later anyway
8052 *p++ = 0; // Menu
8053 *p++ = 0; // Class
8054
8055 /* copy the title of the dialog */
8056 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008057 ? (LPSTR)title
8058 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 p += nchar;
8060
8061 if (s_usenewlook)
8062 {
8063 /* do the font, since DS_3DLOOK doesn't work properly */
8064#ifdef USE_SYSMENU_FONT
8065 if (use_lfSysmenu)
8066 {
8067 /* point size */
8068 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8069 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008070 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 }
8072 else
8073#endif
8074 {
8075 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008076 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078 p += nchar;
8079 }
8080
8081 /*
8082 * Loop over all the items in the menu.
8083 * But skip over the tearbar.
8084 */
8085 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8086 menu = menu->children->next;
8087 else
8088 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008089 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 for ( ; menu != NULL; menu = menu->next)
8091 {
8092 if (menu->modes == 0) /* this menu has just been deleted */
8093 continue;
8094 if (menu_is_separator(menu->dname))
8095 {
8096 sepPadding += 3;
8097 continue;
8098 }
8099
8100 /* Check if there still is plenty of room in the template. Make it
8101 * larger when needed. */
8102 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8103 {
8104 WORD *newp;
8105
8106 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8107 if (newp != NULL)
8108 {
8109 template_len += 4096;
8110 mch_memmove(newp, pdlgtemplate,
8111 (char *)p - (char *)pdlgtemplate);
8112 p = newp + (p - pdlgtemplate);
8113 pnumitems = newp + (pnumitems - pdlgtemplate);
8114 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8115 LocalFree(LocalHandle(pdlgtemplate));
8116 pdlgtemplate = newp;
8117 }
8118 }
8119
8120 /* Figure out minimal length of this menu label. Use "name" for the
8121 * actual text, "dname" for estimating the displayed size. "name"
8122 * has "&a" for mnemonic and includes the accelerator. */
8123 len = nameLen = (int)STRLEN(menu->name);
8124 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8125 (int)STRLEN(menu->dname))) / spaceWidth;
8126 len += padding0;
8127
8128 if (menu->actext != NULL)
8129 {
8130 acLen = (int)STRLEN(menu->actext);
8131 len += acLen;
8132 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8133 }
8134 else
8135 textWidth = 0;
8136 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8137 len += padding1;
8138
8139 if (menu->children == NULL)
8140 {
8141 padding2 = submenuWidth / spaceWidth;
8142 len += padding2;
8143 menuID = (WORD)(menu->id);
8144 }
8145 else
8146 {
8147 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008148 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 }
8150
8151 /* Allocate menu label and fill it in */
8152 text = label = alloc((unsigned)len + 1);
8153 if (label == NULL)
8154 break;
8155
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008156 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8158 if (text == NULL)
8159 text = label + nameLen; /* no actext, use whole name */
8160 while (padding0-- > 0)
8161 *text++ = ' ';
8162 if (menu->actext != NULL)
8163 {
8164 STRNCPY(text, menu->actext, acLen);
8165 text += acLen;
8166 }
8167 while (padding1-- > 0)
8168 *text++ = ' ';
8169 if (menu->children != NULL)
8170 {
8171 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8172 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8173 }
8174 else
8175 {
8176 while (padding2-- > 0)
8177 *text++ = ' ';
8178 }
8179 *text = NUL;
8180
8181 /*
8182 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8183 * W95/NT4 it makes the tear-off look more like a menu.
8184 */
8185 p = add_dialog_element(p,
8186 BS_PUSHBUTTON|BS_LEFT,
8187 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8188 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8189 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8190 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008191 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 vim_free(label);
8193 (*pnumitems)++;
8194 }
8195
8196 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8197
8198
8199 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008200 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 s_hinst,
8202 (LPDLGTEMPLATE)pdlgtemplate,
8203 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008204 (DLGPROC)tearoff_callback,
8205 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206
8207 LocalFree(LocalHandle(pdlgtemplate));
8208 SelectFont(hdc, oldFont);
8209 DeleteObject(font);
8210 ReleaseDC(hwnd, hdc);
8211
8212 /*
8213 * Reassert ourselves as the active window. This is so that after creating
8214 * a tearoff, the user doesn't have to click with the mouse just to start
8215 * typing again!
8216 */
8217 (void)SetActiveWindow(s_hwnd);
8218
8219 /* make sure the right buttons are enabled */
8220 force_menu_update = TRUE;
8221}
8222#endif
8223
8224#if defined(FEAT_TOOLBAR) || defined(PROTO)
8225#include "gui_w32_rc.h"
8226
8227/* This not defined in older SDKs */
8228# ifndef TBSTYLE_FLAT
8229# define TBSTYLE_FLAT 0x0800
8230# endif
8231
8232/*
8233 * Create the toolbar, initially unpopulated.
8234 * (just like the menu, there are no defaults, it's all
8235 * set up through menu.vim)
8236 */
8237 static void
8238initialise_toolbar(void)
8239{
8240 InitCommonControls();
8241 s_toolbarhwnd = CreateToolbarEx(
8242 s_hwnd,
8243 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8244 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008245 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 s_hinst,
8247 IDR_TOOLBAR1, // id of initial bitmap
8248 NULL,
8249 0, // initial number of buttons
8250 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8251 TOOLBAR_BUTTON_HEIGHT,
8252 TOOLBAR_BUTTON_WIDTH,
8253 TOOLBAR_BUTTON_HEIGHT,
8254 sizeof(TBBUTTON)
8255 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008256 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
8258 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8259}
8260
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008261 static LRESULT CALLBACK
8262toolbar_wndproc(
8263 HWND hwnd,
8264 UINT uMsg,
8265 WPARAM wParam,
8266 LPARAM lParam)
8267{
8268 HandleMouseHide(uMsg, lParam);
8269 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8270}
8271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 static int
8273get_toolbar_bitmap(vimmenu_T *menu)
8274{
8275 int i = -1;
8276
8277 /*
8278 * Check user bitmaps first, unless builtin is specified.
8279 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008280 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {
8282 char_u fname[MAXPATHL];
8283 HANDLE hbitmap = NULL;
8284
8285 if (menu->iconfile != NULL)
8286 {
8287 gui_find_iconfile(menu->iconfile, fname, "bmp");
8288 hbitmap = LoadImage(
8289 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008290 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 IMAGE_BITMAP,
8292 TOOLBAR_BUTTON_WIDTH,
8293 TOOLBAR_BUTTON_HEIGHT,
8294 LR_LOADFROMFILE |
8295 LR_LOADMAP3DCOLORS
8296 );
8297 }
8298
8299 /*
8300 * If the LoadImage call failed, or the "icon=" file
8301 * didn't exist or wasn't specified, try the menu name
8302 */
8303 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008304 && (gui_find_bitmap(
8305#ifdef FEAT_MULTI_LANG
8306 menu->en_dname != NULL ? menu->en_dname :
8307#endif
8308 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 hbitmap = LoadImage(
8310 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008311 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 IMAGE_BITMAP,
8313 TOOLBAR_BUTTON_WIDTH,
8314 TOOLBAR_BUTTON_HEIGHT,
8315 LR_LOADFROMFILE |
8316 LR_LOADMAP3DCOLORS
8317 );
8318
8319 if (hbitmap != NULL)
8320 {
8321 TBADDBITMAP tbAddBitmap;
8322
8323 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008324 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8327 (WPARAM)1, (LPARAM)&tbAddBitmap);
8328 /* i will be set to -1 if it fails */
8329 }
8330 }
8331 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8332 i = menu->iconidx;
8333
8334 return i;
8335}
8336#endif
8337
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008338#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8339 static void
8340initialise_tabline(void)
8341{
8342 InitCommonControls();
8343
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008344 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008345 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008346 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8347 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008348 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008349
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008350 gui.tabline_height = TABLINE_HEIGHT;
8351
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008352# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008353 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008354# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008355}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008356
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008357/*
8358 * Get tabpage_T from POINT.
8359 */
8360 static tabpage_T *
8361GetTabFromPoint(
8362 HWND hWnd,
8363 POINT pt)
8364{
8365 tabpage_T *ptp = NULL;
8366
8367 if (gui_mch_showing_tabline())
8368 {
8369 TCHITTESTINFO htinfo;
8370 htinfo.pt = pt;
8371 /* ignore if a window under cusor is not tabcontrol. */
8372 if (s_tabhwnd == hWnd)
8373 {
8374 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8375 if (idx != -1)
8376 ptp = find_tabpage(idx + 1);
8377 }
8378 }
8379 return ptp;
8380}
8381
8382static POINT s_pt = {0, 0};
8383static HCURSOR s_hCursor = NULL;
8384
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008385 static LRESULT CALLBACK
8386tabline_wndproc(
8387 HWND hwnd,
8388 UINT uMsg,
8389 WPARAM wParam,
8390 LPARAM lParam)
8391{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008392 POINT pt;
8393 tabpage_T *tp;
8394 RECT rect;
8395 int nCenter;
8396 int idx0;
8397 int idx1;
8398
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008399 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008400
8401 switch (uMsg)
8402 {
8403 case WM_LBUTTONDOWN:
8404 {
8405 s_pt.x = GET_X_LPARAM(lParam);
8406 s_pt.y = GET_Y_LPARAM(lParam);
8407 SetCapture(hwnd);
8408 s_hCursor = GetCursor(); /* backup default cursor */
8409 break;
8410 }
8411 case WM_MOUSEMOVE:
8412 if (GetCapture() == hwnd
8413 && ((wParam & MK_LBUTTON)) != 0)
8414 {
8415 pt.x = GET_X_LPARAM(lParam);
8416 pt.y = s_pt.y;
8417 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8418 {
8419 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8420
8421 tp = GetTabFromPoint(hwnd, pt);
8422 if (tp != NULL)
8423 {
8424 idx0 = tabpage_index(curtab) - 1;
8425 idx1 = tabpage_index(tp) - 1;
8426
8427 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8428 nCenter = rect.left + (rect.right - rect.left) / 2;
8429
8430 /* Check if the mouse cursor goes over the center of
8431 * the next tab to prevent "flickering". */
8432 if ((idx0 < idx1) && (nCenter < pt.x))
8433 {
8434 tabpage_move(idx1 + 1);
8435 update_screen(0);
8436 }
8437 else if ((idx1 < idx0) && (pt.x < nCenter))
8438 {
8439 tabpage_move(idx1);
8440 update_screen(0);
8441 }
8442 }
8443 }
8444 }
8445 break;
8446 case WM_LBUTTONUP:
8447 {
8448 if (GetCapture() == hwnd)
8449 {
8450 SetCursor(s_hCursor);
8451 ReleaseCapture();
8452 }
8453 break;
8454 }
8455 default:
8456 break;
8457 }
8458
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008459 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8460}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008461#endif
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8464/*
8465 * Make the GUI window come to the foreground.
8466 */
8467 void
8468gui_mch_set_foreground(void)
8469{
8470 if (IsIconic(s_hwnd))
8471 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8472 SetForegroundWindow(s_hwnd);
8473}
8474#endif
8475
8476#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8477 static void
8478dyn_imm_load(void)
8479{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008480 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (hLibImm == NULL)
8482 return;
8483
8484 pImmGetCompositionStringA
8485 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8486 pImmGetCompositionStringW
8487 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8488 pImmGetContext
8489 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8490 pImmAssociateContext
8491 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8492 pImmReleaseContext
8493 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8494 pImmGetOpenStatus
8495 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8496 pImmSetOpenStatus
8497 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8498 pImmGetCompositionFont
8499 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8500 pImmSetCompositionFont
8501 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8502 pImmSetCompositionWindow
8503 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8504 pImmGetConversionStatus
8505 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008506 pImmSetConversionStatus
8507 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
8509 if ( pImmGetCompositionStringA == NULL
8510 || pImmGetCompositionStringW == NULL
8511 || pImmGetContext == NULL
8512 || pImmAssociateContext == NULL
8513 || pImmReleaseContext == NULL
8514 || pImmGetOpenStatus == NULL
8515 || pImmSetOpenStatus == NULL
8516 || pImmGetCompositionFont == NULL
8517 || pImmSetCompositionFont == NULL
8518 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008519 || pImmGetConversionStatus == NULL
8520 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 {
8522 FreeLibrary(hLibImm);
8523 hLibImm = NULL;
8524 pImmGetContext = NULL;
8525 return;
8526 }
8527
8528 return;
8529}
8530
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531#endif
8532
8533#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8534
8535# ifdef FEAT_XPM_W32
8536# define IMAGE_XPM 100
8537# endif
8538
8539typedef struct _signicon_t
8540{
8541 HANDLE hImage;
8542 UINT uType;
8543#ifdef FEAT_XPM_W32
8544 HANDLE hShape; /* Mask bitmap handle */
8545#endif
8546} signicon_t;
8547
8548 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008549gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 signicon_t *sign;
8552 int x, y, w, h;
8553
8554 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8555 return;
8556
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008557#if defined(FEAT_DIRECTX)
8558 if (IS_ENABLE_DIRECTX())
8559 DWriteContext_Flush(s_dwc);
8560#endif
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 x = TEXT_X(col);
8563 y = TEXT_Y(row);
8564 w = gui.char_width * 2;
8565 h = gui.char_height;
8566 switch (sign->uType)
8567 {
8568 case IMAGE_BITMAP:
8569 {
8570 HDC hdcMem;
8571 HBITMAP hbmpOld;
8572
8573 hdcMem = CreateCompatibleDC(s_hdc);
8574 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8575 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8576 SelectObject(hdcMem, hbmpOld);
8577 DeleteDC(hdcMem);
8578 }
8579 break;
8580 case IMAGE_ICON:
8581 case IMAGE_CURSOR:
8582 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8583 break;
8584#ifdef FEAT_XPM_W32
8585 case IMAGE_XPM:
8586 {
8587 HDC hdcMem;
8588 HBITMAP hbmpOld;
8589
8590 hdcMem = CreateCompatibleDC(s_hdc);
8591 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8592 /* Make hole */
8593 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8594
8595 SelectObject(hdcMem, sign->hImage);
8596 /* Paint sign */
8597 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8598 SelectObject(hdcMem, hbmpOld);
8599 DeleteDC(hdcMem);
8600 }
8601 break;
8602#endif
8603 }
8604}
8605
8606 static void
8607close_signicon_image(signicon_t *sign)
8608{
8609 if (sign)
8610 switch (sign->uType)
8611 {
8612 case IMAGE_BITMAP:
8613 DeleteObject((HGDIOBJ)sign->hImage);
8614 break;
8615 case IMAGE_CURSOR:
8616 DestroyCursor((HCURSOR)sign->hImage);
8617 break;
8618 case IMAGE_ICON:
8619 DestroyIcon((HICON)sign->hImage);
8620 break;
8621#ifdef FEAT_XPM_W32
8622 case IMAGE_XPM:
8623 DeleteObject((HBITMAP)sign->hImage);
8624 DeleteObject((HBITMAP)sign->hShape);
8625 break;
8626#endif
8627 }
8628}
8629
8630 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008631gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 signicon_t sign, *psign;
8634 char_u *ext;
8635
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008637 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 if (ext > signfile)
8639 {
8640 int do_load = 1;
8641
8642 if (!STRICMP(ext, ".bmp"))
8643 sign.uType = IMAGE_BITMAP;
8644 else if (!STRICMP(ext, ".ico"))
8645 sign.uType = IMAGE_ICON;
8646 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8647 sign.uType = IMAGE_CURSOR;
8648 else
8649 do_load = 0;
8650
8651 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008652 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 gui.char_width * 2, gui.char_height,
8654 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8655#ifdef FEAT_XPM_W32
8656 if (!STRICMP(ext, ".xpm"))
8657 {
8658 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008659 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8660 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662#endif
8663 }
8664
8665 psign = NULL;
8666 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8667 != NULL)
8668 *psign = sign;
8669
8670 if (!psign)
8671 {
8672 if (sign.hImage)
8673 close_signicon_image(&sign);
8674 EMSG(_(e_signdata));
8675 }
8676 return (void *)psign;
8677
8678}
8679
8680 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008681gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682{
8683 if (sign)
8684 {
8685 close_signicon_image((signicon_t *)sign);
8686 vim_free(sign);
8687 }
8688}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008691#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692
8693/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008694 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008696 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8698 * to get current mouse position).
8699 *
8700 * Trying to use as more Windows services as possible, and as less
8701 * IE version as possible :)).
8702 *
8703 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8704 * BalloonEval struct.
8705 * 2) Enable/Disable simply create/kill BalloonEval Timer
8706 * 3) When there was enough inactivity, timer procedure posts
8707 * async request to debugger
8708 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8709 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008710 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 */
8712
Bram Moolenaar45360022005-07-21 21:08:21 +00008713/*
8714 * determine whether installed Common Controls support multiline tooltips
8715 * (i.e. their version is >= 4.70
8716 */
8717 int
8718multiline_balloon_available(void)
8719{
8720 HINSTANCE hDll;
8721 static char comctl_dll[] = "comctl32.dll";
8722 static int multiline_tip = MAYBE;
8723
8724 if (multiline_tip != MAYBE)
8725 return multiline_tip;
8726
8727 hDll = GetModuleHandle(comctl_dll);
8728 if (hDll != NULL)
8729 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008730 DLLGETVERSIONPROC pGetVer;
8731 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008732
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008733 if (pGetVer != NULL)
8734 {
8735 DLLVERSIONINFO dvi;
8736 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008737
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008738 ZeroMemory(&dvi, sizeof(dvi));
8739 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008740
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008741 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008742
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008743 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008744 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008745 || (dvi.dwMajorVersion == 4
8746 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008747 {
8748 multiline_tip = TRUE;
8749 return multiline_tip;
8750 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008751 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008752 else
8753 {
8754 /* there is chance we have ancient CommCtl 4.70
8755 which doesn't export DllGetVersion */
8756 DWORD dwHandle = 0;
8757 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8758 if (len > 0)
8759 {
8760 VS_FIXEDFILEINFO *ver;
8761 UINT vlen = 0;
8762 void *data = alloc(len);
8763
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008764 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008765 && GetFileVersionInfo(comctl_dll, 0, len, data)
8766 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8767 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008768 && HIWORD(ver->dwFileVersionMS) > 4)
8769 || ((HIWORD(ver->dwFileVersionMS) == 4
8770 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008771 {
8772 vim_free(data);
8773 multiline_tip = TRUE;
8774 return multiline_tip;
8775 }
8776 vim_free(data);
8777 }
8778 }
8779 }
8780 multiline_tip = FALSE;
8781 return multiline_tip;
8782}
8783
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008784#ifdef FEAT_MBYTE
8785 static void
8786make_tooltipw(BalloonEval *beval, char *text, POINT pt)
8787{
8788 TOOLINFOW *pti;
8789 int ToolInfoSize;
8790 WCHAR *tofree = NULL;
8791
8792 if (multiline_balloon_available() == TRUE)
8793 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8794 else
8795 ToolInfoSize = sizeof(TOOLINFOW);
8796
8797 pti = (TOOLINFOW *)alloc(ToolInfoSize);
8798 if (pti == NULL)
8799 return;
8800
8801 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8802 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8803 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8804 beval->target, NULL, s_hinst, NULL);
8805
8806 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8807 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8808
8809 pti->cbSize = ToolInfoSize;
8810 pti->uFlags = TTF_SUBCLASS;
8811 pti->hwnd = beval->target;
8812 pti->hinst = 0; // Don't use string resources
8813 pti->uId = ID_BEVAL_TOOLTIP;
8814
8815 if (multiline_balloon_available() == TRUE)
8816 {
8817 RECT rect;
8818 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8819 pti->lpszText = LPSTR_TEXTCALLBACKW;
8820 tofree = enc_to_utf16((char_u*)text, NULL);
8821 ptin->lParam = (LPARAM)tofree;
8822 // switch multiline tooltips on
8823 if (GetClientRect(s_textArea, &rect))
8824 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8825 (LPARAM)rect.right);
8826 }
8827 else
8828 {
8829 // do this old way
8830 tofree = enc_to_utf16((char_u*)text, NULL);
8831 pti->lpszText = tofree;
8832 }
8833
8834 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8835 pti->rect.left = pt.x - 3;
8836 pti->rect.top = pt.y - 3;
8837 pti->rect.right = pt.x + 3;
8838 pti->rect.bottom = pt.y + 3;
8839
8840 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8841 // Make tooltip appear sooner.
8842 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8843 // I've performed some tests and it seems the longest possible life time
8844 // of tooltip is 30 seconds.
8845 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8846 /*
8847 * HACK: force tooltip to appear, because it'll not appear until
8848 * first mouse move. D*mn M$
8849 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8850 */
8851 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8852 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8853 vim_free(pti);
8854 if (tofree != NULL)
8855 vim_free(tofree);
8856}
8857#endif
8858
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008860make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaar45360022005-07-21 21:08:21 +00008862 TOOLINFO *pti;
8863 int ToolInfoSize;
8864
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008865#ifdef FEAT_MBYTE
8866 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
8867 {
8868 make_tooltipw(beval, text, pt);
8869 return;
8870 }
8871#endif
8872
Bram Moolenaar45360022005-07-21 21:08:21 +00008873 if (multiline_balloon_available() == TRUE)
8874 ToolInfoSize = sizeof(TOOLINFO_NEW);
8875 else
8876 ToolInfoSize = sizeof(TOOLINFO);
8877
8878 pti = (TOOLINFO *)alloc(ToolInfoSize);
8879 if (pti == NULL)
8880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8883 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8884 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8885 beval->target, NULL, s_hinst, NULL);
8886
8887 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8888 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8889
Bram Moolenaar45360022005-07-21 21:08:21 +00008890 pti->cbSize = ToolInfoSize;
8891 pti->uFlags = TTF_SUBCLASS;
8892 pti->hwnd = beval->target;
8893 pti->hinst = 0; /* Don't use string resources */
8894 pti->uId = ID_BEVAL_TOOLTIP;
8895
8896 if (multiline_balloon_available() == TRUE)
8897 {
8898 RECT rect;
8899 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8900 pti->lpszText = LPSTR_TEXTCALLBACK;
8901 ptin->lParam = (LPARAM)text;
8902 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8903 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8904 (LPARAM)rect.right);
8905 }
8906 else
8907 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008910 pti->rect.left = pt.x - 3;
8911 pti->rect.top = pt.y - 3;
8912 pti->rect.right = pt.x + 3;
8913 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914
Bram Moolenaar45360022005-07-21 21:08:21 +00008915 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 /* Make tooltip appear sooner */
8917 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008918 /* I've performed some tests and it seems the longest possible life time
8919 * of tooltip is 30 seconds */
8920 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 /*
8922 * HACK: force tooltip to appear, because it'll not appear until
8923 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008924 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008926 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008928 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
8931 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008932delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008934 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935}
8936
8937 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008938BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008939 HWND hwnd UNUSED,
8940 UINT uMsg UNUSED,
8941 UINT_PTR idEvent UNUSED,
8942 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
8944 POINT pt;
8945 RECT rect;
8946
8947 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8948 return;
8949
8950 GetCursorPos(&pt);
8951 if (WindowFromPoint(pt) != s_textArea)
8952 return;
8953
8954 ScreenToClient(s_textArea, &pt);
8955 GetClientRect(s_textArea, &rect);
8956 if (!PtInRect(&rect, pt))
8957 return;
8958
8959 if (LastActivity > 0
8960 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8961 && (cur_beval->showState != ShS_PENDING
8962 || abs(cur_beval->x - pt.x) > 3
8963 || abs(cur_beval->y - pt.y) > 3))
8964 {
8965 /* Pointer resting in one place long enough, it's time to show
8966 * the tooltip. */
8967 cur_beval->showState = ShS_PENDING;
8968 cur_beval->x = pt.x;
8969 cur_beval->y = pt.y;
8970
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008971 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
8973 if (cur_beval->msgCB != NULL)
8974 (*cur_beval->msgCB)(cur_beval, 0);
8975 }
8976}
8977
8978 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008979gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008981 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008983 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984}
8985
8986 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008987gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008989 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (beval == NULL)
8991 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008992 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008993 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008994 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008998gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999{
9000 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01009001
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009002 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 if (beval->showState == ShS_SHOWING)
9004 return;
9005 GetCursorPos(&pt);
9006 ScreenToClient(s_textArea, &pt);
9007
9008 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01009010 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 gui_mch_disable_beval_area(cur_beval);
9012 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01009013 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009015 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016}
9017
9018 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009019gui_mch_create_beval_area(
9020 void *target, /* ignored, always use s_textArea */
9021 char_u *mesg,
9022 void (*mesgCB)(BalloonEval *, int),
9023 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
9025 /* partially stolen from gui_beval.c */
9026 BalloonEval *beval;
9027
9028 if (mesg != NULL && mesgCB != NULL)
9029 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01009030 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 return NULL;
9032 }
9033
Bram Moolenaarca4b6132018-06-28 12:05:11 +02009034 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 if (beval != NULL)
9036 {
9037 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038
9039 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 beval->msg = mesg;
9041 beval->msgCB = mesgCB;
9042 beval->clientData = clientData;
9043
9044 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 cur_beval = beval;
9046
9047 if (p_beval)
9048 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 }
9050 return beval;
9051}
9052
9053 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01009054Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055{
9056 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
9057 return;
9058
9059 if (cur_beval != NULL)
9060 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009061 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 {
Bram Moolenaar45360022005-07-21 21:08:21 +00009063 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009064 // TRACE0("TTN_SHOW {{{");
9065 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00009066 break;
9067 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009068 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 delete_tooltip(cur_beval);
9070 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00009071 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00009074 break;
9075 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00009076 {
9077 /* if you get there then we have new common controls */
9078 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
9079 info->lpszText = (LPSTR)info->lParam;
9080 info->uFlags |= TTF_DI_SETITEM;
9081 }
Bram Moolenaar45360022005-07-21 21:08:21 +00009082 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01009083#ifdef FEAT_MBYTE
9084 case TTN_GETDISPINFOW:
9085 {
9086 // if we get here then we have new common controls
9087 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
9088 info->lpszText = (LPWSTR)info->lParam;
9089 info->uFlags |= TTF_DI_SETITEM;
9090 }
9091 break;
9092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 }
9094 }
9095}
9096
9097 static void
9098TrackUserActivity(UINT uMsg)
9099{
9100 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9101 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
9102 LastActivity = GetTickCount();
9103}
9104
9105 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009106gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02009108#ifdef FEAT_VARTABS
9109 if (beval->vts)
9110 vim_free(beval->vts);
9111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 vim_free(beval);
9113}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01009114#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
9116#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9117/*
9118 * We have multiple signs to draw at the same location. Draw the
9119 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9120 */
9121 void
9122netbeans_draw_multisign_indicator(int row)
9123{
9124 int i;
9125 int y;
9126 int x;
9127
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009128 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009129 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009130
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 x = 0;
9132 y = TEXT_Y(row);
9133
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009134#if defined(FEAT_DIRECTX)
9135 if (IS_ENABLE_DIRECTX())
9136 DWriteContext_Flush(s_dwc);
9137#endif
9138
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 for (i = 0; i < gui.char_height - 3; i++)
9140 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9141
9142 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9143 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9144 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9145 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9146 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9147 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9148 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9149}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009150#endif