blob: 074954a6ca6409e654779a7841776ffc1dc45bbb [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaar7f88b652017-12-14 13:15:19 +010033# ifndef FEAT_MBYTE
34# error FEAT_MBYTE is required for FEAT_DIRECTX.
35# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020036static DWriteContext *s_dwc = NULL;
37static int s_directx_enabled = 0;
38static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010039# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010040static int directx_enabled(void);
41static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020043
Bram Moolenaar065bbac2016-02-20 13:08:46 +010044#ifdef FEAT_MENU
45static int gui_mswin_get_menu_height(int fix_window);
46#endif
47
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020048#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
49 int
50gui_mch_set_rendering_options(char_u *s)
51{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010052# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053 char_u *p, *q;
54
55 int dx_enable = 0;
56 int dx_flags = 0;
57 float dx_gamma = 0.0f;
58 float dx_contrast = 0.0f;
59 float dx_level = 0.0f;
60 int dx_geom = 0;
61 int dx_renmode = 0;
62 int dx_taamode = 0;
63
64 /* parse string as rendering options. */
65 for (p = s; p != NULL && *p != NUL; )
66 {
67 char_u item[256];
68 char_u name[128];
69 char_u value[128];
70
Bram Moolenaarcde88542015-08-11 19:14:00 +020071 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020072 if (p == NULL)
73 break;
74 q = &item[0];
75 copy_option_part(&q, name, sizeof(name), ":");
76 if (q == NULL)
77 return FAIL;
78 copy_option_part(&q, value, sizeof(value), ":");
79
80 if (STRCMP(name, "type") == 0)
81 {
82 if (STRCMP(value, "directx") == 0)
83 dx_enable = 1;
84 else
85 return FAIL;
86 }
87 else if (STRCMP(name, "gamma") == 0)
88 {
89 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010090 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020091 }
92 else if (STRCMP(name, "contrast") == 0)
93 {
94 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "level") == 0)
98 {
99 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "geom") == 0)
103 {
104 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 if (dx_geom < 0 || dx_geom > 2)
107 return FAIL;
108 }
109 else if (STRCMP(name, "renmode") == 0)
110 {
111 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100112 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200113 if (dx_renmode < 0 || dx_renmode > 6)
114 return FAIL;
115 }
116 else if (STRCMP(name, "taamode") == 0)
117 {
118 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100119 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200120 if (dx_taamode < 0 || dx_taamode > 3)
121 return FAIL;
122 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 else if (STRCMP(name, "scrlines") == 0)
124 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100125 /* Deprecated. Simply ignore it. */
Bram Moolenaar92467d32017-12-05 13:22:16 +0100126 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200127 else
128 return FAIL;
129 }
130
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100131 if (!gui.in_use)
132 return OK; /* only checking the syntax of the value */
133
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 /* Enable DirectX/DirectWrite */
135 if (dx_enable)
136 {
137 if (!directx_enabled())
138 return FAIL;
139 DWriteContext_SetRenderingParams(s_dwc, NULL);
140 if (dx_flags)
141 {
142 DWriteRenderingParams param;
143 DWriteContext_GetRenderingParams(s_dwc, &param);
144 if (dx_flags & (1 << 0))
145 param.gamma = dx_gamma;
146 if (dx_flags & (1 << 1))
147 param.enhancedContrast = dx_contrast;
148 if (dx_flags & (1 << 2))
149 param.clearTypeLevel = dx_level;
150 if (dx_flags & (1 << 3))
151 param.pixelGeometry = dx_geom;
152 if (dx_flags & (1 << 4))
153 param.renderingMode = dx_renmode;
154 if (dx_flags & (1 << 5))
155 param.textAntialiasMode = dx_taamode;
156 DWriteContext_SetRenderingParams(s_dwc, &param);
157 }
158 }
159 s_directx_enabled = dx_enable;
160
161 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100162# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200163 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100164# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200165}
166#endif
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * These are new in Windows ME/XP, only defined in recent compilers.
170 */
171#ifndef HANDLE_WM_XBUTTONUP
172# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
173 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
174#endif
175#ifndef HANDLE_WM_XBUTTONDOWN
176# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDBLCLK
180# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100184
185#include "version.h" /* used by dialog box routine for default title */
186#ifdef DEBUG
187# include <tchar.h>
188#endif
189
190/* cproto fails on missing include files */
191#ifndef PROTO
192
193#ifndef __MINGW32__
194# include <shellapi.h>
195#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100196#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197# include <commctrl.h>
198#endif
199#include <windowsx.h>
200
201#ifdef GLOBAL_IME
202# include "glbl_ime.h"
203#endif
204
205#endif /* PROTO */
206
207#ifdef FEAT_MENU
208# define MENUHINTS /* show menu hints in command line */
209#endif
210
211/* Some parameters for dialog boxes. All in pixels. */
212#define DLG_PADDING_X 10
213#define DLG_PADDING_Y 10
214#define DLG_OLD_STYLE_PADDING_X 5
215#define DLG_OLD_STYLE_PADDING_Y 5
216#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
217#define DLG_VERT_PADDING_Y 4
218#define DLG_ICON_WIDTH 34
219#define DLG_ICON_HEIGHT 34
220#define DLG_MIN_WIDTH 150
221#define DLG_FONT_NAME "MS Sans Serif"
222#define DLG_FONT_POINT_SIZE 8
223#define DLG_MIN_MAX_WIDTH 400
224#define DLG_MIN_MAX_HEIGHT 400
225
226#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
227
228#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
229# define WM_XBUTTONDOWN 0x020B
230# define WM_XBUTTONUP 0x020C
231# define WM_XBUTTONDBLCLK 0x020D
232# define MK_XBUTTON1 0x0020
233# define MK_XBUTTON2 0x0040
234#endif
235
236#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238 * Define a few things for generating prototypes. This is just to avoid
239 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241# define APIENTRY
242# define CALLBACK
243# define CONST
244# define FAR
245# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
259typedef int LOGFONT[];
260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
299static WORD s_dlgfntheight; /* height of the dialog font */
300static WORD s_dlgfntwidth; /* width of the dialog font */
301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
307static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
308#endif
309
310/* Flag that is set while processing a message that must not be interrupted by
311 * processing another message. */
312static int s_busy_processing = FALSE;
313
314static int destroying = FALSE; /* call DestroyWindow() ourselves */
315
316#ifdef MSWIN_FIND_REPLACE
317static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
318static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200319# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100320static FINDREPLACEW s_findrep_struct_w;
321# endif
322static HWND s_findrep_hwnd = NULL;
323static int s_findrep_is_find; /* TRUE for find dialog, FALSE
324 for find/replace dialog */
325#endif
326
327static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100328#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329static
330#endif
331HWND s_hwnd = NULL;
332static HDC s_hdc = NULL;
333static HBRUSH s_brush = NULL;
334
335#ifdef FEAT_TOOLBAR
336static HWND s_toolbarhwnd = NULL;
337static WNDPROC s_toolbar_wndproc = NULL;
338#endif
339
340#ifdef FEAT_GUI_TABLINE
341static HWND s_tabhwnd = NULL;
342static WNDPROC s_tabline_wndproc = NULL;
343static int showing_tabline = 0;
344#endif
345
346static WPARAM s_wParam = 0;
347static LPARAM s_lParam = 0;
348
349static HWND s_textArea = NULL;
350static UINT s_uMsg = 0;
351
352static char_u *s_textfield; /* Used by dialogs to pass back strings */
353
354static int s_need_activate = FALSE;
355
356/* This variable is set when waiting for an event, which is the only moment
357 * scrollbar dragging can be done directly. It's not allowed while commands
358 * are executed, because it may move the cursor and that may cause unexpected
359 * problems (e.g., while ":s" is working).
360 */
361static int allow_scrollbar = FALSE;
362
363#ifdef GLOBAL_IME
364# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
365#else
366# define MyTranslateMessage(x) TranslateMessage(x)
367#endif
368
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100369#if defined(FEAT_DIRECTX)
370 static int
371directx_enabled(void)
372{
373 if (s_dwc != NULL)
374 return 1;
375 else if (s_directx_load_attempted)
376 return 0;
377 /* load DirectX */
378 DWrite_Init();
379 s_directx_load_attempted = 1;
380 s_dwc = DWriteContext_Open();
381 directx_binddc();
382 return s_dwc != NULL ? 1 : 0;
383}
384
385 static void
386directx_binddc(void)
387{
388 if (s_textArea != NULL)
389 {
390 RECT rect;
391 GetClientRect(s_textArea, &rect);
392 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
393 }
394}
395#endif
396
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200397#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100398 /* use of WindowProc depends on wide_WindowProc */
399# define MyWindowProc vim_WindowProc
400#else
401 /* use ordinary WindowProc */
402# define MyWindowProc DefWindowProc
403#endif
404
405extern int current_font_height; /* this is in os_mswin.c */
406
407static struct
408{
409 UINT key_sym;
410 char_u vim_code0;
411 char_u vim_code1;
412} special_keys[] =
413{
414 {VK_UP, 'k', 'u'},
415 {VK_DOWN, 'k', 'd'},
416 {VK_LEFT, 'k', 'l'},
417 {VK_RIGHT, 'k', 'r'},
418
419 {VK_F1, 'k', '1'},
420 {VK_F2, 'k', '2'},
421 {VK_F3, 'k', '3'},
422 {VK_F4, 'k', '4'},
423 {VK_F5, 'k', '5'},
424 {VK_F6, 'k', '6'},
425 {VK_F7, 'k', '7'},
426 {VK_F8, 'k', '8'},
427 {VK_F9, 'k', '9'},
428 {VK_F10, 'k', ';'},
429
430 {VK_F11, 'F', '1'},
431 {VK_F12, 'F', '2'},
432 {VK_F13, 'F', '3'},
433 {VK_F14, 'F', '4'},
434 {VK_F15, 'F', '5'},
435 {VK_F16, 'F', '6'},
436 {VK_F17, 'F', '7'},
437 {VK_F18, 'F', '8'},
438 {VK_F19, 'F', '9'},
439 {VK_F20, 'F', 'A'},
440
441 {VK_F21, 'F', 'B'},
442#ifdef FEAT_NETBEANS_INTG
443 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
444#endif
445 {VK_F22, 'F', 'C'},
446 {VK_F23, 'F', 'D'},
447 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
448
449 {VK_HELP, '%', '1'},
450 {VK_BACK, 'k', 'b'},
451 {VK_INSERT, 'k', 'I'},
452 {VK_DELETE, 'k', 'D'},
453 {VK_HOME, 'k', 'h'},
454 {VK_END, '@', '7'},
455 {VK_PRIOR, 'k', 'P'},
456 {VK_NEXT, 'k', 'N'},
457 {VK_PRINT, '%', '9'},
458 {VK_ADD, 'K', '6'},
459 {VK_SUBTRACT, 'K', '7'},
460 {VK_DIVIDE, 'K', '8'},
461 {VK_MULTIPLY, 'K', '9'},
462 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
463 {VK_DECIMAL, 'K', 'B'},
464
465 {VK_NUMPAD0, 'K', 'C'},
466 {VK_NUMPAD1, 'K', 'D'},
467 {VK_NUMPAD2, 'K', 'E'},
468 {VK_NUMPAD3, 'K', 'F'},
469 {VK_NUMPAD4, 'K', 'G'},
470 {VK_NUMPAD5, 'K', 'H'},
471 {VK_NUMPAD6, 'K', 'I'},
472 {VK_NUMPAD7, 'K', 'J'},
473 {VK_NUMPAD8, 'K', 'K'},
474 {VK_NUMPAD9, 'K', 'L'},
475
476 /* Keys that we want to be able to use any modifier with: */
477 {VK_SPACE, ' ', NUL},
478 {VK_TAB, TAB, NUL},
479 {VK_ESCAPE, ESC, NUL},
480 {NL, NL, NUL},
481 {CAR, CAR, NUL},
482
483 /* End of list marker: */
484 {0, 0, 0}
485};
486
487/* Local variables */
488static int s_button_pending = -1;
489
490/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
491 * so don't reset s_button_pending. */
492static int s_getting_focus = FALSE;
493
494static int s_x_pending;
495static int s_y_pending;
496static UINT s_kFlags_pending;
497static UINT s_wait_timer = 0; /* Timer for get char from user */
498static int s_timed_out = FALSE;
499static int dead_key = 0; /* 0: no dead key, 1: dead key pressed */
500
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100501#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100502/* balloon-eval WM_NOTIFY_HANDLER */
503static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
504static void TrackUserActivity(UINT uMsg);
505#endif
506
507/*
508 * For control IME.
509 *
510 * These LOGFONT used for IME.
511 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100512#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100513/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
514static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100515#endif
516#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100517/* holds LOGFONT for 'guifont' always. */
518static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100519#endif
520
521#ifdef FEAT_MBYTE_IME
522static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
523#endif
524
525#if defined(FEAT_BROWSE)
526static char_u *convert_filter(char_u *s);
527#endif
528
529#ifdef DEBUG_PRINT_ERROR
530/*
531 * Print out the last Windows error message
532 */
533 static void
534print_windows_error(void)
535{
536 LPVOID lpMsgBuf;
537
538 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
539 NULL, GetLastError(),
540 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
541 (LPTSTR) &lpMsgBuf, 0, NULL);
542 TRACE1("Error: %s\n", lpMsgBuf);
543 LocalFree(lpMsgBuf);
544}
545#endif
546
547/*
548 * Cursor blink functions.
549 *
550 * This is a simple state machine:
551 * BLINK_NONE not blinking at all
552 * BLINK_OFF blinking, cursor is not shown
553 * BLINK_ON blinking, cursor is shown
554 */
555
556#define BLINK_NONE 0
557#define BLINK_OFF 1
558#define BLINK_ON 2
559
560static int blink_state = BLINK_NONE;
561static long_u blink_waittime = 700;
562static long_u blink_ontime = 400;
563static long_u blink_offtime = 250;
564static UINT blink_timer = 0;
565
Bram Moolenaar703a8042016-06-04 16:24:32 +0200566 int
567gui_mch_is_blinking(void)
568{
569 return blink_state != BLINK_NONE;
570}
571
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200572 int
573gui_mch_is_blink_off(void)
574{
575 return blink_state == BLINK_OFF;
576}
577
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100578 void
579gui_mch_set_blinking(long wait, long on, long off)
580{
581 blink_waittime = wait;
582 blink_ontime = on;
583 blink_offtime = off;
584}
585
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100586 static VOID CALLBACK
587_OnBlinkTimer(
588 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100589 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100590 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100591 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100592{
593 MSG msg;
594
595 /*
596 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
597 */
598
599 KillTimer(NULL, idEvent);
600
601 /* Eat spurious WM_TIMER messages */
602 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
603 ;
604
605 if (blink_state == BLINK_ON)
606 {
607 gui_undraw_cursor();
608 blink_state = BLINK_OFF;
609 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
610 (TIMERPROC)_OnBlinkTimer);
611 }
612 else
613 {
614 gui_update_cursor(TRUE, FALSE);
615 blink_state = BLINK_ON;
616 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100617 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100618 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100619 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620}
621
622 static void
623gui_mswin_rm_blink_timer(void)
624{
625 MSG msg;
626
627 if (blink_timer != 0)
628 {
629 KillTimer(NULL, blink_timer);
630 /* Eat spurious WM_TIMER messages */
631 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
632 ;
633 blink_timer = 0;
634 }
635}
636
637/*
638 * Stop the cursor blinking. Show the cursor if it wasn't shown.
639 */
640 void
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
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003130 rc.left = FILL_X(gui.scroll_region_left);
3131 rc.right = FILL_X(gui.scroll_region_right + 1);
3132 rc.top = FILL_Y(row);
3133 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3134
Bram Moolenaar92467d32017-12-05 13:22:16 +01003135#if defined(FEAT_DIRECTX)
3136 if (IS_ENABLE_DIRECTX())
3137 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003138 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3139 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003140 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003141 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003142#endif
3143 {
3144 intel_gpu_workaround();
3145 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003146 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003147 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003148 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003150 /* This seems to be required to avoid the cursor disappearing when
3151 * scrolling such that the cursor ends up in the top-left character on
3152 * the screen... But why? (Webb) */
3153 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3154 /* gui.cursor_is_valid = FALSE; */
3155
3156 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3157 gui.scroll_region_left,
3158 gui.scroll_region_bot, gui.scroll_region_right);
3159}
3160
3161/*
3162 * Insert the given number of lines before the given row, scrolling down any
3163 * following text within the scroll region.
3164 */
3165 void
3166gui_mch_insert_lines(
3167 int row,
3168 int num_lines)
3169{
3170 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003171
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 rc.left = FILL_X(gui.scroll_region_left);
3173 rc.right = FILL_X(gui.scroll_region_right + 1);
3174 rc.top = FILL_Y(row);
3175 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003176
3177#if defined(FEAT_DIRECTX)
3178 if (IS_ENABLE_DIRECTX())
3179 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003180 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3181 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003182 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003183 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003184#endif
3185 {
3186 intel_gpu_workaround();
3187 /* The SW_INVALIDATE is required when part of the window is covered or
3188 * off-screen. How do we avoid it when it's not needed? */
3189 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003190 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003191 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003192 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 gui_clear_block(row, gui.scroll_region_left,
3195 row + num_lines - 1, gui.scroll_region_right);
3196}
3197
3198
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003200gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003201{
3202#if defined(FEAT_DIRECTX)
3203 DWriteContext_Close(s_dwc);
3204 DWrite_Final();
3205 s_dwc = NULL;
3206#endif
3207
3208 ReleaseDC(s_textArea, s_hdc);
3209 DeleteObject(s_brush);
3210
3211#ifdef FEAT_TEAROFF
3212 /* Unload the tearoff bitmap */
3213 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3214#endif
3215
3216 /* Destroy our window (if we have one). */
3217 if (s_hwnd != NULL)
3218 {
3219 destroying = TRUE; /* ignore WM_DESTROY message now */
3220 DestroyWindow(s_hwnd);
3221 }
3222
3223#ifdef GLOBAL_IME
3224 global_ime_end();
3225#endif
3226}
3227
3228 static char_u *
3229logfont2name(LOGFONT lf)
3230{
3231 char *p;
3232 char *res;
3233 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003234 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 char *font_name = lf.lfFaceName;
3236
3237 charset_name = charset_id2name((int)lf.lfCharSet);
3238#ifdef FEAT_MBYTE
3239 /* Convert a font name from the current codepage to 'encoding'.
3240 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3241 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3242 {
3243 int len;
3244 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3245 (char_u **)&font_name, &len);
3246 }
3247#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003248 quality_name = quality_id2name((int)lf.lfQuality);
3249
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003250 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3251 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3252 if (res != NULL)
3253 {
3254 p = res;
3255 /* make a normal font string out of the lf thing:*/
3256 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3257 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3258 while (*p)
3259 {
3260 if (*p == ' ')
3261 *p = '_';
3262 ++p;
3263 }
3264 if (lf.lfItalic)
3265 STRCAT(p, ":i");
3266 if (lf.lfWeight >= FW_BOLD)
3267 STRCAT(p, ":b");
3268 if (lf.lfUnderline)
3269 STRCAT(p, ":u");
3270 if (lf.lfStrikeOut)
3271 STRCAT(p, ":s");
3272 if (charset_name != NULL)
3273 {
3274 STRCAT(p, ":c");
3275 STRCAT(p, charset_name);
3276 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003277 if (quality_name != NULL)
3278 {
3279 STRCAT(p, ":q");
3280 STRCAT(p, quality_name);
3281 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003282 }
3283
3284#ifdef FEAT_MBYTE
3285 if (font_name != lf.lfFaceName)
3286 vim_free(font_name);
3287#endif
3288 return (char_u *)res;
3289}
3290
3291
3292#ifdef FEAT_MBYTE_IME
3293/*
3294 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3295 * 'guifont'
3296 */
3297 static void
3298update_im_font(void)
3299{
3300 LOGFONT lf_wide;
3301
3302 if (p_guifontwide != NULL && *p_guifontwide != NUL
3303 && gui.wide_font != NOFONT
3304 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3305 norm_logfont = lf_wide;
3306 else
3307 norm_logfont = sub_logfont;
3308 im_set_font(&norm_logfont);
3309}
3310#endif
3311
3312#ifdef FEAT_MBYTE
3313/*
3314 * Handler of gui.wide_font (p_guifontwide) changed notification.
3315 */
3316 void
3317gui_mch_wide_font_changed(void)
3318{
3319 LOGFONT lf;
3320
3321# ifdef FEAT_MBYTE_IME
3322 update_im_font();
3323# endif
3324
3325 gui_mch_free_font(gui.wide_ital_font);
3326 gui.wide_ital_font = NOFONT;
3327 gui_mch_free_font(gui.wide_bold_font);
3328 gui.wide_bold_font = NOFONT;
3329 gui_mch_free_font(gui.wide_boldital_font);
3330 gui.wide_boldital_font = NOFONT;
3331
3332 if (gui.wide_font
3333 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3334 {
3335 if (!lf.lfItalic)
3336 {
3337 lf.lfItalic = TRUE;
3338 gui.wide_ital_font = get_font_handle(&lf);
3339 lf.lfItalic = FALSE;
3340 }
3341 if (lf.lfWeight < FW_BOLD)
3342 {
3343 lf.lfWeight = FW_BOLD;
3344 gui.wide_bold_font = get_font_handle(&lf);
3345 if (!lf.lfItalic)
3346 {
3347 lf.lfItalic = TRUE;
3348 gui.wide_boldital_font = get_font_handle(&lf);
3349 }
3350 }
3351 }
3352}
3353#endif
3354
3355/*
3356 * Initialise vim to use the font with the given name.
3357 * Return FAIL if the font could not be loaded, OK otherwise.
3358 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003360gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003361{
3362 LOGFONT lf;
3363 GuiFont font = NOFONT;
3364 char_u *p;
3365
3366 /* Load the font */
3367 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3368 font = get_font_handle(&lf);
3369 if (font == NOFONT)
3370 return FAIL;
3371
3372 if (font_name == NULL)
3373 font_name = (char_u *)lf.lfFaceName;
3374#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3375 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003376#endif
3377#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378 sub_logfont = lf;
3379#endif
3380#ifdef FEAT_MBYTE_IME
3381 update_im_font();
3382#endif
3383 gui_mch_free_font(gui.norm_font);
3384 gui.norm_font = font;
3385 current_font_height = lf.lfHeight;
3386 GetFontSize(font);
3387
3388 p = logfont2name(lf);
3389 if (p != NULL)
3390 {
3391 hl_set_font_name(p);
3392
3393 /* When setting 'guifont' to "*" replace it with the actual font name.
3394 * */
3395 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3396 {
3397 vim_free(p_guifont);
3398 p_guifont = p;
3399 }
3400 else
3401 vim_free(p);
3402 }
3403
3404 gui_mch_free_font(gui.ital_font);
3405 gui.ital_font = NOFONT;
3406 gui_mch_free_font(gui.bold_font);
3407 gui.bold_font = NOFONT;
3408 gui_mch_free_font(gui.boldital_font);
3409 gui.boldital_font = NOFONT;
3410
3411 if (!lf.lfItalic)
3412 {
3413 lf.lfItalic = TRUE;
3414 gui.ital_font = get_font_handle(&lf);
3415 lf.lfItalic = FALSE;
3416 }
3417 if (lf.lfWeight < FW_BOLD)
3418 {
3419 lf.lfWeight = FW_BOLD;
3420 gui.bold_font = get_font_handle(&lf);
3421 if (!lf.lfItalic)
3422 {
3423 lf.lfItalic = TRUE;
3424 gui.boldital_font = get_font_handle(&lf);
3425 }
3426 }
3427
3428 return OK;
3429}
3430
3431#ifndef WPF_RESTORETOMAXIMIZED
3432# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3433#endif
3434
3435/*
3436 * Return TRUE if the GUI window is maximized, filling the whole screen.
3437 */
3438 int
3439gui_mch_maximized(void)
3440{
3441 WINDOWPLACEMENT wp;
3442
3443 wp.length = sizeof(WINDOWPLACEMENT);
3444 if (GetWindowPlacement(s_hwnd, &wp))
3445 return wp.showCmd == SW_SHOWMAXIMIZED
3446 || (wp.showCmd == SW_SHOWMINIMIZED
3447 && wp.flags == WPF_RESTORETOMAXIMIZED);
3448
3449 return 0;
3450}
3451
3452/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003453 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3454 * is set. Compute the new Rows and Columns. This is like resizing the
3455 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 */
3457 void
3458gui_mch_newfont(void)
3459{
3460 RECT rect;
3461
3462 GetWindowRect(s_hwnd, &rect);
3463 if (win_socket_id == 0)
3464 {
3465 gui_resize_shell(rect.right - rect.left
3466 - (GetSystemMetrics(SM_CXFRAME) +
3467 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3468 rect.bottom - rect.top
3469 - (GetSystemMetrics(SM_CYFRAME) +
3470 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3471 - GetSystemMetrics(SM_CYCAPTION)
3472#ifdef FEAT_MENU
3473 - gui_mswin_get_menu_height(FALSE)
3474#endif
3475 );
3476 }
3477 else
3478 {
3479 /* Inside another window, don't use the frame and border. */
3480 gui_resize_shell(rect.right - rect.left,
3481 rect.bottom - rect.top
3482#ifdef FEAT_MENU
3483 - gui_mswin_get_menu_height(FALSE)
3484#endif
3485 );
3486 }
3487}
3488
3489/*
3490 * Set the window title
3491 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 void
3493gui_mch_settitle(
3494 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003495 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496{
3497 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3498}
3499
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003500#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003501/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3502 * misc2.c! */
3503static LPCSTR mshape_idcs[] =
3504{
3505 IDC_ARROW, /* arrow */
3506 MAKEINTRESOURCE(0), /* blank */
3507 IDC_IBEAM, /* beam */
3508 IDC_SIZENS, /* updown */
3509 IDC_SIZENS, /* udsizing */
3510 IDC_SIZEWE, /* leftright */
3511 IDC_SIZEWE, /* lrsizing */
3512 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003513 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003514 IDC_ARROW, /* crosshair */
3515 IDC_ARROW, /* hand1 */
3516 IDC_ARROW, /* hand2 */
3517 IDC_ARROW, /* pencil */
3518 IDC_ARROW, /* question */
3519 IDC_ARROW, /* right-arrow */
3520 IDC_UPARROW, /* up-arrow */
3521 IDC_ARROW /* last one */
3522};
3523
3524 void
3525mch_set_mouse_shape(int shape)
3526{
3527 LPCSTR idc;
3528
3529 if (shape == MSHAPE_HIDE)
3530 ShowCursor(FALSE);
3531 else
3532 {
3533 if (shape >= MSHAPE_NUMBERED)
3534 idc = IDC_ARROW;
3535 else
3536 idc = mshape_idcs[shape];
3537#ifdef SetClassLongPtr
3538 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3539#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003541#endif
3542 if (!p_mh)
3543 {
3544 POINT mp;
3545
3546 /* Set the position to make it redrawn with the new shape. */
3547 (void)GetCursorPos((LPPOINT)&mp);
3548 (void)SetCursorPos(mp.x, mp.y);
3549 ShowCursor(TRUE);
3550 }
3551 }
3552}
3553#endif
3554
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003555#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003556/*
3557 * The file browser exists in two versions: with "W" uses wide characters,
3558 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3559 * Windows NT/2000/XP the "W" functions are used.
3560 */
3561
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003562# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003563/*
3564 * Wide version of convert_filter().
3565 */
3566 static WCHAR *
3567convert_filterW(char_u *s)
3568{
3569 char_u *tmp;
3570 int len;
3571 WCHAR *res;
3572
3573 tmp = convert_filter(s);
3574 if (tmp == NULL)
3575 return NULL;
3576 len = (int)STRLEN(s) + 3;
3577 res = enc_to_utf16(tmp, &len);
3578 vim_free(tmp);
3579 return res;
3580}
3581
3582/*
3583 * Wide version of gui_mch_browse(). Keep in sync!
3584 */
3585 static char_u *
3586gui_mch_browseW(
3587 int saving,
3588 char_u *title,
3589 char_u *dflt,
3590 char_u *ext,
3591 char_u *initdir,
3592 char_u *filter)
3593{
3594 /* We always use the wide function. This means enc_to_utf16() must work,
3595 * otherwise it fails miserably! */
3596 OPENFILENAMEW fileStruct;
3597 WCHAR fileBuf[MAXPATHL];
3598 WCHAR *wp;
3599 int i;
3600 WCHAR *titlep = NULL;
3601 WCHAR *extp = NULL;
3602 WCHAR *initdirp = NULL;
3603 WCHAR *filterp;
3604 char_u *p;
3605
3606 if (dflt == NULL)
3607 fileBuf[0] = NUL;
3608 else
3609 {
3610 wp = enc_to_utf16(dflt, NULL);
3611 if (wp == NULL)
3612 fileBuf[0] = NUL;
3613 else
3614 {
3615 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3616 fileBuf[i] = wp[i];
3617 fileBuf[i] = NUL;
3618 vim_free(wp);
3619 }
3620 }
3621
3622 /* Convert the filter to Windows format. */
3623 filterp = convert_filterW(filter);
3624
3625 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003626# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003628 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003629# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003631# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632
3633 if (title != NULL)
3634 titlep = enc_to_utf16(title, NULL);
3635 fileStruct.lpstrTitle = titlep;
3636
3637 if (ext != NULL)
3638 extp = enc_to_utf16(ext, NULL);
3639 fileStruct.lpstrDefExt = extp;
3640
3641 fileStruct.lpstrFile = fileBuf;
3642 fileStruct.nMaxFile = MAXPATHL;
3643 fileStruct.lpstrFilter = filterp;
3644 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3645 /* has an initial dir been specified? */
3646 if (initdir != NULL && *initdir != NUL)
3647 {
3648 /* Must have backslashes here, no matter what 'shellslash' says */
3649 initdirp = enc_to_utf16(initdir, NULL);
3650 if (initdirp != NULL)
3651 {
3652 for (wp = initdirp; *wp != NUL; ++wp)
3653 if (*wp == '/')
3654 *wp = '\\';
3655 }
3656 fileStruct.lpstrInitialDir = initdirp;
3657 }
3658
3659 /*
3660 * TODO: Allow selection of multiple files. Needs another arg to this
3661 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3662 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3663 * files that don't exist yet, so I haven't put it in. What about
3664 * OFN_PATHMUSTEXIST?
3665 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3666 */
3667 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003668# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003669 if (curbuf->b_p_bin)
3670 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003671# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003672 if (saving)
3673 {
3674 if (!GetSaveFileNameW(&fileStruct))
3675 return NULL;
3676 }
3677 else
3678 {
3679 if (!GetOpenFileNameW(&fileStruct))
3680 return NULL;
3681 }
3682
3683 vim_free(filterp);
3684 vim_free(initdirp);
3685 vim_free(titlep);
3686 vim_free(extp);
3687
3688 /* Convert from UCS2 to 'encoding'. */
3689 p = utf16_to_enc(fileBuf, NULL);
3690 if (p != NULL)
3691 /* when out of memory we get garbage for non-ASCII chars */
3692 STRCPY(fileBuf, p);
3693 vim_free(p);
3694
3695 /* Give focus back to main window (when using MDI). */
3696 SetFocus(s_hwnd);
3697
3698 /* Shorten the file name if possible */
3699 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3700}
3701# endif /* FEAT_MBYTE */
3702
3703
3704/*
3705 * Convert the string s to the proper format for a filter string by replacing
3706 * the \t and \n delimiters with \0.
3707 * Returns the converted string in allocated memory.
3708 *
3709 * Keep in sync with convert_filterW() above!
3710 */
3711 static char_u *
3712convert_filter(char_u *s)
3713{
3714 char_u *res;
3715 unsigned s_len = (unsigned)STRLEN(s);
3716 unsigned i;
3717
3718 res = alloc(s_len + 3);
3719 if (res != NULL)
3720 {
3721 for (i = 0; i < s_len; ++i)
3722 if (s[i] == '\t' || s[i] == '\n')
3723 res[i] = '\0';
3724 else
3725 res[i] = s[i];
3726 res[s_len] = NUL;
3727 /* Add two extra NULs to make sure it's properly terminated. */
3728 res[s_len + 1] = NUL;
3729 res[s_len + 2] = NUL;
3730 }
3731 return res;
3732}
3733
3734/*
3735 * Select a directory.
3736 */
3737 char_u *
3738gui_mch_browsedir(char_u *title, char_u *initdir)
3739{
3740 /* We fake this: Use a filter that doesn't select anything and a default
3741 * file name that won't be used. */
3742 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3743 initdir, (char_u *)_("Directory\t*.nothing\n"));
3744}
3745
3746/*
3747 * Pop open a file browser and return the file selected, in allocated memory,
3748 * or NULL if Cancel is hit.
3749 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3750 * title - Title message for the file browser dialog.
3751 * dflt - Default name of file.
3752 * ext - Default extension to be added to files without extensions.
3753 * initdir - directory in which to open the browser (NULL = current dir)
3754 * filter - Filter for matched files to choose from.
3755 *
3756 * Keep in sync with gui_mch_browseW() above!
3757 */
3758 char_u *
3759gui_mch_browse(
3760 int saving,
3761 char_u *title,
3762 char_u *dflt,
3763 char_u *ext,
3764 char_u *initdir,
3765 char_u *filter)
3766{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003767# ifdef FEAT_MBYTE
3768 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3769# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 OPENFILENAME fileStruct;
3771 char_u fileBuf[MAXPATHL];
3772 char_u *initdirp = NULL;
3773 char_u *filterp;
3774 char_u *p;
3775
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003776 if (dflt == NULL)
3777 fileBuf[0] = NUL;
3778 else
3779 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3780
3781 /* Convert the filter to Windows format. */
3782 filterp = convert_filter(filter);
3783
3784 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003785# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 /* be compatible with Windows NT 4.0 */
3787 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003788# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003789 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003790# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791
3792 fileStruct.lpstrTitle = (LPSTR)title;
3793 fileStruct.lpstrDefExt = (LPSTR)ext;
3794
3795 fileStruct.lpstrFile = (LPSTR)fileBuf;
3796 fileStruct.nMaxFile = MAXPATHL;
3797 fileStruct.lpstrFilter = (LPSTR)filterp;
3798 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3799 /* has an initial dir been specified? */
3800 if (initdir != NULL && *initdir != NUL)
3801 {
3802 /* Must have backslashes here, no matter what 'shellslash' says */
3803 initdirp = vim_strsave(initdir);
3804 if (initdirp != NULL)
3805 for (p = initdirp; *p != NUL; ++p)
3806 if (*p == '/')
3807 *p = '\\';
3808 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3809 }
3810
3811 /*
3812 * TODO: Allow selection of multiple files. Needs another arg to this
3813 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3814 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3815 * files that don't exist yet, so I haven't put it in. What about
3816 * OFN_PATHMUSTEXIST?
3817 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3818 */
3819 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003820# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 if (curbuf->b_p_bin)
3822 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003823# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003824 if (saving)
3825 {
3826 if (!GetSaveFileName(&fileStruct))
3827 return NULL;
3828 }
3829 else
3830 {
3831 if (!GetOpenFileName(&fileStruct))
3832 return NULL;
3833 }
3834
3835 vim_free(filterp);
3836 vim_free(initdirp);
3837
3838 /* Give focus back to main window (when using MDI). */
3839 SetFocus(s_hwnd);
3840
3841 /* Shorten the file name if possible */
3842 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003843# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003844}
3845#endif /* FEAT_BROWSE */
3846
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 static void
3848_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003849 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 HDROP hDrop)
3851{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003852#define BUFPATHLEN _MAX_PATH
3853#define DRAGQVAL 0xFFFFFFFF
3854#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003856#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003857 char szFile[BUFPATHLEN];
3858 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3859 UINT i;
3860 char_u **fnames;
3861 POINT pt;
3862 int_u modifiers = 0;
3863
3864 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3865
3866 /* Obtain dropped position */
3867 DragQueryPoint(hDrop, &pt);
3868 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3869
3870 reset_VIsual();
3871
3872 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3873
3874 if (fnames != NULL)
3875 for (i = 0; i < cFiles; ++i)
3876 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003877#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3879 fnames[i] = utf16_to_enc(wszFile, NULL);
3880 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003881#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 {
3883 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3884 fnames[i] = vim_strsave((char_u *)szFile);
3885 }
3886 }
3887
3888 DragFinish(hDrop);
3889
3890 if (fnames != NULL)
3891 {
3892 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3893 modifiers |= MOUSE_SHIFT;
3894 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3895 modifiers |= MOUSE_CTRL;
3896 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3897 modifiers |= MOUSE_ALT;
3898
3899 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3900
3901 s_need_activate = TRUE;
3902 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003903}
3904
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 static int
3906_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003907 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 HWND hwndCtl,
3909 UINT code,
3910 int pos)
3911{
3912 static UINT prev_code = 0; /* code of previous call */
3913 scrollbar_T *sb, *sb_info;
3914 long val;
3915 int dragging = FALSE;
3916 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 SCROLLINFO si;
3918
3919 si.cbSize = sizeof(si);
3920 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921
3922 sb = gui_mswin_find_scrollbar(hwndCtl);
3923 if (sb == NULL)
3924 return 0;
3925
3926 if (sb->wp != NULL) /* Left or right scrollbar */
3927 {
3928 /*
3929 * Careful: need to get scrollbar info out of first (left) scrollbar
3930 * for window, but keep real scrollbar too because we must pass it to
3931 * gui_drag_scrollbar().
3932 */
3933 sb_info = &sb->wp->w_scrollbars[0];
3934 }
3935 else /* Bottom scrollbar */
3936 sb_info = sb;
3937 val = sb_info->value;
3938
3939 switch (code)
3940 {
3941 case SB_THUMBTRACK:
3942 val = pos;
3943 dragging = TRUE;
3944 if (sb->scroll_shift > 0)
3945 val <<= sb->scroll_shift;
3946 break;
3947 case SB_LINEDOWN:
3948 val++;
3949 break;
3950 case SB_LINEUP:
3951 val--;
3952 break;
3953 case SB_PAGEDOWN:
3954 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3955 break;
3956 case SB_PAGEUP:
3957 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3958 break;
3959 case SB_TOP:
3960 val = 0;
3961 break;
3962 case SB_BOTTOM:
3963 val = sb_info->max;
3964 break;
3965 case SB_ENDSCROLL:
3966 if (prev_code == SB_THUMBTRACK)
3967 {
3968 /*
3969 * "pos" only gives us 16-bit data. In case of large file,
3970 * use GetScrollPos() which returns 32-bit. Unfortunately it
3971 * is not valid while the scrollbar is being dragged.
3972 */
3973 val = GetScrollPos(hwndCtl, SB_CTL);
3974 if (sb->scroll_shift > 0)
3975 val <<= sb->scroll_shift;
3976 }
3977 break;
3978
3979 default:
3980 /* TRACE("Unknown scrollbar event %d\n", code); */
3981 return 0;
3982 }
3983 prev_code = code;
3984
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003985 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3986 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003987
3988 /*
3989 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3990 */
3991 if (sb->wp != NULL)
3992 {
3993 scrollbar_T *sba = sb->wp->w_scrollbars;
3994 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3995
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003996 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003997 }
3998
3999 /* Don't let us be interrupted here by another message. */
4000 s_busy_processing = TRUE;
4001
4002 /* When "allow_scrollbar" is FALSE still need to remember the new
4003 * position, but don't actually scroll by setting "dont_scroll". */
4004 dont_scroll = !allow_scrollbar;
4005
Bram Moolenaara338adc2018-01-31 20:51:47 +01004006 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004008 mch_enable_flush();
4009 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010
4011 s_busy_processing = FALSE;
4012 dont_scroll = dont_scroll_save;
4013
4014 return 0;
4015}
4016
4017
4018/*
4019 * Get command line arguments.
4020 * Use "prog" as the name of the program and "cmdline" as the arguments.
4021 * Copy the arguments to allocated memory.
4022 * Return the number of arguments (including program name).
4023 * Return pointers to the arguments in "argvp". Memory is allocated with
4024 * malloc(), use free() instead of vim_free().
4025 * Return pointer to buffer in "tofree".
4026 * Returns zero when out of memory.
4027 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004028 int
4029get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4030{
4031 int i;
4032 char *p;
4033 char *progp;
4034 char *pnew = NULL;
4035 char *newcmdline;
4036 int inquote;
4037 int argc;
4038 char **argv = NULL;
4039 int round;
4040
4041 *tofree = NULL;
4042
4043#ifdef FEAT_MBYTE
4044 /* Try using the Unicode version first, it takes care of conversion when
4045 * 'encoding' is changed. */
4046 argc = get_cmd_argsW(&argv);
4047 if (argc != 0)
4048 goto done;
4049#endif
4050
4051 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4052 * non-space. */
4053 p = strrchr(prog, '.');
4054 if (p != NULL)
4055 *p = NUL;
4056 for (progp = prog; *progp == ' '; ++progp)
4057 ;
4058
4059 /* The command line is copied to allocated memory, so that we can change
4060 * it. Add the size of the string, the separating NUL and a terminating
4061 * NUL. */
4062 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4063 if (newcmdline == NULL)
4064 return 0;
4065
4066 /*
4067 * First round: count the number of arguments ("pnew" == NULL).
4068 * Second round: produce the arguments.
4069 */
4070 for (round = 1; round <= 2; ++round)
4071 {
4072 /* First argument is the program name. */
4073 if (pnew != NULL)
4074 {
4075 argv[0] = pnew;
4076 strcpy(pnew, progp);
4077 pnew += strlen(pnew);
4078 *pnew++ = NUL;
4079 }
4080
4081 /*
4082 * Isolate each argument and put it in argv[].
4083 */
4084 p = cmdline;
4085 argc = 1;
4086 while (*p != NUL)
4087 {
4088 inquote = FALSE;
4089 if (pnew != NULL)
4090 argv[argc] = pnew;
4091 ++argc;
4092 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4093 {
4094 /* Backslashes are only special when followed by a double
4095 * quote. */
4096 i = (int)strspn(p, "\\");
4097 if (p[i] == '"')
4098 {
4099 /* Halve the number of backslashes. */
4100 if (i > 1 && pnew != NULL)
4101 {
4102 vim_memset(pnew, '\\', i / 2);
4103 pnew += i / 2;
4104 }
4105
4106 /* Even nr of backslashes toggles quoting, uneven copies
4107 * the double quote. */
4108 if ((i & 1) == 0)
4109 inquote = !inquote;
4110 else if (pnew != NULL)
4111 *pnew++ = '"';
4112 p += i + 1;
4113 }
4114 else if (i > 0)
4115 {
4116 /* Copy span of backslashes unmodified. */
4117 if (pnew != NULL)
4118 {
4119 vim_memset(pnew, '\\', i);
4120 pnew += i;
4121 }
4122 p += i;
4123 }
4124 else
4125 {
4126 if (pnew != NULL)
4127 *pnew++ = *p;
4128#ifdef FEAT_MBYTE
4129 /* Can't use mb_* functions, because 'encoding' is not
4130 * initialized yet here. */
4131 if (IsDBCSLeadByte(*p))
4132 {
4133 ++p;
4134 if (pnew != NULL)
4135 *pnew++ = *p;
4136 }
4137#endif
4138 ++p;
4139 }
4140 }
4141
4142 if (pnew != NULL)
4143 *pnew++ = NUL;
4144 while (*p == ' ' || *p == '\t')
4145 ++p; /* advance until a non-space */
4146 }
4147
4148 if (round == 1)
4149 {
4150 argv = (char **)malloc((argc + 1) * sizeof(char *));
4151 if (argv == NULL )
4152 {
4153 free(newcmdline);
4154 return 0; /* malloc error */
4155 }
4156 pnew = newcmdline;
4157 *tofree = newcmdline;
4158 }
4159 }
4160
4161#ifdef FEAT_MBYTE
4162done:
4163#endif
4164 argv[argc] = NULL; /* NULL-terminated list */
4165 *argvp = argv;
4166 return argc;
4167}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169#ifdef FEAT_XPM_W32
4170# include "xpm_w32.h"
4171#endif
4172
4173#ifdef PROTO
4174# define WINAPI
4175#endif
4176
4177#ifdef __MINGW32__
4178/*
4179 * Add a lot of missing defines.
4180 * They are not always missing, we need the #ifndef's.
4181 */
4182# ifndef _cdecl
4183# define _cdecl
4184# endif
4185# ifndef IsMinimized
4186# define IsMinimized(hwnd) IsIconic(hwnd)
4187# endif
4188# ifndef IsMaximized
4189# define IsMaximized(hwnd) IsZoomed(hwnd)
4190# endif
4191# ifndef SelectFont
4192# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4193# endif
4194# ifndef GetStockBrush
4195# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4196# endif
4197# ifndef DeleteBrush
4198# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4199# endif
4200
4201# ifndef HANDLE_WM_RBUTTONDBLCLK
4202# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4203 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4204# endif
4205# ifndef HANDLE_WM_MBUTTONUP
4206# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4207 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4208# endif
4209# ifndef HANDLE_WM_MBUTTONDBLCLK
4210# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4211 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4212# endif
4213# ifndef HANDLE_WM_LBUTTONDBLCLK
4214# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4215 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4216# endif
4217# ifndef HANDLE_WM_RBUTTONDOWN
4218# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4219 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4220# endif
4221# ifndef HANDLE_WM_MOUSEMOVE
4222# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4223 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4224# endif
4225# ifndef HANDLE_WM_RBUTTONUP
4226# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4227 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4228# endif
4229# ifndef HANDLE_WM_MBUTTONDOWN
4230# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4231 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4232# endif
4233# ifndef HANDLE_WM_LBUTTONUP
4234# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4235 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4236# endif
4237# ifndef HANDLE_WM_LBUTTONDOWN
4238# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4239 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4240# endif
4241# ifndef HANDLE_WM_SYSCHAR
4242# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4243 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4244# endif
4245# ifndef HANDLE_WM_ACTIVATEAPP
4246# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4247 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4248# endif
4249# ifndef HANDLE_WM_WINDOWPOSCHANGING
4250# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4251 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4252# endif
4253# ifndef HANDLE_WM_VSCROLL
4254# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4255 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4256# endif
4257# ifndef HANDLE_WM_SETFOCUS
4258# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4259 ((fn)((hwnd), (HWND)(wParam)), 0L)
4260# endif
4261# ifndef HANDLE_WM_KILLFOCUS
4262# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4263 ((fn)((hwnd), (HWND)(wParam)), 0L)
4264# endif
4265# ifndef HANDLE_WM_HSCROLL
4266# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4267 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4268# endif
4269# ifndef HANDLE_WM_DROPFILES
4270# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4271 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4272# endif
4273# ifndef HANDLE_WM_CHAR
4274# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4275 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4276# endif
4277# ifndef HANDLE_WM_SYSDEADCHAR
4278# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4279 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4280# endif
4281# ifndef HANDLE_WM_DEADCHAR
4282# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4283 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4284# endif
4285#endif /* __MINGW32__ */
4286
4287
4288/* Some parameters for tearoff menus. All in pixels. */
4289#define TEAROFF_PADDING_X 2
4290#define TEAROFF_BUTTON_PAD_X 8
4291#define TEAROFF_MIN_WIDTH 200
4292#define TEAROFF_SUBMENU_LABEL ">>"
4293#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4294
4295
4296/* For the Intellimouse: */
4297#ifndef WM_MOUSEWHEEL
4298#define WM_MOUSEWHEEL 0x20a
4299#endif
4300
4301
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004302#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303# define ID_BEVAL_TOOLTIP 200
4304# define BEVAL_TEXT_LEN MAXPATHL
4305
Bram Moolenaar167632f2010-05-26 21:42:54 +02004306#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004307/* Work around old versions of basetsd.h which wrongly declares
4308 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004309# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004310# define UINT_PTR UINT
4311#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004312
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004313static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4314static void delete_tooltip(BalloonEval *beval);
4315static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004318static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004320
Bram Moolenaar82881492012-11-20 16:53:39 +01004321
4322/* cproto fails on missing include files */
4323#ifndef PROTO
4324
Bram Moolenaar45360022005-07-21 21:08:21 +00004325/*
4326 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004327 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004328 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004329# include <pshpack1.h>
4330
4331#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004332
4333typedef struct _DllVersionInfo
4334{
4335 DWORD cbSize;
4336 DWORD dwMajorVersion;
4337 DWORD dwMinorVersion;
4338 DWORD dwBuildNumber;
4339 DWORD dwPlatformID;
4340} DLLVERSIONINFO;
4341
Bram Moolenaar82881492012-11-20 16:53:39 +01004342#ifndef PROTO
4343# include <poppack.h>
4344#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004345
Bram Moolenaar45360022005-07-21 21:08:21 +00004346typedef struct tagTOOLINFOA_NEW
4347{
4348 UINT cbSize;
4349 UINT uFlags;
4350 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004351 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004352 RECT rect;
4353 HINSTANCE hinst;
4354 LPSTR lpszText;
4355 LPARAM lParam;
4356} TOOLINFO_NEW;
4357
4358typedef struct tagNMTTDISPINFO_NEW
4359{
4360 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004361 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004362 char szText[80];
4363 HINSTANCE hinst;
4364 UINT uFlags;
4365 LPARAM lParam;
4366} NMTTDISPINFO_NEW;
4367
Bram Moolenaar45360022005-07-21 21:08:21 +00004368typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4369#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004370# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#endif
4372
Bram Moolenaar45360022005-07-21 21:08:21 +00004373#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004374# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004375#endif
4376
4377#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004378# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004379#endif
4380
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004381#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004382
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004383#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4384/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4385 * it here if LPNMTTDISPINFO isn't defined.
4386 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4387 * _MSC_VER. */
4388# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4389typedef struct tagNMTTDISPINFOA {
4390 NMHDR hdr;
4391 LPSTR lpszText;
4392 char szText[80];
4393 HINSTANCE hinst;
4394 UINT uFlags;
4395 LPARAM lParam;
4396} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4397# define LPNMTTDISPINFO LPNMTTDISPINFOA
4398
4399# ifdef FEAT_MBYTE
4400typedef struct tagNMTTDISPINFOW {
4401 NMHDR hdr;
4402 LPWSTR lpszText;
4403 WCHAR szText[80];
4404 HINSTANCE hinst;
4405 UINT uFlags;
4406 LPARAM lParam;
4407} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4408# endif
4409# endif
4410#endif
4411
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004412#ifndef TTN_GETDISPINFOW
4413# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4414#endif
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416/* Local variables: */
4417
4418#ifdef FEAT_MENU
4419static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422/*
4423 * Use the system font for dialogs and tear-off menus. Remove this line to
4424 * use DLG_FONT_NAME.
4425 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004426#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427
4428#define VIM_NAME "vim"
4429#define VIM_CLASS "Vim"
4430#define VIM_CLASSW L"Vim"
4431
4432/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4433 * thus there should be room for every dialog. For tearoffs it's made bigger
4434 * when needed. */
4435#define DLG_ALLOC_SIZE 16 * 1024
4436
4437/*
4438 * stuff for dialogs, menus, tearoffs etc.
4439 */
4440static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004441#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444static PWORD
4445add_dialog_element(
4446 PWORD p,
4447 DWORD lStyle,
4448 WORD x,
4449 WORD y,
4450 WORD w,
4451 WORD h,
4452 WORD Id,
4453 WORD clss,
4454 const char *caption);
4455static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004456static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004457#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460static void get_dialog_font_metrics(void);
4461
4462static int dialog_default_button = -1;
4463
4464/* Intellimouse support */
4465static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4468#ifdef FEAT_TOOLBAR
4469static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004470static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471static int get_toolbar_bitmap(vimmenu_T *menu);
4472#endif
4473
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004474#ifdef FEAT_GUI_TABLINE
4475static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004476static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004477#endif
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_MBYTE_IME
4480static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4481static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4482#endif
4483#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4484# ifdef NOIME
4485typedef struct tagCOMPOSITIONFORM {
4486 DWORD dwStyle;
4487 POINT ptCurrentPos;
4488 RECT rcArea;
4489} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4490typedef HANDLE HIMC;
4491# endif
4492
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004493static HINSTANCE hLibImm = NULL;
4494static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4495static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4496static HIMC (WINAPI *pImmGetContext)(HWND);
4497static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4498static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4499static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4500static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4501static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4502static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4503static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4504static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004505static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506static void dyn_imm_load(void);
4507#else
4508# define pImmGetCompositionStringA ImmGetCompositionStringA
4509# define pImmGetCompositionStringW ImmGetCompositionStringW
4510# define pImmGetContext ImmGetContext
4511# define pImmAssociateContext ImmAssociateContext
4512# define pImmReleaseContext ImmReleaseContext
4513# define pImmGetOpenStatus ImmGetOpenStatus
4514# define pImmSetOpenStatus ImmSetOpenStatus
4515# define pImmGetCompositionFont ImmGetCompositionFontA
4516# define pImmSetCompositionFont ImmSetCompositionFontA
4517# define pImmSetCompositionWindow ImmSetCompositionWindow
4518# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004519# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#endif
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522#ifdef FEAT_MENU
4523/*
4524 * Figure out how high the menu bar is at the moment.
4525 */
4526 static int
4527gui_mswin_get_menu_height(
4528 int fix_window) /* If TRUE, resize window if menu height changed */
4529{
4530 static int old_menu_height = -1;
4531
4532 RECT rc1, rc2;
4533 int num;
4534 int menu_height;
4535
4536 if (gui.menu_is_active)
4537 num = GetMenuItemCount(s_menuBar);
4538 else
4539 num = 0;
4540
4541 if (num == 0)
4542 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004543 else if (IsMinimized(s_hwnd))
4544 {
4545 /* The height of the menu cannot be determined while the window is
4546 * minimized. Take the previous height if the menu is changed in that
4547 * state, to avoid that Vim's vertical window size accidentally
4548 * increases due to the unaccounted-for menu height. */
4549 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 else
4552 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004553 /*
4554 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4555 * seem to have been set yet, so menu wraps in default window
4556 * width which is very narrow. Instead just return height of a
4557 * single menu item. Will still be wrong when the menu really
4558 * should wrap over more than one line.
4559 */
4560 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4561 if (gui.starting)
4562 menu_height = rc1.bottom - rc1.top + 1;
4563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004565 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4566 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 }
4569
4570 if (fix_window && menu_height != old_menu_height)
4571 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004572 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004574 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576 return menu_height;
4577}
4578#endif /*FEAT_MENU*/
4579
4580
4581/*
4582 * Setup for the Intellimouse
4583 */
4584 static void
4585init_mouse_wheel(void)
4586{
4587
4588#ifndef SPI_GETWHEELSCROLLLINES
4589# define SPI_GETWHEELSCROLLLINES 104
4590#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004591#ifndef SPI_SETWHEELSCROLLLINES
4592# define SPI_SETWHEELSCROLLLINES 105
4593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4596#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4597#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4598#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4599
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 mouse_scroll_lines = 3; /* reasonable default */
4601
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004602 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4603 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4604 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
4607
4608/* Intellimouse wheel handler */
4609 static void
4610_OnMouseWheel(
4611 HWND hwnd,
4612 short zDelta)
4613{
4614/* Treat a mouse wheel event as if it were a scroll request */
4615 int i;
4616 int size;
4617 HWND hwndCtl;
4618
4619 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4620 {
4621 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4622 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4623 }
4624 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4625 {
4626 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4627 size = curwin->w_scrollbars[SBAR_LEFT].size;
4628 }
4629 else
4630 return;
4631
4632 size = curwin->w_height;
4633 if (mouse_scroll_lines == 0)
4634 init_mouse_wheel();
4635
Bram Moolenaara338adc2018-01-31 20:51:47 +01004636 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (mouse_scroll_lines > 0
4638 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4639 {
4640 for (i = mouse_scroll_lines; i > 0; --i)
4641 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4642 }
4643 else
4644 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004645 mch_enable_flush();
4646 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
4648
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004649#ifdef USE_SYSMENU_FONT
4650/*
4651 * Get Menu Font.
4652 * Return OK or FAIL.
4653 */
4654 static int
4655gui_w32_get_menu_font(LOGFONT *lf)
4656{
4657 NONCLIENTMETRICS nm;
4658
4659 nm.cbSize = sizeof(NONCLIENTMETRICS);
4660 if (!SystemParametersInfo(
4661 SPI_GETNONCLIENTMETRICS,
4662 sizeof(NONCLIENTMETRICS),
4663 &nm,
4664 0))
4665 return FAIL;
4666 *lf = nm.lfMenuFont;
4667 return OK;
4668}
4669#endif
4670
4671
4672#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4673/*
4674 * Set the GUI tabline font to the system menu font
4675 */
4676 static void
4677set_tabline_font(void)
4678{
4679 LOGFONT lfSysmenu;
4680 HFONT font;
4681 HWND hwnd;
4682 HDC hdc;
4683 HFONT hfntOld;
4684 TEXTMETRIC tm;
4685
4686 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4687 return;
4688
4689 font = CreateFontIndirect(&lfSysmenu);
4690
4691 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4692
4693 /*
4694 * Compute the height of the font used for the tab text
4695 */
4696 hwnd = GetDesktopWindow();
4697 hdc = GetWindowDC(hwnd);
4698 hfntOld = SelectFont(hdc, font);
4699
4700 GetTextMetrics(hdc, &tm);
4701
4702 SelectFont(hdc, hfntOld);
4703 ReleaseDC(hwnd, hdc);
4704
4705 /*
4706 * The space used by the tab border and the space between the tab label
4707 * and the tab border is included as 7.
4708 */
4709 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4710}
4711#endif
4712
Bram Moolenaar520470a2005-06-16 21:59:56 +00004713/*
4714 * Invoked when a setting was changed.
4715 */
4716 static LRESULT CALLBACK
4717_OnSettingChange(UINT n)
4718{
4719 if (n == SPI_SETWHEELSCROLLLINES)
4720 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4721 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004722#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4723 if (n == SPI_SETNONCLIENTMETRICS)
4724 set_tabline_font();
4725#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004726 return 0;
4727}
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#ifdef FEAT_NETBEANS_INTG
4730 static void
4731_OnWindowPosChanged(
4732 HWND hwnd,
4733 const LPWINDOWPOS lpwpos)
4734{
4735 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004736 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4739 || lpwpos->cx != cx || lpwpos->cy != cy))
4740 {
4741 x = lpwpos->x;
4742 y = lpwpos->y;
4743 cx = lpwpos->cx;
4744 cy = lpwpos->cy;
4745 netbeans_frame_moved(x, y);
4746 }
4747 /* Allow to send WM_SIZE and WM_MOVE */
4748 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4749}
4750#endif
4751
4752 static int
4753_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 UINT fwSide,
4755 LPRECT lprc)
4756{
4757 int w, h;
4758 int valid_w, valid_h;
4759 int w_offset, h_offset;
4760
4761 w = lprc->right - lprc->left;
4762 h = lprc->bottom - lprc->top;
4763 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4764 w_offset = w - valid_w;
4765 h_offset = h - valid_h;
4766
4767 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4768 || fwSide == WMSZ_BOTTOMLEFT)
4769 lprc->left += w_offset;
4770 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4771 || fwSide == WMSZ_BOTTOMRIGHT)
4772 lprc->right -= w_offset;
4773
4774 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4775 || fwSide == WMSZ_TOPRIGHT)
4776 lprc->top += h_offset;
4777 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4778 || fwSide == WMSZ_BOTTOMRIGHT)
4779 lprc->bottom -= h_offset;
4780 return TRUE;
4781}
4782
4783
4784
4785 static LRESULT CALLBACK
4786_WndProc(
4787 HWND hwnd,
4788 UINT uMsg,
4789 WPARAM wParam,
4790 LPARAM lParam)
4791{
4792 /*
4793 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4794 hwnd, uMsg, wParam, lParam);
4795 */
4796
4797 HandleMouseHide(uMsg, lParam);
4798
4799 s_uMsg = uMsg;
4800 s_wParam = wParam;
4801 s_lParam = lParam;
4802
4803 switch (uMsg)
4804 {
4805 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4806 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4807 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4808 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4809 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4810 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4811 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4812 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4813 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4814#ifdef FEAT_MENU
4815 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4816#endif
4817 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4818 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4819 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4820 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4821 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4822 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4823 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4824 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4825 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4826#ifdef FEAT_NETBEANS_INTG
4827 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4828#endif
4829
Bram Moolenaarafa24992006-03-27 20:58:26 +00004830#ifdef FEAT_GUI_TABLINE
4831 case WM_RBUTTONUP:
4832 {
4833 if (gui_mch_showing_tabline())
4834 {
4835 POINT pt;
4836 RECT rect;
4837
4838 /*
4839 * If the cursor is on the tabline, display the tab menu
4840 */
4841 GetCursorPos((LPPOINT)&pt);
4842 GetWindowRect(s_textArea, &rect);
4843 if (pt.y < rect.top)
4844 {
4845 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004846 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004847 }
4848 }
4849 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004851 case WM_LBUTTONDBLCLK:
4852 {
4853 /*
4854 * If the user double clicked the tabline, create a new tab
4855 */
4856 if (gui_mch_showing_tabline())
4857 {
4858 POINT pt;
4859 RECT rect;
4860
4861 GetCursorPos((LPPOINT)&pt);
4862 GetWindowRect(s_textArea, &rect);
4863 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004864 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004865 }
4866 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4867 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004868#endif
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 case WM_QUERYENDSESSION: /* System wants to go down. */
4871 gui_shell_closed(); /* Will exit when no changed buffers. */
4872 return FALSE; /* Do NOT allow system to go down. */
4873
4874 case WM_ENDSESSION:
4875 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004878 return 0L;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 break;
4881
4882 case WM_CHAR:
4883 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4884 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004885 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return 0L;
4887
4888 case WM_SYSCHAR:
4889 /*
4890 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4891 * shortcut key, handle like a typed ALT key, otherwise call Windows
4892 * ALT key handling.
4893 */
4894#ifdef FEAT_MENU
4895 if ( !gui.menu_is_active
4896 || p_wak[0] == 'n'
4897 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4898 )
4899#endif
4900 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004901 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 return 0L;
4903 }
4904#ifdef FEAT_MENU
4905 else
4906 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4907#endif
4908
4909 case WM_SYSKEYUP:
4910#ifdef FEAT_MENU
4911 /* This used to be done only when menu is active: ALT key is used for
4912 * that. But that caused problems when menu is disabled and using
4913 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4914 * are received, mouse pointer remains hidden. */
4915 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4916#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004917 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918#endif
4919
4920 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 case WM_MOUSEWHEEL:
4924 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004925 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
Bram Moolenaar520470a2005-06-16 21:59:56 +00004927 /* Notification for change in SystemParametersInfo() */
4928 case WM_SETTINGCHANGE:
4929 return _OnSettingChange((UINT)wParam);
4930
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004931#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 case WM_NOTIFY:
4933 switch (((LPNMHDR) lParam)->code)
4934 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004935# ifdef FEAT_MBYTE
4936 case TTN_GETDISPINFOW:
4937# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004938 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004940 LPNMHDR hdr = (LPNMHDR)lParam;
4941 char_u *str = NULL;
4942 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004943
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004944 vim_free(tt_text);
4945 tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004946
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004947# ifdef FEAT_GUI_TABLINE
4948 if (gui_mch_showing_tabline()
4949 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004950 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004951 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004952 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004953 * Mouse is over the GUI tabline. Display the
4954 * tooltip for the tab under the cursor
4955 *
4956 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004957 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004958 GetCursorPos(&pt);
4959 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004960 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004961 TCHITTESTINFO htinfo;
4962 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004963
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004964 /*
4965 * Get the tab under the cursor
4966 */
4967 htinfo.pt.x = pt.x;
4968 htinfo.pt.y = pt.y;
4969 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4970 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004971 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004972 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004973
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004974 tp = find_tabpage(idx + 1);
4975 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004976 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004977 get_tabline_label(tp, TRUE);
4978 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004979 }
4980 }
4981 }
4982 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004983# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004984# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004985# ifdef FEAT_GUI_TABLINE
4986 else
4987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004989 UINT idButton;
4990 vimmenu_T *pMenu;
4991
4992 idButton = (UINT) hdr->idFrom;
4993 pMenu = gui_mswin_find_menu(root_menu, idButton);
4994 if (pMenu)
4995 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004997# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004998 if (str != NULL)
4999 {
5000# ifdef FEAT_MBYTE
5001 if (hdr->code == TTN_GETDISPINFOW)
5002 {
5003 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5004
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005005 /* Set the maximum width, this also enables using
5006 * \n for line break. */
5007 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5008 0, 500);
5009
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005010 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005011 lpdi->lpszText = tt_text;
5012 /* can't show tooltip if failed */
5013 }
5014 else
5015# endif
5016 {
5017 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5018
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005019 /* Set the maximum width, this also enables using
5020 * \n for line break. */
5021 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5022 0, 500);
5023
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005024 if (STRLEN(str) < sizeof(lpdi->szText)
5025 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005026 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005027 sizeof(lpdi->szText) - 1);
5028 else
5029 lpdi->lpszText = tt_text;
5030 }
5031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005034# ifdef FEAT_GUI_TABLINE
5035 case TCN_SELCHANGE:
5036 if (gui_mch_showing_tabline()
5037 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005038 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005039 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005040 return 0L;
5041 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005042 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005043
5044 case NM_RCLICK:
5045 if (gui_mch_showing_tabline()
5046 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005047 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005048 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005049 return 0L;
5050 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005051 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005054# ifdef FEAT_GUI_TABLINE
5055 if (gui_mch_showing_tabline()
5056 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5057 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 break;
5060 }
5061 break;
5062#endif
5063#if defined(MENUHINTS) && defined(FEAT_MENU)
5064 case WM_MENUSELECT:
5065 if (((UINT) HIWORD(wParam)
5066 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5067 == MF_HILITE
5068 && (State & CMDLINE) == 0)
5069 {
5070 UINT idButton;
5071 vimmenu_T *pMenu;
5072 static int did_menu_tip = FALSE;
5073
5074 if (did_menu_tip)
5075 {
5076 msg_clr_cmdline();
5077 setcursor();
5078 out_flush();
5079 did_menu_tip = FALSE;
5080 }
5081
5082 idButton = (UINT)LOWORD(wParam);
5083 pMenu = gui_mswin_find_menu(root_menu, idButton);
5084 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5085 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5086 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005087 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005089 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 setcursor();
5091 out_flush();
5092 did_menu_tip = TRUE;
5093 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005094 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 break;
5097#endif
5098 case WM_NCHITTEST:
5099 {
5100 LRESULT result;
5101 int x, y;
5102 int xPos = GET_X_LPARAM(lParam);
5103
5104 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5105 if (result == HTCLIENT)
5106 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005107#ifdef FEAT_GUI_TABLINE
5108 if (gui_mch_showing_tabline())
5109 {
5110 int yPos = GET_Y_LPARAM(lParam);
5111 RECT rct;
5112
5113 /* If the cursor is on the GUI tabline, don't process this
5114 * event */
5115 GetWindowRect(s_textArea, &rct);
5116 if (yPos < rct.top)
5117 return result;
5118 }
5119#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005120 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 xPos -= x;
5122
5123 if (xPos < 48) /* <VN> TODO should use system metric? */
5124 return HTBOTTOMLEFT;
5125 else
5126 return HTBOTTOMRIGHT;
5127 }
5128 else
5129 return result;
5130 }
5131 /* break; notreached */
5132
5133#ifdef FEAT_MBYTE_IME
5134 case WM_IME_NOTIFY:
5135 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5136 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005137 return 1L;
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 case WM_IME_COMPOSITION:
5140 if (!_OnImeComposition(hwnd, wParam, lParam))
5141 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005142 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143#endif
5144
5145 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005147 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 _OnFindRepl();
5150 }
5151#endif
5152 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5153 }
5154
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005155 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156}
5157
5158/*
5159 * End of call-back routines
5160 */
5161
5162/* parent window, if specified with -P */
5163HWND vim_parent_hwnd = NULL;
5164
5165 static BOOL CALLBACK
5166FindWindowTitle(HWND hwnd, LPARAM lParam)
5167{
5168 char buf[2048];
5169 char *title = (char *)lParam;
5170
5171 if (GetWindowText(hwnd, buf, sizeof(buf)))
5172 {
5173 if (strstr(buf, title) != NULL)
5174 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005175 /* Found it. Store the window ref. and quit searching if MDI
5176 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005178 if (vim_parent_hwnd != NULL)
5179 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 }
5182 return TRUE; /* continue searching */
5183}
5184
5185/*
5186 * Invoked for '-P "title"' argument: search for parent application to open
5187 * our window in.
5188 */
5189 void
5190gui_mch_set_parent(char *title)
5191{
5192 EnumWindows(FindWindowTitle, (LPARAM)title);
5193 if (vim_parent_hwnd == NULL)
5194 {
5195 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5196 mch_exit(2);
5197 }
5198}
5199
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 static void
5202ole_error(char *arg)
5203{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005204 char buf[IOSIZE];
5205
5206 /* Can't use EMSG() here, we have not finished initialisation yet. */
5207 vim_snprintf(buf, IOSIZE,
5208 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5209 arg);
5210 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
5214/*
5215 * Parse the GUI related command-line arguments. Any arguments used are
5216 * deleted from argv, and *argc is decremented accordingly. This is called
5217 * when vim is started, whether or not the GUI has been started.
5218 */
5219 void
5220gui_mch_prepare(int *argc, char **argv)
5221{
5222 int silent = FALSE;
5223 int idx;
5224
5225 /* Check for special OLE command line parameters */
5226 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5227 {
5228 /* Check for a "-silent" argument first. */
5229 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5230 && (argv[2][0] == '-' || argv[2][0] == '/'))
5231 {
5232 silent = TRUE;
5233 idx = 2;
5234 }
5235 else
5236 idx = 1;
5237
5238 /* Register Vim as an OLE Automation server */
5239 if (STRICMP(argv[idx] + 1, "register") == 0)
5240 {
5241#ifdef FEAT_OLE
5242 RegisterMe(silent);
5243 mch_exit(0);
5244#else
5245 if (!silent)
5246 ole_error("register");
5247 mch_exit(2);
5248#endif
5249 }
5250
5251 /* Unregister Vim as an OLE Automation server */
5252 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5253 {
5254#ifdef FEAT_OLE
5255 UnregisterMe(!silent);
5256 mch_exit(0);
5257#else
5258 if (!silent)
5259 ole_error("unregister");
5260 mch_exit(2);
5261#endif
5262 }
5263
5264 /* Ignore an -embedding argument. It is only relevant if the
5265 * application wants to treat the case when it is started manually
5266 * differently from the case where it is started via automation (and
5267 * we don't).
5268 */
5269 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5270 {
5271#ifdef FEAT_OLE
5272 *argc = 1;
5273#else
5274 ole_error("embedding");
5275 mch_exit(2);
5276#endif
5277 }
5278 }
5279
5280#ifdef FEAT_OLE
5281 {
5282 int bDoRestart = FALSE;
5283
5284 InitOLE(&bDoRestart);
5285 /* automatically exit after registering */
5286 if (bDoRestart)
5287 mch_exit(0);
5288 }
5289#endif
5290
5291#ifdef FEAT_NETBEANS_INTG
5292 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005293 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 int arg;
5295
5296 for (arg = 1; arg < *argc; arg++)
5297 if (strncmp("-nb", argv[arg], 3) == 0)
5298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 netbeansArg = argv[arg];
5300 mch_memmove(&argv[arg], &argv[arg + 1],
5301 (--*argc - arg) * sizeof(char *));
5302 argv[*argc] = NULL;
5303 break; /* enough? */
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307}
5308
5309/*
5310 * Initialise the GUI. Create all the windows, set up all the call-backs
5311 * etc.
5312 */
5313 int
5314gui_mch_init(void)
5315{
5316 const char szVimWndClass[] = VIM_CLASS;
5317 const char szTextAreaClass[] = "VimTextArea";
5318 WNDCLASS wndclass;
5319#ifdef FEAT_MBYTE
5320 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005321 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 WNDCLASSW wndclassw;
5323#endif
5324#ifdef GLOBAL_IME
5325 ATOM atom;
5326#endif
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 /* Return here if the window was already opened (happens when
5329 * gui_mch_dialog() is called early). */
5330 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005331 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332
5333 /*
5334 * Load the tearoff bitmap
5335 */
5336#ifdef FEAT_TEAROFF
5337 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5338#endif
5339
5340 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5341 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5342#ifdef FEAT_MENU
5343 gui.menu_height = 0; /* Windows takes care of this */
5344#endif
5345 gui.border_width = 0;
5346
5347 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5348
5349#ifdef FEAT_MBYTE
5350 /* First try using the wide version, so that we can use any title.
5351 * Otherwise only characters in the active codepage will work. */
5352 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5353 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005354 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 wndclassw.lpfnWndProc = _WndProc;
5356 wndclassw.cbClsExtra = 0;
5357 wndclassw.cbWndExtra = 0;
5358 wndclassw.hInstance = s_hinst;
5359 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5360 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5361 wndclassw.hbrBackground = s_brush;
5362 wndclassw.lpszMenuName = NULL;
5363 wndclassw.lpszClassName = szVimWndClassW;
5364
5365 if ((
5366#ifdef GLOBAL_IME
5367 atom =
5368#endif
5369 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 else
5372 wide_WindowProc = TRUE;
5373 }
5374
5375 if (!wide_WindowProc)
5376#endif
5377
5378 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5379 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005380 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 wndclass.lpfnWndProc = _WndProc;
5382 wndclass.cbClsExtra = 0;
5383 wndclass.cbWndExtra = 0;
5384 wndclass.hInstance = s_hinst;
5385 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5386 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5387 wndclass.hbrBackground = s_brush;
5388 wndclass.lpszMenuName = NULL;
5389 wndclass.lpszClassName = szVimWndClass;
5390
5391 if ((
5392#ifdef GLOBAL_IME
5393 atom =
5394#endif
5395 RegisterClass(&wndclass)) == 0)
5396 return FAIL;
5397 }
5398
5399 if (vim_parent_hwnd != NULL)
5400 {
5401#ifdef HAVE_TRY_EXCEPT
5402 __try
5403 {
5404#endif
5405 /* Open inside the specified parent window.
5406 * TODO: last argument should point to a CLIENTCREATESTRUCT
5407 * structure. */
5408 s_hwnd = CreateWindowEx(
5409 WS_EX_MDICHILD,
5410 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005411 WS_OVERLAPPEDWINDOW | WS_CHILD
5412 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5414 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5415 100, /* Any value will do */
5416 100, /* Any value will do */
5417 vim_parent_hwnd, NULL,
5418 s_hinst, NULL);
5419#ifdef HAVE_TRY_EXCEPT
5420 }
5421 __except(EXCEPTION_EXECUTE_HANDLER)
5422 {
5423 /* NOP */
5424 }
5425#endif
5426 if (s_hwnd == NULL)
5427 {
5428 EMSG(_("E672: Unable to open window inside MDI application"));
5429 mch_exit(2);
5430 }
5431 }
5432 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005433 {
5434 /* If the provided windowid is not valid reset it to zero, so that it
5435 * is ignored and we open our own window. */
5436 if (IsWindow((HWND)win_socket_id) <= 0)
5437 win_socket_id = 0;
5438
5439 /* Create a window. If win_socket_id is not zero without border and
5440 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005442 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005443 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5444 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005445 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5446 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5447 100, /* Any value will do */
5448 100, /* Any value will do */
5449 NULL, NULL,
5450 s_hinst, NULL);
5451 if (s_hwnd != NULL && win_socket_id != 0)
5452 {
5453 SetParent(s_hwnd, (HWND)win_socket_id);
5454 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5455 }
5456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458 if (s_hwnd == NULL)
5459 return FAIL;
5460
5461#ifdef GLOBAL_IME
5462 global_ime_init(atom, s_hwnd);
5463#endif
5464#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5465 dyn_imm_load();
5466#endif
5467
5468 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005469#ifdef FEAT_MBYTE
5470 if (wide_WindowProc)
5471 {
5472 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5473 {
5474 wndclassw.style = CS_OWNDC;
5475 wndclassw.lpfnWndProc = _TextAreaWndProc;
5476 wndclassw.cbClsExtra = 0;
5477 wndclassw.cbWndExtra = 0;
5478 wndclassw.hInstance = s_hinst;
5479 wndclassw.hIcon = NULL;
5480 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5481 wndclassw.hbrBackground = NULL;
5482 wndclassw.lpszMenuName = NULL;
5483 wndclassw.lpszClassName = szTextAreaClassW;
5484
5485 if (RegisterClassW(&wndclassw) == 0)
5486 return FAIL;
5487 }
5488 }
5489 else
5490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5492 {
5493 wndclass.style = CS_OWNDC;
5494 wndclass.lpfnWndProc = _TextAreaWndProc;
5495 wndclass.cbClsExtra = 0;
5496 wndclass.cbWndExtra = 0;
5497 wndclass.hInstance = s_hinst;
5498 wndclass.hIcon = NULL;
5499 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5500 wndclass.hbrBackground = NULL;
5501 wndclass.lpszMenuName = NULL;
5502 wndclass.lpszClassName = szTextAreaClass;
5503
5504 if (RegisterClass(&wndclass) == 0)
5505 return FAIL;
5506 }
5507 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005508 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 szTextAreaClass, "Vim text area",
5510 WS_CHILD | WS_VISIBLE, 0, 0,
5511 100, /* Any value will do for now */
5512 100, /* Any value will do for now */
5513 s_hwnd, NULL,
5514 s_hinst, NULL);
5515
5516 if (s_textArea == NULL)
5517 return FAIL;
5518
Bram Moolenaar20321902016-02-17 12:30:17 +01005519#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005520 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5521 {
5522 HANDLE hIcon = NULL;
5523
5524 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005525 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005526 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005527#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#ifdef FEAT_MENU
5530 s_menuBar = CreateMenu();
5531#endif
5532 s_hdc = GetDC(s_textArea);
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
5536 /* Do we need to bother with this? */
5537 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5538
5539 /* Get background/foreground colors from the system */
5540 gui_mch_def_colors();
5541
5542 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5543 * file) */
5544 set_normal_colors();
5545
5546 /*
5547 * Check that none of the colors are the same as the background color.
5548 * Then store the current values as the defaults.
5549 */
5550 gui_check_colors();
5551 gui.def_norm_pixel = gui.norm_pixel;
5552 gui.def_back_pixel = gui.back_pixel;
5553
5554 /* Get the colors for the highlight groups (gui_check_colors() might have
5555 * changed them) */
5556 highlight_gui_started();
5557
5558 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005559 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005561 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
5563 /*
5564 * Set up for Intellimouse processing
5565 */
5566 init_mouse_wheel();
5567
5568 /*
5569 * compute a couple of metrics used for the dialogs
5570 */
5571 get_dialog_font_metrics();
5572#ifdef FEAT_TOOLBAR
5573 /*
5574 * Create the toolbar
5575 */
5576 initialise_toolbar();
5577#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005578#ifdef FEAT_GUI_TABLINE
5579 /*
5580 * Create the tabline
5581 */
5582 initialise_tabline();
5583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584#ifdef MSWIN_FIND_REPLACE
5585 /*
5586 * Initialise the dialog box stuff
5587 */
5588 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5589
5590 /* Initialise the struct */
5591 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005592 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005594 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5596 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5597 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005598# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005599 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5600 s_findrep_struct_w.lpstrFindWhat =
5601 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5602 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5603 s_findrep_struct_w.lpstrReplaceWith =
5604 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5605 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5606 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5607 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005611#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005612# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5613/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5614# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005615# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005616# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005617# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005618 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005619 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005620#endif
5621
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005622#ifdef FEAT_RENDER_OPTIONS
5623 if (p_rop)
5624 (void)gui_mch_set_rendering_options(p_rop);
5625#endif
5626
Bram Moolenaar748bf032005-02-02 23:04:36 +00005627theend:
5628 /* Display any pending error messages */
5629 display_errors();
5630
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 return OK;
5632}
5633
5634/*
5635 * Get the size of the screen, taking position on multiple monitors into
5636 * account (if supported).
5637 */
5638 static void
5639get_work_area(RECT *spi_rect)
5640{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005641 HMONITOR mon;
5642 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005644 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005645 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005646 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005648 moninfo.cbSize = sizeof(MONITORINFO);
5649 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005651 *spi_rect = moninfo.rcWork;
5652 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654 }
5655 /* this is the old method... */
5656 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5657}
5658
5659/*
5660 * Set the size of the window to the given width and height in pixels.
5661 */
5662 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005663gui_mch_set_shellsize(
5664 int width,
5665 int height,
5666 int min_width UNUSED,
5667 int min_height UNUSED,
5668 int base_width UNUSED,
5669 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005670 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 RECT workarea_rect;
5673 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 WINDOWPLACEMENT wndpl;
5675
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005676 /* Try to keep window completely on screen. */
5677 /* Get position of the screen work area. This is the part that is not
5678 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 get_work_area(&workarea_rect);
5680
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005681 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005682 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 wndpl.length = sizeof(WINDOWPLACEMENT);
5684 GetWindowPlacement(s_hwnd, &wndpl);
5685
5686 /* Resizing a maximized window looks very strange, unzoom it first.
5687 * But don't do it when still starting up, it may have been requested in
5688 * the shortcut. */
5689 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5690 {
5691 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5692 /* Need to get the settings of the normal window. */
5693 GetWindowPlacement(s_hwnd, &wndpl);
5694 }
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005697 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005698 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005699 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005700 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 + GetSystemMetrics(SM_CYCAPTION)
5702#ifdef FEAT_MENU
5703 + gui_mswin_get_menu_height(FALSE)
5704#endif
5705 ;
5706
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005707 /* The following should take care of keeping Vim on the same monitor, no
5708 * matter if the secondary monitor is left or right of the primary
5709 * monitor. */
5710 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5711 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005712
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005713 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005714 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005715 && wndpl.rcNormalPosition.right > workarea_rect.right)
5716 OffsetRect(&wndpl.rcNormalPosition,
5717 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005719 if ((direction & RESIZE_HOR)
5720 && wndpl.rcNormalPosition.left < workarea_rect.left)
5721 OffsetRect(&wndpl.rcNormalPosition,
5722 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
Bram Moolenaarafa24992006-03-27 20:58:26 +00005724 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005725 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5726 OffsetRect(&wndpl.rcNormalPosition,
5727 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005729 if ((direction & RESIZE_VERT)
5730 && wndpl.rcNormalPosition.top < workarea_rect.top)
5731 OffsetRect(&wndpl.rcNormalPosition,
5732 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 /* set window position - we should use SetWindowPlacement rather than
5735 * SetWindowPos as the MSDN docs say the coord systems returned by
5736 * these two are not compatible. */
5737 SetWindowPlacement(s_hwnd, &wndpl);
5738
5739 SetActiveWindow(s_hwnd);
5740 SetFocus(s_hwnd);
5741
5742#ifdef FEAT_MENU
5743 /* Menu may wrap differently now */
5744 gui_mswin_get_menu_height(!gui.starting);
5745#endif
5746}
5747
5748
5749 void
5750gui_mch_set_scrollbar_thumb(
5751 scrollbar_T *sb,
5752 long val,
5753 long size,
5754 long max)
5755{
5756 SCROLLINFO info;
5757
5758 sb->scroll_shift = 0;
5759 while (max > 32767)
5760 {
5761 max = (max + 1) >> 1;
5762 val >>= 1;
5763 size >>= 1;
5764 ++sb->scroll_shift;
5765 }
5766
5767 if (sb->scroll_shift > 0)
5768 ++size;
5769
5770 info.cbSize = sizeof(info);
5771 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5772 info.nPos = val;
5773 info.nMin = 0;
5774 info.nMax = max;
5775 info.nPage = size;
5776 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5777}
5778
5779
5780/*
5781 * Set the current text font.
5782 */
5783 void
5784gui_mch_set_font(GuiFont font)
5785{
5786 gui.currFont = font;
5787}
5788
5789
5790/*
5791 * Set the current text foreground color.
5792 */
5793 void
5794gui_mch_set_fg_color(guicolor_T color)
5795{
5796 gui.currFgColor = color;
5797}
5798
5799/*
5800 * Set the current text background color.
5801 */
5802 void
5803gui_mch_set_bg_color(guicolor_T color)
5804{
5805 gui.currBgColor = color;
5806}
5807
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005808/*
5809 * Set the current text special color.
5810 */
5811 void
5812gui_mch_set_sp_color(guicolor_T color)
5813{
5814 gui.currSpColor = color;
5815}
5816
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005817#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818/*
5819 * Multi-byte handling, originally by Sung-Hoon Baek.
5820 * First static functions (no prototypes generated).
5821 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005822# ifdef _MSC_VER
5823# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5824# endif
5825# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
5827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 * handle WM_IME_NOTIFY message
5829 */
5830 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005831_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 LRESULT lResult = 0;
5834 HIMC hImc;
5835
5836 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5837 return lResult;
5838 switch (dwCommand)
5839 {
5840 case IMN_SETOPENSTATUS:
5841 if (pImmGetOpenStatus(hImc))
5842 {
5843 pImmSetCompositionFont(hImc, &norm_logfont);
5844 im_set_position(gui.row, gui.col);
5845
5846 /* Disable langmap */
5847 State &= ~LANGMAP;
5848 if (State & INSERT)
5849 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005850#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 /* Unshown 'keymap' in status lines */
5852 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5853 {
5854 /* Save cursor position */
5855 int old_row = gui.row;
5856 int old_col = gui.col;
5857
5858 // This must be called here before
5859 // status_redraw_curbuf(), otherwise the mode
5860 // message may appear in the wrong position.
5861 showmode();
5862 status_redraw_curbuf();
5863 update_screen(0);
5864 /* Restore cursor position */
5865 gui.row = old_row;
5866 gui.col = old_col;
5867 }
5868#endif
5869 }
5870 }
5871 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005872 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 lResult = 0;
5874 break;
5875 }
5876 pImmReleaseContext(hWnd, hImc);
5877 return lResult;
5878}
5879
5880 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005881_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
5883 char_u *ret;
5884 int len;
5885
5886 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5887 return 0;
5888
5889 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5890 if (ret != NULL)
5891 {
5892 add_to_input_buf_csi(ret, len);
5893 vim_free(ret);
5894 return 1;
5895 }
5896 return 0;
5897}
5898
5899/*
5900 * get the current composition string, in UCS-2; *lenp is the number of
5901 * *lenp is the number of Unicode characters.
5902 */
5903 static short_u *
5904GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5905{
5906 LONG ret;
5907 LPWSTR wbuf = NULL;
5908 char_u *buf;
5909
5910 if (!pImmGetContext)
5911 return NULL; /* no imm32.dll */
5912
5913 /* Try Unicode; this'll always work on NT regardless of codepage. */
5914 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5915 if (ret == 0)
5916 return NULL; /* empty */
5917
5918 if (ret > 0)
5919 {
5920 /* Allocate the requested buffer plus space for the NUL character. */
5921 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5922 if (wbuf != NULL)
5923 {
5924 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5925 *lenp = ret / sizeof(WCHAR);
5926 }
5927 return (short_u *)wbuf;
5928 }
5929
5930 /* ret < 0; we got an error, so try the ANSI version. This'll work
5931 * on 9x/ME, but only if the codepage happens to be set to whatever
5932 * we're inputting. */
5933 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5934 if (ret <= 0)
5935 return NULL; /* empty or error */
5936
5937 buf = alloc(ret);
5938 if (buf == NULL)
5939 return NULL;
5940 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5941
5942 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005943 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 vim_free(buf);
5945
5946 return (short_u *)wbuf;
5947}
5948
5949/*
5950 * void GetResultStr()
5951 *
5952 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5953 * get complete composition string
5954 */
5955 static char_u *
5956GetResultStr(HWND hwnd, int GCS, int *lenp)
5957{
5958 HIMC hIMC; /* Input context handle. */
5959 short_u *buf = NULL;
5960 char_u *convbuf = NULL;
5961
5962 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5963 return NULL;
5964
5965 /* Reads in the composition string. */
5966 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5967 if (buf == NULL)
5968 return NULL;
5969
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005970 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 pImmReleaseContext(hwnd, hIMC);
5972 vim_free(buf);
5973 return convbuf;
5974}
5975#endif
5976
5977/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005978#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
5980/*
5981 * set font to IM.
5982 */
5983 void
5984im_set_font(LOGFONT *lf)
5985{
5986 HIMC hImc;
5987
5988 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5989 {
5990 pImmSetCompositionFont(hImc, lf);
5991 pImmReleaseContext(s_hwnd, hImc);
5992 }
5993}
5994
5995/*
5996 * Notify cursor position to IM.
5997 */
5998 void
5999im_set_position(int row, int col)
6000{
6001 HIMC hImc;
6002
6003 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6004 {
6005 COMPOSITIONFORM cfs;
6006
6007 cfs.dwStyle = CFS_POINT;
6008 cfs.ptCurrentPos.x = FILL_X(col);
6009 cfs.ptCurrentPos.y = FILL_Y(row);
6010 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6011 pImmSetCompositionWindow(hImc, &cfs);
6012
6013 pImmReleaseContext(s_hwnd, hImc);
6014 }
6015}
6016
6017/*
6018 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6019 */
6020 void
6021im_set_active(int active)
6022{
6023 HIMC hImc;
6024 static HIMC hImcOld = (HIMC)0;
6025
6026 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6027 {
6028 if (p_imdisable)
6029 {
6030 if (hImcOld == (HIMC)0)
6031 {
6032 hImcOld = pImmGetContext(s_hwnd);
6033 if (hImcOld)
6034 pImmAssociateContext(s_hwnd, (HIMC)0);
6035 }
6036 active = FALSE;
6037 }
6038 else if (hImcOld != (HIMC)0)
6039 {
6040 pImmAssociateContext(s_hwnd, hImcOld);
6041 hImcOld = (HIMC)0;
6042 }
6043
6044 hImc = pImmGetContext(s_hwnd);
6045 if (hImc)
6046 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006047 /*
6048 * for Korean ime
6049 */
6050 HKL hKL = GetKeyboardLayout(0);
6051
6052 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6053 {
6054 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6055 static BOOL bSaved = FALSE;
6056
6057 if (active)
6058 {
6059 /* if we have a saved conversion status, restore it */
6060 if (bSaved)
6061 pImmSetConversionStatus(hImc, dwConversionSaved,
6062 dwSentenceSaved);
6063 bSaved = FALSE;
6064 }
6065 else
6066 {
6067 /* save conversion status and disable korean */
6068 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6069 &dwSentenceSaved))
6070 {
6071 bSaved = TRUE;
6072 pImmSetConversionStatus(hImc,
6073 dwConversionSaved & ~(IME_CMODE_NATIVE
6074 | IME_CMODE_FULLSHAPE),
6075 dwSentenceSaved);
6076 }
6077 }
6078 }
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 pImmSetOpenStatus(hImc, active);
6081 pImmReleaseContext(s_hwnd, hImc);
6082 }
6083 }
6084}
6085
6086/*
6087 * Get IM status. When IM is on, return not 0. Else return 0.
6088 */
6089 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006090im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091{
6092 int status = 0;
6093 HIMC hImc;
6094
6095 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6096 {
6097 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6098 pImmReleaseContext(s_hwnd, hImc);
6099 }
6100 return status;
6101}
6102
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006103#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6106/* Win32 with GLOBAL IME */
6107
6108/*
6109 * Notify cursor position to IM.
6110 */
6111 void
6112im_set_position(int row, int col)
6113{
6114 /* Win32 with GLOBAL IME */
6115 POINT p;
6116
6117 p.x = FILL_X(col);
6118 p.y = FILL_Y(row);
6119 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6120 global_ime_set_position(&p);
6121}
6122
6123/*
6124 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6125 */
6126 void
6127im_set_active(int active)
6128{
6129 global_ime_set_status(active);
6130}
6131
6132/*
6133 * Get IM status. When IM is on, return not 0. Else return 0.
6134 */
6135 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006136im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 return global_ime_get_status();
6139}
6140#endif
6141
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006142#ifdef FEAT_MBYTE
6143/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006144 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006145 */
6146 static void
6147latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6148{
6149 int c;
6150
Bram Moolenaarca003e12006-03-17 23:19:38 +00006151 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006152 {
6153 c = *text++;
6154 switch (c)
6155 {
6156 case 0xa4: c = 0x20ac; break; /* euro */
6157 case 0xa6: c = 0x0160; break; /* S hat */
6158 case 0xa8: c = 0x0161; break; /* S -hat */
6159 case 0xb4: c = 0x017d; break; /* Z hat */
6160 case 0xb8: c = 0x017e; break; /* Z -hat */
6161 case 0xbc: c = 0x0152; break; /* OE */
6162 case 0xbd: c = 0x0153; break; /* oe */
6163 case 0xbe: c = 0x0178; break; /* Y */
6164 }
6165 *unicodebuf++ = c;
6166 }
6167}
6168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
6170#ifdef FEAT_RIGHTLEFT
6171/*
6172 * What is this for? In the case where you are using Win98 or Win2K or later,
6173 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6174 * reverses the string sent to the TextOut... family. This sucks, because we
6175 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6176 * way to tell Windblows not to do this!
6177 *
6178 * The short of it is that this 'RevOut' only gets called if you are running
6179 * one of the new, "improved" MS OSes, and only if you are running in
6180 * 'rightleft' mode. It makes display take *slightly* longer, but not
6181 * noticeably so.
6182 */
6183 static void
6184RevOut( HDC s_hdc,
6185 int col,
6186 int row,
6187 UINT foptions,
6188 CONST RECT *pcliprect,
6189 LPCTSTR text,
6190 UINT len,
6191 CONST INT *padding)
6192{
6193 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006195 for (ix = 0; ix < (int)len; ++ix)
6196 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6197 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198}
6199#endif
6200
Bram Moolenaar92467d32017-12-05 13:22:16 +01006201 static void
6202draw_line(
6203 int x1,
6204 int y1,
6205 int x2,
6206 int y2,
6207 COLORREF color)
6208{
6209#if defined(FEAT_DIRECTX)
6210 if (IS_ENABLE_DIRECTX())
6211 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6212 else
6213#endif
6214 {
6215 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6216 HPEN old_pen = SelectObject(s_hdc, hpen);
6217 MoveToEx(s_hdc, x1, y1, NULL);
6218 /* Note: LineTo() excludes the last pixel in the line. */
6219 LineTo(s_hdc, x2, y2);
6220 DeleteObject(SelectObject(s_hdc, old_pen));
6221 }
6222}
6223
6224 static void
6225set_pixel(
6226 int x,
6227 int y,
6228 COLORREF color)
6229{
6230#if defined(FEAT_DIRECTX)
6231 if (IS_ENABLE_DIRECTX())
6232 DWriteContext_SetPixel(s_dwc, x, y, color);
6233 else
6234#endif
6235 SetPixel(s_hdc, x, y, color);
6236}
6237
6238 static void
6239fill_rect(
6240 const RECT *rcp,
6241 HBRUSH hbr,
6242 COLORREF color)
6243{
6244#if defined(FEAT_DIRECTX)
6245 if (IS_ENABLE_DIRECTX())
6246 DWriteContext_FillRect(s_dwc, rcp, color);
6247 else
6248#endif
6249 {
6250 HBRUSH hbr2;
6251
6252 if (hbr == NULL)
6253 hbr2 = CreateSolidBrush(color);
6254 else
6255 hbr2 = hbr;
6256 FillRect(s_hdc, rcp, hbr2);
6257 if (hbr == NULL)
6258 DeleteBrush(hbr2);
6259 }
6260}
6261
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 void
6263gui_mch_draw_string(
6264 int row,
6265 int col,
6266 char_u *text,
6267 int len,
6268 int flags)
6269{
6270 static int *padding = NULL;
6271 static int pad_size = 0;
6272 int i;
6273 const RECT *pcliprect = NULL;
6274 UINT foptions = 0;
6275#ifdef FEAT_MBYTE
6276 static WCHAR *unicodebuf = NULL;
6277 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006278 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 int n = 0;
6280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 int y;
6282
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 /*
6284 * Italic and bold text seems to have an extra row of pixels at the bottom
6285 * (below where the bottom of the character should be). If we draw the
6286 * characters with a solid background, the top row of pixels in the
6287 * character below will be overwritten. We can fix this by filling in the
6288 * background ourselves, to the correct character proportions, and then
6289 * writing the character in transparent mode. Still have a problem when
6290 * the character is "_", which gets written on to the character below.
6291 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6292 * pixel in their slots, which fixes the problem with the bottom row of
6293 * pixels. We still need this code because otherwise the top row of pixels
6294 * becomes a problem. - webb.
6295 */
6296 static HBRUSH hbr_cache[2] = {NULL, NULL};
6297 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6298 static int brush_lru = 0;
6299 HBRUSH hbr;
6300 RECT rc;
6301
6302 if (!(flags & DRAW_TRANSP))
6303 {
6304 /*
6305 * Clear background first.
6306 * Note: FillRect() excludes right and bottom of rectangle.
6307 */
6308 rc.left = FILL_X(col);
6309 rc.top = FILL_Y(row);
6310#ifdef FEAT_MBYTE
6311 if (has_mbyte)
6312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006314 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
6316 else
6317#endif
6318 rc.right = FILL_X(col + len);
6319 rc.bottom = FILL_Y(row + 1);
6320
6321 /* Cache the created brush, that saves a lot of time. We need two:
6322 * one for cursor background and one for the normal background. */
6323 if (gui.currBgColor == brush_color[0])
6324 {
6325 hbr = hbr_cache[0];
6326 brush_lru = 1;
6327 }
6328 else if (gui.currBgColor == brush_color[1])
6329 {
6330 hbr = hbr_cache[1];
6331 brush_lru = 0;
6332 }
6333 else
6334 {
6335 if (hbr_cache[brush_lru] != NULL)
6336 DeleteBrush(hbr_cache[brush_lru]);
6337 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6338 brush_color[brush_lru] = gui.currBgColor;
6339 hbr = hbr_cache[brush_lru];
6340 brush_lru = !brush_lru;
6341 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006342
Bram Moolenaar92467d32017-12-05 13:22:16 +01006343 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344
6345 SetBkMode(s_hdc, TRANSPARENT);
6346
6347 /*
6348 * When drawing block cursor, prevent inverted character spilling
6349 * over character cell (can happen with bold/italic)
6350 */
6351 if (flags & DRAW_CURSOR)
6352 {
6353 pcliprect = &rc;
6354 foptions = ETO_CLIPPED;
6355 }
6356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 SetTextColor(s_hdc, gui.currFgColor);
6358 SelectFont(s_hdc, gui.currFont);
6359
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006360#ifdef FEAT_DIRECTX
6361 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006362 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006363#endif
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6366 {
6367 vim_free(padding);
6368 pad_size = Columns;
6369
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006370 /* Don't give an out-of-memory message here, it would call us
6371 * recursively. */
6372 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (padding != NULL)
6374 for (i = 0; i < pad_size; i++)
6375 padding[i] = gui.char_width;
6376 }
6377
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378 /*
6379 * We have to provide the padding argument because italic and bold versions
6380 * of fixed-width fonts are often one pixel or so wider than their normal
6381 * versions.
6382 * No check for DRAW_BOLD, Windows will have done it already.
6383 */
6384
6385#ifdef FEAT_MBYTE
6386 /* Check if there are any UTF-8 characters. If not, use normal text
6387 * output to speed up output. */
6388 if (enc_utf8)
6389 for (n = 0; n < len; ++n)
6390 if (text[n] >= 0x80)
6391 break;
6392
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006393# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006394 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6395 * required that unicode drawing routine, currently. So this forces it
6396 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006397 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006398 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006399# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006400
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006402 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006404 if ((enc_utf8
6405 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6406 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 && (unicodebuf == NULL || len > unibuflen))
6408 {
6409 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006410 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
6412 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006413 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 unibuflen = len;
6416 }
6417
6418 if (enc_utf8 && n < len && unicodebuf != NULL)
6419 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006420 /* Output UTF-8 characters. Composing characters should be
6421 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006422 int i;
6423 int wlen; /* string length in words */
6424 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 int cells; /* cell width of string up to composing char */
6426 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006427 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006429 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006430 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006432 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006434 c = utf_ptr2char(text + i);
6435 if (c >= 0x10000)
6436 {
6437 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006438 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6439 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006440 }
6441 else
6442 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006443 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006444 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006445
6446 if (utf_iscomposing(c))
6447 cw = 0;
6448 else
6449 {
6450 cw = utf_char2cells(c);
6451 if (cw > 2) /* don't use 4 for unprintable char */
6452 cw = 1;
6453 }
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 if (unicodepdy != NULL)
6456 {
6457 /* Use unicodepdy to make characters fit as we expect, even
6458 * when the font uses different widths (e.g., bold character
6459 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006460 if (c >= 0x10000)
6461 {
6462 unicodepdy[wlen - 2] = cw * gui.char_width;
6463 unicodepdy[wlen - 1] = 0;
6464 }
6465 else
6466 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 }
6468 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006469 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006470 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006472# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006473 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006474 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006475 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006476 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006477 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006478 gui.char_width, gui.currFgColor,
6479 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006480 }
6481 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006482# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006483 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6484 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 len = cells; /* used for underlining */
6486 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006487 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
6489 /* If we want to display codepage data, and the current CP is not the
6490 * ANSI one, we need to go via Unicode. */
6491 if (unicodebuf != NULL)
6492 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006493 if (enc_latin9)
6494 latin9_to_ucs(text, len, unicodebuf);
6495 else
6496 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 MB_PRECOMPOSED,
6498 (char *)text, len,
6499 (LPWSTR)unicodebuf, unibuflen);
6500 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006501 {
6502 /* Use unicodepdy to make characters fit as we expect, even
6503 * when the font uses different widths (e.g., bold character
6504 * is wider). */
6505 if (unicodepdy != NULL)
6506 {
6507 int i;
6508 int cw;
6509
6510 for (i = 0; i < len; ++i)
6511 {
6512 cw = utf_char2cells(unicodebuf[i]);
6513 if (cw > 2)
6514 cw = 1;
6515 unicodepdy[i] = cw * gui.char_width;
6516 }
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006519 foptions, pcliprect, unicodebuf, len, unicodepdy);
6520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522 }
6523 else
6524#endif
6525 {
6526#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006527 /* Windows will mess up RL text, so we have to draw it character by
6528 * character. Only do this if RL is on, since it's slow. */
6529 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6531 foptions, pcliprect, (char *)text, len, padding);
6532 else
6533#endif
6534 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6535 foptions, pcliprect, (char *)text, len, padding);
6536 }
6537
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006538 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (flags & DRAW_UNDERL)
6540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /* When p_linespace is 0, overwrite the bottom row of pixels.
6542 * Otherwise put the line just below the character. */
6543 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (p_linespace > 1)
6545 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006546 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006548
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006549 /* Strikethrough */
6550 if (flags & DRAW_STRIKE)
6551 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006552 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006553 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006554 }
6555
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006556 /* Undercurl */
6557 if (flags & DRAW_UNDERC)
6558 {
6559 int x;
6560 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006561 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006562
6563 y = FILL_Y(row + 1) - 1;
6564 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6565 {
6566 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006567 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006568 }
6569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570}
6571
6572
6573/*
6574 * Output routines.
6575 */
6576
6577/* Flush any output to the screen */
6578 void
6579gui_mch_flush(void)
6580{
6581# if defined(__BORLANDC__)
6582 /*
6583 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6584 * prototype declaration.
6585 * The compiler complains if __stdcall is not used in both declarations.
6586 */
6587 BOOL __stdcall GdiFlush(void);
6588# endif
6589
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006590#if defined(FEAT_DIRECTX)
6591 if (IS_ENABLE_DIRECTX())
6592 DWriteContext_Flush(s_dwc);
6593#endif
6594
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 GdiFlush();
6596}
6597
6598 static void
6599clear_rect(RECT *rcp)
6600{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006601 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602}
6603
6604
Bram Moolenaarc716c302006-01-21 22:12:51 +00006605 void
6606gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6607{
6608 RECT workarea_rect;
6609
6610 get_work_area(&workarea_rect);
6611
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006612 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006613 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006614 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006615
6616 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6617 * the menubar for MSwin, we subtract it from the screen height, so that
6618 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006619 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006620 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006621 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006622 - GetSystemMetrics(SM_CYCAPTION)
6623#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006624 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006625#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006626 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006627}
6628
6629
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630#if defined(FEAT_MENU) || defined(PROTO)
6631/*
6632 * Add a sub menu to the menu bar.
6633 */
6634 void
6635gui_mch_add_menu(
6636 vimmenu_T *menu,
6637 int pos)
6638{
6639 vimmenu_T *parent = menu->parent;
6640
6641 menu->submenu_id = CreatePopupMenu();
6642 menu->id = s_menu_id++;
6643
6644 if (menu_is_menubar(menu->name))
6645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006647 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006649 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6650 {
6651 /* 'encoding' differs from active codepage: convert menu name
6652 * and use wide function */
6653 wn = enc_to_utf16(menu->name, NULL);
6654 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006656 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006658 infow.cbSize = sizeof(infow);
6659 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6660 | MIIM_SUBMENU;
6661 infow.dwItemData = (long_u)menu;
6662 infow.wID = menu->id;
6663 infow.fType = MFT_STRING;
6664 infow.dwTypeData = wn;
6665 infow.cch = (UINT)wcslen(wn);
6666 infow.hSubMenu = menu->submenu_id;
6667 InsertMenuItemW((parent == NULL)
6668 ? s_menuBar : parent->submenu_id,
6669 (UINT)pos, TRUE, &infow);
6670 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006674 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006676 {
6677 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006679 info.cbSize = sizeof(info);
6680 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6681 info.dwItemData = (long_u)menu;
6682 info.wID = menu->id;
6683 info.fType = MFT_STRING;
6684 info.dwTypeData = (LPTSTR)menu->name;
6685 info.cch = (UINT)STRLEN(menu->name);
6686 info.hSubMenu = menu->submenu_id;
6687 InsertMenuItem((parent == NULL)
6688 ? s_menuBar : parent->submenu_id,
6689 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 }
6691 }
6692
6693 /* Fix window size if menu may have wrapped */
6694 if (parent == NULL)
6695 gui_mswin_get_menu_height(!gui.starting);
6696#ifdef FEAT_TEAROFF
6697 else if (IsWindow(parent->tearoff_handle))
6698 rebuild_tearoff(parent);
6699#endif
6700}
6701
6702 void
6703gui_mch_show_popupmenu(vimmenu_T *menu)
6704{
6705 POINT mp;
6706
6707 (void)GetCursorPos((LPPOINT)&mp);
6708 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6709}
6710
6711 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006712gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 vimmenu_T *menu = gui_find_menu(path_name);
6715
6716 if (menu != NULL)
6717 {
6718 POINT p;
6719
6720 /* Find the position of the current cursor */
6721 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006722 if (mouse_pos)
6723 {
6724 int mx, my;
6725
6726 gui_mch_getmouse(&mx, &my);
6727 p.x += mx;
6728 p.y += my;
6729 }
6730 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006732 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6734 }
6735 msg_scroll = FALSE;
6736 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6737 }
6738}
6739
6740#if defined(FEAT_TEAROFF) || defined(PROTO)
6741/*
6742 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6743 * create it as a pseudo-"tearoff menu".
6744 */
6745 void
6746gui_make_tearoff(char_u *path_name)
6747{
6748 vimmenu_T *menu = gui_find_menu(path_name);
6749
6750 /* Found the menu, so tear it off. */
6751 if (menu != NULL)
6752 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6753}
6754#endif
6755
6756/*
6757 * Add a menu item to a menu
6758 */
6759 void
6760gui_mch_add_menu_item(
6761 vimmenu_T *menu,
6762 int idx)
6763{
6764 vimmenu_T *parent = menu->parent;
6765
6766 menu->id = s_menu_id++;
6767 menu->submenu_id = NULL;
6768
6769#ifdef FEAT_TEAROFF
6770 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6771 {
6772 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6773 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6774 }
6775 else
6776#endif
6777#ifdef FEAT_TOOLBAR
6778 if (menu_is_toolbar(parent->name))
6779 {
6780 TBBUTTON newtb;
6781
6782 vim_memset(&newtb, 0, sizeof(newtb));
6783 if (menu_is_separator(menu->name))
6784 {
6785 newtb.iBitmap = 0;
6786 newtb.fsStyle = TBSTYLE_SEP;
6787 }
6788 else
6789 {
6790 newtb.iBitmap = get_toolbar_bitmap(menu);
6791 newtb.fsStyle = TBSTYLE_BUTTON;
6792 }
6793 newtb.idCommand = menu->id;
6794 newtb.fsState = TBSTATE_ENABLED;
6795 newtb.iString = 0;
6796 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6797 (LPARAM)&newtb);
6798 menu->submenu_id = (HMENU)-1;
6799 }
6800 else
6801#endif
6802 {
6803#ifdef FEAT_MBYTE
6804 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6807 {
6808 /* 'encoding' differs from active codepage: convert menu item name
6809 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006810 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 if (wn != NULL)
6812 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006813 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 (menu_is_separator(menu->name)
6815 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6816 (UINT)menu->id, wn);
6817 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819 }
6820 if (wn == NULL)
6821#endif
6822 InsertMenu(parent->submenu_id, (UINT)idx,
6823 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6824 | MF_BYPOSITION,
6825 (UINT)menu->id, (LPCTSTR)menu->name);
6826#ifdef FEAT_TEAROFF
6827 if (IsWindow(parent->tearoff_handle))
6828 rebuild_tearoff(parent);
6829#endif
6830 }
6831}
6832
6833/*
6834 * Destroy the machine specific menu widget.
6835 */
6836 void
6837gui_mch_destroy_menu(vimmenu_T *menu)
6838{
6839#ifdef FEAT_TOOLBAR
6840 /*
6841 * is this a toolbar button?
6842 */
6843 if (menu->submenu_id == (HMENU)-1)
6844 {
6845 int iButton;
6846
6847 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6848 (WPARAM)menu->id, 0);
6849 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6850 }
6851 else
6852#endif
6853 {
6854 if (menu->parent != NULL
6855 && menu_is_popup(menu->parent->dname)
6856 && menu->parent->submenu_id != NULL)
6857 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6858 else
6859 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6860 if (menu->submenu_id != NULL)
6861 DestroyMenu(menu->submenu_id);
6862#ifdef FEAT_TEAROFF
6863 if (IsWindow(menu->tearoff_handle))
6864 DestroyWindow(menu->tearoff_handle);
6865 if (menu->parent != NULL
6866 && menu->parent->children != NULL
6867 && IsWindow(menu->parent->tearoff_handle))
6868 {
6869 /* This menu must not show up when rebuilding the tearoff window. */
6870 menu->modes = 0;
6871 rebuild_tearoff(menu->parent);
6872 }
6873#endif
6874 }
6875}
6876
6877#ifdef FEAT_TEAROFF
6878 static void
6879rebuild_tearoff(vimmenu_T *menu)
6880{
6881 /*hackish*/
6882 char_u tbuf[128];
6883 RECT trect;
6884 RECT rct;
6885 RECT roct;
6886 int x, y;
6887
6888 HWND thwnd = menu->tearoff_handle;
6889
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006890 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (GetWindowRect(thwnd, &trect)
6892 && GetWindowRect(s_hwnd, &rct)
6893 && GetClientRect(s_hwnd, &roct))
6894 {
6895 x = trect.left - rct.left;
6896 y = (trect.top - rct.bottom + roct.bottom);
6897 }
6898 else
6899 {
6900 x = y = 0xffffL;
6901 }
6902 DestroyWindow(thwnd);
6903 if (menu->children != NULL)
6904 {
6905 gui_mch_tearoff(tbuf, menu, x, y);
6906 if (IsWindow(menu->tearoff_handle))
6907 (void) SetWindowPos(menu->tearoff_handle,
6908 NULL,
6909 (int)trect.left,
6910 (int)trect.top,
6911 0, 0,
6912 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6913 }
6914}
6915#endif /* FEAT_TEAROFF */
6916
6917/*
6918 * Make a menu either grey or not grey.
6919 */
6920 void
6921gui_mch_menu_grey(
6922 vimmenu_T *menu,
6923 int grey)
6924{
6925#ifdef FEAT_TOOLBAR
6926 /*
6927 * is this a toolbar button?
6928 */
6929 if (menu->submenu_id == (HMENU)-1)
6930 {
6931 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6932 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6933 }
6934 else
6935#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006936 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6937 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939#ifdef FEAT_TEAROFF
6940 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6941 {
6942 WORD menuID;
6943 HWND menuHandle;
6944
6945 /*
6946 * A tearoff button has changed state.
6947 */
6948 if (menu->children == NULL)
6949 menuID = (WORD)(menu->id);
6950 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006951 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6953 if (menuHandle)
6954 EnableWindow(menuHandle, !grey);
6955
6956 }
6957#endif
6958}
6959
6960#endif /* FEAT_MENU */
6961
6962
6963/* define some macros used to make the dialogue creation more readable */
6964
6965#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6966#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006967#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
6969#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6970/*
6971 * stuff for dialogs
6972 */
6973
6974/*
6975 * The callback routine used by all the dialogs. Very simple. First,
6976 * acknowledges the INITDIALOG message so that Windows knows to do standard
6977 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6978 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6979 * number.
6980 */
6981 static LRESULT CALLBACK
6982dialog_callback(
6983 HWND hwnd,
6984 UINT message,
6985 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006986 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987{
6988 if (message == WM_INITDIALOG)
6989 {
6990 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
6991 /* Set focus to the dialog. Set the default button, if specified. */
6992 (void)SetFocus(hwnd);
6993 if (dialog_default_button > IDCANCEL)
6994 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006995 else
6996 /* We don't have a default, set focus on another element of the
6997 * dialog window, probably the icon */
6998 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 return FALSE;
7000 }
7001
7002 if (message == WM_COMMAND)
7003 {
7004 int button = LOWORD(wParam);
7005
7006 /* Don't end the dialog if something was selected that was
7007 * not a button.
7008 */
7009 if (button >= DLG_NONBUTTON_CONTROL)
7010 return TRUE;
7011
7012 /* If the edit box exists, copy the string. */
7013 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007014 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007015# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007016 /* If the OS is Windows NT, and 'encoding' differs from active
7017 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007018 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007019 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007020 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7021 char_u *p;
7022
7023 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7024 p = utf16_to_enc(wp, NULL);
7025 vim_strncpy(s_textfield, p, IOSIZE);
7026 vim_free(p);
7027 vim_free(wp);
7028 }
7029 else
7030# endif
7031 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007032 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 /*
7036 * Need to check for IDOK because if the user just hits Return to
7037 * accept the default value, some reason this is what we get.
7038 */
7039 if (button == IDOK)
7040 {
7041 if (dialog_default_button > IDCANCEL)
7042 EndDialog(hwnd, dialog_default_button);
7043 }
7044 else
7045 EndDialog(hwnd, button - IDCANCEL);
7046 return TRUE;
7047 }
7048
7049 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7050 {
7051 EndDialog(hwnd, 0);
7052 return TRUE;
7053 }
7054 return FALSE;
7055}
7056
7057/*
7058 * Create a dialog dynamically from the parameter strings.
7059 * type = type of dialog (question, alert, etc.)
7060 * title = dialog title. may be NULL for default title.
7061 * message = text to display. Dialog sizes to accommodate it.
7062 * buttons = '\n' separated list of button captions, default first.
7063 * dfltbutton = number of default button.
7064 *
7065 * This routine returns 1 if the first button is pressed,
7066 * 2 for the second, etc.
7067 *
7068 * 0 indicates Esc was pressed.
7069 * -1 for unexpected error
7070 *
7071 * If stubbing out this fn, return 1.
7072 */
7073
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007074static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 "IDR_VIM",
7077 "IDR_VIM_ERROR",
7078 "IDR_VIM_ALERT",
7079 "IDR_VIM_INFO",
7080 "IDR_VIM_QUESTION"
7081};
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 int
7084gui_mch_dialog(
7085 int type,
7086 char_u *title,
7087 char_u *message,
7088 char_u *buttons,
7089 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007090 char_u *textfield,
7091 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007094 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 int numButtons;
7096 int *buttonWidths, *buttonPositions;
7097 int buttonYpos;
7098 int nchar, i;
7099 DWORD lStyle;
7100 int dlgwidth = 0;
7101 int dlgheight;
7102 int editboxheight;
7103 int horizWidth = 0;
7104 int msgheight;
7105 char_u *pstart;
7106 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007107 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 char_u *tbuffer;
7109 RECT rect;
7110 HWND hwnd;
7111 HDC hdc;
7112 HFONT font, oldFont;
7113 TEXTMETRIC fontInfo;
7114 int fontHeight;
7115 int textWidth, minButtonWidth, messageWidth;
7116 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007117 int maxDialogHeight;
7118 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 int vertical;
7120 int dlgPaddingX;
7121 int dlgPaddingY;
7122#ifdef USE_SYSMENU_FONT
7123 LOGFONT lfSysmenu;
7124 int use_lfSysmenu = FALSE;
7125#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007126 garray_T ga;
7127 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
7129#ifndef NO_CONSOLE
7130 /* Don't output anything in silent mode ("ex -s") */
7131 if (silent_mode)
7132 return dfltbutton; /* return default option */
7133#endif
7134
Bram Moolenaar748bf032005-02-02 23:04:36 +00007135 if (s_hwnd == NULL)
7136 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 if ((type < 0) || (type > VIM_LAST_TYPE))
7139 type = 0;
7140
7141 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007142 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007143 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007144 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 if (p == NULL)
7147 return -1;
7148
7149 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007150 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7152 * const.
7153 */
7154 tbuffer = vim_strsave(buttons);
7155 if (tbuffer == NULL)
7156 return -1;
7157
7158 --dfltbutton; /* Change from one-based to zero-based */
7159
7160 /* Count buttons */
7161 numButtons = 1;
7162 for (i = 0; tbuffer[i] != '\0'; i++)
7163 {
7164 if (tbuffer[i] == DLG_BUTTON_SEP)
7165 numButtons++;
7166 }
7167 if (dfltbutton >= numButtons)
7168 dfltbutton = -1;
7169
7170 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007171 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (buttonWidths == NULL)
7173 return -1;
7174
7175 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007176 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (buttonPositions == NULL)
7178 return -1;
7179
7180 /*
7181 * Calculate how big the dialog must be.
7182 */
7183 hwnd = GetDesktopWindow();
7184 hdc = GetWindowDC(hwnd);
7185#ifdef USE_SYSMENU_FONT
7186 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7187 {
7188 font = CreateFontIndirect(&lfSysmenu);
7189 use_lfSysmenu = TRUE;
7190 }
7191 else
7192#endif
7193 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7194 VARIABLE_PITCH , DLG_FONT_NAME);
7195 if (s_usenewlook)
7196 {
7197 oldFont = SelectFont(hdc, font);
7198 dlgPaddingX = DLG_PADDING_X;
7199 dlgPaddingY = DLG_PADDING_Y;
7200 }
7201 else
7202 {
7203 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7204 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7205 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7206 }
7207 GetTextMetrics(hdc, &fontInfo);
7208 fontHeight = fontInfo.tmHeight;
7209
7210 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007211 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
7213 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007214 if (s_hwnd == NULL)
7215 {
7216 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Bram Moolenaarc716c302006-01-21 22:12:51 +00007218 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007219 get_work_area(&workarea_rect);
7220 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7221 if (maxDialogWidth > 600)
7222 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007223 /* Leave some room for the taskbar. */
7224 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007225 }
7226 else
7227 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007228 /* Use our own window for the size, unless it's very small. */
7229 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007230 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007231 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007232 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007233 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7234 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007235
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007236 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007237 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007238 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007239 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007240 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7241 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7242 }
7243
7244 /* Set dlgwidth to width of message.
7245 * Copy the message into "ga", changing NL to CR-NL and inserting line
7246 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 pstart = message;
7248 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007249 msgheight = 0;
7250 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 do
7252 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007253 msgheight += fontHeight; /* at least one line */
7254
7255 /* Need to figure out where to break the string. The system does it
7256 * at a word boundary, which would mean we can't compute the number of
7257 * wrapped lines. */
7258 textWidth = 0;
7259 last_white = NULL;
7260 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007261 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007262#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007263 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007264#else
7265 l = 1;
7266#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007267 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007268 && textWidth > maxDialogWidth * 3 / 4)
7269 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007270 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007271 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007272 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007273 /* Line will wrap. */
7274 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007275 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007276 textWidth = 0;
7277
7278 if (last_white != NULL)
7279 {
7280 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007282 pend = last_white + 1;
7283 last_white = NULL;
7284 }
7285 ga_append(&ga, '\r');
7286 ga_append(&ga, '\n');
7287 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007288 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007289
7290 while (--l >= 0)
7291 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007292 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007293 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007295
7296 ga_append(&ga, '\r');
7297 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 pstart = pend + 1;
7299 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007300
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007301 if (ga.ga_data != NULL)
7302 message = ga.ga_data;
7303
Bram Moolenaar748bf032005-02-02 23:04:36 +00007304 messageWidth += 10; /* roundoff space */
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007307 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7308 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309
7310 if (msgheight < DLG_ICON_HEIGHT)
7311 msgheight = DLG_ICON_HEIGHT;
7312
7313 /*
7314 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007315 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007317 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (!vertical)
7319 {
7320 // Place buttons horizontally if they fit.
7321 horizWidth = dlgPaddingX;
7322 pstart = tbuffer;
7323 i = 0;
7324 do
7325 {
7326 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7327 if (pend == NULL)
7328 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007329 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (textWidth < minButtonWidth)
7331 textWidth = minButtonWidth;
7332 textWidth += dlgPaddingX; /* Padding within button */
7333 buttonWidths[i] = textWidth;
7334 buttonPositions[i++] = horizWidth;
7335 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7336 pstart = pend + 1;
7337 } while (*pend != NUL);
7338
7339 if (horizWidth > maxDialogWidth)
7340 vertical = TRUE; // Too wide to fit on the screen.
7341 else if (horizWidth > dlgwidth)
7342 dlgwidth = horizWidth;
7343 }
7344
7345 if (vertical)
7346 {
7347 // Stack buttons vertically.
7348 pstart = tbuffer;
7349 do
7350 {
7351 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7352 if (pend == NULL)
7353 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007354 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 textWidth += dlgPaddingX; /* Padding within button */
7356 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7357 if (textWidth > dlgwidth)
7358 dlgwidth = textWidth;
7359 pstart = pend + 1;
7360 } while (*pend != NUL);
7361 }
7362
7363 if (dlgwidth < DLG_MIN_WIDTH)
7364 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7365
7366 /* start to fill in the dlgtemplate information. addressing by WORDs */
7367 if (s_usenewlook)
7368 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7369 else
7370 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7371
7372 add_long(lStyle);
7373 add_long(0); // (lExtendedStyle)
7374 pnumitems = p; /*save where the number of items must be stored*/
7375 add_word(0); // NumberOfItems(will change later)
7376 add_word(10); // x
7377 add_word(10); // y
7378 add_word(PixelToDialogX(dlgwidth)); // cx
7379
7380 // Dialog height.
7381 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007382 dlgheight = msgheight + 2 * dlgPaddingY
7383 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 else
7385 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7386
7387 // Dialog needs to be taller if contains an edit box.
7388 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7389 if (textfield != NULL)
7390 dlgheight += editboxheight;
7391
Bram Moolenaara95d8232013-08-07 15:27:11 +02007392 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7393 if (dlgheight > maxDialogHeight)
7394 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007395 msgheight = msgheight - (dlgheight - maxDialogHeight);
7396 dlgheight = maxDialogHeight;
7397 scroll_flag = WS_VSCROLL;
7398 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7399 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007400 }
7401
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 add_word(PixelToDialogY(dlgheight));
7403
7404 add_word(0); // Menu
7405 add_word(0); // Class
7406
7407 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007408 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7409 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 p += nchar;
7411
7412 if (s_usenewlook)
7413 {
7414 /* do the font, since DS_3DLOOK doesn't work properly */
7415#ifdef USE_SYSMENU_FONT
7416 if (use_lfSysmenu)
7417 {
7418 /* point size */
7419 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7420 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007421 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423 else
7424#endif
7425 {
7426 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007427 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429 p += nchar;
7430 }
7431
7432 buttonYpos = msgheight + 2 * dlgPaddingY;
7433
7434 if (textfield != NULL)
7435 buttonYpos += editboxheight;
7436
7437 pstart = tbuffer;
7438 if (!vertical)
7439 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7440 for (i = 0; i < numButtons; i++)
7441 {
7442 /* get end of this button. */
7443 for ( pend = pstart;
7444 *pend && (*pend != DLG_BUTTON_SEP);
7445 pend++)
7446 ;
7447
7448 if (*pend)
7449 *pend = '\0';
7450
7451 /*
7452 * old NOTE:
7453 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7454 * the focus to the first tab-able button and in so doing makes that
7455 * the default!! Grrr. Workaround: Make the default button the only
7456 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7457 * he/she can use arrow keys.
7458 *
7459 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007460 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 * dialog. Also needed for when the textfield is the default control.
7462 * It appears to work now (perhaps not on Win95?).
7463 */
7464 if (vertical)
7465 {
7466 p = add_dialog_element(p,
7467 (i == dfltbutton
7468 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7469 PixelToDialogX(DLG_VERT_PADDING_X),
7470 PixelToDialogY(buttonYpos /* TBK */
7471 + 2 * fontHeight * i),
7472 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7473 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007474 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476 else
7477 {
7478 p = add_dialog_element(p,
7479 (i == dfltbutton
7480 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7481 PixelToDialogX(horizWidth + buttonPositions[i]),
7482 PixelToDialogY(buttonYpos), /* TBK */
7483 PixelToDialogX(buttonWidths[i]),
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 pstart = pend + 1; /*next button*/
7488 }
7489 *pnumitems += numButtons;
7490
7491 /* Vim icon */
7492 p = add_dialog_element(p, SS_ICON,
7493 PixelToDialogX(dlgPaddingX),
7494 PixelToDialogY(dlgPaddingY),
7495 PixelToDialogX(DLG_ICON_WIDTH),
7496 PixelToDialogY(DLG_ICON_HEIGHT),
7497 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7498 dlg_icons[type]);
7499
Bram Moolenaar748bf032005-02-02 23:04:36 +00007500 /* Dialog message */
7501 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7502 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7503 PixelToDialogY(dlgPaddingY),
7504 (WORD)(PixelToDialogX(messageWidth) + 1),
7505 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007506 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
7508 /* Edit box */
7509 if (textfield != NULL)
7510 {
7511 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7512 PixelToDialogX(2 * dlgPaddingX),
7513 PixelToDialogY(2 * dlgPaddingY + msgheight),
7514 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7515 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007516 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 *pnumitems += 1;
7518 }
7519
7520 *pnumitems += 2;
7521
7522 SelectFont(hdc, oldFont);
7523 DeleteObject(font);
7524 ReleaseDC(hwnd, hdc);
7525
7526 /* Let the dialog_callback() function know which button to make default
7527 * If we have an edit box, make that the default. We also need to tell
7528 * dialog_callback() if this dialog contains an edit box or not. We do
7529 * this by setting s_textfield if it does.
7530 */
7531 if (textfield != NULL)
7532 {
7533 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7534 s_textfield = textfield;
7535 }
7536 else
7537 {
7538 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7539 s_textfield = NULL;
7540 }
7541
7542 /* show the dialog box modally and get a return value */
7543 nchar = (int)DialogBoxIndirect(
7544 s_hinst,
7545 (LPDLGTEMPLATE)pdlgtemplate,
7546 s_hwnd,
7547 (DLGPROC)dialog_callback);
7548
7549 LocalFree(LocalHandle(pdlgtemplate));
7550 vim_free(tbuffer);
7551 vim_free(buttonWidths);
7552 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007553 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
7555 /* Focus back to our window (for when MDI is used). */
7556 (void)SetFocus(s_hwnd);
7557
7558 return nchar;
7559}
7560
7561#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007562
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563/*
7564 * Put a simple element (basic class) onto a dialog template in memory.
7565 * return a pointer to where the next item should be added.
7566 *
7567 * parameters:
7568 * lStyle = additional style flags
7569 * (be careful, NT3.51 & Win32s will ignore the new ones)
7570 * x,y = x & y positions IN DIALOG UNITS
7571 * w,h = width and height IN DIALOG UNITS
7572 * Id = ID used in messages
7573 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7574 * caption = usually text or resource name
7575 *
7576 * TODO: use the length information noted here to enable the dialog creation
7577 * routines to work out more exactly how much memory they need to alloc.
7578 */
7579 static PWORD
7580add_dialog_element(
7581 PWORD p,
7582 DWORD lStyle,
7583 WORD x,
7584 WORD y,
7585 WORD w,
7586 WORD h,
7587 WORD Id,
7588 WORD clss,
7589 const char *caption)
7590{
7591 int nchar;
7592
7593 p = lpwAlign(p); /* Align to dword boundary*/
7594 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7595 *p++ = LOWORD(lStyle);
7596 *p++ = HIWORD(lStyle);
7597 *p++ = 0; // LOWORD (lExtendedStyle)
7598 *p++ = 0; // HIWORD (lExtendedStyle)
7599 *p++ = x;
7600 *p++ = y;
7601 *p++ = w;
7602 *p++ = h;
7603 *p++ = Id; //9 or 10 words in all
7604
7605 *p++ = (WORD)0xffff;
7606 *p++ = clss; //2 more here
7607
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007608 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 p += nchar;
7610
7611 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7612
7613 return p; //total = 15+ (strlen(caption)) words
7614 // = 30 + 2(strlen(caption) bytes reqd
7615}
7616
7617
7618/*
7619 * Helper routine. Take an input pointer, return closest pointer that is
7620 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7621 */
7622 static LPWORD
7623lpwAlign(
7624 LPWORD lpIn)
7625{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007626 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007628 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 ul += 3;
7630 ul >>= 2;
7631 ul <<= 2;
7632 return (LPWORD)ul;
7633}
7634
7635/*
7636 * Helper routine. Takes second parameter as Ansi string, copies it to first
7637 * parameter as wide character (16-bits / char) string, and returns integer
7638 * number of wide characters (words) in string (including the trailing wide
7639 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007640 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7641 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 static int
7643nCopyAnsiToWideChar(
7644 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007645 LPSTR lpAnsiIn,
7646 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
7648 int nChar = 0;
7649#ifdef FEAT_MBYTE
7650 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7651 int i;
7652 WCHAR *wn;
7653
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007654 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 {
7656 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007657 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 if (wn != NULL)
7659 {
7660 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007661 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 vim_free(wn);
7663 }
7664 }
7665 if (nChar == 0)
7666 /* Use Win32 conversion function. */
7667 nChar = MultiByteToWideChar(
7668 enc_codepage > 0 ? enc_codepage : CP_ACP,
7669 MB_PRECOMPOSED,
7670 lpAnsiIn, len,
7671 lpWCStr, len);
7672 for (i = 0; i < nChar; ++i)
7673 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7674 lpWCStr[i] = (WORD)' ';
7675#else
7676 do
7677 {
7678 if (*lpAnsiIn == '\t')
7679 *lpWCStr++ = (WORD)' ';
7680 else
7681 *lpWCStr++ = (WORD)*lpAnsiIn;
7682 nChar++;
7683 } while (*lpAnsiIn++);
7684#endif
7685
7686 return nChar;
7687}
7688
7689
7690#ifdef FEAT_TEAROFF
7691/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007692 * Lookup menu handle from "menu_id".
7693 */
7694 static HMENU
7695tearoff_lookup_menuhandle(
7696 vimmenu_T *menu,
7697 WORD menu_id)
7698{
7699 for ( ; menu != NULL; menu = menu->next)
7700 {
7701 if (menu->modes == 0) /* this menu has just been deleted */
7702 continue;
7703 if (menu_is_separator(menu->dname))
7704 continue;
7705 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7706 return menu->submenu_id;
7707 }
7708 return NULL;
7709}
7710
7711/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 * The callback function for all the modeless dialogs that make up the
7713 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7714 * thinking its menus have been clicked), and go away when closed.
7715 */
7716 static LRESULT CALLBACK
7717tearoff_callback(
7718 HWND hwnd,
7719 UINT message,
7720 WPARAM wParam,
7721 LPARAM lParam)
7722{
7723 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007724 {
7725 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 /* May show the mouse pointer again. */
7730 HandleMouseHide(message, lParam);
7731
7732 if (message == WM_COMMAND)
7733 {
7734 if ((WORD)(LOWORD(wParam)) & 0x8000)
7735 {
7736 POINT mp;
7737 RECT rect;
7738
7739 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7740 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007741 vimmenu_T *menu;
7742
7743 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007745 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7747 (int)rect.right - 8,
7748 (int)mp.y,
7749 (int)0, /*reserved param*/
7750 s_hwnd,
7751 NULL);
7752 /*
7753 * NOTE: The pop-up menu can eat the mouse up event.
7754 * We deal with this in normal.c.
7755 */
7756 }
7757 }
7758 else
7759 /* Pass on messages to the main Vim window */
7760 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7761 /*
7762 * Give main window the focus back: this is so after
7763 * choosing a tearoff button you can start typing again
7764 * straight away.
7765 */
7766 (void)SetFocus(s_hwnd);
7767 return TRUE;
7768 }
7769 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7770 {
7771 DestroyWindow(hwnd);
7772 return TRUE;
7773 }
7774
7775 /* When moved around, give main window the focus back. */
7776 if (message == WM_EXITSIZEMOVE)
7777 (void)SetActiveWindow(s_hwnd);
7778
7779 return FALSE;
7780}
7781#endif
7782
7783
7784/*
7785 * Decide whether to use the "new look" (small, non-bold font) or the "old
7786 * look" (big, clanky font) for dialogs, and work out a few values for use
7787 * later accordingly.
7788 */
7789 static void
7790get_dialog_font_metrics(void)
7791{
7792 HDC hdc;
7793 HFONT hfontTools = 0;
7794 DWORD dlgFontSize;
7795 SIZE size;
7796#ifdef USE_SYSMENU_FONT
7797 LOGFONT lfSysmenu;
7798#endif
7799
7800 s_usenewlook = FALSE;
7801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007803 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7804 hfontTools = CreateFontIndirect(&lfSysmenu);
7805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806#endif
7807 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7808 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7809
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007810 if (hfontTools)
7811 {
7812 hdc = GetDC(s_hwnd);
7813 SelectObject(hdc, hfontTools);
7814 /*
7815 * GetTextMetrics() doesn't return the right value in
7816 * tmAveCharWidth, so we have to figure out the dialog base units
7817 * ourselves.
7818 */
7819 GetTextExtentPoint(hdc,
7820 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7821 52, &size);
7822 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007824 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7825 s_dlgfntheight = (WORD)size.cy;
7826 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828
7829 if (!s_usenewlook)
7830 {
7831 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7832 s_dlgfntwidth = LOWORD(dlgFontSize);
7833 s_dlgfntheight = HIWORD(dlgFontSize);
7834 }
7835}
7836
7837#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7838/*
7839 * Create a pseudo-"tearoff menu" based on the child
7840 * items of a given menu pointer.
7841 */
7842 static void
7843gui_mch_tearoff(
7844 char_u *title,
7845 vimmenu_T *menu,
7846 int initX,
7847 int initY)
7848{
7849 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7850 int template_len;
7851 int nchar, textWidth, submenuWidth;
7852 DWORD lStyle;
7853 DWORD lExtendedStyle;
7854 WORD dlgwidth;
7855 WORD menuID;
7856 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007857 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 vimmenu_T *the_menu = menu;
7859 HWND hwnd;
7860 HDC hdc;
7861 HFONT font, oldFont;
7862 int col, spaceWidth, len;
7863 int columnWidths[2];
7864 char_u *label, *text;
7865 int acLen = 0;
7866 int nameLen;
7867 int padding0, padding1, padding2 = 0;
7868 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007869 int x;
7870 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871#ifdef USE_SYSMENU_FONT
7872 LOGFONT lfSysmenu;
7873 int use_lfSysmenu = FALSE;
7874#endif
7875
7876 /*
7877 * If this menu is already torn off, move it to the mouse position.
7878 */
7879 if (IsWindow(menu->tearoff_handle))
7880 {
7881 POINT mp;
7882 if (GetCursorPos((LPPOINT)&mp))
7883 {
7884 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7885 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7886 }
7887 return;
7888 }
7889
7890 /*
7891 * Create a new tearoff.
7892 */
7893 if (*title == MNU_HIDDEN_CHAR)
7894 title++;
7895
7896 /* Allocate memory to store the dialog template. It's made bigger when
7897 * needed. */
7898 template_len = DLG_ALLOC_SIZE;
7899 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7900 if (p == NULL)
7901 return;
7902
7903 hwnd = GetDesktopWindow();
7904 hdc = GetWindowDC(hwnd);
7905#ifdef USE_SYSMENU_FONT
7906 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7907 {
7908 font = CreateFontIndirect(&lfSysmenu);
7909 use_lfSysmenu = TRUE;
7910 }
7911 else
7912#endif
7913 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7914 VARIABLE_PITCH , DLG_FONT_NAME);
7915 if (s_usenewlook)
7916 oldFont = SelectFont(hdc, font);
7917 else
7918 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7919
7920 /* Calculate width of a single space. Used for padding columns to the
7921 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007922 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924 /* Figure out max width of the text column, the accelerator column and the
7925 * optional submenu column. */
7926 submenuWidth = 0;
7927 for (col = 0; col < 2; col++)
7928 {
7929 columnWidths[col] = 0;
7930 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7931 {
7932 /* Use "dname" here to compute the width of the visible text. */
7933 text = (col == 0) ? pmenu->dname : pmenu->actext;
7934 if (text != NULL && *text != NUL)
7935 {
7936 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7937 if (textWidth > columnWidths[col])
7938 columnWidths[col] = textWidth;
7939 }
7940 if (pmenu->children != NULL)
7941 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7942 }
7943 }
7944 if (columnWidths[1] == 0)
7945 {
7946 /* no accelerators */
7947 if (submenuWidth != 0)
7948 columnWidths[0] += submenuWidth;
7949 else
7950 columnWidths[0] += spaceWidth;
7951 }
7952 else
7953 {
7954 /* there is an accelerator column */
7955 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7956 columnWidths[1] += submenuWidth;
7957 }
7958
7959 /*
7960 * Now find the total width of our 'menu'.
7961 */
7962 textWidth = columnWidths[0] + columnWidths[1];
7963 if (submenuWidth != 0)
7964 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007965 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7967 textWidth += submenuWidth;
7968 }
7969 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7970 if (textWidth > dlgwidth)
7971 dlgwidth = textWidth;
7972 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7973
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 /* start to fill in the dlgtemplate information. addressing by WORDs */
7975 if (s_usenewlook)
7976 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7977 else
7978 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7979
7980 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7981 *p++ = LOWORD(lStyle);
7982 *p++ = HIWORD(lStyle);
7983 *p++ = LOWORD(lExtendedStyle);
7984 *p++ = HIWORD(lExtendedStyle);
7985 pnumitems = p; /* save where the number of items must be stored */
7986 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007987 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007989 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 else
7991 *p++ = PixelToDialogX(initX); // x
7992 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007993 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 else
7995 *p++ = PixelToDialogY(initY); // y
7996 *p++ = PixelToDialogX(dlgwidth); // cx
7997 ptrueheight = p;
7998 *p++ = 0; // dialog height: changed later anyway
7999 *p++ = 0; // Menu
8000 *p++ = 0; // Class
8001
8002 /* copy the title of the dialog */
8003 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008004 ? (LPSTR)title
8005 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 p += nchar;
8007
8008 if (s_usenewlook)
8009 {
8010 /* do the font, since DS_3DLOOK doesn't work properly */
8011#ifdef USE_SYSMENU_FONT
8012 if (use_lfSysmenu)
8013 {
8014 /* point size */
8015 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8016 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008017 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 }
8019 else
8020#endif
8021 {
8022 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008023 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 p += nchar;
8026 }
8027
8028 /*
8029 * Loop over all the items in the menu.
8030 * But skip over the tearbar.
8031 */
8032 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8033 menu = menu->children->next;
8034 else
8035 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008036 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 for ( ; menu != NULL; menu = menu->next)
8038 {
8039 if (menu->modes == 0) /* this menu has just been deleted */
8040 continue;
8041 if (menu_is_separator(menu->dname))
8042 {
8043 sepPadding += 3;
8044 continue;
8045 }
8046
8047 /* Check if there still is plenty of room in the template. Make it
8048 * larger when needed. */
8049 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8050 {
8051 WORD *newp;
8052
8053 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8054 if (newp != NULL)
8055 {
8056 template_len += 4096;
8057 mch_memmove(newp, pdlgtemplate,
8058 (char *)p - (char *)pdlgtemplate);
8059 p = newp + (p - pdlgtemplate);
8060 pnumitems = newp + (pnumitems - pdlgtemplate);
8061 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8062 LocalFree(LocalHandle(pdlgtemplate));
8063 pdlgtemplate = newp;
8064 }
8065 }
8066
8067 /* Figure out minimal length of this menu label. Use "name" for the
8068 * actual text, "dname" for estimating the displayed size. "name"
8069 * has "&a" for mnemonic and includes the accelerator. */
8070 len = nameLen = (int)STRLEN(menu->name);
8071 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8072 (int)STRLEN(menu->dname))) / spaceWidth;
8073 len += padding0;
8074
8075 if (menu->actext != NULL)
8076 {
8077 acLen = (int)STRLEN(menu->actext);
8078 len += acLen;
8079 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8080 }
8081 else
8082 textWidth = 0;
8083 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8084 len += padding1;
8085
8086 if (menu->children == NULL)
8087 {
8088 padding2 = submenuWidth / spaceWidth;
8089 len += padding2;
8090 menuID = (WORD)(menu->id);
8091 }
8092 else
8093 {
8094 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008095 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 }
8097
8098 /* Allocate menu label and fill it in */
8099 text = label = alloc((unsigned)len + 1);
8100 if (label == NULL)
8101 break;
8102
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008103 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8105 if (text == NULL)
8106 text = label + nameLen; /* no actext, use whole name */
8107 while (padding0-- > 0)
8108 *text++ = ' ';
8109 if (menu->actext != NULL)
8110 {
8111 STRNCPY(text, menu->actext, acLen);
8112 text += acLen;
8113 }
8114 while (padding1-- > 0)
8115 *text++ = ' ';
8116 if (menu->children != NULL)
8117 {
8118 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8119 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8120 }
8121 else
8122 {
8123 while (padding2-- > 0)
8124 *text++ = ' ';
8125 }
8126 *text = NUL;
8127
8128 /*
8129 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8130 * W95/NT4 it makes the tear-off look more like a menu.
8131 */
8132 p = add_dialog_element(p,
8133 BS_PUSHBUTTON|BS_LEFT,
8134 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8135 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8136 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8137 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008138 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 vim_free(label);
8140 (*pnumitems)++;
8141 }
8142
8143 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8144
8145
8146 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008147 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 s_hinst,
8149 (LPDLGTEMPLATE)pdlgtemplate,
8150 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008151 (DLGPROC)tearoff_callback,
8152 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153
8154 LocalFree(LocalHandle(pdlgtemplate));
8155 SelectFont(hdc, oldFont);
8156 DeleteObject(font);
8157 ReleaseDC(hwnd, hdc);
8158
8159 /*
8160 * Reassert ourselves as the active window. This is so that after creating
8161 * a tearoff, the user doesn't have to click with the mouse just to start
8162 * typing again!
8163 */
8164 (void)SetActiveWindow(s_hwnd);
8165
8166 /* make sure the right buttons are enabled */
8167 force_menu_update = TRUE;
8168}
8169#endif
8170
8171#if defined(FEAT_TOOLBAR) || defined(PROTO)
8172#include "gui_w32_rc.h"
8173
8174/* This not defined in older SDKs */
8175# ifndef TBSTYLE_FLAT
8176# define TBSTYLE_FLAT 0x0800
8177# endif
8178
8179/*
8180 * Create the toolbar, initially unpopulated.
8181 * (just like the menu, there are no defaults, it's all
8182 * set up through menu.vim)
8183 */
8184 static void
8185initialise_toolbar(void)
8186{
8187 InitCommonControls();
8188 s_toolbarhwnd = CreateToolbarEx(
8189 s_hwnd,
8190 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8191 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008192 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 s_hinst,
8194 IDR_TOOLBAR1, // id of initial bitmap
8195 NULL,
8196 0, // initial number of buttons
8197 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8198 TOOLBAR_BUTTON_HEIGHT,
8199 TOOLBAR_BUTTON_WIDTH,
8200 TOOLBAR_BUTTON_HEIGHT,
8201 sizeof(TBBUTTON)
8202 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008203 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8206}
8207
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008208 static LRESULT CALLBACK
8209toolbar_wndproc(
8210 HWND hwnd,
8211 UINT uMsg,
8212 WPARAM wParam,
8213 LPARAM lParam)
8214{
8215 HandleMouseHide(uMsg, lParam);
8216 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8217}
8218
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 static int
8220get_toolbar_bitmap(vimmenu_T *menu)
8221{
8222 int i = -1;
8223
8224 /*
8225 * Check user bitmaps first, unless builtin is specified.
8226 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008227 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
8229 char_u fname[MAXPATHL];
8230 HANDLE hbitmap = NULL;
8231
8232 if (menu->iconfile != NULL)
8233 {
8234 gui_find_iconfile(menu->iconfile, fname, "bmp");
8235 hbitmap = LoadImage(
8236 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008237 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 IMAGE_BITMAP,
8239 TOOLBAR_BUTTON_WIDTH,
8240 TOOLBAR_BUTTON_HEIGHT,
8241 LR_LOADFROMFILE |
8242 LR_LOADMAP3DCOLORS
8243 );
8244 }
8245
8246 /*
8247 * If the LoadImage call failed, or the "icon=" file
8248 * didn't exist or wasn't specified, try the menu name
8249 */
8250 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008251 && (gui_find_bitmap(
8252#ifdef FEAT_MULTI_LANG
8253 menu->en_dname != NULL ? menu->en_dname :
8254#endif
8255 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 hbitmap = LoadImage(
8257 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008258 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 IMAGE_BITMAP,
8260 TOOLBAR_BUTTON_WIDTH,
8261 TOOLBAR_BUTTON_HEIGHT,
8262 LR_LOADFROMFILE |
8263 LR_LOADMAP3DCOLORS
8264 );
8265
8266 if (hbitmap != NULL)
8267 {
8268 TBADDBITMAP tbAddBitmap;
8269
8270 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008271 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
8273 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8274 (WPARAM)1, (LPARAM)&tbAddBitmap);
8275 /* i will be set to -1 if it fails */
8276 }
8277 }
8278 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8279 i = menu->iconidx;
8280
8281 return i;
8282}
8283#endif
8284
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008285#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8286 static void
8287initialise_tabline(void)
8288{
8289 InitCommonControls();
8290
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008291 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008292 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008293 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8294 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008295 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008296
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008297 gui.tabline_height = TABLINE_HEIGHT;
8298
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008299# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008300 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008301# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008302}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008303
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008304/*
8305 * Get tabpage_T from POINT.
8306 */
8307 static tabpage_T *
8308GetTabFromPoint(
8309 HWND hWnd,
8310 POINT pt)
8311{
8312 tabpage_T *ptp = NULL;
8313
8314 if (gui_mch_showing_tabline())
8315 {
8316 TCHITTESTINFO htinfo;
8317 htinfo.pt = pt;
8318 /* ignore if a window under cusor is not tabcontrol. */
8319 if (s_tabhwnd == hWnd)
8320 {
8321 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8322 if (idx != -1)
8323 ptp = find_tabpage(idx + 1);
8324 }
8325 }
8326 return ptp;
8327}
8328
8329static POINT s_pt = {0, 0};
8330static HCURSOR s_hCursor = NULL;
8331
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008332 static LRESULT CALLBACK
8333tabline_wndproc(
8334 HWND hwnd,
8335 UINT uMsg,
8336 WPARAM wParam,
8337 LPARAM lParam)
8338{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008339 POINT pt;
8340 tabpage_T *tp;
8341 RECT rect;
8342 int nCenter;
8343 int idx0;
8344 int idx1;
8345
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008346 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008347
8348 switch (uMsg)
8349 {
8350 case WM_LBUTTONDOWN:
8351 {
8352 s_pt.x = GET_X_LPARAM(lParam);
8353 s_pt.y = GET_Y_LPARAM(lParam);
8354 SetCapture(hwnd);
8355 s_hCursor = GetCursor(); /* backup default cursor */
8356 break;
8357 }
8358 case WM_MOUSEMOVE:
8359 if (GetCapture() == hwnd
8360 && ((wParam & MK_LBUTTON)) != 0)
8361 {
8362 pt.x = GET_X_LPARAM(lParam);
8363 pt.y = s_pt.y;
8364 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8365 {
8366 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8367
8368 tp = GetTabFromPoint(hwnd, pt);
8369 if (tp != NULL)
8370 {
8371 idx0 = tabpage_index(curtab) - 1;
8372 idx1 = tabpage_index(tp) - 1;
8373
8374 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8375 nCenter = rect.left + (rect.right - rect.left) / 2;
8376
8377 /* Check if the mouse cursor goes over the center of
8378 * the next tab to prevent "flickering". */
8379 if ((idx0 < idx1) && (nCenter < pt.x))
8380 {
8381 tabpage_move(idx1 + 1);
8382 update_screen(0);
8383 }
8384 else if ((idx1 < idx0) && (pt.x < nCenter))
8385 {
8386 tabpage_move(idx1);
8387 update_screen(0);
8388 }
8389 }
8390 }
8391 }
8392 break;
8393 case WM_LBUTTONUP:
8394 {
8395 if (GetCapture() == hwnd)
8396 {
8397 SetCursor(s_hCursor);
8398 ReleaseCapture();
8399 }
8400 break;
8401 }
8402 default:
8403 break;
8404 }
8405
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008406 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8407}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008408#endif
8409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8411/*
8412 * Make the GUI window come to the foreground.
8413 */
8414 void
8415gui_mch_set_foreground(void)
8416{
8417 if (IsIconic(s_hwnd))
8418 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8419 SetForegroundWindow(s_hwnd);
8420}
8421#endif
8422
8423#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8424 static void
8425dyn_imm_load(void)
8426{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008427 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 if (hLibImm == NULL)
8429 return;
8430
8431 pImmGetCompositionStringA
8432 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8433 pImmGetCompositionStringW
8434 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8435 pImmGetContext
8436 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8437 pImmAssociateContext
8438 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8439 pImmReleaseContext
8440 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8441 pImmGetOpenStatus
8442 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8443 pImmSetOpenStatus
8444 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8445 pImmGetCompositionFont
8446 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8447 pImmSetCompositionFont
8448 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8449 pImmSetCompositionWindow
8450 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8451 pImmGetConversionStatus
8452 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008453 pImmSetConversionStatus
8454 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455
8456 if ( pImmGetCompositionStringA == NULL
8457 || pImmGetCompositionStringW == NULL
8458 || pImmGetContext == NULL
8459 || pImmAssociateContext == NULL
8460 || pImmReleaseContext == NULL
8461 || pImmGetOpenStatus == NULL
8462 || pImmSetOpenStatus == NULL
8463 || pImmGetCompositionFont == NULL
8464 || pImmSetCompositionFont == NULL
8465 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008466 || pImmGetConversionStatus == NULL
8467 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 {
8469 FreeLibrary(hLibImm);
8470 hLibImm = NULL;
8471 pImmGetContext = NULL;
8472 return;
8473 }
8474
8475 return;
8476}
8477
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478#endif
8479
8480#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8481
8482# ifdef FEAT_XPM_W32
8483# define IMAGE_XPM 100
8484# endif
8485
8486typedef struct _signicon_t
8487{
8488 HANDLE hImage;
8489 UINT uType;
8490#ifdef FEAT_XPM_W32
8491 HANDLE hShape; /* Mask bitmap handle */
8492#endif
8493} signicon_t;
8494
8495 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008496gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497{
8498 signicon_t *sign;
8499 int x, y, w, h;
8500
8501 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8502 return;
8503
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008504#if defined(FEAT_DIRECTX)
8505 if (IS_ENABLE_DIRECTX())
8506 DWriteContext_Flush(s_dwc);
8507#endif
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 x = TEXT_X(col);
8510 y = TEXT_Y(row);
8511 w = gui.char_width * 2;
8512 h = gui.char_height;
8513 switch (sign->uType)
8514 {
8515 case IMAGE_BITMAP:
8516 {
8517 HDC hdcMem;
8518 HBITMAP hbmpOld;
8519
8520 hdcMem = CreateCompatibleDC(s_hdc);
8521 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8522 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8523 SelectObject(hdcMem, hbmpOld);
8524 DeleteDC(hdcMem);
8525 }
8526 break;
8527 case IMAGE_ICON:
8528 case IMAGE_CURSOR:
8529 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8530 break;
8531#ifdef FEAT_XPM_W32
8532 case IMAGE_XPM:
8533 {
8534 HDC hdcMem;
8535 HBITMAP hbmpOld;
8536
8537 hdcMem = CreateCompatibleDC(s_hdc);
8538 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8539 /* Make hole */
8540 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8541
8542 SelectObject(hdcMem, sign->hImage);
8543 /* Paint sign */
8544 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8545 SelectObject(hdcMem, hbmpOld);
8546 DeleteDC(hdcMem);
8547 }
8548 break;
8549#endif
8550 }
8551}
8552
8553 static void
8554close_signicon_image(signicon_t *sign)
8555{
8556 if (sign)
8557 switch (sign->uType)
8558 {
8559 case IMAGE_BITMAP:
8560 DeleteObject((HGDIOBJ)sign->hImage);
8561 break;
8562 case IMAGE_CURSOR:
8563 DestroyCursor((HCURSOR)sign->hImage);
8564 break;
8565 case IMAGE_ICON:
8566 DestroyIcon((HICON)sign->hImage);
8567 break;
8568#ifdef FEAT_XPM_W32
8569 case IMAGE_XPM:
8570 DeleteObject((HBITMAP)sign->hImage);
8571 DeleteObject((HBITMAP)sign->hShape);
8572 break;
8573#endif
8574 }
8575}
8576
8577 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008578gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 signicon_t sign, *psign;
8581 char_u *ext;
8582
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008584 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (ext > signfile)
8586 {
8587 int do_load = 1;
8588
8589 if (!STRICMP(ext, ".bmp"))
8590 sign.uType = IMAGE_BITMAP;
8591 else if (!STRICMP(ext, ".ico"))
8592 sign.uType = IMAGE_ICON;
8593 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8594 sign.uType = IMAGE_CURSOR;
8595 else
8596 do_load = 0;
8597
8598 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008599 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 gui.char_width * 2, gui.char_height,
8601 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8602#ifdef FEAT_XPM_W32
8603 if (!STRICMP(ext, ".xpm"))
8604 {
8605 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008606 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8607 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609#endif
8610 }
8611
8612 psign = NULL;
8613 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8614 != NULL)
8615 *psign = sign;
8616
8617 if (!psign)
8618 {
8619 if (sign.hImage)
8620 close_signicon_image(&sign);
8621 EMSG(_(e_signdata));
8622 }
8623 return (void *)psign;
8624
8625}
8626
8627 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008628gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 if (sign)
8631 {
8632 close_signicon_image((signicon_t *)sign);
8633 vim_free(sign);
8634 }
8635}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008638#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008641 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008643 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8645 * to get current mouse position).
8646 *
8647 * Trying to use as more Windows services as possible, and as less
8648 * IE version as possible :)).
8649 *
8650 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8651 * BalloonEval struct.
8652 * 2) Enable/Disable simply create/kill BalloonEval Timer
8653 * 3) When there was enough inactivity, timer procedure posts
8654 * async request to debugger
8655 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8656 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008657 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 */
8659
Bram Moolenaar45360022005-07-21 21:08:21 +00008660/*
8661 * determine whether installed Common Controls support multiline tooltips
8662 * (i.e. their version is >= 4.70
8663 */
8664 int
8665multiline_balloon_available(void)
8666{
8667 HINSTANCE hDll;
8668 static char comctl_dll[] = "comctl32.dll";
8669 static int multiline_tip = MAYBE;
8670
8671 if (multiline_tip != MAYBE)
8672 return multiline_tip;
8673
8674 hDll = GetModuleHandle(comctl_dll);
8675 if (hDll != NULL)
8676 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008677 DLLGETVERSIONPROC pGetVer;
8678 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008679
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008680 if (pGetVer != NULL)
8681 {
8682 DLLVERSIONINFO dvi;
8683 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008684
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008685 ZeroMemory(&dvi, sizeof(dvi));
8686 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008687
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008688 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008689
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008690 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008691 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008692 || (dvi.dwMajorVersion == 4
8693 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008694 {
8695 multiline_tip = TRUE;
8696 return multiline_tip;
8697 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008698 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008699 else
8700 {
8701 /* there is chance we have ancient CommCtl 4.70
8702 which doesn't export DllGetVersion */
8703 DWORD dwHandle = 0;
8704 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8705 if (len > 0)
8706 {
8707 VS_FIXEDFILEINFO *ver;
8708 UINT vlen = 0;
8709 void *data = alloc(len);
8710
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008711 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008712 && GetFileVersionInfo(comctl_dll, 0, len, data)
8713 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8714 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008715 && HIWORD(ver->dwFileVersionMS) > 4)
8716 || ((HIWORD(ver->dwFileVersionMS) == 4
8717 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008718 {
8719 vim_free(data);
8720 multiline_tip = TRUE;
8721 return multiline_tip;
8722 }
8723 vim_free(data);
8724 }
8725 }
8726 }
8727 multiline_tip = FALSE;
8728 return multiline_tip;
8729}
8730
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008732make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
Bram Moolenaar45360022005-07-21 21:08:21 +00008734 TOOLINFO *pti;
8735 int ToolInfoSize;
8736
8737 if (multiline_balloon_available() == TRUE)
8738 ToolInfoSize = sizeof(TOOLINFO_NEW);
8739 else
8740 ToolInfoSize = sizeof(TOOLINFO);
8741
8742 pti = (TOOLINFO *)alloc(ToolInfoSize);
8743 if (pti == NULL)
8744 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745
8746 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8747 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8748 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8749 beval->target, NULL, s_hinst, NULL);
8750
8751 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8752 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8753
Bram Moolenaar45360022005-07-21 21:08:21 +00008754 pti->cbSize = ToolInfoSize;
8755 pti->uFlags = TTF_SUBCLASS;
8756 pti->hwnd = beval->target;
8757 pti->hinst = 0; /* Don't use string resources */
8758 pti->uId = ID_BEVAL_TOOLTIP;
8759
8760 if (multiline_balloon_available() == TRUE)
8761 {
8762 RECT rect;
8763 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8764 pti->lpszText = LPSTR_TEXTCALLBACK;
8765 ptin->lParam = (LPARAM)text;
8766 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8767 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8768 (LPARAM)rect.right);
8769 }
8770 else
8771 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008774 pti->rect.left = pt.x - 3;
8775 pti->rect.top = pt.y - 3;
8776 pti->rect.right = pt.x + 3;
8777 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
Bram Moolenaar45360022005-07-21 21:08:21 +00008779 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 /* Make tooltip appear sooner */
8781 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008782 /* I've performed some tests and it seems the longest possible life time
8783 * of tooltip is 30 seconds */
8784 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 /*
8786 * HACK: force tooltip to appear, because it'll not appear until
8787 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008788 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008790 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008792 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008796delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008798 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
8801 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008802BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008803 HWND hwnd UNUSED,
8804 UINT uMsg UNUSED,
8805 UINT_PTR idEvent UNUSED,
8806 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
8808 POINT pt;
8809 RECT rect;
8810
8811 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8812 return;
8813
8814 GetCursorPos(&pt);
8815 if (WindowFromPoint(pt) != s_textArea)
8816 return;
8817
8818 ScreenToClient(s_textArea, &pt);
8819 GetClientRect(s_textArea, &rect);
8820 if (!PtInRect(&rect, pt))
8821 return;
8822
8823 if (LastActivity > 0
8824 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8825 && (cur_beval->showState != ShS_PENDING
8826 || abs(cur_beval->x - pt.x) > 3
8827 || abs(cur_beval->y - pt.y) > 3))
8828 {
8829 /* Pointer resting in one place long enough, it's time to show
8830 * the tooltip. */
8831 cur_beval->showState = ShS_PENDING;
8832 cur_beval->x = pt.x;
8833 cur_beval->y = pt.y;
8834
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008835 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
8837 if (cur_beval->msgCB != NULL)
8838 (*cur_beval->msgCB)(cur_beval, 0);
8839 }
8840}
8841
8842 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008843gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008845 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008847 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848}
8849
8850 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008851gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008853 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (beval == NULL)
8855 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008856 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008857 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008858 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859}
8860
8861 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008862gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
8864 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008865
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008866 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 if (beval->showState == ShS_SHOWING)
8868 return;
8869 GetCursorPos(&pt);
8870 ScreenToClient(s_textArea, &pt);
8871
8872 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008874 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 gui_mch_disable_beval_area(cur_beval);
8876 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008877 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008879 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880}
8881
8882 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008883gui_mch_create_beval_area(
8884 void *target, /* ignored, always use s_textArea */
8885 char_u *mesg,
8886 void (*mesgCB)(BalloonEval *, int),
8887 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 /* partially stolen from gui_beval.c */
8890 BalloonEval *beval;
8891
8892 if (mesg != NULL && mesgCB != NULL)
8893 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008894 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 return NULL;
8896 }
8897
8898 beval = (BalloonEval *)alloc(sizeof(BalloonEval));
8899 if (beval != NULL)
8900 {
8901 beval->target = s_textArea;
8902 beval->balloon = NULL;
8903
8904 beval->showState = ShS_NEUTRAL;
8905 beval->x = 0;
8906 beval->y = 0;
8907 beval->msg = mesg;
8908 beval->msgCB = mesgCB;
8909 beval->clientData = clientData;
8910
8911 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 cur_beval = beval;
8913
8914 if (p_beval)
8915 gui_mch_enable_beval_area(beval);
8916
8917 }
8918 return beval;
8919}
8920
8921 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008922Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923{
8924 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8925 return;
8926
8927 if (cur_beval != NULL)
8928 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008929 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008931 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008932 // TRACE0("TTN_SHOW {{{");
8933 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008934 break;
8935 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008936 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 delete_tooltip(cur_beval);
8938 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008939 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940
8941 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008942 break;
8943 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008944 {
8945 /* if you get there then we have new common controls */
8946 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8947 info->lpszText = (LPSTR)info->lParam;
8948 info->uFlags |= TTF_DI_SETITEM;
8949 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008950 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952 }
8953}
8954
8955 static void
8956TrackUserActivity(UINT uMsg)
8957{
8958 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8959 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8960 LastActivity = GetTickCount();
8961}
8962
8963 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008964gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 vim_free(beval);
8967}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008968#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969
8970#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8971/*
8972 * We have multiple signs to draw at the same location. Draw the
8973 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8974 */
8975 void
8976netbeans_draw_multisign_indicator(int row)
8977{
8978 int i;
8979 int y;
8980 int x;
8981
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008982 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008983 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008984
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 x = 0;
8986 y = TEXT_Y(row);
8987
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008988#if defined(FEAT_DIRECTX)
8989 if (IS_ENABLE_DIRECTX())
8990 DWriteContext_Flush(s_dwc);
8991#endif
8992
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 for (i = 0; i < gui.char_height - 3; i++)
8994 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8995
8996 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8997 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8998 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8999 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9000 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9001 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9002 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9003}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009004#endif