blob: 6596cf81e935662c2071e372cef59d8b3d7d090e [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 Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
299static WORD s_dlgfntheight; /* height of the dialog font */
300static WORD s_dlgfntwidth; /* width of the dialog font */
301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
307static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
308#endif
309
310/* Flag that is set while processing a message that must not be interrupted by
311 * processing another message. */
312static int s_busy_processing = FALSE;
313
314static int destroying = FALSE; /* call DestroyWindow() ourselves */
315
316#ifdef MSWIN_FIND_REPLACE
317static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
318static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200319# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100320static FINDREPLACEW s_findrep_struct_w;
321# endif
322static HWND s_findrep_hwnd = NULL;
323static int s_findrep_is_find; /* TRUE for find dialog, FALSE
324 for find/replace dialog */
325#endif
326
327static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100328#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329static
330#endif
331HWND s_hwnd = NULL;
332static HDC s_hdc = NULL;
333static HBRUSH s_brush = NULL;
334
335#ifdef FEAT_TOOLBAR
336static HWND s_toolbarhwnd = NULL;
337static WNDPROC s_toolbar_wndproc = NULL;
338#endif
339
340#ifdef FEAT_GUI_TABLINE
341static HWND s_tabhwnd = NULL;
342static WNDPROC s_tabline_wndproc = NULL;
343static int showing_tabline = 0;
344#endif
345
346static WPARAM s_wParam = 0;
347static LPARAM s_lParam = 0;
348
349static HWND s_textArea = NULL;
350static UINT s_uMsg = 0;
351
352static char_u *s_textfield; /* Used by dialogs to pass back strings */
353
354static int s_need_activate = FALSE;
355
356/* This variable is set when waiting for an event, which is the only moment
357 * scrollbar dragging can be done directly. It's not allowed while commands
358 * are executed, because it may move the cursor and that may cause unexpected
359 * problems (e.g., while ":s" is working).
360 */
361static int allow_scrollbar = FALSE;
362
363#ifdef GLOBAL_IME
364# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
365#else
366# define MyTranslateMessage(x) TranslateMessage(x)
367#endif
368
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100369#if defined(FEAT_DIRECTX)
370 static int
371directx_enabled(void)
372{
373 if (s_dwc != NULL)
374 return 1;
375 else if (s_directx_load_attempted)
376 return 0;
377 /* load DirectX */
378 DWrite_Init();
379 s_directx_load_attempted = 1;
380 s_dwc = DWriteContext_Open();
381 directx_binddc();
382 return s_dwc != NULL ? 1 : 0;
383}
384
385 static void
386directx_binddc(void)
387{
388 if (s_textArea != NULL)
389 {
390 RECT rect;
391 GetClientRect(s_textArea, &rect);
392 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
393 }
394}
395#endif
396
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200397#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100398 /* use of WindowProc depends on wide_WindowProc */
399# define MyWindowProc vim_WindowProc
400#else
401 /* use ordinary WindowProc */
402# define MyWindowProc DefWindowProc
403#endif
404
405extern int current_font_height; /* this is in os_mswin.c */
406
407static struct
408{
409 UINT key_sym;
410 char_u vim_code0;
411 char_u vim_code1;
412} special_keys[] =
413{
414 {VK_UP, 'k', 'u'},
415 {VK_DOWN, 'k', 'd'},
416 {VK_LEFT, 'k', 'l'},
417 {VK_RIGHT, 'k', 'r'},
418
419 {VK_F1, 'k', '1'},
420 {VK_F2, 'k', '2'},
421 {VK_F3, 'k', '3'},
422 {VK_F4, 'k', '4'},
423 {VK_F5, 'k', '5'},
424 {VK_F6, 'k', '6'},
425 {VK_F7, 'k', '7'},
426 {VK_F8, 'k', '8'},
427 {VK_F9, 'k', '9'},
428 {VK_F10, 'k', ';'},
429
430 {VK_F11, 'F', '1'},
431 {VK_F12, 'F', '2'},
432 {VK_F13, 'F', '3'},
433 {VK_F14, 'F', '4'},
434 {VK_F15, 'F', '5'},
435 {VK_F16, 'F', '6'},
436 {VK_F17, 'F', '7'},
437 {VK_F18, 'F', '8'},
438 {VK_F19, 'F', '9'},
439 {VK_F20, 'F', 'A'},
440
441 {VK_F21, 'F', 'B'},
442#ifdef FEAT_NETBEANS_INTG
443 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
444#endif
445 {VK_F22, 'F', 'C'},
446 {VK_F23, 'F', 'D'},
447 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
448
449 {VK_HELP, '%', '1'},
450 {VK_BACK, 'k', 'b'},
451 {VK_INSERT, 'k', 'I'},
452 {VK_DELETE, 'k', 'D'},
453 {VK_HOME, 'k', 'h'},
454 {VK_END, '@', '7'},
455 {VK_PRIOR, 'k', 'P'},
456 {VK_NEXT, 'k', 'N'},
457 {VK_PRINT, '%', '9'},
458 {VK_ADD, 'K', '6'},
459 {VK_SUBTRACT, 'K', '7'},
460 {VK_DIVIDE, 'K', '8'},
461 {VK_MULTIPLY, 'K', '9'},
462 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
463 {VK_DECIMAL, 'K', 'B'},
464
465 {VK_NUMPAD0, 'K', 'C'},
466 {VK_NUMPAD1, 'K', 'D'},
467 {VK_NUMPAD2, 'K', 'E'},
468 {VK_NUMPAD3, 'K', 'F'},
469 {VK_NUMPAD4, 'K', 'G'},
470 {VK_NUMPAD5, 'K', 'H'},
471 {VK_NUMPAD6, 'K', 'I'},
472 {VK_NUMPAD7, 'K', 'J'},
473 {VK_NUMPAD8, 'K', 'K'},
474 {VK_NUMPAD9, 'K', 'L'},
475
476 /* Keys that we want to be able to use any modifier with: */
477 {VK_SPACE, ' ', NUL},
478 {VK_TAB, TAB, NUL},
479 {VK_ESCAPE, ESC, NUL},
480 {NL, NL, NUL},
481 {CAR, CAR, NUL},
482
483 /* End of list marker: */
484 {0, 0, 0}
485};
486
487/* Local variables */
488static int s_button_pending = -1;
489
490/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
491 * so don't reset s_button_pending. */
492static int s_getting_focus = FALSE;
493
494static int s_x_pending;
495static int s_y_pending;
496static UINT s_kFlags_pending;
497static UINT s_wait_timer = 0; /* Timer for get char from user */
498static int s_timed_out = FALSE;
499static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
500
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100501#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100502/* balloon-eval WM_NOTIFY_HANDLER */
503static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
504static void TrackUserActivity(UINT uMsg);
505#endif
506
507/*
508 * For control IME.
509 *
510 * These LOGFONT used for IME.
511 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100512#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
514static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100515#endif
516#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100517/* holds LOGFONT for 'guifont' always. */
518static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519#endif
520
521#ifdef FEAT_MBYTE_IME
522static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
523#endif
524
525#if defined(FEAT_BROWSE)
526static char_u *convert_filter(char_u *s);
527#endif
528
529#ifdef DEBUG_PRINT_ERROR
530/*
531 * Print out the last Windows error message
532 */
533 static void
534print_windows_error(void)
535{
536 LPVOID lpMsgBuf;
537
538 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
539 NULL, GetLastError(),
540 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
541 (LPTSTR) &lpMsgBuf, 0, NULL);
542 TRACE1("Error: %s\n", lpMsgBuf);
543 LocalFree(lpMsgBuf);
544}
545#endif
546
547/*
548 * Cursor blink functions.
549 *
550 * This is a simple state machine:
551 * BLINK_NONE not blinking at all
552 * BLINK_OFF blinking, cursor is not shown
553 * BLINK_ON blinking, cursor is shown
554 */
555
556#define BLINK_NONE 0
557#define BLINK_OFF 1
558#define BLINK_ON 2
559
560static int blink_state = BLINK_NONE;
561static long_u blink_waittime = 700;
562static long_u blink_ontime = 400;
563static long_u blink_offtime = 250;
564static UINT blink_timer = 0;
565
Bram Moolenaar703a8042016-06-04 16:24:32 +0200566 int
567gui_mch_is_blinking(void)
568{
569 return blink_state != BLINK_NONE;
570}
571
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200572 int
573gui_mch_is_blink_off(void)
574{
575 return blink_state == BLINK_OFF;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 void
579gui_mch_set_blinking(long wait, long on, long off)
580{
581 blink_waittime = wait;
582 blink_ontime = on;
583 blink_offtime = off;
584}
585
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100586 static VOID CALLBACK
587_OnBlinkTimer(
588 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100589 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100590 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100591 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100592{
593 MSG msg;
594
595 /*
596 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
597 */
598
599 KillTimer(NULL, idEvent);
600
601 /* Eat spurious WM_TIMER messages */
602 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
603 ;
604
605 if (blink_state == BLINK_ON)
606 {
607 gui_undraw_cursor();
608 blink_state = BLINK_OFF;
609 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
610 (TIMERPROC)_OnBlinkTimer);
611 }
612 else
613 {
614 gui_update_cursor(TRUE, FALSE);
615 blink_state = BLINK_ON;
616 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100617 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100618 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100619 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620}
621
622 static void
623gui_mswin_rm_blink_timer(void)
624{
625 MSG msg;
626
627 if (blink_timer != 0)
628 {
629 KillTimer(NULL, blink_timer);
630 /* Eat spurious WM_TIMER messages */
631 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
632 ;
633 blink_timer = 0;
634 }
635}
636
637/*
638 * Stop the cursor blinking. Show the cursor if it wasn't shown.
639 */
640 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100641gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100642{
643 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100644 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100645 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100647 gui_mch_flush();
648 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100649 blink_state = BLINK_NONE;
650}
651
652/*
653 * Start the cursor blinking. If it was already blinking, this restarts the
654 * waiting time and shows the cursor.
655 */
656 void
657gui_mch_start_blink(void)
658{
659 gui_mswin_rm_blink_timer();
660
661 /* Only switch blinking on if none of the times is zero */
662 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
663 {
664 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
665 (TIMERPROC)_OnBlinkTimer);
666 blink_state = BLINK_ON;
667 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100668 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 }
670}
671
672/*
673 * Call-back routines.
674 */
675
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100676 static VOID CALLBACK
677_OnTimer(
678 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100679 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100680 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100681 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100682{
683 MSG msg;
684
685 /*
686 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
687 */
688 KillTimer(NULL, idEvent);
689 s_timed_out = TRUE;
690
691 /* Eat spurious WM_TIMER messages */
692 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
693 ;
694 if (idEvent == s_wait_timer)
695 s_wait_timer = 0;
696}
697
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 static void
699_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100700 HWND hwnd UNUSED,
701 UINT ch UNUSED,
702 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100703{
704 dead_key = 1;
705}
706
707/*
708 * Convert Unicode character "ch" to bytes in "string[slen]".
709 * When "had_alt" is TRUE the ALT key was included in "ch".
710 * Return the length.
711 */
712 static int
713char_to_string(int ch, char_u *string, int slen, int had_alt)
714{
715 int len;
716 int i;
717#ifdef FEAT_MBYTE
718 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200719 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100720
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200721 wstring[0] = ch;
722 len = 1;
723
724 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
725 * "enc_codepage" is non-zero use the standard Win32 function,
726 * otherwise use our own conversion function (e.g., for UTF-8). */
727 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100728 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200729 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
730 (LPSTR)string, slen, 0, NULL);
731 /* If we had included the ALT key into the character but now the
732 * upper bit is no longer set, that probably means the conversion
733 * failed. Convert the original character and set the upper bit
734 * afterwards. */
735 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200737 wstring[0] = ch & 0x7f;
738 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
739 (LPSTR)string, slen, 0, NULL);
740 if (len == 1) /* safety check */
741 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100742 }
743 }
744 else
745 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100746 len = 1;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200747 ws = utf16_to_enc(wstring, &len);
748 if (ws == NULL)
749 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100750 else
751 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200752 if (len > slen) /* just in case */
753 len = slen;
754 mch_memmove(string, ws, len);
755 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100756 }
757 }
758
759 if (len == 0)
760#endif
761 {
762 string[0] = ch;
763 len = 1;
764 }
765
766 for (i = 0; i < len; ++i)
767 if (string[i] == CSI && len <= slen - 2)
768 {
769 /* Insert CSI as K_CSI. */
770 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
771 string[++i] = KS_EXTRA;
772 string[++i] = (int)KE_CSI;
773 len += 2;
774 }
775
776 return len;
777}
778
779/*
780 * Key hit, add it to the input buffer.
781 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100782 static void
783_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100784 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100785 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100786 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100787{
788 char_u string[40];
789 int len = 0;
790
791 dead_key = 0;
792
793 len = char_to_string(ch, string, 40, FALSE);
794 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
795 {
796 trash_input_buf();
797 got_int = TRUE;
798 }
799
800 add_to_input_buf(string, len);
801}
802
803/*
804 * Alt-Key hit, add it to the input buffer.
805 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 static void
807_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100808 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100809 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100810 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811{
812 char_u string[40]; /* Enough for multibyte character */
813 int len;
814 int modifiers;
815 int ch = cch; /* special keys are negative */
816
817 dead_key = 0;
818
819 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
820
821 /* OK, we have a character key (given by ch) which was entered with the
822 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
823 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
824 * CAPSLOCK is pressed) at this point.
825 */
826 modifiers = MOD_MASK_ALT;
827 if (GetKeyState(VK_SHIFT) & 0x8000)
828 modifiers |= MOD_MASK_SHIFT;
829 if (GetKeyState(VK_CONTROL) & 0x8000)
830 modifiers |= MOD_MASK_CTRL;
831
832 ch = simplify_key(ch, &modifiers);
833 /* remove the SHIFT modifier for keys where it's already included, e.g.,
834 * '(' and '*' */
835 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
836 modifiers &= ~MOD_MASK_SHIFT;
837
838 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
839 ch = extract_modifiers(ch, &modifiers);
840 if (ch == CSI)
841 ch = K_CSI;
842
843 len = 0;
844 if (modifiers)
845 {
846 string[len++] = CSI;
847 string[len++] = KS_MODIFIER;
848 string[len++] = modifiers;
849 }
850
851 if (IS_SPECIAL((int)ch))
852 {
853 string[len++] = CSI;
854 string[len++] = K_SECOND((int)ch);
855 string[len++] = K_THIRD((int)ch);
856 }
857 else
858 {
859 /* Although the documentation isn't clear about it, we assume "ch" is
860 * a Unicode character. */
861 len += char_to_string(ch, string + len, 40 - len, TRUE);
862 }
863
864 add_to_input_buf(string, len);
865}
866
867 static void
868_OnMouseEvent(
869 int button,
870 int x,
871 int y,
872 int repeated_click,
873 UINT keyFlags)
874{
875 int vim_modifiers = 0x0;
876
877 s_getting_focus = FALSE;
878
879 if (keyFlags & MK_SHIFT)
880 vim_modifiers |= MOUSE_SHIFT;
881 if (keyFlags & MK_CONTROL)
882 vim_modifiers |= MOUSE_CTRL;
883 if (GetKeyState(VK_MENU) & 0x8000)
884 vim_modifiers |= MOUSE_ALT;
885
886 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
887}
888
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100889 static void
890_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100891 HWND hwnd UNUSED,
892 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100893 int x,
894 int y,
895 UINT keyFlags)
896{
897 static LONG s_prevTime = 0;
898
899 LONG currentTime = GetMessageTime();
900 int button = -1;
901 int repeated_click;
902
903 /* Give main window the focus: this is so the cursor isn't hollow. */
904 (void)SetFocus(s_hwnd);
905
906 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
907 button = MOUSE_LEFT;
908 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
909 button = MOUSE_MIDDLE;
910 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
911 button = MOUSE_RIGHT;
912 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
913 {
914#ifndef GET_XBUTTON_WPARAM
915# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
916#endif
917 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
918 }
919 else if (s_uMsg == WM_CAPTURECHANGED)
920 {
921 /* on W95/NT4, somehow you get in here with an odd Msg
922 * if you press one button while holding down the other..*/
923 if (s_button_pending == MOUSE_LEFT)
924 button = MOUSE_RIGHT;
925 else
926 button = MOUSE_LEFT;
927 }
928 if (button >= 0)
929 {
930 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
931
932 /*
933 * Holding down the left and right buttons simulates pushing the middle
934 * button.
935 */
936 if (repeated_click
937 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
938 || (button == MOUSE_RIGHT
939 && s_button_pending == MOUSE_LEFT)))
940 {
941 /*
942 * Hmm, gui.c will ignore more than one button down at a time, so
943 * pretend we let go of it first.
944 */
945 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
946 button = MOUSE_MIDDLE;
947 repeated_click = FALSE;
948 s_button_pending = -1;
949 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
950 }
951 else if ((repeated_click)
952 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
953 {
954 if (s_button_pending > -1)
955 {
956 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
957 s_button_pending = -1;
958 }
959 /* TRACE("Button down at x %d, y %d\n", x, y); */
960 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
961 }
962 else
963 {
964 /*
965 * If this is the first press (i.e. not a multiple click) don't
966 * action immediately, but store and wait for:
967 * i) button-up
968 * ii) mouse move
969 * iii) another button press
970 * before using it.
971 * This enables us to make left+right simulate middle button,
972 * without left or right being actioned first. The side-effect is
973 * that if you click and hold the mouse without dragging, the
974 * cursor doesn't move until you release the button. In practice
975 * this is hardly a problem.
976 */
977 s_button_pending = button;
978 s_x_pending = x;
979 s_y_pending = y;
980 s_kFlags_pending = keyFlags;
981 }
982
983 s_prevTime = currentTime;
984 }
985}
986
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100987 static void
988_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100989 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100990 int x,
991 int y,
992 UINT keyFlags)
993{
994 int button;
995
996 s_getting_focus = FALSE;
997 if (s_button_pending > -1)
998 {
999 /* Delayed action for mouse down event */
1000 _OnMouseEvent(s_button_pending, s_x_pending,
1001 s_y_pending, FALSE, s_kFlags_pending);
1002 s_button_pending = -1;
1003 }
1004 if (s_uMsg == WM_MOUSEMOVE)
1005 {
1006 /*
1007 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1008 * down.
1009 */
1010 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1011 | MK_XBUTTON1 | MK_XBUTTON2)))
1012 {
1013 gui_mouse_moved(x, y);
1014 return;
1015 }
1016
1017 /*
1018 * While button is down, keep grabbing mouse move events when
1019 * the mouse goes outside the window
1020 */
1021 SetCapture(s_textArea);
1022 button = MOUSE_DRAG;
1023 /* TRACE(" move at x %d, y %d\n", x, y); */
1024 }
1025 else
1026 {
1027 ReleaseCapture();
1028 button = MOUSE_RELEASE;
1029 /* TRACE(" up at x %d, y %d\n", x, y); */
1030 }
1031
1032 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1033}
1034
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001035 static void
1036_OnSizeTextArea(
1037 HWND hwnd UNUSED,
1038 UINT state UNUSED,
1039 int cx UNUSED,
1040 int cy UNUSED)
1041{
1042#if defined(FEAT_DIRECTX)
1043 if (IS_ENABLE_DIRECTX())
1044 directx_binddc();
1045#endif
1046}
1047
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001048#ifdef FEAT_MENU
1049/*
1050 * Find the vimmenu_T with the given id
1051 */
1052 static vimmenu_T *
1053gui_mswin_find_menu(
1054 vimmenu_T *pMenu,
1055 int id)
1056{
1057 vimmenu_T *pChildMenu;
1058
1059 while (pMenu)
1060 {
1061 if (pMenu->id == (UINT)id)
1062 break;
1063 if (pMenu->children != NULL)
1064 {
1065 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1066 if (pChildMenu)
1067 {
1068 pMenu = pChildMenu;
1069 break;
1070 }
1071 }
1072 pMenu = pMenu->next;
1073 }
1074 return pMenu;
1075}
1076
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001077 static void
1078_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001079 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001080 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001081 HWND hwndCtl UNUSED,
1082 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001083{
1084 vimmenu_T *pMenu;
1085
1086 pMenu = gui_mswin_find_menu(root_menu, id);
1087 if (pMenu)
1088 gui_menu_cb(pMenu);
1089}
1090#endif
1091
1092#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001093# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094/*
1095 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1096 */
1097 static void
1098findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1099{
1100 WCHAR *wp;
1101
1102 lpfrw->hwndOwner = lpfr->hwndOwner;
1103 lpfrw->Flags = lpfr->Flags;
1104
1105 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1106 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1107 vim_free(wp);
1108
1109 /* the field "lpstrReplaceWith" doesn't need to be copied */
1110}
1111
1112/*
1113 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1114 */
1115 static void
1116findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1117{
1118 char_u *p;
1119
1120 lpfr->Flags = lpfrw->Flags;
1121
1122 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1123 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1124 vim_free(p);
1125
1126 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1127 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1128 vim_free(p);
1129}
1130# endif
1131
1132/*
1133 * Handle a Find/Replace window message.
1134 */
1135 static void
1136_OnFindRepl(void)
1137{
1138 int flags = 0;
1139 int down;
1140
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001141# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001142 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1143 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001144 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 {
1146 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1147 }
1148# endif
1149
1150 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1151 /* Give main window the focus back. */
1152 (void)SetFocus(s_hwnd);
1153
1154 if (s_findrep_struct.Flags & FR_FINDNEXT)
1155 {
1156 flags = FRD_FINDNEXT;
1157
1158 /* Give main window the focus back: this is so the cursor isn't
1159 * hollow. */
1160 (void)SetFocus(s_hwnd);
1161 }
1162 else if (s_findrep_struct.Flags & FR_REPLACE)
1163 {
1164 flags = FRD_REPLACE;
1165
1166 /* Give main window the focus back: this is so the cursor isn't
1167 * hollow. */
1168 (void)SetFocus(s_hwnd);
1169 }
1170 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1171 {
1172 flags = FRD_REPLACEALL;
1173 }
1174
1175 if (flags != 0)
1176 {
1177 /* Call the generic GUI function to do the actual work. */
1178 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1179 flags |= FRD_WHOLE_WORD;
1180 if (s_findrep_struct.Flags & FR_MATCHCASE)
1181 flags |= FRD_MATCH_CASE;
1182 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1183 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1184 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1185 }
1186}
1187#endif
1188
1189 static void
1190HandleMouseHide(UINT uMsg, LPARAM lParam)
1191{
1192 static LPARAM last_lParam = 0L;
1193
1194 /* We sometimes get a mousemove when the mouse didn't move... */
1195 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1196 {
1197 if (lParam == last_lParam)
1198 return;
1199 last_lParam = lParam;
1200 }
1201
1202 /* Handle specially, to centralise coding. We need to be sure we catch all
1203 * possible events which should cause us to restore the cursor (as it is a
1204 * shared resource, we take full responsibility for it).
1205 */
1206 switch (uMsg)
1207 {
1208 case WM_KEYUP:
1209 case WM_CHAR:
1210 /*
1211 * blank out the pointer if necessary
1212 */
1213 if (p_mh)
1214 gui_mch_mousehide(TRUE);
1215 break;
1216
1217 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1218 case WM_SYSCHAR:
1219 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1220 case WM_LBUTTONDOWN:
1221 case WM_LBUTTONUP:
1222 case WM_MBUTTONDOWN:
1223 case WM_MBUTTONUP:
1224 case WM_RBUTTONDOWN:
1225 case WM_RBUTTONUP:
1226 case WM_XBUTTONDOWN:
1227 case WM_XBUTTONUP:
1228 case WM_NCMOUSEMOVE:
1229 case WM_NCLBUTTONDOWN:
1230 case WM_NCLBUTTONUP:
1231 case WM_NCMBUTTONDOWN:
1232 case WM_NCMBUTTONUP:
1233 case WM_NCRBUTTONDOWN:
1234 case WM_NCRBUTTONUP:
1235 case WM_KILLFOCUS:
1236 /*
1237 * if the pointer is currently hidden, then we should show it.
1238 */
1239 gui_mch_mousehide(FALSE);
1240 break;
1241 }
1242}
1243
1244 static LRESULT CALLBACK
1245_TextAreaWndProc(
1246 HWND hwnd,
1247 UINT uMsg,
1248 WPARAM wParam,
1249 LPARAM lParam)
1250{
1251 /*
1252 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1253 hwnd, uMsg, wParam, lParam);
1254 */
1255
1256 HandleMouseHide(uMsg, lParam);
1257
1258 s_uMsg = uMsg;
1259 s_wParam = wParam;
1260 s_lParam = lParam;
1261
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001262#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001263 TrackUserActivity(uMsg);
1264#endif
1265
1266 switch (uMsg)
1267 {
1268 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1269 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1270 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1271 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1272 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1273 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1274 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1275 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1276 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1277 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1278 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1279 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1280 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1281 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001282 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001283
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001284#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001285 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1286 return TRUE;
1287#endif
1288 default:
1289 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1290 }
1291}
1292
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001293#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001294 || defined(GLOBAL_IME) \
1295 || defined(PROTO)
1296# ifdef PROTO
1297typedef int WINAPI;
1298# endif
1299
1300 LRESULT WINAPI
1301vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1302{
1303# ifdef GLOBAL_IME
1304 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1305# else
1306 if (wide_WindowProc)
1307 return DefWindowProcW(hwnd, message, wParam, lParam);
1308 return DefWindowProc(hwnd, message, wParam, lParam);
1309#endif
1310}
1311#endif
1312
1313/*
1314 * Called when the foreground or background color has been changed.
1315 */
1316 void
1317gui_mch_new_colors(void)
1318{
1319 /* nothing to do? */
1320}
1321
1322/*
1323 * Set the colors to their default values.
1324 */
1325 void
1326gui_mch_def_colors(void)
1327{
1328 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1329 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1330 gui.def_norm_pixel = gui.norm_pixel;
1331 gui.def_back_pixel = gui.back_pixel;
1332}
1333
1334/*
1335 * Open the GUI window which was created by a call to gui_mch_init().
1336 */
1337 int
1338gui_mch_open(void)
1339{
1340#ifndef SW_SHOWDEFAULT
1341# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1342#endif
1343 /* Actually open the window, if not already visible
1344 * (may be done already in gui_mch_set_shellsize) */
1345 if (!IsWindowVisible(s_hwnd))
1346 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1347
1348#ifdef MSWIN_FIND_REPLACE
1349 /* Init replace string here, so that we keep it when re-opening the
1350 * dialog. */
1351 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1352#endif
1353
1354 return OK;
1355}
1356
1357/*
1358 * Get the position of the top left corner of the window.
1359 */
1360 int
1361gui_mch_get_winpos(int *x, int *y)
1362{
1363 RECT rect;
1364
1365 GetWindowRect(s_hwnd, &rect);
1366 *x = rect.left;
1367 *y = rect.top;
1368 return OK;
1369}
1370
1371/*
1372 * Set the position of the top left corner of the window to the given
1373 * coordinates.
1374 */
1375 void
1376gui_mch_set_winpos(int x, int y)
1377{
1378 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1379 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1380}
1381 void
1382gui_mch_set_text_area_pos(int x, int y, int w, int h)
1383{
1384 static int oldx = 0;
1385 static int oldy = 0;
1386
1387 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1388
1389#ifdef FEAT_TOOLBAR
1390 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1391 SendMessage(s_toolbarhwnd, WM_SIZE,
1392 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1393#endif
1394#if defined(FEAT_GUI_TABLINE)
1395 if (showing_tabline)
1396 {
1397 int top = 0;
1398 RECT rect;
1399
1400# ifdef FEAT_TOOLBAR
1401 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1402 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1403# endif
1404 GetClientRect(s_hwnd, &rect);
1405 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1406 }
1407#endif
1408
1409 /* When side scroll bar is unshown, the size of window will change.
1410 * then, the text area move left or right. thus client rect should be
1411 * forcedly redrawn. (Yasuhiro Matsumoto) */
1412 if (oldx != x || oldy != y)
1413 {
1414 InvalidateRect(s_hwnd, NULL, FALSE);
1415 oldx = x;
1416 oldy = y;
1417 }
1418}
1419
1420
1421/*
1422 * Scrollbar stuff:
1423 */
1424
1425 void
1426gui_mch_enable_scrollbar(
1427 scrollbar_T *sb,
1428 int flag)
1429{
1430 ShowScrollBar(sb->id, SB_CTL, flag);
1431
1432 /* TODO: When the window is maximized, the size of the window stays the
1433 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1434 * NT 4.0 it's not... */
1435}
1436
1437 void
1438gui_mch_set_scrollbar_pos(
1439 scrollbar_T *sb,
1440 int x,
1441 int y,
1442 int w,
1443 int h)
1444{
1445 SetWindowPos(sb->id, NULL, x, y, w, h,
1446 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1447}
1448
1449 void
1450gui_mch_create_scrollbar(
1451 scrollbar_T *sb,
1452 int orient) /* SBAR_VERT or SBAR_HORIZ */
1453{
1454 sb->id = CreateWindow(
1455 "SCROLLBAR", "Scrollbar",
1456 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1457 10, /* Any value will do for now */
1458 10, /* Any value will do for now */
1459 s_hwnd, NULL,
1460 s_hinst, NULL);
1461}
1462
1463/*
1464 * Find the scrollbar with the given hwnd.
1465 */
1466 static scrollbar_T *
1467gui_mswin_find_scrollbar(HWND hwnd)
1468{
1469 win_T *wp;
1470
1471 if (gui.bottom_sbar.id == hwnd)
1472 return &gui.bottom_sbar;
1473 FOR_ALL_WINDOWS(wp)
1474 {
1475 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1476 return &wp->w_scrollbars[SBAR_LEFT];
1477 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1478 return &wp->w_scrollbars[SBAR_RIGHT];
1479 }
1480 return NULL;
1481}
1482
1483/*
1484 * Get the character size of a font.
1485 */
1486 static void
1487GetFontSize(GuiFont font)
1488{
1489 HWND hwnd = GetDesktopWindow();
1490 HDC hdc = GetWindowDC(hwnd);
1491 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1492 TEXTMETRIC tm;
1493
1494 GetTextMetrics(hdc, &tm);
1495 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1496
1497 gui.char_height = tm.tmHeight + p_linespace;
1498
1499 SelectFont(hdc, hfntOld);
1500
1501 ReleaseDC(hwnd, hdc);
1502}
1503
1504/*
1505 * Adjust gui.char_height (after 'linespace' was changed).
1506 */
1507 int
1508gui_mch_adjust_charheight(void)
1509{
1510 GetFontSize(gui.norm_font);
1511 return OK;
1512}
1513
1514 static GuiFont
1515get_font_handle(LOGFONT *lf)
1516{
1517 HFONT font = NULL;
1518
1519 /* Load the font */
1520 font = CreateFontIndirect(lf);
1521
1522 if (font == NULL)
1523 return NOFONT;
1524
1525 return (GuiFont)font;
1526}
1527
1528 static int
1529pixels_to_points(int pixels, int vertical)
1530{
1531 int points;
1532 HWND hwnd;
1533 HDC hdc;
1534
1535 hwnd = GetDesktopWindow();
1536 hdc = GetWindowDC(hwnd);
1537
1538 points = MulDiv(pixels, 72,
1539 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1540
1541 ReleaseDC(hwnd, hdc);
1542
1543 return points;
1544}
1545
1546 GuiFont
1547gui_mch_get_font(
1548 char_u *name,
1549 int giveErrorIfMissing)
1550{
1551 LOGFONT lf;
1552 GuiFont font = NOFONT;
1553
1554 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1555 font = get_font_handle(&lf);
1556 if (font == NOFONT && giveErrorIfMissing)
1557 EMSG2(_(e_font), name);
1558 return font;
1559}
1560
1561#if defined(FEAT_EVAL) || defined(PROTO)
1562/*
1563 * Return the name of font "font" in allocated memory.
1564 * Don't know how to get the actual name, thus use the provided name.
1565 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001566 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001567gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001568{
1569 if (name == NULL)
1570 return NULL;
1571 return vim_strsave(name);
1572}
1573#endif
1574
1575 void
1576gui_mch_free_font(GuiFont font)
1577{
1578 if (font)
1579 DeleteObject((HFONT)font);
1580}
1581
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582/*
1583 * Return the Pixel value (color) for the given color name.
1584 * Return INVALCOLOR for error.
1585 */
1586 guicolor_T
1587gui_mch_get_color(char_u *name)
1588{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001589 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590
1591 typedef struct SysColorTable
1592 {
1593 char *name;
1594 int color;
1595 } SysColorTable;
1596
1597 static SysColorTable sys_table[] =
1598 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001599 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1600 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001601#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001602 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1603#endif
1604 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1605 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1606 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1607 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1608 {"SYS_DESKTOP", COLOR_DESKTOP},
1609 {"SYS_INFOBK", COLOR_INFOBK},
1610 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1611 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001612 {"SYS_BTNFACE", COLOR_BTNFACE},
1613 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1614 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1615 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1616 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1617 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1618 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1619 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1620 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1621 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1622 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1623 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1624 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1625 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1626 {"SYS_MENU", COLOR_MENU},
1627 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1628 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1629 {"SYS_WINDOW", COLOR_WINDOW},
1630 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1631 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1632 };
1633
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001634 /*
1635 * Try to look up a system colour.
1636 */
1637 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1638 if (STRICMP(name, sys_table[i].name) == 0)
1639 return GetSysColor(sys_table[i].color);
1640
Bram Moolenaarab302212016-04-26 20:59:29 +02001641 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001642}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001643
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001644 guicolor_T
1645gui_mch_get_rgb_color(int r, int g, int b)
1646{
1647 return gui_get_rgb_color_cmn(r, g, b);
1648}
1649
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001650/*
1651 * Return OK if the key with the termcap name "name" is supported.
1652 */
1653 int
1654gui_mch_haskey(char_u *name)
1655{
1656 int i;
1657
1658 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1659 if (name[0] == special_keys[i].vim_code0 &&
1660 name[1] == special_keys[i].vim_code1)
1661 return OK;
1662 return FAIL;
1663}
1664
1665 void
1666gui_mch_beep(void)
1667{
1668 MessageBeep(MB_OK);
1669}
1670/*
1671 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1672 */
1673 void
1674gui_mch_invert_rectangle(
1675 int r,
1676 int c,
1677 int nr,
1678 int nc)
1679{
1680 RECT rc;
1681
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001682#if defined(FEAT_DIRECTX)
1683 if (IS_ENABLE_DIRECTX())
1684 DWriteContext_Flush(s_dwc);
1685#endif
1686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687 /*
1688 * Note: InvertRect() excludes right and bottom of rectangle.
1689 */
1690 rc.left = FILL_X(c);
1691 rc.top = FILL_Y(r);
1692 rc.right = rc.left + nc * gui.char_width;
1693 rc.bottom = rc.top + nr * gui.char_height;
1694 InvertRect(s_hdc, &rc);
1695}
1696
1697/*
1698 * Iconify the GUI window.
1699 */
1700 void
1701gui_mch_iconify(void)
1702{
1703 ShowWindow(s_hwnd, SW_MINIMIZE);
1704}
1705
1706/*
1707 * Draw a cursor without focus.
1708 */
1709 void
1710gui_mch_draw_hollow_cursor(guicolor_T color)
1711{
1712 HBRUSH hbr;
1713 RECT rc;
1714
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001715#if defined(FEAT_DIRECTX)
1716 if (IS_ENABLE_DIRECTX())
1717 DWriteContext_Flush(s_dwc);
1718#endif
1719
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720 /*
1721 * Note: FrameRect() excludes right and bottom of rectangle.
1722 */
1723 rc.left = FILL_X(gui.col);
1724 rc.top = FILL_Y(gui.row);
1725 rc.right = rc.left + gui.char_width;
1726#ifdef FEAT_MBYTE
1727 if (mb_lefthalve(gui.row, gui.col))
1728 rc.right += gui.char_width;
1729#endif
1730 rc.bottom = rc.top + gui.char_height;
1731 hbr = CreateSolidBrush(color);
1732 FrameRect(s_hdc, &rc, hbr);
1733 DeleteBrush(hbr);
1734}
1735/*
1736 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1737 * color "color".
1738 */
1739 void
1740gui_mch_draw_part_cursor(
1741 int w,
1742 int h,
1743 guicolor_T color)
1744{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001745 RECT rc;
1746
1747 /*
1748 * Note: FillRect() excludes right and bottom of rectangle.
1749 */
1750 rc.left =
1751#ifdef FEAT_RIGHTLEFT
1752 /* vertical line should be on the right of current point */
1753 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1754#endif
1755 FILL_X(gui.col);
1756 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1757 rc.right = rc.left + w;
1758 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001759
Bram Moolenaar92467d32017-12-05 13:22:16 +01001760 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761}
1762
1763
1764/*
1765 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1766 * dead key's nominal character and re-post the original message.
1767 */
1768 static void
1769outputDeadKey_rePost(MSG originalMsg)
1770{
1771 static MSG deadCharExpel;
1772
1773 if (!dead_key)
1774 return;
1775
1776 dead_key = 0;
1777
1778 /* Make Windows generate the dead key's character */
1779 deadCharExpel.message = originalMsg.message;
1780 deadCharExpel.hwnd = originalMsg.hwnd;
1781 deadCharExpel.wParam = VK_SPACE;
1782
1783 MyTranslateMessage(&deadCharExpel);
1784
1785 /* re-generate the current character free of the dead char influence */
1786 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1787 originalMsg.lParam);
1788}
1789
1790
1791/*
1792 * Process a single Windows message.
1793 * If one is not available we hang until one is.
1794 */
1795 static void
1796process_message(void)
1797{
1798 MSG msg;
1799 UINT vk = 0; /* Virtual key */
1800 char_u string[40];
1801 int i;
1802 int modifiers = 0;
1803 int key;
1804#ifdef FEAT_MENU
1805 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1806#endif
1807
1808 pGetMessage(&msg, NULL, 0, 0);
1809
1810#ifdef FEAT_OLE
1811 /* Look after OLE Automation commands */
1812 if (msg.message == WM_OLE)
1813 {
1814 char_u *str = (char_u *)msg.lParam;
1815 if (str == NULL || *str == NUL)
1816 {
1817 /* Message can't be ours, forward it. Fixes problem with Ultramon
1818 * 3.0.4 */
1819 pDispatchMessage(&msg);
1820 }
1821 else
1822 {
1823 add_to_input_buf(str, (int)STRLEN(str));
1824 vim_free(str); /* was allocated in CVim::SendKeys() */
1825 }
1826 return;
1827 }
1828#endif
1829
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830#ifdef MSWIN_FIND_REPLACE
1831 /* Don't process messages used by the dialog */
1832 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1833 {
1834 HandleMouseHide(msg.message, msg.lParam);
1835 return;
1836 }
1837#endif
1838
1839 /*
1840 * Check if it's a special key that we recognise. If not, call
1841 * TranslateMessage().
1842 */
1843 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1844 {
1845 vk = (int) msg.wParam;
1846
1847 /*
1848 * Handle dead keys in special conditions in other cases we let Windows
1849 * handle them and do not interfere.
1850 *
1851 * The dead_key flag must be reset on several occasions:
1852 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1853 * consumed at that point (This is when we let Windows combine the
1854 * dead character on its own)
1855 *
1856 * - Before doing something special such as regenerating keypresses to
1857 * expel the dead character as this could trigger an infinite loop if
1858 * for some reason MyTranslateMessage() do not trigger a call
1859 * immediately to _OnChar() (or _OnSysChar()).
1860 */
1861 if (dead_key)
1862 {
1863 /*
1864 * If a dead key was pressed and the user presses VK_SPACE,
1865 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1866 * with the dead char now, so do nothing special and let Windows
1867 * handle it.
1868 *
1869 * Note that VK_SPACE combines with the dead_key's character and
1870 * only one WM_CHAR will be generated by TranslateMessage(), in
1871 * the two other cases two WM_CHAR will be generated: the dead
1872 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1873 * user expects.
1874 */
1875 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1876 {
1877 dead_key = 0;
1878 MyTranslateMessage(&msg);
1879 return;
1880 }
1881 /* In modes where we are not typing, dead keys should behave
1882 * normally */
1883 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1884 {
1885 outputDeadKey_rePost(msg);
1886 return;
1887 }
1888 }
1889
1890 /* Check for CTRL-BREAK */
1891 if (vk == VK_CANCEL)
1892 {
1893 trash_input_buf();
1894 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001895 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 string[0] = Ctrl_C;
1897 add_to_input_buf(string, 1);
1898 }
1899
1900 for (i = 0; special_keys[i].key_sym != 0; i++)
1901 {
1902 /* ignore VK_SPACE when ALT key pressed: system menu */
1903 if (special_keys[i].key_sym == vk
1904 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1905 {
1906 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001907 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001908 * is a key that would normally trigger the dead key nominal
1909 * character output (such as a NUMPAD printable character or
1910 * the TAB key, etc...).
1911 */
1912 if (dead_key && (special_keys[i].vim_code0 == 'K'
1913 || vk == VK_TAB || vk == CAR))
1914 {
1915 outputDeadKey_rePost(msg);
1916 return;
1917 }
1918
1919#ifdef FEAT_MENU
1920 /* Check for <F10>: Windows selects the menu. When <F10> is
1921 * mapped we want to use the mapping instead. */
1922 if (vk == VK_F10
1923 && gui.menu_is_active
1924 && check_map(k10, State, FALSE, TRUE, FALSE,
1925 NULL, NULL) == NULL)
1926 break;
1927#endif
1928 if (GetKeyState(VK_SHIFT) & 0x8000)
1929 modifiers |= MOD_MASK_SHIFT;
1930 /*
1931 * Don't use caps-lock as shift, because these are special keys
1932 * being considered here, and we only want letters to get
1933 * shifted -- webb
1934 */
1935 /*
1936 if (GetKeyState(VK_CAPITAL) & 0x0001)
1937 modifiers ^= MOD_MASK_SHIFT;
1938 */
1939 if (GetKeyState(VK_CONTROL) & 0x8000)
1940 modifiers |= MOD_MASK_CTRL;
1941 if (GetKeyState(VK_MENU) & 0x8000)
1942 modifiers |= MOD_MASK_ALT;
1943
1944 if (special_keys[i].vim_code1 == NUL)
1945 key = special_keys[i].vim_code0;
1946 else
1947 key = TO_SPECIAL(special_keys[i].vim_code0,
1948 special_keys[i].vim_code1);
1949 key = simplify_key(key, &modifiers);
1950 if (key == CSI)
1951 key = K_CSI;
1952
1953 if (modifiers)
1954 {
1955 string[0] = CSI;
1956 string[1] = KS_MODIFIER;
1957 string[2] = modifiers;
1958 add_to_input_buf(string, 3);
1959 }
1960
1961 if (IS_SPECIAL(key))
1962 {
1963 string[0] = CSI;
1964 string[1] = K_SECOND(key);
1965 string[2] = K_THIRD(key);
1966 add_to_input_buf(string, 3);
1967 }
1968 else
1969 {
1970 int len;
1971
1972 /* Handle "key" as a Unicode character. */
1973 len = char_to_string(key, string, 40, FALSE);
1974 add_to_input_buf(string, len);
1975 }
1976 break;
1977 }
1978 }
1979 if (special_keys[i].key_sym == 0)
1980 {
1981 /* Some keys need C-S- where they should only need C-.
1982 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1983 * system startup (Helmut Stiegler, 2003 Oct 3). */
1984 if (vk != 0xff
1985 && (GetKeyState(VK_CONTROL) & 0x8000)
1986 && !(GetKeyState(VK_SHIFT) & 0x8000)
1987 && !(GetKeyState(VK_MENU) & 0x8000))
1988 {
1989 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
1990 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1991 {
1992 string[0] = Ctrl_HAT;
1993 add_to_input_buf(string, 1);
1994 }
1995 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
1996 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
1997 {
1998 string[0] = Ctrl__;
1999 add_to_input_buf(string, 1);
2000 }
2001 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2002 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2003 {
2004 string[0] = Ctrl_AT;
2005 add_to_input_buf(string, 1);
2006 }
2007 else
2008 MyTranslateMessage(&msg);
2009 }
2010 else
2011 MyTranslateMessage(&msg);
2012 }
2013 }
2014#ifdef FEAT_MBYTE_IME
2015 else if (msg.message == WM_IME_NOTIFY)
2016 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2017 else if (msg.message == WM_KEYUP && im_get_status())
2018 /* added for non-MS IME (Yasuhiro Matsumoto) */
2019 MyTranslateMessage(&msg);
2020#endif
2021#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2022/* GIME_TEST */
2023 else if (msg.message == WM_IME_STARTCOMPOSITION)
2024 {
2025 POINT point;
2026
2027 global_ime_set_font(&norm_logfont);
2028 point.x = FILL_X(gui.col);
2029 point.y = FILL_Y(gui.row);
2030 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2031 global_ime_set_position(&point);
2032 }
2033#endif
2034
2035#ifdef FEAT_MENU
2036 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2037 * mapped we need to stop it here to avoid strange effects (e.g., for the
2038 * key-up event) */
2039 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2040 NULL, NULL) == NULL)
2041#endif
2042 pDispatchMessage(&msg);
2043}
2044
2045/*
2046 * Catch up with any queued events. This may put keyboard input into the
2047 * input buffer, call resize call-backs, trigger timers etc. If there is
2048 * nothing in the event queue (& no timers pending), then we return
2049 * immediately.
2050 */
2051 void
2052gui_mch_update(void)
2053{
2054 MSG msg;
2055
2056 if (!s_busy_processing)
2057 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2058 && !vim_is_input_buf_full())
2059 process_message();
2060}
2061
Bram Moolenaar4231da42016-06-02 14:30:04 +02002062 static void
2063remove_any_timer(void)
2064{
2065 MSG msg;
2066
2067 if (s_wait_timer != 0 && !s_timed_out)
2068 {
2069 KillTimer(NULL, s_wait_timer);
2070
2071 /* Eat spurious WM_TIMER messages */
2072 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2073 ;
2074 s_wait_timer = 0;
2075 }
2076}
2077
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002078/*
2079 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2080 * from the keyboard.
2081 * wtime == -1 Wait forever.
2082 * wtime == 0 This should never happen.
2083 * wtime > 0 Wait wtime milliseconds for a character.
2084 * Returns OK if a character was found to be available within the given time,
2085 * or FAIL otherwise.
2086 */
2087 int
2088gui_mch_wait_for_chars(int wtime)
2089{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002090 int focus;
2091
2092 s_timed_out = FALSE;
2093
2094 if (wtime > 0)
2095 {
2096 /* Don't do anything while processing a (scroll) message. */
2097 if (s_busy_processing)
2098 return FAIL;
2099 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2100 (TIMERPROC)_OnTimer);
2101 }
2102
2103 allow_scrollbar = TRUE;
2104
2105 focus = gui.in_focus;
2106 while (!s_timed_out)
2107 {
2108 /* Stop or start blinking when focus changes */
2109 if (gui.in_focus != focus)
2110 {
2111 if (gui.in_focus)
2112 gui_mch_start_blink();
2113 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002114 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115 focus = gui.in_focus;
2116 }
2117
2118 if (s_need_activate)
2119 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002120 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 s_need_activate = FALSE;
2122 }
2123
Bram Moolenaar4231da42016-06-02 14:30:04 +02002124#ifdef FEAT_TIMERS
2125 did_add_timer = FALSE;
2126#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002127#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002128 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002129 for (;;)
2130 {
2131 MSG msg;
2132
2133 parse_queued_messages();
2134
Bram Moolenaar62426e12017-08-13 15:37:58 +02002135 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2136 {
2137 process_message();
2138 break;
2139 }
2140 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2141 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002142 break;
2143 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002144#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002145 /*
2146 * Don't use gui_mch_update() because then we will spin-lock until a
2147 * char arrives, instead we use GetMessage() to hang until an
2148 * event arrives. No need to check for input_buf_full because we are
2149 * returning as soon as it contains a single char -- webb
2150 */
2151 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002152#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153
2154 if (input_available())
2155 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002156 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002157 allow_scrollbar = FALSE;
2158
2159 /* Clear pending mouse button, the release event may have been
2160 * taken by the dialog window. But don't do this when getting
2161 * focus, we need the mouse-up event then. */
2162 if (!s_getting_focus)
2163 s_button_pending = -1;
2164
2165 return OK;
2166 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002167
2168#ifdef FEAT_TIMERS
2169 if (did_add_timer)
2170 {
2171 /* Need to recompute the waiting time. */
2172 remove_any_timer();
2173 break;
2174 }
2175#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002176 }
2177 allow_scrollbar = FALSE;
2178 return FAIL;
2179}
2180
2181/*
2182 * Clear a rectangular region of the screen from text pos (row1, col1) to
2183 * (row2, col2) inclusive.
2184 */
2185 void
2186gui_mch_clear_block(
2187 int row1,
2188 int col1,
2189 int row2,
2190 int col2)
2191{
2192 RECT rc;
2193
2194 /*
2195 * Clear one extra pixel at the far right, for when bold characters have
2196 * spilled over to the window border.
2197 * Note: FillRect() excludes right and bottom of rectangle.
2198 */
2199 rc.left = FILL_X(col1);
2200 rc.top = FILL_Y(row1);
2201 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2202 rc.bottom = FILL_Y(row2 + 1);
2203 clear_rect(&rc);
2204}
2205
2206/*
2207 * Clear the whole text window.
2208 */
2209 void
2210gui_mch_clear_all(void)
2211{
2212 RECT rc;
2213
2214 rc.left = 0;
2215 rc.top = 0;
2216 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2217 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2218 clear_rect(&rc);
2219}
2220/*
2221 * Menu stuff.
2222 */
2223
2224 void
2225gui_mch_enable_menu(int flag)
2226{
2227#ifdef FEAT_MENU
2228 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2229#endif
2230}
2231
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002232 void
2233gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002234 int x UNUSED,
2235 int y UNUSED,
2236 int w UNUSED,
2237 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238{
2239 /* It will be in the right place anyway */
2240}
2241
2242#if defined(FEAT_MENU) || defined(PROTO)
2243/*
2244 * Make menu item hidden or not hidden
2245 */
2246 void
2247gui_mch_menu_hidden(
2248 vimmenu_T *menu,
2249 int hidden)
2250{
2251 /*
2252 * This doesn't do what we want. Hmm, just grey the menu items for now.
2253 */
2254 /*
2255 if (hidden)
2256 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2257 else
2258 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2259 */
2260 gui_mch_menu_grey(menu, hidden);
2261}
2262
2263/*
2264 * This is called after setting all the menus to grey/hidden or not.
2265 */
2266 void
2267gui_mch_draw_menubar(void)
2268{
2269 DrawMenuBar(s_hwnd);
2270}
2271#endif /*FEAT_MENU*/
2272
2273#ifndef PROTO
2274void
2275#ifdef VIMDLL
2276_export
2277#endif
2278_cdecl
2279SaveInst(HINSTANCE hInst)
2280{
2281 s_hinst = hInst;
2282}
2283#endif
2284
2285/*
2286 * Return the RGB value of a pixel as a long.
2287 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002288 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002289gui_mch_get_rgb(guicolor_T pixel)
2290{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002291 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2292 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002293}
2294
2295#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2296/* Convert pixels in X to dialog units */
2297 static WORD
2298PixelToDialogX(int numPixels)
2299{
2300 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2301}
2302
2303/* Convert pixels in Y to dialog units */
2304 static WORD
2305PixelToDialogY(int numPixels)
2306{
2307 return (WORD)((numPixels * 8) / s_dlgfntheight);
2308}
2309
2310/* Return the width in pixels of the given text in the given DC. */
2311 static int
2312GetTextWidth(HDC hdc, char_u *str, int len)
2313{
2314 SIZE size;
2315
2316 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2317 return size.cx;
2318}
2319
2320#ifdef FEAT_MBYTE
2321/*
2322 * Return the width in pixels of the given text in the given DC, taking care
2323 * of 'encoding' to active codepage conversion.
2324 */
2325 static int
2326GetTextWidthEnc(HDC hdc, char_u *str, int len)
2327{
2328 SIZE size;
2329 WCHAR *wstr;
2330 int n;
2331 int wlen = len;
2332
2333 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2334 {
2335 /* 'encoding' differs from active codepage: convert text and use wide
2336 * function */
2337 wstr = enc_to_utf16(str, &wlen);
2338 if (wstr != NULL)
2339 {
2340 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2341 vim_free(wstr);
2342 if (n)
2343 return size.cx;
2344 }
2345 }
2346
2347 return GetTextWidth(hdc, str, len);
2348}
2349#else
2350# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2351#endif
2352
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002353static void get_work_area(RECT *spi_rect);
2354
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355/*
2356 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002357 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2358 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359 */
2360 static BOOL
2361CenterWindow(
2362 HWND hwndChild,
2363 HWND hwndParent)
2364{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002365 HMONITOR mon;
2366 MONITORINFO moninfo;
2367 RECT rChild, rParent, rScreen;
2368 int wChild, hChild, wParent, hParent;
2369 int xNew, yNew;
2370 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371
2372 GetWindowRect(hwndChild, &rChild);
2373 wChild = rChild.right - rChild.left;
2374 hChild = rChild.bottom - rChild.top;
2375
2376 /* If Vim is minimized put the window in the middle of the screen. */
2377 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002378 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002379 else
2380 GetWindowRect(hwndParent, &rParent);
2381 wParent = rParent.right - rParent.left;
2382 hParent = rParent.bottom - rParent.top;
2383
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002384 moninfo.cbSize = sizeof(MONITORINFO);
2385 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2386 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002388 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002389 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002390 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002392 hdc = GetDC(hwndChild);
2393 rScreen.left = 0;
2394 rScreen.top = 0;
2395 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2396 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2397 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398 }
2399
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002400 xNew = rParent.left + ((wParent - wChild) / 2);
2401 if (xNew < rScreen.left)
2402 xNew = rScreen.left;
2403 else if ((xNew + wChild) > rScreen.right)
2404 xNew = rScreen.right - wChild;
2405
2406 yNew = rParent.top + ((hParent - hChild) / 2);
2407 if (yNew < rScreen.top)
2408 yNew = rScreen.top;
2409 else if ((yNew + hChild) > rScreen.bottom)
2410 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002411
2412 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2413 SWP_NOSIZE | SWP_NOZORDER);
2414}
2415#endif /* FEAT_GUI_DIALOG */
2416
2417void
2418gui_mch_activate_window(void)
2419{
2420 (void)SetActiveWindow(s_hwnd);
2421}
2422
2423#if defined(FEAT_TOOLBAR) || defined(PROTO)
2424 void
2425gui_mch_show_toolbar(int showit)
2426{
2427 if (s_toolbarhwnd == NULL)
2428 return;
2429
2430 if (showit)
2431 {
2432# ifdef FEAT_MBYTE
2433# ifndef TB_SETUNICODEFORMAT
2434 /* For older compilers. We assume this never changes. */
2435# define TB_SETUNICODEFORMAT 0x2005
2436# endif
2437 /* Enable/disable unicode support */
2438 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2439 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2440# endif
2441 ShowWindow(s_toolbarhwnd, SW_SHOW);
2442 }
2443 else
2444 ShowWindow(s_toolbarhwnd, SW_HIDE);
2445}
2446
2447/* Then number of bitmaps is fixed. Exit is missing! */
2448#define TOOLBAR_BITMAP_COUNT 31
2449
2450#endif
2451
2452#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2453 static void
2454add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2455{
2456#ifdef FEAT_MBYTE
2457 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002458
2459 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2460 {
2461 /* 'encoding' differs from active codepage: convert menu name
2462 * and use wide function */
2463 wn = enc_to_utf16(item_text, NULL);
2464 if (wn != NULL)
2465 {
2466 MENUITEMINFOW infow;
2467
2468 infow.cbSize = sizeof(infow);
2469 infow.fMask = MIIM_TYPE | MIIM_ID;
2470 infow.wID = item_id;
2471 infow.fType = MFT_STRING;
2472 infow.dwTypeData = wn;
2473 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002474 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002476 }
2477 }
2478
2479 if (wn == NULL)
2480#endif
2481 {
2482 MENUITEMINFO info;
2483
2484 info.cbSize = sizeof(info);
2485 info.fMask = MIIM_TYPE | MIIM_ID;
2486 info.wID = item_id;
2487 info.fType = MFT_STRING;
2488 info.dwTypeData = (LPTSTR)item_text;
2489 info.cch = (UINT)STRLEN(item_text);
2490 InsertMenuItem(pmenu, item_id, FALSE, &info);
2491 }
2492}
2493
2494 static void
2495show_tabline_popup_menu(void)
2496{
2497 HMENU tab_pmenu;
2498 long rval;
2499 POINT pt;
2500
2501 /* When ignoring events don't show the menu. */
2502 if (hold_gui_events
2503# ifdef FEAT_CMDWIN
2504 || cmdwin_type != 0
2505# endif
2506 )
2507 return;
2508
2509 tab_pmenu = CreatePopupMenu();
2510 if (tab_pmenu == NULL)
2511 return;
2512
2513 if (first_tabpage->tp_next != NULL)
2514 add_tabline_popup_menu_entry(tab_pmenu,
2515 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2516 add_tabline_popup_menu_entry(tab_pmenu,
2517 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2518 add_tabline_popup_menu_entry(tab_pmenu,
2519 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2520
2521 GetCursorPos(&pt);
2522 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2523 NULL);
2524
2525 DestroyMenu(tab_pmenu);
2526
2527 /* Add the string cmd into input buffer */
2528 if (rval > 0)
2529 {
2530 TCHITTESTINFO htinfo;
2531 int idx;
2532
2533 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2534 return;
2535
2536 htinfo.pt.x = pt.x;
2537 htinfo.pt.y = pt.y;
2538 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2539 if (idx == -1)
2540 idx = 0;
2541 else
2542 idx += 1;
2543
2544 send_tabline_menu_event(idx, (int)rval);
2545 }
2546}
2547
2548/*
2549 * Show or hide the tabline.
2550 */
2551 void
2552gui_mch_show_tabline(int showit)
2553{
2554 if (s_tabhwnd == NULL)
2555 return;
2556
2557 if (!showit != !showing_tabline)
2558 {
2559 if (showit)
2560 ShowWindow(s_tabhwnd, SW_SHOW);
2561 else
2562 ShowWindow(s_tabhwnd, SW_HIDE);
2563 showing_tabline = showit;
2564 }
2565}
2566
2567/*
2568 * Return TRUE when tabline is displayed.
2569 */
2570 int
2571gui_mch_showing_tabline(void)
2572{
2573 return s_tabhwnd != NULL && showing_tabline;
2574}
2575
2576/*
2577 * Update the labels of the tabline.
2578 */
2579 void
2580gui_mch_update_tabline(void)
2581{
2582 tabpage_T *tp;
2583 TCITEM tie;
2584 int nr = 0;
2585 int curtabidx = 0;
2586 int tabadded = 0;
2587#ifdef FEAT_MBYTE
2588 static int use_unicode = FALSE;
2589 int uu;
2590 WCHAR *wstr = NULL;
2591#endif
2592
2593 if (s_tabhwnd == NULL)
2594 return;
2595
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002596#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002597# ifndef CCM_SETUNICODEFORMAT
2598 /* For older compilers. We assume this never changes. */
2599# define CCM_SETUNICODEFORMAT 0x2005
2600# endif
2601 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2602 if (uu != use_unicode)
2603 {
2604 /* Enable/disable unicode support */
2605 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2606 use_unicode = uu;
2607 }
2608#endif
2609
2610 tie.mask = TCIF_TEXT;
2611 tie.iImage = -1;
2612
2613 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2614 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2615
2616 /* Add a label for each tab page. They all contain the same text area. */
2617 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2618 {
2619 if (tp == curtab)
2620 curtabidx = nr;
2621
2622 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2623 {
2624 /* Add the tab */
2625 tie.pszText = "-Empty-";
2626 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2627 tabadded = 1;
2628 }
2629
2630 get_tabline_label(tp, FALSE);
2631 tie.pszText = (LPSTR)NameBuff;
2632#ifdef FEAT_MBYTE
2633 wstr = NULL;
2634 if (use_unicode)
2635 {
2636 /* Need to go through Unicode. */
2637 wstr = enc_to_utf16(NameBuff, NULL);
2638 if (wstr != NULL)
2639 {
2640 TCITEMW tiw;
2641
2642 tiw.mask = TCIF_TEXT;
2643 tiw.iImage = -1;
2644 tiw.pszText = wstr;
2645 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2646 vim_free(wstr);
2647 }
2648 }
2649 if (wstr == NULL)
2650#endif
2651 {
2652 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2653 }
2654 }
2655
2656 /* Remove any old labels. */
2657 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2658 TabCtrl_DeleteItem(s_tabhwnd, nr);
2659
2660 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2661 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2662
2663 /* Re-enable redraw and redraw. */
2664 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2665 RedrawWindow(s_tabhwnd, NULL, NULL,
2666 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2667
2668 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2669 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2670}
2671
2672/*
2673 * Set the current tab to "nr". First tab is 1.
2674 */
2675 void
2676gui_mch_set_curtab(int nr)
2677{
2678 if (s_tabhwnd == NULL)
2679 return;
2680
2681 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2682 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2683}
2684
2685#endif
2686
2687/*
2688 * ":simalt" command.
2689 */
2690 void
2691ex_simalt(exarg_T *eap)
2692{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002693 char_u *keys = eap->arg;
2694 int fill_typebuf = FALSE;
2695 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002696
2697 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2698 while (*keys)
2699 {
2700 if (*keys == '~')
2701 *keys = ' '; /* for showing system menu */
2702 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2703 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002704 fill_typebuf = TRUE;
2705 }
2706 if (fill_typebuf)
2707 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002708 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002709 * processed. */
2710 key_name[0] = K_SPECIAL;
2711 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002712 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002713 key_name[3] = NUL;
2714 typebuf_was_filled = TRUE;
2715 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 }
2717}
2718
2719/*
2720 * Create the find & replace dialogs.
2721 * You can't have both at once: ":find" when replace is showing, destroys
2722 * the replace dialog first, and the other way around.
2723 */
2724#ifdef MSWIN_FIND_REPLACE
2725 static void
2726initialise_findrep(char_u *initial_string)
2727{
2728 int wword = FALSE;
2729 int mcase = !p_ic;
2730 char_u *entry_text;
2731
2732 /* Get the search string to use. */
2733 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2734
2735 s_findrep_struct.hwndOwner = s_hwnd;
2736 s_findrep_struct.Flags = FR_DOWN;
2737 if (mcase)
2738 s_findrep_struct.Flags |= FR_MATCHCASE;
2739 if (wword)
2740 s_findrep_struct.Flags |= FR_WHOLEWORD;
2741 if (entry_text != NULL && *entry_text != NUL)
2742 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2743 s_findrep_struct.wFindWhatLen - 1);
2744 vim_free(entry_text);
2745}
2746#endif
2747
2748 static void
2749set_window_title(HWND hwnd, char *title)
2750{
2751#ifdef FEAT_MBYTE
2752 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2753 {
2754 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002755
2756 /* Convert the title from 'encoding' to UTF-16. */
2757 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2758 if (wbuf != NULL)
2759 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002760 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002762 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002763 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002764 }
2765#endif
2766 (void)SetWindowText(hwnd, (LPCSTR)title);
2767}
2768
2769 void
2770gui_mch_find_dialog(exarg_T *eap)
2771{
2772#ifdef MSWIN_FIND_REPLACE
2773 if (s_findrep_msg != 0)
2774 {
2775 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2776 DestroyWindow(s_findrep_hwnd);
2777
2778 if (!IsWindow(s_findrep_hwnd))
2779 {
2780 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002781# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002782 /* If the OS is Windows NT, and 'encoding' differs from active
2783 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002784 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785 {
2786 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2787 s_findrep_hwnd = FindTextW(
2788 (LPFINDREPLACEW) &s_findrep_struct_w);
2789 }
2790 else
2791# endif
2792 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2793 }
2794
2795 set_window_title(s_findrep_hwnd,
2796 _("Find string (use '\\\\' to find a '\\')"));
2797 (void)SetFocus(s_findrep_hwnd);
2798
2799 s_findrep_is_find = TRUE;
2800 }
2801#endif
2802}
2803
2804
2805 void
2806gui_mch_replace_dialog(exarg_T *eap)
2807{
2808#ifdef MSWIN_FIND_REPLACE
2809 if (s_findrep_msg != 0)
2810 {
2811 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2812 DestroyWindow(s_findrep_hwnd);
2813
2814 if (!IsWindow(s_findrep_hwnd))
2815 {
2816 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002817# ifdef FEAT_MBYTE
2818 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002819 {
2820 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2821 s_findrep_hwnd = ReplaceTextW(
2822 (LPFINDREPLACEW) &s_findrep_struct_w);
2823 }
2824 else
2825# endif
2826 s_findrep_hwnd = ReplaceText(
2827 (LPFINDREPLACE) &s_findrep_struct);
2828 }
2829
2830 set_window_title(s_findrep_hwnd,
2831 _("Find & Replace (use '\\\\' to find a '\\')"));
2832 (void)SetFocus(s_findrep_hwnd);
2833
2834 s_findrep_is_find = FALSE;
2835 }
2836#endif
2837}
2838
2839
2840/*
2841 * Set visibility of the pointer.
2842 */
2843 void
2844gui_mch_mousehide(int hide)
2845{
2846 if (hide != gui.pointer_hidden)
2847 {
2848 ShowCursor(!hide);
2849 gui.pointer_hidden = hide;
2850 }
2851}
2852
2853#ifdef FEAT_MENU
2854 static void
2855gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2856{
2857 /* Unhide the mouse, we don't get move events here. */
2858 gui_mch_mousehide(FALSE);
2859
2860 (void)TrackPopupMenu(
2861 (HMENU)menu->submenu_id,
2862 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2863 x, y,
2864 (int)0, /*reserved param*/
2865 s_hwnd,
2866 NULL);
2867 /*
2868 * NOTE: The pop-up menu can eat the mouse up event.
2869 * We deal with this in normal.c.
2870 */
2871}
2872#endif
2873
2874/*
2875 * Got a message when the system will go down.
2876 */
2877 static void
2878_OnEndSession(void)
2879{
2880 getout_preserve_modified(1);
2881}
2882
2883/*
2884 * Get this message when the user clicks on the cross in the top right corner
2885 * of a Windows95 window.
2886 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002887 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002888_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002889{
2890 gui_shell_closed();
2891}
2892
2893/*
2894 * Get a message when the window is being destroyed.
2895 */
2896 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002897_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002898{
2899 if (!destroying)
2900 _OnClose(hwnd);
2901}
2902
2903 static void
2904_OnPaint(
2905 HWND hwnd)
2906{
2907 if (!IsMinimized(hwnd))
2908 {
2909 PAINTSTRUCT ps;
2910
2911 out_flush(); /* make sure all output has been processed */
2912 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913
2914#ifdef FEAT_MBYTE
2915 /* prevent multi-byte characters from misprinting on an invalid
2916 * rectangle */
2917 if (has_mbyte)
2918 {
2919 RECT rect;
2920
2921 GetClientRect(hwnd, &rect);
2922 ps.rcPaint.left = rect.left;
2923 ps.rcPaint.right = rect.right;
2924 }
2925#endif
2926
2927 if (!IsRectEmpty(&ps.rcPaint))
2928 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002929 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2930 ps.rcPaint.right - ps.rcPaint.left + 1,
2931 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2932 }
2933
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934 EndPaint(hwnd, &ps);
2935 }
2936}
2937
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002938 static void
2939_OnSize(
2940 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002941 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002942 int cx,
2943 int cy)
2944{
2945 if (!IsMinimized(hwnd))
2946 {
2947 gui_resize_shell(cx, cy);
2948
2949#ifdef FEAT_MENU
2950 /* Menu bar may wrap differently now */
2951 gui_mswin_get_menu_height(TRUE);
2952#endif
2953 }
2954}
2955
2956 static void
2957_OnSetFocus(
2958 HWND hwnd,
2959 HWND hwndOldFocus)
2960{
2961 gui_focus_change(TRUE);
2962 s_getting_focus = TRUE;
2963 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2964}
2965
2966 static void
2967_OnKillFocus(
2968 HWND hwnd,
2969 HWND hwndNewFocus)
2970{
2971 gui_focus_change(FALSE);
2972 s_getting_focus = FALSE;
2973 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2974}
2975
2976/*
2977 * Get a message when the user switches back to vim
2978 */
2979 static LRESULT
2980_OnActivateApp(
2981 HWND hwnd,
2982 BOOL fActivate,
2983 DWORD dwThreadId)
2984{
2985 /* we call gui_focus_change() in _OnSetFocus() */
2986 /* gui_focus_change((int)fActivate); */
2987 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2988}
2989
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002990 void
2991gui_mch_destroy_scrollbar(scrollbar_T *sb)
2992{
2993 DestroyWindow(sb->id);
2994}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995
2996/*
2997 * Get current mouse coordinates in text window.
2998 */
2999 void
3000gui_mch_getmouse(int *x, int *y)
3001{
3002 RECT rct;
3003 POINT mp;
3004
3005 (void)GetWindowRect(s_textArea, &rct);
3006 (void)GetCursorPos((LPPOINT)&mp);
3007 *x = (int)(mp.x - rct.left);
3008 *y = (int)(mp.y - rct.top);
3009}
3010
3011/*
3012 * Move mouse pointer to character at (x, y).
3013 */
3014 void
3015gui_mch_setmouse(int x, int y)
3016{
3017 RECT rct;
3018
3019 (void)GetWindowRect(s_textArea, &rct);
3020 (void)SetCursorPos(x + gui.border_offset + rct.left,
3021 y + gui.border_offset + rct.top);
3022}
3023
3024 static void
3025gui_mswin_get_valid_dimensions(
3026 int w,
3027 int h,
3028 int *valid_w,
3029 int *valid_h)
3030{
3031 int base_width, base_height;
3032
3033 base_width = gui_get_base_width()
3034 + (GetSystemMetrics(SM_CXFRAME) +
3035 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3036 base_height = gui_get_base_height()
3037 + (GetSystemMetrics(SM_CYFRAME) +
3038 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3039 + GetSystemMetrics(SM_CYCAPTION)
3040#ifdef FEAT_MENU
3041 + gui_mswin_get_menu_height(FALSE)
3042#endif
3043 ;
3044 *valid_w = base_width +
3045 ((w - base_width) / gui.char_width) * gui.char_width;
3046 *valid_h = base_height +
3047 ((h - base_height) / gui.char_height) * gui.char_height;
3048}
3049
3050 void
3051gui_mch_flash(int msec)
3052{
3053 RECT rc;
3054
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003055#if defined(FEAT_DIRECTX)
3056 if (IS_ENABLE_DIRECTX())
3057 DWriteContext_Flush(s_dwc);
3058#endif
3059
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 /*
3061 * Note: InvertRect() excludes right and bottom of rectangle.
3062 */
3063 rc.left = 0;
3064 rc.top = 0;
3065 rc.right = gui.num_cols * gui.char_width;
3066 rc.bottom = gui.num_rows * gui.char_height;
3067 InvertRect(s_hdc, &rc);
3068 gui_mch_flush(); /* make sure it's displayed */
3069
3070 ui_delay((long)msec, TRUE); /* wait for a few msec */
3071
3072 InvertRect(s_hdc, &rc);
3073}
3074
3075/*
3076 * Return flags used for scrolling.
3077 * The SW_INVALIDATE is required when part of the window is covered or
3078 * off-screen. Refer to MS KB Q75236.
3079 */
3080 static int
3081get_scroll_flags(void)
3082{
3083 HWND hwnd;
3084 RECT rcVim, rcOther, rcDest;
3085
3086 GetWindowRect(s_hwnd, &rcVim);
3087
3088 /* Check if the window is partly above or below the screen. We don't care
3089 * about partly left or right of the screen, it is not relevant when
3090 * scrolling up or down. */
3091 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3092 return SW_INVALIDATE;
3093
3094 /* Check if there is an window (partly) on top of us. */
3095 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3096 if (IsWindowVisible(hwnd))
3097 {
3098 GetWindowRect(hwnd, &rcOther);
3099 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3100 return SW_INVALIDATE;
3101 }
3102 return 0;
3103}
3104
3105/*
3106 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3107 * may not be scrolled out properly.
3108 * For gVim, when _OnScroll() is repeated, the character at the
3109 * previous cursor position may be left drawn after scroll.
3110 * The problem can be avoided by calling GetPixel() to get a pixel in
3111 * the region before ScrollWindowEx().
3112 */
3113 static void
3114intel_gpu_workaround(void)
3115{
3116 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3117}
3118
3119/*
3120 * Delete the given number of lines from the given row, scrolling up any
3121 * text further down within the scroll region.
3122 */
3123 void
3124gui_mch_delete_lines(
3125 int row,
3126 int num_lines)
3127{
3128 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003129
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 rc.left = FILL_X(gui.scroll_region_left);
3131 rc.right = FILL_X(gui.scroll_region_right + 1);
3132 rc.top = FILL_Y(row);
3133 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3134
Bram Moolenaar92467d32017-12-05 13:22:16 +01003135#if defined(FEAT_DIRECTX)
3136 if (IS_ENABLE_DIRECTX())
3137 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003138 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3139 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003140 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003141 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003142#endif
3143 {
3144 intel_gpu_workaround();
3145 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003147 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003148 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150 /* This seems to be required to avoid the cursor disappearing when
3151 * scrolling such that the cursor ends up in the top-left character on
3152 * the screen... But why? (Webb) */
3153 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3154 /* gui.cursor_is_valid = FALSE; */
3155
3156 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3157 gui.scroll_region_left,
3158 gui.scroll_region_bot, gui.scroll_region_right);
3159}
3160
3161/*
3162 * Insert the given number of lines before the given row, scrolling down any
3163 * following text within the scroll region.
3164 */
3165 void
3166gui_mch_insert_lines(
3167 int row,
3168 int num_lines)
3169{
3170 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003171
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 rc.left = FILL_X(gui.scroll_region_left);
3173 rc.right = FILL_X(gui.scroll_region_right + 1);
3174 rc.top = FILL_Y(row);
3175 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176
3177#if defined(FEAT_DIRECTX)
3178 if (IS_ENABLE_DIRECTX())
3179 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003180 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3181 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003182 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003183 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003184#endif
3185 {
3186 intel_gpu_workaround();
3187 /* The SW_INVALIDATE is required when part of the window is covered or
3188 * off-screen. How do we avoid it when it's not needed? */
3189 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003190 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003191 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003192 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 gui_clear_block(row, gui.scroll_region_left,
3195 row + num_lines - 1, gui.scroll_region_right);
3196}
3197
3198
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003200gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201{
3202#if defined(FEAT_DIRECTX)
3203 DWriteContext_Close(s_dwc);
3204 DWrite_Final();
3205 s_dwc = NULL;
3206#endif
3207
3208 ReleaseDC(s_textArea, s_hdc);
3209 DeleteObject(s_brush);
3210
3211#ifdef FEAT_TEAROFF
3212 /* Unload the tearoff bitmap */
3213 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3214#endif
3215
3216 /* Destroy our window (if we have one). */
3217 if (s_hwnd != NULL)
3218 {
3219 destroying = TRUE; /* ignore WM_DESTROY message now */
3220 DestroyWindow(s_hwnd);
3221 }
3222
3223#ifdef GLOBAL_IME
3224 global_ime_end();
3225#endif
3226}
3227
3228 static char_u *
3229logfont2name(LOGFONT lf)
3230{
3231 char *p;
3232 char *res;
3233 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003234 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 char *font_name = lf.lfFaceName;
3236
3237 charset_name = charset_id2name((int)lf.lfCharSet);
3238#ifdef FEAT_MBYTE
3239 /* Convert a font name from the current codepage to 'encoding'.
3240 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3241 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3242 {
3243 int len;
3244 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3245 (char_u **)&font_name, &len);
3246 }
3247#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003248 quality_name = quality_id2name((int)lf.lfQuality);
3249
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003250 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3251 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3252 if (res != NULL)
3253 {
3254 p = res;
3255 /* make a normal font string out of the lf thing:*/
3256 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3257 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3258 while (*p)
3259 {
3260 if (*p == ' ')
3261 *p = '_';
3262 ++p;
3263 }
3264 if (lf.lfItalic)
3265 STRCAT(p, ":i");
3266 if (lf.lfWeight >= FW_BOLD)
3267 STRCAT(p, ":b");
3268 if (lf.lfUnderline)
3269 STRCAT(p, ":u");
3270 if (lf.lfStrikeOut)
3271 STRCAT(p, ":s");
3272 if (charset_name != NULL)
3273 {
3274 STRCAT(p, ":c");
3275 STRCAT(p, charset_name);
3276 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003277 if (quality_name != NULL)
3278 {
3279 STRCAT(p, ":q");
3280 STRCAT(p, quality_name);
3281 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 }
3283
3284#ifdef FEAT_MBYTE
3285 if (font_name != lf.lfFaceName)
3286 vim_free(font_name);
3287#endif
3288 return (char_u *)res;
3289}
3290
3291
3292#ifdef FEAT_MBYTE_IME
3293/*
3294 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3295 * 'guifont'
3296 */
3297 static void
3298update_im_font(void)
3299{
3300 LOGFONT lf_wide;
3301
3302 if (p_guifontwide != NULL && *p_guifontwide != NUL
3303 && gui.wide_font != NOFONT
3304 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3305 norm_logfont = lf_wide;
3306 else
3307 norm_logfont = sub_logfont;
3308 im_set_font(&norm_logfont);
3309}
3310#endif
3311
3312#ifdef FEAT_MBYTE
3313/*
3314 * Handler of gui.wide_font (p_guifontwide) changed notification.
3315 */
3316 void
3317gui_mch_wide_font_changed(void)
3318{
3319 LOGFONT lf;
3320
3321# ifdef FEAT_MBYTE_IME
3322 update_im_font();
3323# endif
3324
3325 gui_mch_free_font(gui.wide_ital_font);
3326 gui.wide_ital_font = NOFONT;
3327 gui_mch_free_font(gui.wide_bold_font);
3328 gui.wide_bold_font = NOFONT;
3329 gui_mch_free_font(gui.wide_boldital_font);
3330 gui.wide_boldital_font = NOFONT;
3331
3332 if (gui.wide_font
3333 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3334 {
3335 if (!lf.lfItalic)
3336 {
3337 lf.lfItalic = TRUE;
3338 gui.wide_ital_font = get_font_handle(&lf);
3339 lf.lfItalic = FALSE;
3340 }
3341 if (lf.lfWeight < FW_BOLD)
3342 {
3343 lf.lfWeight = FW_BOLD;
3344 gui.wide_bold_font = get_font_handle(&lf);
3345 if (!lf.lfItalic)
3346 {
3347 lf.lfItalic = TRUE;
3348 gui.wide_boldital_font = get_font_handle(&lf);
3349 }
3350 }
3351 }
3352}
3353#endif
3354
3355/*
3356 * Initialise vim to use the font with the given name.
3357 * Return FAIL if the font could not be loaded, OK otherwise.
3358 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003360gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361{
3362 LOGFONT lf;
3363 GuiFont font = NOFONT;
3364 char_u *p;
3365
3366 /* Load the font */
3367 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3368 font = get_font_handle(&lf);
3369 if (font == NOFONT)
3370 return FAIL;
3371
3372 if (font_name == NULL)
3373 font_name = (char_u *)lf.lfFaceName;
3374#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3375 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003376#endif
3377#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378 sub_logfont = lf;
3379#endif
3380#ifdef FEAT_MBYTE_IME
3381 update_im_font();
3382#endif
3383 gui_mch_free_font(gui.norm_font);
3384 gui.norm_font = font;
3385 current_font_height = lf.lfHeight;
3386 GetFontSize(font);
3387
3388 p = logfont2name(lf);
3389 if (p != NULL)
3390 {
3391 hl_set_font_name(p);
3392
3393 /* When setting 'guifont' to "*" replace it with the actual font name.
3394 * */
3395 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3396 {
3397 vim_free(p_guifont);
3398 p_guifont = p;
3399 }
3400 else
3401 vim_free(p);
3402 }
3403
3404 gui_mch_free_font(gui.ital_font);
3405 gui.ital_font = NOFONT;
3406 gui_mch_free_font(gui.bold_font);
3407 gui.bold_font = NOFONT;
3408 gui_mch_free_font(gui.boldital_font);
3409 gui.boldital_font = NOFONT;
3410
3411 if (!lf.lfItalic)
3412 {
3413 lf.lfItalic = TRUE;
3414 gui.ital_font = get_font_handle(&lf);
3415 lf.lfItalic = FALSE;
3416 }
3417 if (lf.lfWeight < FW_BOLD)
3418 {
3419 lf.lfWeight = FW_BOLD;
3420 gui.bold_font = get_font_handle(&lf);
3421 if (!lf.lfItalic)
3422 {
3423 lf.lfItalic = TRUE;
3424 gui.boldital_font = get_font_handle(&lf);
3425 }
3426 }
3427
3428 return OK;
3429}
3430
3431#ifndef WPF_RESTORETOMAXIMIZED
3432# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3433#endif
3434
3435/*
3436 * Return TRUE if the GUI window is maximized, filling the whole screen.
3437 */
3438 int
3439gui_mch_maximized(void)
3440{
3441 WINDOWPLACEMENT wp;
3442
3443 wp.length = sizeof(WINDOWPLACEMENT);
3444 if (GetWindowPlacement(s_hwnd, &wp))
3445 return wp.showCmd == SW_SHOWMAXIMIZED
3446 || (wp.showCmd == SW_SHOWMINIMIZED
3447 && wp.flags == WPF_RESTORETOMAXIMIZED);
3448
3449 return 0;
3450}
3451
3452/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003453 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3454 * is set. Compute the new Rows and Columns. This is like resizing the
3455 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 */
3457 void
3458gui_mch_newfont(void)
3459{
3460 RECT rect;
3461
3462 GetWindowRect(s_hwnd, &rect);
3463 if (win_socket_id == 0)
3464 {
3465 gui_resize_shell(rect.right - rect.left
3466 - (GetSystemMetrics(SM_CXFRAME) +
3467 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3468 rect.bottom - rect.top
3469 - (GetSystemMetrics(SM_CYFRAME) +
3470 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3471 - GetSystemMetrics(SM_CYCAPTION)
3472#ifdef FEAT_MENU
3473 - gui_mswin_get_menu_height(FALSE)
3474#endif
3475 );
3476 }
3477 else
3478 {
3479 /* Inside another window, don't use the frame and border. */
3480 gui_resize_shell(rect.right - rect.left,
3481 rect.bottom - rect.top
3482#ifdef FEAT_MENU
3483 - gui_mswin_get_menu_height(FALSE)
3484#endif
3485 );
3486 }
3487}
3488
3489/*
3490 * Set the window title
3491 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 void
3493gui_mch_settitle(
3494 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003495 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496{
3497 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3498}
3499
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003500#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3502 * misc2.c! */
3503static LPCSTR mshape_idcs[] =
3504{
3505 IDC_ARROW, /* arrow */
3506 MAKEINTRESOURCE(0), /* blank */
3507 IDC_IBEAM, /* beam */
3508 IDC_SIZENS, /* updown */
3509 IDC_SIZENS, /* udsizing */
3510 IDC_SIZEWE, /* leftright */
3511 IDC_SIZEWE, /* lrsizing */
3512 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 IDC_ARROW, /* crosshair */
3515 IDC_ARROW, /* hand1 */
3516 IDC_ARROW, /* hand2 */
3517 IDC_ARROW, /* pencil */
3518 IDC_ARROW, /* question */
3519 IDC_ARROW, /* right-arrow */
3520 IDC_UPARROW, /* up-arrow */
3521 IDC_ARROW /* last one */
3522};
3523
3524 void
3525mch_set_mouse_shape(int shape)
3526{
3527 LPCSTR idc;
3528
3529 if (shape == MSHAPE_HIDE)
3530 ShowCursor(FALSE);
3531 else
3532 {
3533 if (shape >= MSHAPE_NUMBERED)
3534 idc = IDC_ARROW;
3535 else
3536 idc = mshape_idcs[shape];
3537#ifdef SetClassLongPtr
3538 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3539#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541#endif
3542 if (!p_mh)
3543 {
3544 POINT mp;
3545
3546 /* Set the position to make it redrawn with the new shape. */
3547 (void)GetCursorPos((LPPOINT)&mp);
3548 (void)SetCursorPos(mp.x, mp.y);
3549 ShowCursor(TRUE);
3550 }
3551 }
3552}
3553#endif
3554
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003555#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556/*
3557 * The file browser exists in two versions: with "W" uses wide characters,
3558 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3559 * Windows NT/2000/XP the "W" functions are used.
3560 */
3561
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003562# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003563/*
3564 * Wide version of convert_filter().
3565 */
3566 static WCHAR *
3567convert_filterW(char_u *s)
3568{
3569 char_u *tmp;
3570 int len;
3571 WCHAR *res;
3572
3573 tmp = convert_filter(s);
3574 if (tmp == NULL)
3575 return NULL;
3576 len = (int)STRLEN(s) + 3;
3577 res = enc_to_utf16(tmp, &len);
3578 vim_free(tmp);
3579 return res;
3580}
3581
3582/*
3583 * Wide version of gui_mch_browse(). Keep in sync!
3584 */
3585 static char_u *
3586gui_mch_browseW(
3587 int saving,
3588 char_u *title,
3589 char_u *dflt,
3590 char_u *ext,
3591 char_u *initdir,
3592 char_u *filter)
3593{
3594 /* We always use the wide function. This means enc_to_utf16() must work,
3595 * otherwise it fails miserably! */
3596 OPENFILENAMEW fileStruct;
3597 WCHAR fileBuf[MAXPATHL];
3598 WCHAR *wp;
3599 int i;
3600 WCHAR *titlep = NULL;
3601 WCHAR *extp = NULL;
3602 WCHAR *initdirp = NULL;
3603 WCHAR *filterp;
3604 char_u *p;
3605
3606 if (dflt == NULL)
3607 fileBuf[0] = NUL;
3608 else
3609 {
3610 wp = enc_to_utf16(dflt, NULL);
3611 if (wp == NULL)
3612 fileBuf[0] = NUL;
3613 else
3614 {
3615 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3616 fileBuf[i] = wp[i];
3617 fileBuf[i] = NUL;
3618 vim_free(wp);
3619 }
3620 }
3621
3622 /* Convert the filter to Windows format. */
3623 filterp = convert_filterW(filter);
3624
3625 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003626# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003628 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003629# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003631# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632
3633 if (title != NULL)
3634 titlep = enc_to_utf16(title, NULL);
3635 fileStruct.lpstrTitle = titlep;
3636
3637 if (ext != NULL)
3638 extp = enc_to_utf16(ext, NULL);
3639 fileStruct.lpstrDefExt = extp;
3640
3641 fileStruct.lpstrFile = fileBuf;
3642 fileStruct.nMaxFile = MAXPATHL;
3643 fileStruct.lpstrFilter = filterp;
3644 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3645 /* has an initial dir been specified? */
3646 if (initdir != NULL && *initdir != NUL)
3647 {
3648 /* Must have backslashes here, no matter what 'shellslash' says */
3649 initdirp = enc_to_utf16(initdir, NULL);
3650 if (initdirp != NULL)
3651 {
3652 for (wp = initdirp; *wp != NUL; ++wp)
3653 if (*wp == '/')
3654 *wp = '\\';
3655 }
3656 fileStruct.lpstrInitialDir = initdirp;
3657 }
3658
3659 /*
3660 * TODO: Allow selection of multiple files. Needs another arg to this
3661 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3662 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3663 * files that don't exist yet, so I haven't put it in. What about
3664 * OFN_PATHMUSTEXIST?
3665 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3666 */
3667 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003668# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003669 if (curbuf->b_p_bin)
3670 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003671# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672 if (saving)
3673 {
3674 if (!GetSaveFileNameW(&fileStruct))
3675 return NULL;
3676 }
3677 else
3678 {
3679 if (!GetOpenFileNameW(&fileStruct))
3680 return NULL;
3681 }
3682
3683 vim_free(filterp);
3684 vim_free(initdirp);
3685 vim_free(titlep);
3686 vim_free(extp);
3687
3688 /* Convert from UCS2 to 'encoding'. */
3689 p = utf16_to_enc(fileBuf, NULL);
3690 if (p != NULL)
3691 /* when out of memory we get garbage for non-ASCII chars */
3692 STRCPY(fileBuf, p);
3693 vim_free(p);
3694
3695 /* Give focus back to main window (when using MDI). */
3696 SetFocus(s_hwnd);
3697
3698 /* Shorten the file name if possible */
3699 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3700}
3701# endif /* FEAT_MBYTE */
3702
3703
3704/*
3705 * Convert the string s to the proper format for a filter string by replacing
3706 * the \t and \n delimiters with \0.
3707 * Returns the converted string in allocated memory.
3708 *
3709 * Keep in sync with convert_filterW() above!
3710 */
3711 static char_u *
3712convert_filter(char_u *s)
3713{
3714 char_u *res;
3715 unsigned s_len = (unsigned)STRLEN(s);
3716 unsigned i;
3717
3718 res = alloc(s_len + 3);
3719 if (res != NULL)
3720 {
3721 for (i = 0; i < s_len; ++i)
3722 if (s[i] == '\t' || s[i] == '\n')
3723 res[i] = '\0';
3724 else
3725 res[i] = s[i];
3726 res[s_len] = NUL;
3727 /* Add two extra NULs to make sure it's properly terminated. */
3728 res[s_len + 1] = NUL;
3729 res[s_len + 2] = NUL;
3730 }
3731 return res;
3732}
3733
3734/*
3735 * Select a directory.
3736 */
3737 char_u *
3738gui_mch_browsedir(char_u *title, char_u *initdir)
3739{
3740 /* We fake this: Use a filter that doesn't select anything and a default
3741 * file name that won't be used. */
3742 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3743 initdir, (char_u *)_("Directory\t*.nothing\n"));
3744}
3745
3746/*
3747 * Pop open a file browser and return the file selected, in allocated memory,
3748 * or NULL if Cancel is hit.
3749 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3750 * title - Title message for the file browser dialog.
3751 * dflt - Default name of file.
3752 * ext - Default extension to be added to files without extensions.
3753 * initdir - directory in which to open the browser (NULL = current dir)
3754 * filter - Filter for matched files to choose from.
3755 *
3756 * Keep in sync with gui_mch_browseW() above!
3757 */
3758 char_u *
3759gui_mch_browse(
3760 int saving,
3761 char_u *title,
3762 char_u *dflt,
3763 char_u *ext,
3764 char_u *initdir,
3765 char_u *filter)
3766{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003767# ifdef FEAT_MBYTE
3768 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3769# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 OPENFILENAME fileStruct;
3771 char_u fileBuf[MAXPATHL];
3772 char_u *initdirp = NULL;
3773 char_u *filterp;
3774 char_u *p;
3775
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 if (dflt == NULL)
3777 fileBuf[0] = NUL;
3778 else
3779 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3780
3781 /* Convert the filter to Windows format. */
3782 filterp = convert_filter(filter);
3783
3784 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003785# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 /* be compatible with Windows NT 4.0 */
3787 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003788# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003790# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791
3792 fileStruct.lpstrTitle = (LPSTR)title;
3793 fileStruct.lpstrDefExt = (LPSTR)ext;
3794
3795 fileStruct.lpstrFile = (LPSTR)fileBuf;
3796 fileStruct.nMaxFile = MAXPATHL;
3797 fileStruct.lpstrFilter = (LPSTR)filterp;
3798 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3799 /* has an initial dir been specified? */
3800 if (initdir != NULL && *initdir != NUL)
3801 {
3802 /* Must have backslashes here, no matter what 'shellslash' says */
3803 initdirp = vim_strsave(initdir);
3804 if (initdirp != NULL)
3805 for (p = initdirp; *p != NUL; ++p)
3806 if (*p == '/')
3807 *p = '\\';
3808 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3809 }
3810
3811 /*
3812 * TODO: Allow selection of multiple files. Needs another arg to this
3813 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3814 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3815 * files that don't exist yet, so I haven't put it in. What about
3816 * OFN_PATHMUSTEXIST?
3817 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3818 */
3819 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003820# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 if (curbuf->b_p_bin)
3822 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003823# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003824 if (saving)
3825 {
3826 if (!GetSaveFileName(&fileStruct))
3827 return NULL;
3828 }
3829 else
3830 {
3831 if (!GetOpenFileName(&fileStruct))
3832 return NULL;
3833 }
3834
3835 vim_free(filterp);
3836 vim_free(initdirp);
3837
3838 /* Give focus back to main window (when using MDI). */
3839 SetFocus(s_hwnd);
3840
3841 /* Shorten the file name if possible */
3842 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003843# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844}
3845#endif /* FEAT_BROWSE */
3846
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 static void
3848_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003849 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 HDROP hDrop)
3851{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003852#define BUFPATHLEN _MAX_PATH
3853#define DRAGQVAL 0xFFFFFFFF
3854#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003856#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003857 char szFile[BUFPATHLEN];
3858 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3859 UINT i;
3860 char_u **fnames;
3861 POINT pt;
3862 int_u modifiers = 0;
3863
3864 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3865
3866 /* Obtain dropped position */
3867 DragQueryPoint(hDrop, &pt);
3868 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3869
3870 reset_VIsual();
3871
3872 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3873
3874 if (fnames != NULL)
3875 for (i = 0; i < cFiles; ++i)
3876 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003877#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3879 fnames[i] = utf16_to_enc(wszFile, NULL);
3880 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003881#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 {
3883 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3884 fnames[i] = vim_strsave((char_u *)szFile);
3885 }
3886 }
3887
3888 DragFinish(hDrop);
3889
3890 if (fnames != NULL)
3891 {
3892 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3893 modifiers |= MOUSE_SHIFT;
3894 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3895 modifiers |= MOUSE_CTRL;
3896 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3897 modifiers |= MOUSE_ALT;
3898
3899 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3900
3901 s_need_activate = TRUE;
3902 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903}
3904
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 static int
3906_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003907 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 HWND hwndCtl,
3909 UINT code,
3910 int pos)
3911{
3912 static UINT prev_code = 0; /* code of previous call */
3913 scrollbar_T *sb, *sb_info;
3914 long val;
3915 int dragging = FALSE;
3916 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 SCROLLINFO si;
3918
3919 si.cbSize = sizeof(si);
3920 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921
3922 sb = gui_mswin_find_scrollbar(hwndCtl);
3923 if (sb == NULL)
3924 return 0;
3925
3926 if (sb->wp != NULL) /* Left or right scrollbar */
3927 {
3928 /*
3929 * Careful: need to get scrollbar info out of first (left) scrollbar
3930 * for window, but keep real scrollbar too because we must pass it to
3931 * gui_drag_scrollbar().
3932 */
3933 sb_info = &sb->wp->w_scrollbars[0];
3934 }
3935 else /* Bottom scrollbar */
3936 sb_info = sb;
3937 val = sb_info->value;
3938
3939 switch (code)
3940 {
3941 case SB_THUMBTRACK:
3942 val = pos;
3943 dragging = TRUE;
3944 if (sb->scroll_shift > 0)
3945 val <<= sb->scroll_shift;
3946 break;
3947 case SB_LINEDOWN:
3948 val++;
3949 break;
3950 case SB_LINEUP:
3951 val--;
3952 break;
3953 case SB_PAGEDOWN:
3954 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3955 break;
3956 case SB_PAGEUP:
3957 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3958 break;
3959 case SB_TOP:
3960 val = 0;
3961 break;
3962 case SB_BOTTOM:
3963 val = sb_info->max;
3964 break;
3965 case SB_ENDSCROLL:
3966 if (prev_code == SB_THUMBTRACK)
3967 {
3968 /*
3969 * "pos" only gives us 16-bit data. In case of large file,
3970 * use GetScrollPos() which returns 32-bit. Unfortunately it
3971 * is not valid while the scrollbar is being dragged.
3972 */
3973 val = GetScrollPos(hwndCtl, SB_CTL);
3974 if (sb->scroll_shift > 0)
3975 val <<= sb->scroll_shift;
3976 }
3977 break;
3978
3979 default:
3980 /* TRACE("Unknown scrollbar event %d\n", code); */
3981 return 0;
3982 }
3983 prev_code = code;
3984
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003985 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3986 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003987
3988 /*
3989 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3990 */
3991 if (sb->wp != NULL)
3992 {
3993 scrollbar_T *sba = sb->wp->w_scrollbars;
3994 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003996 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003997 }
3998
3999 /* Don't let us be interrupted here by another message. */
4000 s_busy_processing = TRUE;
4001
4002 /* When "allow_scrollbar" is FALSE still need to remember the new
4003 * position, but don't actually scroll by setting "dont_scroll". */
4004 dont_scroll = !allow_scrollbar;
4005
Bram Moolenaara338adc2018-01-31 20:51:47 +01004006 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004008 mch_enable_flush();
4009 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010
4011 s_busy_processing = FALSE;
4012 dont_scroll = dont_scroll_save;
4013
4014 return 0;
4015}
4016
4017
4018/*
4019 * Get command line arguments.
4020 * Use "prog" as the name of the program and "cmdline" as the arguments.
4021 * Copy the arguments to allocated memory.
4022 * Return the number of arguments (including program name).
4023 * Return pointers to the arguments in "argvp". Memory is allocated with
4024 * malloc(), use free() instead of vim_free().
4025 * Return pointer to buffer in "tofree".
4026 * Returns zero when out of memory.
4027 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004028 int
4029get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4030{
4031 int i;
4032 char *p;
4033 char *progp;
4034 char *pnew = NULL;
4035 char *newcmdline;
4036 int inquote;
4037 int argc;
4038 char **argv = NULL;
4039 int round;
4040
4041 *tofree = NULL;
4042
4043#ifdef FEAT_MBYTE
4044 /* Try using the Unicode version first, it takes care of conversion when
4045 * 'encoding' is changed. */
4046 argc = get_cmd_argsW(&argv);
4047 if (argc != 0)
4048 goto done;
4049#endif
4050
4051 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4052 * non-space. */
4053 p = strrchr(prog, '.');
4054 if (p != NULL)
4055 *p = NUL;
4056 for (progp = prog; *progp == ' '; ++progp)
4057 ;
4058
4059 /* The command line is copied to allocated memory, so that we can change
4060 * it. Add the size of the string, the separating NUL and a terminating
4061 * NUL. */
4062 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4063 if (newcmdline == NULL)
4064 return 0;
4065
4066 /*
4067 * First round: count the number of arguments ("pnew" == NULL).
4068 * Second round: produce the arguments.
4069 */
4070 for (round = 1; round <= 2; ++round)
4071 {
4072 /* First argument is the program name. */
4073 if (pnew != NULL)
4074 {
4075 argv[0] = pnew;
4076 strcpy(pnew, progp);
4077 pnew += strlen(pnew);
4078 *pnew++ = NUL;
4079 }
4080
4081 /*
4082 * Isolate each argument and put it in argv[].
4083 */
4084 p = cmdline;
4085 argc = 1;
4086 while (*p != NUL)
4087 {
4088 inquote = FALSE;
4089 if (pnew != NULL)
4090 argv[argc] = pnew;
4091 ++argc;
4092 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4093 {
4094 /* Backslashes are only special when followed by a double
4095 * quote. */
4096 i = (int)strspn(p, "\\");
4097 if (p[i] == '"')
4098 {
4099 /* Halve the number of backslashes. */
4100 if (i > 1 && pnew != NULL)
4101 {
4102 vim_memset(pnew, '\\', i / 2);
4103 pnew += i / 2;
4104 }
4105
4106 /* Even nr of backslashes toggles quoting, uneven copies
4107 * the double quote. */
4108 if ((i & 1) == 0)
4109 inquote = !inquote;
4110 else if (pnew != NULL)
4111 *pnew++ = '"';
4112 p += i + 1;
4113 }
4114 else if (i > 0)
4115 {
4116 /* Copy span of backslashes unmodified. */
4117 if (pnew != NULL)
4118 {
4119 vim_memset(pnew, '\\', i);
4120 pnew += i;
4121 }
4122 p += i;
4123 }
4124 else
4125 {
4126 if (pnew != NULL)
4127 *pnew++ = *p;
4128#ifdef FEAT_MBYTE
4129 /* Can't use mb_* functions, because 'encoding' is not
4130 * initialized yet here. */
4131 if (IsDBCSLeadByte(*p))
4132 {
4133 ++p;
4134 if (pnew != NULL)
4135 *pnew++ = *p;
4136 }
4137#endif
4138 ++p;
4139 }
4140 }
4141
4142 if (pnew != NULL)
4143 *pnew++ = NUL;
4144 while (*p == ' ' || *p == '\t')
4145 ++p; /* advance until a non-space */
4146 }
4147
4148 if (round == 1)
4149 {
4150 argv = (char **)malloc((argc + 1) * sizeof(char *));
4151 if (argv == NULL )
4152 {
4153 free(newcmdline);
4154 return 0; /* malloc error */
4155 }
4156 pnew = newcmdline;
4157 *tofree = newcmdline;
4158 }
4159 }
4160
4161#ifdef FEAT_MBYTE
4162done:
4163#endif
4164 argv[argc] = NULL; /* NULL-terminated list */
4165 *argvp = argv;
4166 return argc;
4167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169#ifdef FEAT_XPM_W32
4170# include "xpm_w32.h"
4171#endif
4172
4173#ifdef PROTO
4174# define WINAPI
4175#endif
4176
4177#ifdef __MINGW32__
4178/*
4179 * Add a lot of missing defines.
4180 * They are not always missing, we need the #ifndef's.
4181 */
4182# ifndef _cdecl
4183# define _cdecl
4184# endif
4185# ifndef IsMinimized
4186# define IsMinimized(hwnd) IsIconic(hwnd)
4187# endif
4188# ifndef IsMaximized
4189# define IsMaximized(hwnd) IsZoomed(hwnd)
4190# endif
4191# ifndef SelectFont
4192# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4193# endif
4194# ifndef GetStockBrush
4195# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4196# endif
4197# ifndef DeleteBrush
4198# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4199# endif
4200
4201# ifndef HANDLE_WM_RBUTTONDBLCLK
4202# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4203 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4204# endif
4205# ifndef HANDLE_WM_MBUTTONUP
4206# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4207 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4208# endif
4209# ifndef HANDLE_WM_MBUTTONDBLCLK
4210# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4211 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4212# endif
4213# ifndef HANDLE_WM_LBUTTONDBLCLK
4214# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4215 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4216# endif
4217# ifndef HANDLE_WM_RBUTTONDOWN
4218# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4219 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4220# endif
4221# ifndef HANDLE_WM_MOUSEMOVE
4222# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4223 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4224# endif
4225# ifndef HANDLE_WM_RBUTTONUP
4226# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4227 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4228# endif
4229# ifndef HANDLE_WM_MBUTTONDOWN
4230# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4231 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4232# endif
4233# ifndef HANDLE_WM_LBUTTONUP
4234# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4235 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4236# endif
4237# ifndef HANDLE_WM_LBUTTONDOWN
4238# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4239 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4240# endif
4241# ifndef HANDLE_WM_SYSCHAR
4242# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4243 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4244# endif
4245# ifndef HANDLE_WM_ACTIVATEAPP
4246# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4247 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4248# endif
4249# ifndef HANDLE_WM_WINDOWPOSCHANGING
4250# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4251 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4252# endif
4253# ifndef HANDLE_WM_VSCROLL
4254# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4255 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4256# endif
4257# ifndef HANDLE_WM_SETFOCUS
4258# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4259 ((fn)((hwnd), (HWND)(wParam)), 0L)
4260# endif
4261# ifndef HANDLE_WM_KILLFOCUS
4262# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4263 ((fn)((hwnd), (HWND)(wParam)), 0L)
4264# endif
4265# ifndef HANDLE_WM_HSCROLL
4266# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4267 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4268# endif
4269# ifndef HANDLE_WM_DROPFILES
4270# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4271 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4272# endif
4273# ifndef HANDLE_WM_CHAR
4274# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4275 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4276# endif
4277# ifndef HANDLE_WM_SYSDEADCHAR
4278# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4279 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4280# endif
4281# ifndef HANDLE_WM_DEADCHAR
4282# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4283 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4284# endif
4285#endif /* __MINGW32__ */
4286
4287
4288/* Some parameters for tearoff menus. All in pixels. */
4289#define TEAROFF_PADDING_X 2
4290#define TEAROFF_BUTTON_PAD_X 8
4291#define TEAROFF_MIN_WIDTH 200
4292#define TEAROFF_SUBMENU_LABEL ">>"
4293#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4294
4295
4296/* For the Intellimouse: */
4297#ifndef WM_MOUSEWHEEL
4298#define WM_MOUSEWHEEL 0x20a
4299#endif
4300
4301
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004302#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303# define ID_BEVAL_TOOLTIP 200
4304# define BEVAL_TEXT_LEN MAXPATHL
4305
Bram Moolenaar167632f2010-05-26 21:42:54 +02004306#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004307/* Work around old versions of basetsd.h which wrongly declares
4308 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004309# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004310# define UINT_PTR UINT
4311#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004312
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004313static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4314static void delete_tooltip(BalloonEval *beval);
4315static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004318static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004320
Bram Moolenaar82881492012-11-20 16:53:39 +01004321
4322/* cproto fails on missing include files */
4323#ifndef PROTO
4324
Bram Moolenaar45360022005-07-21 21:08:21 +00004325/*
4326 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004327 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004328 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004329# include <pshpack1.h>
4330
4331#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004332
4333typedef struct _DllVersionInfo
4334{
4335 DWORD cbSize;
4336 DWORD dwMajorVersion;
4337 DWORD dwMinorVersion;
4338 DWORD dwBuildNumber;
4339 DWORD dwPlatformID;
4340} DLLVERSIONINFO;
4341
Bram Moolenaar82881492012-11-20 16:53:39 +01004342#ifndef PROTO
4343# include <poppack.h>
4344#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004345
Bram Moolenaar45360022005-07-21 21:08:21 +00004346typedef struct tagTOOLINFOA_NEW
4347{
4348 UINT cbSize;
4349 UINT uFlags;
4350 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004351 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004352 RECT rect;
4353 HINSTANCE hinst;
4354 LPSTR lpszText;
4355 LPARAM lParam;
4356} TOOLINFO_NEW;
4357
4358typedef struct tagNMTTDISPINFO_NEW
4359{
4360 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004361 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004362 char szText[80];
4363 HINSTANCE hinst;
4364 UINT uFlags;
4365 LPARAM lParam;
4366} NMTTDISPINFO_NEW;
4367
Bram Moolenaar45360022005-07-21 21:08:21 +00004368typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4369#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004370# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#endif
4372
Bram Moolenaar45360022005-07-21 21:08:21 +00004373#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004374# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004375#endif
4376
4377#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004378# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004379#endif
4380
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004381#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004382
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004383#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4384/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4385 * it here if LPNMTTDISPINFO isn't defined.
4386 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4387 * _MSC_VER. */
4388# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4389typedef struct tagNMTTDISPINFOA {
4390 NMHDR hdr;
4391 LPSTR lpszText;
4392 char szText[80];
4393 HINSTANCE hinst;
4394 UINT uFlags;
4395 LPARAM lParam;
4396} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4397# define LPNMTTDISPINFO LPNMTTDISPINFOA
4398
4399# ifdef FEAT_MBYTE
4400typedef struct tagNMTTDISPINFOW {
4401 NMHDR hdr;
4402 LPWSTR lpszText;
4403 WCHAR szText[80];
4404 HINSTANCE hinst;
4405 UINT uFlags;
4406 LPARAM lParam;
4407} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4408# endif
4409# endif
4410#endif
4411
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004412#ifndef TTN_GETDISPINFOW
4413# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4414#endif
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416/* Local variables: */
4417
4418#ifdef FEAT_MENU
4419static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422/*
4423 * Use the system font for dialogs and tear-off menus. Remove this line to
4424 * use DLG_FONT_NAME.
4425 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004426#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428#define VIM_NAME "vim"
4429#define VIM_CLASS "Vim"
4430#define VIM_CLASSW L"Vim"
4431
4432/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4433 * thus there should be room for every dialog. For tearoffs it's made bigger
4434 * when needed. */
4435#define DLG_ALLOC_SIZE 16 * 1024
4436
4437/*
4438 * stuff for dialogs, menus, tearoffs etc.
4439 */
4440static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004441#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444static PWORD
4445add_dialog_element(
4446 PWORD p,
4447 DWORD lStyle,
4448 WORD x,
4449 WORD y,
4450 WORD w,
4451 WORD h,
4452 WORD Id,
4453 WORD clss,
4454 const char *caption);
4455static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004456static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004457#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460static void get_dialog_font_metrics(void);
4461
4462static int dialog_default_button = -1;
4463
4464/* Intellimouse support */
4465static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4468#ifdef FEAT_TOOLBAR
4469static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004470static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471static int get_toolbar_bitmap(vimmenu_T *menu);
4472#endif
4473
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004474#ifdef FEAT_GUI_TABLINE
4475static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004476static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004477#endif
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_MBYTE_IME
4480static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4481static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4482#endif
4483#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4484# ifdef NOIME
4485typedef struct tagCOMPOSITIONFORM {
4486 DWORD dwStyle;
4487 POINT ptCurrentPos;
4488 RECT rcArea;
4489} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4490typedef HANDLE HIMC;
4491# endif
4492
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004493static HINSTANCE hLibImm = NULL;
4494static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4495static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4496static HIMC (WINAPI *pImmGetContext)(HWND);
4497static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4498static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4499static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4500static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4501static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4502static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4503static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4504static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004505static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506static void dyn_imm_load(void);
4507#else
4508# define pImmGetCompositionStringA ImmGetCompositionStringA
4509# define pImmGetCompositionStringW ImmGetCompositionStringW
4510# define pImmGetContext ImmGetContext
4511# define pImmAssociateContext ImmAssociateContext
4512# define pImmReleaseContext ImmReleaseContext
4513# define pImmGetOpenStatus ImmGetOpenStatus
4514# define pImmSetOpenStatus ImmSetOpenStatus
4515# define pImmGetCompositionFont ImmGetCompositionFontA
4516# define pImmSetCompositionFont ImmSetCompositionFontA
4517# define pImmSetCompositionWindow ImmSetCompositionWindow
4518# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004519# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#endif
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#ifdef FEAT_MENU
4523/*
4524 * Figure out how high the menu bar is at the moment.
4525 */
4526 static int
4527gui_mswin_get_menu_height(
4528 int fix_window) /* If TRUE, resize window if menu height changed */
4529{
4530 static int old_menu_height = -1;
4531
4532 RECT rc1, rc2;
4533 int num;
4534 int menu_height;
4535
4536 if (gui.menu_is_active)
4537 num = GetMenuItemCount(s_menuBar);
4538 else
4539 num = 0;
4540
4541 if (num == 0)
4542 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004543 else if (IsMinimized(s_hwnd))
4544 {
4545 /* The height of the menu cannot be determined while the window is
4546 * minimized. Take the previous height if the menu is changed in that
4547 * state, to avoid that Vim's vertical window size accidentally
4548 * increases due to the unaccounted-for menu height. */
4549 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else
4552 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004553 /*
4554 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4555 * seem to have been set yet, so menu wraps in default window
4556 * width which is very narrow. Instead just return height of a
4557 * single menu item. Will still be wrong when the menu really
4558 * should wrap over more than one line.
4559 */
4560 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4561 if (gui.starting)
4562 menu_height = rc1.bottom - rc1.top + 1;
4563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004565 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4566 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 }
4569
4570 if (fix_window && menu_height != old_menu_height)
4571 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004572 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004574 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 return menu_height;
4577}
4578#endif /*FEAT_MENU*/
4579
4580
4581/*
4582 * Setup for the Intellimouse
4583 */
4584 static void
4585init_mouse_wheel(void)
4586{
4587
4588#ifndef SPI_GETWHEELSCROLLLINES
4589# define SPI_GETWHEELSCROLLLINES 104
4590#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004591#ifndef SPI_SETWHEELSCROLLLINES
4592# define SPI_SETWHEELSCROLLLINES 105
4593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4596#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4597#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4598#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 mouse_scroll_lines = 3; /* reasonable default */
4601
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004602 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4603 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4604 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
4607
4608/* Intellimouse wheel handler */
4609 static void
4610_OnMouseWheel(
4611 HWND hwnd,
4612 short zDelta)
4613{
4614/* Treat a mouse wheel event as if it were a scroll request */
4615 int i;
4616 int size;
4617 HWND hwndCtl;
4618
4619 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4620 {
4621 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4622 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4623 }
4624 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4625 {
4626 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4627 size = curwin->w_scrollbars[SBAR_LEFT].size;
4628 }
4629 else
4630 return;
4631
4632 size = curwin->w_height;
4633 if (mouse_scroll_lines == 0)
4634 init_mouse_wheel();
4635
Bram Moolenaara338adc2018-01-31 20:51:47 +01004636 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (mouse_scroll_lines > 0
4638 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4639 {
4640 for (i = mouse_scroll_lines; i > 0; --i)
4641 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4642 }
4643 else
4644 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004645 mch_enable_flush();
4646 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
4648
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004649#ifdef USE_SYSMENU_FONT
4650/*
4651 * Get Menu Font.
4652 * Return OK or FAIL.
4653 */
4654 static int
4655gui_w32_get_menu_font(LOGFONT *lf)
4656{
4657 NONCLIENTMETRICS nm;
4658
4659 nm.cbSize = sizeof(NONCLIENTMETRICS);
4660 if (!SystemParametersInfo(
4661 SPI_GETNONCLIENTMETRICS,
4662 sizeof(NONCLIENTMETRICS),
4663 &nm,
4664 0))
4665 return FAIL;
4666 *lf = nm.lfMenuFont;
4667 return OK;
4668}
4669#endif
4670
4671
4672#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4673/*
4674 * Set the GUI tabline font to the system menu font
4675 */
4676 static void
4677set_tabline_font(void)
4678{
4679 LOGFONT lfSysmenu;
4680 HFONT font;
4681 HWND hwnd;
4682 HDC hdc;
4683 HFONT hfntOld;
4684 TEXTMETRIC tm;
4685
4686 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4687 return;
4688
4689 font = CreateFontIndirect(&lfSysmenu);
4690
4691 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4692
4693 /*
4694 * Compute the height of the font used for the tab text
4695 */
4696 hwnd = GetDesktopWindow();
4697 hdc = GetWindowDC(hwnd);
4698 hfntOld = SelectFont(hdc, font);
4699
4700 GetTextMetrics(hdc, &tm);
4701
4702 SelectFont(hdc, hfntOld);
4703 ReleaseDC(hwnd, hdc);
4704
4705 /*
4706 * The space used by the tab border and the space between the tab label
4707 * and the tab border is included as 7.
4708 */
4709 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4710}
4711#endif
4712
Bram Moolenaar520470a2005-06-16 21:59:56 +00004713/*
4714 * Invoked when a setting was changed.
4715 */
4716 static LRESULT CALLBACK
4717_OnSettingChange(UINT n)
4718{
4719 if (n == SPI_SETWHEELSCROLLLINES)
4720 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4721 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004722#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4723 if (n == SPI_SETNONCLIENTMETRICS)
4724 set_tabline_font();
4725#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004726 return 0;
4727}
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#ifdef FEAT_NETBEANS_INTG
4730 static void
4731_OnWindowPosChanged(
4732 HWND hwnd,
4733 const LPWINDOWPOS lpwpos)
4734{
4735 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004736 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4739 || lpwpos->cx != cx || lpwpos->cy != cy))
4740 {
4741 x = lpwpos->x;
4742 y = lpwpos->y;
4743 cx = lpwpos->cx;
4744 cy = lpwpos->cy;
4745 netbeans_frame_moved(x, y);
4746 }
4747 /* Allow to send WM_SIZE and WM_MOVE */
4748 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4749}
4750#endif
4751
4752 static int
4753_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 UINT fwSide,
4755 LPRECT lprc)
4756{
4757 int w, h;
4758 int valid_w, valid_h;
4759 int w_offset, h_offset;
4760
4761 w = lprc->right - lprc->left;
4762 h = lprc->bottom - lprc->top;
4763 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4764 w_offset = w - valid_w;
4765 h_offset = h - valid_h;
4766
4767 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4768 || fwSide == WMSZ_BOTTOMLEFT)
4769 lprc->left += w_offset;
4770 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4771 || fwSide == WMSZ_BOTTOMRIGHT)
4772 lprc->right -= w_offset;
4773
4774 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4775 || fwSide == WMSZ_TOPRIGHT)
4776 lprc->top += h_offset;
4777 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4778 || fwSide == WMSZ_BOTTOMRIGHT)
4779 lprc->bottom -= h_offset;
4780 return TRUE;
4781}
4782
4783
4784
4785 static LRESULT CALLBACK
4786_WndProc(
4787 HWND hwnd,
4788 UINT uMsg,
4789 WPARAM wParam,
4790 LPARAM lParam)
4791{
4792 /*
4793 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4794 hwnd, uMsg, wParam, lParam);
4795 */
4796
4797 HandleMouseHide(uMsg, lParam);
4798
4799 s_uMsg = uMsg;
4800 s_wParam = wParam;
4801 s_lParam = lParam;
4802
4803 switch (uMsg)
4804 {
4805 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4806 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4807 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4808 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4809 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4810 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4811 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4812 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4813 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4814#ifdef FEAT_MENU
4815 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4816#endif
4817 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4818 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4819 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4820 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4821 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4822 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4823 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4824 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4825 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4826#ifdef FEAT_NETBEANS_INTG
4827 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4828#endif
4829
Bram Moolenaarafa24992006-03-27 20:58:26 +00004830#ifdef FEAT_GUI_TABLINE
4831 case WM_RBUTTONUP:
4832 {
4833 if (gui_mch_showing_tabline())
4834 {
4835 POINT pt;
4836 RECT rect;
4837
4838 /*
4839 * If the cursor is on the tabline, display the tab menu
4840 */
4841 GetCursorPos((LPPOINT)&pt);
4842 GetWindowRect(s_textArea, &rect);
4843 if (pt.y < rect.top)
4844 {
4845 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004846 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004847 }
4848 }
4849 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004851 case WM_LBUTTONDBLCLK:
4852 {
4853 /*
4854 * If the user double clicked the tabline, create a new tab
4855 */
4856 if (gui_mch_showing_tabline())
4857 {
4858 POINT pt;
4859 RECT rect;
4860
4861 GetCursorPos((LPPOINT)&pt);
4862 GetWindowRect(s_textArea, &rect);
4863 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004864 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004865 }
4866 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4867 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004868#endif
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 case WM_QUERYENDSESSION: /* System wants to go down. */
4871 gui_shell_closed(); /* Will exit when no changed buffers. */
4872 return FALSE; /* Do NOT allow system to go down. */
4873
4874 case WM_ENDSESSION:
4875 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004878 return 0L;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 break;
4881
4882 case WM_CHAR:
4883 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4884 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004885 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return 0L;
4887
4888 case WM_SYSCHAR:
4889 /*
4890 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4891 * shortcut key, handle like a typed ALT key, otherwise call Windows
4892 * ALT key handling.
4893 */
4894#ifdef FEAT_MENU
4895 if ( !gui.menu_is_active
4896 || p_wak[0] == 'n'
4897 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4898 )
4899#endif
4900 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004901 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 return 0L;
4903 }
4904#ifdef FEAT_MENU
4905 else
4906 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4907#endif
4908
4909 case WM_SYSKEYUP:
4910#ifdef FEAT_MENU
4911 /* This used to be done only when menu is active: ALT key is used for
4912 * that. But that caused problems when menu is disabled and using
4913 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4914 * are received, mouse pointer remains hidden. */
4915 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4916#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919
4920 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 case WM_MOUSEWHEEL:
4924 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004925 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar520470a2005-06-16 21:59:56 +00004927 /* Notification for change in SystemParametersInfo() */
4928 case WM_SETTINGCHANGE:
4929 return _OnSettingChange((UINT)wParam);
4930
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004931#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 case WM_NOTIFY:
4933 switch (((LPNMHDR) lParam)->code)
4934 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004935# ifdef FEAT_MBYTE
4936 case TTN_GETDISPINFOW:
4937# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004938 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004940 LPNMHDR hdr = (LPNMHDR)lParam;
4941 char_u *str = NULL;
4942 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004943
Bram Moolenaard23a8232018-02-10 18:45:26 +01004944 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004945
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004946# ifdef FEAT_GUI_TABLINE
4947 if (gui_mch_showing_tabline()
4948 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004949 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004950 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004951 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004952 * Mouse is over the GUI tabline. Display the
4953 * tooltip for the tab under the cursor
4954 *
4955 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004956 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004957 GetCursorPos(&pt);
4958 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004959 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004960 TCHITTESTINFO htinfo;
4961 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004962
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004963 /*
4964 * Get the tab under the cursor
4965 */
4966 htinfo.pt.x = pt.x;
4967 htinfo.pt.y = pt.y;
4968 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4969 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004970 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004971 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004972
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004973 tp = find_tabpage(idx + 1);
4974 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004975 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004976 get_tabline_label(tp, TRUE);
4977 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004978 }
4979 }
4980 }
4981 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004982# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004983# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004984# ifdef FEAT_GUI_TABLINE
4985 else
4986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004988 UINT idButton;
4989 vimmenu_T *pMenu;
4990
4991 idButton = (UINT) hdr->idFrom;
4992 pMenu = gui_mswin_find_menu(root_menu, idButton);
4993 if (pMenu)
4994 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004996# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004997 if (str != NULL)
4998 {
4999# ifdef FEAT_MBYTE
5000 if (hdr->code == TTN_GETDISPINFOW)
5001 {
5002 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5003
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005004 /* Set the maximum width, this also enables using
5005 * \n for line break. */
5006 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5007 0, 500);
5008
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005009 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005010 lpdi->lpszText = tt_text;
5011 /* can't show tooltip if failed */
5012 }
5013 else
5014# endif
5015 {
5016 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5017
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005018 /* Set the maximum width, this also enables using
5019 * \n for line break. */
5020 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5021 0, 500);
5022
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005023 if (STRLEN(str) < sizeof(lpdi->szText)
5024 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005025 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005026 sizeof(lpdi->szText) - 1);
5027 else
5028 lpdi->lpszText = tt_text;
5029 }
5030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005033# ifdef FEAT_GUI_TABLINE
5034 case TCN_SELCHANGE:
5035 if (gui_mch_showing_tabline()
5036 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005037 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005038 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005039 return 0L;
5040 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005041 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005042
5043 case NM_RCLICK:
5044 if (gui_mch_showing_tabline()
5045 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005046 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005047 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005048 return 0L;
5049 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005050 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005053# ifdef FEAT_GUI_TABLINE
5054 if (gui_mch_showing_tabline()
5055 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5056 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 break;
5059 }
5060 break;
5061#endif
5062#if defined(MENUHINTS) && defined(FEAT_MENU)
5063 case WM_MENUSELECT:
5064 if (((UINT) HIWORD(wParam)
5065 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5066 == MF_HILITE
5067 && (State & CMDLINE) == 0)
5068 {
5069 UINT idButton;
5070 vimmenu_T *pMenu;
5071 static int did_menu_tip = FALSE;
5072
5073 if (did_menu_tip)
5074 {
5075 msg_clr_cmdline();
5076 setcursor();
5077 out_flush();
5078 did_menu_tip = FALSE;
5079 }
5080
5081 idButton = (UINT)LOWORD(wParam);
5082 pMenu = gui_mswin_find_menu(root_menu, idButton);
5083 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5084 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5085 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005086 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005088 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 setcursor();
5090 out_flush();
5091 did_menu_tip = TRUE;
5092 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005093 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 break;
5096#endif
5097 case WM_NCHITTEST:
5098 {
5099 LRESULT result;
5100 int x, y;
5101 int xPos = GET_X_LPARAM(lParam);
5102
5103 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5104 if (result == HTCLIENT)
5105 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005106#ifdef FEAT_GUI_TABLINE
5107 if (gui_mch_showing_tabline())
5108 {
5109 int yPos = GET_Y_LPARAM(lParam);
5110 RECT rct;
5111
5112 /* If the cursor is on the GUI tabline, don't process this
5113 * event */
5114 GetWindowRect(s_textArea, &rct);
5115 if (yPos < rct.top)
5116 return result;
5117 }
5118#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005119 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 xPos -= x;
5121
5122 if (xPos < 48) /* <VN> TODO should use system metric? */
5123 return HTBOTTOMLEFT;
5124 else
5125 return HTBOTTOMRIGHT;
5126 }
5127 else
5128 return result;
5129 }
5130 /* break; notreached */
5131
5132#ifdef FEAT_MBYTE_IME
5133 case WM_IME_NOTIFY:
5134 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5135 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005136 return 1L;
5137
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 case WM_IME_COMPOSITION:
5139 if (!_OnImeComposition(hwnd, wParam, lParam))
5140 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005141 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142#endif
5143
5144 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005146 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 _OnFindRepl();
5149 }
5150#endif
5151 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5152 }
5153
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005154 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155}
5156
5157/*
5158 * End of call-back routines
5159 */
5160
5161/* parent window, if specified with -P */
5162HWND vim_parent_hwnd = NULL;
5163
5164 static BOOL CALLBACK
5165FindWindowTitle(HWND hwnd, LPARAM lParam)
5166{
5167 char buf[2048];
5168 char *title = (char *)lParam;
5169
5170 if (GetWindowText(hwnd, buf, sizeof(buf)))
5171 {
5172 if (strstr(buf, title) != NULL)
5173 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005174 /* Found it. Store the window ref. and quit searching if MDI
5175 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005177 if (vim_parent_hwnd != NULL)
5178 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 }
5181 return TRUE; /* continue searching */
5182}
5183
5184/*
5185 * Invoked for '-P "title"' argument: search for parent application to open
5186 * our window in.
5187 */
5188 void
5189gui_mch_set_parent(char *title)
5190{
5191 EnumWindows(FindWindowTitle, (LPARAM)title);
5192 if (vim_parent_hwnd == NULL)
5193 {
5194 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5195 mch_exit(2);
5196 }
5197}
5198
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 static void
5201ole_error(char *arg)
5202{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005203 char buf[IOSIZE];
5204
5205 /* Can't use EMSG() here, we have not finished initialisation yet. */
5206 vim_snprintf(buf, IOSIZE,
5207 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5208 arg);
5209 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213/*
5214 * Parse the GUI related command-line arguments. Any arguments used are
5215 * deleted from argv, and *argc is decremented accordingly. This is called
5216 * when vim is started, whether or not the GUI has been started.
5217 */
5218 void
5219gui_mch_prepare(int *argc, char **argv)
5220{
5221 int silent = FALSE;
5222 int idx;
5223
5224 /* Check for special OLE command line parameters */
5225 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5226 {
5227 /* Check for a "-silent" argument first. */
5228 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5229 && (argv[2][0] == '-' || argv[2][0] == '/'))
5230 {
5231 silent = TRUE;
5232 idx = 2;
5233 }
5234 else
5235 idx = 1;
5236
5237 /* Register Vim as an OLE Automation server */
5238 if (STRICMP(argv[idx] + 1, "register") == 0)
5239 {
5240#ifdef FEAT_OLE
5241 RegisterMe(silent);
5242 mch_exit(0);
5243#else
5244 if (!silent)
5245 ole_error("register");
5246 mch_exit(2);
5247#endif
5248 }
5249
5250 /* Unregister Vim as an OLE Automation server */
5251 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5252 {
5253#ifdef FEAT_OLE
5254 UnregisterMe(!silent);
5255 mch_exit(0);
5256#else
5257 if (!silent)
5258 ole_error("unregister");
5259 mch_exit(2);
5260#endif
5261 }
5262
5263 /* Ignore an -embedding argument. It is only relevant if the
5264 * application wants to treat the case when it is started manually
5265 * differently from the case where it is started via automation (and
5266 * we don't).
5267 */
5268 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5269 {
5270#ifdef FEAT_OLE
5271 *argc = 1;
5272#else
5273 ole_error("embedding");
5274 mch_exit(2);
5275#endif
5276 }
5277 }
5278
5279#ifdef FEAT_OLE
5280 {
5281 int bDoRestart = FALSE;
5282
5283 InitOLE(&bDoRestart);
5284 /* automatically exit after registering */
5285 if (bDoRestart)
5286 mch_exit(0);
5287 }
5288#endif
5289
5290#ifdef FEAT_NETBEANS_INTG
5291 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005292 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 int arg;
5294
5295 for (arg = 1; arg < *argc; arg++)
5296 if (strncmp("-nb", argv[arg], 3) == 0)
5297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 netbeansArg = argv[arg];
5299 mch_memmove(&argv[arg], &argv[arg + 1],
5300 (--*argc - arg) * sizeof(char *));
5301 argv[*argc] = NULL;
5302 break; /* enough? */
5303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306}
5307
5308/*
5309 * Initialise the GUI. Create all the windows, set up all the call-backs
5310 * etc.
5311 */
5312 int
5313gui_mch_init(void)
5314{
5315 const char szVimWndClass[] = VIM_CLASS;
5316 const char szTextAreaClass[] = "VimTextArea";
5317 WNDCLASS wndclass;
5318#ifdef FEAT_MBYTE
5319 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005320 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 WNDCLASSW wndclassw;
5322#endif
5323#ifdef GLOBAL_IME
5324 ATOM atom;
5325#endif
5326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 /* Return here if the window was already opened (happens when
5328 * gui_mch_dialog() is called early). */
5329 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005330 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 /*
5333 * Load the tearoff bitmap
5334 */
5335#ifdef FEAT_TEAROFF
5336 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5337#endif
5338
5339 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5340 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5341#ifdef FEAT_MENU
5342 gui.menu_height = 0; /* Windows takes care of this */
5343#endif
5344 gui.border_width = 0;
5345
5346 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5347
5348#ifdef FEAT_MBYTE
5349 /* First try using the wide version, so that we can use any title.
5350 * Otherwise only characters in the active codepage will work. */
5351 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5352 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005353 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 wndclassw.lpfnWndProc = _WndProc;
5355 wndclassw.cbClsExtra = 0;
5356 wndclassw.cbWndExtra = 0;
5357 wndclassw.hInstance = s_hinst;
5358 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5359 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5360 wndclassw.hbrBackground = s_brush;
5361 wndclassw.lpszMenuName = NULL;
5362 wndclassw.lpszClassName = szVimWndClassW;
5363
5364 if ((
5365#ifdef GLOBAL_IME
5366 atom =
5367#endif
5368 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005369 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 else
5371 wide_WindowProc = TRUE;
5372 }
5373
5374 if (!wide_WindowProc)
5375#endif
5376
5377 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5378 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005379 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 wndclass.lpfnWndProc = _WndProc;
5381 wndclass.cbClsExtra = 0;
5382 wndclass.cbWndExtra = 0;
5383 wndclass.hInstance = s_hinst;
5384 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5385 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5386 wndclass.hbrBackground = s_brush;
5387 wndclass.lpszMenuName = NULL;
5388 wndclass.lpszClassName = szVimWndClass;
5389
5390 if ((
5391#ifdef GLOBAL_IME
5392 atom =
5393#endif
5394 RegisterClass(&wndclass)) == 0)
5395 return FAIL;
5396 }
5397
5398 if (vim_parent_hwnd != NULL)
5399 {
5400#ifdef HAVE_TRY_EXCEPT
5401 __try
5402 {
5403#endif
5404 /* Open inside the specified parent window.
5405 * TODO: last argument should point to a CLIENTCREATESTRUCT
5406 * structure. */
5407 s_hwnd = CreateWindowEx(
5408 WS_EX_MDICHILD,
5409 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005410 WS_OVERLAPPEDWINDOW | WS_CHILD
5411 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5413 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5414 100, /* Any value will do */
5415 100, /* Any value will do */
5416 vim_parent_hwnd, NULL,
5417 s_hinst, NULL);
5418#ifdef HAVE_TRY_EXCEPT
5419 }
5420 __except(EXCEPTION_EXECUTE_HANDLER)
5421 {
5422 /* NOP */
5423 }
5424#endif
5425 if (s_hwnd == NULL)
5426 {
5427 EMSG(_("E672: Unable to open window inside MDI application"));
5428 mch_exit(2);
5429 }
5430 }
5431 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005432 {
5433 /* If the provided windowid is not valid reset it to zero, so that it
5434 * is ignored and we open our own window. */
5435 if (IsWindow((HWND)win_socket_id) <= 0)
5436 win_socket_id = 0;
5437
5438 /* Create a window. If win_socket_id is not zero without border and
5439 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005441 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005442 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5443 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005444 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5445 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5446 100, /* Any value will do */
5447 100, /* Any value will do */
5448 NULL, NULL,
5449 s_hinst, NULL);
5450 if (s_hwnd != NULL && win_socket_id != 0)
5451 {
5452 SetParent(s_hwnd, (HWND)win_socket_id);
5453 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5454 }
5455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456
5457 if (s_hwnd == NULL)
5458 return FAIL;
5459
5460#ifdef GLOBAL_IME
5461 global_ime_init(atom, s_hwnd);
5462#endif
5463#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5464 dyn_imm_load();
5465#endif
5466
5467 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005468#ifdef FEAT_MBYTE
5469 if (wide_WindowProc)
5470 {
5471 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5472 {
5473 wndclassw.style = CS_OWNDC;
5474 wndclassw.lpfnWndProc = _TextAreaWndProc;
5475 wndclassw.cbClsExtra = 0;
5476 wndclassw.cbWndExtra = 0;
5477 wndclassw.hInstance = s_hinst;
5478 wndclassw.hIcon = NULL;
5479 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5480 wndclassw.hbrBackground = NULL;
5481 wndclassw.lpszMenuName = NULL;
5482 wndclassw.lpszClassName = szTextAreaClassW;
5483
5484 if (RegisterClassW(&wndclassw) == 0)
5485 return FAIL;
5486 }
5487 }
5488 else
5489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5491 {
5492 wndclass.style = CS_OWNDC;
5493 wndclass.lpfnWndProc = _TextAreaWndProc;
5494 wndclass.cbClsExtra = 0;
5495 wndclass.cbWndExtra = 0;
5496 wndclass.hInstance = s_hinst;
5497 wndclass.hIcon = NULL;
5498 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5499 wndclass.hbrBackground = NULL;
5500 wndclass.lpszMenuName = NULL;
5501 wndclass.lpszClassName = szTextAreaClass;
5502
5503 if (RegisterClass(&wndclass) == 0)
5504 return FAIL;
5505 }
5506 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005507 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 szTextAreaClass, "Vim text area",
5509 WS_CHILD | WS_VISIBLE, 0, 0,
5510 100, /* Any value will do for now */
5511 100, /* Any value will do for now */
5512 s_hwnd, NULL,
5513 s_hinst, NULL);
5514
5515 if (s_textArea == NULL)
5516 return FAIL;
5517
Bram Moolenaar20321902016-02-17 12:30:17 +01005518#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005519 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5520 {
5521 HANDLE hIcon = NULL;
5522
5523 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005524 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005525 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005526#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#ifdef FEAT_MENU
5529 s_menuBar = CreateMenu();
5530#endif
5531 s_hdc = GetDC(s_textArea);
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 /* Do we need to bother with this? */
5536 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5537
5538 /* Get background/foreground colors from the system */
5539 gui_mch_def_colors();
5540
5541 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5542 * file) */
5543 set_normal_colors();
5544
5545 /*
5546 * Check that none of the colors are the same as the background color.
5547 * Then store the current values as the defaults.
5548 */
5549 gui_check_colors();
5550 gui.def_norm_pixel = gui.norm_pixel;
5551 gui.def_back_pixel = gui.back_pixel;
5552
5553 /* Get the colors for the highlight groups (gui_check_colors() might have
5554 * changed them) */
5555 highlight_gui_started();
5556
5557 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005558 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005560 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562 /*
5563 * Set up for Intellimouse processing
5564 */
5565 init_mouse_wheel();
5566
5567 /*
5568 * compute a couple of metrics used for the dialogs
5569 */
5570 get_dialog_font_metrics();
5571#ifdef FEAT_TOOLBAR
5572 /*
5573 * Create the toolbar
5574 */
5575 initialise_toolbar();
5576#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005577#ifdef FEAT_GUI_TABLINE
5578 /*
5579 * Create the tabline
5580 */
5581 initialise_tabline();
5582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583#ifdef MSWIN_FIND_REPLACE
5584 /*
5585 * Initialise the dialog box stuff
5586 */
5587 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5588
5589 /* Initialise the struct */
5590 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005591 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005593 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5595 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5596 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005597# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005598 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5599 s_findrep_struct_w.lpstrFindWhat =
5600 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5601 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5602 s_findrep_struct_w.lpstrReplaceWith =
5603 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5604 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5605 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5606 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5607# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005610#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005611# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5612/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5613# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005614# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005615# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005616# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005617 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005618 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005619#endif
5620
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005621#ifdef FEAT_RENDER_OPTIONS
5622 if (p_rop)
5623 (void)gui_mch_set_rendering_options(p_rop);
5624#endif
5625
Bram Moolenaar748bf032005-02-02 23:04:36 +00005626theend:
5627 /* Display any pending error messages */
5628 display_errors();
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 return OK;
5631}
5632
5633/*
5634 * Get the size of the screen, taking position on multiple monitors into
5635 * account (if supported).
5636 */
5637 static void
5638get_work_area(RECT *spi_rect)
5639{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005640 HMONITOR mon;
5641 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005643 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005644 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005645 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005647 moninfo.cbSize = sizeof(MONITORINFO);
5648 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005650 *spi_rect = moninfo.rcWork;
5651 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 }
5653 }
5654 /* this is the old method... */
5655 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5656}
5657
5658/*
5659 * Set the size of the window to the given width and height in pixels.
5660 */
5661 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005662gui_mch_set_shellsize(
5663 int width,
5664 int height,
5665 int min_width UNUSED,
5666 int min_height UNUSED,
5667 int base_width UNUSED,
5668 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005669 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 RECT workarea_rect;
5672 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 WINDOWPLACEMENT wndpl;
5674
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005675 /* Try to keep window completely on screen. */
5676 /* Get position of the screen work area. This is the part that is not
5677 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 get_work_area(&workarea_rect);
5679
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005680 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005681 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 wndpl.length = sizeof(WINDOWPLACEMENT);
5683 GetWindowPlacement(s_hwnd, &wndpl);
5684
5685 /* Resizing a maximized window looks very strange, unzoom it first.
5686 * But don't do it when still starting up, it may have been requested in
5687 * the shortcut. */
5688 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5689 {
5690 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5691 /* Need to get the settings of the normal window. */
5692 GetWindowPlacement(s_hwnd, &wndpl);
5693 }
5694
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005696 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005697 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005698 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005699 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 + GetSystemMetrics(SM_CYCAPTION)
5701#ifdef FEAT_MENU
5702 + gui_mswin_get_menu_height(FALSE)
5703#endif
5704 ;
5705
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005706 /* The following should take care of keeping Vim on the same monitor, no
5707 * matter if the secondary monitor is left or right of the primary
5708 * monitor. */
5709 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5710 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005711
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005712 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005713 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005714 && wndpl.rcNormalPosition.right > workarea_rect.right)
5715 OffsetRect(&wndpl.rcNormalPosition,
5716 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005718 if ((direction & RESIZE_HOR)
5719 && wndpl.rcNormalPosition.left < workarea_rect.left)
5720 OffsetRect(&wndpl.rcNormalPosition,
5721 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
Bram Moolenaarafa24992006-03-27 20:58:26 +00005723 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005724 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5725 OffsetRect(&wndpl.rcNormalPosition,
5726 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005728 if ((direction & RESIZE_VERT)
5729 && wndpl.rcNormalPosition.top < workarea_rect.top)
5730 OffsetRect(&wndpl.rcNormalPosition,
5731 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732
5733 /* set window position - we should use SetWindowPlacement rather than
5734 * SetWindowPos as the MSDN docs say the coord systems returned by
5735 * these two are not compatible. */
5736 SetWindowPlacement(s_hwnd, &wndpl);
5737
5738 SetActiveWindow(s_hwnd);
5739 SetFocus(s_hwnd);
5740
5741#ifdef FEAT_MENU
5742 /* Menu may wrap differently now */
5743 gui_mswin_get_menu_height(!gui.starting);
5744#endif
5745}
5746
5747
5748 void
5749gui_mch_set_scrollbar_thumb(
5750 scrollbar_T *sb,
5751 long val,
5752 long size,
5753 long max)
5754{
5755 SCROLLINFO info;
5756
5757 sb->scroll_shift = 0;
5758 while (max > 32767)
5759 {
5760 max = (max + 1) >> 1;
5761 val >>= 1;
5762 size >>= 1;
5763 ++sb->scroll_shift;
5764 }
5765
5766 if (sb->scroll_shift > 0)
5767 ++size;
5768
5769 info.cbSize = sizeof(info);
5770 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5771 info.nPos = val;
5772 info.nMin = 0;
5773 info.nMax = max;
5774 info.nPage = size;
5775 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5776}
5777
5778
5779/*
5780 * Set the current text font.
5781 */
5782 void
5783gui_mch_set_font(GuiFont font)
5784{
5785 gui.currFont = font;
5786}
5787
5788
5789/*
5790 * Set the current text foreground color.
5791 */
5792 void
5793gui_mch_set_fg_color(guicolor_T color)
5794{
5795 gui.currFgColor = color;
5796}
5797
5798/*
5799 * Set the current text background color.
5800 */
5801 void
5802gui_mch_set_bg_color(guicolor_T color)
5803{
5804 gui.currBgColor = color;
5805}
5806
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005807/*
5808 * Set the current text special color.
5809 */
5810 void
5811gui_mch_set_sp_color(guicolor_T color)
5812{
5813 gui.currSpColor = color;
5814}
5815
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005816#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817/*
5818 * Multi-byte handling, originally by Sung-Hoon Baek.
5819 * First static functions (no prototypes generated).
5820 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005821# ifdef _MSC_VER
5822# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5823# endif
5824# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 * handle WM_IME_NOTIFY message
5828 */
5829 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005830_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 LRESULT lResult = 0;
5833 HIMC hImc;
5834
5835 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5836 return lResult;
5837 switch (dwCommand)
5838 {
5839 case IMN_SETOPENSTATUS:
5840 if (pImmGetOpenStatus(hImc))
5841 {
5842 pImmSetCompositionFont(hImc, &norm_logfont);
5843 im_set_position(gui.row, gui.col);
5844
5845 /* Disable langmap */
5846 State &= ~LANGMAP;
5847 if (State & INSERT)
5848 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 /* Unshown 'keymap' in status lines */
5851 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5852 {
5853 /* Save cursor position */
5854 int old_row = gui.row;
5855 int old_col = gui.col;
5856
5857 // This must be called here before
5858 // status_redraw_curbuf(), otherwise the mode
5859 // message may appear in the wrong position.
5860 showmode();
5861 status_redraw_curbuf();
5862 update_screen(0);
5863 /* Restore cursor position */
5864 gui.row = old_row;
5865 gui.col = old_col;
5866 }
5867#endif
5868 }
5869 }
5870 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005871 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 lResult = 0;
5873 break;
5874 }
5875 pImmReleaseContext(hWnd, hImc);
5876 return lResult;
5877}
5878
5879 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005880_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 char_u *ret;
5883 int len;
5884
5885 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5886 return 0;
5887
5888 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5889 if (ret != NULL)
5890 {
5891 add_to_input_buf_csi(ret, len);
5892 vim_free(ret);
5893 return 1;
5894 }
5895 return 0;
5896}
5897
5898/*
5899 * get the current composition string, in UCS-2; *lenp is the number of
5900 * *lenp is the number of Unicode characters.
5901 */
5902 static short_u *
5903GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5904{
5905 LONG ret;
5906 LPWSTR wbuf = NULL;
5907 char_u *buf;
5908
5909 if (!pImmGetContext)
5910 return NULL; /* no imm32.dll */
5911
5912 /* Try Unicode; this'll always work on NT regardless of codepage. */
5913 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5914 if (ret == 0)
5915 return NULL; /* empty */
5916
5917 if (ret > 0)
5918 {
5919 /* Allocate the requested buffer plus space for the NUL character. */
5920 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5921 if (wbuf != NULL)
5922 {
5923 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5924 *lenp = ret / sizeof(WCHAR);
5925 }
5926 return (short_u *)wbuf;
5927 }
5928
5929 /* ret < 0; we got an error, so try the ANSI version. This'll work
5930 * on 9x/ME, but only if the codepage happens to be set to whatever
5931 * we're inputting. */
5932 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5933 if (ret <= 0)
5934 return NULL; /* empty or error */
5935
5936 buf = alloc(ret);
5937 if (buf == NULL)
5938 return NULL;
5939 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5940
5941 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005942 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 vim_free(buf);
5944
5945 return (short_u *)wbuf;
5946}
5947
5948/*
5949 * void GetResultStr()
5950 *
5951 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5952 * get complete composition string
5953 */
5954 static char_u *
5955GetResultStr(HWND hwnd, int GCS, int *lenp)
5956{
5957 HIMC hIMC; /* Input context handle. */
5958 short_u *buf = NULL;
5959 char_u *convbuf = NULL;
5960
5961 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5962 return NULL;
5963
5964 /* Reads in the composition string. */
5965 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5966 if (buf == NULL)
5967 return NULL;
5968
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005969 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 pImmReleaseContext(hwnd, hIMC);
5971 vim_free(buf);
5972 return convbuf;
5973}
5974#endif
5975
5976/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005977#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
5979/*
5980 * set font to IM.
5981 */
5982 void
5983im_set_font(LOGFONT *lf)
5984{
5985 HIMC hImc;
5986
5987 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5988 {
5989 pImmSetCompositionFont(hImc, lf);
5990 pImmReleaseContext(s_hwnd, hImc);
5991 }
5992}
5993
5994/*
5995 * Notify cursor position to IM.
5996 */
5997 void
5998im_set_position(int row, int col)
5999{
6000 HIMC hImc;
6001
6002 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6003 {
6004 COMPOSITIONFORM cfs;
6005
6006 cfs.dwStyle = CFS_POINT;
6007 cfs.ptCurrentPos.x = FILL_X(col);
6008 cfs.ptCurrentPos.y = FILL_Y(row);
6009 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6010 pImmSetCompositionWindow(hImc, &cfs);
6011
6012 pImmReleaseContext(s_hwnd, hImc);
6013 }
6014}
6015
6016/*
6017 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6018 */
6019 void
6020im_set_active(int active)
6021{
6022 HIMC hImc;
6023 static HIMC hImcOld = (HIMC)0;
6024
6025 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6026 {
6027 if (p_imdisable)
6028 {
6029 if (hImcOld == (HIMC)0)
6030 {
6031 hImcOld = pImmGetContext(s_hwnd);
6032 if (hImcOld)
6033 pImmAssociateContext(s_hwnd, (HIMC)0);
6034 }
6035 active = FALSE;
6036 }
6037 else if (hImcOld != (HIMC)0)
6038 {
6039 pImmAssociateContext(s_hwnd, hImcOld);
6040 hImcOld = (HIMC)0;
6041 }
6042
6043 hImc = pImmGetContext(s_hwnd);
6044 if (hImc)
6045 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006046 /*
6047 * for Korean ime
6048 */
6049 HKL hKL = GetKeyboardLayout(0);
6050
6051 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6052 {
6053 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6054 static BOOL bSaved = FALSE;
6055
6056 if (active)
6057 {
6058 /* if we have a saved conversion status, restore it */
6059 if (bSaved)
6060 pImmSetConversionStatus(hImc, dwConversionSaved,
6061 dwSentenceSaved);
6062 bSaved = FALSE;
6063 }
6064 else
6065 {
6066 /* save conversion status and disable korean */
6067 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6068 &dwSentenceSaved))
6069 {
6070 bSaved = TRUE;
6071 pImmSetConversionStatus(hImc,
6072 dwConversionSaved & ~(IME_CMODE_NATIVE
6073 | IME_CMODE_FULLSHAPE),
6074 dwSentenceSaved);
6075 }
6076 }
6077 }
6078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 pImmSetOpenStatus(hImc, active);
6080 pImmReleaseContext(s_hwnd, hImc);
6081 }
6082 }
6083}
6084
6085/*
6086 * Get IM status. When IM is on, return not 0. Else return 0.
6087 */
6088 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006089im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 int status = 0;
6092 HIMC hImc;
6093
6094 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6095 {
6096 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6097 pImmReleaseContext(s_hwnd, hImc);
6098 }
6099 return status;
6100}
6101
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006102#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
6104#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6105/* Win32 with GLOBAL IME */
6106
6107/*
6108 * Notify cursor position to IM.
6109 */
6110 void
6111im_set_position(int row, int col)
6112{
6113 /* Win32 with GLOBAL IME */
6114 POINT p;
6115
6116 p.x = FILL_X(col);
6117 p.y = FILL_Y(row);
6118 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6119 global_ime_set_position(&p);
6120}
6121
6122/*
6123 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6124 */
6125 void
6126im_set_active(int active)
6127{
6128 global_ime_set_status(active);
6129}
6130
6131/*
6132 * Get IM status. When IM is on, return not 0. Else return 0.
6133 */
6134 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006135im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 return global_ime_get_status();
6138}
6139#endif
6140
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006141#ifdef FEAT_MBYTE
6142/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006143 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006144 */
6145 static void
6146latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6147{
6148 int c;
6149
Bram Moolenaarca003e12006-03-17 23:19:38 +00006150 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006151 {
6152 c = *text++;
6153 switch (c)
6154 {
6155 case 0xa4: c = 0x20ac; break; /* euro */
6156 case 0xa6: c = 0x0160; break; /* S hat */
6157 case 0xa8: c = 0x0161; break; /* S -hat */
6158 case 0xb4: c = 0x017d; break; /* Z hat */
6159 case 0xb8: c = 0x017e; break; /* Z -hat */
6160 case 0xbc: c = 0x0152; break; /* OE */
6161 case 0xbd: c = 0x0153; break; /* oe */
6162 case 0xbe: c = 0x0178; break; /* Y */
6163 }
6164 *unicodebuf++ = c;
6165 }
6166}
6167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168
6169#ifdef FEAT_RIGHTLEFT
6170/*
6171 * What is this for? In the case where you are using Win98 or Win2K or later,
6172 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6173 * reverses the string sent to the TextOut... family. This sucks, because we
6174 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6175 * way to tell Windblows not to do this!
6176 *
6177 * The short of it is that this 'RevOut' only gets called if you are running
6178 * one of the new, "improved" MS OSes, and only if you are running in
6179 * 'rightleft' mode. It makes display take *slightly* longer, but not
6180 * noticeably so.
6181 */
6182 static void
6183RevOut( HDC s_hdc,
6184 int col,
6185 int row,
6186 UINT foptions,
6187 CONST RECT *pcliprect,
6188 LPCTSTR text,
6189 UINT len,
6190 CONST INT *padding)
6191{
6192 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006194 for (ix = 0; ix < (int)len; ++ix)
6195 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6196 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197}
6198#endif
6199
Bram Moolenaar92467d32017-12-05 13:22:16 +01006200 static void
6201draw_line(
6202 int x1,
6203 int y1,
6204 int x2,
6205 int y2,
6206 COLORREF color)
6207{
6208#if defined(FEAT_DIRECTX)
6209 if (IS_ENABLE_DIRECTX())
6210 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6211 else
6212#endif
6213 {
6214 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6215 HPEN old_pen = SelectObject(s_hdc, hpen);
6216 MoveToEx(s_hdc, x1, y1, NULL);
6217 /* Note: LineTo() excludes the last pixel in the line. */
6218 LineTo(s_hdc, x2, y2);
6219 DeleteObject(SelectObject(s_hdc, old_pen));
6220 }
6221}
6222
6223 static void
6224set_pixel(
6225 int x,
6226 int y,
6227 COLORREF color)
6228{
6229#if defined(FEAT_DIRECTX)
6230 if (IS_ENABLE_DIRECTX())
6231 DWriteContext_SetPixel(s_dwc, x, y, color);
6232 else
6233#endif
6234 SetPixel(s_hdc, x, y, color);
6235}
6236
6237 static void
6238fill_rect(
6239 const RECT *rcp,
6240 HBRUSH hbr,
6241 COLORREF color)
6242{
6243#if defined(FEAT_DIRECTX)
6244 if (IS_ENABLE_DIRECTX())
6245 DWriteContext_FillRect(s_dwc, rcp, color);
6246 else
6247#endif
6248 {
6249 HBRUSH hbr2;
6250
6251 if (hbr == NULL)
6252 hbr2 = CreateSolidBrush(color);
6253 else
6254 hbr2 = hbr;
6255 FillRect(s_hdc, rcp, hbr2);
6256 if (hbr == NULL)
6257 DeleteBrush(hbr2);
6258 }
6259}
6260
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 void
6262gui_mch_draw_string(
6263 int row,
6264 int col,
6265 char_u *text,
6266 int len,
6267 int flags)
6268{
6269 static int *padding = NULL;
6270 static int pad_size = 0;
6271 int i;
6272 const RECT *pcliprect = NULL;
6273 UINT foptions = 0;
6274#ifdef FEAT_MBYTE
6275 static WCHAR *unicodebuf = NULL;
6276 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006277 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 int n = 0;
6279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 int y;
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 /*
6283 * Italic and bold text seems to have an extra row of pixels at the bottom
6284 * (below where the bottom of the character should be). If we draw the
6285 * characters with a solid background, the top row of pixels in the
6286 * character below will be overwritten. We can fix this by filling in the
6287 * background ourselves, to the correct character proportions, and then
6288 * writing the character in transparent mode. Still have a problem when
6289 * the character is "_", which gets written on to the character below.
6290 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6291 * pixel in their slots, which fixes the problem with the bottom row of
6292 * pixels. We still need this code because otherwise the top row of pixels
6293 * becomes a problem. - webb.
6294 */
6295 static HBRUSH hbr_cache[2] = {NULL, NULL};
6296 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6297 static int brush_lru = 0;
6298 HBRUSH hbr;
6299 RECT rc;
6300
6301 if (!(flags & DRAW_TRANSP))
6302 {
6303 /*
6304 * Clear background first.
6305 * Note: FillRect() excludes right and bottom of rectangle.
6306 */
6307 rc.left = FILL_X(col);
6308 rc.top = FILL_Y(row);
6309#ifdef FEAT_MBYTE
6310 if (has_mbyte)
6311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006313 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 }
6315 else
6316#endif
6317 rc.right = FILL_X(col + len);
6318 rc.bottom = FILL_Y(row + 1);
6319
6320 /* Cache the created brush, that saves a lot of time. We need two:
6321 * one for cursor background and one for the normal background. */
6322 if (gui.currBgColor == brush_color[0])
6323 {
6324 hbr = hbr_cache[0];
6325 brush_lru = 1;
6326 }
6327 else if (gui.currBgColor == brush_color[1])
6328 {
6329 hbr = hbr_cache[1];
6330 brush_lru = 0;
6331 }
6332 else
6333 {
6334 if (hbr_cache[brush_lru] != NULL)
6335 DeleteBrush(hbr_cache[brush_lru]);
6336 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6337 brush_color[brush_lru] = gui.currBgColor;
6338 hbr = hbr_cache[brush_lru];
6339 brush_lru = !brush_lru;
6340 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006341
Bram Moolenaar92467d32017-12-05 13:22:16 +01006342 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344 SetBkMode(s_hdc, TRANSPARENT);
6345
6346 /*
6347 * When drawing block cursor, prevent inverted character spilling
6348 * over character cell (can happen with bold/italic)
6349 */
6350 if (flags & DRAW_CURSOR)
6351 {
6352 pcliprect = &rc;
6353 foptions = ETO_CLIPPED;
6354 }
6355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 SetTextColor(s_hdc, gui.currFgColor);
6357 SelectFont(s_hdc, gui.currFont);
6358
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006359#ifdef FEAT_DIRECTX
6360 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006361 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006362#endif
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6365 {
6366 vim_free(padding);
6367 pad_size = Columns;
6368
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006369 /* Don't give an out-of-memory message here, it would call us
6370 * recursively. */
6371 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (padding != NULL)
6373 for (i = 0; i < pad_size; i++)
6374 padding[i] = gui.char_width;
6375 }
6376
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 /*
6378 * We have to provide the padding argument because italic and bold versions
6379 * of fixed-width fonts are often one pixel or so wider than their normal
6380 * versions.
6381 * No check for DRAW_BOLD, Windows will have done it already.
6382 */
6383
6384#ifdef FEAT_MBYTE
6385 /* Check if there are any UTF-8 characters. If not, use normal text
6386 * output to speed up output. */
6387 if (enc_utf8)
6388 for (n = 0; n < len; ++n)
6389 if (text[n] >= 0x80)
6390 break;
6391
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006392# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006393 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6394 * required that unicode drawing routine, currently. So this forces it
6395 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006396 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006397 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006398# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006399
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006401 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006403 if ((enc_utf8
6404 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6405 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 && (unicodebuf == NULL || len > unibuflen))
6407 {
6408 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006409 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006412 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 unibuflen = len;
6415 }
6416
6417 if (enc_utf8 && n < len && unicodebuf != NULL)
6418 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006419 /* Output UTF-8 characters. Composing characters should be
6420 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006421 int i;
6422 int wlen; /* string length in words */
6423 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 int cells; /* cell width of string up to composing char */
6425 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006426 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006428 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006429 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006431 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006433 c = utf_ptr2char(text + i);
6434 if (c >= 0x10000)
6435 {
6436 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006437 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6438 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006439 }
6440 else
6441 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006442 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006443 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006444
6445 if (utf_iscomposing(c))
6446 cw = 0;
6447 else
6448 {
6449 cw = utf_char2cells(c);
6450 if (cw > 2) /* don't use 4 for unprintable char */
6451 cw = 1;
6452 }
6453
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (unicodepdy != NULL)
6455 {
6456 /* Use unicodepdy to make characters fit as we expect, even
6457 * when the font uses different widths (e.g., bold character
6458 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006459 if (c >= 0x10000)
6460 {
6461 unicodepdy[wlen - 2] = cw * gui.char_width;
6462 unicodepdy[wlen - 1] = 0;
6463 }
6464 else
6465 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 }
6467 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006468 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006469 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006471# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006472 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006473 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006474 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006475 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006476 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006477 gui.char_width, gui.currFgColor,
6478 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006479 }
6480 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006481# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006482 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6483 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 len = cells; /* used for underlining */
6485 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006486 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
6488 /* If we want to display codepage data, and the current CP is not the
6489 * ANSI one, we need to go via Unicode. */
6490 if (unicodebuf != NULL)
6491 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006492 if (enc_latin9)
6493 latin9_to_ucs(text, len, unicodebuf);
6494 else
6495 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 MB_PRECOMPOSED,
6497 (char *)text, len,
6498 (LPWSTR)unicodebuf, unibuflen);
6499 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006500 {
6501 /* Use unicodepdy to make characters fit as we expect, even
6502 * when the font uses different widths (e.g., bold character
6503 * is wider). */
6504 if (unicodepdy != NULL)
6505 {
6506 int i;
6507 int cw;
6508
6509 for (i = 0; i < len; ++i)
6510 {
6511 cw = utf_char2cells(unicodebuf[i]);
6512 if (cw > 2)
6513 cw = 1;
6514 unicodepdy[i] = cw * gui.char_width;
6515 }
6516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006518 foptions, pcliprect, unicodebuf, len, unicodepdy);
6519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521 }
6522 else
6523#endif
6524 {
6525#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006526 /* Windows will mess up RL text, so we have to draw it character by
6527 * character. Only do this if RL is on, since it's slow. */
6528 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6530 foptions, pcliprect, (char *)text, len, padding);
6531 else
6532#endif
6533 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6534 foptions, pcliprect, (char *)text, len, padding);
6535 }
6536
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006537 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 if (flags & DRAW_UNDERL)
6539 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 /* When p_linespace is 0, overwrite the bottom row of pixels.
6541 * Otherwise put the line just below the character. */
6542 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (p_linespace > 1)
6544 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006545 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006547
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006548 /* Strikethrough */
6549 if (flags & DRAW_STRIKE)
6550 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006551 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006552 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006553 }
6554
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006555 /* Undercurl */
6556 if (flags & DRAW_UNDERC)
6557 {
6558 int x;
6559 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006560 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006561
6562 y = FILL_Y(row + 1) - 1;
6563 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6564 {
6565 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006566 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006567 }
6568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569}
6570
6571
6572/*
6573 * Output routines.
6574 */
6575
6576/* Flush any output to the screen */
6577 void
6578gui_mch_flush(void)
6579{
6580# if defined(__BORLANDC__)
6581 /*
6582 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6583 * prototype declaration.
6584 * The compiler complains if __stdcall is not used in both declarations.
6585 */
6586 BOOL __stdcall GdiFlush(void);
6587# endif
6588
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006589#if defined(FEAT_DIRECTX)
6590 if (IS_ENABLE_DIRECTX())
6591 DWriteContext_Flush(s_dwc);
6592#endif
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 GdiFlush();
6595}
6596
6597 static void
6598clear_rect(RECT *rcp)
6599{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006600 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
6603
Bram Moolenaarc716c302006-01-21 22:12:51 +00006604 void
6605gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6606{
6607 RECT workarea_rect;
6608
6609 get_work_area(&workarea_rect);
6610
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006611 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006612 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006613 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006614
6615 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6616 * the menubar for MSwin, we subtract it from the screen height, so that
6617 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006618 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006619 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006620 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006621 - GetSystemMetrics(SM_CYCAPTION)
6622#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006623 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006624#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006625 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006626}
6627
6628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629#if defined(FEAT_MENU) || defined(PROTO)
6630/*
6631 * Add a sub menu to the menu bar.
6632 */
6633 void
6634gui_mch_add_menu(
6635 vimmenu_T *menu,
6636 int pos)
6637{
6638 vimmenu_T *parent = menu->parent;
6639
6640 menu->submenu_id = CreatePopupMenu();
6641 menu->id = s_menu_id++;
6642
6643 if (menu_is_menubar(menu->name))
6644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006646 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006648 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6649 {
6650 /* 'encoding' differs from active codepage: convert menu name
6651 * and use wide function */
6652 wn = enc_to_utf16(menu->name, NULL);
6653 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006655 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006657 infow.cbSize = sizeof(infow);
6658 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6659 | MIIM_SUBMENU;
6660 infow.dwItemData = (long_u)menu;
6661 infow.wID = menu->id;
6662 infow.fType = MFT_STRING;
6663 infow.dwTypeData = wn;
6664 infow.cch = (UINT)wcslen(wn);
6665 infow.hSubMenu = menu->submenu_id;
6666 InsertMenuItemW((parent == NULL)
6667 ? s_menuBar : parent->submenu_id,
6668 (UINT)pos, TRUE, &infow);
6669 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006673 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006675 {
6676 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006678 info.cbSize = sizeof(info);
6679 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6680 info.dwItemData = (long_u)menu;
6681 info.wID = menu->id;
6682 info.fType = MFT_STRING;
6683 info.dwTypeData = (LPTSTR)menu->name;
6684 info.cch = (UINT)STRLEN(menu->name);
6685 info.hSubMenu = menu->submenu_id;
6686 InsertMenuItem((parent == NULL)
6687 ? s_menuBar : parent->submenu_id,
6688 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 }
6690 }
6691
6692 /* Fix window size if menu may have wrapped */
6693 if (parent == NULL)
6694 gui_mswin_get_menu_height(!gui.starting);
6695#ifdef FEAT_TEAROFF
6696 else if (IsWindow(parent->tearoff_handle))
6697 rebuild_tearoff(parent);
6698#endif
6699}
6700
6701 void
6702gui_mch_show_popupmenu(vimmenu_T *menu)
6703{
6704 POINT mp;
6705
6706 (void)GetCursorPos((LPPOINT)&mp);
6707 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6708}
6709
6710 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006711gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
6713 vimmenu_T *menu = gui_find_menu(path_name);
6714
6715 if (menu != NULL)
6716 {
6717 POINT p;
6718
6719 /* Find the position of the current cursor */
6720 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006721 if (mouse_pos)
6722 {
6723 int mx, my;
6724
6725 gui_mch_getmouse(&mx, &my);
6726 p.x += mx;
6727 p.y += my;
6728 }
6729 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006731 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6733 }
6734 msg_scroll = FALSE;
6735 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6736 }
6737}
6738
6739#if defined(FEAT_TEAROFF) || defined(PROTO)
6740/*
6741 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6742 * create it as a pseudo-"tearoff menu".
6743 */
6744 void
6745gui_make_tearoff(char_u *path_name)
6746{
6747 vimmenu_T *menu = gui_find_menu(path_name);
6748
6749 /* Found the menu, so tear it off. */
6750 if (menu != NULL)
6751 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6752}
6753#endif
6754
6755/*
6756 * Add a menu item to a menu
6757 */
6758 void
6759gui_mch_add_menu_item(
6760 vimmenu_T *menu,
6761 int idx)
6762{
6763 vimmenu_T *parent = menu->parent;
6764
6765 menu->id = s_menu_id++;
6766 menu->submenu_id = NULL;
6767
6768#ifdef FEAT_TEAROFF
6769 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6770 {
6771 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6772 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6773 }
6774 else
6775#endif
6776#ifdef FEAT_TOOLBAR
6777 if (menu_is_toolbar(parent->name))
6778 {
6779 TBBUTTON newtb;
6780
6781 vim_memset(&newtb, 0, sizeof(newtb));
6782 if (menu_is_separator(menu->name))
6783 {
6784 newtb.iBitmap = 0;
6785 newtb.fsStyle = TBSTYLE_SEP;
6786 }
6787 else
6788 {
6789 newtb.iBitmap = get_toolbar_bitmap(menu);
6790 newtb.fsStyle = TBSTYLE_BUTTON;
6791 }
6792 newtb.idCommand = menu->id;
6793 newtb.fsState = TBSTATE_ENABLED;
6794 newtb.iString = 0;
6795 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6796 (LPARAM)&newtb);
6797 menu->submenu_id = (HMENU)-1;
6798 }
6799 else
6800#endif
6801 {
6802#ifdef FEAT_MBYTE
6803 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6806 {
6807 /* 'encoding' differs from active codepage: convert menu item name
6808 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006809 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (wn != NULL)
6811 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006812 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 (menu_is_separator(menu->name)
6814 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6815 (UINT)menu->id, wn);
6816 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
6818 }
6819 if (wn == NULL)
6820#endif
6821 InsertMenu(parent->submenu_id, (UINT)idx,
6822 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6823 | MF_BYPOSITION,
6824 (UINT)menu->id, (LPCTSTR)menu->name);
6825#ifdef FEAT_TEAROFF
6826 if (IsWindow(parent->tearoff_handle))
6827 rebuild_tearoff(parent);
6828#endif
6829 }
6830}
6831
6832/*
6833 * Destroy the machine specific menu widget.
6834 */
6835 void
6836gui_mch_destroy_menu(vimmenu_T *menu)
6837{
6838#ifdef FEAT_TOOLBAR
6839 /*
6840 * is this a toolbar button?
6841 */
6842 if (menu->submenu_id == (HMENU)-1)
6843 {
6844 int iButton;
6845
6846 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6847 (WPARAM)menu->id, 0);
6848 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6849 }
6850 else
6851#endif
6852 {
6853 if (menu->parent != NULL
6854 && menu_is_popup(menu->parent->dname)
6855 && menu->parent->submenu_id != NULL)
6856 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6857 else
6858 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6859 if (menu->submenu_id != NULL)
6860 DestroyMenu(menu->submenu_id);
6861#ifdef FEAT_TEAROFF
6862 if (IsWindow(menu->tearoff_handle))
6863 DestroyWindow(menu->tearoff_handle);
6864 if (menu->parent != NULL
6865 && menu->parent->children != NULL
6866 && IsWindow(menu->parent->tearoff_handle))
6867 {
6868 /* This menu must not show up when rebuilding the tearoff window. */
6869 menu->modes = 0;
6870 rebuild_tearoff(menu->parent);
6871 }
6872#endif
6873 }
6874}
6875
6876#ifdef FEAT_TEAROFF
6877 static void
6878rebuild_tearoff(vimmenu_T *menu)
6879{
6880 /*hackish*/
6881 char_u tbuf[128];
6882 RECT trect;
6883 RECT rct;
6884 RECT roct;
6885 int x, y;
6886
6887 HWND thwnd = menu->tearoff_handle;
6888
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006889 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (GetWindowRect(thwnd, &trect)
6891 && GetWindowRect(s_hwnd, &rct)
6892 && GetClientRect(s_hwnd, &roct))
6893 {
6894 x = trect.left - rct.left;
6895 y = (trect.top - rct.bottom + roct.bottom);
6896 }
6897 else
6898 {
6899 x = y = 0xffffL;
6900 }
6901 DestroyWindow(thwnd);
6902 if (menu->children != NULL)
6903 {
6904 gui_mch_tearoff(tbuf, menu, x, y);
6905 if (IsWindow(menu->tearoff_handle))
6906 (void) SetWindowPos(menu->tearoff_handle,
6907 NULL,
6908 (int)trect.left,
6909 (int)trect.top,
6910 0, 0,
6911 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6912 }
6913}
6914#endif /* FEAT_TEAROFF */
6915
6916/*
6917 * Make a menu either grey or not grey.
6918 */
6919 void
6920gui_mch_menu_grey(
6921 vimmenu_T *menu,
6922 int grey)
6923{
6924#ifdef FEAT_TOOLBAR
6925 /*
6926 * is this a toolbar button?
6927 */
6928 if (menu->submenu_id == (HMENU)-1)
6929 {
6930 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6931 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6932 }
6933 else
6934#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006935 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6936 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937
6938#ifdef FEAT_TEAROFF
6939 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6940 {
6941 WORD menuID;
6942 HWND menuHandle;
6943
6944 /*
6945 * A tearoff button has changed state.
6946 */
6947 if (menu->children == NULL)
6948 menuID = (WORD)(menu->id);
6949 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006950 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6952 if (menuHandle)
6953 EnableWindow(menuHandle, !grey);
6954
6955 }
6956#endif
6957}
6958
6959#endif /* FEAT_MENU */
6960
6961
6962/* define some macros used to make the dialogue creation more readable */
6963
6964#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6965#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006966#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
6968#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6969/*
6970 * stuff for dialogs
6971 */
6972
6973/*
6974 * The callback routine used by all the dialogs. Very simple. First,
6975 * acknowledges the INITDIALOG message so that Windows knows to do standard
6976 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6977 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6978 * number.
6979 */
6980 static LRESULT CALLBACK
6981dialog_callback(
6982 HWND hwnd,
6983 UINT message,
6984 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006985 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 if (message == WM_INITDIALOG)
6988 {
6989 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6990 /* Set focus to the dialog. Set the default button, if specified. */
6991 (void)SetFocus(hwnd);
6992 if (dialog_default_button > IDCANCEL)
6993 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006994 else
6995 /* We don't have a default, set focus on another element of the
6996 * dialog window, probably the icon */
6997 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 return FALSE;
6999 }
7000
7001 if (message == WM_COMMAND)
7002 {
7003 int button = LOWORD(wParam);
7004
7005 /* Don't end the dialog if something was selected that was
7006 * not a button.
7007 */
7008 if (button >= DLG_NONBUTTON_CONTROL)
7009 return TRUE;
7010
7011 /* If the edit box exists, copy the string. */
7012 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007013 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007014# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007015 /* If the OS is Windows NT, and 'encoding' differs from active
7016 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007017 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007018 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007019 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7020 char_u *p;
7021
7022 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7023 p = utf16_to_enc(wp, NULL);
7024 vim_strncpy(s_textfield, p, IOSIZE);
7025 vim_free(p);
7026 vim_free(wp);
7027 }
7028 else
7029# endif
7030 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007031 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 /*
7035 * Need to check for IDOK because if the user just hits Return to
7036 * accept the default value, some reason this is what we get.
7037 */
7038 if (button == IDOK)
7039 {
7040 if (dialog_default_button > IDCANCEL)
7041 EndDialog(hwnd, dialog_default_button);
7042 }
7043 else
7044 EndDialog(hwnd, button - IDCANCEL);
7045 return TRUE;
7046 }
7047
7048 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7049 {
7050 EndDialog(hwnd, 0);
7051 return TRUE;
7052 }
7053 return FALSE;
7054}
7055
7056/*
7057 * Create a dialog dynamically from the parameter strings.
7058 * type = type of dialog (question, alert, etc.)
7059 * title = dialog title. may be NULL for default title.
7060 * message = text to display. Dialog sizes to accommodate it.
7061 * buttons = '\n' separated list of button captions, default first.
7062 * dfltbutton = number of default button.
7063 *
7064 * This routine returns 1 if the first button is pressed,
7065 * 2 for the second, etc.
7066 *
7067 * 0 indicates Esc was pressed.
7068 * -1 for unexpected error
7069 *
7070 * If stubbing out this fn, return 1.
7071 */
7072
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007073static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 "IDR_VIM",
7076 "IDR_VIM_ERROR",
7077 "IDR_VIM_ALERT",
7078 "IDR_VIM_INFO",
7079 "IDR_VIM_QUESTION"
7080};
7081
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 int
7083gui_mch_dialog(
7084 int type,
7085 char_u *title,
7086 char_u *message,
7087 char_u *buttons,
7088 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007089 char_u *textfield,
7090 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091{
7092 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007093 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 int numButtons;
7095 int *buttonWidths, *buttonPositions;
7096 int buttonYpos;
7097 int nchar, i;
7098 DWORD lStyle;
7099 int dlgwidth = 0;
7100 int dlgheight;
7101 int editboxheight;
7102 int horizWidth = 0;
7103 int msgheight;
7104 char_u *pstart;
7105 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007106 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 char_u *tbuffer;
7108 RECT rect;
7109 HWND hwnd;
7110 HDC hdc;
7111 HFONT font, oldFont;
7112 TEXTMETRIC fontInfo;
7113 int fontHeight;
7114 int textWidth, minButtonWidth, messageWidth;
7115 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007116 int maxDialogHeight;
7117 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 int vertical;
7119 int dlgPaddingX;
7120 int dlgPaddingY;
7121#ifdef USE_SYSMENU_FONT
7122 LOGFONT lfSysmenu;
7123 int use_lfSysmenu = FALSE;
7124#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007125 garray_T ga;
7126 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128#ifndef NO_CONSOLE
7129 /* Don't output anything in silent mode ("ex -s") */
7130 if (silent_mode)
7131 return dfltbutton; /* return default option */
7132#endif
7133
Bram Moolenaar748bf032005-02-02 23:04:36 +00007134 if (s_hwnd == NULL)
7135 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136
7137 if ((type < 0) || (type > VIM_LAST_TYPE))
7138 type = 0;
7139
7140 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007141 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007142 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007143 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 if (p == NULL)
7146 return -1;
7147
7148 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007149 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7151 * const.
7152 */
7153 tbuffer = vim_strsave(buttons);
7154 if (tbuffer == NULL)
7155 return -1;
7156
7157 --dfltbutton; /* Change from one-based to zero-based */
7158
7159 /* Count buttons */
7160 numButtons = 1;
7161 for (i = 0; tbuffer[i] != '\0'; i++)
7162 {
7163 if (tbuffer[i] == DLG_BUTTON_SEP)
7164 numButtons++;
7165 }
7166 if (dfltbutton >= numButtons)
7167 dfltbutton = -1;
7168
7169 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007170 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (buttonWidths == NULL)
7172 return -1;
7173
7174 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007175 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 if (buttonPositions == NULL)
7177 return -1;
7178
7179 /*
7180 * Calculate how big the dialog must be.
7181 */
7182 hwnd = GetDesktopWindow();
7183 hdc = GetWindowDC(hwnd);
7184#ifdef USE_SYSMENU_FONT
7185 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7186 {
7187 font = CreateFontIndirect(&lfSysmenu);
7188 use_lfSysmenu = TRUE;
7189 }
7190 else
7191#endif
7192 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7193 VARIABLE_PITCH , DLG_FONT_NAME);
7194 if (s_usenewlook)
7195 {
7196 oldFont = SelectFont(hdc, font);
7197 dlgPaddingX = DLG_PADDING_X;
7198 dlgPaddingY = DLG_PADDING_Y;
7199 }
7200 else
7201 {
7202 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7203 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7204 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7205 }
7206 GetTextMetrics(hdc, &fontInfo);
7207 fontHeight = fontInfo.tmHeight;
7208
7209 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007210 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211
7212 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007213 if (s_hwnd == NULL)
7214 {
7215 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaarc716c302006-01-21 22:12:51 +00007217 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007218 get_work_area(&workarea_rect);
7219 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7220 if (maxDialogWidth > 600)
7221 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007222 /* Leave some room for the taskbar. */
7223 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007224 }
7225 else
7226 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007227 /* Use our own window for the size, unless it's very small. */
7228 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007229 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007230 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007231 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007232 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7233 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007234
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007235 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007236 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007237 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007238 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007239 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7240 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7241 }
7242
7243 /* Set dlgwidth to width of message.
7244 * Copy the message into "ga", changing NL to CR-NL and inserting line
7245 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 pstart = message;
7247 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007248 msgheight = 0;
7249 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 do
7251 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007252 msgheight += fontHeight; /* at least one line */
7253
7254 /* Need to figure out where to break the string. The system does it
7255 * at a word boundary, which would mean we can't compute the number of
7256 * wrapped lines. */
7257 textWidth = 0;
7258 last_white = NULL;
7259 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007260 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007261#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007262 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007263#else
7264 l = 1;
7265#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007266 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007267 && textWidth > maxDialogWidth * 3 / 4)
7268 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007269 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007270 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007271 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007272 /* Line will wrap. */
7273 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007274 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007275 textWidth = 0;
7276
7277 if (last_white != NULL)
7278 {
7279 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007280 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007281 pend = last_white + 1;
7282 last_white = NULL;
7283 }
7284 ga_append(&ga, '\r');
7285 ga_append(&ga, '\n');
7286 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007287 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007288
7289 while (--l >= 0)
7290 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007291 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007292 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007294
7295 ga_append(&ga, '\r');
7296 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 pstart = pend + 1;
7298 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007299
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007300 if (ga.ga_data != NULL)
7301 message = ga.ga_data;
7302
Bram Moolenaar748bf032005-02-02 23:04:36 +00007303 messageWidth += 10; /* roundoff space */
7304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007306 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7307 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 if (msgheight < DLG_ICON_HEIGHT)
7310 msgheight = DLG_ICON_HEIGHT;
7311
7312 /*
7313 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007314 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007316 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (!vertical)
7318 {
7319 // Place buttons horizontally if they fit.
7320 horizWidth = dlgPaddingX;
7321 pstart = tbuffer;
7322 i = 0;
7323 do
7324 {
7325 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7326 if (pend == NULL)
7327 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007328 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 if (textWidth < minButtonWidth)
7330 textWidth = minButtonWidth;
7331 textWidth += dlgPaddingX; /* Padding within button */
7332 buttonWidths[i] = textWidth;
7333 buttonPositions[i++] = horizWidth;
7334 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7335 pstart = pend + 1;
7336 } while (*pend != NUL);
7337
7338 if (horizWidth > maxDialogWidth)
7339 vertical = TRUE; // Too wide to fit on the screen.
7340 else if (horizWidth > dlgwidth)
7341 dlgwidth = horizWidth;
7342 }
7343
7344 if (vertical)
7345 {
7346 // Stack buttons vertically.
7347 pstart = tbuffer;
7348 do
7349 {
7350 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7351 if (pend == NULL)
7352 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007353 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 textWidth += dlgPaddingX; /* Padding within button */
7355 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7356 if (textWidth > dlgwidth)
7357 dlgwidth = textWidth;
7358 pstart = pend + 1;
7359 } while (*pend != NUL);
7360 }
7361
7362 if (dlgwidth < DLG_MIN_WIDTH)
7363 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7364
7365 /* start to fill in the dlgtemplate information. addressing by WORDs */
7366 if (s_usenewlook)
7367 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7368 else
7369 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7370
7371 add_long(lStyle);
7372 add_long(0); // (lExtendedStyle)
7373 pnumitems = p; /*save where the number of items must be stored*/
7374 add_word(0); // NumberOfItems(will change later)
7375 add_word(10); // x
7376 add_word(10); // y
7377 add_word(PixelToDialogX(dlgwidth)); // cx
7378
7379 // Dialog height.
7380 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007381 dlgheight = msgheight + 2 * dlgPaddingY
7382 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 else
7384 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7385
7386 // Dialog needs to be taller if contains an edit box.
7387 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7388 if (textfield != NULL)
7389 dlgheight += editboxheight;
7390
Bram Moolenaara95d8232013-08-07 15:27:11 +02007391 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7392 if (dlgheight > maxDialogHeight)
7393 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007394 msgheight = msgheight - (dlgheight - maxDialogHeight);
7395 dlgheight = maxDialogHeight;
7396 scroll_flag = WS_VSCROLL;
7397 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7398 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007399 }
7400
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 add_word(PixelToDialogY(dlgheight));
7402
7403 add_word(0); // Menu
7404 add_word(0); // Class
7405
7406 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007407 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7408 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 p += nchar;
7410
7411 if (s_usenewlook)
7412 {
7413 /* do the font, since DS_3DLOOK doesn't work properly */
7414#ifdef USE_SYSMENU_FONT
7415 if (use_lfSysmenu)
7416 {
7417 /* point size */
7418 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7419 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007420 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 }
7422 else
7423#endif
7424 {
7425 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007426 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 p += nchar;
7429 }
7430
7431 buttonYpos = msgheight + 2 * dlgPaddingY;
7432
7433 if (textfield != NULL)
7434 buttonYpos += editboxheight;
7435
7436 pstart = tbuffer;
7437 if (!vertical)
7438 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7439 for (i = 0; i < numButtons; i++)
7440 {
7441 /* get end of this button. */
7442 for ( pend = pstart;
7443 *pend && (*pend != DLG_BUTTON_SEP);
7444 pend++)
7445 ;
7446
7447 if (*pend)
7448 *pend = '\0';
7449
7450 /*
7451 * old NOTE:
7452 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7453 * the focus to the first tab-able button and in so doing makes that
7454 * the default!! Grrr. Workaround: Make the default button the only
7455 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7456 * he/she can use arrow keys.
7457 *
7458 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007459 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 * dialog. Also needed for when the textfield is the default control.
7461 * It appears to work now (perhaps not on Win95?).
7462 */
7463 if (vertical)
7464 {
7465 p = add_dialog_element(p,
7466 (i == dfltbutton
7467 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7468 PixelToDialogX(DLG_VERT_PADDING_X),
7469 PixelToDialogY(buttonYpos /* TBK */
7470 + 2 * fontHeight * i),
7471 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7472 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007473 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475 else
7476 {
7477 p = add_dialog_element(p,
7478 (i == dfltbutton
7479 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7480 PixelToDialogX(horizWidth + buttonPositions[i]),
7481 PixelToDialogY(buttonYpos), /* TBK */
7482 PixelToDialogX(buttonWidths[i]),
7483 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007484 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486 pstart = pend + 1; /*next button*/
7487 }
7488 *pnumitems += numButtons;
7489
7490 /* Vim icon */
7491 p = add_dialog_element(p, SS_ICON,
7492 PixelToDialogX(dlgPaddingX),
7493 PixelToDialogY(dlgPaddingY),
7494 PixelToDialogX(DLG_ICON_WIDTH),
7495 PixelToDialogY(DLG_ICON_HEIGHT),
7496 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7497 dlg_icons[type]);
7498
Bram Moolenaar748bf032005-02-02 23:04:36 +00007499 /* Dialog message */
7500 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7501 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7502 PixelToDialogY(dlgPaddingY),
7503 (WORD)(PixelToDialogX(messageWidth) + 1),
7504 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007505 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
7507 /* Edit box */
7508 if (textfield != NULL)
7509 {
7510 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7511 PixelToDialogX(2 * dlgPaddingX),
7512 PixelToDialogY(2 * dlgPaddingY + msgheight),
7513 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7514 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007515 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 *pnumitems += 1;
7517 }
7518
7519 *pnumitems += 2;
7520
7521 SelectFont(hdc, oldFont);
7522 DeleteObject(font);
7523 ReleaseDC(hwnd, hdc);
7524
7525 /* Let the dialog_callback() function know which button to make default
7526 * If we have an edit box, make that the default. We also need to tell
7527 * dialog_callback() if this dialog contains an edit box or not. We do
7528 * this by setting s_textfield if it does.
7529 */
7530 if (textfield != NULL)
7531 {
7532 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7533 s_textfield = textfield;
7534 }
7535 else
7536 {
7537 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7538 s_textfield = NULL;
7539 }
7540
7541 /* show the dialog box modally and get a return value */
7542 nchar = (int)DialogBoxIndirect(
7543 s_hinst,
7544 (LPDLGTEMPLATE)pdlgtemplate,
7545 s_hwnd,
7546 (DLGPROC)dialog_callback);
7547
7548 LocalFree(LocalHandle(pdlgtemplate));
7549 vim_free(tbuffer);
7550 vim_free(buttonWidths);
7551 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007552 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 /* Focus back to our window (for when MDI is used). */
7555 (void)SetFocus(s_hwnd);
7556
7557 return nchar;
7558}
7559
7560#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007561
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562/*
7563 * Put a simple element (basic class) onto a dialog template in memory.
7564 * return a pointer to where the next item should be added.
7565 *
7566 * parameters:
7567 * lStyle = additional style flags
7568 * (be careful, NT3.51 & Win32s will ignore the new ones)
7569 * x,y = x & y positions IN DIALOG UNITS
7570 * w,h = width and height IN DIALOG UNITS
7571 * Id = ID used in messages
7572 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7573 * caption = usually text or resource name
7574 *
7575 * TODO: use the length information noted here to enable the dialog creation
7576 * routines to work out more exactly how much memory they need to alloc.
7577 */
7578 static PWORD
7579add_dialog_element(
7580 PWORD p,
7581 DWORD lStyle,
7582 WORD x,
7583 WORD y,
7584 WORD w,
7585 WORD h,
7586 WORD Id,
7587 WORD clss,
7588 const char *caption)
7589{
7590 int nchar;
7591
7592 p = lpwAlign(p); /* Align to dword boundary*/
7593 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7594 *p++ = LOWORD(lStyle);
7595 *p++ = HIWORD(lStyle);
7596 *p++ = 0; // LOWORD (lExtendedStyle)
7597 *p++ = 0; // HIWORD (lExtendedStyle)
7598 *p++ = x;
7599 *p++ = y;
7600 *p++ = w;
7601 *p++ = h;
7602 *p++ = Id; //9 or 10 words in all
7603
7604 *p++ = (WORD)0xffff;
7605 *p++ = clss; //2 more here
7606
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007607 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 p += nchar;
7609
7610 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7611
7612 return p; //total = 15+ (strlen(caption)) words
7613 // = 30 + 2(strlen(caption) bytes reqd
7614}
7615
7616
7617/*
7618 * Helper routine. Take an input pointer, return closest pointer that is
7619 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7620 */
7621 static LPWORD
7622lpwAlign(
7623 LPWORD lpIn)
7624{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007625 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007627 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 ul += 3;
7629 ul >>= 2;
7630 ul <<= 2;
7631 return (LPWORD)ul;
7632}
7633
7634/*
7635 * Helper routine. Takes second parameter as Ansi string, copies it to first
7636 * parameter as wide character (16-bits / char) string, and returns integer
7637 * number of wide characters (words) in string (including the trailing wide
7638 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007639 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7640 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 static int
7642nCopyAnsiToWideChar(
7643 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007644 LPSTR lpAnsiIn,
7645 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 int nChar = 0;
7648#ifdef FEAT_MBYTE
7649 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7650 int i;
7651 WCHAR *wn;
7652
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007653 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {
7655 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007656 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (wn != NULL)
7658 {
7659 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007660 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 vim_free(wn);
7662 }
7663 }
7664 if (nChar == 0)
7665 /* Use Win32 conversion function. */
7666 nChar = MultiByteToWideChar(
7667 enc_codepage > 0 ? enc_codepage : CP_ACP,
7668 MB_PRECOMPOSED,
7669 lpAnsiIn, len,
7670 lpWCStr, len);
7671 for (i = 0; i < nChar; ++i)
7672 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7673 lpWCStr[i] = (WORD)' ';
7674#else
7675 do
7676 {
7677 if (*lpAnsiIn == '\t')
7678 *lpWCStr++ = (WORD)' ';
7679 else
7680 *lpWCStr++ = (WORD)*lpAnsiIn;
7681 nChar++;
7682 } while (*lpAnsiIn++);
7683#endif
7684
7685 return nChar;
7686}
7687
7688
7689#ifdef FEAT_TEAROFF
7690/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007691 * Lookup menu handle from "menu_id".
7692 */
7693 static HMENU
7694tearoff_lookup_menuhandle(
7695 vimmenu_T *menu,
7696 WORD menu_id)
7697{
7698 for ( ; menu != NULL; menu = menu->next)
7699 {
7700 if (menu->modes == 0) /* this menu has just been deleted */
7701 continue;
7702 if (menu_is_separator(menu->dname))
7703 continue;
7704 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7705 return menu->submenu_id;
7706 }
7707 return NULL;
7708}
7709
7710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 * The callback function for all the modeless dialogs that make up the
7712 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7713 * thinking its menus have been clicked), and go away when closed.
7714 */
7715 static LRESULT CALLBACK
7716tearoff_callback(
7717 HWND hwnd,
7718 UINT message,
7719 WPARAM wParam,
7720 LPARAM lParam)
7721{
7722 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007723 {
7724 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
7728 /* May show the mouse pointer again. */
7729 HandleMouseHide(message, lParam);
7730
7731 if (message == WM_COMMAND)
7732 {
7733 if ((WORD)(LOWORD(wParam)) & 0x8000)
7734 {
7735 POINT mp;
7736 RECT rect;
7737
7738 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7739 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007740 vimmenu_T *menu;
7741
7742 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007744 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7746 (int)rect.right - 8,
7747 (int)mp.y,
7748 (int)0, /*reserved param*/
7749 s_hwnd,
7750 NULL);
7751 /*
7752 * NOTE: The pop-up menu can eat the mouse up event.
7753 * We deal with this in normal.c.
7754 */
7755 }
7756 }
7757 else
7758 /* Pass on messages to the main Vim window */
7759 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7760 /*
7761 * Give main window the focus back: this is so after
7762 * choosing a tearoff button you can start typing again
7763 * straight away.
7764 */
7765 (void)SetFocus(s_hwnd);
7766 return TRUE;
7767 }
7768 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7769 {
7770 DestroyWindow(hwnd);
7771 return TRUE;
7772 }
7773
7774 /* When moved around, give main window the focus back. */
7775 if (message == WM_EXITSIZEMOVE)
7776 (void)SetActiveWindow(s_hwnd);
7777
7778 return FALSE;
7779}
7780#endif
7781
7782
7783/*
7784 * Decide whether to use the "new look" (small, non-bold font) or the "old
7785 * look" (big, clanky font) for dialogs, and work out a few values for use
7786 * later accordingly.
7787 */
7788 static void
7789get_dialog_font_metrics(void)
7790{
7791 HDC hdc;
7792 HFONT hfontTools = 0;
7793 DWORD dlgFontSize;
7794 SIZE size;
7795#ifdef USE_SYSMENU_FONT
7796 LOGFONT lfSysmenu;
7797#endif
7798
7799 s_usenewlook = FALSE;
7800
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007802 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7803 hfontTools = CreateFontIndirect(&lfSysmenu);
7804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805#endif
7806 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7807 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7808
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007809 if (hfontTools)
7810 {
7811 hdc = GetDC(s_hwnd);
7812 SelectObject(hdc, hfontTools);
7813 /*
7814 * GetTextMetrics() doesn't return the right value in
7815 * tmAveCharWidth, so we have to figure out the dialog base units
7816 * ourselves.
7817 */
7818 GetTextExtentPoint(hdc,
7819 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7820 52, &size);
7821 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007823 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7824 s_dlgfntheight = (WORD)size.cy;
7825 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827
7828 if (!s_usenewlook)
7829 {
7830 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7831 s_dlgfntwidth = LOWORD(dlgFontSize);
7832 s_dlgfntheight = HIWORD(dlgFontSize);
7833 }
7834}
7835
7836#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7837/*
7838 * Create a pseudo-"tearoff menu" based on the child
7839 * items of a given menu pointer.
7840 */
7841 static void
7842gui_mch_tearoff(
7843 char_u *title,
7844 vimmenu_T *menu,
7845 int initX,
7846 int initY)
7847{
7848 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7849 int template_len;
7850 int nchar, textWidth, submenuWidth;
7851 DWORD lStyle;
7852 DWORD lExtendedStyle;
7853 WORD dlgwidth;
7854 WORD menuID;
7855 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007856 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 vimmenu_T *the_menu = menu;
7858 HWND hwnd;
7859 HDC hdc;
7860 HFONT font, oldFont;
7861 int col, spaceWidth, len;
7862 int columnWidths[2];
7863 char_u *label, *text;
7864 int acLen = 0;
7865 int nameLen;
7866 int padding0, padding1, padding2 = 0;
7867 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007868 int x;
7869 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870#ifdef USE_SYSMENU_FONT
7871 LOGFONT lfSysmenu;
7872 int use_lfSysmenu = FALSE;
7873#endif
7874
7875 /*
7876 * If this menu is already torn off, move it to the mouse position.
7877 */
7878 if (IsWindow(menu->tearoff_handle))
7879 {
7880 POINT mp;
7881 if (GetCursorPos((LPPOINT)&mp))
7882 {
7883 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7884 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7885 }
7886 return;
7887 }
7888
7889 /*
7890 * Create a new tearoff.
7891 */
7892 if (*title == MNU_HIDDEN_CHAR)
7893 title++;
7894
7895 /* Allocate memory to store the dialog template. It's made bigger when
7896 * needed. */
7897 template_len = DLG_ALLOC_SIZE;
7898 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7899 if (p == NULL)
7900 return;
7901
7902 hwnd = GetDesktopWindow();
7903 hdc = GetWindowDC(hwnd);
7904#ifdef USE_SYSMENU_FONT
7905 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7906 {
7907 font = CreateFontIndirect(&lfSysmenu);
7908 use_lfSysmenu = TRUE;
7909 }
7910 else
7911#endif
7912 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7913 VARIABLE_PITCH , DLG_FONT_NAME);
7914 if (s_usenewlook)
7915 oldFont = SelectFont(hdc, font);
7916 else
7917 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7918
7919 /* Calculate width of a single space. Used for padding columns to the
7920 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007921 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922
7923 /* Figure out max width of the text column, the accelerator column and the
7924 * optional submenu column. */
7925 submenuWidth = 0;
7926 for (col = 0; col < 2; col++)
7927 {
7928 columnWidths[col] = 0;
7929 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7930 {
7931 /* Use "dname" here to compute the width of the visible text. */
7932 text = (col == 0) ? pmenu->dname : pmenu->actext;
7933 if (text != NULL && *text != NUL)
7934 {
7935 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7936 if (textWidth > columnWidths[col])
7937 columnWidths[col] = textWidth;
7938 }
7939 if (pmenu->children != NULL)
7940 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7941 }
7942 }
7943 if (columnWidths[1] == 0)
7944 {
7945 /* no accelerators */
7946 if (submenuWidth != 0)
7947 columnWidths[0] += submenuWidth;
7948 else
7949 columnWidths[0] += spaceWidth;
7950 }
7951 else
7952 {
7953 /* there is an accelerator column */
7954 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7955 columnWidths[1] += submenuWidth;
7956 }
7957
7958 /*
7959 * Now find the total width of our 'menu'.
7960 */
7961 textWidth = columnWidths[0] + columnWidths[1];
7962 if (submenuWidth != 0)
7963 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007964 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7966 textWidth += submenuWidth;
7967 }
7968 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7969 if (textWidth > dlgwidth)
7970 dlgwidth = textWidth;
7971 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7972
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 /* start to fill in the dlgtemplate information. addressing by WORDs */
7974 if (s_usenewlook)
7975 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7976 else
7977 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7978
7979 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7980 *p++ = LOWORD(lStyle);
7981 *p++ = HIWORD(lStyle);
7982 *p++ = LOWORD(lExtendedStyle);
7983 *p++ = HIWORD(lExtendedStyle);
7984 pnumitems = p; /* save where the number of items must be stored */
7985 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007986 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007988 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 else
7990 *p++ = PixelToDialogX(initX); // x
7991 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007992 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 else
7994 *p++ = PixelToDialogY(initY); // y
7995 *p++ = PixelToDialogX(dlgwidth); // cx
7996 ptrueheight = p;
7997 *p++ = 0; // dialog height: changed later anyway
7998 *p++ = 0; // Menu
7999 *p++ = 0; // Class
8000
8001 /* copy the title of the dialog */
8002 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008003 ? (LPSTR)title
8004 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 p += nchar;
8006
8007 if (s_usenewlook)
8008 {
8009 /* do the font, since DS_3DLOOK doesn't work properly */
8010#ifdef USE_SYSMENU_FONT
8011 if (use_lfSysmenu)
8012 {
8013 /* point size */
8014 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8015 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008016 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 }
8018 else
8019#endif
8020 {
8021 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008022 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 }
8024 p += nchar;
8025 }
8026
8027 /*
8028 * Loop over all the items in the menu.
8029 * But skip over the tearbar.
8030 */
8031 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8032 menu = menu->children->next;
8033 else
8034 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008035 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 for ( ; menu != NULL; menu = menu->next)
8037 {
8038 if (menu->modes == 0) /* this menu has just been deleted */
8039 continue;
8040 if (menu_is_separator(menu->dname))
8041 {
8042 sepPadding += 3;
8043 continue;
8044 }
8045
8046 /* Check if there still is plenty of room in the template. Make it
8047 * larger when needed. */
8048 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8049 {
8050 WORD *newp;
8051
8052 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8053 if (newp != NULL)
8054 {
8055 template_len += 4096;
8056 mch_memmove(newp, pdlgtemplate,
8057 (char *)p - (char *)pdlgtemplate);
8058 p = newp + (p - pdlgtemplate);
8059 pnumitems = newp + (pnumitems - pdlgtemplate);
8060 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8061 LocalFree(LocalHandle(pdlgtemplate));
8062 pdlgtemplate = newp;
8063 }
8064 }
8065
8066 /* Figure out minimal length of this menu label. Use "name" for the
8067 * actual text, "dname" for estimating the displayed size. "name"
8068 * has "&a" for mnemonic and includes the accelerator. */
8069 len = nameLen = (int)STRLEN(menu->name);
8070 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8071 (int)STRLEN(menu->dname))) / spaceWidth;
8072 len += padding0;
8073
8074 if (menu->actext != NULL)
8075 {
8076 acLen = (int)STRLEN(menu->actext);
8077 len += acLen;
8078 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8079 }
8080 else
8081 textWidth = 0;
8082 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8083 len += padding1;
8084
8085 if (menu->children == NULL)
8086 {
8087 padding2 = submenuWidth / spaceWidth;
8088 len += padding2;
8089 menuID = (WORD)(menu->id);
8090 }
8091 else
8092 {
8093 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008094 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096
8097 /* Allocate menu label and fill it in */
8098 text = label = alloc((unsigned)len + 1);
8099 if (label == NULL)
8100 break;
8101
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008102 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8104 if (text == NULL)
8105 text = label + nameLen; /* no actext, use whole name */
8106 while (padding0-- > 0)
8107 *text++ = ' ';
8108 if (menu->actext != NULL)
8109 {
8110 STRNCPY(text, menu->actext, acLen);
8111 text += acLen;
8112 }
8113 while (padding1-- > 0)
8114 *text++ = ' ';
8115 if (menu->children != NULL)
8116 {
8117 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8118 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8119 }
8120 else
8121 {
8122 while (padding2-- > 0)
8123 *text++ = ' ';
8124 }
8125 *text = NUL;
8126
8127 /*
8128 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8129 * W95/NT4 it makes the tear-off look more like a menu.
8130 */
8131 p = add_dialog_element(p,
8132 BS_PUSHBUTTON|BS_LEFT,
8133 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8134 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8135 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8136 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008137 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 vim_free(label);
8139 (*pnumitems)++;
8140 }
8141
8142 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8143
8144
8145 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008146 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 s_hinst,
8148 (LPDLGTEMPLATE)pdlgtemplate,
8149 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008150 (DLGPROC)tearoff_callback,
8151 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152
8153 LocalFree(LocalHandle(pdlgtemplate));
8154 SelectFont(hdc, oldFont);
8155 DeleteObject(font);
8156 ReleaseDC(hwnd, hdc);
8157
8158 /*
8159 * Reassert ourselves as the active window. This is so that after creating
8160 * a tearoff, the user doesn't have to click with the mouse just to start
8161 * typing again!
8162 */
8163 (void)SetActiveWindow(s_hwnd);
8164
8165 /* make sure the right buttons are enabled */
8166 force_menu_update = TRUE;
8167}
8168#endif
8169
8170#if defined(FEAT_TOOLBAR) || defined(PROTO)
8171#include "gui_w32_rc.h"
8172
8173/* This not defined in older SDKs */
8174# ifndef TBSTYLE_FLAT
8175# define TBSTYLE_FLAT 0x0800
8176# endif
8177
8178/*
8179 * Create the toolbar, initially unpopulated.
8180 * (just like the menu, there are no defaults, it's all
8181 * set up through menu.vim)
8182 */
8183 static void
8184initialise_toolbar(void)
8185{
8186 InitCommonControls();
8187 s_toolbarhwnd = CreateToolbarEx(
8188 s_hwnd,
8189 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8190 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008191 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 s_hinst,
8193 IDR_TOOLBAR1, // id of initial bitmap
8194 NULL,
8195 0, // initial number of buttons
8196 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8197 TOOLBAR_BUTTON_HEIGHT,
8198 TOOLBAR_BUTTON_WIDTH,
8199 TOOLBAR_BUTTON_HEIGHT,
8200 sizeof(TBBUTTON)
8201 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008202 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203
8204 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8205}
8206
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008207 static LRESULT CALLBACK
8208toolbar_wndproc(
8209 HWND hwnd,
8210 UINT uMsg,
8211 WPARAM wParam,
8212 LPARAM lParam)
8213{
8214 HandleMouseHide(uMsg, lParam);
8215 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8216}
8217
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 static int
8219get_toolbar_bitmap(vimmenu_T *menu)
8220{
8221 int i = -1;
8222
8223 /*
8224 * Check user bitmaps first, unless builtin is specified.
8225 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008226 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
8228 char_u fname[MAXPATHL];
8229 HANDLE hbitmap = NULL;
8230
8231 if (menu->iconfile != NULL)
8232 {
8233 gui_find_iconfile(menu->iconfile, fname, "bmp");
8234 hbitmap = LoadImage(
8235 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008236 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 IMAGE_BITMAP,
8238 TOOLBAR_BUTTON_WIDTH,
8239 TOOLBAR_BUTTON_HEIGHT,
8240 LR_LOADFROMFILE |
8241 LR_LOADMAP3DCOLORS
8242 );
8243 }
8244
8245 /*
8246 * If the LoadImage call failed, or the "icon=" file
8247 * didn't exist or wasn't specified, try the menu name
8248 */
8249 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008250 && (gui_find_bitmap(
8251#ifdef FEAT_MULTI_LANG
8252 menu->en_dname != NULL ? menu->en_dname :
8253#endif
8254 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 hbitmap = LoadImage(
8256 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008257 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 IMAGE_BITMAP,
8259 TOOLBAR_BUTTON_WIDTH,
8260 TOOLBAR_BUTTON_HEIGHT,
8261 LR_LOADFROMFILE |
8262 LR_LOADMAP3DCOLORS
8263 );
8264
8265 if (hbitmap != NULL)
8266 {
8267 TBADDBITMAP tbAddBitmap;
8268
8269 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008270 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
8272 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8273 (WPARAM)1, (LPARAM)&tbAddBitmap);
8274 /* i will be set to -1 if it fails */
8275 }
8276 }
8277 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8278 i = menu->iconidx;
8279
8280 return i;
8281}
8282#endif
8283
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008284#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8285 static void
8286initialise_tabline(void)
8287{
8288 InitCommonControls();
8289
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008290 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008291 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008292 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8293 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008294 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008295
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008296 gui.tabline_height = TABLINE_HEIGHT;
8297
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008298# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008299 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008300# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008301}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008302
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008303/*
8304 * Get tabpage_T from POINT.
8305 */
8306 static tabpage_T *
8307GetTabFromPoint(
8308 HWND hWnd,
8309 POINT pt)
8310{
8311 tabpage_T *ptp = NULL;
8312
8313 if (gui_mch_showing_tabline())
8314 {
8315 TCHITTESTINFO htinfo;
8316 htinfo.pt = pt;
8317 /* ignore if a window under cusor is not tabcontrol. */
8318 if (s_tabhwnd == hWnd)
8319 {
8320 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8321 if (idx != -1)
8322 ptp = find_tabpage(idx + 1);
8323 }
8324 }
8325 return ptp;
8326}
8327
8328static POINT s_pt = {0, 0};
8329static HCURSOR s_hCursor = NULL;
8330
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008331 static LRESULT CALLBACK
8332tabline_wndproc(
8333 HWND hwnd,
8334 UINT uMsg,
8335 WPARAM wParam,
8336 LPARAM lParam)
8337{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008338 POINT pt;
8339 tabpage_T *tp;
8340 RECT rect;
8341 int nCenter;
8342 int idx0;
8343 int idx1;
8344
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008345 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008346
8347 switch (uMsg)
8348 {
8349 case WM_LBUTTONDOWN:
8350 {
8351 s_pt.x = GET_X_LPARAM(lParam);
8352 s_pt.y = GET_Y_LPARAM(lParam);
8353 SetCapture(hwnd);
8354 s_hCursor = GetCursor(); /* backup default cursor */
8355 break;
8356 }
8357 case WM_MOUSEMOVE:
8358 if (GetCapture() == hwnd
8359 && ((wParam & MK_LBUTTON)) != 0)
8360 {
8361 pt.x = GET_X_LPARAM(lParam);
8362 pt.y = s_pt.y;
8363 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8364 {
8365 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8366
8367 tp = GetTabFromPoint(hwnd, pt);
8368 if (tp != NULL)
8369 {
8370 idx0 = tabpage_index(curtab) - 1;
8371 idx1 = tabpage_index(tp) - 1;
8372
8373 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8374 nCenter = rect.left + (rect.right - rect.left) / 2;
8375
8376 /* Check if the mouse cursor goes over the center of
8377 * the next tab to prevent "flickering". */
8378 if ((idx0 < idx1) && (nCenter < pt.x))
8379 {
8380 tabpage_move(idx1 + 1);
8381 update_screen(0);
8382 }
8383 else if ((idx1 < idx0) && (pt.x < nCenter))
8384 {
8385 tabpage_move(idx1);
8386 update_screen(0);
8387 }
8388 }
8389 }
8390 }
8391 break;
8392 case WM_LBUTTONUP:
8393 {
8394 if (GetCapture() == hwnd)
8395 {
8396 SetCursor(s_hCursor);
8397 ReleaseCapture();
8398 }
8399 break;
8400 }
8401 default:
8402 break;
8403 }
8404
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008405 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8406}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008407#endif
8408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8410/*
8411 * Make the GUI window come to the foreground.
8412 */
8413 void
8414gui_mch_set_foreground(void)
8415{
8416 if (IsIconic(s_hwnd))
8417 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8418 SetForegroundWindow(s_hwnd);
8419}
8420#endif
8421
8422#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8423 static void
8424dyn_imm_load(void)
8425{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008426 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (hLibImm == NULL)
8428 return;
8429
8430 pImmGetCompositionStringA
8431 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8432 pImmGetCompositionStringW
8433 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8434 pImmGetContext
8435 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8436 pImmAssociateContext
8437 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8438 pImmReleaseContext
8439 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8440 pImmGetOpenStatus
8441 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8442 pImmSetOpenStatus
8443 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8444 pImmGetCompositionFont
8445 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8446 pImmSetCompositionFont
8447 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8448 pImmSetCompositionWindow
8449 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8450 pImmGetConversionStatus
8451 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008452 pImmSetConversionStatus
8453 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454
8455 if ( pImmGetCompositionStringA == NULL
8456 || pImmGetCompositionStringW == NULL
8457 || pImmGetContext == NULL
8458 || pImmAssociateContext == NULL
8459 || pImmReleaseContext == NULL
8460 || pImmGetOpenStatus == NULL
8461 || pImmSetOpenStatus == NULL
8462 || pImmGetCompositionFont == NULL
8463 || pImmSetCompositionFont == NULL
8464 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008465 || pImmGetConversionStatus == NULL
8466 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {
8468 FreeLibrary(hLibImm);
8469 hLibImm = NULL;
8470 pImmGetContext = NULL;
8471 return;
8472 }
8473
8474 return;
8475}
8476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477#endif
8478
8479#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8480
8481# ifdef FEAT_XPM_W32
8482# define IMAGE_XPM 100
8483# endif
8484
8485typedef struct _signicon_t
8486{
8487 HANDLE hImage;
8488 UINT uType;
8489#ifdef FEAT_XPM_W32
8490 HANDLE hShape; /* Mask bitmap handle */
8491#endif
8492} signicon_t;
8493
8494 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008495gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 signicon_t *sign;
8498 int x, y, w, h;
8499
8500 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8501 return;
8502
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008503#if defined(FEAT_DIRECTX)
8504 if (IS_ENABLE_DIRECTX())
8505 DWriteContext_Flush(s_dwc);
8506#endif
8507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 x = TEXT_X(col);
8509 y = TEXT_Y(row);
8510 w = gui.char_width * 2;
8511 h = gui.char_height;
8512 switch (sign->uType)
8513 {
8514 case IMAGE_BITMAP:
8515 {
8516 HDC hdcMem;
8517 HBITMAP hbmpOld;
8518
8519 hdcMem = CreateCompatibleDC(s_hdc);
8520 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8521 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8522 SelectObject(hdcMem, hbmpOld);
8523 DeleteDC(hdcMem);
8524 }
8525 break;
8526 case IMAGE_ICON:
8527 case IMAGE_CURSOR:
8528 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8529 break;
8530#ifdef FEAT_XPM_W32
8531 case IMAGE_XPM:
8532 {
8533 HDC hdcMem;
8534 HBITMAP hbmpOld;
8535
8536 hdcMem = CreateCompatibleDC(s_hdc);
8537 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8538 /* Make hole */
8539 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8540
8541 SelectObject(hdcMem, sign->hImage);
8542 /* Paint sign */
8543 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8544 SelectObject(hdcMem, hbmpOld);
8545 DeleteDC(hdcMem);
8546 }
8547 break;
8548#endif
8549 }
8550}
8551
8552 static void
8553close_signicon_image(signicon_t *sign)
8554{
8555 if (sign)
8556 switch (sign->uType)
8557 {
8558 case IMAGE_BITMAP:
8559 DeleteObject((HGDIOBJ)sign->hImage);
8560 break;
8561 case IMAGE_CURSOR:
8562 DestroyCursor((HCURSOR)sign->hImage);
8563 break;
8564 case IMAGE_ICON:
8565 DestroyIcon((HICON)sign->hImage);
8566 break;
8567#ifdef FEAT_XPM_W32
8568 case IMAGE_XPM:
8569 DeleteObject((HBITMAP)sign->hImage);
8570 DeleteObject((HBITMAP)sign->hShape);
8571 break;
8572#endif
8573 }
8574}
8575
8576 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008577gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 signicon_t sign, *psign;
8580 char_u *ext;
8581
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008583 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (ext > signfile)
8585 {
8586 int do_load = 1;
8587
8588 if (!STRICMP(ext, ".bmp"))
8589 sign.uType = IMAGE_BITMAP;
8590 else if (!STRICMP(ext, ".ico"))
8591 sign.uType = IMAGE_ICON;
8592 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8593 sign.uType = IMAGE_CURSOR;
8594 else
8595 do_load = 0;
8596
8597 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008598 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 gui.char_width * 2, gui.char_height,
8600 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8601#ifdef FEAT_XPM_W32
8602 if (!STRICMP(ext, ".xpm"))
8603 {
8604 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008605 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8606 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 }
8608#endif
8609 }
8610
8611 psign = NULL;
8612 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8613 != NULL)
8614 *psign = sign;
8615
8616 if (!psign)
8617 {
8618 if (sign.hImage)
8619 close_signicon_image(&sign);
8620 EMSG(_(e_signdata));
8621 }
8622 return (void *)psign;
8623
8624}
8625
8626 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008627gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 if (sign)
8630 {
8631 close_signicon_image((signicon_t *)sign);
8632 vim_free(sign);
8633 }
8634}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008637#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638
8639/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008640 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008642 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8644 * to get current mouse position).
8645 *
8646 * Trying to use as more Windows services as possible, and as less
8647 * IE version as possible :)).
8648 *
8649 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8650 * BalloonEval struct.
8651 * 2) Enable/Disable simply create/kill BalloonEval Timer
8652 * 3) When there was enough inactivity, timer procedure posts
8653 * async request to debugger
8654 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8655 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008656 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 */
8658
Bram Moolenaar45360022005-07-21 21:08:21 +00008659/*
8660 * determine whether installed Common Controls support multiline tooltips
8661 * (i.e. their version is >= 4.70
8662 */
8663 int
8664multiline_balloon_available(void)
8665{
8666 HINSTANCE hDll;
8667 static char comctl_dll[] = "comctl32.dll";
8668 static int multiline_tip = MAYBE;
8669
8670 if (multiline_tip != MAYBE)
8671 return multiline_tip;
8672
8673 hDll = GetModuleHandle(comctl_dll);
8674 if (hDll != NULL)
8675 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008676 DLLGETVERSIONPROC pGetVer;
8677 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008678
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008679 if (pGetVer != NULL)
8680 {
8681 DLLVERSIONINFO dvi;
8682 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008683
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008684 ZeroMemory(&dvi, sizeof(dvi));
8685 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008686
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008687 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008688
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008689 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008690 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008691 || (dvi.dwMajorVersion == 4
8692 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008693 {
8694 multiline_tip = TRUE;
8695 return multiline_tip;
8696 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008697 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008698 else
8699 {
8700 /* there is chance we have ancient CommCtl 4.70
8701 which doesn't export DllGetVersion */
8702 DWORD dwHandle = 0;
8703 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8704 if (len > 0)
8705 {
8706 VS_FIXEDFILEINFO *ver;
8707 UINT vlen = 0;
8708 void *data = alloc(len);
8709
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008710 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008711 && GetFileVersionInfo(comctl_dll, 0, len, data)
8712 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8713 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008714 && HIWORD(ver->dwFileVersionMS) > 4)
8715 || ((HIWORD(ver->dwFileVersionMS) == 4
8716 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008717 {
8718 vim_free(data);
8719 multiline_tip = TRUE;
8720 return multiline_tip;
8721 }
8722 vim_free(data);
8723 }
8724 }
8725 }
8726 multiline_tip = FALSE;
8727 return multiline_tip;
8728}
8729
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008731make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
Bram Moolenaar45360022005-07-21 21:08:21 +00008733 TOOLINFO *pti;
8734 int ToolInfoSize;
8735
8736 if (multiline_balloon_available() == TRUE)
8737 ToolInfoSize = sizeof(TOOLINFO_NEW);
8738 else
8739 ToolInfoSize = sizeof(TOOLINFO);
8740
8741 pti = (TOOLINFO *)alloc(ToolInfoSize);
8742 if (pti == NULL)
8743 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744
8745 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8746 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8747 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8748 beval->target, NULL, s_hinst, NULL);
8749
8750 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8751 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8752
Bram Moolenaar45360022005-07-21 21:08:21 +00008753 pti->cbSize = ToolInfoSize;
8754 pti->uFlags = TTF_SUBCLASS;
8755 pti->hwnd = beval->target;
8756 pti->hinst = 0; /* Don't use string resources */
8757 pti->uId = ID_BEVAL_TOOLTIP;
8758
8759 if (multiline_balloon_available() == TRUE)
8760 {
8761 RECT rect;
8762 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8763 pti->lpszText = LPSTR_TEXTCALLBACK;
8764 ptin->lParam = (LPARAM)text;
8765 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8766 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8767 (LPARAM)rect.right);
8768 }
8769 else
8770 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771
8772 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008773 pti->rect.left = pt.x - 3;
8774 pti->rect.top = pt.y - 3;
8775 pti->rect.right = pt.x + 3;
8776 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777
Bram Moolenaar45360022005-07-21 21:08:21 +00008778 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 /* Make tooltip appear sooner */
8780 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008781 /* I've performed some tests and it seems the longest possible life time
8782 * of tooltip is 30 seconds */
8783 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 /*
8785 * HACK: force tooltip to appear, because it'll not appear until
8786 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008787 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008789 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008791 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793
8794 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008795delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008797 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798}
8799
8800 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008801BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008802 HWND hwnd UNUSED,
8803 UINT uMsg UNUSED,
8804 UINT_PTR idEvent UNUSED,
8805 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806{
8807 POINT pt;
8808 RECT rect;
8809
8810 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8811 return;
8812
8813 GetCursorPos(&pt);
8814 if (WindowFromPoint(pt) != s_textArea)
8815 return;
8816
8817 ScreenToClient(s_textArea, &pt);
8818 GetClientRect(s_textArea, &rect);
8819 if (!PtInRect(&rect, pt))
8820 return;
8821
8822 if (LastActivity > 0
8823 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8824 && (cur_beval->showState != ShS_PENDING
8825 || abs(cur_beval->x - pt.x) > 3
8826 || abs(cur_beval->y - pt.y) > 3))
8827 {
8828 /* Pointer resting in one place long enough, it's time to show
8829 * the tooltip. */
8830 cur_beval->showState = ShS_PENDING;
8831 cur_beval->x = pt.x;
8832 cur_beval->y = pt.y;
8833
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008834 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835
8836 if (cur_beval->msgCB != NULL)
8837 (*cur_beval->msgCB)(cur_beval, 0);
8838 }
8839}
8840
8841 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008842gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008844 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008846 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847}
8848
8849 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008850gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008852 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (beval == NULL)
8854 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008855 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008856 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008857 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858}
8859
8860 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008861gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862{
8863 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008864
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008865 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (beval->showState == ShS_SHOWING)
8867 return;
8868 GetCursorPos(&pt);
8869 ScreenToClient(s_textArea, &pt);
8870
8871 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008873 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 gui_mch_disable_beval_area(cur_beval);
8875 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008876 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008878 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879}
8880
8881 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008882gui_mch_create_beval_area(
8883 void *target, /* ignored, always use s_textArea */
8884 char_u *mesg,
8885 void (*mesgCB)(BalloonEval *, int),
8886 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 /* partially stolen from gui_beval.c */
8889 BalloonEval *beval;
8890
8891 if (mesg != NULL && mesgCB != NULL)
8892 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008893 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 return NULL;
8895 }
8896
8897 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8898 if (beval != NULL)
8899 {
8900 beval->target = s_textArea;
8901 beval->balloon = NULL;
8902
8903 beval->showState = ShS_NEUTRAL;
8904 beval->x = 0;
8905 beval->y = 0;
8906 beval->msg = mesg;
8907 beval->msgCB = mesgCB;
8908 beval->clientData = clientData;
8909
8910 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 cur_beval = beval;
8912
8913 if (p_beval)
8914 gui_mch_enable_beval_area(beval);
8915
8916 }
8917 return beval;
8918}
8919
8920 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008921Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8924 return;
8925
8926 if (cur_beval != NULL)
8927 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008928 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008930 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008931 // TRACE0("TTN_SHOW {{{");
8932 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008933 break;
8934 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008935 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 delete_tooltip(cur_beval);
8937 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008938 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939
8940 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008941 break;
8942 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008943 {
8944 /* if you get there then we have new common controls */
8945 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8946 info->lpszText = (LPSTR)info->lParam;
8947 info->uFlags |= TTF_DI_SETITEM;
8948 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008949 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 }
8952}
8953
8954 static void
8955TrackUserActivity(UINT uMsg)
8956{
8957 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8958 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8959 LastActivity = GetTickCount();
8960}
8961
8962 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008963gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 vim_free(beval);
8966}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008967#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8970/*
8971 * We have multiple signs to draw at the same location. Draw the
8972 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8973 */
8974 void
8975netbeans_draw_multisign_indicator(int row)
8976{
8977 int i;
8978 int y;
8979 int x;
8980
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008981 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008982 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008983
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 x = 0;
8985 y = TEXT_Y(row);
8986
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008987#if defined(FEAT_DIRECTX)
8988 if (IS_ENABLE_DIRECTX())
8989 DWriteContext_Flush(s_dwc);
8990#endif
8991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 for (i = 0; i < gui.char_height - 3; i++)
8993 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8994
8995 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8996 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8997 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8998 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8999 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9000 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9001 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9002}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009003#endif