blob: 119bcd579063fe143ffd80fb78ebbfaeb1e0ce32 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar92467d32017-12-05 13:22:16 +010036static int s_directx_scrlines = 0;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020037# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010038static int directx_enabled(void);
39static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010040#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020041
Bram Moolenaar065bbac2016-02-20 13:08:46 +010042#ifdef FEAT_MENU
43static int gui_mswin_get_menu_height(int fix_window);
44#endif
45
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
47 int
48gui_mch_set_rendering_options(char_u *s)
49{
50#ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020051 char_u *p, *q;
52
53 int dx_enable = 0;
54 int dx_flags = 0;
55 float dx_gamma = 0.0f;
56 float dx_contrast = 0.0f;
57 float dx_level = 0.0f;
58 int dx_geom = 0;
59 int dx_renmode = 0;
60 int dx_taamode = 0;
Bram Moolenaar92467d32017-12-05 13:22:16 +010061 int dx_scrlines = 0;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062
63 /* parse string as rendering options. */
64 for (p = s; p != NULL && *p != NUL; )
65 {
66 char_u item[256];
67 char_u name[128];
68 char_u value[128];
69
Bram Moolenaarcde88542015-08-11 19:14:00 +020070 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020071 if (p == NULL)
72 break;
73 q = &item[0];
74 copy_option_part(&q, name, sizeof(name), ":");
75 if (q == NULL)
76 return FAIL;
77 copy_option_part(&q, value, sizeof(value), ":");
78
79 if (STRCMP(name, "type") == 0)
80 {
81 if (STRCMP(value, "directx") == 0)
82 dx_enable = 1;
83 else
84 return FAIL;
85 }
86 else if (STRCMP(name, "gamma") == 0)
87 {
88 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010089 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020090 }
91 else if (STRCMP(name, "contrast") == 0)
92 {
93 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010094 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020095 }
96 else if (STRCMP(name, "level") == 0)
97 {
98 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010099 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200100 }
101 else if (STRCMP(name, "geom") == 0)
102 {
103 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100104 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200105 if (dx_geom < 0 || dx_geom > 2)
106 return FAIL;
107 }
108 else if (STRCMP(name, "renmode") == 0)
109 {
110 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100111 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200112 if (dx_renmode < 0 || dx_renmode > 6)
113 return FAIL;
114 }
115 else if (STRCMP(name, "taamode") == 0)
116 {
117 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100118 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200119 if (dx_taamode < 0 || dx_taamode > 3)
120 return FAIL;
121 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100122 else if (STRCMP(name, "scrlines") == 0)
123 {
124 dx_scrlines = atoi((char *)value);
125 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200126 else
127 return FAIL;
128 }
129
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130 if (!gui.in_use)
131 return OK; /* only checking the syntax of the value */
132
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200133 /* Enable DirectX/DirectWrite */
134 if (dx_enable)
135 {
136 if (!directx_enabled())
137 return FAIL;
138 DWriteContext_SetRenderingParams(s_dwc, NULL);
139 if (dx_flags)
140 {
141 DWriteRenderingParams param;
142 DWriteContext_GetRenderingParams(s_dwc, &param);
143 if (dx_flags & (1 << 0))
144 param.gamma = dx_gamma;
145 if (dx_flags & (1 << 1))
146 param.enhancedContrast = dx_contrast;
147 if (dx_flags & (1 << 2))
148 param.clearTypeLevel = dx_level;
149 if (dx_flags & (1 << 3))
150 param.pixelGeometry = dx_geom;
151 if (dx_flags & (1 << 4))
152 param.renderingMode = dx_renmode;
153 if (dx_flags & (1 << 5))
154 param.textAntialiasMode = dx_taamode;
155 DWriteContext_SetRenderingParams(s_dwc, &param);
156 }
157 }
158 s_directx_enabled = dx_enable;
Bram Moolenaar92467d32017-12-05 13:22:16 +0100159 s_directx_scrlines = dx_scrlines;
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160
161 return OK;
162#else
163 return FAIL;
164#endif
165}
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
641gui_mch_stop_blink(void)
642{
643 gui_mswin_rm_blink_timer();
644 if (blink_state == BLINK_OFF)
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
2114 gui_mch_stop_blink();
2115 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#if defined(FEAT_DIRECTX)
Bram Moolenaar92467d32017-12-05 13:22:16 +01003130 int use_redraw = 0;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003131#endif
3132
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133 rc.left = FILL_X(gui.scroll_region_left);
3134 rc.right = FILL_X(gui.scroll_region_right + 1);
3135 rc.top = FILL_Y(row);
3136 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3137
Bram Moolenaar92467d32017-12-05 13:22:16 +01003138#if defined(FEAT_DIRECTX)
3139 if (IS_ENABLE_DIRECTX())
3140 {
3141 if (s_directx_scrlines > 0 && s_directx_scrlines <= num_lines)
3142 {
3143 RedrawWindow(s_textArea, &rc, NULL, RDW_INVALIDATE);
3144 use_redraw = 1;
3145 }
3146 else
3147 DWriteContext_Flush(s_dwc);
3148 }
3149 if (!use_redraw)
3150#endif
3151 {
3152 intel_gpu_workaround();
3153 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003154 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar92467d32017-12-05 13:22:16 +01003155 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156
3157 UpdateWindow(s_textArea);
3158 /* This seems to be required to avoid the cursor disappearing when
3159 * scrolling such that the cursor ends up in the top-left character on
3160 * the screen... But why? (Webb) */
3161 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3162 /* gui.cursor_is_valid = FALSE; */
3163
3164 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3165 gui.scroll_region_left,
3166 gui.scroll_region_bot, gui.scroll_region_right);
3167}
3168
3169/*
3170 * Insert the given number of lines before the given row, scrolling down any
3171 * following text within the scroll region.
3172 */
3173 void
3174gui_mch_insert_lines(
3175 int row,
3176 int num_lines)
3177{
3178 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003179#if defined(FEAT_DIRECTX)
Bram Moolenaar92467d32017-12-05 13:22:16 +01003180 int use_redraw = 0;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003181#endif
3182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183 rc.left = FILL_X(gui.scroll_region_left);
3184 rc.right = FILL_X(gui.scroll_region_right + 1);
3185 rc.top = FILL_Y(row);
3186 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003187
3188#if defined(FEAT_DIRECTX)
3189 if (IS_ENABLE_DIRECTX())
3190 {
3191 if (s_directx_scrlines > 0 && s_directx_scrlines <= num_lines)
3192 {
3193 RedrawWindow(s_textArea, &rc, NULL, RDW_INVALIDATE);
3194 use_redraw = 1;
3195 }
3196 else
3197 DWriteContext_Flush(s_dwc);
3198 }
3199 if (!use_redraw)
3200#endif
3201 {
3202 intel_gpu_workaround();
3203 /* The SW_INVALIDATE is required when part of the window is covered or
3204 * off-screen. How do we avoid it when it's not needed? */
3205 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar92467d32017-12-05 13:22:16 +01003207 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003208
3209 UpdateWindow(s_textArea);
3210
3211 gui_clear_block(row, gui.scroll_region_left,
3212 row + num_lines - 1, gui.scroll_region_right);
3213}
3214
3215
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003216 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003217gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218{
3219#if defined(FEAT_DIRECTX)
3220 DWriteContext_Close(s_dwc);
3221 DWrite_Final();
3222 s_dwc = NULL;
3223#endif
3224
3225 ReleaseDC(s_textArea, s_hdc);
3226 DeleteObject(s_brush);
3227
3228#ifdef FEAT_TEAROFF
3229 /* Unload the tearoff bitmap */
3230 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3231#endif
3232
3233 /* Destroy our window (if we have one). */
3234 if (s_hwnd != NULL)
3235 {
3236 destroying = TRUE; /* ignore WM_DESTROY message now */
3237 DestroyWindow(s_hwnd);
3238 }
3239
3240#ifdef GLOBAL_IME
3241 global_ime_end();
3242#endif
3243}
3244
3245 static char_u *
3246logfont2name(LOGFONT lf)
3247{
3248 char *p;
3249 char *res;
3250 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003251 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003252 char *font_name = lf.lfFaceName;
3253
3254 charset_name = charset_id2name((int)lf.lfCharSet);
3255#ifdef FEAT_MBYTE
3256 /* Convert a font name from the current codepage to 'encoding'.
3257 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3258 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3259 {
3260 int len;
3261 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3262 (char_u **)&font_name, &len);
3263 }
3264#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003265 quality_name = quality_id2name((int)lf.lfQuality);
3266
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3268 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3269 if (res != NULL)
3270 {
3271 p = res;
3272 /* make a normal font string out of the lf thing:*/
3273 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3274 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3275 while (*p)
3276 {
3277 if (*p == ' ')
3278 *p = '_';
3279 ++p;
3280 }
3281 if (lf.lfItalic)
3282 STRCAT(p, ":i");
3283 if (lf.lfWeight >= FW_BOLD)
3284 STRCAT(p, ":b");
3285 if (lf.lfUnderline)
3286 STRCAT(p, ":u");
3287 if (lf.lfStrikeOut)
3288 STRCAT(p, ":s");
3289 if (charset_name != NULL)
3290 {
3291 STRCAT(p, ":c");
3292 STRCAT(p, charset_name);
3293 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003294 if (quality_name != NULL)
3295 {
3296 STRCAT(p, ":q");
3297 STRCAT(p, quality_name);
3298 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299 }
3300
3301#ifdef FEAT_MBYTE
3302 if (font_name != lf.lfFaceName)
3303 vim_free(font_name);
3304#endif
3305 return (char_u *)res;
3306}
3307
3308
3309#ifdef FEAT_MBYTE_IME
3310/*
3311 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3312 * 'guifont'
3313 */
3314 static void
3315update_im_font(void)
3316{
3317 LOGFONT lf_wide;
3318
3319 if (p_guifontwide != NULL && *p_guifontwide != NUL
3320 && gui.wide_font != NOFONT
3321 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3322 norm_logfont = lf_wide;
3323 else
3324 norm_logfont = sub_logfont;
3325 im_set_font(&norm_logfont);
3326}
3327#endif
3328
3329#ifdef FEAT_MBYTE
3330/*
3331 * Handler of gui.wide_font (p_guifontwide) changed notification.
3332 */
3333 void
3334gui_mch_wide_font_changed(void)
3335{
3336 LOGFONT lf;
3337
3338# ifdef FEAT_MBYTE_IME
3339 update_im_font();
3340# endif
3341
3342 gui_mch_free_font(gui.wide_ital_font);
3343 gui.wide_ital_font = NOFONT;
3344 gui_mch_free_font(gui.wide_bold_font);
3345 gui.wide_bold_font = NOFONT;
3346 gui_mch_free_font(gui.wide_boldital_font);
3347 gui.wide_boldital_font = NOFONT;
3348
3349 if (gui.wide_font
3350 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3351 {
3352 if (!lf.lfItalic)
3353 {
3354 lf.lfItalic = TRUE;
3355 gui.wide_ital_font = get_font_handle(&lf);
3356 lf.lfItalic = FALSE;
3357 }
3358 if (lf.lfWeight < FW_BOLD)
3359 {
3360 lf.lfWeight = FW_BOLD;
3361 gui.wide_bold_font = get_font_handle(&lf);
3362 if (!lf.lfItalic)
3363 {
3364 lf.lfItalic = TRUE;
3365 gui.wide_boldital_font = get_font_handle(&lf);
3366 }
3367 }
3368 }
3369}
3370#endif
3371
3372/*
3373 * Initialise vim to use the font with the given name.
3374 * Return FAIL if the font could not be loaded, OK otherwise.
3375 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003377gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378{
3379 LOGFONT lf;
3380 GuiFont font = NOFONT;
3381 char_u *p;
3382
3383 /* Load the font */
3384 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3385 font = get_font_handle(&lf);
3386 if (font == NOFONT)
3387 return FAIL;
3388
3389 if (font_name == NULL)
3390 font_name = (char_u *)lf.lfFaceName;
3391#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3392 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003393#endif
3394#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003395 sub_logfont = lf;
3396#endif
3397#ifdef FEAT_MBYTE_IME
3398 update_im_font();
3399#endif
3400 gui_mch_free_font(gui.norm_font);
3401 gui.norm_font = font;
3402 current_font_height = lf.lfHeight;
3403 GetFontSize(font);
3404
3405 p = logfont2name(lf);
3406 if (p != NULL)
3407 {
3408 hl_set_font_name(p);
3409
3410 /* When setting 'guifont' to "*" replace it with the actual font name.
3411 * */
3412 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3413 {
3414 vim_free(p_guifont);
3415 p_guifont = p;
3416 }
3417 else
3418 vim_free(p);
3419 }
3420
3421 gui_mch_free_font(gui.ital_font);
3422 gui.ital_font = NOFONT;
3423 gui_mch_free_font(gui.bold_font);
3424 gui.bold_font = NOFONT;
3425 gui_mch_free_font(gui.boldital_font);
3426 gui.boldital_font = NOFONT;
3427
3428 if (!lf.lfItalic)
3429 {
3430 lf.lfItalic = TRUE;
3431 gui.ital_font = get_font_handle(&lf);
3432 lf.lfItalic = FALSE;
3433 }
3434 if (lf.lfWeight < FW_BOLD)
3435 {
3436 lf.lfWeight = FW_BOLD;
3437 gui.bold_font = get_font_handle(&lf);
3438 if (!lf.lfItalic)
3439 {
3440 lf.lfItalic = TRUE;
3441 gui.boldital_font = get_font_handle(&lf);
3442 }
3443 }
3444
3445 return OK;
3446}
3447
3448#ifndef WPF_RESTORETOMAXIMIZED
3449# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3450#endif
3451
3452/*
3453 * Return TRUE if the GUI window is maximized, filling the whole screen.
3454 */
3455 int
3456gui_mch_maximized(void)
3457{
3458 WINDOWPLACEMENT wp;
3459
3460 wp.length = sizeof(WINDOWPLACEMENT);
3461 if (GetWindowPlacement(s_hwnd, &wp))
3462 return wp.showCmd == SW_SHOWMAXIMIZED
3463 || (wp.showCmd == SW_SHOWMINIMIZED
3464 && wp.flags == WPF_RESTORETOMAXIMIZED);
3465
3466 return 0;
3467}
3468
3469/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003470 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3471 * is set. Compute the new Rows and Columns. This is like resizing the
3472 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473 */
3474 void
3475gui_mch_newfont(void)
3476{
3477 RECT rect;
3478
3479 GetWindowRect(s_hwnd, &rect);
3480 if (win_socket_id == 0)
3481 {
3482 gui_resize_shell(rect.right - rect.left
3483 - (GetSystemMetrics(SM_CXFRAME) +
3484 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3485 rect.bottom - rect.top
3486 - (GetSystemMetrics(SM_CYFRAME) +
3487 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3488 - GetSystemMetrics(SM_CYCAPTION)
3489#ifdef FEAT_MENU
3490 - gui_mswin_get_menu_height(FALSE)
3491#endif
3492 );
3493 }
3494 else
3495 {
3496 /* Inside another window, don't use the frame and border. */
3497 gui_resize_shell(rect.right - rect.left,
3498 rect.bottom - rect.top
3499#ifdef FEAT_MENU
3500 - gui_mswin_get_menu_height(FALSE)
3501#endif
3502 );
3503 }
3504}
3505
3506/*
3507 * Set the window title
3508 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003509 void
3510gui_mch_settitle(
3511 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003512 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513{
3514 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3515}
3516
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003517#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3519 * misc2.c! */
3520static LPCSTR mshape_idcs[] =
3521{
3522 IDC_ARROW, /* arrow */
3523 MAKEINTRESOURCE(0), /* blank */
3524 IDC_IBEAM, /* beam */
3525 IDC_SIZENS, /* updown */
3526 IDC_SIZENS, /* udsizing */
3527 IDC_SIZEWE, /* leftright */
3528 IDC_SIZEWE, /* lrsizing */
3529 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003530 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003531 IDC_ARROW, /* crosshair */
3532 IDC_ARROW, /* hand1 */
3533 IDC_ARROW, /* hand2 */
3534 IDC_ARROW, /* pencil */
3535 IDC_ARROW, /* question */
3536 IDC_ARROW, /* right-arrow */
3537 IDC_UPARROW, /* up-arrow */
3538 IDC_ARROW /* last one */
3539};
3540
3541 void
3542mch_set_mouse_shape(int shape)
3543{
3544 LPCSTR idc;
3545
3546 if (shape == MSHAPE_HIDE)
3547 ShowCursor(FALSE);
3548 else
3549 {
3550 if (shape >= MSHAPE_NUMBERED)
3551 idc = IDC_ARROW;
3552 else
3553 idc = mshape_idcs[shape];
3554#ifdef SetClassLongPtr
3555 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3556#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003558#endif
3559 if (!p_mh)
3560 {
3561 POINT mp;
3562
3563 /* Set the position to make it redrawn with the new shape. */
3564 (void)GetCursorPos((LPPOINT)&mp);
3565 (void)SetCursorPos(mp.x, mp.y);
3566 ShowCursor(TRUE);
3567 }
3568 }
3569}
3570#endif
3571
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003572#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573/*
3574 * The file browser exists in two versions: with "W" uses wide characters,
3575 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3576 * Windows NT/2000/XP the "W" functions are used.
3577 */
3578
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003579# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003580/*
3581 * Wide version of convert_filter().
3582 */
3583 static WCHAR *
3584convert_filterW(char_u *s)
3585{
3586 char_u *tmp;
3587 int len;
3588 WCHAR *res;
3589
3590 tmp = convert_filter(s);
3591 if (tmp == NULL)
3592 return NULL;
3593 len = (int)STRLEN(s) + 3;
3594 res = enc_to_utf16(tmp, &len);
3595 vim_free(tmp);
3596 return res;
3597}
3598
3599/*
3600 * Wide version of gui_mch_browse(). Keep in sync!
3601 */
3602 static char_u *
3603gui_mch_browseW(
3604 int saving,
3605 char_u *title,
3606 char_u *dflt,
3607 char_u *ext,
3608 char_u *initdir,
3609 char_u *filter)
3610{
3611 /* We always use the wide function. This means enc_to_utf16() must work,
3612 * otherwise it fails miserably! */
3613 OPENFILENAMEW fileStruct;
3614 WCHAR fileBuf[MAXPATHL];
3615 WCHAR *wp;
3616 int i;
3617 WCHAR *titlep = NULL;
3618 WCHAR *extp = NULL;
3619 WCHAR *initdirp = NULL;
3620 WCHAR *filterp;
3621 char_u *p;
3622
3623 if (dflt == NULL)
3624 fileBuf[0] = NUL;
3625 else
3626 {
3627 wp = enc_to_utf16(dflt, NULL);
3628 if (wp == NULL)
3629 fileBuf[0] = NUL;
3630 else
3631 {
3632 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3633 fileBuf[i] = wp[i];
3634 fileBuf[i] = NUL;
3635 vim_free(wp);
3636 }
3637 }
3638
3639 /* Convert the filter to Windows format. */
3640 filterp = convert_filterW(filter);
3641
3642 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003643# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003645 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003646# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003648# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649
3650 if (title != NULL)
3651 titlep = enc_to_utf16(title, NULL);
3652 fileStruct.lpstrTitle = titlep;
3653
3654 if (ext != NULL)
3655 extp = enc_to_utf16(ext, NULL);
3656 fileStruct.lpstrDefExt = extp;
3657
3658 fileStruct.lpstrFile = fileBuf;
3659 fileStruct.nMaxFile = MAXPATHL;
3660 fileStruct.lpstrFilter = filterp;
3661 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3662 /* has an initial dir been specified? */
3663 if (initdir != NULL && *initdir != NUL)
3664 {
3665 /* Must have backslashes here, no matter what 'shellslash' says */
3666 initdirp = enc_to_utf16(initdir, NULL);
3667 if (initdirp != NULL)
3668 {
3669 for (wp = initdirp; *wp != NUL; ++wp)
3670 if (*wp == '/')
3671 *wp = '\\';
3672 }
3673 fileStruct.lpstrInitialDir = initdirp;
3674 }
3675
3676 /*
3677 * TODO: Allow selection of multiple files. Needs another arg to this
3678 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3679 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3680 * files that don't exist yet, so I haven't put it in. What about
3681 * OFN_PATHMUSTEXIST?
3682 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3683 */
3684 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003685# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 if (curbuf->b_p_bin)
3687 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003688# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 if (saving)
3690 {
3691 if (!GetSaveFileNameW(&fileStruct))
3692 return NULL;
3693 }
3694 else
3695 {
3696 if (!GetOpenFileNameW(&fileStruct))
3697 return NULL;
3698 }
3699
3700 vim_free(filterp);
3701 vim_free(initdirp);
3702 vim_free(titlep);
3703 vim_free(extp);
3704
3705 /* Convert from UCS2 to 'encoding'. */
3706 p = utf16_to_enc(fileBuf, NULL);
3707 if (p != NULL)
3708 /* when out of memory we get garbage for non-ASCII chars */
3709 STRCPY(fileBuf, p);
3710 vim_free(p);
3711
3712 /* Give focus back to main window (when using MDI). */
3713 SetFocus(s_hwnd);
3714
3715 /* Shorten the file name if possible */
3716 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3717}
3718# endif /* FEAT_MBYTE */
3719
3720
3721/*
3722 * Convert the string s to the proper format for a filter string by replacing
3723 * the \t and \n delimiters with \0.
3724 * Returns the converted string in allocated memory.
3725 *
3726 * Keep in sync with convert_filterW() above!
3727 */
3728 static char_u *
3729convert_filter(char_u *s)
3730{
3731 char_u *res;
3732 unsigned s_len = (unsigned)STRLEN(s);
3733 unsigned i;
3734
3735 res = alloc(s_len + 3);
3736 if (res != NULL)
3737 {
3738 for (i = 0; i < s_len; ++i)
3739 if (s[i] == '\t' || s[i] == '\n')
3740 res[i] = '\0';
3741 else
3742 res[i] = s[i];
3743 res[s_len] = NUL;
3744 /* Add two extra NULs to make sure it's properly terminated. */
3745 res[s_len + 1] = NUL;
3746 res[s_len + 2] = NUL;
3747 }
3748 return res;
3749}
3750
3751/*
3752 * Select a directory.
3753 */
3754 char_u *
3755gui_mch_browsedir(char_u *title, char_u *initdir)
3756{
3757 /* We fake this: Use a filter that doesn't select anything and a default
3758 * file name that won't be used. */
3759 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3760 initdir, (char_u *)_("Directory\t*.nothing\n"));
3761}
3762
3763/*
3764 * Pop open a file browser and return the file selected, in allocated memory,
3765 * or NULL if Cancel is hit.
3766 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3767 * title - Title message for the file browser dialog.
3768 * dflt - Default name of file.
3769 * ext - Default extension to be added to files without extensions.
3770 * initdir - directory in which to open the browser (NULL = current dir)
3771 * filter - Filter for matched files to choose from.
3772 *
3773 * Keep in sync with gui_mch_browseW() above!
3774 */
3775 char_u *
3776gui_mch_browse(
3777 int saving,
3778 char_u *title,
3779 char_u *dflt,
3780 char_u *ext,
3781 char_u *initdir,
3782 char_u *filter)
3783{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003784# ifdef FEAT_MBYTE
3785 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3786# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003787 OPENFILENAME fileStruct;
3788 char_u fileBuf[MAXPATHL];
3789 char_u *initdirp = NULL;
3790 char_u *filterp;
3791 char_u *p;
3792
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793 if (dflt == NULL)
3794 fileBuf[0] = NUL;
3795 else
3796 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3797
3798 /* Convert the filter to Windows format. */
3799 filterp = convert_filter(filter);
3800
3801 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003802# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 /* be compatible with Windows NT 4.0 */
3804 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003805# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003807# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003808
3809 fileStruct.lpstrTitle = (LPSTR)title;
3810 fileStruct.lpstrDefExt = (LPSTR)ext;
3811
3812 fileStruct.lpstrFile = (LPSTR)fileBuf;
3813 fileStruct.nMaxFile = MAXPATHL;
3814 fileStruct.lpstrFilter = (LPSTR)filterp;
3815 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3816 /* has an initial dir been specified? */
3817 if (initdir != NULL && *initdir != NUL)
3818 {
3819 /* Must have backslashes here, no matter what 'shellslash' says */
3820 initdirp = vim_strsave(initdir);
3821 if (initdirp != NULL)
3822 for (p = initdirp; *p != NUL; ++p)
3823 if (*p == '/')
3824 *p = '\\';
3825 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3826 }
3827
3828 /*
3829 * TODO: Allow selection of multiple files. Needs another arg to this
3830 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3831 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3832 * files that don't exist yet, so I haven't put it in. What about
3833 * OFN_PATHMUSTEXIST?
3834 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3835 */
3836 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003837# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838 if (curbuf->b_p_bin)
3839 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003840# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003841 if (saving)
3842 {
3843 if (!GetSaveFileName(&fileStruct))
3844 return NULL;
3845 }
3846 else
3847 {
3848 if (!GetOpenFileName(&fileStruct))
3849 return NULL;
3850 }
3851
3852 vim_free(filterp);
3853 vim_free(initdirp);
3854
3855 /* Give focus back to main window (when using MDI). */
3856 SetFocus(s_hwnd);
3857
3858 /* Shorten the file name if possible */
3859 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003860# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003861}
3862#endif /* FEAT_BROWSE */
3863
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864 static void
3865_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003866 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003867 HDROP hDrop)
3868{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003869#define BUFPATHLEN _MAX_PATH
3870#define DRAGQVAL 0xFFFFFFFF
3871#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003873#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874 char szFile[BUFPATHLEN];
3875 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3876 UINT i;
3877 char_u **fnames;
3878 POINT pt;
3879 int_u modifiers = 0;
3880
3881 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3882
3883 /* Obtain dropped position */
3884 DragQueryPoint(hDrop, &pt);
3885 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3886
3887 reset_VIsual();
3888
3889 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3890
3891 if (fnames != NULL)
3892 for (i = 0; i < cFiles; ++i)
3893 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003894#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003895 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3896 fnames[i] = utf16_to_enc(wszFile, NULL);
3897 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003898#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003899 {
3900 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3901 fnames[i] = vim_strsave((char_u *)szFile);
3902 }
3903 }
3904
3905 DragFinish(hDrop);
3906
3907 if (fnames != NULL)
3908 {
3909 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3910 modifiers |= MOUSE_SHIFT;
3911 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3912 modifiers |= MOUSE_CTRL;
3913 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3914 modifiers |= MOUSE_ALT;
3915
3916 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3917
3918 s_need_activate = TRUE;
3919 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003920}
3921
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 static int
3923_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003924 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925 HWND hwndCtl,
3926 UINT code,
3927 int pos)
3928{
3929 static UINT prev_code = 0; /* code of previous call */
3930 scrollbar_T *sb, *sb_info;
3931 long val;
3932 int dragging = FALSE;
3933 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934 SCROLLINFO si;
3935
3936 si.cbSize = sizeof(si);
3937 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003938
3939 sb = gui_mswin_find_scrollbar(hwndCtl);
3940 if (sb == NULL)
3941 return 0;
3942
3943 if (sb->wp != NULL) /* Left or right scrollbar */
3944 {
3945 /*
3946 * Careful: need to get scrollbar info out of first (left) scrollbar
3947 * for window, but keep real scrollbar too because we must pass it to
3948 * gui_drag_scrollbar().
3949 */
3950 sb_info = &sb->wp->w_scrollbars[0];
3951 }
3952 else /* Bottom scrollbar */
3953 sb_info = sb;
3954 val = sb_info->value;
3955
3956 switch (code)
3957 {
3958 case SB_THUMBTRACK:
3959 val = pos;
3960 dragging = TRUE;
3961 if (sb->scroll_shift > 0)
3962 val <<= sb->scroll_shift;
3963 break;
3964 case SB_LINEDOWN:
3965 val++;
3966 break;
3967 case SB_LINEUP:
3968 val--;
3969 break;
3970 case SB_PAGEDOWN:
3971 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3972 break;
3973 case SB_PAGEUP:
3974 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3975 break;
3976 case SB_TOP:
3977 val = 0;
3978 break;
3979 case SB_BOTTOM:
3980 val = sb_info->max;
3981 break;
3982 case SB_ENDSCROLL:
3983 if (prev_code == SB_THUMBTRACK)
3984 {
3985 /*
3986 * "pos" only gives us 16-bit data. In case of large file,
3987 * use GetScrollPos() which returns 32-bit. Unfortunately it
3988 * is not valid while the scrollbar is being dragged.
3989 */
3990 val = GetScrollPos(hwndCtl, SB_CTL);
3991 if (sb->scroll_shift > 0)
3992 val <<= sb->scroll_shift;
3993 }
3994 break;
3995
3996 default:
3997 /* TRACE("Unknown scrollbar event %d\n", code); */
3998 return 0;
3999 }
4000 prev_code = code;
4001
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004002 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4003 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004004
4005 /*
4006 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4007 */
4008 if (sb->wp != NULL)
4009 {
4010 scrollbar_T *sba = sb->wp->w_scrollbars;
4011 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4012
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004014 }
4015
4016 /* Don't let us be interrupted here by another message. */
4017 s_busy_processing = TRUE;
4018
4019 /* When "allow_scrollbar" is FALSE still need to remember the new
4020 * position, but don't actually scroll by setting "dont_scroll". */
4021 dont_scroll = !allow_scrollbar;
4022
4023 gui_drag_scrollbar(sb, val, dragging);
4024
4025 s_busy_processing = FALSE;
4026 dont_scroll = dont_scroll_save;
4027
4028 return 0;
4029}
4030
4031
4032/*
4033 * Get command line arguments.
4034 * Use "prog" as the name of the program and "cmdline" as the arguments.
4035 * Copy the arguments to allocated memory.
4036 * Return the number of arguments (including program name).
4037 * Return pointers to the arguments in "argvp". Memory is allocated with
4038 * malloc(), use free() instead of vim_free().
4039 * Return pointer to buffer in "tofree".
4040 * Returns zero when out of memory.
4041 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004042 int
4043get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4044{
4045 int i;
4046 char *p;
4047 char *progp;
4048 char *pnew = NULL;
4049 char *newcmdline;
4050 int inquote;
4051 int argc;
4052 char **argv = NULL;
4053 int round;
4054
4055 *tofree = NULL;
4056
4057#ifdef FEAT_MBYTE
4058 /* Try using the Unicode version first, it takes care of conversion when
4059 * 'encoding' is changed. */
4060 argc = get_cmd_argsW(&argv);
4061 if (argc != 0)
4062 goto done;
4063#endif
4064
4065 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4066 * non-space. */
4067 p = strrchr(prog, '.');
4068 if (p != NULL)
4069 *p = NUL;
4070 for (progp = prog; *progp == ' '; ++progp)
4071 ;
4072
4073 /* The command line is copied to allocated memory, so that we can change
4074 * it. Add the size of the string, the separating NUL and a terminating
4075 * NUL. */
4076 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4077 if (newcmdline == NULL)
4078 return 0;
4079
4080 /*
4081 * First round: count the number of arguments ("pnew" == NULL).
4082 * Second round: produce the arguments.
4083 */
4084 for (round = 1; round <= 2; ++round)
4085 {
4086 /* First argument is the program name. */
4087 if (pnew != NULL)
4088 {
4089 argv[0] = pnew;
4090 strcpy(pnew, progp);
4091 pnew += strlen(pnew);
4092 *pnew++ = NUL;
4093 }
4094
4095 /*
4096 * Isolate each argument and put it in argv[].
4097 */
4098 p = cmdline;
4099 argc = 1;
4100 while (*p != NUL)
4101 {
4102 inquote = FALSE;
4103 if (pnew != NULL)
4104 argv[argc] = pnew;
4105 ++argc;
4106 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4107 {
4108 /* Backslashes are only special when followed by a double
4109 * quote. */
4110 i = (int)strspn(p, "\\");
4111 if (p[i] == '"')
4112 {
4113 /* Halve the number of backslashes. */
4114 if (i > 1 && pnew != NULL)
4115 {
4116 vim_memset(pnew, '\\', i / 2);
4117 pnew += i / 2;
4118 }
4119
4120 /* Even nr of backslashes toggles quoting, uneven copies
4121 * the double quote. */
4122 if ((i & 1) == 0)
4123 inquote = !inquote;
4124 else if (pnew != NULL)
4125 *pnew++ = '"';
4126 p += i + 1;
4127 }
4128 else if (i > 0)
4129 {
4130 /* Copy span of backslashes unmodified. */
4131 if (pnew != NULL)
4132 {
4133 vim_memset(pnew, '\\', i);
4134 pnew += i;
4135 }
4136 p += i;
4137 }
4138 else
4139 {
4140 if (pnew != NULL)
4141 *pnew++ = *p;
4142#ifdef FEAT_MBYTE
4143 /* Can't use mb_* functions, because 'encoding' is not
4144 * initialized yet here. */
4145 if (IsDBCSLeadByte(*p))
4146 {
4147 ++p;
4148 if (pnew != NULL)
4149 *pnew++ = *p;
4150 }
4151#endif
4152 ++p;
4153 }
4154 }
4155
4156 if (pnew != NULL)
4157 *pnew++ = NUL;
4158 while (*p == ' ' || *p == '\t')
4159 ++p; /* advance until a non-space */
4160 }
4161
4162 if (round == 1)
4163 {
4164 argv = (char **)malloc((argc + 1) * sizeof(char *));
4165 if (argv == NULL )
4166 {
4167 free(newcmdline);
4168 return 0; /* malloc error */
4169 }
4170 pnew = newcmdline;
4171 *tofree = newcmdline;
4172 }
4173 }
4174
4175#ifdef FEAT_MBYTE
4176done:
4177#endif
4178 argv[argc] = NULL; /* NULL-terminated list */
4179 *argvp = argv;
4180 return argc;
4181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183#ifdef FEAT_XPM_W32
4184# include "xpm_w32.h"
4185#endif
4186
4187#ifdef PROTO
4188# define WINAPI
4189#endif
4190
4191#ifdef __MINGW32__
4192/*
4193 * Add a lot of missing defines.
4194 * They are not always missing, we need the #ifndef's.
4195 */
4196# ifndef _cdecl
4197# define _cdecl
4198# endif
4199# ifndef IsMinimized
4200# define IsMinimized(hwnd) IsIconic(hwnd)
4201# endif
4202# ifndef IsMaximized
4203# define IsMaximized(hwnd) IsZoomed(hwnd)
4204# endif
4205# ifndef SelectFont
4206# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4207# endif
4208# ifndef GetStockBrush
4209# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4210# endif
4211# ifndef DeleteBrush
4212# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4213# endif
4214
4215# ifndef HANDLE_WM_RBUTTONDBLCLK
4216# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4217 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4218# endif
4219# ifndef HANDLE_WM_MBUTTONUP
4220# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4221 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4222# endif
4223# ifndef HANDLE_WM_MBUTTONDBLCLK
4224# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4225 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4226# endif
4227# ifndef HANDLE_WM_LBUTTONDBLCLK
4228# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4229 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4230# endif
4231# ifndef HANDLE_WM_RBUTTONDOWN
4232# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4233 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4234# endif
4235# ifndef HANDLE_WM_MOUSEMOVE
4236# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4237 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4238# endif
4239# ifndef HANDLE_WM_RBUTTONUP
4240# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4241 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4242# endif
4243# ifndef HANDLE_WM_MBUTTONDOWN
4244# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4245 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4246# endif
4247# ifndef HANDLE_WM_LBUTTONUP
4248# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4249 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4250# endif
4251# ifndef HANDLE_WM_LBUTTONDOWN
4252# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4253 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4254# endif
4255# ifndef HANDLE_WM_SYSCHAR
4256# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4257 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4258# endif
4259# ifndef HANDLE_WM_ACTIVATEAPP
4260# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4261 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4262# endif
4263# ifndef HANDLE_WM_WINDOWPOSCHANGING
4264# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4265 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4266# endif
4267# ifndef HANDLE_WM_VSCROLL
4268# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4269 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4270# endif
4271# ifndef HANDLE_WM_SETFOCUS
4272# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4273 ((fn)((hwnd), (HWND)(wParam)), 0L)
4274# endif
4275# ifndef HANDLE_WM_KILLFOCUS
4276# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4277 ((fn)((hwnd), (HWND)(wParam)), 0L)
4278# endif
4279# ifndef HANDLE_WM_HSCROLL
4280# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4281 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4282# endif
4283# ifndef HANDLE_WM_DROPFILES
4284# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4285 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4286# endif
4287# ifndef HANDLE_WM_CHAR
4288# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4289 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4290# endif
4291# ifndef HANDLE_WM_SYSDEADCHAR
4292# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4293 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4294# endif
4295# ifndef HANDLE_WM_DEADCHAR
4296# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4297 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4298# endif
4299#endif /* __MINGW32__ */
4300
4301
4302/* Some parameters for tearoff menus. All in pixels. */
4303#define TEAROFF_PADDING_X 2
4304#define TEAROFF_BUTTON_PAD_X 8
4305#define TEAROFF_MIN_WIDTH 200
4306#define TEAROFF_SUBMENU_LABEL ">>"
4307#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4308
4309
4310/* For the Intellimouse: */
4311#ifndef WM_MOUSEWHEEL
4312#define WM_MOUSEWHEEL 0x20a
4313#endif
4314
4315
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004316#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317# define ID_BEVAL_TOOLTIP 200
4318# define BEVAL_TEXT_LEN MAXPATHL
4319
Bram Moolenaar167632f2010-05-26 21:42:54 +02004320#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004321/* Work around old versions of basetsd.h which wrongly declares
4322 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004323# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004324# define UINT_PTR UINT
4325#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004326
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004327static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4328static void delete_tooltip(BalloonEval *beval);
4329static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004330
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004332static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004334
Bram Moolenaar82881492012-11-20 16:53:39 +01004335
4336/* cproto fails on missing include files */
4337#ifndef PROTO
4338
Bram Moolenaar45360022005-07-21 21:08:21 +00004339/*
4340 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004341 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004342 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004343# include <pshpack1.h>
4344
4345#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004346
4347typedef struct _DllVersionInfo
4348{
4349 DWORD cbSize;
4350 DWORD dwMajorVersion;
4351 DWORD dwMinorVersion;
4352 DWORD dwBuildNumber;
4353 DWORD dwPlatformID;
4354} DLLVERSIONINFO;
4355
Bram Moolenaar82881492012-11-20 16:53:39 +01004356#ifndef PROTO
4357# include <poppack.h>
4358#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004359
Bram Moolenaar45360022005-07-21 21:08:21 +00004360typedef struct tagTOOLINFOA_NEW
4361{
4362 UINT cbSize;
4363 UINT uFlags;
4364 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004365 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004366 RECT rect;
4367 HINSTANCE hinst;
4368 LPSTR lpszText;
4369 LPARAM lParam;
4370} TOOLINFO_NEW;
4371
4372typedef struct tagNMTTDISPINFO_NEW
4373{
4374 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004375 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004376 char szText[80];
4377 HINSTANCE hinst;
4378 UINT uFlags;
4379 LPARAM lParam;
4380} NMTTDISPINFO_NEW;
4381
Bram Moolenaar45360022005-07-21 21:08:21 +00004382typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4383#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004384# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#endif
4386
Bram Moolenaar45360022005-07-21 21:08:21 +00004387#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004388# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004389#endif
4390
4391#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004392# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004393#endif
4394
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004395#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004396
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004397#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4398/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4399 * it here if LPNMTTDISPINFO isn't defined.
4400 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4401 * _MSC_VER. */
4402# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4403typedef struct tagNMTTDISPINFOA {
4404 NMHDR hdr;
4405 LPSTR lpszText;
4406 char szText[80];
4407 HINSTANCE hinst;
4408 UINT uFlags;
4409 LPARAM lParam;
4410} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4411# define LPNMTTDISPINFO LPNMTTDISPINFOA
4412
4413# ifdef FEAT_MBYTE
4414typedef struct tagNMTTDISPINFOW {
4415 NMHDR hdr;
4416 LPWSTR lpszText;
4417 WCHAR szText[80];
4418 HINSTANCE hinst;
4419 UINT uFlags;
4420 LPARAM lParam;
4421} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4422# endif
4423# endif
4424#endif
4425
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004426#ifndef TTN_GETDISPINFOW
4427# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4428#endif
4429
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430/* Local variables: */
4431
4432#ifdef FEAT_MENU
4433static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436/*
4437 * Use the system font for dialogs and tear-off menus. Remove this line to
4438 * use DLG_FONT_NAME.
4439 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004440#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442#define VIM_NAME "vim"
4443#define VIM_CLASS "Vim"
4444#define VIM_CLASSW L"Vim"
4445
4446/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4447 * thus there should be room for every dialog. For tearoffs it's made bigger
4448 * when needed. */
4449#define DLG_ALLOC_SIZE 16 * 1024
4450
4451/*
4452 * stuff for dialogs, menus, tearoffs etc.
4453 */
4454static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004455#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458static PWORD
4459add_dialog_element(
4460 PWORD p,
4461 DWORD lStyle,
4462 WORD x,
4463 WORD y,
4464 WORD w,
4465 WORD h,
4466 WORD Id,
4467 WORD clss,
4468 const char *caption);
4469static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004470static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004471#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474static void get_dialog_font_metrics(void);
4475
4476static int dialog_default_button = -1;
4477
4478/* Intellimouse support */
4479static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4482#ifdef FEAT_TOOLBAR
4483static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004484static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485static int get_toolbar_bitmap(vimmenu_T *menu);
4486#endif
4487
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004488#ifdef FEAT_GUI_TABLINE
4489static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004490static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004491#endif
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493#ifdef FEAT_MBYTE_IME
4494static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4495static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4496#endif
4497#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4498# ifdef NOIME
4499typedef struct tagCOMPOSITIONFORM {
4500 DWORD dwStyle;
4501 POINT ptCurrentPos;
4502 RECT rcArea;
4503} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4504typedef HANDLE HIMC;
4505# endif
4506
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004507static HINSTANCE hLibImm = NULL;
4508static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4509static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4510static HIMC (WINAPI *pImmGetContext)(HWND);
4511static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4512static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4513static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4514static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4515static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4516static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4517static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4518static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004519static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520static void dyn_imm_load(void);
4521#else
4522# define pImmGetCompositionStringA ImmGetCompositionStringA
4523# define pImmGetCompositionStringW ImmGetCompositionStringW
4524# define pImmGetContext ImmGetContext
4525# define pImmAssociateContext ImmAssociateContext
4526# define pImmReleaseContext ImmReleaseContext
4527# define pImmGetOpenStatus ImmGetOpenStatus
4528# define pImmSetOpenStatus ImmSetOpenStatus
4529# define pImmGetCompositionFont ImmGetCompositionFontA
4530# define pImmSetCompositionFont ImmSetCompositionFontA
4531# define pImmSetCompositionWindow ImmSetCompositionWindow
4532# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004533# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534#endif
4535
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536#ifdef FEAT_MENU
4537/*
4538 * Figure out how high the menu bar is at the moment.
4539 */
4540 static int
4541gui_mswin_get_menu_height(
4542 int fix_window) /* If TRUE, resize window if menu height changed */
4543{
4544 static int old_menu_height = -1;
4545
4546 RECT rc1, rc2;
4547 int num;
4548 int menu_height;
4549
4550 if (gui.menu_is_active)
4551 num = GetMenuItemCount(s_menuBar);
4552 else
4553 num = 0;
4554
4555 if (num == 0)
4556 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004557 else if (IsMinimized(s_hwnd))
4558 {
4559 /* The height of the menu cannot be determined while the window is
4560 * minimized. Take the previous height if the menu is changed in that
4561 * state, to avoid that Vim's vertical window size accidentally
4562 * increases due to the unaccounted-for menu height. */
4563 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 else
4566 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004567 /*
4568 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4569 * seem to have been set yet, so menu wraps in default window
4570 * width which is very narrow. Instead just return height of a
4571 * single menu item. Will still be wrong when the menu really
4572 * should wrap over more than one line.
4573 */
4574 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4575 if (gui.starting)
4576 menu_height = rc1.bottom - rc1.top + 1;
4577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004579 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4580 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583
4584 if (fix_window && menu_height != old_menu_height)
4585 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004586 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004588 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
4590 return menu_height;
4591}
4592#endif /*FEAT_MENU*/
4593
4594
4595/*
4596 * Setup for the Intellimouse
4597 */
4598 static void
4599init_mouse_wheel(void)
4600{
4601
4602#ifndef SPI_GETWHEELSCROLLLINES
4603# define SPI_GETWHEELSCROLLLINES 104
4604#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004605#ifndef SPI_SETWHEELSCROLLLINES
4606# define SPI_SETWHEELSCROLLLINES 105
4607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
4609#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4610#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4611#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4612#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 mouse_scroll_lines = 3; /* reasonable default */
4615
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004616 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4617 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4618 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619}
4620
4621
4622/* Intellimouse wheel handler */
4623 static void
4624_OnMouseWheel(
4625 HWND hwnd,
4626 short zDelta)
4627{
4628/* Treat a mouse wheel event as if it were a scroll request */
4629 int i;
4630 int size;
4631 HWND hwndCtl;
4632
4633 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4634 {
4635 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4636 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4637 }
4638 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4639 {
4640 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4641 size = curwin->w_scrollbars[SBAR_LEFT].size;
4642 }
4643 else
4644 return;
4645
4646 size = curwin->w_height;
4647 if (mouse_scroll_lines == 0)
4648 init_mouse_wheel();
4649
4650 if (mouse_scroll_lines > 0
4651 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4652 {
4653 for (i = mouse_scroll_lines; i > 0; --i)
4654 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4655 }
4656 else
4657 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
4658}
4659
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004660#ifdef USE_SYSMENU_FONT
4661/*
4662 * Get Menu Font.
4663 * Return OK or FAIL.
4664 */
4665 static int
4666gui_w32_get_menu_font(LOGFONT *lf)
4667{
4668 NONCLIENTMETRICS nm;
4669
4670 nm.cbSize = sizeof(NONCLIENTMETRICS);
4671 if (!SystemParametersInfo(
4672 SPI_GETNONCLIENTMETRICS,
4673 sizeof(NONCLIENTMETRICS),
4674 &nm,
4675 0))
4676 return FAIL;
4677 *lf = nm.lfMenuFont;
4678 return OK;
4679}
4680#endif
4681
4682
4683#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4684/*
4685 * Set the GUI tabline font to the system menu font
4686 */
4687 static void
4688set_tabline_font(void)
4689{
4690 LOGFONT lfSysmenu;
4691 HFONT font;
4692 HWND hwnd;
4693 HDC hdc;
4694 HFONT hfntOld;
4695 TEXTMETRIC tm;
4696
4697 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4698 return;
4699
4700 font = CreateFontIndirect(&lfSysmenu);
4701
4702 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4703
4704 /*
4705 * Compute the height of the font used for the tab text
4706 */
4707 hwnd = GetDesktopWindow();
4708 hdc = GetWindowDC(hwnd);
4709 hfntOld = SelectFont(hdc, font);
4710
4711 GetTextMetrics(hdc, &tm);
4712
4713 SelectFont(hdc, hfntOld);
4714 ReleaseDC(hwnd, hdc);
4715
4716 /*
4717 * The space used by the tab border and the space between the tab label
4718 * and the tab border is included as 7.
4719 */
4720 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4721}
4722#endif
4723
Bram Moolenaar520470a2005-06-16 21:59:56 +00004724/*
4725 * Invoked when a setting was changed.
4726 */
4727 static LRESULT CALLBACK
4728_OnSettingChange(UINT n)
4729{
4730 if (n == SPI_SETWHEELSCROLLLINES)
4731 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4732 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004733#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4734 if (n == SPI_SETNONCLIENTMETRICS)
4735 set_tabline_font();
4736#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004737 return 0;
4738}
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740#ifdef FEAT_NETBEANS_INTG
4741 static void
4742_OnWindowPosChanged(
4743 HWND hwnd,
4744 const LPWINDOWPOS lpwpos)
4745{
4746 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004747 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4750 || lpwpos->cx != cx || lpwpos->cy != cy))
4751 {
4752 x = lpwpos->x;
4753 y = lpwpos->y;
4754 cx = lpwpos->cx;
4755 cy = lpwpos->cy;
4756 netbeans_frame_moved(x, y);
4757 }
4758 /* Allow to send WM_SIZE and WM_MOVE */
4759 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4760}
4761#endif
4762
4763 static int
4764_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 UINT fwSide,
4766 LPRECT lprc)
4767{
4768 int w, h;
4769 int valid_w, valid_h;
4770 int w_offset, h_offset;
4771
4772 w = lprc->right - lprc->left;
4773 h = lprc->bottom - lprc->top;
4774 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4775 w_offset = w - valid_w;
4776 h_offset = h - valid_h;
4777
4778 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4779 || fwSide == WMSZ_BOTTOMLEFT)
4780 lprc->left += w_offset;
4781 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4782 || fwSide == WMSZ_BOTTOMRIGHT)
4783 lprc->right -= w_offset;
4784
4785 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4786 || fwSide == WMSZ_TOPRIGHT)
4787 lprc->top += h_offset;
4788 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4789 || fwSide == WMSZ_BOTTOMRIGHT)
4790 lprc->bottom -= h_offset;
4791 return TRUE;
4792}
4793
4794
4795
4796 static LRESULT CALLBACK
4797_WndProc(
4798 HWND hwnd,
4799 UINT uMsg,
4800 WPARAM wParam,
4801 LPARAM lParam)
4802{
4803 /*
4804 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4805 hwnd, uMsg, wParam, lParam);
4806 */
4807
4808 HandleMouseHide(uMsg, lParam);
4809
4810 s_uMsg = uMsg;
4811 s_wParam = wParam;
4812 s_lParam = lParam;
4813
4814 switch (uMsg)
4815 {
4816 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4817 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4818 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4819 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4820 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4821 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4822 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4823 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4824 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4825#ifdef FEAT_MENU
4826 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4827#endif
4828 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4829 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4830 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4831 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4832 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4833 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4834 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4835 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4836 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4837#ifdef FEAT_NETBEANS_INTG
4838 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4839#endif
4840
Bram Moolenaarafa24992006-03-27 20:58:26 +00004841#ifdef FEAT_GUI_TABLINE
4842 case WM_RBUTTONUP:
4843 {
4844 if (gui_mch_showing_tabline())
4845 {
4846 POINT pt;
4847 RECT rect;
4848
4849 /*
4850 * If the cursor is on the tabline, display the tab menu
4851 */
4852 GetCursorPos((LPPOINT)&pt);
4853 GetWindowRect(s_textArea, &rect);
4854 if (pt.y < rect.top)
4855 {
4856 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004857 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004858 }
4859 }
4860 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4861 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004862 case WM_LBUTTONDBLCLK:
4863 {
4864 /*
4865 * If the user double clicked the tabline, create a new tab
4866 */
4867 if (gui_mch_showing_tabline())
4868 {
4869 POINT pt;
4870 RECT rect;
4871
4872 GetCursorPos((LPPOINT)&pt);
4873 GetWindowRect(s_textArea, &rect);
4874 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004875 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004876 }
4877 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4878 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004879#endif
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 case WM_QUERYENDSESSION: /* System wants to go down. */
4882 gui_shell_closed(); /* Will exit when no changed buffers. */
4883 return FALSE; /* Do NOT allow system to go down. */
4884
4885 case WM_ENDSESSION:
4886 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004889 return 0L;
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 break;
4892
4893 case WM_CHAR:
4894 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4895 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004896 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return 0L;
4898
4899 case WM_SYSCHAR:
4900 /*
4901 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4902 * shortcut key, handle like a typed ALT key, otherwise call Windows
4903 * ALT key handling.
4904 */
4905#ifdef FEAT_MENU
4906 if ( !gui.menu_is_active
4907 || p_wak[0] == 'n'
4908 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4909 )
4910#endif
4911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004912 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return 0L;
4914 }
4915#ifdef FEAT_MENU
4916 else
4917 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4918#endif
4919
4920 case WM_SYSKEYUP:
4921#ifdef FEAT_MENU
4922 /* This used to be done only when menu is active: ALT key is used for
4923 * that. But that caused problems when menu is disabled and using
4924 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4925 * are received, mouse pointer remains hidden. */
4926 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4927#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004928 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930
4931 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004932 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 case WM_MOUSEWHEEL:
4935 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004936 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
Bram Moolenaar520470a2005-06-16 21:59:56 +00004938 /* Notification for change in SystemParametersInfo() */
4939 case WM_SETTINGCHANGE:
4940 return _OnSettingChange((UINT)wParam);
4941
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004942#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 case WM_NOTIFY:
4944 switch (((LPNMHDR) lParam)->code)
4945 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004946# ifdef FEAT_MBYTE
4947 case TTN_GETDISPINFOW:
4948# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004949 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004951 LPNMHDR hdr = (LPNMHDR)lParam;
4952 char_u *str = NULL;
4953 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004954
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004955 vim_free(tt_text);
4956 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004957
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004958# ifdef FEAT_GUI_TABLINE
4959 if (gui_mch_showing_tabline()
4960 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004961 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004962 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004963 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004964 * Mouse is over the GUI tabline. Display the
4965 * tooltip for the tab under the cursor
4966 *
4967 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004968 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004969 GetCursorPos(&pt);
4970 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004971 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004972 TCHITTESTINFO htinfo;
4973 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004974
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004975 /*
4976 * Get the tab under the cursor
4977 */
4978 htinfo.pt.x = pt.x;
4979 htinfo.pt.y = pt.y;
4980 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4981 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004982 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004983 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004984
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004985 tp = find_tabpage(idx + 1);
4986 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004987 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004988 get_tabline_label(tp, TRUE);
4989 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004990 }
4991 }
4992 }
4993 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004994# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004995# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004996# ifdef FEAT_GUI_TABLINE
4997 else
4998# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005000 UINT idButton;
5001 vimmenu_T *pMenu;
5002
5003 idButton = (UINT) hdr->idFrom;
5004 pMenu = gui_mswin_find_menu(root_menu, idButton);
5005 if (pMenu)
5006 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005008# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005009 if (str != NULL)
5010 {
5011# ifdef FEAT_MBYTE
5012 if (hdr->code == TTN_GETDISPINFOW)
5013 {
5014 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5015
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005016 /* Set the maximum width, this also enables using
5017 * \n for line break. */
5018 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5019 0, 500);
5020
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005021 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005022 lpdi->lpszText = tt_text;
5023 /* can't show tooltip if failed */
5024 }
5025 else
5026# endif
5027 {
5028 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5029
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005030 /* Set the maximum width, this also enables using
5031 * \n for line break. */
5032 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5033 0, 500);
5034
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005035 if (STRLEN(str) < sizeof(lpdi->szText)
5036 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005037 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005038 sizeof(lpdi->szText) - 1);
5039 else
5040 lpdi->lpszText = tt_text;
5041 }
5042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005045# ifdef FEAT_GUI_TABLINE
5046 case TCN_SELCHANGE:
5047 if (gui_mch_showing_tabline()
5048 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005049 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005050 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005051 return 0L;
5052 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005053 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005054
5055 case NM_RCLICK:
5056 if (gui_mch_showing_tabline()
5057 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005058 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005059 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005060 return 0L;
5061 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005062 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005065# ifdef FEAT_GUI_TABLINE
5066 if (gui_mch_showing_tabline()
5067 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5068 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 break;
5071 }
5072 break;
5073#endif
5074#if defined(MENUHINTS) && defined(FEAT_MENU)
5075 case WM_MENUSELECT:
5076 if (((UINT) HIWORD(wParam)
5077 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5078 == MF_HILITE
5079 && (State & CMDLINE) == 0)
5080 {
5081 UINT idButton;
5082 vimmenu_T *pMenu;
5083 static int did_menu_tip = FALSE;
5084
5085 if (did_menu_tip)
5086 {
5087 msg_clr_cmdline();
5088 setcursor();
5089 out_flush();
5090 did_menu_tip = FALSE;
5091 }
5092
5093 idButton = (UINT)LOWORD(wParam);
5094 pMenu = gui_mswin_find_menu(root_menu, idButton);
5095 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5096 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5097 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005098 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005100 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 setcursor();
5102 out_flush();
5103 did_menu_tip = TRUE;
5104 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005105 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107 break;
5108#endif
5109 case WM_NCHITTEST:
5110 {
5111 LRESULT result;
5112 int x, y;
5113 int xPos = GET_X_LPARAM(lParam);
5114
5115 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5116 if (result == HTCLIENT)
5117 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005118#ifdef FEAT_GUI_TABLINE
5119 if (gui_mch_showing_tabline())
5120 {
5121 int yPos = GET_Y_LPARAM(lParam);
5122 RECT rct;
5123
5124 /* If the cursor is on the GUI tabline, don't process this
5125 * event */
5126 GetWindowRect(s_textArea, &rct);
5127 if (yPos < rct.top)
5128 return result;
5129 }
5130#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005131 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 xPos -= x;
5133
5134 if (xPos < 48) /* <VN> TODO should use system metric? */
5135 return HTBOTTOMLEFT;
5136 else
5137 return HTBOTTOMRIGHT;
5138 }
5139 else
5140 return result;
5141 }
5142 /* break; notreached */
5143
5144#ifdef FEAT_MBYTE_IME
5145 case WM_IME_NOTIFY:
5146 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5147 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005148 return 1L;
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 case WM_IME_COMPOSITION:
5151 if (!_OnImeComposition(hwnd, wParam, lParam))
5152 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005153 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#endif
5155
5156 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005158 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
5160 _OnFindRepl();
5161 }
5162#endif
5163 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5164 }
5165
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005166 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167}
5168
5169/*
5170 * End of call-back routines
5171 */
5172
5173/* parent window, if specified with -P */
5174HWND vim_parent_hwnd = NULL;
5175
5176 static BOOL CALLBACK
5177FindWindowTitle(HWND hwnd, LPARAM lParam)
5178{
5179 char buf[2048];
5180 char *title = (char *)lParam;
5181
5182 if (GetWindowText(hwnd, buf, sizeof(buf)))
5183 {
5184 if (strstr(buf, title) != NULL)
5185 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005186 /* Found it. Store the window ref. and quit searching if MDI
5187 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005189 if (vim_parent_hwnd != NULL)
5190 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 }
5193 return TRUE; /* continue searching */
5194}
5195
5196/*
5197 * Invoked for '-P "title"' argument: search for parent application to open
5198 * our window in.
5199 */
5200 void
5201gui_mch_set_parent(char *title)
5202{
5203 EnumWindows(FindWindowTitle, (LPARAM)title);
5204 if (vim_parent_hwnd == NULL)
5205 {
5206 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5207 mch_exit(2);
5208 }
5209}
5210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 static void
5213ole_error(char *arg)
5214{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005215 char buf[IOSIZE];
5216
5217 /* Can't use EMSG() here, we have not finished initialisation yet. */
5218 vim_snprintf(buf, IOSIZE,
5219 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5220 arg);
5221 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
5225/*
5226 * Parse the GUI related command-line arguments. Any arguments used are
5227 * deleted from argv, and *argc is decremented accordingly. This is called
5228 * when vim is started, whether or not the GUI has been started.
5229 */
5230 void
5231gui_mch_prepare(int *argc, char **argv)
5232{
5233 int silent = FALSE;
5234 int idx;
5235
5236 /* Check for special OLE command line parameters */
5237 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5238 {
5239 /* Check for a "-silent" argument first. */
5240 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5241 && (argv[2][0] == '-' || argv[2][0] == '/'))
5242 {
5243 silent = TRUE;
5244 idx = 2;
5245 }
5246 else
5247 idx = 1;
5248
5249 /* Register Vim as an OLE Automation server */
5250 if (STRICMP(argv[idx] + 1, "register") == 0)
5251 {
5252#ifdef FEAT_OLE
5253 RegisterMe(silent);
5254 mch_exit(0);
5255#else
5256 if (!silent)
5257 ole_error("register");
5258 mch_exit(2);
5259#endif
5260 }
5261
5262 /* Unregister Vim as an OLE Automation server */
5263 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5264 {
5265#ifdef FEAT_OLE
5266 UnregisterMe(!silent);
5267 mch_exit(0);
5268#else
5269 if (!silent)
5270 ole_error("unregister");
5271 mch_exit(2);
5272#endif
5273 }
5274
5275 /* Ignore an -embedding argument. It is only relevant if the
5276 * application wants to treat the case when it is started manually
5277 * differently from the case where it is started via automation (and
5278 * we don't).
5279 */
5280 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5281 {
5282#ifdef FEAT_OLE
5283 *argc = 1;
5284#else
5285 ole_error("embedding");
5286 mch_exit(2);
5287#endif
5288 }
5289 }
5290
5291#ifdef FEAT_OLE
5292 {
5293 int bDoRestart = FALSE;
5294
5295 InitOLE(&bDoRestart);
5296 /* automatically exit after registering */
5297 if (bDoRestart)
5298 mch_exit(0);
5299 }
5300#endif
5301
5302#ifdef FEAT_NETBEANS_INTG
5303 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005304 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 int arg;
5306
5307 for (arg = 1; arg < *argc; arg++)
5308 if (strncmp("-nb", argv[arg], 3) == 0)
5309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 netbeansArg = argv[arg];
5311 mch_memmove(&argv[arg], &argv[arg + 1],
5312 (--*argc - arg) * sizeof(char *));
5313 argv[*argc] = NULL;
5314 break; /* enough? */
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 }
5317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318}
5319
5320/*
5321 * Initialise the GUI. Create all the windows, set up all the call-backs
5322 * etc.
5323 */
5324 int
5325gui_mch_init(void)
5326{
5327 const char szVimWndClass[] = VIM_CLASS;
5328 const char szTextAreaClass[] = "VimTextArea";
5329 WNDCLASS wndclass;
5330#ifdef FEAT_MBYTE
5331 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005332 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 WNDCLASSW wndclassw;
5334#endif
5335#ifdef GLOBAL_IME
5336 ATOM atom;
5337#endif
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 /* Return here if the window was already opened (happens when
5340 * gui_mch_dialog() is called early). */
5341 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005342 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 /*
5345 * Load the tearoff bitmap
5346 */
5347#ifdef FEAT_TEAROFF
5348 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5349#endif
5350
5351 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5352 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5353#ifdef FEAT_MENU
5354 gui.menu_height = 0; /* Windows takes care of this */
5355#endif
5356 gui.border_width = 0;
5357
5358 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5359
5360#ifdef FEAT_MBYTE
5361 /* First try using the wide version, so that we can use any title.
5362 * Otherwise only characters in the active codepage will work. */
5363 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5364 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005365 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 wndclassw.lpfnWndProc = _WndProc;
5367 wndclassw.cbClsExtra = 0;
5368 wndclassw.cbWndExtra = 0;
5369 wndclassw.hInstance = s_hinst;
5370 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5371 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5372 wndclassw.hbrBackground = s_brush;
5373 wndclassw.lpszMenuName = NULL;
5374 wndclassw.lpszClassName = szVimWndClassW;
5375
5376 if ((
5377#ifdef GLOBAL_IME
5378 atom =
5379#endif
5380 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 else
5383 wide_WindowProc = TRUE;
5384 }
5385
5386 if (!wide_WindowProc)
5387#endif
5388
5389 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5390 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005391 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 wndclass.lpfnWndProc = _WndProc;
5393 wndclass.cbClsExtra = 0;
5394 wndclass.cbWndExtra = 0;
5395 wndclass.hInstance = s_hinst;
5396 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5397 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5398 wndclass.hbrBackground = s_brush;
5399 wndclass.lpszMenuName = NULL;
5400 wndclass.lpszClassName = szVimWndClass;
5401
5402 if ((
5403#ifdef GLOBAL_IME
5404 atom =
5405#endif
5406 RegisterClass(&wndclass)) == 0)
5407 return FAIL;
5408 }
5409
5410 if (vim_parent_hwnd != NULL)
5411 {
5412#ifdef HAVE_TRY_EXCEPT
5413 __try
5414 {
5415#endif
5416 /* Open inside the specified parent window.
5417 * TODO: last argument should point to a CLIENTCREATESTRUCT
5418 * structure. */
5419 s_hwnd = CreateWindowEx(
5420 WS_EX_MDICHILD,
5421 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005422 WS_OVERLAPPEDWINDOW | WS_CHILD
5423 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5425 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5426 100, /* Any value will do */
5427 100, /* Any value will do */
5428 vim_parent_hwnd, NULL,
5429 s_hinst, NULL);
5430#ifdef HAVE_TRY_EXCEPT
5431 }
5432 __except(EXCEPTION_EXECUTE_HANDLER)
5433 {
5434 /* NOP */
5435 }
5436#endif
5437 if (s_hwnd == NULL)
5438 {
5439 EMSG(_("E672: Unable to open window inside MDI application"));
5440 mch_exit(2);
5441 }
5442 }
5443 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005444 {
5445 /* If the provided windowid is not valid reset it to zero, so that it
5446 * is ignored and we open our own window. */
5447 if (IsWindow((HWND)win_socket_id) <= 0)
5448 win_socket_id = 0;
5449
5450 /* Create a window. If win_socket_id is not zero without border and
5451 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005453 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005454 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5455 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005456 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5457 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5458 100, /* Any value will do */
5459 100, /* Any value will do */
5460 NULL, NULL,
5461 s_hinst, NULL);
5462 if (s_hwnd != NULL && win_socket_id != 0)
5463 {
5464 SetParent(s_hwnd, (HWND)win_socket_id);
5465 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5466 }
5467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468
5469 if (s_hwnd == NULL)
5470 return FAIL;
5471
5472#ifdef GLOBAL_IME
5473 global_ime_init(atom, s_hwnd);
5474#endif
5475#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5476 dyn_imm_load();
5477#endif
5478
5479 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005480#ifdef FEAT_MBYTE
5481 if (wide_WindowProc)
5482 {
5483 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5484 {
5485 wndclassw.style = CS_OWNDC;
5486 wndclassw.lpfnWndProc = _TextAreaWndProc;
5487 wndclassw.cbClsExtra = 0;
5488 wndclassw.cbWndExtra = 0;
5489 wndclassw.hInstance = s_hinst;
5490 wndclassw.hIcon = NULL;
5491 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5492 wndclassw.hbrBackground = NULL;
5493 wndclassw.lpszMenuName = NULL;
5494 wndclassw.lpszClassName = szTextAreaClassW;
5495
5496 if (RegisterClassW(&wndclassw) == 0)
5497 return FAIL;
5498 }
5499 }
5500 else
5501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5503 {
5504 wndclass.style = CS_OWNDC;
5505 wndclass.lpfnWndProc = _TextAreaWndProc;
5506 wndclass.cbClsExtra = 0;
5507 wndclass.cbWndExtra = 0;
5508 wndclass.hInstance = s_hinst;
5509 wndclass.hIcon = NULL;
5510 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5511 wndclass.hbrBackground = NULL;
5512 wndclass.lpszMenuName = NULL;
5513 wndclass.lpszClassName = szTextAreaClass;
5514
5515 if (RegisterClass(&wndclass) == 0)
5516 return FAIL;
5517 }
5518 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005519 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 szTextAreaClass, "Vim text area",
5521 WS_CHILD | WS_VISIBLE, 0, 0,
5522 100, /* Any value will do for now */
5523 100, /* Any value will do for now */
5524 s_hwnd, NULL,
5525 s_hinst, NULL);
5526
5527 if (s_textArea == NULL)
5528 return FAIL;
5529
Bram Moolenaar20321902016-02-17 12:30:17 +01005530#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005531 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5532 {
5533 HANDLE hIcon = NULL;
5534
5535 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005536 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005537 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005538#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#ifdef FEAT_MENU
5541 s_menuBar = CreateMenu();
5542#endif
5543 s_hdc = GetDC(s_textArea);
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
5547 /* Do we need to bother with this? */
5548 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5549
5550 /* Get background/foreground colors from the system */
5551 gui_mch_def_colors();
5552
5553 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5554 * file) */
5555 set_normal_colors();
5556
5557 /*
5558 * Check that none of the colors are the same as the background color.
5559 * Then store the current values as the defaults.
5560 */
5561 gui_check_colors();
5562 gui.def_norm_pixel = gui.norm_pixel;
5563 gui.def_back_pixel = gui.back_pixel;
5564
5565 /* Get the colors for the highlight groups (gui_check_colors() might have
5566 * changed them) */
5567 highlight_gui_started();
5568
5569 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005570 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005572 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573
5574 /*
5575 * Set up for Intellimouse processing
5576 */
5577 init_mouse_wheel();
5578
5579 /*
5580 * compute a couple of metrics used for the dialogs
5581 */
5582 get_dialog_font_metrics();
5583#ifdef FEAT_TOOLBAR
5584 /*
5585 * Create the toolbar
5586 */
5587 initialise_toolbar();
5588#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005589#ifdef FEAT_GUI_TABLINE
5590 /*
5591 * Create the tabline
5592 */
5593 initialise_tabline();
5594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595#ifdef MSWIN_FIND_REPLACE
5596 /*
5597 * Initialise the dialog box stuff
5598 */
5599 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5600
5601 /* Initialise the struct */
5602 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005603 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005605 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5607 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5608 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005609# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005610 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5611 s_findrep_struct_w.lpstrFindWhat =
5612 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5613 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5614 s_findrep_struct_w.lpstrReplaceWith =
5615 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5616 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5617 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5618 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005622#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005623# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5624/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5625# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005626# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005627# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005628# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005629 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005630 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005631#endif
5632
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005633#ifdef FEAT_RENDER_OPTIONS
5634 if (p_rop)
5635 (void)gui_mch_set_rendering_options(p_rop);
5636#endif
5637
Bram Moolenaar748bf032005-02-02 23:04:36 +00005638theend:
5639 /* Display any pending error messages */
5640 display_errors();
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return OK;
5643}
5644
5645/*
5646 * Get the size of the screen, taking position on multiple monitors into
5647 * account (if supported).
5648 */
5649 static void
5650get_work_area(RECT *spi_rect)
5651{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005652 HMONITOR mon;
5653 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005655 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005656 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005657 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005659 moninfo.cbSize = sizeof(MONITORINFO);
5660 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005662 *spi_rect = moninfo.rcWork;
5663 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665 }
5666 /* this is the old method... */
5667 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5668}
5669
5670/*
5671 * Set the size of the window to the given width and height in pixels.
5672 */
5673 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005674gui_mch_set_shellsize(
5675 int width,
5676 int height,
5677 int min_width UNUSED,
5678 int min_height UNUSED,
5679 int base_width UNUSED,
5680 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005681 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 RECT workarea_rect;
5684 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 WINDOWPLACEMENT wndpl;
5686
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005687 /* Try to keep window completely on screen. */
5688 /* Get position of the screen work area. This is the part that is not
5689 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 get_work_area(&workarea_rect);
5691
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005692 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005693 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 wndpl.length = sizeof(WINDOWPLACEMENT);
5695 GetWindowPlacement(s_hwnd, &wndpl);
5696
5697 /* Resizing a maximized window looks very strange, unzoom it first.
5698 * But don't do it when still starting up, it may have been requested in
5699 * the shortcut. */
5700 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5701 {
5702 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5703 /* Need to get the settings of the normal window. */
5704 GetWindowPlacement(s_hwnd, &wndpl);
5705 }
5706
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005708 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005709 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005710 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005711 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 + GetSystemMetrics(SM_CYCAPTION)
5713#ifdef FEAT_MENU
5714 + gui_mswin_get_menu_height(FALSE)
5715#endif
5716 ;
5717
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005718 /* The following should take care of keeping Vim on the same monitor, no
5719 * matter if the secondary monitor is left or right of the primary
5720 * monitor. */
5721 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5722 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005723
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005724 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005725 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005726 && wndpl.rcNormalPosition.right > workarea_rect.right)
5727 OffsetRect(&wndpl.rcNormalPosition,
5728 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005730 if ((direction & RESIZE_HOR)
5731 && wndpl.rcNormalPosition.left < workarea_rect.left)
5732 OffsetRect(&wndpl.rcNormalPosition,
5733 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
Bram Moolenaarafa24992006-03-27 20:58:26 +00005735 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005736 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5737 OffsetRect(&wndpl.rcNormalPosition,
5738 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005740 if ((direction & RESIZE_VERT)
5741 && wndpl.rcNormalPosition.top < workarea_rect.top)
5742 OffsetRect(&wndpl.rcNormalPosition,
5743 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
5745 /* set window position - we should use SetWindowPlacement rather than
5746 * SetWindowPos as the MSDN docs say the coord systems returned by
5747 * these two are not compatible. */
5748 SetWindowPlacement(s_hwnd, &wndpl);
5749
5750 SetActiveWindow(s_hwnd);
5751 SetFocus(s_hwnd);
5752
5753#ifdef FEAT_MENU
5754 /* Menu may wrap differently now */
5755 gui_mswin_get_menu_height(!gui.starting);
5756#endif
5757}
5758
5759
5760 void
5761gui_mch_set_scrollbar_thumb(
5762 scrollbar_T *sb,
5763 long val,
5764 long size,
5765 long max)
5766{
5767 SCROLLINFO info;
5768
5769 sb->scroll_shift = 0;
5770 while (max > 32767)
5771 {
5772 max = (max + 1) >> 1;
5773 val >>= 1;
5774 size >>= 1;
5775 ++sb->scroll_shift;
5776 }
5777
5778 if (sb->scroll_shift > 0)
5779 ++size;
5780
5781 info.cbSize = sizeof(info);
5782 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5783 info.nPos = val;
5784 info.nMin = 0;
5785 info.nMax = max;
5786 info.nPage = size;
5787 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5788}
5789
5790
5791/*
5792 * Set the current text font.
5793 */
5794 void
5795gui_mch_set_font(GuiFont font)
5796{
5797 gui.currFont = font;
5798}
5799
5800
5801/*
5802 * Set the current text foreground color.
5803 */
5804 void
5805gui_mch_set_fg_color(guicolor_T color)
5806{
5807 gui.currFgColor = color;
5808}
5809
5810/*
5811 * Set the current text background color.
5812 */
5813 void
5814gui_mch_set_bg_color(guicolor_T color)
5815{
5816 gui.currBgColor = color;
5817}
5818
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005819/*
5820 * Set the current text special color.
5821 */
5822 void
5823gui_mch_set_sp_color(guicolor_T color)
5824{
5825 gui.currSpColor = color;
5826}
5827
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005828#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829/*
5830 * Multi-byte handling, originally by Sung-Hoon Baek.
5831 * First static functions (no prototypes generated).
5832 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005833# ifdef _MSC_VER
5834# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5835# endif
5836# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 * handle WM_IME_NOTIFY message
5840 */
5841 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005842_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 LRESULT lResult = 0;
5845 HIMC hImc;
5846
5847 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5848 return lResult;
5849 switch (dwCommand)
5850 {
5851 case IMN_SETOPENSTATUS:
5852 if (pImmGetOpenStatus(hImc))
5853 {
5854 pImmSetCompositionFont(hImc, &norm_logfont);
5855 im_set_position(gui.row, gui.col);
5856
5857 /* Disable langmap */
5858 State &= ~LANGMAP;
5859 if (State & INSERT)
5860 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 /* Unshown 'keymap' in status lines */
5863 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5864 {
5865 /* Save cursor position */
5866 int old_row = gui.row;
5867 int old_col = gui.col;
5868
5869 // This must be called here before
5870 // status_redraw_curbuf(), otherwise the mode
5871 // message may appear in the wrong position.
5872 showmode();
5873 status_redraw_curbuf();
5874 update_screen(0);
5875 /* Restore cursor position */
5876 gui.row = old_row;
5877 gui.col = old_col;
5878 }
5879#endif
5880 }
5881 }
5882 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005883 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 lResult = 0;
5885 break;
5886 }
5887 pImmReleaseContext(hWnd, hImc);
5888 return lResult;
5889}
5890
5891 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005892_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 char_u *ret;
5895 int len;
5896
5897 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5898 return 0;
5899
5900 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5901 if (ret != NULL)
5902 {
5903 add_to_input_buf_csi(ret, len);
5904 vim_free(ret);
5905 return 1;
5906 }
5907 return 0;
5908}
5909
5910/*
5911 * get the current composition string, in UCS-2; *lenp is the number of
5912 * *lenp is the number of Unicode characters.
5913 */
5914 static short_u *
5915GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5916{
5917 LONG ret;
5918 LPWSTR wbuf = NULL;
5919 char_u *buf;
5920
5921 if (!pImmGetContext)
5922 return NULL; /* no imm32.dll */
5923
5924 /* Try Unicode; this'll always work on NT regardless of codepage. */
5925 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5926 if (ret == 0)
5927 return NULL; /* empty */
5928
5929 if (ret > 0)
5930 {
5931 /* Allocate the requested buffer plus space for the NUL character. */
5932 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5933 if (wbuf != NULL)
5934 {
5935 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5936 *lenp = ret / sizeof(WCHAR);
5937 }
5938 return (short_u *)wbuf;
5939 }
5940
5941 /* ret < 0; we got an error, so try the ANSI version. This'll work
5942 * on 9x/ME, but only if the codepage happens to be set to whatever
5943 * we're inputting. */
5944 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5945 if (ret <= 0)
5946 return NULL; /* empty or error */
5947
5948 buf = alloc(ret);
5949 if (buf == NULL)
5950 return NULL;
5951 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5952
5953 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005954 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 vim_free(buf);
5956
5957 return (short_u *)wbuf;
5958}
5959
5960/*
5961 * void GetResultStr()
5962 *
5963 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5964 * get complete composition string
5965 */
5966 static char_u *
5967GetResultStr(HWND hwnd, int GCS, int *lenp)
5968{
5969 HIMC hIMC; /* Input context handle. */
5970 short_u *buf = NULL;
5971 char_u *convbuf = NULL;
5972
5973 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5974 return NULL;
5975
5976 /* Reads in the composition string. */
5977 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5978 if (buf == NULL)
5979 return NULL;
5980
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005981 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 pImmReleaseContext(hwnd, hIMC);
5983 vim_free(buf);
5984 return convbuf;
5985}
5986#endif
5987
5988/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005989#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990
5991/*
5992 * set font to IM.
5993 */
5994 void
5995im_set_font(LOGFONT *lf)
5996{
5997 HIMC hImc;
5998
5999 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6000 {
6001 pImmSetCompositionFont(hImc, lf);
6002 pImmReleaseContext(s_hwnd, hImc);
6003 }
6004}
6005
6006/*
6007 * Notify cursor position to IM.
6008 */
6009 void
6010im_set_position(int row, int col)
6011{
6012 HIMC hImc;
6013
6014 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6015 {
6016 COMPOSITIONFORM cfs;
6017
6018 cfs.dwStyle = CFS_POINT;
6019 cfs.ptCurrentPos.x = FILL_X(col);
6020 cfs.ptCurrentPos.y = FILL_Y(row);
6021 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6022 pImmSetCompositionWindow(hImc, &cfs);
6023
6024 pImmReleaseContext(s_hwnd, hImc);
6025 }
6026}
6027
6028/*
6029 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6030 */
6031 void
6032im_set_active(int active)
6033{
6034 HIMC hImc;
6035 static HIMC hImcOld = (HIMC)0;
6036
6037 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6038 {
6039 if (p_imdisable)
6040 {
6041 if (hImcOld == (HIMC)0)
6042 {
6043 hImcOld = pImmGetContext(s_hwnd);
6044 if (hImcOld)
6045 pImmAssociateContext(s_hwnd, (HIMC)0);
6046 }
6047 active = FALSE;
6048 }
6049 else if (hImcOld != (HIMC)0)
6050 {
6051 pImmAssociateContext(s_hwnd, hImcOld);
6052 hImcOld = (HIMC)0;
6053 }
6054
6055 hImc = pImmGetContext(s_hwnd);
6056 if (hImc)
6057 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006058 /*
6059 * for Korean ime
6060 */
6061 HKL hKL = GetKeyboardLayout(0);
6062
6063 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6064 {
6065 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6066 static BOOL bSaved = FALSE;
6067
6068 if (active)
6069 {
6070 /* if we have a saved conversion status, restore it */
6071 if (bSaved)
6072 pImmSetConversionStatus(hImc, dwConversionSaved,
6073 dwSentenceSaved);
6074 bSaved = FALSE;
6075 }
6076 else
6077 {
6078 /* save conversion status and disable korean */
6079 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6080 &dwSentenceSaved))
6081 {
6082 bSaved = TRUE;
6083 pImmSetConversionStatus(hImc,
6084 dwConversionSaved & ~(IME_CMODE_NATIVE
6085 | IME_CMODE_FULLSHAPE),
6086 dwSentenceSaved);
6087 }
6088 }
6089 }
6090
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 pImmSetOpenStatus(hImc, active);
6092 pImmReleaseContext(s_hwnd, hImc);
6093 }
6094 }
6095}
6096
6097/*
6098 * Get IM status. When IM is on, return not 0. Else return 0.
6099 */
6100 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006101im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
6103 int status = 0;
6104 HIMC hImc;
6105
6106 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6107 {
6108 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6109 pImmReleaseContext(s_hwnd, hImc);
6110 }
6111 return status;
6112}
6113
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006114#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6117/* Win32 with GLOBAL IME */
6118
6119/*
6120 * Notify cursor position to IM.
6121 */
6122 void
6123im_set_position(int row, int col)
6124{
6125 /* Win32 with GLOBAL IME */
6126 POINT p;
6127
6128 p.x = FILL_X(col);
6129 p.y = FILL_Y(row);
6130 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6131 global_ime_set_position(&p);
6132}
6133
6134/*
6135 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6136 */
6137 void
6138im_set_active(int active)
6139{
6140 global_ime_set_status(active);
6141}
6142
6143/*
6144 * Get IM status. When IM is on, return not 0. Else return 0.
6145 */
6146 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 return global_ime_get_status();
6150}
6151#endif
6152
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006153#ifdef FEAT_MBYTE
6154/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006155 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006156 */
6157 static void
6158latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6159{
6160 int c;
6161
Bram Moolenaarca003e12006-03-17 23:19:38 +00006162 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006163 {
6164 c = *text++;
6165 switch (c)
6166 {
6167 case 0xa4: c = 0x20ac; break; /* euro */
6168 case 0xa6: c = 0x0160; break; /* S hat */
6169 case 0xa8: c = 0x0161; break; /* S -hat */
6170 case 0xb4: c = 0x017d; break; /* Z hat */
6171 case 0xb8: c = 0x017e; break; /* Z -hat */
6172 case 0xbc: c = 0x0152; break; /* OE */
6173 case 0xbd: c = 0x0153; break; /* oe */
6174 case 0xbe: c = 0x0178; break; /* Y */
6175 }
6176 *unicodebuf++ = c;
6177 }
6178}
6179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181#ifdef FEAT_RIGHTLEFT
6182/*
6183 * What is this for? In the case where you are using Win98 or Win2K or later,
6184 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6185 * reverses the string sent to the TextOut... family. This sucks, because we
6186 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6187 * way to tell Windblows not to do this!
6188 *
6189 * The short of it is that this 'RevOut' only gets called if you are running
6190 * one of the new, "improved" MS OSes, and only if you are running in
6191 * 'rightleft' mode. It makes display take *slightly* longer, but not
6192 * noticeably so.
6193 */
6194 static void
6195RevOut( HDC s_hdc,
6196 int col,
6197 int row,
6198 UINT foptions,
6199 CONST RECT *pcliprect,
6200 LPCTSTR text,
6201 UINT len,
6202 CONST INT *padding)
6203{
6204 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006206 for (ix = 0; ix < (int)len; ++ix)
6207 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6208 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209}
6210#endif
6211
Bram Moolenaar92467d32017-12-05 13:22:16 +01006212 static void
6213draw_line(
6214 int x1,
6215 int y1,
6216 int x2,
6217 int y2,
6218 COLORREF color)
6219{
6220#if defined(FEAT_DIRECTX)
6221 if (IS_ENABLE_DIRECTX())
6222 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6223 else
6224#endif
6225 {
6226 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6227 HPEN old_pen = SelectObject(s_hdc, hpen);
6228 MoveToEx(s_hdc, x1, y1, NULL);
6229 /* Note: LineTo() excludes the last pixel in the line. */
6230 LineTo(s_hdc, x2, y2);
6231 DeleteObject(SelectObject(s_hdc, old_pen));
6232 }
6233}
6234
6235 static void
6236set_pixel(
6237 int x,
6238 int y,
6239 COLORREF color)
6240{
6241#if defined(FEAT_DIRECTX)
6242 if (IS_ENABLE_DIRECTX())
6243 DWriteContext_SetPixel(s_dwc, x, y, color);
6244 else
6245#endif
6246 SetPixel(s_hdc, x, y, color);
6247}
6248
6249 static void
6250fill_rect(
6251 const RECT *rcp,
6252 HBRUSH hbr,
6253 COLORREF color)
6254{
6255#if defined(FEAT_DIRECTX)
6256 if (IS_ENABLE_DIRECTX())
6257 DWriteContext_FillRect(s_dwc, rcp, color);
6258 else
6259#endif
6260 {
6261 HBRUSH hbr2;
6262
6263 if (hbr == NULL)
6264 hbr2 = CreateSolidBrush(color);
6265 else
6266 hbr2 = hbr;
6267 FillRect(s_hdc, rcp, hbr2);
6268 if (hbr == NULL)
6269 DeleteBrush(hbr2);
6270 }
6271}
6272
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 void
6274gui_mch_draw_string(
6275 int row,
6276 int col,
6277 char_u *text,
6278 int len,
6279 int flags)
6280{
6281 static int *padding = NULL;
6282 static int pad_size = 0;
6283 int i;
6284 const RECT *pcliprect = NULL;
6285 UINT foptions = 0;
6286#ifdef FEAT_MBYTE
6287 static WCHAR *unicodebuf = NULL;
6288 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006289 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 int n = 0;
6291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 int y;
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 /*
6295 * Italic and bold text seems to have an extra row of pixels at the bottom
6296 * (below where the bottom of the character should be). If we draw the
6297 * characters with a solid background, the top row of pixels in the
6298 * character below will be overwritten. We can fix this by filling in the
6299 * background ourselves, to the correct character proportions, and then
6300 * writing the character in transparent mode. Still have a problem when
6301 * the character is "_", which gets written on to the character below.
6302 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6303 * pixel in their slots, which fixes the problem with the bottom row of
6304 * pixels. We still need this code because otherwise the top row of pixels
6305 * becomes a problem. - webb.
6306 */
6307 static HBRUSH hbr_cache[2] = {NULL, NULL};
6308 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6309 static int brush_lru = 0;
6310 HBRUSH hbr;
6311 RECT rc;
6312
6313 if (!(flags & DRAW_TRANSP))
6314 {
6315 /*
6316 * Clear background first.
6317 * Note: FillRect() excludes right and bottom of rectangle.
6318 */
6319 rc.left = FILL_X(col);
6320 rc.top = FILL_Y(row);
6321#ifdef FEAT_MBYTE
6322 if (has_mbyte)
6323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006325 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327 else
6328#endif
6329 rc.right = FILL_X(col + len);
6330 rc.bottom = FILL_Y(row + 1);
6331
6332 /* Cache the created brush, that saves a lot of time. We need two:
6333 * one for cursor background and one for the normal background. */
6334 if (gui.currBgColor == brush_color[0])
6335 {
6336 hbr = hbr_cache[0];
6337 brush_lru = 1;
6338 }
6339 else if (gui.currBgColor == brush_color[1])
6340 {
6341 hbr = hbr_cache[1];
6342 brush_lru = 0;
6343 }
6344 else
6345 {
6346 if (hbr_cache[brush_lru] != NULL)
6347 DeleteBrush(hbr_cache[brush_lru]);
6348 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6349 brush_color[brush_lru] = gui.currBgColor;
6350 hbr = hbr_cache[brush_lru];
6351 brush_lru = !brush_lru;
6352 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006353
Bram Moolenaar92467d32017-12-05 13:22:16 +01006354 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
6356 SetBkMode(s_hdc, TRANSPARENT);
6357
6358 /*
6359 * When drawing block cursor, prevent inverted character spilling
6360 * over character cell (can happen with bold/italic)
6361 */
6362 if (flags & DRAW_CURSOR)
6363 {
6364 pcliprect = &rc;
6365 foptions = ETO_CLIPPED;
6366 }
6367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 SetTextColor(s_hdc, gui.currFgColor);
6369 SelectFont(s_hdc, gui.currFont);
6370
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006371#ifdef FEAT_DIRECTX
6372 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006373 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006374#endif
6375
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6377 {
6378 vim_free(padding);
6379 pad_size = Columns;
6380
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006381 /* Don't give an out-of-memory message here, it would call us
6382 * recursively. */
6383 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 if (padding != NULL)
6385 for (i = 0; i < pad_size; i++)
6386 padding[i] = gui.char_width;
6387 }
6388
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 /*
6390 * We have to provide the padding argument because italic and bold versions
6391 * of fixed-width fonts are often one pixel or so wider than their normal
6392 * versions.
6393 * No check for DRAW_BOLD, Windows will have done it already.
6394 */
6395
6396#ifdef FEAT_MBYTE
6397 /* Check if there are any UTF-8 characters. If not, use normal text
6398 * output to speed up output. */
6399 if (enc_utf8)
6400 for (n = 0; n < len; ++n)
6401 if (text[n] >= 0x80)
6402 break;
6403
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006404#if defined(FEAT_DIRECTX)
6405 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6406 * required that unicode drawing routine, currently. So this forces it
6407 * enabled. */
6408 if (enc_utf8 && IS_ENABLE_DIRECTX())
6409 n = 0; /* Keep n < len, to enter block for unicode. */
6410#endif
6411
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006413 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006415 if ((enc_utf8
6416 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6417 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 && (unicodebuf == NULL || len > unibuflen))
6419 {
6420 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006421 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006424 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426 unibuflen = len;
6427 }
6428
6429 if (enc_utf8 && n < len && unicodebuf != NULL)
6430 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006431 /* Output UTF-8 characters. Composing characters should be
6432 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006433 int i;
6434 int wlen; /* string length in words */
6435 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 int cells; /* cell width of string up to composing char */
6437 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006438 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006440 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006441 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006443 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006445 c = utf_ptr2char(text + i);
6446 if (c >= 0x10000)
6447 {
6448 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006449 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6450 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006451 }
6452 else
6453 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006454 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006455 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006456
6457 if (utf_iscomposing(c))
6458 cw = 0;
6459 else
6460 {
6461 cw = utf_char2cells(c);
6462 if (cw > 2) /* don't use 4 for unprintable char */
6463 cw = 1;
6464 }
6465
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 if (unicodepdy != NULL)
6467 {
6468 /* Use unicodepdy to make characters fit as we expect, even
6469 * when the font uses different widths (e.g., bold character
6470 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006471 if (c >= 0x10000)
6472 {
6473 unicodepdy[wlen - 2] = cw * gui.char_width;
6474 unicodepdy[wlen - 1] = 0;
6475 }
6476 else
6477 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006480 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006481 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006483#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006484 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006485 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006486 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006487 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006488 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006489 gui.char_width, gui.currFgColor,
6490 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006491 }
6492 else
6493#endif
6494 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6495 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 len = cells; /* used for underlining */
6497 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006498 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
6500 /* If we want to display codepage data, and the current CP is not the
6501 * ANSI one, we need to go via Unicode. */
6502 if (unicodebuf != NULL)
6503 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006504 if (enc_latin9)
6505 latin9_to_ucs(text, len, unicodebuf);
6506 else
6507 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 MB_PRECOMPOSED,
6509 (char *)text, len,
6510 (LPWSTR)unicodebuf, unibuflen);
6511 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006512 {
6513 /* Use unicodepdy to make characters fit as we expect, even
6514 * when the font uses different widths (e.g., bold character
6515 * is wider). */
6516 if (unicodepdy != NULL)
6517 {
6518 int i;
6519 int cw;
6520
6521 for (i = 0; i < len; ++i)
6522 {
6523 cw = utf_char2cells(unicodebuf[i]);
6524 if (cw > 2)
6525 cw = 1;
6526 unicodepdy[i] = cw * gui.char_width;
6527 }
6528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006530 foptions, pcliprect, unicodebuf, len, unicodepdy);
6531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 }
6533 }
6534 else
6535#endif
6536 {
6537#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006538 /* Windows will mess up RL text, so we have to draw it character by
6539 * character. Only do this if RL is on, since it's slow. */
6540 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6542 foptions, pcliprect, (char *)text, len, padding);
6543 else
6544#endif
6545 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6546 foptions, pcliprect, (char *)text, len, padding);
6547 }
6548
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006549 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 if (flags & DRAW_UNDERL)
6551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 /* When p_linespace is 0, overwrite the bottom row of pixels.
6553 * Otherwise put the line just below the character. */
6554 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (p_linespace > 1)
6556 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006557 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006559
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006560 /* Strikethrough */
6561 if (flags & DRAW_STRIKE)
6562 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006563 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006564 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006565 }
6566
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006567 /* Undercurl */
6568 if (flags & DRAW_UNDERC)
6569 {
6570 int x;
6571 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006572 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006573
6574 y = FILL_Y(row + 1) - 1;
6575 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6576 {
6577 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006578 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006579 }
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581}
6582
6583
6584/*
6585 * Output routines.
6586 */
6587
6588/* Flush any output to the screen */
6589 void
6590gui_mch_flush(void)
6591{
6592# if defined(__BORLANDC__)
6593 /*
6594 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6595 * prototype declaration.
6596 * The compiler complains if __stdcall is not used in both declarations.
6597 */
6598 BOOL __stdcall GdiFlush(void);
6599# endif
6600
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006601#if defined(FEAT_DIRECTX)
6602 if (IS_ENABLE_DIRECTX())
6603 DWriteContext_Flush(s_dwc);
6604#endif
6605
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 GdiFlush();
6607}
6608
6609 static void
6610clear_rect(RECT *rcp)
6611{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006612 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
6614
6615
Bram Moolenaarc716c302006-01-21 22:12:51 +00006616 void
6617gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6618{
6619 RECT workarea_rect;
6620
6621 get_work_area(&workarea_rect);
6622
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006623 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006624 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006625 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006626
6627 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6628 * the menubar for MSwin, we subtract it from the screen height, so that
6629 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006630 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006631 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006632 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006633 - GetSystemMetrics(SM_CYCAPTION)
6634#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006635 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006636#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006637 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006638}
6639
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641#if defined(FEAT_MENU) || defined(PROTO)
6642/*
6643 * Add a sub menu to the menu bar.
6644 */
6645 void
6646gui_mch_add_menu(
6647 vimmenu_T *menu,
6648 int pos)
6649{
6650 vimmenu_T *parent = menu->parent;
6651
6652 menu->submenu_id = CreatePopupMenu();
6653 menu->id = s_menu_id++;
6654
6655 if (menu_is_menubar(menu->name))
6656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006658 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006660 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6661 {
6662 /* 'encoding' differs from active codepage: convert menu name
6663 * and use wide function */
6664 wn = enc_to_utf16(menu->name, NULL);
6665 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006667 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006669 infow.cbSize = sizeof(infow);
6670 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6671 | MIIM_SUBMENU;
6672 infow.dwItemData = (long_u)menu;
6673 infow.wID = menu->id;
6674 infow.fType = MFT_STRING;
6675 infow.dwTypeData = wn;
6676 infow.cch = (UINT)wcslen(wn);
6677 infow.hSubMenu = menu->submenu_id;
6678 InsertMenuItemW((parent == NULL)
6679 ? s_menuBar : parent->submenu_id,
6680 (UINT)pos, TRUE, &infow);
6681 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006685 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006687 {
6688 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006690 info.cbSize = sizeof(info);
6691 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6692 info.dwItemData = (long_u)menu;
6693 info.wID = menu->id;
6694 info.fType = MFT_STRING;
6695 info.dwTypeData = (LPTSTR)menu->name;
6696 info.cch = (UINT)STRLEN(menu->name);
6697 info.hSubMenu = menu->submenu_id;
6698 InsertMenuItem((parent == NULL)
6699 ? s_menuBar : parent->submenu_id,
6700 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 }
6702 }
6703
6704 /* Fix window size if menu may have wrapped */
6705 if (parent == NULL)
6706 gui_mswin_get_menu_height(!gui.starting);
6707#ifdef FEAT_TEAROFF
6708 else if (IsWindow(parent->tearoff_handle))
6709 rebuild_tearoff(parent);
6710#endif
6711}
6712
6713 void
6714gui_mch_show_popupmenu(vimmenu_T *menu)
6715{
6716 POINT mp;
6717
6718 (void)GetCursorPos((LPPOINT)&mp);
6719 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6720}
6721
6722 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006723gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 vimmenu_T *menu = gui_find_menu(path_name);
6726
6727 if (menu != NULL)
6728 {
6729 POINT p;
6730
6731 /* Find the position of the current cursor */
6732 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006733 if (mouse_pos)
6734 {
6735 int mx, my;
6736
6737 gui_mch_getmouse(&mx, &my);
6738 p.x += mx;
6739 p.y += my;
6740 }
6741 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006743 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6745 }
6746 msg_scroll = FALSE;
6747 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6748 }
6749}
6750
6751#if defined(FEAT_TEAROFF) || defined(PROTO)
6752/*
6753 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6754 * create it as a pseudo-"tearoff menu".
6755 */
6756 void
6757gui_make_tearoff(char_u *path_name)
6758{
6759 vimmenu_T *menu = gui_find_menu(path_name);
6760
6761 /* Found the menu, so tear it off. */
6762 if (menu != NULL)
6763 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6764}
6765#endif
6766
6767/*
6768 * Add a menu item to a menu
6769 */
6770 void
6771gui_mch_add_menu_item(
6772 vimmenu_T *menu,
6773 int idx)
6774{
6775 vimmenu_T *parent = menu->parent;
6776
6777 menu->id = s_menu_id++;
6778 menu->submenu_id = NULL;
6779
6780#ifdef FEAT_TEAROFF
6781 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6782 {
6783 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6784 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6785 }
6786 else
6787#endif
6788#ifdef FEAT_TOOLBAR
6789 if (menu_is_toolbar(parent->name))
6790 {
6791 TBBUTTON newtb;
6792
6793 vim_memset(&newtb, 0, sizeof(newtb));
6794 if (menu_is_separator(menu->name))
6795 {
6796 newtb.iBitmap = 0;
6797 newtb.fsStyle = TBSTYLE_SEP;
6798 }
6799 else
6800 {
6801 newtb.iBitmap = get_toolbar_bitmap(menu);
6802 newtb.fsStyle = TBSTYLE_BUTTON;
6803 }
6804 newtb.idCommand = menu->id;
6805 newtb.fsState = TBSTATE_ENABLED;
6806 newtb.iString = 0;
6807 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6808 (LPARAM)&newtb);
6809 menu->submenu_id = (HMENU)-1;
6810 }
6811 else
6812#endif
6813 {
6814#ifdef FEAT_MBYTE
6815 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6818 {
6819 /* 'encoding' differs from active codepage: convert menu item name
6820 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006821 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 if (wn != NULL)
6823 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006824 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 (menu_is_separator(menu->name)
6826 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6827 (UINT)menu->id, wn);
6828 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
6830 }
6831 if (wn == NULL)
6832#endif
6833 InsertMenu(parent->submenu_id, (UINT)idx,
6834 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6835 | MF_BYPOSITION,
6836 (UINT)menu->id, (LPCTSTR)menu->name);
6837#ifdef FEAT_TEAROFF
6838 if (IsWindow(parent->tearoff_handle))
6839 rebuild_tearoff(parent);
6840#endif
6841 }
6842}
6843
6844/*
6845 * Destroy the machine specific menu widget.
6846 */
6847 void
6848gui_mch_destroy_menu(vimmenu_T *menu)
6849{
6850#ifdef FEAT_TOOLBAR
6851 /*
6852 * is this a toolbar button?
6853 */
6854 if (menu->submenu_id == (HMENU)-1)
6855 {
6856 int iButton;
6857
6858 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6859 (WPARAM)menu->id, 0);
6860 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6861 }
6862 else
6863#endif
6864 {
6865 if (menu->parent != NULL
6866 && menu_is_popup(menu->parent->dname)
6867 && menu->parent->submenu_id != NULL)
6868 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6869 else
6870 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6871 if (menu->submenu_id != NULL)
6872 DestroyMenu(menu->submenu_id);
6873#ifdef FEAT_TEAROFF
6874 if (IsWindow(menu->tearoff_handle))
6875 DestroyWindow(menu->tearoff_handle);
6876 if (menu->parent != NULL
6877 && menu->parent->children != NULL
6878 && IsWindow(menu->parent->tearoff_handle))
6879 {
6880 /* This menu must not show up when rebuilding the tearoff window. */
6881 menu->modes = 0;
6882 rebuild_tearoff(menu->parent);
6883 }
6884#endif
6885 }
6886}
6887
6888#ifdef FEAT_TEAROFF
6889 static void
6890rebuild_tearoff(vimmenu_T *menu)
6891{
6892 /*hackish*/
6893 char_u tbuf[128];
6894 RECT trect;
6895 RECT rct;
6896 RECT roct;
6897 int x, y;
6898
6899 HWND thwnd = menu->tearoff_handle;
6900
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006901 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (GetWindowRect(thwnd, &trect)
6903 && GetWindowRect(s_hwnd, &rct)
6904 && GetClientRect(s_hwnd, &roct))
6905 {
6906 x = trect.left - rct.left;
6907 y = (trect.top - rct.bottom + roct.bottom);
6908 }
6909 else
6910 {
6911 x = y = 0xffffL;
6912 }
6913 DestroyWindow(thwnd);
6914 if (menu->children != NULL)
6915 {
6916 gui_mch_tearoff(tbuf, menu, x, y);
6917 if (IsWindow(menu->tearoff_handle))
6918 (void) SetWindowPos(menu->tearoff_handle,
6919 NULL,
6920 (int)trect.left,
6921 (int)trect.top,
6922 0, 0,
6923 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6924 }
6925}
6926#endif /* FEAT_TEAROFF */
6927
6928/*
6929 * Make a menu either grey or not grey.
6930 */
6931 void
6932gui_mch_menu_grey(
6933 vimmenu_T *menu,
6934 int grey)
6935{
6936#ifdef FEAT_TOOLBAR
6937 /*
6938 * is this a toolbar button?
6939 */
6940 if (menu->submenu_id == (HMENU)-1)
6941 {
6942 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6943 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6944 }
6945 else
6946#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006947 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6948 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950#ifdef FEAT_TEAROFF
6951 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6952 {
6953 WORD menuID;
6954 HWND menuHandle;
6955
6956 /*
6957 * A tearoff button has changed state.
6958 */
6959 if (menu->children == NULL)
6960 menuID = (WORD)(menu->id);
6961 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006962 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6964 if (menuHandle)
6965 EnableWindow(menuHandle, !grey);
6966
6967 }
6968#endif
6969}
6970
6971#endif /* FEAT_MENU */
6972
6973
6974/* define some macros used to make the dialogue creation more readable */
6975
6976#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6977#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006978#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
6980#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6981/*
6982 * stuff for dialogs
6983 */
6984
6985/*
6986 * The callback routine used by all the dialogs. Very simple. First,
6987 * acknowledges the INITDIALOG message so that Windows knows to do standard
6988 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6989 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6990 * number.
6991 */
6992 static LRESULT CALLBACK
6993dialog_callback(
6994 HWND hwnd,
6995 UINT message,
6996 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006997 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998{
6999 if (message == WM_INITDIALOG)
7000 {
7001 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7002 /* Set focus to the dialog. Set the default button, if specified. */
7003 (void)SetFocus(hwnd);
7004 if (dialog_default_button > IDCANCEL)
7005 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007006 else
7007 /* We don't have a default, set focus on another element of the
7008 * dialog window, probably the icon */
7009 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 return FALSE;
7011 }
7012
7013 if (message == WM_COMMAND)
7014 {
7015 int button = LOWORD(wParam);
7016
7017 /* Don't end the dialog if something was selected that was
7018 * not a button.
7019 */
7020 if (button >= DLG_NONBUTTON_CONTROL)
7021 return TRUE;
7022
7023 /* If the edit box exists, copy the string. */
7024 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007025 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007026# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007027 /* If the OS is Windows NT, and 'encoding' differs from active
7028 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007029 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007030 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007031 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7032 char_u *p;
7033
7034 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7035 p = utf16_to_enc(wp, NULL);
7036 vim_strncpy(s_textfield, p, IOSIZE);
7037 vim_free(p);
7038 vim_free(wp);
7039 }
7040 else
7041# endif
7042 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007043 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046 /*
7047 * Need to check for IDOK because if the user just hits Return to
7048 * accept the default value, some reason this is what we get.
7049 */
7050 if (button == IDOK)
7051 {
7052 if (dialog_default_button > IDCANCEL)
7053 EndDialog(hwnd, dialog_default_button);
7054 }
7055 else
7056 EndDialog(hwnd, button - IDCANCEL);
7057 return TRUE;
7058 }
7059
7060 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7061 {
7062 EndDialog(hwnd, 0);
7063 return TRUE;
7064 }
7065 return FALSE;
7066}
7067
7068/*
7069 * Create a dialog dynamically from the parameter strings.
7070 * type = type of dialog (question, alert, etc.)
7071 * title = dialog title. may be NULL for default title.
7072 * message = text to display. Dialog sizes to accommodate it.
7073 * buttons = '\n' separated list of button captions, default first.
7074 * dfltbutton = number of default button.
7075 *
7076 * This routine returns 1 if the first button is pressed,
7077 * 2 for the second, etc.
7078 *
7079 * 0 indicates Esc was pressed.
7080 * -1 for unexpected error
7081 *
7082 * If stubbing out this fn, return 1.
7083 */
7084
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007085static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 "IDR_VIM",
7088 "IDR_VIM_ERROR",
7089 "IDR_VIM_ALERT",
7090 "IDR_VIM_INFO",
7091 "IDR_VIM_QUESTION"
7092};
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 int
7095gui_mch_dialog(
7096 int type,
7097 char_u *title,
7098 char_u *message,
7099 char_u *buttons,
7100 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007101 char_u *textfield,
7102 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007105 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 int numButtons;
7107 int *buttonWidths, *buttonPositions;
7108 int buttonYpos;
7109 int nchar, i;
7110 DWORD lStyle;
7111 int dlgwidth = 0;
7112 int dlgheight;
7113 int editboxheight;
7114 int horizWidth = 0;
7115 int msgheight;
7116 char_u *pstart;
7117 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007118 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 char_u *tbuffer;
7120 RECT rect;
7121 HWND hwnd;
7122 HDC hdc;
7123 HFONT font, oldFont;
7124 TEXTMETRIC fontInfo;
7125 int fontHeight;
7126 int textWidth, minButtonWidth, messageWidth;
7127 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007128 int maxDialogHeight;
7129 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 int vertical;
7131 int dlgPaddingX;
7132 int dlgPaddingY;
7133#ifdef USE_SYSMENU_FONT
7134 LOGFONT lfSysmenu;
7135 int use_lfSysmenu = FALSE;
7136#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007137 garray_T ga;
7138 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140#ifndef NO_CONSOLE
7141 /* Don't output anything in silent mode ("ex -s") */
7142 if (silent_mode)
7143 return dfltbutton; /* return default option */
7144#endif
7145
Bram Moolenaar748bf032005-02-02 23:04:36 +00007146 if (s_hwnd == NULL)
7147 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149 if ((type < 0) || (type > VIM_LAST_TYPE))
7150 type = 0;
7151
7152 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007153 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007154 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007155 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 if (p == NULL)
7158 return -1;
7159
7160 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007161 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7163 * const.
7164 */
7165 tbuffer = vim_strsave(buttons);
7166 if (tbuffer == NULL)
7167 return -1;
7168
7169 --dfltbutton; /* Change from one-based to zero-based */
7170
7171 /* Count buttons */
7172 numButtons = 1;
7173 for (i = 0; tbuffer[i] != '\0'; i++)
7174 {
7175 if (tbuffer[i] == DLG_BUTTON_SEP)
7176 numButtons++;
7177 }
7178 if (dfltbutton >= numButtons)
7179 dfltbutton = -1;
7180
7181 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007182 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 if (buttonWidths == NULL)
7184 return -1;
7185
7186 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007187 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (buttonPositions == NULL)
7189 return -1;
7190
7191 /*
7192 * Calculate how big the dialog must be.
7193 */
7194 hwnd = GetDesktopWindow();
7195 hdc = GetWindowDC(hwnd);
7196#ifdef USE_SYSMENU_FONT
7197 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7198 {
7199 font = CreateFontIndirect(&lfSysmenu);
7200 use_lfSysmenu = TRUE;
7201 }
7202 else
7203#endif
7204 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7205 VARIABLE_PITCH , DLG_FONT_NAME);
7206 if (s_usenewlook)
7207 {
7208 oldFont = SelectFont(hdc, font);
7209 dlgPaddingX = DLG_PADDING_X;
7210 dlgPaddingY = DLG_PADDING_Y;
7211 }
7212 else
7213 {
7214 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7215 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7216 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7217 }
7218 GetTextMetrics(hdc, &fontInfo);
7219 fontHeight = fontInfo.tmHeight;
7220
7221 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007222 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
7224 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007225 if (s_hwnd == NULL)
7226 {
7227 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
Bram Moolenaarc716c302006-01-21 22:12:51 +00007229 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007230 get_work_area(&workarea_rect);
7231 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7232 if (maxDialogWidth > 600)
7233 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007234 /* Leave some room for the taskbar. */
7235 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007236 }
7237 else
7238 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007239 /* Use our own window for the size, unless it's very small. */
7240 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007241 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007242 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007243 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007244 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7245 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007246
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007247 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007248 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007249 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007250 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007251 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7252 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7253 }
7254
7255 /* Set dlgwidth to width of message.
7256 * Copy the message into "ga", changing NL to CR-NL and inserting line
7257 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 pstart = message;
7259 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007260 msgheight = 0;
7261 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 do
7263 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007264 msgheight += fontHeight; /* at least one line */
7265
7266 /* Need to figure out where to break the string. The system does it
7267 * at a word boundary, which would mean we can't compute the number of
7268 * wrapped lines. */
7269 textWidth = 0;
7270 last_white = NULL;
7271 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007272 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007273#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007275#else
7276 l = 1;
7277#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007278 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007279 && textWidth > maxDialogWidth * 3 / 4)
7280 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007281 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007282 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007283 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007284 /* Line will wrap. */
7285 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007286 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007287 textWidth = 0;
7288
7289 if (last_white != NULL)
7290 {
7291 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007293 pend = last_white + 1;
7294 last_white = NULL;
7295 }
7296 ga_append(&ga, '\r');
7297 ga_append(&ga, '\n');
7298 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007299 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007300
7301 while (--l >= 0)
7302 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007303 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007304 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007306
7307 ga_append(&ga, '\r');
7308 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 pstart = pend + 1;
7310 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007311
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007312 if (ga.ga_data != NULL)
7313 message = ga.ga_data;
7314
Bram Moolenaar748bf032005-02-02 23:04:36 +00007315 messageWidth += 10; /* roundoff space */
7316
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007318 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7319 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320
7321 if (msgheight < DLG_ICON_HEIGHT)
7322 msgheight = DLG_ICON_HEIGHT;
7323
7324 /*
7325 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007326 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007328 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 if (!vertical)
7330 {
7331 // Place buttons horizontally if they fit.
7332 horizWidth = dlgPaddingX;
7333 pstart = tbuffer;
7334 i = 0;
7335 do
7336 {
7337 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7338 if (pend == NULL)
7339 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007340 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 if (textWidth < minButtonWidth)
7342 textWidth = minButtonWidth;
7343 textWidth += dlgPaddingX; /* Padding within button */
7344 buttonWidths[i] = textWidth;
7345 buttonPositions[i++] = horizWidth;
7346 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7347 pstart = pend + 1;
7348 } while (*pend != NUL);
7349
7350 if (horizWidth > maxDialogWidth)
7351 vertical = TRUE; // Too wide to fit on the screen.
7352 else if (horizWidth > dlgwidth)
7353 dlgwidth = horizWidth;
7354 }
7355
7356 if (vertical)
7357 {
7358 // Stack buttons vertically.
7359 pstart = tbuffer;
7360 do
7361 {
7362 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7363 if (pend == NULL)
7364 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007365 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 textWidth += dlgPaddingX; /* Padding within button */
7367 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7368 if (textWidth > dlgwidth)
7369 dlgwidth = textWidth;
7370 pstart = pend + 1;
7371 } while (*pend != NUL);
7372 }
7373
7374 if (dlgwidth < DLG_MIN_WIDTH)
7375 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7376
7377 /* start to fill in the dlgtemplate information. addressing by WORDs */
7378 if (s_usenewlook)
7379 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7380 else
7381 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7382
7383 add_long(lStyle);
7384 add_long(0); // (lExtendedStyle)
7385 pnumitems = p; /*save where the number of items must be stored*/
7386 add_word(0); // NumberOfItems(will change later)
7387 add_word(10); // x
7388 add_word(10); // y
7389 add_word(PixelToDialogX(dlgwidth)); // cx
7390
7391 // Dialog height.
7392 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007393 dlgheight = msgheight + 2 * dlgPaddingY
7394 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 else
7396 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7397
7398 // Dialog needs to be taller if contains an edit box.
7399 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7400 if (textfield != NULL)
7401 dlgheight += editboxheight;
7402
Bram Moolenaara95d8232013-08-07 15:27:11 +02007403 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7404 if (dlgheight > maxDialogHeight)
7405 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007406 msgheight = msgheight - (dlgheight - maxDialogHeight);
7407 dlgheight = maxDialogHeight;
7408 scroll_flag = WS_VSCROLL;
7409 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7410 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007411 }
7412
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 add_word(PixelToDialogY(dlgheight));
7414
7415 add_word(0); // Menu
7416 add_word(0); // Class
7417
7418 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007419 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7420 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 p += nchar;
7422
7423 if (s_usenewlook)
7424 {
7425 /* do the font, since DS_3DLOOK doesn't work properly */
7426#ifdef USE_SYSMENU_FONT
7427 if (use_lfSysmenu)
7428 {
7429 /* point size */
7430 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7431 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007432 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 }
7434 else
7435#endif
7436 {
7437 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007438 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440 p += nchar;
7441 }
7442
7443 buttonYpos = msgheight + 2 * dlgPaddingY;
7444
7445 if (textfield != NULL)
7446 buttonYpos += editboxheight;
7447
7448 pstart = tbuffer;
7449 if (!vertical)
7450 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7451 for (i = 0; i < numButtons; i++)
7452 {
7453 /* get end of this button. */
7454 for ( pend = pstart;
7455 *pend && (*pend != DLG_BUTTON_SEP);
7456 pend++)
7457 ;
7458
7459 if (*pend)
7460 *pend = '\0';
7461
7462 /*
7463 * old NOTE:
7464 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7465 * the focus to the first tab-able button and in so doing makes that
7466 * the default!! Grrr. Workaround: Make the default button the only
7467 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7468 * he/she can use arrow keys.
7469 *
7470 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007471 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 * dialog. Also needed for when the textfield is the default control.
7473 * It appears to work now (perhaps not on Win95?).
7474 */
7475 if (vertical)
7476 {
7477 p = add_dialog_element(p,
7478 (i == dfltbutton
7479 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7480 PixelToDialogX(DLG_VERT_PADDING_X),
7481 PixelToDialogY(buttonYpos /* TBK */
7482 + 2 * fontHeight * i),
7483 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7484 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007485 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
7487 else
7488 {
7489 p = add_dialog_element(p,
7490 (i == dfltbutton
7491 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7492 PixelToDialogX(horizWidth + buttonPositions[i]),
7493 PixelToDialogY(buttonYpos), /* TBK */
7494 PixelToDialogX(buttonWidths[i]),
7495 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007496 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498 pstart = pend + 1; /*next button*/
7499 }
7500 *pnumitems += numButtons;
7501
7502 /* Vim icon */
7503 p = add_dialog_element(p, SS_ICON,
7504 PixelToDialogX(dlgPaddingX),
7505 PixelToDialogY(dlgPaddingY),
7506 PixelToDialogX(DLG_ICON_WIDTH),
7507 PixelToDialogY(DLG_ICON_HEIGHT),
7508 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7509 dlg_icons[type]);
7510
Bram Moolenaar748bf032005-02-02 23:04:36 +00007511 /* Dialog message */
7512 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7513 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7514 PixelToDialogY(dlgPaddingY),
7515 (WORD)(PixelToDialogX(messageWidth) + 1),
7516 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007517 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
7519 /* Edit box */
7520 if (textfield != NULL)
7521 {
7522 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7523 PixelToDialogX(2 * dlgPaddingX),
7524 PixelToDialogY(2 * dlgPaddingY + msgheight),
7525 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7526 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007527 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 *pnumitems += 1;
7529 }
7530
7531 *pnumitems += 2;
7532
7533 SelectFont(hdc, oldFont);
7534 DeleteObject(font);
7535 ReleaseDC(hwnd, hdc);
7536
7537 /* Let the dialog_callback() function know which button to make default
7538 * If we have an edit box, make that the default. We also need to tell
7539 * dialog_callback() if this dialog contains an edit box or not. We do
7540 * this by setting s_textfield if it does.
7541 */
7542 if (textfield != NULL)
7543 {
7544 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7545 s_textfield = textfield;
7546 }
7547 else
7548 {
7549 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7550 s_textfield = NULL;
7551 }
7552
7553 /* show the dialog box modally and get a return value */
7554 nchar = (int)DialogBoxIndirect(
7555 s_hinst,
7556 (LPDLGTEMPLATE)pdlgtemplate,
7557 s_hwnd,
7558 (DLGPROC)dialog_callback);
7559
7560 LocalFree(LocalHandle(pdlgtemplate));
7561 vim_free(tbuffer);
7562 vim_free(buttonWidths);
7563 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007564 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
7566 /* Focus back to our window (for when MDI is used). */
7567 (void)SetFocus(s_hwnd);
7568
7569 return nchar;
7570}
7571
7572#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007573
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574/*
7575 * Put a simple element (basic class) onto a dialog template in memory.
7576 * return a pointer to where the next item should be added.
7577 *
7578 * parameters:
7579 * lStyle = additional style flags
7580 * (be careful, NT3.51 & Win32s will ignore the new ones)
7581 * x,y = x & y positions IN DIALOG UNITS
7582 * w,h = width and height IN DIALOG UNITS
7583 * Id = ID used in messages
7584 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7585 * caption = usually text or resource name
7586 *
7587 * TODO: use the length information noted here to enable the dialog creation
7588 * routines to work out more exactly how much memory they need to alloc.
7589 */
7590 static PWORD
7591add_dialog_element(
7592 PWORD p,
7593 DWORD lStyle,
7594 WORD x,
7595 WORD y,
7596 WORD w,
7597 WORD h,
7598 WORD Id,
7599 WORD clss,
7600 const char *caption)
7601{
7602 int nchar;
7603
7604 p = lpwAlign(p); /* Align to dword boundary*/
7605 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7606 *p++ = LOWORD(lStyle);
7607 *p++ = HIWORD(lStyle);
7608 *p++ = 0; // LOWORD (lExtendedStyle)
7609 *p++ = 0; // HIWORD (lExtendedStyle)
7610 *p++ = x;
7611 *p++ = y;
7612 *p++ = w;
7613 *p++ = h;
7614 *p++ = Id; //9 or 10 words in all
7615
7616 *p++ = (WORD)0xffff;
7617 *p++ = clss; //2 more here
7618
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007619 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 p += nchar;
7621
7622 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7623
7624 return p; //total = 15+ (strlen(caption)) words
7625 // = 30 + 2(strlen(caption) bytes reqd
7626}
7627
7628
7629/*
7630 * Helper routine. Take an input pointer, return closest pointer that is
7631 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7632 */
7633 static LPWORD
7634lpwAlign(
7635 LPWORD lpIn)
7636{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007637 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007639 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 ul += 3;
7641 ul >>= 2;
7642 ul <<= 2;
7643 return (LPWORD)ul;
7644}
7645
7646/*
7647 * Helper routine. Takes second parameter as Ansi string, copies it to first
7648 * parameter as wide character (16-bits / char) string, and returns integer
7649 * number of wide characters (words) in string (including the trailing wide
7650 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007651 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7652 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 static int
7654nCopyAnsiToWideChar(
7655 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007656 LPSTR lpAnsiIn,
7657 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658{
7659 int nChar = 0;
7660#ifdef FEAT_MBYTE
7661 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7662 int i;
7663 WCHAR *wn;
7664
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007665 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 {
7667 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007668 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 if (wn != NULL)
7670 {
7671 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007672 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 vim_free(wn);
7674 }
7675 }
7676 if (nChar == 0)
7677 /* Use Win32 conversion function. */
7678 nChar = MultiByteToWideChar(
7679 enc_codepage > 0 ? enc_codepage : CP_ACP,
7680 MB_PRECOMPOSED,
7681 lpAnsiIn, len,
7682 lpWCStr, len);
7683 for (i = 0; i < nChar; ++i)
7684 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7685 lpWCStr[i] = (WORD)' ';
7686#else
7687 do
7688 {
7689 if (*lpAnsiIn == '\t')
7690 *lpWCStr++ = (WORD)' ';
7691 else
7692 *lpWCStr++ = (WORD)*lpAnsiIn;
7693 nChar++;
7694 } while (*lpAnsiIn++);
7695#endif
7696
7697 return nChar;
7698}
7699
7700
7701#ifdef FEAT_TEAROFF
7702/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007703 * Lookup menu handle from "menu_id".
7704 */
7705 static HMENU
7706tearoff_lookup_menuhandle(
7707 vimmenu_T *menu,
7708 WORD menu_id)
7709{
7710 for ( ; menu != NULL; menu = menu->next)
7711 {
7712 if (menu->modes == 0) /* this menu has just been deleted */
7713 continue;
7714 if (menu_is_separator(menu->dname))
7715 continue;
7716 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7717 return menu->submenu_id;
7718 }
7719 return NULL;
7720}
7721
7722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 * The callback function for all the modeless dialogs that make up the
7724 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7725 * thinking its menus have been clicked), and go away when closed.
7726 */
7727 static LRESULT CALLBACK
7728tearoff_callback(
7729 HWND hwnd,
7730 UINT message,
7731 WPARAM wParam,
7732 LPARAM lParam)
7733{
7734 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007735 {
7736 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
7740 /* May show the mouse pointer again. */
7741 HandleMouseHide(message, lParam);
7742
7743 if (message == WM_COMMAND)
7744 {
7745 if ((WORD)(LOWORD(wParam)) & 0x8000)
7746 {
7747 POINT mp;
7748 RECT rect;
7749
7750 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7751 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007752 vimmenu_T *menu;
7753
7754 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007756 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7758 (int)rect.right - 8,
7759 (int)mp.y,
7760 (int)0, /*reserved param*/
7761 s_hwnd,
7762 NULL);
7763 /*
7764 * NOTE: The pop-up menu can eat the mouse up event.
7765 * We deal with this in normal.c.
7766 */
7767 }
7768 }
7769 else
7770 /* Pass on messages to the main Vim window */
7771 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7772 /*
7773 * Give main window the focus back: this is so after
7774 * choosing a tearoff button you can start typing again
7775 * straight away.
7776 */
7777 (void)SetFocus(s_hwnd);
7778 return TRUE;
7779 }
7780 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7781 {
7782 DestroyWindow(hwnd);
7783 return TRUE;
7784 }
7785
7786 /* When moved around, give main window the focus back. */
7787 if (message == WM_EXITSIZEMOVE)
7788 (void)SetActiveWindow(s_hwnd);
7789
7790 return FALSE;
7791}
7792#endif
7793
7794
7795/*
7796 * Decide whether to use the "new look" (small, non-bold font) or the "old
7797 * look" (big, clanky font) for dialogs, and work out a few values for use
7798 * later accordingly.
7799 */
7800 static void
7801get_dialog_font_metrics(void)
7802{
7803 HDC hdc;
7804 HFONT hfontTools = 0;
7805 DWORD dlgFontSize;
7806 SIZE size;
7807#ifdef USE_SYSMENU_FONT
7808 LOGFONT lfSysmenu;
7809#endif
7810
7811 s_usenewlook = FALSE;
7812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007814 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7815 hfontTools = CreateFontIndirect(&lfSysmenu);
7816 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817#endif
7818 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7819 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7820
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007821 if (hfontTools)
7822 {
7823 hdc = GetDC(s_hwnd);
7824 SelectObject(hdc, hfontTools);
7825 /*
7826 * GetTextMetrics() doesn't return the right value in
7827 * tmAveCharWidth, so we have to figure out the dialog base units
7828 * ourselves.
7829 */
7830 GetTextExtentPoint(hdc,
7831 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7832 52, &size);
7833 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007835 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7836 s_dlgfntheight = (WORD)size.cy;
7837 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839
7840 if (!s_usenewlook)
7841 {
7842 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7843 s_dlgfntwidth = LOWORD(dlgFontSize);
7844 s_dlgfntheight = HIWORD(dlgFontSize);
7845 }
7846}
7847
7848#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7849/*
7850 * Create a pseudo-"tearoff menu" based on the child
7851 * items of a given menu pointer.
7852 */
7853 static void
7854gui_mch_tearoff(
7855 char_u *title,
7856 vimmenu_T *menu,
7857 int initX,
7858 int initY)
7859{
7860 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7861 int template_len;
7862 int nchar, textWidth, submenuWidth;
7863 DWORD lStyle;
7864 DWORD lExtendedStyle;
7865 WORD dlgwidth;
7866 WORD menuID;
7867 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007868 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 vimmenu_T *the_menu = menu;
7870 HWND hwnd;
7871 HDC hdc;
7872 HFONT font, oldFont;
7873 int col, spaceWidth, len;
7874 int columnWidths[2];
7875 char_u *label, *text;
7876 int acLen = 0;
7877 int nameLen;
7878 int padding0, padding1, padding2 = 0;
7879 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007880 int x;
7881 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882#ifdef USE_SYSMENU_FONT
7883 LOGFONT lfSysmenu;
7884 int use_lfSysmenu = FALSE;
7885#endif
7886
7887 /*
7888 * If this menu is already torn off, move it to the mouse position.
7889 */
7890 if (IsWindow(menu->tearoff_handle))
7891 {
7892 POINT mp;
7893 if (GetCursorPos((LPPOINT)&mp))
7894 {
7895 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7896 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7897 }
7898 return;
7899 }
7900
7901 /*
7902 * Create a new tearoff.
7903 */
7904 if (*title == MNU_HIDDEN_CHAR)
7905 title++;
7906
7907 /* Allocate memory to store the dialog template. It's made bigger when
7908 * needed. */
7909 template_len = DLG_ALLOC_SIZE;
7910 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7911 if (p == NULL)
7912 return;
7913
7914 hwnd = GetDesktopWindow();
7915 hdc = GetWindowDC(hwnd);
7916#ifdef USE_SYSMENU_FONT
7917 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7918 {
7919 font = CreateFontIndirect(&lfSysmenu);
7920 use_lfSysmenu = TRUE;
7921 }
7922 else
7923#endif
7924 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7925 VARIABLE_PITCH , DLG_FONT_NAME);
7926 if (s_usenewlook)
7927 oldFont = SelectFont(hdc, font);
7928 else
7929 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7930
7931 /* Calculate width of a single space. Used for padding columns to the
7932 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007933 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
7935 /* Figure out max width of the text column, the accelerator column and the
7936 * optional submenu column. */
7937 submenuWidth = 0;
7938 for (col = 0; col < 2; col++)
7939 {
7940 columnWidths[col] = 0;
7941 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7942 {
7943 /* Use "dname" here to compute the width of the visible text. */
7944 text = (col == 0) ? pmenu->dname : pmenu->actext;
7945 if (text != NULL && *text != NUL)
7946 {
7947 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7948 if (textWidth > columnWidths[col])
7949 columnWidths[col] = textWidth;
7950 }
7951 if (pmenu->children != NULL)
7952 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7953 }
7954 }
7955 if (columnWidths[1] == 0)
7956 {
7957 /* no accelerators */
7958 if (submenuWidth != 0)
7959 columnWidths[0] += submenuWidth;
7960 else
7961 columnWidths[0] += spaceWidth;
7962 }
7963 else
7964 {
7965 /* there is an accelerator column */
7966 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7967 columnWidths[1] += submenuWidth;
7968 }
7969
7970 /*
7971 * Now find the total width of our 'menu'.
7972 */
7973 textWidth = columnWidths[0] + columnWidths[1];
7974 if (submenuWidth != 0)
7975 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007976 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7978 textWidth += submenuWidth;
7979 }
7980 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7981 if (textWidth > dlgwidth)
7982 dlgwidth = textWidth;
7983 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7984
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 /* start to fill in the dlgtemplate information. addressing by WORDs */
7986 if (s_usenewlook)
7987 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7988 else
7989 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7990
7991 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7992 *p++ = LOWORD(lStyle);
7993 *p++ = HIWORD(lStyle);
7994 *p++ = LOWORD(lExtendedStyle);
7995 *p++ = HIWORD(lExtendedStyle);
7996 pnumitems = p; /* save where the number of items must be stored */
7997 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007998 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008000 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 else
8002 *p++ = PixelToDialogX(initX); // x
8003 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008004 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 else
8006 *p++ = PixelToDialogY(initY); // y
8007 *p++ = PixelToDialogX(dlgwidth); // cx
8008 ptrueheight = p;
8009 *p++ = 0; // dialog height: changed later anyway
8010 *p++ = 0; // Menu
8011 *p++ = 0; // Class
8012
8013 /* copy the title of the dialog */
8014 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008015 ? (LPSTR)title
8016 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 p += nchar;
8018
8019 if (s_usenewlook)
8020 {
8021 /* do the font, since DS_3DLOOK doesn't work properly */
8022#ifdef USE_SYSMENU_FONT
8023 if (use_lfSysmenu)
8024 {
8025 /* point size */
8026 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8027 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008028 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 }
8030 else
8031#endif
8032 {
8033 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008034 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 p += nchar;
8037 }
8038
8039 /*
8040 * Loop over all the items in the menu.
8041 * But skip over the tearbar.
8042 */
8043 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8044 menu = menu->children->next;
8045 else
8046 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008047 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 for ( ; menu != NULL; menu = menu->next)
8049 {
8050 if (menu->modes == 0) /* this menu has just been deleted */
8051 continue;
8052 if (menu_is_separator(menu->dname))
8053 {
8054 sepPadding += 3;
8055 continue;
8056 }
8057
8058 /* Check if there still is plenty of room in the template. Make it
8059 * larger when needed. */
8060 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8061 {
8062 WORD *newp;
8063
8064 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8065 if (newp != NULL)
8066 {
8067 template_len += 4096;
8068 mch_memmove(newp, pdlgtemplate,
8069 (char *)p - (char *)pdlgtemplate);
8070 p = newp + (p - pdlgtemplate);
8071 pnumitems = newp + (pnumitems - pdlgtemplate);
8072 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8073 LocalFree(LocalHandle(pdlgtemplate));
8074 pdlgtemplate = newp;
8075 }
8076 }
8077
8078 /* Figure out minimal length of this menu label. Use "name" for the
8079 * actual text, "dname" for estimating the displayed size. "name"
8080 * has "&a" for mnemonic and includes the accelerator. */
8081 len = nameLen = (int)STRLEN(menu->name);
8082 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8083 (int)STRLEN(menu->dname))) / spaceWidth;
8084 len += padding0;
8085
8086 if (menu->actext != NULL)
8087 {
8088 acLen = (int)STRLEN(menu->actext);
8089 len += acLen;
8090 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8091 }
8092 else
8093 textWidth = 0;
8094 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8095 len += padding1;
8096
8097 if (menu->children == NULL)
8098 {
8099 padding2 = submenuWidth / spaceWidth;
8100 len += padding2;
8101 menuID = (WORD)(menu->id);
8102 }
8103 else
8104 {
8105 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008106 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108
8109 /* Allocate menu label and fill it in */
8110 text = label = alloc((unsigned)len + 1);
8111 if (label == NULL)
8112 break;
8113
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008114 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8116 if (text == NULL)
8117 text = label + nameLen; /* no actext, use whole name */
8118 while (padding0-- > 0)
8119 *text++ = ' ';
8120 if (menu->actext != NULL)
8121 {
8122 STRNCPY(text, menu->actext, acLen);
8123 text += acLen;
8124 }
8125 while (padding1-- > 0)
8126 *text++ = ' ';
8127 if (menu->children != NULL)
8128 {
8129 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8130 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8131 }
8132 else
8133 {
8134 while (padding2-- > 0)
8135 *text++ = ' ';
8136 }
8137 *text = NUL;
8138
8139 /*
8140 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8141 * W95/NT4 it makes the tear-off look more like a menu.
8142 */
8143 p = add_dialog_element(p,
8144 BS_PUSHBUTTON|BS_LEFT,
8145 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8146 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8147 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8148 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008149 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 vim_free(label);
8151 (*pnumitems)++;
8152 }
8153
8154 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8155
8156
8157 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008158 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 s_hinst,
8160 (LPDLGTEMPLATE)pdlgtemplate,
8161 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008162 (DLGPROC)tearoff_callback,
8163 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 LocalFree(LocalHandle(pdlgtemplate));
8166 SelectFont(hdc, oldFont);
8167 DeleteObject(font);
8168 ReleaseDC(hwnd, hdc);
8169
8170 /*
8171 * Reassert ourselves as the active window. This is so that after creating
8172 * a tearoff, the user doesn't have to click with the mouse just to start
8173 * typing again!
8174 */
8175 (void)SetActiveWindow(s_hwnd);
8176
8177 /* make sure the right buttons are enabled */
8178 force_menu_update = TRUE;
8179}
8180#endif
8181
8182#if defined(FEAT_TOOLBAR) || defined(PROTO)
8183#include "gui_w32_rc.h"
8184
8185/* This not defined in older SDKs */
8186# ifndef TBSTYLE_FLAT
8187# define TBSTYLE_FLAT 0x0800
8188# endif
8189
8190/*
8191 * Create the toolbar, initially unpopulated.
8192 * (just like the menu, there are no defaults, it's all
8193 * set up through menu.vim)
8194 */
8195 static void
8196initialise_toolbar(void)
8197{
8198 InitCommonControls();
8199 s_toolbarhwnd = CreateToolbarEx(
8200 s_hwnd,
8201 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8202 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008203 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 s_hinst,
8205 IDR_TOOLBAR1, // id of initial bitmap
8206 NULL,
8207 0, // initial number of buttons
8208 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8209 TOOLBAR_BUTTON_HEIGHT,
8210 TOOLBAR_BUTTON_WIDTH,
8211 TOOLBAR_BUTTON_HEIGHT,
8212 sizeof(TBBUTTON)
8213 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008214 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215
8216 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8217}
8218
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008219 static LRESULT CALLBACK
8220toolbar_wndproc(
8221 HWND hwnd,
8222 UINT uMsg,
8223 WPARAM wParam,
8224 LPARAM lParam)
8225{
8226 HandleMouseHide(uMsg, lParam);
8227 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8228}
8229
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 static int
8231get_toolbar_bitmap(vimmenu_T *menu)
8232{
8233 int i = -1;
8234
8235 /*
8236 * Check user bitmaps first, unless builtin is specified.
8237 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008238 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
8240 char_u fname[MAXPATHL];
8241 HANDLE hbitmap = NULL;
8242
8243 if (menu->iconfile != NULL)
8244 {
8245 gui_find_iconfile(menu->iconfile, fname, "bmp");
8246 hbitmap = LoadImage(
8247 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008248 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 IMAGE_BITMAP,
8250 TOOLBAR_BUTTON_WIDTH,
8251 TOOLBAR_BUTTON_HEIGHT,
8252 LR_LOADFROMFILE |
8253 LR_LOADMAP3DCOLORS
8254 );
8255 }
8256
8257 /*
8258 * If the LoadImage call failed, or the "icon=" file
8259 * didn't exist or wasn't specified, try the menu name
8260 */
8261 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008262 && (gui_find_bitmap(
8263#ifdef FEAT_MULTI_LANG
8264 menu->en_dname != NULL ? menu->en_dname :
8265#endif
8266 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 hbitmap = LoadImage(
8268 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008269 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 IMAGE_BITMAP,
8271 TOOLBAR_BUTTON_WIDTH,
8272 TOOLBAR_BUTTON_HEIGHT,
8273 LR_LOADFROMFILE |
8274 LR_LOADMAP3DCOLORS
8275 );
8276
8277 if (hbitmap != NULL)
8278 {
8279 TBADDBITMAP tbAddBitmap;
8280
8281 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008282 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8285 (WPARAM)1, (LPARAM)&tbAddBitmap);
8286 /* i will be set to -1 if it fails */
8287 }
8288 }
8289 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8290 i = menu->iconidx;
8291
8292 return i;
8293}
8294#endif
8295
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008296#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8297 static void
8298initialise_tabline(void)
8299{
8300 InitCommonControls();
8301
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008302 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008303 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008304 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8305 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008306 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008307
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008308 gui.tabline_height = TABLINE_HEIGHT;
8309
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008310# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008311 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008312# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008313}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008314
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008315/*
8316 * Get tabpage_T from POINT.
8317 */
8318 static tabpage_T *
8319GetTabFromPoint(
8320 HWND hWnd,
8321 POINT pt)
8322{
8323 tabpage_T *ptp = NULL;
8324
8325 if (gui_mch_showing_tabline())
8326 {
8327 TCHITTESTINFO htinfo;
8328 htinfo.pt = pt;
8329 /* ignore if a window under cusor is not tabcontrol. */
8330 if (s_tabhwnd == hWnd)
8331 {
8332 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8333 if (idx != -1)
8334 ptp = find_tabpage(idx + 1);
8335 }
8336 }
8337 return ptp;
8338}
8339
8340static POINT s_pt = {0, 0};
8341static HCURSOR s_hCursor = NULL;
8342
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008343 static LRESULT CALLBACK
8344tabline_wndproc(
8345 HWND hwnd,
8346 UINT uMsg,
8347 WPARAM wParam,
8348 LPARAM lParam)
8349{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008350 POINT pt;
8351 tabpage_T *tp;
8352 RECT rect;
8353 int nCenter;
8354 int idx0;
8355 int idx1;
8356
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008357 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008358
8359 switch (uMsg)
8360 {
8361 case WM_LBUTTONDOWN:
8362 {
8363 s_pt.x = GET_X_LPARAM(lParam);
8364 s_pt.y = GET_Y_LPARAM(lParam);
8365 SetCapture(hwnd);
8366 s_hCursor = GetCursor(); /* backup default cursor */
8367 break;
8368 }
8369 case WM_MOUSEMOVE:
8370 if (GetCapture() == hwnd
8371 && ((wParam & MK_LBUTTON)) != 0)
8372 {
8373 pt.x = GET_X_LPARAM(lParam);
8374 pt.y = s_pt.y;
8375 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8376 {
8377 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8378
8379 tp = GetTabFromPoint(hwnd, pt);
8380 if (tp != NULL)
8381 {
8382 idx0 = tabpage_index(curtab) - 1;
8383 idx1 = tabpage_index(tp) - 1;
8384
8385 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8386 nCenter = rect.left + (rect.right - rect.left) / 2;
8387
8388 /* Check if the mouse cursor goes over the center of
8389 * the next tab to prevent "flickering". */
8390 if ((idx0 < idx1) && (nCenter < pt.x))
8391 {
8392 tabpage_move(idx1 + 1);
8393 update_screen(0);
8394 }
8395 else if ((idx1 < idx0) && (pt.x < nCenter))
8396 {
8397 tabpage_move(idx1);
8398 update_screen(0);
8399 }
8400 }
8401 }
8402 }
8403 break;
8404 case WM_LBUTTONUP:
8405 {
8406 if (GetCapture() == hwnd)
8407 {
8408 SetCursor(s_hCursor);
8409 ReleaseCapture();
8410 }
8411 break;
8412 }
8413 default:
8414 break;
8415 }
8416
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008417 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8418}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008419#endif
8420
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8422/*
8423 * Make the GUI window come to the foreground.
8424 */
8425 void
8426gui_mch_set_foreground(void)
8427{
8428 if (IsIconic(s_hwnd))
8429 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8430 SetForegroundWindow(s_hwnd);
8431}
8432#endif
8433
8434#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8435 static void
8436dyn_imm_load(void)
8437{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008438 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 if (hLibImm == NULL)
8440 return;
8441
8442 pImmGetCompositionStringA
8443 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8444 pImmGetCompositionStringW
8445 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8446 pImmGetContext
8447 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8448 pImmAssociateContext
8449 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8450 pImmReleaseContext
8451 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8452 pImmGetOpenStatus
8453 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8454 pImmSetOpenStatus
8455 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8456 pImmGetCompositionFont
8457 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8458 pImmSetCompositionFont
8459 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8460 pImmSetCompositionWindow
8461 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8462 pImmGetConversionStatus
8463 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008464 pImmSetConversionStatus
8465 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466
8467 if ( pImmGetCompositionStringA == NULL
8468 || pImmGetCompositionStringW == NULL
8469 || pImmGetContext == NULL
8470 || pImmAssociateContext == NULL
8471 || pImmReleaseContext == NULL
8472 || pImmGetOpenStatus == NULL
8473 || pImmSetOpenStatus == NULL
8474 || pImmGetCompositionFont == NULL
8475 || pImmSetCompositionFont == NULL
8476 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008477 || pImmGetConversionStatus == NULL
8478 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
8480 FreeLibrary(hLibImm);
8481 hLibImm = NULL;
8482 pImmGetContext = NULL;
8483 return;
8484 }
8485
8486 return;
8487}
8488
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489#endif
8490
8491#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8492
8493# ifdef FEAT_XPM_W32
8494# define IMAGE_XPM 100
8495# endif
8496
8497typedef struct _signicon_t
8498{
8499 HANDLE hImage;
8500 UINT uType;
8501#ifdef FEAT_XPM_W32
8502 HANDLE hShape; /* Mask bitmap handle */
8503#endif
8504} signicon_t;
8505
8506 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008507gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 signicon_t *sign;
8510 int x, y, w, h;
8511
8512 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8513 return;
8514
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008515#if defined(FEAT_DIRECTX)
8516 if (IS_ENABLE_DIRECTX())
8517 DWriteContext_Flush(s_dwc);
8518#endif
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 x = TEXT_X(col);
8521 y = TEXT_Y(row);
8522 w = gui.char_width * 2;
8523 h = gui.char_height;
8524 switch (sign->uType)
8525 {
8526 case IMAGE_BITMAP:
8527 {
8528 HDC hdcMem;
8529 HBITMAP hbmpOld;
8530
8531 hdcMem = CreateCompatibleDC(s_hdc);
8532 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8533 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8534 SelectObject(hdcMem, hbmpOld);
8535 DeleteDC(hdcMem);
8536 }
8537 break;
8538 case IMAGE_ICON:
8539 case IMAGE_CURSOR:
8540 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8541 break;
8542#ifdef FEAT_XPM_W32
8543 case IMAGE_XPM:
8544 {
8545 HDC hdcMem;
8546 HBITMAP hbmpOld;
8547
8548 hdcMem = CreateCompatibleDC(s_hdc);
8549 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8550 /* Make hole */
8551 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8552
8553 SelectObject(hdcMem, sign->hImage);
8554 /* Paint sign */
8555 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8556 SelectObject(hdcMem, hbmpOld);
8557 DeleteDC(hdcMem);
8558 }
8559 break;
8560#endif
8561 }
8562}
8563
8564 static void
8565close_signicon_image(signicon_t *sign)
8566{
8567 if (sign)
8568 switch (sign->uType)
8569 {
8570 case IMAGE_BITMAP:
8571 DeleteObject((HGDIOBJ)sign->hImage);
8572 break;
8573 case IMAGE_CURSOR:
8574 DestroyCursor((HCURSOR)sign->hImage);
8575 break;
8576 case IMAGE_ICON:
8577 DestroyIcon((HICON)sign->hImage);
8578 break;
8579#ifdef FEAT_XPM_W32
8580 case IMAGE_XPM:
8581 DeleteObject((HBITMAP)sign->hImage);
8582 DeleteObject((HBITMAP)sign->hShape);
8583 break;
8584#endif
8585 }
8586}
8587
8588 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008589gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
8591 signicon_t sign, *psign;
8592 char_u *ext;
8593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008595 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 if (ext > signfile)
8597 {
8598 int do_load = 1;
8599
8600 if (!STRICMP(ext, ".bmp"))
8601 sign.uType = IMAGE_BITMAP;
8602 else if (!STRICMP(ext, ".ico"))
8603 sign.uType = IMAGE_ICON;
8604 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8605 sign.uType = IMAGE_CURSOR;
8606 else
8607 do_load = 0;
8608
8609 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008610 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 gui.char_width * 2, gui.char_height,
8612 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8613#ifdef FEAT_XPM_W32
8614 if (!STRICMP(ext, ".xpm"))
8615 {
8616 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008617 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8618 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 }
8620#endif
8621 }
8622
8623 psign = NULL;
8624 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8625 != NULL)
8626 *psign = sign;
8627
8628 if (!psign)
8629 {
8630 if (sign.hImage)
8631 close_signicon_image(&sign);
8632 EMSG(_(e_signdata));
8633 }
8634 return (void *)psign;
8635
8636}
8637
8638 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008639gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 if (sign)
8642 {
8643 close_signicon_image((signicon_t *)sign);
8644 vim_free(sign);
8645 }
8646}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008649#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
8651/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008652 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008654 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8656 * to get current mouse position).
8657 *
8658 * Trying to use as more Windows services as possible, and as less
8659 * IE version as possible :)).
8660 *
8661 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8662 * BalloonEval struct.
8663 * 2) Enable/Disable simply create/kill BalloonEval Timer
8664 * 3) When there was enough inactivity, timer procedure posts
8665 * async request to debugger
8666 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8667 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008668 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 */
8670
Bram Moolenaar45360022005-07-21 21:08:21 +00008671/*
8672 * determine whether installed Common Controls support multiline tooltips
8673 * (i.e. their version is >= 4.70
8674 */
8675 int
8676multiline_balloon_available(void)
8677{
8678 HINSTANCE hDll;
8679 static char comctl_dll[] = "comctl32.dll";
8680 static int multiline_tip = MAYBE;
8681
8682 if (multiline_tip != MAYBE)
8683 return multiline_tip;
8684
8685 hDll = GetModuleHandle(comctl_dll);
8686 if (hDll != NULL)
8687 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008688 DLLGETVERSIONPROC pGetVer;
8689 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008690
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008691 if (pGetVer != NULL)
8692 {
8693 DLLVERSIONINFO dvi;
8694 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008695
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008696 ZeroMemory(&dvi, sizeof(dvi));
8697 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008698
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008699 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008700
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008701 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008702 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008703 || (dvi.dwMajorVersion == 4
8704 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008705 {
8706 multiline_tip = TRUE;
8707 return multiline_tip;
8708 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008709 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008710 else
8711 {
8712 /* there is chance we have ancient CommCtl 4.70
8713 which doesn't export DllGetVersion */
8714 DWORD dwHandle = 0;
8715 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8716 if (len > 0)
8717 {
8718 VS_FIXEDFILEINFO *ver;
8719 UINT vlen = 0;
8720 void *data = alloc(len);
8721
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008722 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008723 && GetFileVersionInfo(comctl_dll, 0, len, data)
8724 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8725 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008726 && HIWORD(ver->dwFileVersionMS) > 4)
8727 || ((HIWORD(ver->dwFileVersionMS) == 4
8728 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008729 {
8730 vim_free(data);
8731 multiline_tip = TRUE;
8732 return multiline_tip;
8733 }
8734 vim_free(data);
8735 }
8736 }
8737 }
8738 multiline_tip = FALSE;
8739 return multiline_tip;
8740}
8741
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008743make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744{
Bram Moolenaar45360022005-07-21 21:08:21 +00008745 TOOLINFO *pti;
8746 int ToolInfoSize;
8747
8748 if (multiline_balloon_available() == TRUE)
8749 ToolInfoSize = sizeof(TOOLINFO_NEW);
8750 else
8751 ToolInfoSize = sizeof(TOOLINFO);
8752
8753 pti = (TOOLINFO *)alloc(ToolInfoSize);
8754 if (pti == NULL)
8755 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8758 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8759 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8760 beval->target, NULL, s_hinst, NULL);
8761
8762 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8763 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8764
Bram Moolenaar45360022005-07-21 21:08:21 +00008765 pti->cbSize = ToolInfoSize;
8766 pti->uFlags = TTF_SUBCLASS;
8767 pti->hwnd = beval->target;
8768 pti->hinst = 0; /* Don't use string resources */
8769 pti->uId = ID_BEVAL_TOOLTIP;
8770
8771 if (multiline_balloon_available() == TRUE)
8772 {
8773 RECT rect;
8774 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8775 pti->lpszText = LPSTR_TEXTCALLBACK;
8776 ptin->lParam = (LPARAM)text;
8777 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8778 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8779 (LPARAM)rect.right);
8780 }
8781 else
8782 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008785 pti->rect.left = pt.x - 3;
8786 pti->rect.top = pt.y - 3;
8787 pti->rect.right = pt.x + 3;
8788 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
Bram Moolenaar45360022005-07-21 21:08:21 +00008790 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 /* Make tooltip appear sooner */
8792 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008793 /* I've performed some tests and it seems the longest possible life time
8794 * of tooltip is 30 seconds */
8795 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 /*
8797 * HACK: force tooltip to appear, because it'll not appear until
8798 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008799 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008801 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008803 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008807delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008809 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810}
8811
8812 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008813BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008814 HWND hwnd UNUSED,
8815 UINT uMsg UNUSED,
8816 UINT_PTR idEvent UNUSED,
8817 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
8819 POINT pt;
8820 RECT rect;
8821
8822 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8823 return;
8824
8825 GetCursorPos(&pt);
8826 if (WindowFromPoint(pt) != s_textArea)
8827 return;
8828
8829 ScreenToClient(s_textArea, &pt);
8830 GetClientRect(s_textArea, &rect);
8831 if (!PtInRect(&rect, pt))
8832 return;
8833
8834 if (LastActivity > 0
8835 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8836 && (cur_beval->showState != ShS_PENDING
8837 || abs(cur_beval->x - pt.x) > 3
8838 || abs(cur_beval->y - pt.y) > 3))
8839 {
8840 /* Pointer resting in one place long enough, it's time to show
8841 * the tooltip. */
8842 cur_beval->showState = ShS_PENDING;
8843 cur_beval->x = pt.x;
8844 cur_beval->y = pt.y;
8845
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008846 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847
8848 if (cur_beval->msgCB != NULL)
8849 (*cur_beval->msgCB)(cur_beval, 0);
8850 }
8851}
8852
8853 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008854gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008856 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008858 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008862gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008864 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (beval == NULL)
8866 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008867 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008868 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008869 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
8872 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008873gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008876
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008877 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 if (beval->showState == ShS_SHOWING)
8879 return;
8880 GetCursorPos(&pt);
8881 ScreenToClient(s_textArea, &pt);
8882
8883 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008885 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 gui_mch_disable_beval_area(cur_beval);
8887 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008888 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008890 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891}
8892
8893 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008894gui_mch_create_beval_area(
8895 void *target, /* ignored, always use s_textArea */
8896 char_u *mesg,
8897 void (*mesgCB)(BalloonEval *, int),
8898 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 /* partially stolen from gui_beval.c */
8901 BalloonEval *beval;
8902
8903 if (mesg != NULL && mesgCB != NULL)
8904 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008905 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 return NULL;
8907 }
8908
8909 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8910 if (beval != NULL)
8911 {
8912 beval->target = s_textArea;
8913 beval->balloon = NULL;
8914
8915 beval->showState = ShS_NEUTRAL;
8916 beval->x = 0;
8917 beval->y = 0;
8918 beval->msg = mesg;
8919 beval->msgCB = mesgCB;
8920 beval->clientData = clientData;
8921
8922 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 cur_beval = beval;
8924
8925 if (p_beval)
8926 gui_mch_enable_beval_area(beval);
8927
8928 }
8929 return beval;
8930}
8931
8932 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008933Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8936 return;
8937
8938 if (cur_beval != NULL)
8939 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008940 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008942 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008943 // TRACE0("TTN_SHOW {{{");
8944 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008945 break;
8946 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008947 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 delete_tooltip(cur_beval);
8949 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008950 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008953 break;
8954 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008955 {
8956 /* if you get there then we have new common controls */
8957 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8958 info->lpszText = (LPSTR)info->lParam;
8959 info->uFlags |= TTF_DI_SETITEM;
8960 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 }
8963 }
8964}
8965
8966 static void
8967TrackUserActivity(UINT uMsg)
8968{
8969 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8970 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8971 LastActivity = GetTickCount();
8972}
8973
8974 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008975gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 vim_free(beval);
8978}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008979#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8982/*
8983 * We have multiple signs to draw at the same location. Draw the
8984 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8985 */
8986 void
8987netbeans_draw_multisign_indicator(int row)
8988{
8989 int i;
8990 int y;
8991 int x;
8992
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008993 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008994 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 x = 0;
8997 y = TEXT_Y(row);
8998
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008999#if defined(FEAT_DIRECTX)
9000 if (IS_ENABLE_DIRECTX())
9001 DWriteContext_Flush(s_dwc);
9002#endif
9003
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 for (i = 0; i < gui.char_height - 3; i++)
9005 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9006
9007 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9008 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9009 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9010 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9011 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9012 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9013 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9014}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009015#endif