blob: 8574b63facc1725747884bbfd8fcc33091179b48 [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;
3630 char_u *p;
3631
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);
3716 if (p != NULL)
3717 /* when out of memory we get garbage for non-ASCII chars */
3718 STRCPY(fileBuf, p);
3719 vim_free(p);
3720
3721 /* Give focus back to main window (when using MDI). */
3722 SetFocus(s_hwnd);
3723
3724 /* Shorten the file name if possible */
3725 return vim_strsave(shorten_fname1((char_u *)fileBuf));
3726}
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 Moolenaard25c16e2016-01-29 22:13:30 +01004339static void make_tooltip(BalloonEval *beval, char *text, POINT pt);
4340static void delete_tooltip(BalloonEval *beval);
4341static VOID CALLBACK BevalTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004342
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004344static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00004346
Bram Moolenaar82881492012-11-20 16:53:39 +01004347
4348/* cproto fails on missing include files */
4349#ifndef PROTO
4350
Bram Moolenaar45360022005-07-21 21:08:21 +00004351/*
4352 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004353 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00004354 */
Bram Moolenaar82881492012-11-20 16:53:39 +01004355# include <pshpack1.h>
4356
4357#endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004358
4359typedef struct _DllVersionInfo
4360{
4361 DWORD cbSize;
4362 DWORD dwMajorVersion;
4363 DWORD dwMinorVersion;
4364 DWORD dwBuildNumber;
4365 DWORD dwPlatformID;
4366} DLLVERSIONINFO;
4367
Bram Moolenaar82881492012-11-20 16:53:39 +01004368#ifndef PROTO
4369# include <poppack.h>
4370#endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00004371
Bram Moolenaar45360022005-07-21 21:08:21 +00004372typedef struct tagTOOLINFOA_NEW
4373{
4374 UINT cbSize;
4375 UINT uFlags;
4376 HWND hwnd;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004377 UINT_PTR uId;
Bram Moolenaar45360022005-07-21 21:08:21 +00004378 RECT rect;
4379 HINSTANCE hinst;
4380 LPSTR lpszText;
4381 LPARAM lParam;
4382} TOOLINFO_NEW;
4383
4384typedef struct tagNMTTDISPINFO_NEW
4385{
4386 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00004387 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00004388 char szText[80];
4389 HINSTANCE hinst;
4390 UINT uFlags;
4391 LPARAM lParam;
4392} NMTTDISPINFO_NEW;
4393
Bram Moolenaar45360022005-07-21 21:08:21 +00004394typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
4395#ifndef TTM_SETMAXTIPWIDTH
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004396# define TTM_SETMAXTIPWIDTH (WM_USER+24)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397#endif
4398
Bram Moolenaar45360022005-07-21 21:08:21 +00004399#ifndef TTF_DI_SETITEM
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004400# define TTF_DI_SETITEM 0x8000
Bram Moolenaar45360022005-07-21 21:08:21 +00004401#endif
4402
4403#ifndef TTN_GETDISPINFO
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004404# define TTN_GETDISPINFO (TTN_FIRST - 0)
Bram Moolenaar45360022005-07-21 21:08:21 +00004405#endif
4406
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004407#endif /* defined(FEAT_BEVAL_GUI) */
Bram Moolenaar45360022005-07-21 21:08:21 +00004408
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004409#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4410/* Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4411 * it here if LPNMTTDISPINFO isn't defined.
4412 * MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4413 * _MSC_VER. */
4414# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4415typedef struct tagNMTTDISPINFOA {
4416 NMHDR hdr;
4417 LPSTR lpszText;
4418 char szText[80];
4419 HINSTANCE hinst;
4420 UINT uFlags;
4421 LPARAM lParam;
4422} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4423# define LPNMTTDISPINFO LPNMTTDISPINFOA
4424
4425# ifdef FEAT_MBYTE
4426typedef struct tagNMTTDISPINFOW {
4427 NMHDR hdr;
4428 LPWSTR lpszText;
4429 WCHAR szText[80];
4430 HINSTANCE hinst;
4431 UINT uFlags;
4432 LPARAM lParam;
4433} NMTTDISPINFOW, *LPNMTTDISPINFOW;
4434# endif
4435# endif
4436#endif
4437
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004438#ifndef TTN_GETDISPINFOW
4439# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4440#endif
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442/* Local variables: */
4443
4444#ifdef FEAT_MENU
4445static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448/*
4449 * Use the system font for dialogs and tear-off menus. Remove this line to
4450 * use DLG_FONT_NAME.
4451 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004452#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454#define VIM_NAME "vim"
4455#define VIM_CLASS "Vim"
4456#define VIM_CLASSW L"Vim"
4457
4458/* Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4459 * thus there should be room for every dialog. For tearoffs it's made bigger
4460 * when needed. */
4461#define DLG_ALLOC_SIZE 16 * 1024
4462
4463/*
4464 * stuff for dialogs, menus, tearoffs etc.
4465 */
4466static LRESULT APIENTRY dialog_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004467#ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468static LRESULT APIENTRY tearoff_callback(HWND, UINT, WPARAM, LPARAM);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470static PWORD
4471add_dialog_element(
4472 PWORD p,
4473 DWORD lStyle,
4474 WORD x,
4475 WORD y,
4476 WORD w,
4477 WORD h,
4478 WORD Id,
4479 WORD clss,
4480 const char *caption);
4481static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004482static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004483#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486static void get_dialog_font_metrics(void);
4487
4488static int dialog_default_button = -1;
4489
4490/* Intellimouse support */
4491static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493static int s_usenewlook; /* emulate W95/NT4 non-bold dialogs */
4494#ifdef FEAT_TOOLBAR
4495static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004496static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497static int get_toolbar_bitmap(vimmenu_T *menu);
4498#endif
4499
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004500#ifdef FEAT_GUI_TABLINE
4501static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004502static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004503#endif
4504
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#ifdef FEAT_MBYTE_IME
4506static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4507static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4508#endif
4509#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4510# ifdef NOIME
4511typedef struct tagCOMPOSITIONFORM {
4512 DWORD dwStyle;
4513 POINT ptCurrentPos;
4514 RECT rcArea;
4515} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4516typedef HANDLE HIMC;
4517# endif
4518
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004519static HINSTANCE hLibImm = NULL;
4520static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4521static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4522static HIMC (WINAPI *pImmGetContext)(HWND);
4523static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4524static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4525static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4526static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
4527static BOOL (WINAPI *pImmGetCompositionFont)(HIMC, LPLOGFONTA);
4528static BOOL (WINAPI *pImmSetCompositionFont)(HIMC, LPLOGFONTA);
4529static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4530static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004531static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532static void dyn_imm_load(void);
4533#else
4534# define pImmGetCompositionStringA ImmGetCompositionStringA
4535# define pImmGetCompositionStringW ImmGetCompositionStringW
4536# define pImmGetContext ImmGetContext
4537# define pImmAssociateContext ImmAssociateContext
4538# define pImmReleaseContext ImmReleaseContext
4539# define pImmGetOpenStatus ImmGetOpenStatus
4540# define pImmSetOpenStatus ImmSetOpenStatus
4541# define pImmGetCompositionFont ImmGetCompositionFontA
4542# define pImmSetCompositionFont ImmSetCompositionFontA
4543# define pImmSetCompositionWindow ImmSetCompositionWindow
4544# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004545# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546#endif
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548#ifdef FEAT_MENU
4549/*
4550 * Figure out how high the menu bar is at the moment.
4551 */
4552 static int
4553gui_mswin_get_menu_height(
4554 int fix_window) /* If TRUE, resize window if menu height changed */
4555{
4556 static int old_menu_height = -1;
4557
4558 RECT rc1, rc2;
4559 int num;
4560 int menu_height;
4561
4562 if (gui.menu_is_active)
4563 num = GetMenuItemCount(s_menuBar);
4564 else
4565 num = 0;
4566
4567 if (num == 0)
4568 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004569 else if (IsMinimized(s_hwnd))
4570 {
4571 /* The height of the menu cannot be determined while the window is
4572 * minimized. Take the previous height if the menu is changed in that
4573 * state, to avoid that Vim's vertical window size accidentally
4574 * increases due to the unaccounted-for menu height. */
4575 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004579 /*
4580 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4581 * seem to have been set yet, so menu wraps in default window
4582 * width which is very narrow. Instead just return height of a
4583 * single menu item. Will still be wrong when the menu really
4584 * should wrap over more than one line.
4585 */
4586 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4587 if (gui.starting)
4588 menu_height = rc1.bottom - rc1.top + 1;
4589 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004591 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4592 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 }
4595
4596 if (fix_window && menu_height != old_menu_height)
4597 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004598 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar71371b12015-03-24 17:57:12 +01004600 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
4602 return menu_height;
4603}
4604#endif /*FEAT_MENU*/
4605
4606
4607/*
4608 * Setup for the Intellimouse
4609 */
4610 static void
4611init_mouse_wheel(void)
4612{
4613
4614#ifndef SPI_GETWHEELSCROLLLINES
4615# define SPI_GETWHEELSCROLLLINES 104
4616#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004617#ifndef SPI_SETWHEELSCROLLLINES
4618# define SPI_SETWHEELSCROLLLINES 105
4619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621#define VMOUSEZ_CLASSNAME "MouseZ" /* hidden wheel window class */
4622#define VMOUSEZ_TITLE "Magellan MSWHEEL" /* hidden wheel window title */
4623#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4624#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4625
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 mouse_scroll_lines = 3; /* reasonable default */
4627
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004628 /* if NT 4.0+ (or Win98) get scroll lines directly from system */
4629 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4630 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631}
4632
4633
4634/* Intellimouse wheel handler */
4635 static void
4636_OnMouseWheel(
4637 HWND hwnd,
4638 short zDelta)
4639{
4640/* Treat a mouse wheel event as if it were a scroll request */
4641 int i;
4642 int size;
4643 HWND hwndCtl;
4644
4645 if (curwin->w_scrollbars[SBAR_RIGHT].id != 0)
4646 {
4647 hwndCtl = curwin->w_scrollbars[SBAR_RIGHT].id;
4648 size = curwin->w_scrollbars[SBAR_RIGHT].size;
4649 }
4650 else if (curwin->w_scrollbars[SBAR_LEFT].id != 0)
4651 {
4652 hwndCtl = curwin->w_scrollbars[SBAR_LEFT].id;
4653 size = curwin->w_scrollbars[SBAR_LEFT].size;
4654 }
4655 else
4656 return;
4657
4658 size = curwin->w_height;
4659 if (mouse_scroll_lines == 0)
4660 init_mouse_wheel();
4661
Bram Moolenaara338adc2018-01-31 20:51:47 +01004662 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (mouse_scroll_lines > 0
4664 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4665 {
4666 for (i = mouse_scroll_lines; i > 0; --i)
4667 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4668 }
4669 else
4670 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004671 mch_enable_flush();
4672 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673}
4674
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004675#ifdef USE_SYSMENU_FONT
4676/*
4677 * Get Menu Font.
4678 * Return OK or FAIL.
4679 */
4680 static int
4681gui_w32_get_menu_font(LOGFONT *lf)
4682{
4683 NONCLIENTMETRICS nm;
4684
4685 nm.cbSize = sizeof(NONCLIENTMETRICS);
4686 if (!SystemParametersInfo(
4687 SPI_GETNONCLIENTMETRICS,
4688 sizeof(NONCLIENTMETRICS),
4689 &nm,
4690 0))
4691 return FAIL;
4692 *lf = nm.lfMenuFont;
4693 return OK;
4694}
4695#endif
4696
4697
4698#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4699/*
4700 * Set the GUI tabline font to the system menu font
4701 */
4702 static void
4703set_tabline_font(void)
4704{
4705 LOGFONT lfSysmenu;
4706 HFONT font;
4707 HWND hwnd;
4708 HDC hdc;
4709 HFONT hfntOld;
4710 TEXTMETRIC tm;
4711
4712 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4713 return;
4714
4715 font = CreateFontIndirect(&lfSysmenu);
4716
4717 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4718
4719 /*
4720 * Compute the height of the font used for the tab text
4721 */
4722 hwnd = GetDesktopWindow();
4723 hdc = GetWindowDC(hwnd);
4724 hfntOld = SelectFont(hdc, font);
4725
4726 GetTextMetrics(hdc, &tm);
4727
4728 SelectFont(hdc, hfntOld);
4729 ReleaseDC(hwnd, hdc);
4730
4731 /*
4732 * The space used by the tab border and the space between the tab label
4733 * and the tab border is included as 7.
4734 */
4735 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4736}
4737#endif
4738
Bram Moolenaar520470a2005-06-16 21:59:56 +00004739/*
4740 * Invoked when a setting was changed.
4741 */
4742 static LRESULT CALLBACK
4743_OnSettingChange(UINT n)
4744{
4745 if (n == SPI_SETWHEELSCROLLLINES)
4746 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4747 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004748#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4749 if (n == SPI_SETNONCLIENTMETRICS)
4750 set_tabline_font();
4751#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004752 return 0;
4753}
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#ifdef FEAT_NETBEANS_INTG
4756 static void
4757_OnWindowPosChanged(
4758 HWND hwnd,
4759 const LPWINDOWPOS lpwpos)
4760{
4761 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004762 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4765 || lpwpos->cx != cx || lpwpos->cy != cy))
4766 {
4767 x = lpwpos->x;
4768 y = lpwpos->y;
4769 cx = lpwpos->cx;
4770 cy = lpwpos->cy;
4771 netbeans_frame_moved(x, y);
4772 }
4773 /* Allow to send WM_SIZE and WM_MOVE */
4774 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4775}
4776#endif
4777
4778 static int
4779_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 UINT fwSide,
4781 LPRECT lprc)
4782{
4783 int w, h;
4784 int valid_w, valid_h;
4785 int w_offset, h_offset;
4786
4787 w = lprc->right - lprc->left;
4788 h = lprc->bottom - lprc->top;
4789 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4790 w_offset = w - valid_w;
4791 h_offset = h - valid_h;
4792
4793 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4794 || fwSide == WMSZ_BOTTOMLEFT)
4795 lprc->left += w_offset;
4796 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4797 || fwSide == WMSZ_BOTTOMRIGHT)
4798 lprc->right -= w_offset;
4799
4800 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4801 || fwSide == WMSZ_TOPRIGHT)
4802 lprc->top += h_offset;
4803 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4804 || fwSide == WMSZ_BOTTOMRIGHT)
4805 lprc->bottom -= h_offset;
4806 return TRUE;
4807}
4808
4809
4810
4811 static LRESULT CALLBACK
4812_WndProc(
4813 HWND hwnd,
4814 UINT uMsg,
4815 WPARAM wParam,
4816 LPARAM lParam)
4817{
4818 /*
4819 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4820 hwnd, uMsg, wParam, lParam);
4821 */
4822
4823 HandleMouseHide(uMsg, lParam);
4824
4825 s_uMsg = uMsg;
4826 s_wParam = wParam;
4827 s_lParam = lParam;
4828
4829 switch (uMsg)
4830 {
4831 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4832 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
4833 /* HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate); */
4834 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
4835 /* HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand); */
4836 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4837 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4838 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4839 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4840#ifdef FEAT_MENU
4841 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4842#endif
4843 /* HANDLE_MSG(hwnd, WM_MOVE, _OnMove); */
4844 /* HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate); */
4845 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4846 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
4847 /* HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand); */
4848 /* HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey); */
4849 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4850 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4851 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4852#ifdef FEAT_NETBEANS_INTG
4853 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4854#endif
4855
Bram Moolenaarafa24992006-03-27 20:58:26 +00004856#ifdef FEAT_GUI_TABLINE
4857 case WM_RBUTTONUP:
4858 {
4859 if (gui_mch_showing_tabline())
4860 {
4861 POINT pt;
4862 RECT rect;
4863
4864 /*
4865 * If the cursor is on the tabline, display the tab menu
4866 */
4867 GetCursorPos((LPPOINT)&pt);
4868 GetWindowRect(s_textArea, &rect);
4869 if (pt.y < rect.top)
4870 {
4871 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004872 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004873 }
4874 }
4875 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4876 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004877 case WM_LBUTTONDBLCLK:
4878 {
4879 /*
4880 * If the user double clicked the tabline, create a new tab
4881 */
4882 if (gui_mch_showing_tabline())
4883 {
4884 POINT pt;
4885 RECT rect;
4886
4887 GetCursorPos((LPPOINT)&pt);
4888 GetWindowRect(s_textArea, &rect);
4889 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004890 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004891 }
4892 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4893 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004894#endif
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 case WM_QUERYENDSESSION: /* System wants to go down. */
4897 gui_shell_closed(); /* Will exit when no changed buffers. */
4898 return FALSE; /* Do NOT allow system to go down. */
4899
4900 case WM_ENDSESSION:
4901 if (wParam) /* system only really goes down when wParam is TRUE */
Bram Moolenaar213ae482011-12-15 21:51:36 +01004902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004904 return 0L;
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 break;
4907
4908 case WM_CHAR:
4909 /* Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4910 * byte while we want the UTF-16 character value. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004911 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 return 0L;
4913
4914 case WM_SYSCHAR:
4915 /*
4916 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4917 * shortcut key, handle like a typed ALT key, otherwise call Windows
4918 * ALT key handling.
4919 */
4920#ifdef FEAT_MENU
4921 if ( !gui.menu_is_active
4922 || p_wak[0] == 'n'
4923 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4924 )
4925#endif
4926 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004927 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 return 0L;
4929 }
4930#ifdef FEAT_MENU
4931 else
4932 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4933#endif
4934
4935 case WM_SYSKEYUP:
4936#ifdef FEAT_MENU
4937 /* This used to be done only when menu is active: ALT key is used for
4938 * that. But that caused problems when menu is disabled and using
4939 * Alt-Tab-Esc: get into a strange state where no mouse-moved events
4940 * are received, mouse pointer remains hidden. */
4941 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4942#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004943 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945
4946 case WM_SIZING: /* HANDLE_MSG doesn't seem to handle this one */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004947 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 case WM_MOUSEWHEEL:
4950 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004951 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
Bram Moolenaar520470a2005-06-16 21:59:56 +00004953 /* Notification for change in SystemParametersInfo() */
4954 case WM_SETTINGCHANGE:
4955 return _OnSettingChange((UINT)wParam);
4956
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004957#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 case WM_NOTIFY:
4959 switch (((LPNMHDR) lParam)->code)
4960 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004961# ifdef FEAT_MBYTE
4962 case TTN_GETDISPINFOW:
4963# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004964 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004966 LPNMHDR hdr = (LPNMHDR)lParam;
4967 char_u *str = NULL;
4968 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004969
Bram Moolenaard23a8232018-02-10 18:45:26 +01004970 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004971
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004972# ifdef FEAT_GUI_TABLINE
4973 if (gui_mch_showing_tabline()
4974 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004975 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004976 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004977 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004978 * Mouse is over the GUI tabline. Display the
4979 * tooltip for the tab under the cursor
4980 *
4981 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004982 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004983 GetCursorPos(&pt);
4984 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004985 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004986 TCHITTESTINFO htinfo;
4987 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004988
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004989 /*
4990 * Get the tab under the cursor
4991 */
4992 htinfo.pt.x = pt.x;
4993 htinfo.pt.y = pt.y;
4994 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4995 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004996 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004997 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004998
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004999 tp = find_tabpage(idx + 1);
5000 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005001 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005002 get_tabline_label(tp, TRUE);
5003 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005004 }
5005 }
5006 }
5007 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005008# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005009# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005010# ifdef FEAT_GUI_TABLINE
5011 else
5012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005014 UINT idButton;
5015 vimmenu_T *pMenu;
5016
5017 idButton = (UINT) hdr->idFrom;
5018 pMenu = gui_mswin_find_menu(root_menu, idButton);
5019 if (pMenu)
5020 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005022# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005023 if (str != NULL)
5024 {
5025# ifdef FEAT_MBYTE
5026 if (hdr->code == TTN_GETDISPINFOW)
5027 {
5028 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
5029
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005030 /* Set the maximum width, this also enables using
5031 * \n for line break. */
5032 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5033 0, 500);
5034
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005035 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005036 lpdi->lpszText = tt_text;
5037 /* can't show tooltip if failed */
5038 }
5039 else
5040# endif
5041 {
5042 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
5043
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00005044 /* Set the maximum width, this also enables using
5045 * \n for line break. */
5046 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
5047 0, 500);
5048
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005049 if (STRLEN(str) < sizeof(lpdi->szText)
5050 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005051 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00005052 sizeof(lpdi->szText) - 1);
5053 else
5054 lpdi->lpszText = tt_text;
5055 }
5056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005059# ifdef FEAT_GUI_TABLINE
5060 case TCN_SELCHANGE:
5061 if (gui_mch_showing_tabline()
5062 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005063 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005064 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005065 return 0L;
5066 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005067 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00005068
5069 case NM_RCLICK:
5070 if (gui_mch_showing_tabline()
5071 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01005072 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00005073 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005074 return 0L;
5075 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00005076 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005079# ifdef FEAT_GUI_TABLINE
5080 if (gui_mch_showing_tabline()
5081 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
5082 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085 }
5086 break;
5087#endif
5088#if defined(MENUHINTS) && defined(FEAT_MENU)
5089 case WM_MENUSELECT:
5090 if (((UINT) HIWORD(wParam)
5091 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5092 == MF_HILITE
5093 && (State & CMDLINE) == 0)
5094 {
5095 UINT idButton;
5096 vimmenu_T *pMenu;
5097 static int did_menu_tip = FALSE;
5098
5099 if (did_menu_tip)
5100 {
5101 msg_clr_cmdline();
5102 setcursor();
5103 out_flush();
5104 did_menu_tip = FALSE;
5105 }
5106
5107 idButton = (UINT)LOWORD(wParam);
5108 pMenu = gui_mswin_find_menu(root_menu, idButton);
5109 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5110 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5111 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005112 ++msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 msg(pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00005114 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 setcursor();
5116 out_flush();
5117 did_menu_tip = TRUE;
5118 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01005119 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 break;
5122#endif
5123 case WM_NCHITTEST:
5124 {
5125 LRESULT result;
5126 int x, y;
5127 int xPos = GET_X_LPARAM(lParam);
5128
5129 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
5130 if (result == HTCLIENT)
5131 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005132#ifdef FEAT_GUI_TABLINE
5133 if (gui_mch_showing_tabline())
5134 {
5135 int yPos = GET_Y_LPARAM(lParam);
5136 RECT rct;
5137
5138 /* If the cursor is on the GUI tabline, don't process this
5139 * event */
5140 GetWindowRect(s_textArea, &rct);
5141 if (yPos < rct.top)
5142 return result;
5143 }
5144#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02005145 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 xPos -= x;
5147
5148 if (xPos < 48) /* <VN> TODO should use system metric? */
5149 return HTBOTTOMLEFT;
5150 else
5151 return HTBOTTOMRIGHT;
5152 }
5153 else
5154 return result;
5155 }
5156 /* break; notreached */
5157
5158#ifdef FEAT_MBYTE_IME
5159 case WM_IME_NOTIFY:
5160 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
5161 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005162 return 1L;
5163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 case WM_IME_COMPOSITION:
5165 if (!_OnImeComposition(hwnd, wParam, lParam))
5166 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005167 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#endif
5169
5170 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005172 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 _OnFindRepl();
5175 }
5176#endif
5177 return MyWindowProc(hwnd, uMsg, wParam, lParam);
5178 }
5179
Bram Moolenaar2787ab92011-12-14 15:23:59 +01005180 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181}
5182
5183/*
5184 * End of call-back routines
5185 */
5186
5187/* parent window, if specified with -P */
5188HWND vim_parent_hwnd = NULL;
5189
5190 static BOOL CALLBACK
5191FindWindowTitle(HWND hwnd, LPARAM lParam)
5192{
5193 char buf[2048];
5194 char *title = (char *)lParam;
5195
5196 if (GetWindowText(hwnd, buf, sizeof(buf)))
5197 {
5198 if (strstr(buf, title) != NULL)
5199 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005200 /* Found it. Store the window ref. and quit searching if MDI
5201 * works. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005203 if (vim_parent_hwnd != NULL)
5204 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 }
5207 return TRUE; /* continue searching */
5208}
5209
5210/*
5211 * Invoked for '-P "title"' argument: search for parent application to open
5212 * our window in.
5213 */
5214 void
5215gui_mch_set_parent(char *title)
5216{
5217 EnumWindows(FindWindowTitle, (LPARAM)title);
5218 if (vim_parent_hwnd == NULL)
5219 {
5220 EMSG2(_("E671: Cannot find window title \"%s\""), title);
5221 mch_exit(2);
5222 }
5223}
5224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 static void
5227ole_error(char *arg)
5228{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005229 char buf[IOSIZE];
5230
5231 /* Can't use EMSG() here, we have not finished initialisation yet. */
5232 vim_snprintf(buf, IOSIZE,
5233 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
5234 arg);
5235 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239/*
5240 * Parse the GUI related command-line arguments. Any arguments used are
5241 * deleted from argv, and *argc is decremented accordingly. This is called
5242 * when vim is started, whether or not the GUI has been started.
5243 */
5244 void
5245gui_mch_prepare(int *argc, char **argv)
5246{
5247 int silent = FALSE;
5248 int idx;
5249
5250 /* Check for special OLE command line parameters */
5251 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5252 {
5253 /* Check for a "-silent" argument first. */
5254 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5255 && (argv[2][0] == '-' || argv[2][0] == '/'))
5256 {
5257 silent = TRUE;
5258 idx = 2;
5259 }
5260 else
5261 idx = 1;
5262
5263 /* Register Vim as an OLE Automation server */
5264 if (STRICMP(argv[idx] + 1, "register") == 0)
5265 {
5266#ifdef FEAT_OLE
5267 RegisterMe(silent);
5268 mch_exit(0);
5269#else
5270 if (!silent)
5271 ole_error("register");
5272 mch_exit(2);
5273#endif
5274 }
5275
5276 /* Unregister Vim as an OLE Automation server */
5277 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5278 {
5279#ifdef FEAT_OLE
5280 UnregisterMe(!silent);
5281 mch_exit(0);
5282#else
5283 if (!silent)
5284 ole_error("unregister");
5285 mch_exit(2);
5286#endif
5287 }
5288
5289 /* Ignore an -embedding argument. It is only relevant if the
5290 * application wants to treat the case when it is started manually
5291 * differently from the case where it is started via automation (and
5292 * we don't).
5293 */
5294 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5295 {
5296#ifdef FEAT_OLE
5297 *argc = 1;
5298#else
5299 ole_error("embedding");
5300 mch_exit(2);
5301#endif
5302 }
5303 }
5304
5305#ifdef FEAT_OLE
5306 {
5307 int bDoRestart = FALSE;
5308
5309 InitOLE(&bDoRestart);
5310 /* automatically exit after registering */
5311 if (bDoRestart)
5312 mch_exit(0);
5313 }
5314#endif
5315
5316#ifdef FEAT_NETBEANS_INTG
5317 {
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005318 /* stolen from gui_x11.c */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 int arg;
5320
5321 for (arg = 1; arg < *argc; arg++)
5322 if (strncmp("-nb", argv[arg], 3) == 0)
5323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 netbeansArg = argv[arg];
5325 mch_memmove(&argv[arg], &argv[arg + 1],
5326 (--*argc - arg) * sizeof(char *));
5327 argv[*argc] = NULL;
5328 break; /* enough? */
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 }
5331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332}
5333
5334/*
5335 * Initialise the GUI. Create all the windows, set up all the call-backs
5336 * etc.
5337 */
5338 int
5339gui_mch_init(void)
5340{
5341 const char szVimWndClass[] = VIM_CLASS;
5342 const char szTextAreaClass[] = "VimTextArea";
5343 WNDCLASS wndclass;
5344#ifdef FEAT_MBYTE
5345 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005346 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 WNDCLASSW wndclassw;
5348#endif
5349#ifdef GLOBAL_IME
5350 ATOM atom;
5351#endif
5352
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 /* Return here if the window was already opened (happens when
5354 * gui_mch_dialog() is called early). */
5355 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005356 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 /*
5359 * Load the tearoff bitmap
5360 */
5361#ifdef FEAT_TEAROFF
5362 s_htearbitmap = LoadBitmap(s_hinst, "IDB_TEAROFF");
5363#endif
5364
5365 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5366 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5367#ifdef FEAT_MENU
5368 gui.menu_height = 0; /* Windows takes care of this */
5369#endif
5370 gui.border_width = 0;
5371
5372 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5373
5374#ifdef FEAT_MBYTE
5375 /* First try using the wide version, so that we can use any title.
5376 * Otherwise only characters in the active codepage will work. */
5377 if (GetClassInfoW(s_hinst, szVimWndClassW, &wndclassw) == 0)
5378 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005379 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 wndclassw.lpfnWndProc = _WndProc;
5381 wndclassw.cbClsExtra = 0;
5382 wndclassw.cbWndExtra = 0;
5383 wndclassw.hInstance = s_hinst;
5384 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5385 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5386 wndclassw.hbrBackground = s_brush;
5387 wndclassw.lpszMenuName = NULL;
5388 wndclassw.lpszClassName = szVimWndClassW;
5389
5390 if ((
5391#ifdef GLOBAL_IME
5392 atom =
5393#endif
5394 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005395 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 else
5397 wide_WindowProc = TRUE;
5398 }
5399
5400 if (!wide_WindowProc)
5401#endif
5402
5403 if (GetClassInfo(s_hinst, szVimWndClass, &wndclass) == 0)
5404 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005405 wndclass.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 wndclass.lpfnWndProc = _WndProc;
5407 wndclass.cbClsExtra = 0;
5408 wndclass.cbWndExtra = 0;
5409 wndclass.hInstance = s_hinst;
5410 wndclass.hIcon = LoadIcon(wndclass.hInstance, "IDR_VIM");
5411 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5412 wndclass.hbrBackground = s_brush;
5413 wndclass.lpszMenuName = NULL;
5414 wndclass.lpszClassName = szVimWndClass;
5415
5416 if ((
5417#ifdef GLOBAL_IME
5418 atom =
5419#endif
5420 RegisterClass(&wndclass)) == 0)
5421 return FAIL;
5422 }
5423
5424 if (vim_parent_hwnd != NULL)
5425 {
5426#ifdef HAVE_TRY_EXCEPT
5427 __try
5428 {
5429#endif
5430 /* Open inside the specified parent window.
5431 * TODO: last argument should point to a CLIENTCREATESTRUCT
5432 * structure. */
5433 s_hwnd = CreateWindowEx(
5434 WS_EX_MDICHILD,
5435 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005436 WS_OVERLAPPEDWINDOW | WS_CHILD
5437 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5439 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5440 100, /* Any value will do */
5441 100, /* Any value will do */
5442 vim_parent_hwnd, NULL,
5443 s_hinst, NULL);
5444#ifdef HAVE_TRY_EXCEPT
5445 }
5446 __except(EXCEPTION_EXECUTE_HANDLER)
5447 {
5448 /* NOP */
5449 }
5450#endif
5451 if (s_hwnd == NULL)
5452 {
5453 EMSG(_("E672: Unable to open window inside MDI application"));
5454 mch_exit(2);
5455 }
5456 }
5457 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005458 {
5459 /* If the provided windowid is not valid reset it to zero, so that it
5460 * is ignored and we open our own window. */
5461 if (IsWindow((HWND)win_socket_id) <= 0)
5462 win_socket_id = 0;
5463
5464 /* Create a window. If win_socket_id is not zero without border and
5465 * titlebar, it will be reparented below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 s_hwnd = CreateWindow(
Bram Moolenaar78e17622007-08-30 10:26:19 +00005467 szVimWndClass, "Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005468 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5469 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005470 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5471 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
5472 100, /* Any value will do */
5473 100, /* Any value will do */
5474 NULL, NULL,
5475 s_hinst, NULL);
5476 if (s_hwnd != NULL && win_socket_id != 0)
5477 {
5478 SetParent(s_hwnd, (HWND)win_socket_id);
5479 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5480 }
5481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 if (s_hwnd == NULL)
5484 return FAIL;
5485
5486#ifdef GLOBAL_IME
5487 global_ime_init(atom, s_hwnd);
5488#endif
5489#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5490 dyn_imm_load();
5491#endif
5492
5493 /* Create the text area window */
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005494#ifdef FEAT_MBYTE
5495 if (wide_WindowProc)
5496 {
5497 if (GetClassInfoW(s_hinst, szTextAreaClassW, &wndclassw) == 0)
5498 {
5499 wndclassw.style = CS_OWNDC;
5500 wndclassw.lpfnWndProc = _TextAreaWndProc;
5501 wndclassw.cbClsExtra = 0;
5502 wndclassw.cbWndExtra = 0;
5503 wndclassw.hInstance = s_hinst;
5504 wndclassw.hIcon = NULL;
5505 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5506 wndclassw.hbrBackground = NULL;
5507 wndclassw.lpszMenuName = NULL;
5508 wndclassw.lpszClassName = szTextAreaClassW;
5509
5510 if (RegisterClassW(&wndclassw) == 0)
5511 return FAIL;
5512 }
5513 }
5514 else
5515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 if (GetClassInfo(s_hinst, szTextAreaClass, &wndclass) == 0)
5517 {
5518 wndclass.style = CS_OWNDC;
5519 wndclass.lpfnWndProc = _TextAreaWndProc;
5520 wndclass.cbClsExtra = 0;
5521 wndclass.cbWndExtra = 0;
5522 wndclass.hInstance = s_hinst;
5523 wndclass.hIcon = NULL;
5524 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
5525 wndclass.hbrBackground = NULL;
5526 wndclass.lpszMenuName = NULL;
5527 wndclass.lpszClassName = szTextAreaClass;
5528
5529 if (RegisterClass(&wndclass) == 0)
5530 return FAIL;
5531 }
5532 s_textArea = CreateWindowEx(
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005533 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 szTextAreaClass, "Vim text area",
5535 WS_CHILD | WS_VISIBLE, 0, 0,
5536 100, /* Any value will do for now */
5537 100, /* Any value will do for now */
5538 s_hwnd, NULL,
5539 s_hinst, NULL);
5540
5541 if (s_textArea == NULL)
5542 return FAIL;
5543
Bram Moolenaar20321902016-02-17 12:30:17 +01005544#ifdef FEAT_LIBCALL
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005545 /* Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico. */
5546 {
5547 HANDLE hIcon = NULL;
5548
5549 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005550 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005551 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005552#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554#ifdef FEAT_MENU
5555 s_menuBar = CreateMenu();
5556#endif
5557 s_hdc = GetDC(s_textArea);
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561 /* Do we need to bother with this? */
5562 /* m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); */
5563
5564 /* Get background/foreground colors from the system */
5565 gui_mch_def_colors();
5566
5567 /* Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5568 * file) */
5569 set_normal_colors();
5570
5571 /*
5572 * Check that none of the colors are the same as the background color.
5573 * Then store the current values as the defaults.
5574 */
5575 gui_check_colors();
5576 gui.def_norm_pixel = gui.norm_pixel;
5577 gui.def_back_pixel = gui.back_pixel;
5578
5579 /* Get the colors for the highlight groups (gui_check_colors() might have
5580 * changed them) */
5581 highlight_gui_started();
5582
5583 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005584 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005586 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
5588 /*
5589 * Set up for Intellimouse processing
5590 */
5591 init_mouse_wheel();
5592
5593 /*
5594 * compute a couple of metrics used for the dialogs
5595 */
5596 get_dialog_font_metrics();
5597#ifdef FEAT_TOOLBAR
5598 /*
5599 * Create the toolbar
5600 */
5601 initialise_toolbar();
5602#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005603#ifdef FEAT_GUI_TABLINE
5604 /*
5605 * Create the tabline
5606 */
5607 initialise_tabline();
5608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#ifdef MSWIN_FIND_REPLACE
5610 /*
5611 * Initialise the dialog box stuff
5612 */
5613 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5614
5615 /* Initialise the struct */
5616 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005617 s_findrep_struct.lpstrFindWhat = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005619 s_findrep_struct.lpstrReplaceWith = (LPSTR)alloc(MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5621 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5622 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005623# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00005624 s_findrep_struct_w.lStructSize = sizeof(s_findrep_struct_w);
5625 s_findrep_struct_w.lpstrFindWhat =
5626 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5627 s_findrep_struct_w.lpstrFindWhat[0] = NUL;
5628 s_findrep_struct_w.lpstrReplaceWith =
5629 (LPWSTR)alloc(MSWIN_FR_BUFSIZE * sizeof(WCHAR));
5630 s_findrep_struct_w.lpstrReplaceWith[0] = NUL;
5631 s_findrep_struct_w.wFindWhatLen = MSWIN_FR_BUFSIZE;
5632 s_findrep_struct_w.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005636#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005637# if !defined(_MSC_VER) || (_MSC_VER < 1400)
5638/* Define HandleToLong for old MS and non-MS compilers if not defined. */
5639# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005640# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005641# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005642# endif
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005643 /* set the v:windowid variable */
Bram Moolenaar7154b322011-05-25 21:18:06 +02005644 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005645#endif
5646
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005647#ifdef FEAT_RENDER_OPTIONS
5648 if (p_rop)
5649 (void)gui_mch_set_rendering_options(p_rop);
5650#endif
5651
Bram Moolenaar748bf032005-02-02 23:04:36 +00005652theend:
5653 /* Display any pending error messages */
5654 display_errors();
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return OK;
5657}
5658
5659/*
5660 * Get the size of the screen, taking position on multiple monitors into
5661 * account (if supported).
5662 */
5663 static void
5664get_work_area(RECT *spi_rect)
5665{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005666 HMONITOR mon;
5667 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005669 /* work out which monitor the window is on, and get *it's* work area */
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005670 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005671 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005673 moninfo.cbSize = sizeof(MONITORINFO);
5674 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005676 *spi_rect = moninfo.rcWork;
5677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 }
5680 /* this is the old method... */
5681 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5682}
5683
5684/*
5685 * Set the size of the window to the given width and height in pixels.
5686 */
5687 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005688gui_mch_set_shellsize(
5689 int width,
5690 int height,
5691 int min_width UNUSED,
5692 int min_height UNUSED,
5693 int base_width UNUSED,
5694 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005695 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 RECT workarea_rect;
5698 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 WINDOWPLACEMENT wndpl;
5700
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005701 /* Try to keep window completely on screen. */
5702 /* Get position of the screen work area. This is the part that is not
5703 * used by the taskbar or appbars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 get_work_area(&workarea_rect);
5705
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02005706 /* Get current position of our window. Note that the .left and .top are
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005707 * relative to the work area. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 wndpl.length = sizeof(WINDOWPLACEMENT);
5709 GetWindowPlacement(s_hwnd, &wndpl);
5710
5711 /* Resizing a maximized window looks very strange, unzoom it first.
5712 * But don't do it when still starting up, it may have been requested in
5713 * the shortcut. */
5714 if (wndpl.showCmd == SW_SHOWMAXIMIZED && starting == 0)
5715 {
5716 ShowWindow(s_hwnd, SW_SHOWNORMAL);
5717 /* Need to get the settings of the normal window. */
5718 GetWindowPlacement(s_hwnd, &wndpl);
5719 }
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 /* compute the size of the outside of the window */
Bram Moolenaar9d488952013-07-21 17:53:58 +02005722 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005723 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005724 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005725 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 + GetSystemMetrics(SM_CYCAPTION)
5727#ifdef FEAT_MENU
5728 + gui_mswin_get_menu_height(FALSE)
5729#endif
5730 ;
5731
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005732 /* The following should take care of keeping Vim on the same monitor, no
5733 * matter if the secondary monitor is left or right of the primary
5734 * monitor. */
5735 wndpl.rcNormalPosition.right = wndpl.rcNormalPosition.left + win_width;
5736 wndpl.rcNormalPosition.bottom = wndpl.rcNormalPosition.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005737
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005738 /* If the window is going off the screen, move it on to the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005739 if ((direction & RESIZE_HOR)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005740 && wndpl.rcNormalPosition.right > workarea_rect.right)
5741 OffsetRect(&wndpl.rcNormalPosition,
5742 workarea_rect.right - wndpl.rcNormalPosition.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005744 if ((direction & RESIZE_HOR)
5745 && wndpl.rcNormalPosition.left < workarea_rect.left)
5746 OffsetRect(&wndpl.rcNormalPosition,
5747 workarea_rect.left - wndpl.rcNormalPosition.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748
Bram Moolenaarafa24992006-03-27 20:58:26 +00005749 if ((direction & RESIZE_VERT)
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005750 && wndpl.rcNormalPosition.bottom > workarea_rect.bottom)
5751 OffsetRect(&wndpl.rcNormalPosition,
5752 0, workarea_rect.bottom - wndpl.rcNormalPosition.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
Bram Moolenaar6ef47c22012-01-04 20:29:22 +01005754 if ((direction & RESIZE_VERT)
5755 && wndpl.rcNormalPosition.top < workarea_rect.top)
5756 OffsetRect(&wndpl.rcNormalPosition,
5757 0, workarea_rect.top - wndpl.rcNormalPosition.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759 /* set window position - we should use SetWindowPlacement rather than
5760 * SetWindowPos as the MSDN docs say the coord systems returned by
5761 * these two are not compatible. */
5762 SetWindowPlacement(s_hwnd, &wndpl);
5763
5764 SetActiveWindow(s_hwnd);
5765 SetFocus(s_hwnd);
5766
5767#ifdef FEAT_MENU
5768 /* Menu may wrap differently now */
5769 gui_mswin_get_menu_height(!gui.starting);
5770#endif
5771}
5772
5773
5774 void
5775gui_mch_set_scrollbar_thumb(
5776 scrollbar_T *sb,
5777 long val,
5778 long size,
5779 long max)
5780{
5781 SCROLLINFO info;
5782
5783 sb->scroll_shift = 0;
5784 while (max > 32767)
5785 {
5786 max = (max + 1) >> 1;
5787 val >>= 1;
5788 size >>= 1;
5789 ++sb->scroll_shift;
5790 }
5791
5792 if (sb->scroll_shift > 0)
5793 ++size;
5794
5795 info.cbSize = sizeof(info);
5796 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5797 info.nPos = val;
5798 info.nMin = 0;
5799 info.nMax = max;
5800 info.nPage = size;
5801 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5802}
5803
5804
5805/*
5806 * Set the current text font.
5807 */
5808 void
5809gui_mch_set_font(GuiFont font)
5810{
5811 gui.currFont = font;
5812}
5813
5814
5815/*
5816 * Set the current text foreground color.
5817 */
5818 void
5819gui_mch_set_fg_color(guicolor_T color)
5820{
5821 gui.currFgColor = color;
5822}
5823
5824/*
5825 * Set the current text background color.
5826 */
5827 void
5828gui_mch_set_bg_color(guicolor_T color)
5829{
5830 gui.currBgColor = color;
5831}
5832
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005833/*
5834 * Set the current text special color.
5835 */
5836 void
5837gui_mch_set_sp_color(guicolor_T color)
5838{
5839 gui.currSpColor = color;
5840}
5841
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005842#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843/*
5844 * Multi-byte handling, originally by Sung-Hoon Baek.
5845 * First static functions (no prototypes generated).
5846 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005847# ifdef _MSC_VER
5848# include <ime.h> /* Apparently not needed for Cygwin, MingW or Borland. */
5849# endif
5850# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 * handle WM_IME_NOTIFY message
5854 */
5855 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005856_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 LRESULT lResult = 0;
5859 HIMC hImc;
5860
5861 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5862 return lResult;
5863 switch (dwCommand)
5864 {
5865 case IMN_SETOPENSTATUS:
5866 if (pImmGetOpenStatus(hImc))
5867 {
5868 pImmSetCompositionFont(hImc, &norm_logfont);
5869 im_set_position(gui.row, gui.col);
5870
5871 /* Disable langmap */
5872 State &= ~LANGMAP;
5873 if (State & INSERT)
5874 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005875#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 /* Unshown 'keymap' in status lines */
5877 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5878 {
5879 /* Save cursor position */
5880 int old_row = gui.row;
5881 int old_col = gui.col;
5882
5883 // This must be called here before
5884 // status_redraw_curbuf(), otherwise the mode
5885 // message may appear in the wrong position.
5886 showmode();
5887 status_redraw_curbuf();
5888 update_screen(0);
5889 /* Restore cursor position */
5890 gui.row = old_row;
5891 gui.col = old_col;
5892 }
5893#endif
5894 }
5895 }
5896 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005897 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 lResult = 0;
5899 break;
5900 }
5901 pImmReleaseContext(hWnd, hImc);
5902 return lResult;
5903}
5904
5905 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005906_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 char_u *ret;
5909 int len;
5910
5911 if ((param & GCS_RESULTSTR) == 0) /* Composition unfinished. */
5912 return 0;
5913
5914 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5915 if (ret != NULL)
5916 {
5917 add_to_input_buf_csi(ret, len);
5918 vim_free(ret);
5919 return 1;
5920 }
5921 return 0;
5922}
5923
5924/*
5925 * get the current composition string, in UCS-2; *lenp is the number of
5926 * *lenp is the number of Unicode characters.
5927 */
5928 static short_u *
5929GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5930{
5931 LONG ret;
5932 LPWSTR wbuf = NULL;
5933 char_u *buf;
5934
5935 if (!pImmGetContext)
5936 return NULL; /* no imm32.dll */
5937
5938 /* Try Unicode; this'll always work on NT regardless of codepage. */
5939 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5940 if (ret == 0)
5941 return NULL; /* empty */
5942
5943 if (ret > 0)
5944 {
5945 /* Allocate the requested buffer plus space for the NUL character. */
5946 wbuf = (LPWSTR)alloc(ret + sizeof(WCHAR));
5947 if (wbuf != NULL)
5948 {
5949 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5950 *lenp = ret / sizeof(WCHAR);
5951 }
5952 return (short_u *)wbuf;
5953 }
5954
5955 /* ret < 0; we got an error, so try the ANSI version. This'll work
5956 * on 9x/ME, but only if the codepage happens to be set to whatever
5957 * we're inputting. */
5958 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5959 if (ret <= 0)
5960 return NULL; /* empty or error */
5961
5962 buf = alloc(ret);
5963 if (buf == NULL)
5964 return NULL;
5965 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5966
5967 /* convert from codepage to UCS-2 */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005968 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 vim_free(buf);
5970
5971 return (short_u *)wbuf;
5972}
5973
5974/*
5975 * void GetResultStr()
5976 *
5977 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5978 * get complete composition string
5979 */
5980 static char_u *
5981GetResultStr(HWND hwnd, int GCS, int *lenp)
5982{
5983 HIMC hIMC; /* Input context handle. */
5984 short_u *buf = NULL;
5985 char_u *convbuf = NULL;
5986
5987 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5988 return NULL;
5989
5990 /* Reads in the composition string. */
5991 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5992 if (buf == NULL)
5993 return NULL;
5994
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005995 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 pImmReleaseContext(hwnd, hIMC);
5997 vim_free(buf);
5998 return convbuf;
5999}
6000#endif
6001
6002/* For global functions we need prototypes. */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006003#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
6005/*
6006 * set font to IM.
6007 */
6008 void
6009im_set_font(LOGFONT *lf)
6010{
6011 HIMC hImc;
6012
6013 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6014 {
6015 pImmSetCompositionFont(hImc, lf);
6016 pImmReleaseContext(s_hwnd, hImc);
6017 }
6018}
6019
6020/*
6021 * Notify cursor position to IM.
6022 */
6023 void
6024im_set_position(int row, int col)
6025{
6026 HIMC hImc;
6027
6028 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6029 {
6030 COMPOSITIONFORM cfs;
6031
6032 cfs.dwStyle = CFS_POINT;
6033 cfs.ptCurrentPos.x = FILL_X(col);
6034 cfs.ptCurrentPos.y = FILL_Y(row);
6035 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
6036 pImmSetCompositionWindow(hImc, &cfs);
6037
6038 pImmReleaseContext(s_hwnd, hImc);
6039 }
6040}
6041
6042/*
6043 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6044 */
6045 void
6046im_set_active(int active)
6047{
6048 HIMC hImc;
6049 static HIMC hImcOld = (HIMC)0;
6050
6051 if (pImmGetContext) /* if NULL imm32.dll wasn't loaded (yet) */
6052 {
6053 if (p_imdisable)
6054 {
6055 if (hImcOld == (HIMC)0)
6056 {
6057 hImcOld = pImmGetContext(s_hwnd);
6058 if (hImcOld)
6059 pImmAssociateContext(s_hwnd, (HIMC)0);
6060 }
6061 active = FALSE;
6062 }
6063 else if (hImcOld != (HIMC)0)
6064 {
6065 pImmAssociateContext(s_hwnd, hImcOld);
6066 hImcOld = (HIMC)0;
6067 }
6068
6069 hImc = pImmGetContext(s_hwnd);
6070 if (hImc)
6071 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006072 /*
6073 * for Korean ime
6074 */
6075 HKL hKL = GetKeyboardLayout(0);
6076
6077 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6078 {
6079 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6080 static BOOL bSaved = FALSE;
6081
6082 if (active)
6083 {
6084 /* if we have a saved conversion status, restore it */
6085 if (bSaved)
6086 pImmSetConversionStatus(hImc, dwConversionSaved,
6087 dwSentenceSaved);
6088 bSaved = FALSE;
6089 }
6090 else
6091 {
6092 /* save conversion status and disable korean */
6093 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6094 &dwSentenceSaved))
6095 {
6096 bSaved = TRUE;
6097 pImmSetConversionStatus(hImc,
6098 dwConversionSaved & ~(IME_CMODE_NATIVE
6099 | IME_CMODE_FULLSHAPE),
6100 dwSentenceSaved);
6101 }
6102 }
6103 }
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 pImmSetOpenStatus(hImc, active);
6106 pImmReleaseContext(s_hwnd, hImc);
6107 }
6108 }
6109}
6110
6111/*
6112 * Get IM status. When IM is on, return not 0. Else return 0.
6113 */
6114 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006115im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
6117 int status = 0;
6118 HIMC hImc;
6119
6120 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6121 {
6122 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6123 pImmReleaseContext(s_hwnd, hImc);
6124 }
6125 return status;
6126}
6127
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006128#endif /* FEAT_MBYTE_IME */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130#if defined(FEAT_MBYTE) && !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
6131/* Win32 with GLOBAL IME */
6132
6133/*
6134 * Notify cursor position to IM.
6135 */
6136 void
6137im_set_position(int row, int col)
6138{
6139 /* Win32 with GLOBAL IME */
6140 POINT p;
6141
6142 p.x = FILL_X(col);
6143 p.y = FILL_Y(row);
6144 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
6145 global_ime_set_position(&p);
6146}
6147
6148/*
6149 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6150 */
6151 void
6152im_set_active(int active)
6153{
6154 global_ime_set_status(active);
6155}
6156
6157/*
6158 * Get IM status. When IM is on, return not 0. Else return 0.
6159 */
6160 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006161im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 return global_ime_get_status();
6164}
6165#endif
6166
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006167#ifdef FEAT_MBYTE
6168/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006169 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006170 */
6171 static void
6172latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6173{
6174 int c;
6175
Bram Moolenaarca003e12006-03-17 23:19:38 +00006176 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006177 {
6178 c = *text++;
6179 switch (c)
6180 {
6181 case 0xa4: c = 0x20ac; break; /* euro */
6182 case 0xa6: c = 0x0160; break; /* S hat */
6183 case 0xa8: c = 0x0161; break; /* S -hat */
6184 case 0xb4: c = 0x017d; break; /* Z hat */
6185 case 0xb8: c = 0x017e; break; /* Z -hat */
6186 case 0xbc: c = 0x0152; break; /* OE */
6187 case 0xbd: c = 0x0153; break; /* oe */
6188 case 0xbe: c = 0x0178; break; /* Y */
6189 }
6190 *unicodebuf++ = c;
6191 }
6192}
6193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
6195#ifdef FEAT_RIGHTLEFT
6196/*
6197 * What is this for? In the case where you are using Win98 or Win2K or later,
6198 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6199 * reverses the string sent to the TextOut... family. This sucks, because we
6200 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6201 * way to tell Windblows not to do this!
6202 *
6203 * The short of it is that this 'RevOut' only gets called if you are running
6204 * one of the new, "improved" MS OSes, and only if you are running in
6205 * 'rightleft' mode. It makes display take *slightly* longer, but not
6206 * noticeably so.
6207 */
6208 static void
6209RevOut( HDC s_hdc,
6210 int col,
6211 int row,
6212 UINT foptions,
6213 CONST RECT *pcliprect,
6214 LPCTSTR text,
6215 UINT len,
6216 CONST INT *padding)
6217{
6218 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006220 for (ix = 0; ix < (int)len; ++ix)
6221 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
6222 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223}
6224#endif
6225
Bram Moolenaar92467d32017-12-05 13:22:16 +01006226 static void
6227draw_line(
6228 int x1,
6229 int y1,
6230 int x2,
6231 int y2,
6232 COLORREF color)
6233{
6234#if defined(FEAT_DIRECTX)
6235 if (IS_ENABLE_DIRECTX())
6236 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6237 else
6238#endif
6239 {
6240 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6241 HPEN old_pen = SelectObject(s_hdc, hpen);
6242 MoveToEx(s_hdc, x1, y1, NULL);
6243 /* Note: LineTo() excludes the last pixel in the line. */
6244 LineTo(s_hdc, x2, y2);
6245 DeleteObject(SelectObject(s_hdc, old_pen));
6246 }
6247}
6248
6249 static void
6250set_pixel(
6251 int x,
6252 int y,
6253 COLORREF color)
6254{
6255#if defined(FEAT_DIRECTX)
6256 if (IS_ENABLE_DIRECTX())
6257 DWriteContext_SetPixel(s_dwc, x, y, color);
6258 else
6259#endif
6260 SetPixel(s_hdc, x, y, color);
6261}
6262
6263 static void
6264fill_rect(
6265 const RECT *rcp,
6266 HBRUSH hbr,
6267 COLORREF color)
6268{
6269#if defined(FEAT_DIRECTX)
6270 if (IS_ENABLE_DIRECTX())
6271 DWriteContext_FillRect(s_dwc, rcp, color);
6272 else
6273#endif
6274 {
6275 HBRUSH hbr2;
6276
6277 if (hbr == NULL)
6278 hbr2 = CreateSolidBrush(color);
6279 else
6280 hbr2 = hbr;
6281 FillRect(s_hdc, rcp, hbr2);
6282 if (hbr == NULL)
6283 DeleteBrush(hbr2);
6284 }
6285}
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 void
6288gui_mch_draw_string(
6289 int row,
6290 int col,
6291 char_u *text,
6292 int len,
6293 int flags)
6294{
6295 static int *padding = NULL;
6296 static int pad_size = 0;
6297 int i;
6298 const RECT *pcliprect = NULL;
6299 UINT foptions = 0;
6300#ifdef FEAT_MBYTE
6301 static WCHAR *unicodebuf = NULL;
6302 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006303 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 int n = 0;
6305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 int y;
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /*
6309 * Italic and bold text seems to have an extra row of pixels at the bottom
6310 * (below where the bottom of the character should be). If we draw the
6311 * characters with a solid background, the top row of pixels in the
6312 * character below will be overwritten. We can fix this by filling in the
6313 * background ourselves, to the correct character proportions, and then
6314 * writing the character in transparent mode. Still have a problem when
6315 * the character is "_", which gets written on to the character below.
6316 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6317 * pixel in their slots, which fixes the problem with the bottom row of
6318 * pixels. We still need this code because otherwise the top row of pixels
6319 * becomes a problem. - webb.
6320 */
6321 static HBRUSH hbr_cache[2] = {NULL, NULL};
6322 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6323 static int brush_lru = 0;
6324 HBRUSH hbr;
6325 RECT rc;
6326
6327 if (!(flags & DRAW_TRANSP))
6328 {
6329 /*
6330 * Clear background first.
6331 * Note: FillRect() excludes right and bottom of rectangle.
6332 */
6333 rc.left = FILL_X(col);
6334 rc.top = FILL_Y(row);
6335#ifdef FEAT_MBYTE
6336 if (has_mbyte)
6337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 /* Compute the length in display cells. */
Bram Moolenaar72597a52010-07-18 15:31:08 +02006339 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
6341 else
6342#endif
6343 rc.right = FILL_X(col + len);
6344 rc.bottom = FILL_Y(row + 1);
6345
6346 /* Cache the created brush, that saves a lot of time. We need two:
6347 * one for cursor background and one for the normal background. */
6348 if (gui.currBgColor == brush_color[0])
6349 {
6350 hbr = hbr_cache[0];
6351 brush_lru = 1;
6352 }
6353 else if (gui.currBgColor == brush_color[1])
6354 {
6355 hbr = hbr_cache[1];
6356 brush_lru = 0;
6357 }
6358 else
6359 {
6360 if (hbr_cache[brush_lru] != NULL)
6361 DeleteBrush(hbr_cache[brush_lru]);
6362 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6363 brush_color[brush_lru] = gui.currBgColor;
6364 hbr = hbr_cache[brush_lru];
6365 brush_lru = !brush_lru;
6366 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006367
Bram Moolenaar92467d32017-12-05 13:22:16 +01006368 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370 SetBkMode(s_hdc, TRANSPARENT);
6371
6372 /*
6373 * When drawing block cursor, prevent inverted character spilling
6374 * over character cell (can happen with bold/italic)
6375 */
6376 if (flags & DRAW_CURSOR)
6377 {
6378 pcliprect = &rc;
6379 foptions = ETO_CLIPPED;
6380 }
6381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 SetTextColor(s_hdc, gui.currFgColor);
6383 SelectFont(s_hdc, gui.currFont);
6384
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006385#ifdef FEAT_DIRECTX
6386 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006387 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006388#endif
6389
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6391 {
6392 vim_free(padding);
6393 pad_size = Columns;
6394
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006395 /* Don't give an out-of-memory message here, it would call us
6396 * recursively. */
6397 padding = (int *)lalloc(pad_size * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 if (padding != NULL)
6399 for (i = 0; i < pad_size; i++)
6400 padding[i] = gui.char_width;
6401 }
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 /*
6404 * We have to provide the padding argument because italic and bold versions
6405 * of fixed-width fonts are often one pixel or so wider than their normal
6406 * versions.
6407 * No check for DRAW_BOLD, Windows will have done it already.
6408 */
6409
6410#ifdef FEAT_MBYTE
6411 /* Check if there are any UTF-8 characters. If not, use normal text
6412 * output to speed up output. */
6413 if (enc_utf8)
6414 for (n = 0; n < len; ++n)
6415 if (text[n] >= 0x80)
6416 break;
6417
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006418# if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006419 /* Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6420 * required that unicode drawing routine, currently. So this forces it
6421 * enabled. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006422 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006423 n = 0; /* Keep n < len, to enter block for unicode. */
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006424# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006425
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 /* Check if the Unicode buffer exists and is big enough. Create it
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006427 * with the same length as the multi-byte string, the number of wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 * characters is always equal or smaller. */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006429 if ((enc_utf8
6430 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6431 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 && (unicodebuf == NULL || len > unibuflen))
6433 {
6434 vim_free(unicodebuf);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006435 unicodebuf = (WCHAR *)lalloc(len * sizeof(WCHAR), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
6437 vim_free(unicodepdy);
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00006438 unicodepdy = (int *)lalloc(len * sizeof(int), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
6440 unibuflen = len;
6441 }
6442
6443 if (enc_utf8 && n < len && unicodebuf != NULL)
6444 {
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006445 /* Output UTF-8 characters. Composing characters should be
6446 * handled here. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006447 int i;
6448 int wlen; /* string length in words */
6449 int clen; /* string length in characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 int cells; /* cell width of string up to composing char */
6451 int cw; /* width of current cell */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006452 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006454 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006455 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006457 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006459 c = utf_ptr2char(text + i);
6460 if (c >= 0x10000)
6461 {
6462 /* Turn into UTF-16 encoding. */
Bram Moolenaarca003e12006-03-17 23:19:38 +00006463 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6464 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006465 }
6466 else
6467 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006468 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006469 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006470
6471 if (utf_iscomposing(c))
6472 cw = 0;
6473 else
6474 {
6475 cw = utf_char2cells(c);
6476 if (cw > 2) /* don't use 4 for unprintable char */
6477 cw = 1;
6478 }
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (unicodepdy != NULL)
6481 {
6482 /* Use unicodepdy to make characters fit as we expect, even
6483 * when the font uses different widths (e.g., bold character
6484 * is wider). */
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006485 if (c >= 0x10000)
6486 {
6487 unicodepdy[wlen - 2] = cw * gui.char_width;
6488 unicodepdy[wlen - 1] = 0;
6489 }
6490 else
6491 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006494 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006495 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006497# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006498 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006499 {
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006500 /* Add one to "cells" for italics. */
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006501 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar9b352c42014-08-06 16:49:55 +02006502 TEXT_X(col), TEXT_Y(row), FILL_X(cells + 1), FILL_Y(1),
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006503 gui.char_width, gui.currFgColor,
6504 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006505 }
6506 else
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006507# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006508 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6509 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 len = cells; /* used for underlining */
6511 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006512 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 {
6514 /* If we want to display codepage data, and the current CP is not the
6515 * ANSI one, we need to go via Unicode. */
6516 if (unicodebuf != NULL)
6517 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006518 if (enc_latin9)
6519 latin9_to_ucs(text, len, unicodebuf);
6520 else
6521 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 MB_PRECOMPOSED,
6523 (char *)text, len,
6524 (LPWSTR)unicodebuf, unibuflen);
6525 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006526 {
6527 /* Use unicodepdy to make characters fit as we expect, even
6528 * when the font uses different widths (e.g., bold character
6529 * is wider). */
6530 if (unicodepdy != NULL)
6531 {
6532 int i;
6533 int cw;
6534
6535 for (i = 0; i < len; ++i)
6536 {
6537 cw = utf_char2cells(unicodebuf[i]);
6538 if (cw > 2)
6539 cw = 1;
6540 unicodepdy[i] = cw * gui.char_width;
6541 }
6542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006544 foptions, pcliprect, unicodebuf, len, unicodepdy);
6545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547 }
6548 else
6549#endif
6550 {
6551#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006552 /* Windows will mess up RL text, so we have to draw it character by
6553 * character. Only do this if RL is on, since it's slow. */
6554 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6556 foptions, pcliprect, (char *)text, len, padding);
6557 else
6558#endif
6559 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6560 foptions, pcliprect, (char *)text, len, padding);
6561 }
6562
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006563 /* Underline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (flags & DRAW_UNDERL)
6565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 /* When p_linespace is 0, overwrite the bottom row of pixels.
6567 * Otherwise put the line just below the character. */
6568 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 if (p_linespace > 1)
6570 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006571 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006573
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006574 /* Strikethrough */
6575 if (flags & DRAW_STRIKE)
6576 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006577 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006578 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006579 }
6580
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006581 /* Undercurl */
6582 if (flags & DRAW_UNDERC)
6583 {
6584 int x;
6585 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006586 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006587
6588 y = FILL_Y(row + 1) - 1;
6589 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6590 {
6591 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006592 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006593 }
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
6597
6598/*
6599 * Output routines.
6600 */
6601
6602/* Flush any output to the screen */
6603 void
6604gui_mch_flush(void)
6605{
6606# if defined(__BORLANDC__)
6607 /*
6608 * The GdiFlush declaration (in Borland C 5.01 <wingdi.h>) is not a
6609 * prototype declaration.
6610 * The compiler complains if __stdcall is not used in both declarations.
6611 */
6612 BOOL __stdcall GdiFlush(void);
6613# endif
6614
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006615#if defined(FEAT_DIRECTX)
6616 if (IS_ENABLE_DIRECTX())
6617 DWriteContext_Flush(s_dwc);
6618#endif
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 GdiFlush();
6621}
6622
6623 static void
6624clear_rect(RECT *rcp)
6625{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006626 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
6629
Bram Moolenaarc716c302006-01-21 22:12:51 +00006630 void
6631gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6632{
6633 RECT workarea_rect;
6634
6635 get_work_area(&workarea_rect);
6636
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006637 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006638 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006639 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006640
6641 /* FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6642 * the menubar for MSwin, we subtract it from the screen height, so that
6643 * the window size can be made to fit on the screen. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006644 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006645 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006646 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006647 - GetSystemMetrics(SM_CYCAPTION)
6648#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006649 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006650#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006651 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006652}
6653
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655#if defined(FEAT_MENU) || defined(PROTO)
6656/*
6657 * Add a sub menu to the menu bar.
6658 */
6659 void
6660gui_mch_add_menu(
6661 vimmenu_T *menu,
6662 int pos)
6663{
6664 vimmenu_T *parent = menu->parent;
6665
6666 menu->submenu_id = CreatePopupMenu();
6667 menu->id = s_menu_id++;
6668
6669 if (menu_is_menubar(menu->name))
6670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006672 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006674 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6675 {
6676 /* 'encoding' differs from active codepage: convert menu name
6677 * and use wide function */
6678 wn = enc_to_utf16(menu->name, NULL);
6679 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006681 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006683 infow.cbSize = sizeof(infow);
6684 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6685 | MIIM_SUBMENU;
6686 infow.dwItemData = (long_u)menu;
6687 infow.wID = menu->id;
6688 infow.fType = MFT_STRING;
6689 infow.dwTypeData = wn;
6690 infow.cch = (UINT)wcslen(wn);
6691 infow.hSubMenu = menu->submenu_id;
6692 InsertMenuItemW((parent == NULL)
6693 ? s_menuBar : parent->submenu_id,
6694 (UINT)pos, TRUE, &infow);
6695 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006699 if (wn == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700#endif
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006701 {
6702 MENUITEMINFO info;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006704 info.cbSize = sizeof(info);
6705 info.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID | MIIM_SUBMENU;
6706 info.dwItemData = (long_u)menu;
6707 info.wID = menu->id;
6708 info.fType = MFT_STRING;
6709 info.dwTypeData = (LPTSTR)menu->name;
6710 info.cch = (UINT)STRLEN(menu->name);
6711 info.hSubMenu = menu->submenu_id;
6712 InsertMenuItem((parent == NULL)
6713 ? s_menuBar : parent->submenu_id,
6714 (UINT)pos, TRUE, &info);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
6716 }
6717
6718 /* Fix window size if menu may have wrapped */
6719 if (parent == NULL)
6720 gui_mswin_get_menu_height(!gui.starting);
6721#ifdef FEAT_TEAROFF
6722 else if (IsWindow(parent->tearoff_handle))
6723 rebuild_tearoff(parent);
6724#endif
6725}
6726
6727 void
6728gui_mch_show_popupmenu(vimmenu_T *menu)
6729{
6730 POINT mp;
6731
6732 (void)GetCursorPos((LPPOINT)&mp);
6733 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6734}
6735
6736 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006737gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 vimmenu_T *menu = gui_find_menu(path_name);
6740
6741 if (menu != NULL)
6742 {
6743 POINT p;
6744
6745 /* Find the position of the current cursor */
6746 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006747 if (mouse_pos)
6748 {
6749 int mx, my;
6750
6751 gui_mch_getmouse(&mx, &my);
6752 p.x += mx;
6753 p.y += my;
6754 }
6755 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006757 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6759 }
6760 msg_scroll = FALSE;
6761 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6762 }
6763}
6764
6765#if defined(FEAT_TEAROFF) || defined(PROTO)
6766/*
6767 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6768 * create it as a pseudo-"tearoff menu".
6769 */
6770 void
6771gui_make_tearoff(char_u *path_name)
6772{
6773 vimmenu_T *menu = gui_find_menu(path_name);
6774
6775 /* Found the menu, so tear it off. */
6776 if (menu != NULL)
6777 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6778}
6779#endif
6780
6781/*
6782 * Add a menu item to a menu
6783 */
6784 void
6785gui_mch_add_menu_item(
6786 vimmenu_T *menu,
6787 int idx)
6788{
6789 vimmenu_T *parent = menu->parent;
6790
6791 menu->id = s_menu_id++;
6792 menu->submenu_id = NULL;
6793
6794#ifdef FEAT_TEAROFF
6795 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6796 {
6797 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6798 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6799 }
6800 else
6801#endif
6802#ifdef FEAT_TOOLBAR
6803 if (menu_is_toolbar(parent->name))
6804 {
6805 TBBUTTON newtb;
6806
6807 vim_memset(&newtb, 0, sizeof(newtb));
6808 if (menu_is_separator(menu->name))
6809 {
6810 newtb.iBitmap = 0;
6811 newtb.fsStyle = TBSTYLE_SEP;
6812 }
6813 else
6814 {
6815 newtb.iBitmap = get_toolbar_bitmap(menu);
6816 newtb.fsStyle = TBSTYLE_BUTTON;
6817 }
6818 newtb.idCommand = menu->id;
6819 newtb.fsState = TBSTATE_ENABLED;
6820 newtb.iString = 0;
6821 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6822 (LPARAM)&newtb);
6823 menu->submenu_id = (HMENU)-1;
6824 }
6825 else
6826#endif
6827 {
6828#ifdef FEAT_MBYTE
6829 WCHAR *wn = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830
6831 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
6832 {
6833 /* 'encoding' differs from active codepage: convert menu item name
6834 * and use wide function */
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006835 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 if (wn != NULL)
6837 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006838 InsertMenuW(parent->submenu_id, (UINT)idx,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 (menu_is_separator(menu->name)
6840 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6841 (UINT)menu->id, wn);
6842 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 }
6844 }
6845 if (wn == NULL)
6846#endif
6847 InsertMenu(parent->submenu_id, (UINT)idx,
6848 (menu_is_separator(menu->name) ? MF_SEPARATOR : MF_STRING)
6849 | MF_BYPOSITION,
6850 (UINT)menu->id, (LPCTSTR)menu->name);
6851#ifdef FEAT_TEAROFF
6852 if (IsWindow(parent->tearoff_handle))
6853 rebuild_tearoff(parent);
6854#endif
6855 }
6856}
6857
6858/*
6859 * Destroy the machine specific menu widget.
6860 */
6861 void
6862gui_mch_destroy_menu(vimmenu_T *menu)
6863{
6864#ifdef FEAT_TOOLBAR
6865 /*
6866 * is this a toolbar button?
6867 */
6868 if (menu->submenu_id == (HMENU)-1)
6869 {
6870 int iButton;
6871
6872 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6873 (WPARAM)menu->id, 0);
6874 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6875 }
6876 else
6877#endif
6878 {
6879 if (menu->parent != NULL
6880 && menu_is_popup(menu->parent->dname)
6881 && menu->parent->submenu_id != NULL)
6882 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6883 else
6884 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6885 if (menu->submenu_id != NULL)
6886 DestroyMenu(menu->submenu_id);
6887#ifdef FEAT_TEAROFF
6888 if (IsWindow(menu->tearoff_handle))
6889 DestroyWindow(menu->tearoff_handle);
6890 if (menu->parent != NULL
6891 && menu->parent->children != NULL
6892 && IsWindow(menu->parent->tearoff_handle))
6893 {
6894 /* This menu must not show up when rebuilding the tearoff window. */
6895 menu->modes = 0;
6896 rebuild_tearoff(menu->parent);
6897 }
6898#endif
6899 }
6900}
6901
6902#ifdef FEAT_TEAROFF
6903 static void
6904rebuild_tearoff(vimmenu_T *menu)
6905{
6906 /*hackish*/
6907 char_u tbuf[128];
6908 RECT trect;
6909 RECT rct;
6910 RECT roct;
6911 int x, y;
6912
6913 HWND thwnd = menu->tearoff_handle;
6914
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006915 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 if (GetWindowRect(thwnd, &trect)
6917 && GetWindowRect(s_hwnd, &rct)
6918 && GetClientRect(s_hwnd, &roct))
6919 {
6920 x = trect.left - rct.left;
6921 y = (trect.top - rct.bottom + roct.bottom);
6922 }
6923 else
6924 {
6925 x = y = 0xffffL;
6926 }
6927 DestroyWindow(thwnd);
6928 if (menu->children != NULL)
6929 {
6930 gui_mch_tearoff(tbuf, menu, x, y);
6931 if (IsWindow(menu->tearoff_handle))
6932 (void) SetWindowPos(menu->tearoff_handle,
6933 NULL,
6934 (int)trect.left,
6935 (int)trect.top,
6936 0, 0,
6937 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6938 }
6939}
6940#endif /* FEAT_TEAROFF */
6941
6942/*
6943 * Make a menu either grey or not grey.
6944 */
6945 void
6946gui_mch_menu_grey(
6947 vimmenu_T *menu,
6948 int grey)
6949{
6950#ifdef FEAT_TOOLBAR
6951 /*
6952 * is this a toolbar button?
6953 */
6954 if (menu->submenu_id == (HMENU)-1)
6955 {
6956 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6957 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6958 }
6959 else
6960#endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006961 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6962 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
6964#ifdef FEAT_TEAROFF
6965 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6966 {
6967 WORD menuID;
6968 HWND menuHandle;
6969
6970 /*
6971 * A tearoff button has changed state.
6972 */
6973 if (menu->children == NULL)
6974 menuID = (WORD)(menu->id);
6975 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006976 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6978 if (menuHandle)
6979 EnableWindow(menuHandle, !grey);
6980
6981 }
6982#endif
6983}
6984
6985#endif /* FEAT_MENU */
6986
6987
6988/* define some macros used to make the dialogue creation more readable */
6989
6990#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6991#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006992#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6995/*
6996 * stuff for dialogs
6997 */
6998
6999/*
7000 * The callback routine used by all the dialogs. Very simple. First,
7001 * acknowledges the INITDIALOG message so that Windows knows to do standard
7002 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7003 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7004 * number.
7005 */
7006 static LRESULT CALLBACK
7007dialog_callback(
7008 HWND hwnd,
7009 UINT message,
7010 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007011 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 if (message == WM_INITDIALOG)
7014 {
7015 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
7016 /* Set focus to the dialog. Set the default button, if specified. */
7017 (void)SetFocus(hwnd);
7018 if (dialog_default_button > IDCANCEL)
7019 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007020 else
7021 /* We don't have a default, set focus on another element of the
7022 * dialog window, probably the icon */
7023 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 return FALSE;
7025 }
7026
7027 if (message == WM_COMMAND)
7028 {
7029 int button = LOWORD(wParam);
7030
7031 /* Don't end the dialog if something was selected that was
7032 * not a button.
7033 */
7034 if (button >= DLG_NONBUTTON_CONTROL)
7035 return TRUE;
7036
7037 /* If the edit box exists, copy the string. */
7038 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007039 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007040# ifdef FEAT_MBYTE
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007041 /* If the OS is Windows NT, and 'encoding' differs from active
7042 * codepage: use wide function and convert text. */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007043 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaarcc448b32010-07-14 16:52:17 +02007044 {
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007045 WCHAR *wp = (WCHAR *)alloc(IOSIZE * sizeof(WCHAR));
7046 char_u *p;
7047
7048 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7049 p = utf16_to_enc(wp, NULL);
7050 vim_strncpy(s_textfield, p, IOSIZE);
7051 vim_free(p);
7052 vim_free(wp);
7053 }
7054 else
7055# endif
7056 GetDlgItemText(hwnd, DLG_NONBUTTON_CONTROL + 2,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007057 (LPSTR)s_textfield, IOSIZE);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059
7060 /*
7061 * Need to check for IDOK because if the user just hits Return to
7062 * accept the default value, some reason this is what we get.
7063 */
7064 if (button == IDOK)
7065 {
7066 if (dialog_default_button > IDCANCEL)
7067 EndDialog(hwnd, dialog_default_button);
7068 }
7069 else
7070 EndDialog(hwnd, button - IDCANCEL);
7071 return TRUE;
7072 }
7073
7074 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7075 {
7076 EndDialog(hwnd, 0);
7077 return TRUE;
7078 }
7079 return FALSE;
7080}
7081
7082/*
7083 * Create a dialog dynamically from the parameter strings.
7084 * type = type of dialog (question, alert, etc.)
7085 * title = dialog title. may be NULL for default title.
7086 * message = text to display. Dialog sizes to accommodate it.
7087 * buttons = '\n' separated list of button captions, default first.
7088 * dfltbutton = number of default button.
7089 *
7090 * This routine returns 1 if the first button is pressed,
7091 * 2 for the second, etc.
7092 *
7093 * 0 indicates Esc was pressed.
7094 * -1 for unexpected error
7095 *
7096 * If stubbing out this fn, return 1.
7097 */
7098
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007099static const char *dlg_icons[] = /* must match names in resource file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100{
7101 "IDR_VIM",
7102 "IDR_VIM_ERROR",
7103 "IDR_VIM_ALERT",
7104 "IDR_VIM_INFO",
7105 "IDR_VIM_QUESTION"
7106};
7107
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 int
7109gui_mch_dialog(
7110 int type,
7111 char_u *title,
7112 char_u *message,
7113 char_u *buttons,
7114 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007115 char_u *textfield,
7116 int ex_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
7118 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007119 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 int numButtons;
7121 int *buttonWidths, *buttonPositions;
7122 int buttonYpos;
7123 int nchar, i;
7124 DWORD lStyle;
7125 int dlgwidth = 0;
7126 int dlgheight;
7127 int editboxheight;
7128 int horizWidth = 0;
7129 int msgheight;
7130 char_u *pstart;
7131 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007132 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 char_u *tbuffer;
7134 RECT rect;
7135 HWND hwnd;
7136 HDC hdc;
7137 HFONT font, oldFont;
7138 TEXTMETRIC fontInfo;
7139 int fontHeight;
7140 int textWidth, minButtonWidth, messageWidth;
7141 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007142 int maxDialogHeight;
7143 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 int vertical;
7145 int dlgPaddingX;
7146 int dlgPaddingY;
7147#ifdef USE_SYSMENU_FONT
7148 LOGFONT lfSysmenu;
7149 int use_lfSysmenu = FALSE;
7150#endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007151 garray_T ga;
7152 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154#ifndef NO_CONSOLE
7155 /* Don't output anything in silent mode ("ex -s") */
7156 if (silent_mode)
7157 return dfltbutton; /* return default option */
7158#endif
7159
Bram Moolenaar748bf032005-02-02 23:04:36 +00007160 if (s_hwnd == NULL)
7161 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
7163 if ((type < 0) || (type > VIM_LAST_TYPE))
7164 type = 0;
7165
7166 /* allocate some memory for dialog template */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007167 /* TODO should compute this really */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007168 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007169 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
7171 if (p == NULL)
7172 return -1;
7173
7174 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007175 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7177 * const.
7178 */
7179 tbuffer = vim_strsave(buttons);
7180 if (tbuffer == NULL)
7181 return -1;
7182
7183 --dfltbutton; /* Change from one-based to zero-based */
7184
7185 /* Count buttons */
7186 numButtons = 1;
7187 for (i = 0; tbuffer[i] != '\0'; i++)
7188 {
7189 if (tbuffer[i] == DLG_BUTTON_SEP)
7190 numButtons++;
7191 }
7192 if (dfltbutton >= numButtons)
7193 dfltbutton = -1;
7194
7195 /* Allocate array to hold the width of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007196 buttonWidths = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 if (buttonWidths == NULL)
7198 return -1;
7199
7200 /* Allocate array to hold the X position of each button */
Bram Moolenaarc54b8a72005-09-30 21:20:29 +00007201 buttonPositions = (int *)lalloc(numButtons * sizeof(int), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 if (buttonPositions == NULL)
7203 return -1;
7204
7205 /*
7206 * Calculate how big the dialog must be.
7207 */
7208 hwnd = GetDesktopWindow();
7209 hdc = GetWindowDC(hwnd);
7210#ifdef USE_SYSMENU_FONT
7211 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7212 {
7213 font = CreateFontIndirect(&lfSysmenu);
7214 use_lfSysmenu = TRUE;
7215 }
7216 else
7217#endif
7218 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7219 VARIABLE_PITCH , DLG_FONT_NAME);
7220 if (s_usenewlook)
7221 {
7222 oldFont = SelectFont(hdc, font);
7223 dlgPaddingX = DLG_PADDING_X;
7224 dlgPaddingY = DLG_PADDING_Y;
7225 }
7226 else
7227 {
7228 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7229 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
7230 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
7231 }
7232 GetTextMetrics(hdc, &fontInfo);
7233 fontHeight = fontInfo.tmHeight;
7234
7235 /* Minimum width for horizontal button */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007236 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 /* Maximum width of a dialog, if possible */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007239 if (s_hwnd == NULL)
7240 {
7241 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
Bram Moolenaarc716c302006-01-21 22:12:51 +00007243 /* We don't have a window, use the desktop area. */
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007244 get_work_area(&workarea_rect);
7245 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
7246 if (maxDialogWidth > 600)
7247 maxDialogWidth = 600;
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007248 /* Leave some room for the taskbar. */
7249 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007250 }
7251 else
7252 {
Bram Moolenaara95d8232013-08-07 15:27:11 +02007253 /* Use our own window for the size, unless it's very small. */
7254 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007255 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02007256 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007257 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007258 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
7259 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007260
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007261 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007262 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007263 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02007264 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007265 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
7266 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
7267 }
7268
7269 /* Set dlgwidth to width of message.
7270 * Copy the message into "ga", changing NL to CR-NL and inserting line
7271 * breaks where needed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 pstart = message;
7273 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007274 msgheight = 0;
7275 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 do
7277 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007278 msgheight += fontHeight; /* at least one line */
7279
7280 /* Need to figure out where to break the string. The system does it
7281 * at a word boundary, which would mean we can't compute the number of
7282 * wrapped lines. */
7283 textWidth = 0;
7284 last_white = NULL;
7285 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007286 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007287#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007288 l = (*mb_ptr2len)(pend);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007289#else
7290 l = 1;
7291#endif
Bram Moolenaar1c465442017-03-12 20:10:05 +01007292 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007293 && textWidth > maxDialogWidth * 3 / 4)
7294 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007295 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007296 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007297 {
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007298 /* Line will wrap. */
7299 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007300 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007301 textWidth = 0;
7302
7303 if (last_white != NULL)
7304 {
7305 /* break the line just after a space */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007306 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007307 pend = last_white + 1;
7308 last_white = NULL;
7309 }
7310 ga_append(&ga, '\r');
7311 ga_append(&ga, '\n');
7312 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007313 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007314
7315 while (--l >= 0)
7316 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007317 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007318 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007320
7321 ga_append(&ga, '\r');
7322 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 pstart = pend + 1;
7324 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007325
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007326 if (ga.ga_data != NULL)
7327 message = ga.ga_data;
7328
Bram Moolenaar748bf032005-02-02 23:04:36 +00007329 messageWidth += 10; /* roundoff space */
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /* Add width of icon to dlgwidth, and some space */
Bram Moolenaara95d8232013-08-07 15:27:11 +02007332 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
7333 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334
7335 if (msgheight < DLG_ICON_HEIGHT)
7336 msgheight = DLG_ICON_HEIGHT;
7337
7338 /*
7339 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007340 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007342 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (!vertical)
7344 {
7345 // Place buttons horizontally if they fit.
7346 horizWidth = dlgPaddingX;
7347 pstart = tbuffer;
7348 i = 0;
7349 do
7350 {
7351 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7352 if (pend == NULL)
7353 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007354 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 if (textWidth < minButtonWidth)
7356 textWidth = minButtonWidth;
7357 textWidth += dlgPaddingX; /* Padding within button */
7358 buttonWidths[i] = textWidth;
7359 buttonPositions[i++] = horizWidth;
7360 horizWidth += textWidth + dlgPaddingX; /* Pad between buttons */
7361 pstart = pend + 1;
7362 } while (*pend != NUL);
7363
7364 if (horizWidth > maxDialogWidth)
7365 vertical = TRUE; // Too wide to fit on the screen.
7366 else if (horizWidth > dlgwidth)
7367 dlgwidth = horizWidth;
7368 }
7369
7370 if (vertical)
7371 {
7372 // Stack buttons vertically.
7373 pstart = tbuffer;
7374 do
7375 {
7376 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7377 if (pend == NULL)
7378 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007379 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 textWidth += dlgPaddingX; /* Padding within button */
7381 textWidth += DLG_VERT_PADDING_X * 2; /* Padding around button */
7382 if (textWidth > dlgwidth)
7383 dlgwidth = textWidth;
7384 pstart = pend + 1;
7385 } while (*pend != NUL);
7386 }
7387
7388 if (dlgwidth < DLG_MIN_WIDTH)
7389 dlgwidth = DLG_MIN_WIDTH; /* Don't allow a really thin dialog!*/
7390
7391 /* start to fill in the dlgtemplate information. addressing by WORDs */
7392 if (s_usenewlook)
7393 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7394 else
7395 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7396
7397 add_long(lStyle);
7398 add_long(0); // (lExtendedStyle)
7399 pnumitems = p; /*save where the number of items must be stored*/
7400 add_word(0); // NumberOfItems(will change later)
7401 add_word(10); // x
7402 add_word(10); // y
7403 add_word(PixelToDialogX(dlgwidth)); // cx
7404
7405 // Dialog height.
7406 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007407 dlgheight = msgheight + 2 * dlgPaddingY
7408 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 else
7410 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7411
7412 // Dialog needs to be taller if contains an edit box.
7413 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7414 if (textfield != NULL)
7415 dlgheight += editboxheight;
7416
Bram Moolenaara95d8232013-08-07 15:27:11 +02007417 /* Restrict the size to a maximum. Causes a scrollbar to show up. */
7418 if (dlgheight > maxDialogHeight)
7419 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007420 msgheight = msgheight - (dlgheight - maxDialogHeight);
7421 dlgheight = maxDialogHeight;
7422 scroll_flag = WS_VSCROLL;
7423 /* Make sure scrollbar doesn't appear in the middle of the dialog */
7424 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007425 }
7426
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 add_word(PixelToDialogY(dlgheight));
7428
7429 add_word(0); // Menu
7430 add_word(0); // Class
7431
7432 /* copy the title of the dialog */
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007433 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7434 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 p += nchar;
7436
7437 if (s_usenewlook)
7438 {
7439 /* do the font, since DS_3DLOOK doesn't work properly */
7440#ifdef USE_SYSMENU_FONT
7441 if (use_lfSysmenu)
7442 {
7443 /* point size */
7444 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7445 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007446 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 }
7448 else
7449#endif
7450 {
7451 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007452 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454 p += nchar;
7455 }
7456
7457 buttonYpos = msgheight + 2 * dlgPaddingY;
7458
7459 if (textfield != NULL)
7460 buttonYpos += editboxheight;
7461
7462 pstart = tbuffer;
7463 if (!vertical)
7464 horizWidth = (dlgwidth - horizWidth) / 2; /* Now it's X offset */
7465 for (i = 0; i < numButtons; i++)
7466 {
7467 /* get end of this button. */
7468 for ( pend = pstart;
7469 *pend && (*pend != DLG_BUTTON_SEP);
7470 pend++)
7471 ;
7472
7473 if (*pend)
7474 *pend = '\0';
7475
7476 /*
7477 * old NOTE:
7478 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7479 * the focus to the first tab-able button and in so doing makes that
7480 * the default!! Grrr. Workaround: Make the default button the only
7481 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7482 * he/she can use arrow keys.
7483 *
7484 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007485 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 * dialog. Also needed for when the textfield is the default control.
7487 * It appears to work now (perhaps not on Win95?).
7488 */
7489 if (vertical)
7490 {
7491 p = add_dialog_element(p,
7492 (i == dfltbutton
7493 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7494 PixelToDialogX(DLG_VERT_PADDING_X),
7495 PixelToDialogY(buttonYpos /* TBK */
7496 + 2 * fontHeight * i),
7497 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7498 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007499 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 else
7502 {
7503 p = add_dialog_element(p,
7504 (i == dfltbutton
7505 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7506 PixelToDialogX(horizWidth + buttonPositions[i]),
7507 PixelToDialogY(buttonYpos), /* TBK */
7508 PixelToDialogX(buttonWidths[i]),
7509 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007510 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 }
7512 pstart = pend + 1; /*next button*/
7513 }
7514 *pnumitems += numButtons;
7515
7516 /* Vim icon */
7517 p = add_dialog_element(p, SS_ICON,
7518 PixelToDialogX(dlgPaddingX),
7519 PixelToDialogY(dlgPaddingY),
7520 PixelToDialogX(DLG_ICON_WIDTH),
7521 PixelToDialogY(DLG_ICON_HEIGHT),
7522 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7523 dlg_icons[type]);
7524
Bram Moolenaar748bf032005-02-02 23:04:36 +00007525 /* Dialog message */
7526 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7527 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7528 PixelToDialogY(dlgPaddingY),
7529 (WORD)(PixelToDialogX(messageWidth) + 1),
7530 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007531 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 /* Edit box */
7534 if (textfield != NULL)
7535 {
7536 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7537 PixelToDialogX(2 * dlgPaddingX),
7538 PixelToDialogY(2 * dlgPaddingY + msgheight),
7539 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7540 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007541 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 *pnumitems += 1;
7543 }
7544
7545 *pnumitems += 2;
7546
7547 SelectFont(hdc, oldFont);
7548 DeleteObject(font);
7549 ReleaseDC(hwnd, hdc);
7550
7551 /* Let the dialog_callback() function know which button to make default
7552 * If we have an edit box, make that the default. We also need to tell
7553 * dialog_callback() if this dialog contains an edit box or not. We do
7554 * this by setting s_textfield if it does.
7555 */
7556 if (textfield != NULL)
7557 {
7558 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7559 s_textfield = textfield;
7560 }
7561 else
7562 {
7563 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7564 s_textfield = NULL;
7565 }
7566
7567 /* show the dialog box modally and get a return value */
7568 nchar = (int)DialogBoxIndirect(
7569 s_hinst,
7570 (LPDLGTEMPLATE)pdlgtemplate,
7571 s_hwnd,
7572 (DLGPROC)dialog_callback);
7573
7574 LocalFree(LocalHandle(pdlgtemplate));
7575 vim_free(tbuffer);
7576 vim_free(buttonWidths);
7577 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007578 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 /* Focus back to our window (for when MDI is used). */
7581 (void)SetFocus(s_hwnd);
7582
7583 return nchar;
7584}
7585
7586#endif /* FEAT_GUI_DIALOG */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588/*
7589 * Put a simple element (basic class) onto a dialog template in memory.
7590 * return a pointer to where the next item should be added.
7591 *
7592 * parameters:
7593 * lStyle = additional style flags
7594 * (be careful, NT3.51 & Win32s will ignore the new ones)
7595 * x,y = x & y positions IN DIALOG UNITS
7596 * w,h = width and height IN DIALOG UNITS
7597 * Id = ID used in messages
7598 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7599 * caption = usually text or resource name
7600 *
7601 * TODO: use the length information noted here to enable the dialog creation
7602 * routines to work out more exactly how much memory they need to alloc.
7603 */
7604 static PWORD
7605add_dialog_element(
7606 PWORD p,
7607 DWORD lStyle,
7608 WORD x,
7609 WORD y,
7610 WORD w,
7611 WORD h,
7612 WORD Id,
7613 WORD clss,
7614 const char *caption)
7615{
7616 int nchar;
7617
7618 p = lpwAlign(p); /* Align to dword boundary*/
7619 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7620 *p++ = LOWORD(lStyle);
7621 *p++ = HIWORD(lStyle);
7622 *p++ = 0; // LOWORD (lExtendedStyle)
7623 *p++ = 0; // HIWORD (lExtendedStyle)
7624 *p++ = x;
7625 *p++ = y;
7626 *p++ = w;
7627 *p++ = h;
7628 *p++ = Id; //9 or 10 words in all
7629
7630 *p++ = (WORD)0xffff;
7631 *p++ = clss; //2 more here
7632
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007633 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 p += nchar;
7635
7636 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7637
7638 return p; //total = 15+ (strlen(caption)) words
7639 // = 30 + 2(strlen(caption) bytes reqd
7640}
7641
7642
7643/*
7644 * Helper routine. Take an input pointer, return closest pointer that is
7645 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7646 */
7647 static LPWORD
7648lpwAlign(
7649 LPWORD lpIn)
7650{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007651 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007653 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 ul += 3;
7655 ul >>= 2;
7656 ul <<= 2;
7657 return (LPWORD)ul;
7658}
7659
7660/*
7661 * Helper routine. Takes second parameter as Ansi string, copies it to first
7662 * parameter as wide character (16-bits / char) string, and returns integer
7663 * number of wide characters (words) in string (including the trailing wide
7664 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007665 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7666 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 static int
7668nCopyAnsiToWideChar(
7669 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007670 LPSTR lpAnsiIn,
7671 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672{
7673 int nChar = 0;
7674#ifdef FEAT_MBYTE
7675 int len = lstrlen(lpAnsiIn) + 1; /* include NUL character */
7676 int i;
7677 WCHAR *wn;
7678
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007679 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {
7681 /* Not a codepage, use our own conversion function. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007682 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (wn != NULL)
7684 {
7685 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007686 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 vim_free(wn);
7688 }
7689 }
7690 if (nChar == 0)
7691 /* Use Win32 conversion function. */
7692 nChar = MultiByteToWideChar(
7693 enc_codepage > 0 ? enc_codepage : CP_ACP,
7694 MB_PRECOMPOSED,
7695 lpAnsiIn, len,
7696 lpWCStr, len);
7697 for (i = 0; i < nChar; ++i)
7698 if (lpWCStr[i] == (WORD)'\t') /* replace tabs with spaces */
7699 lpWCStr[i] = (WORD)' ';
7700#else
7701 do
7702 {
7703 if (*lpAnsiIn == '\t')
7704 *lpWCStr++ = (WORD)' ';
7705 else
7706 *lpWCStr++ = (WORD)*lpAnsiIn;
7707 nChar++;
7708 } while (*lpAnsiIn++);
7709#endif
7710
7711 return nChar;
7712}
7713
7714
7715#ifdef FEAT_TEAROFF
7716/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007717 * Lookup menu handle from "menu_id".
7718 */
7719 static HMENU
7720tearoff_lookup_menuhandle(
7721 vimmenu_T *menu,
7722 WORD menu_id)
7723{
7724 for ( ; menu != NULL; menu = menu->next)
7725 {
7726 if (menu->modes == 0) /* this menu has just been deleted */
7727 continue;
7728 if (menu_is_separator(menu->dname))
7729 continue;
7730 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7731 return menu->submenu_id;
7732 }
7733 return NULL;
7734}
7735
7736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 * The callback function for all the modeless dialogs that make up the
7738 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7739 * thinking its menus have been clicked), and go away when closed.
7740 */
7741 static LRESULT CALLBACK
7742tearoff_callback(
7743 HWND hwnd,
7744 UINT message,
7745 WPARAM wParam,
7746 LPARAM lParam)
7747{
7748 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007749 {
7750 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
7754 /* May show the mouse pointer again. */
7755 HandleMouseHide(message, lParam);
7756
7757 if (message == WM_COMMAND)
7758 {
7759 if ((WORD)(LOWORD(wParam)) & 0x8000)
7760 {
7761 POINT mp;
7762 RECT rect;
7763
7764 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7765 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007766 vimmenu_T *menu;
7767
7768 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007770 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7772 (int)rect.right - 8,
7773 (int)mp.y,
7774 (int)0, /*reserved param*/
7775 s_hwnd,
7776 NULL);
7777 /*
7778 * NOTE: The pop-up menu can eat the mouse up event.
7779 * We deal with this in normal.c.
7780 */
7781 }
7782 }
7783 else
7784 /* Pass on messages to the main Vim window */
7785 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7786 /*
7787 * Give main window the focus back: this is so after
7788 * choosing a tearoff button you can start typing again
7789 * straight away.
7790 */
7791 (void)SetFocus(s_hwnd);
7792 return TRUE;
7793 }
7794 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7795 {
7796 DestroyWindow(hwnd);
7797 return TRUE;
7798 }
7799
7800 /* When moved around, give main window the focus back. */
7801 if (message == WM_EXITSIZEMOVE)
7802 (void)SetActiveWindow(s_hwnd);
7803
7804 return FALSE;
7805}
7806#endif
7807
7808
7809/*
7810 * Decide whether to use the "new look" (small, non-bold font) or the "old
7811 * look" (big, clanky font) for dialogs, and work out a few values for use
7812 * later accordingly.
7813 */
7814 static void
7815get_dialog_font_metrics(void)
7816{
7817 HDC hdc;
7818 HFONT hfontTools = 0;
7819 DWORD dlgFontSize;
7820 SIZE size;
7821#ifdef USE_SYSMENU_FONT
7822 LOGFONT lfSysmenu;
7823#endif
7824
7825 s_usenewlook = FALSE;
7826
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007828 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7829 hfontTools = CreateFontIndirect(&lfSysmenu);
7830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7833 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7834
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007835 if (hfontTools)
7836 {
7837 hdc = GetDC(s_hwnd);
7838 SelectObject(hdc, hfontTools);
7839 /*
7840 * GetTextMetrics() doesn't return the right value in
7841 * tmAveCharWidth, so we have to figure out the dialog base units
7842 * ourselves.
7843 */
7844 GetTextExtentPoint(hdc,
7845 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7846 52, &size);
7847 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007849 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7850 s_dlgfntheight = (WORD)size.cy;
7851 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 }
7853
7854 if (!s_usenewlook)
7855 {
7856 dlgFontSize = GetDialogBaseUnits(); /* fall back to big old system*/
7857 s_dlgfntwidth = LOWORD(dlgFontSize);
7858 s_dlgfntheight = HIWORD(dlgFontSize);
7859 }
7860}
7861
7862#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7863/*
7864 * Create a pseudo-"tearoff menu" based on the child
7865 * items of a given menu pointer.
7866 */
7867 static void
7868gui_mch_tearoff(
7869 char_u *title,
7870 vimmenu_T *menu,
7871 int initX,
7872 int initY)
7873{
7874 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7875 int template_len;
7876 int nchar, textWidth, submenuWidth;
7877 DWORD lStyle;
7878 DWORD lExtendedStyle;
7879 WORD dlgwidth;
7880 WORD menuID;
7881 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007882 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 vimmenu_T *the_menu = menu;
7884 HWND hwnd;
7885 HDC hdc;
7886 HFONT font, oldFont;
7887 int col, spaceWidth, len;
7888 int columnWidths[2];
7889 char_u *label, *text;
7890 int acLen = 0;
7891 int nameLen;
7892 int padding0, padding1, padding2 = 0;
7893 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007894 int x;
7895 int y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896#ifdef USE_SYSMENU_FONT
7897 LOGFONT lfSysmenu;
7898 int use_lfSysmenu = FALSE;
7899#endif
7900
7901 /*
7902 * If this menu is already torn off, move it to the mouse position.
7903 */
7904 if (IsWindow(menu->tearoff_handle))
7905 {
7906 POINT mp;
7907 if (GetCursorPos((LPPOINT)&mp))
7908 {
7909 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7910 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7911 }
7912 return;
7913 }
7914
7915 /*
7916 * Create a new tearoff.
7917 */
7918 if (*title == MNU_HIDDEN_CHAR)
7919 title++;
7920
7921 /* Allocate memory to store the dialog template. It's made bigger when
7922 * needed. */
7923 template_len = DLG_ALLOC_SIZE;
7924 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7925 if (p == NULL)
7926 return;
7927
7928 hwnd = GetDesktopWindow();
7929 hdc = GetWindowDC(hwnd);
7930#ifdef USE_SYSMENU_FONT
7931 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7932 {
7933 font = CreateFontIndirect(&lfSysmenu);
7934 use_lfSysmenu = TRUE;
7935 }
7936 else
7937#endif
7938 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7939 VARIABLE_PITCH , DLG_FONT_NAME);
7940 if (s_usenewlook)
7941 oldFont = SelectFont(hdc, font);
7942 else
7943 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7944
7945 /* Calculate width of a single space. Used for padding columns to the
7946 * right width. */
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007947 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948
7949 /* Figure out max width of the text column, the accelerator column and the
7950 * optional submenu column. */
7951 submenuWidth = 0;
7952 for (col = 0; col < 2; col++)
7953 {
7954 columnWidths[col] = 0;
7955 for (pmenu = menu->children; pmenu != NULL; pmenu = pmenu->next)
7956 {
7957 /* Use "dname" here to compute the width of the visible text. */
7958 text = (col == 0) ? pmenu->dname : pmenu->actext;
7959 if (text != NULL && *text != NUL)
7960 {
7961 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7962 if (textWidth > columnWidths[col])
7963 columnWidths[col] = textWidth;
7964 }
7965 if (pmenu->children != NULL)
7966 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7967 }
7968 }
7969 if (columnWidths[1] == 0)
7970 {
7971 /* no accelerators */
7972 if (submenuWidth != 0)
7973 columnWidths[0] += submenuWidth;
7974 else
7975 columnWidths[0] += spaceWidth;
7976 }
7977 else
7978 {
7979 /* there is an accelerator column */
7980 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7981 columnWidths[1] += submenuWidth;
7982 }
7983
7984 /*
7985 * Now find the total width of our 'menu'.
7986 */
7987 textWidth = columnWidths[0] + columnWidths[1];
7988 if (submenuWidth != 0)
7989 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007990 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7992 textWidth += submenuWidth;
7993 }
7994 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7995 if (textWidth > dlgwidth)
7996 dlgwidth = textWidth;
7997 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7998
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 /* start to fill in the dlgtemplate information. addressing by WORDs */
8000 if (s_usenewlook)
8001 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
8002 else
8003 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
8004
8005 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8006 *p++ = LOWORD(lStyle);
8007 *p++ = HIWORD(lStyle);
8008 *p++ = LOWORD(lExtendedStyle);
8009 *p++ = HIWORD(lExtendedStyle);
8010 pnumitems = p; /* save where the number of items must be stored */
8011 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008012 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008014 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 else
8016 *p++ = PixelToDialogX(initX); // x
8017 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008018 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 else
8020 *p++ = PixelToDialogY(initY); // y
8021 *p++ = PixelToDialogX(dlgwidth); // cx
8022 ptrueheight = p;
8023 *p++ = 0; // dialog height: changed later anyway
8024 *p++ = 0; // Menu
8025 *p++ = 0; // Class
8026
8027 /* copy the title of the dialog */
8028 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008029 ? (LPSTR)title
8030 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 p += nchar;
8032
8033 if (s_usenewlook)
8034 {
8035 /* do the font, since DS_3DLOOK doesn't work properly */
8036#ifdef USE_SYSMENU_FONT
8037 if (use_lfSysmenu)
8038 {
8039 /* point size */
8040 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8041 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008042 nchar = nCopyAnsiToWideChar(p, lfSysmenu.lfFaceName, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 else
8045#endif
8046 {
8047 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008048 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050 p += nchar;
8051 }
8052
8053 /*
8054 * Loop over all the items in the menu.
8055 * But skip over the tearbar.
8056 */
8057 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8058 menu = menu->children->next;
8059 else
8060 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008061 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 for ( ; menu != NULL; menu = menu->next)
8063 {
8064 if (menu->modes == 0) /* this menu has just been deleted */
8065 continue;
8066 if (menu_is_separator(menu->dname))
8067 {
8068 sepPadding += 3;
8069 continue;
8070 }
8071
8072 /* Check if there still is plenty of room in the template. Make it
8073 * larger when needed. */
8074 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8075 {
8076 WORD *newp;
8077
8078 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8079 if (newp != NULL)
8080 {
8081 template_len += 4096;
8082 mch_memmove(newp, pdlgtemplate,
8083 (char *)p - (char *)pdlgtemplate);
8084 p = newp + (p - pdlgtemplate);
8085 pnumitems = newp + (pnumitems - pdlgtemplate);
8086 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8087 LocalFree(LocalHandle(pdlgtemplate));
8088 pdlgtemplate = newp;
8089 }
8090 }
8091
8092 /* Figure out minimal length of this menu label. Use "name" for the
8093 * actual text, "dname" for estimating the displayed size. "name"
8094 * has "&a" for mnemonic and includes the accelerator. */
8095 len = nameLen = (int)STRLEN(menu->name);
8096 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8097 (int)STRLEN(menu->dname))) / spaceWidth;
8098 len += padding0;
8099
8100 if (menu->actext != NULL)
8101 {
8102 acLen = (int)STRLEN(menu->actext);
8103 len += acLen;
8104 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8105 }
8106 else
8107 textWidth = 0;
8108 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8109 len += padding1;
8110
8111 if (menu->children == NULL)
8112 {
8113 padding2 = submenuWidth / spaceWidth;
8114 len += padding2;
8115 menuID = (WORD)(menu->id);
8116 }
8117 else
8118 {
8119 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008120 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
8122
8123 /* Allocate menu label and fill it in */
8124 text = label = alloc((unsigned)len + 1);
8125 if (label == NULL)
8126 break;
8127
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008128 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 text = vim_strchr(text, TAB); /* stop at TAB before actext */
8130 if (text == NULL)
8131 text = label + nameLen; /* no actext, use whole name */
8132 while (padding0-- > 0)
8133 *text++ = ' ';
8134 if (menu->actext != NULL)
8135 {
8136 STRNCPY(text, menu->actext, acLen);
8137 text += acLen;
8138 }
8139 while (padding1-- > 0)
8140 *text++ = ' ';
8141 if (menu->children != NULL)
8142 {
8143 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8144 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8145 }
8146 else
8147 {
8148 while (padding2-- > 0)
8149 *text++ = ' ';
8150 }
8151 *text = NUL;
8152
8153 /*
8154 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8155 * W95/NT4 it makes the tear-off look more like a menu.
8156 */
8157 p = add_dialog_element(p,
8158 BS_PUSHBUTTON|BS_LEFT,
8159 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8160 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8161 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8162 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008163 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 vim_free(label);
8165 (*pnumitems)++;
8166 }
8167
8168 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8169
8170
8171 /* show modelessly */
Bram Moolenaar66857f42017-10-22 16:43:20 +02008172 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 s_hinst,
8174 (LPDLGTEMPLATE)pdlgtemplate,
8175 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008176 (DLGPROC)tearoff_callback,
8177 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 LocalFree(LocalHandle(pdlgtemplate));
8180 SelectFont(hdc, oldFont);
8181 DeleteObject(font);
8182 ReleaseDC(hwnd, hdc);
8183
8184 /*
8185 * Reassert ourselves as the active window. This is so that after creating
8186 * a tearoff, the user doesn't have to click with the mouse just to start
8187 * typing again!
8188 */
8189 (void)SetActiveWindow(s_hwnd);
8190
8191 /* make sure the right buttons are enabled */
8192 force_menu_update = TRUE;
8193}
8194#endif
8195
8196#if defined(FEAT_TOOLBAR) || defined(PROTO)
8197#include "gui_w32_rc.h"
8198
8199/* This not defined in older SDKs */
8200# ifndef TBSTYLE_FLAT
8201# define TBSTYLE_FLAT 0x0800
8202# endif
8203
8204/*
8205 * Create the toolbar, initially unpopulated.
8206 * (just like the menu, there are no defaults, it's all
8207 * set up through menu.vim)
8208 */
8209 static void
8210initialise_toolbar(void)
8211{
8212 InitCommonControls();
8213 s_toolbarhwnd = CreateToolbarEx(
8214 s_hwnd,
8215 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8216 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008217 31, //number of images in initial bitmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 s_hinst,
8219 IDR_TOOLBAR1, // id of initial bitmap
8220 NULL,
8221 0, // initial number of buttons
8222 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8223 TOOLBAR_BUTTON_HEIGHT,
8224 TOOLBAR_BUTTON_WIDTH,
8225 TOOLBAR_BUTTON_HEIGHT,
8226 sizeof(TBBUTTON)
8227 );
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008228 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
8231}
8232
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008233 static LRESULT CALLBACK
8234toolbar_wndproc(
8235 HWND hwnd,
8236 UINT uMsg,
8237 WPARAM wParam,
8238 LPARAM lParam)
8239{
8240 HandleMouseHide(uMsg, lParam);
8241 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8242}
8243
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 static int
8245get_toolbar_bitmap(vimmenu_T *menu)
8246{
8247 int i = -1;
8248
8249 /*
8250 * Check user bitmaps first, unless builtin is specified.
8251 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008252 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {
8254 char_u fname[MAXPATHL];
8255 HANDLE hbitmap = NULL;
8256
8257 if (menu->iconfile != NULL)
8258 {
8259 gui_find_iconfile(menu->iconfile, fname, "bmp");
8260 hbitmap = LoadImage(
8261 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008262 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 IMAGE_BITMAP,
8264 TOOLBAR_BUTTON_WIDTH,
8265 TOOLBAR_BUTTON_HEIGHT,
8266 LR_LOADFROMFILE |
8267 LR_LOADMAP3DCOLORS
8268 );
8269 }
8270
8271 /*
8272 * If the LoadImage call failed, or the "icon=" file
8273 * didn't exist or wasn't specified, try the menu name
8274 */
8275 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008276 && (gui_find_bitmap(
8277#ifdef FEAT_MULTI_LANG
8278 menu->en_dname != NULL ? menu->en_dname :
8279#endif
8280 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 hbitmap = LoadImage(
8282 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008283 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 IMAGE_BITMAP,
8285 TOOLBAR_BUTTON_WIDTH,
8286 TOOLBAR_BUTTON_HEIGHT,
8287 LR_LOADFROMFILE |
8288 LR_LOADMAP3DCOLORS
8289 );
8290
8291 if (hbitmap != NULL)
8292 {
8293 TBADDBITMAP tbAddBitmap;
8294
8295 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008296 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297
8298 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8299 (WPARAM)1, (LPARAM)&tbAddBitmap);
8300 /* i will be set to -1 if it fails */
8301 }
8302 }
8303 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8304 i = menu->iconidx;
8305
8306 return i;
8307}
8308#endif
8309
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008310#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8311 static void
8312initialise_tabline(void)
8313{
8314 InitCommonControls();
8315
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008316 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008318 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8319 CW_USEDEFAULT, s_hwnd, NULL, s_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008320 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008321
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008322 gui.tabline_height = TABLINE_HEIGHT;
8323
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008324# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008325 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008326# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008327}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008328
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008329/*
8330 * Get tabpage_T from POINT.
8331 */
8332 static tabpage_T *
8333GetTabFromPoint(
8334 HWND hWnd,
8335 POINT pt)
8336{
8337 tabpage_T *ptp = NULL;
8338
8339 if (gui_mch_showing_tabline())
8340 {
8341 TCHITTESTINFO htinfo;
8342 htinfo.pt = pt;
8343 /* ignore if a window under cusor is not tabcontrol. */
8344 if (s_tabhwnd == hWnd)
8345 {
8346 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8347 if (idx != -1)
8348 ptp = find_tabpage(idx + 1);
8349 }
8350 }
8351 return ptp;
8352}
8353
8354static POINT s_pt = {0, 0};
8355static HCURSOR s_hCursor = NULL;
8356
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008357 static LRESULT CALLBACK
8358tabline_wndproc(
8359 HWND hwnd,
8360 UINT uMsg,
8361 WPARAM wParam,
8362 LPARAM lParam)
8363{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008364 POINT pt;
8365 tabpage_T *tp;
8366 RECT rect;
8367 int nCenter;
8368 int idx0;
8369 int idx1;
8370
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008371 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008372
8373 switch (uMsg)
8374 {
8375 case WM_LBUTTONDOWN:
8376 {
8377 s_pt.x = GET_X_LPARAM(lParam);
8378 s_pt.y = GET_Y_LPARAM(lParam);
8379 SetCapture(hwnd);
8380 s_hCursor = GetCursor(); /* backup default cursor */
8381 break;
8382 }
8383 case WM_MOUSEMOVE:
8384 if (GetCapture() == hwnd
8385 && ((wParam & MK_LBUTTON)) != 0)
8386 {
8387 pt.x = GET_X_LPARAM(lParam);
8388 pt.y = s_pt.y;
8389 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8390 {
8391 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8392
8393 tp = GetTabFromPoint(hwnd, pt);
8394 if (tp != NULL)
8395 {
8396 idx0 = tabpage_index(curtab) - 1;
8397 idx1 = tabpage_index(tp) - 1;
8398
8399 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8400 nCenter = rect.left + (rect.right - rect.left) / 2;
8401
8402 /* Check if the mouse cursor goes over the center of
8403 * the next tab to prevent "flickering". */
8404 if ((idx0 < idx1) && (nCenter < pt.x))
8405 {
8406 tabpage_move(idx1 + 1);
8407 update_screen(0);
8408 }
8409 else if ((idx1 < idx0) && (pt.x < nCenter))
8410 {
8411 tabpage_move(idx1);
8412 update_screen(0);
8413 }
8414 }
8415 }
8416 }
8417 break;
8418 case WM_LBUTTONUP:
8419 {
8420 if (GetCapture() == hwnd)
8421 {
8422 SetCursor(s_hCursor);
8423 ReleaseCapture();
8424 }
8425 break;
8426 }
8427 default:
8428 break;
8429 }
8430
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008431 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8432}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008433#endif
8434
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8436/*
8437 * Make the GUI window come to the foreground.
8438 */
8439 void
8440gui_mch_set_foreground(void)
8441{
8442 if (IsIconic(s_hwnd))
8443 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8444 SetForegroundWindow(s_hwnd);
8445}
8446#endif
8447
8448#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8449 static void
8450dyn_imm_load(void)
8451{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008452 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 if (hLibImm == NULL)
8454 return;
8455
8456 pImmGetCompositionStringA
8457 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8458 pImmGetCompositionStringW
8459 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8460 pImmGetContext
8461 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8462 pImmAssociateContext
8463 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8464 pImmReleaseContext
8465 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8466 pImmGetOpenStatus
8467 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8468 pImmSetOpenStatus
8469 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
8470 pImmGetCompositionFont
8471 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontA");
8472 pImmSetCompositionFont
8473 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontA");
8474 pImmSetCompositionWindow
8475 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8476 pImmGetConversionStatus
8477 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008478 pImmSetConversionStatus
8479 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480
8481 if ( pImmGetCompositionStringA == NULL
8482 || pImmGetCompositionStringW == NULL
8483 || pImmGetContext == NULL
8484 || pImmAssociateContext == NULL
8485 || pImmReleaseContext == NULL
8486 || pImmGetOpenStatus == NULL
8487 || pImmSetOpenStatus == NULL
8488 || pImmGetCompositionFont == NULL
8489 || pImmSetCompositionFont == NULL
8490 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008491 || pImmGetConversionStatus == NULL
8492 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 FreeLibrary(hLibImm);
8495 hLibImm = NULL;
8496 pImmGetContext = NULL;
8497 return;
8498 }
8499
8500 return;
8501}
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503#endif
8504
8505#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8506
8507# ifdef FEAT_XPM_W32
8508# define IMAGE_XPM 100
8509# endif
8510
8511typedef struct _signicon_t
8512{
8513 HANDLE hImage;
8514 UINT uType;
8515#ifdef FEAT_XPM_W32
8516 HANDLE hShape; /* Mask bitmap handle */
8517#endif
8518} signicon_t;
8519
8520 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008521gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 signicon_t *sign;
8524 int x, y, w, h;
8525
8526 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8527 return;
8528
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008529#if defined(FEAT_DIRECTX)
8530 if (IS_ENABLE_DIRECTX())
8531 DWriteContext_Flush(s_dwc);
8532#endif
8533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 x = TEXT_X(col);
8535 y = TEXT_Y(row);
8536 w = gui.char_width * 2;
8537 h = gui.char_height;
8538 switch (sign->uType)
8539 {
8540 case IMAGE_BITMAP:
8541 {
8542 HDC hdcMem;
8543 HBITMAP hbmpOld;
8544
8545 hdcMem = CreateCompatibleDC(s_hdc);
8546 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8547 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8548 SelectObject(hdcMem, hbmpOld);
8549 DeleteDC(hdcMem);
8550 }
8551 break;
8552 case IMAGE_ICON:
8553 case IMAGE_CURSOR:
8554 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8555 break;
8556#ifdef FEAT_XPM_W32
8557 case IMAGE_XPM:
8558 {
8559 HDC hdcMem;
8560 HBITMAP hbmpOld;
8561
8562 hdcMem = CreateCompatibleDC(s_hdc);
8563 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
8564 /* Make hole */
8565 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8566
8567 SelectObject(hdcMem, sign->hImage);
8568 /* Paint sign */
8569 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8570 SelectObject(hdcMem, hbmpOld);
8571 DeleteDC(hdcMem);
8572 }
8573 break;
8574#endif
8575 }
8576}
8577
8578 static void
8579close_signicon_image(signicon_t *sign)
8580{
8581 if (sign)
8582 switch (sign->uType)
8583 {
8584 case IMAGE_BITMAP:
8585 DeleteObject((HGDIOBJ)sign->hImage);
8586 break;
8587 case IMAGE_CURSOR:
8588 DestroyCursor((HCURSOR)sign->hImage);
8589 break;
8590 case IMAGE_ICON:
8591 DestroyIcon((HICON)sign->hImage);
8592 break;
8593#ifdef FEAT_XPM_W32
8594 case IMAGE_XPM:
8595 DeleteObject((HBITMAP)sign->hImage);
8596 DeleteObject((HBITMAP)sign->hShape);
8597 break;
8598#endif
8599 }
8600}
8601
8602 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008603gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 signicon_t sign, *psign;
8606 char_u *ext;
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 sign.hImage = NULL;
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008609 ext = signfile + STRLEN(signfile) - 4; /* get extension */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (ext > signfile)
8611 {
8612 int do_load = 1;
8613
8614 if (!STRICMP(ext, ".bmp"))
8615 sign.uType = IMAGE_BITMAP;
8616 else if (!STRICMP(ext, ".ico"))
8617 sign.uType = IMAGE_ICON;
8618 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8619 sign.uType = IMAGE_CURSOR;
8620 else
8621 do_load = 0;
8622
8623 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008624 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 gui.char_width * 2, gui.char_height,
8626 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
8627#ifdef FEAT_XPM_W32
8628 if (!STRICMP(ext, ".xpm"))
8629 {
8630 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008631 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8632 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 }
8634#endif
8635 }
8636
8637 psign = NULL;
8638 if (sign.hImage && (psign = (signicon_t *)alloc(sizeof(signicon_t)))
8639 != NULL)
8640 *psign = sign;
8641
8642 if (!psign)
8643 {
8644 if (sign.hImage)
8645 close_signicon_image(&sign);
8646 EMSG(_(e_signdata));
8647 }
8648 return (void *)psign;
8649
8650}
8651
8652 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008653gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654{
8655 if (sign)
8656 {
8657 close_signicon_image((signicon_t *)sign);
8658 vim_free(sign);
8659 }
8660}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008663#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665/* BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008666 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008668 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8670 * to get current mouse position).
8671 *
8672 * Trying to use as more Windows services as possible, and as less
8673 * IE version as possible :)).
8674 *
8675 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8676 * BalloonEval struct.
8677 * 2) Enable/Disable simply create/kill BalloonEval Timer
8678 * 3) When there was enough inactivity, timer procedure posts
8679 * async request to debugger
8680 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8681 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008682 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 */
8684
Bram Moolenaar45360022005-07-21 21:08:21 +00008685/*
8686 * determine whether installed Common Controls support multiline tooltips
8687 * (i.e. their version is >= 4.70
8688 */
8689 int
8690multiline_balloon_available(void)
8691{
8692 HINSTANCE hDll;
8693 static char comctl_dll[] = "comctl32.dll";
8694 static int multiline_tip = MAYBE;
8695
8696 if (multiline_tip != MAYBE)
8697 return multiline_tip;
8698
8699 hDll = GetModuleHandle(comctl_dll);
8700 if (hDll != NULL)
8701 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008702 DLLGETVERSIONPROC pGetVer;
8703 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008704
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008705 if (pGetVer != NULL)
8706 {
8707 DLLVERSIONINFO dvi;
8708 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008709
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008710 ZeroMemory(&dvi, sizeof(dvi));
8711 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008712
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008713 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008714
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008715 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008716 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008717 || (dvi.dwMajorVersion == 4
8718 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008719 {
8720 multiline_tip = TRUE;
8721 return multiline_tip;
8722 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008723 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008724 else
8725 {
8726 /* there is chance we have ancient CommCtl 4.70
8727 which doesn't export DllGetVersion */
8728 DWORD dwHandle = 0;
8729 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8730 if (len > 0)
8731 {
8732 VS_FIXEDFILEINFO *ver;
8733 UINT vlen = 0;
8734 void *data = alloc(len);
8735
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008736 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008737 && GetFileVersionInfo(comctl_dll, 0, len, data)
8738 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8739 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008740 && HIWORD(ver->dwFileVersionMS) > 4)
8741 || ((HIWORD(ver->dwFileVersionMS) == 4
8742 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008743 {
8744 vim_free(data);
8745 multiline_tip = TRUE;
8746 return multiline_tip;
8747 }
8748 vim_free(data);
8749 }
8750 }
8751 }
8752 multiline_tip = FALSE;
8753 return multiline_tip;
8754}
8755
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008757make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
Bram Moolenaar45360022005-07-21 21:08:21 +00008759 TOOLINFO *pti;
8760 int ToolInfoSize;
8761
8762 if (multiline_balloon_available() == TRUE)
8763 ToolInfoSize = sizeof(TOOLINFO_NEW);
8764 else
8765 ToolInfoSize = sizeof(TOOLINFO);
8766
8767 pti = (TOOLINFO *)alloc(ToolInfoSize);
8768 if (pti == NULL)
8769 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770
8771 beval->balloon = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS,
8772 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8773 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
8774 beval->target, NULL, s_hinst, NULL);
8775
8776 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8777 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8778
Bram Moolenaar45360022005-07-21 21:08:21 +00008779 pti->cbSize = ToolInfoSize;
8780 pti->uFlags = TTF_SUBCLASS;
8781 pti->hwnd = beval->target;
8782 pti->hinst = 0; /* Don't use string resources */
8783 pti->uId = ID_BEVAL_TOOLTIP;
8784
8785 if (multiline_balloon_available() == TRUE)
8786 {
8787 RECT rect;
8788 TOOLINFO_NEW *ptin = (TOOLINFO_NEW *)pti;
8789 pti->lpszText = LPSTR_TEXTCALLBACK;
8790 ptin->lParam = (LPARAM)text;
8791 if (GetClientRect(s_textArea, &rect)) /* switch multiline tooltips on */
8792 SendMessage(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8793 (LPARAM)rect.right);
8794 }
8795 else
8796 pti->lpszText = text; /* do this old way */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798 /* Limit ballooneval bounding rect to CursorPos neighbourhood */
Bram Moolenaar45360022005-07-21 21:08:21 +00008799 pti->rect.left = pt.x - 3;
8800 pti->rect.top = pt.y - 3;
8801 pti->rect.right = pt.x + 3;
8802 pti->rect.bottom = pt.y + 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803
Bram Moolenaar45360022005-07-21 21:08:21 +00008804 SendMessage(beval->balloon, TTM_ADDTOOL, 0, (LPARAM)pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 /* Make tooltip appear sooner */
8806 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008807 /* I've performed some tests and it seems the longest possible life time
8808 * of tooltip is 30 seconds */
8809 SendMessage(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 /*
8811 * HACK: force tooltip to appear, because it'll not appear until
8812 * first mouse move. D*mn M$
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008813 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 */
Bram Moolenaarb52e5322008-01-05 12:15:52 +00008815 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
Bram Moolenaar45360022005-07-21 21:08:21 +00008817 vim_free(pti);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
8820 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008821delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008823 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824}
8825
8826 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008827BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008828 HWND hwnd UNUSED,
8829 UINT uMsg UNUSED,
8830 UINT_PTR idEvent UNUSED,
8831 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832{
8833 POINT pt;
8834 RECT rect;
8835
8836 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8837 return;
8838
8839 GetCursorPos(&pt);
8840 if (WindowFromPoint(pt) != s_textArea)
8841 return;
8842
8843 ScreenToClient(s_textArea, &pt);
8844 GetClientRect(s_textArea, &rect);
8845 if (!PtInRect(&rect, pt))
8846 return;
8847
8848 if (LastActivity > 0
8849 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8850 && (cur_beval->showState != ShS_PENDING
8851 || abs(cur_beval->x - pt.x) > 3
8852 || abs(cur_beval->y - pt.y) > 3))
8853 {
8854 /* Pointer resting in one place long enough, it's time to show
8855 * the tooltip. */
8856 cur_beval->showState = ShS_PENDING;
8857 cur_beval->x = pt.x;
8858 cur_beval->y = pt.y;
8859
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008860 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861
8862 if (cur_beval->msgCB != NULL)
8863 (*cur_beval->msgCB)(cur_beval, 0);
8864 }
8865}
8866
8867 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008868gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008870 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008872 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873}
8874
8875 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008876gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008878 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 if (beval == NULL)
8880 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008881 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008882 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008883 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
8886 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008887gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008890
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008891 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 if (beval->showState == ShS_SHOWING)
8893 return;
8894 GetCursorPos(&pt);
8895 ScreenToClient(s_textArea, &pt);
8896
8897 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01008899 /* cursor is still here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 gui_mch_disable_beval_area(cur_beval);
8901 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008902 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008904 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905}
8906
8907 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008908gui_mch_create_beval_area(
8909 void *target, /* ignored, always use s_textArea */
8910 char_u *mesg,
8911 void (*mesgCB)(BalloonEval *, int),
8912 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913{
8914 /* partially stolen from gui_beval.c */
8915 BalloonEval *beval;
8916
8917 if (mesg != NULL && mesgCB != NULL)
8918 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01008919 IEMSG(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 return NULL;
8921 }
8922
Bram Moolenaarca4b6132018-06-28 12:05:11 +02008923 beval = (BalloonEval *)alloc_clear(sizeof(BalloonEval));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 if (beval != NULL)
8925 {
8926 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927
8928 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 beval->msg = mesg;
8930 beval->msgCB = mesgCB;
8931 beval->clientData = clientData;
8932
8933 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 cur_beval = beval;
8935
8936 if (p_beval)
8937 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 }
8939 return beval;
8940}
8941
8942 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008943Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944{
8945 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) /* it is not our tooltip */
8946 return;
8947
8948 if (cur_beval != NULL)
8949 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008950 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008952 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008953 // TRACE0("TTN_SHOW {{{");
8954 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008955 break;
8956 case TTN_POP: /* Before tooltip disappear */
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008957 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 delete_tooltip(cur_beval);
8959 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008960 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961
8962 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008963 break;
8964 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008965 {
8966 /* if you get there then we have new common controls */
8967 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8968 info->lpszText = (LPSTR)info->lParam;
8969 info->uFlags |= TTF_DI_SETITEM;
8970 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008971 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 }
8973 }
8974}
8975
8976 static void
8977TrackUserActivity(UINT uMsg)
8978{
8979 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8980 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8981 LastActivity = GetTickCount();
8982}
8983
8984 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008985gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
Bram Moolenaar04958cb2018-06-23 19:23:02 +02008987#ifdef FEAT_VARTABS
8988 if (beval->vts)
8989 vim_free(beval->vts);
8990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 vim_free(beval);
8992}
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008993#endif /* FEAT_BEVAL_GUI */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994
8995#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8996/*
8997 * We have multiple signs to draw at the same location. Draw the
8998 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8999 */
9000 void
9001netbeans_draw_multisign_indicator(int row)
9002{
9003 int i;
9004 int y;
9005 int x;
9006
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009007 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009008 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009009
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 x = 0;
9011 y = TEXT_Y(row);
9012
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009013#if defined(FEAT_DIRECTX)
9014 if (IS_ENABLE_DIRECTX())
9015 DWriteContext_Flush(s_dwc);
9016#endif
9017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 for (i = 0; i < gui.char_height - 3; i++)
9019 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9020
9021 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9022 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9023 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9024 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9025 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9026 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9027 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9028}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009029#endif