blob: a29fa91b0a2a1585d0e1496dbb411678021ce490 [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
Anton Sharonov68d94722024-01-23 23:19:02 +010053typedef struct keycode_trans_strategy {
54 void (*ptr_on_char) (HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
55 void (*ptr_on_sys_char) (HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
56 void (*ptr_process_message_usual_key) (UINT /*vk*/, const MSG* /*pmsg*/);
57 int (*ptr_get_active_modifiers)(void);
58 int (*is_experimental)(void);
59} keycode_trans_strategy;
60
61// forward declarations for input instance initializer
62static void _OnChar_experimental(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
63static void _OnSysChar_experimental(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
64static void process_message_usual_key_experimental(UINT /*vk*/, const MSG* /*pmsg*/);
65static int get_active_modifiers_experimental(void);
66static int is_experimental_true(void);
67
68keycode_trans_strategy keycode_trans_strategy_experimental = {
69 _OnChar_experimental // ptr_on_char
70 , _OnSysChar_experimental // ptr_on_sys_char
71 , process_message_usual_key_experimental // ptr_process_message_usual_key
72 , get_active_modifiers_experimental
73 , is_experimental_true
74};
75
76// forward declarations for input instance initializer
77static void _OnChar_classic(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
78static void _OnSysChar_classic(HWND /*hwnd UNUSED*/, UINT /*cch*/, int /*cRepeat UNUSED*/);
79static void process_message_usual_key_classic(UINT /*vk*/, const MSG* /*pmsg*/);
80static int get_active_modifiers_classic(void);
81static int is_experimental_false(void);
82
83keycode_trans_strategy keycode_trans_strategy_classic = {
84 _OnChar_classic // ptr_on_char
85 , _OnSysChar_classic // ptr_on_sys_char
86 , process_message_usual_key_classic // ptr_process_message_usual_key
87 , get_active_modifiers_classic
88 , is_experimental_false
89};
90
91keycode_trans_strategy *keycode_trans_strategy_used = NULL;
92
93static int is_experimental_true(void)
94{
95 return 1;
96}
97
98static int is_experimental_false(void)
99{
100 return 0;
101}
102
103/*
104 * Initialize the keycode translation strategy.
105 */
106static void keycode_trans_strategy_init(void)
107{
108 const char *strategy = NULL;
109
110 // set default value as fallback
111 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
112
113 strategy = getenv("VIM_KEYCODE_TRANS_STRATEGY");
114 if (strategy == NULL)
115 {
116 return;
117 }
118
119 if (STRICMP(strategy, "classic") == 0)
120 {
121 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
122 return;
123 }
124
125 if (STRICMP(strategy, "experimental") == 0)
126 {
127 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
128 return;
129 }
130
131}
132
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200133#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
134 int
135gui_mch_set_rendering_options(char_u *s)
136{
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100137# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200138 char_u *p, *q;
139
140 int dx_enable = 0;
141 int dx_flags = 0;
142 float dx_gamma = 0.0f;
143 float dx_contrast = 0.0f;
144 float dx_level = 0.0f;
145 int dx_geom = 0;
146 int dx_renmode = 0;
147 int dx_taamode = 0;
148
Bram Moolenaar734a8672019-12-02 22:49:38 +0100149 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200150 for (p = s; p != NULL && *p != NUL; )
151 {
152 char_u item[256];
153 char_u name[128];
154 char_u value[128];
155
Bram Moolenaarcde88542015-08-11 19:14:00 +0200156 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200157 if (p == NULL)
158 break;
159 q = &item[0];
160 copy_option_part(&q, name, sizeof(name), ":");
161 if (q == NULL)
162 return FAIL;
163 copy_option_part(&q, value, sizeof(value), ":");
164
165 if (STRCMP(name, "type") == 0)
166 {
167 if (STRCMP(value, "directx") == 0)
168 dx_enable = 1;
169 else
170 return FAIL;
171 }
172 else if (STRCMP(name, "gamma") == 0)
173 {
174 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100175 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200176 }
177 else if (STRCMP(name, "contrast") == 0)
178 {
179 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100180 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200181 }
182 else if (STRCMP(name, "level") == 0)
183 {
184 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100185 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200186 }
187 else if (STRCMP(name, "geom") == 0)
188 {
189 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100190 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200191 if (dx_geom < 0 || dx_geom > 2)
192 return FAIL;
193 }
194 else if (STRCMP(name, "renmode") == 0)
195 {
196 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100197 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200198 if (dx_renmode < 0 || dx_renmode > 6)
199 return FAIL;
200 }
201 else if (STRCMP(name, "taamode") == 0)
202 {
203 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100204 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200205 if (dx_taamode < 0 || dx_taamode > 3)
206 return FAIL;
207 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100208 else if (STRCMP(name, "scrlines") == 0)
209 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100211 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200212 else
213 return FAIL;
214 }
215
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100216 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100217 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100218
Bram Moolenaar734a8672019-12-02 22:49:38 +0100219 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200220 if (dx_enable)
221 {
222 if (!directx_enabled())
223 return FAIL;
224 DWriteContext_SetRenderingParams(s_dwc, NULL);
225 if (dx_flags)
226 {
227 DWriteRenderingParams param;
228 DWriteContext_GetRenderingParams(s_dwc, &param);
229 if (dx_flags & (1 << 0))
230 param.gamma = dx_gamma;
231 if (dx_flags & (1 << 1))
232 param.enhancedContrast = dx_contrast;
233 if (dx_flags & (1 << 2))
234 param.clearTypeLevel = dx_level;
235 if (dx_flags & (1 << 3))
236 param.pixelGeometry = dx_geom;
237 if (dx_flags & (1 << 4))
238 param.renderingMode = dx_renmode;
239 if (dx_flags & (1 << 5))
240 param.textAntialiasMode = dx_taamode;
241 DWriteContext_SetRenderingParams(s_dwc, &param);
242 }
243 }
244 s_directx_enabled = dx_enable;
245
246 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100247# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200248 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100249# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200250}
251#endif
252
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253/*
254 * These are new in Windows ME/XP, only defined in recent compilers.
255 */
256#ifndef HANDLE_WM_XBUTTONUP
257# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
258 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
259#endif
260#ifndef HANDLE_WM_XBUTTONDOWN
261# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
262 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
263#endif
264#ifndef HANDLE_WM_XBUTTONDBLCLK
265# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
266 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
267#endif
268
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100269
Bram Moolenaar734a8672019-12-02 22:49:38 +0100270#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100271#ifdef DEBUG
272# include <tchar.h>
273#endif
274
Bram Moolenaar734a8672019-12-02 22:49:38 +0100275// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100276#ifndef PROTO
K.Takata56f587b2024-06-19 19:56:03 +0200277# include <shellapi.h>
K.Takata2da11a42022-09-10 13:03:12 +0100278# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100279# include <windowsx.h>
Bram Moolenaar734a8672019-12-02 22:49:38 +0100280#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100281
282#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100283# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284#endif
285
Bram Moolenaar734a8672019-12-02 22:49:38 +0100286// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100287#define DLG_PADDING_X 10
288#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100289#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100290#define DLG_VERT_PADDING_Y 4
291#define DLG_ICON_WIDTH 34
292#define DLG_ICON_HEIGHT 34
293#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000294#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100295#define DLG_FONT_POINT_SIZE 8
296#define DLG_MIN_MAX_WIDTH 400
297#define DLG_MIN_MAX_HEIGHT 400
298
Bram Moolenaar734a8672019-12-02 22:49:38 +0100299#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100300
K.Takatac81e9bf2022-01-16 14:15:49 +0000301#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100302# define WM_DPICHANGED 0x02E0
303#endif
304
Ken Takata7086b3e2023-10-02 21:26:03 +0200305#ifndef WM_GETDPISCALEDSIZE
306# define WM_GETDPISCALEDSIZE 0x02E4
307#endif
308
LemonBoy365d8f72022-05-05 19:23:07 +0100309#ifndef WM_MOUSEHWHEEL
310# define WM_MOUSEHWHEEL 0x020E
311#endif
312
313#ifndef SPI_GETWHEELSCROLLCHARS
314# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000315#endif
316
LemonBoyc27747e2022-05-07 12:25:40 +0100317#ifndef SPI_SETWHEELSCROLLCHARS
318# define SPI_SETWHEELSCROLLCHARS 0x006D
319#endif
320
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100323 * Define a few things for generating prototypes. This is just to avoid
324 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100326# define APIENTRY
327# define CALLBACK
328# define CONST
329# define FAR
330# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200331# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200332# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333# define _cdecl
334typedef int BOOL;
335typedef int BYTE;
336typedef int DWORD;
337typedef int WCHAR;
338typedef int ENUMLOGFONT;
339typedef int FINDREPLACE;
340typedef int HANDLE;
341typedef int HBITMAP;
342typedef int HBRUSH;
343typedef int HDROP;
344typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100345typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100346typedef int LPARAM;
347typedef int LPCREATESTRUCT;
348typedef int LPCSTR;
349typedef int LPCTSTR;
350typedef int LPRECT;
351typedef int LPSTR;
352typedef int LPWINDOWPOS;
353typedef int LPWORD;
354typedef int LRESULT;
355typedef int HRESULT;
356# undef MSG
357typedef int MSG;
358typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100359typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100360typedef int OSVERSIONINFO;
361typedef int PWORD;
362typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100363typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100364typedef int UINT;
365typedef int WORD;
366typedef int WPARAM;
367typedef int POINT;
368typedef void *HINSTANCE;
369typedef void *HMENU;
370typedef void *HWND;
371typedef void *HDC;
372typedef void VOID;
373typedef int LPNMHDR;
374typedef int LONG;
375typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200376typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200377typedef int COLORREF;
378typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100379#endif
380
K.Takata45f9cfb2022-01-21 11:11:00 +0000381static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100382static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100383static void clear_rect(RECT *rcp);
384
Bram Moolenaar734a8672019-12-02 22:49:38 +0100385static WORD s_dlgfntheight; // height of the dialog font
386static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100387
388#ifdef FEAT_MENU
389static HMENU s_menuBar = NULL;
390#endif
391#ifdef FEAT_TEAROFF
392static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100393static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100394#endif
395
Bram Moolenaar734a8672019-12-02 22:49:38 +0100396// Flag that is set while processing a message that must not be interrupted by
397// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100398static int s_busy_processing = FALSE;
399
Bram Moolenaar734a8672019-12-02 22:49:38 +0100400static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100401
402#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000403static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200404static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100405static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200406static int s_findrep_is_find; // TRUE for find dialog, FALSE
407 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100408#endif
409
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100410HWND s_hwnd = NULL;
411static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100412static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100413
414#ifdef FEAT_TOOLBAR
415static HWND s_toolbarhwnd = NULL;
416static WNDPROC s_toolbar_wndproc = NULL;
417#endif
418
419#ifdef FEAT_GUI_TABLINE
420static HWND s_tabhwnd = NULL;
421static WNDPROC s_tabline_wndproc = NULL;
422static int showing_tabline = 0;
423#endif
424
425static WPARAM s_wParam = 0;
426static LPARAM s_lParam = 0;
427
428static HWND s_textArea = NULL;
429static UINT s_uMsg = 0;
430
Bram Moolenaar734a8672019-12-02 22:49:38 +0100431static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100432
433static int s_need_activate = FALSE;
434
Bram Moolenaar734a8672019-12-02 22:49:38 +0100435// This variable is set when waiting for an event, which is the only moment
436// scrollbar dragging can be done directly. It's not allowed while commands
437// are executed, because it may move the cursor and that may cause unexpected
438// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100439static int allow_scrollbar = FALSE;
440
K.Takatac81e9bf2022-01-16 14:15:49 +0000441#ifndef _DPI_AWARENESS_CONTEXTS_
442typedef HANDLE DPI_AWARENESS_CONTEXT;
443
444typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000445 DPI_AWARENESS_INVALID = -1,
446 DPI_AWARENESS_UNAWARE = 0,
447 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000448 DPI_AWARENESS_PER_MONITOR_AWARE = 2
449} DPI_AWARENESS;
450
K.Takatab0b2b732022-01-19 12:59:21 +0000451# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
452# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000453# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
454# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
455# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
456#endif
457
458#define DEFAULT_DPI 96
459static int s_dpi = DEFAULT_DPI;
460static BOOL s_in_dpichanged = FALSE;
461static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
Ken Takata7086b3e2023-10-02 21:26:03 +0200462static RECT s_suggested_rect;
K.Takatac81e9bf2022-01-16 14:15:49 +0000463
464static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
465static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
466static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
467//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
468static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
469static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
470
K.Takatac81e9bf2022-01-16 14:15:49 +0000471 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100472stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000473{
474 return GetSystemMetrics(nIndex);
475}
476
477 static int
478adjust_fontsize_by_dpi(int size)
479{
480 return size * s_dpi / (int)pGetDpiForSystem();
481}
482
483 static int
484adjust_by_system_dpi(int size)
485{
486 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
487}
488
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100489#if defined(FEAT_DIRECTX)
490 static int
491directx_enabled(void)
492{
493 if (s_dwc != NULL)
494 return 1;
495 else if (s_directx_load_attempted)
496 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100497 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100498 DWrite_Init();
499 s_directx_load_attempted = 1;
500 s_dwc = DWriteContext_Open();
501 directx_binddc();
502 return s_dwc != NULL ? 1 : 0;
503}
504
505 static void
506directx_binddc(void)
507{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000508 if (s_textArea == NULL)
509 return;
510
511 RECT rect;
512 GetClientRect(s_textArea, &rect);
513 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100514}
515#endif
516
Bram Moolenaar734a8672019-12-02 22:49:38 +0100517extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100518
519static struct
520{
521 UINT key_sym;
522 char_u vim_code0;
523 char_u vim_code1;
524} special_keys[] =
525{
526 {VK_UP, 'k', 'u'},
527 {VK_DOWN, 'k', 'd'},
528 {VK_LEFT, 'k', 'l'},
529 {VK_RIGHT, 'k', 'r'},
530
531 {VK_F1, 'k', '1'},
532 {VK_F2, 'k', '2'},
533 {VK_F3, 'k', '3'},
534 {VK_F4, 'k', '4'},
535 {VK_F5, 'k', '5'},
536 {VK_F6, 'k', '6'},
537 {VK_F7, 'k', '7'},
538 {VK_F8, 'k', '8'},
539 {VK_F9, 'k', '9'},
540 {VK_F10, 'k', ';'},
541
542 {VK_F11, 'F', '1'},
543 {VK_F12, 'F', '2'},
544 {VK_F13, 'F', '3'},
545 {VK_F14, 'F', '4'},
546 {VK_F15, 'F', '5'},
547 {VK_F16, 'F', '6'},
548 {VK_F17, 'F', '7'},
549 {VK_F18, 'F', '8'},
550 {VK_F19, 'F', '9'},
551 {VK_F20, 'F', 'A'},
552
553 {VK_F21, 'F', 'B'},
554#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100555 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100556#endif
557 {VK_F22, 'F', 'C'},
558 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100559 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100560
561 {VK_HELP, '%', '1'},
562 {VK_BACK, 'k', 'b'},
563 {VK_INSERT, 'k', 'I'},
564 {VK_DELETE, 'k', 'D'},
565 {VK_HOME, 'k', 'h'},
566 {VK_END, '@', '7'},
567 {VK_PRIOR, 'k', 'P'},
568 {VK_NEXT, 'k', 'N'},
569 {VK_PRINT, '%', '9'},
570 {VK_ADD, 'K', '6'},
571 {VK_SUBTRACT, 'K', '7'},
572 {VK_DIVIDE, 'K', '8'},
573 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100574 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100575 {VK_DECIMAL, 'K', 'B'},
576
577 {VK_NUMPAD0, 'K', 'C'},
578 {VK_NUMPAD1, 'K', 'D'},
579 {VK_NUMPAD2, 'K', 'E'},
580 {VK_NUMPAD3, 'K', 'F'},
581 {VK_NUMPAD4, 'K', 'G'},
582 {VK_NUMPAD5, 'K', 'H'},
583 {VK_NUMPAD6, 'K', 'I'},
584 {VK_NUMPAD7, 'K', 'J'},
585 {VK_NUMPAD8, 'K', 'K'},
586 {VK_NUMPAD9, 'K', 'L'},
587
Bram Moolenaar734a8672019-12-02 22:49:38 +0100588 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100589 {VK_SPACE, ' ', NUL},
590 {VK_TAB, TAB, NUL},
591 {VK_ESCAPE, ESC, NUL},
592 {NL, NL, NUL},
593 {CAR, CAR, NUL},
594
Bram Moolenaar734a8672019-12-02 22:49:38 +0100595 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100596 {0, 0, 0}
597};
598
Bram Moolenaar734a8672019-12-02 22:49:38 +0100599// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100600static int s_button_pending = -1;
601
Bram Moolenaar734a8672019-12-02 22:49:38 +0100602// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
603// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100604static int s_getting_focus = FALSE;
605
606static int s_x_pending;
607static int s_y_pending;
608static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000609static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100610static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100611static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200612static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
613 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100614
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100615#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100616// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100617static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000618static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100619#endif
620
621/*
622 * For control IME.
623 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100624 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100625 */
K.Takata4ac893f2022-01-20 12:44:28 +0000626#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100627// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100628static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100629// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100630static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100631#endif
632
633#ifdef FEAT_MBYTE_IME
634static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
635#endif
636
637#if defined(FEAT_BROWSE)
638static char_u *convert_filter(char_u *s);
639#endif
640
641#ifdef DEBUG_PRINT_ERROR
642/*
643 * Print out the last Windows error message
644 */
645 static void
646print_windows_error(void)
647{
648 LPVOID lpMsgBuf;
649
650 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
651 NULL, GetLastError(),
652 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
653 (LPTSTR) &lpMsgBuf, 0, NULL);
654 TRACE1("Error: %s\n", lpMsgBuf);
655 LocalFree(lpMsgBuf);
656}
657#endif
658
659/*
660 * Cursor blink functions.
661 *
662 * This is a simple state machine:
663 * BLINK_NONE not blinking at all
664 * BLINK_OFF blinking, cursor is not shown
665 * BLINK_ON blinking, cursor is shown
666 */
667
668#define BLINK_NONE 0
669#define BLINK_OFF 1
670#define BLINK_ON 2
671
672static int blink_state = BLINK_NONE;
673static long_u blink_waittime = 700;
674static long_u blink_ontime = 400;
675static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000676static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100677
Bram Moolenaar703a8042016-06-04 16:24:32 +0200678 int
679gui_mch_is_blinking(void)
680{
681 return blink_state != BLINK_NONE;
682}
683
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200684 int
685gui_mch_is_blink_off(void)
686{
687 return blink_state == BLINK_OFF;
688}
689
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 void
691gui_mch_set_blinking(long wait, long on, long off)
692{
693 blink_waittime = wait;
694 blink_ontime = on;
695 blink_offtime = off;
696}
697
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100698 static VOID CALLBACK
699_OnBlinkTimer(
700 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100701 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000702 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100703 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704{
705 MSG msg;
706
707 /*
708 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
709 */
710
711 KillTimer(NULL, idEvent);
712
Bram Moolenaar734a8672019-12-02 22:49:38 +0100713 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000714 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100715 ;
716
717 if (blink_state == BLINK_ON)
718 {
719 gui_undraw_cursor();
720 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000721 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100722 }
723 else
724 {
725 gui_update_cursor(TRUE, FALSE);
726 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000727 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100728 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100729 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100730}
731
732 static void
733gui_mswin_rm_blink_timer(void)
734{
735 MSG msg;
736
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000737 if (blink_timer == 0)
738 return;
739
740 KillTimer(NULL, blink_timer);
741 // Eat spurious WM_TIMER messages
742 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
743 ;
744 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100745}
746
747/*
748 * Stop the cursor blinking. Show the cursor if it wasn't shown.
749 */
750 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100751gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100752{
753 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100754 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100755 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100756 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100757 gui_mch_flush();
758 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100759 blink_state = BLINK_NONE;
760}
761
762/*
763 * Start the cursor blinking. If it was already blinking, this restarts the
764 * waiting time and shows the cursor.
765 */
766 void
767gui_mch_start_blink(void)
768{
769 gui_mswin_rm_blink_timer();
770
Bram Moolenaar734a8672019-12-02 22:49:38 +0100771 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100772 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
773 {
K.Takataa8ec4912022-02-03 14:32:33 +0000774 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100775 blink_state = BLINK_ON;
776 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100777 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100778 }
779}
780
781/*
782 * Call-back routines.
783 */
784
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100785 static VOID CALLBACK
786_OnTimer(
787 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100788 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000789 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100790 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100791{
792 MSG msg;
793
794 /*
795 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
796 */
797 KillTimer(NULL, idEvent);
798 s_timed_out = TRUE;
799
Bram Moolenaar734a8672019-12-02 22:49:38 +0100800 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000801 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100802 ;
803 if (idEvent == s_wait_timer)
804 s_wait_timer = 0;
805}
806
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100807 static void
808_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100809 HWND hwnd UNUSED,
810 UINT ch UNUSED,
811 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100812{
Anton Sharonov68d94722024-01-23 23:19:02 +0100813 dead_key = DEAD_KEY_SET_DEFAULT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100814}
815
816/*
817 * Convert Unicode character "ch" to bytes in "string[slen]".
818 * When "had_alt" is TRUE the ALT key was included in "ch".
819 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200820 * Because the Windows API uses UTF-16, we have to deal with surrogate
821 * pairs; this is where we choose to deal with them: if "ch" is a high
822 * surrogate, it will be stored, and the length returned will be zero; the next
823 * char_to_string call will then include the high surrogate, decoding the pair
824 * of UTF-16 code units to a single Unicode code point, presuming it is the
825 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826 */
827 static int
828char_to_string(int ch, char_u *string, int slen, int had_alt)
829{
830 int len;
831 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100832 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200833 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100834
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200835 if (surrogate_pending_ch != 0)
836 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100837 // We don't guarantee ch is a low surrogate to match the high surrogate
838 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200839 wstring[0] = surrogate_pending_ch;
840 wstring[1] = ch;
841 surrogate_pending_ch = 0;
842 len = 2;
843 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100844 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200845 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100846 // We don't have the entire code point yet, only the first UTF-16 code
847 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200848 surrogate_pending_ch = ch;
849 return 0;
850 }
851 else
852 {
853 wstring[0] = ch;
854 len = 1;
855 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200856
Bram Moolenaar734a8672019-12-02 22:49:38 +0100857 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
858 // "enc_codepage" is non-zero use the standard Win32 function,
859 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200860 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100861 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200862 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
863 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100864 // If we had included the ALT key into the character but now the
865 // upper bit is no longer set, that probably means the conversion
866 // failed. Convert the original character and set the upper bit
867 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200868 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100869 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200870 wstring[0] = ch & 0x7f;
871 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
872 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100873 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200874 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100875 }
876 }
877 else
878 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200879 ws = utf16_to_enc(wstring, &len);
880 if (ws == NULL)
881 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100882 else
883 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100884 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200885 len = slen;
886 mch_memmove(string, ws, len);
887 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100888 }
889 }
890
891 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100892 {
893 string[0] = ch;
894 len = 1;
895 }
896
897 for (i = 0; i < len; ++i)
898 if (string[i] == CSI && len <= slen - 2)
899 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100900 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
902 string[++i] = KS_EXTRA;
903 string[++i] = (int)KE_CSI;
904 len += 2;
905 }
906
907 return len;
908}
909
Anton Sharonov68d94722024-01-23 23:19:02 +0100910/*
911 * Experimental implementation, introduced in v8.2.4807
912 * "processing key event in Win32 GUI is not ideal"
913 *
914 * TODO: since introduction, this experimental function started
915 * to be used as well outside of original key press/processing
916 * area, and usages not via "get_active_modifiers_via_ptr" should
917 * be watched.
918 */
LemonBoy45684c62022-04-24 15:46:42 +0100919 static int
Anton Sharonov68d94722024-01-23 23:19:02 +0100920get_active_modifiers_experimental(void)
LemonBoy45684c62022-04-24 15:46:42 +0100921{
922 int modifiers = 0;
923
924 if (GetKeyState(VK_CONTROL) & 0x8000)
925 modifiers |= MOD_MASK_CTRL;
926 if (GetKeyState(VK_SHIFT) & 0x8000)
927 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100928 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
929 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100930 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100931 if (GetKeyState(VK_LMENU) & 0x8000)
932 modifiers |= MOD_MASK_ALT;
933 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
934 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100935 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
936 // is pressed, Windows claims that Ctrl is hold as well. That way we can
937 // recognize Right-ALT alone and be sure that not AltGr is hold.
938 if (!(GetKeyState(VK_CONTROL) & 0x8000)
939 && (GetKeyState(VK_RMENU) & 0x8000)
940 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
941 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100942
943 return modifiers;
944}
945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100946/*
Anton Sharonov68d94722024-01-23 23:19:02 +0100947 * "Classic" implementation, existing prior to v8.2.4807
948 */
949 static int
950get_active_modifiers_classic(void)
951{
952 int modifiers = 0;
953
954 if (GetKeyState(VK_SHIFT) & 0x8000)
955 modifiers |= MOD_MASK_SHIFT;
956 /*
957 * Don't use caps-lock as shift, because these are special keys
958 * being considered here, and we only want letters to get
959 * shifted -- webb
960 */
961 /*
962 if (GetKeyState(VK_CAPITAL) & 0x0001)
963 modifiers ^= MOD_MASK_SHIFT;
964 */
965 if (GetKeyState(VK_CONTROL) & 0x8000)
966 modifiers |= MOD_MASK_CTRL;
967 if (GetKeyState(VK_MENU) & 0x8000)
968 modifiers |= MOD_MASK_ALT;
969
970 return modifiers;
971}
972
973 static int
974get_active_modifiers(void)
975{
976 return get_active_modifiers_experimental();
977}
978
979 static int
980get_active_modifiers_via_ptr(void)
981{
982 // marshal to corresponding implementation
983 return keycode_trans_strategy_used->ptr_get_active_modifiers();
984}
985
986/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100987 * Key hit, add it to the input buffer.
988 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100989 static void
990_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100991 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100992 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100993 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100994{
Anton Sharonov68d94722024-01-23 23:19:02 +0100995 // marshal to corresponding implementation
996 keycode_trans_strategy_used->ptr_on_char(hwnd, cch, cRepeat);
997}
998
999/*
1000 * Experimental implementation, introduced in v8.2.4807
1001 * "processing key event in Win32 GUI is not ideal"
1002 */
1003 static void
1004_OnChar_experimental(
1005 HWND hwnd UNUSED,
1006 UINT cch,
1007 int cRepeat UNUSED)
1008{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001009 char_u string[40];
1010 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001011 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +01001012 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001013
Anton Sharonov3f026672022-07-26 21:26:18 +01001014 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1015 return;
1016
1017 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
1018 // process_message()
1019 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1020 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001021
Anton Sharonov68d94722024-01-23 23:19:02 +01001022 modifiers = get_active_modifiers_experimental();
LemonBoy77fc0b02022-04-22 22:45:52 +01001023
1024 ch = simplify_key(ch, &modifiers);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001025
Christopher Plewright7b0afc12022-12-30 16:54:58 +00001026 // Some keys need adjustment when the Ctrl modifier is used.
1027 ++no_reduce_keys;
1028 ch = may_adjust_key_for_ctrl(modifiers, ch);
1029 --no_reduce_keys;
1030
LemonBoy77fc0b02022-04-22 22:45:52 +01001031 // remove the SHIFT modifier for keys where it's already included, e.g.,
1032 // '(' and '*'
1033 modifiers = may_remove_shift_modifier(modifiers, ch);
1034
1035 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1036 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1037 if (ch == CSI)
1038 ch = K_CSI;
1039
1040 if (modifiers)
1041 {
1042 string[0] = CSI;
1043 string[1] = KS_MODIFIER;
1044 string[2] = modifiers;
1045 add_to_input_buf(string, 3);
1046 }
1047
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001048 len = char_to_string(ch, string, 40, FALSE);
1049 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1050 {
1051 trash_input_buf();
1052 got_int = TRUE;
1053 }
1054
1055 add_to_input_buf(string, len);
1056}
1057
1058/*
Anton Sharonov68d94722024-01-23 23:19:02 +01001059 * "Classic" implementation, existing prior to v8.2.4807
1060 */
1061 static void
1062_OnChar_classic(
1063 HWND hwnd UNUSED,
1064 UINT ch,
1065 int cRepeat UNUSED)
1066{
1067 char_u string[40];
1068 int len = 0;
1069
1070 dead_key = 0;
1071
1072 len = char_to_string(ch, string, 40, FALSE);
1073 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1074 {
1075 trash_input_buf();
1076 got_int = TRUE;
1077 }
1078
1079 add_to_input_buf(string, len);
1080}
1081
1082/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001083 * Alt-Key hit, add it to the input buffer.
1084 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001085 static void
1086_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001087 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001088 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001089 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090{
Anton Sharonov68d94722024-01-23 23:19:02 +01001091 // marshal to corresponding implementation
1092 keycode_trans_strategy_used->ptr_on_sys_char(hwnd, cch, cRepeat);
1093}
1094
1095/*
1096 * Experimental implementation, introduced in v8.2.4807
1097 * "processing key event in Win32 GUI is not ideal"
1098 */
1099 static void
1100_OnSysChar_experimental(
1101 HWND hwnd UNUSED,
1102 UINT cch,
1103 int cRepeat UNUSED)
1104{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001105 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001106 int len;
1107 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001108 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001109
Anton Sharonov3f026672022-07-26 21:26:18 +01001110 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001111
Bram Moolenaar734a8672019-12-02 22:49:38 +01001112 // OK, we have a character key (given by ch) which was entered with the
1113 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1114 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1115 // CAPSLOCK is pressed) at this point.
Anton Sharonov68d94722024-01-23 23:19:02 +01001116 modifiers = get_active_modifiers_experimental();
1117 ch = simplify_key(ch, &modifiers);
1118 // remove the SHIFT modifier for keys where it's already included, e.g.,
1119 // '(' and '*'
1120 modifiers = may_remove_shift_modifier(modifiers, ch);
1121
1122 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1123 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1124 if (ch == CSI)
1125 ch = K_CSI;
1126
1127 len = 0;
1128 if (modifiers)
1129 {
1130 string[len++] = CSI;
1131 string[len++] = KS_MODIFIER;
1132 string[len++] = modifiers;
1133 }
1134
1135 if (IS_SPECIAL((int)ch))
1136 {
1137 string[len++] = CSI;
1138 string[len++] = K_SECOND((int)ch);
1139 string[len++] = K_THIRD((int)ch);
1140 }
1141 else
1142 {
1143 // Although the documentation isn't clear about it, we assume "ch" is
1144 // a Unicode character.
1145 len += char_to_string(ch, string + len, 40 - len, TRUE);
1146 }
1147
1148 add_to_input_buf(string, len);
1149}
1150
1151/*
1152 * "Classic" implementation, existing prior to v8.2.4807
1153 */
1154 static void
1155_OnSysChar_classic(
1156 HWND hwnd UNUSED,
1157 UINT cch,
1158 int cRepeat UNUSED)
1159{
1160 char_u string[40]; // Enough for multibyte character
1161 int len;
1162 int modifiers;
1163 int ch = cch; // special keys are negative
1164
1165 dead_key = 0;
1166
1167 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
1168
1169 // OK, we have a character key (given by ch) which was entered with the
1170 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1171 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1172 // CAPSLOCK is pressed) at this point.
1173 modifiers = MOD_MASK_ALT;
1174 if (GetKeyState(VK_SHIFT) & 0x8000)
1175 modifiers |= MOD_MASK_SHIFT;
1176 if (GetKeyState(VK_CONTROL) & 0x8000)
1177 modifiers |= MOD_MASK_CTRL;
1178
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001179 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001180 // remove the SHIFT modifier for keys where it's already included, e.g.,
1181 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001182 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001183
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001184 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1185 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186 if (ch == CSI)
1187 ch = K_CSI;
1188
1189 len = 0;
1190 if (modifiers)
1191 {
1192 string[len++] = CSI;
1193 string[len++] = KS_MODIFIER;
1194 string[len++] = modifiers;
1195 }
1196
1197 if (IS_SPECIAL((int)ch))
1198 {
1199 string[len++] = CSI;
1200 string[len++] = K_SECOND((int)ch);
1201 string[len++] = K_THIRD((int)ch);
1202 }
1203 else
1204 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001205 // Although the documentation isn't clear about it, we assume "ch" is
1206 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001207 len += char_to_string(ch, string + len, 40 - len, TRUE);
1208 }
1209
1210 add_to_input_buf(string, len);
1211}
1212
1213 static void
1214_OnMouseEvent(
1215 int button,
1216 int x,
1217 int y,
1218 int repeated_click,
1219 UINT keyFlags)
1220{
1221 int vim_modifiers = 0x0;
1222
1223 s_getting_focus = FALSE;
1224
1225 if (keyFlags & MK_SHIFT)
1226 vim_modifiers |= MOUSE_SHIFT;
1227 if (keyFlags & MK_CONTROL)
1228 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +01001229 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001230 vim_modifiers |= MOUSE_ALT;
1231
1232 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1233}
1234
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 static void
1236_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001237 HWND hwnd UNUSED,
1238 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001239 int x,
1240 int y,
1241 UINT keyFlags)
1242{
1243 static LONG s_prevTime = 0;
1244
1245 LONG currentTime = GetMessageTime();
1246 int button = -1;
1247 int repeated_click;
1248
Bram Moolenaar734a8672019-12-02 22:49:38 +01001249 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001250 (void)SetFocus(s_hwnd);
1251
1252 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1253 button = MOUSE_LEFT;
1254 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1255 button = MOUSE_MIDDLE;
1256 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1257 button = MOUSE_RIGHT;
1258 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1259 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001260 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1261 }
1262 else if (s_uMsg == WM_CAPTURECHANGED)
1263 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001264 // on W95/NT4, somehow you get in here with an odd Msg
1265 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266 if (s_button_pending == MOUSE_LEFT)
1267 button = MOUSE_RIGHT;
1268 else
1269 button = MOUSE_LEFT;
1270 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001271
1272 if (button < 0)
1273 return;
1274
1275 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1276
1277 /*
1278 * Holding down the left and right buttons simulates pushing the middle
1279 * button.
1280 */
1281 if (repeated_click
1282 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1283 || (button == MOUSE_RIGHT
1284 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001285 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001286 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001287 * Hmm, gui.c will ignore more than one button down at a time, so
1288 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001289 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001290 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1291 button = MOUSE_MIDDLE;
1292 repeated_click = FALSE;
1293 s_button_pending = -1;
1294 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001295 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001296 else if ((repeated_click)
1297 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1298 {
1299 if (s_button_pending > -1)
1300 {
1301 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1302 s_button_pending = -1;
1303 }
1304 // TRACE("Button down at x %d, y %d\n", x, y);
1305 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1306 }
1307 else
1308 {
1309 /*
1310 * If this is the first press (i.e. not a multiple click) don't
1311 * action immediately, but store and wait for:
1312 * i) button-up
1313 * ii) mouse move
1314 * iii) another button press
1315 * before using it.
1316 * This enables us to make left+right simulate middle button,
1317 * without left or right being actioned first. The side-effect is
1318 * that if you click and hold the mouse without dragging, the
1319 * cursor doesn't move until you release the button. In practice
1320 * this is hardly a problem.
1321 */
1322 s_button_pending = button;
1323 s_x_pending = x;
1324 s_y_pending = y;
1325 s_kFlags_pending = keyFlags;
1326 }
1327
1328 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001329}
1330
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001331 static void
1332_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001333 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001334 int x,
1335 int y,
1336 UINT keyFlags)
1337{
1338 int button;
1339
1340 s_getting_focus = FALSE;
1341 if (s_button_pending > -1)
1342 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001343 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001344 _OnMouseEvent(s_button_pending, s_x_pending,
1345 s_y_pending, FALSE, s_kFlags_pending);
1346 s_button_pending = -1;
1347 }
1348 if (s_uMsg == WM_MOUSEMOVE)
1349 {
1350 /*
1351 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1352 * down.
1353 */
1354 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1355 | MK_XBUTTON1 | MK_XBUTTON2)))
1356 {
1357 gui_mouse_moved(x, y);
1358 return;
1359 }
1360
1361 /*
1362 * While button is down, keep grabbing mouse move events when
1363 * the mouse goes outside the window
1364 */
1365 SetCapture(s_textArea);
1366 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001367 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001368 }
1369 else
1370 {
1371 ReleaseCapture();
1372 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001373 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001374 }
1375
1376 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1377}
1378
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001379 static void
1380_OnSizeTextArea(
1381 HWND hwnd UNUSED,
1382 UINT state UNUSED,
1383 int cx UNUSED,
1384 int cy UNUSED)
1385{
1386#if defined(FEAT_DIRECTX)
1387 if (IS_ENABLE_DIRECTX())
1388 directx_binddc();
1389#endif
1390}
1391
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001392#ifdef FEAT_MENU
1393/*
1394 * Find the vimmenu_T with the given id
1395 */
1396 static vimmenu_T *
1397gui_mswin_find_menu(
1398 vimmenu_T *pMenu,
1399 int id)
1400{
1401 vimmenu_T *pChildMenu;
1402
1403 while (pMenu)
1404 {
1405 if (pMenu->id == (UINT)id)
1406 break;
1407 if (pMenu->children != NULL)
1408 {
1409 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1410 if (pChildMenu)
1411 {
1412 pMenu = pChildMenu;
1413 break;
1414 }
1415 }
1416 pMenu = pMenu->next;
1417 }
1418 return pMenu;
1419}
1420
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001421 static void
1422_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001423 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001424 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001425 HWND hwndCtl UNUSED,
1426 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001427{
1428 vimmenu_T *pMenu;
1429
1430 pMenu = gui_mswin_find_menu(root_menu, id);
1431 if (pMenu)
1432 gui_menu_cb(pMenu);
1433}
1434#endif
1435
1436#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001437/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001438 * Handle a Find/Replace window message.
1439 */
1440 static void
1441_OnFindRepl(void)
1442{
1443 int flags = 0;
1444 int down;
1445
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001446 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001447 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448 (void)SetFocus(s_hwnd);
1449
1450 if (s_findrep_struct.Flags & FR_FINDNEXT)
1451 {
1452 flags = FRD_FINDNEXT;
1453
Bram Moolenaar734a8672019-12-02 22:49:38 +01001454 // Give main window the focus back: this is so the cursor isn't
1455 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001456 (void)SetFocus(s_hwnd);
1457 }
1458 else if (s_findrep_struct.Flags & FR_REPLACE)
1459 {
1460 flags = FRD_REPLACE;
1461
Bram Moolenaar734a8672019-12-02 22:49:38 +01001462 // Give main window the focus back: this is so the cursor isn't
1463 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001464 (void)SetFocus(s_hwnd);
1465 }
1466 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1467 {
1468 flags = FRD_REPLACEALL;
1469 }
1470
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001471 if (flags == 0)
1472 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001473
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001474 char_u *p, *q;
1475
1476 // Call the generic GUI function to do the actual work.
1477 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1478 flags |= FRD_WHOLE_WORD;
1479 if (s_findrep_struct.Flags & FR_MATCHCASE)
1480 flags |= FRD_MATCH_CASE;
1481 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1482 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1483 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1484 if (p != NULL && q != NULL)
1485 gui_do_findrepl(flags, p, q, down);
1486 vim_free(p);
1487 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001488}
1489#endif
1490
1491 static void
1492HandleMouseHide(UINT uMsg, LPARAM lParam)
1493{
1494 static LPARAM last_lParam = 0L;
1495
Bram Moolenaar734a8672019-12-02 22:49:38 +01001496 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001497 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1498 {
1499 if (lParam == last_lParam)
1500 return;
1501 last_lParam = lParam;
1502 }
1503
Bram Moolenaar734a8672019-12-02 22:49:38 +01001504 // Handle specially, to centralise coding. We need to be sure we catch all
1505 // possible events which should cause us to restore the cursor (as it is a
1506 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001507 switch (uMsg)
1508 {
1509 case WM_KEYUP:
1510 case WM_CHAR:
1511 /*
1512 * blank out the pointer if necessary
1513 */
1514 if (p_mh)
1515 gui_mch_mousehide(TRUE);
1516 break;
1517
Bram Moolenaar734a8672019-12-02 22:49:38 +01001518 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001519 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001520 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001521 case WM_LBUTTONDOWN:
1522 case WM_LBUTTONUP:
1523 case WM_MBUTTONDOWN:
1524 case WM_MBUTTONUP:
1525 case WM_RBUTTONDOWN:
1526 case WM_RBUTTONUP:
1527 case WM_XBUTTONDOWN:
1528 case WM_XBUTTONUP:
1529 case WM_NCMOUSEMOVE:
1530 case WM_NCLBUTTONDOWN:
1531 case WM_NCLBUTTONUP:
1532 case WM_NCMBUTTONDOWN:
1533 case WM_NCMBUTTONUP:
1534 case WM_NCRBUTTONDOWN:
1535 case WM_NCRBUTTONUP:
1536 case WM_KILLFOCUS:
1537 /*
1538 * if the pointer is currently hidden, then we should show it.
1539 */
1540 gui_mch_mousehide(FALSE);
1541 break;
1542 }
1543}
1544
1545 static LRESULT CALLBACK
1546_TextAreaWndProc(
1547 HWND hwnd,
1548 UINT uMsg,
1549 WPARAM wParam,
1550 LPARAM lParam)
1551{
1552 /*
1553 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1554 hwnd, uMsg, wParam, lParam);
1555 */
1556
1557 HandleMouseHide(uMsg, lParam);
1558
1559 s_uMsg = uMsg;
1560 s_wParam = wParam;
1561 s_lParam = lParam;
1562
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001563#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001564 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001565#endif
1566
1567 switch (uMsg)
1568 {
1569 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1570 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1571 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1572 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1573 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1574 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1575 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1576 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1577 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1578 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1579 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1580 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1581 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1582 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001583 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001585#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001586 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1587 return TRUE;
1588#endif
1589 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001590 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591 }
1592}
1593
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001594/*
1595 * Called when the foreground or background color has been changed.
1596 */
1597 void
1598gui_mch_new_colors(void)
1599{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001600 HBRUSH prevBrush;
1601
1602 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001603 prevBrush = (HBRUSH)SetClassLongPtr(
1604 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001605 InvalidateRect(s_hwnd, NULL, TRUE);
1606 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001607}
1608
1609/*
1610 * Set the colors to their default values.
1611 */
1612 void
1613gui_mch_def_colors(void)
1614{
1615 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1616 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1617 gui.def_norm_pixel = gui.norm_pixel;
1618 gui.def_back_pixel = gui.back_pixel;
1619}
1620
1621/*
1622 * Open the GUI window which was created by a call to gui_mch_init().
1623 */
1624 int
1625gui_mch_open(void)
1626{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001627 // Actually open the window, if not already visible
1628 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001629 if (!IsWindowVisible(s_hwnd))
1630 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1631
1632#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001633 // Init replace string here, so that we keep it when re-opening the
1634 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001635 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1636#endif
1637
1638 return OK;
1639}
1640
1641/*
1642 * Get the position of the top left corner of the window.
1643 */
1644 int
1645gui_mch_get_winpos(int *x, int *y)
1646{
1647 RECT rect;
1648
1649 GetWindowRect(s_hwnd, &rect);
1650 *x = rect.left;
1651 *y = rect.top;
1652 return OK;
1653}
1654
1655/*
1656 * Set the position of the top left corner of the window to the given
1657 * coordinates.
1658 */
1659 void
1660gui_mch_set_winpos(int x, int y)
1661{
1662 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1663 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1664}
1665 void
1666gui_mch_set_text_area_pos(int x, int y, int w, int h)
1667{
1668 static int oldx = 0;
1669 static int oldy = 0;
1670
1671 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1672
1673#ifdef FEAT_TOOLBAR
1674 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1675 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001676 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001677#endif
1678#if defined(FEAT_GUI_TABLINE)
1679 if (showing_tabline)
1680 {
1681 int top = 0;
1682 RECT rect;
1683
1684# ifdef FEAT_TOOLBAR
1685 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001686 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687# endif
1688 GetClientRect(s_hwnd, &rect);
1689 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1690 }
1691#endif
1692
Bram Moolenaar734a8672019-12-02 22:49:38 +01001693 // When side scroll bar is unshown, the size of window will change.
1694 // then, the text area move left or right. thus client rect should be
1695 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001696 if (oldx != x || oldy != y)
1697 {
1698 InvalidateRect(s_hwnd, NULL, FALSE);
1699 oldx = x;
1700 oldy = y;
1701 }
1702}
1703
1704
1705/*
1706 * Scrollbar stuff:
1707 */
1708
1709 void
1710gui_mch_enable_scrollbar(
1711 scrollbar_T *sb,
1712 int flag)
1713{
1714 ShowScrollBar(sb->id, SB_CTL, flag);
1715
Bram Moolenaar734a8672019-12-02 22:49:38 +01001716 // TODO: When the window is maximized, the size of the window stays the
1717 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1718 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001719}
1720
1721 void
1722gui_mch_set_scrollbar_pos(
1723 scrollbar_T *sb,
1724 int x,
1725 int y,
1726 int w,
1727 int h)
1728{
1729 SetWindowPos(sb->id, NULL, x, y, w, h,
1730 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1731}
1732
Bram Moolenaar203ec772020-07-17 20:43:43 +02001733 int
1734gui_mch_get_scrollbar_xpadding(void)
1735{
1736 RECT rcTxt, rcWnd;
1737 int xpad;
1738
1739 GetWindowRect(s_textArea, &rcTxt);
1740 GetWindowRect(s_hwnd, &rcWnd);
1741 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001742 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1743 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001744 return (xpad < 0) ? 0 : xpad;
1745}
1746
1747 int
1748gui_mch_get_scrollbar_ypadding(void)
1749{
1750 RECT rcTxt, rcWnd;
1751 int ypad;
1752
1753 GetWindowRect(s_textArea, &rcTxt);
1754 GetWindowRect(s_hwnd, &rcWnd);
1755 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001756 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1757 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001758 return (ypad < 0) ? 0 : ypad;
1759}
1760
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001761 void
1762gui_mch_create_scrollbar(
1763 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001764 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765{
1766 sb->id = CreateWindow(
1767 "SCROLLBAR", "Scrollbar",
1768 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001769 10, // Any value will do for now
1770 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001771 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001772 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001773}
1774
1775/*
1776 * Find the scrollbar with the given hwnd.
1777 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001778 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001779gui_mswin_find_scrollbar(HWND hwnd)
1780{
1781 win_T *wp;
1782
1783 if (gui.bottom_sbar.id == hwnd)
1784 return &gui.bottom_sbar;
1785 FOR_ALL_WINDOWS(wp)
1786 {
1787 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1788 return &wp->w_scrollbars[SBAR_LEFT];
1789 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1790 return &wp->w_scrollbars[SBAR_RIGHT];
1791 }
1792 return NULL;
1793}
1794
K.Takatac81e9bf2022-01-16 14:15:49 +00001795 static void
1796update_scrollbar_size(void)
1797{
1798 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1799 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1800}
1801
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001802/*
K.Takataabe628e2022-01-23 16:25:17 +00001803 * Get the average character size of a font.
1804 */
1805 static void
1806GetAverageFontSize(HDC hdc, SIZE *size)
1807{
1808 // GetTextMetrics() may not return the right value in tmAveCharWidth
1809 // for some fonts. Do our own average computation.
1810 GetTextExtentPoint(hdc,
1811 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1812 52, size);
1813 size->cx = (size->cx / 26 + 1) / 2;
1814}
1815
1816/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 * Get the character size of a font.
1818 */
1819 static void
Ken Takatada5da652023-10-05 20:20:58 +02001820GetFontSize(GuiFont font, int *char_width, int *char_height)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821{
1822 HWND hwnd = GetDesktopWindow();
1823 HDC hdc = GetWindowDC(hwnd);
1824 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001825 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001826 TEXTMETRIC tm;
1827
1828 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001829 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830
Ken Takatada5da652023-10-05 20:20:58 +02001831 if (char_width)
1832 *char_width = size.cx + tm.tmOverhang;
1833 if (char_height)
1834 *char_height = tm.tmHeight + p_linespace;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001835
1836 SelectFont(hdc, hfntOld);
1837
1838 ReleaseDC(hwnd, hdc);
1839}
1840
1841/*
Ken Takatada5da652023-10-05 20:20:58 +02001842 * Update the character size in "gui" structure with the specified font.
1843 */
1844 static void
1845UpdateFontSize(GuiFont font)
1846{
1847 GetFontSize(font, &gui.char_width, &gui.char_height);
1848}
1849
1850/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001851 * Adjust gui.char_height (after 'linespace' was changed).
1852 */
1853 int
1854gui_mch_adjust_charheight(void)
1855{
Ken Takatada5da652023-10-05 20:20:58 +02001856 UpdateFontSize(gui.norm_font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001857 return OK;
1858}
1859
1860 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001861get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862{
1863 HFONT font = NULL;
1864
Bram Moolenaar734a8672019-12-02 22:49:38 +01001865 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001866 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001867
1868 if (font == NULL)
1869 return NOFONT;
1870
1871 return (GuiFont)font;
1872}
1873
1874 static int
1875pixels_to_points(int pixels, int vertical)
1876{
1877 int points;
1878 HWND hwnd;
1879 HDC hdc;
1880
1881 hwnd = GetDesktopWindow();
1882 hdc = GetWindowDC(hwnd);
1883
1884 points = MulDiv(pixels, 72,
1885 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1886
1887 ReleaseDC(hwnd, hdc);
1888
1889 return points;
1890}
1891
1892 GuiFont
1893gui_mch_get_font(
1894 char_u *name,
1895 int giveErrorIfMissing)
1896{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001897 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001898 GuiFont font = NOFONT;
1899
1900 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001901 {
1902 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001903 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001904 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001905 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001906 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001907 return font;
1908}
1909
1910#if defined(FEAT_EVAL) || defined(PROTO)
1911/*
1912 * Return the name of font "font" in allocated memory.
1913 * Don't know how to get the actual name, thus use the provided name.
1914 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001915 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001916gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001917{
1918 if (name == NULL)
1919 return NULL;
1920 return vim_strsave(name);
1921}
1922#endif
1923
1924 void
1925gui_mch_free_font(GuiFont font)
1926{
1927 if (font)
1928 DeleteObject((HFONT)font);
1929}
1930
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001931/*
1932 * Return the Pixel value (color) for the given color name.
1933 * Return INVALCOLOR for error.
1934 */
1935 guicolor_T
1936gui_mch_get_color(char_u *name)
1937{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001938 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001939
1940 typedef struct SysColorTable
1941 {
1942 char *name;
1943 int color;
1944 } SysColorTable;
1945
1946 static SysColorTable sys_table[] =
1947 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001948 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1949 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001950#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001951 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1952#endif
1953 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1954 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1955 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1956 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1957 {"SYS_DESKTOP", COLOR_DESKTOP},
1958 {"SYS_INFOBK", COLOR_INFOBK},
1959 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1960 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001961 {"SYS_BTNFACE", COLOR_BTNFACE},
1962 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1963 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1964 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1965 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1966 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1967 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1968 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1969 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1970 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1971 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1972 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1973 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1974 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1975 {"SYS_MENU", COLOR_MENU},
1976 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1977 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1978 {"SYS_WINDOW", COLOR_WINDOW},
1979 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1980 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1981 };
1982
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001983 /*
1984 * Try to look up a system colour.
1985 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001986 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 if (STRICMP(name, sys_table[i].name) == 0)
1988 return GetSysColor(sys_table[i].color);
1989
Bram Moolenaarab302212016-04-26 20:59:29 +02001990 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001991}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001992
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001993 guicolor_T
1994gui_mch_get_rgb_color(int r, int g, int b)
1995{
1996 return gui_get_rgb_color_cmn(r, g, b);
1997}
1998
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001999/*
2000 * Return OK if the key with the termcap name "name" is supported.
2001 */
2002 int
2003gui_mch_haskey(char_u *name)
2004{
2005 int i;
2006
2007 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01002008 if (name[0] == special_keys[i].vim_code0
2009 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 return OK;
2011 return FAIL;
2012}
2013
2014 void
2015gui_mch_beep(void)
2016{
LemonBoy77771d32022-04-13 11:47:25 +01002017 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002018}
2019/*
2020 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2021 */
2022 void
2023gui_mch_invert_rectangle(
2024 int r,
2025 int c,
2026 int nr,
2027 int nc)
2028{
2029 RECT rc;
2030
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002031#if defined(FEAT_DIRECTX)
2032 if (IS_ENABLE_DIRECTX())
2033 DWriteContext_Flush(s_dwc);
2034#endif
2035
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002036 /*
2037 * Note: InvertRect() excludes right and bottom of rectangle.
2038 */
2039 rc.left = FILL_X(c);
2040 rc.top = FILL_Y(r);
2041 rc.right = rc.left + nc * gui.char_width;
2042 rc.bottom = rc.top + nr * gui.char_height;
2043 InvertRect(s_hdc, &rc);
2044}
2045
2046/*
2047 * Iconify the GUI window.
2048 */
2049 void
2050gui_mch_iconify(void)
2051{
2052 ShowWindow(s_hwnd, SW_MINIMIZE);
2053}
2054
2055/*
2056 * Draw a cursor without focus.
2057 */
2058 void
2059gui_mch_draw_hollow_cursor(guicolor_T color)
2060{
2061 HBRUSH hbr;
2062 RECT rc;
2063
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002064#if defined(FEAT_DIRECTX)
2065 if (IS_ENABLE_DIRECTX())
2066 DWriteContext_Flush(s_dwc);
2067#endif
2068
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002069 /*
2070 * Note: FrameRect() excludes right and bottom of rectangle.
2071 */
2072 rc.left = FILL_X(gui.col);
2073 rc.top = FILL_Y(gui.row);
2074 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002075 if (mb_lefthalve(gui.row, gui.col))
2076 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002077 rc.bottom = rc.top + gui.char_height;
2078 hbr = CreateSolidBrush(color);
2079 FrameRect(s_hdc, &rc, hbr);
2080 DeleteBrush(hbr);
2081}
2082/*
2083 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2084 * color "color".
2085 */
2086 void
2087gui_mch_draw_part_cursor(
2088 int w,
2089 int h,
2090 guicolor_T color)
2091{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002092 RECT rc;
2093
2094 /*
2095 * Note: FillRect() excludes right and bottom of rectangle.
2096 */
2097 rc.left =
2098#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002099 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002100 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2101#endif
2102 FILL_X(gui.col);
2103 rc.top = FILL_Y(gui.row) + gui.char_height - h;
2104 rc.right = rc.left + w;
2105 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002106
Bram Moolenaar92467d32017-12-05 13:22:16 +01002107 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002108}
2109
2110
2111/*
2112 * Generates a VK_SPACE when the internal dead_key flag is set to output the
2113 * dead key's nominal character and re-post the original message.
2114 */
2115 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01002116outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002117{
2118 static MSG deadCharExpel;
2119
Anton Sharonov3f026672022-07-26 21:26:18 +01002120 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121 return;
2122
Anton Sharonov3f026672022-07-26 21:26:18 +01002123 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002124
Bram Moolenaar734a8672019-12-02 22:49:38 +01002125 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002126 deadCharExpel.message = originalMsg.message;
2127 deadCharExpel.hwnd = originalMsg.hwnd;
2128 deadCharExpel.wParam = VK_SPACE;
2129
K.Takata4ac893f2022-01-20 12:44:28 +00002130 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002131
Bram Moolenaar734a8672019-12-02 22:49:38 +01002132 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002133 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
2134 originalMsg.lParam);
2135}
2136
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002137/*
Anton Sharonov3f026672022-07-26 21:26:18 +01002138 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
2139 */
2140 static void
2141outputDeadKey_rePost(MSG originalMsg)
2142{
2143 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
2144}
2145
2146/*
Anton Sharonov68d94722024-01-23 23:19:02 +01002147 * Refactored out part of process_message(), responsible for
2148 * handling the case of "not a special key"
2149 */
2150static void process_message_usual_key(UINT vk, const MSG *pmsg)
2151{
2152 // marshal to corresponding implementation
2153 keycode_trans_strategy_used->ptr_process_message_usual_key(vk, pmsg);
2154}
2155
2156/*
2157 * Experimental implementation, introduced in v8.2.4807
2158 * "processing key event in Win32 GUI is not ideal"
2159 */
John Marriott411ae582025-04-27 15:05:06 +02002160 static void
2161process_message_usual_key_experimental(UINT vk, const MSG *pmsg)
Anton Sharonov68d94722024-01-23 23:19:02 +01002162{
2163 WCHAR ch[8];
2164 int len;
2165 int i;
2166 UINT scan_code;
2167 BYTE keyboard_state[256];
2168
2169 // Construct the state table with only a few modifiers, we don't
2170 // really care about the presence of Ctrl/Alt as those modifiers are
2171 // handled by Vim separately.
2172 memset(keyboard_state, 0, 256);
2173 if (GetKeyState(VK_SHIFT) & 0x8000)
2174 keyboard_state[VK_SHIFT] = 0x80;
2175 if (GetKeyState(VK_CAPITAL) & 0x0001)
2176 keyboard_state[VK_CAPITAL] = 0x01;
2177 // Alt-Gr is synthesized as Alt + Ctrl.
2178 if ((GetKeyState(VK_RMENU) & 0x8000)
2179 && (GetKeyState(VK_CONTROL) & 0x8000))
2180 {
2181 keyboard_state[VK_MENU] = 0x80;
2182 keyboard_state[VK_CONTROL] = 0x80;
2183 }
2184
2185 // Translate the virtual key according to the current keyboard
2186 // layout.
2187 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2188 // Convert the scan-code into a sequence of zero or more unicode
2189 // codepoints.
2190 // If this is a dead key ToUnicode returns a negative value.
2191 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2192 0);
2193 if (len < 0)
2194 dead_key = DEAD_KEY_SET_DEFAULT;
2195
2196 if (len <= 0)
2197 {
2198 int wm_char = NUL;
2199
2200 if (dead_key == DEAD_KEY_SET_DEFAULT
2201 && (GetKeyState(VK_CONTROL) & 0x8000))
2202 {
2203 if ( // AZERTY CTRL+dead_circumflex
2204 (vk == 221 && scan_code == 26)
2205 // QWERTZ CTRL+dead_circumflex
2206 || (vk == 220 && scan_code == 41))
2207 wm_char = '[';
2208 if ( // QWERTZ CTRL+dead_two-overdots
2209 (vk == 192 && scan_code == 27))
2210 wm_char = ']';
2211 }
2212 if (wm_char != NUL)
2213 {
2214 // post WM_CHAR='[' - which will be interpreted with CTRL
2215 // still hold as ESC
2216 PostMessageW(pmsg->hwnd, WM_CHAR, wm_char, pmsg->lParam);
2217 // ask _OnChar() to not touch this state, wait for next key
2218 // press and maintain knowledge that we are "poisoned" with
2219 // "dead state"
2220 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2221 }
2222 return;
2223 }
2224
2225 // Post the message as TranslateMessage would do.
2226 if (pmsg->message == WM_KEYDOWN)
2227 {
2228 for (i = 0; i < len; i++)
2229 PostMessageW(pmsg->hwnd, WM_CHAR, ch[i], pmsg->lParam);
2230 }
2231 else
2232 {
2233 for (i = 0; i < len; i++)
2234 PostMessageW(pmsg->hwnd, WM_SYSCHAR, ch[i], pmsg->lParam);
2235 }
2236}
2237
2238/*
2239 * "Classic" implementation, existing prior to v8.2.4807
2240 */
John Marriott411ae582025-04-27 15:05:06 +02002241 static void
2242process_message_usual_key_classic(UINT vk, const MSG *pmsg)
Anton Sharonov68d94722024-01-23 23:19:02 +01002243{
2244 char_u string[40];
2245
2246 // Some keys need C-S- where they should only need C-.
2247 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2248 // system startup (Helmut Stiegler, 2003 Oct 3).
2249 if (vk != 0xff
2250 && (GetKeyState(VK_CONTROL) & 0x8000)
2251 && !(GetKeyState(VK_SHIFT) & 0x8000)
2252 && !(GetKeyState(VK_MENU) & 0x8000))
2253 {
2254 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
2255 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2256 {
2257 string[0] = Ctrl_HAT;
2258 add_to_input_buf(string, 1);
2259 }
2260 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2261 else if (vk == 0xBD) // QWERTY for CTRL-'-'
2262 {
2263 string[0] = Ctrl__;
2264 add_to_input_buf(string, 1);
2265 }
2266 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
2267 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2268 {
2269 string[0] = Ctrl_AT;
2270 add_to_input_buf(string, 1);
2271 }
2272 else
2273 TranslateMessage(pmsg);
2274 }
2275 else
2276 TranslateMessage(pmsg);
2277}
2278
2279/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002280 * Process a single Windows message.
2281 * If one is not available we hang until one is.
2282 */
2283 static void
2284process_message(void)
2285{
2286 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002287 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002288 char_u string[40];
2289 int i;
2290 int modifiers = 0;
2291 int key;
2292#ifdef FEAT_MENU
2293 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
2294#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002295 static int keycode_trans_strategy_initialized = 0;
2296
2297 // lazy initialize - first time only
2298 if (!keycode_trans_strategy_initialized)
2299 {
2300 keycode_trans_strategy_initialized = 1;
2301 keycode_trans_strategy_init();
2302 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303
K.Takatab7057bd2022-01-21 11:37:07 +00002304 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
2306#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002307 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002308 if (msg.message == WM_OLE)
2309 {
2310 char_u *str = (char_u *)msg.lParam;
2311 if (str == NULL || *str == NUL)
2312 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002313 // Message can't be ours, forward it. Fixes problem with Ultramon
2314 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00002315 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002316 }
2317 else
2318 {
2319 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01002320 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321 }
2322 return;
2323 }
2324#endif
2325
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002326#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002327 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00002328 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002329 {
2330 HandleMouseHide(msg.message, msg.lParam);
2331 return;
2332 }
2333#endif
2334
2335 /*
2336 * Check if it's a special key that we recognise. If not, call
2337 * TranslateMessage().
2338 */
2339 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
2340 {
2341 vk = (int) msg.wParam;
2342
2343 /*
2344 * Handle dead keys in special conditions in other cases we let Windows
2345 * handle them and do not interfere.
2346 *
2347 * The dead_key flag must be reset on several occasions:
2348 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
2349 * consumed at that point (This is when we let Windows combine the
2350 * dead character on its own)
2351 *
2352 * - Before doing something special such as regenerating keypresses to
2353 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00002354 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002355 * immediately to _OnChar() (or _OnSysChar()).
2356 */
Anton Sharonov3f026672022-07-26 21:26:18 +01002357
2358 /*
2359 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
2360 * was handled by _WndProc, this keypress we want to process normally
2361 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002362 if (keycode_trans_strategy_used->is_experimental()
2363 && dead_key == DEAD_KEY_SKIP_ON_CHAR)
2364 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002365 dead_key = DEAD_KEY_OFF;
Anton Sharonov68d94722024-01-23 23:19:02 +01002366 }
Anton Sharonov3f026672022-07-26 21:26:18 +01002367
2368 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002369 {
2370 /*
dundargocc57b5bc2022-11-02 13:30:51 +00002371 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01002372 *
2373 * After dead key was pressed with Ctrl in some cases, ESC was
2374 * artificially injected and handled by _OnChar(), now we are
2375 * dealing with completely new key press from the user. If we don't
2376 * do anything, ToUnicode() call will interpret this vk+scan_code
2377 * under influence of "dead-modifier". To prevent this we translate
2378 * this message replacing current char from user with VK_SPACE,
2379 * which will cause WM_CHAR with dead_key's character itself. Using
2380 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
2381 * ignore this one WM_CHAR event completely. Afterwards (due to
2382 * usage of PostMessage), this procedure is scheduled to be called
2383 * again with user char and on next entry we will clean
2384 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2385 * outputDeadKey_rePost() since we do not wish to reset dead_key
2386 * value.
2387 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002388 if (keycode_trans_strategy_used->is_experimental() &&
2389 dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
Anton Sharonov3f026672022-07-26 21:26:18 +01002390 {
2391 outputDeadKey_rePost_Ex(msg,
2392 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2393 return;
2394 }
2395
2396 if (dead_key != DEAD_KEY_SET_DEFAULT)
2397 {
2398 // should never happen - is there a way to make ASSERT here?
2399 return;
2400 }
2401
2402 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002403 * If a dead key was pressed and the user presses VK_SPACE,
2404 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2405 * with the dead char now, so do nothing special and let Windows
2406 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002407 *
2408 * Note that VK_SPACE combines with the dead_key's character and
2409 * only one WM_CHAR will be generated by TranslateMessage(), in
2410 * the two other cases two WM_CHAR will be generated: the dead
2411 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2412 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413 */
2414 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2415 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002416 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002417 TranslateMessage(&msg);
2418 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002420 // In modes where we are not typing, dead keys should behave
2421 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002422 else if ((get_real_state()
2423 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002424 {
2425 outputDeadKey_rePost(msg);
2426 return;
2427 }
2428 }
2429
Bram Moolenaar734a8672019-12-02 22:49:38 +01002430 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002431 if (vk == VK_CANCEL)
2432 {
2433 trash_input_buf();
2434 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002435 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436 string[0] = Ctrl_C;
2437 add_to_input_buf(string, 1);
2438 }
2439
LemonBoy77fc0b02022-04-22 22:45:52 +01002440 // This is an IME event or a synthetic keystroke, let Windows handle it.
2441 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2442 {
2443 TranslateMessage(&msg);
2444 return;
2445 }
2446
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447 for (i = 0; special_keys[i].key_sym != 0; i++)
2448 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002449 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002451 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452 {
2453 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002454 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 * is a key that would normally trigger the dead key nominal
2456 * character output (such as a NUMPAD printable character or
2457 * the TAB key, etc...).
2458 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002459 if (dead_key == DEAD_KEY_SET_DEFAULT
2460 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002461 || vk == VK_TAB || vk == CAR))
2462 {
2463 outputDeadKey_rePost(msg);
2464 return;
2465 }
2466
2467#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002468 // Check for <F10>: Windows selects the menu. When <F10> is
2469 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002470 if (vk == VK_F10
2471 && gui.menu_is_active
2472 && check_map(k10, State, FALSE, TRUE, FALSE,
2473 NULL, NULL) == NULL)
2474 break;
2475#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002476 modifiers = get_active_modifiers_via_ptr();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002477
2478 if (special_keys[i].vim_code1 == NUL)
2479 key = special_keys[i].vim_code0;
2480 else
2481 key = TO_SPECIAL(special_keys[i].vim_code0,
2482 special_keys[i].vim_code1);
2483 key = simplify_key(key, &modifiers);
2484 if (key == CSI)
2485 key = K_CSI;
2486
2487 if (modifiers)
2488 {
2489 string[0] = CSI;
2490 string[1] = KS_MODIFIER;
2491 string[2] = modifiers;
2492 add_to_input_buf(string, 3);
2493 }
2494
2495 if (IS_SPECIAL(key))
2496 {
2497 string[0] = CSI;
2498 string[1] = K_SECOND(key);
2499 string[2] = K_THIRD(key);
2500 add_to_input_buf(string, 3);
2501 }
2502 else
2503 {
2504 int len;
2505
Bram Moolenaar734a8672019-12-02 22:49:38 +01002506 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002507 len = char_to_string(key, string, 40, FALSE);
2508 add_to_input_buf(string, len);
2509 }
2510 break;
2511 }
2512 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002513
2514 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002515 if (special_keys[i].key_sym == 0)
2516 {
Anton Sharonov68d94722024-01-23 23:19:02 +01002517 process_message_usual_key(vk, &msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518 }
2519 }
2520#ifdef FEAT_MBYTE_IME
2521 else if (msg.message == WM_IME_NOTIFY)
2522 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2523 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002524 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002525 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002526#endif
2527
2528#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002529 // Check for <F10>: Default effect is to select the menu. When <F10> is
2530 // mapped we need to stop it here to avoid strange effects (e.g., for the
2531 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002532 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2533 NULL, NULL) == NULL)
2534#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002535 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002536}
2537
2538/*
2539 * Catch up with any queued events. This may put keyboard input into the
2540 * input buffer, call resize call-backs, trigger timers etc. If there is
2541 * nothing in the event queue (& no timers pending), then we return
2542 * immediately.
2543 */
2544 void
2545gui_mch_update(void)
2546{
2547 MSG msg;
2548
2549 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002550 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002551 && !vim_is_input_buf_full())
2552 process_message();
2553}
2554
Bram Moolenaar4231da42016-06-02 14:30:04 +02002555 static void
2556remove_any_timer(void)
2557{
2558 MSG msg;
2559
2560 if (s_wait_timer != 0 && !s_timed_out)
2561 {
2562 KillTimer(NULL, s_wait_timer);
2563
Bram Moolenaar734a8672019-12-02 22:49:38 +01002564 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002565 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002566 ;
2567 s_wait_timer = 0;
2568 }
2569}
2570
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002571/*
2572 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2573 * from the keyboard.
2574 * wtime == -1 Wait forever.
2575 * wtime == 0 This should never happen.
2576 * wtime > 0 Wait wtime milliseconds for a character.
2577 * Returns OK if a character was found to be available within the given time,
2578 * or FAIL otherwise.
2579 */
2580 int
2581gui_mch_wait_for_chars(int wtime)
2582{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002583 int focus;
2584
2585 s_timed_out = FALSE;
2586
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002587 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002588 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002589 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590 if (s_busy_processing)
2591 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002592
2593 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002594 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2595 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596 }
2597
2598 allow_scrollbar = TRUE;
2599
2600 focus = gui.in_focus;
2601 while (!s_timed_out)
2602 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002603 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002604 if (gui.in_focus != focus)
2605 {
2606 if (gui.in_focus)
2607 gui_mch_start_blink();
2608 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002609 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002610 focus = gui.in_focus;
2611 }
2612
2613 if (s_need_activate)
2614 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002615 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002616 s_need_activate = FALSE;
2617 }
2618
Bram Moolenaar4231da42016-06-02 14:30:04 +02002619#ifdef FEAT_TIMERS
2620 did_add_timer = FALSE;
2621#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002622#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002623 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002624 for (;;)
2625 {
2626 MSG msg;
2627
2628 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002629# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002630 if (did_add_timer)
2631 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002632# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002633 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002634 {
2635 process_message();
2636 break;
2637 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002638 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002639 // TODO: The 10 msec is a compromise between laggy response
2640 // and consuming more CPU time. Better would be to handle
2641 // channel messages when they arrive.
2642 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002643 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002644 break;
2645 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002646#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002647 // Don't use gui_mch_update() because then we will spin-lock until a
2648 // char arrives, instead we use GetMessage() to hang until an
2649 // event arrives. No need to check for input_buf_full because we are
2650 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002651 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002652#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002653
2654 if (input_available())
2655 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002656 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002657 allow_scrollbar = FALSE;
2658
Bram Moolenaar89c00032019-09-04 13:53:21 +02002659 // Clear pending mouse button, the release event may have been
2660 // taken by the dialog window. But don't do this when getting
2661 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 if (!s_getting_focus)
2663 s_button_pending = -1;
2664
2665 return OK;
2666 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002667
2668#ifdef FEAT_TIMERS
2669 if (did_add_timer)
2670 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002671 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002672 remove_any_timer();
2673 break;
2674 }
2675#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002676 }
2677 allow_scrollbar = FALSE;
2678 return FAIL;
2679}
2680
2681/*
2682 * Clear a rectangular region of the screen from text pos (row1, col1) to
2683 * (row2, col2) inclusive.
2684 */
2685 void
2686gui_mch_clear_block(
2687 int row1,
2688 int col1,
2689 int row2,
2690 int col2)
2691{
2692 RECT rc;
2693
2694 /*
2695 * Clear one extra pixel at the far right, for when bold characters have
2696 * spilled over to the window border.
2697 * Note: FillRect() excludes right and bottom of rectangle.
2698 */
2699 rc.left = FILL_X(col1);
2700 rc.top = FILL_Y(row1);
2701 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2702 rc.bottom = FILL_Y(row2 + 1);
2703 clear_rect(&rc);
2704}
2705
2706/*
2707 * Clear the whole text window.
2708 */
2709 void
2710gui_mch_clear_all(void)
2711{
2712 RECT rc;
2713
2714 rc.left = 0;
2715 rc.top = 0;
2716 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2717 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2718 clear_rect(&rc);
2719}
2720/*
2721 * Menu stuff.
2722 */
2723
2724 void
2725gui_mch_enable_menu(int flag)
2726{
2727#ifdef FEAT_MENU
2728 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2729#endif
2730}
2731
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732 void
2733gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002734 int x UNUSED,
2735 int y UNUSED,
2736 int w UNUSED,
2737 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002739 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740}
2741
2742#if defined(FEAT_MENU) || defined(PROTO)
2743/*
2744 * Make menu item hidden or not hidden
2745 */
2746 void
2747gui_mch_menu_hidden(
2748 vimmenu_T *menu,
2749 int hidden)
2750{
2751 /*
2752 * This doesn't do what we want. Hmm, just grey the menu items for now.
2753 */
2754 /*
2755 if (hidden)
2756 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2757 else
2758 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2759 */
2760 gui_mch_menu_grey(menu, hidden);
2761}
2762
2763/*
2764 * This is called after setting all the menus to grey/hidden or not.
2765 */
2766 void
2767gui_mch_draw_menubar(void)
2768{
2769 DrawMenuBar(s_hwnd);
2770}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002771#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002772
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773/*
2774 * Return the RGB value of a pixel as a long.
2775 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002776 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002777gui_mch_get_rgb(guicolor_T pixel)
2778{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002779 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2780 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781}
2782
2783#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002784/*
2785 * Convert pixels in X to dialog units
2786 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002787 static WORD
2788PixelToDialogX(int numPixels)
2789{
2790 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2791}
2792
Bram Moolenaar734a8672019-12-02 22:49:38 +01002793/*
2794 * Convert pixels in Y to dialog units
2795 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002796 static WORD
2797PixelToDialogY(int numPixels)
2798{
2799 return (WORD)((numPixels * 8) / s_dlgfntheight);
2800}
2801
Bram Moolenaar734a8672019-12-02 22:49:38 +01002802/*
2803 * Return the width in pixels of the given text in the given DC.
2804 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805 static int
2806GetTextWidth(HDC hdc, char_u *str, int len)
2807{
2808 SIZE size;
2809
2810 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2811 return size.cx;
2812}
2813
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002814/*
2815 * Return the width in pixels of the given text in the given DC, taking care
2816 * of 'encoding' to active codepage conversion.
2817 */
2818 static int
2819GetTextWidthEnc(HDC hdc, char_u *str, int len)
2820{
2821 SIZE size;
2822 WCHAR *wstr;
2823 int n;
2824 int wlen = len;
2825
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002826 wstr = enc_to_utf16(str, &wlen);
2827 if (wstr == NULL)
2828 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002829
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002830 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2831 vim_free(wstr);
2832 if (n)
2833 return size.cx;
2834 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002835}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002836
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002837static void get_work_area(RECT *spi_rect);
2838
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002839/*
2840 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002841 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2842 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002843 */
2844 static BOOL
2845CenterWindow(
2846 HWND hwndChild,
2847 HWND hwndParent)
2848{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002849 HMONITOR mon;
2850 MONITORINFO moninfo;
2851 RECT rChild, rParent, rScreen;
2852 int wChild, hChild, wParent, hParent;
2853 int xNew, yNew;
2854 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002855
2856 GetWindowRect(hwndChild, &rChild);
2857 wChild = rChild.right - rChild.left;
2858 hChild = rChild.bottom - rChild.top;
2859
Bram Moolenaar734a8672019-12-02 22:49:38 +01002860 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002861 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002862 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002863 else
2864 GetWindowRect(hwndParent, &rParent);
2865 wParent = rParent.right - rParent.left;
2866 hParent = rParent.bottom - rParent.top;
2867
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002868 moninfo.cbSize = sizeof(MONITORINFO);
2869 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2870 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002872 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002874 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002876 hdc = GetDC(hwndChild);
2877 rScreen.left = 0;
2878 rScreen.top = 0;
2879 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2880 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2881 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002882 }
2883
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002884 xNew = rParent.left + ((wParent - wChild) / 2);
2885 if (xNew < rScreen.left)
2886 xNew = rScreen.left;
2887 else if ((xNew + wChild) > rScreen.right)
2888 xNew = rScreen.right - wChild;
2889
2890 yNew = rParent.top + ((hParent - hChild) / 2);
2891 if (yNew < rScreen.top)
2892 yNew = rScreen.top;
2893 else if ((yNew + hChild) > rScreen.bottom)
2894 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002895
2896 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2897 SWP_NOSIZE | SWP_NOZORDER);
2898}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002899#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002901#if defined(FEAT_TOOLBAR) || defined(PROTO)
2902 void
2903gui_mch_show_toolbar(int showit)
2904{
2905 if (s_toolbarhwnd == NULL)
2906 return;
2907
2908 if (showit)
2909 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002910 // Enable unicode support
2911 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2912 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002913 ShowWindow(s_toolbarhwnd, SW_SHOW);
2914 }
2915 else
2916 ShowWindow(s_toolbarhwnd, SW_HIDE);
2917}
2918
Bram Moolenaar734a8672019-12-02 22:49:38 +01002919// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002920# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002921
2922#endif
2923
2924#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2925 static void
2926add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2927{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002928 WCHAR *wn;
2929 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002930
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002931 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002933 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002935 infow.cbSize = sizeof(infow);
2936 infow.fMask = MIIM_TYPE | MIIM_ID;
2937 infow.wID = item_id;
2938 infow.fType = MFT_STRING;
2939 infow.dwTypeData = wn;
2940 infow.cch = (UINT)wcslen(wn);
2941 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2942 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002943}
2944
2945 static void
2946show_tabline_popup_menu(void)
2947{
2948 HMENU tab_pmenu;
2949 long rval;
2950 POINT pt;
2951
Bram Moolenaar734a8672019-12-02 22:49:38 +01002952 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002953 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002954 return;
2955
2956 tab_pmenu = CreatePopupMenu();
2957 if (tab_pmenu == NULL)
2958 return;
2959
2960 if (first_tabpage->tp_next != NULL)
2961 add_tabline_popup_menu_entry(tab_pmenu,
2962 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2963 add_tabline_popup_menu_entry(tab_pmenu,
2964 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2965 add_tabline_popup_menu_entry(tab_pmenu,
2966 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2967
2968 GetCursorPos(&pt);
2969 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2970 NULL);
2971
2972 DestroyMenu(tab_pmenu);
2973
Bram Moolenaar734a8672019-12-02 22:49:38 +01002974 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002975 if (rval > 0)
2976 {
2977 TCHITTESTINFO htinfo;
2978 int idx;
2979
2980 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2981 return;
2982
2983 htinfo.pt.x = pt.x;
2984 htinfo.pt.y = pt.y;
2985 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2986 if (idx == -1)
2987 idx = 0;
2988 else
2989 idx += 1;
2990
2991 send_tabline_menu_event(idx, (int)rval);
2992 }
2993}
2994
2995/*
2996 * Show or hide the tabline.
2997 */
2998 void
2999gui_mch_show_tabline(int showit)
3000{
3001 if (s_tabhwnd == NULL)
3002 return;
3003
3004 if (!showit != !showing_tabline)
3005 {
3006 if (showit)
3007 ShowWindow(s_tabhwnd, SW_SHOW);
3008 else
3009 ShowWindow(s_tabhwnd, SW_HIDE);
3010 showing_tabline = showit;
3011 }
3012}
3013
3014/*
3015 * Return TRUE when tabline is displayed.
3016 */
3017 int
3018gui_mch_showing_tabline(void)
3019{
3020 return s_tabhwnd != NULL && showing_tabline;
3021}
3022
3023/*
3024 * Update the labels of the tabline.
3025 */
3026 void
3027gui_mch_update_tabline(void)
3028{
3029 tabpage_T *tp;
3030 TCITEM tie;
3031 int nr = 0;
3032 int curtabidx = 0;
3033 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003034 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003035
3036 if (s_tabhwnd == NULL)
3037 return;
3038
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003039 // Enable unicode support
3040 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003041
3042 tie.mask = TCIF_TEXT;
3043 tie.iImage = -1;
3044
Bram Moolenaar734a8672019-12-02 22:49:38 +01003045 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
3047
Bram Moolenaar734a8672019-12-02 22:49:38 +01003048 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003049 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3050 {
3051 if (tp == curtab)
3052 curtabidx = nr;
3053
3054 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
3055 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003056 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057 tie.pszText = "-Empty-";
3058 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
3059 tabadded = 1;
3060 }
3061
3062 get_tabline_label(tp, FALSE);
3063 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003064
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003065 wstr = enc_to_utf16(NameBuff, NULL);
3066 if (wstr != NULL)
3067 {
3068 TCITEMW tiw;
3069
3070 tiw.mask = TCIF_TEXT;
3071 tiw.iImage = -1;
3072 tiw.pszText = wstr;
3073 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
3074 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003075 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 }
3077
Bram Moolenaar734a8672019-12-02 22:49:38 +01003078 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003079 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
3080 TabCtrl_DeleteItem(s_tabhwnd, nr);
3081
3082 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3083 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3084
Bram Moolenaar734a8672019-12-02 22:49:38 +01003085 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003086 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
3087 RedrawWindow(s_tabhwnd, NULL, NULL,
3088 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
3089
3090 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3091 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3092}
3093
3094/*
3095 * Set the current tab to "nr". First tab is 1.
3096 */
3097 void
3098gui_mch_set_curtab(int nr)
3099{
3100 if (s_tabhwnd == NULL)
3101 return;
3102
3103 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
3104 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
3105}
3106
3107#endif
3108
3109/*
3110 * ":simalt" command.
3111 */
3112 void
3113ex_simalt(exarg_T *eap)
3114{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003115 char_u *keys = eap->arg;
3116 int fill_typebuf = FALSE;
3117 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003118
3119 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
3120 while (*keys)
3121 {
3122 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01003123 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003124 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
3125 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003126 fill_typebuf = TRUE;
3127 }
3128 if (fill_typebuf)
3129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003130 // Put a NOP in the typeahead buffer so that the message will get
3131 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003132 key_name[0] = K_SPECIAL;
3133 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02003134 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003135 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003136#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003137 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003138#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003139 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140 }
3141}
3142
3143/*
3144 * Create the find & replace dialogs.
3145 * You can't have both at once: ":find" when replace is showing, destroys
3146 * the replace dialog first, and the other way around.
3147 */
3148#ifdef MSWIN_FIND_REPLACE
3149 static void
3150initialise_findrep(char_u *initial_string)
3151{
3152 int wword = FALSE;
3153 int mcase = !p_ic;
3154 char_u *entry_text;
3155
Bram Moolenaar734a8672019-12-02 22:49:38 +01003156 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003157 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
3158
3159 s_findrep_struct.hwndOwner = s_hwnd;
3160 s_findrep_struct.Flags = FR_DOWN;
3161 if (mcase)
3162 s_findrep_struct.Flags |= FR_MATCHCASE;
3163 if (wword)
3164 s_findrep_struct.Flags |= FR_WHOLEWORD;
3165 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003166 {
3167 WCHAR *p = enc_to_utf16(entry_text, NULL);
3168 if (p != NULL)
3169 {
3170 int len = s_findrep_struct.wFindWhatLen - 1;
3171
3172 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
3173 s_findrep_struct.lpstrFindWhat[len] = NUL;
3174 vim_free(p);
3175 }
3176 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003177 vim_free(entry_text);
3178}
3179#endif
3180
3181 static void
3182set_window_title(HWND hwnd, char *title)
3183{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003184 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185 {
3186 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187
Bram Moolenaar734a8672019-12-02 22:49:38 +01003188 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
3190 if (wbuf != NULL)
3191 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003192 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003193 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003194 }
3195 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003196 else
3197 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003198}
3199
3200 void
3201gui_mch_find_dialog(exarg_T *eap)
3202{
3203#ifdef MSWIN_FIND_REPLACE
3204 if (s_findrep_msg != 0)
3205 {
3206 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
3207 DestroyWindow(s_findrep_hwnd);
3208
3209 if (!IsWindow(s_findrep_hwnd))
3210 {
3211 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003212 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003213 }
3214
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003215 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003216 (void)SetFocus(s_findrep_hwnd);
3217
3218 s_findrep_is_find = TRUE;
3219 }
3220#endif
3221}
3222
3223
3224 void
3225gui_mch_replace_dialog(exarg_T *eap)
3226{
3227#ifdef MSWIN_FIND_REPLACE
3228 if (s_findrep_msg != 0)
3229 {
3230 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
3231 DestroyWindow(s_findrep_hwnd);
3232
3233 if (!IsWindow(s_findrep_hwnd))
3234 {
3235 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003236 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003237 }
3238
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003239 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003240 (void)SetFocus(s_findrep_hwnd);
3241
3242 s_findrep_is_find = FALSE;
3243 }
3244#endif
3245}
3246
3247
3248/*
3249 * Set visibility of the pointer.
3250 */
3251 void
3252gui_mch_mousehide(int hide)
3253{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003254 if (hide == gui.pointer_hidden)
3255 return;
3256
3257 ShowCursor(!hide);
3258 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003259}
3260
3261#ifdef FEAT_MENU
3262 static void
3263gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
3264{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003265 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266 gui_mch_mousehide(FALSE);
3267
3268 (void)TrackPopupMenu(
3269 (HMENU)menu->submenu_id,
3270 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3271 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01003272 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003273 s_hwnd,
3274 NULL);
3275 /*
3276 * NOTE: The pop-up menu can eat the mouse up event.
3277 * We deal with this in normal.c.
3278 */
3279}
3280#endif
3281
3282/*
3283 * Got a message when the system will go down.
3284 */
3285 static void
3286_OnEndSession(void)
3287{
3288 getout_preserve_modified(1);
3289}
3290
3291/*
3292 * Get this message when the user clicks on the cross in the top right corner
3293 * of a Windows95 window.
3294 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003296_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003297{
3298 gui_shell_closed();
3299}
3300
3301/*
3302 * Get a message when the window is being destroyed.
3303 */
3304 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003305_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003306{
3307 if (!destroying)
3308 _OnClose(hwnd);
3309}
3310
3311 static void
3312_OnPaint(
3313 HWND hwnd)
3314{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003315 if (IsMinimized(hwnd))
3316 return;
3317
3318 PAINTSTRUCT ps;
3319
3320 out_flush(); // make sure all output has been processed
3321 (void)BeginPaint(hwnd, &ps);
3322
3323 // prevent multi-byte characters from misprinting on an invalid
3324 // rectangle
3325 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003327 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003329 GetClientRect(hwnd, &rect);
3330 ps.rcPaint.left = rect.left;
3331 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003333
3334 if (!IsRectEmpty(&ps.rcPaint))
3335 {
3336 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3337 ps.rcPaint.right - ps.rcPaint.left + 1,
3338 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3339 }
3340
3341 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342}
3343
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344 static void
3345_OnSize(
3346 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003347 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348 int cx,
3349 int cy)
3350{
K.Takatac81e9bf2022-01-16 14:15:49 +00003351 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352 {
3353 gui_resize_shell(cx, cy);
3354
Bram Moolenaar734a8672019-12-02 22:49:38 +01003355 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003356 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357 }
3358}
3359
3360 static void
3361_OnSetFocus(
3362 HWND hwnd,
3363 HWND hwndOldFocus)
3364{
3365 gui_focus_change(TRUE);
3366 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003367 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003368}
3369
3370 static void
3371_OnKillFocus(
3372 HWND hwnd,
3373 HWND hwndNewFocus)
3374{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003375 if (destroying)
3376 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003377 gui_focus_change(FALSE);
3378 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003379 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003380}
3381
3382/*
3383 * Get a message when the user switches back to vim
3384 */
3385 static LRESULT
3386_OnActivateApp(
3387 HWND hwnd,
3388 BOOL fActivate,
3389 DWORD dwThreadId)
3390{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003391 // we call gui_focus_change() in _OnSetFocus()
3392 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003393 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394}
3395
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396 void
3397gui_mch_destroy_scrollbar(scrollbar_T *sb)
3398{
3399 DestroyWindow(sb->id);
3400}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003401
3402/*
3403 * Get current mouse coordinates in text window.
3404 */
3405 void
3406gui_mch_getmouse(int *x, int *y)
3407{
3408 RECT rct;
3409 POINT mp;
3410
3411 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003412 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003413 *x = (int)(mp.x - rct.left);
3414 *y = (int)(mp.y - rct.top);
3415}
3416
3417/*
3418 * Move mouse pointer to character at (x, y).
3419 */
3420 void
3421gui_mch_setmouse(int x, int y)
3422{
3423 RECT rct;
3424
3425 (void)GetWindowRect(s_textArea, &rct);
3426 (void)SetCursorPos(x + gui.border_offset + rct.left,
3427 y + gui.border_offset + rct.top);
3428}
3429
3430 static void
3431gui_mswin_get_valid_dimensions(
3432 int w,
3433 int h,
3434 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003435 int *valid_h,
3436 int *cols,
3437 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003438{
3439 int base_width, base_height;
3440
3441 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003442 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3443 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003445 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3446 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3447 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3448 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003449 *cols = (w - base_width) / gui.char_width;
3450 *rows = (h - base_height) / gui.char_height;
3451 *valid_w = base_width + *cols * gui.char_width;
3452 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003453}
3454
3455 void
3456gui_mch_flash(int msec)
3457{
3458 RECT rc;
3459
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003460#if defined(FEAT_DIRECTX)
3461 if (IS_ENABLE_DIRECTX())
3462 DWriteContext_Flush(s_dwc);
3463#endif
3464
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003465 /*
3466 * Note: InvertRect() excludes right and bottom of rectangle.
3467 */
3468 rc.left = 0;
3469 rc.top = 0;
3470 rc.right = gui.num_cols * gui.char_width;
3471 rc.bottom = gui.num_rows * gui.char_height;
3472 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003473 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474
Bram Moolenaar734a8672019-12-02 22:49:38 +01003475 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476
3477 InvertRect(s_hdc, &rc);
3478}
3479
3480/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003481 * Check if the specified point is on-screen. (multi-monitor aware)
3482 */
3483 static BOOL
3484is_point_onscreen(int x, int y)
3485{
3486 POINT pt = {x, y};
3487
3488 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3489}
3490
3491/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003492 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003493 *
3494 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3495 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3496 * only works when the whole window is on-screen.
3497 */
3498 static BOOL
3499is_window_onscreen(HWND hwnd)
3500{
3501 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003502 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003503
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003504 GetClientRect(hwnd, &rc);
3505 p1.x = rc.left;
3506 p1.y = rc.top;
3507 p2.x = rc.right - 1;
3508 p2.y = rc.bottom - 1;
3509 ClientToScreen(hwnd, &p1);
3510 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003511
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003512 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003513 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003514 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003515 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003516 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003517 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003518 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003519 return FALSE;
3520 return TRUE;
3521}
3522
3523/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003524 * Return flags used for scrolling.
3525 * The SW_INVALIDATE is required when part of the window is covered or
3526 * off-screen. Refer to MS KB Q75236.
3527 */
3528 static int
3529get_scroll_flags(void)
3530{
3531 HWND hwnd;
3532 RECT rcVim, rcOther, rcDest;
3533
Bram Moolenaar185577e2020-10-29 20:08:21 +01003534 // Check if the window is (partly) off-screen.
3535 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003536 return SW_INVALIDATE;
3537
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003538 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003539 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3541 if (IsWindowVisible(hwnd))
3542 {
3543 GetWindowRect(hwnd, &rcOther);
3544 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3545 return SW_INVALIDATE;
3546 }
3547 return 0;
3548}
3549
3550/*
3551 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3552 * may not be scrolled out properly.
3553 * For gVim, when _OnScroll() is repeated, the character at the
3554 * previous cursor position may be left drawn after scroll.
3555 * The problem can be avoided by calling GetPixel() to get a pixel in
3556 * the region before ScrollWindowEx().
3557 */
3558 static void
3559intel_gpu_workaround(void)
3560{
3561 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3562}
3563
3564/*
3565 * Delete the given number of lines from the given row, scrolling up any
3566 * text further down within the scroll region.
3567 */
3568 void
3569gui_mch_delete_lines(
3570 int row,
3571 int num_lines)
3572{
3573 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003574
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575 rc.left = FILL_X(gui.scroll_region_left);
3576 rc.right = FILL_X(gui.scroll_region_right + 1);
3577 rc.top = FILL_Y(row);
3578 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3579
Bram Moolenaar92467d32017-12-05 13:22:16 +01003580#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003581 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003582 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003583 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003584 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003585 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003586#endif
3587 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003588#if defined(FEAT_DIRECTX)
3589 if (IS_ENABLE_DIRECTX())
3590 DWriteContext_Flush(s_dwc);
3591#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003592 intel_gpu_workaround();
3593 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003594 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003595 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003596 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003597
Bram Moolenaar734a8672019-12-02 22:49:38 +01003598 // This seems to be required to avoid the cursor disappearing when
3599 // scrolling such that the cursor ends up in the top-left character on
3600 // the screen... But why? (Webb)
3601 // It's probably fixed by disabling drawing the cursor while scrolling.
3602 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003603
3604 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3605 gui.scroll_region_left,
3606 gui.scroll_region_bot, gui.scroll_region_right);
3607}
3608
3609/*
3610 * Insert the given number of lines before the given row, scrolling down any
3611 * following text within the scroll region.
3612 */
3613 void
3614gui_mch_insert_lines(
3615 int row,
3616 int num_lines)
3617{
3618 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003619
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620 rc.left = FILL_X(gui.scroll_region_left);
3621 rc.right = FILL_X(gui.scroll_region_right + 1);
3622 rc.top = FILL_Y(row);
3623 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003624
3625#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003626 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003627 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003628 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003629 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003630 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003631#endif
3632 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003633#if defined(FEAT_DIRECTX)
3634 if (IS_ENABLE_DIRECTX())
3635 DWriteContext_Flush(s_dwc);
3636#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003637 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003638 // The SW_INVALIDATE is required when part of the window is covered or
3639 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003640 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003641 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003642 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003643 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003644
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003645 gui_clear_block(row, gui.scroll_region_left,
3646 row + num_lines - 1, gui.scroll_region_right);
3647}
3648
3649
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003651gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652{
3653#if defined(FEAT_DIRECTX)
3654 DWriteContext_Close(s_dwc);
3655 DWrite_Final();
3656 s_dwc = NULL;
3657#endif
3658
3659 ReleaseDC(s_textArea, s_hdc);
3660 DeleteObject(s_brush);
3661
3662#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003663 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3665#endif
3666
Bram Moolenaar734a8672019-12-02 22:49:38 +01003667 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 if (s_hwnd != NULL)
3669 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003670 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671 DestroyWindow(s_hwnd);
3672 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003673}
3674
3675 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003676logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003678 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003679 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003680 char *font_name;
John Marriott411ae582025-04-27 15:05:06 +02003681 size_t res_size;
3682 char *res;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003683
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003684 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3685 if (font_name == NULL)
3686 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003687 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003688 quality_name = quality_id2name((int)lf.lfQuality);
3689
John Marriott411ae582025-04-27 15:05:06 +02003690 res_size = STRLEN(font_name) + 30
3691 + (charset_name == NULL ? 0 : STRLEN(charset_name) + 2)
3692 + (quality_name == NULL ? 0 : STRLEN(quality_name) + 2);
3693 res = alloc(res_size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003694 if (res != NULL)
3695 {
John Marriott411ae582025-04-27 15:05:06 +02003696 char *p;
3697 int points;
3698 size_t res_len;
3699
3700 // replace spaces in font_name with underscores.
3701 for (p = font_name; *p != NUL; ++p)
3702 {
3703 if (isspace(*p))
3704 *p = '_';
3705 }
3706
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003707 // make a normal font string out of the lf thing:
3708 points = pixels_to_points(
3709 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3710 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
John Marriott411ae582025-04-27 15:05:06 +02003711 res_len = vim_snprintf_safelen(
3712 (char *)res, res_size, "%s:h%d", font_name, points);
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003713 else
John Marriott411ae582025-04-27 15:05:06 +02003714 res_len = vim_snprintf_safelen(
3715 (char *)res, res_size, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
3716
3717 res_len += vim_snprintf_safelen(
3718 (char *)res + res_len,
3719 res_size - res_len,
3720 "%s%s%s%s",
3721 lf.lfItalic ? ":i" : "",
3722 lf.lfWeight ? ":b" : "",
3723 lf.lfUnderline ? ":u" : "",
3724 lf.lfStrikeOut ? ":s" : "");
3725
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 if (charset_name != NULL)
John Marriott411ae582025-04-27 15:05:06 +02003727 res_len += vim_snprintf_safelen((char *)res + res_len,
3728 res_size - res_len, ":c%s", charset_name);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003729 if (quality_name != NULL)
John Marriott411ae582025-04-27 15:05:06 +02003730 vim_snprintf((char *)res + res_len,
3731 res_size - res_len, ":q%s", quality_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 }
3733
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003734 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 return (char_u *)res;
3736}
3737
3738
3739#ifdef FEAT_MBYTE_IME
3740/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003741 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003742 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003743 */
3744 static void
3745update_im_font(void)
3746{
K.Takatac81e9bf2022-01-16 14:15:49 +00003747 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003748
3749 if (p_guifontwide != NULL && *p_guifontwide != NUL
3750 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003751 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003752 norm_logfont = lf_wide;
3753 else
3754 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003755
3756 lf = norm_logfont;
3757 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3758 // Work around when PerMonitorV2 is not enabled in the process level.
3759 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3760 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761}
3762#endif
3763
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003764/*
3765 * Handler of gui.wide_font (p_guifontwide) changed notification.
3766 */
3767 void
3768gui_mch_wide_font_changed(void)
3769{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003770 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003772#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003774#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003775
3776 gui_mch_free_font(gui.wide_ital_font);
3777 gui.wide_ital_font = NOFONT;
3778 gui_mch_free_font(gui.wide_bold_font);
3779 gui.wide_bold_font = NOFONT;
3780 gui_mch_free_font(gui.wide_boldital_font);
3781 gui.wide_boldital_font = NOFONT;
3782
3783 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003784 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785 {
3786 if (!lf.lfItalic)
3787 {
3788 lf.lfItalic = TRUE;
3789 gui.wide_ital_font = get_font_handle(&lf);
3790 lf.lfItalic = FALSE;
3791 }
3792 if (lf.lfWeight < FW_BOLD)
3793 {
3794 lf.lfWeight = FW_BOLD;
3795 gui.wide_bold_font = get_font_handle(&lf);
3796 if (!lf.lfItalic)
3797 {
3798 lf.lfItalic = TRUE;
3799 gui.wide_boldital_font = get_font_handle(&lf);
3800 }
3801 }
3802 }
3803}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003804
3805/*
3806 * Initialise vim to use the font with the given name.
3807 * Return FAIL if the font could not be loaded, OK otherwise.
3808 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003810gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003811{
K.Takatac81e9bf2022-01-16 14:15:49 +00003812 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003813 GuiFont font = NOFONT;
3814 char_u *p;
3815
Bram Moolenaar734a8672019-12-02 22:49:38 +01003816 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003818 {
3819 lfOrig = lf;
3820 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003822 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003823 if (font == NOFONT)
3824 return FAIL;
3825
3826 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003827 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003828#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003829 norm_logfont = lf;
3830 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003831 if (!s_in_dpichanged)
3832 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003833#endif
3834 gui_mch_free_font(gui.norm_font);
3835 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003836 current_font_height = lfOrig.lfHeight;
Ken Takatada5da652023-10-05 20:20:58 +02003837 UpdateFontSize(font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838
K.Takatac81e9bf2022-01-16 14:15:49 +00003839 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003840 if (p != NULL)
3841 {
3842 hl_set_font_name(p);
3843
Bram Moolenaar734a8672019-12-02 22:49:38 +01003844 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3846 {
3847 vim_free(p_guifont);
3848 p_guifont = p;
3849 }
3850 else
3851 vim_free(p);
3852 }
3853
3854 gui_mch_free_font(gui.ital_font);
3855 gui.ital_font = NOFONT;
3856 gui_mch_free_font(gui.bold_font);
3857 gui.bold_font = NOFONT;
3858 gui_mch_free_font(gui.boldital_font);
3859 gui.boldital_font = NOFONT;
3860
3861 if (!lf.lfItalic)
3862 {
3863 lf.lfItalic = TRUE;
3864 gui.ital_font = get_font_handle(&lf);
3865 lf.lfItalic = FALSE;
3866 }
3867 if (lf.lfWeight < FW_BOLD)
3868 {
3869 lf.lfWeight = FW_BOLD;
3870 gui.bold_font = get_font_handle(&lf);
3871 if (!lf.lfItalic)
3872 {
3873 lf.lfItalic = TRUE;
3874 gui.boldital_font = get_font_handle(&lf);
3875 }
3876 }
3877
3878 return OK;
3879}
3880
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003881/*
3882 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003883 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884 */
3885 int
3886gui_mch_maximized(void)
3887{
3888 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003889 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890
3891 wp.length = sizeof(WINDOWPLACEMENT);
3892 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003893 {
3894 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003895 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003896 && wp.flags == WPF_RESTORETOMAXIMIZED))
3897 return TRUE;
3898 if (wp.showCmd == SW_SHOWMINIMIZED)
3899 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003900
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003901 // Assume the window is snapped when the sizes from two APIs differ.
3902 GetWindowRect(s_hwnd, &rc);
3903 if ((rc.right - rc.left !=
3904 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3905 || (rc.bottom - rc.top !=
3906 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3907 return TRUE;
3908 }
3909 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910}
3911
3912/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003913 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3914 * is set. Compute the new Rows and Columns. This is like resizing the
3915 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916 */
3917 void
3918gui_mch_newfont(void)
3919{
3920 RECT rect;
3921
3922 GetWindowRect(s_hwnd, &rect);
3923 if (win_socket_id == 0)
3924 {
3925 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003926 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3927 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003929 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3930 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3931 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3932 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 }
3934 else
3935 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003936 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003937 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003938 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939 }
3940}
3941
3942/*
3943 * Set the window title
3944 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003945 void
3946gui_mch_settitle(
3947 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003948 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003949{
3950 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3951}
3952
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003953#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003954// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3955// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003956static LPCSTR mshape_idcs[] =
3957{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003958 IDC_ARROW, // arrow
3959 MAKEINTRESOURCE(0), // blank
3960 IDC_IBEAM, // beam
3961 IDC_SIZENS, // updown
3962 IDC_SIZENS, // udsizing
3963 IDC_SIZEWE, // leftright
3964 IDC_SIZEWE, // lrsizing
3965 IDC_WAIT, // busy
3966 IDC_NO, // no
3967 IDC_ARROW, // crosshair
3968 IDC_ARROW, // hand1
3969 IDC_ARROW, // hand2
3970 IDC_ARROW, // pencil
3971 IDC_ARROW, // question
3972 IDC_ARROW, // right-arrow
3973 IDC_UPARROW, // up-arrow
3974 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003975};
3976
3977 void
3978mch_set_mouse_shape(int shape)
3979{
3980 LPCSTR idc;
3981
3982 if (shape == MSHAPE_HIDE)
3983 ShowCursor(FALSE);
3984 else
3985 {
3986 if (shape >= MSHAPE_NUMBERED)
3987 idc = IDC_ARROW;
3988 else
3989 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003990 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003991 if (!p_mh)
3992 {
3993 POINT mp;
3994
Bram Moolenaar734a8672019-12-02 22:49:38 +01003995 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003996 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003997 (void)SetCursorPos(mp.x, mp.y);
3998 ShowCursor(TRUE);
3999 }
4000 }
4001}
4002#endif
4003
Bram Moolenaara6b7a082016-08-10 20:53:05 +02004004#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004005/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004006 * Wide version of convert_filter().
4007 */
4008 static WCHAR *
4009convert_filterW(char_u *s)
4010{
4011 char_u *tmp;
4012 int len;
4013 WCHAR *res;
4014
4015 tmp = convert_filter(s);
4016 if (tmp == NULL)
4017 return NULL;
4018 len = (int)STRLEN(s) + 3;
4019 res = enc_to_utf16(tmp, &len);
4020 vim_free(tmp);
4021 return res;
4022}
4023
4024/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004025 * Pop open a file browser and return the file selected, in allocated memory,
4026 * or NULL if Cancel is hit.
4027 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
4028 * title - Title message for the file browser dialog.
4029 * dflt - Default name of file.
4030 * ext - Default extension to be added to files without extensions.
4031 * initdir - directory in which to open the browser (NULL = current dir)
4032 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004033 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01004034 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004035gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004036 int saving,
4037 char_u *title,
4038 char_u *dflt,
4039 char_u *ext,
4040 char_u *initdir,
4041 char_u *filter)
4042{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004043 // We always use the wide function. This means enc_to_utf16() must work,
4044 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004045 OPENFILENAMEW fileStruct;
4046 WCHAR fileBuf[MAXPATHL];
4047 WCHAR *wp;
4048 int i;
4049 WCHAR *titlep = NULL;
4050 WCHAR *extp = NULL;
4051 WCHAR *initdirp = NULL;
4052 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004053 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00004054 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004055
4056 if (dflt == NULL)
4057 fileBuf[0] = NUL;
4058 else
4059 {
4060 wp = enc_to_utf16(dflt, NULL);
4061 if (wp == NULL)
4062 fileBuf[0] = NUL;
4063 else
4064 {
4065 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
4066 fileBuf[i] = wp[i];
4067 fileBuf[i] = NUL;
4068 vim_free(wp);
4069 }
4070 }
4071
Bram Moolenaar734a8672019-12-02 22:49:38 +01004072 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004073 filterp = convert_filterW(filter);
4074
Bram Moolenaara80faa82020-04-12 19:37:17 +02004075 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004076# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01004077 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01004078 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004079# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004080 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004081# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004082
4083 if (title != NULL)
4084 titlep = enc_to_utf16(title, NULL);
4085 fileStruct.lpstrTitle = titlep;
4086
4087 if (ext != NULL)
4088 extp = enc_to_utf16(ext, NULL);
4089 fileStruct.lpstrDefExt = extp;
4090
4091 fileStruct.lpstrFile = fileBuf;
4092 fileStruct.nMaxFile = MAXPATHL;
4093 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004094 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
4095 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004096 if (initdir != NULL && *initdir != NUL)
4097 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004098 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004099 initdirp = enc_to_utf16(initdir, NULL);
4100 if (initdirp != NULL)
4101 {
4102 for (wp = initdirp; *wp != NUL; ++wp)
4103 if (*wp == '/')
4104 *wp = '\\';
4105 }
4106 fileStruct.lpstrInitialDir = initdirp;
4107 }
4108
4109 /*
4110 * TODO: Allow selection of multiple files. Needs another arg to this
4111 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
4112 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
4113 * files that don't exist yet, so I haven't put it in. What about
4114 * OFN_PATHMUSTEXIST?
4115 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
4116 */
4117 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004118# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004119 if (curbuf->b_p_bin)
4120 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004121# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004122 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00004123 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004124 else
K.Takata14b8d6a2022-01-20 15:05:22 +00004125 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004126
4127 vim_free(filterp);
4128 vim_free(initdirp);
4129 vim_free(titlep);
4130 vim_free(extp);
4131
K.Takata14b8d6a2022-01-20 15:05:22 +00004132 if (!ret)
4133 return NULL;
4134
4135 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004136 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004137 if (p == NULL)
4138 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004139
Bram Moolenaar734a8672019-12-02 22:49:38 +01004140 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004141 SetFocus(s_hwnd);
4142
Bram Moolenaar734a8672019-12-02 22:49:38 +01004143 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004144 q = vim_strsave(shorten_fname1(p));
4145 vim_free(p);
4146 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004147}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004148
4149
4150/*
4151 * Convert the string s to the proper format for a filter string by replacing
4152 * the \t and \n delimiters with \0.
4153 * Returns the converted string in allocated memory.
4154 *
4155 * Keep in sync with convert_filterW() above!
4156 */
4157 static char_u *
4158convert_filter(char_u *s)
4159{
4160 char_u *res;
4161 unsigned s_len = (unsigned)STRLEN(s);
4162 unsigned i;
4163
4164 res = alloc(s_len + 3);
4165 if (res != NULL)
4166 {
4167 for (i = 0; i < s_len; ++i)
4168 if (s[i] == '\t' || s[i] == '\n')
4169 res[i] = '\0';
4170 else
4171 res[i] = s[i];
4172 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004173 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004174 res[s_len + 1] = NUL;
4175 res[s_len + 2] = NUL;
4176 }
4177 return res;
4178}
4179
4180/*
4181 * Select a directory.
4182 */
4183 char_u *
4184gui_mch_browsedir(char_u *title, char_u *initdir)
4185{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004186 // We fake this: Use a filter that doesn't select anything and a default
4187 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004188 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
4189 initdir, (char_u *)_("Directory\t*.nothing\n"));
4190}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004191#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004192
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004193 static void
4194_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004195 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004196 HDROP hDrop)
4197{
Bram Moolenaar4033c552017-09-16 20:54:51 +02004198#define BUFPATHLEN _MAX_PATH
4199#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004200 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004201 char szFile[BUFPATHLEN];
4202 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
4203 UINT i;
4204 char_u **fnames;
4205 POINT pt;
4206 int_u modifiers = 0;
4207
Bram Moolenaar734a8672019-12-02 22:49:38 +01004208 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004209 DragQueryPoint(hDrop, &pt);
4210 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
4211
4212 reset_VIsual();
4213
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004214 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004215
4216 if (fnames != NULL)
4217 for (i = 0; i < cFiles; ++i)
4218 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004219 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
4220 fnames[i] = utf16_to_enc(wszFile, NULL);
4221 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004222 {
4223 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
4224 fnames[i] = vim_strsave((char_u *)szFile);
4225 }
4226 }
4227
4228 DragFinish(hDrop);
4229
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004230 if (fnames == NULL)
4231 return;
LemonBoy45684c62022-04-24 15:46:42 +01004232
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004233 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004234
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004235 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4236 modifiers |= MOUSE_SHIFT;
4237 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4238 modifiers |= MOUSE_CTRL;
4239 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4240 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004241
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004242 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
4243
4244 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004245}
4246
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004247 static int
4248_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004249 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004250 HWND hwndCtl,
4251 UINT code,
4252 int pos)
4253{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004254 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004255 scrollbar_T *sb, *sb_info;
4256 long val;
4257 int dragging = FALSE;
4258 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004259 SCROLLINFO si;
4260
4261 si.cbSize = sizeof(si);
4262 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004263
4264 sb = gui_mswin_find_scrollbar(hwndCtl);
4265 if (sb == NULL)
4266 return 0;
4267
Bram Moolenaar734a8672019-12-02 22:49:38 +01004268 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004269 {
4270 /*
4271 * Careful: need to get scrollbar info out of first (left) scrollbar
4272 * for window, but keep real scrollbar too because we must pass it to
4273 * gui_drag_scrollbar().
4274 */
4275 sb_info = &sb->wp->w_scrollbars[0];
4276 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004277 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004278 sb_info = sb;
4279 val = sb_info->value;
4280
4281 switch (code)
4282 {
4283 case SB_THUMBTRACK:
4284 val = pos;
4285 dragging = TRUE;
4286 if (sb->scroll_shift > 0)
4287 val <<= sb->scroll_shift;
4288 break;
4289 case SB_LINEDOWN:
4290 val++;
4291 break;
4292 case SB_LINEUP:
4293 val--;
4294 break;
4295 case SB_PAGEDOWN:
4296 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4297 break;
4298 case SB_PAGEUP:
4299 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4300 break;
4301 case SB_TOP:
4302 val = 0;
4303 break;
4304 case SB_BOTTOM:
4305 val = sb_info->max;
4306 break;
4307 case SB_ENDSCROLL:
4308 if (prev_code == SB_THUMBTRACK)
4309 {
4310 /*
4311 * "pos" only gives us 16-bit data. In case of large file,
4312 * use GetScrollPos() which returns 32-bit. Unfortunately it
4313 * is not valid while the scrollbar is being dragged.
4314 */
4315 val = GetScrollPos(hwndCtl, SB_CTL);
4316 if (sb->scroll_shift > 0)
4317 val <<= sb->scroll_shift;
4318 }
4319 break;
4320
4321 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004322 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004323 return 0;
4324 }
4325 prev_code = code;
4326
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004327 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4328 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004329
4330 /*
4331 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4332 */
4333 if (sb->wp != NULL)
4334 {
4335 scrollbar_T *sba = sb->wp->w_scrollbars;
4336 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4337
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004338 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004339 }
4340
Bram Moolenaar734a8672019-12-02 22:49:38 +01004341 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004342 s_busy_processing = TRUE;
4343
Bram Moolenaar734a8672019-12-02 22:49:38 +01004344 // When "allow_scrollbar" is FALSE still need to remember the new
4345 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004346 dont_scroll = !allow_scrollbar;
4347
Bram Moolenaara338adc2018-01-31 20:51:47 +01004348 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004349 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004350 mch_enable_flush();
4351 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004352
4353 s_busy_processing = FALSE;
4354 dont_scroll = dont_scroll_save;
4355
4356 return 0;
4357}
4358
4359
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#ifdef FEAT_XPM_W32
4361# include "xpm_w32.h"
4362#endif
4363
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaar734a8672019-12-02 22:49:38 +01004365// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366#define TEAROFF_PADDING_X 2
4367#define TEAROFF_BUTTON_PAD_X 8
4368#define TEAROFF_MIN_WIDTH 200
4369#define TEAROFF_SUBMENU_LABEL ">>"
4370#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4371
4372
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004373#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374# define ID_BEVAL_TOOLTIP 200
4375# define BEVAL_TEXT_LEN MAXPATHL
4376
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004378static UINT_PTR beval_timer_id = 0;
4379static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004380#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004381
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004382
Bram Moolenaar734a8672019-12-02 22:49:38 +01004383// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385#ifdef FEAT_MENU
4386static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389/*
4390 * Use the system font for dialogs and tear-off menus. Remove this line to
4391 * use DLG_FONT_NAME.
4392 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004393#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#define VIM_CLASSW L"Vim"
4397
Bram Moolenaar734a8672019-12-02 22:49:38 +01004398// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4399// thus there should be room for every dialog. For tearoffs it's made bigger
4400// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401#define DLG_ALLOC_SIZE 16 * 1024
4402
4403/*
4404 * stuff for dialogs, menus, tearoffs etc.
4405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406static PWORD
4407add_dialog_element(
4408 PWORD p,
4409 DWORD lStyle,
4410 WORD x,
4411 WORD y,
4412 WORD w,
4413 WORD h,
4414 WORD Id,
4415 WORD clss,
4416 const char *caption);
4417static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004418static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004419#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422static void get_dialog_font_metrics(void);
4423
4424static int dialog_default_button = -1;
4425
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#ifdef FEAT_TOOLBAR
4427static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004428static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004429static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004431#else
4432# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#endif
4434
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004435#ifdef FEAT_GUI_TABLINE
4436static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004437static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004438#endif
4439
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#ifdef FEAT_MBYTE_IME
4441static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4442static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4443#endif
4444#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4445# ifdef NOIME
4446typedef struct tagCOMPOSITIONFORM {
4447 DWORD dwStyle;
4448 POINT ptCurrentPos;
4449 RECT rcArea;
4450} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4451typedef HANDLE HIMC;
4452# endif
4453
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004454static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004455static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4456static HIMC (WINAPI *pImmGetContext)(HWND);
4457static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4458static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4459static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4460static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004461static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4462static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004463static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4464static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004465static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466static void dyn_imm_load(void);
4467#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468# define pImmGetCompositionStringW ImmGetCompositionStringW
4469# define pImmGetContext ImmGetContext
4470# define pImmAssociateContext ImmAssociateContext
4471# define pImmReleaseContext ImmReleaseContext
4472# define pImmGetOpenStatus ImmGetOpenStatus
4473# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004474# define pImmGetCompositionFontW ImmGetCompositionFontW
4475# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476# define pImmSetCompositionWindow ImmSetCompositionWindow
4477# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004478# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#endif
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#ifdef FEAT_MENU
4482/*
4483 * Figure out how high the menu bar is at the moment.
4484 */
4485 static int
4486gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004487 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 static int old_menu_height = -1;
4490
4491 RECT rc1, rc2;
4492 int num;
4493 int menu_height;
4494
4495 if (gui.menu_is_active)
4496 num = GetMenuItemCount(s_menuBar);
4497 else
4498 num = 0;
4499
4500 if (num == 0)
4501 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004502 else if (IsMinimized(s_hwnd))
4503 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004504 // The height of the menu cannot be determined while the window is
4505 // minimized. Take the previous height if the menu is changed in that
4506 // state, to avoid that Vim's vertical window size accidentally
4507 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004508 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 else
4511 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004512 /*
4513 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4514 * seem to have been set yet, so menu wraps in default window
4515 * width which is very narrow. Instead just return height of a
4516 * single menu item. Will still be wrong when the menu really
4517 * should wrap over more than one line.
4518 */
4519 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4520 if (gui.starting)
4521 menu_height = rc1.bottom - rc1.top + 1;
4522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004524 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4525 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 }
4527 }
4528
4529 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004530 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004531 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 return menu_height;
4534}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004535#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536
4537
4538/*
4539 * Setup for the Intellimouse
4540 */
LemonBoyc27747e2022-05-07 12:25:40 +01004541 static long
4542mouse_vertical_scroll_step(void)
4543{
4544 UINT val;
4545 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4546 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4547 return 3; // Safe default;
4548}
4549
4550 static long
4551mouse_horizontal_scroll_step(void)
4552{
4553 UINT val;
4554 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4555 return (long)val;
4556 return 3; // Safe default;
4557}
4558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 static void
4560init_mouse_wheel(void)
4561{
LemonBoyc27747e2022-05-07 12:25:40 +01004562 // Get the default values for the horizontal and vertical scroll steps from
4563 // the system.
4564 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4565 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566}
4567
Bram Moolenaarae20f342019-11-05 21:09:23 +01004568/*
LemonBoya773d842022-05-10 20:54:46 +01004569 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004572_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573{
LemonBoy365d8f72022-05-05 19:23:07 +01004574 int button;
4575 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004576 int modifiers = 0;
4577 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004578 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4579 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarae20f342019-11-05 21:09:23 +01004581 wp = gui_mouse_window(FIND_POPUP);
4582
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004583#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004584 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004585 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004586 cmdarg_T cap;
4587 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004588
Bram Moolenaarae20f342019-11-05 21:09:23 +01004589 // Mouse hovers over popup window, scroll it if possible.
4590 mouse_row = wp->w_winrow;
4591 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004592 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004593 if (horizontal)
4594 {
4595 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4596 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4597 }
4598 else
4599 {
4600 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4601 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4602 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004603 clear_oparg(&oa);
4604 cap.oap = &oa;
4605 nv_mousescroll(&cap);
4606 update_screen(0);
4607 setcursor();
4608 out_flush();
4609 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004610 }
4611#endif
4612
Bram Moolenaarae20f342019-11-05 21:09:23 +01004613 if (wp == NULL || !p_scf)
4614 wp = curwin;
4615
LemonBoy365d8f72022-05-05 19:23:07 +01004616 // Translate the scroll event into an event that Vim can process so that
4617 // the user has a chance to map the scrollwheel buttons.
4618 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004619 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 else
LemonBoy365d8f72022-05-05 19:23:07 +01004621 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004622
4623 kbd_modifiers = get_active_modifiers();
4624
4625 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4626 modifiers |= MOUSE_SHIFT;
4627 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4628 modifiers |= MOUSE_CTRL;
4629 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4630 modifiers |= MOUSE_ALT;
4631
LemonBoya773d842022-05-10 20:54:46 +01004632 // The cursor position is relative to the upper-left corner of the screen.
4633 pt.x = GET_X_LPARAM(lParam);
4634 pt.y = GET_Y_LPARAM(lParam);
4635 ScreenToClient(s_textArea, &pt);
4636
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004637 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638}
4639
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004640#ifdef USE_SYSMENU_FONT
4641/*
4642 * Get Menu Font.
4643 * Return OK or FAIL.
4644 */
4645 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004646gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004647{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004648 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004649
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004650 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4651 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004652 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004653 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004654 &nm,
4655 0))
4656 return FAIL;
4657 *lf = nm.lfMenuFont;
4658 return OK;
4659}
4660#endif
4661
4662
4663#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4664/*
4665 * Set the GUI tabline font to the system menu font
4666 */
4667 static void
4668set_tabline_font(void)
4669{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004670 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004671 HFONT font;
4672 HWND hwnd;
4673 HDC hdc;
4674 HFONT hfntOld;
4675 TEXTMETRIC tm;
4676
4677 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4678 return;
4679
K.Takatac81e9bf2022-01-16 14:15:49 +00004680 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004681 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004682
4683 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4684
4685 /*
4686 * Compute the height of the font used for the tab text
4687 */
4688 hwnd = GetDesktopWindow();
4689 hdc = GetWindowDC(hwnd);
4690 hfntOld = SelectFont(hdc, font);
4691
4692 GetTextMetrics(hdc, &tm);
4693
4694 SelectFont(hdc, hfntOld);
4695 ReleaseDC(hwnd, hdc);
4696
4697 /*
4698 * The space used by the tab border and the space between the tab label
4699 * and the tab border is included as 7.
4700 */
4701 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4702}
K.Takatac81e9bf2022-01-16 14:15:49 +00004703#else
4704# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004705#endif
4706
Bram Moolenaar520470a2005-06-16 21:59:56 +00004707/*
4708 * Invoked when a setting was changed.
4709 */
4710 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004711_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004712{
LemonBoy365d8f72022-05-05 19:23:07 +01004713 switch (param)
4714 {
4715 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004716 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004717 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004718 case SPI_SETWHEELSCROLLCHARS:
4719 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004720 break;
4721 case SPI_SETNONCLIENTMETRICS:
4722 set_tabline_font();
4723 break;
4724 default:
4725 break;
4726 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004727 return 0;
4728}
4729
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730#ifdef FEAT_NETBEANS_INTG
4731 static void
4732_OnWindowPosChanged(
4733 HWND hwnd,
4734 const LPWINDOWPOS lpwpos)
4735{
4736 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004737 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
4739 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4740 || lpwpos->cx != cx || lpwpos->cy != cy))
4741 {
4742 x = lpwpos->x;
4743 y = lpwpos->y;
4744 cx = lpwpos->cx;
4745 cy = lpwpos->cy;
4746 netbeans_frame_moved(x, y);
4747 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004748 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004749 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750}
4751#endif
4752
K.Takata4f2417f2021-06-05 16:25:32 +02004753
4754static HWND hwndTip = NULL;
4755
4756 static void
4757show_sizing_tip(int cols, int rows)
4758{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004759 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004760 char buf[32];
4761
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004762 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004763 ti.hwnd = s_hwnd;
4764 ti.uId = (UINT_PTR)s_hwnd;
4765 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4766 ti.lpszText = buf;
4767 sprintf(buf, "%dx%d", cols, rows);
4768 if (hwndTip == NULL)
4769 {
4770 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4771 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4772 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4773 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4774 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4775 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4776 }
4777 else
4778 {
4779 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4780 }
4781 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4782}
4783
4784 static void
4785destroy_sizing_tip(void)
4786{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004787 if (hwndTip == NULL)
4788 return;
4789
4790 DestroyWindow(hwndTip);
4791 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004792}
4793
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 static int
4795_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 UINT fwSide,
4797 LPRECT lprc)
4798{
4799 int w, h;
4800 int valid_w, valid_h;
4801 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004802 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 w = lprc->right - lprc->left;
4805 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004806 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 w_offset = w - valid_w;
4808 h_offset = h - valid_h;
4809
4810 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4811 || fwSide == WMSZ_BOTTOMLEFT)
4812 lprc->left += w_offset;
4813 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4814 || fwSide == WMSZ_BOTTOMRIGHT)
4815 lprc->right -= w_offset;
4816
4817 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4818 || fwSide == WMSZ_TOPRIGHT)
4819 lprc->top += h_offset;
4820 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4821 || fwSide == WMSZ_BOTTOMRIGHT)
4822 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004823
4824 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return TRUE;
4826}
4827
K.Takata92000e22022-01-20 15:10:57 +00004828#ifdef FEAT_GUI_TABLINE
4829 static void
4830_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4831{
4832 if (gui_mch_showing_tabline())
4833 {
4834 POINT pt;
4835 RECT rect;
4836
4837 /*
4838 * If the cursor is on the tabline, display the tab menu
4839 */
4840 GetCursorPos(&pt);
4841 GetWindowRect(s_textArea, &rect);
4842 if (pt.y < rect.top)
4843 {
4844 show_tabline_popup_menu();
4845 return;
4846 }
4847 }
4848 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4849}
4850
4851 static void
4852_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4853{
4854 /*
4855 * If the user double clicked the tabline, create a new tab
4856 */
4857 if (gui_mch_showing_tabline())
4858 {
4859 POINT pt;
4860 RECT rect;
4861
4862 GetCursorPos(&pt);
4863 GetWindowRect(s_textArea, &rect);
4864 if (pt.y < rect.top)
4865 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4866 }
4867 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4868}
4869#endif
4870
4871 static UINT
4872_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4873{
4874 UINT result;
4875 int x, y;
4876
4877 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4878 if (result != HTCLIENT)
4879 return result;
4880
4881#ifdef FEAT_GUI_TABLINE
4882 if (gui_mch_showing_tabline())
4883 {
4884 RECT rct;
4885
4886 // If the cursor is on the GUI tabline, don't process this event
4887 GetWindowRect(s_textArea, &rct);
4888 if (yPos < rct.top)
4889 return result;
4890 }
4891#endif
4892 (void)gui_mch_get_winpos(&x, &y);
4893 xPos -= x;
4894
4895 if (xPos < 48) // <VN> TODO should use system metric?
4896 return HTBOTTOMLEFT;
4897 else
4898 return HTBOTTOMRIGHT;
4899}
4900
4901#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4902 static LRESULT
4903_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4904{
4905 switch (hdr->code)
4906 {
4907 case TTN_GETDISPINFOW:
4908 case TTN_GETDISPINFO:
4909 {
4910 char_u *str = NULL;
4911 static void *tt_text = NULL;
4912
4913 VIM_CLEAR(tt_text);
4914
4915# ifdef FEAT_GUI_TABLINE
4916 if (gui_mch_showing_tabline()
4917 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4918 {
4919 POINT pt;
4920 /*
4921 * Mouse is over the GUI tabline. Display the
4922 * tooltip for the tab under the cursor
4923 *
4924 * Get the cursor position within the tab control
4925 */
4926 GetCursorPos(&pt);
4927 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4928 {
4929 TCHITTESTINFO htinfo;
4930 int idx;
4931
4932 /*
4933 * Get the tab under the cursor
4934 */
4935 htinfo.pt.x = pt.x;
4936 htinfo.pt.y = pt.y;
4937 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4938 if (idx != -1)
4939 {
4940 tabpage_T *tp;
4941
4942 tp = find_tabpage(idx + 1);
4943 if (tp != NULL)
4944 {
4945 get_tabline_label(tp, TRUE);
4946 str = NameBuff;
4947 }
4948 }
4949 }
4950 }
4951# endif
4952# ifdef FEAT_TOOLBAR
4953# ifdef FEAT_GUI_TABLINE
4954 else
4955# endif
4956 {
4957 UINT idButton;
4958 vimmenu_T *pMenu;
4959
4960 idButton = (UINT) hdr->idFrom;
4961 pMenu = gui_mswin_find_menu(root_menu, idButton);
4962 if (pMenu)
4963 str = pMenu->strings[MENU_INDEX_TIP];
4964 }
4965# endif
4966 if (str == NULL)
4967 break;
4968
4969 // Set the maximum width, this also enables using \n for
4970 // line break.
4971 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4972
4973 if (hdr->code == TTN_GETDISPINFOW)
4974 {
4975 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4976
4977 tt_text = enc_to_utf16(str, NULL);
4978 lpdi->lpszText = tt_text;
4979 // can't show tooltip if failed
4980 }
4981 else
4982 {
4983 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
John Marriott411ae582025-04-27 15:05:06 +02004984 size_t len = STRLEN(str);
K.Takata92000e22022-01-20 15:10:57 +00004985
John Marriott411ae582025-04-27 15:05:06 +02004986 if (len < sizeof(lpdi->szText)
4987 || ((tt_text = vim_strnsave(str, len)) == NULL))
K.Takata92000e22022-01-20 15:10:57 +00004988 vim_strncpy((char_u *)lpdi->szText, str,
4989 sizeof(lpdi->szText) - 1);
4990 else
4991 lpdi->lpszText = tt_text;
4992 }
4993 }
4994 break;
4995
4996# ifdef FEAT_GUI_TABLINE
4997 case TCN_SELCHANGE:
4998 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4999 {
5000 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
5001 return 0L;
5002 }
5003 break;
5004
5005 case NM_RCLICK:
5006 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
5007 {
5008 show_tabline_popup_menu();
5009 return 0L;
5010 }
5011 break;
5012# endif
5013
5014 default:
5015 break;
5016 }
5017 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
5018}
5019#endif
5020
5021#if defined(MENUHINTS) && defined(FEAT_MENU)
5022 static LRESULT
5023_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
5024{
5025 if (((UINT) HIWORD(wParam)
5026 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5027 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01005028 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00005029 {
K.Takata92000e22022-01-20 15:10:57 +00005030 vimmenu_T *pMenu;
5031 static int did_menu_tip = FALSE;
5032
5033 if (did_menu_tip)
5034 {
5035 msg_clr_cmdline();
5036 setcursor();
5037 out_flush();
5038 did_menu_tip = FALSE;
5039 }
5040
John Marriott411ae582025-04-27 15:05:06 +02005041 pMenu = gui_mswin_find_menu(root_menu, (UINT)LOWORD(wParam));
5042 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != NULL)
K.Takata92000e22022-01-20 15:10:57 +00005043 {
John Marriott411ae582025-04-27 15:05:06 +02005044 MENUITEMINFO menuinfo;
5045
5046 menuinfo.cbSize = sizeof(MENUITEMINFO);
5047 menuinfo.fMask = MIIM_ID; // We only want to check if the menu item exists,
5048 // so retrieve something simple.
5049 if (GetMenuItemInfo(s_menuBar, pMenu->id, FALSE, &menuinfo))
5050 {
5051 ++msg_hist_off;
5052 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
5053 --msg_hist_off;
5054 setcursor();
5055 out_flush();
5056 did_menu_tip = TRUE;
5057 }
K.Takata92000e22022-01-20 15:10:57 +00005058 }
5059 return 0L;
5060 }
5061 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
5062}
5063#endif
5064
Ken Takata7086b3e2023-10-02 21:26:03 +02005065 static BOOL
John Marriott411ae582025-04-27 15:05:06 +02005066_OnGetDpiScaledSize(HWND hwnd UNUSED, UINT dpi, SIZE *size)
Ken Takata7086b3e2023-10-02 21:26:03 +02005067{
Ken Takatada5da652023-10-05 20:20:58 +02005068 int old_width, old_height;
5069 int new_width, new_height;
5070 LOGFONTW lf;
5071 HFONT font;
5072
Ken Takata7086b3e2023-10-02 21:26:03 +02005073 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
5074
5075 // Calculate new approximate size.
Ken Takatada5da652023-10-05 20:20:58 +02005076 GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
5077 GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
5078 lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
5079 font = CreateFontIndirectW(&lf);
5080 if (font)
5081 {
5082 GetFontSize((GuiFont)font, &new_width, &new_height); // New size
5083 DeleteFont(font);
5084 }
5085 else
5086 {
5087 new_width = old_width;
5088 new_height = old_height;
5089 }
5090 size->cx = size->cx * new_width / old_width;
5091 size->cy = size->cy * new_height / old_height;
Ken Takata7086b3e2023-10-02 21:26:03 +02005092 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
5093
Ken Takatada5da652023-10-05 20:20:58 +02005094 return TRUE;
Ken Takata7086b3e2023-10-02 21:26:03 +02005095}
5096
K.Takatac81e9bf2022-01-16 14:15:49 +00005097 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02005098_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00005099{
5100 s_dpi = ydpi;
5101 s_in_dpichanged = TRUE;
5102 //TRACE("DPI: %d", ydpi);
5103
Ken Takata7086b3e2023-10-02 21:26:03 +02005104 s_suggested_rect = *rc;
5105 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
5106 // rc->right - rc->left, rc->bottom - rc->top);
5107
K.Takatac81e9bf2022-01-16 14:15:49 +00005108 update_scrollbar_size();
5109 update_toolbar_size();
5110 set_tabline_font();
5111
5112 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
5113 gui_get_wide_font();
5114 gui_mswin_get_menu_height(FALSE);
5115#ifdef FEAT_MBYTE_IME
5116 im_set_position(gui.row, gui.col);
5117#endif
5118 InvalidateRect(hwnd, NULL, TRUE);
5119
5120 s_in_dpichanged = FALSE;
5121 return 0L;
5122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124
5125 static LRESULT CALLBACK
5126_WndProc(
5127 HWND hwnd,
5128 UINT uMsg,
5129 WPARAM wParam,
5130 LPARAM lParam)
5131{
LemonBoya773d842022-05-10 20:54:46 +01005132 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
5133 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 HandleMouseHide(uMsg, lParam);
5136
5137 s_uMsg = uMsg;
5138 s_wParam = wParam;
5139 s_lParam = lParam;
5140
5141 switch (uMsg)
5142 {
5143 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
5144 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005145 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005147 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
5149 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
5150 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
5151 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
5152#ifdef FEAT_MENU
5153 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
5154#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
5156 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
5158 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005159 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
5160 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5162 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5163 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5164#ifdef FEAT_NETBEANS_INTG
5165 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5166#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00005167#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00005168 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
5169 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005170#endif
K.Takata92000e22022-01-20 15:10:57 +00005171 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005172
Bram Moolenaar734a8672019-12-02 22:49:38 +01005173 case WM_QUERYENDSESSION: // System wants to go down.
5174 gui_shell_closed(); // Will exit when no changed buffers.
5175 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176
5177 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005178 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01005179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005181 return 0L;
5182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005186 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5187 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005188 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 return 0L;
5190
5191 case WM_SYSCHAR:
5192 /*
5193 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5194 * shortcut key, handle like a typed ALT key, otherwise call Windows
5195 * ALT key handling.
5196 */
5197#ifdef FEAT_MENU
5198 if ( !gui.menu_is_active
5199 || p_wak[0] == 'n'
5200 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5201 )
5202#endif
5203 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005204 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 return 0L;
5206 }
5207#ifdef FEAT_MENU
5208 else
K.Takata4ac893f2022-01-20 12:44:28 +00005209 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210#endif
5211
5212 case WM_SYSKEYUP:
5213#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005214 // This used to be done only when menu is active: ALT key is used for
5215 // that. But that caused problems when menu is disabled and using
5216 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
5217 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00005218 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01005220 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#endif
5222
K.Takata4f2417f2021-06-05 16:25:32 +02005223 case WM_EXITSIZEMOVE:
5224 destroy_sizing_tip();
5225 break;
5226
Bram Moolenaar734a8672019-12-02 22:49:38 +01005227 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229
5230 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01005231 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01005232 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005233 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
Bram Moolenaar734a8672019-12-02 22:49:38 +01005235 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00005236 case WM_SETTINGCHANGE:
5237 return _OnSettingChange((UINT)wParam);
5238
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005239#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00005241 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#endif
K.Takata92000e22022-01-20 15:10:57 +00005243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#if defined(MENUHINTS) && defined(FEAT_MENU)
5245 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00005246 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
5249#ifdef FEAT_MBYTE_IME
5250 case WM_IME_NOTIFY:
5251 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005252 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005253 return 1L;
5254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 case WM_IME_COMPOSITION:
5256 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005257 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005258 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02005260 case WM_GETDPISCALEDSIZE:
5261 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00005262 case WM_DPICHANGED:
5263 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5264 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005268 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#endif
K.Takata92000e22022-01-20 15:10:57 +00005271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
5273
K.Takata4ac893f2022-01-20 12:44:28 +00005274 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275}
5276
5277/*
5278 * End of call-back routines
5279 */
5280
Bram Moolenaar734a8672019-12-02 22:49:38 +01005281// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282HWND vim_parent_hwnd = NULL;
5283
5284 static BOOL CALLBACK
5285FindWindowTitle(HWND hwnd, LPARAM lParam)
5286{
5287 char buf[2048];
5288 char *title = (char *)lParam;
5289
5290 if (GetWindowText(hwnd, buf, sizeof(buf)))
5291 {
5292 if (strstr(buf, title) != NULL)
5293 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005294 // Found it. Store the window ref. and quit searching if MDI
5295 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005297 if (vim_parent_hwnd != NULL)
5298 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005301 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302}
5303
5304/*
5305 * Invoked for '-P "title"' argument: search for parent application to open
5306 * our window in.
5307 */
5308 void
5309gui_mch_set_parent(char *title)
5310{
5311 EnumWindows(FindWindowTitle, (LPARAM)title);
5312 if (vim_parent_hwnd == NULL)
5313 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005314 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 mch_exit(2);
5316 }
5317}
5318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 static void
5321ole_error(char *arg)
5322{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005323 char buf[IOSIZE];
5324
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005325# ifdef VIMDLL
5326 gui.in_use = mch_is_gui_executable();
5327# endif
5328
Bram Moolenaar734a8672019-12-02 22:49:38 +01005329 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005330 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005331 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005332 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005336#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5337 static char *
5338gvim_error(void)
5339{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005340 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005341
5342 if (starting)
5343 {
5344 mch_errmsg(msg);
5345 mch_errmsg("\n");
5346 mch_exit(2);
5347 }
5348 return msg;
5349}
5350
5351 char *
5352gui_mch_do_spawn(char_u *arg)
5353{
5354 int len;
5355# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5356 char_u *session = NULL;
5357 LPWSTR tofree1 = NULL;
5358# endif
5359 WCHAR name[MAX_PATH];
5360 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5361 STARTUPINFOW si = {sizeof(si)};
5362 PROCESS_INFORMATION pi;
5363
5364 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5365 goto error;
5366 p = wcsrchr(name, L'\\');
5367 if (p == NULL)
5368 goto error;
5369 // Replace the executable name from vim(d).exe to gvim(d).exe.
5370# ifdef DEBUG
5371 wcscpy(p + 1, L"gvimd.exe");
5372# else
5373 wcscpy(p + 1, L"gvim.exe");
5374# endif
5375
5376# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5377 if (starting)
5378# endif
5379 {
5380 // Pass the command line to the new process.
5381 p = GetCommandLineW();
5382 // Skip 1st argument.
5383 while (*p && *p != L' ' && *p != L'\t')
5384 {
5385 if (*p == L'"')
5386 {
GuyBrushcf488eb2025-05-10 20:44:33 +02005387 // Skip quoted strings
5388 while (*++p && *p != L'"');
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005389 }
GuyBrushcf488eb2025-05-10 20:44:33 +02005390
5391 ++p;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005392 }
5393 cmd = p;
5394 }
5395# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5396 else
5397 {
5398 // Create a session file and pass it to the new process.
5399 LPWSTR wsession;
5400 char_u *savebg;
5401 int ret;
5402
5403 session = vim_tempname('s', FALSE);
5404 if (session == NULL)
5405 goto error;
5406 savebg = p_bg;
John Marriott411ae582025-04-27 15:05:06 +02005407 p_bg = vim_strnsave((char_u *)"light", 5); // Set 'bg' to "light".
5408 if (p_bg == NULL)
5409 {
5410 p_bg = savebg;
5411 goto error;
5412 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005413 ret = write_session_file(session);
5414 vim_free(p_bg);
5415 p_bg = savebg;
5416 if (!ret)
5417 goto error;
5418 wsession = enc_to_utf16(session, NULL);
5419 if (wsession == NULL)
5420 goto error;
5421 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005422 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005423 if (cmd == NULL)
5424 {
5425 vim_free(wsession);
5426 goto error;
5427 }
5428 tofree1 = cmd;
5429 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5430 wsession, wsession);
5431 vim_free(wsession);
5432 }
5433# endif
5434
5435 // Check additional arguments to the `:gui` command.
5436 if (arg != NULL)
5437 {
5438 warg = enc_to_utf16(arg, NULL);
5439 if (warg == NULL)
5440 goto error;
5441 tofree2 = warg;
5442 }
5443 else
5444 warg = L"";
5445
5446 // Set up the new command line.
5447 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005448 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005449 if (newcmd == NULL)
5450 goto error;
5451 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5452
5453 // Spawn a new GUI process.
5454 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5455 NULL, NULL, &si, &pi))
5456 goto error;
5457 CloseHandle(pi.hProcess);
5458 CloseHandle(pi.hThread);
5459 mch_exit(0);
5460
5461error:
5462# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5463 if (session)
5464 mch_remove(session);
5465 vim_free(session);
5466 vim_free(tofree1);
5467# endif
5468 vim_free(newcmd);
5469 vim_free(tofree2);
5470 return gvim_error();
5471}
5472#endif
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474/*
5475 * Parse the GUI related command-line arguments. Any arguments used are
5476 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005477 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
5479 void
5480gui_mch_prepare(int *argc, char **argv)
5481{
5482 int silent = FALSE;
5483 int idx;
5484
Bram Moolenaar734a8672019-12-02 22:49:38 +01005485 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5487 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005488 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5490 && (argv[2][0] == '-' || argv[2][0] == '/'))
5491 {
5492 silent = TRUE;
5493 idx = 2;
5494 }
5495 else
5496 idx = 1;
5497
Bram Moolenaar734a8672019-12-02 22:49:38 +01005498 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 if (STRICMP(argv[idx] + 1, "register") == 0)
5500 {
5501#ifdef FEAT_OLE
5502 RegisterMe(silent);
5503 mch_exit(0);
5504#else
5505 if (!silent)
5506 ole_error("register");
5507 mch_exit(2);
5508#endif
5509 }
5510
Bram Moolenaar734a8672019-12-02 22:49:38 +01005511 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5513 {
5514#ifdef FEAT_OLE
5515 UnregisterMe(!silent);
5516 mch_exit(0);
5517#else
5518 if (!silent)
5519 ole_error("unregister");
5520 mch_exit(2);
5521#endif
5522 }
5523
Bram Moolenaar734a8672019-12-02 22:49:38 +01005524 // Ignore an -embedding argument. It is only relevant if the
5525 // application wants to treat the case when it is started manually
5526 // differently from the case where it is started via automation (and
5527 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5529 {
5530#ifdef FEAT_OLE
5531 *argc = 1;
5532#else
5533 ole_error("embedding");
5534 mch_exit(2);
5535#endif
5536 }
5537 }
5538
5539#ifdef FEAT_OLE
Ken Takatabaaf6de2024-07-29 20:57:19 +02005540# ifdef VIMDLL
5541 if (mch_is_gui_executable())
5542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 {
5544 int bDoRestart = FALSE;
5545
5546 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 if (bDoRestart)
5549 mch_exit(0);
5550 }
5551#endif
5552
5553#ifdef FEAT_NETBEANS_INTG
5554 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005555 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int arg;
5557
5558 for (arg = 1; arg < *argc; arg++)
5559 if (strncmp("-nb", argv[arg], 3) == 0)
5560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 netbeansArg = argv[arg];
5562 mch_memmove(&argv[arg], &argv[arg + 1],
5563 (--*argc - arg) * sizeof(char *));
5564 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005565 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 }
5568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
K.Takatac81e9bf2022-01-16 14:15:49 +00005571 static void
5572load_dpi_func(void)
5573{
5574 HMODULE hUser32;
5575
5576 hUser32 = GetModuleHandle("user32.dll");
5577 if (hUser32 == NULL)
5578 goto fail;
5579
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005580 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5581 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5582 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005583 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005584 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5585 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005586
5587 if (pSetThreadDpiAwarenessContext != NULL)
5588 {
5589 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5590 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5591 if (oldctx != NULL)
5592 {
5593 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5594 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5595#ifdef DEBUG
5596 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5597 {
5598 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5599 }
5600#endif
5601 return;
5602 }
5603 }
5604
5605fail:
5606 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005607 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005608 pGetDpiForWindow = NULL;
5609 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5610 pSetThreadDpiAwarenessContext = NULL;
5611 pGetAwarenessFromDpiAwarenessContext = NULL;
5612}
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614/*
5615 * Initialise the GUI. Create all the windows, set up all the call-backs
5616 * etc.
5617 */
5618 int
5619gui_mch_init(void)
5620{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005622 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaar734a8672019-12-02 22:49:38 +01005625 // Return here if the window was already opened (happens when
5626 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005628 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630 /*
5631 * Load the tearoff bitmap
5632 */
5633#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005634 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635#endif
5636
K.Takatac81e9bf2022-01-16 14:15:49 +00005637 load_dpi_func();
5638
5639 s_dpi = pGetDpiForSystem();
5640 update_scrollbar_size();
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005643 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#endif
5645 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005646#ifdef FEAT_TOOLBAR
5647 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5651
Bram Moolenaar734a8672019-12-02 22:49:38 +01005652 // First try using the wide version, so that we can use any title.
5653 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005654 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005656 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 wndclassw.lpfnWndProc = _WndProc;
5658 wndclassw.cbClsExtra = 0;
5659 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005660 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5662 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5663 wndclassw.hbrBackground = s_brush;
5664 wndclassw.lpszMenuName = NULL;
5665 wndclassw.lpszClassName = szVimWndClassW;
5666
K.Takata4ac893f2022-01-20 12:44:28 +00005667 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 if (vim_parent_hwnd != NULL)
5672 {
5673#ifdef HAVE_TRY_EXCEPT
5674 __try
5675 {
5676#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005677 // Open inside the specified parent window.
5678 // TODO: last argument should point to a CLIENTCREATESTRUCT
5679 // structure.
5680 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005682 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005683 WS_OVERLAPPEDWINDOW | WS_CHILD
5684 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5686 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005687 100, // Any value will do
5688 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005690 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691#ifdef HAVE_TRY_EXCEPT
5692 }
5693 __except(EXCEPTION_EXECUTE_HANDLER)
5694 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005695 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697#endif
5698 if (s_hwnd == NULL)
5699 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005700 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 mch_exit(2);
5702 }
5703 }
5704 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005705 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005706 // If the provided windowid is not valid reset it to zero, so that it
5707 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005708 if (IsWindow((HWND)win_socket_id) <= 0)
5709 win_socket_id = 0;
5710
Bram Moolenaar734a8672019-12-02 22:49:38 +01005711 // Create a window. If win_socket_id is not zero without border and
5712 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005713 s_hwnd = CreateWindowW(
5714 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005715 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5716 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005717 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5718 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005719 100, // Any value will do
5720 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005721 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005722 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005723 if (s_hwnd != NULL && win_socket_id != 0)
5724 {
5725 SetParent(s_hwnd, (HWND)win_socket_id);
5726 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5727 }
5728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729
5730 if (s_hwnd == NULL)
5731 return FAIL;
5732
K.Takatac81e9bf2022-01-16 14:15:49 +00005733 if (pGetDpiForWindow != NULL)
5734 {
5735 s_dpi = pGetDpiForWindow(s_hwnd);
5736 update_scrollbar_size();
5737 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5738 }
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5741 dyn_imm_load();
5742#endif
5743
Bram Moolenaar734a8672019-12-02 22:49:38 +01005744 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005745 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005746 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005747 wndclassw.style = CS_OWNDC;
5748 wndclassw.lpfnWndProc = _TextAreaWndProc;
5749 wndclassw.cbClsExtra = 0;
5750 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005751 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005752 wndclassw.hIcon = NULL;
5753 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5754 wndclassw.hbrBackground = NULL;
5755 wndclassw.lpszMenuName = NULL;
5756 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005757
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005758 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 return FAIL;
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005762 s_textArea = CreateWindowExW(
5763 0,
5764 szTextAreaClassW, L"Vim text area",
5765 WS_CHILD | WS_VISIBLE, 0, 0,
5766 100, // Any value will do for now
5767 100, // Any value will do for now
5768 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005769 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (s_textArea == NULL)
5772 return FAIL;
5773
Bram Moolenaar20321902016-02-17 12:30:17 +01005774#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005775 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005776 {
5777 HANDLE hIcon = NULL;
5778
5779 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005780 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005781 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005782#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#ifdef FEAT_MENU
5785 s_menuBar = CreateMenu();
5786#endif
5787 s_hdc = GetDC(s_textArea);
5788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
Bram Moolenaar734a8672019-12-02 22:49:38 +01005791 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005792 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
Bram Moolenaar734a8672019-12-02 22:49:38 +01005794 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 gui_mch_def_colors();
5796
Bram Moolenaar734a8672019-12-02 22:49:38 +01005797 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5798 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 set_normal_colors();
5800
5801 /*
5802 * Check that none of the colors are the same as the background color.
5803 * Then store the current values as the defaults.
5804 */
5805 gui_check_colors();
5806 gui.def_norm_pixel = gui.norm_pixel;
5807 gui.def_back_pixel = gui.back_pixel;
5808
Bram Moolenaar734a8672019-12-02 22:49:38 +01005809 // Get the colors for the highlight groups (gui_check_colors() might have
5810 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 highlight_gui_started();
5812
5813 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005814 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005816 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 /*
5819 * Set up for Intellimouse processing
5820 */
5821 init_mouse_wheel();
5822
5823 /*
5824 * compute a couple of metrics used for the dialogs
5825 */
5826 get_dialog_font_metrics();
5827#ifdef FEAT_TOOLBAR
5828 /*
5829 * Create the toolbar
5830 */
5831 initialise_toolbar();
5832#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005833#ifdef FEAT_GUI_TABLINE
5834 /*
5835 * Create the tabline
5836 */
5837 initialise_tabline();
5838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839#ifdef MSWIN_FIND_REPLACE
5840 /*
5841 * Initialise the dialog box stuff
5842 */
5843 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5844
Bram Moolenaar734a8672019-12-02 22:49:38 +01005845 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005847 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005849 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5851 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5852 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005855#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005856 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005857 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005858#endif
5859
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005860#ifdef FEAT_RENDER_OPTIONS
5861 if (p_rop)
5862 (void)gui_mch_set_rendering_options(p_rop);
5863#endif
5864
Bram Moolenaar748bf032005-02-02 23:04:36 +00005865theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005866 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005867 display_errors();
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 return OK;
5870}
5871
5872/*
5873 * Get the size of the screen, taking position on multiple monitors into
5874 * account (if supported).
5875 */
5876 static void
5877get_work_area(RECT *spi_rect)
5878{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005879 HMONITOR mon;
5880 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881
Bram Moolenaar734a8672019-12-02 22:49:38 +01005882 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005883 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005884 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005886 moninfo.cbSize = sizeof(MONITORINFO);
5887 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005889 *spi_rect = moninfo.rcWork;
5890 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005893 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5895}
5896
5897/*
5898 * Set the size of the window to the given width and height in pixels.
5899 */
5900 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005901gui_mch_set_shellsize(
5902 int width,
5903 int height,
5904 int min_width UNUSED,
5905 int min_height UNUSED,
5906 int base_width UNUSED,
5907 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005908 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005911 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914 // Try to keep window completely on screen.
5915 // Get position of the screen work area. This is the part that is not
5916 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 get_work_area(&workarea_rect);
5918
Bram Moolenaar734a8672019-12-02 22:49:38 +01005919 // Resizing a maximized window looks very strange, unzoom it first.
5920 // But don't do it when still starting up, it may have been requested in
5921 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005922 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005924
Ken Takata7086b3e2023-10-02 21:26:03 +02005925 if (s_in_dpichanged)
5926 // Use the suggested position when in WM_DPICHANGED.
5927 window_rect = s_suggested_rect;
5928 else
5929 // Use current position.
5930 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaar734a8672019-12-02 22:49:38 +01005932 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005933 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5934 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5935 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5936 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5937 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5938 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Bram Moolenaar734a8672019-12-02 22:49:38 +01005940 // The following should take care of keeping Vim on the same monitor, no
5941 // matter if the secondary monitor is left or right of the primary
5942 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005943 window_rect.right = window_rect.left + win_width;
5944 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005945
Bram Moolenaar734a8672019-12-02 22:49:38 +01005946 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005947 // Don't adjust the position when in WM_DPICHANGED.
5948 if (!s_in_dpichanged)
5949 {
5950 if ((direction & RESIZE_HOR)
5951 && window_rect.right > workarea_rect.right)
5952 OffsetRect(&window_rect,
5953 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Ken Takata7086b3e2023-10-02 21:26:03 +02005955 if ((direction & RESIZE_HOR)
5956 && window_rect.left < workarea_rect.left)
5957 OffsetRect(&window_rect,
5958 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Ken Takata7086b3e2023-10-02 21:26:03 +02005960 if ((direction & RESIZE_VERT)
5961 && window_rect.bottom > workarea_rect.bottom)
5962 OffsetRect(&window_rect,
5963 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Ken Takata7086b3e2023-10-02 21:26:03 +02005965 if ((direction & RESIZE_VERT)
5966 && window_rect.top < workarea_rect.top)
5967 OffsetRect(&window_rect,
5968 0, workarea_rect.top - window_rect.top);
5969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005971 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5972 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973
Ken Takata7086b3e2023-10-02 21:26:03 +02005974 //TRACE("New pos: %d,%d New size: %d,%d",
5975 // window_rect.left, window_rect.top, win_width, win_height);
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 SetActiveWindow(s_hwnd);
5978 SetFocus(s_hwnd);
5979
Bram Moolenaar734a8672019-12-02 22:49:38 +01005980 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982}
5983
5984
5985 void
5986gui_mch_set_scrollbar_thumb(
5987 scrollbar_T *sb,
5988 long val,
5989 long size,
5990 long max)
5991{
5992 SCROLLINFO info;
5993
5994 sb->scroll_shift = 0;
5995 while (max > 32767)
5996 {
5997 max = (max + 1) >> 1;
5998 val >>= 1;
5999 size >>= 1;
6000 ++sb->scroll_shift;
6001 }
6002
6003 if (sb->scroll_shift > 0)
6004 ++size;
6005
6006 info.cbSize = sizeof(info);
6007 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
6008 info.nPos = val;
6009 info.nMin = 0;
6010 info.nMax = max;
6011 info.nPage = size;
6012 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
6013}
6014
6015
6016/*
6017 * Set the current text font.
6018 */
6019 void
6020gui_mch_set_font(GuiFont font)
6021{
6022 gui.currFont = font;
6023}
6024
6025
6026/*
6027 * Set the current text foreground color.
6028 */
6029 void
6030gui_mch_set_fg_color(guicolor_T color)
6031{
6032 gui.currFgColor = color;
6033}
6034
6035/*
6036 * Set the current text background color.
6037 */
6038 void
6039gui_mch_set_bg_color(guicolor_T color)
6040{
6041 gui.currBgColor = color;
6042}
6043
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006044/*
6045 * Set the current text special color.
6046 */
6047 void
6048gui_mch_set_sp_color(guicolor_T color)
6049{
6050 gui.currSpColor = color;
6051}
6052
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006053#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054/*
6055 * Multi-byte handling, originally by Sung-Hoon Baek.
6056 * First static functions (no prototypes generated).
6057 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006058# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01006059# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006060# endif
6061# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 * handle WM_IME_NOTIFY message
6065 */
6066 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006067_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 LRESULT lResult = 0;
6070 HIMC hImc;
6071
6072 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6073 return lResult;
6074 switch (dwCommand)
6075 {
6076 case IMN_SETOPENSTATUS:
6077 if (pImmGetOpenStatus(hImc))
6078 {
K.Takatac81e9bf2022-01-16 14:15:49 +00006079 LOGFONTW lf = norm_logfont;
6080 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6081 // Work around when PerMonitorV2 is not enabled in the process level.
6082 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6083 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 im_set_position(gui.row, gui.col);
6085
Bram Moolenaar734a8672019-12-02 22:49:38 +01006086 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01006087 State &= ~MODE_LANGMAP;
6088 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006090# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6093 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006094 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 int old_row = gui.row;
6096 int old_col = gui.col;
6097
6098 // This must be called here before
6099 // status_redraw_curbuf(), otherwise the mode
6100 // message may appear in the wrong position.
6101 showmode();
6102 status_redraw_curbuf();
6103 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006104 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 gui.row = old_row;
6106 gui.col = old_col;
6107 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 }
6110 }
6111 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01006112 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 lResult = 0;
6114 break;
6115 }
6116 pImmReleaseContext(hWnd, hImc);
6117 return lResult;
6118}
6119
6120 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006121_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122{
6123 char_u *ret;
6124 int len;
6125
Bram Moolenaar734a8672019-12-02 22:49:38 +01006126 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 return 0;
6128
6129 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6130 if (ret != NULL)
6131 {
6132 add_to_input_buf_csi(ret, len);
6133 vim_free(ret);
6134 return 1;
6135 }
6136 return 0;
6137}
6138
6139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 * void GetResultStr()
6141 *
6142 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6143 * get complete composition string
6144 */
6145 static char_u *
6146GetResultStr(HWND hwnd, int GCS, int *lenp)
6147{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006148 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00006149 LONG ret;
6150 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 char_u *convbuf = NULL;
6152
6153 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6154 return NULL;
6155
K.Takatab0b2b732022-01-19 12:59:21 +00006156 // Get the length of the composition string.
6157 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6158 if (ret <= 0)
6159 return NULL;
6160
6161 // Allocate the requested buffer plus space for the NUL character.
6162 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 if (buf == NULL)
6164 return NULL;
6165
K.Takatab0b2b732022-01-19 12:59:21 +00006166 // Reads in the composition string.
6167 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6168 *lenp = ret / sizeof(WCHAR);
6169
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006170 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 pImmReleaseContext(hwnd, hIMC);
6172 vim_free(buf);
6173 return convbuf;
6174}
6175#endif
6176
Bram Moolenaar734a8672019-12-02 22:49:38 +01006177// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006178#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180/*
6181 * set font to IM.
6182 */
6183 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006184im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 HIMC hImc;
6187
6188 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6189 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006190 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 pImmReleaseContext(s_hwnd, hImc);
6192 }
6193}
6194
6195/*
6196 * Notify cursor position to IM.
6197 */
6198 void
6199im_set_position(int row, int col)
6200{
6201 HIMC hImc;
6202
6203 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6204 {
6205 COMPOSITIONFORM cfs;
6206
6207 cfs.dwStyle = CFS_POINT;
6208 cfs.ptCurrentPos.x = FILL_X(col);
6209 cfs.ptCurrentPos.y = FILL_Y(row);
6210 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00006211 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6212 {
6213 // Work around when PerMonitorV2 is not enabled in the process level.
6214 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6215 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 pImmSetCompositionWindow(hImc, &cfs);
6218
6219 pImmReleaseContext(s_hwnd, hImc);
6220 }
6221}
6222
6223/*
6224 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6225 */
6226 void
6227im_set_active(int active)
6228{
6229 HIMC hImc;
6230 static HIMC hImcOld = (HIMC)0;
6231
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006232# ifdef VIMDLL
6233 if (!gui.in_use && !gui.starting)
6234 {
6235 mbyte_im_set_active(active);
6236 return;
6237 }
6238# endif
6239
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006240 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6241 return;
6242
6243 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006245 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006247 hImcOld = pImmGetContext(s_hwnd);
6248 if (hImcOld)
6249 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006251 active = FALSE;
6252 }
6253 else if (hImcOld != (HIMC)0)
6254 {
6255 pImmAssociateContext(s_hwnd, hImcOld);
6256 hImcOld = (HIMC)0;
6257 }
6258
6259 hImc = pImmGetContext(s_hwnd);
6260 if (!hImc)
6261 return;
6262
6263 /*
6264 * for Korean ime
6265 */
6266 HKL hKL = GetKeyboardLayout(0);
6267
6268 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6269 {
6270 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6271 static BOOL bSaved = FALSE;
6272
6273 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006275 // if we have a saved conversion status, restore it
6276 if (bSaved)
6277 pImmSetConversionStatus(hImc, dwConversionSaved,
6278 dwSentenceSaved);
6279 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006283 // save conversion status and disable korean
6284 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6285 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00006286 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006287 bSaved = TRUE;
6288 pImmSetConversionStatus(hImc,
6289 dwConversionSaved & ~(IME_CMODE_NATIVE
6290 | IME_CMODE_FULLSHAPE),
6291 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006295
6296 pImmSetOpenStatus(hImc, active);
6297 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298}
6299
6300/*
6301 * Get IM status. When IM is on, return not 0. Else return 0.
6302 */
6303 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006304im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 int status = 0;
6307 HIMC hImc;
6308
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006309# ifdef VIMDLL
6310 if (!gui.in_use && !gui.starting)
6311 return mbyte_im_get_status();
6312# endif
6313
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6315 {
6316 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6317 pImmReleaseContext(s_hwnd, hImc);
6318 }
6319 return status;
6320}
6321
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006325/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006326 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006327 */
6328 static void
6329latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6330{
6331 int c;
6332
Bram Moolenaarca003e12006-03-17 23:19:38 +00006333 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006334 {
6335 c = *text++;
6336 switch (c)
6337 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006338 case 0xa4: c = 0x20ac; break; // euro
6339 case 0xa6: c = 0x0160; break; // S hat
6340 case 0xa8: c = 0x0161; break; // S -hat
6341 case 0xb4: c = 0x017d; break; // Z hat
6342 case 0xb8: c = 0x017e; break; // Z -hat
6343 case 0xbc: c = 0x0152; break; // OE
6344 case 0xbd: c = 0x0153; break; // oe
6345 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006346 }
6347 *unicodebuf++ = c;
6348 }
6349}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351#ifdef FEAT_RIGHTLEFT
6352/*
6353 * What is this for? In the case where you are using Win98 or Win2K or later,
6354 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6355 * reverses the string sent to the TextOut... family. This sucks, because we
6356 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6357 * way to tell Windblows not to do this!
6358 *
6359 * The short of it is that this 'RevOut' only gets called if you are running
6360 * one of the new, "improved" MS OSes, and only if you are running in
6361 * 'rightleft' mode. It makes display take *slightly* longer, but not
6362 * noticeably so.
6363 */
6364 static void
K.Takata135e1522022-01-29 15:27:58 +00006365RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 int col,
6367 int row,
6368 UINT foptions,
6369 CONST RECT *pcliprect,
6370 LPCTSTR text,
6371 UINT len,
6372 CONST INT *padding)
6373{
6374 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006376 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006377 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006378 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379}
6380#endif
6381
Bram Moolenaar92467d32017-12-05 13:22:16 +01006382 static void
6383draw_line(
6384 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006385 int y1,
6386 int x2,
6387 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006388 COLORREF color)
6389{
6390#if defined(FEAT_DIRECTX)
6391 if (IS_ENABLE_DIRECTX())
6392 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6393 else
6394#endif
6395 {
6396 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6397 HPEN old_pen = SelectObject(s_hdc, hpen);
6398 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006399 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006400 LineTo(s_hdc, x2, y2);
6401 DeleteObject(SelectObject(s_hdc, old_pen));
6402 }
6403}
6404
6405 static void
6406set_pixel(
6407 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006408 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006409 COLORREF color)
6410{
6411#if defined(FEAT_DIRECTX)
6412 if (IS_ENABLE_DIRECTX())
6413 DWriteContext_SetPixel(s_dwc, x, y, color);
6414 else
6415#endif
6416 SetPixel(s_hdc, x, y, color);
6417}
6418
6419 static void
6420fill_rect(
6421 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006422 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006423 COLORREF color)
6424{
6425#if defined(FEAT_DIRECTX)
6426 if (IS_ENABLE_DIRECTX())
6427 DWriteContext_FillRect(s_dwc, rcp, color);
6428 else
6429#endif
6430 {
6431 HBRUSH hbr2;
6432
6433 if (hbr == NULL)
6434 hbr2 = CreateSolidBrush(color);
6435 else
6436 hbr2 = hbr;
6437 FillRect(s_hdc, rcp, hbr2);
6438 if (hbr == NULL)
6439 DeleteBrush(hbr2);
6440 }
6441}
6442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 void
6444gui_mch_draw_string(
6445 int row,
6446 int col,
6447 char_u *text,
6448 int len,
6449 int flags)
6450{
6451 static int *padding = NULL;
6452 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 const RECT *pcliprect = NULL;
6454 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 static WCHAR *unicodebuf = NULL;
6456 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006457 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 int y;
6460
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 /*
6462 * Italic and bold text seems to have an extra row of pixels at the bottom
6463 * (below where the bottom of the character should be). If we draw the
6464 * characters with a solid background, the top row of pixels in the
6465 * character below will be overwritten. We can fix this by filling in the
6466 * background ourselves, to the correct character proportions, and then
6467 * writing the character in transparent mode. Still have a problem when
6468 * the character is "_", which gets written on to the character below.
6469 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6470 * pixel in their slots, which fixes the problem with the bottom row of
6471 * pixels. We still need this code because otherwise the top row of pixels
6472 * becomes a problem. - webb.
6473 */
6474 static HBRUSH hbr_cache[2] = {NULL, NULL};
6475 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6476 static int brush_lru = 0;
6477 HBRUSH hbr;
6478 RECT rc;
6479
6480 if (!(flags & DRAW_TRANSP))
6481 {
6482 /*
6483 * Clear background first.
6484 * Note: FillRect() excludes right and bottom of rectangle.
6485 */
6486 rc.left = FILL_X(col);
6487 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (has_mbyte)
6489 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006490 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006491 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 rc.right = FILL_X(col + len);
6495 rc.bottom = FILL_Y(row + 1);
6496
Bram Moolenaar734a8672019-12-02 22:49:38 +01006497 // Cache the created brush, that saves a lot of time. We need two:
6498 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (gui.currBgColor == brush_color[0])
6500 {
6501 hbr = hbr_cache[0];
6502 brush_lru = 1;
6503 }
6504 else if (gui.currBgColor == brush_color[1])
6505 {
6506 hbr = hbr_cache[1];
6507 brush_lru = 0;
6508 }
6509 else
6510 {
6511 if (hbr_cache[brush_lru] != NULL)
6512 DeleteBrush(hbr_cache[brush_lru]);
6513 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6514 brush_color[brush_lru] = gui.currBgColor;
6515 hbr = hbr_cache[brush_lru];
6516 brush_lru = !brush_lru;
6517 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006518
Bram Moolenaar92467d32017-12-05 13:22:16 +01006519 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521 SetBkMode(s_hdc, TRANSPARENT);
6522
6523 /*
6524 * When drawing block cursor, prevent inverted character spilling
6525 * over character cell (can happen with bold/italic)
6526 */
6527 if (flags & DRAW_CURSOR)
6528 {
6529 pcliprect = &rc;
6530 foptions = ETO_CLIPPED;
6531 }
6532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 SetTextColor(s_hdc, gui.currFgColor);
6534 SelectFont(s_hdc, gui.currFont);
6535
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006536#ifdef FEAT_DIRECTX
6537 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006538 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006539#endif
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6542 {
K.Takata135e1522022-01-29 15:27:58 +00006543 int i;
6544
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 vim_free(padding);
6546 pad_size = Columns;
6547
Bram Moolenaar734a8672019-12-02 22:49:38 +01006548 // Don't give an out-of-memory message here, it would call us
6549 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006550 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (padding != NULL)
6552 for (i = 0; i < pad_size; i++)
6553 padding[i] = gui.char_width;
6554 }
6555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 /*
6557 * We have to provide the padding argument because italic and bold versions
6558 * of fixed-width fonts are often one pixel or so wider than their normal
6559 * versions.
6560 * No check for DRAW_BOLD, Windows will have done it already.
6561 */
6562
Bram Moolenaar734a8672019-12-02 22:49:38 +01006563 // Check if there are any UTF-8 characters. If not, use normal text
6564 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (enc_utf8)
6566 for (n = 0; n < len; ++n)
6567 if (text[n] >= 0x80)
6568 break;
6569
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006570#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006571 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6572 // required that unicode drawing routine, currently. So this forces it
6573 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006574 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006575 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006576#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006577
Bram Moolenaar734a8672019-12-02 22:49:38 +01006578 // Check if the Unicode buffer exists and is big enough. Create it
6579 // with the same length as the multi-byte string, the number of wide
6580 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006581 if ((enc_utf8
6582 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6583 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 && (unicodebuf == NULL || len > unibuflen))
6585 {
6586 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006587 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006590 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
6592 unibuflen = len;
6593 }
6594
6595 if (enc_utf8 && n < len && unicodebuf != NULL)
6596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006597 // Output UTF-8 characters. Composing characters should be
6598 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006599 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006600 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006601 int cells; // cell width of string up to composing char
6602 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006603 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006605 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006607 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006609 c = utf_ptr2char(text + i);
6610 if (c >= 0x10000)
6611 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006612 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006613 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6614 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006615 }
6616 else
6617 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006618 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006619 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006620
6621 if (utf_iscomposing(c))
6622 cw = 0;
6623 else
6624 {
6625 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006626 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006627 cw = 1;
6628 }
6629
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (unicodepdy != NULL)
6631 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006632 // Use unicodepdy to make characters fit as we expect, even
6633 // when the font uses different widths (e.g., bold character
6634 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006635 if (c >= 0x10000)
6636 {
6637 unicodepdy[wlen - 2] = cw * gui.char_width;
6638 unicodepdy[wlen - 1] = 0;
6639 }
6640 else
6641 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006644 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006646#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006647 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006648 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006649 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006650 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006651 TEXT_X(col), TEXT_Y(row),
6652 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006653 gui.char_width, gui.currFgColor,
6654 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006655 }
6656 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006657#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006658 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6659 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006660 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006662 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006664 // If we want to display codepage data, and the current CP is not the
6665 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (unicodebuf != NULL)
6667 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006668 if (enc_latin9)
6669 latin9_to_ucs(text, len, unicodebuf);
6670 else
6671 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 MB_PRECOMPOSED,
6673 (char *)text, len,
6674 (LPWSTR)unicodebuf, unibuflen);
6675 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006676 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006677 // Use unicodepdy to make characters fit as we expect, even
6678 // when the font uses different widths (e.g., bold character
6679 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006680 if (unicodepdy != NULL)
6681 {
6682 int i;
6683 int cw;
6684
6685 for (i = 0; i < len; ++i)
6686 {
6687 cw = utf_char2cells(unicodebuf[i]);
6688 if (cw > 2)
6689 cw = 1;
6690 unicodepdy[i] = cw * gui.char_width;
6691 }
6692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006694 foptions, pcliprect, unicodebuf, len, unicodepdy);
6695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
6697 }
6698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 {
6700#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006701 // Windows will mess up RL text, so we have to draw it character by
6702 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006703 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6705 foptions, pcliprect, (char *)text, len, padding);
6706 else
6707#endif
6708 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6709 foptions, pcliprect, (char *)text, len, padding);
6710 }
6711
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (flags & DRAW_UNDERL)
6714 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006715 // When p_linespace is 0, overwrite the bottom row of pixels.
6716 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 if (p_linespace > 1)
6719 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006720 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006722
Bram Moolenaar734a8672019-12-02 22:49:38 +01006723 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006724 if (flags & DRAW_STRIKE)
6725 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006726 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006727 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006728 }
6729
Bram Moolenaar734a8672019-12-02 22:49:38 +01006730 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006731 if (flags & DRAW_UNDERC)
6732 {
6733 int x;
6734 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006735 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006736
6737 y = FILL_Y(row + 1) - 1;
6738 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6739 {
6740 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006741 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006742 }
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744}
6745
6746
6747/*
6748 * Output routines.
6749 */
6750
Bram Moolenaar734a8672019-12-02 22:49:38 +01006751/*
6752 * Flush any output to the screen
6753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 void
6755gui_mch_flush(void)
6756{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006757#if defined(FEAT_DIRECTX)
6758 if (IS_ENABLE_DIRECTX())
6759 DWriteContext_Flush(s_dwc);
6760#endif
6761
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 GdiFlush();
6763}
6764
6765 static void
6766clear_rect(RECT *rcp)
6767{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006768 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769}
6770
6771
Bram Moolenaarc716c302006-01-21 22:12:51 +00006772 void
6773gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6774{
6775 RECT workarea_rect;
6776
6777 get_work_area(&workarea_rect);
6778
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006779 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006780 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6781 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006782
Bram Moolenaar734a8672019-12-02 22:49:38 +01006783 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6784 // the menubar for MSwin, we subtract it from the screen height, so that
6785 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006787 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6788 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6789 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6790 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006791}
6792
6793
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794#if defined(FEAT_MENU) || defined(PROTO)
6795/*
6796 * Add a sub menu to the menu bar.
6797 */
6798 void
6799gui_mch_add_menu(
6800 vimmenu_T *menu,
6801 int pos)
6802{
6803 vimmenu_T *parent = menu->parent;
6804
6805 menu->submenu_id = CreatePopupMenu();
6806 menu->id = s_menu_id++;
6807
6808 if (menu_is_menubar(menu->name))
6809 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006810 WCHAR *wn;
6811 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006813 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006814 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006815 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006817 infow.cbSize = sizeof(infow);
6818 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6819 | MIIM_SUBMENU;
6820 infow.dwItemData = (long_u)menu;
6821 infow.wID = menu->id;
6822 infow.fType = MFT_STRING;
6823 infow.dwTypeData = wn;
6824 infow.cch = (UINT)wcslen(wn);
6825 infow.hSubMenu = menu->submenu_id;
6826 InsertMenuItemW((parent == NULL)
6827 ? s_menuBar : parent->submenu_id,
6828 (UINT)pos, TRUE, &infow);
6829 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831
Bram Moolenaar734a8672019-12-02 22:49:38 +01006832 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 if (parent == NULL)
6834 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006835# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 else if (IsWindow(parent->tearoff_handle))
6837 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839}
6840
6841 void
6842gui_mch_show_popupmenu(vimmenu_T *menu)
6843{
6844 POINT mp;
6845
K.Takata45f9cfb2022-01-21 11:11:00 +00006846 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6848}
6849
6850 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006851gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 vimmenu_T *menu = gui_find_menu(path_name);
6854
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006855 if (menu == NULL)
6856 return;
6857
6858 POINT p;
6859
6860 // Find the position of the current cursor
6861 GetDCOrgEx(s_hdc, &p);
6862 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006864 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006866 gui_mch_getmouse(&mx, &my);
6867 p.x += mx;
6868 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006870 else if (curwin != NULL)
6871 {
6872 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6873 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6874 }
6875 msg_scroll = FALSE;
6876 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877}
6878
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006879# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880/*
6881 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6882 * create it as a pseudo-"tearoff menu".
6883 */
6884 void
6885gui_make_tearoff(char_u *path_name)
6886{
6887 vimmenu_T *menu = gui_find_menu(path_name);
6888
Bram Moolenaar734a8672019-12-02 22:49:38 +01006889 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (menu != NULL)
6891 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6892}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894
6895/*
6896 * Add a menu item to a menu
6897 */
6898 void
6899gui_mch_add_menu_item(
6900 vimmenu_T *menu,
6901 int idx)
6902{
6903 vimmenu_T *parent = menu->parent;
6904
6905 menu->id = s_menu_id++;
6906 menu->submenu_id = NULL;
6907
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006908# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6910 {
6911 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6912 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6913 }
6914 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006915# endif
6916# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 if (menu_is_toolbar(parent->name))
6918 {
6919 TBBUTTON newtb;
6920
Bram Moolenaara80faa82020-04-12 19:37:17 +02006921 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 if (menu_is_separator(menu->name))
6923 {
6924 newtb.iBitmap = 0;
6925 newtb.fsStyle = TBSTYLE_SEP;
6926 }
6927 else
6928 {
6929 newtb.iBitmap = get_toolbar_bitmap(menu);
6930 newtb.fsStyle = TBSTYLE_BUTTON;
6931 }
6932 newtb.idCommand = menu->id;
6933 newtb.fsState = TBSTATE_ENABLED;
6934 newtb.iString = 0;
6935 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6936 (LPARAM)&newtb);
6937 menu->submenu_id = (HMENU)-1;
6938 }
6939 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006942 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006944 wn = enc_to_utf16(menu->name, NULL);
6945 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006947 InsertMenuW(parent->submenu_id, (UINT)idx,
6948 (menu_is_separator(menu->name)
6949 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6950 (UINT)menu->id, wn);
6951 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006953# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 if (IsWindow(parent->tearoff_handle))
6955 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958}
6959
6960/*
6961 * Destroy the machine specific menu widget.
6962 */
6963 void
6964gui_mch_destroy_menu(vimmenu_T *menu)
6965{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006966# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 /*
6968 * is this a toolbar button?
6969 */
6970 if (menu->submenu_id == (HMENU)-1)
6971 {
6972 int iButton;
6973
6974 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6975 (WPARAM)menu->id, 0);
6976 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6977 }
6978 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 {
6981 if (menu->parent != NULL
6982 && menu_is_popup(menu->parent->dname)
6983 && menu->parent->submenu_id != NULL)
6984 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6985 else
6986 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6987 if (menu->submenu_id != NULL)
6988 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006989# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (IsWindow(menu->tearoff_handle))
6991 DestroyWindow(menu->tearoff_handle);
6992 if (menu->parent != NULL
6993 && menu->parent->children != NULL
6994 && IsWindow(menu->parent->tearoff_handle))
6995 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006996 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 menu->modes = 0;
6998 rebuild_tearoff(menu->parent);
6999 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 }
7002}
7003
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007004# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 static void
7006rebuild_tearoff(vimmenu_T *menu)
7007{
Bram Moolenaar734a8672019-12-02 22:49:38 +01007008 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 char_u tbuf[128];
7010 RECT trect;
7011 RECT rct;
7012 RECT roct;
7013 int x, y;
7014
7015 HWND thwnd = menu->tearoff_handle;
7016
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007017 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 if (GetWindowRect(thwnd, &trect)
7019 && GetWindowRect(s_hwnd, &rct)
7020 && GetClientRect(s_hwnd, &roct))
7021 {
7022 x = trect.left - rct.left;
7023 y = (trect.top - rct.bottom + roct.bottom);
7024 }
7025 else
7026 {
7027 x = y = 0xffffL;
7028 }
7029 DestroyWindow(thwnd);
7030 if (menu->children != NULL)
7031 {
7032 gui_mch_tearoff(tbuf, menu, x, y);
7033 if (IsWindow(menu->tearoff_handle))
7034 (void) SetWindowPos(menu->tearoff_handle,
7035 NULL,
7036 (int)trect.left,
7037 (int)trect.top,
7038 0, 0,
7039 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7040 }
7041}
Bram Moolenaar734a8672019-12-02 22:49:38 +01007042# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043
7044/*
7045 * Make a menu either grey or not grey.
7046 */
7047 void
7048gui_mch_menu_grey(
7049 vimmenu_T *menu,
7050 int grey)
7051{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007052# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 /*
7054 * is this a toolbar button?
7055 */
7056 if (menu->submenu_id == (HMENU)-1)
7057 {
7058 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00007059 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
7061 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007062# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007063 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7064 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007066# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7068 {
7069 WORD menuID;
7070 HWND menuHandle;
7071
7072 /*
7073 * A tearoff button has changed state.
7074 */
7075 if (menu->children == NULL)
7076 menuID = (WORD)(menu->id);
7077 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007078 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7080 if (menuHandle)
7081 EnableWindow(menuHandle, !grey);
7082
7083 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007084# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085}
7086
Bram Moolenaar734a8672019-12-02 22:49:38 +01007087#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
7089
Bram Moolenaar734a8672019-12-02 22:49:38 +01007090// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007093#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
7095#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7096/*
7097 * stuff for dialogs
7098 */
7099
7100/*
7101 * The callback routine used by all the dialogs. Very simple. First,
7102 * acknowledges the INITDIALOG message so that Windows knows to do standard
7103 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7104 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7105 * number.
7106 */
7107 static LRESULT CALLBACK
7108dialog_callback(
7109 HWND hwnd,
7110 UINT message,
7111 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007112 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 if (message == WM_INITDIALOG)
7115 {
7116 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 (void)SetFocus(hwnd);
7119 if (dialog_default_button > IDCANCEL)
7120 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007121 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007122 // We don't have a default, set focus on another element of the
7123 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007124 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 return FALSE;
7126 }
7127
7128 if (message == WM_COMMAND)
7129 {
7130 int button = LOWORD(wParam);
7131
Bram Moolenaar734a8672019-12-02 22:49:38 +01007132 // Don't end the dialog if something was selected that was
7133 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (button >= DLG_NONBUTTON_CONTROL)
7135 return TRUE;
7136
Bram Moolenaar734a8672019-12-02 22:49:38 +01007137 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007139 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007140 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007141 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007142
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007143 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7144 p = utf16_to_enc(wp, NULL);
John Marriott031f2272025-04-23 20:56:08 +02007145 if (p != NULL)
7146 {
7147 vim_strncpy(s_textfield, p, IOSIZE);
7148 vim_free(p);
7149 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007150 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 /*
7154 * Need to check for IDOK because if the user just hits Return to
7155 * accept the default value, some reason this is what we get.
7156 */
7157 if (button == IDOK)
7158 {
7159 if (dialog_default_button > IDCANCEL)
7160 EndDialog(hwnd, dialog_default_button);
7161 }
7162 else
7163 EndDialog(hwnd, button - IDCANCEL);
7164 return TRUE;
7165 }
7166
7167 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7168 {
7169 EndDialog(hwnd, 0);
7170 return TRUE;
7171 }
7172 return FALSE;
7173}
7174
7175/*
7176 * Create a dialog dynamically from the parameter strings.
7177 * type = type of dialog (question, alert, etc.)
7178 * title = dialog title. may be NULL for default title.
7179 * message = text to display. Dialog sizes to accommodate it.
7180 * buttons = '\n' separated list of button captions, default first.
7181 * dfltbutton = number of default button.
7182 *
7183 * This routine returns 1 if the first button is pressed,
7184 * 2 for the second, etc.
7185 *
7186 * 0 indicates Esc was pressed.
7187 * -1 for unexpected error
7188 *
7189 * If stubbing out this fn, return 1.
7190 */
7191
Bram Moolenaar734a8672019-12-02 22:49:38 +01007192static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 "IDR_VIM",
7195 "IDR_VIM_ERROR",
7196 "IDR_VIM_ALERT",
7197 "IDR_VIM_INFO",
7198 "IDR_VIM_QUESTION"
7199};
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int
7202gui_mch_dialog(
7203 int type,
7204 char_u *title,
7205 char_u *message,
7206 char_u *buttons,
7207 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007208 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007209 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007212 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 int numButtons;
7214 int *buttonWidths, *buttonPositions;
7215 int buttonYpos;
7216 int nchar, i;
7217 DWORD lStyle;
7218 int dlgwidth = 0;
7219 int dlgheight;
7220 int editboxheight;
7221 int horizWidth = 0;
7222 int msgheight;
7223 char_u *pstart;
7224 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007225 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 char_u *tbuffer;
7227 RECT rect;
7228 HWND hwnd;
7229 HDC hdc;
7230 HFONT font, oldFont;
7231 TEXTMETRIC fontInfo;
7232 int fontHeight;
7233 int textWidth, minButtonWidth, messageWidth;
7234 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007235 int maxDialogHeight;
7236 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 int vertical;
7238 int dlgPaddingX;
7239 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007240# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007241 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007243# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007244 garray_T ga;
7245 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007246 int dlg_icon_width;
7247 int dlg_icon_height;
7248 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007250# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007251 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007252# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007253 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007254# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007255 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007256 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
Bram Moolenaar748bf032005-02-02 23:04:36 +00007259 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007260 {
7261 load_dpi_func();
7262 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007263 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007264 }
7265 else
7266 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267
7268 if ((type < 0) || (type > VIM_LAST_TYPE))
7269 type = 0;
7270
Bram Moolenaar734a8672019-12-02 22:49:38 +01007271 // allocate some memory for dialog template
7272 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007273 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007274 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
7276 if (p == NULL)
7277 return -1;
7278
7279 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007280 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7282 * const.
7283 */
7284 tbuffer = vim_strsave(buttons);
7285 if (tbuffer == NULL)
7286 return -1;
7287
Bram Moolenaar734a8672019-12-02 22:49:38 +01007288 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289
Bram Moolenaar734a8672019-12-02 22:49:38 +01007290 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 numButtons = 1;
7292 for (i = 0; tbuffer[i] != '\0'; i++)
7293 {
7294 if (tbuffer[i] == DLG_BUTTON_SEP)
7295 numButtons++;
7296 }
7297 if (dfltbutton >= numButtons)
7298 dfltbutton = -1;
7299
Bram Moolenaar734a8672019-12-02 22:49:38 +01007300 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007301 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (buttonWidths == NULL)
7303 return -1;
7304
Bram Moolenaar734a8672019-12-02 22:49:38 +01007305 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007306 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (buttonPositions == NULL)
7308 return -1;
7309
7310 /*
7311 * Calculate how big the dialog must be.
7312 */
7313 hwnd = GetDesktopWindow();
7314 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007315# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7317 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007318 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 use_lfSysmenu = TRUE;
7320 }
7321 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007322# endif
K.Takatad1c58992022-01-23 12:31:57 +00007323 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7324 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7325
7326 oldFont = SelectFont(hdc, font);
7327 dlgPaddingX = DLG_PADDING_X;
7328 dlgPaddingY = DLG_PADDING_Y;
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 GetTextMetrics(hdc, &fontInfo);
7331 fontHeight = fontInfo.tmHeight;
7332
Bram Moolenaar734a8672019-12-02 22:49:38 +01007333 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007334 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar734a8672019-12-02 22:49:38 +01007336 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007337 if (s_hwnd == NULL)
7338 {
7339 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340
Bram Moolenaar734a8672019-12-02 22:49:38 +01007341 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007342 get_work_area(&workarea_rect);
7343 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007344 if (maxDialogWidth > adjust_by_system_dpi(600))
7345 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007346 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007347 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007348 }
7349 else
7350 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007351 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007352 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007353 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007354 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7355 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7356 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7357 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007358
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007359 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007360 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7361 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7362 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7363 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7364 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007365 }
7366
Bram Moolenaar734a8672019-12-02 22:49:38 +01007367 // Set dlgwidth to width of message.
7368 // Copy the message into "ga", changing NL to CR-NL and inserting line
7369 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 pstart = message;
7371 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007372 msgheight = 0;
7373 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 do
7375 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007376 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007377
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 // Need to figure out where to break the string. The system does it
7379 // at a word boundary, which would mean we can't compute the number of
7380 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007381 textWidth = 0;
7382 last_white = NULL;
7383 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007384 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007385 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007386 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007387 && textWidth > maxDialogWidth * 3 / 4)
7388 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007389 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007390 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007391 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007392 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007393 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007394 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007395 textWidth = 0;
7396
7397 if (last_white != NULL)
7398 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007399 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007400 if (pend > last_white)
7401 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007402 pend = last_white + 1;
7403 last_white = NULL;
7404 }
7405 ga_append(&ga, '\r');
7406 ga_append(&ga, '\n');
7407 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007408 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007409
7410 while (--l >= 0)
7411 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007412 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007413 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007415
7416 ga_append(&ga, '\r');
7417 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 pstart = pend + 1;
7419 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007420
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007421 if (ga.ga_data != NULL)
7422 message = ga.ga_data;
7423
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007425
K.Takatac81e9bf2022-01-16 14:15:49 +00007426 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7427 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
K.Takatac81e9bf2022-01-16 14:15:49 +00007429 // Add width of icon to dlgwidth, and some space
7430 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7431 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7432
7433 if (msgheight < dlg_icon_height)
7434 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
7436 /*
7437 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007438 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007440 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (!vertical)
7442 {
7443 // Place buttons horizontally if they fit.
7444 horizWidth = dlgPaddingX;
7445 pstart = tbuffer;
7446 i = 0;
7447 do
7448 {
7449 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7450 if (pend == NULL)
7451 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007452 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 if (textWidth < minButtonWidth)
7454 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007455 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456 buttonWidths[i] = textWidth;
7457 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007458 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 pstart = pend + 1;
7460 } while (*pend != NUL);
7461
7462 if (horizWidth > maxDialogWidth)
7463 vertical = TRUE; // Too wide to fit on the screen.
7464 else if (horizWidth > dlgwidth)
7465 dlgwidth = horizWidth;
7466 }
7467
7468 if (vertical)
7469 {
7470 // Stack buttons vertically.
7471 pstart = tbuffer;
7472 do
7473 {
7474 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7475 if (pend == NULL)
7476 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007477 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007478 textWidth += dlgPaddingX; // Padding within button
7479 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (textWidth > dlgwidth)
7481 dlgwidth = textWidth;
7482 pstart = pend + 1;
7483 } while (*pend != NUL);
7484 }
7485
7486 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007487 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Bram Moolenaar734a8672019-12-02 22:49:38 +01007489 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007490 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
7492 add_long(lStyle);
7493 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007494 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 add_word(0); // NumberOfItems(will change later)
7496 add_word(10); // x
7497 add_word(10); // y
7498 add_word(PixelToDialogX(dlgwidth)); // cx
7499
7500 // Dialog height.
7501 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007502 dlgheight = msgheight + 2 * dlgPaddingY
7503 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 else
7505 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7506
7507 // Dialog needs to be taller if contains an edit box.
7508 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7509 if (textfield != NULL)
7510 dlgheight += editboxheight;
7511
Bram Moolenaar734a8672019-12-02 22:49:38 +01007512 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007513 if (dlgheight > maxDialogHeight)
7514 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007515 msgheight = msgheight - (dlgheight - maxDialogHeight);
7516 dlgheight = maxDialogHeight;
7517 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007518 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007519 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007520 }
7521
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 add_word(PixelToDialogY(dlgheight));
7523
7524 add_word(0); // Menu
7525 add_word(0); // Class
7526
Bram Moolenaar734a8672019-12-02 22:49:38 +01007527 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007528 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7529 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 p += nchar;
7531
K.Takatad1c58992022-01-23 12:31:57 +00007532 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007533# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007534 if (use_lfSysmenu)
7535 {
7536 // point size
7537 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7538 GetDeviceCaps(hdc, LOGPIXELSY));
7539 wcscpy(p, lfSysmenu.lfFaceName);
7540 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
K.Takatad1c58992022-01-23 12:31:57 +00007542 else
7543# endif
7544 {
7545 *p++ = DLG_FONT_POINT_SIZE; // point size
7546 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7547 }
7548 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 buttonYpos = msgheight + 2 * dlgPaddingY;
7551
7552 if (textfield != NULL)
7553 buttonYpos += editboxheight;
7554
7555 pstart = tbuffer;
7556 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007557 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 for (i = 0; i < numButtons; i++)
7559 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007560 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 for ( pend = pstart;
7562 *pend && (*pend != DLG_BUTTON_SEP);
7563 pend++)
7564 ;
7565
7566 if (*pend)
7567 *pend = '\0';
7568
7569 /*
7570 * old NOTE:
7571 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7572 * the focus to the first tab-able button and in so doing makes that
7573 * the default!! Grrr. Workaround: Make the default button the only
7574 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7575 * he/she can use arrow keys.
7576 *
7577 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007578 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 * dialog. Also needed for when the textfield is the default control.
7580 * It appears to work now (perhaps not on Win95?).
7581 */
7582 if (vertical)
7583 {
7584 p = add_dialog_element(p,
7585 (i == dfltbutton
7586 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7587 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007588 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 + 2 * fontHeight * i),
7590 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7591 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007592 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 }
7594 else
7595 {
7596 p = add_dialog_element(p,
7597 (i == dfltbutton
7598 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7599 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007600 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 PixelToDialogX(buttonWidths[i]),
7602 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007603 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007605 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607 *pnumitems += numButtons;
7608
Bram Moolenaar734a8672019-12-02 22:49:38 +01007609 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 p = add_dialog_element(p, SS_ICON,
7611 PixelToDialogX(dlgPaddingX),
7612 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007613 PixelToDialogX(dlg_icon_width),
7614 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7616 dlg_icons[type]);
7617
Bram Moolenaar734a8672019-12-02 22:49:38 +01007618 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007619 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007620 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007621 PixelToDialogY(dlgPaddingY),
7622 (WORD)(PixelToDialogX(messageWidth) + 1),
7623 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007624 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625
Bram Moolenaar734a8672019-12-02 22:49:38 +01007626 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (textfield != NULL)
7628 {
7629 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7630 PixelToDialogX(2 * dlgPaddingX),
7631 PixelToDialogY(2 * dlgPaddingY + msgheight),
7632 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7633 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007634 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 *pnumitems += 1;
7636 }
7637
7638 *pnumitems += 2;
7639
7640 SelectFont(hdc, oldFont);
7641 DeleteObject(font);
7642 ReleaseDC(hwnd, hdc);
7643
Bram Moolenaar734a8672019-12-02 22:49:38 +01007644 // Let the dialog_callback() function know which button to make default
7645 // If we have an edit box, make that the default. We also need to tell
7646 // dialog_callback() if this dialog contains an edit box or not. We do
7647 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 if (textfield != NULL)
7649 {
7650 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7651 s_textfield = textfield;
7652 }
7653 else
7654 {
7655 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7656 s_textfield = NULL;
7657 }
7658
Bram Moolenaar734a8672019-12-02 22:49:38 +01007659 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007661 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 (LPDLGTEMPLATE)pdlgtemplate,
7663 s_hwnd,
7664 (DLGPROC)dialog_callback);
7665
7666 LocalFree(LocalHandle(pdlgtemplate));
7667 vim_free(tbuffer);
7668 vim_free(buttonWidths);
7669 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007670 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
Bram Moolenaar734a8672019-12-02 22:49:38 +01007672 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 (void)SetFocus(s_hwnd);
7674
7675 return nchar;
7676}
7677
Bram Moolenaar734a8672019-12-02 22:49:38 +01007678#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007679
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680/*
7681 * Put a simple element (basic class) onto a dialog template in memory.
7682 * return a pointer to where the next item should be added.
7683 *
7684 * parameters:
7685 * lStyle = additional style flags
7686 * (be careful, NT3.51 & Win32s will ignore the new ones)
7687 * x,y = x & y positions IN DIALOG UNITS
7688 * w,h = width and height IN DIALOG UNITS
7689 * Id = ID used in messages
7690 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7691 * caption = usually text or resource name
7692 *
7693 * TODO: use the length information noted here to enable the dialog creation
7694 * routines to work out more exactly how much memory they need to alloc.
7695 */
7696 static PWORD
7697add_dialog_element(
7698 PWORD p,
7699 DWORD lStyle,
7700 WORD x,
7701 WORD y,
7702 WORD w,
7703 WORD h,
7704 WORD Id,
7705 WORD clss,
7706 const char *caption)
7707{
7708 int nchar;
7709
Bram Moolenaar734a8672019-12-02 22:49:38 +01007710 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7712 *p++ = LOWORD(lStyle);
7713 *p++ = HIWORD(lStyle);
7714 *p++ = 0; // LOWORD (lExtendedStyle)
7715 *p++ = 0; // HIWORD (lExtendedStyle)
7716 *p++ = x;
7717 *p++ = y;
7718 *p++ = w;
7719 *p++ = h;
7720 *p++ = Id; //9 or 10 words in all
7721
7722 *p++ = (WORD)0xffff;
7723 *p++ = clss; //2 more here
7724
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007725 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 p += nchar;
7727
7728 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7729
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007730 return p; // total = 15 + strlen(caption) words
7731 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
7734
7735/*
7736 * Helper routine. Take an input pointer, return closest pointer that is
7737 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7738 */
7739 static LPWORD
7740lpwAlign(
7741 LPWORD lpIn)
7742{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007743 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007745 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 ul += 3;
7747 ul >>= 2;
7748 ul <<= 2;
7749 return (LPWORD)ul;
7750}
7751
7752/*
7753 * Helper routine. Takes second parameter as Ansi string, copies it to first
7754 * parameter as wide character (16-bits / char) string, and returns integer
7755 * number of wide characters (words) in string (including the trailing wide
7756 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007757 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7758 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 static int
7760nCopyAnsiToWideChar(
7761 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007762 LPSTR lpAnsiIn,
7763 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007766 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 int i;
7768 WCHAR *wn;
7769
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007770 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007772 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007773 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (wn != NULL)
7775 {
7776 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007777 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 vim_free(wn);
7779 }
7780 }
7781 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007782 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 nChar = MultiByteToWideChar(
7784 enc_codepage > 0 ? enc_codepage : CP_ACP,
7785 MB_PRECOMPOSED,
7786 lpAnsiIn, len,
7787 lpWCStr, len);
7788 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007789 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791
7792 return nChar;
7793}
7794
7795
7796#ifdef FEAT_TEAROFF
7797/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007798 * Lookup menu handle from "menu_id".
7799 */
7800 static HMENU
7801tearoff_lookup_menuhandle(
7802 vimmenu_T *menu,
7803 WORD menu_id)
7804{
7805 for ( ; menu != NULL; menu = menu->next)
7806 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007807 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007808 continue;
7809 if (menu_is_separator(menu->dname))
7810 continue;
7811 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7812 return menu->submenu_id;
7813 }
7814 return NULL;
7815}
7816
7817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 * The callback function for all the modeless dialogs that make up the
7819 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7820 * thinking its menus have been clicked), and go away when closed.
7821 */
7822 static LRESULT CALLBACK
7823tearoff_callback(
7824 HWND hwnd,
7825 UINT message,
7826 WPARAM wParam,
7827 LPARAM lParam)
7828{
7829 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007830 {
7831 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
Bram Moolenaar734a8672019-12-02 22:49:38 +01007835 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 HandleMouseHide(message, lParam);
7837
7838 if (message == WM_COMMAND)
7839 {
7840 if ((WORD)(LOWORD(wParam)) & 0x8000)
7841 {
7842 POINT mp;
7843 RECT rect;
7844
7845 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7846 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007847 vimmenu_T *menu;
7848
7849 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007851 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7853 (int)rect.right - 8,
7854 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007855 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 s_hwnd,
7857 NULL);
7858 /*
7859 * NOTE: The pop-up menu can eat the mouse up event.
7860 * We deal with this in normal.c.
7861 */
7862 }
7863 }
7864 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007865 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7867 /*
7868 * Give main window the focus back: this is so after
7869 * choosing a tearoff button you can start typing again
7870 * straight away.
7871 */
7872 (void)SetFocus(s_hwnd);
7873 return TRUE;
7874 }
7875 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7876 {
7877 DestroyWindow(hwnd);
7878 return TRUE;
7879 }
7880
Bram Moolenaar734a8672019-12-02 22:49:38 +01007881 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (message == WM_EXITSIZEMOVE)
7883 (void)SetActiveWindow(s_hwnd);
7884
7885 return FALSE;
7886}
7887#endif
7888
7889
7890/*
K.Takatad1c58992022-01-23 12:31:57 +00007891 * Computes the dialog base units based on the current dialog font.
7892 * We don't use the GetDialogBaseUnits() API, because we don't use the
7893 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 */
7895 static void
7896get_dialog_font_metrics(void)
7897{
7898 HDC hdc;
7899 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 SIZE size;
7901#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007902 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903#endif
7904
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007906 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007907 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#endif
7910 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007911 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912
K.Takatad1c58992022-01-23 12:31:57 +00007913 hdc = GetDC(s_hwnd);
7914 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007915 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007916 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917
K.Takataabe628e2022-01-23 16:25:17 +00007918 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007919 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920}
7921
7922#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7923/*
7924 * Create a pseudo-"tearoff menu" based on the child
7925 * items of a given menu pointer.
7926 */
7927 static void
7928gui_mch_tearoff(
7929 char_u *title,
7930 vimmenu_T *menu,
7931 int initX,
7932 int initY)
7933{
7934 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7935 int template_len;
7936 int nchar, textWidth, submenuWidth;
7937 DWORD lStyle;
7938 DWORD lExtendedStyle;
7939 WORD dlgwidth;
7940 WORD menuID;
7941 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007942 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 vimmenu_T *the_menu = menu;
7944 HWND hwnd;
7945 HDC hdc;
7946 HFONT font, oldFont;
7947 int col, spaceWidth, len;
7948 int columnWidths[2];
7949 char_u *label, *text;
7950 int acLen = 0;
7951 int nameLen;
7952 int padding0, padding1, padding2 = 0;
7953 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007954 int x;
7955 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007956# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007957 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960
7961 /*
7962 * If this menu is already torn off, move it to the mouse position.
7963 */
7964 if (IsWindow(menu->tearoff_handle))
7965 {
7966 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007967 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
7969 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7970 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7971 }
7972 return;
7973 }
7974
7975 /*
7976 * Create a new tearoff.
7977 */
7978 if (*title == MNU_HIDDEN_CHAR)
7979 title++;
7980
Bram Moolenaar734a8672019-12-02 22:49:38 +01007981 // Allocate memory to store the dialog template. It's made bigger when
7982 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 template_len = DLG_ALLOC_SIZE;
7984 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7985 if (p == NULL)
7986 return;
7987
7988 hwnd = GetDesktopWindow();
7989 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007990# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7992 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007993 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 use_lfSysmenu = TRUE;
7995 }
7996 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007997# endif
K.Takatad1c58992022-01-23 12:31:57 +00007998 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7999 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
8000
8001 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
Bram Moolenaar734a8672019-12-02 22:49:38 +01008003 // Calculate width of a single space. Used for padding columns to the
8004 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008005 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
Bram Moolenaar734a8672019-12-02 22:49:38 +01008007 // Figure out max width of the text column, the accelerator column and the
8008 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 submenuWidth = 0;
8010 for (col = 0; col < 2; col++)
8011 {
8012 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008013 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008015 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 text = (col == 0) ? pmenu->dname : pmenu->actext;
8017 if (text != NULL && *text != NUL)
8018 {
8019 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8020 if (textWidth > columnWidths[col])
8021 columnWidths[col] = textWidth;
8022 }
8023 if (pmenu->children != NULL)
8024 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8025 }
8026 }
8027 if (columnWidths[1] == 0)
8028 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008029 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 if (submenuWidth != 0)
8031 columnWidths[0] += submenuWidth;
8032 else
8033 columnWidths[0] += spaceWidth;
8034 }
8035 else
8036 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008037 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8039 columnWidths[1] += submenuWidth;
8040 }
8041
8042 /*
8043 * Now find the total width of our 'menu'.
8044 */
8045 textWidth = columnWidths[0] + columnWidths[1];
8046 if (submenuWidth != 0)
8047 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008048 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8050 textWidth += submenuWidth;
8051 }
8052 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8053 if (textWidth > dlgwidth)
8054 dlgwidth = textWidth;
8055 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8056
Bram Moolenaar734a8672019-12-02 22:49:38 +01008057 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00008058 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
8060 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8061 *p++ = LOWORD(lStyle);
8062 *p++ = HIWORD(lStyle);
8063 *p++ = LOWORD(lExtendedStyle);
8064 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008065 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008067 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008069 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 else
8071 *p++ = PixelToDialogX(initX); // x
8072 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008073 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 else
8075 *p++ = PixelToDialogY(initY); // y
8076 *p++ = PixelToDialogX(dlgwidth); // cx
8077 ptrueheight = p;
8078 *p++ = 0; // dialog height: changed later anyway
8079 *p++ = 0; // Menu
8080 *p++ = 0; // Class
8081
Bram Moolenaar734a8672019-12-02 22:49:38 +01008082 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008084 ? (LPSTR)title
8085 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 p += nchar;
8087
K.Takatad1c58992022-01-23 12:31:57 +00008088 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008089# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00008090 if (use_lfSysmenu)
8091 {
8092 // point size
8093 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8094 GetDeviceCaps(hdc, LOGPIXELSY));
8095 wcscpy(p, lfSysmenu.lfFaceName);
8096 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 }
K.Takatad1c58992022-01-23 12:31:57 +00008098 else
8099# endif
8100 {
8101 *p++ = DLG_FONT_POINT_SIZE; // point size
8102 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8103 }
8104 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105
8106 /*
8107 * Loop over all the items in the menu.
8108 * But skip over the tearbar.
8109 */
8110 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8111 menu = menu->children->next;
8112 else
8113 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008114 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 for ( ; menu != NULL; menu = menu->next)
8116 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008117 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 continue;
8119 if (menu_is_separator(menu->dname))
8120 {
8121 sepPadding += 3;
8122 continue;
8123 }
8124
Bram Moolenaar734a8672019-12-02 22:49:38 +01008125 // Check if there still is plenty of room in the template. Make it
8126 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8128 {
8129 WORD *newp;
8130
8131 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8132 if (newp != NULL)
8133 {
8134 template_len += 4096;
8135 mch_memmove(newp, pdlgtemplate,
8136 (char *)p - (char *)pdlgtemplate);
8137 p = newp + (p - pdlgtemplate);
8138 pnumitems = newp + (pnumitems - pdlgtemplate);
8139 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8140 LocalFree(LocalHandle(pdlgtemplate));
8141 pdlgtemplate = newp;
8142 }
8143 }
8144
Bram Moolenaar734a8672019-12-02 22:49:38 +01008145 // Figure out minimal length of this menu label. Use "name" for the
8146 // actual text, "dname" for estimating the displayed size. "name"
8147 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 len = nameLen = (int)STRLEN(menu->name);
8149 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8150 (int)STRLEN(menu->dname))) / spaceWidth;
8151 len += padding0;
8152
8153 if (menu->actext != NULL)
8154 {
8155 acLen = (int)STRLEN(menu->actext);
8156 len += acLen;
8157 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8158 }
8159 else
8160 textWidth = 0;
8161 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8162 len += padding1;
8163
8164 if (menu->children == NULL)
8165 {
8166 padding2 = submenuWidth / spaceWidth;
8167 len += padding2;
8168 menuID = (WORD)(menu->id);
8169 }
8170 else
8171 {
8172 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008173 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 }
8175
Bram Moolenaar734a8672019-12-02 22:49:38 +01008176 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008177 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 if (label == NULL)
8179 break;
8180
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008181 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008182 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008184 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 while (padding0-- > 0)
8186 *text++ = ' ';
8187 if (menu->actext != NULL)
8188 {
8189 STRNCPY(text, menu->actext, acLen);
8190 text += acLen;
8191 }
8192 while (padding1-- > 0)
8193 *text++ = ' ';
8194 if (menu->children != NULL)
8195 {
8196 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8197 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8198 }
8199 else
8200 {
8201 while (padding2-- > 0)
8202 *text++ = ' ';
8203 }
8204 *text = NUL;
8205
8206 /*
8207 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8208 * W95/NT4 it makes the tear-off look more like a menu.
8209 */
8210 p = add_dialog_element(p,
8211 BS_PUSHBUTTON|BS_LEFT,
8212 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8213 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8214 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8215 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008216 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 vim_free(label);
8218 (*pnumitems)++;
8219 }
8220
8221 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8222
8223
Bram Moolenaar734a8672019-12-02 22:49:38 +01008224 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008225 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008226 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 (LPDLGTEMPLATE)pdlgtemplate,
8228 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008229 (DLGPROC)tearoff_callback,
8230 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 LocalFree(LocalHandle(pdlgtemplate));
8233 SelectFont(hdc, oldFont);
8234 DeleteObject(font);
8235 ReleaseDC(hwnd, hdc);
8236
8237 /*
8238 * Reassert ourselves as the active window. This is so that after creating
8239 * a tearoff, the user doesn't have to click with the mouse just to start
8240 * typing again!
8241 */
8242 (void)SetActiveWindow(s_hwnd);
8243
Bram Moolenaar734a8672019-12-02 22:49:38 +01008244 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 force_menu_update = TRUE;
8246}
8247#endif
8248
8249#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008250# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252/*
8253 * Create the toolbar, initially unpopulated.
8254 * (just like the menu, there are no defaults, it's all
8255 * set up through menu.vim)
8256 */
8257 static void
8258initialise_toolbar(void)
8259{
8260 InitCommonControls();
8261 s_toolbarhwnd = CreateToolbarEx(
8262 s_hwnd,
8263 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8264 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008265 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008266 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 IDR_TOOLBAR1, // id of initial bitmap
8268 NULL,
8269 0, // initial number of buttons
8270 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8271 TOOLBAR_BUTTON_HEIGHT,
8272 TOOLBAR_BUTTON_WIDTH,
8273 TOOLBAR_BUTTON_HEIGHT,
8274 sizeof(TBBUTTON)
8275 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008276
8277 // Remove transparency from the toolbar to prevent the main window
8278 // background colour showing through
8279 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8280 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8281
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008282 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283
8284 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008285
8286 update_toolbar_size();
8287}
8288
8289 static void
8290update_toolbar_size(void)
8291{
8292 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008293 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00008294
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008295 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00008296 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8297 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8298 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8299 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8300
8301 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8302 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8303 //TRACE("button size: %d, %d", w, h);
8304 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8305 gui.toolbar_height = h + 6;
8306
8307 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8308 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8309
8310 // TODO:
8311 // Currently, this function only updates the size of toolbar buttons.
8312 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313}
8314
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008315 static LRESULT CALLBACK
8316toolbar_wndproc(
8317 HWND hwnd,
8318 UINT uMsg,
8319 WPARAM wParam,
8320 LPARAM lParam)
8321{
8322 HandleMouseHide(uMsg, lParam);
8323 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8324}
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 static int
8327get_toolbar_bitmap(vimmenu_T *menu)
8328{
8329 int i = -1;
8330
8331 /*
8332 * Check user bitmaps first, unless builtin is specified.
8333 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008334 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
8336 char_u fname[MAXPATHL];
8337 HANDLE hbitmap = NULL;
8338
8339 if (menu->iconfile != NULL)
8340 {
8341 gui_find_iconfile(menu->iconfile, fname, "bmp");
8342 hbitmap = LoadImage(
8343 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008344 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 IMAGE_BITMAP,
8346 TOOLBAR_BUTTON_WIDTH,
8347 TOOLBAR_BUTTON_HEIGHT,
8348 LR_LOADFROMFILE |
8349 LR_LOADMAP3DCOLORS
8350 );
8351 }
8352
8353 /*
8354 * If the LoadImage call failed, or the "icon=" file
8355 * didn't exist or wasn't specified, try the menu name
8356 */
8357 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008358 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008359# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008360 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008361# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008362 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 hbitmap = LoadImage(
8364 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008365 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 IMAGE_BITMAP,
8367 TOOLBAR_BUTTON_WIDTH,
8368 TOOLBAR_BUTTON_HEIGHT,
8369 LR_LOADFROMFILE |
8370 LR_LOADMAP3DCOLORS
8371 );
8372
8373 if (hbitmap != NULL)
8374 {
8375 TBADDBITMAP tbAddBitmap;
8376
8377 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008378 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379
8380 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8381 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008382 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384 }
8385 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8386 i = menu->iconidx;
8387
8388 return i;
8389}
8390#endif
8391
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008392#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8393 static void
8394initialise_tabline(void)
8395{
8396 InitCommonControls();
8397
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008398 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008399 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008400 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008401 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008402 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008403
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008404 gui.tabline_height = TABLINE_HEIGHT;
8405
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008406 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008407}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008408
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008409/*
8410 * Get tabpage_T from POINT.
8411 */
8412 static tabpage_T *
8413GetTabFromPoint(
8414 HWND hWnd,
8415 POINT pt)
8416{
8417 tabpage_T *ptp = NULL;
8418
8419 if (gui_mch_showing_tabline())
8420 {
8421 TCHITTESTINFO htinfo;
8422 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008423 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008424 if (s_tabhwnd == hWnd)
8425 {
8426 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8427 if (idx != -1)
8428 ptp = find_tabpage(idx + 1);
8429 }
8430 }
8431 return ptp;
8432}
8433
8434static POINT s_pt = {0, 0};
8435static HCURSOR s_hCursor = NULL;
8436
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008437 static LRESULT CALLBACK
8438tabline_wndproc(
8439 HWND hwnd,
8440 UINT uMsg,
8441 WPARAM wParam,
8442 LPARAM lParam)
8443{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008444 POINT pt;
8445 tabpage_T *tp;
8446 RECT rect;
8447 int nCenter;
8448 int idx0;
8449 int idx1;
8450
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008451 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008452
8453 switch (uMsg)
8454 {
8455 case WM_LBUTTONDOWN:
8456 {
8457 s_pt.x = GET_X_LPARAM(lParam);
8458 s_pt.y = GET_Y_LPARAM(lParam);
8459 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008460 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008461 break;
8462 }
8463 case WM_MOUSEMOVE:
8464 if (GetCapture() == hwnd
8465 && ((wParam & MK_LBUTTON)) != 0)
8466 {
8467 pt.x = GET_X_LPARAM(lParam);
8468 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008469 if (abs(pt.x - s_pt.x) >
8470 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008471 {
8472 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8473
8474 tp = GetTabFromPoint(hwnd, pt);
8475 if (tp != NULL)
8476 {
8477 idx0 = tabpage_index(curtab) - 1;
8478 idx1 = tabpage_index(tp) - 1;
8479
8480 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8481 nCenter = rect.left + (rect.right - rect.left) / 2;
8482
Bram Moolenaar734a8672019-12-02 22:49:38 +01008483 // Check if the mouse cursor goes over the center of
8484 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008485 if ((idx0 < idx1) && (nCenter < pt.x))
8486 {
8487 tabpage_move(idx1 + 1);
8488 update_screen(0);
8489 }
8490 else if ((idx1 < idx0) && (pt.x < nCenter))
8491 {
8492 tabpage_move(idx1);
8493 update_screen(0);
8494 }
8495 }
8496 }
8497 }
8498 break;
8499 case WM_LBUTTONUP:
8500 {
8501 if (GetCapture() == hwnd)
8502 {
8503 SetCursor(s_hCursor);
8504 ReleaseCapture();
8505 }
8506 break;
8507 }
regomne3bdef102022-09-26 20:48:32 +01008508 case WM_MBUTTONUP:
8509 {
8510 TCHITTESTINFO htinfo;
8511
8512 htinfo.pt.x = GET_X_LPARAM(lParam);
8513 htinfo.pt.y = GET_Y_LPARAM(lParam);
8514 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8515 if (idx0 != -1)
8516 {
8517 idx0 += 1;
8518 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8519 }
8520 break;
8521 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008522 default:
8523 break;
8524 }
8525
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008526 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8527}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008528#endif
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8531/*
8532 * Make the GUI window come to the foreground.
8533 */
8534 void
8535gui_mch_set_foreground(void)
8536{
8537 if (IsIconic(s_hwnd))
8538 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8539 SetForegroundWindow(s_hwnd);
8540}
8541#endif
8542
8543#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8544 static void
8545dyn_imm_load(void)
8546{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008547 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 if (hLibImm == NULL)
8549 return;
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008552 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008554 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008556 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008558 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008560 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008562 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008563 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008564 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008565 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008566 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008568 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008570 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008571 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008572 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573
K.Takatab0b2b732022-01-19 12:59:21 +00008574 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 || pImmGetContext == NULL
8576 || pImmAssociateContext == NULL
8577 || pImmReleaseContext == NULL
8578 || pImmGetOpenStatus == NULL
8579 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008580 || pImmGetCompositionFontW == NULL
8581 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008583 || pImmGetConversionStatus == NULL
8584 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
8586 FreeLibrary(hLibImm);
8587 hLibImm = NULL;
8588 pImmGetContext = NULL;
8589 return;
8590 }
8591
8592 return;
8593}
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595#endif
8596
8597#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8598
8599# ifdef FEAT_XPM_W32
8600# define IMAGE_XPM 100
8601# endif
8602
8603typedef struct _signicon_t
8604{
8605 HANDLE hImage;
8606 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008607# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008608 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610} signicon_t;
8611
8612 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008613gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614{
8615 signicon_t *sign;
8616 int x, y, w, h;
8617
8618 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8619 return;
8620
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008621# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008622 if (IS_ENABLE_DIRECTX())
8623 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008624# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008625
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 x = TEXT_X(col);
8627 y = TEXT_Y(row);
8628 w = gui.char_width * 2;
8629 h = gui.char_height;
8630 switch (sign->uType)
8631 {
8632 case IMAGE_BITMAP:
8633 {
8634 HDC hdcMem;
8635 HBITMAP hbmpOld;
8636
8637 hdcMem = CreateCompatibleDC(s_hdc);
8638 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8639 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8640 SelectObject(hdcMem, hbmpOld);
8641 DeleteDC(hdcMem);
8642 }
8643 break;
8644 case IMAGE_ICON:
8645 case IMAGE_CURSOR:
8646 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8647 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008648# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 case IMAGE_XPM:
8650 {
8651 HDC hdcMem;
8652 HBITMAP hbmpOld;
8653
8654 hdcMem = CreateCompatibleDC(s_hdc);
8655 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008656 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8658
8659 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008660 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8662 SelectObject(hdcMem, hbmpOld);
8663 DeleteDC(hdcMem);
8664 }
8665 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 }
8668}
8669
8670 static void
8671close_signicon_image(signicon_t *sign)
8672{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008673 if (sign == NULL)
8674 return;
8675
8676 switch (sign->uType)
8677 {
8678 case IMAGE_BITMAP:
8679 DeleteObject((HGDIOBJ)sign->hImage);
8680 break;
8681 case IMAGE_CURSOR:
8682 DestroyCursor((HCURSOR)sign->hImage);
8683 break;
8684 case IMAGE_ICON:
8685 DestroyIcon((HICON)sign->hImage);
8686 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008687# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008688 case IMAGE_XPM:
8689 DeleteObject((HBITMAP)sign->hImage);
8690 DeleteObject((HBITMAP)sign->hShape);
8691 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008692# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694}
8695
8696 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008697gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 signicon_t sign, *psign;
8700 char_u *ext;
8701
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008703 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (ext > signfile)
8705 {
8706 int do_load = 1;
8707
8708 if (!STRICMP(ext, ".bmp"))
8709 sign.uType = IMAGE_BITMAP;
8710 else if (!STRICMP(ext, ".ico"))
8711 sign.uType = IMAGE_ICON;
8712 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8713 sign.uType = IMAGE_CURSOR;
8714 else
8715 do_load = 0;
8716
8717 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008718 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 gui.char_width * 2, gui.char_height,
8720 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008721# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 if (!STRICMP(ext, ".xpm"))
8723 {
8724 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008725 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8726 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730
8731 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008732 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 *psign = sign;
8734
8735 if (!psign)
8736 {
8737 if (sign.hImage)
8738 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008739 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 }
8741 return (void *)psign;
8742
8743}
8744
8745 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008746gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008748 if (sign == NULL)
8749 return;
8750
8751 close_signicon_image((signicon_t *)sign);
8752 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008756#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
Bram Moolenaar734a8672019-12-02 22:49:38 +01008758/*
8759 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008760 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008762 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8764 * to get current mouse position).
8765 *
8766 * Trying to use as more Windows services as possible, and as less
8767 * IE version as possible :)).
8768 *
8769 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8770 * BalloonEval struct.
8771 * 2) Enable/Disable simply create/kill BalloonEval Timer
8772 * 3) When there was enough inactivity, timer procedure posts
8773 * async request to debugger
8774 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8775 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008776 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 */
8778
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008779 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008780make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008781{
K.Takata76687d22022-01-25 10:31:37 +00008782 TOOLINFOW *pti;
8783 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008784
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008785 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008786 if (pti == NULL)
8787 return;
8788
8789 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8790 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8791 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008792 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008793
8794 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8795 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8796
K.Takata76687d22022-01-25 10:31:37 +00008797 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008798 pti->uFlags = TTF_SUBCLASS;
8799 pti->hwnd = beval->target;
8800 pti->hinst = 0; // Don't use string resources
8801 pti->uId = ID_BEVAL_TOOLTIP;
8802
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008803 pti->lpszText = LPSTR_TEXTCALLBACKW;
8804 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8805 pti->lParam = (LPARAM)beval->tofree;
8806 // switch multiline tooltips on
8807 if (GetClientRect(s_textArea, &rect))
8808 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8809 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008810
8811 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8812 pti->rect.left = pt.x - 3;
8813 pti->rect.top = pt.y - 3;
8814 pti->rect.right = pt.x + 3;
8815 pti->rect.bottom = pt.y + 3;
8816
8817 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8818 // Make tooltip appear sooner.
8819 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8820 // I've performed some tests and it seems the longest possible life time
8821 // of tooltip is 30 seconds.
8822 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8823 /*
8824 * HACK: force tooltip to appear, because it'll not appear until
8825 * first mouse move. D*mn M$
8826 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8827 */
8828 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8829 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8830 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008831}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008832
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008834delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008836 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837}
8838
8839 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008840beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008841 HWND hwnd UNUSED,
8842 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008843 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008844 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 POINT pt;
8847 RECT rect;
8848
8849 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8850 return;
8851
8852 GetCursorPos(&pt);
8853 if (WindowFromPoint(pt) != s_textArea)
8854 return;
8855
8856 ScreenToClient(s_textArea, &pt);
8857 GetClientRect(s_textArea, &rect);
8858 if (!PtInRect(&rect, pt))
8859 return;
8860
K.Takataa8ec4912022-02-03 14:32:33 +00008861 if (last_user_activity > 0
8862 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 && (cur_beval->showState != ShS_PENDING
8864 || abs(cur_beval->x - pt.x) > 3
8865 || abs(cur_beval->y - pt.y) > 3))
8866 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008867 // Pointer resting in one place long enough, it's time to show
8868 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 cur_beval->showState = ShS_PENDING;
8870 cur_beval->x = pt.x;
8871 cur_beval->y = pt.y;
8872
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 if (cur_beval->msgCB != NULL)
8874 (*cur_beval->msgCB)(cur_beval, 0);
8875 }
8876}
8877
8878 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008879gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
K.Takataa8ec4912022-02-03 14:32:33 +00008881 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882}
8883
8884 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008885gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 if (beval == NULL)
8888 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008889 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8890 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891}
8892
8893 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008894gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895{
8896 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008897
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008898 vim_free(beval->msg);
8899 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8900 if (beval->msg == NULL)
8901 {
8902 delete_tooltip(beval);
8903 beval->showState = ShS_NEUTRAL;
8904 return;
8905 }
8906
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (beval->showState == ShS_SHOWING)
8908 return;
8909 GetCursorPos(&pt);
8910 ScreenToClient(s_textArea, &pt);
8911
8912 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008914 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 gui_mch_disable_beval_area(cur_beval);
8916 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008917 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919}
8920
8921 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008922gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008923 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008924 char_u *mesg,
8925 void (*mesgCB)(BalloonEval *, int),
8926 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008928 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 BalloonEval *beval;
8930
8931 if (mesg != NULL && mesgCB != NULL)
8932 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008933 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 return NULL;
8935 }
8936
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008937 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 if (beval != NULL)
8939 {
8940 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941
8942 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 beval->msg = mesg;
8944 beval->msgCB = mesgCB;
8945 beval->clientData = clientData;
8946
8947 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 cur_beval = beval;
8949
8950 if (p_beval)
8951 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953 return beval;
8954}
8955
8956 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008957Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008959 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 return;
8961
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008962 if (cur_beval == NULL)
8963 return;
8964
8965 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008967 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008968 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008969 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 delete_tooltip(cur_beval);
8971 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
8973 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008974 break;
8975 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008976 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008977 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008978 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008979 info->lpszText = (LPSTR)info->lParam;
8980 info->uFlags |= TTF_DI_SETITEM;
8981 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008982 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008983 case TTN_GETDISPINFOW:
8984 {
8985 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008986 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008987 info->lpszText = (LPWSTR)info->lParam;
8988 info->uFlags |= TTF_DI_SETITEM;
8989 }
8990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 }
8992}
8993
8994 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008995track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996{
8997 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8998 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008999 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009003gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009005# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009006 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009007# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009008 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 vim_free(beval);
9010}
Bram Moolenaar734a8672019-12-02 22:49:38 +01009011#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012
9013#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9014/*
9015 * We have multiple signs to draw at the same location. Draw the
9016 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9017 */
9018 void
9019netbeans_draw_multisign_indicator(int row)
9020{
9021 int i;
9022 int y;
9023 int x;
9024
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009025 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009026 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 x = 0;
9029 y = TEXT_Y(row);
9030
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009031# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009032 if (IS_ENABLE_DIRECTX())
9033 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009034# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009035
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 for (i = 0; i < gui.char_height - 3; i++)
9037 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9038
9039 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9040 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9041 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9042 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9043 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9044 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9045 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9046}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009047#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009048
9049#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009050
Christopher Plewright20b795e2022-12-20 20:01:58 +00009051// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9052// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00009053// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00009054 static int
9055test_gui_w32_sendevent_mouse(dict_T *args)
9056{
9057 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009058 return FALSE;
9059
Christopher Plewright20b795e2022-12-20 20:01:58 +00009060 // Note: "move" is optional, requires fewer arguments
9061 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009062
Christopher Plewright20b795e2022-12-20 20:01:58 +00009063 if (!move && (!dict_has_key(args, "button")
9064 || !dict_has_key(args, "multiclick")
9065 || !dict_has_key(args, "modifiers")))
9066 return FALSE;
9067
9068 int row = (int)dict_get_number(args, "row");
9069 int col = (int)dict_get_number(args, "col");
9070
9071 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009072 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00009073 // the "move" argument expects row and col coordnates to be in pixels,
9074 // unless "cell" is specified and is TRUE.
9075 if (dict_get_bool(args, "cell", FALSE))
9076 {
9077 // calculate the middle of the character cell
Hirohito Higashifbe4a8f2025-04-27 15:28:30 +02009078 // Note: Cell coordinates are 1-based from Vim script
Christopher Plewright20b795e2022-12-20 20:01:58 +00009079 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9080 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9081 gui_mouse_moved(pX, pY);
9082 }
9083 else
9084 gui_mouse_moved(col, row);
9085 }
9086 else
9087 {
9088 int button = (int)dict_get_number(args, "button");
9089 int repeated_click = (int)dict_get_number(args, "multiclick");
9090 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009091
Christopher Plewright20b795e2022-12-20 20:01:58 +00009092 // Reset the scroll values to known values.
9093 // XXX: Remove this when/if the scroll step is made configurable.
9094 mouse_set_hor_scroll_step(6);
9095 mouse_set_vert_scroll_step(3);
9096
9097 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9098 repeated_click, mods);
9099 }
9100 return TRUE;
9101}
9102
9103 static int
9104test_gui_w32_sendevent_keyboard(dict_T *args)
9105{
9106 INPUT inputs[1];
9107 INPUT modkeys[3];
9108 SecureZeroMemory(inputs, sizeof(INPUT));
9109 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9110
9111 char_u *event = dict_get_string(args, "event", TRUE);
9112
9113 if (event && (STRICMP(event, "keydown") == 0
9114 || STRICMP(event, "keyup") == 0))
9115 {
9116 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009117 if (vkCode <= 0 || vkCode >= 0xFF)
9118 {
9119 semsg(_(e_invalid_argument_nr), (long)vkCode);
9120 return FALSE;
9121 }
9122
Christopher Plewright20b795e2022-12-20 20:01:58 +00009123 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9124 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9125 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9126 || vkCode == VK_LMENU || vkCode == VK_RMENU );
9127
9128 BOOL unwrapMods = FALSE;
9129 int mods = (int)dict_get_number(args, "modifiers");
9130
9131 // If there are modifiers in the args, and it is not a keyup event and
9132 // vkCode is not a modifier key, then we generate virtual modifier key
9133 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009134 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009135 {
9136 int n = 0;
9137 if (mods & MOD_MASK_SHIFT)
9138 {
9139 modkeys[n].type = INPUT_KEYBOARD;
9140 modkeys[n].ki.wVk = VK_LSHIFT;
9141 n++;
9142 }
9143 if (mods & MOD_MASK_CTRL)
9144 {
9145 modkeys[n].type = INPUT_KEYBOARD;
9146 modkeys[n].ki.wVk = VK_LCONTROL;
9147 n++;
9148 }
9149 if (mods & MOD_MASK_ALT)
9150 {
9151 modkeys[n].type = INPUT_KEYBOARD;
9152 modkeys[n].ki.wVk = VK_LMENU;
9153 n++;
9154 }
9155 if (n)
9156 {
9157 (void)SetForegroundWindow(s_hwnd);
9158 SendInput(n, modkeys, sizeof(INPUT));
9159 }
9160 }
9161
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009162 inputs[0].type = INPUT_KEYBOARD;
9163 inputs[0].ki.wVk = vkCode;
9164 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009165 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009166 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009167 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009168 unwrapMods = TRUE;
9169 }
9170
K.Takatafef38d82022-09-07 19:03:42 +01009171 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009172 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00009173 vim_free(event);
9174
9175 if (unwrapMods)
9176 {
9177 modkeys[0].type = INPUT_KEYBOARD;
9178 modkeys[0].ki.wVk = VK_LSHIFT;
9179 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9180
9181 modkeys[1].type = INPUT_KEYBOARD;
9182 modkeys[1].ki.wVk = VK_LCONTROL;
9183 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9184
9185 modkeys[2].type = INPUT_KEYBOARD;
9186 modkeys[2].ki.wVk = VK_LMENU;
9187 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9188
9189 (void)SetForegroundWindow(s_hwnd);
9190 SendInput(3, modkeys, sizeof(INPUT));
9191 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009192 }
9193 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00009194 {
9195 if (event == NULL)
9196 {
9197 semsg(_(e_missing_argument_str), "event");
9198 }
9199 else
9200 {
9201 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9202 vim_free(event);
9203 }
9204 return FALSE;
9205 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009206 return TRUE;
9207}
Christopher Plewright20b795e2022-12-20 20:01:58 +00009208
Anton Sharonov68d94722024-01-23 23:19:02 +01009209 static int
9210test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9211{
9212 int handled = 0;
9213 char_u *strategy = dict_get_string(args, "strategy", TRUE);
9214
9215 if (strategy)
9216 {
9217 if (STRICMP(strategy, "classic") == 0)
9218 {
9219 handled = 1;
9220 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9221 }
9222 else if (STRICMP(strategy, "experimental") == 0)
9223 {
9224 handled = 1;
9225 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9226 }
9227 }
9228
9229 if (!handled)
9230 {
9231 if (strategy == NULL)
9232 {
9233 semsg(_(e_missing_argument_str), "strategy");
9234 }
9235 else
9236 {
9237 semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9238 vim_free(strategy);
9239 }
9240 return FALSE;
9241 }
9242 return TRUE;
9243}
9244
9245
Christopher Plewright20b795e2022-12-20 20:01:58 +00009246 int
9247test_gui_w32_sendevent(char_u *event, dict_T *args)
9248{
9249 if (STRICMP(event, "key") == 0)
9250 return test_gui_w32_sendevent_keyboard(args);
9251 else if (STRICMP(event, "mouse") == 0)
9252 return test_gui_w32_sendevent_mouse(args);
Anton Sharonov68d94722024-01-23 23:19:02 +01009253 else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9254 return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
Christopher Plewright20b795e2022-12-20 20:01:58 +00009255 else
9256 {
9257 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9258 return FALSE;
9259 }
9260}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009261#endif