blob: 95fe0cbd6d1396282bcad36924e152a970c8f599 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaar7f88b652017-12-14 13:15:19 +010033# ifndef FEAT_MBYTE
34# error FEAT_MBYTE is required for FEAT_DIRECTX.
35# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020036static DWriteContext *s_dwc = NULL;
37static int s_directx_enabled = 0;
38static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010039# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010040static int directx_enabled(void);
41static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010042#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020043
Bram Moolenaar065bbac2016-02-20 13:08:46 +010044#ifdef FEAT_MENU
45static int gui_mswin_get_menu_height(int fix_window);
46#endif
47
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020048#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
49 int
50gui_mch_set_rendering_options(char_u *s)
51{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010052# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053 char_u *p, *q;
54
55 int dx_enable = 0;
56 int dx_flags = 0;
57 float dx_gamma = 0.0f;
58 float dx_contrast = 0.0f;
59 float dx_level = 0.0f;
60 int dx_geom = 0;
61 int dx_renmode = 0;
62 int dx_taamode = 0;
63
64 /* parse string as rendering options. */
65 for (p = s; p != NULL && *p != NUL; )
66 {
67 char_u item[256];
68 char_u name[128];
69 char_u value[128];
70
Bram Moolenaarcde88542015-08-11 19:14:00 +020071 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020072 if (p == NULL)
73 break;
74 q = &item[0];
75 copy_option_part(&q, name, sizeof(name), ":");
76 if (q == NULL)
77 return FAIL;
78 copy_option_part(&q, value, sizeof(value), ":");
79
80 if (STRCMP(name, "type") == 0)
81 {
82 if (STRCMP(value, "directx") == 0)
83 dx_enable = 1;
84 else
85 return FAIL;
86 }
87 else if (STRCMP(name, "gamma") == 0)
88 {
89 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010090 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020091 }
92 else if (STRCMP(name, "contrast") == 0)
93 {
94 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "level") == 0)
98 {
99 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "geom") == 0)
103 {
104 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 if (dx_geom < 0 || dx_geom > 2)
107 return FAIL;
108 }
109 else if (STRCMP(name, "renmode") == 0)
110 {
111 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100112 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200113 if (dx_renmode < 0 || dx_renmode > 6)
114 return FAIL;
115 }
116 else if (STRCMP(name, "taamode") == 0)
117 {
118 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100119 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200120 if (dx_taamode < 0 || dx_taamode > 3)
121 return FAIL;
122 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 else if (STRCMP(name, "scrlines") == 0)
124 {
Bram Moolenaara338adc2018-01-31 20:51:47 +0100125 /* Deprecated. Simply ignore it. */
Bram Moolenaar92467d32017-12-05 13:22:16 +0100126 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200127 else
128 return FAIL;
129 }
130
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100131 if (!gui.in_use)
132 return OK; /* only checking the syntax of the value */
133
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200134 /* Enable DirectX/DirectWrite */
135 if (dx_enable)
136 {
137 if (!directx_enabled())
138 return FAIL;
139 DWriteContext_SetRenderingParams(s_dwc, NULL);
140 if (dx_flags)
141 {
142 DWriteRenderingParams param;
143 DWriteContext_GetRenderingParams(s_dwc, &param);
144 if (dx_flags & (1 << 0))
145 param.gamma = dx_gamma;
146 if (dx_flags & (1 << 1))
147 param.enhancedContrast = dx_contrast;
148 if (dx_flags & (1 << 2))
149 param.clearTypeLevel = dx_level;
150 if (dx_flags & (1 << 3))
151 param.pixelGeometry = dx_geom;
152 if (dx_flags & (1 << 4))
153 param.renderingMode = dx_renmode;
154 if (dx_flags & (1 << 5))
155 param.textAntialiasMode = dx_taamode;
156 DWriteContext_SetRenderingParams(s_dwc, &param);
157 }
158 }
159 s_directx_enabled = dx_enable;
160
161 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100162# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200163 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100164# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200165}
166#endif
167
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168/*
169 * These are new in Windows ME/XP, only defined in recent compilers.
170 */
171#ifndef HANDLE_WM_XBUTTONUP
172# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
173 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
174#endif
175#ifndef HANDLE_WM_XBUTTONDOWN
176# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
177 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
178#endif
179#ifndef HANDLE_WM_XBUTTONDBLCLK
180# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
181 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
182#endif
183
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100184
185#include "version.h" /* used by dialog box routine for default title */
186#ifdef DEBUG
187# include <tchar.h>
188#endif
189
190/* cproto fails on missing include files */
191#ifndef PROTO
192
193#ifndef __MINGW32__
194# include <shellapi.h>
195#endif
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100196#if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197# include <commctrl.h>
198#endif
199#include <windowsx.h>
200
201#ifdef GLOBAL_IME
202# include "glbl_ime.h"
203#endif
204
205#endif /* PROTO */
206
207#ifdef FEAT_MENU
208# define MENUHINTS /* show menu hints in command line */
209#endif
210
211/* Some parameters for dialog boxes. All in pixels. */
212#define DLG_PADDING_X 10
213#define DLG_PADDING_Y 10
214#define DLG_OLD_STYLE_PADDING_X 5
215#define DLG_OLD_STYLE_PADDING_Y 5
216#define DLG_VERT_PADDING_X 4 /* For vertical buttons */
217#define DLG_VERT_PADDING_Y 4
218#define DLG_ICON_WIDTH 34
219#define DLG_ICON_HEIGHT 34
220#define DLG_MIN_WIDTH 150
221#define DLG_FONT_NAME "MS Sans Serif"
222#define DLG_FONT_POINT_SIZE 8
223#define DLG_MIN_MAX_WIDTH 400
224#define DLG_MIN_MAX_HEIGHT 400
225
226#define DLG_NONBUTTON_CONTROL 5000 /* First ID of non-button controls */
227
228#ifndef WM_XBUTTONDOWN /* For Win2K / winME ONLY */
229# define WM_XBUTTONDOWN 0x020B
230# define WM_XBUTTONUP 0x020C
231# define WM_XBUTTONDBLCLK 0x020D
232# define MK_XBUTTON1 0x0020
233# define MK_XBUTTON2 0x0040
234#endif
235
236#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238 * Define a few things for generating prototypes. This is just to avoid
239 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241# define APIENTRY
242# define CALLBACK
243# define CONST
244# define FAR
245# define NEAR
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200246# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247# define _cdecl
248typedef int BOOL;
249typedef int BYTE;
250typedef int DWORD;
251typedef int WCHAR;
252typedef int ENUMLOGFONT;
253typedef int FINDREPLACE;
254typedef int HANDLE;
255typedef int HBITMAP;
256typedef int HBRUSH;
257typedef int HDROP;
258typedef int INT;
259typedef int LOGFONT[];
260typedef int LPARAM;
261typedef int LPCREATESTRUCT;
262typedef int LPCSTR;
263typedef int LPCTSTR;
264typedef int LPRECT;
265typedef int LPSTR;
266typedef int LPWINDOWPOS;
267typedef int LPWORD;
268typedef int LRESULT;
269typedef int HRESULT;
270# undef MSG
271typedef int MSG;
272typedef int NEWTEXTMETRIC;
273typedef int OSVERSIONINFO;
274typedef int PWORD;
275typedef int RECT;
276typedef int UINT;
277typedef int WORD;
278typedef int WPARAM;
279typedef int POINT;
280typedef void *HINSTANCE;
281typedef void *HMENU;
282typedef void *HWND;
283typedef void *HDC;
284typedef void VOID;
285typedef int LPNMHDR;
286typedef int LONG;
287typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200288typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200289typedef int COLORREF;
290typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#endif
292
293#ifndef GET_X_LPARAM
294# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
295#endif
296
297static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100298static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299static void clear_rect(RECT *rcp);
300
301static WORD s_dlgfntheight; /* height of the dialog font */
302static WORD s_dlgfntwidth; /* width of the dialog font */
303
304#ifdef FEAT_MENU
305static HMENU s_menuBar = NULL;
306#endif
307#ifdef FEAT_TEAROFF
308static void rebuild_tearoff(vimmenu_T *menu);
309static HBITMAP s_htearbitmap; /* bitmap used to indicate tearoff */
310#endif
311
312/* Flag that is set while processing a message that must not be interrupted by
313 * processing another message. */
314static int s_busy_processing = FALSE;
315
316static int destroying = FALSE; /* call DestroyWindow() ourselves */
317
318#ifdef MSWIN_FIND_REPLACE
319static UINT s_findrep_msg = 0; /* set in gui_w[16/32].c */
320static FINDREPLACE s_findrep_struct;
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200321# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322static FINDREPLACEW s_findrep_struct_w;
323# endif
324static HWND s_findrep_hwnd = NULL;
325static int s_findrep_is_find; /* TRUE for find dialog, FALSE
326 for find/replace dialog */
327#endif
328
329static HINSTANCE s_hinst = NULL;
Bram Moolenaar85b11762016-02-27 18:13:23 +0100330#if !defined(FEAT_GUI)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100331static
332#endif
333HWND s_hwnd = NULL;
334static HDC s_hdc = NULL;
335static HBRUSH s_brush = NULL;
336
337#ifdef FEAT_TOOLBAR
338static HWND s_toolbarhwnd = NULL;
339static WNDPROC s_toolbar_wndproc = NULL;
340#endif
341
342#ifdef FEAT_GUI_TABLINE
343static HWND s_tabhwnd = NULL;
344static WNDPROC s_tabline_wndproc = NULL;
345static int showing_tabline = 0;
346#endif
347
348static WPARAM s_wParam = 0;
349static LPARAM s_lParam = 0;
350
351static HWND s_textArea = NULL;
352static UINT s_uMsg = 0;
353
354static char_u *s_textfield; /* Used by dialogs to pass back strings */
355
356static int s_need_activate = FALSE;
357
358/* This variable is set when waiting for an event, which is the only moment
359 * scrollbar dragging can be done directly. It's not allowed while commands
360 * are executed, because it may move the cursor and that may cause unexpected
361 * problems (e.g., while ":s" is working).
362 */
363static int allow_scrollbar = FALSE;
364
365#ifdef GLOBAL_IME
366# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
367#else
368# define MyTranslateMessage(x) TranslateMessage(x)
369#endif
370
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100371#if defined(FEAT_DIRECTX)
372 static int
373directx_enabled(void)
374{
375 if (s_dwc != NULL)
376 return 1;
377 else if (s_directx_load_attempted)
378 return 0;
379 /* load DirectX */
380 DWrite_Init();
381 s_directx_load_attempted = 1;
382 s_dwc = DWriteContext_Open();
383 directx_binddc();
384 return s_dwc != NULL ? 1 : 0;
385}
386
387 static void
388directx_binddc(void)
389{
390 if (s_textArea != NULL)
391 {
392 RECT rect;
393 GetClientRect(s_textArea, &rect);
394 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
395 }
396}
397#endif
398
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200399#if defined(FEAT_MBYTE) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100400 /* use of WindowProc depends on wide_WindowProc */
401# define MyWindowProc vim_WindowProc
402#else
403 /* use ordinary WindowProc */
404# define MyWindowProc DefWindowProc
405#endif
406
407extern int current_font_height; /* this is in os_mswin.c */
408
409static struct
410{
411 UINT key_sym;
412 char_u vim_code0;
413 char_u vim_code1;
414} special_keys[] =
415{
416 {VK_UP, 'k', 'u'},
417 {VK_DOWN, 'k', 'd'},
418 {VK_LEFT, 'k', 'l'},
419 {VK_RIGHT, 'k', 'r'},
420
421 {VK_F1, 'k', '1'},
422 {VK_F2, 'k', '2'},
423 {VK_F3, 'k', '3'},
424 {VK_F4, 'k', '4'},
425 {VK_F5, 'k', '5'},
426 {VK_F6, 'k', '6'},
427 {VK_F7, 'k', '7'},
428 {VK_F8, 'k', '8'},
429 {VK_F9, 'k', '9'},
430 {VK_F10, 'k', ';'},
431
432 {VK_F11, 'F', '1'},
433 {VK_F12, 'F', '2'},
434 {VK_F13, 'F', '3'},
435 {VK_F14, 'F', '4'},
436 {VK_F15, 'F', '5'},
437 {VK_F16, 'F', '6'},
438 {VK_F17, 'F', '7'},
439 {VK_F18, 'F', '8'},
440 {VK_F19, 'F', '9'},
441 {VK_F20, 'F', 'A'},
442
443 {VK_F21, 'F', 'B'},
444#ifdef FEAT_NETBEANS_INTG
445 {VK_PAUSE, 'F', 'B'}, /* Pause == F21 (see gui_gtk_x11.c) */
446#endif
447 {VK_F22, 'F', 'C'},
448 {VK_F23, 'F', 'D'},
449 {VK_F24, 'F', 'E'}, /* winuser.h defines up to F24 */
450
451 {VK_HELP, '%', '1'},
452 {VK_BACK, 'k', 'b'},
453 {VK_INSERT, 'k', 'I'},
454 {VK_DELETE, 'k', 'D'},
455 {VK_HOME, 'k', 'h'},
456 {VK_END, '@', '7'},
457 {VK_PRIOR, 'k', 'P'},
458 {VK_NEXT, 'k', 'N'},
459 {VK_PRINT, '%', '9'},
460 {VK_ADD, 'K', '6'},
461 {VK_SUBTRACT, 'K', '7'},
462 {VK_DIVIDE, 'K', '8'},
463 {VK_MULTIPLY, 'K', '9'},
464 {VK_SEPARATOR, 'K', 'A'}, /* Keypad Enter */
465 {VK_DECIMAL, 'K', 'B'},
466
467 {VK_NUMPAD0, 'K', 'C'},
468 {VK_NUMPAD1, 'K', 'D'},
469 {VK_NUMPAD2, 'K', 'E'},
470 {VK_NUMPAD3, 'K', 'F'},
471 {VK_NUMPAD4, 'K', 'G'},
472 {VK_NUMPAD5, 'K', 'H'},
473 {VK_NUMPAD6, 'K', 'I'},
474 {VK_NUMPAD7, 'K', 'J'},
475 {VK_NUMPAD8, 'K', 'K'},
476 {VK_NUMPAD9, 'K', 'L'},
477
478 /* Keys that we want to be able to use any modifier with: */
479 {VK_SPACE, ' ', NUL},
480 {VK_TAB, TAB, NUL},
481 {VK_ESCAPE, ESC, NUL},
482 {NL, NL, NUL},
483 {CAR, CAR, NUL},
484
485 /* End of list marker: */
486 {0, 0, 0}
487};
488
489/* Local variables */
490static int s_button_pending = -1;
491
492/* s_getting_focus is set when we got focus but didn't see mouse-up event yet,
493 * so don't reset s_button_pending. */
494static int s_getting_focus = FALSE;
495
496static int s_x_pending;
497static int s_y_pending;
498static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200499static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200501static int dead_key = 0; // 0: no dead key, 1: dead key pressed
502static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
503 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100505#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100506/* balloon-eval WM_NOTIFY_HANDLER */
507static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
508static void TrackUserActivity(UINT uMsg);
509#endif
510
511/*
512 * For control IME.
513 *
514 * These LOGFONT used for IME.
515 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100516#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100517/* holds LOGFONT for 'guifontwide' if available, otherwise 'guifont' */
518static LOGFONT norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100519#endif
520#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100521/* holds LOGFONT for 'guifont' always. */
522static LOGFONT sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523#endif
524
525#ifdef FEAT_MBYTE_IME
526static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
527#endif
528
529#if defined(FEAT_BROWSE)
530static char_u *convert_filter(char_u *s);
531#endif
532
533#ifdef DEBUG_PRINT_ERROR
534/*
535 * Print out the last Windows error message
536 */
537 static void
538print_windows_error(void)
539{
540 LPVOID lpMsgBuf;
541
542 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
543 NULL, GetLastError(),
544 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
545 (LPTSTR) &lpMsgBuf, 0, NULL);
546 TRACE1("Error: %s\n", lpMsgBuf);
547 LocalFree(lpMsgBuf);
548}
549#endif
550
551/*
552 * Cursor blink functions.
553 *
554 * This is a simple state machine:
555 * BLINK_NONE not blinking at all
556 * BLINK_OFF blinking, cursor is not shown
557 * BLINK_ON blinking, cursor is shown
558 */
559
560#define BLINK_NONE 0
561#define BLINK_OFF 1
562#define BLINK_ON 2
563
564static int blink_state = BLINK_NONE;
565static long_u blink_waittime = 700;
566static long_u blink_ontime = 400;
567static long_u blink_offtime = 250;
568static UINT blink_timer = 0;
569
Bram Moolenaar703a8042016-06-04 16:24:32 +0200570 int
571gui_mch_is_blinking(void)
572{
573 return blink_state != BLINK_NONE;
574}
575
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200576 int
577gui_mch_is_blink_off(void)
578{
579 return blink_state == BLINK_OFF;
580}
581
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100582 void
583gui_mch_set_blinking(long wait, long on, long off)
584{
585 blink_waittime = wait;
586 blink_ontime = on;
587 blink_offtime = off;
588}
589
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100590 static VOID CALLBACK
591_OnBlinkTimer(
592 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100593 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100594 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100595 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100596{
597 MSG msg;
598
599 /*
600 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
601 */
602
603 KillTimer(NULL, idEvent);
604
605 /* Eat spurious WM_TIMER messages */
606 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
607 ;
608
609 if (blink_state == BLINK_ON)
610 {
611 gui_undraw_cursor();
612 blink_state = BLINK_OFF;
613 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
614 (TIMERPROC)_OnBlinkTimer);
615 }
616 else
617 {
618 gui_update_cursor(TRUE, FALSE);
619 blink_state = BLINK_ON;
620 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100621 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100622 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100623 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100624}
625
626 static void
627gui_mswin_rm_blink_timer(void)
628{
629 MSG msg;
630
631 if (blink_timer != 0)
632 {
633 KillTimer(NULL, blink_timer);
634 /* Eat spurious WM_TIMER messages */
635 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
636 ;
637 blink_timer = 0;
638 }
639}
640
641/*
642 * Stop the cursor blinking. Show the cursor if it wasn't shown.
643 */
644 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100645gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100646{
647 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100648 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100649 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100650 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100651 gui_mch_flush();
652 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100653 blink_state = BLINK_NONE;
654}
655
656/*
657 * Start the cursor blinking. If it was already blinking, this restarts the
658 * waiting time and shows the cursor.
659 */
660 void
661gui_mch_start_blink(void)
662{
663 gui_mswin_rm_blink_timer();
664
665 /* Only switch blinking on if none of the times is zero */
666 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
667 {
668 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
669 (TIMERPROC)_OnBlinkTimer);
670 blink_state = BLINK_ON;
671 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100672 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100673 }
674}
675
676/*
677 * Call-back routines.
678 */
679
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100680 static VOID CALLBACK
681_OnTimer(
682 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100683 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100684 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100685 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686{
687 MSG msg;
688
689 /*
690 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
691 */
692 KillTimer(NULL, idEvent);
693 s_timed_out = TRUE;
694
695 /* Eat spurious WM_TIMER messages */
696 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
697 ;
698 if (idEvent == s_wait_timer)
699 s_wait_timer = 0;
700}
701
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702 static void
703_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100704 HWND hwnd UNUSED,
705 UINT ch UNUSED,
706 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100707{
708 dead_key = 1;
709}
710
711/*
712 * Convert Unicode character "ch" to bytes in "string[slen]".
713 * When "had_alt" is TRUE the ALT key was included in "ch".
714 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 * Because the Windows API uses UTF-16, we have to deal with surrogate
716 * pairs; this is where we choose to deal with them: if "ch" is a high
717 * surrogate, it will be stored, and the length returned will be zero; the next
718 * char_to_string call will then include the high surrogate, decoding the pair
719 * of UTF-16 code units to a single Unicode code point, presuming it is the
720 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100721 */
722 static int
723char_to_string(int ch, char_u *string, int slen, int had_alt)
724{
725 int len;
726 int i;
727#ifdef FEAT_MBYTE
728 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200729 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200731 if (surrogate_pending_ch != 0)
732 {
733 /* We don't guarantee ch is a low surrogate to match the high surrogate
734 * we already have; it should be, but if it isn't, tough luck. */
735 wstring[0] = surrogate_pending_ch;
736 wstring[1] = ch;
737 surrogate_pending_ch = 0;
738 len = 2;
739 }
740 else if (ch >= 0xD800 && ch <= 0xDBFF) /* high surrogate */
741 {
742 /* We don't have the entire code point yet, only the first UTF-16 code
743 * unit; so just remember it and use it in the next call. */
744 surrogate_pending_ch = ch;
745 return 0;
746 }
747 else
748 {
749 wstring[0] = ch;
750 len = 1;
751 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200752
753 /* "ch" is a UTF-16 character. Convert it to a string of bytes. When
754 * "enc_codepage" is non-zero use the standard Win32 function,
755 * otherwise use our own conversion function (e.g., for UTF-8). */
756 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100757 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200758 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
759 (LPSTR)string, slen, 0, NULL);
760 /* If we had included the ALT key into the character but now the
761 * upper bit is no longer set, that probably means the conversion
762 * failed. Convert the original character and set the upper bit
763 * afterwards. */
764 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100765 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200766 wstring[0] = ch & 0x7f;
767 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
768 (LPSTR)string, slen, 0, NULL);
769 if (len == 1) /* safety check */
770 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100771 }
772 }
773 else
774 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200775 ws = utf16_to_enc(wstring, &len);
776 if (ws == NULL)
777 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100778 else
779 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200780 if (len > slen) /* just in case */
781 len = slen;
782 mch_memmove(string, ws, len);
783 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100784 }
785 }
786
787 if (len == 0)
788#endif
789 {
790 string[0] = ch;
791 len = 1;
792 }
793
794 for (i = 0; i < len; ++i)
795 if (string[i] == CSI && len <= slen - 2)
796 {
797 /* Insert CSI as K_CSI. */
798 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
799 string[++i] = KS_EXTRA;
800 string[++i] = (int)KE_CSI;
801 len += 2;
802 }
803
804 return len;
805}
806
807/*
808 * Key hit, add it to the input buffer.
809 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100810 static void
811_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100812 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100813 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100814 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100815{
816 char_u string[40];
817 int len = 0;
818
819 dead_key = 0;
820
821 len = char_to_string(ch, string, 40, FALSE);
822 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
823 {
824 trash_input_buf();
825 got_int = TRUE;
826 }
827
828 add_to_input_buf(string, len);
829}
830
831/*
832 * Alt-Key hit, add it to the input buffer.
833 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100834 static void
835_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100836 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100837 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100838 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100839{
840 char_u string[40]; /* Enough for multibyte character */
841 int len;
842 int modifiers;
843 int ch = cch; /* special keys are negative */
844
845 dead_key = 0;
846
847 /* TRACE("OnSysChar(%d, %c)\n", ch, ch); */
848
849 /* OK, we have a character key (given by ch) which was entered with the
850 * ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
851 * that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
852 * CAPSLOCK is pressed) at this point.
853 */
854 modifiers = MOD_MASK_ALT;
855 if (GetKeyState(VK_SHIFT) & 0x8000)
856 modifiers |= MOD_MASK_SHIFT;
857 if (GetKeyState(VK_CONTROL) & 0x8000)
858 modifiers |= MOD_MASK_CTRL;
859
860 ch = simplify_key(ch, &modifiers);
861 /* remove the SHIFT modifier for keys where it's already included, e.g.,
862 * '(' and '*' */
863 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
864 modifiers &= ~MOD_MASK_SHIFT;
865
866 /* Interpret the ALT key as making the key META, include SHIFT, etc. */
867 ch = extract_modifiers(ch, &modifiers);
868 if (ch == CSI)
869 ch = K_CSI;
870
871 len = 0;
872 if (modifiers)
873 {
874 string[len++] = CSI;
875 string[len++] = KS_MODIFIER;
876 string[len++] = modifiers;
877 }
878
879 if (IS_SPECIAL((int)ch))
880 {
881 string[len++] = CSI;
882 string[len++] = K_SECOND((int)ch);
883 string[len++] = K_THIRD((int)ch);
884 }
885 else
886 {
887 /* Although the documentation isn't clear about it, we assume "ch" is
888 * a Unicode character. */
889 len += char_to_string(ch, string + len, 40 - len, TRUE);
890 }
891
892 add_to_input_buf(string, len);
893}
894
895 static void
896_OnMouseEvent(
897 int button,
898 int x,
899 int y,
900 int repeated_click,
901 UINT keyFlags)
902{
903 int vim_modifiers = 0x0;
904
905 s_getting_focus = FALSE;
906
907 if (keyFlags & MK_SHIFT)
908 vim_modifiers |= MOUSE_SHIFT;
909 if (keyFlags & MK_CONTROL)
910 vim_modifiers |= MOUSE_CTRL;
911 if (GetKeyState(VK_MENU) & 0x8000)
912 vim_modifiers |= MOUSE_ALT;
913
914 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
915}
916
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100917 static void
918_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100919 HWND hwnd UNUSED,
920 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921 int x,
922 int y,
923 UINT keyFlags)
924{
925 static LONG s_prevTime = 0;
926
927 LONG currentTime = GetMessageTime();
928 int button = -1;
929 int repeated_click;
930
931 /* Give main window the focus: this is so the cursor isn't hollow. */
932 (void)SetFocus(s_hwnd);
933
934 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
935 button = MOUSE_LEFT;
936 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
937 button = MOUSE_MIDDLE;
938 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
939 button = MOUSE_RIGHT;
940 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
941 {
942#ifndef GET_XBUTTON_WPARAM
943# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
944#endif
945 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
946 }
947 else if (s_uMsg == WM_CAPTURECHANGED)
948 {
949 /* on W95/NT4, somehow you get in here with an odd Msg
950 * if you press one button while holding down the other..*/
951 if (s_button_pending == MOUSE_LEFT)
952 button = MOUSE_RIGHT;
953 else
954 button = MOUSE_LEFT;
955 }
956 if (button >= 0)
957 {
958 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
959
960 /*
961 * Holding down the left and right buttons simulates pushing the middle
962 * button.
963 */
964 if (repeated_click
965 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
966 || (button == MOUSE_RIGHT
967 && s_button_pending == MOUSE_LEFT)))
968 {
969 /*
970 * Hmm, gui.c will ignore more than one button down at a time, so
971 * pretend we let go of it first.
972 */
973 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
974 button = MOUSE_MIDDLE;
975 repeated_click = FALSE;
976 s_button_pending = -1;
977 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
978 }
979 else if ((repeated_click)
980 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
981 {
982 if (s_button_pending > -1)
983 {
984 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
985 s_button_pending = -1;
986 }
987 /* TRACE("Button down at x %d, y %d\n", x, y); */
988 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
989 }
990 else
991 {
992 /*
993 * If this is the first press (i.e. not a multiple click) don't
994 * action immediately, but store and wait for:
995 * i) button-up
996 * ii) mouse move
997 * iii) another button press
998 * before using it.
999 * This enables us to make left+right simulate middle button,
1000 * without left or right being actioned first. The side-effect is
1001 * that if you click and hold the mouse without dragging, the
1002 * cursor doesn't move until you release the button. In practice
1003 * this is hardly a problem.
1004 */
1005 s_button_pending = button;
1006 s_x_pending = x;
1007 s_y_pending = y;
1008 s_kFlags_pending = keyFlags;
1009 }
1010
1011 s_prevTime = currentTime;
1012 }
1013}
1014
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001015 static void
1016_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001017 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001018 int x,
1019 int y,
1020 UINT keyFlags)
1021{
1022 int button;
1023
1024 s_getting_focus = FALSE;
1025 if (s_button_pending > -1)
1026 {
1027 /* Delayed action for mouse down event */
1028 _OnMouseEvent(s_button_pending, s_x_pending,
1029 s_y_pending, FALSE, s_kFlags_pending);
1030 s_button_pending = -1;
1031 }
1032 if (s_uMsg == WM_MOUSEMOVE)
1033 {
1034 /*
1035 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1036 * down.
1037 */
1038 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1039 | MK_XBUTTON1 | MK_XBUTTON2)))
1040 {
1041 gui_mouse_moved(x, y);
1042 return;
1043 }
1044
1045 /*
1046 * While button is down, keep grabbing mouse move events when
1047 * the mouse goes outside the window
1048 */
1049 SetCapture(s_textArea);
1050 button = MOUSE_DRAG;
1051 /* TRACE(" move at x %d, y %d\n", x, y); */
1052 }
1053 else
1054 {
1055 ReleaseCapture();
1056 button = MOUSE_RELEASE;
1057 /* TRACE(" up at x %d, y %d\n", x, y); */
1058 }
1059
1060 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1061}
1062
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001063 static void
1064_OnSizeTextArea(
1065 HWND hwnd UNUSED,
1066 UINT state UNUSED,
1067 int cx UNUSED,
1068 int cy UNUSED)
1069{
1070#if defined(FEAT_DIRECTX)
1071 if (IS_ENABLE_DIRECTX())
1072 directx_binddc();
1073#endif
1074}
1075
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001076#ifdef FEAT_MENU
1077/*
1078 * Find the vimmenu_T with the given id
1079 */
1080 static vimmenu_T *
1081gui_mswin_find_menu(
1082 vimmenu_T *pMenu,
1083 int id)
1084{
1085 vimmenu_T *pChildMenu;
1086
1087 while (pMenu)
1088 {
1089 if (pMenu->id == (UINT)id)
1090 break;
1091 if (pMenu->children != NULL)
1092 {
1093 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1094 if (pChildMenu)
1095 {
1096 pMenu = pChildMenu;
1097 break;
1098 }
1099 }
1100 pMenu = pMenu->next;
1101 }
1102 return pMenu;
1103}
1104
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105 static void
1106_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001107 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001108 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001109 HWND hwndCtl UNUSED,
1110 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111{
1112 vimmenu_T *pMenu;
1113
1114 pMenu = gui_mswin_find_menu(root_menu, id);
1115 if (pMenu)
1116 gui_menu_cb(pMenu);
1117}
1118#endif
1119
1120#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001121# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001122/*
1123 * copy useful data from structure LPFINDREPLACE to structure LPFINDREPLACEW
1124 */
1125 static void
1126findrep_atow(LPFINDREPLACEW lpfrw, LPFINDREPLACE lpfr)
1127{
1128 WCHAR *wp;
1129
1130 lpfrw->hwndOwner = lpfr->hwndOwner;
1131 lpfrw->Flags = lpfr->Flags;
1132
1133 wp = enc_to_utf16((char_u *)lpfr->lpstrFindWhat, NULL);
1134 wcsncpy(lpfrw->lpstrFindWhat, wp, lpfrw->wFindWhatLen - 1);
1135 vim_free(wp);
1136
1137 /* the field "lpstrReplaceWith" doesn't need to be copied */
1138}
1139
1140/*
1141 * copy useful data from structure LPFINDREPLACEW to structure LPFINDREPLACE
1142 */
1143 static void
1144findrep_wtoa(LPFINDREPLACE lpfr, LPFINDREPLACEW lpfrw)
1145{
1146 char_u *p;
1147
1148 lpfr->Flags = lpfrw->Flags;
1149
1150 p = utf16_to_enc((short_u*)lpfrw->lpstrFindWhat, NULL);
1151 vim_strncpy((char_u *)lpfr->lpstrFindWhat, p, lpfr->wFindWhatLen - 1);
1152 vim_free(p);
1153
1154 p = utf16_to_enc((short_u*)lpfrw->lpstrReplaceWith, NULL);
1155 vim_strncpy((char_u *)lpfr->lpstrReplaceWith, p, lpfr->wReplaceWithLen - 1);
1156 vim_free(p);
1157}
1158# endif
1159
1160/*
1161 * Handle a Find/Replace window message.
1162 */
1163 static void
1164_OnFindRepl(void)
1165{
1166 int flags = 0;
1167 int down;
1168
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001169# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001170 /* If the OS is Windows NT, and 'encoding' differs from active codepage:
1171 * convert text from wide string. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001172 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 {
1174 findrep_wtoa(&s_findrep_struct, &s_findrep_struct_w);
1175 }
1176# endif
1177
1178 if (s_findrep_struct.Flags & FR_DIALOGTERM)
1179 /* Give main window the focus back. */
1180 (void)SetFocus(s_hwnd);
1181
1182 if (s_findrep_struct.Flags & FR_FINDNEXT)
1183 {
1184 flags = FRD_FINDNEXT;
1185
1186 /* Give main window the focus back: this is so the cursor isn't
1187 * hollow. */
1188 (void)SetFocus(s_hwnd);
1189 }
1190 else if (s_findrep_struct.Flags & FR_REPLACE)
1191 {
1192 flags = FRD_REPLACE;
1193
1194 /* Give main window the focus back: this is so the cursor isn't
1195 * hollow. */
1196 (void)SetFocus(s_hwnd);
1197 }
1198 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1199 {
1200 flags = FRD_REPLACEALL;
1201 }
1202
1203 if (flags != 0)
1204 {
1205 /* Call the generic GUI function to do the actual work. */
1206 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1207 flags |= FRD_WHOLE_WORD;
1208 if (s_findrep_struct.Flags & FR_MATCHCASE)
1209 flags |= FRD_MATCH_CASE;
1210 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1211 gui_do_findrepl(flags, (char_u *)s_findrep_struct.lpstrFindWhat,
1212 (char_u *)s_findrep_struct.lpstrReplaceWith, down);
1213 }
1214}
1215#endif
1216
1217 static void
1218HandleMouseHide(UINT uMsg, LPARAM lParam)
1219{
1220 static LPARAM last_lParam = 0L;
1221
1222 /* We sometimes get a mousemove when the mouse didn't move... */
1223 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1224 {
1225 if (lParam == last_lParam)
1226 return;
1227 last_lParam = lParam;
1228 }
1229
1230 /* Handle specially, to centralise coding. We need to be sure we catch all
1231 * possible events which should cause us to restore the cursor (as it is a
1232 * shared resource, we take full responsibility for it).
1233 */
1234 switch (uMsg)
1235 {
1236 case WM_KEYUP:
1237 case WM_CHAR:
1238 /*
1239 * blank out the pointer if necessary
1240 */
1241 if (p_mh)
1242 gui_mch_mousehide(TRUE);
1243 break;
1244
1245 case WM_SYSKEYUP: /* show the pointer when a system-key is pressed */
1246 case WM_SYSCHAR:
1247 case WM_MOUSEMOVE: /* show the pointer on any mouse action */
1248 case WM_LBUTTONDOWN:
1249 case WM_LBUTTONUP:
1250 case WM_MBUTTONDOWN:
1251 case WM_MBUTTONUP:
1252 case WM_RBUTTONDOWN:
1253 case WM_RBUTTONUP:
1254 case WM_XBUTTONDOWN:
1255 case WM_XBUTTONUP:
1256 case WM_NCMOUSEMOVE:
1257 case WM_NCLBUTTONDOWN:
1258 case WM_NCLBUTTONUP:
1259 case WM_NCMBUTTONDOWN:
1260 case WM_NCMBUTTONUP:
1261 case WM_NCRBUTTONDOWN:
1262 case WM_NCRBUTTONUP:
1263 case WM_KILLFOCUS:
1264 /*
1265 * if the pointer is currently hidden, then we should show it.
1266 */
1267 gui_mch_mousehide(FALSE);
1268 break;
1269 }
1270}
1271
1272 static LRESULT CALLBACK
1273_TextAreaWndProc(
1274 HWND hwnd,
1275 UINT uMsg,
1276 WPARAM wParam,
1277 LPARAM lParam)
1278{
1279 /*
1280 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1281 hwnd, uMsg, wParam, lParam);
1282 */
1283
1284 HandleMouseHide(uMsg, lParam);
1285
1286 s_uMsg = uMsg;
1287 s_wParam = wParam;
1288 s_lParam = lParam;
1289
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001290#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001291 TrackUserActivity(uMsg);
1292#endif
1293
1294 switch (uMsg)
1295 {
1296 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1297 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1298 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1299 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1300 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1301 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1302 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1303 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1304 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1305 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1306 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1307 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1308 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1309 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001310 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001312#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001313 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1314 return TRUE;
1315#endif
1316 default:
1317 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1318 }
1319}
1320
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001321#if defined(FEAT_MBYTE) \
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001322 || defined(GLOBAL_IME) \
1323 || defined(PROTO)
1324# ifdef PROTO
1325typedef int WINAPI;
1326# endif
1327
1328 LRESULT WINAPI
1329vim_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
1330{
1331# ifdef GLOBAL_IME
1332 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
1333# else
1334 if (wide_WindowProc)
1335 return DefWindowProcW(hwnd, message, wParam, lParam);
1336 return DefWindowProc(hwnd, message, wParam, lParam);
1337#endif
1338}
1339#endif
1340
1341/*
1342 * Called when the foreground or background color has been changed.
1343 */
1344 void
1345gui_mch_new_colors(void)
1346{
1347 /* nothing to do? */
1348}
1349
1350/*
1351 * Set the colors to their default values.
1352 */
1353 void
1354gui_mch_def_colors(void)
1355{
1356 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1357 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1358 gui.def_norm_pixel = gui.norm_pixel;
1359 gui.def_back_pixel = gui.back_pixel;
1360}
1361
1362/*
1363 * Open the GUI window which was created by a call to gui_mch_init().
1364 */
1365 int
1366gui_mch_open(void)
1367{
1368#ifndef SW_SHOWDEFAULT
1369# define SW_SHOWDEFAULT 10 /* Borland 5.0 doesn't have it */
1370#endif
1371 /* Actually open the window, if not already visible
1372 * (may be done already in gui_mch_set_shellsize) */
1373 if (!IsWindowVisible(s_hwnd))
1374 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1375
1376#ifdef MSWIN_FIND_REPLACE
1377 /* Init replace string here, so that we keep it when re-opening the
1378 * dialog. */
1379 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1380#endif
1381
1382 return OK;
1383}
1384
1385/*
1386 * Get the position of the top left corner of the window.
1387 */
1388 int
1389gui_mch_get_winpos(int *x, int *y)
1390{
1391 RECT rect;
1392
1393 GetWindowRect(s_hwnd, &rect);
1394 *x = rect.left;
1395 *y = rect.top;
1396 return OK;
1397}
1398
1399/*
1400 * Set the position of the top left corner of the window to the given
1401 * coordinates.
1402 */
1403 void
1404gui_mch_set_winpos(int x, int y)
1405{
1406 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1407 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1408}
1409 void
1410gui_mch_set_text_area_pos(int x, int y, int w, int h)
1411{
1412 static int oldx = 0;
1413 static int oldy = 0;
1414
1415 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1416
1417#ifdef FEAT_TOOLBAR
1418 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1419 SendMessage(s_toolbarhwnd, WM_SIZE,
1420 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1421#endif
1422#if defined(FEAT_GUI_TABLINE)
1423 if (showing_tabline)
1424 {
1425 int top = 0;
1426 RECT rect;
1427
1428# ifdef FEAT_TOOLBAR
1429 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1430 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1431# endif
1432 GetClientRect(s_hwnd, &rect);
1433 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1434 }
1435#endif
1436
1437 /* When side scroll bar is unshown, the size of window will change.
1438 * then, the text area move left or right. thus client rect should be
1439 * forcedly redrawn. (Yasuhiro Matsumoto) */
1440 if (oldx != x || oldy != y)
1441 {
1442 InvalidateRect(s_hwnd, NULL, FALSE);
1443 oldx = x;
1444 oldy = y;
1445 }
1446}
1447
1448
1449/*
1450 * Scrollbar stuff:
1451 */
1452
1453 void
1454gui_mch_enable_scrollbar(
1455 scrollbar_T *sb,
1456 int flag)
1457{
1458 ShowScrollBar(sb->id, SB_CTL, flag);
1459
1460 /* TODO: When the window is maximized, the size of the window stays the
1461 * same, thus the size of the text area changes. On Win98 it's OK, on Win
1462 * NT 4.0 it's not... */
1463}
1464
1465 void
1466gui_mch_set_scrollbar_pos(
1467 scrollbar_T *sb,
1468 int x,
1469 int y,
1470 int w,
1471 int h)
1472{
1473 SetWindowPos(sb->id, NULL, x, y, w, h,
1474 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1475}
1476
1477 void
1478gui_mch_create_scrollbar(
1479 scrollbar_T *sb,
1480 int orient) /* SBAR_VERT or SBAR_HORIZ */
1481{
1482 sb->id = CreateWindow(
1483 "SCROLLBAR", "Scrollbar",
1484 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
1485 10, /* Any value will do for now */
1486 10, /* Any value will do for now */
1487 s_hwnd, NULL,
1488 s_hinst, NULL);
1489}
1490
1491/*
1492 * Find the scrollbar with the given hwnd.
1493 */
1494 static scrollbar_T *
1495gui_mswin_find_scrollbar(HWND hwnd)
1496{
1497 win_T *wp;
1498
1499 if (gui.bottom_sbar.id == hwnd)
1500 return &gui.bottom_sbar;
1501 FOR_ALL_WINDOWS(wp)
1502 {
1503 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1504 return &wp->w_scrollbars[SBAR_LEFT];
1505 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1506 return &wp->w_scrollbars[SBAR_RIGHT];
1507 }
1508 return NULL;
1509}
1510
1511/*
1512 * Get the character size of a font.
1513 */
1514 static void
1515GetFontSize(GuiFont font)
1516{
1517 HWND hwnd = GetDesktopWindow();
1518 HDC hdc = GetWindowDC(hwnd);
1519 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
1520 TEXTMETRIC tm;
1521
1522 GetTextMetrics(hdc, &tm);
1523 gui.char_width = tm.tmAveCharWidth + tm.tmOverhang;
1524
1525 gui.char_height = tm.tmHeight + p_linespace;
1526
1527 SelectFont(hdc, hfntOld);
1528
1529 ReleaseDC(hwnd, hdc);
1530}
1531
1532/*
1533 * Adjust gui.char_height (after 'linespace' was changed).
1534 */
1535 int
1536gui_mch_adjust_charheight(void)
1537{
1538 GetFontSize(gui.norm_font);
1539 return OK;
1540}
1541
1542 static GuiFont
1543get_font_handle(LOGFONT *lf)
1544{
1545 HFONT font = NULL;
1546
1547 /* Load the font */
1548 font = CreateFontIndirect(lf);
1549
1550 if (font == NULL)
1551 return NOFONT;
1552
1553 return (GuiFont)font;
1554}
1555
1556 static int
1557pixels_to_points(int pixels, int vertical)
1558{
1559 int points;
1560 HWND hwnd;
1561 HDC hdc;
1562
1563 hwnd = GetDesktopWindow();
1564 hdc = GetWindowDC(hwnd);
1565
1566 points = MulDiv(pixels, 72,
1567 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1568
1569 ReleaseDC(hwnd, hdc);
1570
1571 return points;
1572}
1573
1574 GuiFont
1575gui_mch_get_font(
1576 char_u *name,
1577 int giveErrorIfMissing)
1578{
1579 LOGFONT lf;
1580 GuiFont font = NOFONT;
1581
1582 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1583 font = get_font_handle(&lf);
1584 if (font == NOFONT && giveErrorIfMissing)
1585 EMSG2(_(e_font), name);
1586 return font;
1587}
1588
1589#if defined(FEAT_EVAL) || defined(PROTO)
1590/*
1591 * Return the name of font "font" in allocated memory.
1592 * Don't know how to get the actual name, thus use the provided name.
1593 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001594 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001595gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596{
1597 if (name == NULL)
1598 return NULL;
1599 return vim_strsave(name);
1600}
1601#endif
1602
1603 void
1604gui_mch_free_font(GuiFont font)
1605{
1606 if (font)
1607 DeleteObject((HFONT)font);
1608}
1609
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610/*
1611 * Return the Pixel value (color) for the given color name.
1612 * Return INVALCOLOR for error.
1613 */
1614 guicolor_T
1615gui_mch_get_color(char_u *name)
1616{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001617 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001618
1619 typedef struct SysColorTable
1620 {
1621 char *name;
1622 int color;
1623 } SysColorTable;
1624
1625 static SysColorTable sys_table[] =
1626 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1628 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001629#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001630 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1631#endif
1632 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1633 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1634 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1635 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1636 {"SYS_DESKTOP", COLOR_DESKTOP},
1637 {"SYS_INFOBK", COLOR_INFOBK},
1638 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1639 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001640 {"SYS_BTNFACE", COLOR_BTNFACE},
1641 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1642 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1643 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1644 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1645 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1646 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1647 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1648 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1649 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1650 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1651 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1652 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1653 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1654 {"SYS_MENU", COLOR_MENU},
1655 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1656 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1657 {"SYS_WINDOW", COLOR_WINDOW},
1658 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1659 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1660 };
1661
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001662 /*
1663 * Try to look up a system colour.
1664 */
1665 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1666 if (STRICMP(name, sys_table[i].name) == 0)
1667 return GetSysColor(sys_table[i].color);
1668
Bram Moolenaarab302212016-04-26 20:59:29 +02001669 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001670}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001671
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001672 guicolor_T
1673gui_mch_get_rgb_color(int r, int g, int b)
1674{
1675 return gui_get_rgb_color_cmn(r, g, b);
1676}
1677
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001678/*
1679 * Return OK if the key with the termcap name "name" is supported.
1680 */
1681 int
1682gui_mch_haskey(char_u *name)
1683{
1684 int i;
1685
1686 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1687 if (name[0] == special_keys[i].vim_code0 &&
1688 name[1] == special_keys[i].vim_code1)
1689 return OK;
1690 return FAIL;
1691}
1692
1693 void
1694gui_mch_beep(void)
1695{
1696 MessageBeep(MB_OK);
1697}
1698/*
1699 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1700 */
1701 void
1702gui_mch_invert_rectangle(
1703 int r,
1704 int c,
1705 int nr,
1706 int nc)
1707{
1708 RECT rc;
1709
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001710#if defined(FEAT_DIRECTX)
1711 if (IS_ENABLE_DIRECTX())
1712 DWriteContext_Flush(s_dwc);
1713#endif
1714
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001715 /*
1716 * Note: InvertRect() excludes right and bottom of rectangle.
1717 */
1718 rc.left = FILL_X(c);
1719 rc.top = FILL_Y(r);
1720 rc.right = rc.left + nc * gui.char_width;
1721 rc.bottom = rc.top + nr * gui.char_height;
1722 InvertRect(s_hdc, &rc);
1723}
1724
1725/*
1726 * Iconify the GUI window.
1727 */
1728 void
1729gui_mch_iconify(void)
1730{
1731 ShowWindow(s_hwnd, SW_MINIMIZE);
1732}
1733
1734/*
1735 * Draw a cursor without focus.
1736 */
1737 void
1738gui_mch_draw_hollow_cursor(guicolor_T color)
1739{
1740 HBRUSH hbr;
1741 RECT rc;
1742
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001743#if defined(FEAT_DIRECTX)
1744 if (IS_ENABLE_DIRECTX())
1745 DWriteContext_Flush(s_dwc);
1746#endif
1747
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001748 /*
1749 * Note: FrameRect() excludes right and bottom of rectangle.
1750 */
1751 rc.left = FILL_X(gui.col);
1752 rc.top = FILL_Y(gui.row);
1753 rc.right = rc.left + gui.char_width;
1754#ifdef FEAT_MBYTE
1755 if (mb_lefthalve(gui.row, gui.col))
1756 rc.right += gui.char_width;
1757#endif
1758 rc.bottom = rc.top + gui.char_height;
1759 hbr = CreateSolidBrush(color);
1760 FrameRect(s_hdc, &rc, hbr);
1761 DeleteBrush(hbr);
1762}
1763/*
1764 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1765 * color "color".
1766 */
1767 void
1768gui_mch_draw_part_cursor(
1769 int w,
1770 int h,
1771 guicolor_T color)
1772{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001773 RECT rc;
1774
1775 /*
1776 * Note: FillRect() excludes right and bottom of rectangle.
1777 */
1778 rc.left =
1779#ifdef FEAT_RIGHTLEFT
1780 /* vertical line should be on the right of current point */
1781 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1782#endif
1783 FILL_X(gui.col);
1784 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1785 rc.right = rc.left + w;
1786 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001787
Bram Moolenaar92467d32017-12-05 13:22:16 +01001788 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001789}
1790
1791
1792/*
1793 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1794 * dead key's nominal character and re-post the original message.
1795 */
1796 static void
1797outputDeadKey_rePost(MSG originalMsg)
1798{
1799 static MSG deadCharExpel;
1800
1801 if (!dead_key)
1802 return;
1803
1804 dead_key = 0;
1805
1806 /* Make Windows generate the dead key's character */
1807 deadCharExpel.message = originalMsg.message;
1808 deadCharExpel.hwnd = originalMsg.hwnd;
1809 deadCharExpel.wParam = VK_SPACE;
1810
1811 MyTranslateMessage(&deadCharExpel);
1812
1813 /* re-generate the current character free of the dead char influence */
1814 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1815 originalMsg.lParam);
1816}
1817
1818
1819/*
1820 * Process a single Windows message.
1821 * If one is not available we hang until one is.
1822 */
1823 static void
1824process_message(void)
1825{
1826 MSG msg;
1827 UINT vk = 0; /* Virtual key */
1828 char_u string[40];
1829 int i;
1830 int modifiers = 0;
1831 int key;
1832#ifdef FEAT_MENU
1833 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1834#endif
1835
1836 pGetMessage(&msg, NULL, 0, 0);
1837
1838#ifdef FEAT_OLE
1839 /* Look after OLE Automation commands */
1840 if (msg.message == WM_OLE)
1841 {
1842 char_u *str = (char_u *)msg.lParam;
1843 if (str == NULL || *str == NUL)
1844 {
1845 /* Message can't be ours, forward it. Fixes problem with Ultramon
1846 * 3.0.4 */
1847 pDispatchMessage(&msg);
1848 }
1849 else
1850 {
1851 add_to_input_buf(str, (int)STRLEN(str));
1852 vim_free(str); /* was allocated in CVim::SendKeys() */
1853 }
1854 return;
1855 }
1856#endif
1857
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858#ifdef MSWIN_FIND_REPLACE
1859 /* Don't process messages used by the dialog */
1860 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1861 {
1862 HandleMouseHide(msg.message, msg.lParam);
1863 return;
1864 }
1865#endif
1866
1867 /*
1868 * Check if it's a special key that we recognise. If not, call
1869 * TranslateMessage().
1870 */
1871 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1872 {
1873 vk = (int) msg.wParam;
1874
1875 /*
1876 * Handle dead keys in special conditions in other cases we let Windows
1877 * handle them and do not interfere.
1878 *
1879 * The dead_key flag must be reset on several occasions:
1880 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1881 * consumed at that point (This is when we let Windows combine the
1882 * dead character on its own)
1883 *
1884 * - Before doing something special such as regenerating keypresses to
1885 * expel the dead character as this could trigger an infinite loop if
1886 * for some reason MyTranslateMessage() do not trigger a call
1887 * immediately to _OnChar() (or _OnSysChar()).
1888 */
1889 if (dead_key)
1890 {
1891 /*
1892 * If a dead key was pressed and the user presses VK_SPACE,
1893 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1894 * with the dead char now, so do nothing special and let Windows
1895 * handle it.
1896 *
1897 * Note that VK_SPACE combines with the dead_key's character and
1898 * only one WM_CHAR will be generated by TranslateMessage(), in
1899 * the two other cases two WM_CHAR will be generated: the dead
1900 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1901 * user expects.
1902 */
1903 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1904 {
1905 dead_key = 0;
1906 MyTranslateMessage(&msg);
1907 return;
1908 }
1909 /* In modes where we are not typing, dead keys should behave
1910 * normally */
1911 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1912 {
1913 outputDeadKey_rePost(msg);
1914 return;
1915 }
1916 }
1917
1918 /* Check for CTRL-BREAK */
1919 if (vk == VK_CANCEL)
1920 {
1921 trash_input_buf();
1922 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001923 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924 string[0] = Ctrl_C;
1925 add_to_input_buf(string, 1);
1926 }
1927
1928 for (i = 0; special_keys[i].key_sym != 0; i++)
1929 {
1930 /* ignore VK_SPACE when ALT key pressed: system menu */
1931 if (special_keys[i].key_sym == vk
1932 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1933 {
1934 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001935 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001936 * is a key that would normally trigger the dead key nominal
1937 * character output (such as a NUMPAD printable character or
1938 * the TAB key, etc...).
1939 */
1940 if (dead_key && (special_keys[i].vim_code0 == 'K'
1941 || vk == VK_TAB || vk == CAR))
1942 {
1943 outputDeadKey_rePost(msg);
1944 return;
1945 }
1946
1947#ifdef FEAT_MENU
1948 /* Check for <F10>: Windows selects the menu. When <F10> is
1949 * mapped we want to use the mapping instead. */
1950 if (vk == VK_F10
1951 && gui.menu_is_active
1952 && check_map(k10, State, FALSE, TRUE, FALSE,
1953 NULL, NULL) == NULL)
1954 break;
1955#endif
1956 if (GetKeyState(VK_SHIFT) & 0x8000)
1957 modifiers |= MOD_MASK_SHIFT;
1958 /*
1959 * Don't use caps-lock as shift, because these are special keys
1960 * being considered here, and we only want letters to get
1961 * shifted -- webb
1962 */
1963 /*
1964 if (GetKeyState(VK_CAPITAL) & 0x0001)
1965 modifiers ^= MOD_MASK_SHIFT;
1966 */
1967 if (GetKeyState(VK_CONTROL) & 0x8000)
1968 modifiers |= MOD_MASK_CTRL;
1969 if (GetKeyState(VK_MENU) & 0x8000)
1970 modifiers |= MOD_MASK_ALT;
1971
1972 if (special_keys[i].vim_code1 == NUL)
1973 key = special_keys[i].vim_code0;
1974 else
1975 key = TO_SPECIAL(special_keys[i].vim_code0,
1976 special_keys[i].vim_code1);
1977 key = simplify_key(key, &modifiers);
1978 if (key == CSI)
1979 key = K_CSI;
1980
1981 if (modifiers)
1982 {
1983 string[0] = CSI;
1984 string[1] = KS_MODIFIER;
1985 string[2] = modifiers;
1986 add_to_input_buf(string, 3);
1987 }
1988
1989 if (IS_SPECIAL(key))
1990 {
1991 string[0] = CSI;
1992 string[1] = K_SECOND(key);
1993 string[2] = K_THIRD(key);
1994 add_to_input_buf(string, 3);
1995 }
1996 else
1997 {
1998 int len;
1999
2000 /* Handle "key" as a Unicode character. */
2001 len = char_to_string(key, string, 40, FALSE);
2002 add_to_input_buf(string, len);
2003 }
2004 break;
2005 }
2006 }
2007 if (special_keys[i].key_sym == 0)
2008 {
2009 /* Some keys need C-S- where they should only need C-.
2010 * Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2011 * system startup (Helmut Stiegler, 2003 Oct 3). */
2012 if (vk != 0xff
2013 && (GetKeyState(VK_CONTROL) & 0x8000)
2014 && !(GetKeyState(VK_SHIFT) & 0x8000)
2015 && !(GetKeyState(VK_MENU) & 0x8000))
2016 {
2017 /* CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE */
2018 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2019 {
2020 string[0] = Ctrl_HAT;
2021 add_to_input_buf(string, 1);
2022 }
2023 /* vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY! */
2024 else if (vk == 0xBD) /* QWERTY for CTRL-'-' */
2025 {
2026 string[0] = Ctrl__;
2027 add_to_input_buf(string, 1);
2028 }
2029 /* CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0 */
2030 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2031 {
2032 string[0] = Ctrl_AT;
2033 add_to_input_buf(string, 1);
2034 }
2035 else
2036 MyTranslateMessage(&msg);
2037 }
2038 else
2039 MyTranslateMessage(&msg);
2040 }
2041 }
2042#ifdef FEAT_MBYTE_IME
2043 else if (msg.message == WM_IME_NOTIFY)
2044 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2045 else if (msg.message == WM_KEYUP && im_get_status())
2046 /* added for non-MS IME (Yasuhiro Matsumoto) */
2047 MyTranslateMessage(&msg);
2048#endif
2049#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
2050/* GIME_TEST */
2051 else if (msg.message == WM_IME_STARTCOMPOSITION)
2052 {
2053 POINT point;
2054
2055 global_ime_set_font(&norm_logfont);
2056 point.x = FILL_X(gui.col);
2057 point.y = FILL_Y(gui.row);
2058 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2059 global_ime_set_position(&point);
2060 }
2061#endif
2062
2063#ifdef FEAT_MENU
2064 /* Check for <F10>: Default effect is to select the menu. When <F10> is
2065 * mapped we need to stop it here to avoid strange effects (e.g., for the
2066 * key-up event) */
2067 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2068 NULL, NULL) == NULL)
2069#endif
2070 pDispatchMessage(&msg);
2071}
2072
2073/*
2074 * Catch up with any queued events. This may put keyboard input into the
2075 * input buffer, call resize call-backs, trigger timers etc. If there is
2076 * nothing in the event queue (& no timers pending), then we return
2077 * immediately.
2078 */
2079 void
2080gui_mch_update(void)
2081{
2082 MSG msg;
2083
2084 if (!s_busy_processing)
2085 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2086 && !vim_is_input_buf_full())
2087 process_message();
2088}
2089
Bram Moolenaar4231da42016-06-02 14:30:04 +02002090 static void
2091remove_any_timer(void)
2092{
2093 MSG msg;
2094
2095 if (s_wait_timer != 0 && !s_timed_out)
2096 {
2097 KillTimer(NULL, s_wait_timer);
2098
2099 /* Eat spurious WM_TIMER messages */
2100 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2101 ;
2102 s_wait_timer = 0;
2103 }
2104}
2105
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002106/*
2107 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2108 * from the keyboard.
2109 * wtime == -1 Wait forever.
2110 * wtime == 0 This should never happen.
2111 * wtime > 0 Wait wtime milliseconds for a character.
2112 * Returns OK if a character was found to be available within the given time,
2113 * or FAIL otherwise.
2114 */
2115 int
2116gui_mch_wait_for_chars(int wtime)
2117{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002118 int focus;
2119
2120 s_timed_out = FALSE;
2121
2122 if (wtime > 0)
2123 {
2124 /* Don't do anything while processing a (scroll) message. */
2125 if (s_busy_processing)
2126 return FAIL;
2127 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)wtime,
2128 (TIMERPROC)_OnTimer);
2129 }
2130
2131 allow_scrollbar = TRUE;
2132
2133 focus = gui.in_focus;
2134 while (!s_timed_out)
2135 {
2136 /* Stop or start blinking when focus changes */
2137 if (gui.in_focus != focus)
2138 {
2139 if (gui.in_focus)
2140 gui_mch_start_blink();
2141 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002142 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002143 focus = gui.in_focus;
2144 }
2145
2146 if (s_need_activate)
2147 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002148 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149 s_need_activate = FALSE;
2150 }
2151
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152#ifdef FEAT_TIMERS
2153 did_add_timer = FALSE;
2154#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002155#ifdef MESSAGE_QUEUE
Bram Moolenaar62426e12017-08-13 15:37:58 +02002156 /* Check channel I/O while waiting for a message. */
Bram Moolenaar9186a272016-02-23 19:34:01 +01002157 for (;;)
2158 {
2159 MSG msg;
2160
2161 parse_queued_messages();
2162
Bram Moolenaar62426e12017-08-13 15:37:58 +02002163 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2164 {
2165 process_message();
2166 break;
2167 }
2168 else if (MsgWaitForMultipleObjects(0, NULL, FALSE, 100, QS_ALLINPUT)
2169 != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002170 break;
2171 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002172#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002173 /*
2174 * Don't use gui_mch_update() because then we will spin-lock until a
2175 * char arrives, instead we use GetMessage() to hang until an
2176 * event arrives. No need to check for input_buf_full because we are
2177 * returning as soon as it contains a single char -- webb
2178 */
2179 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002180#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002181
2182 if (input_available())
2183 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002184 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185 allow_scrollbar = FALSE;
2186
2187 /* Clear pending mouse button, the release event may have been
2188 * taken by the dialog window. But don't do this when getting
2189 * focus, we need the mouse-up event then. */
2190 if (!s_getting_focus)
2191 s_button_pending = -1;
2192
2193 return OK;
2194 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002195
2196#ifdef FEAT_TIMERS
2197 if (did_add_timer)
2198 {
2199 /* Need to recompute the waiting time. */
2200 remove_any_timer();
2201 break;
2202 }
2203#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002204 }
2205 allow_scrollbar = FALSE;
2206 return FAIL;
2207}
2208
2209/*
2210 * Clear a rectangular region of the screen from text pos (row1, col1) to
2211 * (row2, col2) inclusive.
2212 */
2213 void
2214gui_mch_clear_block(
2215 int row1,
2216 int col1,
2217 int row2,
2218 int col2)
2219{
2220 RECT rc;
2221
2222 /*
2223 * Clear one extra pixel at the far right, for when bold characters have
2224 * spilled over to the window border.
2225 * Note: FillRect() excludes right and bottom of rectangle.
2226 */
2227 rc.left = FILL_X(col1);
2228 rc.top = FILL_Y(row1);
2229 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2230 rc.bottom = FILL_Y(row2 + 1);
2231 clear_rect(&rc);
2232}
2233
2234/*
2235 * Clear the whole text window.
2236 */
2237 void
2238gui_mch_clear_all(void)
2239{
2240 RECT rc;
2241
2242 rc.left = 0;
2243 rc.top = 0;
2244 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2245 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2246 clear_rect(&rc);
2247}
2248/*
2249 * Menu stuff.
2250 */
2251
2252 void
2253gui_mch_enable_menu(int flag)
2254{
2255#ifdef FEAT_MENU
2256 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2257#endif
2258}
2259
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002260 void
2261gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002262 int x UNUSED,
2263 int y UNUSED,
2264 int w UNUSED,
2265 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002266{
2267 /* It will be in the right place anyway */
2268}
2269
2270#if defined(FEAT_MENU) || defined(PROTO)
2271/*
2272 * Make menu item hidden or not hidden
2273 */
2274 void
2275gui_mch_menu_hidden(
2276 vimmenu_T *menu,
2277 int hidden)
2278{
2279 /*
2280 * This doesn't do what we want. Hmm, just grey the menu items for now.
2281 */
2282 /*
2283 if (hidden)
2284 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2285 else
2286 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2287 */
2288 gui_mch_menu_grey(menu, hidden);
2289}
2290
2291/*
2292 * This is called after setting all the menus to grey/hidden or not.
2293 */
2294 void
2295gui_mch_draw_menubar(void)
2296{
2297 DrawMenuBar(s_hwnd);
2298}
2299#endif /*FEAT_MENU*/
2300
2301#ifndef PROTO
2302void
2303#ifdef VIMDLL
2304_export
2305#endif
2306_cdecl
2307SaveInst(HINSTANCE hInst)
2308{
2309 s_hinst = hInst;
2310}
2311#endif
2312
2313/*
2314 * Return the RGB value of a pixel as a long.
2315 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002316 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002317gui_mch_get_rgb(guicolor_T pixel)
2318{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002319 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2320 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321}
2322
2323#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
2324/* Convert pixels in X to dialog units */
2325 static WORD
2326PixelToDialogX(int numPixels)
2327{
2328 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2329}
2330
2331/* Convert pixels in Y to dialog units */
2332 static WORD
2333PixelToDialogY(int numPixels)
2334{
2335 return (WORD)((numPixels * 8) / s_dlgfntheight);
2336}
2337
2338/* Return the width in pixels of the given text in the given DC. */
2339 static int
2340GetTextWidth(HDC hdc, char_u *str, int len)
2341{
2342 SIZE size;
2343
2344 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2345 return size.cx;
2346}
2347
2348#ifdef FEAT_MBYTE
2349/*
2350 * Return the width in pixels of the given text in the given DC, taking care
2351 * of 'encoding' to active codepage conversion.
2352 */
2353 static int
2354GetTextWidthEnc(HDC hdc, char_u *str, int len)
2355{
2356 SIZE size;
2357 WCHAR *wstr;
2358 int n;
2359 int wlen = len;
2360
2361 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2362 {
2363 /* 'encoding' differs from active codepage: convert text and use wide
2364 * function */
2365 wstr = enc_to_utf16(str, &wlen);
2366 if (wstr != NULL)
2367 {
2368 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2369 vim_free(wstr);
2370 if (n)
2371 return size.cx;
2372 }
2373 }
2374
2375 return GetTextWidth(hdc, str, len);
2376}
2377#else
2378# define GetTextWidthEnc(h, s, l) GetTextWidth((h), (s), (l))
2379#endif
2380
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002381static void get_work_area(RECT *spi_rect);
2382
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002383/*
2384 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002385 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2386 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002387 */
2388 static BOOL
2389CenterWindow(
2390 HWND hwndChild,
2391 HWND hwndParent)
2392{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002393 HMONITOR mon;
2394 MONITORINFO moninfo;
2395 RECT rChild, rParent, rScreen;
2396 int wChild, hChild, wParent, hParent;
2397 int xNew, yNew;
2398 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399
2400 GetWindowRect(hwndChild, &rChild);
2401 wChild = rChild.right - rChild.left;
2402 hChild = rChild.bottom - rChild.top;
2403
2404 /* If Vim is minimized put the window in the middle of the screen. */
2405 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002406 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 else
2408 GetWindowRect(hwndParent, &rParent);
2409 wParent = rParent.right - rParent.left;
2410 hParent = rParent.bottom - rParent.top;
2411
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002412 moninfo.cbSize = sizeof(MONITORINFO);
2413 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2414 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002416 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002417 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002418 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002420 hdc = GetDC(hwndChild);
2421 rScreen.left = 0;
2422 rScreen.top = 0;
2423 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2424 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2425 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426 }
2427
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002428 xNew = rParent.left + ((wParent - wChild) / 2);
2429 if (xNew < rScreen.left)
2430 xNew = rScreen.left;
2431 else if ((xNew + wChild) > rScreen.right)
2432 xNew = rScreen.right - wChild;
2433
2434 yNew = rParent.top + ((hParent - hChild) / 2);
2435 if (yNew < rScreen.top)
2436 yNew = rScreen.top;
2437 else if ((yNew + hChild) > rScreen.bottom)
2438 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002439
2440 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2441 SWP_NOSIZE | SWP_NOZORDER);
2442}
2443#endif /* FEAT_GUI_DIALOG */
2444
2445void
2446gui_mch_activate_window(void)
2447{
2448 (void)SetActiveWindow(s_hwnd);
2449}
2450
2451#if defined(FEAT_TOOLBAR) || defined(PROTO)
2452 void
2453gui_mch_show_toolbar(int showit)
2454{
2455 if (s_toolbarhwnd == NULL)
2456 return;
2457
2458 if (showit)
2459 {
2460# ifdef FEAT_MBYTE
2461# ifndef TB_SETUNICODEFORMAT
2462 /* For older compilers. We assume this never changes. */
2463# define TB_SETUNICODEFORMAT 0x2005
2464# endif
2465 /* Enable/disable unicode support */
2466 int uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2467 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2468# endif
2469 ShowWindow(s_toolbarhwnd, SW_SHOW);
2470 }
2471 else
2472 ShowWindow(s_toolbarhwnd, SW_HIDE);
2473}
2474
2475/* Then number of bitmaps is fixed. Exit is missing! */
2476#define TOOLBAR_BITMAP_COUNT 31
2477
2478#endif
2479
2480#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2481 static void
2482add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2483{
2484#ifdef FEAT_MBYTE
2485 WCHAR *wn = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002486
2487 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2488 {
2489 /* 'encoding' differs from active codepage: convert menu name
2490 * and use wide function */
2491 wn = enc_to_utf16(item_text, NULL);
2492 if (wn != NULL)
2493 {
2494 MENUITEMINFOW infow;
2495
2496 infow.cbSize = sizeof(infow);
2497 infow.fMask = MIIM_TYPE | MIIM_ID;
2498 infow.wID = item_id;
2499 infow.fType = MFT_STRING;
2500 infow.dwTypeData = wn;
2501 infow.cch = (UINT)wcslen(wn);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002502 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002503 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002504 }
2505 }
2506
2507 if (wn == NULL)
2508#endif
2509 {
2510 MENUITEMINFO info;
2511
2512 info.cbSize = sizeof(info);
2513 info.fMask = MIIM_TYPE | MIIM_ID;
2514 info.wID = item_id;
2515 info.fType = MFT_STRING;
2516 info.dwTypeData = (LPTSTR)item_text;
2517 info.cch = (UINT)STRLEN(item_text);
2518 InsertMenuItem(pmenu, item_id, FALSE, &info);
2519 }
2520}
2521
2522 static void
2523show_tabline_popup_menu(void)
2524{
2525 HMENU tab_pmenu;
2526 long rval;
2527 POINT pt;
2528
2529 /* When ignoring events don't show the menu. */
2530 if (hold_gui_events
2531# ifdef FEAT_CMDWIN
2532 || cmdwin_type != 0
2533# endif
2534 )
2535 return;
2536
2537 tab_pmenu = CreatePopupMenu();
2538 if (tab_pmenu == NULL)
2539 return;
2540
2541 if (first_tabpage->tp_next != NULL)
2542 add_tabline_popup_menu_entry(tab_pmenu,
2543 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2544 add_tabline_popup_menu_entry(tab_pmenu,
2545 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2546 add_tabline_popup_menu_entry(tab_pmenu,
2547 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2548
2549 GetCursorPos(&pt);
2550 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2551 NULL);
2552
2553 DestroyMenu(tab_pmenu);
2554
2555 /* Add the string cmd into input buffer */
2556 if (rval > 0)
2557 {
2558 TCHITTESTINFO htinfo;
2559 int idx;
2560
2561 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2562 return;
2563
2564 htinfo.pt.x = pt.x;
2565 htinfo.pt.y = pt.y;
2566 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2567 if (idx == -1)
2568 idx = 0;
2569 else
2570 idx += 1;
2571
2572 send_tabline_menu_event(idx, (int)rval);
2573 }
2574}
2575
2576/*
2577 * Show or hide the tabline.
2578 */
2579 void
2580gui_mch_show_tabline(int showit)
2581{
2582 if (s_tabhwnd == NULL)
2583 return;
2584
2585 if (!showit != !showing_tabline)
2586 {
2587 if (showit)
2588 ShowWindow(s_tabhwnd, SW_SHOW);
2589 else
2590 ShowWindow(s_tabhwnd, SW_HIDE);
2591 showing_tabline = showit;
2592 }
2593}
2594
2595/*
2596 * Return TRUE when tabline is displayed.
2597 */
2598 int
2599gui_mch_showing_tabline(void)
2600{
2601 return s_tabhwnd != NULL && showing_tabline;
2602}
2603
2604/*
2605 * Update the labels of the tabline.
2606 */
2607 void
2608gui_mch_update_tabline(void)
2609{
2610 tabpage_T *tp;
2611 TCITEM tie;
2612 int nr = 0;
2613 int curtabidx = 0;
2614 int tabadded = 0;
2615#ifdef FEAT_MBYTE
2616 static int use_unicode = FALSE;
2617 int uu;
2618 WCHAR *wstr = NULL;
2619#endif
2620
2621 if (s_tabhwnd == NULL)
2622 return;
2623
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002624#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002625# ifndef CCM_SETUNICODEFORMAT
2626 /* For older compilers. We assume this never changes. */
2627# define CCM_SETUNICODEFORMAT 0x2005
2628# endif
2629 uu = (enc_codepage >= 0 && (int)GetACP() != enc_codepage);
2630 if (uu != use_unicode)
2631 {
2632 /* Enable/disable unicode support */
2633 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)uu, (LPARAM)0);
2634 use_unicode = uu;
2635 }
2636#endif
2637
2638 tie.mask = TCIF_TEXT;
2639 tie.iImage = -1;
2640
2641 /* Disable redraw for tab updates to eliminate O(N^2) draws. */
2642 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2643
2644 /* Add a label for each tab page. They all contain the same text area. */
2645 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2646 {
2647 if (tp == curtab)
2648 curtabidx = nr;
2649
2650 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2651 {
2652 /* Add the tab */
2653 tie.pszText = "-Empty-";
2654 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2655 tabadded = 1;
2656 }
2657
2658 get_tabline_label(tp, FALSE);
2659 tie.pszText = (LPSTR)NameBuff;
2660#ifdef FEAT_MBYTE
2661 wstr = NULL;
2662 if (use_unicode)
2663 {
2664 /* Need to go through Unicode. */
2665 wstr = enc_to_utf16(NameBuff, NULL);
2666 if (wstr != NULL)
2667 {
2668 TCITEMW tiw;
2669
2670 tiw.mask = TCIF_TEXT;
2671 tiw.iImage = -1;
2672 tiw.pszText = wstr;
2673 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2674 vim_free(wstr);
2675 }
2676 }
2677 if (wstr == NULL)
2678#endif
2679 {
2680 TabCtrl_SetItem(s_tabhwnd, nr, &tie);
2681 }
2682 }
2683
2684 /* Remove any old labels. */
2685 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2686 TabCtrl_DeleteItem(s_tabhwnd, nr);
2687
2688 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2689 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2690
2691 /* Re-enable redraw and redraw. */
2692 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2693 RedrawWindow(s_tabhwnd, NULL, NULL,
2694 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2695
2696 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2697 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2698}
2699
2700/*
2701 * Set the current tab to "nr". First tab is 1.
2702 */
2703 void
2704gui_mch_set_curtab(int nr)
2705{
2706 if (s_tabhwnd == NULL)
2707 return;
2708
2709 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2710 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2711}
2712
2713#endif
2714
2715/*
2716 * ":simalt" command.
2717 */
2718 void
2719ex_simalt(exarg_T *eap)
2720{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002721 char_u *keys = eap->arg;
2722 int fill_typebuf = FALSE;
2723 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724
2725 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2726 while (*keys)
2727 {
2728 if (*keys == '~')
2729 *keys = ' '; /* for showing system menu */
2730 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2731 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002732 fill_typebuf = TRUE;
2733 }
2734 if (fill_typebuf)
2735 {
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002736 /* Put a NOP in the typeahead buffer so that the message will get
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002737 * processed. */
2738 key_name[0] = K_SPECIAL;
2739 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002740 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002741 key_name[3] = NUL;
2742 typebuf_was_filled = TRUE;
2743 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 }
2745}
2746
2747/*
2748 * Create the find & replace dialogs.
2749 * You can't have both at once: ":find" when replace is showing, destroys
2750 * the replace dialog first, and the other way around.
2751 */
2752#ifdef MSWIN_FIND_REPLACE
2753 static void
2754initialise_findrep(char_u *initial_string)
2755{
2756 int wword = FALSE;
2757 int mcase = !p_ic;
2758 char_u *entry_text;
2759
2760 /* Get the search string to use. */
2761 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2762
2763 s_findrep_struct.hwndOwner = s_hwnd;
2764 s_findrep_struct.Flags = FR_DOWN;
2765 if (mcase)
2766 s_findrep_struct.Flags |= FR_MATCHCASE;
2767 if (wword)
2768 s_findrep_struct.Flags |= FR_WHOLEWORD;
2769 if (entry_text != NULL && *entry_text != NUL)
2770 vim_strncpy((char_u *)s_findrep_struct.lpstrFindWhat, entry_text,
2771 s_findrep_struct.wFindWhatLen - 1);
2772 vim_free(entry_text);
2773}
2774#endif
2775
2776 static void
2777set_window_title(HWND hwnd, char *title)
2778{
2779#ifdef FEAT_MBYTE
2780 if (title != NULL && enc_codepage >= 0 && enc_codepage != (int)GetACP())
2781 {
2782 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783
2784 /* Convert the title from 'encoding' to UTF-16. */
2785 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2786 if (wbuf != NULL)
2787 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002788 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002789 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002790 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002791 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002792 }
2793#endif
2794 (void)SetWindowText(hwnd, (LPCSTR)title);
2795}
2796
2797 void
2798gui_mch_find_dialog(exarg_T *eap)
2799{
2800#ifdef MSWIN_FIND_REPLACE
2801 if (s_findrep_msg != 0)
2802 {
2803 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2804 DestroyWindow(s_findrep_hwnd);
2805
2806 if (!IsWindow(s_findrep_hwnd))
2807 {
2808 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002809# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002810 /* If the OS is Windows NT, and 'encoding' differs from active
2811 * codepage: convert text and use wide function. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002812 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002813 {
2814 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2815 s_findrep_hwnd = FindTextW(
2816 (LPFINDREPLACEW) &s_findrep_struct_w);
2817 }
2818 else
2819# endif
2820 s_findrep_hwnd = FindText((LPFINDREPLACE) &s_findrep_struct);
2821 }
2822
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002823 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002824 (void)SetFocus(s_findrep_hwnd);
2825
2826 s_findrep_is_find = TRUE;
2827 }
2828#endif
2829}
2830
2831
2832 void
2833gui_mch_replace_dialog(exarg_T *eap)
2834{
2835#ifdef MSWIN_FIND_REPLACE
2836 if (s_findrep_msg != 0)
2837 {
2838 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2839 DestroyWindow(s_findrep_hwnd);
2840
2841 if (!IsWindow(s_findrep_hwnd))
2842 {
2843 initialise_findrep(eap->arg);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002844# ifdef FEAT_MBYTE
2845 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002846 {
2847 findrep_atow(&s_findrep_struct_w, &s_findrep_struct);
2848 s_findrep_hwnd = ReplaceTextW(
2849 (LPFINDREPLACEW) &s_findrep_struct_w);
2850 }
2851 else
2852# endif
2853 s_findrep_hwnd = ReplaceText(
2854 (LPFINDREPLACE) &s_findrep_struct);
2855 }
2856
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002857 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002858 (void)SetFocus(s_findrep_hwnd);
2859
2860 s_findrep_is_find = FALSE;
2861 }
2862#endif
2863}
2864
2865
2866/*
2867 * Set visibility of the pointer.
2868 */
2869 void
2870gui_mch_mousehide(int hide)
2871{
2872 if (hide != gui.pointer_hidden)
2873 {
2874 ShowCursor(!hide);
2875 gui.pointer_hidden = hide;
2876 }
2877}
2878
2879#ifdef FEAT_MENU
2880 static void
2881gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2882{
2883 /* Unhide the mouse, we don't get move events here. */
2884 gui_mch_mousehide(FALSE);
2885
2886 (void)TrackPopupMenu(
2887 (HMENU)menu->submenu_id,
2888 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2889 x, y,
2890 (int)0, /*reserved param*/
2891 s_hwnd,
2892 NULL);
2893 /*
2894 * NOTE: The pop-up menu can eat the mouse up event.
2895 * We deal with this in normal.c.
2896 */
2897}
2898#endif
2899
2900/*
2901 * Got a message when the system will go down.
2902 */
2903 static void
2904_OnEndSession(void)
2905{
2906 getout_preserve_modified(1);
2907}
2908
2909/*
2910 * Get this message when the user clicks on the cross in the top right corner
2911 * of a Windows95 window.
2912 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002914_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915{
2916 gui_shell_closed();
2917}
2918
2919/*
2920 * Get a message when the window is being destroyed.
2921 */
2922 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002923_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924{
2925 if (!destroying)
2926 _OnClose(hwnd);
2927}
2928
2929 static void
2930_OnPaint(
2931 HWND hwnd)
2932{
2933 if (!IsMinimized(hwnd))
2934 {
2935 PAINTSTRUCT ps;
2936
2937 out_flush(); /* make sure all output has been processed */
2938 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939
2940#ifdef FEAT_MBYTE
2941 /* prevent multi-byte characters from misprinting on an invalid
2942 * rectangle */
2943 if (has_mbyte)
2944 {
2945 RECT rect;
2946
2947 GetClientRect(hwnd, &rect);
2948 ps.rcPaint.left = rect.left;
2949 ps.rcPaint.right = rect.right;
2950 }
2951#endif
2952
2953 if (!IsRectEmpty(&ps.rcPaint))
2954 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002955 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2956 ps.rcPaint.right - ps.rcPaint.left + 1,
2957 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2958 }
2959
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960 EndPaint(hwnd, &ps);
2961 }
2962}
2963
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002964 static void
2965_OnSize(
2966 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002967 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002968 int cx,
2969 int cy)
2970{
2971 if (!IsMinimized(hwnd))
2972 {
2973 gui_resize_shell(cx, cy);
2974
2975#ifdef FEAT_MENU
2976 /* Menu bar may wrap differently now */
2977 gui_mswin_get_menu_height(TRUE);
2978#endif
2979 }
2980}
2981
2982 static void
2983_OnSetFocus(
2984 HWND hwnd,
2985 HWND hwndOldFocus)
2986{
2987 gui_focus_change(TRUE);
2988 s_getting_focus = TRUE;
2989 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2990}
2991
2992 static void
2993_OnKillFocus(
2994 HWND hwnd,
2995 HWND hwndNewFocus)
2996{
2997 gui_focus_change(FALSE);
2998 s_getting_focus = FALSE;
2999 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
3000}
3001
3002/*
3003 * Get a message when the user switches back to vim
3004 */
3005 static LRESULT
3006_OnActivateApp(
3007 HWND hwnd,
3008 BOOL fActivate,
3009 DWORD dwThreadId)
3010{
3011 /* we call gui_focus_change() in _OnSetFocus() */
3012 /* gui_focus_change((int)fActivate); */
3013 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
3014}
3015
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 void
3017gui_mch_destroy_scrollbar(scrollbar_T *sb)
3018{
3019 DestroyWindow(sb->id);
3020}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003021
3022/*
3023 * Get current mouse coordinates in text window.
3024 */
3025 void
3026gui_mch_getmouse(int *x, int *y)
3027{
3028 RECT rct;
3029 POINT mp;
3030
3031 (void)GetWindowRect(s_textArea, &rct);
3032 (void)GetCursorPos((LPPOINT)&mp);
3033 *x = (int)(mp.x - rct.left);
3034 *y = (int)(mp.y - rct.top);
3035}
3036
3037/*
3038 * Move mouse pointer to character at (x, y).
3039 */
3040 void
3041gui_mch_setmouse(int x, int y)
3042{
3043 RECT rct;
3044
3045 (void)GetWindowRect(s_textArea, &rct);
3046 (void)SetCursorPos(x + gui.border_offset + rct.left,
3047 y + gui.border_offset + rct.top);
3048}
3049
3050 static void
3051gui_mswin_get_valid_dimensions(
3052 int w,
3053 int h,
3054 int *valid_w,
3055 int *valid_h)
3056{
3057 int base_width, base_height;
3058
3059 base_width = gui_get_base_width()
3060 + (GetSystemMetrics(SM_CXFRAME) +
3061 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
3062 base_height = gui_get_base_height()
3063 + (GetSystemMetrics(SM_CYFRAME) +
3064 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3065 + GetSystemMetrics(SM_CYCAPTION)
3066#ifdef FEAT_MENU
3067 + gui_mswin_get_menu_height(FALSE)
3068#endif
3069 ;
3070 *valid_w = base_width +
3071 ((w - base_width) / gui.char_width) * gui.char_width;
3072 *valid_h = base_height +
3073 ((h - base_height) / gui.char_height) * gui.char_height;
3074}
3075
3076 void
3077gui_mch_flash(int msec)
3078{
3079 RECT rc;
3080
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003081#if defined(FEAT_DIRECTX)
3082 if (IS_ENABLE_DIRECTX())
3083 DWriteContext_Flush(s_dwc);
3084#endif
3085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003086 /*
3087 * Note: InvertRect() excludes right and bottom of rectangle.
3088 */
3089 rc.left = 0;
3090 rc.top = 0;
3091 rc.right = gui.num_cols * gui.char_width;
3092 rc.bottom = gui.num_rows * gui.char_height;
3093 InvertRect(s_hdc, &rc);
3094 gui_mch_flush(); /* make sure it's displayed */
3095
3096 ui_delay((long)msec, TRUE); /* wait for a few msec */
3097
3098 InvertRect(s_hdc, &rc);
3099}
3100
3101/*
3102 * Return flags used for scrolling.
3103 * The SW_INVALIDATE is required when part of the window is covered or
3104 * off-screen. Refer to MS KB Q75236.
3105 */
3106 static int
3107get_scroll_flags(void)
3108{
3109 HWND hwnd;
3110 RECT rcVim, rcOther, rcDest;
3111
3112 GetWindowRect(s_hwnd, &rcVim);
3113
3114 /* Check if the window is partly above or below the screen. We don't care
3115 * about partly left or right of the screen, it is not relevant when
3116 * scrolling up or down. */
3117 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3118 return SW_INVALIDATE;
3119
3120 /* Check if there is an window (partly) on top of us. */
3121 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3122 if (IsWindowVisible(hwnd))
3123 {
3124 GetWindowRect(hwnd, &rcOther);
3125 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3126 return SW_INVALIDATE;
3127 }
3128 return 0;
3129}
3130
3131/*
3132 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3133 * may not be scrolled out properly.
3134 * For gVim, when _OnScroll() is repeated, the character at the
3135 * previous cursor position may be left drawn after scroll.
3136 * The problem can be avoided by calling GetPixel() to get a pixel in
3137 * the region before ScrollWindowEx().
3138 */
3139 static void
3140intel_gpu_workaround(void)
3141{
3142 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3143}
3144
3145/*
3146 * Delete the given number of lines from the given row, scrolling up any
3147 * text further down within the scroll region.
3148 */
3149 void
3150gui_mch_delete_lines(
3151 int row,
3152 int num_lines)
3153{
3154 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003155
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003156 rc.left = FILL_X(gui.scroll_region_left);
3157 rc.right = FILL_X(gui.scroll_region_right + 1);
3158 rc.top = FILL_Y(row);
3159 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3160
Bram Moolenaar92467d32017-12-05 13:22:16 +01003161#if defined(FEAT_DIRECTX)
3162 if (IS_ENABLE_DIRECTX())
3163 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003164 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3165 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003166 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003167 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003168#endif
3169 {
3170 intel_gpu_workaround();
3171 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003172 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003173 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003174 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003176 /* This seems to be required to avoid the cursor disappearing when
3177 * scrolling such that the cursor ends up in the top-left character on
3178 * the screen... But why? (Webb) */
3179 /* It's probably fixed by disabling drawing the cursor while scrolling. */
3180 /* gui.cursor_is_valid = FALSE; */
3181
3182 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3183 gui.scroll_region_left,
3184 gui.scroll_region_bot, gui.scroll_region_right);
3185}
3186
3187/*
3188 * Insert the given number of lines before the given row, scrolling down any
3189 * following text within the scroll region.
3190 */
3191 void
3192gui_mch_insert_lines(
3193 int row,
3194 int num_lines)
3195{
3196 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003197
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 rc.left = FILL_X(gui.scroll_region_left);
3199 rc.right = FILL_X(gui.scroll_region_right + 1);
3200 rc.top = FILL_Y(row);
3201 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003202
3203#if defined(FEAT_DIRECTX)
3204 if (IS_ENABLE_DIRECTX())
3205 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003206 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3207 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003208 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003209 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003210#endif
3211 {
3212 intel_gpu_workaround();
3213 /* The SW_INVALIDATE is required when part of the window is covered or
3214 * off-screen. How do we avoid it when it's not needed? */
3215 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003216 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003217 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003218 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003219
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003220 gui_clear_block(row, gui.scroll_region_left,
3221 row + num_lines - 1, gui.scroll_region_right);
3222}
3223
3224
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003225 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003226gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227{
3228#if defined(FEAT_DIRECTX)
3229 DWriteContext_Close(s_dwc);
3230 DWrite_Final();
3231 s_dwc = NULL;
3232#endif
3233
3234 ReleaseDC(s_textArea, s_hdc);
3235 DeleteObject(s_brush);
3236
3237#ifdef FEAT_TEAROFF
3238 /* Unload the tearoff bitmap */
3239 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3240#endif
3241
3242 /* Destroy our window (if we have one). */
3243 if (s_hwnd != NULL)
3244 {
3245 destroying = TRUE; /* ignore WM_DESTROY message now */
3246 DestroyWindow(s_hwnd);
3247 }
3248
3249#ifdef GLOBAL_IME
3250 global_ime_end();
3251#endif
3252}
3253
3254 static char_u *
3255logfont2name(LOGFONT lf)
3256{
3257 char *p;
3258 char *res;
3259 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003260 char *quality_name;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261 char *font_name = lf.lfFaceName;
3262
3263 charset_name = charset_id2name((int)lf.lfCharSet);
3264#ifdef FEAT_MBYTE
3265 /* Convert a font name from the current codepage to 'encoding'.
3266 * TODO: Use Wide APIs (including LOGFONTW) instead of ANSI APIs. */
3267 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3268 {
3269 int len;
3270 acp_to_enc((char_u *)lf.lfFaceName, (int)strlen(lf.lfFaceName),
3271 (char_u **)&font_name, &len);
3272 }
3273#endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003274 quality_name = quality_id2name((int)lf.lfQuality);
3275
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003276 res = (char *)alloc((unsigned)(strlen(font_name) + 20
3277 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)));
3278 if (res != NULL)
3279 {
3280 p = res;
3281 /* make a normal font string out of the lf thing:*/
3282 sprintf((char *)p, "%s:h%d", font_name, pixels_to_points(
3283 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE));
3284 while (*p)
3285 {
3286 if (*p == ' ')
3287 *p = '_';
3288 ++p;
3289 }
3290 if (lf.lfItalic)
3291 STRCAT(p, ":i");
3292 if (lf.lfWeight >= FW_BOLD)
3293 STRCAT(p, ":b");
3294 if (lf.lfUnderline)
3295 STRCAT(p, ":u");
3296 if (lf.lfStrikeOut)
3297 STRCAT(p, ":s");
3298 if (charset_name != NULL)
3299 {
3300 STRCAT(p, ":c");
3301 STRCAT(p, charset_name);
3302 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003303 if (quality_name != NULL)
3304 {
3305 STRCAT(p, ":q");
3306 STRCAT(p, quality_name);
3307 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 }
3309
3310#ifdef FEAT_MBYTE
3311 if (font_name != lf.lfFaceName)
3312 vim_free(font_name);
3313#endif
3314 return (char_u *)res;
3315}
3316
3317
3318#ifdef FEAT_MBYTE_IME
3319/*
3320 * Set correct LOGFONT to IME. Use 'guifontwide' if available, otherwise use
3321 * 'guifont'
3322 */
3323 static void
3324update_im_font(void)
3325{
3326 LOGFONT lf_wide;
3327
3328 if (p_guifontwide != NULL && *p_guifontwide != NUL
3329 && gui.wide_font != NOFONT
3330 && GetObject((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
3331 norm_logfont = lf_wide;
3332 else
3333 norm_logfont = sub_logfont;
3334 im_set_font(&norm_logfont);
3335}
3336#endif
3337
3338#ifdef FEAT_MBYTE
3339/*
3340 * Handler of gui.wide_font (p_guifontwide) changed notification.
3341 */
3342 void
3343gui_mch_wide_font_changed(void)
3344{
3345 LOGFONT lf;
3346
3347# ifdef FEAT_MBYTE_IME
3348 update_im_font();
3349# endif
3350
3351 gui_mch_free_font(gui.wide_ital_font);
3352 gui.wide_ital_font = NOFONT;
3353 gui_mch_free_font(gui.wide_bold_font);
3354 gui.wide_bold_font = NOFONT;
3355 gui_mch_free_font(gui.wide_boldital_font);
3356 gui.wide_boldital_font = NOFONT;
3357
3358 if (gui.wide_font
3359 && GetObject((HFONT)gui.wide_font, sizeof(lf), &lf))
3360 {
3361 if (!lf.lfItalic)
3362 {
3363 lf.lfItalic = TRUE;
3364 gui.wide_ital_font = get_font_handle(&lf);
3365 lf.lfItalic = FALSE;
3366 }
3367 if (lf.lfWeight < FW_BOLD)
3368 {
3369 lf.lfWeight = FW_BOLD;
3370 gui.wide_bold_font = get_font_handle(&lf);
3371 if (!lf.lfItalic)
3372 {
3373 lf.lfItalic = TRUE;
3374 gui.wide_boldital_font = get_font_handle(&lf);
3375 }
3376 }
3377 }
3378}
3379#endif
3380
3381/*
3382 * Initialise vim to use the font with the given name.
3383 * Return FAIL if the font could not be loaded, OK otherwise.
3384 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003385 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003386gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387{
3388 LOGFONT lf;
3389 GuiFont font = NOFONT;
3390 char_u *p;
3391
3392 /* Load the font */
3393 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3394 font = get_font_handle(&lf);
3395 if (font == NOFONT)
3396 return FAIL;
3397
3398 if (font_name == NULL)
3399 font_name = (char_u *)lf.lfFaceName;
3400#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3401 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003402#endif
3403#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404 sub_logfont = lf;
3405#endif
3406#ifdef FEAT_MBYTE_IME
3407 update_im_font();
3408#endif
3409 gui_mch_free_font(gui.norm_font);
3410 gui.norm_font = font;
3411 current_font_height = lf.lfHeight;
3412 GetFontSize(font);
3413
3414 p = logfont2name(lf);
3415 if (p != NULL)
3416 {
3417 hl_set_font_name(p);
3418
3419 /* When setting 'guifont' to "*" replace it with the actual font name.
3420 * */
3421 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3422 {
3423 vim_free(p_guifont);
3424 p_guifont = p;
3425 }
3426 else
3427 vim_free(p);
3428 }
3429
3430 gui_mch_free_font(gui.ital_font);
3431 gui.ital_font = NOFONT;
3432 gui_mch_free_font(gui.bold_font);
3433 gui.bold_font = NOFONT;
3434 gui_mch_free_font(gui.boldital_font);
3435 gui.boldital_font = NOFONT;
3436
3437 if (!lf.lfItalic)
3438 {
3439 lf.lfItalic = TRUE;
3440 gui.ital_font = get_font_handle(&lf);
3441 lf.lfItalic = FALSE;
3442 }
3443 if (lf.lfWeight < FW_BOLD)
3444 {
3445 lf.lfWeight = FW_BOLD;
3446 gui.bold_font = get_font_handle(&lf);
3447 if (!lf.lfItalic)
3448 {
3449 lf.lfItalic = TRUE;
3450 gui.boldital_font = get_font_handle(&lf);
3451 }
3452 }
3453
3454 return OK;
3455}
3456
3457#ifndef WPF_RESTORETOMAXIMIZED
3458# define WPF_RESTORETOMAXIMIZED 2 /* just in case someone doesn't have it */
3459#endif
3460
3461/*
3462 * Return TRUE if the GUI window is maximized, filling the whole screen.
3463 */
3464 int
3465gui_mch_maximized(void)
3466{
3467 WINDOWPLACEMENT wp;
3468
3469 wp.length = sizeof(WINDOWPLACEMENT);
3470 if (GetWindowPlacement(s_hwnd, &wp))
3471 return wp.showCmd == SW_SHOWMAXIMIZED
3472 || (wp.showCmd == SW_SHOWMINIMIZED
3473 && wp.flags == WPF_RESTORETOMAXIMIZED);
3474
3475 return 0;
3476}
3477
3478/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003479 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3480 * is set. Compute the new Rows and Columns. This is like resizing the
3481 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482 */
3483 void
3484gui_mch_newfont(void)
3485{
3486 RECT rect;
3487
3488 GetWindowRect(s_hwnd, &rect);
3489 if (win_socket_id == 0)
3490 {
3491 gui_resize_shell(rect.right - rect.left
3492 - (GetSystemMetrics(SM_CXFRAME) +
3493 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3494 rect.bottom - rect.top
3495 - (GetSystemMetrics(SM_CYFRAME) +
3496 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3497 - GetSystemMetrics(SM_CYCAPTION)
3498#ifdef FEAT_MENU
3499 - gui_mswin_get_menu_height(FALSE)
3500#endif
3501 );
3502 }
3503 else
3504 {
3505 /* Inside another window, don't use the frame and border. */
3506 gui_resize_shell(rect.right - rect.left,
3507 rect.bottom - rect.top
3508#ifdef FEAT_MENU
3509 - gui_mswin_get_menu_height(FALSE)
3510#endif
3511 );
3512 }
3513}
3514
3515/*
3516 * Set the window title
3517 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003518 void
3519gui_mch_settitle(
3520 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003521 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522{
3523 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3524}
3525
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003526#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527/* Table for shape IDCs. Keep in sync with the mshape_names[] table in
3528 * misc2.c! */
3529static LPCSTR mshape_idcs[] =
3530{
3531 IDC_ARROW, /* arrow */
3532 MAKEINTRESOURCE(0), /* blank */
3533 IDC_IBEAM, /* beam */
3534 IDC_SIZENS, /* updown */
3535 IDC_SIZENS, /* udsizing */
3536 IDC_SIZEWE, /* leftright */
3537 IDC_SIZEWE, /* lrsizing */
3538 IDC_WAIT, /* busy */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539 IDC_NO, /* no */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 IDC_ARROW, /* crosshair */
3541 IDC_ARROW, /* hand1 */
3542 IDC_ARROW, /* hand2 */
3543 IDC_ARROW, /* pencil */
3544 IDC_ARROW, /* question */
3545 IDC_ARROW, /* right-arrow */
3546 IDC_UPARROW, /* up-arrow */
3547 IDC_ARROW /* last one */
3548};
3549
3550 void
3551mch_set_mouse_shape(int shape)
3552{
3553 LPCSTR idc;
3554
3555 if (shape == MSHAPE_HIDE)
3556 ShowCursor(FALSE);
3557 else
3558 {
3559 if (shape >= MSHAPE_NUMBERED)
3560 idc = IDC_ARROW;
3561 else
3562 idc = mshape_idcs[shape];
3563#ifdef SetClassLongPtr
3564 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (__int3264)(LONG_PTR)LoadCursor(NULL, idc));
3565#else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566 SetClassLong(s_textArea, GCL_HCURSOR, (long_u)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567#endif
3568 if (!p_mh)
3569 {
3570 POINT mp;
3571
3572 /* Set the position to make it redrawn with the new shape. */
3573 (void)GetCursorPos((LPPOINT)&mp);
3574 (void)SetCursorPos(mp.x, mp.y);
3575 ShowCursor(TRUE);
3576 }
3577 }
3578}
3579#endif
3580
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003581#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003582/*
3583 * The file browser exists in two versions: with "W" uses wide characters,
3584 * without "W" the current codepage. When FEAT_MBYTE is defined and on
3585 * Windows NT/2000/XP the "W" functions are used.
3586 */
3587
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003588# ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003589/*
3590 * Wide version of convert_filter().
3591 */
3592 static WCHAR *
3593convert_filterW(char_u *s)
3594{
3595 char_u *tmp;
3596 int len;
3597 WCHAR *res;
3598
3599 tmp = convert_filter(s);
3600 if (tmp == NULL)
3601 return NULL;
3602 len = (int)STRLEN(s) + 3;
3603 res = enc_to_utf16(tmp, &len);
3604 vim_free(tmp);
3605 return res;
3606}
3607
3608/*
3609 * Wide version of gui_mch_browse(). Keep in sync!
3610 */
3611 static char_u *
3612gui_mch_browseW(
3613 int saving,
3614 char_u *title,
3615 char_u *dflt,
3616 char_u *ext,
3617 char_u *initdir,
3618 char_u *filter)
3619{
3620 /* We always use the wide function. This means enc_to_utf16() must work,
3621 * otherwise it fails miserably! */
3622 OPENFILENAMEW fileStruct;
3623 WCHAR fileBuf[MAXPATHL];
3624 WCHAR *wp;
3625 int i;
3626 WCHAR *titlep = NULL;
3627 WCHAR *extp = NULL;
3628 WCHAR *initdirp = NULL;
3629 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003630 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631
3632 if (dflt == NULL)
3633 fileBuf[0] = NUL;
3634 else
3635 {
3636 wp = enc_to_utf16(dflt, NULL);
3637 if (wp == NULL)
3638 fileBuf[0] = NUL;
3639 else
3640 {
3641 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3642 fileBuf[i] = wp[i];
3643 fileBuf[i] = NUL;
3644 vim_free(wp);
3645 }
3646 }
3647
3648 /* Convert the filter to Windows format. */
3649 filterp = convert_filterW(filter);
3650
3651 vim_memset(&fileStruct, 0, sizeof(OPENFILENAMEW));
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003652# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 /* be compatible with Windows NT 4.0 */
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003654 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003655# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003657# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658
3659 if (title != NULL)
3660 titlep = enc_to_utf16(title, NULL);
3661 fileStruct.lpstrTitle = titlep;
3662
3663 if (ext != NULL)
3664 extp = enc_to_utf16(ext, NULL);
3665 fileStruct.lpstrDefExt = extp;
3666
3667 fileStruct.lpstrFile = fileBuf;
3668 fileStruct.nMaxFile = MAXPATHL;
3669 fileStruct.lpstrFilter = filterp;
3670 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3671 /* has an initial dir been specified? */
3672 if (initdir != NULL && *initdir != NUL)
3673 {
3674 /* Must have backslashes here, no matter what 'shellslash' says */
3675 initdirp = enc_to_utf16(initdir, NULL);
3676 if (initdirp != NULL)
3677 {
3678 for (wp = initdirp; *wp != NUL; ++wp)
3679 if (*wp == '/')
3680 *wp = '\\';
3681 }
3682 fileStruct.lpstrInitialDir = initdirp;
3683 }
3684
3685 /*
3686 * TODO: Allow selection of multiple files. Needs another arg to this
3687 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3688 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3689 * files that don't exist yet, so I haven't put it in. What about
3690 * OFN_PATHMUSTEXIST?
3691 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3692 */
3693 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003694# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 if (curbuf->b_p_bin)
3696 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01003697# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003698 if (saving)
3699 {
3700 if (!GetSaveFileNameW(&fileStruct))
3701 return NULL;
3702 }
3703 else
3704 {
3705 if (!GetOpenFileNameW(&fileStruct))
3706 return NULL;
3707 }
3708
3709 vim_free(filterp);
3710 vim_free(initdirp);
3711 vim_free(titlep);
3712 vim_free(extp);
3713
3714 /* Convert from UCS2 to 'encoding'. */
3715 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003716 if (p == NULL)
3717 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003718
3719 /* Give focus back to main window (when using MDI). */
3720 SetFocus(s_hwnd);
3721
3722 /* Shorten the file name if possible */
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003723 q = vim_strsave(shorten_fname1(p));
3724 vim_free(p);
3725 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726}
3727# endif /* FEAT_MBYTE */
3728
3729
3730/*
3731 * Convert the string s to the proper format for a filter string by replacing
3732 * the \t and \n delimiters with \0.
3733 * Returns the converted string in allocated memory.
3734 *
3735 * Keep in sync with convert_filterW() above!
3736 */
3737 static char_u *
3738convert_filter(char_u *s)
3739{
3740 char_u *res;
3741 unsigned s_len = (unsigned)STRLEN(s);
3742 unsigned i;
3743
3744 res = alloc(s_len + 3);
3745 if (res != NULL)
3746 {
3747 for (i = 0; i < s_len; ++i)
3748 if (s[i] == '\t' || s[i] == '\n')
3749 res[i] = '\0';
3750 else
3751 res[i] = s[i];
3752 res[s_len] = NUL;
3753 /* Add two extra NULs to make sure it's properly terminated. */
3754 res[s_len + 1] = NUL;
3755 res[s_len + 2] = NUL;
3756 }
3757 return res;
3758}
3759
3760/*
3761 * Select a directory.
3762 */
3763 char_u *
3764gui_mch_browsedir(char_u *title, char_u *initdir)
3765{
3766 /* We fake this: Use a filter that doesn't select anything and a default
3767 * file name that won't be used. */
3768 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3769 initdir, (char_u *)_("Directory\t*.nothing\n"));
3770}
3771
3772/*
3773 * Pop open a file browser and return the file selected, in allocated memory,
3774 * or NULL if Cancel is hit.
3775 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3776 * title - Title message for the file browser dialog.
3777 * dflt - Default name of file.
3778 * ext - Default extension to be added to files without extensions.
3779 * initdir - directory in which to open the browser (NULL = current dir)
3780 * filter - Filter for matched files to choose from.
3781 *
3782 * Keep in sync with gui_mch_browseW() above!
3783 */
3784 char_u *
3785gui_mch_browse(
3786 int saving,
3787 char_u *title,
3788 char_u *dflt,
3789 char_u *ext,
3790 char_u *initdir,
3791 char_u *filter)
3792{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003793# ifdef FEAT_MBYTE
3794 return gui_mch_browseW(saving, title, dflt, ext, initdir, filter);
3795# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 OPENFILENAME fileStruct;
3797 char_u fileBuf[MAXPATHL];
3798 char_u *initdirp = NULL;
3799 char_u *filterp;
3800 char_u *p;
3801
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 if (dflt == NULL)
3803 fileBuf[0] = NUL;
3804 else
3805 vim_strncpy(fileBuf, dflt, MAXPATHL - 1);
3806
3807 /* Convert the filter to Windows format. */
3808 filterp = convert_filter(filter);
3809
3810 vim_memset(&fileStruct, 0, sizeof(OPENFILENAME));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003811# ifdef OPENFILENAME_SIZE_VERSION_400
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 /* be compatible with Windows NT 4.0 */
3813 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003814# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003816# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817
3818 fileStruct.lpstrTitle = (LPSTR)title;
3819 fileStruct.lpstrDefExt = (LPSTR)ext;
3820
3821 fileStruct.lpstrFile = (LPSTR)fileBuf;
3822 fileStruct.nMaxFile = MAXPATHL;
3823 fileStruct.lpstrFilter = (LPSTR)filterp;
3824 fileStruct.hwndOwner = s_hwnd; /* main Vim window is owner*/
3825 /* has an initial dir been specified? */
3826 if (initdir != NULL && *initdir != NUL)
3827 {
3828 /* Must have backslashes here, no matter what 'shellslash' says */
3829 initdirp = vim_strsave(initdir);
3830 if (initdirp != NULL)
3831 for (p = initdirp; *p != NUL; ++p)
3832 if (*p == '/')
3833 *p = '\\';
3834 fileStruct.lpstrInitialDir = (LPSTR)initdirp;
3835 }
3836
3837 /*
3838 * TODO: Allow selection of multiple files. Needs another arg to this
3839 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3840 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3841 * files that don't exist yet, so I haven't put it in. What about
3842 * OFN_PATHMUSTEXIST?
3843 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3844 */
3845 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003846# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 if (curbuf->b_p_bin)
3848 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003849# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 if (saving)
3851 {
3852 if (!GetSaveFileName(&fileStruct))
3853 return NULL;
3854 }
3855 else
3856 {
3857 if (!GetOpenFileName(&fileStruct))
3858 return NULL;
3859 }
3860
3861 vim_free(filterp);
3862 vim_free(initdirp);
3863
3864 /* Give focus back to main window (when using MDI). */
3865 SetFocus(s_hwnd);
3866
3867 /* Shorten the file name if possible */
3868 return vim_strsave(shorten_fname1((char_u *)fileBuf));
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003869# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870}
3871#endif /* FEAT_BROWSE */
3872
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003873 static void
3874_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003875 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876 HDROP hDrop)
3877{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003878#define BUFPATHLEN _MAX_PATH
3879#define DRAGQVAL 0xFFFFFFFF
3880#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003881 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaar4033c552017-09-16 20:54:51 +02003882#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 char szFile[BUFPATHLEN];
3884 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3885 UINT i;
3886 char_u **fnames;
3887 POINT pt;
3888 int_u modifiers = 0;
3889
3890 /* TRACE("_OnDropFiles: %d files dropped\n", cFiles); */
3891
3892 /* Obtain dropped position */
3893 DragQueryPoint(hDrop, &pt);
3894 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3895
3896 reset_VIsual();
3897
3898 fnames = (char_u **)alloc(cFiles * sizeof(char_u *));
3899
3900 if (fnames != NULL)
3901 for (i = 0; i < cFiles; ++i)
3902 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003903#ifdef FEAT_MBYTE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3905 fnames[i] = utf16_to_enc(wszFile, NULL);
3906 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02003907#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 {
3909 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3910 fnames[i] = vim_strsave((char_u *)szFile);
3911 }
3912 }
3913
3914 DragFinish(hDrop);
3915
3916 if (fnames != NULL)
3917 {
3918 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3919 modifiers |= MOUSE_SHIFT;
3920 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3921 modifiers |= MOUSE_CTRL;
3922 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3923 modifiers |= MOUSE_ALT;
3924
3925 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3926
3927 s_need_activate = TRUE;
3928 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003929}
3930
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 static int
3932_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003933 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934 HWND hwndCtl,
3935 UINT code,
3936 int pos)
3937{
3938 static UINT prev_code = 0; /* code of previous call */
3939 scrollbar_T *sb, *sb_info;
3940 long val;
3941 int dragging = FALSE;
3942 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003943 SCROLLINFO si;
3944
3945 si.cbSize = sizeof(si);
3946 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003947
3948 sb = gui_mswin_find_scrollbar(hwndCtl);
3949 if (sb == NULL)
3950 return 0;
3951
3952 if (sb->wp != NULL) /* Left or right scrollbar */
3953 {
3954 /*
3955 * Careful: need to get scrollbar info out of first (left) scrollbar
3956 * for window, but keep real scrollbar too because we must pass it to
3957 * gui_drag_scrollbar().
3958 */
3959 sb_info = &sb->wp->w_scrollbars[0];
3960 }
3961 else /* Bottom scrollbar */
3962 sb_info = sb;
3963 val = sb_info->value;
3964
3965 switch (code)
3966 {
3967 case SB_THUMBTRACK:
3968 val = pos;
3969 dragging = TRUE;
3970 if (sb->scroll_shift > 0)
3971 val <<= sb->scroll_shift;
3972 break;
3973 case SB_LINEDOWN:
3974 val++;
3975 break;
3976 case SB_LINEUP:
3977 val--;
3978 break;
3979 case SB_PAGEDOWN:
3980 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3981 break;
3982 case SB_PAGEUP:
3983 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3984 break;
3985 case SB_TOP:
3986 val = 0;
3987 break;
3988 case SB_BOTTOM:
3989 val = sb_info->max;
3990 break;
3991 case SB_ENDSCROLL:
3992 if (prev_code == SB_THUMBTRACK)
3993 {
3994 /*
3995 * "pos" only gives us 16-bit data. In case of large file,
3996 * use GetScrollPos() which returns 32-bit. Unfortunately it
3997 * is not valid while the scrollbar is being dragged.
3998 */
3999 val = GetScrollPos(hwndCtl, SB_CTL);
4000 if (sb->scroll_shift > 0)
4001 val <<= sb->scroll_shift;
4002 }
4003 break;
4004
4005 default:
4006 /* TRACE("Unknown scrollbar event %d\n", code); */
4007 return 0;
4008 }
4009 prev_code = code;
4010
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4012 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013
4014 /*
4015 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4016 */
4017 if (sb->wp != NULL)
4018 {
4019 scrollbar_T *sba = sb->wp->w_scrollbars;
4020 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4021
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004022 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023 }
4024
4025 /* Don't let us be interrupted here by another message. */
4026 s_busy_processing = TRUE;
4027
4028 /* When "allow_scrollbar" is FALSE still need to remember the new
4029 * position, but don't actually scroll by setting "dont_scroll". */
4030 dont_scroll = !allow_scrollbar;
4031
Bram Moolenaara338adc2018-01-31 20:51:47 +01004032 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004033 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004034 mch_enable_flush();
4035 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004036
4037 s_busy_processing = FALSE;
4038 dont_scroll = dont_scroll_save;
4039
4040 return 0;
4041}
4042
4043
4044/*
4045 * Get command line arguments.
4046 * Use "prog" as the name of the program and "cmdline" as the arguments.
4047 * Copy the arguments to allocated memory.
4048 * Return the number of arguments (including program name).
4049 * Return pointers to the arguments in "argvp". Memory is allocated with
4050 * malloc(), use free() instead of vim_free().
4051 * Return pointer to buffer in "tofree".
4052 * Returns zero when out of memory.
4053 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004054 int
4055get_cmd_args(char *prog, char *cmdline, char ***argvp, char **tofree)
4056{
4057 int i;
4058 char *p;
4059 char *progp;
4060 char *pnew = NULL;
4061 char *newcmdline;
4062 int inquote;
4063 int argc;
4064 char **argv = NULL;
4065 int round;
4066
4067 *tofree = NULL;
4068
4069#ifdef FEAT_MBYTE
4070 /* Try using the Unicode version first, it takes care of conversion when
4071 * 'encoding' is changed. */
4072 argc = get_cmd_argsW(&argv);
4073 if (argc != 0)
4074 goto done;
4075#endif
4076
4077 /* Handle the program name. Remove the ".exe" extension, and find the 1st
4078 * non-space. */
4079 p = strrchr(prog, '.');
4080 if (p != NULL)
4081 *p = NUL;
4082 for (progp = prog; *progp == ' '; ++progp)
4083 ;
4084
4085 /* The command line is copied to allocated memory, so that we can change
4086 * it. Add the size of the string, the separating NUL and a terminating
4087 * NUL. */
4088 newcmdline = malloc(STRLEN(cmdline) + STRLEN(progp) + 2);
4089 if (newcmdline == NULL)
4090 return 0;
4091
4092 /*
4093 * First round: count the number of arguments ("pnew" == NULL).
4094 * Second round: produce the arguments.
4095 */
4096 for (round = 1; round <= 2; ++round)
4097 {
4098 /* First argument is the program name. */
4099 if (pnew != NULL)
4100 {
4101 argv[0] = pnew;
4102 strcpy(pnew, progp);
4103 pnew += strlen(pnew);
4104 *pnew++ = NUL;
4105 }
4106
4107 /*
4108 * Isolate each argument and put it in argv[].
4109 */
4110 p = cmdline;
4111 argc = 1;
4112 while (*p != NUL)
4113 {
4114 inquote = FALSE;
4115 if (pnew != NULL)
4116 argv[argc] = pnew;
4117 ++argc;
4118 while (*p != NUL && (inquote || (*p != ' ' && *p != '\t')))
4119 {
4120 /* Backslashes are only special when followed by a double
4121 * quote. */
4122 i = (int)strspn(p, "\\");
4123 if (p[i] == '"')
4124 {
4125 /* Halve the number of backslashes. */
4126 if (i > 1 && pnew != NULL)
4127 {
4128 vim_memset(pnew, '\\', i / 2);
4129 pnew += i / 2;
4130 }
4131
4132 /* Even nr of backslashes toggles quoting, uneven copies
4133 * the double quote. */
4134 if ((i & 1) == 0)
4135 inquote = !inquote;
4136 else if (pnew != NULL)
4137 *pnew++ = '"';
4138 p += i + 1;
4139 }
4140 else if (i > 0)
4141 {
4142 /* Copy span of backslashes unmodified. */
4143 if (pnew != NULL)
4144 {
4145 vim_memset(pnew, '\\', i);
4146 pnew += i;
4147 }
4148 p += i;
4149 }
4150 else
4151 {
4152 if (pnew != NULL)
4153 *pnew++ = *p;
4154#ifdef FEAT_MBYTE
4155 /* Can't use mb_* functions, because 'encoding' is not
4156 * initialized yet here. */
4157 if (IsDBCSLeadByte(*p))
4158 {
4159 ++p;
4160 if (pnew != NULL)
4161 *pnew++ = *p;
4162 }
4163#endif
4164 ++p;
4165 }
4166 }
4167
4168 if (pnew != NULL)
4169 *pnew++ = NUL;
4170 while (*p == ' ' || *p == '\t')
4171 ++p; /* advance until a non-space */
4172 }
4173
4174 if (round == 1)
4175 {
4176 argv = (char **)malloc((argc + 1) * sizeof(char *));
4177 if (argv == NULL )
4178 {
4179 free(newcmdline);
4180 return 0; /* malloc error */
4181 }
4182 pnew = newcmdline;
4183 *tofree = newcmdline;
4184 }
4185 }
4186
4187#ifdef FEAT_MBYTE
4188done:
4189#endif
4190 argv[argc] = NULL; /* NULL-terminated list */
4191 *argvp = argv;
4192 return argc;
4193}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195#ifdef FEAT_XPM_W32
4196# include "xpm_w32.h"
4197#endif
4198
4199#ifdef PROTO
4200# define WINAPI
4201#endif
4202
4203#ifdef __MINGW32__
4204/*
4205 * Add a lot of missing defines.
4206 * They are not always missing, we need the #ifndef's.
4207 */
4208# ifndef _cdecl
4209# define _cdecl
4210# endif
4211# ifndef IsMinimized
4212# define IsMinimized(hwnd) IsIconic(hwnd)
4213# endif
4214# ifndef IsMaximized
4215# define IsMaximized(hwnd) IsZoomed(hwnd)
4216# endif
4217# ifndef SelectFont
4218# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
4219# endif
4220# ifndef GetStockBrush
4221# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
4222# endif
4223# ifndef DeleteBrush
4224# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
4225# endif
4226
4227# ifndef HANDLE_WM_RBUTTONDBLCLK
4228# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4229 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4230# endif
4231# ifndef HANDLE_WM_MBUTTONUP
4232# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
4233 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4234# endif
4235# ifndef HANDLE_WM_MBUTTONDBLCLK
4236# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4237 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4238# endif
4239# ifndef HANDLE_WM_LBUTTONDBLCLK
4240# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
4241 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4242# endif
4243# ifndef HANDLE_WM_RBUTTONDOWN
4244# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
4245 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4246# endif
4247# ifndef HANDLE_WM_MOUSEMOVE
4248# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
4249 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4250# endif
4251# ifndef HANDLE_WM_RBUTTONUP
4252# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
4253 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4254# endif
4255# ifndef HANDLE_WM_MBUTTONDOWN
4256# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
4257 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4258# endif
4259# ifndef HANDLE_WM_LBUTTONUP
4260# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
4261 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4262# endif
4263# ifndef HANDLE_WM_LBUTTONDOWN
4264# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
4265 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
4266# endif
4267# ifndef HANDLE_WM_SYSCHAR
4268# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
4269 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4270# endif
4271# ifndef HANDLE_WM_ACTIVATEAPP
4272# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
4273 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
4274# endif
4275# ifndef HANDLE_WM_WINDOWPOSCHANGING
4276# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
4277 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
4278# endif
4279# ifndef HANDLE_WM_VSCROLL
4280# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
4281 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4282# endif
4283# ifndef HANDLE_WM_SETFOCUS
4284# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
4285 ((fn)((hwnd), (HWND)(wParam)), 0L)
4286# endif
4287# ifndef HANDLE_WM_KILLFOCUS
4288# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
4289 ((fn)((hwnd), (HWND)(wParam)), 0L)
4290# endif
4291# ifndef HANDLE_WM_HSCROLL
4292# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
4293 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
4294# endif
4295# ifndef HANDLE_WM_DROPFILES
4296# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
4297 ((fn)((hwnd), (HDROP)(wParam)), 0L)
4298# endif
4299# ifndef HANDLE_WM_CHAR
4300# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
4301 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4302# endif
4303# ifndef HANDLE_WM_SYSDEADCHAR
4304# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
4305 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4306# endif
4307# ifndef HANDLE_WM_DEADCHAR
4308# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
4309 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
4310# endif
4311#endif /* __MINGW32__ */
4312
4313
4314/* Some parameters for tearoff menus. All in pixels. */
4315#define TEAROFF_PADDING_X 2
4316#define TEAROFF_BUTTON_PAD_X 8
4317#define TEAROFF_MIN_WIDTH 200
4318#define TEAROFF_SUBMENU_LABEL ">>"
4319#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4320
4321
4322/* For the Intellimouse: */
4323#ifndef WM_MOUSEWHEEL
4324#define WM_MOUSEWHEEL 0x20a
4325#endif
4326
4327
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004328#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329# define ID_BEVAL_TOOLTIP 200
4330# define BEVAL_TEXT_LEN MAXPATHL
4331
Bram Moolenaar167632f2010-05-26 21:42:54 +02004332#if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar446cb832008-06-24 21:56:24 +00004333/* Work around old versions of basetsd.h which wrongly declares
4334 * UINT_PTR as unsigned long. */
Bram Moolenaar167632f2010-05-26 21:42:54 +02004335# undef UINT_PTR
Bram Moolenaar8424a622006-04-19 21:23:36 +00004336# define UINT_PTR UINT
4337#endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004338
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004340static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004342
Bram Moolenaar82881492012-11-20 16:53:39 +01004343
4344/* cproto fails on missing include files */
4345#ifndef PROTO
4346
Bram Moolenaar45360022005-07-21 21:08:21 +00004347/*
4348 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004349 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004350 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004351# include <pshpack1.h>
4352
4353#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004354
4355typedef struct _DllVersionInfo
4356{
4357 DWORD cbSize;
4358 DWORD dwMajorVersion;
4359 DWORD dwMinorVersion;
4360 DWORD dwBuildNumber;
4361 DWORD dwPlatformID;
4362} DLLVERSIONINFO;
4363
Bram Moolenaar82881492012-11-20 16:53:39 +01004364#ifndef PROTO
4365# include <poppack.h>
4366#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004367
Bram Moolenaar45360022005-07-21 21:08:21 +00004368typedef struct tagTOOLINFOA_NEW
4369{
4370 UINT cbSize;
4371 UINT uFlags;
4372 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004373 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004374 RECT rect;
4375 HINSTANCE hinst;
4376 LPSTR lpszText;
4377 LPARAM lParam;
4378} TOOLINFO_NEW;
4379
4380typedef struct tagNMTTDISPINFO_NEW
4381{
4382 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004383 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004384 char szText[80];
4385 HINSTANCE hinst;
4386 UINT uFlags;
4387 LPARAM lParam;
4388} NMTTDISPINFO_NEW;
4389
Bram Moolenaar45360022005-07-21 21:08:21 +00004390typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4391#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004392# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393#endif
4394
Bram Moolenaar45360022005-07-21 21:08:21 +00004395#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004396# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004397#endif
4398
4399#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004400# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004401#endif
4402
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004403#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004404
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004405#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4406/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4407 * it here if LPNMTTDISPINFO isn't defined.
4408 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4409 * _MSC_VER. */
4410# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4411typedef struct tagNMTTDISPINFOA {
4412 NMHDR hdr;
4413 LPSTR lpszText;
4414 char szText[80];
4415 HINSTANCE hinst;
4416 UINT uFlags;
4417 LPARAM lParam;
4418} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4419# define LPNMTTDISPINFO LPNMTTDISPINFOA
4420
4421# ifdef FEAT_MBYTE
4422typedef struct tagNMTTDISPINFOW {
4423 NMHDR hdr;
4424 LPWSTR lpszText;
4425 WCHAR szText[80];
4426 HINSTANCE hinst;
4427 UINT uFlags;
4428 LPARAM lParam;
4429} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4430# endif
4431# endif
4432#endif
4433
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004434#ifndef TTN_GETDISPINFOW
4435# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4436#endif
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438/* Local variables: */
4439
4440#ifdef FEAT_MENU
4441static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444/*
4445 * Use the system font for dialogs and tear-off menus. Remove this line to
4446 * use DLG_FONT_NAME.
4447 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004448#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450#define VIM_NAME "vim"
4451#define VIM_CLASS "Vim"
4452#define VIM_CLASSW L"Vim"
4453
4454/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4455 * thus there should be room for every dialog. For tearoffs it's made bigger
4456 * when needed. */
4457#define DLG_ALLOC_SIZE 16 * 1024
4458
4459/*
4460 * stuff for dialogs, menus, tearoffs etc.
4461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462static PWORD
4463add_dialog_element(
4464 PWORD p,
4465 DWORD lStyle,
4466 WORD x,
4467 WORD y,
4468 WORD w,
4469 WORD h,
4470 WORD Id,
4471 WORD clss,
4472 const char *caption);
4473static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004474static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004475#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478static void get_dialog_font_metrics(void);
4479
4480static int dialog_default_button = -1;
4481
4482/* Intellimouse support */
4483static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4486#ifdef FEAT_TOOLBAR
4487static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004488static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489static int get_toolbar_bitmap(vimmenu_T *menu);
4490#endif
4491
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004492#ifdef FEAT_GUI_TABLINE
4493static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004494static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004495#endif
4496
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497#ifdef FEAT_MBYTE_IME
4498static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4499static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4500#endif
4501#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4502# ifdef NOIME
4503typedef struct tagCOMPOSITIONFORM {
4504 DWORD dwStyle;
4505 POINT ptCurrentPos;
4506 RECT rcArea;
4507} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4508typedef HANDLE HIMC;
4509# endif
4510
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004511static HINSTANCE hLibImm = NULL;
4512static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4513static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4514static HIMC (WINAPI *pImmGetContext)(HWND);
4515static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4516static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4517static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4518static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4519static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4520static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4521static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4522static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004523static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524static void dyn_imm_load(void);
4525#else
4526# define pImmGetCompositionStringA ImmGetCompositionStringA
4527# define pImmGetCompositionStringW ImmGetCompositionStringW
4528# define pImmGetContext ImmGetContext
4529# define pImmAssociateContext ImmAssociateContext
4530# define pImmReleaseContext ImmReleaseContext
4531# define pImmGetOpenStatus ImmGetOpenStatus
4532# define pImmSetOpenStatus ImmSetOpenStatus
4533# define pImmGetCompositionFont ImmGetCompositionFontA
4534# define pImmSetCompositionFont ImmSetCompositionFontA
4535# define pImmSetCompositionWindow ImmSetCompositionWindow
4536# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004537# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540#ifdef FEAT_MENU
4541/*
4542 * Figure out how high the menu bar is at the moment.
4543 */
4544 static int
4545gui_mswin_get_menu_height(
4546 int fix_window) /* If TRUE, resize window if menu height changed */
4547{
4548 static int old_menu_height = -1;
4549
4550 RECT rc1, rc2;
4551 int num;
4552 int menu_height;
4553
4554 if (gui.menu_is_active)
4555 num = GetMenuItemCount(s_menuBar);
4556 else
4557 num = 0;
4558
4559 if (num == 0)
4560 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004561 else if (IsMinimized(s_hwnd))
4562 {
4563 /* The height of the menu cannot be determined while the window is
4564 * minimized. Take the previous height if the menu is changed in that
4565 * state, to avoid that Vim's vertical window size accidentally
4566 * increases due to the unaccounted-for menu height. */
4567 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
4570 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004571 /*
4572 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4573 * seem to have been set yet, so menu wraps in default window
4574 * width which is very narrow. Instead just return height of a
4575 * single menu item. Will still be wrong when the menu really
4576 * should wrap over more than one line.
4577 */
4578 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4579 if (gui.starting)
4580 menu_height = rc1.bottom - rc1.top + 1;
4581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004583 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4584 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 }
4587
4588 if (fix_window && menu_height != old_menu_height)
4589 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004590 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004592 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
4594 return menu_height;
4595}
4596#endif /*FEAT_MENU*/
4597
4598
4599/*
4600 * Setup for the Intellimouse
4601 */
4602 static void
4603init_mouse_wheel(void)
4604{
4605
4606#ifndef SPI_GETWHEELSCROLLLINES
4607# define SPI_GETWHEELSCROLLLINES 104
4608#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004609#ifndef SPI_SETWHEELSCROLLLINES
4610# define SPI_SETWHEELSCROLLLINES 105
4611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
4613#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4614#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4615#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4616#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 mouse_scroll_lines = 3; /* reasonable default */
4619
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004620 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4621 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4622 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623}
4624
4625
4626/* Intellimouse wheel handler */
4627 static void
4628_OnMouseWheel(
4629 HWND hwnd,
4630 short zDelta)
4631{
4632/* Treat a mouse wheel event as if it were a scroll request */
4633 int i;
4634 int size;
4635 HWND hwndCtl;
4636
4637 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4638 {
4639 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4640 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4641 }
4642 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4643 {
4644 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4645 size = curwin->w_scrollbars[SBAR_LEFT].size;
4646 }
4647 else
4648 return;
4649
4650 size = curwin->w_height;
4651 if (mouse_scroll_lines == 0)
4652 init_mouse_wheel();
4653
Bram Moolenaara338adc2018-01-31 20:51:47 +01004654 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (mouse_scroll_lines > 0
4656 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4657 {
4658 for (i = mouse_scroll_lines; i > 0; --i)
4659 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4660 }
4661 else
4662 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004663 mch_enable_flush();
4664 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665}
4666
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004667#ifdef USE_SYSMENU_FONT
4668/*
4669 * Get Menu Font.
4670 * Return OK or FAIL.
4671 */
4672 static int
4673gui_w32_get_menu_font(LOGFONT *lf)
4674{
4675 NONCLIENTMETRICS nm;
4676
4677 nm.cbSize = sizeof(NONCLIENTMETRICS);
4678 if (!SystemParametersInfo(
4679 SPI_GETNONCLIENTMETRICS,
4680 sizeof(NONCLIENTMETRICS),
4681 &nm,
4682 0))
4683 return FAIL;
4684 *lf = nm.lfMenuFont;
4685 return OK;
4686}
4687#endif
4688
4689
4690#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4691/*
4692 * Set the GUI tabline font to the system menu font
4693 */
4694 static void
4695set_tabline_font(void)
4696{
4697 LOGFONT lfSysmenu;
4698 HFONT font;
4699 HWND hwnd;
4700 HDC hdc;
4701 HFONT hfntOld;
4702 TEXTMETRIC tm;
4703
4704 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4705 return;
4706
4707 font = CreateFontIndirect(&lfSysmenu);
4708
4709 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4710
4711 /*
4712 * Compute the height of the font used for the tab text
4713 */
4714 hwnd = GetDesktopWindow();
4715 hdc = GetWindowDC(hwnd);
4716 hfntOld = SelectFont(hdc, font);
4717
4718 GetTextMetrics(hdc, &tm);
4719
4720 SelectFont(hdc, hfntOld);
4721 ReleaseDC(hwnd, hdc);
4722
4723 /*
4724 * The space used by the tab border and the space between the tab label
4725 * and the tab border is included as 7.
4726 */
4727 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4728}
4729#endif
4730
Bram Moolenaar520470a2005-06-16 21:59:56 +00004731/*
4732 * Invoked when a setting was changed.
4733 */
4734 static LRESULT CALLBACK
4735_OnSettingChange(UINT n)
4736{
4737 if (n == SPI_SETWHEELSCROLLLINES)
4738 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4739 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004740#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4741 if (n == SPI_SETNONCLIENTMETRICS)
4742 set_tabline_font();
4743#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004744 return 0;
4745}
4746
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#ifdef FEAT_NETBEANS_INTG
4748 static void
4749_OnWindowPosChanged(
4750 HWND hwnd,
4751 const LPWINDOWPOS lpwpos)
4752{
4753 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004754 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4757 || lpwpos->cx != cx || lpwpos->cy != cy))
4758 {
4759 x = lpwpos->x;
4760 y = lpwpos->y;
4761 cx = lpwpos->cx;
4762 cy = lpwpos->cy;
4763 netbeans_frame_moved(x, y);
4764 }
4765 /* Allow to send WM_SIZE and WM_MOVE */
4766 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4767}
4768#endif
4769
4770 static int
4771_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 UINT fwSide,
4773 LPRECT lprc)
4774{
4775 int w, h;
4776 int valid_w, valid_h;
4777 int w_offset, h_offset;
4778
4779 w = lprc->right - lprc->left;
4780 h = lprc->bottom - lprc->top;
4781 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4782 w_offset = w - valid_w;
4783 h_offset = h - valid_h;
4784
4785 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4786 || fwSide == WMSZ_BOTTOMLEFT)
4787 lprc->left += w_offset;
4788 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4789 || fwSide == WMSZ_BOTTOMRIGHT)
4790 lprc->right -= w_offset;
4791
4792 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4793 || fwSide == WMSZ_TOPRIGHT)
4794 lprc->top += h_offset;
4795 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4796 || fwSide == WMSZ_BOTTOMRIGHT)
4797 lprc->bottom -= h_offset;
4798 return TRUE;
4799}
4800
4801
4802
4803 static LRESULT CALLBACK
4804_WndProc(
4805 HWND hwnd,
4806 UINT uMsg,
4807 WPARAM wParam,
4808 LPARAM lParam)
4809{
4810 /*
4811 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4812 hwnd, uMsg, wParam, lParam);
4813 */
4814
4815 HandleMouseHide(uMsg, lParam);
4816
4817 s_uMsg = uMsg;
4818 s_wParam = wParam;
4819 s_lParam = lParam;
4820
4821 switch (uMsg)
4822 {
4823 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4824 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4825 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4826 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4827 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4828 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4829 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4830 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4831 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4832#ifdef FEAT_MENU
4833 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4834#endif
4835 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4836 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4837 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4838 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4839 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4840 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4841 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4842 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4843 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4844#ifdef FEAT_NETBEANS_INTG
4845 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4846#endif
4847
Bram Moolenaarafa24992006-03-27 20:58:26 +00004848#ifdef FEAT_GUI_TABLINE
4849 case WM_RBUTTONUP:
4850 {
4851 if (gui_mch_showing_tabline())
4852 {
4853 POINT pt;
4854 RECT rect;
4855
4856 /*
4857 * If the cursor is on the tabline, display the tab menu
4858 */
4859 GetCursorPos((LPPOINT)&pt);
4860 GetWindowRect(s_textArea, &rect);
4861 if (pt.y < rect.top)
4862 {
4863 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004864 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004865 }
4866 }
4867 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4868 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004869 case WM_LBUTTONDBLCLK:
4870 {
4871 /*
4872 * If the user double clicked the tabline, create a new tab
4873 */
4874 if (gui_mch_showing_tabline())
4875 {
4876 POINT pt;
4877 RECT rect;
4878
4879 GetCursorPos((LPPOINT)&pt);
4880 GetWindowRect(s_textArea, &rect);
4881 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004882 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004883 }
4884 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4885 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004886#endif
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 case WM_QUERYENDSESSION: /* System wants to go down. */
4889 gui_shell_closed(); /* Will exit when no changed buffers. */
4890 return FALSE; /* Do NOT allow system to go down. */
4891
4892 case WM_ENDSESSION:
4893 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004896 return 0L;
4897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 break;
4899
4900 case WM_CHAR:
4901 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4902 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004903 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 return 0L;
4905
4906 case WM_SYSCHAR:
4907 /*
4908 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4909 * shortcut key, handle like a typed ALT key, otherwise call Windows
4910 * ALT key handling.
4911 */
4912#ifdef FEAT_MENU
4913 if ( !gui.menu_is_active
4914 || p_wak[0] == 'n'
4915 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4916 )
4917#endif
4918 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004919 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 return 0L;
4921 }
4922#ifdef FEAT_MENU
4923 else
4924 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4925#endif
4926
4927 case WM_SYSKEYUP:
4928#ifdef FEAT_MENU
4929 /* This used to be done only when menu is active: ALT key is used for
4930 * that. But that caused problems when menu is disabled and using
4931 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4932 * are received, mouse pointer remains hidden. */
4933 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4934#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004935 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
4937
4938 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004939 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 case WM_MOUSEWHEEL:
4942 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004943 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
Bram Moolenaar520470a2005-06-16 21:59:56 +00004945 /* Notification for change in SystemParametersInfo() */
4946 case WM_SETTINGCHANGE:
4947 return _OnSettingChange((UINT)wParam);
4948
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004949#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 case WM_NOTIFY:
4951 switch (((LPNMHDR) lParam)->code)
4952 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004953# ifdef FEAT_MBYTE
4954 case TTN_GETDISPINFOW:
4955# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004956 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004958 LPNMHDR hdr = (LPNMHDR)lParam;
4959 char_u *str = NULL;
4960 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004961
Bram Moolenaard23a8232018-02-10 18:45:26 +01004962 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004963
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004964# ifdef FEAT_GUI_TABLINE
4965 if (gui_mch_showing_tabline()
4966 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004967 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004968 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004969 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004970 * Mouse is over the GUI tabline. Display the
4971 * tooltip for the tab under the cursor
4972 *
4973 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004974 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004975 GetCursorPos(&pt);
4976 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004977 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004978 TCHITTESTINFO htinfo;
4979 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004980
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004981 /*
4982 * Get the tab under the cursor
4983 */
4984 htinfo.pt.x = pt.x;
4985 htinfo.pt.y = pt.y;
4986 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4987 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004988 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004989 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004990
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004991 tp = find_tabpage(idx + 1);
4992 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004993 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004994 get_tabline_label(tp, TRUE);
4995 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004996 }
4997 }
4998 }
4999 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005000# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005001# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005002# ifdef FEAT_GUI_TABLINE
5003 else
5004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005006 UINT idButton;
5007 vimmenu_T *pMenu;
5008
5009 idButton = (UINT) hdr->idFrom;
5010 pMenu = gui_mswin_find_menu(root_menu, idButton);
5011 if (pMenu)
5012 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005014# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005015 if (str != NULL)
5016 {
5017# ifdef FEAT_MBYTE
5018 if (hdr->code == TTN_GETDISPINFOW)
5019 {
5020 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5021
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005022 /* Set the maximum width, this also enables using
5023 * \n for line break. */
5024 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5025 0, 500);
5026
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005027 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005028 lpdi->lpszText = tt_text;
5029 /* can't show tooltip if failed */
5030 }
5031 else
5032# endif
5033 {
5034 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5035
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005036 /* Set the maximum width, this also enables using
5037 * \n for line break. */
5038 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5039 0, 500);
5040
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005041 if (STRLEN(str) < sizeof(lpdi->szText)
5042 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005043 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005044 sizeof(lpdi->szText) - 1);
5045 else
5046 lpdi->lpszText = tt_text;
5047 }
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 }
5050 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005051# ifdef FEAT_GUI_TABLINE
5052 case TCN_SELCHANGE:
5053 if (gui_mch_showing_tabline()
5054 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005055 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005056 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005057 return 0L;
5058 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005059 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005060
5061 case NM_RCLICK:
5062 if (gui_mch_showing_tabline()
5063 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005064 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005065 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005066 return 0L;
5067 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005068 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005071# ifdef FEAT_GUI_TABLINE
5072 if (gui_mch_showing_tabline()
5073 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5074 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 break;
5077 }
5078 break;
5079#endif
5080#if defined(MENUHINTS) && defined(FEAT_MENU)
5081 case WM_MENUSELECT:
5082 if (((UINT) HIWORD(wParam)
5083 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5084 == MF_HILITE
5085 && (State & CMDLINE) == 0)
5086 {
5087 UINT idButton;
5088 vimmenu_T *pMenu;
5089 static int did_menu_tip = FALSE;
5090
5091 if (did_menu_tip)
5092 {
5093 msg_clr_cmdline();
5094 setcursor();
5095 out_flush();
5096 did_menu_tip = FALSE;
5097 }
5098
5099 idButton = (UINT)LOWORD(wParam);
5100 pMenu = gui_mswin_find_menu(root_menu, idButton);
5101 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5102 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5103 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005104 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005106 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 setcursor();
5108 out_flush();
5109 did_menu_tip = TRUE;
5110 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005111 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113 break;
5114#endif
5115 case WM_NCHITTEST:
5116 {
5117 LRESULT result;
5118 int x, y;
5119 int xPos = GET_X_LPARAM(lParam);
5120
5121 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5122 if (result == HTCLIENT)
5123 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005124#ifdef FEAT_GUI_TABLINE
5125 if (gui_mch_showing_tabline())
5126 {
5127 int yPos = GET_Y_LPARAM(lParam);
5128 RECT rct;
5129
5130 /* If the cursor is on the GUI tabline, don't process this
5131 * event */
5132 GetWindowRect(s_textArea, &rct);
5133 if (yPos < rct.top)
5134 return result;
5135 }
5136#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005137 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 xPos -= x;
5139
5140 if (xPos < 48) /* <VN> TODO should use system metric? */
5141 return HTBOTTOMLEFT;
5142 else
5143 return HTBOTTOMRIGHT;
5144 }
5145 else
5146 return result;
5147 }
5148 /* break; notreached */
5149
5150#ifdef FEAT_MBYTE_IME
5151 case WM_IME_NOTIFY:
5152 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5153 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005154 return 1L;
5155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 case WM_IME_COMPOSITION:
5157 if (!_OnImeComposition(hwnd, wParam, lParam))
5158 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005159 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#endif
5161
5162 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005164 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 _OnFindRepl();
5167 }
5168#endif
5169 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5170 }
5171
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005172 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173}
5174
5175/*
5176 * End of call-back routines
5177 */
5178
5179/* parent window, if specified with -P */
5180HWND vim_parent_hwnd = NULL;
5181
5182 static BOOL CALLBACK
5183FindWindowTitle(HWND hwnd, LPARAM lParam)
5184{
5185 char buf[2048];
5186 char *title = (char *)lParam;
5187
5188 if (GetWindowText(hwnd, buf, sizeof(buf)))
5189 {
5190 if (strstr(buf, title) != NULL)
5191 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005192 /* Found it. Store the window ref. and quit searching if MDI
5193 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005195 if (vim_parent_hwnd != NULL)
5196 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 }
5199 return TRUE; /* continue searching */
5200}
5201
5202/*
5203 * Invoked for '-P "title"' argument: search for parent application to open
5204 * our window in.
5205 */
5206 void
5207gui_mch_set_parent(char *title)
5208{
5209 EnumWindows(FindWindowTitle, (LPARAM)title);
5210 if (vim_parent_hwnd == NULL)
5211 {
5212 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5213 mch_exit(2);
5214 }
5215}
5216
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005217#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 static void
5219ole_error(char *arg)
5220{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005221 char buf[IOSIZE];
5222
5223 /* Can't use EMSG() here, we have not finished initialisation yet. */
5224 vim_snprintf(buf, IOSIZE,
5225 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5226 arg);
5227 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
5231/*
5232 * Parse the GUI related command-line arguments. Any arguments used are
5233 * deleted from argv, and *argc is decremented accordingly. This is called
5234 * when vim is started, whether or not the GUI has been started.
5235 */
5236 void
5237gui_mch_prepare(int *argc, char **argv)
5238{
5239 int silent = FALSE;
5240 int idx;
5241
5242 /* Check for special OLE command line parameters */
5243 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5244 {
5245 /* Check for a "-silent" argument first. */
5246 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5247 && (argv[2][0] == '-' || argv[2][0] == '/'))
5248 {
5249 silent = TRUE;
5250 idx = 2;
5251 }
5252 else
5253 idx = 1;
5254
5255 /* Register Vim as an OLE Automation server */
5256 if (STRICMP(argv[idx] + 1, "register") == 0)
5257 {
5258#ifdef FEAT_OLE
5259 RegisterMe(silent);
5260 mch_exit(0);
5261#else
5262 if (!silent)
5263 ole_error("register");
5264 mch_exit(2);
5265#endif
5266 }
5267
5268 /* Unregister Vim as an OLE Automation server */
5269 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5270 {
5271#ifdef FEAT_OLE
5272 UnregisterMe(!silent);
5273 mch_exit(0);
5274#else
5275 if (!silent)
5276 ole_error("unregister");
5277 mch_exit(2);
5278#endif
5279 }
5280
5281 /* Ignore an -embedding argument. It is only relevant if the
5282 * application wants to treat the case when it is started manually
5283 * differently from the case where it is started via automation (and
5284 * we don't).
5285 */
5286 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5287 {
5288#ifdef FEAT_OLE
5289 *argc = 1;
5290#else
5291 ole_error("embedding");
5292 mch_exit(2);
5293#endif
5294 }
5295 }
5296
5297#ifdef FEAT_OLE
5298 {
5299 int bDoRestart = FALSE;
5300
5301 InitOLE(&bDoRestart);
5302 /* automatically exit after registering */
5303 if (bDoRestart)
5304 mch_exit(0);
5305 }
5306#endif
5307
5308#ifdef FEAT_NETBEANS_INTG
5309 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005310 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 int arg;
5312
5313 for (arg = 1; arg < *argc; arg++)
5314 if (strncmp("-nb", argv[arg], 3) == 0)
5315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 netbeansArg = argv[arg];
5317 mch_memmove(&argv[arg], &argv[arg + 1],
5318 (--*argc - arg) * sizeof(char *));
5319 argv[*argc] = NULL;
5320 break; /* enough? */
5321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324}
5325
5326/*
5327 * Initialise the GUI. Create all the windows, set up all the call-backs
5328 * etc.
5329 */
5330 int
5331gui_mch_init(void)
5332{
5333 const char szVimWndClass[] = VIM_CLASS;
5334 const char szTextAreaClass[] = "VimTextArea";
5335 WNDCLASS wndclass;
5336#ifdef FEAT_MBYTE
5337 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005338 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 WNDCLASSW wndclassw;
5340#endif
5341#ifdef GLOBAL_IME
5342 ATOM atom;
5343#endif
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 /* Return here if the window was already opened (happens when
5346 * gui_mch_dialog() is called early). */
5347 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005348 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349
5350 /*
5351 * Load the tearoff bitmap
5352 */
5353#ifdef FEAT_TEAROFF
5354 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5355#endif
5356
5357 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5358 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5359#ifdef FEAT_MENU
5360 gui.menu_height = 0; /* Windows takes care of this */
5361#endif
5362 gui.border_width = 0;
5363
5364 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5365
5366#ifdef FEAT_MBYTE
5367 /* First try using the wide version, so that we can use any title.
5368 * Otherwise only characters in the active codepage will work. */
5369 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5370 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005371 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 wndclassw.lpfnWndProc = _WndProc;
5373 wndclassw.cbClsExtra = 0;
5374 wndclassw.cbWndExtra = 0;
5375 wndclassw.hInstance = s_hinst;
5376 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5377 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5378 wndclassw.hbrBackground = s_brush;
5379 wndclassw.lpszMenuName = NULL;
5380 wndclassw.lpszClassName = szVimWndClassW;
5381
5382 if ((
5383#ifdef GLOBAL_IME
5384 atom =
5385#endif
5386 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 else
5389 wide_WindowProc = TRUE;
5390 }
5391
5392 if (!wide_WindowProc)
5393#endif
5394
5395 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5396 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005397 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 wndclass.lpfnWndProc = _WndProc;
5399 wndclass.cbClsExtra = 0;
5400 wndclass.cbWndExtra = 0;
5401 wndclass.hInstance = s_hinst;
5402 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5403 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5404 wndclass.hbrBackground = s_brush;
5405 wndclass.lpszMenuName = NULL;
5406 wndclass.lpszClassName = szVimWndClass;
5407
5408 if ((
5409#ifdef GLOBAL_IME
5410 atom =
5411#endif
5412 RegisterClass(&wndclass)) == 0)
5413 return FAIL;
5414 }
5415
5416 if (vim_parent_hwnd != NULL)
5417 {
5418#ifdef HAVE_TRY_EXCEPT
5419 __try
5420 {
5421#endif
5422 /* Open inside the specified parent window.
5423 * TODO: last argument should point to a CLIENTCREATESTRUCT
5424 * structure. */
5425 s_hwnd = CreateWindowEx(
5426 WS_EX_MDICHILD,
5427 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005428 WS_OVERLAPPEDWINDOW | WS_CHILD
5429 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5431 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5432 100, /* Any value will do */
5433 100, /* Any value will do */
5434 vim_parent_hwnd, NULL,
5435 s_hinst, NULL);
5436#ifdef HAVE_TRY_EXCEPT
5437 }
5438 __except(EXCEPTION_EXECUTE_HANDLER)
5439 {
5440 /* NOP */
5441 }
5442#endif
5443 if (s_hwnd == NULL)
5444 {
5445 EMSG(_("E672: Unable to open window inside MDI application"));
5446 mch_exit(2);
5447 }
5448 }
5449 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005450 {
5451 /* If the provided windowid is not valid reset it to zero, so that it
5452 * is ignored and we open our own window. */
5453 if (IsWindow((HWND)win_socket_id) <= 0)
5454 win_socket_id = 0;
5455
5456 /* Create a window. If win_socket_id is not zero without border and
5457 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005459 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005460 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5461 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005462 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5463 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5464 100, /* Any value will do */
5465 100, /* Any value will do */
5466 NULL, NULL,
5467 s_hinst, NULL);
5468 if (s_hwnd != NULL && win_socket_id != 0)
5469 {
5470 SetParent(s_hwnd, (HWND)win_socket_id);
5471 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5472 }
5473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 if (s_hwnd == NULL)
5476 return FAIL;
5477
5478#ifdef GLOBAL_IME
5479 global_ime_init(atom, s_hwnd);
5480#endif
5481#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5482 dyn_imm_load();
5483#endif
5484
5485 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005486#ifdef FEAT_MBYTE
5487 if (wide_WindowProc)
5488 {
5489 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5490 {
5491 wndclassw.style = CS_OWNDC;
5492 wndclassw.lpfnWndProc = _TextAreaWndProc;
5493 wndclassw.cbClsExtra = 0;
5494 wndclassw.cbWndExtra = 0;
5495 wndclassw.hInstance = s_hinst;
5496 wndclassw.hIcon = NULL;
5497 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5498 wndclassw.hbrBackground = NULL;
5499 wndclassw.lpszMenuName = NULL;
5500 wndclassw.lpszClassName = szTextAreaClassW;
5501
5502 if (RegisterClassW(&wndclassw) == 0)
5503 return FAIL;
5504 }
5505 }
5506 else
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5509 {
5510 wndclass.style = CS_OWNDC;
5511 wndclass.lpfnWndProc = _TextAreaWndProc;
5512 wndclass.cbClsExtra = 0;
5513 wndclass.cbWndExtra = 0;
5514 wndclass.hInstance = s_hinst;
5515 wndclass.hIcon = NULL;
5516 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5517 wndclass.hbrBackground = NULL;
5518 wndclass.lpszMenuName = NULL;
5519 wndclass.lpszClassName = szTextAreaClass;
5520
5521 if (RegisterClass(&wndclass) == 0)
5522 return FAIL;
5523 }
5524 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005525 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 szTextAreaClass, "Vim text area",
5527 WS_CHILD | WS_VISIBLE, 0, 0,
5528 100, /* Any value will do for now */
5529 100, /* Any value will do for now */
5530 s_hwnd, NULL,
5531 s_hinst, NULL);
5532
5533 if (s_textArea == NULL)
5534 return FAIL;
5535
Bram Moolenaar20321902016-02-17 12:30:17 +01005536#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005537 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5538 {
5539 HANDLE hIcon = NULL;
5540
5541 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005542 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005543 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005544#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005545
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546#ifdef FEAT_MENU
5547 s_menuBar = CreateMenu();
5548#endif
5549 s_hdc = GetDC(s_textArea);
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 /* Do we need to bother with this? */
5554 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5555
5556 /* Get background/foreground colors from the system */
5557 gui_mch_def_colors();
5558
5559 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5560 * file) */
5561 set_normal_colors();
5562
5563 /*
5564 * Check that none of the colors are the same as the background color.
5565 * Then store the current values as the defaults.
5566 */
5567 gui_check_colors();
5568 gui.def_norm_pixel = gui.norm_pixel;
5569 gui.def_back_pixel = gui.back_pixel;
5570
5571 /* Get the colors for the highlight groups (gui_check_colors() might have
5572 * changed them) */
5573 highlight_gui_started();
5574
5575 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005576 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005578 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 /*
5581 * Set up for Intellimouse processing
5582 */
5583 init_mouse_wheel();
5584
5585 /*
5586 * compute a couple of metrics used for the dialogs
5587 */
5588 get_dialog_font_metrics();
5589#ifdef FEAT_TOOLBAR
5590 /*
5591 * Create the toolbar
5592 */
5593 initialise_toolbar();
5594#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005595#ifdef FEAT_GUI_TABLINE
5596 /*
5597 * Create the tabline
5598 */
5599 initialise_tabline();
5600#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601#ifdef MSWIN_FIND_REPLACE
5602 /*
5603 * Initialise the dialog box stuff
5604 */
5605 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5606
5607 /* Initialise the struct */
5608 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005609 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005611 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5613 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5614 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005615# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005616 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5617 s_findrep_struct_w.lpstrFindWhat =
5618 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5619 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5620 s_findrep_struct_w.lpstrReplaceWith =
5621 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5622 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5623 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5624 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005628#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005629# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5630/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5631# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005632# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005633# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005634# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005635 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005636 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005637#endif
5638
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005639#ifdef FEAT_RENDER_OPTIONS
5640 if (p_rop)
5641 (void)gui_mch_set_rendering_options(p_rop);
5642#endif
5643
Bram Moolenaar748bf032005-02-02 23:04:36 +00005644theend:
5645 /* Display any pending error messages */
5646 display_errors();
5647
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 return OK;
5649}
5650
5651/*
5652 * Get the size of the screen, taking position on multiple monitors into
5653 * account (if supported).
5654 */
5655 static void
5656get_work_area(RECT *spi_rect)
5657{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005658 HMONITOR mon;
5659 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005661 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005662 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005663 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005665 moninfo.cbSize = sizeof(MONITORINFO);
5666 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005668 *spi_rect = moninfo.rcWork;
5669 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671 }
5672 /* this is the old method... */
5673 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5674}
5675
5676/*
5677 * Set the size of the window to the given width and height in pixels.
5678 */
5679 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005680gui_mch_set_shellsize(
5681 int width,
5682 int height,
5683 int min_width UNUSED,
5684 int min_height UNUSED,
5685 int base_width UNUSED,
5686 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005687 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 RECT workarea_rect;
5690 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 WINDOWPLACEMENT wndpl;
5692
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005693 /* Try to keep window completely on screen. */
5694 /* Get position of the screen work area. This is the part that is not
5695 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 get_work_area(&workarea_rect);
5697
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005698 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005699 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 wndpl.length = sizeof(WINDOWPLACEMENT);
5701 GetWindowPlacement(s_hwnd, &wndpl);
5702
5703 /* Resizing a maximized window looks very strange, unzoom it first.
5704 * But don't do it when still starting up, it may have been requested in
5705 * the shortcut. */
5706 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5707 {
5708 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5709 /* Need to get the settings of the normal window. */
5710 GetWindowPlacement(s_hwnd, &wndpl);
5711 }
5712
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005714 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005715 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005716 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005717 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 + GetSystemMetrics(SM_CYCAPTION)
5719#ifdef FEAT_MENU
5720 + gui_mswin_get_menu_height(FALSE)
5721#endif
5722 ;
5723
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005724 /* The following should take care of keeping Vim on the same monitor, no
5725 * matter if the secondary monitor is left or right of the primary
5726 * monitor. */
5727 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5728 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005729
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005730 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005731 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005732 && wndpl.rcNormalPosition.right > workarea_rect.right)
5733 OffsetRect(&wndpl.rcNormalPosition,
5734 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005736 if ((direction & RESIZE_HOR)
5737 && wndpl.rcNormalPosition.left < workarea_rect.left)
5738 OffsetRect(&wndpl.rcNormalPosition,
5739 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740
Bram Moolenaarafa24992006-03-27 20:58:26 +00005741 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005742 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5743 OffsetRect(&wndpl.rcNormalPosition,
5744 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005746 if ((direction & RESIZE_VERT)
5747 && wndpl.rcNormalPosition.top < workarea_rect.top)
5748 OffsetRect(&wndpl.rcNormalPosition,
5749 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 /* set window position - we should use SetWindowPlacement rather than
5752 * SetWindowPos as the MSDN docs say the coord systems returned by
5753 * these two are not compatible. */
5754 SetWindowPlacement(s_hwnd, &wndpl);
5755
5756 SetActiveWindow(s_hwnd);
5757 SetFocus(s_hwnd);
5758
5759#ifdef FEAT_MENU
5760 /* Menu may wrap differently now */
5761 gui_mswin_get_menu_height(!gui.starting);
5762#endif
5763}
5764
5765
5766 void
5767gui_mch_set_scrollbar_thumb(
5768 scrollbar_T *sb,
5769 long val,
5770 long size,
5771 long max)
5772{
5773 SCROLLINFO info;
5774
5775 sb->scroll_shift = 0;
5776 while (max > 32767)
5777 {
5778 max = (max + 1) >> 1;
5779 val >>= 1;
5780 size >>= 1;
5781 ++sb->scroll_shift;
5782 }
5783
5784 if (sb->scroll_shift > 0)
5785 ++size;
5786
5787 info.cbSize = sizeof(info);
5788 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5789 info.nPos = val;
5790 info.nMin = 0;
5791 info.nMax = max;
5792 info.nPage = size;
5793 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5794}
5795
5796
5797/*
5798 * Set the current text font.
5799 */
5800 void
5801gui_mch_set_font(GuiFont font)
5802{
5803 gui.currFont = font;
5804}
5805
5806
5807/*
5808 * Set the current text foreground color.
5809 */
5810 void
5811gui_mch_set_fg_color(guicolor_T color)
5812{
5813 gui.currFgColor = color;
5814}
5815
5816/*
5817 * Set the current text background color.
5818 */
5819 void
5820gui_mch_set_bg_color(guicolor_T color)
5821{
5822 gui.currBgColor = color;
5823}
5824
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005825/*
5826 * Set the current text special color.
5827 */
5828 void
5829gui_mch_set_sp_color(guicolor_T color)
5830{
5831 gui.currSpColor = color;
5832}
5833
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005834#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835/*
5836 * Multi-byte handling, originally by Sung-Hoon Baek.
5837 * First static functions (no prototypes generated).
5838 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005839# ifdef _MSC_VER
5840# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5841# endif
5842# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 * handle WM_IME_NOTIFY message
5846 */
5847 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005848_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 LRESULT lResult = 0;
5851 HIMC hImc;
5852
5853 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5854 return lResult;
5855 switch (dwCommand)
5856 {
5857 case IMN_SETOPENSTATUS:
5858 if (pImmGetOpenStatus(hImc))
5859 {
5860 pImmSetCompositionFont(hImc, &norm_logfont);
5861 im_set_position(gui.row, gui.col);
5862
5863 /* Disable langmap */
5864 State &= ~LANGMAP;
5865 if (State & INSERT)
5866 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005867#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* Unshown 'keymap' in status lines */
5869 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5870 {
5871 /* Save cursor position */
5872 int old_row = gui.row;
5873 int old_col = gui.col;
5874
5875 // This must be called here before
5876 // status_redraw_curbuf(), otherwise the mode
5877 // message may appear in the wrong position.
5878 showmode();
5879 status_redraw_curbuf();
5880 update_screen(0);
5881 /* Restore cursor position */
5882 gui.row = old_row;
5883 gui.col = old_col;
5884 }
5885#endif
5886 }
5887 }
5888 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005889 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 lResult = 0;
5891 break;
5892 }
5893 pImmReleaseContext(hWnd, hImc);
5894 return lResult;
5895}
5896
5897 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005898_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899{
5900 char_u *ret;
5901 int len;
5902
5903 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5904 return 0;
5905
5906 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5907 if (ret != NULL)
5908 {
5909 add_to_input_buf_csi(ret, len);
5910 vim_free(ret);
5911 return 1;
5912 }
5913 return 0;
5914}
5915
5916/*
5917 * get the current composition string, in UCS-2; *lenp is the number of
5918 * *lenp is the number of Unicode characters.
5919 */
5920 static short_u *
5921GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5922{
5923 LONG ret;
5924 LPWSTR wbuf = NULL;
5925 char_u *buf;
5926
5927 if (!pImmGetContext)
5928 return NULL; /* no imm32.dll */
5929
5930 /* Try Unicode; this'll always work on NT regardless of codepage. */
5931 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5932 if (ret == 0)
5933 return NULL; /* empty */
5934
5935 if (ret > 0)
5936 {
5937 /* Allocate the requested buffer plus space for the NUL character. */
5938 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5939 if (wbuf != NULL)
5940 {
5941 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5942 *lenp = ret / sizeof(WCHAR);
5943 }
5944 return (short_u *)wbuf;
5945 }
5946
5947 /* ret < 0; we got an error, so try the ANSI version. This'll work
5948 * on 9x/ME, but only if the codepage happens to be set to whatever
5949 * we're inputting. */
5950 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5951 if (ret <= 0)
5952 return NULL; /* empty or error */
5953
5954 buf = alloc(ret);
5955 if (buf == NULL)
5956 return NULL;
5957 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5958
5959 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005960 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 vim_free(buf);
5962
5963 return (short_u *)wbuf;
5964}
5965
5966/*
5967 * void GetResultStr()
5968 *
5969 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5970 * get complete composition string
5971 */
5972 static char_u *
5973GetResultStr(HWND hwnd, int GCS, int *lenp)
5974{
5975 HIMC hIMC; /* Input context handle. */
5976 short_u *buf = NULL;
5977 char_u *convbuf = NULL;
5978
5979 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5980 return NULL;
5981
5982 /* Reads in the composition string. */
5983 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5984 if (buf == NULL)
5985 return NULL;
5986
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005987 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 pImmReleaseContext(hwnd, hIMC);
5989 vim_free(buf);
5990 return convbuf;
5991}
5992#endif
5993
5994/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005995#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
5997/*
5998 * set font to IM.
5999 */
6000 void
6001im_set_font(LOGFONT *lf)
6002{
6003 HIMC hImc;
6004
6005 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6006 {
6007 pImmSetCompositionFont(hImc, lf);
6008 pImmReleaseContext(s_hwnd, hImc);
6009 }
6010}
6011
6012/*
6013 * Notify cursor position to IM.
6014 */
6015 void
6016im_set_position(int row, int col)
6017{
6018 HIMC hImc;
6019
6020 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6021 {
6022 COMPOSITIONFORM cfs;
6023
6024 cfs.dwStyle = CFS_POINT;
6025 cfs.ptCurrentPos.x = FILL_X(col);
6026 cfs.ptCurrentPos.y = FILL_Y(row);
6027 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6028 pImmSetCompositionWindow(hImc, &cfs);
6029
6030 pImmReleaseContext(s_hwnd, hImc);
6031 }
6032}
6033
6034/*
6035 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6036 */
6037 void
6038im_set_active(int active)
6039{
6040 HIMC hImc;
6041 static HIMC hImcOld = (HIMC)0;
6042
6043 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6044 {
6045 if (p_imdisable)
6046 {
6047 if (hImcOld == (HIMC)0)
6048 {
6049 hImcOld = pImmGetContext(s_hwnd);
6050 if (hImcOld)
6051 pImmAssociateContext(s_hwnd, (HIMC)0);
6052 }
6053 active = FALSE;
6054 }
6055 else if (hImcOld != (HIMC)0)
6056 {
6057 pImmAssociateContext(s_hwnd, hImcOld);
6058 hImcOld = (HIMC)0;
6059 }
6060
6061 hImc = pImmGetContext(s_hwnd);
6062 if (hImc)
6063 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006064 /*
6065 * for Korean ime
6066 */
6067 HKL hKL = GetKeyboardLayout(0);
6068
6069 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6070 {
6071 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6072 static BOOL bSaved = FALSE;
6073
6074 if (active)
6075 {
6076 /* if we have a saved conversion status, restore it */
6077 if (bSaved)
6078 pImmSetConversionStatus(hImc, dwConversionSaved,
6079 dwSentenceSaved);
6080 bSaved = FALSE;
6081 }
6082 else
6083 {
6084 /* save conversion status and disable korean */
6085 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6086 &dwSentenceSaved))
6087 {
6088 bSaved = TRUE;
6089 pImmSetConversionStatus(hImc,
6090 dwConversionSaved & ~(IME_CMODE_NATIVE
6091 | IME_CMODE_FULLSHAPE),
6092 dwSentenceSaved);
6093 }
6094 }
6095 }
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 pImmSetOpenStatus(hImc, active);
6098 pImmReleaseContext(s_hwnd, hImc);
6099 }
6100 }
6101}
6102
6103/*
6104 * Get IM status. When IM is on, return not 0. Else return 0.
6105 */
6106 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006107im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 int status = 0;
6110 HIMC hImc;
6111
6112 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6113 {
6114 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6115 pImmReleaseContext(s_hwnd, hImc);
6116 }
6117 return status;
6118}
6119
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006120#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6123/* Win32 with GLOBAL IME */
6124
6125/*
6126 * Notify cursor position to IM.
6127 */
6128 void
6129im_set_position(int row, int col)
6130{
6131 /* Win32 with GLOBAL IME */
6132 POINT p;
6133
6134 p.x = FILL_X(col);
6135 p.y = FILL_Y(row);
6136 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6137 global_ime_set_position(&p);
6138}
6139
6140/*
6141 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6142 */
6143 void
6144im_set_active(int active)
6145{
6146 global_ime_set_status(active);
6147}
6148
6149/*
6150 * Get IM status. When IM is on, return not 0. Else return 0.
6151 */
6152 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006153im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 return global_ime_get_status();
6156}
6157#endif
6158
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006159#ifdef FEAT_MBYTE
6160/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006161 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006162 */
6163 static void
6164latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6165{
6166 int c;
6167
Bram Moolenaarca003e12006-03-17 23:19:38 +00006168 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006169 {
6170 c = *text++;
6171 switch (c)
6172 {
6173 case 0xa4: c = 0x20ac; break; /* euro */
6174 case 0xa6: c = 0x0160; break; /* S hat */
6175 case 0xa8: c = 0x0161; break; /* S -hat */
6176 case 0xb4: c = 0x017d; break; /* Z hat */
6177 case 0xb8: c = 0x017e; break; /* Z -hat */
6178 case 0xbc: c = 0x0152; break; /* OE */
6179 case 0xbd: c = 0x0153; break; /* oe */
6180 case 0xbe: c = 0x0178; break; /* Y */
6181 }
6182 *unicodebuf++ = c;
6183 }
6184}
6185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186
6187#ifdef FEAT_RIGHTLEFT
6188/*
6189 * What is this for? In the case where you are using Win98 or Win2K or later,
6190 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6191 * reverses the string sent to the TextOut... family. This sucks, because we
6192 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6193 * way to tell Windblows not to do this!
6194 *
6195 * The short of it is that this 'RevOut' only gets called if you are running
6196 * one of the new, "improved" MS OSes, and only if you are running in
6197 * 'rightleft' mode. It makes display take *slightly* longer, but not
6198 * noticeably so.
6199 */
6200 static void
6201RevOut( HDC s_hdc,
6202 int col,
6203 int row,
6204 UINT foptions,
6205 CONST RECT *pcliprect,
6206 LPCTSTR text,
6207 UINT len,
6208 CONST INT *padding)
6209{
6210 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006212 for (ix = 0; ix < (int)len; ++ix)
6213 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6214 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215}
6216#endif
6217
Bram Moolenaar92467d32017-12-05 13:22:16 +01006218 static void
6219draw_line(
6220 int x1,
6221 int y1,
6222 int x2,
6223 int y2,
6224 COLORREF color)
6225{
6226#if defined(FEAT_DIRECTX)
6227 if (IS_ENABLE_DIRECTX())
6228 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6229 else
6230#endif
6231 {
6232 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6233 HPEN old_pen = SelectObject(s_hdc, hpen);
6234 MoveToEx(s_hdc, x1, y1, NULL);
6235 /* Note: LineTo() excludes the last pixel in the line. */
6236 LineTo(s_hdc, x2, y2);
6237 DeleteObject(SelectObject(s_hdc, old_pen));
6238 }
6239}
6240
6241 static void
6242set_pixel(
6243 int x,
6244 int y,
6245 COLORREF color)
6246{
6247#if defined(FEAT_DIRECTX)
6248 if (IS_ENABLE_DIRECTX())
6249 DWriteContext_SetPixel(s_dwc, x, y, color);
6250 else
6251#endif
6252 SetPixel(s_hdc, x, y, color);
6253}
6254
6255 static void
6256fill_rect(
6257 const RECT *rcp,
6258 HBRUSH hbr,
6259 COLORREF color)
6260{
6261#if defined(FEAT_DIRECTX)
6262 if (IS_ENABLE_DIRECTX())
6263 DWriteContext_FillRect(s_dwc, rcp, color);
6264 else
6265#endif
6266 {
6267 HBRUSH hbr2;
6268
6269 if (hbr == NULL)
6270 hbr2 = CreateSolidBrush(color);
6271 else
6272 hbr2 = hbr;
6273 FillRect(s_hdc, rcp, hbr2);
6274 if (hbr == NULL)
6275 DeleteBrush(hbr2);
6276 }
6277}
6278
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 void
6280gui_mch_draw_string(
6281 int row,
6282 int col,
6283 char_u *text,
6284 int len,
6285 int flags)
6286{
6287 static int *padding = NULL;
6288 static int pad_size = 0;
6289 int i;
6290 const RECT *pcliprect = NULL;
6291 UINT foptions = 0;
6292#ifdef FEAT_MBYTE
6293 static WCHAR *unicodebuf = NULL;
6294 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006295 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 int n = 0;
6297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 int y;
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 /*
6301 * Italic and bold text seems to have an extra row of pixels at the bottom
6302 * (below where the bottom of the character should be). If we draw the
6303 * characters with a solid background, the top row of pixels in the
6304 * character below will be overwritten. We can fix this by filling in the
6305 * background ourselves, to the correct character proportions, and then
6306 * writing the character in transparent mode. Still have a problem when
6307 * the character is "_", which gets written on to the character below.
6308 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6309 * pixel in their slots, which fixes the problem with the bottom row of
6310 * pixels. We still need this code because otherwise the top row of pixels
6311 * becomes a problem. - webb.
6312 */
6313 static HBRUSH hbr_cache[2] = {NULL, NULL};
6314 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6315 static int brush_lru = 0;
6316 HBRUSH hbr;
6317 RECT rc;
6318
6319 if (!(flags & DRAW_TRANSP))
6320 {
6321 /*
6322 * Clear background first.
6323 * Note: FillRect() excludes right and bottom of rectangle.
6324 */
6325 rc.left = FILL_X(col);
6326 rc.top = FILL_Y(row);
6327#ifdef FEAT_MBYTE
6328 if (has_mbyte)
6329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006331 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 else
6334#endif
6335 rc.right = FILL_X(col + len);
6336 rc.bottom = FILL_Y(row + 1);
6337
6338 /* Cache the created brush, that saves a lot of time. We need two:
6339 * one for cursor background and one for the normal background. */
6340 if (gui.currBgColor == brush_color[0])
6341 {
6342 hbr = hbr_cache[0];
6343 brush_lru = 1;
6344 }
6345 else if (gui.currBgColor == brush_color[1])
6346 {
6347 hbr = hbr_cache[1];
6348 brush_lru = 0;
6349 }
6350 else
6351 {
6352 if (hbr_cache[brush_lru] != NULL)
6353 DeleteBrush(hbr_cache[brush_lru]);
6354 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6355 brush_color[brush_lru] = gui.currBgColor;
6356 hbr = hbr_cache[brush_lru];
6357 brush_lru = !brush_lru;
6358 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006359
Bram Moolenaar92467d32017-12-05 13:22:16 +01006360 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 SetBkMode(s_hdc, TRANSPARENT);
6363
6364 /*
6365 * When drawing block cursor, prevent inverted character spilling
6366 * over character cell (can happen with bold/italic)
6367 */
6368 if (flags & DRAW_CURSOR)
6369 {
6370 pcliprect = &rc;
6371 foptions = ETO_CLIPPED;
6372 }
6373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 SetTextColor(s_hdc, gui.currFgColor);
6375 SelectFont(s_hdc, gui.currFont);
6376
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006377#ifdef FEAT_DIRECTX
6378 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006379 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006380#endif
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6383 {
6384 vim_free(padding);
6385 pad_size = Columns;
6386
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006387 /* Don't give an out-of-memory message here, it would call us
6388 * recursively. */
6389 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (padding != NULL)
6391 for (i = 0; i < pad_size; i++)
6392 padding[i] = gui.char_width;
6393 }
6394
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 /*
6396 * We have to provide the padding argument because italic and bold versions
6397 * of fixed-width fonts are often one pixel or so wider than their normal
6398 * versions.
6399 * No check for DRAW_BOLD, Windows will have done it already.
6400 */
6401
6402#ifdef FEAT_MBYTE
6403 /* Check if there are any UTF-8 characters. If not, use normal text
6404 * output to speed up output. */
6405 if (enc_utf8)
6406 for (n = 0; n < len; ++n)
6407 if (text[n] >= 0x80)
6408 break;
6409
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006410# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006411 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6412 * required that unicode drawing routine, currently. So this forces it
6413 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006414 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006415 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006416# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006419 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006421 if ((enc_utf8
6422 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6423 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 && (unicodebuf == NULL || len > unibuflen))
6425 {
6426 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006427 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006430 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 unibuflen = len;
6433 }
6434
6435 if (enc_utf8 && n < len && unicodebuf != NULL)
6436 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006437 /* Output UTF-8 characters. Composing characters should be
6438 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006439 int i;
6440 int wlen; /* string length in words */
6441 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 int cells; /* cell width of string up to composing char */
6443 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006444 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006446 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006447 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006449 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006451 c = utf_ptr2char(text + i);
6452 if (c >= 0x10000)
6453 {
6454 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006455 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6456 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006457 }
6458 else
6459 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006460 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006461 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006462
6463 if (utf_iscomposing(c))
6464 cw = 0;
6465 else
6466 {
6467 cw = utf_char2cells(c);
6468 if (cw > 2) /* don't use 4 for unprintable char */
6469 cw = 1;
6470 }
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (unicodepdy != NULL)
6473 {
6474 /* Use unicodepdy to make characters fit as we expect, even
6475 * when the font uses different widths (e.g., bold character
6476 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006477 if (c >= 0x10000)
6478 {
6479 unicodepdy[wlen - 2] = cw * gui.char_width;
6480 unicodepdy[wlen - 1] = 0;
6481 }
6482 else
6483 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006486 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006487 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006489# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006490 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006491 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006492 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006493 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006494 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006495 gui.char_width, gui.currFgColor,
6496 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006497 }
6498 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006499# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006500 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6501 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 len = cells; /* used for underlining */
6503 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006504 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 /* If we want to display codepage data, and the current CP is not the
6507 * ANSI one, we need to go via Unicode. */
6508 if (unicodebuf != NULL)
6509 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006510 if (enc_latin9)
6511 latin9_to_ucs(text, len, unicodebuf);
6512 else
6513 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 MB_PRECOMPOSED,
6515 (char *)text, len,
6516 (LPWSTR)unicodebuf, unibuflen);
6517 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006518 {
6519 /* Use unicodepdy to make characters fit as we expect, even
6520 * when the font uses different widths (e.g., bold character
6521 * is wider). */
6522 if (unicodepdy != NULL)
6523 {
6524 int i;
6525 int cw;
6526
6527 for (i = 0; i < len; ++i)
6528 {
6529 cw = utf_char2cells(unicodebuf[i]);
6530 if (cw > 2)
6531 cw = 1;
6532 unicodepdy[i] = cw * gui.char_width;
6533 }
6534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006536 foptions, pcliprect, unicodebuf, len, unicodepdy);
6537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 }
6539 }
6540 else
6541#endif
6542 {
6543#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006544 /* Windows will mess up RL text, so we have to draw it character by
6545 * character. Only do this if RL is on, since it's slow. */
6546 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6548 foptions, pcliprect, (char *)text, len, padding);
6549 else
6550#endif
6551 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6552 foptions, pcliprect, (char *)text, len, padding);
6553 }
6554
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006555 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (flags & DRAW_UNDERL)
6557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 /* When p_linespace is 0, overwrite the bottom row of pixels.
6559 * Otherwise put the line just below the character. */
6560 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (p_linespace > 1)
6562 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006563 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006565
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006566 /* Strikethrough */
6567 if (flags & DRAW_STRIKE)
6568 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006569 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006570 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006571 }
6572
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006573 /* Undercurl */
6574 if (flags & DRAW_UNDERC)
6575 {
6576 int x;
6577 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006578 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006579
6580 y = FILL_Y(row + 1) - 1;
6581 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6582 {
6583 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006584 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006585 }
6586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587}
6588
6589
6590/*
6591 * Output routines.
6592 */
6593
6594/* Flush any output to the screen */
6595 void
6596gui_mch_flush(void)
6597{
6598# if defined(__BORLANDC__)
6599 /*
6600 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6601 * prototype declaration.
6602 * The compiler complains if __stdcall is not used in both declarations.
6603 */
6604 BOOL __stdcall GdiFlush(void);
6605# endif
6606
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006607#if defined(FEAT_DIRECTX)
6608 if (IS_ENABLE_DIRECTX())
6609 DWriteContext_Flush(s_dwc);
6610#endif
6611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 GdiFlush();
6613}
6614
6615 static void
6616clear_rect(RECT *rcp)
6617{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006618 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619}
6620
6621
Bram Moolenaarc716c302006-01-21 22:12:51 +00006622 void
6623gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6624{
6625 RECT workarea_rect;
6626
6627 get_work_area(&workarea_rect);
6628
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006629 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006630 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006631 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006632
6633 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6634 * the menubar for MSwin, we subtract it from the screen height, so that
6635 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006636 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006637 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006638 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006639 - GetSystemMetrics(SM_CYCAPTION)
6640#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006641 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006642#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006643 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006644}
6645
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647#if defined(FEAT_MENU) || defined(PROTO)
6648/*
6649 * Add a sub menu to the menu bar.
6650 */
6651 void
6652gui_mch_add_menu(
6653 vimmenu_T *menu,
6654 int pos)
6655{
6656 vimmenu_T *parent = menu->parent;
6657
6658 menu->submenu_id = CreatePopupMenu();
6659 menu->id = s_menu_id++;
6660
6661 if (menu_is_menubar(menu->name))
6662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006664 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006666 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6667 {
6668 /* 'encoding' differs from active codepage: convert menu name
6669 * and use wide function */
6670 wn = enc_to_utf16(menu->name, NULL);
6671 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006673 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006675 infow.cbSize = sizeof(infow);
6676 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6677 | MIIM_SUBMENU;
6678 infow.dwItemData = (long_u)menu;
6679 infow.wID = menu->id;
6680 infow.fType = MFT_STRING;
6681 infow.dwTypeData = wn;
6682 infow.cch = (UINT)wcslen(wn);
6683 infow.hSubMenu = menu->submenu_id;
6684 InsertMenuItemW((parent == NULL)
6685 ? s_menuBar : parent->submenu_id,
6686 (UINT)pos, TRUE, &infow);
6687 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006691 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006693 {
6694 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006696 info.cbSize = sizeof(info);
6697 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6698 info.dwItemData = (long_u)menu;
6699 info.wID = menu->id;
6700 info.fType = MFT_STRING;
6701 info.dwTypeData = (LPTSTR)menu->name;
6702 info.cch = (UINT)STRLEN(menu->name);
6703 info.hSubMenu = menu->submenu_id;
6704 InsertMenuItem((parent == NULL)
6705 ? s_menuBar : parent->submenu_id,
6706 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708 }
6709
6710 /* Fix window size if menu may have wrapped */
6711 if (parent == NULL)
6712 gui_mswin_get_menu_height(!gui.starting);
6713#ifdef FEAT_TEAROFF
6714 else if (IsWindow(parent->tearoff_handle))
6715 rebuild_tearoff(parent);
6716#endif
6717}
6718
6719 void
6720gui_mch_show_popupmenu(vimmenu_T *menu)
6721{
6722 POINT mp;
6723
6724 (void)GetCursorPos((LPPOINT)&mp);
6725 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6726}
6727
6728 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006729gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 vimmenu_T *menu = gui_find_menu(path_name);
6732
6733 if (menu != NULL)
6734 {
6735 POINT p;
6736
6737 /* Find the position of the current cursor */
6738 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006739 if (mouse_pos)
6740 {
6741 int mx, my;
6742
6743 gui_mch_getmouse(&mx, &my);
6744 p.x += mx;
6745 p.y += my;
6746 }
6747 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006749 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6751 }
6752 msg_scroll = FALSE;
6753 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6754 }
6755}
6756
6757#if defined(FEAT_TEAROFF) || defined(PROTO)
6758/*
6759 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6760 * create it as a pseudo-"tearoff menu".
6761 */
6762 void
6763gui_make_tearoff(char_u *path_name)
6764{
6765 vimmenu_T *menu = gui_find_menu(path_name);
6766
6767 /* Found the menu, so tear it off. */
6768 if (menu != NULL)
6769 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6770}
6771#endif
6772
6773/*
6774 * Add a menu item to a menu
6775 */
6776 void
6777gui_mch_add_menu_item(
6778 vimmenu_T *menu,
6779 int idx)
6780{
6781 vimmenu_T *parent = menu->parent;
6782
6783 menu->id = s_menu_id++;
6784 menu->submenu_id = NULL;
6785
6786#ifdef FEAT_TEAROFF
6787 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6788 {
6789 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6790 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6791 }
6792 else
6793#endif
6794#ifdef FEAT_TOOLBAR
6795 if (menu_is_toolbar(parent->name))
6796 {
6797 TBBUTTON newtb;
6798
6799 vim_memset(&newtb, 0, sizeof(newtb));
6800 if (menu_is_separator(menu->name))
6801 {
6802 newtb.iBitmap = 0;
6803 newtb.fsStyle = TBSTYLE_SEP;
6804 }
6805 else
6806 {
6807 newtb.iBitmap = get_toolbar_bitmap(menu);
6808 newtb.fsStyle = TBSTYLE_BUTTON;
6809 }
6810 newtb.idCommand = menu->id;
6811 newtb.fsState = TBSTATE_ENABLED;
6812 newtb.iString = 0;
6813 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6814 (LPARAM)&newtb);
6815 menu->submenu_id = (HMENU)-1;
6816 }
6817 else
6818#endif
6819 {
6820#ifdef FEAT_MBYTE
6821 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6824 {
6825 /* 'encoding' differs from active codepage: convert menu item name
6826 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006827 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 if (wn != NULL)
6829 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006830 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 (menu_is_separator(menu->name)
6832 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6833 (UINT)menu->id, wn);
6834 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836 }
6837 if (wn == NULL)
6838#endif
6839 InsertMenu(parent->submenu_id, (UINT)idx,
6840 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6841 | MF_BYPOSITION,
6842 (UINT)menu->id, (LPCTSTR)menu->name);
6843#ifdef FEAT_TEAROFF
6844 if (IsWindow(parent->tearoff_handle))
6845 rebuild_tearoff(parent);
6846#endif
6847 }
6848}
6849
6850/*
6851 * Destroy the machine specific menu widget.
6852 */
6853 void
6854gui_mch_destroy_menu(vimmenu_T *menu)
6855{
6856#ifdef FEAT_TOOLBAR
6857 /*
6858 * is this a toolbar button?
6859 */
6860 if (menu->submenu_id == (HMENU)-1)
6861 {
6862 int iButton;
6863
6864 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6865 (WPARAM)menu->id, 0);
6866 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6867 }
6868 else
6869#endif
6870 {
6871 if (menu->parent != NULL
6872 && menu_is_popup(menu->parent->dname)
6873 && menu->parent->submenu_id != NULL)
6874 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6875 else
6876 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6877 if (menu->submenu_id != NULL)
6878 DestroyMenu(menu->submenu_id);
6879#ifdef FEAT_TEAROFF
6880 if (IsWindow(menu->tearoff_handle))
6881 DestroyWindow(menu->tearoff_handle);
6882 if (menu->parent != NULL
6883 && menu->parent->children != NULL
6884 && IsWindow(menu->parent->tearoff_handle))
6885 {
6886 /* This menu must not show up when rebuilding the tearoff window. */
6887 menu->modes = 0;
6888 rebuild_tearoff(menu->parent);
6889 }
6890#endif
6891 }
6892}
6893
6894#ifdef FEAT_TEAROFF
6895 static void
6896rebuild_tearoff(vimmenu_T *menu)
6897{
6898 /*hackish*/
6899 char_u tbuf[128];
6900 RECT trect;
6901 RECT rct;
6902 RECT roct;
6903 int x, y;
6904
6905 HWND thwnd = menu->tearoff_handle;
6906
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006907 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (GetWindowRect(thwnd, &trect)
6909 && GetWindowRect(s_hwnd, &rct)
6910 && GetClientRect(s_hwnd, &roct))
6911 {
6912 x = trect.left - rct.left;
6913 y = (trect.top - rct.bottom + roct.bottom);
6914 }
6915 else
6916 {
6917 x = y = 0xffffL;
6918 }
6919 DestroyWindow(thwnd);
6920 if (menu->children != NULL)
6921 {
6922 gui_mch_tearoff(tbuf, menu, x, y);
6923 if (IsWindow(menu->tearoff_handle))
6924 (void) SetWindowPos(menu->tearoff_handle,
6925 NULL,
6926 (int)trect.left,
6927 (int)trect.top,
6928 0, 0,
6929 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6930 }
6931}
6932#endif /* FEAT_TEAROFF */
6933
6934/*
6935 * Make a menu either grey or not grey.
6936 */
6937 void
6938gui_mch_menu_grey(
6939 vimmenu_T *menu,
6940 int grey)
6941{
6942#ifdef FEAT_TOOLBAR
6943 /*
6944 * is this a toolbar button?
6945 */
6946 if (menu->submenu_id == (HMENU)-1)
6947 {
6948 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6949 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6950 }
6951 else
6952#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006953 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6954 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956#ifdef FEAT_TEAROFF
6957 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6958 {
6959 WORD menuID;
6960 HWND menuHandle;
6961
6962 /*
6963 * A tearoff button has changed state.
6964 */
6965 if (menu->children == NULL)
6966 menuID = (WORD)(menu->id);
6967 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006968 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6970 if (menuHandle)
6971 EnableWindow(menuHandle, !grey);
6972
6973 }
6974#endif
6975}
6976
6977#endif /* FEAT_MENU */
6978
6979
6980/* define some macros used to make the dialogue creation more readable */
6981
6982#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6983#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006984#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6987/*
6988 * stuff for dialogs
6989 */
6990
6991/*
6992 * The callback routine used by all the dialogs. Very simple. First,
6993 * acknowledges the INITDIALOG message so that Windows knows to do standard
6994 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6995 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6996 * number.
6997 */
6998 static LRESULT CALLBACK
6999dialog_callback(
7000 HWND hwnd,
7001 UINT message,
7002 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007003 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 if (message == WM_INITDIALOG)
7006 {
7007 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7008 /* Set focus to the dialog. Set the default button, if specified. */
7009 (void)SetFocus(hwnd);
7010 if (dialog_default_button > IDCANCEL)
7011 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007012 else
7013 /* We don't have a default, set focus on another element of the
7014 * dialog window, probably the icon */
7015 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 return FALSE;
7017 }
7018
7019 if (message == WM_COMMAND)
7020 {
7021 int button = LOWORD(wParam);
7022
7023 /* Don't end the dialog if something was selected that was
7024 * not a button.
7025 */
7026 if (button >= DLG_NONBUTTON_CONTROL)
7027 return TRUE;
7028
7029 /* If the edit box exists, copy the string. */
7030 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007031 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007032# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007033 /* If the OS is Windows NT, and 'encoding' differs from active
7034 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007035 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007036 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007037 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7038 char_u *p;
7039
7040 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7041 p = utf16_to_enc(wp, NULL);
7042 vim_strncpy(s_textfield, p, IOSIZE);
7043 vim_free(p);
7044 vim_free(wp);
7045 }
7046 else
7047# endif
7048 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007049 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 /*
7053 * Need to check for IDOK because if the user just hits Return to
7054 * accept the default value, some reason this is what we get.
7055 */
7056 if (button == IDOK)
7057 {
7058 if (dialog_default_button > IDCANCEL)
7059 EndDialog(hwnd, dialog_default_button);
7060 }
7061 else
7062 EndDialog(hwnd, button - IDCANCEL);
7063 return TRUE;
7064 }
7065
7066 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7067 {
7068 EndDialog(hwnd, 0);
7069 return TRUE;
7070 }
7071 return FALSE;
7072}
7073
7074/*
7075 * Create a dialog dynamically from the parameter strings.
7076 * type = type of dialog (question, alert, etc.)
7077 * title = dialog title. may be NULL for default title.
7078 * message = text to display. Dialog sizes to accommodate it.
7079 * buttons = '\n' separated list of button captions, default first.
7080 * dfltbutton = number of default button.
7081 *
7082 * This routine returns 1 if the first button is pressed,
7083 * 2 for the second, etc.
7084 *
7085 * 0 indicates Esc was pressed.
7086 * -1 for unexpected error
7087 *
7088 * If stubbing out this fn, return 1.
7089 */
7090
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007091static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 "IDR_VIM",
7094 "IDR_VIM_ERROR",
7095 "IDR_VIM_ALERT",
7096 "IDR_VIM_INFO",
7097 "IDR_VIM_QUESTION"
7098};
7099
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 int
7101gui_mch_dialog(
7102 int type,
7103 char_u *title,
7104 char_u *message,
7105 char_u *buttons,
7106 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007107 char_u *textfield,
7108 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109{
7110 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007111 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 int numButtons;
7113 int *buttonWidths, *buttonPositions;
7114 int buttonYpos;
7115 int nchar, i;
7116 DWORD lStyle;
7117 int dlgwidth = 0;
7118 int dlgheight;
7119 int editboxheight;
7120 int horizWidth = 0;
7121 int msgheight;
7122 char_u *pstart;
7123 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007124 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 char_u *tbuffer;
7126 RECT rect;
7127 HWND hwnd;
7128 HDC hdc;
7129 HFONT font, oldFont;
7130 TEXTMETRIC fontInfo;
7131 int fontHeight;
7132 int textWidth, minButtonWidth, messageWidth;
7133 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007134 int maxDialogHeight;
7135 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int vertical;
7137 int dlgPaddingX;
7138 int dlgPaddingY;
7139#ifdef USE_SYSMENU_FONT
7140 LOGFONT lfSysmenu;
7141 int use_lfSysmenu = FALSE;
7142#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007143 garray_T ga;
7144 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146#ifndef NO_CONSOLE
7147 /* Don't output anything in silent mode ("ex -s") */
7148 if (silent_mode)
7149 return dfltbutton; /* return default option */
7150#endif
7151
Bram Moolenaar748bf032005-02-02 23:04:36 +00007152 if (s_hwnd == NULL)
7153 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 if ((type < 0) || (type > VIM_LAST_TYPE))
7156 type = 0;
7157
7158 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007159 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007160 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007161 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 if (p == NULL)
7164 return -1;
7165
7166 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007167 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7169 * const.
7170 */
7171 tbuffer = vim_strsave(buttons);
7172 if (tbuffer == NULL)
7173 return -1;
7174
7175 --dfltbutton; /* Change from one-based to zero-based */
7176
7177 /* Count buttons */
7178 numButtons = 1;
7179 for (i = 0; tbuffer[i] != '\0'; i++)
7180 {
7181 if (tbuffer[i] == DLG_BUTTON_SEP)
7182 numButtons++;
7183 }
7184 if (dfltbutton >= numButtons)
7185 dfltbutton = -1;
7186
7187 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007188 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (buttonWidths == NULL)
7190 return -1;
7191
7192 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007193 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (buttonPositions == NULL)
7195 return -1;
7196
7197 /*
7198 * Calculate how big the dialog must be.
7199 */
7200 hwnd = GetDesktopWindow();
7201 hdc = GetWindowDC(hwnd);
7202#ifdef USE_SYSMENU_FONT
7203 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7204 {
7205 font = CreateFontIndirect(&lfSysmenu);
7206 use_lfSysmenu = TRUE;
7207 }
7208 else
7209#endif
7210 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7211 VARIABLE_PITCH , DLG_FONT_NAME);
7212 if (s_usenewlook)
7213 {
7214 oldFont = SelectFont(hdc, font);
7215 dlgPaddingX = DLG_PADDING_X;
7216 dlgPaddingY = DLG_PADDING_Y;
7217 }
7218 else
7219 {
7220 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7221 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7222 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7223 }
7224 GetTextMetrics(hdc, &fontInfo);
7225 fontHeight = fontInfo.tmHeight;
7226
7227 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007228 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007231 if (s_hwnd == NULL)
7232 {
7233 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaarc716c302006-01-21 22:12:51 +00007235 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007236 get_work_area(&workarea_rect);
7237 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7238 if (maxDialogWidth > 600)
7239 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007240 /* Leave some room for the taskbar. */
7241 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007242 }
7243 else
7244 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007245 /* Use our own window for the size, unless it's very small. */
7246 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007247 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007248 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007249 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007250 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7251 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007252
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007253 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007254 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007255 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007256 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007257 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7258 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7259 }
7260
7261 /* Set dlgwidth to width of message.
7262 * Copy the message into "ga", changing NL to CR-NL and inserting line
7263 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 pstart = message;
7265 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007266 msgheight = 0;
7267 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 do
7269 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007270 msgheight += fontHeight; /* at least one line */
7271
7272 /* Need to figure out where to break the string. The system does it
7273 * at a word boundary, which would mean we can't compute the number of
7274 * wrapped lines. */
7275 textWidth = 0;
7276 last_white = NULL;
7277 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007278 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007279#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007280 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007281#else
7282 l = 1;
7283#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007284 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007285 && textWidth > maxDialogWidth * 3 / 4)
7286 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007287 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007288 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007289 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007290 /* Line will wrap. */
7291 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007292 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007293 textWidth = 0;
7294
7295 if (last_white != NULL)
7296 {
7297 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007299 pend = last_white + 1;
7300 last_white = NULL;
7301 }
7302 ga_append(&ga, '\r');
7303 ga_append(&ga, '\n');
7304 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007305 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007306
7307 while (--l >= 0)
7308 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007309 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007310 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007312
7313 ga_append(&ga, '\r');
7314 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 pstart = pend + 1;
7316 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007317
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007318 if (ga.ga_data != NULL)
7319 message = ga.ga_data;
7320
Bram Moolenaar748bf032005-02-02 23:04:36 +00007321 messageWidth += 10; /* roundoff space */
7322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007324 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7325 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
7327 if (msgheight < DLG_ICON_HEIGHT)
7328 msgheight = DLG_ICON_HEIGHT;
7329
7330 /*
7331 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007332 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007334 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (!vertical)
7336 {
7337 // Place buttons horizontally if they fit.
7338 horizWidth = dlgPaddingX;
7339 pstart = tbuffer;
7340 i = 0;
7341 do
7342 {
7343 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7344 if (pend == NULL)
7345 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007346 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 if (textWidth < minButtonWidth)
7348 textWidth = minButtonWidth;
7349 textWidth += dlgPaddingX; /* Padding within button */
7350 buttonWidths[i] = textWidth;
7351 buttonPositions[i++] = horizWidth;
7352 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7353 pstart = pend + 1;
7354 } while (*pend != NUL);
7355
7356 if (horizWidth > maxDialogWidth)
7357 vertical = TRUE; // Too wide to fit on the screen.
7358 else if (horizWidth > dlgwidth)
7359 dlgwidth = horizWidth;
7360 }
7361
7362 if (vertical)
7363 {
7364 // Stack buttons vertically.
7365 pstart = tbuffer;
7366 do
7367 {
7368 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7369 if (pend == NULL)
7370 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007371 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 textWidth += dlgPaddingX; /* Padding within button */
7373 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7374 if (textWidth > dlgwidth)
7375 dlgwidth = textWidth;
7376 pstart = pend + 1;
7377 } while (*pend != NUL);
7378 }
7379
7380 if (dlgwidth < DLG_MIN_WIDTH)
7381 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7382
7383 /* start to fill in the dlgtemplate information. addressing by WORDs */
7384 if (s_usenewlook)
7385 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7386 else
7387 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7388
7389 add_long(lStyle);
7390 add_long(0); // (lExtendedStyle)
7391 pnumitems = p; /*save where the number of items must be stored*/
7392 add_word(0); // NumberOfItems(will change later)
7393 add_word(10); // x
7394 add_word(10); // y
7395 add_word(PixelToDialogX(dlgwidth)); // cx
7396
7397 // Dialog height.
7398 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007399 dlgheight = msgheight + 2 * dlgPaddingY
7400 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 else
7402 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7403
7404 // Dialog needs to be taller if contains an edit box.
7405 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7406 if (textfield != NULL)
7407 dlgheight += editboxheight;
7408
Bram Moolenaara95d8232013-08-07 15:27:11 +02007409 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7410 if (dlgheight > maxDialogHeight)
7411 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007412 msgheight = msgheight - (dlgheight - maxDialogHeight);
7413 dlgheight = maxDialogHeight;
7414 scroll_flag = WS_VSCROLL;
7415 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7416 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007417 }
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 add_word(PixelToDialogY(dlgheight));
7420
7421 add_word(0); // Menu
7422 add_word(0); // Class
7423
7424 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007425 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7426 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 p += nchar;
7428
7429 if (s_usenewlook)
7430 {
7431 /* do the font, since DS_3DLOOK doesn't work properly */
7432#ifdef USE_SYSMENU_FONT
7433 if (use_lfSysmenu)
7434 {
7435 /* point size */
7436 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7437 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007438 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440 else
7441#endif
7442 {
7443 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007444 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446 p += nchar;
7447 }
7448
7449 buttonYpos = msgheight + 2 * dlgPaddingY;
7450
7451 if (textfield != NULL)
7452 buttonYpos += editboxheight;
7453
7454 pstart = tbuffer;
7455 if (!vertical)
7456 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7457 for (i = 0; i < numButtons; i++)
7458 {
7459 /* get end of this button. */
7460 for ( pend = pstart;
7461 *pend && (*pend != DLG_BUTTON_SEP);
7462 pend++)
7463 ;
7464
7465 if (*pend)
7466 *pend = '\0';
7467
7468 /*
7469 * old NOTE:
7470 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7471 * the focus to the first tab-able button and in so doing makes that
7472 * the default!! Grrr. Workaround: Make the default button the only
7473 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7474 * he/she can use arrow keys.
7475 *
7476 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007477 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 * dialog. Also needed for when the textfield is the default control.
7479 * It appears to work now (perhaps not on Win95?).
7480 */
7481 if (vertical)
7482 {
7483 p = add_dialog_element(p,
7484 (i == dfltbutton
7485 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7486 PixelToDialogX(DLG_VERT_PADDING_X),
7487 PixelToDialogY(buttonYpos /* TBK */
7488 + 2 * fontHeight * i),
7489 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7490 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007491 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 else
7494 {
7495 p = add_dialog_element(p,
7496 (i == dfltbutton
7497 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7498 PixelToDialogX(horizWidth + buttonPositions[i]),
7499 PixelToDialogY(buttonYpos), /* TBK */
7500 PixelToDialogX(buttonWidths[i]),
7501 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007502 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504 pstart = pend + 1; /*next button*/
7505 }
7506 *pnumitems += numButtons;
7507
7508 /* Vim icon */
7509 p = add_dialog_element(p, SS_ICON,
7510 PixelToDialogX(dlgPaddingX),
7511 PixelToDialogY(dlgPaddingY),
7512 PixelToDialogX(DLG_ICON_WIDTH),
7513 PixelToDialogY(DLG_ICON_HEIGHT),
7514 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7515 dlg_icons[type]);
7516
Bram Moolenaar748bf032005-02-02 23:04:36 +00007517 /* Dialog message */
7518 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7519 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7520 PixelToDialogY(dlgPaddingY),
7521 (WORD)(PixelToDialogX(messageWidth) + 1),
7522 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007523 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524
7525 /* Edit box */
7526 if (textfield != NULL)
7527 {
7528 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7529 PixelToDialogX(2 * dlgPaddingX),
7530 PixelToDialogY(2 * dlgPaddingY + msgheight),
7531 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7532 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007533 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 *pnumitems += 1;
7535 }
7536
7537 *pnumitems += 2;
7538
7539 SelectFont(hdc, oldFont);
7540 DeleteObject(font);
7541 ReleaseDC(hwnd, hdc);
7542
7543 /* Let the dialog_callback() function know which button to make default
7544 * If we have an edit box, make that the default. We also need to tell
7545 * dialog_callback() if this dialog contains an edit box or not. We do
7546 * this by setting s_textfield if it does.
7547 */
7548 if (textfield != NULL)
7549 {
7550 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7551 s_textfield = textfield;
7552 }
7553 else
7554 {
7555 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7556 s_textfield = NULL;
7557 }
7558
7559 /* show the dialog box modally and get a return value */
7560 nchar = (int)DialogBoxIndirect(
7561 s_hinst,
7562 (LPDLGTEMPLATE)pdlgtemplate,
7563 s_hwnd,
7564 (DLGPROC)dialog_callback);
7565
7566 LocalFree(LocalHandle(pdlgtemplate));
7567 vim_free(tbuffer);
7568 vim_free(buttonWidths);
7569 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007570 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 /* Focus back to our window (for when MDI is used). */
7573 (void)SetFocus(s_hwnd);
7574
7575 return nchar;
7576}
7577
7578#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580/*
7581 * Put a simple element (basic class) onto a dialog template in memory.
7582 * return a pointer to where the next item should be added.
7583 *
7584 * parameters:
7585 * lStyle = additional style flags
7586 * (be careful, NT3.51 & Win32s will ignore the new ones)
7587 * x,y = x & y positions IN DIALOG UNITS
7588 * w,h = width and height IN DIALOG UNITS
7589 * Id = ID used in messages
7590 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7591 * caption = usually text or resource name
7592 *
7593 * TODO: use the length information noted here to enable the dialog creation
7594 * routines to work out more exactly how much memory they need to alloc.
7595 */
7596 static PWORD
7597add_dialog_element(
7598 PWORD p,
7599 DWORD lStyle,
7600 WORD x,
7601 WORD y,
7602 WORD w,
7603 WORD h,
7604 WORD Id,
7605 WORD clss,
7606 const char *caption)
7607{
7608 int nchar;
7609
7610 p = lpwAlign(p); /* Align to dword boundary*/
7611 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7612 *p++ = LOWORD(lStyle);
7613 *p++ = HIWORD(lStyle);
7614 *p++ = 0; // LOWORD (lExtendedStyle)
7615 *p++ = 0; // HIWORD (lExtendedStyle)
7616 *p++ = x;
7617 *p++ = y;
7618 *p++ = w;
7619 *p++ = h;
7620 *p++ = Id; //9 or 10 words in all
7621
7622 *p++ = (WORD)0xffff;
7623 *p++ = clss; //2 more here
7624
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007625 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 p += nchar;
7627
7628 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7629
7630 return p; //total = 15+ (strlen(caption)) words
7631 // = 30 + 2(strlen(caption) bytes reqd
7632}
7633
7634
7635/*
7636 * Helper routine. Take an input pointer, return closest pointer that is
7637 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7638 */
7639 static LPWORD
7640lpwAlign(
7641 LPWORD lpIn)
7642{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007643 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007645 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 ul += 3;
7647 ul >>= 2;
7648 ul <<= 2;
7649 return (LPWORD)ul;
7650}
7651
7652/*
7653 * Helper routine. Takes second parameter as Ansi string, copies it to first
7654 * parameter as wide character (16-bits / char) string, and returns integer
7655 * number of wide characters (words) in string (including the trailing wide
7656 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007657 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7658 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 static int
7660nCopyAnsiToWideChar(
7661 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007662 LPSTR lpAnsiIn,
7663 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
7665 int nChar = 0;
7666#ifdef FEAT_MBYTE
7667 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7668 int i;
7669 WCHAR *wn;
7670
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007671 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {
7673 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007674 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (wn != NULL)
7676 {
7677 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007678 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 vim_free(wn);
7680 }
7681 }
7682 if (nChar == 0)
7683 /* Use Win32 conversion function. */
7684 nChar = MultiByteToWideChar(
7685 enc_codepage > 0 ? enc_codepage : CP_ACP,
7686 MB_PRECOMPOSED,
7687 lpAnsiIn, len,
7688 lpWCStr, len);
7689 for (i = 0; i < nChar; ++i)
7690 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7691 lpWCStr[i] = (WORD)' ';
7692#else
7693 do
7694 {
7695 if (*lpAnsiIn == '\t')
7696 *lpWCStr++ = (WORD)' ';
7697 else
7698 *lpWCStr++ = (WORD)*lpAnsiIn;
7699 nChar++;
7700 } while (*lpAnsiIn++);
7701#endif
7702
7703 return nChar;
7704}
7705
7706
7707#ifdef FEAT_TEAROFF
7708/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007709 * Lookup menu handle from "menu_id".
7710 */
7711 static HMENU
7712tearoff_lookup_menuhandle(
7713 vimmenu_T *menu,
7714 WORD menu_id)
7715{
7716 for ( ; menu != NULL; menu = menu->next)
7717 {
7718 if (menu->modes == 0) /* this menu has just been deleted */
7719 continue;
7720 if (menu_is_separator(menu->dname))
7721 continue;
7722 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7723 return menu->submenu_id;
7724 }
7725 return NULL;
7726}
7727
7728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 * The callback function for all the modeless dialogs that make up the
7730 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7731 * thinking its menus have been clicked), and go away when closed.
7732 */
7733 static LRESULT CALLBACK
7734tearoff_callback(
7735 HWND hwnd,
7736 UINT message,
7737 WPARAM wParam,
7738 LPARAM lParam)
7739{
7740 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007741 {
7742 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
7746 /* May show the mouse pointer again. */
7747 HandleMouseHide(message, lParam);
7748
7749 if (message == WM_COMMAND)
7750 {
7751 if ((WORD)(LOWORD(wParam)) & 0x8000)
7752 {
7753 POINT mp;
7754 RECT rect;
7755
7756 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7757 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007758 vimmenu_T *menu;
7759
7760 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007762 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7764 (int)rect.right - 8,
7765 (int)mp.y,
7766 (int)0, /*reserved param*/
7767 s_hwnd,
7768 NULL);
7769 /*
7770 * NOTE: The pop-up menu can eat the mouse up event.
7771 * We deal with this in normal.c.
7772 */
7773 }
7774 }
7775 else
7776 /* Pass on messages to the main Vim window */
7777 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7778 /*
7779 * Give main window the focus back: this is so after
7780 * choosing a tearoff button you can start typing again
7781 * straight away.
7782 */
7783 (void)SetFocus(s_hwnd);
7784 return TRUE;
7785 }
7786 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7787 {
7788 DestroyWindow(hwnd);
7789 return TRUE;
7790 }
7791
7792 /* When moved around, give main window the focus back. */
7793 if (message == WM_EXITSIZEMOVE)
7794 (void)SetActiveWindow(s_hwnd);
7795
7796 return FALSE;
7797}
7798#endif
7799
7800
7801/*
7802 * Decide whether to use the "new look" (small, non-bold font) or the "old
7803 * look" (big, clanky font) for dialogs, and work out a few values for use
7804 * later accordingly.
7805 */
7806 static void
7807get_dialog_font_metrics(void)
7808{
7809 HDC hdc;
7810 HFONT hfontTools = 0;
7811 DWORD dlgFontSize;
7812 SIZE size;
7813#ifdef USE_SYSMENU_FONT
7814 LOGFONT lfSysmenu;
7815#endif
7816
7817 s_usenewlook = FALSE;
7818
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007820 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7821 hfontTools = CreateFontIndirect(&lfSysmenu);
7822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823#endif
7824 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7825 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7826
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007827 if (hfontTools)
7828 {
7829 hdc = GetDC(s_hwnd);
7830 SelectObject(hdc, hfontTools);
7831 /*
7832 * GetTextMetrics() doesn't return the right value in
7833 * tmAveCharWidth, so we have to figure out the dialog base units
7834 * ourselves.
7835 */
7836 GetTextExtentPoint(hdc,
7837 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7838 52, &size);
7839 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007841 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7842 s_dlgfntheight = (WORD)size.cy;
7843 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 }
7845
7846 if (!s_usenewlook)
7847 {
7848 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7849 s_dlgfntwidth = LOWORD(dlgFontSize);
7850 s_dlgfntheight = HIWORD(dlgFontSize);
7851 }
7852}
7853
7854#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7855/*
7856 * Create a pseudo-"tearoff menu" based on the child
7857 * items of a given menu pointer.
7858 */
7859 static void
7860gui_mch_tearoff(
7861 char_u *title,
7862 vimmenu_T *menu,
7863 int initX,
7864 int initY)
7865{
7866 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7867 int template_len;
7868 int nchar, textWidth, submenuWidth;
7869 DWORD lStyle;
7870 DWORD lExtendedStyle;
7871 WORD dlgwidth;
7872 WORD menuID;
7873 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007874 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 vimmenu_T *the_menu = menu;
7876 HWND hwnd;
7877 HDC hdc;
7878 HFONT font, oldFont;
7879 int col, spaceWidth, len;
7880 int columnWidths[2];
7881 char_u *label, *text;
7882 int acLen = 0;
7883 int nameLen;
7884 int padding0, padding1, padding2 = 0;
7885 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007886 int x;
7887 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#ifdef USE_SYSMENU_FONT
7889 LOGFONT lfSysmenu;
7890 int use_lfSysmenu = FALSE;
7891#endif
7892
7893 /*
7894 * If this menu is already torn off, move it to the mouse position.
7895 */
7896 if (IsWindow(menu->tearoff_handle))
7897 {
7898 POINT mp;
7899 if (GetCursorPos((LPPOINT)&mp))
7900 {
7901 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7902 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7903 }
7904 return;
7905 }
7906
7907 /*
7908 * Create a new tearoff.
7909 */
7910 if (*title == MNU_HIDDEN_CHAR)
7911 title++;
7912
7913 /* Allocate memory to store the dialog template. It's made bigger when
7914 * needed. */
7915 template_len = DLG_ALLOC_SIZE;
7916 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7917 if (p == NULL)
7918 return;
7919
7920 hwnd = GetDesktopWindow();
7921 hdc = GetWindowDC(hwnd);
7922#ifdef USE_SYSMENU_FONT
7923 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7924 {
7925 font = CreateFontIndirect(&lfSysmenu);
7926 use_lfSysmenu = TRUE;
7927 }
7928 else
7929#endif
7930 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7931 VARIABLE_PITCH , DLG_FONT_NAME);
7932 if (s_usenewlook)
7933 oldFont = SelectFont(hdc, font);
7934 else
7935 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7936
7937 /* Calculate width of a single space. Used for padding columns to the
7938 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007939 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940
7941 /* Figure out max width of the text column, the accelerator column and the
7942 * optional submenu column. */
7943 submenuWidth = 0;
7944 for (col = 0; col < 2; col++)
7945 {
7946 columnWidths[col] = 0;
7947 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7948 {
7949 /* Use "dname" here to compute the width of the visible text. */
7950 text = (col == 0) ? pmenu->dname : pmenu->actext;
7951 if (text != NULL && *text != NUL)
7952 {
7953 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7954 if (textWidth > columnWidths[col])
7955 columnWidths[col] = textWidth;
7956 }
7957 if (pmenu->children != NULL)
7958 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7959 }
7960 }
7961 if (columnWidths[1] == 0)
7962 {
7963 /* no accelerators */
7964 if (submenuWidth != 0)
7965 columnWidths[0] += submenuWidth;
7966 else
7967 columnWidths[0] += spaceWidth;
7968 }
7969 else
7970 {
7971 /* there is an accelerator column */
7972 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7973 columnWidths[1] += submenuWidth;
7974 }
7975
7976 /*
7977 * Now find the total width of our 'menu'.
7978 */
7979 textWidth = columnWidths[0] + columnWidths[1];
7980 if (submenuWidth != 0)
7981 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007982 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7984 textWidth += submenuWidth;
7985 }
7986 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7987 if (textWidth > dlgwidth)
7988 dlgwidth = textWidth;
7989 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7990
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 /* start to fill in the dlgtemplate information. addressing by WORDs */
7992 if (s_usenewlook)
7993 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7994 else
7995 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7996
7997 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7998 *p++ = LOWORD(lStyle);
7999 *p++ = HIWORD(lStyle);
8000 *p++ = LOWORD(lExtendedStyle);
8001 *p++ = HIWORD(lExtendedStyle);
8002 pnumitems = p; /* save where the number of items must be stored */
8003 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008004 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008006 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 else
8008 *p++ = PixelToDialogX(initX); // x
8009 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008010 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 else
8012 *p++ = PixelToDialogY(initY); // y
8013 *p++ = PixelToDialogX(dlgwidth); // cx
8014 ptrueheight = p;
8015 *p++ = 0; // dialog height: changed later anyway
8016 *p++ = 0; // Menu
8017 *p++ = 0; // Class
8018
8019 /* copy the title of the dialog */
8020 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008021 ? (LPSTR)title
8022 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 p += nchar;
8024
8025 if (s_usenewlook)
8026 {
8027 /* do the font, since DS_3DLOOK doesn't work properly */
8028#ifdef USE_SYSMENU_FONT
8029 if (use_lfSysmenu)
8030 {
8031 /* point size */
8032 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8033 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008034 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 else
8037#endif
8038 {
8039 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008040 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
8042 p += nchar;
8043 }
8044
8045 /*
8046 * Loop over all the items in the menu.
8047 * But skip over the tearbar.
8048 */
8049 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8050 menu = menu->children->next;
8051 else
8052 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008053 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 for ( ; menu != NULL; menu = menu->next)
8055 {
8056 if (menu->modes == 0) /* this menu has just been deleted */
8057 continue;
8058 if (menu_is_separator(menu->dname))
8059 {
8060 sepPadding += 3;
8061 continue;
8062 }
8063
8064 /* Check if there still is plenty of room in the template. Make it
8065 * larger when needed. */
8066 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8067 {
8068 WORD *newp;
8069
8070 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8071 if (newp != NULL)
8072 {
8073 template_len += 4096;
8074 mch_memmove(newp, pdlgtemplate,
8075 (char *)p - (char *)pdlgtemplate);
8076 p = newp + (p - pdlgtemplate);
8077 pnumitems = newp + (pnumitems - pdlgtemplate);
8078 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8079 LocalFree(LocalHandle(pdlgtemplate));
8080 pdlgtemplate = newp;
8081 }
8082 }
8083
8084 /* Figure out minimal length of this menu label. Use "name" for the
8085 * actual text, "dname" for estimating the displayed size. "name"
8086 * has "&a" for mnemonic and includes the accelerator. */
8087 len = nameLen = (int)STRLEN(menu->name);
8088 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8089 (int)STRLEN(menu->dname))) / spaceWidth;
8090 len += padding0;
8091
8092 if (menu->actext != NULL)
8093 {
8094 acLen = (int)STRLEN(menu->actext);
8095 len += acLen;
8096 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8097 }
8098 else
8099 textWidth = 0;
8100 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8101 len += padding1;
8102
8103 if (menu->children == NULL)
8104 {
8105 padding2 = submenuWidth / spaceWidth;
8106 len += padding2;
8107 menuID = (WORD)(menu->id);
8108 }
8109 else
8110 {
8111 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008112 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 }
8114
8115 /* Allocate menu label and fill it in */
8116 text = label = alloc((unsigned)len + 1);
8117 if (label == NULL)
8118 break;
8119
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008120 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8122 if (text == NULL)
8123 text = label + nameLen; /* no actext, use whole name */
8124 while (padding0-- > 0)
8125 *text++ = ' ';
8126 if (menu->actext != NULL)
8127 {
8128 STRNCPY(text, menu->actext, acLen);
8129 text += acLen;
8130 }
8131 while (padding1-- > 0)
8132 *text++ = ' ';
8133 if (menu->children != NULL)
8134 {
8135 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8136 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8137 }
8138 else
8139 {
8140 while (padding2-- > 0)
8141 *text++ = ' ';
8142 }
8143 *text = NUL;
8144
8145 /*
8146 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8147 * W95/NT4 it makes the tear-off look more like a menu.
8148 */
8149 p = add_dialog_element(p,
8150 BS_PUSHBUTTON|BS_LEFT,
8151 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8152 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8153 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8154 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008155 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 vim_free(label);
8157 (*pnumitems)++;
8158 }
8159
8160 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8161
8162
8163 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008164 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 s_hinst,
8166 (LPDLGTEMPLATE)pdlgtemplate,
8167 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008168 (DLGPROC)tearoff_callback,
8169 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170
8171 LocalFree(LocalHandle(pdlgtemplate));
8172 SelectFont(hdc, oldFont);
8173 DeleteObject(font);
8174 ReleaseDC(hwnd, hdc);
8175
8176 /*
8177 * Reassert ourselves as the active window. This is so that after creating
8178 * a tearoff, the user doesn't have to click with the mouse just to start
8179 * typing again!
8180 */
8181 (void)SetActiveWindow(s_hwnd);
8182
8183 /* make sure the right buttons are enabled */
8184 force_menu_update = TRUE;
8185}
8186#endif
8187
8188#if defined(FEAT_TOOLBAR) || defined(PROTO)
8189#include "gui_w32_rc.h"
8190
8191/* This not defined in older SDKs */
8192# ifndef TBSTYLE_FLAT
8193# define TBSTYLE_FLAT 0x0800
8194# endif
8195
8196/*
8197 * Create the toolbar, initially unpopulated.
8198 * (just like the menu, there are no defaults, it's all
8199 * set up through menu.vim)
8200 */
8201 static void
8202initialise_toolbar(void)
8203{
8204 InitCommonControls();
8205 s_toolbarhwnd = CreateToolbarEx(
8206 s_hwnd,
8207 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8208 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008209 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 s_hinst,
8211 IDR_TOOLBAR1, // id of initial bitmap
8212 NULL,
8213 0, // initial number of buttons
8214 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8215 TOOLBAR_BUTTON_HEIGHT,
8216 TOOLBAR_BUTTON_WIDTH,
8217 TOOLBAR_BUTTON_HEIGHT,
8218 sizeof(TBBUTTON)
8219 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008220 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
8222 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8223}
8224
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008225 static LRESULT CALLBACK
8226toolbar_wndproc(
8227 HWND hwnd,
8228 UINT uMsg,
8229 WPARAM wParam,
8230 LPARAM lParam)
8231{
8232 HandleMouseHide(uMsg, lParam);
8233 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8234}
8235
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 static int
8237get_toolbar_bitmap(vimmenu_T *menu)
8238{
8239 int i = -1;
8240
8241 /*
8242 * Check user bitmaps first, unless builtin is specified.
8243 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008244 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
8246 char_u fname[MAXPATHL];
8247 HANDLE hbitmap = NULL;
8248
8249 if (menu->iconfile != NULL)
8250 {
8251 gui_find_iconfile(menu->iconfile, fname, "bmp");
8252 hbitmap = LoadImage(
8253 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008254 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 IMAGE_BITMAP,
8256 TOOLBAR_BUTTON_WIDTH,
8257 TOOLBAR_BUTTON_HEIGHT,
8258 LR_LOADFROMFILE |
8259 LR_LOADMAP3DCOLORS
8260 );
8261 }
8262
8263 /*
8264 * If the LoadImage call failed, or the "icon=" file
8265 * didn't exist or wasn't specified, try the menu name
8266 */
8267 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008268 && (gui_find_bitmap(
8269#ifdef FEAT_MULTI_LANG
8270 menu->en_dname != NULL ? menu->en_dname :
8271#endif
8272 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 hbitmap = LoadImage(
8274 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008275 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 IMAGE_BITMAP,
8277 TOOLBAR_BUTTON_WIDTH,
8278 TOOLBAR_BUTTON_HEIGHT,
8279 LR_LOADFROMFILE |
8280 LR_LOADMAP3DCOLORS
8281 );
8282
8283 if (hbitmap != NULL)
8284 {
8285 TBADDBITMAP tbAddBitmap;
8286
8287 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008288 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8291 (WPARAM)1, (LPARAM)&tbAddBitmap);
8292 /* i will be set to -1 if it fails */
8293 }
8294 }
8295 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8296 i = menu->iconidx;
8297
8298 return i;
8299}
8300#endif
8301
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008302#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8303 static void
8304initialise_tabline(void)
8305{
8306 InitCommonControls();
8307
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008308 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008309 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008310 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8311 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008312 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008313
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008314 gui.tabline_height = TABLINE_HEIGHT;
8315
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008316# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008317 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008318# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008319}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008320
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008321/*
8322 * Get tabpage_T from POINT.
8323 */
8324 static tabpage_T *
8325GetTabFromPoint(
8326 HWND hWnd,
8327 POINT pt)
8328{
8329 tabpage_T *ptp = NULL;
8330
8331 if (gui_mch_showing_tabline())
8332 {
8333 TCHITTESTINFO htinfo;
8334 htinfo.pt = pt;
8335 /* ignore if a window under cusor is not tabcontrol. */
8336 if (s_tabhwnd == hWnd)
8337 {
8338 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8339 if (idx != -1)
8340 ptp = find_tabpage(idx + 1);
8341 }
8342 }
8343 return ptp;
8344}
8345
8346static POINT s_pt = {0, 0};
8347static HCURSOR s_hCursor = NULL;
8348
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008349 static LRESULT CALLBACK
8350tabline_wndproc(
8351 HWND hwnd,
8352 UINT uMsg,
8353 WPARAM wParam,
8354 LPARAM lParam)
8355{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008356 POINT pt;
8357 tabpage_T *tp;
8358 RECT rect;
8359 int nCenter;
8360 int idx0;
8361 int idx1;
8362
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008363 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008364
8365 switch (uMsg)
8366 {
8367 case WM_LBUTTONDOWN:
8368 {
8369 s_pt.x = GET_X_LPARAM(lParam);
8370 s_pt.y = GET_Y_LPARAM(lParam);
8371 SetCapture(hwnd);
8372 s_hCursor = GetCursor(); /* backup default cursor */
8373 break;
8374 }
8375 case WM_MOUSEMOVE:
8376 if (GetCapture() == hwnd
8377 && ((wParam & MK_LBUTTON)) != 0)
8378 {
8379 pt.x = GET_X_LPARAM(lParam);
8380 pt.y = s_pt.y;
8381 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8382 {
8383 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8384
8385 tp = GetTabFromPoint(hwnd, pt);
8386 if (tp != NULL)
8387 {
8388 idx0 = tabpage_index(curtab) - 1;
8389 idx1 = tabpage_index(tp) - 1;
8390
8391 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8392 nCenter = rect.left + (rect.right - rect.left) / 2;
8393
8394 /* Check if the mouse cursor goes over the center of
8395 * the next tab to prevent "flickering". */
8396 if ((idx0 < idx1) && (nCenter < pt.x))
8397 {
8398 tabpage_move(idx1 + 1);
8399 update_screen(0);
8400 }
8401 else if ((idx1 < idx0) && (pt.x < nCenter))
8402 {
8403 tabpage_move(idx1);
8404 update_screen(0);
8405 }
8406 }
8407 }
8408 }
8409 break;
8410 case WM_LBUTTONUP:
8411 {
8412 if (GetCapture() == hwnd)
8413 {
8414 SetCursor(s_hCursor);
8415 ReleaseCapture();
8416 }
8417 break;
8418 }
8419 default:
8420 break;
8421 }
8422
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008423 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8424}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008425#endif
8426
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8428/*
8429 * Make the GUI window come to the foreground.
8430 */
8431 void
8432gui_mch_set_foreground(void)
8433{
8434 if (IsIconic(s_hwnd))
8435 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8436 SetForegroundWindow(s_hwnd);
8437}
8438#endif
8439
8440#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8441 static void
8442dyn_imm_load(void)
8443{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008444 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 if (hLibImm == NULL)
8446 return;
8447
8448 pImmGetCompositionStringA
8449 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8450 pImmGetCompositionStringW
8451 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8452 pImmGetContext
8453 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8454 pImmAssociateContext
8455 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8456 pImmReleaseContext
8457 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8458 pImmGetOpenStatus
8459 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8460 pImmSetOpenStatus
8461 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8462 pImmGetCompositionFont
8463 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8464 pImmSetCompositionFont
8465 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8466 pImmSetCompositionWindow
8467 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8468 pImmGetConversionStatus
8469 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008470 pImmSetConversionStatus
8471 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
8473 if ( pImmGetCompositionStringA == NULL
8474 || pImmGetCompositionStringW == NULL
8475 || pImmGetContext == NULL
8476 || pImmAssociateContext == NULL
8477 || pImmReleaseContext == NULL
8478 || pImmGetOpenStatus == NULL
8479 || pImmSetOpenStatus == NULL
8480 || pImmGetCompositionFont == NULL
8481 || pImmSetCompositionFont == NULL
8482 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008483 || pImmGetConversionStatus == NULL
8484 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
8486 FreeLibrary(hLibImm);
8487 hLibImm = NULL;
8488 pImmGetContext = NULL;
8489 return;
8490 }
8491
8492 return;
8493}
8494
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495#endif
8496
8497#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8498
8499# ifdef FEAT_XPM_W32
8500# define IMAGE_XPM 100
8501# endif
8502
8503typedef struct _signicon_t
8504{
8505 HANDLE hImage;
8506 UINT uType;
8507#ifdef FEAT_XPM_W32
8508 HANDLE hShape; /* Mask bitmap handle */
8509#endif
8510} signicon_t;
8511
8512 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008513gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 signicon_t *sign;
8516 int x, y, w, h;
8517
8518 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8519 return;
8520
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008521#if defined(FEAT_DIRECTX)
8522 if (IS_ENABLE_DIRECTX())
8523 DWriteContext_Flush(s_dwc);
8524#endif
8525
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 x = TEXT_X(col);
8527 y = TEXT_Y(row);
8528 w = gui.char_width * 2;
8529 h = gui.char_height;
8530 switch (sign->uType)
8531 {
8532 case IMAGE_BITMAP:
8533 {
8534 HDC hdcMem;
8535 HBITMAP hbmpOld;
8536
8537 hdcMem = CreateCompatibleDC(s_hdc);
8538 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8539 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8540 SelectObject(hdcMem, hbmpOld);
8541 DeleteDC(hdcMem);
8542 }
8543 break;
8544 case IMAGE_ICON:
8545 case IMAGE_CURSOR:
8546 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8547 break;
8548#ifdef FEAT_XPM_W32
8549 case IMAGE_XPM:
8550 {
8551 HDC hdcMem;
8552 HBITMAP hbmpOld;
8553
8554 hdcMem = CreateCompatibleDC(s_hdc);
8555 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8556 /* Make hole */
8557 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8558
8559 SelectObject(hdcMem, sign->hImage);
8560 /* Paint sign */
8561 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8562 SelectObject(hdcMem, hbmpOld);
8563 DeleteDC(hdcMem);
8564 }
8565 break;
8566#endif
8567 }
8568}
8569
8570 static void
8571close_signicon_image(signicon_t *sign)
8572{
8573 if (sign)
8574 switch (sign->uType)
8575 {
8576 case IMAGE_BITMAP:
8577 DeleteObject((HGDIOBJ)sign->hImage);
8578 break;
8579 case IMAGE_CURSOR:
8580 DestroyCursor((HCURSOR)sign->hImage);
8581 break;
8582 case IMAGE_ICON:
8583 DestroyIcon((HICON)sign->hImage);
8584 break;
8585#ifdef FEAT_XPM_W32
8586 case IMAGE_XPM:
8587 DeleteObject((HBITMAP)sign->hImage);
8588 DeleteObject((HBITMAP)sign->hShape);
8589 break;
8590#endif
8591 }
8592}
8593
8594 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008595gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 signicon_t sign, *psign;
8598 char_u *ext;
8599
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008601 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (ext > signfile)
8603 {
8604 int do_load = 1;
8605
8606 if (!STRICMP(ext, ".bmp"))
8607 sign.uType = IMAGE_BITMAP;
8608 else if (!STRICMP(ext, ".ico"))
8609 sign.uType = IMAGE_ICON;
8610 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8611 sign.uType = IMAGE_CURSOR;
8612 else
8613 do_load = 0;
8614
8615 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008616 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 gui.char_width * 2, gui.char_height,
8618 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8619#ifdef FEAT_XPM_W32
8620 if (!STRICMP(ext, ".xpm"))
8621 {
8622 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008623 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8624 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626#endif
8627 }
8628
8629 psign = NULL;
8630 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8631 != NULL)
8632 *psign = sign;
8633
8634 if (!psign)
8635 {
8636 if (sign.hImage)
8637 close_signicon_image(&sign);
8638 EMSG(_(e_signdata));
8639 }
8640 return (void *)psign;
8641
8642}
8643
8644 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008645gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 if (sign)
8648 {
8649 close_signicon_image((signicon_t *)sign);
8650 vim_free(sign);
8651 }
8652}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008655#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
8657/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008658 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008660 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8662 * to get current mouse position).
8663 *
8664 * Trying to use as more Windows services as possible, and as less
8665 * IE version as possible :)).
8666 *
8667 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8668 * BalloonEval struct.
8669 * 2) Enable/Disable simply create/kill BalloonEval Timer
8670 * 3) When there was enough inactivity, timer procedure posts
8671 * async request to debugger
8672 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8673 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008674 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 */
8676
Bram Moolenaar45360022005-07-21 21:08:21 +00008677/*
8678 * determine whether installed Common Controls support multiline tooltips
8679 * (i.e. their version is >= 4.70
8680 */
8681 int
8682multiline_balloon_available(void)
8683{
8684 HINSTANCE hDll;
8685 static char comctl_dll[] = "comctl32.dll";
8686 static int multiline_tip = MAYBE;
8687
8688 if (multiline_tip != MAYBE)
8689 return multiline_tip;
8690
8691 hDll = GetModuleHandle(comctl_dll);
8692 if (hDll != NULL)
8693 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008694 DLLGETVERSIONPROC pGetVer;
8695 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008696
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008697 if (pGetVer != NULL)
8698 {
8699 DLLVERSIONINFO dvi;
8700 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008701
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008702 ZeroMemory(&dvi, sizeof(dvi));
8703 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008704
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008705 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008706
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008707 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008708 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008709 || (dvi.dwMajorVersion == 4
8710 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008711 {
8712 multiline_tip = TRUE;
8713 return multiline_tip;
8714 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008715 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008716 else
8717 {
8718 /* there is chance we have ancient CommCtl 4.70
8719 which doesn't export DllGetVersion */
8720 DWORD dwHandle = 0;
8721 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8722 if (len > 0)
8723 {
8724 VS_FIXEDFILEINFO *ver;
8725 UINT vlen = 0;
8726 void *data = alloc(len);
8727
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008728 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008729 && GetFileVersionInfo(comctl_dll, 0, len, data)
8730 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8731 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008732 && HIWORD(ver->dwFileVersionMS) > 4)
8733 || ((HIWORD(ver->dwFileVersionMS) == 4
8734 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008735 {
8736 vim_free(data);
8737 multiline_tip = TRUE;
8738 return multiline_tip;
8739 }
8740 vim_free(data);
8741 }
8742 }
8743 }
8744 multiline_tip = FALSE;
8745 return multiline_tip;
8746}
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008749make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750{
Bram Moolenaar45360022005-07-21 21:08:21 +00008751 TOOLINFO *pti;
8752 int ToolInfoSize;
8753
8754 if (multiline_balloon_available() == TRUE)
8755 ToolInfoSize = sizeof(TOOLINFO_NEW);
8756 else
8757 ToolInfoSize = sizeof(TOOLINFO);
8758
8759 pti = (TOOLINFO *)alloc(ToolInfoSize);
8760 if (pti == NULL)
8761 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762
8763 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8764 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8765 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8766 beval->target, NULL, s_hinst, NULL);
8767
8768 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8769 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8770
Bram Moolenaar45360022005-07-21 21:08:21 +00008771 pti->cbSize = ToolInfoSize;
8772 pti->uFlags = TTF_SUBCLASS;
8773 pti->hwnd = beval->target;
8774 pti->hinst = 0; /* Don't use string resources */
8775 pti->uId = ID_BEVAL_TOOLTIP;
8776
8777 if (multiline_balloon_available() == TRUE)
8778 {
8779 RECT rect;
8780 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8781 pti->lpszText = LPSTR_TEXTCALLBACK;
8782 ptin->lParam = (LPARAM)text;
8783 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8784 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8785 (LPARAM)rect.right);
8786 }
8787 else
8788 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789
8790 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008791 pti->rect.left = pt.x - 3;
8792 pti->rect.top = pt.y - 3;
8793 pti->rect.right = pt.x + 3;
8794 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
Bram Moolenaar45360022005-07-21 21:08:21 +00008796 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 /* Make tooltip appear sooner */
8798 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008799 /* I've performed some tests and it seems the longest possible life time
8800 * of tooltip is 30 seconds */
8801 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 /*
8803 * HACK: force tooltip to appear, because it'll not appear until
8804 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008805 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008807 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008809 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810}
8811
8812 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008813delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008815 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816}
8817
8818 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008819BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008820 HWND hwnd UNUSED,
8821 UINT uMsg UNUSED,
8822 UINT_PTR idEvent UNUSED,
8823 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 POINT pt;
8826 RECT rect;
8827
8828 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8829 return;
8830
8831 GetCursorPos(&pt);
8832 if (WindowFromPoint(pt) != s_textArea)
8833 return;
8834
8835 ScreenToClient(s_textArea, &pt);
8836 GetClientRect(s_textArea, &rect);
8837 if (!PtInRect(&rect, pt))
8838 return;
8839
8840 if (LastActivity > 0
8841 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8842 && (cur_beval->showState != ShS_PENDING
8843 || abs(cur_beval->x - pt.x) > 3
8844 || abs(cur_beval->y - pt.y) > 3))
8845 {
8846 /* Pointer resting in one place long enough, it's time to show
8847 * the tooltip. */
8848 cur_beval->showState = ShS_PENDING;
8849 cur_beval->x = pt.x;
8850 cur_beval->y = pt.y;
8851
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008852 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853
8854 if (cur_beval->msgCB != NULL)
8855 (*cur_beval->msgCB)(cur_beval, 0);
8856 }
8857}
8858
8859 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008860gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008862 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008864 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865}
8866
8867 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008868gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008870 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 if (beval == NULL)
8872 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008873 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008874 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008875 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876}
8877
8878 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008879gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008882
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008883 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 if (beval->showState == ShS_SHOWING)
8885 return;
8886 GetCursorPos(&pt);
8887 ScreenToClient(s_textArea, &pt);
8888
8889 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008891 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 gui_mch_disable_beval_area(cur_beval);
8893 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008894 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008896 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897}
8898
8899 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008900gui_mch_create_beval_area(
8901 void *target, /* ignored, always use s_textArea */
8902 char_u *mesg,
8903 void (*mesgCB)(BalloonEval *, int),
8904 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905{
8906 /* partially stolen from gui_beval.c */
8907 BalloonEval *beval;
8908
8909 if (mesg != NULL && mesgCB != NULL)
8910 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008911 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 return NULL;
8913 }
8914
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008915 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 if (beval != NULL)
8917 {
8918 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
8920 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 beval->msg = mesg;
8922 beval->msgCB = mesgCB;
8923 beval->clientData = clientData;
8924
8925 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 cur_beval = beval;
8927
8928 if (p_beval)
8929 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931 return beval;
8932}
8933
8934 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008935Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8938 return;
8939
8940 if (cur_beval != NULL)
8941 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008942 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008944 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008945 // TRACE0("TTN_SHOW {{{");
8946 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008947 break;
8948 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008949 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 delete_tooltip(cur_beval);
8951 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008952 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953
8954 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008955 break;
8956 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008957 {
8958 /* if you get there then we have new common controls */
8959 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8960 info->lpszText = (LPSTR)info->lParam;
8961 info->uFlags |= TTF_DI_SETITEM;
8962 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 }
8965 }
8966}
8967
8968 static void
8969TrackUserActivity(UINT uMsg)
8970{
8971 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8972 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8973 LastActivity = GetTickCount();
8974}
8975
8976 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008977gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008979#ifdef FEAT_VARTABS
8980 if (beval->vts)
8981 vim_free(beval->vts);
8982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 vim_free(beval);
8984}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008985#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
8987#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8988/*
8989 * We have multiple signs to draw at the same location. Draw the
8990 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8991 */
8992 void
8993netbeans_draw_multisign_indicator(int row)
8994{
8995 int i;
8996 int y;
8997 int x;
8998
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008999 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009000 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009001
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 x = 0;
9003 y = TEXT_Y(row);
9004
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009005#if defined(FEAT_DIRECTX)
9006 if (IS_ENABLE_DIRECTX())
9007 DWriteContext_Flush(s_dwc);
9008#endif
9009
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 for (i = 0; i < gui.char_height - 3; i++)
9011 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9012
9013 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9014 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9015 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9016 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9017 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9018 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9019 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9020}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009021#endif