blob: 46358dc13194acebcb2f8d99a8706b1b0690a6cc [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 * GUI support by Robert Webb
5 *
6 * Do ":help uganda" in Vim to read copying and usage conditions.
7 * Do ":help credits" in Vim to see a list of people who contributed.
8 * See README.txt for an overview of the Vim source code.
9 */
10/*
11 * Windows GUI.
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * GUI support for Microsoft Windows, aka Win32. Also for Win64.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * George V. Reilly <george@reilly.org> wrote the original Win32 GUI.
16 * Robert Webb reworked it to use the existing GUI stuff and added menu,
17 * scrollbars, etc.
18 *
19 * Note: Clipboard stuff, for cutting and pasting text to other windows, is in
Bram Moolenaarcde88542015-08-11 19:14:00 +020020 * winclip.c. (It can also be done from the terminal version).
Bram Moolenaar071d4272004-06-13 20:20:40 +000021 *
22 * TODO: Some of the function signatures ought to be updated for Win64;
23 * e.g., replace LONG with LONG_PTR, etc.
24 */
25
Bram Moolenaar78e17622007-08-30 10:26:19 +000026#include "vim.h"
27
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020028#if defined(FEAT_DIRECTX)
29# include "gui_dwrite.h"
30#endif
31
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010032#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020033static DWriteContext *s_dwc = NULL;
34static int s_directx_enabled = 0;
35static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010036# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010037static int directx_enabled(void);
38static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010039#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020040
Bram Moolenaar065bbac2016-02-20 13:08:46 +010041#ifdef FEAT_MENU
42static int gui_mswin_get_menu_height(int fix_window);
43#endif
44
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020045#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
46 int
47gui_mch_set_rendering_options(char_u *s)
48{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010049# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020050 char_u *p, *q;
51
52 int dx_enable = 0;
53 int dx_flags = 0;
54 float dx_gamma = 0.0f;
55 float dx_contrast = 0.0f;
56 float dx_level = 0.0f;
57 int dx_geom = 0;
58 int dx_renmode = 0;
59 int dx_taamode = 0;
60
Bram Moolenaar734a8672019-12-02 22:49:38 +010061 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020062 for (p = s; p != NULL && *p != NUL; )
63 {
64 char_u item[256];
65 char_u name[128];
66 char_u value[128];
67
Bram Moolenaarcde88542015-08-11 19:14:00 +020068 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020069 if (p == NULL)
70 break;
71 q = &item[0];
72 copy_option_part(&q, name, sizeof(name), ":");
73 if (q == NULL)
74 return FAIL;
75 copy_option_part(&q, value, sizeof(value), ":");
76
77 if (STRCMP(name, "type") == 0)
78 {
79 if (STRCMP(value, "directx") == 0)
80 dx_enable = 1;
81 else
82 return FAIL;
83 }
84 else if (STRCMP(name, "gamma") == 0)
85 {
86 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010087 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020088 }
89 else if (STRCMP(name, "contrast") == 0)
90 {
91 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010092 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020093 }
94 else if (STRCMP(name, "level") == 0)
95 {
96 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010097 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020098 }
99 else if (STRCMP(name, "geom") == 0)
100 {
101 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100102 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200103 if (dx_geom < 0 || dx_geom > 2)
104 return FAIL;
105 }
106 else if (STRCMP(name, "renmode") == 0)
107 {
108 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100109 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200110 if (dx_renmode < 0 || dx_renmode > 6)
111 return FAIL;
112 }
113 else if (STRCMP(name, "taamode") == 0)
114 {
115 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100116 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200117 if (dx_taamode < 0 || dx_taamode > 3)
118 return FAIL;
119 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100120 else if (STRCMP(name, "scrlines") == 0)
121 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100122 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100123 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200124 else
125 return FAIL;
126 }
127
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100128 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100129 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100130
Bram Moolenaar734a8672019-12-02 22:49:38 +0100131 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 if (dx_enable)
133 {
134 if (!directx_enabled())
135 return FAIL;
136 DWriteContext_SetRenderingParams(s_dwc, NULL);
137 if (dx_flags)
138 {
139 DWriteRenderingParams param;
140 DWriteContext_GetRenderingParams(s_dwc, &param);
141 if (dx_flags & (1 << 0))
142 param.gamma = dx_gamma;
143 if (dx_flags & (1 << 1))
144 param.enhancedContrast = dx_contrast;
145 if (dx_flags & (1 << 2))
146 param.clearTypeLevel = dx_level;
147 if (dx_flags & (1 << 3))
148 param.pixelGeometry = dx_geom;
149 if (dx_flags & (1 << 4))
150 param.renderingMode = dx_renmode;
151 if (dx_flags & (1 << 5))
152 param.textAntialiasMode = dx_taamode;
153 DWriteContext_SetRenderingParams(s_dwc, &param);
154 }
155 }
156 s_directx_enabled = dx_enable;
157
158 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100159# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200160 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100161# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200162}
163#endif
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * These are new in Windows ME/XP, only defined in recent compilers.
167 */
168#ifndef HANDLE_WM_XBUTTONUP
169# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
170 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
171#endif
172#ifndef HANDLE_WM_XBUTTONDOWN
173# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
174 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
175#endif
176#ifndef HANDLE_WM_XBUTTONDBLCLK
177# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100181
Bram Moolenaar734a8672019-12-02 22:49:38 +0100182#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100183#ifdef DEBUG
184# include <tchar.h>
185#endif
186
Bram Moolenaar734a8672019-12-02 22:49:38 +0100187// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100188#ifndef PROTO
189
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100190# ifndef __MINGW32__
191# include <shellapi.h>
192# endif
193# if defined(FEAT_TOOLBAR) || defined(FEAT_BEVAL_GUI) || defined(FEAT_GUI_TABLINE)
194# include <commctrl.h>
195# endif
196# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifdef GLOBAL_IME
199# include "glbl_ime.h"
200# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100201
Bram Moolenaar734a8672019-12-02 22:49:38 +0100202#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
204#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100205# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100206#endif
207
Bram Moolenaar734a8672019-12-02 22:49:38 +0100208// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100209#define DLG_PADDING_X 10
210#define DLG_PADDING_Y 10
211#define DLG_OLD_STYLE_PADDING_X 5
212#define DLG_OLD_STYLE_PADDING_Y 5
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
218#define DLG_FONT_NAME "MS Sans Serif"
219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
Bram Moolenaar734a8672019-12-02 22:49:38 +0100225#ifndef WM_XBUTTONDOWN // For Win2K / winME ONLY
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100226# define WM_XBUTTONDOWN 0x020B
227# define WM_XBUTTONUP 0x020C
228# define WM_XBUTTONDBLCLK 0x020D
229# define MK_XBUTTON1 0x0020
230# define MK_XBUTTON2 0x0040
231#endif
232
233#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100235 * Define a few things for generating prototypes. This is just to avoid
236 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100238# define APIENTRY
239# define CALLBACK
240# define CONST
241# define FAR
242# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200243# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200244# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245# define _cdecl
246typedef int BOOL;
247typedef int BYTE;
248typedef int DWORD;
249typedef int WCHAR;
250typedef int ENUMLOGFONT;
251typedef int FINDREPLACE;
252typedef int HANDLE;
253typedef int HBITMAP;
254typedef int HBRUSH;
255typedef int HDROP;
256typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100257typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100258typedef int LPARAM;
259typedef int LPCREATESTRUCT;
260typedef int LPCSTR;
261typedef int LPCTSTR;
262typedef int LPRECT;
263typedef int LPSTR;
264typedef int LPWINDOWPOS;
265typedef int LPWORD;
266typedef int LRESULT;
267typedef int HRESULT;
268# undef MSG
269typedef int MSG;
270typedef int NEWTEXTMETRIC;
271typedef int OSVERSIONINFO;
272typedef int PWORD;
273typedef int RECT;
274typedef int UINT;
275typedef int WORD;
276typedef int WPARAM;
277typedef int POINT;
278typedef void *HINSTANCE;
279typedef void *HMENU;
280typedef void *HWND;
281typedef void *HDC;
282typedef void VOID;
283typedef int LPNMHDR;
284typedef int LONG;
285typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200286typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200287typedef int COLORREF;
288typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100289#endif
290
291#ifndef GET_X_LPARAM
292# define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
293#endif
294
295static void _OnPaint( HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100296static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100297static void clear_rect(RECT *rcp);
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299static WORD s_dlgfntheight; // height of the dialog font
300static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100301
302#ifdef FEAT_MENU
303static HMENU s_menuBar = NULL;
304#endif
305#ifdef FEAT_TEAROFF
306static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100307static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100308#endif
309
Bram Moolenaar734a8672019-12-02 22:49:38 +0100310// Flag that is set while processing a message that must not be interrupted by
311// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100312static int s_busy_processing = FALSE;
313
Bram Moolenaar734a8672019-12-02 22:49:38 +0100314static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100315
316#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200317static UINT s_findrep_msg = 0; // set in gui_w[16/32].c
318static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100319static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200320static int s_findrep_is_find; // TRUE for find dialog, FALSE
321 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322#endif
323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100324HWND s_hwnd = NULL;
325static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100326static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327
328#ifdef FEAT_TOOLBAR
329static HWND s_toolbarhwnd = NULL;
330static WNDPROC s_toolbar_wndproc = NULL;
331#endif
332
333#ifdef FEAT_GUI_TABLINE
334static HWND s_tabhwnd = NULL;
335static WNDPROC s_tabline_wndproc = NULL;
336static int showing_tabline = 0;
337#endif
338
339static WPARAM s_wParam = 0;
340static LPARAM s_lParam = 0;
341
342static HWND s_textArea = NULL;
343static UINT s_uMsg = 0;
344
Bram Moolenaar734a8672019-12-02 22:49:38 +0100345static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346
347static int s_need_activate = FALSE;
348
Bram Moolenaar734a8672019-12-02 22:49:38 +0100349// This variable is set when waiting for an event, which is the only moment
350// scrollbar dragging can be done directly. It's not allowed while commands
351// are executed, because it may move the cursor and that may cause unexpected
352// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100353static int allow_scrollbar = FALSE;
354
355#ifdef GLOBAL_IME
356# define MyTranslateMessage(x) global_ime_TranslateMessage(x)
357#else
358# define MyTranslateMessage(x) TranslateMessage(x)
359#endif
360
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100361#if defined(FEAT_DIRECTX)
362 static int
363directx_enabled(void)
364{
365 if (s_dwc != NULL)
366 return 1;
367 else if (s_directx_load_attempted)
368 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100369 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100370 DWrite_Init();
371 s_directx_load_attempted = 1;
372 s_dwc = DWriteContext_Open();
373 directx_binddc();
374 return s_dwc != NULL ? 1 : 0;
375}
376
377 static void
378directx_binddc(void)
379{
380 if (s_textArea != NULL)
381 {
382 RECT rect;
383 GetClientRect(s_textArea, &rect);
384 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
385 }
386}
387#endif
388
Bram Moolenaar734a8672019-12-02 22:49:38 +0100389// use of WindowProc depends on Global IME
Bram Moolenaar945c8572020-07-17 22:17:03 +0200390static LRESULT WINAPI MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100391
Bram Moolenaar734a8672019-12-02 22:49:38 +0100392extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100393
394static struct
395{
396 UINT key_sym;
397 char_u vim_code0;
398 char_u vim_code1;
399} special_keys[] =
400{
401 {VK_UP, 'k', 'u'},
402 {VK_DOWN, 'k', 'd'},
403 {VK_LEFT, 'k', 'l'},
404 {VK_RIGHT, 'k', 'r'},
405
406 {VK_F1, 'k', '1'},
407 {VK_F2, 'k', '2'},
408 {VK_F3, 'k', '3'},
409 {VK_F4, 'k', '4'},
410 {VK_F5, 'k', '5'},
411 {VK_F6, 'k', '6'},
412 {VK_F7, 'k', '7'},
413 {VK_F8, 'k', '8'},
414 {VK_F9, 'k', '9'},
415 {VK_F10, 'k', ';'},
416
417 {VK_F11, 'F', '1'},
418 {VK_F12, 'F', '2'},
419 {VK_F13, 'F', '3'},
420 {VK_F14, 'F', '4'},
421 {VK_F15, 'F', '5'},
422 {VK_F16, 'F', '6'},
423 {VK_F17, 'F', '7'},
424 {VK_F18, 'F', '8'},
425 {VK_F19, 'F', '9'},
426 {VK_F20, 'F', 'A'},
427
428 {VK_F21, 'F', 'B'},
429#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100430 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100431#endif
432 {VK_F22, 'F', 'C'},
433 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100434 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100435
436 {VK_HELP, '%', '1'},
437 {VK_BACK, 'k', 'b'},
438 {VK_INSERT, 'k', 'I'},
439 {VK_DELETE, 'k', 'D'},
440 {VK_HOME, 'k', 'h'},
441 {VK_END, '@', '7'},
442 {VK_PRIOR, 'k', 'P'},
443 {VK_NEXT, 'k', 'N'},
444 {VK_PRINT, '%', '9'},
445 {VK_ADD, 'K', '6'},
446 {VK_SUBTRACT, 'K', '7'},
447 {VK_DIVIDE, 'K', '8'},
448 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100449 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100450 {VK_DECIMAL, 'K', 'B'},
451
452 {VK_NUMPAD0, 'K', 'C'},
453 {VK_NUMPAD1, 'K', 'D'},
454 {VK_NUMPAD2, 'K', 'E'},
455 {VK_NUMPAD3, 'K', 'F'},
456 {VK_NUMPAD4, 'K', 'G'},
457 {VK_NUMPAD5, 'K', 'H'},
458 {VK_NUMPAD6, 'K', 'I'},
459 {VK_NUMPAD7, 'K', 'J'},
460 {VK_NUMPAD8, 'K', 'K'},
461 {VK_NUMPAD9, 'K', 'L'},
462
Bram Moolenaar734a8672019-12-02 22:49:38 +0100463 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100464 {VK_SPACE, ' ', NUL},
465 {VK_TAB, TAB, NUL},
466 {VK_ESCAPE, ESC, NUL},
467 {NL, NL, NUL},
468 {CAR, CAR, NUL},
469
Bram Moolenaar734a8672019-12-02 22:49:38 +0100470 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100471 {0, 0, 0}
472};
473
Bram Moolenaar734a8672019-12-02 22:49:38 +0100474// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100475static int s_button_pending = -1;
476
Bram Moolenaar734a8672019-12-02 22:49:38 +0100477// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
478// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100479static int s_getting_focus = FALSE;
480
481static int s_x_pending;
482static int s_y_pending;
483static UINT s_kFlags_pending;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200484static UINT s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485static int s_timed_out = FALSE;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200486static int dead_key = 0; // 0: no dead key, 1: dead key pressed
487static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
488 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100490#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100491// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100492static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
493static void TrackUserActivity(UINT uMsg);
494#endif
495
496/*
497 * For control IME.
498 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100499 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100500 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100501#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100502// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100503static LOGFONTW norm_logfont;
Bram Moolenaarbdb81392017-11-27 23:24:08 +0100504#endif
505#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100506// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100507static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100508#endif
509
510#ifdef FEAT_MBYTE_IME
511static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
512#endif
513
514#if defined(FEAT_BROWSE)
515static char_u *convert_filter(char_u *s);
516#endif
517
518#ifdef DEBUG_PRINT_ERROR
519/*
520 * Print out the last Windows error message
521 */
522 static void
523print_windows_error(void)
524{
525 LPVOID lpMsgBuf;
526
527 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
528 NULL, GetLastError(),
529 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
530 (LPTSTR) &lpMsgBuf, 0, NULL);
531 TRACE1("Error: %s\n", lpMsgBuf);
532 LocalFree(lpMsgBuf);
533}
534#endif
535
536/*
537 * Cursor blink functions.
538 *
539 * This is a simple state machine:
540 * BLINK_NONE not blinking at all
541 * BLINK_OFF blinking, cursor is not shown
542 * BLINK_ON blinking, cursor is shown
543 */
544
545#define BLINK_NONE 0
546#define BLINK_OFF 1
547#define BLINK_ON 2
548
549static int blink_state = BLINK_NONE;
550static long_u blink_waittime = 700;
551static long_u blink_ontime = 400;
552static long_u blink_offtime = 250;
553static UINT blink_timer = 0;
554
Bram Moolenaar703a8042016-06-04 16:24:32 +0200555 int
556gui_mch_is_blinking(void)
557{
558 return blink_state != BLINK_NONE;
559}
560
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200561 int
562gui_mch_is_blink_off(void)
563{
564 return blink_state == BLINK_OFF;
565}
566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100567 void
568gui_mch_set_blinking(long wait, long on, long off)
569{
570 blink_waittime = wait;
571 blink_ontime = on;
572 blink_offtime = off;
573}
574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 static VOID CALLBACK
576_OnBlinkTimer(
577 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100578 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100580 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100581{
582 MSG msg;
583
584 /*
585 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
586 */
587
588 KillTimer(NULL, idEvent);
589
Bram Moolenaar734a8672019-12-02 22:49:38 +0100590 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100591 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
592 ;
593
594 if (blink_state == BLINK_ON)
595 {
596 gui_undraw_cursor();
597 blink_state = BLINK_OFF;
598 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_offtime,
599 (TIMERPROC)_OnBlinkTimer);
600 }
601 else
602 {
603 gui_update_cursor(TRUE, FALSE);
604 blink_state = BLINK_ON;
605 blink_timer = (UINT) SetTimer(NULL, 0, (UINT)blink_ontime,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100606 (TIMERPROC)_OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100607 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100608 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100609}
610
611 static void
612gui_mswin_rm_blink_timer(void)
613{
614 MSG msg;
615
616 if (blink_timer != 0)
617 {
618 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100619 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100620 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
621 ;
622 blink_timer = 0;
623 }
624}
625
626/*
627 * Stop the cursor blinking. Show the cursor if it wasn't shown.
628 */
629 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100630gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100631{
632 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100633 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100634 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100636 gui_mch_flush();
637 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638 blink_state = BLINK_NONE;
639}
640
641/*
642 * Start the cursor blinking. If it was already blinking, this restarts the
643 * waiting time and shows the cursor.
644 */
645 void
646gui_mch_start_blink(void)
647{
648 gui_mswin_rm_blink_timer();
649
Bram Moolenaar734a8672019-12-02 22:49:38 +0100650 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
652 {
653 blink_timer = (UINT)SetTimer(NULL, 0, (UINT)blink_waittime,
654 (TIMERPROC)_OnBlinkTimer);
655 blink_state = BLINK_ON;
656 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100657 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100658 }
659}
660
661/*
662 * Call-back routines.
663 */
664
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100665 static VOID CALLBACK
666_OnTimer(
667 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100668 UINT uMsg UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100669 UINT idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100670 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671{
672 MSG msg;
673
674 /*
675 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
676 */
677 KillTimer(NULL, idEvent);
678 s_timed_out = TRUE;
679
Bram Moolenaar734a8672019-12-02 22:49:38 +0100680 // Eat spurious WM_TIMER messages
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681 while (pPeekMessage(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
682 ;
683 if (idEvent == s_wait_timer)
684 s_wait_timer = 0;
685}
686
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100687 static void
688_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100689 HWND hwnd UNUSED,
690 UINT ch UNUSED,
691 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100692{
693 dead_key = 1;
694}
695
696/*
697 * Convert Unicode character "ch" to bytes in "string[slen]".
698 * When "had_alt" is TRUE the ALT key was included in "ch".
699 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200700 * Because the Windows API uses UTF-16, we have to deal with surrogate
701 * pairs; this is where we choose to deal with them: if "ch" is a high
702 * surrogate, it will be stored, and the length returned will be zero; the next
703 * char_to_string call will then include the high surrogate, decoding the pair
704 * of UTF-16 code units to a single Unicode code point, presuming it is the
705 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 */
707 static int
708char_to_string(int ch, char_u *string, int slen, int had_alt)
709{
710 int len;
711 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200713 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200715 if (surrogate_pending_ch != 0)
716 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100717 // We don't guarantee ch is a low surrogate to match the high surrogate
718 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200719 wstring[0] = surrogate_pending_ch;
720 wstring[1] = ch;
721 surrogate_pending_ch = 0;
722 len = 2;
723 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100724 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200725 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100726 // We don't have the entire code point yet, only the first UTF-16 code
727 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200728 surrogate_pending_ch = ch;
729 return 0;
730 }
731 else
732 {
733 wstring[0] = ch;
734 len = 1;
735 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200736
Bram Moolenaar734a8672019-12-02 22:49:38 +0100737 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
738 // "enc_codepage" is non-zero use the standard Win32 function,
739 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200740 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200742 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
743 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100744 // If we had included the ALT key into the character but now the
745 // upper bit is no longer set, that probably means the conversion
746 // failed. Convert the original character and set the upper bit
747 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200748 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200750 wstring[0] = ch & 0x7f;
751 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
752 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100753 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200754 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 }
756 }
757 else
758 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200759 ws = utf16_to_enc(wstring, &len);
760 if (ws == NULL)
761 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100762 else
763 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100764 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200765 len = slen;
766 mch_memmove(string, ws, len);
767 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768 }
769 }
770
771 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 {
773 string[0] = ch;
774 len = 1;
775 }
776
777 for (i = 0; i < len; ++i)
778 if (string[i] == CSI && len <= slen - 2)
779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100781 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
782 string[++i] = KS_EXTRA;
783 string[++i] = (int)KE_CSI;
784 len += 2;
785 }
786
787 return len;
788}
789
790/*
791 * Key hit, add it to the input buffer.
792 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100793 static void
794_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100795 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100796 UINT ch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100797 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798{
799 char_u string[40];
800 int len = 0;
801
802 dead_key = 0;
803
804 len = char_to_string(ch, string, 40, FALSE);
805 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
806 {
807 trash_input_buf();
808 got_int = TRUE;
809 }
810
811 add_to_input_buf(string, len);
812}
813
814/*
815 * Alt-Key hit, add it to the input buffer.
816 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 static void
818_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100819 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100820 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100821 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100823 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100824 int len;
825 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100826 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100827
828 dead_key = 0;
829
Bram Moolenaar734a8672019-12-02 22:49:38 +0100830 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100831
Bram Moolenaar734a8672019-12-02 22:49:38 +0100832 // OK, we have a character key (given by ch) which was entered with the
833 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
834 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
835 // CAPSLOCK is pressed) at this point.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100836 modifiers = MOD_MASK_ALT;
837 if (GetKeyState(VK_SHIFT) & 0x8000)
838 modifiers |= MOD_MASK_SHIFT;
839 if (GetKeyState(VK_CONTROL) & 0x8000)
840 modifiers |= MOD_MASK_CTRL;
841
842 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100843 // remove the SHIFT modifier for keys where it's already included, e.g.,
844 // '(' and '*'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100845 if (ch < 0x100 && !isalpha(ch) && isprint(ch))
846 modifiers &= ~MOD_MASK_SHIFT;
847
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200848 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
849 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100850 if (ch == CSI)
851 ch = K_CSI;
852
853 len = 0;
854 if (modifiers)
855 {
856 string[len++] = CSI;
857 string[len++] = KS_MODIFIER;
858 string[len++] = modifiers;
859 }
860
861 if (IS_SPECIAL((int)ch))
862 {
863 string[len++] = CSI;
864 string[len++] = K_SECOND((int)ch);
865 string[len++] = K_THIRD((int)ch);
866 }
867 else
868 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100869 // Although the documentation isn't clear about it, we assume "ch" is
870 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100871 len += char_to_string(ch, string + len, 40 - len, TRUE);
872 }
873
874 add_to_input_buf(string, len);
875}
876
877 static void
878_OnMouseEvent(
879 int button,
880 int x,
881 int y,
882 int repeated_click,
883 UINT keyFlags)
884{
885 int vim_modifiers = 0x0;
886
887 s_getting_focus = FALSE;
888
889 if (keyFlags & MK_SHIFT)
890 vim_modifiers |= MOUSE_SHIFT;
891 if (keyFlags & MK_CONTROL)
892 vim_modifiers |= MOUSE_CTRL;
893 if (GetKeyState(VK_MENU) & 0x8000)
894 vim_modifiers |= MOUSE_ALT;
895
896 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
897}
898
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100899 static void
900_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100901 HWND hwnd UNUSED,
902 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100903 int x,
904 int y,
905 UINT keyFlags)
906{
907 static LONG s_prevTime = 0;
908
909 LONG currentTime = GetMessageTime();
910 int button = -1;
911 int repeated_click;
912
Bram Moolenaar734a8672019-12-02 22:49:38 +0100913 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914 (void)SetFocus(s_hwnd);
915
916 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
917 button = MOUSE_LEFT;
918 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
919 button = MOUSE_MIDDLE;
920 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
921 button = MOUSE_RIGHT;
922 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
923 {
924#ifndef GET_XBUTTON_WPARAM
925# define GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
926#endif
927 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
928 }
929 else if (s_uMsg == WM_CAPTURECHANGED)
930 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100931 // on W95/NT4, somehow you get in here with an odd Msg
932 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933 if (s_button_pending == MOUSE_LEFT)
934 button = MOUSE_RIGHT;
935 else
936 button = MOUSE_LEFT;
937 }
938 if (button >= 0)
939 {
940 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
941
942 /*
943 * Holding down the left and right buttons simulates pushing the middle
944 * button.
945 */
946 if (repeated_click
947 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
948 || (button == MOUSE_RIGHT
949 && s_button_pending == MOUSE_LEFT)))
950 {
951 /*
952 * Hmm, gui.c will ignore more than one button down at a time, so
953 * pretend we let go of it first.
954 */
955 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
956 button = MOUSE_MIDDLE;
957 repeated_click = FALSE;
958 s_button_pending = -1;
959 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
960 }
961 else if ((repeated_click)
962 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
963 {
964 if (s_button_pending > -1)
965 {
966 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
967 s_button_pending = -1;
968 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100969 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100970 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
971 }
972 else
973 {
974 /*
975 * If this is the first press (i.e. not a multiple click) don't
976 * action immediately, but store and wait for:
977 * i) button-up
978 * ii) mouse move
979 * iii) another button press
980 * before using it.
981 * This enables us to make left+right simulate middle button,
982 * without left or right being actioned first. The side-effect is
983 * that if you click and hold the mouse without dragging, the
984 * cursor doesn't move until you release the button. In practice
985 * this is hardly a problem.
986 */
987 s_button_pending = button;
988 s_x_pending = x;
989 s_y_pending = y;
990 s_kFlags_pending = keyFlags;
991 }
992
993 s_prevTime = currentTime;
994 }
995}
996
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100997 static void
998_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100999 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001000 int x,
1001 int y,
1002 UINT keyFlags)
1003{
1004 int button;
1005
1006 s_getting_focus = FALSE;
1007 if (s_button_pending > -1)
1008 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001009 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001010 _OnMouseEvent(s_button_pending, s_x_pending,
1011 s_y_pending, FALSE, s_kFlags_pending);
1012 s_button_pending = -1;
1013 }
1014 if (s_uMsg == WM_MOUSEMOVE)
1015 {
1016 /*
1017 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1018 * down.
1019 */
1020 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1021 | MK_XBUTTON1 | MK_XBUTTON2)))
1022 {
1023 gui_mouse_moved(x, y);
1024 return;
1025 }
1026
1027 /*
1028 * While button is down, keep grabbing mouse move events when
1029 * the mouse goes outside the window
1030 */
1031 SetCapture(s_textArea);
1032 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001033 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001034 }
1035 else
1036 {
1037 ReleaseCapture();
1038 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001039 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001040 }
1041
1042 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1043}
1044
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001045 static void
1046_OnSizeTextArea(
1047 HWND hwnd UNUSED,
1048 UINT state UNUSED,
1049 int cx UNUSED,
1050 int cy UNUSED)
1051{
1052#if defined(FEAT_DIRECTX)
1053 if (IS_ENABLE_DIRECTX())
1054 directx_binddc();
1055#endif
1056}
1057
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001058#ifdef FEAT_MENU
1059/*
1060 * Find the vimmenu_T with the given id
1061 */
1062 static vimmenu_T *
1063gui_mswin_find_menu(
1064 vimmenu_T *pMenu,
1065 int id)
1066{
1067 vimmenu_T *pChildMenu;
1068
1069 while (pMenu)
1070 {
1071 if (pMenu->id == (UINT)id)
1072 break;
1073 if (pMenu->children != NULL)
1074 {
1075 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1076 if (pChildMenu)
1077 {
1078 pMenu = pChildMenu;
1079 break;
1080 }
1081 }
1082 pMenu = pMenu->next;
1083 }
1084 return pMenu;
1085}
1086
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 static void
1088_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001089 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001091 HWND hwndCtl UNUSED,
1092 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001093{
1094 vimmenu_T *pMenu;
1095
1096 pMenu = gui_mswin_find_menu(root_menu, id);
1097 if (pMenu)
1098 gui_menu_cb(pMenu);
1099}
1100#endif
1101
1102#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001104 * Handle a Find/Replace window message.
1105 */
1106 static void
1107_OnFindRepl(void)
1108{
1109 int flags = 0;
1110 int down;
1111
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001112 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001113 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001114 (void)SetFocus(s_hwnd);
1115
1116 if (s_findrep_struct.Flags & FR_FINDNEXT)
1117 {
1118 flags = FRD_FINDNEXT;
1119
Bram Moolenaar734a8672019-12-02 22:49:38 +01001120 // Give main window the focus back: this is so the cursor isn't
1121 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001122 (void)SetFocus(s_hwnd);
1123 }
1124 else if (s_findrep_struct.Flags & FR_REPLACE)
1125 {
1126 flags = FRD_REPLACE;
1127
Bram Moolenaar734a8672019-12-02 22:49:38 +01001128 // Give main window the focus back: this is so the cursor isn't
1129 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001130 (void)SetFocus(s_hwnd);
1131 }
1132 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1133 {
1134 flags = FRD_REPLACEALL;
1135 }
1136
1137 if (flags != 0)
1138 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001139 char_u *p, *q;
1140
Bram Moolenaar734a8672019-12-02 22:49:38 +01001141 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001142 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1143 flags |= FRD_WHOLE_WORD;
1144 if (s_findrep_struct.Flags & FR_MATCHCASE)
1145 flags |= FRD_MATCH_CASE;
1146 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001147 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1148 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1149 if (p != NULL && q != NULL)
1150 gui_do_findrepl(flags, p, q, down);
1151 vim_free(p);
1152 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001153 }
1154}
1155#endif
1156
1157 static void
1158HandleMouseHide(UINT uMsg, LPARAM lParam)
1159{
1160 static LPARAM last_lParam = 0L;
1161
Bram Moolenaar734a8672019-12-02 22:49:38 +01001162 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1164 {
1165 if (lParam == last_lParam)
1166 return;
1167 last_lParam = lParam;
1168 }
1169
Bram Moolenaar734a8672019-12-02 22:49:38 +01001170 // Handle specially, to centralise coding. We need to be sure we catch all
1171 // possible events which should cause us to restore the cursor (as it is a
1172 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 switch (uMsg)
1174 {
1175 case WM_KEYUP:
1176 case WM_CHAR:
1177 /*
1178 * blank out the pointer if necessary
1179 */
1180 if (p_mh)
1181 gui_mch_mousehide(TRUE);
1182 break;
1183
Bram Moolenaar734a8672019-12-02 22:49:38 +01001184 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001185 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001186 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187 case WM_LBUTTONDOWN:
1188 case WM_LBUTTONUP:
1189 case WM_MBUTTONDOWN:
1190 case WM_MBUTTONUP:
1191 case WM_RBUTTONDOWN:
1192 case WM_RBUTTONUP:
1193 case WM_XBUTTONDOWN:
1194 case WM_XBUTTONUP:
1195 case WM_NCMOUSEMOVE:
1196 case WM_NCLBUTTONDOWN:
1197 case WM_NCLBUTTONUP:
1198 case WM_NCMBUTTONDOWN:
1199 case WM_NCMBUTTONUP:
1200 case WM_NCRBUTTONDOWN:
1201 case WM_NCRBUTTONUP:
1202 case WM_KILLFOCUS:
1203 /*
1204 * if the pointer is currently hidden, then we should show it.
1205 */
1206 gui_mch_mousehide(FALSE);
1207 break;
1208 }
1209}
1210
1211 static LRESULT CALLBACK
1212_TextAreaWndProc(
1213 HWND hwnd,
1214 UINT uMsg,
1215 WPARAM wParam,
1216 LPARAM lParam)
1217{
1218 /*
1219 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1220 hwnd, uMsg, wParam, lParam);
1221 */
1222
1223 HandleMouseHide(uMsg, lParam);
1224
1225 s_uMsg = uMsg;
1226 s_wParam = wParam;
1227 s_lParam = lParam;
1228
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001229#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 TrackUserActivity(uMsg);
1231#endif
1232
1233 switch (uMsg)
1234 {
1235 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1236 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1237 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1238 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1239 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1240 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1241 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1242 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1243 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1244 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1245 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1246 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1247 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1248 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001249 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001250
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001251#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001252 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1253 return TRUE;
1254#endif
1255 default:
1256 return MyWindowProc(hwnd, uMsg, wParam, lParam);
1257 }
1258}
1259
Bram Moolenaar945c8572020-07-17 22:17:03 +02001260 static LRESULT WINAPI
1261MyWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001262{
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001263#ifdef GLOBAL_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001264 return global_ime_DefWindowProc(hwnd, message, wParam, lParam);
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01001265#else
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001266 return DefWindowProcW(hwnd, message, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001267#endif
1268}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001269
1270/*
1271 * Called when the foreground or background color has been changed.
1272 */
1273 void
1274gui_mch_new_colors(void)
1275{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001276 HBRUSH prevBrush;
1277
1278 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001279 prevBrush = (HBRUSH)SetClassLongPtr(
1280 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001281 InvalidateRect(s_hwnd, NULL, TRUE);
1282 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001283}
1284
1285/*
1286 * Set the colors to their default values.
1287 */
1288 void
1289gui_mch_def_colors(void)
1290{
1291 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1292 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1293 gui.def_norm_pixel = gui.norm_pixel;
1294 gui.def_back_pixel = gui.back_pixel;
1295}
1296
1297/*
1298 * Open the GUI window which was created by a call to gui_mch_init().
1299 */
1300 int
1301gui_mch_open(void)
1302{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001303 // Actually open the window, if not already visible
1304 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001305 if (!IsWindowVisible(s_hwnd))
1306 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1307
1308#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001309 // Init replace string here, so that we keep it when re-opening the
1310 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001311 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1312#endif
1313
1314 return OK;
1315}
1316
1317/*
1318 * Get the position of the top left corner of the window.
1319 */
1320 int
1321gui_mch_get_winpos(int *x, int *y)
1322{
1323 RECT rect;
1324
1325 GetWindowRect(s_hwnd, &rect);
1326 *x = rect.left;
1327 *y = rect.top;
1328 return OK;
1329}
1330
1331/*
1332 * Set the position of the top left corner of the window to the given
1333 * coordinates.
1334 */
1335 void
1336gui_mch_set_winpos(int x, int y)
1337{
1338 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1339 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1340}
1341 void
1342gui_mch_set_text_area_pos(int x, int y, int w, int h)
1343{
1344 static int oldx = 0;
1345 static int oldy = 0;
1346
1347 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1348
1349#ifdef FEAT_TOOLBAR
1350 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1351 SendMessage(s_toolbarhwnd, WM_SIZE,
1352 (WPARAM)0, (LPARAM)(w + ((long)(TOOLBAR_BUTTON_HEIGHT+8)<<16)));
1353#endif
1354#if defined(FEAT_GUI_TABLINE)
1355 if (showing_tabline)
1356 {
1357 int top = 0;
1358 RECT rect;
1359
1360# ifdef FEAT_TOOLBAR
1361 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1362 top = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
1363# endif
1364 GetClientRect(s_hwnd, &rect);
1365 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1366 }
1367#endif
1368
Bram Moolenaar734a8672019-12-02 22:49:38 +01001369 // When side scroll bar is unshown, the size of window will change.
1370 // then, the text area move left or right. thus client rect should be
1371 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001372 if (oldx != x || oldy != y)
1373 {
1374 InvalidateRect(s_hwnd, NULL, FALSE);
1375 oldx = x;
1376 oldy = y;
1377 }
1378}
1379
1380
1381/*
1382 * Scrollbar stuff:
1383 */
1384
1385 void
1386gui_mch_enable_scrollbar(
1387 scrollbar_T *sb,
1388 int flag)
1389{
1390 ShowScrollBar(sb->id, SB_CTL, flag);
1391
Bram Moolenaar734a8672019-12-02 22:49:38 +01001392 // TODO: When the window is maximized, the size of the window stays the
1393 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1394 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001395}
1396
1397 void
1398gui_mch_set_scrollbar_pos(
1399 scrollbar_T *sb,
1400 int x,
1401 int y,
1402 int w,
1403 int h)
1404{
1405 SetWindowPos(sb->id, NULL, x, y, w, h,
1406 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1407}
1408
Bram Moolenaar203ec772020-07-17 20:43:43 +02001409 int
1410gui_mch_get_scrollbar_xpadding(void)
1411{
1412 RECT rcTxt, rcWnd;
1413 int xpad;
1414
1415 GetWindowRect(s_textArea, &rcTxt);
1416 GetWindowRect(s_hwnd, &rcWnd);
1417 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
1418 - GetSystemMetrics(SM_CXFRAME)
1419 - GetSystemMetrics(SM_CXPADDEDBORDER);
1420 return (xpad < 0) ? 0 : xpad;
1421}
1422
1423 int
1424gui_mch_get_scrollbar_ypadding(void)
1425{
1426 RECT rcTxt, rcWnd;
1427 int ypad;
1428
1429 GetWindowRect(s_textArea, &rcTxt);
1430 GetWindowRect(s_hwnd, &rcWnd);
1431 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
1432 - GetSystemMetrics(SM_CYFRAME)
1433 - GetSystemMetrics(SM_CXPADDEDBORDER);
1434 return (ypad < 0) ? 0 : ypad;
1435}
1436
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001437 void
1438gui_mch_create_scrollbar(
1439 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001440 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001441{
1442 sb->id = CreateWindow(
1443 "SCROLLBAR", "Scrollbar",
1444 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001445 10, // Any value will do for now
1446 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001447 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001448 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001449}
1450
1451/*
1452 * Find the scrollbar with the given hwnd.
1453 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001454 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001455gui_mswin_find_scrollbar(HWND hwnd)
1456{
1457 win_T *wp;
1458
1459 if (gui.bottom_sbar.id == hwnd)
1460 return &gui.bottom_sbar;
1461 FOR_ALL_WINDOWS(wp)
1462 {
1463 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1464 return &wp->w_scrollbars[SBAR_LEFT];
1465 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1466 return &wp->w_scrollbars[SBAR_RIGHT];
1467 }
1468 return NULL;
1469}
1470
1471/*
1472 * Get the character size of a font.
1473 */
1474 static void
1475GetFontSize(GuiFont font)
1476{
1477 HWND hwnd = GetDesktopWindow();
1478 HDC hdc = GetWindowDC(hwnd);
1479 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001480 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001481 TEXTMETRIC tm;
1482
1483 GetTextMetrics(hdc, &tm);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001484 // GetTextMetrics() may not return the right value in tmAveCharWidth
1485 // for some fonts. Do our own average computation.
1486 GetTextExtentPoint(hdc,
1487 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1488 52, &size);
1489 gui.char_width = (size.cx / 26 + 1) / 2 + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490
1491 gui.char_height = tm.tmHeight + p_linespace;
1492
1493 SelectFont(hdc, hfntOld);
1494
1495 ReleaseDC(hwnd, hdc);
1496}
1497
1498/*
1499 * Adjust gui.char_height (after 'linespace' was changed).
1500 */
1501 int
1502gui_mch_adjust_charheight(void)
1503{
1504 GetFontSize(gui.norm_font);
1505 return OK;
1506}
1507
1508 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001509get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001510{
1511 HFONT font = NULL;
1512
Bram Moolenaar734a8672019-12-02 22:49:38 +01001513 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001514 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001515
1516 if (font == NULL)
1517 return NOFONT;
1518
1519 return (GuiFont)font;
1520}
1521
1522 static int
1523pixels_to_points(int pixels, int vertical)
1524{
1525 int points;
1526 HWND hwnd;
1527 HDC hdc;
1528
1529 hwnd = GetDesktopWindow();
1530 hdc = GetWindowDC(hwnd);
1531
1532 points = MulDiv(pixels, 72,
1533 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1534
1535 ReleaseDC(hwnd, hdc);
1536
1537 return points;
1538}
1539
1540 GuiFont
1541gui_mch_get_font(
1542 char_u *name,
1543 int giveErrorIfMissing)
1544{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001545 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001546 GuiFont font = NOFONT;
1547
1548 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
1549 font = get_font_handle(&lf);
1550 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001551 semsg(_(e_font), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001552 return font;
1553}
1554
1555#if defined(FEAT_EVAL) || defined(PROTO)
1556/*
1557 * Return the name of font "font" in allocated memory.
1558 * Don't know how to get the actual name, thus use the provided name.
1559 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001560 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001561gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001562{
1563 if (name == NULL)
1564 return NULL;
1565 return vim_strsave(name);
1566}
1567#endif
1568
1569 void
1570gui_mch_free_font(GuiFont font)
1571{
1572 if (font)
1573 DeleteObject((HFONT)font);
1574}
1575
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001576/*
1577 * Return the Pixel value (color) for the given color name.
1578 * Return INVALCOLOR for error.
1579 */
1580 guicolor_T
1581gui_mch_get_color(char_u *name)
1582{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001583 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
1585 typedef struct SysColorTable
1586 {
1587 char *name;
1588 int color;
1589 } SysColorTable;
1590
1591 static SysColorTable sys_table[] =
1592 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001593 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1594 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001595#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001596 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1597#endif
1598 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1599 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1600 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1601 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1602 {"SYS_DESKTOP", COLOR_DESKTOP},
1603 {"SYS_INFOBK", COLOR_INFOBK},
1604 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1605 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001606 {"SYS_BTNFACE", COLOR_BTNFACE},
1607 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1608 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1609 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1610 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1611 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1612 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1613 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1614 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1615 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1616 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1617 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1618 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1619 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1620 {"SYS_MENU", COLOR_MENU},
1621 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1622 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1623 {"SYS_WINDOW", COLOR_WINDOW},
1624 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1625 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1626 };
1627
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001628 /*
1629 * Try to look up a system colour.
1630 */
1631 for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
1632 if (STRICMP(name, sys_table[i].name) == 0)
1633 return GetSysColor(sys_table[i].color);
1634
Bram Moolenaarab302212016-04-26 20:59:29 +02001635 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001636}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001637
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001638 guicolor_T
1639gui_mch_get_rgb_color(int r, int g, int b)
1640{
1641 return gui_get_rgb_color_cmn(r, g, b);
1642}
1643
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644/*
1645 * Return OK if the key with the termcap name "name" is supported.
1646 */
1647 int
1648gui_mch_haskey(char_u *name)
1649{
1650 int i;
1651
1652 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
1653 if (name[0] == special_keys[i].vim_code0 &&
1654 name[1] == special_keys[i].vim_code1)
1655 return OK;
1656 return FAIL;
1657}
1658
1659 void
1660gui_mch_beep(void)
1661{
1662 MessageBeep(MB_OK);
1663}
1664/*
1665 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1666 */
1667 void
1668gui_mch_invert_rectangle(
1669 int r,
1670 int c,
1671 int nr,
1672 int nc)
1673{
1674 RECT rc;
1675
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001676#if defined(FEAT_DIRECTX)
1677 if (IS_ENABLE_DIRECTX())
1678 DWriteContext_Flush(s_dwc);
1679#endif
1680
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001681 /*
1682 * Note: InvertRect() excludes right and bottom of rectangle.
1683 */
1684 rc.left = FILL_X(c);
1685 rc.top = FILL_Y(r);
1686 rc.right = rc.left + nc * gui.char_width;
1687 rc.bottom = rc.top + nr * gui.char_height;
1688 InvertRect(s_hdc, &rc);
1689}
1690
1691/*
1692 * Iconify the GUI window.
1693 */
1694 void
1695gui_mch_iconify(void)
1696{
1697 ShowWindow(s_hwnd, SW_MINIMIZE);
1698}
1699
1700/*
1701 * Draw a cursor without focus.
1702 */
1703 void
1704gui_mch_draw_hollow_cursor(guicolor_T color)
1705{
1706 HBRUSH hbr;
1707 RECT rc;
1708
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001709#if defined(FEAT_DIRECTX)
1710 if (IS_ENABLE_DIRECTX())
1711 DWriteContext_Flush(s_dwc);
1712#endif
1713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001714 /*
1715 * Note: FrameRect() excludes right and bottom of rectangle.
1716 */
1717 rc.left = FILL_X(gui.col);
1718 rc.top = FILL_Y(gui.row);
1719 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001720 if (mb_lefthalve(gui.row, gui.col))
1721 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001722 rc.bottom = rc.top + gui.char_height;
1723 hbr = CreateSolidBrush(color);
1724 FrameRect(s_hdc, &rc, hbr);
1725 DeleteBrush(hbr);
1726}
1727/*
1728 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1729 * color "color".
1730 */
1731 void
1732gui_mch_draw_part_cursor(
1733 int w,
1734 int h,
1735 guicolor_T color)
1736{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001737 RECT rc;
1738
1739 /*
1740 * Note: FillRect() excludes right and bottom of rectangle.
1741 */
1742 rc.left =
1743#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001744 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001745 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1746#endif
1747 FILL_X(gui.col);
1748 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1749 rc.right = rc.left + w;
1750 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001751
Bram Moolenaar92467d32017-12-05 13:22:16 +01001752 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001753}
1754
1755
1756/*
1757 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1758 * dead key's nominal character and re-post the original message.
1759 */
1760 static void
1761outputDeadKey_rePost(MSG originalMsg)
1762{
1763 static MSG deadCharExpel;
1764
1765 if (!dead_key)
1766 return;
1767
1768 dead_key = 0;
1769
Bram Moolenaar734a8672019-12-02 22:49:38 +01001770 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001771 deadCharExpel.message = originalMsg.message;
1772 deadCharExpel.hwnd = originalMsg.hwnd;
1773 deadCharExpel.wParam = VK_SPACE;
1774
1775 MyTranslateMessage(&deadCharExpel);
1776
Bram Moolenaar734a8672019-12-02 22:49:38 +01001777 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001778 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1779 originalMsg.lParam);
1780}
1781
1782
1783/*
1784 * Process a single Windows message.
1785 * If one is not available we hang until one is.
1786 */
1787 static void
1788process_message(void)
1789{
1790 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001791 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001792 char_u string[40];
1793 int i;
1794 int modifiers = 0;
1795 int key;
1796#ifdef FEAT_MENU
1797 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1798#endif
1799
1800 pGetMessage(&msg, NULL, 0, 0);
1801
1802#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001803 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001804 if (msg.message == WM_OLE)
1805 {
1806 char_u *str = (char_u *)msg.lParam;
1807 if (str == NULL || *str == NUL)
1808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001809 // Message can't be ours, forward it. Fixes problem with Ultramon
1810 // 3.0.4
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001811 pDispatchMessage(&msg);
1812 }
1813 else
1814 {
1815 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001816 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 }
1818 return;
1819 }
1820#endif
1821
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001822#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001823 // Don't process messages used by the dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001824 if (s_findrep_hwnd != NULL && pIsDialogMessage(s_findrep_hwnd, &msg))
1825 {
1826 HandleMouseHide(msg.message, msg.lParam);
1827 return;
1828 }
1829#endif
1830
1831 /*
1832 * Check if it's a special key that we recognise. If not, call
1833 * TranslateMessage().
1834 */
1835 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1836 {
1837 vk = (int) msg.wParam;
1838
1839 /*
1840 * Handle dead keys in special conditions in other cases we let Windows
1841 * handle them and do not interfere.
1842 *
1843 * The dead_key flag must be reset on several occasions:
1844 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1845 * consumed at that point (This is when we let Windows combine the
1846 * dead character on its own)
1847 *
1848 * - Before doing something special such as regenerating keypresses to
1849 * expel the dead character as this could trigger an infinite loop if
1850 * for some reason MyTranslateMessage() do not trigger a call
1851 * immediately to _OnChar() (or _OnSysChar()).
1852 */
1853 if (dead_key)
1854 {
1855 /*
1856 * If a dead key was pressed and the user presses VK_SPACE,
1857 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
1858 * with the dead char now, so do nothing special and let Windows
1859 * handle it.
1860 *
1861 * Note that VK_SPACE combines with the dead_key's character and
1862 * only one WM_CHAR will be generated by TranslateMessage(), in
1863 * the two other cases two WM_CHAR will be generated: the dead
1864 * char and VK_BACK or VK_ESCAPE. That is most likely what the
1865 * user expects.
1866 */
1867 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
1868 {
1869 dead_key = 0;
1870 MyTranslateMessage(&msg);
1871 return;
1872 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001873 // In modes where we are not typing, dead keys should behave
1874 // normally
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001875 else if (!(get_real_state() & (INSERT | CMDLINE | SELECTMODE)))
1876 {
1877 outputDeadKey_rePost(msg);
1878 return;
1879 }
1880 }
1881
Bram Moolenaar734a8672019-12-02 22:49:38 +01001882 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001883 if (vk == VK_CANCEL)
1884 {
1885 trash_input_buf();
1886 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001887 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 string[0] = Ctrl_C;
1889 add_to_input_buf(string, 1);
1890 }
1891
1892 for (i = 0; special_keys[i].key_sym != 0; i++)
1893 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001894 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001895 if (special_keys[i].key_sym == vk
1896 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
1897 {
1898 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02001899 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001900 * is a key that would normally trigger the dead key nominal
1901 * character output (such as a NUMPAD printable character or
1902 * the TAB key, etc...).
1903 */
1904 if (dead_key && (special_keys[i].vim_code0 == 'K'
1905 || vk == VK_TAB || vk == CAR))
1906 {
1907 outputDeadKey_rePost(msg);
1908 return;
1909 }
1910
1911#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01001912 // Check for <F10>: Windows selects the menu. When <F10> is
1913 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001914 if (vk == VK_F10
1915 && gui.menu_is_active
1916 && check_map(k10, State, FALSE, TRUE, FALSE,
1917 NULL, NULL) == NULL)
1918 break;
1919#endif
1920 if (GetKeyState(VK_SHIFT) & 0x8000)
1921 modifiers |= MOD_MASK_SHIFT;
1922 /*
1923 * Don't use caps-lock as shift, because these are special keys
1924 * being considered here, and we only want letters to get
1925 * shifted -- webb
1926 */
1927 /*
1928 if (GetKeyState(VK_CAPITAL) & 0x0001)
1929 modifiers ^= MOD_MASK_SHIFT;
1930 */
1931 if (GetKeyState(VK_CONTROL) & 0x8000)
1932 modifiers |= MOD_MASK_CTRL;
1933 if (GetKeyState(VK_MENU) & 0x8000)
1934 modifiers |= MOD_MASK_ALT;
1935
1936 if (special_keys[i].vim_code1 == NUL)
1937 key = special_keys[i].vim_code0;
1938 else
1939 key = TO_SPECIAL(special_keys[i].vim_code0,
1940 special_keys[i].vim_code1);
1941 key = simplify_key(key, &modifiers);
1942 if (key == CSI)
1943 key = K_CSI;
1944
1945 if (modifiers)
1946 {
1947 string[0] = CSI;
1948 string[1] = KS_MODIFIER;
1949 string[2] = modifiers;
1950 add_to_input_buf(string, 3);
1951 }
1952
1953 if (IS_SPECIAL(key))
1954 {
1955 string[0] = CSI;
1956 string[1] = K_SECOND(key);
1957 string[2] = K_THIRD(key);
1958 add_to_input_buf(string, 3);
1959 }
1960 else
1961 {
1962 int len;
1963
Bram Moolenaar734a8672019-12-02 22:49:38 +01001964 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001965 len = char_to_string(key, string, 40, FALSE);
1966 add_to_input_buf(string, len);
1967 }
1968 break;
1969 }
1970 }
1971 if (special_keys[i].key_sym == 0)
1972 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001973 // Some keys need C-S- where they should only need C-.
1974 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
1975 // system startup (Helmut Stiegler, 2003 Oct 3).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001976 if (vk != 0xff
1977 && (GetKeyState(VK_CONTROL) & 0x8000)
1978 && !(GetKeyState(VK_SHIFT) & 0x8000)
1979 && !(GetKeyState(VK_MENU) & 0x8000))
1980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001981 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001982 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
1983 {
1984 string[0] = Ctrl_HAT;
1985 add_to_input_buf(string, 1);
1986 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001987 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
1988 else if (vk == 0xBD) // QWERTY for CTRL-'-'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001989 {
1990 string[0] = Ctrl__;
1991 add_to_input_buf(string, 1);
1992 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001993 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001994 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
1995 {
1996 string[0] = Ctrl_AT;
1997 add_to_input_buf(string, 1);
1998 }
1999 else
2000 MyTranslateMessage(&msg);
2001 }
2002 else
2003 MyTranslateMessage(&msg);
2004 }
2005 }
2006#ifdef FEAT_MBYTE_IME
2007 else if (msg.message == WM_IME_NOTIFY)
2008 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2009 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002010 // added for non-MS IME (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002011 MyTranslateMessage(&msg);
2012#endif
2013#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002014// GIME_TEST
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002015 else if (msg.message == WM_IME_STARTCOMPOSITION)
2016 {
2017 POINT point;
2018
2019 global_ime_set_font(&norm_logfont);
2020 point.x = FILL_X(gui.col);
2021 point.y = FILL_Y(gui.row);
2022 MapWindowPoints(s_textArea, s_hwnd, &point, 1);
2023 global_ime_set_position(&point);
2024 }
2025#endif
2026
2027#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002028 // Check for <F10>: Default effect is to select the menu. When <F10> is
2029 // mapped we need to stop it here to avoid strange effects (e.g., for the
2030 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002031 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2032 NULL, NULL) == NULL)
2033#endif
2034 pDispatchMessage(&msg);
2035}
2036
2037/*
2038 * Catch up with any queued events. This may put keyboard input into the
2039 * input buffer, call resize call-backs, trigger timers etc. If there is
2040 * nothing in the event queue (& no timers pending), then we return
2041 * immediately.
2042 */
2043 void
2044gui_mch_update(void)
2045{
2046 MSG msg;
2047
2048 if (!s_busy_processing)
2049 while (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)
2050 && !vim_is_input_buf_full())
2051 process_message();
2052}
2053
Bram Moolenaar4231da42016-06-02 14:30:04 +02002054 static void
2055remove_any_timer(void)
2056{
2057 MSG msg;
2058
2059 if (s_wait_timer != 0 && !s_timed_out)
2060 {
2061 KillTimer(NULL, s_wait_timer);
2062
Bram Moolenaar734a8672019-12-02 22:49:38 +01002063 // Eat spurious WM_TIMER messages
Bram Moolenaar4231da42016-06-02 14:30:04 +02002064 while (pPeekMessage(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
2065 ;
2066 s_wait_timer = 0;
2067 }
2068}
2069
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002070/*
2071 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2072 * from the keyboard.
2073 * wtime == -1 Wait forever.
2074 * wtime == 0 This should never happen.
2075 * wtime > 0 Wait wtime milliseconds for a character.
2076 * Returns OK if a character was found to be available within the given time,
2077 * or FAIL otherwise.
2078 */
2079 int
2080gui_mch_wait_for_chars(int wtime)
2081{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002082 int focus;
2083
2084 s_timed_out = FALSE;
2085
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002086 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002087 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002088 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002089 if (s_busy_processing)
2090 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002091
2092 // When called with "wtime" zero, just want one msec.
2093 s_wait_timer = (UINT)SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002094 (TIMERPROC)_OnTimer);
2095 }
2096
2097 allow_scrollbar = TRUE;
2098
2099 focus = gui.in_focus;
2100 while (!s_timed_out)
2101 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002102 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002103 if (gui.in_focus != focus)
2104 {
2105 if (gui.in_focus)
2106 gui_mch_start_blink();
2107 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002108 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002109 focus = gui.in_focus;
2110 }
2111
2112 if (s_need_activate)
2113 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002114 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002115 s_need_activate = FALSE;
2116 }
2117
Bram Moolenaar4231da42016-06-02 14:30:04 +02002118#ifdef FEAT_TIMERS
2119 did_add_timer = FALSE;
2120#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002122 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002123 for (;;)
2124 {
2125 MSG msg;
2126
2127 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002128# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002129 if (did_add_timer)
2130 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002131# endif
Bram Moolenaar62426e12017-08-13 15:37:58 +02002132 if (pPeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
2133 {
2134 process_message();
2135 break;
2136 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002137 else if (input_available()
2138 || MsgWaitForMultipleObjects(0, NULL, FALSE, 100,
2139 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002140 break;
2141 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002142#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002143 // Don't use gui_mch_update() because then we will spin-lock until a
2144 // char arrives, instead we use GetMessage() to hang until an
2145 // event arrives. No need to check for input_buf_full because we are
2146 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002147 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002148#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002149
2150 if (input_available())
2151 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002152 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002153 allow_scrollbar = FALSE;
2154
Bram Moolenaar89c00032019-09-04 13:53:21 +02002155 // Clear pending mouse button, the release event may have been
2156 // taken by the dialog window. But don't do this when getting
2157 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002158 if (!s_getting_focus)
2159 s_button_pending = -1;
2160
2161 return OK;
2162 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002163
2164#ifdef FEAT_TIMERS
2165 if (did_add_timer)
2166 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002167 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002168 remove_any_timer();
2169 break;
2170 }
2171#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002172 }
2173 allow_scrollbar = FALSE;
2174 return FAIL;
2175}
2176
2177/*
2178 * Clear a rectangular region of the screen from text pos (row1, col1) to
2179 * (row2, col2) inclusive.
2180 */
2181 void
2182gui_mch_clear_block(
2183 int row1,
2184 int col1,
2185 int row2,
2186 int col2)
2187{
2188 RECT rc;
2189
2190 /*
2191 * Clear one extra pixel at the far right, for when bold characters have
2192 * spilled over to the window border.
2193 * Note: FillRect() excludes right and bottom of rectangle.
2194 */
2195 rc.left = FILL_X(col1);
2196 rc.top = FILL_Y(row1);
2197 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2198 rc.bottom = FILL_Y(row2 + 1);
2199 clear_rect(&rc);
2200}
2201
2202/*
2203 * Clear the whole text window.
2204 */
2205 void
2206gui_mch_clear_all(void)
2207{
2208 RECT rc;
2209
2210 rc.left = 0;
2211 rc.top = 0;
2212 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2213 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2214 clear_rect(&rc);
2215}
2216/*
2217 * Menu stuff.
2218 */
2219
2220 void
2221gui_mch_enable_menu(int flag)
2222{
2223#ifdef FEAT_MENU
2224 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2225#endif
2226}
2227
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002228 void
2229gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002230 int x UNUSED,
2231 int y UNUSED,
2232 int w UNUSED,
2233 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002234{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002235 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002236}
2237
2238#if defined(FEAT_MENU) || defined(PROTO)
2239/*
2240 * Make menu item hidden or not hidden
2241 */
2242 void
2243gui_mch_menu_hidden(
2244 vimmenu_T *menu,
2245 int hidden)
2246{
2247 /*
2248 * This doesn't do what we want. Hmm, just grey the menu items for now.
2249 */
2250 /*
2251 if (hidden)
2252 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2253 else
2254 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2255 */
2256 gui_mch_menu_grey(menu, hidden);
2257}
2258
2259/*
2260 * This is called after setting all the menus to grey/hidden or not.
2261 */
2262 void
2263gui_mch_draw_menubar(void)
2264{
2265 DrawMenuBar(s_hwnd);
2266}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002267#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002268
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269/*
2270 * Return the RGB value of a pixel as a long.
2271 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002272 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002273gui_mch_get_rgb(guicolor_T pixel)
2274{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002275 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2276 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002277}
2278
2279#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002280/*
2281 * Convert pixels in X to dialog units
2282 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002283 static WORD
2284PixelToDialogX(int numPixels)
2285{
2286 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2287}
2288
Bram Moolenaar734a8672019-12-02 22:49:38 +01002289/*
2290 * Convert pixels in Y to dialog units
2291 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002292 static WORD
2293PixelToDialogY(int numPixels)
2294{
2295 return (WORD)((numPixels * 8) / s_dlgfntheight);
2296}
2297
Bram Moolenaar734a8672019-12-02 22:49:38 +01002298/*
2299 * Return the width in pixels of the given text in the given DC.
2300 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301 static int
2302GetTextWidth(HDC hdc, char_u *str, int len)
2303{
2304 SIZE size;
2305
2306 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2307 return size.cx;
2308}
2309
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310/*
2311 * Return the width in pixels of the given text in the given DC, taking care
2312 * of 'encoding' to active codepage conversion.
2313 */
2314 static int
2315GetTextWidthEnc(HDC hdc, char_u *str, int len)
2316{
2317 SIZE size;
2318 WCHAR *wstr;
2319 int n;
2320 int wlen = len;
2321
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002322 wstr = enc_to_utf16(str, &wlen);
2323 if (wstr == NULL)
2324 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002325
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002326 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2327 vim_free(wstr);
2328 if (n)
2329 return size.cx;
2330 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002332
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002333static void get_work_area(RECT *spi_rect);
2334
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335/*
2336 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002337 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2338 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002339 */
2340 static BOOL
2341CenterWindow(
2342 HWND hwndChild,
2343 HWND hwndParent)
2344{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002345 HMONITOR mon;
2346 MONITORINFO moninfo;
2347 RECT rChild, rParent, rScreen;
2348 int wChild, hChild, wParent, hParent;
2349 int xNew, yNew;
2350 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351
2352 GetWindowRect(hwndChild, &rChild);
2353 wChild = rChild.right - rChild.left;
2354 hChild = rChild.bottom - rChild.top;
2355
Bram Moolenaar734a8672019-12-02 22:49:38 +01002356 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002358 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002359 else
2360 GetWindowRect(hwndParent, &rParent);
2361 wParent = rParent.right - rParent.left;
2362 hParent = rParent.bottom - rParent.top;
2363
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002364 moninfo.cbSize = sizeof(MONITORINFO);
2365 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2366 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002368 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002370 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002372 hdc = GetDC(hwndChild);
2373 rScreen.left = 0;
2374 rScreen.top = 0;
2375 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2376 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2377 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002378 }
2379
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002380 xNew = rParent.left + ((wParent - wChild) / 2);
2381 if (xNew < rScreen.left)
2382 xNew = rScreen.left;
2383 else if ((xNew + wChild) > rScreen.right)
2384 xNew = rScreen.right - wChild;
2385
2386 yNew = rParent.top + ((hParent - hChild) / 2);
2387 if (yNew < rScreen.top)
2388 yNew = rScreen.top;
2389 else if ((yNew + hChild) > rScreen.bottom)
2390 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391
2392 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2393 SWP_NOSIZE | SWP_NOZORDER);
2394}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002395#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002396
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397#if defined(FEAT_TOOLBAR) || defined(PROTO)
2398 void
2399gui_mch_show_toolbar(int showit)
2400{
2401 if (s_toolbarhwnd == NULL)
2402 return;
2403
2404 if (showit)
2405 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002406# ifndef TB_SETUNICODEFORMAT
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002407 // For older compilers. We assume this never changes.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002408# define TB_SETUNICODEFORMAT 0x2005
2409# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002410 // Enable unicode support
2411 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2412 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413 ShowWindow(s_toolbarhwnd, SW_SHOW);
2414 }
2415 else
2416 ShowWindow(s_toolbarhwnd, SW_HIDE);
2417}
2418
Bram Moolenaar734a8672019-12-02 22:49:38 +01002419// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002420# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421
2422#endif
2423
2424#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2425 static void
2426add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2427{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002428 WCHAR *wn;
2429 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002430
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002431 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002433 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002435 infow.cbSize = sizeof(infow);
2436 infow.fMask = MIIM_TYPE | MIIM_ID;
2437 infow.wID = item_id;
2438 infow.fType = MFT_STRING;
2439 infow.dwTypeData = wn;
2440 infow.cch = (UINT)wcslen(wn);
2441 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2442 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443}
2444
2445 static void
2446show_tabline_popup_menu(void)
2447{
2448 HMENU tab_pmenu;
2449 long rval;
2450 POINT pt;
2451
Bram Moolenaar734a8672019-12-02 22:49:38 +01002452 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 if (hold_gui_events
2454# ifdef FEAT_CMDWIN
2455 || cmdwin_type != 0
2456# endif
2457 )
2458 return;
2459
2460 tab_pmenu = CreatePopupMenu();
2461 if (tab_pmenu == NULL)
2462 return;
2463
2464 if (first_tabpage->tp_next != NULL)
2465 add_tabline_popup_menu_entry(tab_pmenu,
2466 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2467 add_tabline_popup_menu_entry(tab_pmenu,
2468 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2469 add_tabline_popup_menu_entry(tab_pmenu,
2470 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2471
2472 GetCursorPos(&pt);
2473 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2474 NULL);
2475
2476 DestroyMenu(tab_pmenu);
2477
Bram Moolenaar734a8672019-12-02 22:49:38 +01002478 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479 if (rval > 0)
2480 {
2481 TCHITTESTINFO htinfo;
2482 int idx;
2483
2484 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2485 return;
2486
2487 htinfo.pt.x = pt.x;
2488 htinfo.pt.y = pt.y;
2489 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2490 if (idx == -1)
2491 idx = 0;
2492 else
2493 idx += 1;
2494
2495 send_tabline_menu_event(idx, (int)rval);
2496 }
2497}
2498
2499/*
2500 * Show or hide the tabline.
2501 */
2502 void
2503gui_mch_show_tabline(int showit)
2504{
2505 if (s_tabhwnd == NULL)
2506 return;
2507
2508 if (!showit != !showing_tabline)
2509 {
2510 if (showit)
2511 ShowWindow(s_tabhwnd, SW_SHOW);
2512 else
2513 ShowWindow(s_tabhwnd, SW_HIDE);
2514 showing_tabline = showit;
2515 }
2516}
2517
2518/*
2519 * Return TRUE when tabline is displayed.
2520 */
2521 int
2522gui_mch_showing_tabline(void)
2523{
2524 return s_tabhwnd != NULL && showing_tabline;
2525}
2526
2527/*
2528 * Update the labels of the tabline.
2529 */
2530 void
2531gui_mch_update_tabline(void)
2532{
2533 tabpage_T *tp;
2534 TCITEM tie;
2535 int nr = 0;
2536 int curtabidx = 0;
2537 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002539
2540 if (s_tabhwnd == NULL)
2541 return;
2542
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002543# ifndef CCM_SETUNICODEFORMAT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002544 // For older compilers. We assume this never changes.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# define CCM_SETUNICODEFORMAT 0x2005
2546# endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002547 // Enable unicode support
2548 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002549
2550 tie.mask = TCIF_TEXT;
2551 tie.iImage = -1;
2552
Bram Moolenaar734a8672019-12-02 22:49:38 +01002553 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2555
Bram Moolenaar734a8672019-12-02 22:49:38 +01002556 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002557 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2558 {
2559 if (tp == curtab)
2560 curtabidx = nr;
2561
2562 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2563 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002564 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002565 tie.pszText = "-Empty-";
2566 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2567 tabadded = 1;
2568 }
2569
2570 get_tabline_label(tp, FALSE);
2571 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002572
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002573 wstr = enc_to_utf16(NameBuff, NULL);
2574 if (wstr != NULL)
2575 {
2576 TCITEMW tiw;
2577
2578 tiw.mask = TCIF_TEXT;
2579 tiw.iImage = -1;
2580 tiw.pszText = wstr;
2581 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2582 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002584 }
2585
Bram Moolenaar734a8672019-12-02 22:49:38 +01002586 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2588 TabCtrl_DeleteItem(s_tabhwnd, nr);
2589
2590 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2591 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2592
Bram Moolenaar734a8672019-12-02 22:49:38 +01002593 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2595 RedrawWindow(s_tabhwnd, NULL, NULL,
2596 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2597
2598 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2599 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2600}
2601
2602/*
2603 * Set the current tab to "nr". First tab is 1.
2604 */
2605 void
2606gui_mch_set_curtab(int nr)
2607{
2608 if (s_tabhwnd == NULL)
2609 return;
2610
2611 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2612 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2613}
2614
2615#endif
2616
2617/*
2618 * ":simalt" command.
2619 */
2620 void
2621ex_simalt(exarg_T *eap)
2622{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002623 char_u *keys = eap->arg;
2624 int fill_typebuf = FALSE;
2625 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002626
2627 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2628 while (*keys)
2629 {
2630 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002631 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002632 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2633 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002634 fill_typebuf = TRUE;
2635 }
2636 if (fill_typebuf)
2637 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002638 // Put a NOP in the typeahead buffer so that the message will get
2639 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002640 key_name[0] = K_SPECIAL;
2641 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002642 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002643 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002644#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002645 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002646#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002647 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002648 }
2649}
2650
2651/*
2652 * Create the find & replace dialogs.
2653 * You can't have both at once: ":find" when replace is showing, destroys
2654 * the replace dialog first, and the other way around.
2655 */
2656#ifdef MSWIN_FIND_REPLACE
2657 static void
2658initialise_findrep(char_u *initial_string)
2659{
2660 int wword = FALSE;
2661 int mcase = !p_ic;
2662 char_u *entry_text;
2663
Bram Moolenaar734a8672019-12-02 22:49:38 +01002664 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002665 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2666
2667 s_findrep_struct.hwndOwner = s_hwnd;
2668 s_findrep_struct.Flags = FR_DOWN;
2669 if (mcase)
2670 s_findrep_struct.Flags |= FR_MATCHCASE;
2671 if (wword)
2672 s_findrep_struct.Flags |= FR_WHOLEWORD;
2673 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002674 {
2675 WCHAR *p = enc_to_utf16(entry_text, NULL);
2676 if (p != NULL)
2677 {
2678 int len = s_findrep_struct.wFindWhatLen - 1;
2679
2680 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2681 s_findrep_struct.lpstrFindWhat[len] = NUL;
2682 vim_free(p);
2683 }
2684 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002685 vim_free(entry_text);
2686}
2687#endif
2688
2689 static void
2690set_window_title(HWND hwnd, char *title)
2691{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002692 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 {
2694 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002695
Bram Moolenaar734a8672019-12-02 22:49:38 +01002696 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2698 if (wbuf != NULL)
2699 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002700 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002702 }
2703 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002704 else
2705 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002706}
2707
2708 void
2709gui_mch_find_dialog(exarg_T *eap)
2710{
2711#ifdef MSWIN_FIND_REPLACE
2712 if (s_findrep_msg != 0)
2713 {
2714 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2715 DestroyWindow(s_findrep_hwnd);
2716
2717 if (!IsWindow(s_findrep_hwnd))
2718 {
2719 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002720 s_findrep_hwnd = FindTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 }
2722
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002723 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 (void)SetFocus(s_findrep_hwnd);
2725
2726 s_findrep_is_find = TRUE;
2727 }
2728#endif
2729}
2730
2731
2732 void
2733gui_mch_replace_dialog(exarg_T *eap)
2734{
2735#ifdef MSWIN_FIND_REPLACE
2736 if (s_findrep_msg != 0)
2737 {
2738 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2739 DestroyWindow(s_findrep_hwnd);
2740
2741 if (!IsWindow(s_findrep_hwnd))
2742 {
2743 initialise_findrep(eap->arg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002744 s_findrep_hwnd = ReplaceTextW((LPFINDREPLACEW) &s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 }
2746
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002747 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002748 (void)SetFocus(s_findrep_hwnd);
2749
2750 s_findrep_is_find = FALSE;
2751 }
2752#endif
2753}
2754
2755
2756/*
2757 * Set visibility of the pointer.
2758 */
2759 void
2760gui_mch_mousehide(int hide)
2761{
2762 if (hide != gui.pointer_hidden)
2763 {
2764 ShowCursor(!hide);
2765 gui.pointer_hidden = hide;
2766 }
2767}
2768
2769#ifdef FEAT_MENU
2770 static void
2771gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2772{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002773 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774 gui_mch_mousehide(FALSE);
2775
2776 (void)TrackPopupMenu(
2777 (HMENU)menu->submenu_id,
2778 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2779 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002780 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781 s_hwnd,
2782 NULL);
2783 /*
2784 * NOTE: The pop-up menu can eat the mouse up event.
2785 * We deal with this in normal.c.
2786 */
2787}
2788#endif
2789
2790/*
2791 * Got a message when the system will go down.
2792 */
2793 static void
2794_OnEndSession(void)
2795{
2796 getout_preserve_modified(1);
2797}
2798
2799/*
2800 * Get this message when the user clicks on the cross in the top right corner
2801 * of a Windows95 window.
2802 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002803 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002804_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805{
2806 gui_shell_closed();
2807}
2808
2809/*
2810 * Get a message when the window is being destroyed.
2811 */
2812 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002813_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002814{
2815 if (!destroying)
2816 _OnClose(hwnd);
2817}
2818
2819 static void
2820_OnPaint(
2821 HWND hwnd)
2822{
2823 if (!IsMinimized(hwnd))
2824 {
2825 PAINTSTRUCT ps;
2826
Bram Moolenaar734a8672019-12-02 22:49:38 +01002827 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002828 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002829
Bram Moolenaar734a8672019-12-02 22:49:38 +01002830 // prevent multi-byte characters from misprinting on an invalid
2831 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002832 if (has_mbyte)
2833 {
2834 RECT rect;
2835
2836 GetClientRect(hwnd, &rect);
2837 ps.rcPaint.left = rect.left;
2838 ps.rcPaint.right = rect.right;
2839 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840
2841 if (!IsRectEmpty(&ps.rcPaint))
2842 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002843 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2844 ps.rcPaint.right - ps.rcPaint.left + 1,
2845 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2846 }
2847
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848 EndPaint(hwnd, &ps);
2849 }
2850}
2851
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 static void
2853_OnSize(
2854 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002855 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 int cx,
2857 int cy)
2858{
2859 if (!IsMinimized(hwnd))
2860 {
2861 gui_resize_shell(cx, cy);
2862
2863#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002864 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 gui_mswin_get_menu_height(TRUE);
2866#endif
2867 }
2868}
2869
2870 static void
2871_OnSetFocus(
2872 HWND hwnd,
2873 HWND hwndOldFocus)
2874{
2875 gui_focus_change(TRUE);
2876 s_getting_focus = TRUE;
2877 (void)MyWindowProc(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
2878}
2879
2880 static void
2881_OnKillFocus(
2882 HWND hwnd,
2883 HWND hwndNewFocus)
2884{
2885 gui_focus_change(FALSE);
2886 s_getting_focus = FALSE;
2887 (void)MyWindowProc(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
2888}
2889
2890/*
2891 * Get a message when the user switches back to vim
2892 */
2893 static LRESULT
2894_OnActivateApp(
2895 HWND hwnd,
2896 BOOL fActivate,
2897 DWORD dwThreadId)
2898{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002899 // we call gui_focus_change() in _OnSetFocus()
2900 // gui_focus_change((int)fActivate);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901 return MyWindowProc(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
2902}
2903
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002904 void
2905gui_mch_destroy_scrollbar(scrollbar_T *sb)
2906{
2907 DestroyWindow(sb->id);
2908}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002909
2910/*
2911 * Get current mouse coordinates in text window.
2912 */
2913 void
2914gui_mch_getmouse(int *x, int *y)
2915{
2916 RECT rct;
2917 POINT mp;
2918
2919 (void)GetWindowRect(s_textArea, &rct);
2920 (void)GetCursorPos((LPPOINT)&mp);
2921 *x = (int)(mp.x - rct.left);
2922 *y = (int)(mp.y - rct.top);
2923}
2924
2925/*
2926 * Move mouse pointer to character at (x, y).
2927 */
2928 void
2929gui_mch_setmouse(int x, int y)
2930{
2931 RECT rct;
2932
2933 (void)GetWindowRect(s_textArea, &rct);
2934 (void)SetCursorPos(x + gui.border_offset + rct.left,
2935 y + gui.border_offset + rct.top);
2936}
2937
2938 static void
2939gui_mswin_get_valid_dimensions(
2940 int w,
2941 int h,
2942 int *valid_w,
2943 int *valid_h)
2944{
2945 int base_width, base_height;
2946
2947 base_width = gui_get_base_width()
2948 + (GetSystemMetrics(SM_CXFRAME) +
2949 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
2950 base_height = gui_get_base_height()
2951 + (GetSystemMetrics(SM_CYFRAME) +
2952 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
2953 + GetSystemMetrics(SM_CYCAPTION)
2954#ifdef FEAT_MENU
2955 + gui_mswin_get_menu_height(FALSE)
2956#endif
2957 ;
2958 *valid_w = base_width +
2959 ((w - base_width) / gui.char_width) * gui.char_width;
2960 *valid_h = base_height +
2961 ((h - base_height) / gui.char_height) * gui.char_height;
2962}
2963
2964 void
2965gui_mch_flash(int msec)
2966{
2967 RECT rc;
2968
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002969#if defined(FEAT_DIRECTX)
2970 if (IS_ENABLE_DIRECTX())
2971 DWriteContext_Flush(s_dwc);
2972#endif
2973
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002974 /*
2975 * Note: InvertRect() excludes right and bottom of rectangle.
2976 */
2977 rc.left = 0;
2978 rc.top = 0;
2979 rc.right = gui.num_cols * gui.char_width;
2980 rc.bottom = gui.num_rows * gui.char_height;
2981 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01002982 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002983
Bram Moolenaar734a8672019-12-02 22:49:38 +01002984 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002985
2986 InvertRect(s_hdc, &rc);
2987}
2988
2989/*
2990 * Return flags used for scrolling.
2991 * The SW_INVALIDATE is required when part of the window is covered or
2992 * off-screen. Refer to MS KB Q75236.
2993 */
2994 static int
2995get_scroll_flags(void)
2996{
2997 HWND hwnd;
2998 RECT rcVim, rcOther, rcDest;
2999
3000 GetWindowRect(s_hwnd, &rcVim);
3001
Bram Moolenaar734a8672019-12-02 22:49:38 +01003002 // Check if the window is partly above or below the screen. We don't care
3003 // about partly left or right of the screen, it is not relevant when
3004 // scrolling up or down.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003005 if (rcVim.top < 0 || rcVim.bottom > GetSystemMetrics(SM_CYFULLSCREEN))
3006 return SW_INVALIDATE;
3007
Bram Moolenaar734a8672019-12-02 22:49:38 +01003008 // Check if there is an window (partly) on top of us.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003009 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3010 if (IsWindowVisible(hwnd))
3011 {
3012 GetWindowRect(hwnd, &rcOther);
3013 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3014 return SW_INVALIDATE;
3015 }
3016 return 0;
3017}
3018
3019/*
3020 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3021 * may not be scrolled out properly.
3022 * For gVim, when _OnScroll() is repeated, the character at the
3023 * previous cursor position may be left drawn after scroll.
3024 * The problem can be avoided by calling GetPixel() to get a pixel in
3025 * the region before ScrollWindowEx().
3026 */
3027 static void
3028intel_gpu_workaround(void)
3029{
3030 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3031}
3032
3033/*
3034 * Delete the given number of lines from the given row, scrolling up any
3035 * text further down within the scroll region.
3036 */
3037 void
3038gui_mch_delete_lines(
3039 int row,
3040 int num_lines)
3041{
3042 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 rc.left = FILL_X(gui.scroll_region_left);
3045 rc.right = FILL_X(gui.scroll_region_right + 1);
3046 rc.top = FILL_Y(row);
3047 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3048
Bram Moolenaar92467d32017-12-05 13:22:16 +01003049#if defined(FEAT_DIRECTX)
3050 if (IS_ENABLE_DIRECTX())
3051 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003052 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
3053 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003054 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003055 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003056#endif
3057 {
3058 intel_gpu_workaround();
3059 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003061 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003062 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003063
Bram Moolenaar734a8672019-12-02 22:49:38 +01003064 // This seems to be required to avoid the cursor disappearing when
3065 // scrolling such that the cursor ends up in the top-left character on
3066 // the screen... But why? (Webb)
3067 // It's probably fixed by disabling drawing the cursor while scrolling.
3068 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003069
3070 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3071 gui.scroll_region_left,
3072 gui.scroll_region_bot, gui.scroll_region_right);
3073}
3074
3075/*
3076 * Insert the given number of lines before the given row, scrolling down any
3077 * following text within the scroll region.
3078 */
3079 void
3080gui_mch_insert_lines(
3081 int row,
3082 int num_lines)
3083{
3084 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003085
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003086 rc.left = FILL_X(gui.scroll_region_left);
3087 rc.right = FILL_X(gui.scroll_region_right + 1);
3088 rc.top = FILL_Y(row);
3089 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003090
3091#if defined(FEAT_DIRECTX)
3092 if (IS_ENABLE_DIRECTX())
3093 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003094 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
3095 DWriteContext_Flush(s_dwc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003096 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003097 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003098#endif
3099 {
3100 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003101 // The SW_INVALIDATE is required when part of the window is covered or
3102 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003103 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003104 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003105 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003106 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003108 gui_clear_block(row, gui.scroll_region_left,
3109 row + num_lines - 1, gui.scroll_region_right);
3110}
3111
3112
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003114gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003115{
3116#if defined(FEAT_DIRECTX)
3117 DWriteContext_Close(s_dwc);
3118 DWrite_Final();
3119 s_dwc = NULL;
3120#endif
3121
3122 ReleaseDC(s_textArea, s_hdc);
3123 DeleteObject(s_brush);
3124
3125#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003126 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003127 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3128#endif
3129
Bram Moolenaar734a8672019-12-02 22:49:38 +01003130 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003131 if (s_hwnd != NULL)
3132 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003133 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003134 DestroyWindow(s_hwnd);
3135 }
3136
3137#ifdef GLOBAL_IME
3138 global_ime_end();
3139#endif
3140}
3141
3142 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003143logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003144{
3145 char *p;
3146 char *res;
3147 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003148 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003149 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003150 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003152 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3153 if (font_name == NULL)
3154 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003155 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003156 quality_name = quality_id2name((int)lf.lfQuality);
3157
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003158 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003159 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003160 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161 if (res != NULL)
3162 {
3163 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003164 // make a normal font string out of the lf thing:
3165 points = pixels_to_points(
3166 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3167 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3168 sprintf((char *)p, "%s:h%d", font_name, points);
3169 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003170 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003171 while (*p)
3172 {
3173 if (*p == ' ')
3174 *p = '_';
3175 ++p;
3176 }
3177 if (lf.lfItalic)
3178 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003179 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003180 STRCAT(p, ":b");
3181 if (lf.lfUnderline)
3182 STRCAT(p, ":u");
3183 if (lf.lfStrikeOut)
3184 STRCAT(p, ":s");
3185 if (charset_name != NULL)
3186 {
3187 STRCAT(p, ":c");
3188 STRCAT(p, charset_name);
3189 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003190 if (quality_name != NULL)
3191 {
3192 STRCAT(p, ":q");
3193 STRCAT(p, quality_name);
3194 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 }
3196
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003197 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198 return (char_u *)res;
3199}
3200
3201
3202#ifdef FEAT_MBYTE_IME
3203/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003204 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003205 * 'guifont'
3206 */
3207 static void
3208update_im_font(void)
3209{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003210 LOGFONTW lf_wide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211
3212 if (p_guifontwide != NULL && *p_guifontwide != NUL
3213 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003214 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 norm_logfont = lf_wide;
3216 else
3217 norm_logfont = sub_logfont;
3218 im_set_font(&norm_logfont);
3219}
3220#endif
3221
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003222/*
3223 * Handler of gui.wide_font (p_guifontwide) changed notification.
3224 */
3225 void
3226gui_mch_wide_font_changed(void)
3227{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003228 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003229
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003230#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003231 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003232#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003233
3234 gui_mch_free_font(gui.wide_ital_font);
3235 gui.wide_ital_font = NOFONT;
3236 gui_mch_free_font(gui.wide_bold_font);
3237 gui.wide_bold_font = NOFONT;
3238 gui_mch_free_font(gui.wide_boldital_font);
3239 gui.wide_boldital_font = NOFONT;
3240
3241 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003242 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003243 {
3244 if (!lf.lfItalic)
3245 {
3246 lf.lfItalic = TRUE;
3247 gui.wide_ital_font = get_font_handle(&lf);
3248 lf.lfItalic = FALSE;
3249 }
3250 if (lf.lfWeight < FW_BOLD)
3251 {
3252 lf.lfWeight = FW_BOLD;
3253 gui.wide_bold_font = get_font_handle(&lf);
3254 if (!lf.lfItalic)
3255 {
3256 lf.lfItalic = TRUE;
3257 gui.wide_boldital_font = get_font_handle(&lf);
3258 }
3259 }
3260 }
3261}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
3263/*
3264 * Initialise vim to use the font with the given name.
3265 * Return FAIL if the font could not be loaded, OK otherwise.
3266 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003267 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003268gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003270 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271 GuiFont font = NOFONT;
3272 char_u *p;
3273
Bram Moolenaar734a8672019-12-02 22:49:38 +01003274 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
3276 font = get_font_handle(&lf);
3277 if (font == NOFONT)
3278 return FAIL;
3279
3280 if (font_name == NULL)
3281 font_name = (char_u *)lf.lfFaceName;
3282#if defined(FEAT_MBYTE_IME) || defined(GLOBAL_IME)
3283 norm_logfont = lf;
Bram Moolenaarbdb81392017-11-27 23:24:08 +01003284#endif
3285#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003286 sub_logfont = lf;
3287#endif
3288#ifdef FEAT_MBYTE_IME
3289 update_im_font();
3290#endif
3291 gui_mch_free_font(gui.norm_font);
3292 gui.norm_font = font;
3293 current_font_height = lf.lfHeight;
3294 GetFontSize(font);
3295
3296 p = logfont2name(lf);
3297 if (p != NULL)
3298 {
3299 hl_set_font_name(p);
3300
Bram Moolenaar734a8672019-12-02 22:49:38 +01003301 // When setting 'guifont' to "*" replace it with the actual font name.
3302 //
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3304 {
3305 vim_free(p_guifont);
3306 p_guifont = p;
3307 }
3308 else
3309 vim_free(p);
3310 }
3311
3312 gui_mch_free_font(gui.ital_font);
3313 gui.ital_font = NOFONT;
3314 gui_mch_free_font(gui.bold_font);
3315 gui.bold_font = NOFONT;
3316 gui_mch_free_font(gui.boldital_font);
3317 gui.boldital_font = NOFONT;
3318
3319 if (!lf.lfItalic)
3320 {
3321 lf.lfItalic = TRUE;
3322 gui.ital_font = get_font_handle(&lf);
3323 lf.lfItalic = FALSE;
3324 }
3325 if (lf.lfWeight < FW_BOLD)
3326 {
3327 lf.lfWeight = FW_BOLD;
3328 gui.bold_font = get_font_handle(&lf);
3329 if (!lf.lfItalic)
3330 {
3331 lf.lfItalic = TRUE;
3332 gui.boldital_font = get_font_handle(&lf);
3333 }
3334 }
3335
3336 return OK;
3337}
3338
3339#ifndef WPF_RESTORETOMAXIMIZED
Bram Moolenaar734a8672019-12-02 22:49:38 +01003340# define WPF_RESTORETOMAXIMIZED 2 // just in case someone doesn't have it
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003341#endif
3342
3343/*
3344 * Return TRUE if the GUI window is maximized, filling the whole screen.
3345 */
3346 int
3347gui_mch_maximized(void)
3348{
3349 WINDOWPLACEMENT wp;
3350
3351 wp.length = sizeof(WINDOWPLACEMENT);
3352 if (GetWindowPlacement(s_hwnd, &wp))
3353 return wp.showCmd == SW_SHOWMAXIMIZED
3354 || (wp.showCmd == SW_SHOWMINIMIZED
3355 && wp.flags == WPF_RESTORETOMAXIMIZED);
3356
3357 return 0;
3358}
3359
3360/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003361 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3362 * is set. Compute the new Rows and Columns. This is like resizing the
3363 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003364 */
3365 void
3366gui_mch_newfont(void)
3367{
3368 RECT rect;
3369
3370 GetWindowRect(s_hwnd, &rect);
3371 if (win_socket_id == 0)
3372 {
3373 gui_resize_shell(rect.right - rect.left
3374 - (GetSystemMetrics(SM_CXFRAME) +
3375 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2,
3376 rect.bottom - rect.top
3377 - (GetSystemMetrics(SM_CYFRAME) +
3378 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
3379 - GetSystemMetrics(SM_CYCAPTION)
3380#ifdef FEAT_MENU
3381 - gui_mswin_get_menu_height(FALSE)
3382#endif
3383 );
3384 }
3385 else
3386 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003387 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 gui_resize_shell(rect.right - rect.left,
3389 rect.bottom - rect.top
3390#ifdef FEAT_MENU
3391 - gui_mswin_get_menu_height(FALSE)
3392#endif
3393 );
3394 }
3395}
3396
3397/*
3398 * Set the window title
3399 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400 void
3401gui_mch_settitle(
3402 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003403 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003404{
3405 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3406}
3407
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003408#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003409// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3410// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411static LPCSTR mshape_idcs[] =
3412{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003413 IDC_ARROW, // arrow
3414 MAKEINTRESOURCE(0), // blank
3415 IDC_IBEAM, // beam
3416 IDC_SIZENS, // updown
3417 IDC_SIZENS, // udsizing
3418 IDC_SIZEWE, // leftright
3419 IDC_SIZEWE, // lrsizing
3420 IDC_WAIT, // busy
3421 IDC_NO, // no
3422 IDC_ARROW, // crosshair
3423 IDC_ARROW, // hand1
3424 IDC_ARROW, // hand2
3425 IDC_ARROW, // pencil
3426 IDC_ARROW, // question
3427 IDC_ARROW, // right-arrow
3428 IDC_UPARROW, // up-arrow
3429 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430};
3431
3432 void
3433mch_set_mouse_shape(int shape)
3434{
3435 LPCSTR idc;
3436
3437 if (shape == MSHAPE_HIDE)
3438 ShowCursor(FALSE);
3439 else
3440 {
3441 if (shape >= MSHAPE_NUMBERED)
3442 idc = IDC_ARROW;
3443 else
3444 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003445 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446 if (!p_mh)
3447 {
3448 POINT mp;
3449
Bram Moolenaar734a8672019-12-02 22:49:38 +01003450 // Set the position to make it redrawn with the new shape.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451 (void)GetCursorPos((LPPOINT)&mp);
3452 (void)SetCursorPos(mp.x, mp.y);
3453 ShowCursor(TRUE);
3454 }
3455 }
3456}
3457#endif
3458
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003459#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003460/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003461 * Wide version of convert_filter().
3462 */
3463 static WCHAR *
3464convert_filterW(char_u *s)
3465{
3466 char_u *tmp;
3467 int len;
3468 WCHAR *res;
3469
3470 tmp = convert_filter(s);
3471 if (tmp == NULL)
3472 return NULL;
3473 len = (int)STRLEN(s) + 3;
3474 res = enc_to_utf16(tmp, &len);
3475 vim_free(tmp);
3476 return res;
3477}
3478
3479/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003480 * Pop open a file browser and return the file selected, in allocated memory,
3481 * or NULL if Cancel is hit.
3482 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3483 * title - Title message for the file browser dialog.
3484 * dflt - Default name of file.
3485 * ext - Default extension to be added to files without extensions.
3486 * initdir - directory in which to open the browser (NULL = current dir)
3487 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003489 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003490gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491 int saving,
3492 char_u *title,
3493 char_u *dflt,
3494 char_u *ext,
3495 char_u *initdir,
3496 char_u *filter)
3497{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003498 // We always use the wide function. This means enc_to_utf16() must work,
3499 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 OPENFILENAMEW fileStruct;
3501 WCHAR fileBuf[MAXPATHL];
3502 WCHAR *wp;
3503 int i;
3504 WCHAR *titlep = NULL;
3505 WCHAR *extp = NULL;
3506 WCHAR *initdirp = NULL;
3507 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003508 char_u *p, *q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003509
3510 if (dflt == NULL)
3511 fileBuf[0] = NUL;
3512 else
3513 {
3514 wp = enc_to_utf16(dflt, NULL);
3515 if (wp == NULL)
3516 fileBuf[0] = NUL;
3517 else
3518 {
3519 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3520 fileBuf[i] = wp[i];
3521 fileBuf[i] = NUL;
3522 vim_free(wp);
3523 }
3524 }
3525
Bram Moolenaar734a8672019-12-02 22:49:38 +01003526 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003527 filterp = convert_filterW(filter);
3528
Bram Moolenaara80faa82020-04-12 19:37:17 +02003529 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003530# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003531 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003532 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003533# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003534 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003535# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003536
3537 if (title != NULL)
3538 titlep = enc_to_utf16(title, NULL);
3539 fileStruct.lpstrTitle = titlep;
3540
3541 if (ext != NULL)
3542 extp = enc_to_utf16(ext, NULL);
3543 fileStruct.lpstrDefExt = extp;
3544
3545 fileStruct.lpstrFile = fileBuf;
3546 fileStruct.nMaxFile = MAXPATHL;
3547 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003548 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3549 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550 if (initdir != NULL && *initdir != NUL)
3551 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003552 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003553 initdirp = enc_to_utf16(initdir, NULL);
3554 if (initdirp != NULL)
3555 {
3556 for (wp = initdirp; *wp != NUL; ++wp)
3557 if (*wp == '/')
3558 *wp = '\\';
3559 }
3560 fileStruct.lpstrInitialDir = initdirp;
3561 }
3562
3563 /*
3564 * TODO: Allow selection of multiple files. Needs another arg to this
3565 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3566 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3567 * files that don't exist yet, so I haven't put it in. What about
3568 * OFN_PATHMUSTEXIST?
3569 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3570 */
3571 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003572# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 if (curbuf->b_p_bin)
3574 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003575# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003576 if (saving)
3577 {
3578 if (!GetSaveFileNameW(&fileStruct))
3579 return NULL;
3580 }
3581 else
3582 {
3583 if (!GetOpenFileNameW(&fileStruct))
3584 return NULL;
3585 }
3586
3587 vim_free(filterp);
3588 vim_free(initdirp);
3589 vim_free(titlep);
3590 vim_free(extp);
3591
Bram Moolenaar734a8672019-12-02 22:49:38 +01003592 // Convert from UCS2 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003594 if (p == NULL)
3595 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596
Bram Moolenaar734a8672019-12-02 22:49:38 +01003597 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003598 SetFocus(s_hwnd);
3599
Bram Moolenaar734a8672019-12-02 22:49:38 +01003600 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003601 q = vim_strsave(shorten_fname1(p));
3602 vim_free(p);
3603 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605
3606
3607/*
3608 * Convert the string s to the proper format for a filter string by replacing
3609 * the \t and \n delimiters with \0.
3610 * Returns the converted string in allocated memory.
3611 *
3612 * Keep in sync with convert_filterW() above!
3613 */
3614 static char_u *
3615convert_filter(char_u *s)
3616{
3617 char_u *res;
3618 unsigned s_len = (unsigned)STRLEN(s);
3619 unsigned i;
3620
3621 res = alloc(s_len + 3);
3622 if (res != NULL)
3623 {
3624 for (i = 0; i < s_len; ++i)
3625 if (s[i] == '\t' || s[i] == '\n')
3626 res[i] = '\0';
3627 else
3628 res[i] = s[i];
3629 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003630 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003631 res[s_len + 1] = NUL;
3632 res[s_len + 2] = NUL;
3633 }
3634 return res;
3635}
3636
3637/*
3638 * Select a directory.
3639 */
3640 char_u *
3641gui_mch_browsedir(char_u *title, char_u *initdir)
3642{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003643 // We fake this: Use a filter that doesn't select anything and a default
3644 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3646 initdir, (char_u *)_("Directory\t*.nothing\n"));
3647}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003648#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003649
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 static void
3651_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003652 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 HDROP hDrop)
3654{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003655#define BUFPATHLEN _MAX_PATH
3656#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003657 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658 char szFile[BUFPATHLEN];
3659 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3660 UINT i;
3661 char_u **fnames;
3662 POINT pt;
3663 int_u modifiers = 0;
3664
Bram Moolenaar734a8672019-12-02 22:49:38 +01003665 // TRACE("_OnDropFiles: %d files dropped\n", cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666
Bram Moolenaar734a8672019-12-02 22:49:38 +01003667 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 DragQueryPoint(hDrop, &pt);
3669 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3670
3671 reset_VIsual();
3672
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003673 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674
3675 if (fnames != NULL)
3676 for (i = 0; i < cFiles; ++i)
3677 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3679 fnames[i] = utf16_to_enc(wszFile, NULL);
3680 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003681 {
3682 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3683 fnames[i] = vim_strsave((char_u *)szFile);
3684 }
3685 }
3686
3687 DragFinish(hDrop);
3688
3689 if (fnames != NULL)
3690 {
3691 if ((GetKeyState(VK_SHIFT) & 0x8000) != 0)
3692 modifiers |= MOUSE_SHIFT;
3693 if ((GetKeyState(VK_CONTROL) & 0x8000) != 0)
3694 modifiers |= MOUSE_CTRL;
3695 if ((GetKeyState(VK_MENU) & 0x8000) != 0)
3696 modifiers |= MOUSE_ALT;
3697
3698 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3699
3700 s_need_activate = TRUE;
3701 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702}
3703
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 static int
3705_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003706 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003707 HWND hwndCtl,
3708 UINT code,
3709 int pos)
3710{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003711 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003712 scrollbar_T *sb, *sb_info;
3713 long val;
3714 int dragging = FALSE;
3715 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 SCROLLINFO si;
3717
3718 si.cbSize = sizeof(si);
3719 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003720
3721 sb = gui_mswin_find_scrollbar(hwndCtl);
3722 if (sb == NULL)
3723 return 0;
3724
Bram Moolenaar734a8672019-12-02 22:49:38 +01003725 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 {
3727 /*
3728 * Careful: need to get scrollbar info out of first (left) scrollbar
3729 * for window, but keep real scrollbar too because we must pass it to
3730 * gui_drag_scrollbar().
3731 */
3732 sb_info = &sb->wp->w_scrollbars[0];
3733 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003734 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 sb_info = sb;
3736 val = sb_info->value;
3737
3738 switch (code)
3739 {
3740 case SB_THUMBTRACK:
3741 val = pos;
3742 dragging = TRUE;
3743 if (sb->scroll_shift > 0)
3744 val <<= sb->scroll_shift;
3745 break;
3746 case SB_LINEDOWN:
3747 val++;
3748 break;
3749 case SB_LINEUP:
3750 val--;
3751 break;
3752 case SB_PAGEDOWN:
3753 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3754 break;
3755 case SB_PAGEUP:
3756 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3757 break;
3758 case SB_TOP:
3759 val = 0;
3760 break;
3761 case SB_BOTTOM:
3762 val = sb_info->max;
3763 break;
3764 case SB_ENDSCROLL:
3765 if (prev_code == SB_THUMBTRACK)
3766 {
3767 /*
3768 * "pos" only gives us 16-bit data. In case of large file,
3769 * use GetScrollPos() which returns 32-bit. Unfortunately it
3770 * is not valid while the scrollbar is being dragged.
3771 */
3772 val = GetScrollPos(hwndCtl, SB_CTL);
3773 if (sb->scroll_shift > 0)
3774 val <<= sb->scroll_shift;
3775 }
3776 break;
3777
3778 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003779 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003780 return 0;
3781 }
3782 prev_code = code;
3783
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3785 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786
3787 /*
3788 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3789 */
3790 if (sb->wp != NULL)
3791 {
3792 scrollbar_T *sba = sb->wp->w_scrollbars;
3793 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3794
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 }
3797
Bram Moolenaar734a8672019-12-02 22:49:38 +01003798 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003799 s_busy_processing = TRUE;
3800
Bram Moolenaar734a8672019-12-02 22:49:38 +01003801 // When "allow_scrollbar" is FALSE still need to remember the new
3802 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 dont_scroll = !allow_scrollbar;
3804
Bram Moolenaara338adc2018-01-31 20:51:47 +01003805 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01003807 mch_enable_flush();
3808 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809
3810 s_busy_processing = FALSE;
3811 dont_scroll = dont_scroll_save;
3812
3813 return 0;
3814}
3815
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817#ifdef FEAT_XPM_W32
3818# include "xpm_w32.h"
3819#endif
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821#ifdef __MINGW32__
3822/*
3823 * Add a lot of missing defines.
3824 * They are not always missing, we need the #ifndef's.
3825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826# ifndef IsMinimized
3827# define IsMinimized(hwnd) IsIconic(hwnd)
3828# endif
3829# ifndef IsMaximized
3830# define IsMaximized(hwnd) IsZoomed(hwnd)
3831# endif
3832# ifndef SelectFont
3833# define SelectFont(hdc, hfont) ((HFONT)SelectObject((hdc), (HGDIOBJ)(HFONT)(hfont)))
3834# endif
3835# ifndef GetStockBrush
3836# define GetStockBrush(i) ((HBRUSH)GetStockObject(i))
3837# endif
3838# ifndef DeleteBrush
3839# define DeleteBrush(hbr) DeleteObject((HGDIOBJ)(HBRUSH)(hbr))
3840# endif
3841
3842# ifndef HANDLE_WM_RBUTTONDBLCLK
3843# define HANDLE_WM_RBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3844 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3845# endif
3846# ifndef HANDLE_WM_MBUTTONUP
3847# define HANDLE_WM_MBUTTONUP(hwnd, wParam, lParam, fn) \
3848 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3849# endif
3850# ifndef HANDLE_WM_MBUTTONDBLCLK
3851# define HANDLE_WM_MBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3852 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3853# endif
3854# ifndef HANDLE_WM_LBUTTONDBLCLK
3855# define HANDLE_WM_LBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
3856 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3857# endif
3858# ifndef HANDLE_WM_RBUTTONDOWN
3859# define HANDLE_WM_RBUTTONDOWN(hwnd, wParam, lParam, fn) \
3860 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3861# endif
3862# ifndef HANDLE_WM_MOUSEMOVE
3863# define HANDLE_WM_MOUSEMOVE(hwnd, wParam, lParam, fn) \
3864 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3865# endif
3866# ifndef HANDLE_WM_RBUTTONUP
3867# define HANDLE_WM_RBUTTONUP(hwnd, wParam, lParam, fn) \
3868 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3869# endif
3870# ifndef HANDLE_WM_MBUTTONDOWN
3871# define HANDLE_WM_MBUTTONDOWN(hwnd, wParam, lParam, fn) \
3872 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3873# endif
3874# ifndef HANDLE_WM_LBUTTONUP
3875# define HANDLE_WM_LBUTTONUP(hwnd, wParam, lParam, fn) \
3876 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3877# endif
3878# ifndef HANDLE_WM_LBUTTONDOWN
3879# define HANDLE_WM_LBUTTONDOWN(hwnd, wParam, lParam, fn) \
3880 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
3881# endif
3882# ifndef HANDLE_WM_SYSCHAR
3883# define HANDLE_WM_SYSCHAR(hwnd, wParam, lParam, fn) \
3884 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3885# endif
3886# ifndef HANDLE_WM_ACTIVATEAPP
3887# define HANDLE_WM_ACTIVATEAPP(hwnd, wParam, lParam, fn) \
3888 ((fn)((hwnd), (BOOL)(wParam), (DWORD)(lParam)), 0L)
3889# endif
3890# ifndef HANDLE_WM_WINDOWPOSCHANGING
3891# define HANDLE_WM_WINDOWPOSCHANGING(hwnd, wParam, lParam, fn) \
3892 (LRESULT)(DWORD)(BOOL)(fn)((hwnd), (LPWINDOWPOS)(lParam))
3893# endif
3894# ifndef HANDLE_WM_VSCROLL
3895# define HANDLE_WM_VSCROLL(hwnd, wParam, lParam, fn) \
3896 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3897# endif
3898# ifndef HANDLE_WM_SETFOCUS
3899# define HANDLE_WM_SETFOCUS(hwnd, wParam, lParam, fn) \
3900 ((fn)((hwnd), (HWND)(wParam)), 0L)
3901# endif
3902# ifndef HANDLE_WM_KILLFOCUS
3903# define HANDLE_WM_KILLFOCUS(hwnd, wParam, lParam, fn) \
3904 ((fn)((hwnd), (HWND)(wParam)), 0L)
3905# endif
3906# ifndef HANDLE_WM_HSCROLL
3907# define HANDLE_WM_HSCROLL(hwnd, wParam, lParam, fn) \
3908 ((fn)((hwnd), (HWND)(lParam), (UINT)(LOWORD(wParam)), (int)(short)HIWORD(wParam)), 0L)
3909# endif
3910# ifndef HANDLE_WM_DROPFILES
3911# define HANDLE_WM_DROPFILES(hwnd, wParam, lParam, fn) \
3912 ((fn)((hwnd), (HDROP)(wParam)), 0L)
3913# endif
3914# ifndef HANDLE_WM_CHAR
3915# define HANDLE_WM_CHAR(hwnd, wParam, lParam, fn) \
3916 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3917# endif
3918# ifndef HANDLE_WM_SYSDEADCHAR
3919# define HANDLE_WM_SYSDEADCHAR(hwnd, wParam, lParam, fn) \
3920 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3921# endif
3922# ifndef HANDLE_WM_DEADCHAR
3923# define HANDLE_WM_DEADCHAR(hwnd, wParam, lParam, fn) \
3924 ((fn)((hwnd), (TCHAR)(wParam), (int)(short)LOWORD(lParam)), 0L)
3925# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01003926#endif // __MINGW32__
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928
Bram Moolenaar734a8672019-12-02 22:49:38 +01003929// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930#define TEAROFF_PADDING_X 2
3931#define TEAROFF_BUTTON_PAD_X 8
3932#define TEAROFF_MIN_WIDTH 200
3933#define TEAROFF_SUBMENU_LABEL ">>"
3934#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
3935
3936
Bram Moolenaar734a8672019-12-02 22:49:38 +01003937// For the Intellimouse:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938#ifndef WM_MOUSEWHEEL
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003939# define WM_MOUSEWHEEL 0x20a
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#endif
3941
3942
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01003943#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944# define ID_BEVAL_TOOLTIP 200
3945# define BEVAL_TEXT_LEN MAXPATHL
3946
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003947# if (defined(_MSC_VER) && _MSC_VER < 1300) || !defined(MAXULONG_PTR)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003948// Work around old versions of basetsd.h which wrongly declares
3949// UINT_PTR as unsigned long.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003950# undef UINT_PTR
3951# define UINT_PTR UINT
3952# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003953
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954static BalloonEval *cur_beval = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003955static UINT_PTR BevalTimerId = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956static DWORD LastActivity = 0;
Bram Moolenaar45360022005-07-21 21:08:21 +00003957
Bram Moolenaar82881492012-11-20 16:53:39 +01003958
Bram Moolenaar734a8672019-12-02 22:49:38 +01003959// cproto fails on missing include files
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003960# ifndef PROTO
Bram Moolenaar82881492012-11-20 16:53:39 +01003961
Bram Moolenaar45360022005-07-21 21:08:21 +00003962/*
3963 * excerpts from headers since this may not be presented
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00003964 * in the extremely old compilers
Bram Moolenaar45360022005-07-21 21:08:21 +00003965 */
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003966# include <pshpack1.h>
Bram Moolenaar82881492012-11-20 16:53:39 +01003967
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003968# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00003969
3970typedef struct _DllVersionInfo
3971{
3972 DWORD cbSize;
3973 DWORD dwMajorVersion;
3974 DWORD dwMinorVersion;
3975 DWORD dwBuildNumber;
3976 DWORD dwPlatformID;
3977} DLLVERSIONINFO;
3978
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003979# ifndef PROTO
3980# include <poppack.h>
3981# endif
Bram Moolenaar281daf62009-12-24 15:11:40 +00003982
Bram Moolenaar45360022005-07-21 21:08:21 +00003983typedef struct tagTOOLINFOA_NEW
3984{
Bram Moolenaard385b5d2018-12-27 22:43:08 +01003985 UINT cbSize;
3986 UINT uFlags;
3987 HWND hwnd;
3988 UINT_PTR uId;
3989 RECT rect;
3990 HINSTANCE hinst;
3991 LPSTR lpszText;
3992 LPARAM lParam;
Bram Moolenaar45360022005-07-21 21:08:21 +00003993} TOOLINFO_NEW;
3994
3995typedef struct tagNMTTDISPINFO_NEW
3996{
3997 NMHDR hdr;
Bram Moolenaar281daf62009-12-24 15:11:40 +00003998 LPSTR lpszText;
Bram Moolenaar45360022005-07-21 21:08:21 +00003999 char szText[80];
4000 HINSTANCE hinst;
4001 UINT uFlags;
4002 LPARAM lParam;
4003} NMTTDISPINFO_NEW;
4004
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004005typedef struct tagTOOLINFOW_NEW
4006{
4007 UINT cbSize;
4008 UINT uFlags;
4009 HWND hwnd;
4010 UINT_PTR uId;
4011 RECT rect;
4012 HINSTANCE hinst;
4013 LPWSTR lpszText;
4014 LPARAM lParam;
4015 void *lpReserved;
4016} TOOLINFOW_NEW;
4017
4018typedef struct tagNMTTDISPINFOW_NEW
4019{
4020 NMHDR hdr;
4021 LPWSTR lpszText;
4022 WCHAR szText[80];
4023 HINSTANCE hinst;
4024 UINT uFlags;
4025 LPARAM lParam;
4026} NMTTDISPINFOW_NEW;
4027
Bram Moolenaard385b5d2018-12-27 22:43:08 +01004028
Bram Moolenaar45360022005-07-21 21:08:21 +00004029typedef HRESULT (WINAPI* DLLGETVERSIONPROC)(DLLVERSIONINFO *);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004030# ifndef TTM_SETMAXTIPWIDTH
4031# define TTM_SETMAXTIPWIDTH (WM_USER+24)
4032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004034# ifndef TTF_DI_SETITEM
4035# define TTF_DI_SETITEM 0x8000
4036# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004037
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004038# ifndef TTN_GETDISPINFO
4039# define TTN_GETDISPINFO (TTN_FIRST - 0)
4040# endif
Bram Moolenaar45360022005-07-21 21:08:21 +00004041
Bram Moolenaar734a8672019-12-02 22:49:38 +01004042#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004043
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004044#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar734a8672019-12-02 22:49:38 +01004045// Older MSVC compilers don't have LPNMTTDISPINFO[AW] thus we need to define
4046// it here if LPNMTTDISPINFO isn't defined.
4047// MingW doesn't define LPNMTTDISPINFO but typedefs it. Thus we need to check
4048// _MSC_VER.
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004049# if !defined(LPNMTTDISPINFO) && defined(_MSC_VER)
4050typedef struct tagNMTTDISPINFOA {
4051 NMHDR hdr;
4052 LPSTR lpszText;
4053 char szText[80];
4054 HINSTANCE hinst;
4055 UINT uFlags;
4056 LPARAM lParam;
4057} NMTTDISPINFOA, *LPNMTTDISPINFOA;
4058# define LPNMTTDISPINFO LPNMTTDISPINFOA
4059
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004060typedef struct tagNMTTDISPINFOW {
4061 NMHDR hdr;
4062 LPWSTR lpszText;
4063 WCHAR szText[80];
4064 HINSTANCE hinst;
4065 UINT uFlags;
4066 LPARAM lParam;
4067} NMTTDISPINFOW, *LPNMTTDISPINFOW;
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00004068# endif
4069#endif
4070
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004071#ifndef TTN_GETDISPINFOW
4072# define TTN_GETDISPINFOW (TTN_FIRST - 10)
4073#endif
4074
Bram Moolenaar734a8672019-12-02 22:49:38 +01004075// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077#ifdef FEAT_MENU
4078static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081/*
4082 * Use the system font for dialogs and tear-off menus. Remove this line to
4083 * use DLG_FONT_NAME.
4084 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004085#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#define VIM_CLASSW L"Vim"
4089
Bram Moolenaar734a8672019-12-02 22:49:38 +01004090// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4091// thus there should be room for every dialog. For tearoffs it's made bigger
4092// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093#define DLG_ALLOC_SIZE 16 * 1024
4094
4095/*
4096 * stuff for dialogs, menus, tearoffs etc.
4097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098static PWORD
4099add_dialog_element(
4100 PWORD p,
4101 DWORD lStyle,
4102 WORD x,
4103 WORD y,
4104 WORD w,
4105 WORD h,
4106 WORD Id,
4107 WORD clss,
4108 const char *caption);
4109static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004110static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004111#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114static void get_dialog_font_metrics(void);
4115
4116static int dialog_default_button = -1;
4117
Bram Moolenaar734a8672019-12-02 22:49:38 +01004118// Intellimouse support
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119static int mouse_scroll_lines = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaar734a8672019-12-02 22:49:38 +01004121static int s_usenewlook; // emulate W95/NT4 non-bold dialogs
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122#ifdef FEAT_TOOLBAR
4123static void initialise_toolbar(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004124static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125static int get_toolbar_bitmap(vimmenu_T *menu);
4126#endif
4127
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004128#ifdef FEAT_GUI_TABLINE
4129static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004130static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004131#endif
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133#ifdef FEAT_MBYTE_IME
4134static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4135static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4136#endif
4137#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4138# ifdef NOIME
4139typedef struct tagCOMPOSITIONFORM {
4140 DWORD dwStyle;
4141 POINT ptCurrentPos;
4142 RECT rcArea;
4143} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4144typedef HANDLE HIMC;
4145# endif
4146
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004147static HINSTANCE hLibImm = NULL;
4148static LONG (WINAPI *pImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
4149static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4150static HIMC (WINAPI *pImmGetContext)(HWND);
4151static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4152static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4153static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4154static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004155static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4156static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004157static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4158static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004159static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160static void dyn_imm_load(void);
4161#else
4162# define pImmGetCompositionStringA ImmGetCompositionStringA
4163# define pImmGetCompositionStringW ImmGetCompositionStringW
4164# define pImmGetContext ImmGetContext
4165# define pImmAssociateContext ImmAssociateContext
4166# define pImmReleaseContext ImmReleaseContext
4167# define pImmGetOpenStatus ImmGetOpenStatus
4168# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004169# define pImmGetCompositionFontW ImmGetCompositionFontW
4170# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171# define pImmSetCompositionWindow ImmSetCompositionWindow
4172# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004173# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#endif
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#ifdef FEAT_MENU
4177/*
4178 * Figure out how high the menu bar is at the moment.
4179 */
4180 static int
4181gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004182 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 static int old_menu_height = -1;
4185
4186 RECT rc1, rc2;
4187 int num;
4188 int menu_height;
4189
4190 if (gui.menu_is_active)
4191 num = GetMenuItemCount(s_menuBar);
4192 else
4193 num = 0;
4194
4195 if (num == 0)
4196 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004197 else if (IsMinimized(s_hwnd))
4198 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004199 // The height of the menu cannot be determined while the window is
4200 // minimized. Take the previous height if the menu is changed in that
4201 // state, to avoid that Vim's vertical window size accidentally
4202 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004203 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 else
4206 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004207 /*
4208 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4209 * seem to have been set yet, so menu wraps in default window
4210 * width which is very narrow. Instead just return height of a
4211 * single menu item. Will still be wrong when the menu really
4212 * should wrap over more than one line.
4213 */
4214 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4215 if (gui.starting)
4216 menu_height = rc1.bottom - rc1.top + 1;
4217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004219 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4220 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 }
4223
4224 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004225 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004226 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 return menu_height;
4229}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004230#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
4232
4233/*
4234 * Setup for the Intellimouse
4235 */
4236 static void
4237init_mouse_wheel(void)
4238{
4239
4240#ifndef SPI_GETWHEELSCROLLLINES
4241# define SPI_GETWHEELSCROLLLINES 104
4242#endif
Bram Moolenaare7566042005-06-17 22:00:15 +00004243#ifndef SPI_SETWHEELSCROLLLINES
4244# define SPI_SETWHEELSCROLLLINES 105
4245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar734a8672019-12-02 22:49:38 +01004247#define VMOUSEZ_CLASSNAME "MouseZ" // hidden wheel window class
4248#define VMOUSEZ_TITLE "Magellan MSWHEEL" // hidden wheel window title
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249#define VMSH_MOUSEWHEEL "MSWHEEL_ROLLMSG"
4250#define VMSH_SCROLL_LINES "MSH_SCROLL_LINES_MSG"
4251
Bram Moolenaar734a8672019-12-02 22:49:38 +01004252 mouse_scroll_lines = 3; // reasonable default
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar734a8672019-12-02 22:49:38 +01004254 // if NT 4.0+ (or Win98) get scroll lines directly from system
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004255 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4256 &mouse_scroll_lines, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257}
4258
4259
Bram Moolenaarae20f342019-11-05 21:09:23 +01004260/*
4261 * Intellimouse wheel handler.
4262 * Treat a mouse wheel event as if it were a scroll request.
4263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 static void
4265_OnMouseWheel(
4266 HWND hwnd,
4267 short zDelta)
4268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int i;
4270 int size;
4271 HWND hwndCtl;
Bram Moolenaarae20f342019-11-05 21:09:23 +01004272 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 if (mouse_scroll_lines == 0)
4275 init_mouse_wheel();
4276
Bram Moolenaarae20f342019-11-05 21:09:23 +01004277 wp = gui_mouse_window(FIND_POPUP);
4278
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004279#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004280 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004281 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004282 cmdarg_T cap;
4283 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004284
Bram Moolenaarae20f342019-11-05 21:09:23 +01004285 // Mouse hovers over popup window, scroll it if possible.
4286 mouse_row = wp->w_winrow;
4287 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004288 CLEAR_FIELD(cap);
Bram Moolenaarae20f342019-11-05 21:09:23 +01004289 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4290 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4291 clear_oparg(&oa);
4292 cap.oap = &oa;
4293 nv_mousescroll(&cap);
4294 update_screen(0);
4295 setcursor();
4296 out_flush();
4297 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004298 }
4299#endif
4300
Bram Moolenaarae20f342019-11-05 21:09:23 +01004301 if (wp == NULL || !p_scf)
4302 wp = curwin;
4303
4304 if (wp->w_scrollbars[SBAR_RIGHT].id != 0)
4305 hwndCtl = wp->w_scrollbars[SBAR_RIGHT].id;
4306 else if (wp->w_scrollbars[SBAR_LEFT].id != 0)
4307 hwndCtl = wp->w_scrollbars[SBAR_LEFT].id;
4308 else
4309 return;
4310 size = wp->w_height;
4311
Bram Moolenaara338adc2018-01-31 20:51:47 +01004312 mch_disable_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 if (mouse_scroll_lines > 0
4314 && mouse_scroll_lines < (size > 2 ? size - 2 : 1))
4315 {
4316 for (i = mouse_scroll_lines; i > 0; --i)
4317 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_LINEUP : SB_LINEDOWN, 0);
4318 }
4319 else
4320 _OnScroll(hwnd, hwndCtl, zDelta >= 0 ? SB_PAGEUP : SB_PAGEDOWN, 0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004321 mch_enable_flush();
4322 gui_may_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323}
4324
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004325#ifdef USE_SYSMENU_FONT
4326/*
4327 * Get Menu Font.
4328 * Return OK or FAIL.
4329 */
4330 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004331gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004332{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004333 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004334
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004335 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4336 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004337 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004338 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004339 &nm,
4340 0))
4341 return FAIL;
4342 *lf = nm.lfMenuFont;
4343 return OK;
4344}
4345#endif
4346
4347
4348#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4349/*
4350 * Set the GUI tabline font to the system menu font
4351 */
4352 static void
4353set_tabline_font(void)
4354{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004355 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004356 HFONT font;
4357 HWND hwnd;
4358 HDC hdc;
4359 HFONT hfntOld;
4360 TEXTMETRIC tm;
4361
4362 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4363 return;
4364
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004365 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004366
4367 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4368
4369 /*
4370 * Compute the height of the font used for the tab text
4371 */
4372 hwnd = GetDesktopWindow();
4373 hdc = GetWindowDC(hwnd);
4374 hfntOld = SelectFont(hdc, font);
4375
4376 GetTextMetrics(hdc, &tm);
4377
4378 SelectFont(hdc, hfntOld);
4379 ReleaseDC(hwnd, hdc);
4380
4381 /*
4382 * The space used by the tab border and the space between the tab label
4383 * and the tab border is included as 7.
4384 */
4385 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4386}
4387#endif
4388
Bram Moolenaar520470a2005-06-16 21:59:56 +00004389/*
4390 * Invoked when a setting was changed.
4391 */
4392 static LRESULT CALLBACK
4393_OnSettingChange(UINT n)
4394{
4395 if (n == SPI_SETWHEELSCROLLLINES)
4396 SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4397 &mouse_scroll_lines, 0);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004398#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4399 if (n == SPI_SETNONCLIENTMETRICS)
4400 set_tabline_font();
4401#endif
Bram Moolenaar520470a2005-06-16 21:59:56 +00004402 return 0;
4403}
4404
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405#ifdef FEAT_NETBEANS_INTG
4406 static void
4407_OnWindowPosChanged(
4408 HWND hwnd,
4409 const LPWINDOWPOS lpwpos)
4410{
4411 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004412 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4415 || lpwpos->cx != cx || lpwpos->cy != cy))
4416 {
4417 x = lpwpos->x;
4418 y = lpwpos->y;
4419 cx = lpwpos->cx;
4420 cy = lpwpos->cy;
4421 netbeans_frame_moved(x, y);
4422 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004423 // Allow to send WM_SIZE and WM_MOVE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, MyWindowProc);
4425}
4426#endif
4427
4428 static int
4429_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 UINT fwSide,
4431 LPRECT lprc)
4432{
4433 int w, h;
4434 int valid_w, valid_h;
4435 int w_offset, h_offset;
4436
4437 w = lprc->right - lprc->left;
4438 h = lprc->bottom - lprc->top;
4439 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h);
4440 w_offset = w - valid_w;
4441 h_offset = h - valid_h;
4442
4443 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4444 || fwSide == WMSZ_BOTTOMLEFT)
4445 lprc->left += w_offset;
4446 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4447 || fwSide == WMSZ_BOTTOMRIGHT)
4448 lprc->right -= w_offset;
4449
4450 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4451 || fwSide == WMSZ_TOPRIGHT)
4452 lprc->top += h_offset;
4453 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4454 || fwSide == WMSZ_BOTTOMRIGHT)
4455 lprc->bottom -= h_offset;
4456 return TRUE;
4457}
4458
4459
4460
4461 static LRESULT CALLBACK
4462_WndProc(
4463 HWND hwnd,
4464 UINT uMsg,
4465 WPARAM wParam,
4466 LPARAM lParam)
4467{
4468 /*
4469 TRACE("WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
4470 hwnd, uMsg, wParam, lParam);
4471 */
4472
4473 HandleMouseHide(uMsg, lParam);
4474
4475 s_uMsg = uMsg;
4476 s_wParam = wParam;
4477 s_lParam = lParam;
4478
4479 switch (uMsg)
4480 {
4481 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4482 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004483 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004485 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4487 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4488 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4489 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4490#ifdef FEAT_MENU
4491 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4492#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004493 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4494 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4496 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004497 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4498 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4500 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4501 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4502#ifdef FEAT_NETBEANS_INTG
4503 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4504#endif
4505
Bram Moolenaarafa24992006-03-27 20:58:26 +00004506#ifdef FEAT_GUI_TABLINE
4507 case WM_RBUTTONUP:
4508 {
4509 if (gui_mch_showing_tabline())
4510 {
4511 POINT pt;
4512 RECT rect;
4513
4514 /*
4515 * If the cursor is on the tabline, display the tab menu
4516 */
4517 GetCursorPos((LPPOINT)&pt);
4518 GetWindowRect(s_textArea, &rect);
4519 if (pt.y < rect.top)
4520 {
4521 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004522 return 0L;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004523 }
4524 }
4525 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4526 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004527 case WM_LBUTTONDBLCLK:
4528 {
4529 /*
4530 * If the user double clicked the tabline, create a new tab
4531 */
4532 if (gui_mch_showing_tabline())
4533 {
4534 POINT pt;
4535 RECT rect;
4536
4537 GetCursorPos((LPPOINT)&pt);
4538 GetWindowRect(s_textArea, &rect);
4539 if (pt.y < rect.top)
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004540 send_tabline_menu_event(0, TABLINE_MENU_NEW);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004541 }
4542 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4543 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004544#endif
4545
Bram Moolenaar734a8672019-12-02 22:49:38 +01004546 case WM_QUERYENDSESSION: // System wants to go down.
4547 gui_shell_closed(); // Will exit when no changed buffers.
4548 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
4550 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004551 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004554 return 0L;
4555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 break;
4557
4558 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004559 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4560 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004561 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 return 0L;
4563
4564 case WM_SYSCHAR:
4565 /*
4566 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4567 * shortcut key, handle like a typed ALT key, otherwise call Windows
4568 * ALT key handling.
4569 */
4570#ifdef FEAT_MENU
4571 if ( !gui.menu_is_active
4572 || p_wak[0] == 'n'
4573 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4574 )
4575#endif
4576 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004577 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 return 0L;
4579 }
4580#ifdef FEAT_MENU
4581 else
4582 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4583#endif
4584
4585 case WM_SYSKEYUP:
4586#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004587 // This used to be done only when menu is active: ALT key is used for
4588 // that. But that caused problems when menu is disabled and using
4589 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4590 // are received, mouse pointer remains hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4592#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004593 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595
Bram Moolenaar734a8672019-12-02 22:49:38 +01004596 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004597 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 case WM_MOUSEWHEEL:
4600 _OnMouseWheel(hwnd, HIWORD(wParam));
Bram Moolenaar213ae482011-12-15 21:51:36 +01004601 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaar734a8672019-12-02 22:49:38 +01004603 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004604 case WM_SETTINGCHANGE:
4605 return _OnSettingChange((UINT)wParam);
4606
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004607#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 case WM_NOTIFY:
4609 switch (((LPNMHDR) lParam)->code)
4610 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004611 case TTN_GETDISPINFOW:
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004612 case TTN_GETDISPINFO:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004614 LPNMHDR hdr = (LPNMHDR)lParam;
4615 char_u *str = NULL;
4616 static void *tt_text = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004617
Bram Moolenaard23a8232018-02-10 18:45:26 +01004618 VIM_CLEAR(tt_text);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004619
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004620# ifdef FEAT_GUI_TABLINE
4621 if (gui_mch_showing_tabline()
4622 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004623 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004624 POINT pt;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004625 /*
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004626 * Mouse is over the GUI tabline. Display the
4627 * tooltip for the tab under the cursor
4628 *
4629 * Get the cursor position within the tab control
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004630 */
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004631 GetCursorPos(&pt);
4632 if (ScreenToClient(s_tabhwnd, &pt) != 0)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004633 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004634 TCHITTESTINFO htinfo;
4635 int idx;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004636
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004637 /*
4638 * Get the tab under the cursor
4639 */
4640 htinfo.pt.x = pt.x;
4641 htinfo.pt.y = pt.y;
4642 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4643 if (idx != -1)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004644 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004645 tabpage_T *tp;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004646
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004647 tp = find_tabpage(idx + 1);
4648 if (tp != NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004649 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004650 get_tabline_label(tp, TRUE);
4651 str = NameBuff;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004652 }
4653 }
4654 }
4655 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004656# endif
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004657# ifdef FEAT_TOOLBAR
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004658# ifdef FEAT_GUI_TABLINE
4659 else
4660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004662 UINT idButton;
4663 vimmenu_T *pMenu;
4664
4665 idButton = (UINT) hdr->idFrom;
4666 pMenu = gui_mswin_find_menu(root_menu, idButton);
4667 if (pMenu)
4668 str = pMenu->strings[MENU_INDEX_TIP];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004670# endif
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004671 if (str != NULL)
4672 {
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004673 if (hdr->code == TTN_GETDISPINFOW)
4674 {
4675 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)lParam;
4676
Bram Moolenaar734a8672019-12-02 22:49:38 +01004677 // Set the maximum width, this also enables using
4678 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004679 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4680 0, 500);
4681
Bram Moolenaar36f692d2008-11-20 16:10:17 +00004682 tt_text = enc_to_utf16(str, NULL);
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004683 lpdi->lpszText = tt_text;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004684 // can't show tooltip if failed
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004685 }
4686 else
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004687 {
4688 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)lParam;
4689
Bram Moolenaar734a8672019-12-02 22:49:38 +01004690 // Set the maximum width, this also enables using
4691 // \n for line break.
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00004692 SendMessage(lpdi->hdr.hwndFrom, TTM_SETMAXTIPWIDTH,
4693 0, 500);
4694
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004695 if (STRLEN(str) < sizeof(lpdi->szText)
4696 || ((tt_text = vim_strsave(str)) == NULL))
Bram Moolenaar418f81b2016-02-16 20:12:02 +01004697 vim_strncpy((char_u *)lpdi->szText, str,
Bram Moolenaar8f2ff9f2006-08-29 19:26:50 +00004698 sizeof(lpdi->szText) - 1);
4699 else
4700 lpdi->lpszText = tt_text;
4701 }
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004705# ifdef FEAT_GUI_TABLINE
4706 case TCN_SELCHANGE:
4707 if (gui_mch_showing_tabline()
4708 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004709 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004710 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004711 return 0L;
4712 }
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004713 break;
Bram Moolenaarafa24992006-03-27 20:58:26 +00004714
4715 case NM_RCLICK:
4716 if (gui_mch_showing_tabline()
4717 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
Bram Moolenaar213ae482011-12-15 21:51:36 +01004718 {
Bram Moolenaarafa24992006-03-27 20:58:26 +00004719 show_tabline_popup_menu();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004720 return 0L;
4721 }
Bram Moolenaarafa24992006-03-27 20:58:26 +00004722 break;
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 default:
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004725# ifdef FEAT_GUI_TABLINE
4726 if (gui_mch_showing_tabline()
4727 && ((LPNMHDR)lParam)->hwndFrom == s_tabhwnd)
4728 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 break;
4731 }
4732 break;
4733#endif
4734#if defined(MENUHINTS) && defined(FEAT_MENU)
4735 case WM_MENUSELECT:
4736 if (((UINT) HIWORD(wParam)
4737 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4738 == MF_HILITE
4739 && (State & CMDLINE) == 0)
4740 {
4741 UINT idButton;
4742 vimmenu_T *pMenu;
4743 static int did_menu_tip = FALSE;
4744
4745 if (did_menu_tip)
4746 {
4747 msg_clr_cmdline();
4748 setcursor();
4749 out_flush();
4750 did_menu_tip = FALSE;
4751 }
4752
4753 idButton = (UINT)LOWORD(wParam);
4754 pMenu = gui_mswin_find_menu(root_menu, idButton);
4755 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4756 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4757 {
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004758 ++msg_hist_off;
Bram Moolenaar32526b32019-01-19 17:43:09 +01004759 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
Bram Moolenaar2d8ab992007-06-19 08:06:18 +00004760 --msg_hist_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 setcursor();
4762 out_flush();
4763 did_menu_tip = TRUE;
4764 }
Bram Moolenaar213ae482011-12-15 21:51:36 +01004765 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 }
4767 break;
4768#endif
4769 case WM_NCHITTEST:
4770 {
4771 LRESULT result;
4772 int x, y;
4773 int xPos = GET_X_LPARAM(lParam);
4774
4775 result = MyWindowProc(hwnd, uMsg, wParam, lParam);
4776 if (result == HTCLIENT)
4777 {
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004778#ifdef FEAT_GUI_TABLINE
4779 if (gui_mch_showing_tabline())
4780 {
4781 int yPos = GET_Y_LPARAM(lParam);
4782 RECT rct;
4783
Bram Moolenaar734a8672019-12-02 22:49:38 +01004784 // If the cursor is on the GUI tabline, don't process this
4785 // event
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004786 GetWindowRect(s_textArea, &rct);
4787 if (yPos < rct.top)
4788 return result;
4789 }
4790#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +02004791 (void)gui_mch_get_winpos(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 xPos -= x;
4793
Bram Moolenaar734a8672019-12-02 22:49:38 +01004794 if (xPos < 48) // <VN> TODO should use system metric?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return HTBOTTOMLEFT;
4796 else
4797 return HTBOTTOMRIGHT;
4798 }
4799 else
4800 return result;
4801 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004802 // break; notreached
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804#ifdef FEAT_MBYTE_IME
4805 case WM_IME_NOTIFY:
4806 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
4807 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004808 return 1L;
4809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 case WM_IME_COMPOSITION:
4811 if (!_OnImeComposition(hwnd, wParam, lParam))
4812 return MyWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004813 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
4815
4816 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004818 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821 return MyWindowProc(hwnd, uMsg, wParam, lParam);
4822 }
4823
Bram Moolenaar2787ab92011-12-14 15:23:59 +01004824 return DefWindowProc(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
4826
4827/*
4828 * End of call-back routines
4829 */
4830
Bram Moolenaar734a8672019-12-02 22:49:38 +01004831// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832HWND vim_parent_hwnd = NULL;
4833
4834 static BOOL CALLBACK
4835FindWindowTitle(HWND hwnd, LPARAM lParam)
4836{
4837 char buf[2048];
4838 char *title = (char *)lParam;
4839
4840 if (GetWindowText(hwnd, buf, sizeof(buf)))
4841 {
4842 if (strstr(buf, title) != NULL)
4843 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004844 // Found it. Store the window ref. and quit searching if MDI
4845 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004847 if (vim_parent_hwnd != NULL)
4848 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004851 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852}
4853
4854/*
4855 * Invoked for '-P "title"' argument: search for parent application to open
4856 * our window in.
4857 */
4858 void
4859gui_mch_set_parent(char *title)
4860{
4861 EnumWindows(FindWindowTitle, (LPARAM)title);
4862 if (vim_parent_hwnd == NULL)
4863 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004864 semsg(_("E671: Cannot find window title \"%s\""), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 mch_exit(2);
4866 }
4867}
4868
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004869#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 static void
4871ole_error(char *arg)
4872{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004873 char buf[IOSIZE];
4874
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004875# ifdef VIMDLL
4876 gui.in_use = mch_is_gui_executable();
4877# endif
4878
Bram Moolenaar734a8672019-12-02 22:49:38 +01004879 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004880 vim_snprintf(buf, IOSIZE,
4881 _("E243: Argument not supported: \"-%s\"; Use the OLE version."),
4882 arg);
4883 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004887#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4888 static char *
4889gvim_error(void)
4890{
4891 char *msg = _("E988: GUI cannot be used. Cannot execute gvim.exe.");
4892
4893 if (starting)
4894 {
4895 mch_errmsg(msg);
4896 mch_errmsg("\n");
4897 mch_exit(2);
4898 }
4899 return msg;
4900}
4901
4902 char *
4903gui_mch_do_spawn(char_u *arg)
4904{
4905 int len;
4906# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4907 char_u *session = NULL;
4908 LPWSTR tofree1 = NULL;
4909# endif
4910 WCHAR name[MAX_PATH];
4911 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4912 STARTUPINFOW si = {sizeof(si)};
4913 PROCESS_INFORMATION pi;
4914
4915 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4916 goto error;
4917 p = wcsrchr(name, L'\\');
4918 if (p == NULL)
4919 goto error;
4920 // Replace the executable name from vim(d).exe to gvim(d).exe.
4921# ifdef DEBUG
4922 wcscpy(p + 1, L"gvimd.exe");
4923# else
4924 wcscpy(p + 1, L"gvim.exe");
4925# endif
4926
4927# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4928 if (starting)
4929# endif
4930 {
4931 // Pass the command line to the new process.
4932 p = GetCommandLineW();
4933 // Skip 1st argument.
4934 while (*p && *p != L' ' && *p != L'\t')
4935 {
4936 if (*p == L'"')
4937 {
4938 while (*p && *p != L'"')
4939 ++p;
4940 if (*p)
4941 ++p;
4942 }
4943 else
4944 ++p;
4945 }
4946 cmd = p;
4947 }
4948# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4949 else
4950 {
4951 // Create a session file and pass it to the new process.
4952 LPWSTR wsession;
4953 char_u *savebg;
4954 int ret;
4955
4956 session = vim_tempname('s', FALSE);
4957 if (session == NULL)
4958 goto error;
4959 savebg = p_bg;
4960 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
4961 ret = write_session_file(session);
4962 vim_free(p_bg);
4963 p_bg = savebg;
4964 if (!ret)
4965 goto error;
4966 wsession = enc_to_utf16(session, NULL);
4967 if (wsession == NULL)
4968 goto error;
4969 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004970 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004971 if (cmd == NULL)
4972 {
4973 vim_free(wsession);
4974 goto error;
4975 }
4976 tofree1 = cmd;
4977 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
4978 wsession, wsession);
4979 vim_free(wsession);
4980 }
4981# endif
4982
4983 // Check additional arguments to the `:gui` command.
4984 if (arg != NULL)
4985 {
4986 warg = enc_to_utf16(arg, NULL);
4987 if (warg == NULL)
4988 goto error;
4989 tofree2 = warg;
4990 }
4991 else
4992 warg = L"";
4993
4994 // Set up the new command line.
4995 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004996 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004997 if (newcmd == NULL)
4998 goto error;
4999 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5000
5001 // Spawn a new GUI process.
5002 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5003 NULL, NULL, &si, &pi))
5004 goto error;
5005 CloseHandle(pi.hProcess);
5006 CloseHandle(pi.hThread);
5007 mch_exit(0);
5008
5009error:
5010# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5011 if (session)
5012 mch_remove(session);
5013 vim_free(session);
5014 vim_free(tofree1);
5015# endif
5016 vim_free(newcmd);
5017 vim_free(tofree2);
5018 return gvim_error();
5019}
5020#endif
5021
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022/*
5023 * Parse the GUI related command-line arguments. Any arguments used are
5024 * deleted from argv, and *argc is decremented accordingly. This is called
5025 * when vim is started, whether or not the GUI has been started.
5026 */
5027 void
5028gui_mch_prepare(int *argc, char **argv)
5029{
5030 int silent = FALSE;
5031 int idx;
5032
Bram Moolenaar734a8672019-12-02 22:49:38 +01005033 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5035 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005036 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5038 && (argv[2][0] == '-' || argv[2][0] == '/'))
5039 {
5040 silent = TRUE;
5041 idx = 2;
5042 }
5043 else
5044 idx = 1;
5045
Bram Moolenaar734a8672019-12-02 22:49:38 +01005046 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (STRICMP(argv[idx] + 1, "register") == 0)
5048 {
5049#ifdef FEAT_OLE
5050 RegisterMe(silent);
5051 mch_exit(0);
5052#else
5053 if (!silent)
5054 ole_error("register");
5055 mch_exit(2);
5056#endif
5057 }
5058
Bram Moolenaar734a8672019-12-02 22:49:38 +01005059 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5061 {
5062#ifdef FEAT_OLE
5063 UnregisterMe(!silent);
5064 mch_exit(0);
5065#else
5066 if (!silent)
5067 ole_error("unregister");
5068 mch_exit(2);
5069#endif
5070 }
5071
Bram Moolenaar734a8672019-12-02 22:49:38 +01005072 // Ignore an -embedding argument. It is only relevant if the
5073 // application wants to treat the case when it is started manually
5074 // differently from the case where it is started via automation (and
5075 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5077 {
5078#ifdef FEAT_OLE
5079 *argc = 1;
5080#else
5081 ole_error("embedding");
5082 mch_exit(2);
5083#endif
5084 }
5085 }
5086
5087#ifdef FEAT_OLE
5088 {
5089 int bDoRestart = FALSE;
5090
5091 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005092 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (bDoRestart)
5094 mch_exit(0);
5095 }
5096#endif
5097
5098#ifdef FEAT_NETBEANS_INTG
5099 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005100 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 int arg;
5102
5103 for (arg = 1; arg < *argc; arg++)
5104 if (strncmp("-nb", argv[arg], 3) == 0)
5105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 netbeansArg = argv[arg];
5107 mch_memmove(&argv[arg], &argv[arg + 1],
5108 (--*argc - arg) * sizeof(char *));
5109 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005110 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
5113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114}
5115
5116/*
5117 * Initialise the GUI. Create all the windows, set up all the call-backs
5118 * etc.
5119 */
5120 int
5121gui_mch_init(void)
5122{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005124 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126#ifdef GLOBAL_IME
5127 ATOM atom;
5128#endif
5129
Bram Moolenaar734a8672019-12-02 22:49:38 +01005130 // Return here if the window was already opened (happens when
5131 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005133 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 /*
5136 * Load the tearoff bitmap
5137 */
5138#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005139 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140#endif
5141
5142 gui.scrollbar_width = GetSystemMetrics(SM_CXVSCROLL);
5143 gui.scrollbar_height = GetSystemMetrics(SM_CYHSCROLL);
5144#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005145 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146#endif
5147 gui.border_width = 0;
5148
5149 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5150
Bram Moolenaar734a8672019-12-02 22:49:38 +01005151 // First try using the wide version, so that we can use any title.
5152 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005153 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005155 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 wndclassw.lpfnWndProc = _WndProc;
5157 wndclassw.cbClsExtra = 0;
5158 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005159 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5161 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5162 wndclassw.hbrBackground = s_brush;
5163 wndclassw.lpszMenuName = NULL;
5164 wndclassw.lpszClassName = szVimWndClassW;
5165
5166 if ((
5167#ifdef GLOBAL_IME
5168 atom =
5169#endif
5170 RegisterClassW(&wndclassw)) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (vim_parent_hwnd != NULL)
5175 {
5176#ifdef HAVE_TRY_EXCEPT
5177 __try
5178 {
5179#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005180 // Open inside the specified parent window.
5181 // TODO: last argument should point to a CLIENTCREATESTRUCT
5182 // structure.
5183 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005185 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005186 WS_OVERLAPPEDWINDOW | WS_CHILD
5187 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5189 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005190 100, // Any value will do
5191 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005193 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194#ifdef HAVE_TRY_EXCEPT
5195 }
5196 __except(EXCEPTION_EXECUTE_HANDLER)
5197 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005198 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200#endif
5201 if (s_hwnd == NULL)
5202 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005203 emsg(_("E672: Unable to open window inside MDI application"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 mch_exit(2);
5205 }
5206 }
5207 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005208 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005209 // If the provided windowid is not valid reset it to zero, so that it
5210 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005211 if (IsWindow((HWND)win_socket_id) <= 0)
5212 win_socket_id = 0;
5213
Bram Moolenaar734a8672019-12-02 22:49:38 +01005214 // Create a window. If win_socket_id is not zero without border and
5215 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005216 s_hwnd = CreateWindowW(
5217 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005218 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5219 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005220 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5221 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005222 100, // Any value will do
5223 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005224 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005225 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005226 if (s_hwnd != NULL && win_socket_id != 0)
5227 {
5228 SetParent(s_hwnd, (HWND)win_socket_id);
5229 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5230 }
5231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 if (s_hwnd == NULL)
5234 return FAIL;
5235
5236#ifdef GLOBAL_IME
5237 global_ime_init(atom, s_hwnd);
5238#endif
5239#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5240 dyn_imm_load();
5241#endif
5242
Bram Moolenaar734a8672019-12-02 22:49:38 +01005243 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005244 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005245 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005246 wndclassw.style = CS_OWNDC;
5247 wndclassw.lpfnWndProc = _TextAreaWndProc;
5248 wndclassw.cbClsExtra = 0;
5249 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005250 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005251 wndclassw.hIcon = NULL;
5252 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5253 wndclassw.hbrBackground = NULL;
5254 wndclassw.lpszMenuName = NULL;
5255 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005256
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005257 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return FAIL;
5259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005261 s_textArea = CreateWindowExW(
5262 0,
5263 szTextAreaClassW, L"Vim text area",
5264 WS_CHILD | WS_VISIBLE, 0, 0,
5265 100, // Any value will do for now
5266 100, // Any value will do for now
5267 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005268 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 if (s_textArea == NULL)
5271 return FAIL;
5272
Bram Moolenaar20321902016-02-17 12:30:17 +01005273#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005274 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005275 {
5276 HANDLE hIcon = NULL;
5277
5278 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005279 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005280 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005281#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#ifdef FEAT_MENU
5284 s_menuBar = CreateMenu();
5285#endif
5286 s_hdc = GetDC(s_textArea);
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289
Bram Moolenaar734a8672019-12-02 22:49:38 +01005290 // Do we need to bother with this?
5291 // m_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292
Bram Moolenaar734a8672019-12-02 22:49:38 +01005293 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 gui_mch_def_colors();
5295
Bram Moolenaar734a8672019-12-02 22:49:38 +01005296 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5297 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 set_normal_colors();
5299
5300 /*
5301 * Check that none of the colors are the same as the background color.
5302 * Then store the current values as the defaults.
5303 */
5304 gui_check_colors();
5305 gui.def_norm_pixel = gui.norm_pixel;
5306 gui.def_back_pixel = gui.back_pixel;
5307
Bram Moolenaar734a8672019-12-02 22:49:38 +01005308 // Get the colors for the highlight groups (gui_check_colors() might have
5309 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 highlight_gui_started();
5311
5312 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005313 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005315 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 /*
5318 * Set up for Intellimouse processing
5319 */
5320 init_mouse_wheel();
5321
5322 /*
5323 * compute a couple of metrics used for the dialogs
5324 */
5325 get_dialog_font_metrics();
5326#ifdef FEAT_TOOLBAR
5327 /*
5328 * Create the toolbar
5329 */
5330 initialise_toolbar();
5331#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005332#ifdef FEAT_GUI_TABLINE
5333 /*
5334 * Create the tabline
5335 */
5336 initialise_tabline();
5337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#ifdef MSWIN_FIND_REPLACE
5339 /*
5340 * Initialise the dialog box stuff
5341 */
5342 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5343
Bram Moolenaar734a8672019-12-02 22:49:38 +01005344 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005346 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005348 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5350 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5351 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005354#ifdef FEAT_EVAL
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005355# if !defined(_MSC_VER) || (_MSC_VER < 1400)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005356// Define HandleToLong for old MS and non-MS compilers if not defined.
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005357# ifndef HandleToLong
Bram Moolenaara87e2c22016-02-17 20:48:19 +01005358# define HandleToLong(h) ((long)(intptr_t)(h))
Bram Moolenaarf32c5cd2016-01-10 16:07:44 +01005359# endif
Bram Moolenaar4da95d32011-07-07 17:43:41 +02005360# endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005361 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005362 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005363#endif
5364
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005365#ifdef FEAT_RENDER_OPTIONS
5366 if (p_rop)
5367 (void)gui_mch_set_rendering_options(p_rop);
5368#endif
5369
Bram Moolenaar748bf032005-02-02 23:04:36 +00005370theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005371 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005372 display_errors();
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 return OK;
5375}
5376
5377/*
5378 * Get the size of the screen, taking position on multiple monitors into
5379 * account (if supported).
5380 */
5381 static void
5382get_work_area(RECT *spi_rect)
5383{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005384 HMONITOR mon;
5385 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
Bram Moolenaar734a8672019-12-02 22:49:38 +01005387 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005388 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005389 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005391 moninfo.cbSize = sizeof(MONITORINFO);
5392 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005394 *spi_rect = moninfo.rcWork;
5395 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 }
5397 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005398 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5400}
5401
5402/*
5403 * Set the size of the window to the given width and height in pixels.
5404 */
5405 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005406gui_mch_set_shellsize(
5407 int width,
5408 int height,
5409 int min_width UNUSED,
5410 int min_height UNUSED,
5411 int base_width UNUSED,
5412 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005413 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005416 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar734a8672019-12-02 22:49:38 +01005419 // Try to keep window completely on screen.
5420 // Get position of the screen work area. This is the part that is not
5421 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 get_work_area(&workarea_rect);
5423
Bram Moolenaar734a8672019-12-02 22:49:38 +01005424 // Resizing a maximized window looks very strange, unzoom it first.
5425 // But don't do it when still starting up, it may have been requested in
5426 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005427 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005429
5430 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431
Bram Moolenaar734a8672019-12-02 22:49:38 +01005432 // compute the size of the outside of the window
Bram Moolenaar9d488952013-07-21 17:53:58 +02005433 win_width = width + (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005434 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar9d488952013-07-21 17:53:58 +02005435 win_height = height + (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005436 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 + GetSystemMetrics(SM_CYCAPTION)
5438#ifdef FEAT_MENU
5439 + gui_mswin_get_menu_height(FALSE)
5440#endif
5441 ;
5442
Bram Moolenaar734a8672019-12-02 22:49:38 +01005443 // The following should take care of keeping Vim on the same monitor, no
5444 // matter if the secondary monitor is left or right of the primary
5445 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005446 window_rect.right = window_rect.left + win_width;
5447 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005448
Bram Moolenaar734a8672019-12-02 22:49:38 +01005449 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005450 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5451 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005453 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5454 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005456 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5457 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005459 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5460 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005462 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5463 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464
5465 SetActiveWindow(s_hwnd);
5466 SetFocus(s_hwnd);
5467
5468#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005469 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 gui_mswin_get_menu_height(!gui.starting);
5471#endif
5472}
5473
5474
5475 void
5476gui_mch_set_scrollbar_thumb(
5477 scrollbar_T *sb,
5478 long val,
5479 long size,
5480 long max)
5481{
5482 SCROLLINFO info;
5483
5484 sb->scroll_shift = 0;
5485 while (max > 32767)
5486 {
5487 max = (max + 1) >> 1;
5488 val >>= 1;
5489 size >>= 1;
5490 ++sb->scroll_shift;
5491 }
5492
5493 if (sb->scroll_shift > 0)
5494 ++size;
5495
5496 info.cbSize = sizeof(info);
5497 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5498 info.nPos = val;
5499 info.nMin = 0;
5500 info.nMax = max;
5501 info.nPage = size;
5502 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5503}
5504
5505
5506/*
5507 * Set the current text font.
5508 */
5509 void
5510gui_mch_set_font(GuiFont font)
5511{
5512 gui.currFont = font;
5513}
5514
5515
5516/*
5517 * Set the current text foreground color.
5518 */
5519 void
5520gui_mch_set_fg_color(guicolor_T color)
5521{
5522 gui.currFgColor = color;
5523}
5524
5525/*
5526 * Set the current text background color.
5527 */
5528 void
5529gui_mch_set_bg_color(guicolor_T color)
5530{
5531 gui.currBgColor = color;
5532}
5533
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005534/*
5535 * Set the current text special color.
5536 */
5537 void
5538gui_mch_set_sp_color(guicolor_T color)
5539{
5540 gui.currSpColor = color;
5541}
5542
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005543#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544/*
5545 * Multi-byte handling, originally by Sung-Hoon Baek.
5546 * First static functions (no prototypes generated).
5547 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005548# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005549# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005550# endif
5551# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 * handle WM_IME_NOTIFY message
5555 */
5556 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005557_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 LRESULT lResult = 0;
5560 HIMC hImc;
5561
5562 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5563 return lResult;
5564 switch (dwCommand)
5565 {
5566 case IMN_SETOPENSTATUS:
5567 if (pImmGetOpenStatus(hImc))
5568 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005569 pImmSetCompositionFontW(hImc, &norm_logfont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 im_set_position(gui.row, gui.col);
5571
Bram Moolenaar734a8672019-12-02 22:49:38 +01005572 // Disable langmap
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 State &= ~LANGMAP;
5574 if (State & INSERT)
5575 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005576# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005577 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5579 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005580 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int old_row = gui.row;
5582 int old_col = gui.col;
5583
5584 // This must be called here before
5585 // status_redraw_curbuf(), otherwise the mode
5586 // message may appear in the wrong position.
5587 showmode();
5588 status_redraw_curbuf();
5589 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005590 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 gui.row = old_row;
5592 gui.col = old_col;
5593 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596 }
5597 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005598 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 lResult = 0;
5600 break;
5601 }
5602 pImmReleaseContext(hWnd, hImc);
5603 return lResult;
5604}
5605
5606 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005607_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 char_u *ret;
5610 int len;
5611
Bram Moolenaar734a8672019-12-02 22:49:38 +01005612 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 return 0;
5614
5615 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5616 if (ret != NULL)
5617 {
5618 add_to_input_buf_csi(ret, len);
5619 vim_free(ret);
5620 return 1;
5621 }
5622 return 0;
5623}
5624
5625/*
5626 * get the current composition string, in UCS-2; *lenp is the number of
5627 * *lenp is the number of Unicode characters.
5628 */
5629 static short_u *
5630GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp)
5631{
5632 LONG ret;
5633 LPWSTR wbuf = NULL;
5634 char_u *buf;
5635
5636 if (!pImmGetContext)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005637 return NULL; // no imm32.dll
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
Bram Moolenaar734a8672019-12-02 22:49:38 +01005639 // Try Unicode; this'll always work on NT regardless of codepage.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5641 if (ret == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005642 return NULL; // empty
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 if (ret > 0)
5645 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005646 // Allocate the requested buffer plus space for the NUL character.
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005647 wbuf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 if (wbuf != NULL)
5649 {
5650 pImmGetCompositionStringW(hIMC, GCS, wbuf, ret);
5651 *lenp = ret / sizeof(WCHAR);
5652 }
5653 return (short_u *)wbuf;
5654 }
5655
Bram Moolenaar734a8672019-12-02 22:49:38 +01005656 // ret < 0; we got an error, so try the ANSI version. This'll work
5657 // on 9x/ME, but only if the codepage happens to be set to whatever
5658 // we're inputting.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0);
5660 if (ret <= 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005661 return NULL; // empty or error
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 buf = alloc(ret);
5664 if (buf == NULL)
5665 return NULL;
5666 pImmGetCompositionStringA(hIMC, GCS, buf, ret);
5667
Bram Moolenaar734a8672019-12-02 22:49:38 +01005668 // convert from codepage to UCS-2
Bram Moolenaar418f81b2016-02-16 20:12:02 +01005669 MultiByteToWideChar_alloc(GetACP(), 0, (LPCSTR)buf, ret, &wbuf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 vim_free(buf);
5671
5672 return (short_u *)wbuf;
5673}
5674
5675/*
5676 * void GetResultStr()
5677 *
5678 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5679 * get complete composition string
5680 */
5681 static char_u *
5682GetResultStr(HWND hwnd, int GCS, int *lenp)
5683{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005684 HIMC hIMC; // Input context handle.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 short_u *buf = NULL;
5686 char_u *convbuf = NULL;
5687
5688 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5689 return NULL;
5690
Bram Moolenaar734a8672019-12-02 22:49:38 +01005691 // Reads in the composition string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 buf = GetCompositionString_inUCS2(hIMC, GCS, lenp);
5693 if (buf == NULL)
5694 return NULL;
5695
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005696 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 pImmReleaseContext(hwnd, hIMC);
5698 vim_free(buf);
5699 return convbuf;
5700}
5701#endif
5702
Bram Moolenaar734a8672019-12-02 22:49:38 +01005703// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005704#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
5706/*
5707 * set font to IM.
5708 */
5709 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005710im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 HIMC hImc;
5713
5714 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5715 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005716 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 pImmReleaseContext(s_hwnd, hImc);
5718 }
5719}
5720
5721/*
5722 * Notify cursor position to IM.
5723 */
5724 void
5725im_set_position(int row, int col)
5726{
5727 HIMC hImc;
5728
5729 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5730 {
5731 COMPOSITIONFORM cfs;
5732
5733 cfs.dwStyle = CFS_POINT;
5734 cfs.ptCurrentPos.x = FILL_X(col);
5735 cfs.ptCurrentPos.y = FILL_Y(row);
5736 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
5737 pImmSetCompositionWindow(hImc, &cfs);
5738
5739 pImmReleaseContext(s_hwnd, hImc);
5740 }
5741}
5742
5743/*
5744 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5745 */
5746 void
5747im_set_active(int active)
5748{
5749 HIMC hImc;
5750 static HIMC hImcOld = (HIMC)0;
5751
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005752# ifdef VIMDLL
5753 if (!gui.in_use && !gui.starting)
5754 {
5755 mbyte_im_set_active(active);
5756 return;
5757 }
5758# endif
5759
Bram Moolenaar734a8672019-12-02 22:49:38 +01005760 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 if (p_imdisable)
5763 {
5764 if (hImcOld == (HIMC)0)
5765 {
5766 hImcOld = pImmGetContext(s_hwnd);
5767 if (hImcOld)
5768 pImmAssociateContext(s_hwnd, (HIMC)0);
5769 }
5770 active = FALSE;
5771 }
5772 else if (hImcOld != (HIMC)0)
5773 {
5774 pImmAssociateContext(s_hwnd, hImcOld);
5775 hImcOld = (HIMC)0;
5776 }
5777
5778 hImc = pImmGetContext(s_hwnd);
5779 if (hImc)
5780 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005781 /*
5782 * for Korean ime
5783 */
5784 HKL hKL = GetKeyboardLayout(0);
5785
5786 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5787 {
5788 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5789 static BOOL bSaved = FALSE;
5790
5791 if (active)
5792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005793 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005794 if (bSaved)
5795 pImmSetConversionStatus(hImc, dwConversionSaved,
5796 dwSentenceSaved);
5797 bSaved = FALSE;
5798 }
5799 else
5800 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005801 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005802 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5803 &dwSentenceSaved))
5804 {
5805 bSaved = TRUE;
5806 pImmSetConversionStatus(hImc,
5807 dwConversionSaved & ~(IME_CMODE_NATIVE
5808 | IME_CMODE_FULLSHAPE),
5809 dwSentenceSaved);
5810 }
5811 }
5812 }
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 pImmSetOpenStatus(hImc, active);
5815 pImmReleaseContext(s_hwnd, hImc);
5816 }
5817 }
5818}
5819
5820/*
5821 * Get IM status. When IM is on, return not 0. Else return 0.
5822 */
5823 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005824im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 int status = 0;
5827 HIMC hImc;
5828
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005829# ifdef VIMDLL
5830 if (!gui.in_use && !gui.starting)
5831 return mbyte_im_get_status();
5832# endif
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5835 {
5836 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5837 pImmReleaseContext(s_hwnd, hImc);
5838 }
5839 return status;
5840}
5841
Bram Moolenaar734a8672019-12-02 22:49:38 +01005842#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005844#if !defined(FEAT_MBYTE_IME) && defined(GLOBAL_IME)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005845// Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847/*
5848 * Notify cursor position to IM.
5849 */
5850 void
5851im_set_position(int row, int col)
5852{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005853 // Win32 with GLOBAL IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 POINT p;
5855
5856 p.x = FILL_X(col);
5857 p.y = FILL_Y(row);
5858 MapWindowPoints(s_textArea, s_hwnd, &p, 1);
5859 global_ime_set_position(&p);
5860}
5861
5862/*
5863 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5864 */
5865 void
5866im_set_active(int active)
5867{
5868 global_ime_set_status(active);
5869}
5870
5871/*
5872 * Get IM status. When IM is on, return not 0. Else return 0.
5873 */
5874 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005875im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 return global_ime_get_status();
5878}
5879#endif
5880
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005881/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005882 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005883 */
5884 static void
5885latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5886{
5887 int c;
5888
Bram Moolenaarca003e12006-03-17 23:19:38 +00005889 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005890 {
5891 c = *text++;
5892 switch (c)
5893 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005894 case 0xa4: c = 0x20ac; break; // euro
5895 case 0xa6: c = 0x0160; break; // S hat
5896 case 0xa8: c = 0x0161; break; // S -hat
5897 case 0xb4: c = 0x017d; break; // Z hat
5898 case 0xb8: c = 0x017e; break; // Z -hat
5899 case 0xbc: c = 0x0152; break; // OE
5900 case 0xbd: c = 0x0153; break; // oe
5901 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005902 }
5903 *unicodebuf++ = c;
5904 }
5905}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
5907#ifdef FEAT_RIGHTLEFT
5908/*
5909 * What is this for? In the case where you are using Win98 or Win2K or later,
5910 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5911 * reverses the string sent to the TextOut... family. This sucks, because we
5912 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5913 * way to tell Windblows not to do this!
5914 *
5915 * The short of it is that this 'RevOut' only gets called if you are running
5916 * one of the new, "improved" MS OSes, and only if you are running in
5917 * 'rightleft' mode. It makes display take *slightly* longer, but not
5918 * noticeably so.
5919 */
5920 static void
5921RevOut( HDC s_hdc,
5922 int col,
5923 int row,
5924 UINT foptions,
5925 CONST RECT *pcliprect,
5926 LPCTSTR text,
5927 UINT len,
5928 CONST INT *padding)
5929{
5930 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005932 for (ix = 0; ix < (int)len; ++ix)
5933 ExtTextOut(s_hdc, col + TEXT_X(ix), row, foptions,
5934 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935}
5936#endif
5937
Bram Moolenaar92467d32017-12-05 13:22:16 +01005938 static void
5939draw_line(
5940 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005941 int y1,
5942 int x2,
5943 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005944 COLORREF color)
5945{
5946#if defined(FEAT_DIRECTX)
5947 if (IS_ENABLE_DIRECTX())
5948 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5949 else
5950#endif
5951 {
5952 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5953 HPEN old_pen = SelectObject(s_hdc, hpen);
5954 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005955 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005956 LineTo(s_hdc, x2, y2);
5957 DeleteObject(SelectObject(s_hdc, old_pen));
5958 }
5959}
5960
5961 static void
5962set_pixel(
5963 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005964 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005965 COLORREF color)
5966{
5967#if defined(FEAT_DIRECTX)
5968 if (IS_ENABLE_DIRECTX())
5969 DWriteContext_SetPixel(s_dwc, x, y, color);
5970 else
5971#endif
5972 SetPixel(s_hdc, x, y, color);
5973}
5974
5975 static void
5976fill_rect(
5977 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005978 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005979 COLORREF color)
5980{
5981#if defined(FEAT_DIRECTX)
5982 if (IS_ENABLE_DIRECTX())
5983 DWriteContext_FillRect(s_dwc, rcp, color);
5984 else
5985#endif
5986 {
5987 HBRUSH hbr2;
5988
5989 if (hbr == NULL)
5990 hbr2 = CreateSolidBrush(color);
5991 else
5992 hbr2 = hbr;
5993 FillRect(s_hdc, rcp, hbr2);
5994 if (hbr == NULL)
5995 DeleteBrush(hbr2);
5996 }
5997}
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 void
6000gui_mch_draw_string(
6001 int row,
6002 int col,
6003 char_u *text,
6004 int len,
6005 int flags)
6006{
6007 static int *padding = NULL;
6008 static int pad_size = 0;
6009 int i;
6010 const RECT *pcliprect = NULL;
6011 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 static WCHAR *unicodebuf = NULL;
6013 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006014 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 int y;
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 /*
6019 * Italic and bold text seems to have an extra row of pixels at the bottom
6020 * (below where the bottom of the character should be). If we draw the
6021 * characters with a solid background, the top row of pixels in the
6022 * character below will be overwritten. We can fix this by filling in the
6023 * background ourselves, to the correct character proportions, and then
6024 * writing the character in transparent mode. Still have a problem when
6025 * the character is "_", which gets written on to the character below.
6026 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6027 * pixel in their slots, which fixes the problem with the bottom row of
6028 * pixels. We still need this code because otherwise the top row of pixels
6029 * becomes a problem. - webb.
6030 */
6031 static HBRUSH hbr_cache[2] = {NULL, NULL};
6032 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6033 static int brush_lru = 0;
6034 HBRUSH hbr;
6035 RECT rc;
6036
6037 if (!(flags & DRAW_TRANSP))
6038 {
6039 /*
6040 * Clear background first.
6041 * Note: FillRect() excludes right and bottom of rectangle.
6042 */
6043 rc.left = FILL_X(col);
6044 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 if (has_mbyte)
6046 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006047 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006048 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 rc.right = FILL_X(col + len);
6052 rc.bottom = FILL_Y(row + 1);
6053
Bram Moolenaar734a8672019-12-02 22:49:38 +01006054 // Cache the created brush, that saves a lot of time. We need two:
6055 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (gui.currBgColor == brush_color[0])
6057 {
6058 hbr = hbr_cache[0];
6059 brush_lru = 1;
6060 }
6061 else if (gui.currBgColor == brush_color[1])
6062 {
6063 hbr = hbr_cache[1];
6064 brush_lru = 0;
6065 }
6066 else
6067 {
6068 if (hbr_cache[brush_lru] != NULL)
6069 DeleteBrush(hbr_cache[brush_lru]);
6070 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6071 brush_color[brush_lru] = gui.currBgColor;
6072 hbr = hbr_cache[brush_lru];
6073 brush_lru = !brush_lru;
6074 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006075
Bram Moolenaar92467d32017-12-05 13:22:16 +01006076 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077
6078 SetBkMode(s_hdc, TRANSPARENT);
6079
6080 /*
6081 * When drawing block cursor, prevent inverted character spilling
6082 * over character cell (can happen with bold/italic)
6083 */
6084 if (flags & DRAW_CURSOR)
6085 {
6086 pcliprect = &rc;
6087 foptions = ETO_CLIPPED;
6088 }
6089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 SetTextColor(s_hdc, gui.currFgColor);
6091 SelectFont(s_hdc, gui.currFont);
6092
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006093#ifdef FEAT_DIRECTX
6094 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006095 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006096#endif
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6099 {
6100 vim_free(padding);
6101 pad_size = Columns;
6102
Bram Moolenaar734a8672019-12-02 22:49:38 +01006103 // Don't give an out-of-memory message here, it would call us
6104 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006105 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (padding != NULL)
6107 for (i = 0; i < pad_size; i++)
6108 padding[i] = gui.char_width;
6109 }
6110
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 /*
6112 * We have to provide the padding argument because italic and bold versions
6113 * of fixed-width fonts are often one pixel or so wider than their normal
6114 * versions.
6115 * No check for DRAW_BOLD, Windows will have done it already.
6116 */
6117
Bram Moolenaar734a8672019-12-02 22:49:38 +01006118 // Check if there are any UTF-8 characters. If not, use normal text
6119 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 if (enc_utf8)
6121 for (n = 0; n < len; ++n)
6122 if (text[n] >= 0x80)
6123 break;
6124
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006125#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006126 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6127 // required that unicode drawing routine, currently. So this forces it
6128 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006129 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006130 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006131#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006132
Bram Moolenaar734a8672019-12-02 22:49:38 +01006133 // Check if the Unicode buffer exists and is big enough. Create it
6134 // with the same length as the multi-byte string, the number of wide
6135 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006136 if ((enc_utf8
6137 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6138 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 && (unicodebuf == NULL || len > unibuflen))
6140 {
6141 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006142 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143
6144 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006145 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 unibuflen = len;
6148 }
6149
6150 if (enc_utf8 && n < len && unicodebuf != NULL)
6151 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006152 // Output UTF-8 characters. Composing characters should be
6153 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006154 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006155 int wlen; // string length in words
6156 int clen; // string length in characters
6157 int cells; // cell width of string up to composing char
6158 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006159 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006161 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006162 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006164 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006166 c = utf_ptr2char(text + i);
6167 if (c >= 0x10000)
6168 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006169 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006170 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6171 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006172 }
6173 else
6174 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006175 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006176 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006177
6178 if (utf_iscomposing(c))
6179 cw = 0;
6180 else
6181 {
6182 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006183 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006184 cw = 1;
6185 }
6186
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 if (unicodepdy != NULL)
6188 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006189 // Use unicodepdy to make characters fit as we expect, even
6190 // when the font uses different widths (e.g., bold character
6191 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006192 if (c >= 0x10000)
6193 {
6194 unicodepdy[wlen - 2] = cw * gui.char_width;
6195 unicodepdy[wlen - 1] = 0;
6196 }
6197 else
6198 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 }
6200 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006201 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006202 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006204#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006205 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006206 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006207 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006208 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006209 TEXT_X(col), TEXT_Y(row),
6210 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006211 gui.char_width, gui.currFgColor,
6212 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006213 }
6214 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006215#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006216 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6217 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006218 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006220 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006222 // If we want to display codepage data, and the current CP is not the
6223 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 if (unicodebuf != NULL)
6225 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006226 if (enc_latin9)
6227 latin9_to_ucs(text, len, unicodebuf);
6228 else
6229 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 MB_PRECOMPOSED,
6231 (char *)text, len,
6232 (LPWSTR)unicodebuf, unibuflen);
6233 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006234 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006235 // Use unicodepdy to make characters fit as we expect, even
6236 // when the font uses different widths (e.g., bold character
6237 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006238 if (unicodepdy != NULL)
6239 {
6240 int i;
6241 int cw;
6242
6243 for (i = 0; i < len; ++i)
6244 {
6245 cw = utf_char2cells(unicodebuf[i]);
6246 if (cw > 2)
6247 cw = 1;
6248 unicodepdy[i] = cw * gui.char_width;
6249 }
6250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006252 foptions, pcliprect, unicodebuf, len, unicodepdy);
6253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255 }
6256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006259 // Windows will mess up RL text, so we have to draw it character by
6260 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006261 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6263 foptions, pcliprect, (char *)text, len, padding);
6264 else
6265#endif
6266 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6267 foptions, pcliprect, (char *)text, len, padding);
6268 }
6269
Bram Moolenaar734a8672019-12-02 22:49:38 +01006270 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 if (flags & DRAW_UNDERL)
6272 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006273 // When p_linespace is 0, overwrite the bottom row of pixels.
6274 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 if (p_linespace > 1)
6277 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006278 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006280
Bram Moolenaar734a8672019-12-02 22:49:38 +01006281 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006282 if (flags & DRAW_STRIKE)
6283 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006284 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006285 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006286 }
6287
Bram Moolenaar734a8672019-12-02 22:49:38 +01006288 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006289 if (flags & DRAW_UNDERC)
6290 {
6291 int x;
6292 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006293 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006294
6295 y = FILL_Y(row + 1) - 1;
6296 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6297 {
6298 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006299 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006300 }
6301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302}
6303
6304
6305/*
6306 * Output routines.
6307 */
6308
Bram Moolenaar734a8672019-12-02 22:49:38 +01006309/*
6310 * Flush any output to the screen
6311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 void
6313gui_mch_flush(void)
6314{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006315#if defined(FEAT_DIRECTX)
6316 if (IS_ENABLE_DIRECTX())
6317 DWriteContext_Flush(s_dwc);
6318#endif
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 GdiFlush();
6321}
6322
6323 static void
6324clear_rect(RECT *rcp)
6325{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006326 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327}
6328
6329
Bram Moolenaarc716c302006-01-21 22:12:51 +00006330 void
6331gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6332{
6333 RECT workarea_rect;
6334
6335 get_work_area(&workarea_rect);
6336
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006337 *screen_w = workarea_rect.right - workarea_rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006338 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006339 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006340
Bram Moolenaar734a8672019-12-02 22:49:38 +01006341 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6342 // the menubar for MSwin, we subtract it from the screen height, so that
6343 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006344 *screen_h = workarea_rect.bottom - workarea_rect.top
Bram Moolenaar9d488952013-07-21 17:53:58 +02006345 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006346 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2
Bram Moolenaarc716c302006-01-21 22:12:51 +00006347 - GetSystemMetrics(SM_CYCAPTION)
6348#ifdef FEAT_MENU
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006349 - gui_mswin_get_menu_height(FALSE)
Bram Moolenaarc716c302006-01-21 22:12:51 +00006350#endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00006351 ;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006352}
6353
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355#if defined(FEAT_MENU) || defined(PROTO)
6356/*
6357 * Add a sub menu to the menu bar.
6358 */
6359 void
6360gui_mch_add_menu(
6361 vimmenu_T *menu,
6362 int pos)
6363{
6364 vimmenu_T *parent = menu->parent;
6365
6366 menu->submenu_id = CreatePopupMenu();
6367 menu->id = s_menu_id++;
6368
6369 if (menu_is_menubar(menu->name))
6370 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006371 WCHAR *wn;
6372 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006374 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006375 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006376 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006378 infow.cbSize = sizeof(infow);
6379 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6380 | MIIM_SUBMENU;
6381 infow.dwItemData = (long_u)menu;
6382 infow.wID = menu->id;
6383 infow.fType = MFT_STRING;
6384 infow.dwTypeData = wn;
6385 infow.cch = (UINT)wcslen(wn);
6386 infow.hSubMenu = menu->submenu_id;
6387 InsertMenuItemW((parent == NULL)
6388 ? s_menuBar : parent->submenu_id,
6389 (UINT)pos, TRUE, &infow);
6390 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392
Bram Moolenaar734a8672019-12-02 22:49:38 +01006393 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (parent == NULL)
6395 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006396# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 else if (IsWindow(parent->tearoff_handle))
6398 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400}
6401
6402 void
6403gui_mch_show_popupmenu(vimmenu_T *menu)
6404{
6405 POINT mp;
6406
6407 (void)GetCursorPos((LPPOINT)&mp);
6408 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6409}
6410
6411 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006412gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 vimmenu_T *menu = gui_find_menu(path_name);
6415
6416 if (menu != NULL)
6417 {
6418 POINT p;
6419
Bram Moolenaar734a8672019-12-02 22:49:38 +01006420 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006422 if (mouse_pos)
6423 {
6424 int mx, my;
6425
6426 gui_mch_getmouse(&mx, &my);
6427 p.x += mx;
6428 p.y += my;
6429 }
6430 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006432 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6434 }
6435 msg_scroll = FALSE;
6436 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6437 }
6438}
6439
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006440# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441/*
6442 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6443 * create it as a pseudo-"tearoff menu".
6444 */
6445 void
6446gui_make_tearoff(char_u *path_name)
6447{
6448 vimmenu_T *menu = gui_find_menu(path_name);
6449
Bram Moolenaar734a8672019-12-02 22:49:38 +01006450 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (menu != NULL)
6452 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6453}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455
6456/*
6457 * Add a menu item to a menu
6458 */
6459 void
6460gui_mch_add_menu_item(
6461 vimmenu_T *menu,
6462 int idx)
6463{
6464 vimmenu_T *parent = menu->parent;
6465
6466 menu->id = s_menu_id++;
6467 menu->submenu_id = NULL;
6468
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006469# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6471 {
6472 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6473 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6474 }
6475 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006476# endif
6477# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (menu_is_toolbar(parent->name))
6479 {
6480 TBBUTTON newtb;
6481
Bram Moolenaara80faa82020-04-12 19:37:17 +02006482 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (menu_is_separator(menu->name))
6484 {
6485 newtb.iBitmap = 0;
6486 newtb.fsStyle = TBSTYLE_SEP;
6487 }
6488 else
6489 {
6490 newtb.iBitmap = get_toolbar_bitmap(menu);
6491 newtb.fsStyle = TBSTYLE_BUTTON;
6492 }
6493 newtb.idCommand = menu->id;
6494 newtb.fsState = TBSTATE_ENABLED;
6495 newtb.iString = 0;
6496 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6497 (LPARAM)&newtb);
6498 menu->submenu_id = (HMENU)-1;
6499 }
6500 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006503 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006505 wn = enc_to_utf16(menu->name, NULL);
6506 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006508 InsertMenuW(parent->submenu_id, (UINT)idx,
6509 (menu_is_separator(menu->name)
6510 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6511 (UINT)menu->id, wn);
6512 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006514# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (IsWindow(parent->tearoff_handle))
6516 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519}
6520
6521/*
6522 * Destroy the machine specific menu widget.
6523 */
6524 void
6525gui_mch_destroy_menu(vimmenu_T *menu)
6526{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006527# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 /*
6529 * is this a toolbar button?
6530 */
6531 if (menu->submenu_id == (HMENU)-1)
6532 {
6533 int iButton;
6534
6535 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6536 (WPARAM)menu->id, 0);
6537 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6538 }
6539 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
6542 if (menu->parent != NULL
6543 && menu_is_popup(menu->parent->dname)
6544 && menu->parent->submenu_id != NULL)
6545 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6546 else
6547 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6548 if (menu->submenu_id != NULL)
6549 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006550# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (IsWindow(menu->tearoff_handle))
6552 DestroyWindow(menu->tearoff_handle);
6553 if (menu->parent != NULL
6554 && menu->parent->children != NULL
6555 && IsWindow(menu->parent->tearoff_handle))
6556 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 menu->modes = 0;
6559 rebuild_tearoff(menu->parent);
6560 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563}
6564
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006565# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 static void
6567rebuild_tearoff(vimmenu_T *menu)
6568{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006569 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 char_u tbuf[128];
6571 RECT trect;
6572 RECT rct;
6573 RECT roct;
6574 int x, y;
6575
6576 HWND thwnd = menu->tearoff_handle;
6577
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006578 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (GetWindowRect(thwnd, &trect)
6580 && GetWindowRect(s_hwnd, &rct)
6581 && GetClientRect(s_hwnd, &roct))
6582 {
6583 x = trect.left - rct.left;
6584 y = (trect.top - rct.bottom + roct.bottom);
6585 }
6586 else
6587 {
6588 x = y = 0xffffL;
6589 }
6590 DestroyWindow(thwnd);
6591 if (menu->children != NULL)
6592 {
6593 gui_mch_tearoff(tbuf, menu, x, y);
6594 if (IsWindow(menu->tearoff_handle))
6595 (void) SetWindowPos(menu->tearoff_handle,
6596 NULL,
6597 (int)trect.left,
6598 (int)trect.top,
6599 0, 0,
6600 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6601 }
6602}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006603# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
6605/*
6606 * Make a menu either grey or not grey.
6607 */
6608 void
6609gui_mch_menu_grey(
6610 vimmenu_T *menu,
6611 int grey)
6612{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006613# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 /*
6615 * is this a toolbar button?
6616 */
6617 if (menu->submenu_id == (HMENU)-1)
6618 {
6619 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
6620 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0) );
6621 }
6622 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006623# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006624 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6625 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006627# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6629 {
6630 WORD menuID;
6631 HWND menuHandle;
6632
6633 /*
6634 * A tearoff button has changed state.
6635 */
6636 if (menu->children == NULL)
6637 menuID = (WORD)(menu->id);
6638 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006639 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6641 if (menuHandle)
6642 EnableWindow(menuHandle, !grey);
6643
6644 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
Bram Moolenaar734a8672019-12-02 22:49:38 +01006648#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
6650
Bram Moolenaar734a8672019-12-02 22:49:38 +01006651// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653#define add_string(s) strcpy((LPSTR)p, s); (LPSTR)p += (strlen((LPSTR)p) + 1)
6654#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006655#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6658/*
6659 * stuff for dialogs
6660 */
6661
6662/*
6663 * The callback routine used by all the dialogs. Very simple. First,
6664 * acknowledges the INITDIALOG message so that Windows knows to do standard
6665 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6666 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6667 * number.
6668 */
6669 static LRESULT CALLBACK
6670dialog_callback(
6671 HWND hwnd,
6672 UINT message,
6673 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006674 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 if (message == WM_INITDIALOG)
6677 {
6678 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006679 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 (void)SetFocus(hwnd);
6681 if (dialog_default_button > IDCANCEL)
6682 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006683 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006684 // We don't have a default, set focus on another element of the
6685 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006686 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 return FALSE;
6688 }
6689
6690 if (message == WM_COMMAND)
6691 {
6692 int button = LOWORD(wParam);
6693
Bram Moolenaar734a8672019-12-02 22:49:38 +01006694 // Don't end the dialog if something was selected that was
6695 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (button >= DLG_NONBUTTON_CONTROL)
6697 return TRUE;
6698
Bram Moolenaar734a8672019-12-02 22:49:38 +01006699 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006701 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006702 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006703 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006704
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006705 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6706 p = utf16_to_enc(wp, NULL);
6707 vim_strncpy(s_textfield, p, IOSIZE);
6708 vim_free(p);
6709 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712 /*
6713 * Need to check for IDOK because if the user just hits Return to
6714 * accept the default value, some reason this is what we get.
6715 */
6716 if (button == IDOK)
6717 {
6718 if (dialog_default_button > IDCANCEL)
6719 EndDialog(hwnd, dialog_default_button);
6720 }
6721 else
6722 EndDialog(hwnd, button - IDCANCEL);
6723 return TRUE;
6724 }
6725
6726 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6727 {
6728 EndDialog(hwnd, 0);
6729 return TRUE;
6730 }
6731 return FALSE;
6732}
6733
6734/*
6735 * Create a dialog dynamically from the parameter strings.
6736 * type = type of dialog (question, alert, etc.)
6737 * title = dialog title. may be NULL for default title.
6738 * message = text to display. Dialog sizes to accommodate it.
6739 * buttons = '\n' separated list of button captions, default first.
6740 * dfltbutton = number of default button.
6741 *
6742 * This routine returns 1 if the first button is pressed,
6743 * 2 for the second, etc.
6744 *
6745 * 0 indicates Esc was pressed.
6746 * -1 for unexpected error
6747 *
6748 * If stubbing out this fn, return 1.
6749 */
6750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 "IDR_VIM",
6754 "IDR_VIM_ERROR",
6755 "IDR_VIM_ALERT",
6756 "IDR_VIM_INFO",
6757 "IDR_VIM_QUESTION"
6758};
6759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 int
6761gui_mch_dialog(
6762 int type,
6763 char_u *title,
6764 char_u *message,
6765 char_u *buttons,
6766 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006767 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006768 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
6770 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006771 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 int numButtons;
6773 int *buttonWidths, *buttonPositions;
6774 int buttonYpos;
6775 int nchar, i;
6776 DWORD lStyle;
6777 int dlgwidth = 0;
6778 int dlgheight;
6779 int editboxheight;
6780 int horizWidth = 0;
6781 int msgheight;
6782 char_u *pstart;
6783 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006784 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 char_u *tbuffer;
6786 RECT rect;
6787 HWND hwnd;
6788 HDC hdc;
6789 HFONT font, oldFont;
6790 TEXTMETRIC fontInfo;
6791 int fontHeight;
6792 int textWidth, minButtonWidth, messageWidth;
6793 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006794 int maxDialogHeight;
6795 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int vertical;
6797 int dlgPaddingX;
6798 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006799# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006800 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006802# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006803 garray_T ga;
6804 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006806# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006807 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006808# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006809 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006810# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006811 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006812 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar748bf032005-02-02 23:04:36 +00006815 if (s_hwnd == NULL)
6816 get_dialog_font_metrics();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 if ((type < 0) || (type > VIM_LAST_TYPE))
6819 type = 0;
6820
Bram Moolenaar734a8672019-12-02 22:49:38 +01006821 // allocate some memory for dialog template
6822 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006823 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006824 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
6826 if (p == NULL)
6827 return -1;
6828
6829 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006830 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6832 * const.
6833 */
6834 tbuffer = vim_strsave(buttons);
6835 if (tbuffer == NULL)
6836 return -1;
6837
Bram Moolenaar734a8672019-12-02 22:49:38 +01006838 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar734a8672019-12-02 22:49:38 +01006840 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 numButtons = 1;
6842 for (i = 0; tbuffer[i] != '\0'; i++)
6843 {
6844 if (tbuffer[i] == DLG_BUTTON_SEP)
6845 numButtons++;
6846 }
6847 if (dfltbutton >= numButtons)
6848 dfltbutton = -1;
6849
Bram Moolenaar734a8672019-12-02 22:49:38 +01006850 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006851 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (buttonWidths == NULL)
6853 return -1;
6854
Bram Moolenaar734a8672019-12-02 22:49:38 +01006855 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006856 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (buttonPositions == NULL)
6858 return -1;
6859
6860 /*
6861 * Calculate how big the dialog must be.
6862 */
6863 hwnd = GetDesktopWindow();
6864 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006865# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6867 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006868 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 use_lfSysmenu = TRUE;
6870 }
6871 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6874 VARIABLE_PITCH , DLG_FONT_NAME);
6875 if (s_usenewlook)
6876 {
6877 oldFont = SelectFont(hdc, font);
6878 dlgPaddingX = DLG_PADDING_X;
6879 dlgPaddingY = DLG_PADDING_Y;
6880 }
6881 else
6882 {
6883 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
6884 dlgPaddingX = DLG_OLD_STYLE_PADDING_X;
6885 dlgPaddingY = DLG_OLD_STYLE_PADDING_Y;
6886 }
6887 GetTextMetrics(hdc, &fontInfo);
6888 fontHeight = fontInfo.tmHeight;
6889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006891 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892
Bram Moolenaar734a8672019-12-02 22:49:38 +01006893 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006894 if (s_hwnd == NULL)
6895 {
6896 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar734a8672019-12-02 22:49:38 +01006898 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006899 get_work_area(&workarea_rect);
6900 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
6901 if (maxDialogWidth > 600)
6902 maxDialogWidth = 600;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006903 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006904 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006905 }
6906 else
6907 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006908 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006909 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006910 maxDialogWidth = rect.right - rect.left
Bram Moolenaar9d488952013-07-21 17:53:58 +02006911 - (GetSystemMetrics(SM_CXFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006912 GetSystemMetrics(SM_CXPADDEDBORDER)) * 2;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006913 if (maxDialogWidth < DLG_MIN_MAX_WIDTH)
6914 maxDialogWidth = DLG_MIN_MAX_WIDTH;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006915
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006916 maxDialogHeight = rect.bottom - rect.top
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006917 - (GetSystemMetrics(SM_CYFRAME) +
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006918 GetSystemMetrics(SM_CXPADDEDBORDER)) * 4
Bram Moolenaara95d8232013-08-07 15:27:11 +02006919 - GetSystemMetrics(SM_CYCAPTION);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006920 if (maxDialogHeight < DLG_MIN_MAX_HEIGHT)
6921 maxDialogHeight = DLG_MIN_MAX_HEIGHT;
6922 }
6923
Bram Moolenaar734a8672019-12-02 22:49:38 +01006924 // Set dlgwidth to width of message.
6925 // Copy the message into "ga", changing NL to CR-NL and inserting line
6926 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 pstart = message;
6928 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006929 msgheight = 0;
6930 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 do
6932 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006933 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006934
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 // Need to figure out where to break the string. The system does it
6936 // at a word boundary, which would mean we can't compute the number of
6937 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006938 textWidth = 0;
6939 last_white = NULL;
6940 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006941 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006942 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006943 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944 && textWidth > maxDialogWidth * 3 / 4)
6945 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006946 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006947 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006948 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006949 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006950 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006951 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006952 textWidth = 0;
6953
6954 if (last_white != NULL)
6955 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // break the line just after a space
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006957 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006958 pend = last_white + 1;
6959 last_white = NULL;
6960 }
6961 ga_append(&ga, '\r');
6962 ga_append(&ga, '\n');
6963 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006964 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006965
6966 while (--l >= 0)
6967 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006968 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006969 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006971
6972 ga_append(&ga, '\r');
6973 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 pstart = pend + 1;
6975 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006976
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006977 if (ga.ga_data != NULL)
6978 message = ga.ga_data;
6979
Bram Moolenaar734a8672019-12-02 22:49:38 +01006980 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00006981
Bram Moolenaar734a8672019-12-02 22:49:38 +01006982 // Add width of icon to dlgwidth, and some space
Bram Moolenaara95d8232013-08-07 15:27:11 +02006983 dlgwidth = messageWidth + DLG_ICON_WIDTH + 3 * dlgPaddingX
6984 + GetSystemMetrics(SM_CXVSCROLL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986 if (msgheight < DLG_ICON_HEIGHT)
6987 msgheight = DLG_ICON_HEIGHT;
6988
6989 /*
6990 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006991 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00006993 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (!vertical)
6995 {
6996 // Place buttons horizontally if they fit.
6997 horizWidth = dlgPaddingX;
6998 pstart = tbuffer;
6999 i = 0;
7000 do
7001 {
7002 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7003 if (pend == NULL)
7004 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007005 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 if (textWidth < minButtonWidth)
7007 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007008 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 buttonWidths[i] = textWidth;
7010 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007011 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 pstart = pend + 1;
7013 } while (*pend != NUL);
7014
7015 if (horizWidth > maxDialogWidth)
7016 vertical = TRUE; // Too wide to fit on the screen.
7017 else if (horizWidth > dlgwidth)
7018 dlgwidth = horizWidth;
7019 }
7020
7021 if (vertical)
7022 {
7023 // Stack buttons vertically.
7024 pstart = tbuffer;
7025 do
7026 {
7027 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7028 if (pend == NULL)
7029 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007030 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007031 textWidth += dlgPaddingX; // Padding within button
7032 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 if (textWidth > dlgwidth)
7034 dlgwidth = textWidth;
7035 pstart = pend + 1;
7036 } while (*pend != NUL);
7037 }
7038
7039 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007040 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
Bram Moolenaar734a8672019-12-02 22:49:38 +01007042 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (s_usenewlook)
7044 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE |DS_SETFONT;
7045 else
7046 lStyle = DS_MODALFRAME | WS_CAPTION |DS_3DLOOK| WS_VISIBLE;
7047
7048 add_long(lStyle);
7049 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007050 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 add_word(0); // NumberOfItems(will change later)
7052 add_word(10); // x
7053 add_word(10); // y
7054 add_word(PixelToDialogX(dlgwidth)); // cx
7055
7056 // Dialog height.
7057 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007058 dlgheight = msgheight + 2 * dlgPaddingY
7059 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 else
7061 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7062
7063 // Dialog needs to be taller if contains an edit box.
7064 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7065 if (textfield != NULL)
7066 dlgheight += editboxheight;
7067
Bram Moolenaar734a8672019-12-02 22:49:38 +01007068 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007069 if (dlgheight > maxDialogHeight)
7070 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007071 msgheight = msgheight - (dlgheight - maxDialogHeight);
7072 dlgheight = maxDialogHeight;
7073 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007074 // Make sure scrollbar doesn't appear in the middle of the dialog
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007075 messageWidth = dlgwidth - DLG_ICON_WIDTH - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007076 }
7077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 add_word(PixelToDialogY(dlgheight));
7079
7080 add_word(0); // Menu
7081 add_word(0); // Class
7082
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007084 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7085 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 p += nchar;
7087
7088 if (s_usenewlook)
7089 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007090 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007091# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 if (use_lfSysmenu)
7093 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007094 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7096 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007097 wcscpy(p, lfSysmenu.lfFaceName);
7098 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 {
7103 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007104 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106 p += nchar;
7107 }
7108
7109 buttonYpos = msgheight + 2 * dlgPaddingY;
7110
7111 if (textfield != NULL)
7112 buttonYpos += editboxheight;
7113
7114 pstart = tbuffer;
7115 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 for (i = 0; i < numButtons; i++)
7118 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 for ( pend = pstart;
7121 *pend && (*pend != DLG_BUTTON_SEP);
7122 pend++)
7123 ;
7124
7125 if (*pend)
7126 *pend = '\0';
7127
7128 /*
7129 * old NOTE:
7130 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7131 * the focus to the first tab-able button and in so doing makes that
7132 * the default!! Grrr. Workaround: Make the default button the only
7133 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7134 * he/she can use arrow keys.
7135 *
7136 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007137 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 * dialog. Also needed for when the textfield is the default control.
7139 * It appears to work now (perhaps not on Win95?).
7140 */
7141 if (vertical)
7142 {
7143 p = add_dialog_element(p,
7144 (i == dfltbutton
7145 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7146 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007147 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 + 2 * fontHeight * i),
7149 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7150 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007151 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
7153 else
7154 {
7155 p = add_dialog_element(p,
7156 (i == dfltbutton
7157 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7158 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007159 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 PixelToDialogX(buttonWidths[i]),
7161 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007162 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007164 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 }
7166 *pnumitems += numButtons;
7167
Bram Moolenaar734a8672019-12-02 22:49:38 +01007168 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 p = add_dialog_element(p, SS_ICON,
7170 PixelToDialogX(dlgPaddingX),
7171 PixelToDialogY(dlgPaddingY),
7172 PixelToDialogX(DLG_ICON_WIDTH),
7173 PixelToDialogY(DLG_ICON_HEIGHT),
7174 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7175 dlg_icons[type]);
7176
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007178 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
7179 PixelToDialogX(2 * dlgPaddingX + DLG_ICON_WIDTH),
7180 PixelToDialogY(dlgPaddingY),
7181 (WORD)(PixelToDialogX(messageWidth) + 1),
7182 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007183 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
Bram Moolenaar734a8672019-12-02 22:49:38 +01007185 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (textfield != NULL)
7187 {
7188 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7189 PixelToDialogX(2 * dlgPaddingX),
7190 PixelToDialogY(2 * dlgPaddingY + msgheight),
7191 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7192 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007193 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 *pnumitems += 1;
7195 }
7196
7197 *pnumitems += 2;
7198
7199 SelectFont(hdc, oldFont);
7200 DeleteObject(font);
7201 ReleaseDC(hwnd, hdc);
7202
Bram Moolenaar734a8672019-12-02 22:49:38 +01007203 // Let the dialog_callback() function know which button to make default
7204 // If we have an edit box, make that the default. We also need to tell
7205 // dialog_callback() if this dialog contains an edit box or not. We do
7206 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 if (textfield != NULL)
7208 {
7209 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7210 s_textfield = textfield;
7211 }
7212 else
7213 {
7214 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7215 s_textfield = NULL;
7216 }
7217
Bram Moolenaar734a8672019-12-02 22:49:38 +01007218 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007220 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 (LPDLGTEMPLATE)pdlgtemplate,
7222 s_hwnd,
7223 (DLGPROC)dialog_callback);
7224
7225 LocalFree(LocalHandle(pdlgtemplate));
7226 vim_free(tbuffer);
7227 vim_free(buttonWidths);
7228 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007229 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
Bram Moolenaar734a8672019-12-02 22:49:38 +01007231 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 (void)SetFocus(s_hwnd);
7233
7234 return nchar;
7235}
7236
Bram Moolenaar734a8672019-12-02 22:49:38 +01007237#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239/*
7240 * Put a simple element (basic class) onto a dialog template in memory.
7241 * return a pointer to where the next item should be added.
7242 *
7243 * parameters:
7244 * lStyle = additional style flags
7245 * (be careful, NT3.51 & Win32s will ignore the new ones)
7246 * x,y = x & y positions IN DIALOG UNITS
7247 * w,h = width and height IN DIALOG UNITS
7248 * Id = ID used in messages
7249 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7250 * caption = usually text or resource name
7251 *
7252 * TODO: use the length information noted here to enable the dialog creation
7253 * routines to work out more exactly how much memory they need to alloc.
7254 */
7255 static PWORD
7256add_dialog_element(
7257 PWORD p,
7258 DWORD lStyle,
7259 WORD x,
7260 WORD y,
7261 WORD w,
7262 WORD h,
7263 WORD Id,
7264 WORD clss,
7265 const char *caption)
7266{
7267 int nchar;
7268
Bram Moolenaar734a8672019-12-02 22:49:38 +01007269 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7271 *p++ = LOWORD(lStyle);
7272 *p++ = HIWORD(lStyle);
7273 *p++ = 0; // LOWORD (lExtendedStyle)
7274 *p++ = 0; // HIWORD (lExtendedStyle)
7275 *p++ = x;
7276 *p++ = y;
7277 *p++ = w;
7278 *p++ = h;
7279 *p++ = Id; //9 or 10 words in all
7280
7281 *p++ = (WORD)0xffff;
7282 *p++ = clss; //2 more here
7283
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007284 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 p += nchar;
7286
7287 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7288
7289 return p; //total = 15+ (strlen(caption)) words
7290 // = 30 + 2(strlen(caption) bytes reqd
7291}
7292
7293
7294/*
7295 * Helper routine. Take an input pointer, return closest pointer that is
7296 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7297 */
7298 static LPWORD
7299lpwAlign(
7300 LPWORD lpIn)
7301{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007302 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007304 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 ul += 3;
7306 ul >>= 2;
7307 ul <<= 2;
7308 return (LPWORD)ul;
7309}
7310
7311/*
7312 * Helper routine. Takes second parameter as Ansi string, copies it to first
7313 * parameter as wide character (16-bits / char) string, and returns integer
7314 * number of wide characters (words) in string (including the trailing wide
7315 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007316 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7317 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static int
7319nCopyAnsiToWideChar(
7320 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007321 LPSTR lpAnsiIn,
7322 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
7324 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007325 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 int i;
7327 WCHAR *wn;
7328
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007329 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007331 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007332 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (wn != NULL)
7334 {
7335 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007336 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 vim_free(wn);
7338 }
7339 }
7340 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 nChar = MultiByteToWideChar(
7343 enc_codepage > 0 ? enc_codepage : CP_ACP,
7344 MB_PRECOMPOSED,
7345 lpAnsiIn, len,
7346 lpWCStr, len);
7347 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007348 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 return nChar;
7352}
7353
7354
7355#ifdef FEAT_TEAROFF
7356/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007357 * Lookup menu handle from "menu_id".
7358 */
7359 static HMENU
7360tearoff_lookup_menuhandle(
7361 vimmenu_T *menu,
7362 WORD menu_id)
7363{
7364 for ( ; menu != NULL; menu = menu->next)
7365 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007366 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007367 continue;
7368 if (menu_is_separator(menu->dname))
7369 continue;
7370 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7371 return menu->submenu_id;
7372 }
7373 return NULL;
7374}
7375
7376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 * The callback function for all the modeless dialogs that make up the
7378 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7379 * thinking its menus have been clicked), and go away when closed.
7380 */
7381 static LRESULT CALLBACK
7382tearoff_callback(
7383 HWND hwnd,
7384 UINT message,
7385 WPARAM wParam,
7386 LPARAM lParam)
7387{
7388 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007389 {
7390 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
Bram Moolenaar734a8672019-12-02 22:49:38 +01007394 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 HandleMouseHide(message, lParam);
7396
7397 if (message == WM_COMMAND)
7398 {
7399 if ((WORD)(LOWORD(wParam)) & 0x8000)
7400 {
7401 POINT mp;
7402 RECT rect;
7403
7404 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7405 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007406 vimmenu_T *menu;
7407
7408 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007410 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7412 (int)rect.right - 8,
7413 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007414 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 s_hwnd,
7416 NULL);
7417 /*
7418 * NOTE: The pop-up menu can eat the mouse up event.
7419 * We deal with this in normal.c.
7420 */
7421 }
7422 }
7423 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7426 /*
7427 * Give main window the focus back: this is so after
7428 * choosing a tearoff button you can start typing again
7429 * straight away.
7430 */
7431 (void)SetFocus(s_hwnd);
7432 return TRUE;
7433 }
7434 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7435 {
7436 DestroyWindow(hwnd);
7437 return TRUE;
7438 }
7439
Bram Moolenaar734a8672019-12-02 22:49:38 +01007440 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (message == WM_EXITSIZEMOVE)
7442 (void)SetActiveWindow(s_hwnd);
7443
7444 return FALSE;
7445}
7446#endif
7447
7448
7449/*
7450 * Decide whether to use the "new look" (small, non-bold font) or the "old
7451 * look" (big, clanky font) for dialogs, and work out a few values for use
7452 * later accordingly.
7453 */
7454 static void
7455get_dialog_font_metrics(void)
7456{
7457 HDC hdc;
7458 HFONT hfontTools = 0;
7459 DWORD dlgFontSize;
7460 SIZE size;
7461#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007462 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463#endif
7464
7465 s_usenewlook = FALSE;
7466
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007468 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007469 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471#endif
7472 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7473 0, 0, 0, 0, VARIABLE_PITCH , DLG_FONT_NAME);
7474
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007475 if (hfontTools)
7476 {
7477 hdc = GetDC(s_hwnd);
7478 SelectObject(hdc, hfontTools);
7479 /*
7480 * GetTextMetrics() doesn't return the right value in
7481 * tmAveCharWidth, so we have to figure out the dialog base units
7482 * ourselves.
7483 */
7484 GetTextExtentPoint(hdc,
7485 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
7486 52, &size);
7487 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007489 s_dlgfntwidth = (WORD)((size.cx / 26 + 1) / 2);
7490 s_dlgfntheight = (WORD)size.cy;
7491 s_usenewlook = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493
7494 if (!s_usenewlook)
7495 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007496 dlgFontSize = GetDialogBaseUnits(); // fall back to big old system
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 s_dlgfntwidth = LOWORD(dlgFontSize);
7498 s_dlgfntheight = HIWORD(dlgFontSize);
7499 }
7500}
7501
7502#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7503/*
7504 * Create a pseudo-"tearoff menu" based on the child
7505 * items of a given menu pointer.
7506 */
7507 static void
7508gui_mch_tearoff(
7509 char_u *title,
7510 vimmenu_T *menu,
7511 int initX,
7512 int initY)
7513{
7514 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7515 int template_len;
7516 int nchar, textWidth, submenuWidth;
7517 DWORD lStyle;
7518 DWORD lExtendedStyle;
7519 WORD dlgwidth;
7520 WORD menuID;
7521 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007522 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 vimmenu_T *the_menu = menu;
7524 HWND hwnd;
7525 HDC hdc;
7526 HFONT font, oldFont;
7527 int col, spaceWidth, len;
7528 int columnWidths[2];
7529 char_u *label, *text;
7530 int acLen = 0;
7531 int nameLen;
7532 int padding0, padding1, padding2 = 0;
7533 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007534 int x;
7535 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007536# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007537 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007539# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
7541 /*
7542 * If this menu is already torn off, move it to the mouse position.
7543 */
7544 if (IsWindow(menu->tearoff_handle))
7545 {
7546 POINT mp;
7547 if (GetCursorPos((LPPOINT)&mp))
7548 {
7549 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7550 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7551 }
7552 return;
7553 }
7554
7555 /*
7556 * Create a new tearoff.
7557 */
7558 if (*title == MNU_HIDDEN_CHAR)
7559 title++;
7560
Bram Moolenaar734a8672019-12-02 22:49:38 +01007561 // Allocate memory to store the dialog template. It's made bigger when
7562 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 template_len = DLG_ALLOC_SIZE;
7564 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7565 if (p == NULL)
7566 return;
7567
7568 hwnd = GetDesktopWindow();
7569 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007570# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7572 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007573 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 use_lfSysmenu = TRUE;
7575 }
7576 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7579 VARIABLE_PITCH , DLG_FONT_NAME);
7580 if (s_usenewlook)
7581 oldFont = SelectFont(hdc, font);
7582 else
7583 oldFont = SelectFont(hdc, GetStockObject(SYSTEM_FONT));
7584
Bram Moolenaar734a8672019-12-02 22:49:38 +01007585 // Calculate width of a single space. Used for padding columns to the
7586 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007587 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588
Bram Moolenaar734a8672019-12-02 22:49:38 +01007589 // Figure out max width of the text column, the accelerator column and the
7590 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 submenuWidth = 0;
7592 for (col = 0; col < 2; col++)
7593 {
7594 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007595 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007597 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 text = (col == 0) ? pmenu->dname : pmenu->actext;
7599 if (text != NULL && *text != NUL)
7600 {
7601 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7602 if (textWidth > columnWidths[col])
7603 columnWidths[col] = textWidth;
7604 }
7605 if (pmenu->children != NULL)
7606 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7607 }
7608 }
7609 if (columnWidths[1] == 0)
7610 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (submenuWidth != 0)
7613 columnWidths[0] += submenuWidth;
7614 else
7615 columnWidths[0] += spaceWidth;
7616 }
7617 else
7618 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007619 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7621 columnWidths[1] += submenuWidth;
7622 }
7623
7624 /*
7625 * Now find the total width of our 'menu'.
7626 */
7627 textWidth = columnWidths[0] + columnWidths[1];
7628 if (submenuWidth != 0)
7629 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007630 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7632 textWidth += submenuWidth;
7633 }
7634 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7635 if (textWidth > dlgwidth)
7636 dlgwidth = textWidth;
7637 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7638
Bram Moolenaar734a8672019-12-02 22:49:38 +01007639 // start to fill in the dlgtemplate information. addressing by WORDs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 if (s_usenewlook)
7641 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU |DS_SETFONT| WS_VISIBLE;
7642 else
7643 lStyle = DS_MODALFRAME | WS_CAPTION| WS_SYSMENU | WS_VISIBLE;
7644
7645 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7646 *p++ = LOWORD(lStyle);
7647 *p++ = HIWORD(lStyle);
7648 *p++ = LOWORD(lExtendedStyle);
7649 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007650 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007652 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007654 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 else
7656 *p++ = PixelToDialogX(initX); // x
7657 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007658 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 else
7660 *p++ = PixelToDialogY(initY); // y
7661 *p++ = PixelToDialogX(dlgwidth); // cx
7662 ptrueheight = p;
7663 *p++ = 0; // dialog height: changed later anyway
7664 *p++ = 0; // Menu
7665 *p++ = 0; // Class
7666
Bram Moolenaar734a8672019-12-02 22:49:38 +01007667 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007669 ? (LPSTR)title
7670 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 p += nchar;
7672
7673 if (s_usenewlook)
7674 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007675 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007676# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (use_lfSysmenu)
7678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007679 // point size
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7681 GetDeviceCaps(hdc, LOGPIXELSY));
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007682 wcscpy(p, lfSysmenu.lfFaceName);
7683 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 }
7685 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
7688 *p++ = DLG_FONT_POINT_SIZE; // point size
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007689 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691 p += nchar;
7692 }
7693
7694 /*
7695 * Loop over all the items in the menu.
7696 * But skip over the tearbar.
7697 */
7698 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7699 menu = menu->children->next;
7700 else
7701 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007702 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 for ( ; menu != NULL; menu = menu->next)
7704 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007705 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 continue;
7707 if (menu_is_separator(menu->dname))
7708 {
7709 sepPadding += 3;
7710 continue;
7711 }
7712
Bram Moolenaar734a8672019-12-02 22:49:38 +01007713 // Check if there still is plenty of room in the template. Make it
7714 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7716 {
7717 WORD *newp;
7718
7719 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7720 if (newp != NULL)
7721 {
7722 template_len += 4096;
7723 mch_memmove(newp, pdlgtemplate,
7724 (char *)p - (char *)pdlgtemplate);
7725 p = newp + (p - pdlgtemplate);
7726 pnumitems = newp + (pnumitems - pdlgtemplate);
7727 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7728 LocalFree(LocalHandle(pdlgtemplate));
7729 pdlgtemplate = newp;
7730 }
7731 }
7732
Bram Moolenaar734a8672019-12-02 22:49:38 +01007733 // Figure out minimal length of this menu label. Use "name" for the
7734 // actual text, "dname" for estimating the displayed size. "name"
7735 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 len = nameLen = (int)STRLEN(menu->name);
7737 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7738 (int)STRLEN(menu->dname))) / spaceWidth;
7739 len += padding0;
7740
7741 if (menu->actext != NULL)
7742 {
7743 acLen = (int)STRLEN(menu->actext);
7744 len += acLen;
7745 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7746 }
7747 else
7748 textWidth = 0;
7749 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7750 len += padding1;
7751
7752 if (menu->children == NULL)
7753 {
7754 padding2 = submenuWidth / spaceWidth;
7755 len += padding2;
7756 menuID = (WORD)(menu->id);
7757 }
7758 else
7759 {
7760 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007761 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763
Bram Moolenaar734a8672019-12-02 22:49:38 +01007764 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007765 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 if (label == NULL)
7767 break;
7768
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007769 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007770 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007772 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 while (padding0-- > 0)
7774 *text++ = ' ';
7775 if (menu->actext != NULL)
7776 {
7777 STRNCPY(text, menu->actext, acLen);
7778 text += acLen;
7779 }
7780 while (padding1-- > 0)
7781 *text++ = ' ';
7782 if (menu->children != NULL)
7783 {
7784 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7785 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7786 }
7787 else
7788 {
7789 while (padding2-- > 0)
7790 *text++ = ' ';
7791 }
7792 *text = NUL;
7793
7794 /*
7795 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7796 * W95/NT4 it makes the tear-off look more like a menu.
7797 */
7798 p = add_dialog_element(p,
7799 BS_PUSHBUTTON|BS_LEFT,
7800 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7801 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7802 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7803 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007804 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 vim_free(label);
7806 (*pnumitems)++;
7807 }
7808
7809 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7810
7811
Bram Moolenaar734a8672019-12-02 22:49:38 +01007812 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007813 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007814 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 (LPDLGTEMPLATE)pdlgtemplate,
7816 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007817 (DLGPROC)tearoff_callback,
7818 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820 LocalFree(LocalHandle(pdlgtemplate));
7821 SelectFont(hdc, oldFont);
7822 DeleteObject(font);
7823 ReleaseDC(hwnd, hdc);
7824
7825 /*
7826 * Reassert ourselves as the active window. This is so that after creating
7827 * a tearoff, the user doesn't have to click with the mouse just to start
7828 * typing again!
7829 */
7830 (void)SetActiveWindow(s_hwnd);
7831
Bram Moolenaar734a8672019-12-02 22:49:38 +01007832 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 force_menu_update = TRUE;
7834}
7835#endif
7836
7837#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007838# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
Bram Moolenaar734a8672019-12-02 22:49:38 +01007840// This not defined in older SDKs
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841# ifndef TBSTYLE_FLAT
7842# define TBSTYLE_FLAT 0x0800
7843# endif
7844
7845/*
7846 * Create the toolbar, initially unpopulated.
7847 * (just like the menu, there are no defaults, it's all
7848 * set up through menu.vim)
7849 */
7850 static void
7851initialise_toolbar(void)
7852{
7853 InitCommonControls();
7854 s_toolbarhwnd = CreateToolbarEx(
7855 s_hwnd,
7856 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7857 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007858 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007859 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 IDR_TOOLBAR1, // id of initial bitmap
7861 NULL,
7862 0, // initial number of buttons
7863 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7864 TOOLBAR_BUTTON_HEIGHT,
7865 TOOLBAR_BUTTON_WIDTH,
7866 TOOLBAR_BUTTON_HEIGHT,
7867 sizeof(TBBUTTON)
7868 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007869
7870 // Remove transparency from the toolbar to prevent the main window
7871 // background colour showing through
7872 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7873 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7874
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007875 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876
7877 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
7878}
7879
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007880 static LRESULT CALLBACK
7881toolbar_wndproc(
7882 HWND hwnd,
7883 UINT uMsg,
7884 WPARAM wParam,
7885 LPARAM lParam)
7886{
7887 HandleMouseHide(uMsg, lParam);
7888 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7889}
7890
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 static int
7892get_toolbar_bitmap(vimmenu_T *menu)
7893{
7894 int i = -1;
7895
7896 /*
7897 * Check user bitmaps first, unless builtin is specified.
7898 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007899 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 char_u fname[MAXPATHL];
7902 HANDLE hbitmap = NULL;
7903
7904 if (menu->iconfile != NULL)
7905 {
7906 gui_find_iconfile(menu->iconfile, fname, "bmp");
7907 hbitmap = LoadImage(
7908 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007909 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 IMAGE_BITMAP,
7911 TOOLBAR_BUTTON_WIDTH,
7912 TOOLBAR_BUTTON_HEIGHT,
7913 LR_LOADFROMFILE |
7914 LR_LOADMAP3DCOLORS
7915 );
7916 }
7917
7918 /*
7919 * If the LoadImage call failed, or the "icon=" file
7920 * didn't exist or wasn't specified, try the menu name
7921 */
7922 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007923 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007924# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007925 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007926# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007927 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 hbitmap = LoadImage(
7929 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007930 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 IMAGE_BITMAP,
7932 TOOLBAR_BUTTON_WIDTH,
7933 TOOLBAR_BUTTON_HEIGHT,
7934 LR_LOADFROMFILE |
7935 LR_LOADMAP3DCOLORS
7936 );
7937
7938 if (hbitmap != NULL)
7939 {
7940 TBADDBITMAP tbAddBitmap;
7941
7942 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007943 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944
7945 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7946 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007947 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 }
7949 }
7950 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7951 i = menu->iconidx;
7952
7953 return i;
7954}
7955#endif
7956
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007957#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7958 static void
7959initialise_tabline(void)
7960{
7961 InitCommonControls();
7962
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007963 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007964 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007965 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007966 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007967 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007968
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007969 gui.tabline_height = TABLINE_HEIGHT;
7970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007971# ifdef USE_SYSMENU_FONT
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007972 set_tabline_font();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007973# endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007974}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007975
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007976/*
7977 * Get tabpage_T from POINT.
7978 */
7979 static tabpage_T *
7980GetTabFromPoint(
7981 HWND hWnd,
7982 POINT pt)
7983{
7984 tabpage_T *ptp = NULL;
7985
7986 if (gui_mch_showing_tabline())
7987 {
7988 TCHITTESTINFO htinfo;
7989 htinfo.pt = pt;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007990 // ignore if a window under cusor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007991 if (s_tabhwnd == hWnd)
7992 {
7993 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
7994 if (idx != -1)
7995 ptp = find_tabpage(idx + 1);
7996 }
7997 }
7998 return ptp;
7999}
8000
8001static POINT s_pt = {0, 0};
8002static HCURSOR s_hCursor = NULL;
8003
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008004 static LRESULT CALLBACK
8005tabline_wndproc(
8006 HWND hwnd,
8007 UINT uMsg,
8008 WPARAM wParam,
8009 LPARAM lParam)
8010{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008011 POINT pt;
8012 tabpage_T *tp;
8013 RECT rect;
8014 int nCenter;
8015 int idx0;
8016 int idx1;
8017
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008018 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008019
8020 switch (uMsg)
8021 {
8022 case WM_LBUTTONDOWN:
8023 {
8024 s_pt.x = GET_X_LPARAM(lParam);
8025 s_pt.y = GET_Y_LPARAM(lParam);
8026 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008027 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008028 break;
8029 }
8030 case WM_MOUSEMOVE:
8031 if (GetCapture() == hwnd
8032 && ((wParam & MK_LBUTTON)) != 0)
8033 {
8034 pt.x = GET_X_LPARAM(lParam);
8035 pt.y = s_pt.y;
8036 if (abs(pt.x - s_pt.x) > GetSystemMetrics(SM_CXDRAG))
8037 {
8038 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8039
8040 tp = GetTabFromPoint(hwnd, pt);
8041 if (tp != NULL)
8042 {
8043 idx0 = tabpage_index(curtab) - 1;
8044 idx1 = tabpage_index(tp) - 1;
8045
8046 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8047 nCenter = rect.left + (rect.right - rect.left) / 2;
8048
Bram Moolenaar734a8672019-12-02 22:49:38 +01008049 // Check if the mouse cursor goes over the center of
8050 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008051 if ((idx0 < idx1) && (nCenter < pt.x))
8052 {
8053 tabpage_move(idx1 + 1);
8054 update_screen(0);
8055 }
8056 else if ((idx1 < idx0) && (pt.x < nCenter))
8057 {
8058 tabpage_move(idx1);
8059 update_screen(0);
8060 }
8061 }
8062 }
8063 }
8064 break;
8065 case WM_LBUTTONUP:
8066 {
8067 if (GetCapture() == hwnd)
8068 {
8069 SetCursor(s_hCursor);
8070 ReleaseCapture();
8071 }
8072 break;
8073 }
8074 default:
8075 break;
8076 }
8077
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008078 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8079}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008080#endif
8081
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8083/*
8084 * Make the GUI window come to the foreground.
8085 */
8086 void
8087gui_mch_set_foreground(void)
8088{
8089 if (IsIconic(s_hwnd))
8090 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8091 SetForegroundWindow(s_hwnd);
8092}
8093#endif
8094
8095#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8096 static void
8097dyn_imm_load(void)
8098{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008099 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (hLibImm == NULL)
8101 return;
8102
8103 pImmGetCompositionStringA
8104 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringA");
8105 pImmGetCompositionStringW
8106 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionStringW");
8107 pImmGetContext
8108 = (void *)GetProcAddress(hLibImm, "ImmGetContext");
8109 pImmAssociateContext
8110 = (void *)GetProcAddress(hLibImm, "ImmAssociateContext");
8111 pImmReleaseContext
8112 = (void *)GetProcAddress(hLibImm, "ImmReleaseContext");
8113 pImmGetOpenStatus
8114 = (void *)GetProcAddress(hLibImm, "ImmGetOpenStatus");
8115 pImmSetOpenStatus
8116 = (void *)GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008117 pImmGetCompositionFontW
8118 = (void *)GetProcAddress(hLibImm, "ImmGetCompositionFontW");
8119 pImmSetCompositionFontW
8120 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 pImmSetCompositionWindow
8122 = (void *)GetProcAddress(hLibImm, "ImmSetCompositionWindow");
8123 pImmGetConversionStatus
8124 = (void *)GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008125 pImmSetConversionStatus
8126 = (void *)GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 if ( pImmGetCompositionStringA == NULL
8129 || pImmGetCompositionStringW == NULL
8130 || pImmGetContext == NULL
8131 || pImmAssociateContext == NULL
8132 || pImmReleaseContext == NULL
8133 || pImmGetOpenStatus == NULL
8134 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008135 || pImmGetCompositionFontW == NULL
8136 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008138 || pImmGetConversionStatus == NULL
8139 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {
8141 FreeLibrary(hLibImm);
8142 hLibImm = NULL;
8143 pImmGetContext = NULL;
8144 return;
8145 }
8146
8147 return;
8148}
8149
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150#endif
8151
8152#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8153
8154# ifdef FEAT_XPM_W32
8155# define IMAGE_XPM 100
8156# endif
8157
8158typedef struct _signicon_t
8159{
8160 HANDLE hImage;
8161 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008162# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008163 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165} signicon_t;
8166
8167 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008168gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 signicon_t *sign;
8171 int x, y, w, h;
8172
8173 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8174 return;
8175
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008176# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008177 if (IS_ENABLE_DIRECTX())
8178 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008179# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 x = TEXT_X(col);
8182 y = TEXT_Y(row);
8183 w = gui.char_width * 2;
8184 h = gui.char_height;
8185 switch (sign->uType)
8186 {
8187 case IMAGE_BITMAP:
8188 {
8189 HDC hdcMem;
8190 HBITMAP hbmpOld;
8191
8192 hdcMem = CreateCompatibleDC(s_hdc);
8193 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8194 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8195 SelectObject(hdcMem, hbmpOld);
8196 DeleteDC(hdcMem);
8197 }
8198 break;
8199 case IMAGE_ICON:
8200 case IMAGE_CURSOR:
8201 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8202 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008203# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 case IMAGE_XPM:
8205 {
8206 HDC hdcMem;
8207 HBITMAP hbmpOld;
8208
8209 hdcMem = CreateCompatibleDC(s_hdc);
8210 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008211 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8213
8214 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008215 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8217 SelectObject(hdcMem, hbmpOld);
8218 DeleteDC(hdcMem);
8219 }
8220 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008221# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 }
8223}
8224
8225 static void
8226close_signicon_image(signicon_t *sign)
8227{
8228 if (sign)
8229 switch (sign->uType)
8230 {
8231 case IMAGE_BITMAP:
8232 DeleteObject((HGDIOBJ)sign->hImage);
8233 break;
8234 case IMAGE_CURSOR:
8235 DestroyCursor((HCURSOR)sign->hImage);
8236 break;
8237 case IMAGE_ICON:
8238 DestroyIcon((HICON)sign->hImage);
8239 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008240# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 case IMAGE_XPM:
8242 DeleteObject((HBITMAP)sign->hImage);
8243 DeleteObject((HBITMAP)sign->hShape);
8244 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008245# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 }
8247}
8248
8249 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008250gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 signicon_t sign, *psign;
8253 char_u *ext;
8254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008256 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 if (ext > signfile)
8258 {
8259 int do_load = 1;
8260
8261 if (!STRICMP(ext, ".bmp"))
8262 sign.uType = IMAGE_BITMAP;
8263 else if (!STRICMP(ext, ".ico"))
8264 sign.uType = IMAGE_ICON;
8265 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8266 sign.uType = IMAGE_CURSOR;
8267 else
8268 do_load = 0;
8269
8270 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008271 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 gui.char_width * 2, gui.char_height,
8273 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008274# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 if (!STRICMP(ext, ".xpm"))
8276 {
8277 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008278 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8279 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 }
8283
8284 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008285 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 *psign = sign;
8287
8288 if (!psign)
8289 {
8290 if (sign.hImage)
8291 close_signicon_image(&sign);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 emsg(_(e_signdata));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
8294 return (void *)psign;
8295
8296}
8297
8298 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008299gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 if (sign)
8302 {
8303 close_signicon_image((signicon_t *)sign);
8304 vim_free(sign);
8305 }
8306}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008309#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310
Bram Moolenaar734a8672019-12-02 22:49:38 +01008311/*
8312 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008313 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008315 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8317 * to get current mouse position).
8318 *
8319 * Trying to use as more Windows services as possible, and as less
8320 * IE version as possible :)).
8321 *
8322 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8323 * BalloonEval struct.
8324 * 2) Enable/Disable simply create/kill BalloonEval Timer
8325 * 3) When there was enough inactivity, timer procedure posts
8326 * async request to debugger
8327 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8328 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008329 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 */
8331
Bram Moolenaar45360022005-07-21 21:08:21 +00008332/*
8333 * determine whether installed Common Controls support multiline tooltips
8334 * (i.e. their version is >= 4.70
8335 */
8336 int
8337multiline_balloon_available(void)
8338{
8339 HINSTANCE hDll;
8340 static char comctl_dll[] = "comctl32.dll";
8341 static int multiline_tip = MAYBE;
8342
8343 if (multiline_tip != MAYBE)
8344 return multiline_tip;
8345
8346 hDll = GetModuleHandle(comctl_dll);
8347 if (hDll != NULL)
8348 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008349 DLLGETVERSIONPROC pGetVer;
8350 pGetVer = (DLLGETVERSIONPROC)GetProcAddress(hDll, "DllGetVersion");
Bram Moolenaar45360022005-07-21 21:08:21 +00008351
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008352 if (pGetVer != NULL)
8353 {
8354 DLLVERSIONINFO dvi;
8355 HRESULT hr;
Bram Moolenaar45360022005-07-21 21:08:21 +00008356
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008357 ZeroMemory(&dvi, sizeof(dvi));
8358 dvi.cbSize = sizeof(dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008359
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008360 hr = (*pGetVer)(&dvi);
Bram Moolenaar45360022005-07-21 21:08:21 +00008361
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008362 if (SUCCEEDED(hr)
Bram Moolenaar45360022005-07-21 21:08:21 +00008363 && (dvi.dwMajorVersion > 4
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008364 || (dvi.dwMajorVersion == 4
8365 && dvi.dwMinorVersion >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008366 {
8367 multiline_tip = TRUE;
8368 return multiline_tip;
8369 }
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008370 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008371 else
8372 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008373 // there is chance we have ancient CommCtl 4.70
8374 // which doesn't export DllGetVersion
Bram Moolenaar45360022005-07-21 21:08:21 +00008375 DWORD dwHandle = 0;
8376 DWORD len = GetFileVersionInfoSize(comctl_dll, &dwHandle);
8377 if (len > 0)
8378 {
8379 VS_FIXEDFILEINFO *ver;
8380 UINT vlen = 0;
8381 void *data = alloc(len);
8382
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008383 if ((data != NULL
Bram Moolenaar45360022005-07-21 21:08:21 +00008384 && GetFileVersionInfo(comctl_dll, 0, len, data)
8385 && VerQueryValue(data, "\\", (void **)&ver, &vlen)
8386 && vlen
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008387 && HIWORD(ver->dwFileVersionMS) > 4)
8388 || ((HIWORD(ver->dwFileVersionMS) == 4
8389 && LOWORD(ver->dwFileVersionMS) >= 70)))
Bram Moolenaar45360022005-07-21 21:08:21 +00008390 {
8391 vim_free(data);
8392 multiline_tip = TRUE;
8393 return multiline_tip;
8394 }
8395 vim_free(data);
8396 }
8397 }
8398 }
8399 multiline_tip = FALSE;
8400 return multiline_tip;
8401}
8402
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008403 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008404make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008405{
8406 TOOLINFOW *pti;
8407 int ToolInfoSize;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008408
8409 if (multiline_balloon_available() == TRUE)
8410 ToolInfoSize = sizeof(TOOLINFOW_NEW);
8411 else
8412 ToolInfoSize = sizeof(TOOLINFOW);
8413
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008414 pti = alloc(ToolInfoSize);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008415 if (pti == NULL)
8416 return;
8417
8418 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8419 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8420 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008421 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008422
8423 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8424 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8425
8426 pti->cbSize = ToolInfoSize;
8427 pti->uFlags = TTF_SUBCLASS;
8428 pti->hwnd = beval->target;
8429 pti->hinst = 0; // Don't use string resources
8430 pti->uId = ID_BEVAL_TOOLTIP;
8431
8432 if (multiline_balloon_available() == TRUE)
8433 {
8434 RECT rect;
8435 TOOLINFOW_NEW *ptin = (TOOLINFOW_NEW *)pti;
8436 pti->lpszText = LPSTR_TEXTCALLBACKW;
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008437 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8438 ptin->lParam = (LPARAM)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008439 // switch multiline tooltips on
8440 if (GetClientRect(s_textArea, &rect))
8441 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8442 (LPARAM)rect.right);
8443 }
8444 else
8445 {
8446 // do this old way
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008447 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8448 pti->lpszText = (LPWSTR)beval->tofree;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008449 }
8450
8451 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8452 pti->rect.left = pt.x - 3;
8453 pti->rect.top = pt.y - 3;
8454 pti->rect.right = pt.x + 3;
8455 pti->rect.bottom = pt.y + 3;
8456
8457 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8458 // Make tooltip appear sooner.
8459 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8460 // I've performed some tests and it seems the longest possible life time
8461 // of tooltip is 30 seconds.
8462 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8463 /*
8464 * HACK: force tooltip to appear, because it'll not appear until
8465 * first mouse move. D*mn M$
8466 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8467 */
8468 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8469 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8470 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008471}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008472
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008474delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008476 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477}
8478
8479 static VOID CALLBACK
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008480BevalTimerProc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008481 HWND hwnd UNUSED,
8482 UINT uMsg UNUSED,
8483 UINT_PTR idEvent UNUSED,
8484 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 POINT pt;
8487 RECT rect;
8488
8489 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8490 return;
8491
8492 GetCursorPos(&pt);
8493 if (WindowFromPoint(pt) != s_textArea)
8494 return;
8495
8496 ScreenToClient(s_textArea, &pt);
8497 GetClientRect(s_textArea, &rect);
8498 if (!PtInRect(&rect, pt))
8499 return;
8500
8501 if (LastActivity > 0
8502 && (dwTime - LastActivity) >= (DWORD)p_bdlay
8503 && (cur_beval->showState != ShS_PENDING
8504 || abs(cur_beval->x - pt.x) > 3
8505 || abs(cur_beval->y - pt.y) > 3))
8506 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008507 // Pointer resting in one place long enough, it's time to show
8508 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 cur_beval->showState = ShS_PENDING;
8510 cur_beval->x = pt.x;
8511 cur_beval->y = pt.y;
8512
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008513 // TRACE0("BevalTimerProc: sending request");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
8515 if (cur_beval->msgCB != NULL)
8516 (*cur_beval->msgCB)(cur_beval, 0);
8517 }
8518}
8519
8520 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008521gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008523 // TRACE0("gui_mch_disable_beval_area {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 KillTimer(s_textArea, BevalTimerId);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008525 // TRACE0("gui_mch_disable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527
8528 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008529gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008531 // TRACE0("gui_mch_enable_beval_area |||");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 if (beval == NULL)
8533 return;
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008534 // TRACE0("gui_mch_enable_beval_area {{{");
Bram Moolenaar167632f2010-05-26 21:42:54 +02008535 BevalTimerId = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2), BevalTimerProc);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008536 // TRACE0("gui_mch_enable_beval_area }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537}
8538
8539 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008540gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
8542 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008543
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008544 vim_free(beval->msg);
8545 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8546 if (beval->msg == NULL)
8547 {
8548 delete_tooltip(beval);
8549 beval->showState = ShS_NEUTRAL;
8550 return;
8551 }
8552
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008553 // TRACE0("gui_mch_post_balloon {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (beval->showState == ShS_SHOWING)
8555 return;
8556 GetCursorPos(&pt);
8557 ScreenToClient(s_textArea, &pt);
8558
8559 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008561 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 gui_mch_disable_beval_area(cur_beval);
8563 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008564 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008566 // TRACE0("gui_mch_post_balloon }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567}
8568
8569 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008570gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008571 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008572 char_u *mesg,
8573 void (*mesgCB)(BalloonEval *, int),
8574 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008576 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 BalloonEval *beval;
8578
8579 if (mesg != NULL && mesgCB != NULL)
8580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008581 iemsg(_("E232: Cannot create BalloonEval with both message and callback"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 return NULL;
8583 }
8584
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008585 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 if (beval != NULL)
8587 {
8588 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
8590 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 beval->msg = mesg;
8592 beval->msgCB = mesgCB;
8593 beval->clientData = clientData;
8594
8595 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 cur_beval = beval;
8597
8598 if (p_beval)
8599 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 }
8601 return beval;
8602}
8603
8604 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008605Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008607 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 return;
8609
8610 if (cur_beval != NULL)
8611 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008612 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008614 case TTN_SHOW:
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008615 // TRACE0("TTN_SHOW {{{");
8616 // TRACE0("TTN_SHOW }}}");
Bram Moolenaar45360022005-07-21 21:08:21 +00008617 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008618 case TTN_POP: // Before tooltip disappear
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008619 // TRACE0("TTN_POP {{{");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 delete_tooltip(cur_beval);
8621 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00008622 // TRACE0("TTN_POP }}}");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008625 break;
8626 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008627 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008628 // if you get there then we have new common controls
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008629 NMTTDISPINFO_NEW *info = (NMTTDISPINFO_NEW *)pnmh;
8630 info->lpszText = (LPSTR)info->lParam;
8631 info->uFlags |= TTF_DI_SETITEM;
8632 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008633 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008634 case TTN_GETDISPINFOW:
8635 {
8636 // if we get here then we have new common controls
8637 NMTTDISPINFOW_NEW *info = (NMTTDISPINFOW_NEW *)pnmh;
8638 info->lpszText = (LPWSTR)info->lParam;
8639 info->uFlags |= TTF_DI_SETITEM;
8640 }
8641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643 }
8644}
8645
8646 static void
8647TrackUserActivity(UINT uMsg)
8648{
8649 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8650 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
8651 LastActivity = GetTickCount();
8652}
8653
8654 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008655gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008657# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008658 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008659# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008660 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 vim_free(beval);
8662}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008663#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8666/*
8667 * We have multiple signs to draw at the same location. Draw the
8668 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8669 */
8670 void
8671netbeans_draw_multisign_indicator(int row)
8672{
8673 int i;
8674 int y;
8675 int x;
8676
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008677 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008678 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008679
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 x = 0;
8681 y = TEXT_Y(row);
8682
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008683# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008684 if (IS_ENABLE_DIRECTX())
8685 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008686# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 for (i = 0; i < gui.char_height - 3; i++)
8689 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8690
8691 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8692 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8693 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8694 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8695 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8696 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8697 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8698}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008699#endif