blob: 5e8fbe2a802ed98ddf2aeea317b908df727eb091 [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
Anton Sharonov3f026672022-07-26 21:26:18 +010032// values for "dead_key"
33#define DEAD_KEY_OFF 0 // no dead key
34#define DEAD_KEY_SET_DEFAULT 1 // dead key pressed
35#define DEAD_KEY_TRANSIENT_IN_ON_CHAR 2 // wait for next key press
36#define DEAD_KEY_SKIP_ON_CHAR 3 // skip next _OnChar()
37
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010038#if defined(FEAT_DIRECTX)
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020039static DWriteContext *s_dwc = NULL;
40static int s_directx_enabled = 0;
41static int s_directx_load_attempted = 0;
Bram Moolenaar7f88b652017-12-14 13:15:19 +010042# define IS_ENABLE_DIRECTX() (s_directx_enabled && s_dwc != NULL && enc_utf8)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +010043static int directx_enabled(void);
44static void directx_binddc(void);
Bram Moolenaarb8e0bdb2014-11-12 16:10:48 +010045#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020046
Bram Moolenaar065bbac2016-02-20 13:08:46 +010047#ifdef FEAT_MENU
48static int gui_mswin_get_menu_height(int fix_window);
K.Takatac81e9bf2022-01-16 14:15:49 +000049#else
50# define gui_mswin_get_menu_height(fix_window) 0
Bram Moolenaar065bbac2016-02-20 13:08:46 +010051#endif
52
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010057# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020058 char_u *p, *q;
59
60 int dx_enable = 0;
61 int dx_flags = 0;
62 float dx_gamma = 0.0f;
63 float dx_contrast = 0.0f;
64 float dx_level = 0.0f;
65 int dx_geom = 0;
66 int dx_renmode = 0;
67 int dx_taamode = 0;
68
Bram Moolenaar734a8672019-12-02 22:49:38 +010069 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020070 for (p = s; p != NULL && *p != NUL; )
71 {
72 char_u item[256];
73 char_u name[128];
74 char_u value[128];
75
Bram Moolenaarcde88542015-08-11 19:14:00 +020076 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020077 if (p == NULL)
78 break;
79 q = &item[0];
80 copy_option_part(&q, name, sizeof(name), ":");
81 if (q == NULL)
82 return FAIL;
83 copy_option_part(&q, value, sizeof(value), ":");
84
85 if (STRCMP(name, "type") == 0)
86 {
87 if (STRCMP(value, "directx") == 0)
88 dx_enable = 1;
89 else
90 return FAIL;
91 }
92 else if (STRCMP(name, "gamma") == 0)
93 {
94 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "contrast") == 0)
98 {
99 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "level") == 0)
103 {
104 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 }
107 else if (STRCMP(name, "geom") == 0)
108 {
109 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100110 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200111 if (dx_geom < 0 || dx_geom > 2)
112 return FAIL;
113 }
114 else if (STRCMP(name, "renmode") == 0)
115 {
116 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100117 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200118 if (dx_renmode < 0 || dx_renmode > 6)
119 return FAIL;
120 }
121 else if (STRCMP(name, "taamode") == 0)
122 {
123 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100124 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200125 if (dx_taamode < 0 || dx_taamode > 3)
126 return FAIL;
127 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100128 else if (STRCMP(name, "scrlines") == 0)
129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100131 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 else
133 return FAIL;
134 }
135
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100136 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100138
Bram Moolenaar734a8672019-12-02 22:49:38 +0100139 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200140 if (dx_enable)
141 {
142 if (!directx_enabled())
143 return FAIL;
144 DWriteContext_SetRenderingParams(s_dwc, NULL);
145 if (dx_flags)
146 {
147 DWriteRenderingParams param;
148 DWriteContext_GetRenderingParams(s_dwc, &param);
149 if (dx_flags & (1 << 0))
150 param.gamma = dx_gamma;
151 if (dx_flags & (1 << 1))
152 param.enhancedContrast = dx_contrast;
153 if (dx_flags & (1 << 2))
154 param.clearTypeLevel = dx_level;
155 if (dx_flags & (1 << 3))
156 param.pixelGeometry = dx_geom;
157 if (dx_flags & (1 << 4))
158 param.renderingMode = dx_renmode;
159 if (dx_flags & (1 << 5))
160 param.textAntialiasMode = dx_taamode;
161 DWriteContext_SetRenderingParams(s_dwc, &param);
162 }
163 }
164 s_directx_enabled = dx_enable;
165
166 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100167# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200168 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100169# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200170}
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
174 * These are new in Windows ME/XP, only defined in recent compilers.
175 */
176#ifndef HANDLE_WM_XBUTTONUP
177# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDOWN
181# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184#ifndef HANDLE_WM_XBUTTONDBLCLK
185# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
187#endif
188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100189
Bram Moolenaar734a8672019-12-02 22:49:38 +0100190#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100191#ifdef DEBUG
192# include <tchar.h>
193#endif
194
Bram Moolenaar734a8672019-12-02 22:49:38 +0100195// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100196#ifndef PROTO
197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifndef __MINGW32__
199# include <shellapi.h>
200# endif
K.Takata2da11a42022-09-10 13:03:12 +0100201# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100202# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
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
K.Takatad1c58992022-01-23 12:31:57 +0000218#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100219#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
K.Takatac81e9bf2022-01-16 14:15:49 +0000225#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100226# define WM_DPICHANGED 0x02E0
227#endif
228
229#ifndef WM_MOUSEHWHEEL
230# define WM_MOUSEHWHEEL 0x020E
231#endif
232
233#ifndef SPI_GETWHEELSCROLLCHARS
234# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000235#endif
236
LemonBoyc27747e2022-05-07 12:25:40 +0100237#ifndef SPI_SETWHEELSCROLLCHARS
238# define SPI_SETWHEELSCROLLCHARS 0x006D
239#endif
240
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100243 * Define a few things for generating prototypes. This is just to avoid
244 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100246# define APIENTRY
247# define CALLBACK
248# define CONST
249# define FAR
250# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200251# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200252# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100253# define _cdecl
254typedef int BOOL;
255typedef int BYTE;
256typedef int DWORD;
257typedef int WCHAR;
258typedef int ENUMLOGFONT;
259typedef int FINDREPLACE;
260typedef int HANDLE;
261typedef int HBITMAP;
262typedef int HBRUSH;
263typedef int HDROP;
264typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100265typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100266typedef int LPARAM;
267typedef int LPCREATESTRUCT;
268typedef int LPCSTR;
269typedef int LPCTSTR;
270typedef int LPRECT;
271typedef int LPSTR;
272typedef int LPWINDOWPOS;
273typedef int LPWORD;
274typedef int LRESULT;
275typedef int HRESULT;
276# undef MSG
277typedef int MSG;
278typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100279typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100280typedef int OSVERSIONINFO;
281typedef int PWORD;
282typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int UINT;
285typedef int WORD;
286typedef int WPARAM;
287typedef int POINT;
288typedef void *HINSTANCE;
289typedef void *HMENU;
290typedef void *HWND;
291typedef void *HDC;
292typedef void VOID;
293typedef int LPNMHDR;
294typedef int LONG;
295typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200296typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200297typedef int COLORREF;
298typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299#endif
300
K.Takata45f9cfb2022-01-21 11:11:00 +0000301static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100302static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303static void clear_rect(RECT *rcp);
304
Bram Moolenaar734a8672019-12-02 22:49:38 +0100305static WORD s_dlgfntheight; // height of the dialog font
306static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307
308#ifdef FEAT_MENU
309static HMENU s_menuBar = NULL;
310#endif
311#ifdef FEAT_TEAROFF
312static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314#endif
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316// Flag that is set while processing a message that must not be interrupted by
317// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static int s_busy_processing = FALSE;
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321
322#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000323static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200324static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static int s_findrep_is_find; // TRUE for find dialog, FALSE
327 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100328#endif
329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330HWND s_hwnd = NULL;
331static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100332static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333
334#ifdef FEAT_TOOLBAR
335static HWND s_toolbarhwnd = NULL;
336static WNDPROC s_toolbar_wndproc = NULL;
337#endif
338
339#ifdef FEAT_GUI_TABLINE
340static HWND s_tabhwnd = NULL;
341static WNDPROC s_tabline_wndproc = NULL;
342static int showing_tabline = 0;
343#endif
344
345static WPARAM s_wParam = 0;
346static LPARAM s_lParam = 0;
347
348static HWND s_textArea = NULL;
349static UINT s_uMsg = 0;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100352
353static int s_need_activate = FALSE;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355// This variable is set when waiting for an event, which is the only moment
356// scrollbar dragging can be done directly. It's not allowed while commands
357// are executed, because it may move the cursor and that may cause unexpected
358// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100359static int allow_scrollbar = FALSE;
360
K.Takatac81e9bf2022-01-16 14:15:49 +0000361#ifndef _DPI_AWARENESS_CONTEXTS_
362typedef HANDLE DPI_AWARENESS_CONTEXT;
363
364typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000365 DPI_AWARENESS_INVALID = -1,
366 DPI_AWARENESS_UNAWARE = 0,
367 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000368 DPI_AWARENESS_PER_MONITOR_AWARE = 2
369} DPI_AWARENESS;
370
K.Takatab0b2b732022-01-19 12:59:21 +0000371# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
372# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000373# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
374# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
375# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
376#endif
377
378#define DEFAULT_DPI 96
379static int s_dpi = DEFAULT_DPI;
380static BOOL s_in_dpichanged = FALSE;
381static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
382
383static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
384static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
385static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
386//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
387static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
388static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
389
390 static UINT WINAPI
391stubGetDpiForSystem(void)
392{
393 HWND hwnd = GetDesktopWindow();
394 HDC hdc = GetWindowDC(hwnd);
395 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
396 ReleaseDC(hwnd, hdc);
397 return dpi;
398}
399
400 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100401stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000402{
403 return GetSystemMetrics(nIndex);
404}
405
406 static int
407adjust_fontsize_by_dpi(int size)
408{
409 return size * s_dpi / (int)pGetDpiForSystem();
410}
411
412 static int
413adjust_by_system_dpi(int size)
414{
415 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
416}
417
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100418#if defined(FEAT_DIRECTX)
419 static int
420directx_enabled(void)
421{
422 if (s_dwc != NULL)
423 return 1;
424 else if (s_directx_load_attempted)
425 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100426 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100427 DWrite_Init();
428 s_directx_load_attempted = 1;
429 s_dwc = DWriteContext_Open();
430 directx_binddc();
431 return s_dwc != NULL ? 1 : 0;
432}
433
434 static void
435directx_binddc(void)
436{
437 if (s_textArea != NULL)
438 {
439 RECT rect;
440 GetClientRect(s_textArea, &rect);
441 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
442 }
443}
444#endif
445
Bram Moolenaar734a8672019-12-02 22:49:38 +0100446extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100447
448static struct
449{
450 UINT key_sym;
451 char_u vim_code0;
452 char_u vim_code1;
453} special_keys[] =
454{
455 {VK_UP, 'k', 'u'},
456 {VK_DOWN, 'k', 'd'},
457 {VK_LEFT, 'k', 'l'},
458 {VK_RIGHT, 'k', 'r'},
459
460 {VK_F1, 'k', '1'},
461 {VK_F2, 'k', '2'},
462 {VK_F3, 'k', '3'},
463 {VK_F4, 'k', '4'},
464 {VK_F5, 'k', '5'},
465 {VK_F6, 'k', '6'},
466 {VK_F7, 'k', '7'},
467 {VK_F8, 'k', '8'},
468 {VK_F9, 'k', '9'},
469 {VK_F10, 'k', ';'},
470
471 {VK_F11, 'F', '1'},
472 {VK_F12, 'F', '2'},
473 {VK_F13, 'F', '3'},
474 {VK_F14, 'F', '4'},
475 {VK_F15, 'F', '5'},
476 {VK_F16, 'F', '6'},
477 {VK_F17, 'F', '7'},
478 {VK_F18, 'F', '8'},
479 {VK_F19, 'F', '9'},
480 {VK_F20, 'F', 'A'},
481
482 {VK_F21, 'F', 'B'},
483#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485#endif
486 {VK_F22, 'F', 'C'},
487 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100488 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
490 {VK_HELP, '%', '1'},
491 {VK_BACK, 'k', 'b'},
492 {VK_INSERT, 'k', 'I'},
493 {VK_DELETE, 'k', 'D'},
494 {VK_HOME, 'k', 'h'},
495 {VK_END, '@', '7'},
496 {VK_PRIOR, 'k', 'P'},
497 {VK_NEXT, 'k', 'N'},
498 {VK_PRINT, '%', '9'},
499 {VK_ADD, 'K', '6'},
500 {VK_SUBTRACT, 'K', '7'},
501 {VK_DIVIDE, 'K', '8'},
502 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100503 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504 {VK_DECIMAL, 'K', 'B'},
505
506 {VK_NUMPAD0, 'K', 'C'},
507 {VK_NUMPAD1, 'K', 'D'},
508 {VK_NUMPAD2, 'K', 'E'},
509 {VK_NUMPAD3, 'K', 'F'},
510 {VK_NUMPAD4, 'K', 'G'},
511 {VK_NUMPAD5, 'K', 'H'},
512 {VK_NUMPAD6, 'K', 'I'},
513 {VK_NUMPAD7, 'K', 'J'},
514 {VK_NUMPAD8, 'K', 'K'},
515 {VK_NUMPAD9, 'K', 'L'},
516
Bram Moolenaar734a8672019-12-02 22:49:38 +0100517 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100518 {VK_SPACE, ' ', NUL},
519 {VK_TAB, TAB, NUL},
520 {VK_ESCAPE, ESC, NUL},
521 {NL, NL, NUL},
522 {CAR, CAR, NUL},
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525 {0, 0, 0}
526};
527
Bram Moolenaar734a8672019-12-02 22:49:38 +0100528// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_button_pending = -1;
530
Bram Moolenaar734a8672019-12-02 22:49:38 +0100531// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
532// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_getting_focus = FALSE;
534
535static int s_x_pending;
536static int s_y_pending;
537static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000538static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100540static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200541static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
542 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100543
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100544#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100545// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100546static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000547static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548#endif
549
550/*
551 * For control IME.
552 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554 */
K.Takata4ac893f2022-01-20 12:44:28 +0000555#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100556// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100557static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100558// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100559static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100560#endif
561
562#ifdef FEAT_MBYTE_IME
563static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
564#endif
565
566#if defined(FEAT_BROWSE)
567static char_u *convert_filter(char_u *s);
568#endif
569
570#ifdef DEBUG_PRINT_ERROR
571/*
572 * Print out the last Windows error message
573 */
574 static void
575print_windows_error(void)
576{
577 LPVOID lpMsgBuf;
578
579 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
580 NULL, GetLastError(),
581 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
582 (LPTSTR) &lpMsgBuf, 0, NULL);
583 TRACE1("Error: %s\n", lpMsgBuf);
584 LocalFree(lpMsgBuf);
585}
586#endif
587
588/*
589 * Cursor blink functions.
590 *
591 * This is a simple state machine:
592 * BLINK_NONE not blinking at all
593 * BLINK_OFF blinking, cursor is not shown
594 * BLINK_ON blinking, cursor is shown
595 */
596
597#define BLINK_NONE 0
598#define BLINK_OFF 1
599#define BLINK_ON 2
600
601static int blink_state = BLINK_NONE;
602static long_u blink_waittime = 700;
603static long_u blink_ontime = 400;
604static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000605static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100606
Bram Moolenaar703a8042016-06-04 16:24:32 +0200607 int
608gui_mch_is_blinking(void)
609{
610 return blink_state != BLINK_NONE;
611}
612
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200613 int
614gui_mch_is_blink_off(void)
615{
616 return blink_state == BLINK_OFF;
617}
618
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100619 void
620gui_mch_set_blinking(long wait, long on, long off)
621{
622 blink_waittime = wait;
623 blink_ontime = on;
624 blink_offtime = off;
625}
626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 static VOID CALLBACK
628_OnBlinkTimer(
629 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100630 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000631 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100632 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633{
634 MSG msg;
635
636 /*
637 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
638 */
639
640 KillTimer(NULL, idEvent);
641
Bram Moolenaar734a8672019-12-02 22:49:38 +0100642 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000643 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100644 ;
645
646 if (blink_state == BLINK_ON)
647 {
648 gui_undraw_cursor();
649 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000650 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 }
652 else
653 {
654 gui_update_cursor(TRUE, FALSE);
655 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000656 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100658 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100659}
660
661 static void
662gui_mswin_rm_blink_timer(void)
663{
664 MSG msg;
665
666 if (blink_timer != 0)
667 {
668 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100669 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000670 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671 ;
672 blink_timer = 0;
673 }
674}
675
676/*
677 * Stop the cursor blinking. Show the cursor if it wasn't shown.
678 */
679 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100680gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681{
682 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100683 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100685 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100686 gui_mch_flush();
687 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100688 blink_state = BLINK_NONE;
689}
690
691/*
692 * Start the cursor blinking. If it was already blinking, this restarts the
693 * waiting time and shows the cursor.
694 */
695 void
696gui_mch_start_blink(void)
697{
698 gui_mswin_rm_blink_timer();
699
Bram Moolenaar734a8672019-12-02 22:49:38 +0100700 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
702 {
K.Takataa8ec4912022-02-03 14:32:33 +0000703 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704 blink_state = BLINK_ON;
705 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100706 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100707 }
708}
709
710/*
711 * Call-back routines.
712 */
713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 static VOID CALLBACK
715_OnTimer(
716 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100717 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000718 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100719 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100720{
721 MSG msg;
722
723 /*
724 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
725 */
726 KillTimer(NULL, idEvent);
727 s_timed_out = TRUE;
728
Bram Moolenaar734a8672019-12-02 22:49:38 +0100729 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000730 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100731 ;
732 if (idEvent == s_wait_timer)
733 s_wait_timer = 0;
734}
735
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 static void
737_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100738 HWND hwnd UNUSED,
739 UINT ch UNUSED,
740 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741{
742 dead_key = 1;
743}
744
745/*
746 * Convert Unicode character "ch" to bytes in "string[slen]".
747 * When "had_alt" is TRUE the ALT key was included in "ch".
748 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200749 * Because the Windows API uses UTF-16, we have to deal with surrogate
750 * pairs; this is where we choose to deal with them: if "ch" is a high
751 * surrogate, it will be stored, and the length returned will be zero; the next
752 * char_to_string call will then include the high surrogate, decoding the pair
753 * of UTF-16 code units to a single Unicode code point, presuming it is the
754 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 */
756 static int
757char_to_string(int ch, char_u *string, int slen, int had_alt)
758{
759 int len;
760 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100761 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200762 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100763
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200764 if (surrogate_pending_ch != 0)
765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100766 // We don't guarantee ch is a low surrogate to match the high surrogate
767 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200768 wstring[0] = surrogate_pending_ch;
769 wstring[1] = ch;
770 surrogate_pending_ch = 0;
771 len = 2;
772 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100773 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // We don't have the entire code point yet, only the first UTF-16 code
776 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200777 surrogate_pending_ch = ch;
778 return 0;
779 }
780 else
781 {
782 wstring[0] = ch;
783 len = 1;
784 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785
Bram Moolenaar734a8672019-12-02 22:49:38 +0100786 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
787 // "enc_codepage" is non-zero use the standard Win32 function,
788 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200789 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100790 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
792 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100793 // If we had included the ALT key into the character but now the
794 // upper bit is no longer set, that probably means the conversion
795 // failed. Convert the original character and set the upper bit
796 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200797 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 wstring[0] = ch & 0x7f;
800 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
801 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100802 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100804 }
805 }
806 else
807 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 ws = utf16_to_enc(wstring, &len);
809 if (ws == NULL)
810 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 else
812 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100813 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200814 len = slen;
815 mch_memmove(string, ws, len);
816 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 }
818 }
819
820 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100821 {
822 string[0] = ch;
823 len = 1;
824 }
825
826 for (i = 0; i < len; ++i)
827 if (string[i] == CSI && len <= slen - 2)
828 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100829 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100830 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
831 string[++i] = KS_EXTRA;
832 string[++i] = (int)KE_CSI;
833 len += 2;
834 }
835
836 return len;
837}
838
LemonBoy45684c62022-04-24 15:46:42 +0100839 static int
840get_active_modifiers(void)
841{
842 int modifiers = 0;
843
844 if (GetKeyState(VK_CONTROL) & 0x8000)
845 modifiers |= MOD_MASK_CTRL;
846 if (GetKeyState(VK_SHIFT) & 0x8000)
847 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100848 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
849 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100850 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100851 if (GetKeyState(VK_LMENU) & 0x8000)
852 modifiers |= MOD_MASK_ALT;
853 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
854 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100855
856 return modifiers;
857}
858
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100859/*
860 * Key hit, add it to the input buffer.
861 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100862 static void
863_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100864 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100865 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100866 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100867{
868 char_u string[40];
869 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100870 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100871 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872
Anton Sharonov3f026672022-07-26 21:26:18 +0100873 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
874 return;
875
876 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
877 // process_message()
878 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
879 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880
LemonBoy45684c62022-04-24 15:46:42 +0100881 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100882
883 ch = simplify_key(ch, &modifiers);
884 // remove the SHIFT modifier for keys where it's already included, e.g.,
885 // '(' and '*'
886 modifiers = may_remove_shift_modifier(modifiers, ch);
887
888 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
889 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
890 if (ch == CSI)
891 ch = K_CSI;
892
893 if (modifiers)
894 {
895 string[0] = CSI;
896 string[1] = KS_MODIFIER;
897 string[2] = modifiers;
898 add_to_input_buf(string, 3);
899 }
900
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 len = char_to_string(ch, string, 40, FALSE);
902 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
903 {
904 trash_input_buf();
905 got_int = TRUE;
906 }
907
908 add_to_input_buf(string, len);
909}
910
911/*
912 * Alt-Key hit, add it to the input buffer.
913 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914 static void
915_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100916 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100917 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100918 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100920 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921 int len;
922 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100923 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924
Anton Sharonov3f026672022-07-26 21:26:18 +0100925 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100926
Bram Moolenaar734a8672019-12-02 22:49:38 +0100927 // OK, we have a character key (given by ch) which was entered with the
928 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
929 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
930 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100931 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100933 // remove the SHIFT modifier for keys where it's already included, e.g.,
934 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200935 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100936
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200937 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
938 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939 if (ch == CSI)
940 ch = K_CSI;
941
942 len = 0;
943 if (modifiers)
944 {
945 string[len++] = CSI;
946 string[len++] = KS_MODIFIER;
947 string[len++] = modifiers;
948 }
949
950 if (IS_SPECIAL((int)ch))
951 {
952 string[len++] = CSI;
953 string[len++] = K_SECOND((int)ch);
954 string[len++] = K_THIRD((int)ch);
955 }
956 else
957 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // Although the documentation isn't clear about it, we assume "ch" is
959 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100960 len += char_to_string(ch, string + len, 40 - len, TRUE);
961 }
962
963 add_to_input_buf(string, len);
964}
965
966 static void
967_OnMouseEvent(
968 int button,
969 int x,
970 int y,
971 int repeated_click,
972 UINT keyFlags)
973{
974 int vim_modifiers = 0x0;
975
976 s_getting_focus = FALSE;
977
978 if (keyFlags & MK_SHIFT)
979 vim_modifiers |= MOUSE_SHIFT;
980 if (keyFlags & MK_CONTROL)
981 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100982 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100983 vim_modifiers |= MOUSE_ALT;
984
985 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
986}
987
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100988 static void
989_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100990 HWND hwnd UNUSED,
991 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100992 int x,
993 int y,
994 UINT keyFlags)
995{
996 static LONG s_prevTime = 0;
997
998 LONG currentTime = GetMessageTime();
999 int button = -1;
1000 int repeated_click;
1001
Bram Moolenaar734a8672019-12-02 22:49:38 +01001002 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001003 (void)SetFocus(s_hwnd);
1004
1005 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1006 button = MOUSE_LEFT;
1007 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1008 button = MOUSE_MIDDLE;
1009 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1010 button = MOUSE_RIGHT;
1011 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1012 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001013 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1014 }
1015 else if (s_uMsg == WM_CAPTURECHANGED)
1016 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001017 // on W95/NT4, somehow you get in here with an odd Msg
1018 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001019 if (s_button_pending == MOUSE_LEFT)
1020 button = MOUSE_RIGHT;
1021 else
1022 button = MOUSE_LEFT;
1023 }
1024 if (button >= 0)
1025 {
1026 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1027
1028 /*
1029 * Holding down the left and right buttons simulates pushing the middle
1030 * button.
1031 */
1032 if (repeated_click
1033 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1034 || (button == MOUSE_RIGHT
1035 && s_button_pending == MOUSE_LEFT)))
1036 {
1037 /*
1038 * Hmm, gui.c will ignore more than one button down at a time, so
1039 * pretend we let go of it first.
1040 */
1041 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1042 button = MOUSE_MIDDLE;
1043 repeated_click = FALSE;
1044 s_button_pending = -1;
1045 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1046 }
1047 else if ((repeated_click)
1048 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1049 {
1050 if (s_button_pending > -1)
1051 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001052 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1053 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001054 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001055 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1057 }
1058 else
1059 {
1060 /*
1061 * If this is the first press (i.e. not a multiple click) don't
1062 * action immediately, but store and wait for:
1063 * i) button-up
1064 * ii) mouse move
1065 * iii) another button press
1066 * before using it.
1067 * This enables us to make left+right simulate middle button,
1068 * without left or right being actioned first. The side-effect is
1069 * that if you click and hold the mouse without dragging, the
1070 * cursor doesn't move until you release the button. In practice
1071 * this is hardly a problem.
1072 */
1073 s_button_pending = button;
1074 s_x_pending = x;
1075 s_y_pending = y;
1076 s_kFlags_pending = keyFlags;
1077 }
1078
1079 s_prevTime = currentTime;
1080 }
1081}
1082
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001083 static void
1084_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001085 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 int x,
1087 int y,
1088 UINT keyFlags)
1089{
1090 int button;
1091
1092 s_getting_focus = FALSE;
1093 if (s_button_pending > -1)
1094 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001095 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 _OnMouseEvent(s_button_pending, s_x_pending,
1097 s_y_pending, FALSE, s_kFlags_pending);
1098 s_button_pending = -1;
1099 }
1100 if (s_uMsg == WM_MOUSEMOVE)
1101 {
1102 /*
1103 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1104 * down.
1105 */
1106 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1107 | MK_XBUTTON1 | MK_XBUTTON2)))
1108 {
1109 gui_mouse_moved(x, y);
1110 return;
1111 }
1112
1113 /*
1114 * While button is down, keep grabbing mouse move events when
1115 * the mouse goes outside the window
1116 */
1117 SetCapture(s_textArea);
1118 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120 }
1121 else
1122 {
1123 ReleaseCapture();
1124 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001125 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001126 }
1127
1128 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1129}
1130
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001131 static void
1132_OnSizeTextArea(
1133 HWND hwnd UNUSED,
1134 UINT state UNUSED,
1135 int cx UNUSED,
1136 int cy UNUSED)
1137{
1138#if defined(FEAT_DIRECTX)
1139 if (IS_ENABLE_DIRECTX())
1140 directx_binddc();
1141#endif
1142}
1143
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001144#ifdef FEAT_MENU
1145/*
1146 * Find the vimmenu_T with the given id
1147 */
1148 static vimmenu_T *
1149gui_mswin_find_menu(
1150 vimmenu_T *pMenu,
1151 int id)
1152{
1153 vimmenu_T *pChildMenu;
1154
1155 while (pMenu)
1156 {
1157 if (pMenu->id == (UINT)id)
1158 break;
1159 if (pMenu->children != NULL)
1160 {
1161 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1162 if (pChildMenu)
1163 {
1164 pMenu = pChildMenu;
1165 break;
1166 }
1167 }
1168 pMenu = pMenu->next;
1169 }
1170 return pMenu;
1171}
1172
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 static void
1174_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001175 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001176 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001177 HWND hwndCtl UNUSED,
1178 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001179{
1180 vimmenu_T *pMenu;
1181
1182 pMenu = gui_mswin_find_menu(root_menu, id);
1183 if (pMenu)
1184 gui_menu_cb(pMenu);
1185}
1186#endif
1187
1188#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 * Handle a Find/Replace window message.
1191 */
1192 static void
1193_OnFindRepl(void)
1194{
1195 int flags = 0;
1196 int down;
1197
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001199 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 (void)SetFocus(s_hwnd);
1201
1202 if (s_findrep_struct.Flags & FR_FINDNEXT)
1203 {
1204 flags = FRD_FINDNEXT;
1205
Bram Moolenaar734a8672019-12-02 22:49:38 +01001206 // Give main window the focus back: this is so the cursor isn't
1207 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001208 (void)SetFocus(s_hwnd);
1209 }
1210 else if (s_findrep_struct.Flags & FR_REPLACE)
1211 {
1212 flags = FRD_REPLACE;
1213
Bram Moolenaar734a8672019-12-02 22:49:38 +01001214 // Give main window the focus back: this is so the cursor isn't
1215 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001216 (void)SetFocus(s_hwnd);
1217 }
1218 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1219 {
1220 flags = FRD_REPLACEALL;
1221 }
1222
1223 if (flags != 0)
1224 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001225 char_u *p, *q;
1226
Bram Moolenaar734a8672019-12-02 22:49:38 +01001227 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001228 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1229 flags |= FRD_WHOLE_WORD;
1230 if (s_findrep_struct.Flags & FR_MATCHCASE)
1231 flags |= FRD_MATCH_CASE;
1232 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001233 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1234 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1235 if (p != NULL && q != NULL)
1236 gui_do_findrepl(flags, p, q, down);
1237 vim_free(p);
1238 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001239 }
1240}
1241#endif
1242
1243 static void
1244HandleMouseHide(UINT uMsg, LPARAM lParam)
1245{
1246 static LPARAM last_lParam = 0L;
1247
Bram Moolenaar734a8672019-12-02 22:49:38 +01001248 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1250 {
1251 if (lParam == last_lParam)
1252 return;
1253 last_lParam = lParam;
1254 }
1255
Bram Moolenaar734a8672019-12-02 22:49:38 +01001256 // Handle specially, to centralise coding. We need to be sure we catch all
1257 // possible events which should cause us to restore the cursor (as it is a
1258 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259 switch (uMsg)
1260 {
1261 case WM_KEYUP:
1262 case WM_CHAR:
1263 /*
1264 * blank out the pointer if necessary
1265 */
1266 if (p_mh)
1267 gui_mch_mousehide(TRUE);
1268 break;
1269
Bram Moolenaar734a8672019-12-02 22:49:38 +01001270 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001271 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001272 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 case WM_LBUTTONDOWN:
1274 case WM_LBUTTONUP:
1275 case WM_MBUTTONDOWN:
1276 case WM_MBUTTONUP:
1277 case WM_RBUTTONDOWN:
1278 case WM_RBUTTONUP:
1279 case WM_XBUTTONDOWN:
1280 case WM_XBUTTONUP:
1281 case WM_NCMOUSEMOVE:
1282 case WM_NCLBUTTONDOWN:
1283 case WM_NCLBUTTONUP:
1284 case WM_NCMBUTTONDOWN:
1285 case WM_NCMBUTTONUP:
1286 case WM_NCRBUTTONDOWN:
1287 case WM_NCRBUTTONUP:
1288 case WM_KILLFOCUS:
1289 /*
1290 * if the pointer is currently hidden, then we should show it.
1291 */
1292 gui_mch_mousehide(FALSE);
1293 break;
1294 }
1295}
1296
1297 static LRESULT CALLBACK
1298_TextAreaWndProc(
1299 HWND hwnd,
1300 UINT uMsg,
1301 WPARAM wParam,
1302 LPARAM lParam)
1303{
1304 /*
1305 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1306 hwnd, uMsg, wParam, lParam);
1307 */
1308
1309 HandleMouseHide(uMsg, lParam);
1310
1311 s_uMsg = uMsg;
1312 s_wParam = wParam;
1313 s_lParam = lParam;
1314
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001315#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001316 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001317#endif
1318
1319 switch (uMsg)
1320 {
1321 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1322 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1323 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1324 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1325 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1326 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1327 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1328 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1329 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1330 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1331 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1332 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1333 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1334 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001335 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001336
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001337#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001338 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1339 return TRUE;
1340#endif
1341 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001342 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001343 }
1344}
1345
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001346/*
1347 * Called when the foreground or background color has been changed.
1348 */
1349 void
1350gui_mch_new_colors(void)
1351{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001352 HBRUSH prevBrush;
1353
1354 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001355 prevBrush = (HBRUSH)SetClassLongPtr(
1356 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001357 InvalidateRect(s_hwnd, NULL, TRUE);
1358 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001359}
1360
1361/*
1362 * Set the colors to their default values.
1363 */
1364 void
1365gui_mch_def_colors(void)
1366{
1367 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1368 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1369 gui.def_norm_pixel = gui.norm_pixel;
1370 gui.def_back_pixel = gui.back_pixel;
1371}
1372
1373/*
1374 * Open the GUI window which was created by a call to gui_mch_init().
1375 */
1376 int
1377gui_mch_open(void)
1378{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001379 // Actually open the window, if not already visible
1380 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001381 if (!IsWindowVisible(s_hwnd))
1382 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1383
1384#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001385 // Init replace string here, so that we keep it when re-opening the
1386 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001387 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1388#endif
1389
1390 return OK;
1391}
1392
1393/*
1394 * Get the position of the top left corner of the window.
1395 */
1396 int
1397gui_mch_get_winpos(int *x, int *y)
1398{
1399 RECT rect;
1400
1401 GetWindowRect(s_hwnd, &rect);
1402 *x = rect.left;
1403 *y = rect.top;
1404 return OK;
1405}
1406
1407/*
1408 * Set the position of the top left corner of the window to the given
1409 * coordinates.
1410 */
1411 void
1412gui_mch_set_winpos(int x, int y)
1413{
1414 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1415 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1416}
1417 void
1418gui_mch_set_text_area_pos(int x, int y, int w, int h)
1419{
1420 static int oldx = 0;
1421 static int oldy = 0;
1422
1423 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1424
1425#ifdef FEAT_TOOLBAR
1426 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1427 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001428 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001429#endif
1430#if defined(FEAT_GUI_TABLINE)
1431 if (showing_tabline)
1432 {
1433 int top = 0;
1434 RECT rect;
1435
1436# ifdef FEAT_TOOLBAR
1437 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001438 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001439# endif
1440 GetClientRect(s_hwnd, &rect);
1441 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1442 }
1443#endif
1444
Bram Moolenaar734a8672019-12-02 22:49:38 +01001445 // When side scroll bar is unshown, the size of window will change.
1446 // then, the text area move left or right. thus client rect should be
1447 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448 if (oldx != x || oldy != y)
1449 {
1450 InvalidateRect(s_hwnd, NULL, FALSE);
1451 oldx = x;
1452 oldy = y;
1453 }
1454}
1455
1456
1457/*
1458 * Scrollbar stuff:
1459 */
1460
1461 void
1462gui_mch_enable_scrollbar(
1463 scrollbar_T *sb,
1464 int flag)
1465{
1466 ShowScrollBar(sb->id, SB_CTL, flag);
1467
Bram Moolenaar734a8672019-12-02 22:49:38 +01001468 // TODO: When the window is maximized, the size of the window stays the
1469 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1470 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001471}
1472
1473 void
1474gui_mch_set_scrollbar_pos(
1475 scrollbar_T *sb,
1476 int x,
1477 int y,
1478 int w,
1479 int h)
1480{
1481 SetWindowPos(sb->id, NULL, x, y, w, h,
1482 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1483}
1484
Bram Moolenaar203ec772020-07-17 20:43:43 +02001485 int
1486gui_mch_get_scrollbar_xpadding(void)
1487{
1488 RECT rcTxt, rcWnd;
1489 int xpad;
1490
1491 GetWindowRect(s_textArea, &rcTxt);
1492 GetWindowRect(s_hwnd, &rcWnd);
1493 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001494 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1495 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001496 return (xpad < 0) ? 0 : xpad;
1497}
1498
1499 int
1500gui_mch_get_scrollbar_ypadding(void)
1501{
1502 RECT rcTxt, rcWnd;
1503 int ypad;
1504
1505 GetWindowRect(s_textArea, &rcTxt);
1506 GetWindowRect(s_hwnd, &rcWnd);
1507 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1509 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001510 return (ypad < 0) ? 0 : ypad;
1511}
1512
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513 void
1514gui_mch_create_scrollbar(
1515 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001516 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001517{
1518 sb->id = CreateWindow(
1519 "SCROLLBAR", "Scrollbar",
1520 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001521 10, // Any value will do for now
1522 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001523 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001524 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525}
1526
1527/*
1528 * Find the scrollbar with the given hwnd.
1529 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001530 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001531gui_mswin_find_scrollbar(HWND hwnd)
1532{
1533 win_T *wp;
1534
1535 if (gui.bottom_sbar.id == hwnd)
1536 return &gui.bottom_sbar;
1537 FOR_ALL_WINDOWS(wp)
1538 {
1539 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1540 return &wp->w_scrollbars[SBAR_LEFT];
1541 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1542 return &wp->w_scrollbars[SBAR_RIGHT];
1543 }
1544 return NULL;
1545}
1546
K.Takatac81e9bf2022-01-16 14:15:49 +00001547 static void
1548update_scrollbar_size(void)
1549{
1550 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1551 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1552}
1553
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554/*
K.Takataabe628e2022-01-23 16:25:17 +00001555 * Get the average character size of a font.
1556 */
1557 static void
1558GetAverageFontSize(HDC hdc, SIZE *size)
1559{
1560 // GetTextMetrics() may not return the right value in tmAveCharWidth
1561 // for some fonts. Do our own average computation.
1562 GetTextExtentPoint(hdc,
1563 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1564 52, size);
1565 size->cx = (size->cx / 26 + 1) / 2;
1566}
1567
1568/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001569 * Get the character size of a font.
1570 */
1571 static void
1572GetFontSize(GuiFont font)
1573{
1574 HWND hwnd = GetDesktopWindow();
1575 HDC hdc = GetWindowDC(hwnd);
1576 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001577 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001578 TEXTMETRIC tm;
1579
1580 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001581 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582
K.Takataabe628e2022-01-23 16:25:17 +00001583 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584 gui.char_height = tm.tmHeight + p_linespace;
1585
1586 SelectFont(hdc, hfntOld);
1587
1588 ReleaseDC(hwnd, hdc);
1589}
1590
1591/*
1592 * Adjust gui.char_height (after 'linespace' was changed).
1593 */
1594 int
1595gui_mch_adjust_charheight(void)
1596{
1597 GetFontSize(gui.norm_font);
1598 return OK;
1599}
1600
1601 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001602get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603{
1604 HFONT font = NULL;
1605
Bram Moolenaar734a8672019-12-02 22:49:38 +01001606 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001607 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001608
1609 if (font == NULL)
1610 return NOFONT;
1611
1612 return (GuiFont)font;
1613}
1614
1615 static int
1616pixels_to_points(int pixels, int vertical)
1617{
1618 int points;
1619 HWND hwnd;
1620 HDC hdc;
1621
1622 hwnd = GetDesktopWindow();
1623 hdc = GetWindowDC(hwnd);
1624
1625 points = MulDiv(pixels, 72,
1626 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1627
1628 ReleaseDC(hwnd, hdc);
1629
1630 return points;
1631}
1632
1633 GuiFont
1634gui_mch_get_font(
1635 char_u *name,
1636 int giveErrorIfMissing)
1637{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001638 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001639 GuiFont font = NOFONT;
1640
1641 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001642 {
1643 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001645 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001647 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648 return font;
1649}
1650
1651#if defined(FEAT_EVAL) || defined(PROTO)
1652/*
1653 * Return the name of font "font" in allocated memory.
1654 * Don't know how to get the actual name, thus use the provided name.
1655 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001657gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658{
1659 if (name == NULL)
1660 return NULL;
1661 return vim_strsave(name);
1662}
1663#endif
1664
1665 void
1666gui_mch_free_font(GuiFont font)
1667{
1668 if (font)
1669 DeleteObject((HFONT)font);
1670}
1671
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001672/*
1673 * Return the Pixel value (color) for the given color name.
1674 * Return INVALCOLOR for error.
1675 */
1676 guicolor_T
1677gui_mch_get_color(char_u *name)
1678{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001679 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680
1681 typedef struct SysColorTable
1682 {
1683 char *name;
1684 int color;
1685 } SysColorTable;
1686
1687 static SysColorTable sys_table[] =
1688 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001689 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1690 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001691#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1693#endif
1694 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1695 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1696 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1697 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1698 {"SYS_DESKTOP", COLOR_DESKTOP},
1699 {"SYS_INFOBK", COLOR_INFOBK},
1700 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1701 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001702 {"SYS_BTNFACE", COLOR_BTNFACE},
1703 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1704 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1705 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1706 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1707 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1708 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1709 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1710 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1711 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1712 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1713 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1714 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1715 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1716 {"SYS_MENU", COLOR_MENU},
1717 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1718 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1719 {"SYS_WINDOW", COLOR_WINDOW},
1720 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1721 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1722 };
1723
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001724 /*
1725 * Try to look up a system colour.
1726 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001727 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 if (STRICMP(name, sys_table[i].name) == 0)
1729 return GetSysColor(sys_table[i].color);
1730
Bram Moolenaarab302212016-04-26 20:59:29 +02001731 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001732}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001733
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001734 guicolor_T
1735gui_mch_get_rgb_color(int r, int g, int b)
1736{
1737 return gui_get_rgb_color_cmn(r, g, b);
1738}
1739
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001740/*
1741 * Return OK if the key with the termcap name "name" is supported.
1742 */
1743 int
1744gui_mch_haskey(char_u *name)
1745{
1746 int i;
1747
1748 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001749 if (name[0] == special_keys[i].vim_code0
1750 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 return OK;
1752 return FAIL;
1753}
1754
1755 void
1756gui_mch_beep(void)
1757{
LemonBoy77771d32022-04-13 11:47:25 +01001758 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759}
1760/*
1761 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1762 */
1763 void
1764gui_mch_invert_rectangle(
1765 int r,
1766 int c,
1767 int nr,
1768 int nc)
1769{
1770 RECT rc;
1771
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001772#if defined(FEAT_DIRECTX)
1773 if (IS_ENABLE_DIRECTX())
1774 DWriteContext_Flush(s_dwc);
1775#endif
1776
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 /*
1778 * Note: InvertRect() excludes right and bottom of rectangle.
1779 */
1780 rc.left = FILL_X(c);
1781 rc.top = FILL_Y(r);
1782 rc.right = rc.left + nc * gui.char_width;
1783 rc.bottom = rc.top + nr * gui.char_height;
1784 InvertRect(s_hdc, &rc);
1785}
1786
1787/*
1788 * Iconify the GUI window.
1789 */
1790 void
1791gui_mch_iconify(void)
1792{
1793 ShowWindow(s_hwnd, SW_MINIMIZE);
1794}
1795
1796/*
1797 * Draw a cursor without focus.
1798 */
1799 void
1800gui_mch_draw_hollow_cursor(guicolor_T color)
1801{
1802 HBRUSH hbr;
1803 RECT rc;
1804
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001805#if defined(FEAT_DIRECTX)
1806 if (IS_ENABLE_DIRECTX())
1807 DWriteContext_Flush(s_dwc);
1808#endif
1809
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 /*
1811 * Note: FrameRect() excludes right and bottom of rectangle.
1812 */
1813 rc.left = FILL_X(gui.col);
1814 rc.top = FILL_Y(gui.row);
1815 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816 if (mb_lefthalve(gui.row, gui.col))
1817 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 rc.bottom = rc.top + gui.char_height;
1819 hbr = CreateSolidBrush(color);
1820 FrameRect(s_hdc, &rc, hbr);
1821 DeleteBrush(hbr);
1822}
1823/*
1824 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1825 * color "color".
1826 */
1827 void
1828gui_mch_draw_part_cursor(
1829 int w,
1830 int h,
1831 guicolor_T color)
1832{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001833 RECT rc;
1834
1835 /*
1836 * Note: FillRect() excludes right and bottom of rectangle.
1837 */
1838 rc.left =
1839#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001840 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001841 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1842#endif
1843 FILL_X(gui.col);
1844 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1845 rc.right = rc.left + w;
1846 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001847
Bram Moolenaar92467d32017-12-05 13:22:16 +01001848 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001849}
1850
1851
1852/*
1853 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1854 * dead key's nominal character and re-post the original message.
1855 */
1856 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001857outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858{
1859 static MSG deadCharExpel;
1860
Anton Sharonov3f026672022-07-26 21:26:18 +01001861 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 return;
1863
Anton Sharonov3f026672022-07-26 21:26:18 +01001864 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001865
Bram Moolenaar734a8672019-12-02 22:49:38 +01001866 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001867 deadCharExpel.message = originalMsg.message;
1868 deadCharExpel.hwnd = originalMsg.hwnd;
1869 deadCharExpel.wParam = VK_SPACE;
1870
K.Takata4ac893f2022-01-20 12:44:28 +00001871 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872
Bram Moolenaar734a8672019-12-02 22:49:38 +01001873 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1875 originalMsg.lParam);
1876}
1877
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001878/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001879 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1880 */
1881 static void
1882outputDeadKey_rePost(MSG originalMsg)
1883{
1884 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1885}
1886
1887/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 * Process a single Windows message.
1889 * If one is not available we hang until one is.
1890 */
1891 static void
1892process_message(void)
1893{
1894 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 char_u string[40];
1897 int i;
1898 int modifiers = 0;
1899 int key;
1900#ifdef FEAT_MENU
1901 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1902#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001903 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001904
K.Takatab7057bd2022-01-21 11:37:07 +00001905 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906
1907#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001908 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 if (msg.message == WM_OLE)
1910 {
1911 char_u *str = (char_u *)msg.lParam;
1912 if (str == NULL || *str == NUL)
1913 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001914 // Message can't be ours, forward it. Fixes problem with Ultramon
1915 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001916 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001917 }
1918 else
1919 {
1920 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001921 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 }
1923 return;
1924 }
1925#endif
1926
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001927#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001929 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 {
1931 HandleMouseHide(msg.message, msg.lParam);
1932 return;
1933 }
1934#endif
1935
1936 /*
1937 * Check if it's a special key that we recognise. If not, call
1938 * TranslateMessage().
1939 */
1940 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1941 {
1942 vk = (int) msg.wParam;
1943
1944 /*
1945 * Handle dead keys in special conditions in other cases we let Windows
1946 * handle them and do not interfere.
1947 *
1948 * The dead_key flag must be reset on several occasions:
1949 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1950 * consumed at that point (This is when we let Windows combine the
1951 * dead character on its own)
1952 *
1953 * - Before doing something special such as regenerating keypresses to
1954 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001955 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001956 * immediately to _OnChar() (or _OnSysChar()).
1957 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001958
1959 /*
1960 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1961 * was handled by _WndProc, this keypress we want to process normally
1962 */
1963 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1964 dead_key = DEAD_KEY_OFF;
1965
1966 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001967 {
1968 /*
Anton Sharonov3f026672022-07-26 21:26:18 +01001969 * Expell the dead key pressed with Ctrl in a special way.
1970 *
1971 * After dead key was pressed with Ctrl in some cases, ESC was
1972 * artificially injected and handled by _OnChar(), now we are
1973 * dealing with completely new key press from the user. If we don't
1974 * do anything, ToUnicode() call will interpret this vk+scan_code
1975 * under influence of "dead-modifier". To prevent this we translate
1976 * this message replacing current char from user with VK_SPACE,
1977 * which will cause WM_CHAR with dead_key's character itself. Using
1978 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1979 * ignore this one WM_CHAR event completely. Afterwards (due to
1980 * usage of PostMessage), this procedure is scheduled to be called
1981 * again with user char and on next entry we will clean
1982 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1983 * outputDeadKey_rePost() since we do not wish to reset dead_key
1984 * value.
1985 */
1986 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1987 {
1988 outputDeadKey_rePost_Ex(msg,
1989 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
1990 return;
1991 }
1992
1993 if (dead_key != DEAD_KEY_SET_DEFAULT)
1994 {
1995 // should never happen - is there a way to make ASSERT here?
1996 return;
1997 }
1998
1999 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002000 * If a dead key was pressed and the user presses VK_SPACE,
2001 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2002 * with the dead char now, so do nothing special and let Windows
2003 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002004 *
2005 * Note that VK_SPACE combines with the dead_key's character and
2006 * only one WM_CHAR will be generated by TranslateMessage(), in
2007 * the two other cases two WM_CHAR will be generated: the dead
2008 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2009 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 */
2011 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2012 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002013 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002014 TranslateMessage(&msg);
2015 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002016 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002017 // In modes where we are not typing, dead keys should behave
2018 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002019 else if ((get_real_state()
2020 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002021 {
2022 outputDeadKey_rePost(msg);
2023 return;
2024 }
2025 }
2026
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002028 if (vk == VK_CANCEL)
2029 {
2030 trash_input_buf();
2031 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002032 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002033 string[0] = Ctrl_C;
2034 add_to_input_buf(string, 1);
2035 }
2036
LemonBoy77fc0b02022-04-22 22:45:52 +01002037 // This is an IME event or a synthetic keystroke, let Windows handle it.
2038 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2039 {
2040 TranslateMessage(&msg);
2041 return;
2042 }
2043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002044 for (i = 0; special_keys[i].key_sym != 0; i++)
2045 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002046 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002047 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002048 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049 {
2050 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002051 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002052 * is a key that would normally trigger the dead key nominal
2053 * character output (such as a NUMPAD printable character or
2054 * the TAB key, etc...).
2055 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002056 if (dead_key == DEAD_KEY_SET_DEFAULT
2057 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002058 || vk == VK_TAB || vk == CAR))
2059 {
2060 outputDeadKey_rePost(msg);
2061 return;
2062 }
2063
2064#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // Check for <F10>: Windows selects the menu. When <F10> is
2066 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 if (vk == VK_F10
2068 && gui.menu_is_active
2069 && check_map(k10, State, FALSE, TRUE, FALSE,
2070 NULL, NULL) == NULL)
2071 break;
2072#endif
LemonBoy45684c62022-04-24 15:46:42 +01002073 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074
2075 if (special_keys[i].vim_code1 == NUL)
2076 key = special_keys[i].vim_code0;
2077 else
2078 key = TO_SPECIAL(special_keys[i].vim_code0,
2079 special_keys[i].vim_code1);
2080 key = simplify_key(key, &modifiers);
2081 if (key == CSI)
2082 key = K_CSI;
2083
2084 if (modifiers)
2085 {
2086 string[0] = CSI;
2087 string[1] = KS_MODIFIER;
2088 string[2] = modifiers;
2089 add_to_input_buf(string, 3);
2090 }
2091
2092 if (IS_SPECIAL(key))
2093 {
2094 string[0] = CSI;
2095 string[1] = K_SECOND(key);
2096 string[2] = K_THIRD(key);
2097 add_to_input_buf(string, 3);
2098 }
2099 else
2100 {
2101 int len;
2102
Bram Moolenaar734a8672019-12-02 22:49:38 +01002103 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104 len = char_to_string(key, string, 40, FALSE);
2105 add_to_input_buf(string, len);
2106 }
2107 break;
2108 }
2109 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002110
2111 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112 if (special_keys[i].key_sym == 0)
2113 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002114 WCHAR ch[8];
2115 int len;
2116 int i;
2117 UINT scan_code;
2118
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002119 // Construct the state table with only a few modifiers, we don't
2120 // really care about the presence of Ctrl/Alt as those modifiers are
2121 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002122 memset(keyboard_state, 0, 256);
2123 if (GetKeyState(VK_SHIFT) & 0x8000)
2124 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002125 if (GetKeyState(VK_CAPITAL) & 0x0001)
2126 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002127 // Alt-Gr is synthesized as Alt + Ctrl.
2128 if ((GetKeyState(VK_RMENU) & 0x8000)
2129 && (GetKeyState(VK_CONTROL) & 0x8000))
2130 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002131 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002132 keyboard_state[VK_CONTROL] = 0x80;
2133 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002134
2135 // Translate the virtual key according to the current keyboard
2136 // layout.
2137 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2138 // Convert the scan-code into a sequence of zero or more unicode
2139 // codepoints.
2140 // If this is a dead key ToUnicode returns a negative value.
2141 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2142 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002143 if (len < 0)
2144 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002145
2146 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002147 {
2148 if ( dead_key == DEAD_KEY_SET_DEFAULT
2149 && (GetKeyState(VK_CONTROL) & 0x8000)
2150 && ( (vk == 221 && scan_code == 26) // AZERTY CTRL+dead_circumflex
2151 || (vk == 220 && scan_code == 41) // QWERTZ CTRL+dead_circumflex
2152 )
2153 )
2154 {
2155 // post WM_CHAR='[' - which will be interpreted with CTRL
2156 // stil hold as ESC
2157 PostMessageW(msg.hwnd, WM_CHAR, '[', msg.lParam);
2158 // ask _OnChar() to not touch this state, wait for next key
2159 // press and maintain knowledge that we are "poisoned" with
2160 // "dead state"
2161 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2162 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002163 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002164 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002165
2166 // Post the message as TranslateMessage would do.
2167 if (msg.message == WM_KEYDOWN)
2168 {
2169 for (i = 0; i < len; i++)
2170 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 }
2172 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002173 {
2174 for (i = 0; i < len; i++)
2175 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2176 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002177 }
2178 }
2179#ifdef FEAT_MBYTE_IME
2180 else if (msg.message == WM_IME_NOTIFY)
2181 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2182 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002183 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002184 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185#endif
2186
2187#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002188 // Check for <F10>: Default effect is to select the menu. When <F10> is
2189 // mapped we need to stop it here to avoid strange effects (e.g., for the
2190 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002191 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2192 NULL, NULL) == NULL)
2193#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002194 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195}
2196
2197/*
2198 * Catch up with any queued events. This may put keyboard input into the
2199 * input buffer, call resize call-backs, trigger timers etc. If there is
2200 * nothing in the event queue (& no timers pending), then we return
2201 * immediately.
2202 */
2203 void
2204gui_mch_update(void)
2205{
2206 MSG msg;
2207
2208 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002209 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002210 && !vim_is_input_buf_full())
2211 process_message();
2212}
2213
Bram Moolenaar4231da42016-06-02 14:30:04 +02002214 static void
2215remove_any_timer(void)
2216{
2217 MSG msg;
2218
2219 if (s_wait_timer != 0 && !s_timed_out)
2220 {
2221 KillTimer(NULL, s_wait_timer);
2222
Bram Moolenaar734a8672019-12-02 22:49:38 +01002223 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002224 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002225 ;
2226 s_wait_timer = 0;
2227 }
2228}
2229
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230/*
2231 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2232 * from the keyboard.
2233 * wtime == -1 Wait forever.
2234 * wtime == 0 This should never happen.
2235 * wtime > 0 Wait wtime milliseconds for a character.
2236 * Returns OK if a character was found to be available within the given time,
2237 * or FAIL otherwise.
2238 */
2239 int
2240gui_mch_wait_for_chars(int wtime)
2241{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242 int focus;
2243
2244 s_timed_out = FALSE;
2245
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002246 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002248 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249 if (s_busy_processing)
2250 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002251
2252 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002253 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2254 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255 }
2256
2257 allow_scrollbar = TRUE;
2258
2259 focus = gui.in_focus;
2260 while (!s_timed_out)
2261 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002262 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263 if (gui.in_focus != focus)
2264 {
2265 if (gui.in_focus)
2266 gui_mch_start_blink();
2267 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002268 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269 focus = gui.in_focus;
2270 }
2271
2272 if (s_need_activate)
2273 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002274 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275 s_need_activate = FALSE;
2276 }
2277
Bram Moolenaar4231da42016-06-02 14:30:04 +02002278#ifdef FEAT_TIMERS
2279 did_add_timer = FALSE;
2280#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002282 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002283 for (;;)
2284 {
2285 MSG msg;
2286
2287 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002288# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002289 if (did_add_timer)
2290 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002291# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002292 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002293 {
2294 process_message();
2295 break;
2296 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002297 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002298 // TODO: The 10 msec is a compromise between laggy response
2299 // and consuming more CPU time. Better would be to handle
2300 // channel messages when they arrive.
2301 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002302 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002303 break;
2304 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002305#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002306 // Don't use gui_mch_update() because then we will spin-lock until a
2307 // char arrives, instead we use GetMessage() to hang until an
2308 // event arrives. No need to check for input_buf_full because we are
2309 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002311#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312
2313 if (input_available())
2314 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002315 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002316 allow_scrollbar = FALSE;
2317
Bram Moolenaar89c00032019-09-04 13:53:21 +02002318 // Clear pending mouse button, the release event may have been
2319 // taken by the dialog window. But don't do this when getting
2320 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321 if (!s_getting_focus)
2322 s_button_pending = -1;
2323
2324 return OK;
2325 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002326
2327#ifdef FEAT_TIMERS
2328 if (did_add_timer)
2329 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002330 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002331 remove_any_timer();
2332 break;
2333 }
2334#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335 }
2336 allow_scrollbar = FALSE;
2337 return FAIL;
2338}
2339
2340/*
2341 * Clear a rectangular region of the screen from text pos (row1, col1) to
2342 * (row2, col2) inclusive.
2343 */
2344 void
2345gui_mch_clear_block(
2346 int row1,
2347 int col1,
2348 int row2,
2349 int col2)
2350{
2351 RECT rc;
2352
2353 /*
2354 * Clear one extra pixel at the far right, for when bold characters have
2355 * spilled over to the window border.
2356 * Note: FillRect() excludes right and bottom of rectangle.
2357 */
2358 rc.left = FILL_X(col1);
2359 rc.top = FILL_Y(row1);
2360 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2361 rc.bottom = FILL_Y(row2 + 1);
2362 clear_rect(&rc);
2363}
2364
2365/*
2366 * Clear the whole text window.
2367 */
2368 void
2369gui_mch_clear_all(void)
2370{
2371 RECT rc;
2372
2373 rc.left = 0;
2374 rc.top = 0;
2375 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2376 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2377 clear_rect(&rc);
2378}
2379/*
2380 * Menu stuff.
2381 */
2382
2383 void
2384gui_mch_enable_menu(int flag)
2385{
2386#ifdef FEAT_MENU
2387 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2388#endif
2389}
2390
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391 void
2392gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002393 int x UNUSED,
2394 int y UNUSED,
2395 int w UNUSED,
2396 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002398 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399}
2400
2401#if defined(FEAT_MENU) || defined(PROTO)
2402/*
2403 * Make menu item hidden or not hidden
2404 */
2405 void
2406gui_mch_menu_hidden(
2407 vimmenu_T *menu,
2408 int hidden)
2409{
2410 /*
2411 * This doesn't do what we want. Hmm, just grey the menu items for now.
2412 */
2413 /*
2414 if (hidden)
2415 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2416 else
2417 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2418 */
2419 gui_mch_menu_grey(menu, hidden);
2420}
2421
2422/*
2423 * This is called after setting all the menus to grey/hidden or not.
2424 */
2425 void
2426gui_mch_draw_menubar(void)
2427{
2428 DrawMenuBar(s_hwnd);
2429}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002430#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002431
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432/*
2433 * Return the RGB value of a pixel as a long.
2434 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002435 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436gui_mch_get_rgb(guicolor_T pixel)
2437{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002438 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2439 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002440}
2441
2442#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002443/*
2444 * Convert pixels in X to dialog units
2445 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 static WORD
2447PixelToDialogX(int numPixels)
2448{
2449 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2450}
2451
Bram Moolenaar734a8672019-12-02 22:49:38 +01002452/*
2453 * Convert pixels in Y to dialog units
2454 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 static WORD
2456PixelToDialogY(int numPixels)
2457{
2458 return (WORD)((numPixels * 8) / s_dlgfntheight);
2459}
2460
Bram Moolenaar734a8672019-12-02 22:49:38 +01002461/*
2462 * Return the width in pixels of the given text in the given DC.
2463 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002464 static int
2465GetTextWidth(HDC hdc, char_u *str, int len)
2466{
2467 SIZE size;
2468
2469 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2470 return size.cx;
2471}
2472
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002473/*
2474 * Return the width in pixels of the given text in the given DC, taking care
2475 * of 'encoding' to active codepage conversion.
2476 */
2477 static int
2478GetTextWidthEnc(HDC hdc, char_u *str, int len)
2479{
2480 SIZE size;
2481 WCHAR *wstr;
2482 int n;
2483 int wlen = len;
2484
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002485 wstr = enc_to_utf16(str, &wlen);
2486 if (wstr == NULL)
2487 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002489 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2490 vim_free(wstr);
2491 if (n)
2492 return size.cx;
2493 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002494}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002496static void get_work_area(RECT *spi_rect);
2497
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002498/*
2499 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002500 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2501 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502 */
2503 static BOOL
2504CenterWindow(
2505 HWND hwndChild,
2506 HWND hwndParent)
2507{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002508 HMONITOR mon;
2509 MONITORINFO moninfo;
2510 RECT rChild, rParent, rScreen;
2511 int wChild, hChild, wParent, hParent;
2512 int xNew, yNew;
2513 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514
2515 GetWindowRect(hwndChild, &rChild);
2516 wChild = rChild.right - rChild.left;
2517 hChild = rChild.bottom - rChild.top;
2518
Bram Moolenaar734a8672019-12-02 22:49:38 +01002519 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002521 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522 else
2523 GetWindowRect(hwndParent, &rParent);
2524 wParent = rParent.right - rParent.left;
2525 hParent = rParent.bottom - rParent.top;
2526
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002527 moninfo.cbSize = sizeof(MONITORINFO);
2528 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2529 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002530 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002531 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002532 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002533 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002535 hdc = GetDC(hwndChild);
2536 rScreen.left = 0;
2537 rScreen.top = 0;
2538 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2539 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2540 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541 }
2542
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002543 xNew = rParent.left + ((wParent - wChild) / 2);
2544 if (xNew < rScreen.left)
2545 xNew = rScreen.left;
2546 else if ((xNew + wChild) > rScreen.right)
2547 xNew = rScreen.right - wChild;
2548
2549 yNew = rParent.top + ((hParent - hChild) / 2);
2550 if (yNew < rScreen.top)
2551 yNew = rScreen.top;
2552 else if ((yNew + hChild) > rScreen.bottom)
2553 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554
2555 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2556 SWP_NOSIZE | SWP_NOZORDER);
2557}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002560#if defined(FEAT_TOOLBAR) || defined(PROTO)
2561 void
2562gui_mch_show_toolbar(int showit)
2563{
2564 if (s_toolbarhwnd == NULL)
2565 return;
2566
2567 if (showit)
2568 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002569 // Enable unicode support
2570 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2571 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002572 ShowWindow(s_toolbarhwnd, SW_SHOW);
2573 }
2574 else
2575 ShowWindow(s_toolbarhwnd, SW_HIDE);
2576}
2577
Bram Moolenaar734a8672019-12-02 22:49:38 +01002578// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002579# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002580
2581#endif
2582
2583#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2584 static void
2585add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2586{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002587 WCHAR *wn;
2588 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002590 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002594 infow.cbSize = sizeof(infow);
2595 infow.fMask = MIIM_TYPE | MIIM_ID;
2596 infow.wID = item_id;
2597 infow.fType = MFT_STRING;
2598 infow.dwTypeData = wn;
2599 infow.cch = (UINT)wcslen(wn);
2600 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2601 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602}
2603
2604 static void
2605show_tabline_popup_menu(void)
2606{
2607 HMENU tab_pmenu;
2608 long rval;
2609 POINT pt;
2610
Bram Moolenaar734a8672019-12-02 22:49:38 +01002611 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002612 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 return;
2614
2615 tab_pmenu = CreatePopupMenu();
2616 if (tab_pmenu == NULL)
2617 return;
2618
2619 if (first_tabpage->tp_next != NULL)
2620 add_tabline_popup_menu_entry(tab_pmenu,
2621 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2622 add_tabline_popup_menu_entry(tab_pmenu,
2623 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2624 add_tabline_popup_menu_entry(tab_pmenu,
2625 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2626
2627 GetCursorPos(&pt);
2628 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2629 NULL);
2630
2631 DestroyMenu(tab_pmenu);
2632
Bram Moolenaar734a8672019-12-02 22:49:38 +01002633 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002634 if (rval > 0)
2635 {
2636 TCHITTESTINFO htinfo;
2637 int idx;
2638
2639 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2640 return;
2641
2642 htinfo.pt.x = pt.x;
2643 htinfo.pt.y = pt.y;
2644 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2645 if (idx == -1)
2646 idx = 0;
2647 else
2648 idx += 1;
2649
2650 send_tabline_menu_event(idx, (int)rval);
2651 }
2652}
2653
2654/*
2655 * Show or hide the tabline.
2656 */
2657 void
2658gui_mch_show_tabline(int showit)
2659{
2660 if (s_tabhwnd == NULL)
2661 return;
2662
2663 if (!showit != !showing_tabline)
2664 {
2665 if (showit)
2666 ShowWindow(s_tabhwnd, SW_SHOW);
2667 else
2668 ShowWindow(s_tabhwnd, SW_HIDE);
2669 showing_tabline = showit;
2670 }
2671}
2672
2673/*
2674 * Return TRUE when tabline is displayed.
2675 */
2676 int
2677gui_mch_showing_tabline(void)
2678{
2679 return s_tabhwnd != NULL && showing_tabline;
2680}
2681
2682/*
2683 * Update the labels of the tabline.
2684 */
2685 void
2686gui_mch_update_tabline(void)
2687{
2688 tabpage_T *tp;
2689 TCITEM tie;
2690 int nr = 0;
2691 int curtabidx = 0;
2692 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002693 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002694
2695 if (s_tabhwnd == NULL)
2696 return;
2697
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002698 // Enable unicode support
2699 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002700
2701 tie.mask = TCIF_TEXT;
2702 tie.iImage = -1;
2703
Bram Moolenaar734a8672019-12-02 22:49:38 +01002704 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002705 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2706
Bram Moolenaar734a8672019-12-02 22:49:38 +01002707 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002708 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2709 {
2710 if (tp == curtab)
2711 curtabidx = nr;
2712
2713 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2714 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002715 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716 tie.pszText = "-Empty-";
2717 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2718 tabadded = 1;
2719 }
2720
2721 get_tabline_label(tp, FALSE);
2722 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002724 wstr = enc_to_utf16(NameBuff, NULL);
2725 if (wstr != NULL)
2726 {
2727 TCITEMW tiw;
2728
2729 tiw.mask = TCIF_TEXT;
2730 tiw.iImage = -1;
2731 tiw.pszText = wstr;
2732 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2733 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002735 }
2736
Bram Moolenaar734a8672019-12-02 22:49:38 +01002737 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2739 TabCtrl_DeleteItem(s_tabhwnd, nr);
2740
2741 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2742 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2743
Bram Moolenaar734a8672019-12-02 22:49:38 +01002744 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2746 RedrawWindow(s_tabhwnd, NULL, NULL,
2747 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2748
2749 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2750 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2751}
2752
2753/*
2754 * Set the current tab to "nr". First tab is 1.
2755 */
2756 void
2757gui_mch_set_curtab(int nr)
2758{
2759 if (s_tabhwnd == NULL)
2760 return;
2761
2762 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2763 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2764}
2765
2766#endif
2767
2768/*
2769 * ":simalt" command.
2770 */
2771 void
2772ex_simalt(exarg_T *eap)
2773{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002774 char_u *keys = eap->arg;
2775 int fill_typebuf = FALSE;
2776 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777
2778 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2779 while (*keys)
2780 {
2781 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002782 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2784 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002785 fill_typebuf = TRUE;
2786 }
2787 if (fill_typebuf)
2788 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002789 // Put a NOP in the typeahead buffer so that the message will get
2790 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002791 key_name[0] = K_SPECIAL;
2792 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002793 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002794 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002795#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002796 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002797#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002798 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 }
2800}
2801
2802/*
2803 * Create the find & replace dialogs.
2804 * You can't have both at once: ":find" when replace is showing, destroys
2805 * the replace dialog first, and the other way around.
2806 */
2807#ifdef MSWIN_FIND_REPLACE
2808 static void
2809initialise_findrep(char_u *initial_string)
2810{
2811 int wword = FALSE;
2812 int mcase = !p_ic;
2813 char_u *entry_text;
2814
Bram Moolenaar734a8672019-12-02 22:49:38 +01002815 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2817
2818 s_findrep_struct.hwndOwner = s_hwnd;
2819 s_findrep_struct.Flags = FR_DOWN;
2820 if (mcase)
2821 s_findrep_struct.Flags |= FR_MATCHCASE;
2822 if (wword)
2823 s_findrep_struct.Flags |= FR_WHOLEWORD;
2824 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002825 {
2826 WCHAR *p = enc_to_utf16(entry_text, NULL);
2827 if (p != NULL)
2828 {
2829 int len = s_findrep_struct.wFindWhatLen - 1;
2830
2831 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2832 s_findrep_struct.lpstrFindWhat[len] = NUL;
2833 vim_free(p);
2834 }
2835 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002836 vim_free(entry_text);
2837}
2838#endif
2839
2840 static void
2841set_window_title(HWND hwnd, char *title)
2842{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002843 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002844 {
2845 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002846
Bram Moolenaar734a8672019-12-02 22:49:38 +01002847 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2849 if (wbuf != NULL)
2850 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002851 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002853 }
2854 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002855 else
2856 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857}
2858
2859 void
2860gui_mch_find_dialog(exarg_T *eap)
2861{
2862#ifdef MSWIN_FIND_REPLACE
2863 if (s_findrep_msg != 0)
2864 {
2865 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2866 DestroyWindow(s_findrep_hwnd);
2867
2868 if (!IsWindow(s_findrep_hwnd))
2869 {
2870 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002871 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002872 }
2873
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002874 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 (void)SetFocus(s_findrep_hwnd);
2876
2877 s_findrep_is_find = TRUE;
2878 }
2879#endif
2880}
2881
2882
2883 void
2884gui_mch_replace_dialog(exarg_T *eap)
2885{
2886#ifdef MSWIN_FIND_REPLACE
2887 if (s_findrep_msg != 0)
2888 {
2889 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2890 DestroyWindow(s_findrep_hwnd);
2891
2892 if (!IsWindow(s_findrep_hwnd))
2893 {
2894 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002895 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002896 }
2897
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002898 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002899 (void)SetFocus(s_findrep_hwnd);
2900
2901 s_findrep_is_find = FALSE;
2902 }
2903#endif
2904}
2905
2906
2907/*
2908 * Set visibility of the pointer.
2909 */
2910 void
2911gui_mch_mousehide(int hide)
2912{
2913 if (hide != gui.pointer_hidden)
2914 {
2915 ShowCursor(!hide);
2916 gui.pointer_hidden = hide;
2917 }
2918}
2919
2920#ifdef FEAT_MENU
2921 static void
2922gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2923{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002924 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002925 gui_mch_mousehide(FALSE);
2926
2927 (void)TrackPopupMenu(
2928 (HMENU)menu->submenu_id,
2929 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2930 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002931 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932 s_hwnd,
2933 NULL);
2934 /*
2935 * NOTE: The pop-up menu can eat the mouse up event.
2936 * We deal with this in normal.c.
2937 */
2938}
2939#endif
2940
2941/*
2942 * Got a message when the system will go down.
2943 */
2944 static void
2945_OnEndSession(void)
2946{
2947 getout_preserve_modified(1);
2948}
2949
2950/*
2951 * Get this message when the user clicks on the cross in the top right corner
2952 * of a Windows95 window.
2953 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002955_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002956{
2957 gui_shell_closed();
2958}
2959
2960/*
2961 * Get a message when the window is being destroyed.
2962 */
2963 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002964_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002965{
2966 if (!destroying)
2967 _OnClose(hwnd);
2968}
2969
2970 static void
2971_OnPaint(
2972 HWND hwnd)
2973{
2974 if (!IsMinimized(hwnd))
2975 {
2976 PAINTSTRUCT ps;
2977
Bram Moolenaar734a8672019-12-02 22:49:38 +01002978 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002979 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002980
Bram Moolenaar734a8672019-12-02 22:49:38 +01002981 // prevent multi-byte characters from misprinting on an invalid
2982 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002983 if (has_mbyte)
2984 {
2985 RECT rect;
2986
2987 GetClientRect(hwnd, &rect);
2988 ps.rcPaint.left = rect.left;
2989 ps.rcPaint.right = rect.right;
2990 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002991
2992 if (!IsRectEmpty(&ps.rcPaint))
2993 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002994 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2995 ps.rcPaint.right - ps.rcPaint.left + 1,
2996 ps.rcPaint.bottom - ps.rcPaint.top + 1);
2997 }
2998
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002999 EndPaint(hwnd, &ps);
3000 }
3001}
3002
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003003 static void
3004_OnSize(
3005 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003006 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003007 int cx,
3008 int cy)
3009{
K.Takatac81e9bf2022-01-16 14:15:49 +00003010 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003011 {
3012 gui_resize_shell(cx, cy);
3013
Bram Moolenaar734a8672019-12-02 22:49:38 +01003014 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003016 }
3017}
3018
3019 static void
3020_OnSetFocus(
3021 HWND hwnd,
3022 HWND hwndOldFocus)
3023{
3024 gui_focus_change(TRUE);
3025 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003026 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003027}
3028
3029 static void
3030_OnKillFocus(
3031 HWND hwnd,
3032 HWND hwndNewFocus)
3033{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003034 if (destroying)
3035 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 gui_focus_change(FALSE);
3037 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003038 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039}
3040
3041/*
3042 * Get a message when the user switches back to vim
3043 */
3044 static LRESULT
3045_OnActivateApp(
3046 HWND hwnd,
3047 BOOL fActivate,
3048 DWORD dwThreadId)
3049{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003050 // we call gui_focus_change() in _OnSetFocus()
3051 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003052 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003053}
3054
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055 void
3056gui_mch_destroy_scrollbar(scrollbar_T *sb)
3057{
3058 DestroyWindow(sb->id);
3059}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060
3061/*
3062 * Get current mouse coordinates in text window.
3063 */
3064 void
3065gui_mch_getmouse(int *x, int *y)
3066{
3067 RECT rct;
3068 POINT mp;
3069
3070 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003071 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003072 *x = (int)(mp.x - rct.left);
3073 *y = (int)(mp.y - rct.top);
3074}
3075
3076/*
3077 * Move mouse pointer to character at (x, y).
3078 */
3079 void
3080gui_mch_setmouse(int x, int y)
3081{
3082 RECT rct;
3083
3084 (void)GetWindowRect(s_textArea, &rct);
3085 (void)SetCursorPos(x + gui.border_offset + rct.left,
3086 y + gui.border_offset + rct.top);
3087}
3088
3089 static void
3090gui_mswin_get_valid_dimensions(
3091 int w,
3092 int h,
3093 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003094 int *valid_h,
3095 int *cols,
3096 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003097{
3098 int base_width, base_height;
3099
3100 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003101 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3102 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003103 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003104 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3105 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3106 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3107 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003108 *cols = (w - base_width) / gui.char_width;
3109 *rows = (h - base_height) / gui.char_height;
3110 *valid_w = base_width + *cols * gui.char_width;
3111 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003112}
3113
3114 void
3115gui_mch_flash(int msec)
3116{
3117 RECT rc;
3118
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003119#if defined(FEAT_DIRECTX)
3120 if (IS_ENABLE_DIRECTX())
3121 DWriteContext_Flush(s_dwc);
3122#endif
3123
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 /*
3125 * Note: InvertRect() excludes right and bottom of rectangle.
3126 */
3127 rc.left = 0;
3128 rc.top = 0;
3129 rc.right = gui.num_cols * gui.char_width;
3130 rc.bottom = gui.num_rows * gui.char_height;
3131 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003132 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003133
Bram Moolenaar734a8672019-12-02 22:49:38 +01003134 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003135
3136 InvertRect(s_hdc, &rc);
3137}
3138
3139/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003140 * Check if the specified point is on-screen. (multi-monitor aware)
3141 */
3142 static BOOL
3143is_point_onscreen(int x, int y)
3144{
3145 POINT pt = {x, y};
3146
3147 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3148}
3149
3150/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003151 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003152 *
3153 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3154 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3155 * only works when the whole window is on-screen.
3156 */
3157 static BOOL
3158is_window_onscreen(HWND hwnd)
3159{
3160 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003161 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003162
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003163 GetClientRect(hwnd, &rc);
3164 p1.x = rc.left;
3165 p1.y = rc.top;
3166 p2.x = rc.right - 1;
3167 p2.y = rc.bottom - 1;
3168 ClientToScreen(hwnd, &p1);
3169 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003170
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003171 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003172 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003173 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003174 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003175 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003176 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003177 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003178 return FALSE;
3179 return TRUE;
3180}
3181
3182/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183 * Return flags used for scrolling.
3184 * The SW_INVALIDATE is required when part of the window is covered or
3185 * off-screen. Refer to MS KB Q75236.
3186 */
3187 static int
3188get_scroll_flags(void)
3189{
3190 HWND hwnd;
3191 RECT rcVim, rcOther, rcDest;
3192
Bram Moolenaar185577e2020-10-29 20:08:21 +01003193 // Check if the window is (partly) off-screen.
3194 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 return SW_INVALIDATE;
3196
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003197 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003198 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3200 if (IsWindowVisible(hwnd))
3201 {
3202 GetWindowRect(hwnd, &rcOther);
3203 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3204 return SW_INVALIDATE;
3205 }
3206 return 0;
3207}
3208
3209/*
3210 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3211 * may not be scrolled out properly.
3212 * For gVim, when _OnScroll() is repeated, the character at the
3213 * previous cursor position may be left drawn after scroll.
3214 * The problem can be avoided by calling GetPixel() to get a pixel in
3215 * the region before ScrollWindowEx().
3216 */
3217 static void
3218intel_gpu_workaround(void)
3219{
3220 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3221}
3222
3223/*
3224 * Delete the given number of lines from the given row, scrolling up any
3225 * text further down within the scroll region.
3226 */
3227 void
3228gui_mch_delete_lines(
3229 int row,
3230 int num_lines)
3231{
3232 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003233
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003234 rc.left = FILL_X(gui.scroll_region_left);
3235 rc.right = FILL_X(gui.scroll_region_right + 1);
3236 rc.top = FILL_Y(row);
3237 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3238
Bram Moolenaar92467d32017-12-05 13:22:16 +01003239#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003240 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003241 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003242 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003243 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003244 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003245#endif
3246 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003247#if defined(FEAT_DIRECTX)
3248 if (IS_ENABLE_DIRECTX())
3249 DWriteContext_Flush(s_dwc);
3250#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003251 intel_gpu_workaround();
3252 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003253 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003254 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003255 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003256
Bram Moolenaar734a8672019-12-02 22:49:38 +01003257 // This seems to be required to avoid the cursor disappearing when
3258 // scrolling such that the cursor ends up in the top-left character on
3259 // the screen... But why? (Webb)
3260 // It's probably fixed by disabling drawing the cursor while scrolling.
3261 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262
3263 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3264 gui.scroll_region_left,
3265 gui.scroll_region_bot, gui.scroll_region_right);
3266}
3267
3268/*
3269 * Insert the given number of lines before the given row, scrolling down any
3270 * following text within the scroll region.
3271 */
3272 void
3273gui_mch_insert_lines(
3274 int row,
3275 int num_lines)
3276{
3277 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003278
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003279 rc.left = FILL_X(gui.scroll_region_left);
3280 rc.right = FILL_X(gui.scroll_region_right + 1);
3281 rc.top = FILL_Y(row);
3282 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003283
3284#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003285 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003286 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003287 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003288 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003289 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003290#endif
3291 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003292#if defined(FEAT_DIRECTX)
3293 if (IS_ENABLE_DIRECTX())
3294 DWriteContext_Flush(s_dwc);
3295#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003296 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003297 // The SW_INVALIDATE is required when part of the window is covered or
3298 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003299 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003300 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003301 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003302 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003303
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003304 gui_clear_block(row, gui.scroll_region_left,
3305 row + num_lines - 1, gui.scroll_region_right);
3306}
3307
3308
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003310gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003311{
3312#if defined(FEAT_DIRECTX)
3313 DWriteContext_Close(s_dwc);
3314 DWrite_Final();
3315 s_dwc = NULL;
3316#endif
3317
3318 ReleaseDC(s_textArea, s_hdc);
3319 DeleteObject(s_brush);
3320
3321#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003322 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003323 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3324#endif
3325
Bram Moolenaar734a8672019-12-02 22:49:38 +01003326 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 if (s_hwnd != NULL)
3328 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003329 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330 DestroyWindow(s_hwnd);
3331 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332}
3333
3334 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003335logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336{
3337 char *p;
3338 char *res;
3339 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003340 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003341 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003342 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003344 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3345 if (font_name == NULL)
3346 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003348 quality_name = quality_id2name((int)lf.lfQuality);
3349
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003350 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003351 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003352 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003353 if (res != NULL)
3354 {
3355 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003356 // make a normal font string out of the lf thing:
3357 points = pixels_to_points(
3358 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3359 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3360 sprintf((char *)p, "%s:h%d", font_name, points);
3361 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003362 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003363 while (*p)
3364 {
3365 if (*p == ' ')
3366 *p = '_';
3367 ++p;
3368 }
3369 if (lf.lfItalic)
3370 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003371 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003372 STRCAT(p, ":b");
3373 if (lf.lfUnderline)
3374 STRCAT(p, ":u");
3375 if (lf.lfStrikeOut)
3376 STRCAT(p, ":s");
3377 if (charset_name != NULL)
3378 {
3379 STRCAT(p, ":c");
3380 STRCAT(p, charset_name);
3381 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003382 if (quality_name != NULL)
3383 {
3384 STRCAT(p, ":q");
3385 STRCAT(p, quality_name);
3386 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003387 }
3388
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003389 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003390 return (char_u *)res;
3391}
3392
3393
3394#ifdef FEAT_MBYTE_IME
3395/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003396 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003397 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 */
3399 static void
3400update_im_font(void)
3401{
K.Takatac81e9bf2022-01-16 14:15:49 +00003402 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403
3404 if (p_guifontwide != NULL && *p_guifontwide != NUL
3405 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003406 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407 norm_logfont = lf_wide;
3408 else
3409 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003410
3411 lf = norm_logfont;
3412 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3413 // Work around when PerMonitorV2 is not enabled in the process level.
3414 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3415 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003416}
3417#endif
3418
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419/*
3420 * Handler of gui.wide_font (p_guifontwide) changed notification.
3421 */
3422 void
3423gui_mch_wide_font_changed(void)
3424{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003425 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003427#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003428 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003429#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430
3431 gui_mch_free_font(gui.wide_ital_font);
3432 gui.wide_ital_font = NOFONT;
3433 gui_mch_free_font(gui.wide_bold_font);
3434 gui.wide_bold_font = NOFONT;
3435 gui_mch_free_font(gui.wide_boldital_font);
3436 gui.wide_boldital_font = NOFONT;
3437
3438 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003439 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003440 {
3441 if (!lf.lfItalic)
3442 {
3443 lf.lfItalic = TRUE;
3444 gui.wide_ital_font = get_font_handle(&lf);
3445 lf.lfItalic = FALSE;
3446 }
3447 if (lf.lfWeight < FW_BOLD)
3448 {
3449 lf.lfWeight = FW_BOLD;
3450 gui.wide_bold_font = get_font_handle(&lf);
3451 if (!lf.lfItalic)
3452 {
3453 lf.lfItalic = TRUE;
3454 gui.wide_boldital_font = get_font_handle(&lf);
3455 }
3456 }
3457 }
3458}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003459
3460/*
3461 * Initialise vim to use the font with the given name.
3462 * Return FAIL if the font could not be loaded, OK otherwise.
3463 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003464 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003465gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466{
K.Takatac81e9bf2022-01-16 14:15:49 +00003467 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003468 GuiFont font = NOFONT;
3469 char_u *p;
3470
Bram Moolenaar734a8672019-12-02 22:49:38 +01003471 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003473 {
3474 lfOrig = lf;
3475 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003477 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003478 if (font == NOFONT)
3479 return FAIL;
3480
3481 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003482 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003483#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484 norm_logfont = lf;
3485 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003486 if (!s_in_dpichanged)
3487 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488#endif
3489 gui_mch_free_font(gui.norm_font);
3490 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003491 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 GetFontSize(font);
3493
K.Takatac81e9bf2022-01-16 14:15:49 +00003494 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495 if (p != NULL)
3496 {
3497 hl_set_font_name(p);
3498
Bram Moolenaar734a8672019-12-02 22:49:38 +01003499 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3501 {
3502 vim_free(p_guifont);
3503 p_guifont = p;
3504 }
3505 else
3506 vim_free(p);
3507 }
3508
3509 gui_mch_free_font(gui.ital_font);
3510 gui.ital_font = NOFONT;
3511 gui_mch_free_font(gui.bold_font);
3512 gui.bold_font = NOFONT;
3513 gui_mch_free_font(gui.boldital_font);
3514 gui.boldital_font = NOFONT;
3515
3516 if (!lf.lfItalic)
3517 {
3518 lf.lfItalic = TRUE;
3519 gui.ital_font = get_font_handle(&lf);
3520 lf.lfItalic = FALSE;
3521 }
3522 if (lf.lfWeight < FW_BOLD)
3523 {
3524 lf.lfWeight = FW_BOLD;
3525 gui.bold_font = get_font_handle(&lf);
3526 if (!lf.lfItalic)
3527 {
3528 lf.lfItalic = TRUE;
3529 gui.boldital_font = get_font_handle(&lf);
3530 }
3531 }
3532
3533 return OK;
3534}
3535
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003536/*
3537 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003538 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003539 */
3540 int
3541gui_mch_maximized(void)
3542{
3543 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003544 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003545
3546 wp.length = sizeof(WINDOWPLACEMENT);
3547 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003548 {
3549 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003550 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003551 && wp.flags == WPF_RESTORETOMAXIMIZED))
3552 return TRUE;
3553 if (wp.showCmd == SW_SHOWMINIMIZED)
3554 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003556 // Assume the window is snapped when the sizes from two APIs differ.
3557 GetWindowRect(s_hwnd, &rc);
3558 if ((rc.right - rc.left !=
3559 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3560 || (rc.bottom - rc.top !=
3561 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3562 return TRUE;
3563 }
3564 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003565}
3566
3567/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003568 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3569 * is set. Compute the new Rows and Columns. This is like resizing the
3570 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571 */
3572 void
3573gui_mch_newfont(void)
3574{
3575 RECT rect;
3576
3577 GetWindowRect(s_hwnd, &rect);
3578 if (win_socket_id == 0)
3579 {
3580 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003581 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3582 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003583 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003584 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3585 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3586 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3587 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003588 }
3589 else
3590 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003591 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003592 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003593 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 }
3595}
3596
3597/*
3598 * Set the window title
3599 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003600 void
3601gui_mch_settitle(
3602 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003603 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604{
3605 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3606}
3607
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003608#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003609// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3610// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611static LPCSTR mshape_idcs[] =
3612{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613 IDC_ARROW, // arrow
3614 MAKEINTRESOURCE(0), // blank
3615 IDC_IBEAM, // beam
3616 IDC_SIZENS, // updown
3617 IDC_SIZENS, // udsizing
3618 IDC_SIZEWE, // leftright
3619 IDC_SIZEWE, // lrsizing
3620 IDC_WAIT, // busy
3621 IDC_NO, // no
3622 IDC_ARROW, // crosshair
3623 IDC_ARROW, // hand1
3624 IDC_ARROW, // hand2
3625 IDC_ARROW, // pencil
3626 IDC_ARROW, // question
3627 IDC_ARROW, // right-arrow
3628 IDC_UPARROW, // up-arrow
3629 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003630};
3631
3632 void
3633mch_set_mouse_shape(int shape)
3634{
3635 LPCSTR idc;
3636
3637 if (shape == MSHAPE_HIDE)
3638 ShowCursor(FALSE);
3639 else
3640 {
3641 if (shape >= MSHAPE_NUMBERED)
3642 idc = IDC_ARROW;
3643 else
3644 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003645 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003646 if (!p_mh)
3647 {
3648 POINT mp;
3649
Bram Moolenaar734a8672019-12-02 22:49:38 +01003650 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003651 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 (void)SetCursorPos(mp.x, mp.y);
3653 ShowCursor(TRUE);
3654 }
3655 }
3656}
3657#endif
3658
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003659#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003660/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003661 * Wide version of convert_filter().
3662 */
3663 static WCHAR *
3664convert_filterW(char_u *s)
3665{
3666 char_u *tmp;
3667 int len;
3668 WCHAR *res;
3669
3670 tmp = convert_filter(s);
3671 if (tmp == NULL)
3672 return NULL;
3673 len = (int)STRLEN(s) + 3;
3674 res = enc_to_utf16(tmp, &len);
3675 vim_free(tmp);
3676 return res;
3677}
3678
3679/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003680 * Pop open a file browser and return the file selected, in allocated memory,
3681 * or NULL if Cancel is hit.
3682 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3683 * title - Title message for the file browser dialog.
3684 * dflt - Default name of file.
3685 * ext - Default extension to be added to files without extensions.
3686 * initdir - directory in which to open the browser (NULL = current dir)
3687 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003689 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003690gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003691 int saving,
3692 char_u *title,
3693 char_u *dflt,
3694 char_u *ext,
3695 char_u *initdir,
3696 char_u *filter)
3697{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003698 // We always use the wide function. This means enc_to_utf16() must work,
3699 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003700 OPENFILENAMEW fileStruct;
3701 WCHAR fileBuf[MAXPATHL];
3702 WCHAR *wp;
3703 int i;
3704 WCHAR *titlep = NULL;
3705 WCHAR *extp = NULL;
3706 WCHAR *initdirp = NULL;
3707 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003708 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003709 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003710
3711 if (dflt == NULL)
3712 fileBuf[0] = NUL;
3713 else
3714 {
3715 wp = enc_to_utf16(dflt, NULL);
3716 if (wp == NULL)
3717 fileBuf[0] = NUL;
3718 else
3719 {
3720 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3721 fileBuf[i] = wp[i];
3722 fileBuf[i] = NUL;
3723 vim_free(wp);
3724 }
3725 }
3726
Bram Moolenaar734a8672019-12-02 22:49:38 +01003727 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728 filterp = convert_filterW(filter);
3729
Bram Moolenaara80faa82020-04-12 19:37:17 +02003730 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003731# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003732 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003733 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003734# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003736# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737
3738 if (title != NULL)
3739 titlep = enc_to_utf16(title, NULL);
3740 fileStruct.lpstrTitle = titlep;
3741
3742 if (ext != NULL)
3743 extp = enc_to_utf16(ext, NULL);
3744 fileStruct.lpstrDefExt = extp;
3745
3746 fileStruct.lpstrFile = fileBuf;
3747 fileStruct.nMaxFile = MAXPATHL;
3748 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003749 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3750 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003751 if (initdir != NULL && *initdir != NUL)
3752 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003753 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003754 initdirp = enc_to_utf16(initdir, NULL);
3755 if (initdirp != NULL)
3756 {
3757 for (wp = initdirp; *wp != NUL; ++wp)
3758 if (*wp == '/')
3759 *wp = '\\';
3760 }
3761 fileStruct.lpstrInitialDir = initdirp;
3762 }
3763
3764 /*
3765 * TODO: Allow selection of multiple files. Needs another arg to this
3766 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3767 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3768 * files that don't exist yet, so I haven't put it in. What about
3769 * OFN_PATHMUSTEXIST?
3770 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3771 */
3772 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003773# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003774 if (curbuf->b_p_bin)
3775 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003776# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003777 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003778 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003780 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781
3782 vim_free(filterp);
3783 vim_free(initdirp);
3784 vim_free(titlep);
3785 vim_free(extp);
3786
K.Takata14b8d6a2022-01-20 15:05:22 +00003787 if (!ret)
3788 return NULL;
3789
3790 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003791 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003792 if (p == NULL)
3793 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003794
Bram Moolenaar734a8672019-12-02 22:49:38 +01003795 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003796 SetFocus(s_hwnd);
3797
Bram Moolenaar734a8672019-12-02 22:49:38 +01003798 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003799 q = vim_strsave(shorten_fname1(p));
3800 vim_free(p);
3801 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803
3804
3805/*
3806 * Convert the string s to the proper format for a filter string by replacing
3807 * the \t and \n delimiters with \0.
3808 * Returns the converted string in allocated memory.
3809 *
3810 * Keep in sync with convert_filterW() above!
3811 */
3812 static char_u *
3813convert_filter(char_u *s)
3814{
3815 char_u *res;
3816 unsigned s_len = (unsigned)STRLEN(s);
3817 unsigned i;
3818
3819 res = alloc(s_len + 3);
3820 if (res != NULL)
3821 {
3822 for (i = 0; i < s_len; ++i)
3823 if (s[i] == '\t' || s[i] == '\n')
3824 res[i] = '\0';
3825 else
3826 res[i] = s[i];
3827 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003828 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829 res[s_len + 1] = NUL;
3830 res[s_len + 2] = NUL;
3831 }
3832 return res;
3833}
3834
3835/*
3836 * Select a directory.
3837 */
3838 char_u *
3839gui_mch_browsedir(char_u *title, char_u *initdir)
3840{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003841 // We fake this: Use a filter that doesn't select anything and a default
3842 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3844 initdir, (char_u *)_("Directory\t*.nothing\n"));
3845}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003846#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003848 static void
3849_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003850 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851 HDROP hDrop)
3852{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003853#define BUFPATHLEN _MAX_PATH
3854#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003856 char szFile[BUFPATHLEN];
3857 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3858 UINT i;
3859 char_u **fnames;
3860 POINT pt;
3861 int_u modifiers = 0;
3862
Bram Moolenaar734a8672019-12-02 22:49:38 +01003863 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864 DragQueryPoint(hDrop, &pt);
3865 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3866
3867 reset_VIsual();
3868
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003869 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003870
3871 if (fnames != NULL)
3872 for (i = 0; i < cFiles; ++i)
3873 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3875 fnames[i] = utf16_to_enc(wszFile, NULL);
3876 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877 {
3878 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3879 fnames[i] = vim_strsave((char_u *)szFile);
3880 }
3881 }
3882
3883 DragFinish(hDrop);
3884
3885 if (fnames != NULL)
3886 {
LemonBoy45684c62022-04-24 15:46:42 +01003887 int kbd_modifiers = get_active_modifiers();
3888
3889 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003891 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003892 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003893 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003894 modifiers |= MOUSE_ALT;
3895
3896 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3897
3898 s_need_activate = TRUE;
3899 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900}
3901
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003902 static int
3903_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003904 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 HWND hwndCtl,
3906 UINT code,
3907 int pos)
3908{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003909 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 scrollbar_T *sb, *sb_info;
3911 long val;
3912 int dragging = FALSE;
3913 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914 SCROLLINFO si;
3915
3916 si.cbSize = sizeof(si);
3917 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918
3919 sb = gui_mswin_find_scrollbar(hwndCtl);
3920 if (sb == NULL)
3921 return 0;
3922
Bram Moolenaar734a8672019-12-02 22:49:38 +01003923 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924 {
3925 /*
3926 * Careful: need to get scrollbar info out of first (left) scrollbar
3927 * for window, but keep real scrollbar too because we must pass it to
3928 * gui_drag_scrollbar().
3929 */
3930 sb_info = &sb->wp->w_scrollbars[0];
3931 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003932 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 sb_info = sb;
3934 val = sb_info->value;
3935
3936 switch (code)
3937 {
3938 case SB_THUMBTRACK:
3939 val = pos;
3940 dragging = TRUE;
3941 if (sb->scroll_shift > 0)
3942 val <<= sb->scroll_shift;
3943 break;
3944 case SB_LINEDOWN:
3945 val++;
3946 break;
3947 case SB_LINEUP:
3948 val--;
3949 break;
3950 case SB_PAGEDOWN:
3951 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3952 break;
3953 case SB_PAGEUP:
3954 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3955 break;
3956 case SB_TOP:
3957 val = 0;
3958 break;
3959 case SB_BOTTOM:
3960 val = sb_info->max;
3961 break;
3962 case SB_ENDSCROLL:
3963 if (prev_code == SB_THUMBTRACK)
3964 {
3965 /*
3966 * "pos" only gives us 16-bit data. In case of large file,
3967 * use GetScrollPos() which returns 32-bit. Unfortunately it
3968 * is not valid while the scrollbar is being dragged.
3969 */
3970 val = GetScrollPos(hwndCtl, SB_CTL);
3971 if (sb->scroll_shift > 0)
3972 val <<= sb->scroll_shift;
3973 }
3974 break;
3975
3976 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003977 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003978 return 0;
3979 }
3980 prev_code = code;
3981
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003982 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3983 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003984
3985 /*
3986 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3987 */
3988 if (sb->wp != NULL)
3989 {
3990 scrollbar_T *sba = sb->wp->w_scrollbars;
3991 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3992
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003993 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003994 }
3995
Bram Moolenaar734a8672019-12-02 22:49:38 +01003996 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003997 s_busy_processing = TRUE;
3998
Bram Moolenaar734a8672019-12-02 22:49:38 +01003999 // When "allow_scrollbar" is FALSE still need to remember the new
4000 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004001 dont_scroll = !allow_scrollbar;
4002
Bram Moolenaara338adc2018-01-31 20:51:47 +01004003 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004004 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004005 mch_enable_flush();
4006 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004007
4008 s_busy_processing = FALSE;
4009 dont_scroll = dont_scroll_save;
4010
4011 return 0;
4012}
4013
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#ifdef FEAT_XPM_W32
4016# include "xpm_w32.h"
4017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
Bram Moolenaar734a8672019-12-02 22:49:38 +01004020// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021#define TEAROFF_PADDING_X 2
4022#define TEAROFF_BUTTON_PAD_X 8
4023#define TEAROFF_MIN_WIDTH 200
4024#define TEAROFF_SUBMENU_LABEL ">>"
4025#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4026
4027
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004028#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029# define ID_BEVAL_TOOLTIP 200
4030# define BEVAL_TEXT_LEN MAXPATHL
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004033static UINT_PTR beval_timer_id = 0;
4034static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004035#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004036
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004037
Bram Moolenaar734a8672019-12-02 22:49:38 +01004038// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040#ifdef FEAT_MENU
4041static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044/*
4045 * Use the system font for dialogs and tear-off menus. Remove this line to
4046 * use DLG_FONT_NAME.
4047 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004048#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#define VIM_CLASSW L"Vim"
4052
Bram Moolenaar734a8672019-12-02 22:49:38 +01004053// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4054// thus there should be room for every dialog. For tearoffs it's made bigger
4055// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056#define DLG_ALLOC_SIZE 16 * 1024
4057
4058/*
4059 * stuff for dialogs, menus, tearoffs etc.
4060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061static PWORD
4062add_dialog_element(
4063 PWORD p,
4064 DWORD lStyle,
4065 WORD x,
4066 WORD y,
4067 WORD w,
4068 WORD h,
4069 WORD Id,
4070 WORD clss,
4071 const char *caption);
4072static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004073static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004074#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077static void get_dialog_font_metrics(void);
4078
4079static int dialog_default_button = -1;
4080
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#ifdef FEAT_TOOLBAR
4082static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004083static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004084static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004086#else
4087# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#endif
4089
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004090#ifdef FEAT_GUI_TABLINE
4091static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004092static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004093#endif
4094
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#ifdef FEAT_MBYTE_IME
4096static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4097static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4098#endif
4099#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4100# ifdef NOIME
4101typedef struct tagCOMPOSITIONFORM {
4102 DWORD dwStyle;
4103 POINT ptCurrentPos;
4104 RECT rcArea;
4105} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4106typedef HANDLE HIMC;
4107# endif
4108
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004109static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004110static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4111static HIMC (WINAPI *pImmGetContext)(HWND);
4112static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4113static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4114static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4115static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004116static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4117static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004118static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4119static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004120static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121static void dyn_imm_load(void);
4122#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123# define pImmGetCompositionStringW ImmGetCompositionStringW
4124# define pImmGetContext ImmGetContext
4125# define pImmAssociateContext ImmAssociateContext
4126# define pImmReleaseContext ImmReleaseContext
4127# define pImmGetOpenStatus ImmGetOpenStatus
4128# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004129# define pImmGetCompositionFontW ImmGetCompositionFontW
4130# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131# define pImmSetCompositionWindow ImmSetCompositionWindow
4132# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004133# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134#endif
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136#ifdef FEAT_MENU
4137/*
4138 * Figure out how high the menu bar is at the moment.
4139 */
4140 static int
4141gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004142 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 static int old_menu_height = -1;
4145
4146 RECT rc1, rc2;
4147 int num;
4148 int menu_height;
4149
4150 if (gui.menu_is_active)
4151 num = GetMenuItemCount(s_menuBar);
4152 else
4153 num = 0;
4154
4155 if (num == 0)
4156 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004157 else if (IsMinimized(s_hwnd))
4158 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004159 // The height of the menu cannot be determined while the window is
4160 // minimized. Take the previous height if the menu is changed in that
4161 // state, to avoid that Vim's vertical window size accidentally
4162 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004163 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 else
4166 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004167 /*
4168 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4169 * seem to have been set yet, so menu wraps in default window
4170 * width which is very narrow. Instead just return height of a
4171 * single menu item. Will still be wrong when the menu really
4172 * should wrap over more than one line.
4173 */
4174 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4175 if (gui.starting)
4176 menu_height = rc1.bottom - rc1.top + 1;
4177 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004179 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4180 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 }
4183
4184 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004185 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004186 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 return menu_height;
4189}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004190#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192
4193/*
4194 * Setup for the Intellimouse
4195 */
LemonBoyc27747e2022-05-07 12:25:40 +01004196 static long
4197mouse_vertical_scroll_step(void)
4198{
4199 UINT val;
4200 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4201 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4202 return 3; // Safe default;
4203}
4204
4205 static long
4206mouse_horizontal_scroll_step(void)
4207{
4208 UINT val;
4209 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4210 return (long)val;
4211 return 3; // Safe default;
4212}
4213
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 static void
4215init_mouse_wheel(void)
4216{
LemonBoyc27747e2022-05-07 12:25:40 +01004217 // Get the default values for the horizontal and vertical scroll steps from
4218 // the system.
4219 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4220 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
Bram Moolenaarae20f342019-11-05 21:09:23 +01004223/*
LemonBoya773d842022-05-10 20:54:46 +01004224 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004227_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
LemonBoy365d8f72022-05-05 19:23:07 +01004229 int button;
4230 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004231 int modifiers = 0;
4232 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004233 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4234 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235
Bram Moolenaarae20f342019-11-05 21:09:23 +01004236 wp = gui_mouse_window(FIND_POPUP);
4237
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004238#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004239 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004240 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004241 cmdarg_T cap;
4242 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004243
Bram Moolenaarae20f342019-11-05 21:09:23 +01004244 // Mouse hovers over popup window, scroll it if possible.
4245 mouse_row = wp->w_winrow;
4246 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004247 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004248 if (horizontal)
4249 {
4250 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4251 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4252 }
4253 else
4254 {
4255 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4256 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4257 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004258 clear_oparg(&oa);
4259 cap.oap = &oa;
4260 nv_mousescroll(&cap);
4261 update_screen(0);
4262 setcursor();
4263 out_flush();
4264 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004265 }
4266#endif
4267
Bram Moolenaarae20f342019-11-05 21:09:23 +01004268 if (wp == NULL || !p_scf)
4269 wp = curwin;
4270
LemonBoy365d8f72022-05-05 19:23:07 +01004271 // Translate the scroll event into an event that Vim can process so that
4272 // the user has a chance to map the scrollwheel buttons.
4273 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004274 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 else
LemonBoy365d8f72022-05-05 19:23:07 +01004276 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004277
4278 kbd_modifiers = get_active_modifiers();
4279
4280 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4281 modifiers |= MOUSE_SHIFT;
4282 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4283 modifiers |= MOUSE_CTRL;
4284 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4285 modifiers |= MOUSE_ALT;
4286
LemonBoya773d842022-05-10 20:54:46 +01004287 // The cursor position is relative to the upper-left corner of the screen.
4288 pt.x = GET_X_LPARAM(lParam);
4289 pt.y = GET_Y_LPARAM(lParam);
4290 ScreenToClient(s_textArea, &pt);
4291
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004292 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293}
4294
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004295#ifdef USE_SYSMENU_FONT
4296/*
4297 * Get Menu Font.
4298 * Return OK or FAIL.
4299 */
4300 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004301gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004302{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004303 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004304
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004305 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4306 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004307 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004308 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004309 &nm,
4310 0))
4311 return FAIL;
4312 *lf = nm.lfMenuFont;
4313 return OK;
4314}
4315#endif
4316
4317
4318#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4319/*
4320 * Set the GUI tabline font to the system menu font
4321 */
4322 static void
4323set_tabline_font(void)
4324{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004325 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004326 HFONT font;
4327 HWND hwnd;
4328 HDC hdc;
4329 HFONT hfntOld;
4330 TEXTMETRIC tm;
4331
4332 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4333 return;
4334
K.Takatac81e9bf2022-01-16 14:15:49 +00004335 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004336 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004337
4338 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4339
4340 /*
4341 * Compute the height of the font used for the tab text
4342 */
4343 hwnd = GetDesktopWindow();
4344 hdc = GetWindowDC(hwnd);
4345 hfntOld = SelectFont(hdc, font);
4346
4347 GetTextMetrics(hdc, &tm);
4348
4349 SelectFont(hdc, hfntOld);
4350 ReleaseDC(hwnd, hdc);
4351
4352 /*
4353 * The space used by the tab border and the space between the tab label
4354 * and the tab border is included as 7.
4355 */
4356 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4357}
K.Takatac81e9bf2022-01-16 14:15:49 +00004358#else
4359# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004360#endif
4361
Bram Moolenaar520470a2005-06-16 21:59:56 +00004362/*
4363 * Invoked when a setting was changed.
4364 */
4365 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004366_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004367{
LemonBoy365d8f72022-05-05 19:23:07 +01004368 switch (param)
4369 {
4370 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004371 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004372 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004373 case SPI_SETWHEELSCROLLCHARS:
4374 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004375 break;
4376 case SPI_SETNONCLIENTMETRICS:
4377 set_tabline_font();
4378 break;
4379 default:
4380 break;
4381 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004382 return 0;
4383}
4384
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385#ifdef FEAT_NETBEANS_INTG
4386 static void
4387_OnWindowPosChanged(
4388 HWND hwnd,
4389 const LPWINDOWPOS lpwpos)
4390{
4391 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004392 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4395 || lpwpos->cx != cx || lpwpos->cy != cy))
4396 {
4397 x = lpwpos->x;
4398 y = lpwpos->y;
4399 cx = lpwpos->cx;
4400 cy = lpwpos->cy;
4401 netbeans_frame_moved(x, y);
4402 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004403 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004404 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405}
4406#endif
4407
K.Takata4f2417f2021-06-05 16:25:32 +02004408
4409static HWND hwndTip = NULL;
4410
4411 static void
4412show_sizing_tip(int cols, int rows)
4413{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004414 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004415 char buf[32];
4416
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004417 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004418 ti.hwnd = s_hwnd;
4419 ti.uId = (UINT_PTR)s_hwnd;
4420 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4421 ti.lpszText = buf;
4422 sprintf(buf, "%dx%d", cols, rows);
4423 if (hwndTip == NULL)
4424 {
4425 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4426 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4427 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4428 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4429 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4430 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4431 }
4432 else
4433 {
4434 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4435 }
4436 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4437}
4438
4439 static void
4440destroy_sizing_tip(void)
4441{
4442 if (hwndTip != NULL)
4443 {
4444 DestroyWindow(hwndTip);
4445 hwndTip = NULL;
4446 }
4447}
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 static int
4450_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 UINT fwSide,
4452 LPRECT lprc)
4453{
4454 int w, h;
4455 int valid_w, valid_h;
4456 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004457 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
4459 w = lprc->right - lprc->left;
4460 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004461 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 w_offset = w - valid_w;
4463 h_offset = h - valid_h;
4464
4465 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4466 || fwSide == WMSZ_BOTTOMLEFT)
4467 lprc->left += w_offset;
4468 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4469 || fwSide == WMSZ_BOTTOMRIGHT)
4470 lprc->right -= w_offset;
4471
4472 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4473 || fwSide == WMSZ_TOPRIGHT)
4474 lprc->top += h_offset;
4475 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4476 || fwSide == WMSZ_BOTTOMRIGHT)
4477 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004478
4479 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 return TRUE;
4481}
4482
K.Takata92000e22022-01-20 15:10:57 +00004483#ifdef FEAT_GUI_TABLINE
4484 static void
4485_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4486{
4487 if (gui_mch_showing_tabline())
4488 {
4489 POINT pt;
4490 RECT rect;
4491
4492 /*
4493 * If the cursor is on the tabline, display the tab menu
4494 */
4495 GetCursorPos(&pt);
4496 GetWindowRect(s_textArea, &rect);
4497 if (pt.y < rect.top)
4498 {
4499 show_tabline_popup_menu();
4500 return;
4501 }
4502 }
4503 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4504}
4505
4506 static void
4507_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4508{
4509 /*
4510 * If the user double clicked the tabline, create a new tab
4511 */
4512 if (gui_mch_showing_tabline())
4513 {
4514 POINT pt;
4515 RECT rect;
4516
4517 GetCursorPos(&pt);
4518 GetWindowRect(s_textArea, &rect);
4519 if (pt.y < rect.top)
4520 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4521 }
4522 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4523}
4524#endif
4525
4526 static UINT
4527_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4528{
4529 UINT result;
4530 int x, y;
4531
4532 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4533 if (result != HTCLIENT)
4534 return result;
4535
4536#ifdef FEAT_GUI_TABLINE
4537 if (gui_mch_showing_tabline())
4538 {
4539 RECT rct;
4540
4541 // If the cursor is on the GUI tabline, don't process this event
4542 GetWindowRect(s_textArea, &rct);
4543 if (yPos < rct.top)
4544 return result;
4545 }
4546#endif
4547 (void)gui_mch_get_winpos(&x, &y);
4548 xPos -= x;
4549
4550 if (xPos < 48) // <VN> TODO should use system metric?
4551 return HTBOTTOMLEFT;
4552 else
4553 return HTBOTTOMRIGHT;
4554}
4555
4556#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4557 static LRESULT
4558_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4559{
4560 switch (hdr->code)
4561 {
4562 case TTN_GETDISPINFOW:
4563 case TTN_GETDISPINFO:
4564 {
4565 char_u *str = NULL;
4566 static void *tt_text = NULL;
4567
4568 VIM_CLEAR(tt_text);
4569
4570# ifdef FEAT_GUI_TABLINE
4571 if (gui_mch_showing_tabline()
4572 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4573 {
4574 POINT pt;
4575 /*
4576 * Mouse is over the GUI tabline. Display the
4577 * tooltip for the tab under the cursor
4578 *
4579 * Get the cursor position within the tab control
4580 */
4581 GetCursorPos(&pt);
4582 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4583 {
4584 TCHITTESTINFO htinfo;
4585 int idx;
4586
4587 /*
4588 * Get the tab under the cursor
4589 */
4590 htinfo.pt.x = pt.x;
4591 htinfo.pt.y = pt.y;
4592 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4593 if (idx != -1)
4594 {
4595 tabpage_T *tp;
4596
4597 tp = find_tabpage(idx + 1);
4598 if (tp != NULL)
4599 {
4600 get_tabline_label(tp, TRUE);
4601 str = NameBuff;
4602 }
4603 }
4604 }
4605 }
4606# endif
4607# ifdef FEAT_TOOLBAR
4608# ifdef FEAT_GUI_TABLINE
4609 else
4610# endif
4611 {
4612 UINT idButton;
4613 vimmenu_T *pMenu;
4614
4615 idButton = (UINT) hdr->idFrom;
4616 pMenu = gui_mswin_find_menu(root_menu, idButton);
4617 if (pMenu)
4618 str = pMenu->strings[MENU_INDEX_TIP];
4619 }
4620# endif
4621 if (str == NULL)
4622 break;
4623
4624 // Set the maximum width, this also enables using \n for
4625 // line break.
4626 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4627
4628 if (hdr->code == TTN_GETDISPINFOW)
4629 {
4630 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4631
4632 tt_text = enc_to_utf16(str, NULL);
4633 lpdi->lpszText = tt_text;
4634 // can't show tooltip if failed
4635 }
4636 else
4637 {
4638 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4639
4640 if (STRLEN(str) < sizeof(lpdi->szText)
4641 || ((tt_text = vim_strsave(str)) == NULL))
4642 vim_strncpy((char_u *)lpdi->szText, str,
4643 sizeof(lpdi->szText) - 1);
4644 else
4645 lpdi->lpszText = tt_text;
4646 }
4647 }
4648 break;
4649
4650# ifdef FEAT_GUI_TABLINE
4651 case TCN_SELCHANGE:
4652 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4653 {
4654 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4655 return 0L;
4656 }
4657 break;
4658
4659 case NM_RCLICK:
4660 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4661 {
4662 show_tabline_popup_menu();
4663 return 0L;
4664 }
4665 break;
4666# endif
4667
4668 default:
4669 break;
4670 }
4671 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4672}
4673#endif
4674
4675#if defined(MENUHINTS) && defined(FEAT_MENU)
4676 static LRESULT
4677_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4678{
4679 if (((UINT) HIWORD(wParam)
4680 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4681 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004682 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004683 {
4684 UINT idButton;
4685 vimmenu_T *pMenu;
4686 static int did_menu_tip = FALSE;
4687
4688 if (did_menu_tip)
4689 {
4690 msg_clr_cmdline();
4691 setcursor();
4692 out_flush();
4693 did_menu_tip = FALSE;
4694 }
4695
4696 idButton = (UINT)LOWORD(wParam);
4697 pMenu = gui_mswin_find_menu(root_menu, idButton);
4698 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4699 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4700 {
4701 ++msg_hist_off;
4702 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4703 --msg_hist_off;
4704 setcursor();
4705 out_flush();
4706 did_menu_tip = TRUE;
4707 }
4708 return 0L;
4709 }
4710 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4711}
4712#endif
4713
K.Takatac81e9bf2022-01-16 14:15:49 +00004714 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004715_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004716{
4717 s_dpi = ydpi;
4718 s_in_dpichanged = TRUE;
4719 //TRACE("DPI: %d", ydpi);
4720
4721 update_scrollbar_size();
4722 update_toolbar_size();
4723 set_tabline_font();
4724
4725 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4726 gui_get_wide_font();
4727 gui_mswin_get_menu_height(FALSE);
4728#ifdef FEAT_MBYTE_IME
4729 im_set_position(gui.row, gui.col);
4730#endif
4731 InvalidateRect(hwnd, NULL, TRUE);
4732
4733 s_in_dpichanged = FALSE;
4734 return 0L;
4735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737
4738 static LRESULT CALLBACK
4739_WndProc(
4740 HWND hwnd,
4741 UINT uMsg,
4742 WPARAM wParam,
4743 LPARAM lParam)
4744{
LemonBoya773d842022-05-10 20:54:46 +01004745 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4746 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748 HandleMouseHide(uMsg, lParam);
4749
4750 s_uMsg = uMsg;
4751 s_wParam = wParam;
4752 s_lParam = lParam;
4753
4754 switch (uMsg)
4755 {
4756 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4757 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004758 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004760 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4762 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4763 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4764 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4765#ifdef FEAT_MENU
4766 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4767#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004768 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4769 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4771 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004772 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4773 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4775 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4776 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4777#ifdef FEAT_NETBEANS_INTG
4778 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4779#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004780#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004781 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4782 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004783#endif
K.Takata92000e22022-01-20 15:10:57 +00004784 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004785
Bram Moolenaar734a8672019-12-02 22:49:38 +01004786 case WM_QUERYENDSESSION: // System wants to go down.
4787 gui_shell_closed(); // Will exit when no changed buffers.
4788 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004791 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004792 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004794 return 0L;
4795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 break;
4797
4798 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004799 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4800 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004801 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 return 0L;
4803
4804 case WM_SYSCHAR:
4805 /*
4806 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4807 * shortcut key, handle like a typed ALT key, otherwise call Windows
4808 * ALT key handling.
4809 */
4810#ifdef FEAT_MENU
4811 if ( !gui.menu_is_active
4812 || p_wak[0] == 'n'
4813 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4814 )
4815#endif
4816 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return 0L;
4819 }
4820#ifdef FEAT_MENU
4821 else
K.Takata4ac893f2022-01-20 12:44:28 +00004822 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#endif
4824
4825 case WM_SYSKEYUP:
4826#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004827 // This used to be done only when menu is active: ALT key is used for
4828 // that. But that caused problems when menu is disabled and using
4829 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4830 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004831 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004833 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
4835
K.Takata4f2417f2021-06-05 16:25:32 +02004836 case WM_EXITSIZEMOVE:
4837 destroy_sizing_tip();
4838 break;
4839
Bram Moolenaar734a8672019-12-02 22:49:38 +01004840 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004844 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004845 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004846 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar734a8672019-12-02 22:49:38 +01004848 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004849 case WM_SETTINGCHANGE:
4850 return _OnSettingChange((UINT)wParam);
4851
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004852#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004854 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#endif
K.Takata92000e22022-01-20 15:10:57 +00004856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#if defined(MENUHINTS) && defined(FEAT_MENU)
4858 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004859 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862#ifdef FEAT_MBYTE_IME
4863 case WM_IME_NOTIFY:
4864 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004865 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004866 return 1L;
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 case WM_IME_COMPOSITION:
4869 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004870 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004871 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004873 case WM_DPICHANGED:
4874 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4875 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004879 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
K.Takata92000e22022-01-20 15:10:57 +00004882 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884
K.Takata4ac893f2022-01-20 12:44:28 +00004885 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886}
4887
4888/*
4889 * End of call-back routines
4890 */
4891
Bram Moolenaar734a8672019-12-02 22:49:38 +01004892// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893HWND vim_parent_hwnd = NULL;
4894
4895 static BOOL CALLBACK
4896FindWindowTitle(HWND hwnd, LPARAM lParam)
4897{
4898 char buf[2048];
4899 char *title = (char *)lParam;
4900
4901 if (GetWindowText(hwnd, buf, sizeof(buf)))
4902 {
4903 if (strstr(buf, title) != NULL)
4904 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004905 // Found it. Store the window ref. and quit searching if MDI
4906 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004908 if (vim_parent_hwnd != NULL)
4909 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004912 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913}
4914
4915/*
4916 * Invoked for '-P "title"' argument: search for parent application to open
4917 * our window in.
4918 */
4919 void
4920gui_mch_set_parent(char *title)
4921{
4922 EnumWindows(FindWindowTitle, (LPARAM)title);
4923 if (vim_parent_hwnd == NULL)
4924 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004925 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 mch_exit(2);
4927 }
4928}
4929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004930#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 static void
4932ole_error(char *arg)
4933{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004934 char buf[IOSIZE];
4935
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004936# ifdef VIMDLL
4937 gui.in_use = mch_is_gui_executable();
4938# endif
4939
Bram Moolenaar734a8672019-12-02 22:49:38 +01004940 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004941 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004942 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004943 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004947#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4948 static char *
4949gvim_error(void)
4950{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004951 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004952
4953 if (starting)
4954 {
4955 mch_errmsg(msg);
4956 mch_errmsg("\n");
4957 mch_exit(2);
4958 }
4959 return msg;
4960}
4961
4962 char *
4963gui_mch_do_spawn(char_u *arg)
4964{
4965 int len;
4966# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4967 char_u *session = NULL;
4968 LPWSTR tofree1 = NULL;
4969# endif
4970 WCHAR name[MAX_PATH];
4971 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4972 STARTUPINFOW si = {sizeof(si)};
4973 PROCESS_INFORMATION pi;
4974
4975 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4976 goto error;
4977 p = wcsrchr(name, L'\\');
4978 if (p == NULL)
4979 goto error;
4980 // Replace the executable name from vim(d).exe to gvim(d).exe.
4981# ifdef DEBUG
4982 wcscpy(p + 1, L"gvimd.exe");
4983# else
4984 wcscpy(p + 1, L"gvim.exe");
4985# endif
4986
4987# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4988 if (starting)
4989# endif
4990 {
4991 // Pass the command line to the new process.
4992 p = GetCommandLineW();
4993 // Skip 1st argument.
4994 while (*p && *p != L' ' && *p != L'\t')
4995 {
4996 if (*p == L'"')
4997 {
4998 while (*p && *p != L'"')
4999 ++p;
5000 if (*p)
5001 ++p;
5002 }
5003 else
5004 ++p;
5005 }
5006 cmd = p;
5007 }
5008# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5009 else
5010 {
5011 // Create a session file and pass it to the new process.
5012 LPWSTR wsession;
5013 char_u *savebg;
5014 int ret;
5015
5016 session = vim_tempname('s', FALSE);
5017 if (session == NULL)
5018 goto error;
5019 savebg = p_bg;
5020 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5021 ret = write_session_file(session);
5022 vim_free(p_bg);
5023 p_bg = savebg;
5024 if (!ret)
5025 goto error;
5026 wsession = enc_to_utf16(session, NULL);
5027 if (wsession == NULL)
5028 goto error;
5029 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005030 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005031 if (cmd == NULL)
5032 {
5033 vim_free(wsession);
5034 goto error;
5035 }
5036 tofree1 = cmd;
5037 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5038 wsession, wsession);
5039 vim_free(wsession);
5040 }
5041# endif
5042
5043 // Check additional arguments to the `:gui` command.
5044 if (arg != NULL)
5045 {
5046 warg = enc_to_utf16(arg, NULL);
5047 if (warg == NULL)
5048 goto error;
5049 tofree2 = warg;
5050 }
5051 else
5052 warg = L"";
5053
5054 // Set up the new command line.
5055 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005056 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005057 if (newcmd == NULL)
5058 goto error;
5059 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5060
5061 // Spawn a new GUI process.
5062 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5063 NULL, NULL, &si, &pi))
5064 goto error;
5065 CloseHandle(pi.hProcess);
5066 CloseHandle(pi.hThread);
5067 mch_exit(0);
5068
5069error:
5070# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5071 if (session)
5072 mch_remove(session);
5073 vim_free(session);
5074 vim_free(tofree1);
5075# endif
5076 vim_free(newcmd);
5077 vim_free(tofree2);
5078 return gvim_error();
5079}
5080#endif
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
5083 * Parse the GUI related command-line arguments. Any arguments used are
5084 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005085 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
5087 void
5088gui_mch_prepare(int *argc, char **argv)
5089{
5090 int silent = FALSE;
5091 int idx;
5092
Bram Moolenaar734a8672019-12-02 22:49:38 +01005093 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5095 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005096 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5098 && (argv[2][0] == '-' || argv[2][0] == '/'))
5099 {
5100 silent = TRUE;
5101 idx = 2;
5102 }
5103 else
5104 idx = 1;
5105
Bram Moolenaar734a8672019-12-02 22:49:38 +01005106 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (STRICMP(argv[idx] + 1, "register") == 0)
5108 {
5109#ifdef FEAT_OLE
5110 RegisterMe(silent);
5111 mch_exit(0);
5112#else
5113 if (!silent)
5114 ole_error("register");
5115 mch_exit(2);
5116#endif
5117 }
5118
Bram Moolenaar734a8672019-12-02 22:49:38 +01005119 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5121 {
5122#ifdef FEAT_OLE
5123 UnregisterMe(!silent);
5124 mch_exit(0);
5125#else
5126 if (!silent)
5127 ole_error("unregister");
5128 mch_exit(2);
5129#endif
5130 }
5131
Bram Moolenaar734a8672019-12-02 22:49:38 +01005132 // Ignore an -embedding argument. It is only relevant if the
5133 // application wants to treat the case when it is started manually
5134 // differently from the case where it is started via automation (and
5135 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5137 {
5138#ifdef FEAT_OLE
5139 *argc = 1;
5140#else
5141 ole_error("embedding");
5142 mch_exit(2);
5143#endif
5144 }
5145 }
5146
5147#ifdef FEAT_OLE
5148 {
5149 int bDoRestart = FALSE;
5150
5151 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005152 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (bDoRestart)
5154 mch_exit(0);
5155 }
5156#endif
5157
5158#ifdef FEAT_NETBEANS_INTG
5159 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005160 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 int arg;
5162
5163 for (arg = 1; arg < *argc; arg++)
5164 if (strncmp("-nb", argv[arg], 3) == 0)
5165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 netbeansArg = argv[arg];
5167 mch_memmove(&argv[arg], &argv[arg + 1],
5168 (--*argc - arg) * sizeof(char *));
5169 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005170 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174}
5175
K.Takatac81e9bf2022-01-16 14:15:49 +00005176 static void
5177load_dpi_func(void)
5178{
5179 HMODULE hUser32;
5180
5181 hUser32 = GetModuleHandle("user32.dll");
5182 if (hUser32 == NULL)
5183 goto fail;
5184
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005185 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5186 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5187 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005188 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005189 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5190 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005191
5192 if (pSetThreadDpiAwarenessContext != NULL)
5193 {
5194 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5195 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5196 if (oldctx != NULL)
5197 {
5198 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5199 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5200#ifdef DEBUG
5201 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5202 {
5203 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5204 }
5205#endif
5206 return;
5207 }
5208 }
5209
5210fail:
5211 // Disable PerMonitorV2 APIs.
5212 pGetDpiForSystem = stubGetDpiForSystem;
5213 pGetDpiForWindow = NULL;
5214 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5215 pSetThreadDpiAwarenessContext = NULL;
5216 pGetAwarenessFromDpiAwarenessContext = NULL;
5217}
5218
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219/*
5220 * Initialise the GUI. Create all the windows, set up all the call-backs
5221 * etc.
5222 */
5223 int
5224gui_mch_init(void)
5225{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005227 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
Bram Moolenaar734a8672019-12-02 22:49:38 +01005230 // Return here if the window was already opened (happens when
5231 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005233 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 /*
5236 * Load the tearoff bitmap
5237 */
5238#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005239 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#endif
5241
K.Takatac81e9bf2022-01-16 14:15:49 +00005242 load_dpi_func();
5243
5244 s_dpi = pGetDpiForSystem();
5245 update_scrollbar_size();
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005248 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005251#ifdef FEAT_TOOLBAR
5252 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
5255 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5256
Bram Moolenaar734a8672019-12-02 22:49:38 +01005257 // First try using the wide version, so that we can use any title.
5258 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005259 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005261 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 wndclassw.lpfnWndProc = _WndProc;
5263 wndclassw.cbClsExtra = 0;
5264 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005265 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5267 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5268 wndclassw.hbrBackground = s_brush;
5269 wndclassw.lpszMenuName = NULL;
5270 wndclassw.lpszClassName = szVimWndClassW;
5271
K.Takata4ac893f2022-01-20 12:44:28 +00005272 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005273 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (vim_parent_hwnd != NULL)
5277 {
5278#ifdef HAVE_TRY_EXCEPT
5279 __try
5280 {
5281#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005282 // Open inside the specified parent window.
5283 // TODO: last argument should point to a CLIENTCREATESTRUCT
5284 // structure.
5285 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005287 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005288 WS_OVERLAPPEDWINDOW | WS_CHILD
5289 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5291 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005292 100, // Any value will do
5293 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005295 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#ifdef HAVE_TRY_EXCEPT
5297 }
5298 __except(EXCEPTION_EXECUTE_HANDLER)
5299 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005300 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302#endif
5303 if (s_hwnd == NULL)
5304 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005305 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 mch_exit(2);
5307 }
5308 }
5309 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005310 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005311 // If the provided windowid is not valid reset it to zero, so that it
5312 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005313 if (IsWindow((HWND)win_socket_id) <= 0)
5314 win_socket_id = 0;
5315
Bram Moolenaar734a8672019-12-02 22:49:38 +01005316 // Create a window. If win_socket_id is not zero without border and
5317 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005318 s_hwnd = CreateWindowW(
5319 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005320 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5321 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005322 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5323 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005324 100, // Any value will do
5325 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005326 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005327 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005328 if (s_hwnd != NULL && win_socket_id != 0)
5329 {
5330 SetParent(s_hwnd, (HWND)win_socket_id);
5331 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5332 }
5333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 if (s_hwnd == NULL)
5336 return FAIL;
5337
K.Takatac81e9bf2022-01-16 14:15:49 +00005338 if (pGetDpiForWindow != NULL)
5339 {
5340 s_dpi = pGetDpiForWindow(s_hwnd);
5341 update_scrollbar_size();
5342 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5343 }
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5346 dyn_imm_load();
5347#endif
5348
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005350 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005351 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005352 wndclassw.style = CS_OWNDC;
5353 wndclassw.lpfnWndProc = _TextAreaWndProc;
5354 wndclassw.cbClsExtra = 0;
5355 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005356 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005357 wndclassw.hIcon = NULL;
5358 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5359 wndclassw.hbrBackground = NULL;
5360 wndclassw.lpszMenuName = NULL;
5361 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005362
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005363 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return FAIL;
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005367 s_textArea = CreateWindowExW(
5368 0,
5369 szTextAreaClassW, L"Vim text area",
5370 WS_CHILD | WS_VISIBLE, 0, 0,
5371 100, // Any value will do for now
5372 100, // Any value will do for now
5373 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005374 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005375
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 if (s_textArea == NULL)
5377 return FAIL;
5378
Bram Moolenaar20321902016-02-17 12:30:17 +01005379#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005380 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005381 {
5382 HANDLE hIcon = NULL;
5383
5384 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005385 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005386 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005387#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#ifdef FEAT_MENU
5390 s_menuBar = CreateMenu();
5391#endif
5392 s_hdc = GetDC(s_textArea);
5393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
Bram Moolenaar734a8672019-12-02 22:49:38 +01005396 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005397 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398
Bram Moolenaar734a8672019-12-02 22:49:38 +01005399 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 gui_mch_def_colors();
5401
Bram Moolenaar734a8672019-12-02 22:49:38 +01005402 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5403 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 set_normal_colors();
5405
5406 /*
5407 * Check that none of the colors are the same as the background color.
5408 * Then store the current values as the defaults.
5409 */
5410 gui_check_colors();
5411 gui.def_norm_pixel = gui.norm_pixel;
5412 gui.def_back_pixel = gui.back_pixel;
5413
Bram Moolenaar734a8672019-12-02 22:49:38 +01005414 // Get the colors for the highlight groups (gui_check_colors() might have
5415 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 highlight_gui_started();
5417
5418 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005419 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005421 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 /*
5424 * Set up for Intellimouse processing
5425 */
5426 init_mouse_wheel();
5427
5428 /*
5429 * compute a couple of metrics used for the dialogs
5430 */
5431 get_dialog_font_metrics();
5432#ifdef FEAT_TOOLBAR
5433 /*
5434 * Create the toolbar
5435 */
5436 initialise_toolbar();
5437#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005438#ifdef FEAT_GUI_TABLINE
5439 /*
5440 * Create the tabline
5441 */
5442 initialise_tabline();
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#ifdef MSWIN_FIND_REPLACE
5445 /*
5446 * Initialise the dialog box stuff
5447 */
5448 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5449
Bram Moolenaar734a8672019-12-02 22:49:38 +01005450 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005452 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005454 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5456 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5457 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005460#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005461 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005462 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005463#endif
5464
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005465#ifdef FEAT_RENDER_OPTIONS
5466 if (p_rop)
5467 (void)gui_mch_set_rendering_options(p_rop);
5468#endif
5469
Bram Moolenaar748bf032005-02-02 23:04:36 +00005470theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005471 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005472 display_errors();
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 return OK;
5475}
5476
5477/*
5478 * Get the size of the screen, taking position on multiple monitors into
5479 * account (if supported).
5480 */
5481 static void
5482get_work_area(RECT *spi_rect)
5483{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005484 HMONITOR mon;
5485 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005488 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005489 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005491 moninfo.cbSize = sizeof(MONITORINFO);
5492 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005494 *spi_rect = moninfo.rcWork;
5495 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005498 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5500}
5501
5502/*
5503 * Set the size of the window to the given width and height in pixels.
5504 */
5505 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005506gui_mch_set_shellsize(
5507 int width,
5508 int height,
5509 int min_width UNUSED,
5510 int min_height UNUSED,
5511 int base_width UNUSED,
5512 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005513 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005516 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaar734a8672019-12-02 22:49:38 +01005519 // Try to keep window completely on screen.
5520 // Get position of the screen work area. This is the part that is not
5521 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 get_work_area(&workarea_rect);
5523
Bram Moolenaar734a8672019-12-02 22:49:38 +01005524 // Resizing a maximized window looks very strange, unzoom it first.
5525 // But don't do it when still starting up, it may have been requested in
5526 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005527 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005529
5530 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
Bram Moolenaar734a8672019-12-02 22:49:38 +01005532 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005533 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5534 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5535 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5536 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5537 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5538 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar734a8672019-12-02 22:49:38 +01005540 // The following should take care of keeping Vim on the same monitor, no
5541 // matter if the secondary monitor is left or right of the primary
5542 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005543 window_rect.right = window_rect.left + win_width;
5544 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005545
Bram Moolenaar734a8672019-12-02 22:49:38 +01005546 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005547 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5548 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005550 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5551 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005553 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5554 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005556 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5557 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005559 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5560 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
5562 SetActiveWindow(s_hwnd);
5563 SetFocus(s_hwnd);
5564
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567}
5568
5569
5570 void
5571gui_mch_set_scrollbar_thumb(
5572 scrollbar_T *sb,
5573 long val,
5574 long size,
5575 long max)
5576{
5577 SCROLLINFO info;
5578
5579 sb->scroll_shift = 0;
5580 while (max > 32767)
5581 {
5582 max = (max + 1) >> 1;
5583 val >>= 1;
5584 size >>= 1;
5585 ++sb->scroll_shift;
5586 }
5587
5588 if (sb->scroll_shift > 0)
5589 ++size;
5590
5591 info.cbSize = sizeof(info);
5592 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5593 info.nPos = val;
5594 info.nMin = 0;
5595 info.nMax = max;
5596 info.nPage = size;
5597 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5598}
5599
5600
5601/*
5602 * Set the current text font.
5603 */
5604 void
5605gui_mch_set_font(GuiFont font)
5606{
5607 gui.currFont = font;
5608}
5609
5610
5611/*
5612 * Set the current text foreground color.
5613 */
5614 void
5615gui_mch_set_fg_color(guicolor_T color)
5616{
5617 gui.currFgColor = color;
5618}
5619
5620/*
5621 * Set the current text background color.
5622 */
5623 void
5624gui_mch_set_bg_color(guicolor_T color)
5625{
5626 gui.currBgColor = color;
5627}
5628
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005629/*
5630 * Set the current text special color.
5631 */
5632 void
5633gui_mch_set_sp_color(guicolor_T color)
5634{
5635 gui.currSpColor = color;
5636}
5637
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005638#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639/*
5640 * Multi-byte handling, originally by Sung-Hoon Baek.
5641 * First static functions (no prototypes generated).
5642 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005643# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005644# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005645# endif
5646# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
5648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 * handle WM_IME_NOTIFY message
5650 */
5651 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005652_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 LRESULT lResult = 0;
5655 HIMC hImc;
5656
5657 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5658 return lResult;
5659 switch (dwCommand)
5660 {
5661 case IMN_SETOPENSTATUS:
5662 if (pImmGetOpenStatus(hImc))
5663 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005664 LOGFONTW lf = norm_logfont;
5665 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5666 // Work around when PerMonitorV2 is not enabled in the process level.
5667 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5668 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 im_set_position(gui.row, gui.col);
5670
Bram Moolenaar734a8672019-12-02 22:49:38 +01005671 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005672 State &= ~MODE_LANGMAP;
5673 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005675# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005676 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005679 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int old_row = gui.row;
5681 int old_col = gui.col;
5682
5683 // This must be called here before
5684 // status_redraw_curbuf(), otherwise the mode
5685 // message may appear in the wrong position.
5686 showmode();
5687 status_redraw_curbuf();
5688 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005689 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 gui.row = old_row;
5691 gui.col = old_col;
5692 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 }
5696 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005697 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 lResult = 0;
5699 break;
5700 }
5701 pImmReleaseContext(hWnd, hImc);
5702 return lResult;
5703}
5704
5705 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005706_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708 char_u *ret;
5709 int len;
5710
Bram Moolenaar734a8672019-12-02 22:49:38 +01005711 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 return 0;
5713
5714 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5715 if (ret != NULL)
5716 {
5717 add_to_input_buf_csi(ret, len);
5718 vim_free(ret);
5719 return 1;
5720 }
5721 return 0;
5722}
5723
5724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 * void GetResultStr()
5726 *
5727 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5728 * get complete composition string
5729 */
5730 static char_u *
5731GetResultStr(HWND hwnd, int GCS, int *lenp)
5732{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005733 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005734 LONG ret;
5735 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 char_u *convbuf = NULL;
5737
5738 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5739 return NULL;
5740
K.Takatab0b2b732022-01-19 12:59:21 +00005741 // Get the length of the composition string.
5742 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5743 if (ret <= 0)
5744 return NULL;
5745
5746 // Allocate the requested buffer plus space for the NUL character.
5747 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (buf == NULL)
5749 return NULL;
5750
K.Takatab0b2b732022-01-19 12:59:21 +00005751 // Reads in the composition string.
5752 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5753 *lenp = ret / sizeof(WCHAR);
5754
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005755 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 pImmReleaseContext(hwnd, hIMC);
5757 vim_free(buf);
5758 return convbuf;
5759}
5760#endif
5761
Bram Moolenaar734a8672019-12-02 22:49:38 +01005762// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005763#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765/*
5766 * set font to IM.
5767 */
5768 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005769im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 HIMC hImc;
5772
5773 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5774 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005775 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 pImmReleaseContext(s_hwnd, hImc);
5777 }
5778}
5779
5780/*
5781 * Notify cursor position to IM.
5782 */
5783 void
5784im_set_position(int row, int col)
5785{
5786 HIMC hImc;
5787
5788 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5789 {
5790 COMPOSITIONFORM cfs;
5791
5792 cfs.dwStyle = CFS_POINT;
5793 cfs.ptCurrentPos.x = FILL_X(col);
5794 cfs.ptCurrentPos.y = FILL_Y(row);
5795 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005796 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5797 {
5798 // Work around when PerMonitorV2 is not enabled in the process level.
5799 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5800 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 pImmSetCompositionWindow(hImc, &cfs);
5803
5804 pImmReleaseContext(s_hwnd, hImc);
5805 }
5806}
5807
5808/*
5809 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5810 */
5811 void
5812im_set_active(int active)
5813{
5814 HIMC hImc;
5815 static HIMC hImcOld = (HIMC)0;
5816
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005817# ifdef VIMDLL
5818 if (!gui.in_use && !gui.starting)
5819 {
5820 mbyte_im_set_active(active);
5821 return;
5822 }
5823# endif
5824
Bram Moolenaar734a8672019-12-02 22:49:38 +01005825 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
5827 if (p_imdisable)
5828 {
5829 if (hImcOld == (HIMC)0)
5830 {
5831 hImcOld = pImmGetContext(s_hwnd);
5832 if (hImcOld)
5833 pImmAssociateContext(s_hwnd, (HIMC)0);
5834 }
5835 active = FALSE;
5836 }
5837 else if (hImcOld != (HIMC)0)
5838 {
5839 pImmAssociateContext(s_hwnd, hImcOld);
5840 hImcOld = (HIMC)0;
5841 }
5842
5843 hImc = pImmGetContext(s_hwnd);
5844 if (hImc)
5845 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005846 /*
5847 * for Korean ime
5848 */
5849 HKL hKL = GetKeyboardLayout(0);
5850
5851 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5852 {
5853 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5854 static BOOL bSaved = FALSE;
5855
5856 if (active)
5857 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005858 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005859 if (bSaved)
5860 pImmSetConversionStatus(hImc, dwConversionSaved,
5861 dwSentenceSaved);
5862 bSaved = FALSE;
5863 }
5864 else
5865 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005866 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005867 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5868 &dwSentenceSaved))
5869 {
5870 bSaved = TRUE;
5871 pImmSetConversionStatus(hImc,
5872 dwConversionSaved & ~(IME_CMODE_NATIVE
5873 | IME_CMODE_FULLSHAPE),
5874 dwSentenceSaved);
5875 }
5876 }
5877 }
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 pImmSetOpenStatus(hImc, active);
5880 pImmReleaseContext(s_hwnd, hImc);
5881 }
5882 }
5883}
5884
5885/*
5886 * Get IM status. When IM is on, return not 0. Else return 0.
5887 */
5888 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005889im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890{
5891 int status = 0;
5892 HIMC hImc;
5893
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005894# ifdef VIMDLL
5895 if (!gui.in_use && !gui.starting)
5896 return mbyte_im_get_status();
5897# endif
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5900 {
5901 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5902 pImmReleaseContext(s_hwnd, hImc);
5903 }
5904 return status;
5905}
5906
Bram Moolenaar734a8672019-12-02 22:49:38 +01005907#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005910/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005911 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005912 */
5913 static void
5914latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5915{
5916 int c;
5917
Bram Moolenaarca003e12006-03-17 23:19:38 +00005918 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005919 {
5920 c = *text++;
5921 switch (c)
5922 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005923 case 0xa4: c = 0x20ac; break; // euro
5924 case 0xa6: c = 0x0160; break; // S hat
5925 case 0xa8: c = 0x0161; break; // S -hat
5926 case 0xb4: c = 0x017d; break; // Z hat
5927 case 0xb8: c = 0x017e; break; // Z -hat
5928 case 0xbc: c = 0x0152; break; // OE
5929 case 0xbd: c = 0x0153; break; // oe
5930 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005931 }
5932 *unicodebuf++ = c;
5933 }
5934}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935
5936#ifdef FEAT_RIGHTLEFT
5937/*
5938 * What is this for? In the case where you are using Win98 or Win2K or later,
5939 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5940 * reverses the string sent to the TextOut... family. This sucks, because we
5941 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5942 * way to tell Windblows not to do this!
5943 *
5944 * The short of it is that this 'RevOut' only gets called if you are running
5945 * one of the new, "improved" MS OSes, and only if you are running in
5946 * 'rightleft' mode. It makes display take *slightly* longer, but not
5947 * noticeably so.
5948 */
5949 static void
K.Takata135e1522022-01-29 15:27:58 +00005950RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 int col,
5952 int row,
5953 UINT foptions,
5954 CONST RECT *pcliprect,
5955 LPCTSTR text,
5956 UINT len,
5957 CONST INT *padding)
5958{
5959 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005961 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005962 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005963 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964}
5965#endif
5966
Bram Moolenaar92467d32017-12-05 13:22:16 +01005967 static void
5968draw_line(
5969 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005970 int y1,
5971 int x2,
5972 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005973 COLORREF color)
5974{
5975#if defined(FEAT_DIRECTX)
5976 if (IS_ENABLE_DIRECTX())
5977 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5978 else
5979#endif
5980 {
5981 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5982 HPEN old_pen = SelectObject(s_hdc, hpen);
5983 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005984 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005985 LineTo(s_hdc, x2, y2);
5986 DeleteObject(SelectObject(s_hdc, old_pen));
5987 }
5988}
5989
5990 static void
5991set_pixel(
5992 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005993 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005994 COLORREF color)
5995{
5996#if defined(FEAT_DIRECTX)
5997 if (IS_ENABLE_DIRECTX())
5998 DWriteContext_SetPixel(s_dwc, x, y, color);
5999 else
6000#endif
6001 SetPixel(s_hdc, x, y, color);
6002}
6003
6004 static void
6005fill_rect(
6006 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006007 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006008 COLORREF color)
6009{
6010#if defined(FEAT_DIRECTX)
6011 if (IS_ENABLE_DIRECTX())
6012 DWriteContext_FillRect(s_dwc, rcp, color);
6013 else
6014#endif
6015 {
6016 HBRUSH hbr2;
6017
6018 if (hbr == NULL)
6019 hbr2 = CreateSolidBrush(color);
6020 else
6021 hbr2 = hbr;
6022 FillRect(s_hdc, rcp, hbr2);
6023 if (hbr == NULL)
6024 DeleteBrush(hbr2);
6025 }
6026}
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 void
6029gui_mch_draw_string(
6030 int row,
6031 int col,
6032 char_u *text,
6033 int len,
6034 int flags)
6035{
6036 static int *padding = NULL;
6037 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 const RECT *pcliprect = NULL;
6039 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 static WCHAR *unicodebuf = NULL;
6041 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006042 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 int y;
6045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 /*
6047 * Italic and bold text seems to have an extra row of pixels at the bottom
6048 * (below where the bottom of the character should be). If we draw the
6049 * characters with a solid background, the top row of pixels in the
6050 * character below will be overwritten. We can fix this by filling in the
6051 * background ourselves, to the correct character proportions, and then
6052 * writing the character in transparent mode. Still have a problem when
6053 * the character is "_", which gets written on to the character below.
6054 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6055 * pixel in their slots, which fixes the problem with the bottom row of
6056 * pixels. We still need this code because otherwise the top row of pixels
6057 * becomes a problem. - webb.
6058 */
6059 static HBRUSH hbr_cache[2] = {NULL, NULL};
6060 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6061 static int brush_lru = 0;
6062 HBRUSH hbr;
6063 RECT rc;
6064
6065 if (!(flags & DRAW_TRANSP))
6066 {
6067 /*
6068 * Clear background first.
6069 * Note: FillRect() excludes right and bottom of rectangle.
6070 */
6071 rc.left = FILL_X(col);
6072 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 if (has_mbyte)
6074 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006075 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006076 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 }
6078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 rc.right = FILL_X(col + len);
6080 rc.bottom = FILL_Y(row + 1);
6081
Bram Moolenaar734a8672019-12-02 22:49:38 +01006082 // Cache the created brush, that saves a lot of time. We need two:
6083 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (gui.currBgColor == brush_color[0])
6085 {
6086 hbr = hbr_cache[0];
6087 brush_lru = 1;
6088 }
6089 else if (gui.currBgColor == brush_color[1])
6090 {
6091 hbr = hbr_cache[1];
6092 brush_lru = 0;
6093 }
6094 else
6095 {
6096 if (hbr_cache[brush_lru] != NULL)
6097 DeleteBrush(hbr_cache[brush_lru]);
6098 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6099 brush_color[brush_lru] = gui.currBgColor;
6100 hbr = hbr_cache[brush_lru];
6101 brush_lru = !brush_lru;
6102 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006103
Bram Moolenaar92467d32017-12-05 13:22:16 +01006104 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 SetBkMode(s_hdc, TRANSPARENT);
6107
6108 /*
6109 * When drawing block cursor, prevent inverted character spilling
6110 * over character cell (can happen with bold/italic)
6111 */
6112 if (flags & DRAW_CURSOR)
6113 {
6114 pcliprect = &rc;
6115 foptions = ETO_CLIPPED;
6116 }
6117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 SetTextColor(s_hdc, gui.currFgColor);
6119 SelectFont(s_hdc, gui.currFont);
6120
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006121#ifdef FEAT_DIRECTX
6122 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006123 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006124#endif
6125
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6127 {
K.Takata135e1522022-01-29 15:27:58 +00006128 int i;
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 vim_free(padding);
6131 pad_size = Columns;
6132
Bram Moolenaar734a8672019-12-02 22:49:38 +01006133 // Don't give an out-of-memory message here, it would call us
6134 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006135 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (padding != NULL)
6137 for (i = 0; i < pad_size; i++)
6138 padding[i] = gui.char_width;
6139 }
6140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 /*
6142 * We have to provide the padding argument because italic and bold versions
6143 * of fixed-width fonts are often one pixel or so wider than their normal
6144 * versions.
6145 * No check for DRAW_BOLD, Windows will have done it already.
6146 */
6147
Bram Moolenaar734a8672019-12-02 22:49:38 +01006148 // Check if there are any UTF-8 characters. If not, use normal text
6149 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (enc_utf8)
6151 for (n = 0; n < len; ++n)
6152 if (text[n] >= 0x80)
6153 break;
6154
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006155#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006156 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6157 // required that unicode drawing routine, currently. So this forces it
6158 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006159 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006160 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006161#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006162
Bram Moolenaar734a8672019-12-02 22:49:38 +01006163 // Check if the Unicode buffer exists and is big enough. Create it
6164 // with the same length as the multi-byte string, the number of wide
6165 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006166 if ((enc_utf8
6167 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6168 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 && (unicodebuf == NULL || len > unibuflen))
6170 {
6171 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006172 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173
6174 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006175 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177 unibuflen = len;
6178 }
6179
6180 if (enc_utf8 && n < len && unicodebuf != NULL)
6181 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006182 // Output UTF-8 characters. Composing characters should be
6183 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006184 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006185 int wlen; // string length in words
6186 int clen; // string length in characters
6187 int cells; // cell width of string up to composing char
6188 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006189 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006191 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006192 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006194 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006196 c = utf_ptr2char(text + i);
6197 if (c >= 0x10000)
6198 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006199 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006200 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6201 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006202 }
6203 else
6204 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006205 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006206 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006207
6208 if (utf_iscomposing(c))
6209 cw = 0;
6210 else
6211 {
6212 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006213 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006214 cw = 1;
6215 }
6216
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (unicodepdy != NULL)
6218 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006219 // Use unicodepdy to make characters fit as we expect, even
6220 // when the font uses different widths (e.g., bold character
6221 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006222 if (c >= 0x10000)
6223 {
6224 unicodepdy[wlen - 2] = cw * gui.char_width;
6225 unicodepdy[wlen - 1] = 0;
6226 }
6227 else
6228 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
6230 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006231 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006232 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006234#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006235 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006236 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006237 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006238 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006239 TEXT_X(col), TEXT_Y(row),
6240 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006241 gui.char_width, gui.currFgColor,
6242 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006243 }
6244 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006245#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006246 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6247 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006248 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006250 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 // If we want to display codepage data, and the current CP is not the
6253 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 if (unicodebuf != NULL)
6255 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006256 if (enc_latin9)
6257 latin9_to_ucs(text, len, unicodebuf);
6258 else
6259 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 MB_PRECOMPOSED,
6261 (char *)text, len,
6262 (LPWSTR)unicodebuf, unibuflen);
6263 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006264 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006265 // Use unicodepdy to make characters fit as we expect, even
6266 // when the font uses different widths (e.g., bold character
6267 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006268 if (unicodepdy != NULL)
6269 {
6270 int i;
6271 int cw;
6272
6273 for (i = 0; i < len; ++i)
6274 {
6275 cw = utf_char2cells(unicodebuf[i]);
6276 if (cw > 2)
6277 cw = 1;
6278 unicodepdy[i] = cw * gui.char_width;
6279 }
6280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006282 foptions, pcliprect, unicodebuf, len, unicodepdy);
6283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285 }
6286 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 {
6288#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006289 // Windows will mess up RL text, so we have to draw it character by
6290 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006291 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6293 foptions, pcliprect, (char *)text, len, padding);
6294 else
6295#endif
6296 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6297 foptions, pcliprect, (char *)text, len, padding);
6298 }
6299
Bram Moolenaar734a8672019-12-02 22:49:38 +01006300 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (flags & DRAW_UNDERL)
6302 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006303 // When p_linespace is 0, overwrite the bottom row of pixels.
6304 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (p_linespace > 1)
6307 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006308 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006310
Bram Moolenaar734a8672019-12-02 22:49:38 +01006311 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006312 if (flags & DRAW_STRIKE)
6313 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006314 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006315 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006316 }
6317
Bram Moolenaar734a8672019-12-02 22:49:38 +01006318 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006319 if (flags & DRAW_UNDERC)
6320 {
6321 int x;
6322 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006323 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006324
6325 y = FILL_Y(row + 1) - 1;
6326 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6327 {
6328 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006329 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006330 }
6331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
6334
6335/*
6336 * Output routines.
6337 */
6338
Bram Moolenaar734a8672019-12-02 22:49:38 +01006339/*
6340 * Flush any output to the screen
6341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 void
6343gui_mch_flush(void)
6344{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006345#if defined(FEAT_DIRECTX)
6346 if (IS_ENABLE_DIRECTX())
6347 DWriteContext_Flush(s_dwc);
6348#endif
6349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 GdiFlush();
6351}
6352
6353 static void
6354clear_rect(RECT *rcp)
6355{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006356 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359
Bram Moolenaarc716c302006-01-21 22:12:51 +00006360 void
6361gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6362{
6363 RECT workarea_rect;
6364
6365 get_work_area(&workarea_rect);
6366
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006367 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006368 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6369 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006370
Bram Moolenaar734a8672019-12-02 22:49:38 +01006371 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6372 // the menubar for MSwin, we subtract it from the screen height, so that
6373 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006374 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006375 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6376 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6377 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6378 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006379}
6380
6381
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382#if defined(FEAT_MENU) || defined(PROTO)
6383/*
6384 * Add a sub menu to the menu bar.
6385 */
6386 void
6387gui_mch_add_menu(
6388 vimmenu_T *menu,
6389 int pos)
6390{
6391 vimmenu_T *parent = menu->parent;
6392
6393 menu->submenu_id = CreatePopupMenu();
6394 menu->id = s_menu_id++;
6395
6396 if (menu_is_menubar(menu->name))
6397 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006398 WCHAR *wn;
6399 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006401 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006402 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006403 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006405 infow.cbSize = sizeof(infow);
6406 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6407 | MIIM_SUBMENU;
6408 infow.dwItemData = (long_u)menu;
6409 infow.wID = menu->id;
6410 infow.fType = MFT_STRING;
6411 infow.dwTypeData = wn;
6412 infow.cch = (UINT)wcslen(wn);
6413 infow.hSubMenu = menu->submenu_id;
6414 InsertMenuItemW((parent == NULL)
6415 ? s_menuBar : parent->submenu_id,
6416 (UINT)pos, TRUE, &infow);
6417 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419
Bram Moolenaar734a8672019-12-02 22:49:38 +01006420 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (parent == NULL)
6422 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006423# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 else if (IsWindow(parent->tearoff_handle))
6425 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006426# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
6428
6429 void
6430gui_mch_show_popupmenu(vimmenu_T *menu)
6431{
6432 POINT mp;
6433
K.Takata45f9cfb2022-01-21 11:11:00 +00006434 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6436}
6437
6438 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006439gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 vimmenu_T *menu = gui_find_menu(path_name);
6442
6443 if (menu != NULL)
6444 {
6445 POINT p;
6446
Bram Moolenaar734a8672019-12-02 22:49:38 +01006447 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006449 if (mouse_pos)
6450 {
6451 int mx, my;
6452
6453 gui_mch_getmouse(&mx, &my);
6454 p.x += mx;
6455 p.y += my;
6456 }
6457 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006459 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6461 }
6462 msg_scroll = FALSE;
6463 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6464 }
6465}
6466
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006467# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468/*
6469 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6470 * create it as a pseudo-"tearoff menu".
6471 */
6472 void
6473gui_make_tearoff(char_u *path_name)
6474{
6475 vimmenu_T *menu = gui_find_menu(path_name);
6476
Bram Moolenaar734a8672019-12-02 22:49:38 +01006477 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (menu != NULL)
6479 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6480}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006481# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483/*
6484 * Add a menu item to a menu
6485 */
6486 void
6487gui_mch_add_menu_item(
6488 vimmenu_T *menu,
6489 int idx)
6490{
6491 vimmenu_T *parent = menu->parent;
6492
6493 menu->id = s_menu_id++;
6494 menu->submenu_id = NULL;
6495
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006496# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6498 {
6499 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6500 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6501 }
6502 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006503# endif
6504# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (menu_is_toolbar(parent->name))
6506 {
6507 TBBUTTON newtb;
6508
Bram Moolenaara80faa82020-04-12 19:37:17 +02006509 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (menu_is_separator(menu->name))
6511 {
6512 newtb.iBitmap = 0;
6513 newtb.fsStyle = TBSTYLE_SEP;
6514 }
6515 else
6516 {
6517 newtb.iBitmap = get_toolbar_bitmap(menu);
6518 newtb.fsStyle = TBSTYLE_BUTTON;
6519 }
6520 newtb.idCommand = menu->id;
6521 newtb.fsState = TBSTATE_ENABLED;
6522 newtb.iString = 0;
6523 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6524 (LPARAM)&newtb);
6525 menu->submenu_id = (HMENU)-1;
6526 }
6527 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006530 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006532 wn = enc_to_utf16(menu->name, NULL);
6533 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006535 InsertMenuW(parent->submenu_id, (UINT)idx,
6536 (menu_is_separator(menu->name)
6537 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6538 (UINT)menu->id, wn);
6539 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006541# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (IsWindow(parent->tearoff_handle))
6543 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546}
6547
6548/*
6549 * Destroy the machine specific menu widget.
6550 */
6551 void
6552gui_mch_destroy_menu(vimmenu_T *menu)
6553{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006554# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 /*
6556 * is this a toolbar button?
6557 */
6558 if (menu->submenu_id == (HMENU)-1)
6559 {
6560 int iButton;
6561
6562 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6563 (WPARAM)menu->id, 0);
6564 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6565 }
6566 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 {
6569 if (menu->parent != NULL
6570 && menu_is_popup(menu->parent->dname)
6571 && menu->parent->submenu_id != NULL)
6572 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6573 else
6574 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6575 if (menu->submenu_id != NULL)
6576 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006577# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (IsWindow(menu->tearoff_handle))
6579 DestroyWindow(menu->tearoff_handle);
6580 if (menu->parent != NULL
6581 && menu->parent->children != NULL
6582 && IsWindow(menu->parent->tearoff_handle))
6583 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006584 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 menu->modes = 0;
6586 rebuild_tearoff(menu->parent);
6587 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590}
6591
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006592# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 static void
6594rebuild_tearoff(vimmenu_T *menu)
6595{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006596 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 char_u tbuf[128];
6598 RECT trect;
6599 RECT rct;
6600 RECT roct;
6601 int x, y;
6602
6603 HWND thwnd = menu->tearoff_handle;
6604
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006605 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 if (GetWindowRect(thwnd, &trect)
6607 && GetWindowRect(s_hwnd, &rct)
6608 && GetClientRect(s_hwnd, &roct))
6609 {
6610 x = trect.left - rct.left;
6611 y = (trect.top - rct.bottom + roct.bottom);
6612 }
6613 else
6614 {
6615 x = y = 0xffffL;
6616 }
6617 DestroyWindow(thwnd);
6618 if (menu->children != NULL)
6619 {
6620 gui_mch_tearoff(tbuf, menu, x, y);
6621 if (IsWindow(menu->tearoff_handle))
6622 (void) SetWindowPos(menu->tearoff_handle,
6623 NULL,
6624 (int)trect.left,
6625 (int)trect.top,
6626 0, 0,
6627 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6628 }
6629}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006630# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632/*
6633 * Make a menu either grey or not grey.
6634 */
6635 void
6636gui_mch_menu_grey(
6637 vimmenu_T *menu,
6638 int grey)
6639{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006640# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 /*
6642 * is this a toolbar button?
6643 */
6644 if (menu->submenu_id == (HMENU)-1)
6645 {
6646 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006647 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
6649 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006650# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006651 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6652 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006654# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6656 {
6657 WORD menuID;
6658 HWND menuHandle;
6659
6660 /*
6661 * A tearoff button has changed state.
6662 */
6663 if (menu->children == NULL)
6664 menuID = (WORD)(menu->id);
6665 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006666 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6668 if (menuHandle)
6669 EnableWindow(menuHandle, !grey);
6670
6671 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673}
6674
Bram Moolenaar734a8672019-12-02 22:49:38 +01006675#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676
6677
Bram Moolenaar734a8672019-12-02 22:49:38 +01006678// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006681#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6684/*
6685 * stuff for dialogs
6686 */
6687
6688/*
6689 * The callback routine used by all the dialogs. Very simple. First,
6690 * acknowledges the INITDIALOG message so that Windows knows to do standard
6691 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6692 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6693 * number.
6694 */
6695 static LRESULT CALLBACK
6696dialog_callback(
6697 HWND hwnd,
6698 UINT message,
6699 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006700 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 if (message == WM_INITDIALOG)
6703 {
6704 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006705 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 (void)SetFocus(hwnd);
6707 if (dialog_default_button > IDCANCEL)
6708 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006709 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006710 // We don't have a default, set focus on another element of the
6711 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006712 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 return FALSE;
6714 }
6715
6716 if (message == WM_COMMAND)
6717 {
6718 int button = LOWORD(wParam);
6719
Bram Moolenaar734a8672019-12-02 22:49:38 +01006720 // Don't end the dialog if something was selected that was
6721 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (button >= DLG_NONBUTTON_CONTROL)
6723 return TRUE;
6724
Bram Moolenaar734a8672019-12-02 22:49:38 +01006725 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006727 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006728 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006729 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006730
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006731 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6732 p = utf16_to_enc(wp, NULL);
6733 vim_strncpy(s_textfield, p, IOSIZE);
6734 vim_free(p);
6735 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 /*
6739 * Need to check for IDOK because if the user just hits Return to
6740 * accept the default value, some reason this is what we get.
6741 */
6742 if (button == IDOK)
6743 {
6744 if (dialog_default_button > IDCANCEL)
6745 EndDialog(hwnd, dialog_default_button);
6746 }
6747 else
6748 EndDialog(hwnd, button - IDCANCEL);
6749 return TRUE;
6750 }
6751
6752 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6753 {
6754 EndDialog(hwnd, 0);
6755 return TRUE;
6756 }
6757 return FALSE;
6758}
6759
6760/*
6761 * Create a dialog dynamically from the parameter strings.
6762 * type = type of dialog (question, alert, etc.)
6763 * title = dialog title. may be NULL for default title.
6764 * message = text to display. Dialog sizes to accommodate it.
6765 * buttons = '\n' separated list of button captions, default first.
6766 * dfltbutton = number of default button.
6767 *
6768 * This routine returns 1 if the first button is pressed,
6769 * 2 for the second, etc.
6770 *
6771 * 0 indicates Esc was pressed.
6772 * -1 for unexpected error
6773 *
6774 * If stubbing out this fn, return 1.
6775 */
6776
Bram Moolenaar734a8672019-12-02 22:49:38 +01006777static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 "IDR_VIM",
6780 "IDR_VIM_ERROR",
6781 "IDR_VIM_ALERT",
6782 "IDR_VIM_INFO",
6783 "IDR_VIM_QUESTION"
6784};
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 int
6787gui_mch_dialog(
6788 int type,
6789 char_u *title,
6790 char_u *message,
6791 char_u *buttons,
6792 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006793 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006794 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795{
6796 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006797 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 int numButtons;
6799 int *buttonWidths, *buttonPositions;
6800 int buttonYpos;
6801 int nchar, i;
6802 DWORD lStyle;
6803 int dlgwidth = 0;
6804 int dlgheight;
6805 int editboxheight;
6806 int horizWidth = 0;
6807 int msgheight;
6808 char_u *pstart;
6809 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006810 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 char_u *tbuffer;
6812 RECT rect;
6813 HWND hwnd;
6814 HDC hdc;
6815 HFONT font, oldFont;
6816 TEXTMETRIC fontInfo;
6817 int fontHeight;
6818 int textWidth, minButtonWidth, messageWidth;
6819 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006820 int maxDialogHeight;
6821 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 int vertical;
6823 int dlgPaddingX;
6824 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006825# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006826 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006828# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006829 garray_T ga;
6830 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006831 int dlg_icon_width;
6832 int dlg_icon_height;
6833 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006835# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006836 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006837# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006838 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006839# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006840 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006841 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
Bram Moolenaar748bf032005-02-02 23:04:36 +00006844 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006845 {
6846 load_dpi_func();
6847 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006848 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006849 }
6850 else
6851 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 if ((type < 0) || (type > VIM_LAST_TYPE))
6854 type = 0;
6855
Bram Moolenaar734a8672019-12-02 22:49:38 +01006856 // allocate some memory for dialog template
6857 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006858 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006859 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860
6861 if (p == NULL)
6862 return -1;
6863
6864 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006865 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6867 * const.
6868 */
6869 tbuffer = vim_strsave(buttons);
6870 if (tbuffer == NULL)
6871 return -1;
6872
Bram Moolenaar734a8672019-12-02 22:49:38 +01006873 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 numButtons = 1;
6877 for (i = 0; tbuffer[i] != '\0'; i++)
6878 {
6879 if (tbuffer[i] == DLG_BUTTON_SEP)
6880 numButtons++;
6881 }
6882 if (dfltbutton >= numButtons)
6883 dfltbutton = -1;
6884
Bram Moolenaar734a8672019-12-02 22:49:38 +01006885 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006886 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (buttonWidths == NULL)
6888 return -1;
6889
Bram Moolenaar734a8672019-12-02 22:49:38 +01006890 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006891 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (buttonPositions == NULL)
6893 return -1;
6894
6895 /*
6896 * Calculate how big the dialog must be.
6897 */
6898 hwnd = GetDesktopWindow();
6899 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006900# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6902 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006903 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 use_lfSysmenu = TRUE;
6905 }
6906 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006907# endif
K.Takatad1c58992022-01-23 12:31:57 +00006908 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6909 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6910
6911 oldFont = SelectFont(hdc, font);
6912 dlgPaddingX = DLG_PADDING_X;
6913 dlgPaddingY = DLG_PADDING_Y;
6914
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 GetTextMetrics(hdc, &fontInfo);
6916 fontHeight = fontInfo.tmHeight;
6917
Bram Moolenaar734a8672019-12-02 22:49:38 +01006918 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006919 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
Bram Moolenaar734a8672019-12-02 22:49:38 +01006921 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006922 if (s_hwnd == NULL)
6923 {
6924 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
Bram Moolenaar734a8672019-12-02 22:49:38 +01006926 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006927 get_work_area(&workarea_rect);
6928 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006929 if (maxDialogWidth > adjust_by_system_dpi(600))
6930 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006931 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006932 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006933 }
6934 else
6935 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006936 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006937 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006938 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006939 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6940 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6941 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6942 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006943
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006944 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006945 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6946 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6947 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6948 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6949 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006950 }
6951
Bram Moolenaar734a8672019-12-02 22:49:38 +01006952 // Set dlgwidth to width of message.
6953 // Copy the message into "ga", changing NL to CR-NL and inserting line
6954 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 pstart = message;
6956 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 msgheight = 0;
6958 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 do
6960 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006961 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006962
Bram Moolenaar734a8672019-12-02 22:49:38 +01006963 // Need to figure out where to break the string. The system does it
6964 // at a word boundary, which would mean we can't compute the number of
6965 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006966 textWidth = 0;
6967 last_white = NULL;
6968 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006970 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006971 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006972 && textWidth > maxDialogWidth * 3 / 4)
6973 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006974 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006975 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006976 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006977 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006978 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006979 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006980 textWidth = 0;
6981
6982 if (last_white != NULL)
6983 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006985 if (pend > last_white)
6986 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006987 pend = last_white + 1;
6988 last_white = NULL;
6989 }
6990 ga_append(&ga, '\r');
6991 ga_append(&ga, '\n');
6992 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006993 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006994
6995 while (--l >= 0)
6996 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006997 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007000
7001 ga_append(&ga, '\r');
7002 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 pstart = pend + 1;
7004 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007005
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007006 if (ga.ga_data != NULL)
7007 message = ga.ga_data;
7008
Bram Moolenaar734a8672019-12-02 22:49:38 +01007009 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007010
K.Takatac81e9bf2022-01-16 14:15:49 +00007011 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7012 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013
K.Takatac81e9bf2022-01-16 14:15:49 +00007014 // Add width of icon to dlgwidth, and some space
7015 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7016 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7017
7018 if (msgheight < dlg_icon_height)
7019 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
7021 /*
7022 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007023 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007025 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 if (!vertical)
7027 {
7028 // Place buttons horizontally if they fit.
7029 horizWidth = dlgPaddingX;
7030 pstart = tbuffer;
7031 i = 0;
7032 do
7033 {
7034 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7035 if (pend == NULL)
7036 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007037 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (textWidth < minButtonWidth)
7039 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007040 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 buttonWidths[i] = textWidth;
7042 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007043 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 pstart = pend + 1;
7045 } while (*pend != NUL);
7046
7047 if (horizWidth > maxDialogWidth)
7048 vertical = TRUE; // Too wide to fit on the screen.
7049 else if (horizWidth > dlgwidth)
7050 dlgwidth = horizWidth;
7051 }
7052
7053 if (vertical)
7054 {
7055 // Stack buttons vertically.
7056 pstart = tbuffer;
7057 do
7058 {
7059 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7060 if (pend == NULL)
7061 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007062 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007063 textWidth += dlgPaddingX; // Padding within button
7064 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 if (textWidth > dlgwidth)
7066 dlgwidth = textWidth;
7067 pstart = pend + 1;
7068 } while (*pend != NUL);
7069 }
7070
7071 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007072 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar734a8672019-12-02 22:49:38 +01007074 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007075 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
7077 add_long(lStyle);
7078 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007079 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 add_word(0); // NumberOfItems(will change later)
7081 add_word(10); // x
7082 add_word(10); // y
7083 add_word(PixelToDialogX(dlgwidth)); // cx
7084
7085 // Dialog height.
7086 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007087 dlgheight = msgheight + 2 * dlgPaddingY
7088 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 else
7090 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7091
7092 // Dialog needs to be taller if contains an edit box.
7093 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7094 if (textfield != NULL)
7095 dlgheight += editboxheight;
7096
Bram Moolenaar734a8672019-12-02 22:49:38 +01007097 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007098 if (dlgheight > maxDialogHeight)
7099 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007100 msgheight = msgheight - (dlgheight - maxDialogHeight);
7101 dlgheight = maxDialogHeight;
7102 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007103 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007104 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007105 }
7106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 add_word(PixelToDialogY(dlgheight));
7108
7109 add_word(0); // Menu
7110 add_word(0); // Class
7111
Bram Moolenaar734a8672019-12-02 22:49:38 +01007112 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007113 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7114 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 p += nchar;
7116
K.Takatad1c58992022-01-23 12:31:57 +00007117 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007118# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007119 if (use_lfSysmenu)
7120 {
7121 // point size
7122 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7123 GetDeviceCaps(hdc, LOGPIXELSY));
7124 wcscpy(p, lfSysmenu.lfFaceName);
7125 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
K.Takatad1c58992022-01-23 12:31:57 +00007127 else
7128# endif
7129 {
7130 *p++ = DLG_FONT_POINT_SIZE; // point size
7131 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7132 }
7133 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135 buttonYpos = msgheight + 2 * dlgPaddingY;
7136
7137 if (textfield != NULL)
7138 buttonYpos += editboxheight;
7139
7140 pstart = tbuffer;
7141 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007142 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 for (i = 0; i < numButtons; i++)
7144 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007145 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 for ( pend = pstart;
7147 *pend && (*pend != DLG_BUTTON_SEP);
7148 pend++)
7149 ;
7150
7151 if (*pend)
7152 *pend = '\0';
7153
7154 /*
7155 * old NOTE:
7156 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7157 * the focus to the first tab-able button and in so doing makes that
7158 * the default!! Grrr. Workaround: Make the default button the only
7159 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7160 * he/she can use arrow keys.
7161 *
7162 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007163 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 * dialog. Also needed for when the textfield is the default control.
7165 * It appears to work now (perhaps not on Win95?).
7166 */
7167 if (vertical)
7168 {
7169 p = add_dialog_element(p,
7170 (i == dfltbutton
7171 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7172 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007173 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 + 2 * fontHeight * i),
7175 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7176 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007177 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 }
7179 else
7180 {
7181 p = add_dialog_element(p,
7182 (i == dfltbutton
7183 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7184 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007185 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 PixelToDialogX(buttonWidths[i]),
7187 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007188 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007190 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192 *pnumitems += numButtons;
7193
Bram Moolenaar734a8672019-12-02 22:49:38 +01007194 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 p = add_dialog_element(p, SS_ICON,
7196 PixelToDialogX(dlgPaddingX),
7197 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007198 PixelToDialogX(dlg_icon_width),
7199 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7201 dlg_icons[type]);
7202
Bram Moolenaar734a8672019-12-02 22:49:38 +01007203 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007204 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007205 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007206 PixelToDialogY(dlgPaddingY),
7207 (WORD)(PixelToDialogX(messageWidth) + 1),
7208 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007209 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaar734a8672019-12-02 22:49:38 +01007211 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (textfield != NULL)
7213 {
7214 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7215 PixelToDialogX(2 * dlgPaddingX),
7216 PixelToDialogY(2 * dlgPaddingY + msgheight),
7217 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7218 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007219 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 *pnumitems += 1;
7221 }
7222
7223 *pnumitems += 2;
7224
7225 SelectFont(hdc, oldFont);
7226 DeleteObject(font);
7227 ReleaseDC(hwnd, hdc);
7228
Bram Moolenaar734a8672019-12-02 22:49:38 +01007229 // Let the dialog_callback() function know which button to make default
7230 // If we have an edit box, make that the default. We also need to tell
7231 // dialog_callback() if this dialog contains an edit box or not. We do
7232 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 if (textfield != NULL)
7234 {
7235 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7236 s_textfield = textfield;
7237 }
7238 else
7239 {
7240 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7241 s_textfield = NULL;
7242 }
7243
Bram Moolenaar734a8672019-12-02 22:49:38 +01007244 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007246 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 (LPDLGTEMPLATE)pdlgtemplate,
7248 s_hwnd,
7249 (DLGPROC)dialog_callback);
7250
7251 LocalFree(LocalHandle(pdlgtemplate));
7252 vim_free(tbuffer);
7253 vim_free(buttonWidths);
7254 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007255 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256
Bram Moolenaar734a8672019-12-02 22:49:38 +01007257 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 (void)SetFocus(s_hwnd);
7259
7260 return nchar;
7261}
7262
Bram Moolenaar734a8672019-12-02 22:49:38 +01007263#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007264
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265/*
7266 * Put a simple element (basic class) onto a dialog template in memory.
7267 * return a pointer to where the next item should be added.
7268 *
7269 * parameters:
7270 * lStyle = additional style flags
7271 * (be careful, NT3.51 & Win32s will ignore the new ones)
7272 * x,y = x & y positions IN DIALOG UNITS
7273 * w,h = width and height IN DIALOG UNITS
7274 * Id = ID used in messages
7275 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7276 * caption = usually text or resource name
7277 *
7278 * TODO: use the length information noted here to enable the dialog creation
7279 * routines to work out more exactly how much memory they need to alloc.
7280 */
7281 static PWORD
7282add_dialog_element(
7283 PWORD p,
7284 DWORD lStyle,
7285 WORD x,
7286 WORD y,
7287 WORD w,
7288 WORD h,
7289 WORD Id,
7290 WORD clss,
7291 const char *caption)
7292{
7293 int nchar;
7294
Bram Moolenaar734a8672019-12-02 22:49:38 +01007295 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7297 *p++ = LOWORD(lStyle);
7298 *p++ = HIWORD(lStyle);
7299 *p++ = 0; // LOWORD (lExtendedStyle)
7300 *p++ = 0; // HIWORD (lExtendedStyle)
7301 *p++ = x;
7302 *p++ = y;
7303 *p++ = w;
7304 *p++ = h;
7305 *p++ = Id; //9 or 10 words in all
7306
7307 *p++ = (WORD)0xffff;
7308 *p++ = clss; //2 more here
7309
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007310 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 p += nchar;
7312
7313 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7314
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007315 return p; // total = 15 + strlen(caption) words
7316 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317}
7318
7319
7320/*
7321 * Helper routine. Take an input pointer, return closest pointer that is
7322 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7323 */
7324 static LPWORD
7325lpwAlign(
7326 LPWORD lpIn)
7327{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007328 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007330 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 ul += 3;
7332 ul >>= 2;
7333 ul <<= 2;
7334 return (LPWORD)ul;
7335}
7336
7337/*
7338 * Helper routine. Takes second parameter as Ansi string, copies it to first
7339 * parameter as wide character (16-bits / char) string, and returns integer
7340 * number of wide characters (words) in string (including the trailing wide
7341 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007342 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7343 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 static int
7345nCopyAnsiToWideChar(
7346 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007347 LPSTR lpAnsiIn,
7348 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
7350 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007351 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 int i;
7353 WCHAR *wn;
7354
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007355 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007357 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007358 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (wn != NULL)
7360 {
7361 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007362 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 vim_free(wn);
7364 }
7365 }
7366 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007367 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 nChar = MultiByteToWideChar(
7369 enc_codepage > 0 ? enc_codepage : CP_ACP,
7370 MB_PRECOMPOSED,
7371 lpAnsiIn, len,
7372 lpWCStr, len);
7373 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007374 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
7377 return nChar;
7378}
7379
7380
7381#ifdef FEAT_TEAROFF
7382/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007383 * Lookup menu handle from "menu_id".
7384 */
7385 static HMENU
7386tearoff_lookup_menuhandle(
7387 vimmenu_T *menu,
7388 WORD menu_id)
7389{
7390 for ( ; menu != NULL; menu = menu->next)
7391 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007392 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007393 continue;
7394 if (menu_is_separator(menu->dname))
7395 continue;
7396 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7397 return menu->submenu_id;
7398 }
7399 return NULL;
7400}
7401
7402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 * The callback function for all the modeless dialogs that make up the
7404 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7405 * thinking its menus have been clicked), and go away when closed.
7406 */
7407 static LRESULT CALLBACK
7408tearoff_callback(
7409 HWND hwnd,
7410 UINT message,
7411 WPARAM wParam,
7412 LPARAM lParam)
7413{
7414 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007415 {
7416 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419
Bram Moolenaar734a8672019-12-02 22:49:38 +01007420 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 HandleMouseHide(message, lParam);
7422
7423 if (message == WM_COMMAND)
7424 {
7425 if ((WORD)(LOWORD(wParam)) & 0x8000)
7426 {
7427 POINT mp;
7428 RECT rect;
7429
7430 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7431 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007432 vimmenu_T *menu;
7433
7434 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007436 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7438 (int)rect.right - 8,
7439 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007440 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 s_hwnd,
7442 NULL);
7443 /*
7444 * NOTE: The pop-up menu can eat the mouse up event.
7445 * We deal with this in normal.c.
7446 */
7447 }
7448 }
7449 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007450 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7452 /*
7453 * Give main window the focus back: this is so after
7454 * choosing a tearoff button you can start typing again
7455 * straight away.
7456 */
7457 (void)SetFocus(s_hwnd);
7458 return TRUE;
7459 }
7460 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7461 {
7462 DestroyWindow(hwnd);
7463 return TRUE;
7464 }
7465
Bram Moolenaar734a8672019-12-02 22:49:38 +01007466 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 if (message == WM_EXITSIZEMOVE)
7468 (void)SetActiveWindow(s_hwnd);
7469
7470 return FALSE;
7471}
7472#endif
7473
7474
7475/*
K.Takatad1c58992022-01-23 12:31:57 +00007476 * Computes the dialog base units based on the current dialog font.
7477 * We don't use the GetDialogBaseUnits() API, because we don't use the
7478 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 */
7480 static void
7481get_dialog_font_metrics(void)
7482{
7483 HDC hdc;
7484 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 SIZE size;
7486#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007487 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488#endif
7489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007491 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007492 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#endif
7495 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007496 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
K.Takatad1c58992022-01-23 12:31:57 +00007498 hdc = GetDC(s_hwnd);
7499 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007500 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007501 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502
K.Takataabe628e2022-01-23 16:25:17 +00007503 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007504 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505}
7506
7507#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7508/*
7509 * Create a pseudo-"tearoff menu" based on the child
7510 * items of a given menu pointer.
7511 */
7512 static void
7513gui_mch_tearoff(
7514 char_u *title,
7515 vimmenu_T *menu,
7516 int initX,
7517 int initY)
7518{
7519 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7520 int template_len;
7521 int nchar, textWidth, submenuWidth;
7522 DWORD lStyle;
7523 DWORD lExtendedStyle;
7524 WORD dlgwidth;
7525 WORD menuID;
7526 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007527 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 vimmenu_T *the_menu = menu;
7529 HWND hwnd;
7530 HDC hdc;
7531 HFONT font, oldFont;
7532 int col, spaceWidth, len;
7533 int columnWidths[2];
7534 char_u *label, *text;
7535 int acLen = 0;
7536 int nameLen;
7537 int padding0, padding1, padding2 = 0;
7538 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007539 int x;
7540 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007541# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007542 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545
7546 /*
7547 * If this menu is already torn off, move it to the mouse position.
7548 */
7549 if (IsWindow(menu->tearoff_handle))
7550 {
7551 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007552 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
7554 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7555 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7556 }
7557 return;
7558 }
7559
7560 /*
7561 * Create a new tearoff.
7562 */
7563 if (*title == MNU_HIDDEN_CHAR)
7564 title++;
7565
Bram Moolenaar734a8672019-12-02 22:49:38 +01007566 // Allocate memory to store the dialog template. It's made bigger when
7567 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 template_len = DLG_ALLOC_SIZE;
7569 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7570 if (p == NULL)
7571 return;
7572
7573 hwnd = GetDesktopWindow();
7574 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007575# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7577 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007578 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 use_lfSysmenu = TRUE;
7580 }
7581 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007582# endif
K.Takatad1c58992022-01-23 12:31:57 +00007583 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7584 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7585
7586 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
Bram Moolenaar734a8672019-12-02 22:49:38 +01007588 // Calculate width of a single space. Used for padding columns to the
7589 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007590 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
Bram Moolenaar734a8672019-12-02 22:49:38 +01007592 // Figure out max width of the text column, the accelerator column and the
7593 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 submenuWidth = 0;
7595 for (col = 0; col < 2; col++)
7596 {
7597 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007598 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007600 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 text = (col == 0) ? pmenu->dname : pmenu->actext;
7602 if (text != NULL && *text != NUL)
7603 {
7604 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7605 if (textWidth > columnWidths[col])
7606 columnWidths[col] = textWidth;
7607 }
7608 if (pmenu->children != NULL)
7609 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7610 }
7611 }
7612 if (columnWidths[1] == 0)
7613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007614 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (submenuWidth != 0)
7616 columnWidths[0] += submenuWidth;
7617 else
7618 columnWidths[0] += spaceWidth;
7619 }
7620 else
7621 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007622 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7624 columnWidths[1] += submenuWidth;
7625 }
7626
7627 /*
7628 * Now find the total width of our 'menu'.
7629 */
7630 textWidth = columnWidths[0] + columnWidths[1];
7631 if (submenuWidth != 0)
7632 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007633 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7635 textWidth += submenuWidth;
7636 }
7637 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7638 if (textWidth > dlgwidth)
7639 dlgwidth = textWidth;
7640 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7641
Bram Moolenaar734a8672019-12-02 22:49:38 +01007642 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007643 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
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
K.Takatad1c58992022-01-23 12:31:57 +00007673 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007674# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007675 if (use_lfSysmenu)
7676 {
7677 // point size
7678 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7679 GetDeviceCaps(hdc, LOGPIXELSY));
7680 wcscpy(p, lfSysmenu.lfFaceName);
7681 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 }
K.Takatad1c58992022-01-23 12:31:57 +00007683 else
7684# endif
7685 {
7686 *p++ = DLG_FONT_POINT_SIZE; // point size
7687 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7688 }
7689 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690
7691 /*
7692 * Loop over all the items in the menu.
7693 * But skip over the tearbar.
7694 */
7695 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7696 menu = menu->children->next;
7697 else
7698 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007699 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 for ( ; menu != NULL; menu = menu->next)
7701 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007702 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 continue;
7704 if (menu_is_separator(menu->dname))
7705 {
7706 sepPadding += 3;
7707 continue;
7708 }
7709
Bram Moolenaar734a8672019-12-02 22:49:38 +01007710 // Check if there still is plenty of room in the template. Make it
7711 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7713 {
7714 WORD *newp;
7715
7716 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7717 if (newp != NULL)
7718 {
7719 template_len += 4096;
7720 mch_memmove(newp, pdlgtemplate,
7721 (char *)p - (char *)pdlgtemplate);
7722 p = newp + (p - pdlgtemplate);
7723 pnumitems = newp + (pnumitems - pdlgtemplate);
7724 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7725 LocalFree(LocalHandle(pdlgtemplate));
7726 pdlgtemplate = newp;
7727 }
7728 }
7729
Bram Moolenaar734a8672019-12-02 22:49:38 +01007730 // Figure out minimal length of this menu label. Use "name" for the
7731 // actual text, "dname" for estimating the displayed size. "name"
7732 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 len = nameLen = (int)STRLEN(menu->name);
7734 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7735 (int)STRLEN(menu->dname))) / spaceWidth;
7736 len += padding0;
7737
7738 if (menu->actext != NULL)
7739 {
7740 acLen = (int)STRLEN(menu->actext);
7741 len += acLen;
7742 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7743 }
7744 else
7745 textWidth = 0;
7746 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7747 len += padding1;
7748
7749 if (menu->children == NULL)
7750 {
7751 padding2 = submenuWidth / spaceWidth;
7752 len += padding2;
7753 menuID = (WORD)(menu->id);
7754 }
7755 else
7756 {
7757 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007758 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
7760
Bram Moolenaar734a8672019-12-02 22:49:38 +01007761 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007762 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (label == NULL)
7764 break;
7765
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007766 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007767 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007769 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 while (padding0-- > 0)
7771 *text++ = ' ';
7772 if (menu->actext != NULL)
7773 {
7774 STRNCPY(text, menu->actext, acLen);
7775 text += acLen;
7776 }
7777 while (padding1-- > 0)
7778 *text++ = ' ';
7779 if (menu->children != NULL)
7780 {
7781 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7782 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7783 }
7784 else
7785 {
7786 while (padding2-- > 0)
7787 *text++ = ' ';
7788 }
7789 *text = NUL;
7790
7791 /*
7792 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7793 * W95/NT4 it makes the tear-off look more like a menu.
7794 */
7795 p = add_dialog_element(p,
7796 BS_PUSHBUTTON|BS_LEFT,
7797 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7798 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7799 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7800 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007801 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 vim_free(label);
7803 (*pnumitems)++;
7804 }
7805
7806 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7807
7808
Bram Moolenaar734a8672019-12-02 22:49:38 +01007809 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007810 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007811 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 (LPDLGTEMPLATE)pdlgtemplate,
7813 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007814 (DLGPROC)tearoff_callback,
7815 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817 LocalFree(LocalHandle(pdlgtemplate));
7818 SelectFont(hdc, oldFont);
7819 DeleteObject(font);
7820 ReleaseDC(hwnd, hdc);
7821
7822 /*
7823 * Reassert ourselves as the active window. This is so that after creating
7824 * a tearoff, the user doesn't have to click with the mouse just to start
7825 * typing again!
7826 */
7827 (void)SetActiveWindow(s_hwnd);
7828
Bram Moolenaar734a8672019-12-02 22:49:38 +01007829 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 force_menu_update = TRUE;
7831}
7832#endif
7833
7834#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007835# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837/*
7838 * Create the toolbar, initially unpopulated.
7839 * (just like the menu, there are no defaults, it's all
7840 * set up through menu.vim)
7841 */
7842 static void
7843initialise_toolbar(void)
7844{
7845 InitCommonControls();
7846 s_toolbarhwnd = CreateToolbarEx(
7847 s_hwnd,
7848 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7849 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007850 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007851 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 IDR_TOOLBAR1, // id of initial bitmap
7853 NULL,
7854 0, // initial number of buttons
7855 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7856 TOOLBAR_BUTTON_HEIGHT,
7857 TOOLBAR_BUTTON_WIDTH,
7858 TOOLBAR_BUTTON_HEIGHT,
7859 sizeof(TBBUTTON)
7860 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007861
7862 // Remove transparency from the toolbar to prevent the main window
7863 // background colour showing through
7864 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7865 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7866
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007867 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
7869 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007870
7871 update_toolbar_size();
7872}
7873
7874 static void
7875update_toolbar_size(void)
7876{
7877 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007878 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007879
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007880 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007881 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7882 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7883 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7884 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7885
7886 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7887 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7888 //TRACE("button size: %d, %d", w, h);
7889 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7890 gui.toolbar_height = h + 6;
7891
7892 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7893 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7894
7895 // TODO:
7896 // Currently, this function only updates the size of toolbar buttons.
7897 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
7899
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007900 static LRESULT CALLBACK
7901toolbar_wndproc(
7902 HWND hwnd,
7903 UINT uMsg,
7904 WPARAM wParam,
7905 LPARAM lParam)
7906{
7907 HandleMouseHide(uMsg, lParam);
7908 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7909}
7910
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 static int
7912get_toolbar_bitmap(vimmenu_T *menu)
7913{
7914 int i = -1;
7915
7916 /*
7917 * Check user bitmaps first, unless builtin is specified.
7918 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007919 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {
7921 char_u fname[MAXPATHL];
7922 HANDLE hbitmap = NULL;
7923
7924 if (menu->iconfile != NULL)
7925 {
7926 gui_find_iconfile(menu->iconfile, fname, "bmp");
7927 hbitmap = LoadImage(
7928 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007929 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 IMAGE_BITMAP,
7931 TOOLBAR_BUTTON_WIDTH,
7932 TOOLBAR_BUTTON_HEIGHT,
7933 LR_LOADFROMFILE |
7934 LR_LOADMAP3DCOLORS
7935 );
7936 }
7937
7938 /*
7939 * If the LoadImage call failed, or the "icon=" file
7940 * didn't exist or wasn't specified, try the menu name
7941 */
7942 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007943 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007944# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007945 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007946# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007947 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 hbitmap = LoadImage(
7949 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007950 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 IMAGE_BITMAP,
7952 TOOLBAR_BUTTON_WIDTH,
7953 TOOLBAR_BUTTON_HEIGHT,
7954 LR_LOADFROMFILE |
7955 LR_LOADMAP3DCOLORS
7956 );
7957
7958 if (hbitmap != NULL)
7959 {
7960 TBADDBITMAP tbAddBitmap;
7961
7962 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007963 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964
7965 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7966 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007967 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 }
7969 }
7970 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7971 i = menu->iconidx;
7972
7973 return i;
7974}
7975#endif
7976
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007977#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7978 static void
7979initialise_tabline(void)
7980{
7981 InitCommonControls();
7982
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007983 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007984 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007985 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007986 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007987 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007988
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007989 gui.tabline_height = TABLINE_HEIGHT;
7990
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007991 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007992}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007993
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007994/*
7995 * Get tabpage_T from POINT.
7996 */
7997 static tabpage_T *
7998GetTabFromPoint(
7999 HWND hWnd,
8000 POINT pt)
8001{
8002 tabpage_T *ptp = NULL;
8003
8004 if (gui_mch_showing_tabline())
8005 {
8006 TCHITTESTINFO htinfo;
8007 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008008 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008009 if (s_tabhwnd == hWnd)
8010 {
8011 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8012 if (idx != -1)
8013 ptp = find_tabpage(idx + 1);
8014 }
8015 }
8016 return ptp;
8017}
8018
8019static POINT s_pt = {0, 0};
8020static HCURSOR s_hCursor = NULL;
8021
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008022 static LRESULT CALLBACK
8023tabline_wndproc(
8024 HWND hwnd,
8025 UINT uMsg,
8026 WPARAM wParam,
8027 LPARAM lParam)
8028{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008029 POINT pt;
8030 tabpage_T *tp;
8031 RECT rect;
8032 int nCenter;
8033 int idx0;
8034 int idx1;
8035
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008036 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008037
8038 switch (uMsg)
8039 {
8040 case WM_LBUTTONDOWN:
8041 {
8042 s_pt.x = GET_X_LPARAM(lParam);
8043 s_pt.y = GET_Y_LPARAM(lParam);
8044 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008045 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008046 break;
8047 }
8048 case WM_MOUSEMOVE:
8049 if (GetCapture() == hwnd
8050 && ((wParam & MK_LBUTTON)) != 0)
8051 {
8052 pt.x = GET_X_LPARAM(lParam);
8053 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008054 if (abs(pt.x - s_pt.x) >
8055 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008056 {
8057 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8058
8059 tp = GetTabFromPoint(hwnd, pt);
8060 if (tp != NULL)
8061 {
8062 idx0 = tabpage_index(curtab) - 1;
8063 idx1 = tabpage_index(tp) - 1;
8064
8065 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8066 nCenter = rect.left + (rect.right - rect.left) / 2;
8067
Bram Moolenaar734a8672019-12-02 22:49:38 +01008068 // Check if the mouse cursor goes over the center of
8069 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008070 if ((idx0 < idx1) && (nCenter < pt.x))
8071 {
8072 tabpage_move(idx1 + 1);
8073 update_screen(0);
8074 }
8075 else if ((idx1 < idx0) && (pt.x < nCenter))
8076 {
8077 tabpage_move(idx1);
8078 update_screen(0);
8079 }
8080 }
8081 }
8082 }
8083 break;
8084 case WM_LBUTTONUP:
8085 {
8086 if (GetCapture() == hwnd)
8087 {
8088 SetCursor(s_hCursor);
8089 ReleaseCapture();
8090 }
8091 break;
8092 }
regomne3bdef102022-09-26 20:48:32 +01008093 case WM_MBUTTONUP:
8094 {
8095 TCHITTESTINFO htinfo;
8096
8097 htinfo.pt.x = GET_X_LPARAM(lParam);
8098 htinfo.pt.y = GET_Y_LPARAM(lParam);
8099 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8100 if (idx0 != -1)
8101 {
8102 idx0 += 1;
8103 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8104 }
8105 break;
8106 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008107 default:
8108 break;
8109 }
8110
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008111 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8112}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008113#endif
8114
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8116/*
8117 * Make the GUI window come to the foreground.
8118 */
8119 void
8120gui_mch_set_foreground(void)
8121{
8122 if (IsIconic(s_hwnd))
8123 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8124 SetForegroundWindow(s_hwnd);
8125}
8126#endif
8127
8128#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8129 static void
8130dyn_imm_load(void)
8131{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008132 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 if (hLibImm == NULL)
8134 return;
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008137 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008139 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008141 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008143 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008145 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008147 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008148 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008149 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008150 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008151 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008153 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008155 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008156 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008157 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158
K.Takatab0b2b732022-01-19 12:59:21 +00008159 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 || pImmGetContext == NULL
8161 || pImmAssociateContext == NULL
8162 || pImmReleaseContext == NULL
8163 || pImmGetOpenStatus == NULL
8164 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008165 || pImmGetCompositionFontW == NULL
8166 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008168 || pImmGetConversionStatus == NULL
8169 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 {
8171 FreeLibrary(hLibImm);
8172 hLibImm = NULL;
8173 pImmGetContext = NULL;
8174 return;
8175 }
8176
8177 return;
8178}
8179
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#endif
8181
8182#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8183
8184# ifdef FEAT_XPM_W32
8185# define IMAGE_XPM 100
8186# endif
8187
8188typedef struct _signicon_t
8189{
8190 HANDLE hImage;
8191 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008192# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008193 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195} signicon_t;
8196
8197 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008198gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199{
8200 signicon_t *sign;
8201 int x, y, w, h;
8202
8203 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8204 return;
8205
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008206# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008207 if (IS_ENABLE_DIRECTX())
8208 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008209# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008210
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 x = TEXT_X(col);
8212 y = TEXT_Y(row);
8213 w = gui.char_width * 2;
8214 h = gui.char_height;
8215 switch (sign->uType)
8216 {
8217 case IMAGE_BITMAP:
8218 {
8219 HDC hdcMem;
8220 HBITMAP hbmpOld;
8221
8222 hdcMem = CreateCompatibleDC(s_hdc);
8223 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8224 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8225 SelectObject(hdcMem, hbmpOld);
8226 DeleteDC(hdcMem);
8227 }
8228 break;
8229 case IMAGE_ICON:
8230 case IMAGE_CURSOR:
8231 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8232 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008233# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 case IMAGE_XPM:
8235 {
8236 HDC hdcMem;
8237 HBITMAP hbmpOld;
8238
8239 hdcMem = CreateCompatibleDC(s_hdc);
8240 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008241 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8243
8244 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008245 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8247 SelectObject(hdcMem, hbmpOld);
8248 DeleteDC(hdcMem);
8249 }
8250 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008251# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 }
8253}
8254
8255 static void
8256close_signicon_image(signicon_t *sign)
8257{
8258 if (sign)
8259 switch (sign->uType)
8260 {
8261 case IMAGE_BITMAP:
8262 DeleteObject((HGDIOBJ)sign->hImage);
8263 break;
8264 case IMAGE_CURSOR:
8265 DestroyCursor((HCURSOR)sign->hImage);
8266 break;
8267 case IMAGE_ICON:
8268 DestroyIcon((HICON)sign->hImage);
8269 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008270# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 case IMAGE_XPM:
8272 DeleteObject((HBITMAP)sign->hImage);
8273 DeleteObject((HBITMAP)sign->hShape);
8274 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 }
8277}
8278
8279 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008280gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281{
8282 signicon_t sign, *psign;
8283 char_u *ext;
8284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008286 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 if (ext > signfile)
8288 {
8289 int do_load = 1;
8290
8291 if (!STRICMP(ext, ".bmp"))
8292 sign.uType = IMAGE_BITMAP;
8293 else if (!STRICMP(ext, ".ico"))
8294 sign.uType = IMAGE_ICON;
8295 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8296 sign.uType = IMAGE_CURSOR;
8297 else
8298 do_load = 0;
8299
8300 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008301 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 gui.char_width * 2, gui.char_height,
8303 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008304# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (!STRICMP(ext, ".xpm"))
8306 {
8307 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008308 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8309 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 }
8313
8314 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008315 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 *psign = sign;
8317
8318 if (!psign)
8319 {
8320 if (sign.hImage)
8321 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008322 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
8324 return (void *)psign;
8325
8326}
8327
8328 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008329gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330{
8331 if (sign)
8332 {
8333 close_signicon_image((signicon_t *)sign);
8334 vim_free(sign);
8335 }
8336}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008339#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340
Bram Moolenaar734a8672019-12-02 22:49:38 +01008341/*
8342 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008343 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008345 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8347 * to get current mouse position).
8348 *
8349 * Trying to use as more Windows services as possible, and as less
8350 * IE version as possible :)).
8351 *
8352 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8353 * BalloonEval struct.
8354 * 2) Enable/Disable simply create/kill BalloonEval Timer
8355 * 3) When there was enough inactivity, timer procedure posts
8356 * async request to debugger
8357 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8358 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008359 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 */
8361
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008362 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008363make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008364{
K.Takata76687d22022-01-25 10:31:37 +00008365 TOOLINFOW *pti;
8366 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008367
K.Takata76687d22022-01-25 10:31:37 +00008368 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008369 if (pti == NULL)
8370 return;
8371
8372 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8373 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8374 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008375 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008376
8377 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8378 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8379
K.Takata76687d22022-01-25 10:31:37 +00008380 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008381 pti->uFlags = TTF_SUBCLASS;
8382 pti->hwnd = beval->target;
8383 pti->hinst = 0; // Don't use string resources
8384 pti->uId = ID_BEVAL_TOOLTIP;
8385
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008386 pti->lpszText = LPSTR_TEXTCALLBACKW;
8387 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8388 pti->lParam = (LPARAM)beval->tofree;
8389 // switch multiline tooltips on
8390 if (GetClientRect(s_textArea, &rect))
8391 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8392 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008393
8394 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8395 pti->rect.left = pt.x - 3;
8396 pti->rect.top = pt.y - 3;
8397 pti->rect.right = pt.x + 3;
8398 pti->rect.bottom = pt.y + 3;
8399
8400 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8401 // Make tooltip appear sooner.
8402 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8403 // I've performed some tests and it seems the longest possible life time
8404 // of tooltip is 30 seconds.
8405 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8406 /*
8407 * HACK: force tooltip to appear, because it'll not appear until
8408 * first mouse move. D*mn M$
8409 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8410 */
8411 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8412 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8413 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008414}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008415
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008417delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008419 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420}
8421
8422 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008423beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008424 HWND hwnd UNUSED,
8425 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008426 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008427 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 POINT pt;
8430 RECT rect;
8431
8432 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8433 return;
8434
8435 GetCursorPos(&pt);
8436 if (WindowFromPoint(pt) != s_textArea)
8437 return;
8438
8439 ScreenToClient(s_textArea, &pt);
8440 GetClientRect(s_textArea, &rect);
8441 if (!PtInRect(&rect, pt))
8442 return;
8443
K.Takataa8ec4912022-02-03 14:32:33 +00008444 if (last_user_activity > 0
8445 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 && (cur_beval->showState != ShS_PENDING
8447 || abs(cur_beval->x - pt.x) > 3
8448 || abs(cur_beval->y - pt.y) > 3))
8449 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008450 // Pointer resting in one place long enough, it's time to show
8451 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 cur_beval->showState = ShS_PENDING;
8453 cur_beval->x = pt.x;
8454 cur_beval->y = pt.y;
8455
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 if (cur_beval->msgCB != NULL)
8457 (*cur_beval->msgCB)(cur_beval, 0);
8458 }
8459}
8460
8461 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008462gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
K.Takataa8ec4912022-02-03 14:32:33 +00008464 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465}
8466
8467 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008468gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (beval == NULL)
8471 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008472 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8473 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474}
8475
8476 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008477gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008480
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008481 vim_free(beval->msg);
8482 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8483 if (beval->msg == NULL)
8484 {
8485 delete_tooltip(beval);
8486 beval->showState = ShS_NEUTRAL;
8487 return;
8488 }
8489
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 if (beval->showState == ShS_SHOWING)
8491 return;
8492 GetCursorPos(&pt);
8493 ScreenToClient(s_textArea, &pt);
8494
8495 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008497 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 gui_mch_disable_beval_area(cur_beval);
8499 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008500 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
8503
8504 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008505gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008506 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008507 char_u *mesg,
8508 void (*mesgCB)(BalloonEval *, int),
8509 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008511 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 BalloonEval *beval;
8513
8514 if (mesg != NULL && mesgCB != NULL)
8515 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008516 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 return NULL;
8518 }
8519
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008520 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (beval != NULL)
8522 {
8523 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524
8525 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 beval->msg = mesg;
8527 beval->msgCB = mesgCB;
8528 beval->clientData = clientData;
8529
8530 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 cur_beval = beval;
8532
8533 if (p_beval)
8534 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536 return beval;
8537}
8538
8539 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008540Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008542 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 return;
8544
8545 if (cur_beval != NULL)
8546 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008547 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008549 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008550 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008551 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 delete_tooltip(cur_beval);
8553 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554
8555 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008556 break;
8557 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008558 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008559 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008560 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008561 info->lpszText = (LPSTR)info->lParam;
8562 info->uFlags |= TTF_DI_SETITEM;
8563 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008564 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008565 case TTN_GETDISPINFOW:
8566 {
8567 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008568 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008569 info->lpszText = (LPWSTR)info->lParam;
8570 info->uFlags |= TTF_DI_SETITEM;
8571 }
8572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 }
8574 }
8575}
8576
8577 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008578track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
8580 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8581 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008582 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584
8585 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008586gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008588# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008589 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008590# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008591 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 vim_free(beval);
8593}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008594#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8597/*
8598 * We have multiple signs to draw at the same location. Draw the
8599 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8600 */
8601 void
8602netbeans_draw_multisign_indicator(int row)
8603{
8604 int i;
8605 int y;
8606 int x;
8607
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008608 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008609 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 x = 0;
8612 y = TEXT_Y(row);
8613
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008614# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008615 if (IS_ENABLE_DIRECTX())
8616 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008617# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 for (i = 0; i < gui.char_height - 3; i++)
8620 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8621
8622 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8623 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8624 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8625 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8626 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8627 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8628 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8629}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008630#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008631
8632#if defined(FEAT_EVAL) || defined(PROTO)
8633 int
8634test_gui_w32_sendevent(dict_T *args)
8635{
8636 char_u *event;
8637 INPUT inputs[1];
8638
Bram Moolenaard61efa52022-07-23 09:52:04 +01008639 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008640 if (event == NULL)
8641 return FALSE;
8642
8643 ZeroMemory(inputs, sizeof(inputs));
8644
8645 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8646 {
8647 WORD vkCode;
8648
Bram Moolenaard61efa52022-07-23 09:52:04 +01008649 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008650 if (vkCode <= 0 || vkCode >= 0xFF)
8651 {
8652 semsg(_(e_invalid_argument_nr), (long)vkCode);
8653 return FALSE;
8654 }
8655
8656 inputs[0].type = INPUT_KEYBOARD;
8657 inputs[0].ki.wVk = vkCode;
8658 if (STRICMP(event, "keyup") == 0)
8659 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
K.Takatafef38d82022-09-07 19:03:42 +01008660 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008661 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8662 }
8663 else
8664 semsg(_(e_invalid_argument_str), event);
8665
8666 vim_free(event);
8667
8668 return TRUE;
8669}
8670#endif