blob: 4e6eca89716433d963400694f5b5116bfcf46d6d [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 {
5387 while (*p && *p != L'"')
5388 ++p;
5389 if (*p)
5390 ++p;
5391 }
5392 else
5393 ++p;
5394 }
5395 cmd = p;
5396 }
5397# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5398 else
5399 {
5400 // Create a session file and pass it to the new process.
5401 LPWSTR wsession;
5402 char_u *savebg;
5403 int ret;
5404
5405 session = vim_tempname('s', FALSE);
5406 if (session == NULL)
5407 goto error;
5408 savebg = p_bg;
John Marriott411ae582025-04-27 15:05:06 +02005409 p_bg = vim_strnsave((char_u *)"light", 5); // Set 'bg' to "light".
5410 if (p_bg == NULL)
5411 {
5412 p_bg = savebg;
5413 goto error;
5414 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005415 ret = write_session_file(session);
5416 vim_free(p_bg);
5417 p_bg = savebg;
5418 if (!ret)
5419 goto error;
5420 wsession = enc_to_utf16(session, NULL);
5421 if (wsession == NULL)
5422 goto error;
5423 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005424 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005425 if (cmd == NULL)
5426 {
5427 vim_free(wsession);
5428 goto error;
5429 }
5430 tofree1 = cmd;
5431 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5432 wsession, wsession);
5433 vim_free(wsession);
5434 }
5435# endif
5436
5437 // Check additional arguments to the `:gui` command.
5438 if (arg != NULL)
5439 {
5440 warg = enc_to_utf16(arg, NULL);
5441 if (warg == NULL)
5442 goto error;
5443 tofree2 = warg;
5444 }
5445 else
5446 warg = L"";
5447
5448 // Set up the new command line.
5449 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005450 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005451 if (newcmd == NULL)
5452 goto error;
5453 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5454
5455 // Spawn a new GUI process.
5456 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5457 NULL, NULL, &si, &pi))
5458 goto error;
5459 CloseHandle(pi.hProcess);
5460 CloseHandle(pi.hThread);
5461 mch_exit(0);
5462
5463error:
5464# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5465 if (session)
5466 mch_remove(session);
5467 vim_free(session);
5468 vim_free(tofree1);
5469# endif
5470 vim_free(newcmd);
5471 vim_free(tofree2);
5472 return gvim_error();
5473}
5474#endif
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476/*
5477 * Parse the GUI related command-line arguments. Any arguments used are
5478 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005479 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 */
5481 void
5482gui_mch_prepare(int *argc, char **argv)
5483{
5484 int silent = FALSE;
5485 int idx;
5486
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5489 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005490 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5492 && (argv[2][0] == '-' || argv[2][0] == '/'))
5493 {
5494 silent = TRUE;
5495 idx = 2;
5496 }
5497 else
5498 idx = 1;
5499
Bram Moolenaar734a8672019-12-02 22:49:38 +01005500 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (STRICMP(argv[idx] + 1, "register") == 0)
5502 {
5503#ifdef FEAT_OLE
5504 RegisterMe(silent);
5505 mch_exit(0);
5506#else
5507 if (!silent)
5508 ole_error("register");
5509 mch_exit(2);
5510#endif
5511 }
5512
Bram Moolenaar734a8672019-12-02 22:49:38 +01005513 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5515 {
5516#ifdef FEAT_OLE
5517 UnregisterMe(!silent);
5518 mch_exit(0);
5519#else
5520 if (!silent)
5521 ole_error("unregister");
5522 mch_exit(2);
5523#endif
5524 }
5525
Bram Moolenaar734a8672019-12-02 22:49:38 +01005526 // Ignore an -embedding argument. It is only relevant if the
5527 // application wants to treat the case when it is started manually
5528 // differently from the case where it is started via automation (and
5529 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5531 {
5532#ifdef FEAT_OLE
5533 *argc = 1;
5534#else
5535 ole_error("embedding");
5536 mch_exit(2);
5537#endif
5538 }
5539 }
5540
5541#ifdef FEAT_OLE
Ken Takatabaaf6de2024-07-29 20:57:19 +02005542# ifdef VIMDLL
5543 if (mch_is_gui_executable())
5544# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
5546 int bDoRestart = FALSE;
5547
5548 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005549 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 if (bDoRestart)
5551 mch_exit(0);
5552 }
5553#endif
5554
5555#ifdef FEAT_NETBEANS_INTG
5556 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005557 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 int arg;
5559
5560 for (arg = 1; arg < *argc; arg++)
5561 if (strncmp("-nb", argv[arg], 3) == 0)
5562 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 netbeansArg = argv[arg];
5564 mch_memmove(&argv[arg], &argv[arg + 1],
5565 (--*argc - arg) * sizeof(char *));
5566 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005567 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
K.Takatac81e9bf2022-01-16 14:15:49 +00005573 static void
5574load_dpi_func(void)
5575{
5576 HMODULE hUser32;
5577
5578 hUser32 = GetModuleHandle("user32.dll");
5579 if (hUser32 == NULL)
5580 goto fail;
5581
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005582 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5583 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5584 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005585 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005586 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5587 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005588
5589 if (pSetThreadDpiAwarenessContext != NULL)
5590 {
5591 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5592 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5593 if (oldctx != NULL)
5594 {
5595 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5596 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5597#ifdef DEBUG
5598 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5599 {
5600 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5601 }
5602#endif
5603 return;
5604 }
5605 }
5606
5607fail:
5608 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005609 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005610 pGetDpiForWindow = NULL;
5611 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5612 pSetThreadDpiAwarenessContext = NULL;
5613 pGetAwarenessFromDpiAwarenessContext = NULL;
5614}
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616/*
5617 * Initialise the GUI. Create all the windows, set up all the call-backs
5618 * etc.
5619 */
5620 int
5621gui_mch_init(void)
5622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005624 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar734a8672019-12-02 22:49:38 +01005627 // Return here if the window was already opened (happens when
5628 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005630 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 /*
5633 * Load the tearoff bitmap
5634 */
5635#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005636 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637#endif
5638
K.Takatac81e9bf2022-01-16 14:15:49 +00005639 load_dpi_func();
5640
5641 s_dpi = pGetDpiForSystem();
5642 update_scrollbar_size();
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005645 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646#endif
5647 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005648#ifdef FEAT_TOOLBAR
5649 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5653
Bram Moolenaar734a8672019-12-02 22:49:38 +01005654 // First try using the wide version, so that we can use any title.
5655 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005656 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005658 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 wndclassw.lpfnWndProc = _WndProc;
5660 wndclassw.cbClsExtra = 0;
5661 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005662 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5664 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5665 wndclassw.hbrBackground = s_brush;
5666 wndclassw.lpszMenuName = NULL;
5667 wndclassw.lpszClassName = szVimWndClassW;
5668
K.Takata4ac893f2022-01-20 12:44:28 +00005669 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005670 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 }
5672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 if (vim_parent_hwnd != NULL)
5674 {
5675#ifdef HAVE_TRY_EXCEPT
5676 __try
5677 {
5678#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005679 // Open inside the specified parent window.
5680 // TODO: last argument should point to a CLIENTCREATESTRUCT
5681 // structure.
5682 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005684 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005685 WS_OVERLAPPEDWINDOW | WS_CHILD
5686 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5688 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005689 100, // Any value will do
5690 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005692 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693#ifdef HAVE_TRY_EXCEPT
5694 }
5695 __except(EXCEPTION_EXECUTE_HANDLER)
5696 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699#endif
5700 if (s_hwnd == NULL)
5701 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005702 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 mch_exit(2);
5704 }
5705 }
5706 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005707 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005708 // If the provided windowid is not valid reset it to zero, so that it
5709 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005710 if (IsWindow((HWND)win_socket_id) <= 0)
5711 win_socket_id = 0;
5712
Bram Moolenaar734a8672019-12-02 22:49:38 +01005713 // Create a window. If win_socket_id is not zero without border and
5714 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005715 s_hwnd = CreateWindowW(
5716 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005717 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5718 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005719 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5720 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005721 100, // Any value will do
5722 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005723 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005724 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005725 if (s_hwnd != NULL && win_socket_id != 0)
5726 {
5727 SetParent(s_hwnd, (HWND)win_socket_id);
5728 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5729 }
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 if (s_hwnd == NULL)
5733 return FAIL;
5734
K.Takatac81e9bf2022-01-16 14:15:49 +00005735 if (pGetDpiForWindow != NULL)
5736 {
5737 s_dpi = pGetDpiForWindow(s_hwnd);
5738 update_scrollbar_size();
5739 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5740 }
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5743 dyn_imm_load();
5744#endif
5745
Bram Moolenaar734a8672019-12-02 22:49:38 +01005746 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005747 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005748 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005749 wndclassw.style = CS_OWNDC;
5750 wndclassw.lpfnWndProc = _TextAreaWndProc;
5751 wndclassw.cbClsExtra = 0;
5752 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005753 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005754 wndclassw.hIcon = NULL;
5755 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5756 wndclassw.hbrBackground = NULL;
5757 wndclassw.lpszMenuName = NULL;
5758 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005759
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005760 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 return FAIL;
5762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005764 s_textArea = CreateWindowExW(
5765 0,
5766 szTextAreaClassW, L"Vim text area",
5767 WS_CHILD | WS_VISIBLE, 0, 0,
5768 100, // Any value will do for now
5769 100, // Any value will do for now
5770 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005771 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 if (s_textArea == NULL)
5774 return FAIL;
5775
Bram Moolenaar20321902016-02-17 12:30:17 +01005776#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005777 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005778 {
5779 HANDLE hIcon = NULL;
5780
5781 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005782 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005783 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005784#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786#ifdef FEAT_MENU
5787 s_menuBar = CreateMenu();
5788#endif
5789 s_hdc = GetDC(s_textArea);
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar734a8672019-12-02 22:49:38 +01005793 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005794 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
Bram Moolenaar734a8672019-12-02 22:49:38 +01005796 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 gui_mch_def_colors();
5798
Bram Moolenaar734a8672019-12-02 22:49:38 +01005799 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5800 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 set_normal_colors();
5802
5803 /*
5804 * Check that none of the colors are the same as the background color.
5805 * Then store the current values as the defaults.
5806 */
5807 gui_check_colors();
5808 gui.def_norm_pixel = gui.norm_pixel;
5809 gui.def_back_pixel = gui.back_pixel;
5810
Bram Moolenaar734a8672019-12-02 22:49:38 +01005811 // Get the colors for the highlight groups (gui_check_colors() might have
5812 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 highlight_gui_started();
5814
5815 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005816 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005818 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 /*
5821 * Set up for Intellimouse processing
5822 */
5823 init_mouse_wheel();
5824
5825 /*
5826 * compute a couple of metrics used for the dialogs
5827 */
5828 get_dialog_font_metrics();
5829#ifdef FEAT_TOOLBAR
5830 /*
5831 * Create the toolbar
5832 */
5833 initialise_toolbar();
5834#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005835#ifdef FEAT_GUI_TABLINE
5836 /*
5837 * Create the tabline
5838 */
5839 initialise_tabline();
5840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#ifdef MSWIN_FIND_REPLACE
5842 /*
5843 * Initialise the dialog box stuff
5844 */
5845 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5846
Bram Moolenaar734a8672019-12-02 22:49:38 +01005847 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005849 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005851 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5853 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5854 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005857#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005858 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005859 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005860#endif
5861
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005862#ifdef FEAT_RENDER_OPTIONS
5863 if (p_rop)
5864 (void)gui_mch_set_rendering_options(p_rop);
5865#endif
5866
Bram Moolenaar748bf032005-02-02 23:04:36 +00005867theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005868 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005869 display_errors();
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 return OK;
5872}
5873
5874/*
5875 * Get the size of the screen, taking position on multiple monitors into
5876 * account (if supported).
5877 */
5878 static void
5879get_work_area(RECT *spi_rect)
5880{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005881 HMONITOR mon;
5882 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar734a8672019-12-02 22:49:38 +01005884 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005885 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005886 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005888 moninfo.cbSize = sizeof(MONITORINFO);
5889 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005891 *spi_rect = moninfo.rcWork;
5892 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
5894 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005895 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5897}
5898
5899/*
5900 * Set the size of the window to the given width and height in pixels.
5901 */
5902 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005903gui_mch_set_shellsize(
5904 int width,
5905 int height,
5906 int min_width UNUSED,
5907 int min_height UNUSED,
5908 int base_width UNUSED,
5909 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005910 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911{
5912 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005913 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar734a8672019-12-02 22:49:38 +01005916 // Try to keep window completely on screen.
5917 // Get position of the screen work area. This is the part that is not
5918 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 get_work_area(&workarea_rect);
5920
Bram Moolenaar734a8672019-12-02 22:49:38 +01005921 // Resizing a maximized window looks very strange, unzoom it first.
5922 // But don't do it when still starting up, it may have been requested in
5923 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005924 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005926
Ken Takata7086b3e2023-10-02 21:26:03 +02005927 if (s_in_dpichanged)
5928 // Use the suggested position when in WM_DPICHANGED.
5929 window_rect = s_suggested_rect;
5930 else
5931 // Use current position.
5932 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar734a8672019-12-02 22:49:38 +01005934 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005935 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5936 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5937 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5938 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5939 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5940 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Bram Moolenaar734a8672019-12-02 22:49:38 +01005942 // The following should take care of keeping Vim on the same monitor, no
5943 // matter if the secondary monitor is left or right of the primary
5944 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005945 window_rect.right = window_rect.left + win_width;
5946 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005947
Bram Moolenaar734a8672019-12-02 22:49:38 +01005948 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005949 // Don't adjust the position when in WM_DPICHANGED.
5950 if (!s_in_dpichanged)
5951 {
5952 if ((direction & RESIZE_HOR)
5953 && window_rect.right > workarea_rect.right)
5954 OffsetRect(&window_rect,
5955 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Ken Takata7086b3e2023-10-02 21:26:03 +02005957 if ((direction & RESIZE_HOR)
5958 && window_rect.left < workarea_rect.left)
5959 OffsetRect(&window_rect,
5960 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961
Ken Takata7086b3e2023-10-02 21:26:03 +02005962 if ((direction & RESIZE_VERT)
5963 && window_rect.bottom > workarea_rect.bottom)
5964 OffsetRect(&window_rect,
5965 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966
Ken Takata7086b3e2023-10-02 21:26:03 +02005967 if ((direction & RESIZE_VERT)
5968 && window_rect.top < workarea_rect.top)
5969 OffsetRect(&window_rect,
5970 0, workarea_rect.top - window_rect.top);
5971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005973 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5974 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Ken Takata7086b3e2023-10-02 21:26:03 +02005976 //TRACE("New pos: %d,%d New size: %d,%d",
5977 // window_rect.left, window_rect.top, win_width, win_height);
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 SetActiveWindow(s_hwnd);
5980 SetFocus(s_hwnd);
5981
Bram Moolenaar734a8672019-12-02 22:49:38 +01005982 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984}
5985
5986
5987 void
5988gui_mch_set_scrollbar_thumb(
5989 scrollbar_T *sb,
5990 long val,
5991 long size,
5992 long max)
5993{
5994 SCROLLINFO info;
5995
5996 sb->scroll_shift = 0;
5997 while (max > 32767)
5998 {
5999 max = (max + 1) >> 1;
6000 val >>= 1;
6001 size >>= 1;
6002 ++sb->scroll_shift;
6003 }
6004
6005 if (sb->scroll_shift > 0)
6006 ++size;
6007
6008 info.cbSize = sizeof(info);
6009 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
6010 info.nPos = val;
6011 info.nMin = 0;
6012 info.nMax = max;
6013 info.nPage = size;
6014 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
6015}
6016
6017
6018/*
6019 * Set the current text font.
6020 */
6021 void
6022gui_mch_set_font(GuiFont font)
6023{
6024 gui.currFont = font;
6025}
6026
6027
6028/*
6029 * Set the current text foreground color.
6030 */
6031 void
6032gui_mch_set_fg_color(guicolor_T color)
6033{
6034 gui.currFgColor = color;
6035}
6036
6037/*
6038 * Set the current text background color.
6039 */
6040 void
6041gui_mch_set_bg_color(guicolor_T color)
6042{
6043 gui.currBgColor = color;
6044}
6045
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006046/*
6047 * Set the current text special color.
6048 */
6049 void
6050gui_mch_set_sp_color(guicolor_T color)
6051{
6052 gui.currSpColor = color;
6053}
6054
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006055#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056/*
6057 * Multi-byte handling, originally by Sung-Hoon Baek.
6058 * First static functions (no prototypes generated).
6059 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006060# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01006061# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006062# endif
6063# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064
6065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 * handle WM_IME_NOTIFY message
6067 */
6068 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006069_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 LRESULT lResult = 0;
6072 HIMC hImc;
6073
6074 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6075 return lResult;
6076 switch (dwCommand)
6077 {
6078 case IMN_SETOPENSTATUS:
6079 if (pImmGetOpenStatus(hImc))
6080 {
K.Takatac81e9bf2022-01-16 14:15:49 +00006081 LOGFONTW lf = norm_logfont;
6082 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6083 // Work around when PerMonitorV2 is not enabled in the process level.
6084 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6085 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 im_set_position(gui.row, gui.col);
6087
Bram Moolenaar734a8672019-12-02 22:49:38 +01006088 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01006089 State &= ~MODE_LANGMAP;
6090 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006092# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006093 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6095 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006096 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 int old_row = gui.row;
6098 int old_col = gui.col;
6099
6100 // This must be called here before
6101 // status_redraw_curbuf(), otherwise the mode
6102 // message may appear in the wrong position.
6103 showmode();
6104 status_redraw_curbuf();
6105 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006106 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 gui.row = old_row;
6108 gui.col = old_col;
6109 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
6112 }
6113 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01006114 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 lResult = 0;
6116 break;
6117 }
6118 pImmReleaseContext(hWnd, hImc);
6119 return lResult;
6120}
6121
6122 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006123_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
6125 char_u *ret;
6126 int len;
6127
Bram Moolenaar734a8672019-12-02 22:49:38 +01006128 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 return 0;
6130
6131 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6132 if (ret != NULL)
6133 {
6134 add_to_input_buf_csi(ret, len);
6135 vim_free(ret);
6136 return 1;
6137 }
6138 return 0;
6139}
6140
6141/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 * void GetResultStr()
6143 *
6144 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6145 * get complete composition string
6146 */
6147 static char_u *
6148GetResultStr(HWND hwnd, int GCS, int *lenp)
6149{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006150 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00006151 LONG ret;
6152 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 char_u *convbuf = NULL;
6154
6155 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6156 return NULL;
6157
K.Takatab0b2b732022-01-19 12:59:21 +00006158 // Get the length of the composition string.
6159 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6160 if (ret <= 0)
6161 return NULL;
6162
6163 // Allocate the requested buffer plus space for the NUL character.
6164 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (buf == NULL)
6166 return NULL;
6167
K.Takatab0b2b732022-01-19 12:59:21 +00006168 // Reads in the composition string.
6169 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6170 *lenp = ret / sizeof(WCHAR);
6171
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006172 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 pImmReleaseContext(hwnd, hIMC);
6174 vim_free(buf);
6175 return convbuf;
6176}
6177#endif
6178
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006180#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182/*
6183 * set font to IM.
6184 */
6185 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006186im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
6188 HIMC hImc;
6189
6190 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6191 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006192 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 pImmReleaseContext(s_hwnd, hImc);
6194 }
6195}
6196
6197/*
6198 * Notify cursor position to IM.
6199 */
6200 void
6201im_set_position(int row, int col)
6202{
6203 HIMC hImc;
6204
6205 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6206 {
6207 COMPOSITIONFORM cfs;
6208
6209 cfs.dwStyle = CFS_POINT;
6210 cfs.ptCurrentPos.x = FILL_X(col);
6211 cfs.ptCurrentPos.y = FILL_Y(row);
6212 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00006213 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6214 {
6215 // Work around when PerMonitorV2 is not enabled in the process level.
6216 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6217 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 pImmSetCompositionWindow(hImc, &cfs);
6220
6221 pImmReleaseContext(s_hwnd, hImc);
6222 }
6223}
6224
6225/*
6226 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6227 */
6228 void
6229im_set_active(int active)
6230{
6231 HIMC hImc;
6232 static HIMC hImcOld = (HIMC)0;
6233
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006234# ifdef VIMDLL
6235 if (!gui.in_use && !gui.starting)
6236 {
6237 mbyte_im_set_active(active);
6238 return;
6239 }
6240# endif
6241
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006242 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6243 return;
6244
6245 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006247 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006249 hImcOld = pImmGetContext(s_hwnd);
6250 if (hImcOld)
6251 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006253 active = FALSE;
6254 }
6255 else if (hImcOld != (HIMC)0)
6256 {
6257 pImmAssociateContext(s_hwnd, hImcOld);
6258 hImcOld = (HIMC)0;
6259 }
6260
6261 hImc = pImmGetContext(s_hwnd);
6262 if (!hImc)
6263 return;
6264
6265 /*
6266 * for Korean ime
6267 */
6268 HKL hKL = GetKeyboardLayout(0);
6269
6270 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6271 {
6272 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6273 static BOOL bSaved = FALSE;
6274
6275 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006277 // if we have a saved conversion status, restore it
6278 if (bSaved)
6279 pImmSetConversionStatus(hImc, dwConversionSaved,
6280 dwSentenceSaved);
6281 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006285 // save conversion status and disable korean
6286 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6287 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00006288 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006289 bSaved = TRUE;
6290 pImmSetConversionStatus(hImc,
6291 dwConversionSaved & ~(IME_CMODE_NATIVE
6292 | IME_CMODE_FULLSHAPE),
6293 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 }
6296 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006297
6298 pImmSetOpenStatus(hImc, active);
6299 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302/*
6303 * Get IM status. When IM is on, return not 0. Else return 0.
6304 */
6305 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006306im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 int status = 0;
6309 HIMC hImc;
6310
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006311# ifdef VIMDLL
6312 if (!gui.in_use && !gui.starting)
6313 return mbyte_im_get_status();
6314# endif
6315
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6317 {
6318 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6319 pImmReleaseContext(s_hwnd, hImc);
6320 }
6321 return status;
6322}
6323
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006327/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006328 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006329 */
6330 static void
6331latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6332{
6333 int c;
6334
Bram Moolenaarca003e12006-03-17 23:19:38 +00006335 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006336 {
6337 c = *text++;
6338 switch (c)
6339 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006340 case 0xa4: c = 0x20ac; break; // euro
6341 case 0xa6: c = 0x0160; break; // S hat
6342 case 0xa8: c = 0x0161; break; // S -hat
6343 case 0xb4: c = 0x017d; break; // Z hat
6344 case 0xb8: c = 0x017e; break; // Z -hat
6345 case 0xbc: c = 0x0152; break; // OE
6346 case 0xbd: c = 0x0153; break; // oe
6347 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006348 }
6349 *unicodebuf++ = c;
6350 }
6351}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353#ifdef FEAT_RIGHTLEFT
6354/*
6355 * What is this for? In the case where you are using Win98 or Win2K or later,
6356 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6357 * reverses the string sent to the TextOut... family. This sucks, because we
6358 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6359 * way to tell Windblows not to do this!
6360 *
6361 * The short of it is that this 'RevOut' only gets called if you are running
6362 * one of the new, "improved" MS OSes, and only if you are running in
6363 * 'rightleft' mode. It makes display take *slightly* longer, but not
6364 * noticeably so.
6365 */
6366 static void
K.Takata135e1522022-01-29 15:27:58 +00006367RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 int col,
6369 int row,
6370 UINT foptions,
6371 CONST RECT *pcliprect,
6372 LPCTSTR text,
6373 UINT len,
6374 CONST INT *padding)
6375{
6376 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006378 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006379 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006380 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381}
6382#endif
6383
Bram Moolenaar92467d32017-12-05 13:22:16 +01006384 static void
6385draw_line(
6386 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006387 int y1,
6388 int x2,
6389 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006390 COLORREF color)
6391{
6392#if defined(FEAT_DIRECTX)
6393 if (IS_ENABLE_DIRECTX())
6394 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6395 else
6396#endif
6397 {
6398 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6399 HPEN old_pen = SelectObject(s_hdc, hpen);
6400 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006401 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006402 LineTo(s_hdc, x2, y2);
6403 DeleteObject(SelectObject(s_hdc, old_pen));
6404 }
6405}
6406
6407 static void
6408set_pixel(
6409 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006410 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006411 COLORREF color)
6412{
6413#if defined(FEAT_DIRECTX)
6414 if (IS_ENABLE_DIRECTX())
6415 DWriteContext_SetPixel(s_dwc, x, y, color);
6416 else
6417#endif
6418 SetPixel(s_hdc, x, y, color);
6419}
6420
6421 static void
6422fill_rect(
6423 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006424 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006425 COLORREF color)
6426{
6427#if defined(FEAT_DIRECTX)
6428 if (IS_ENABLE_DIRECTX())
6429 DWriteContext_FillRect(s_dwc, rcp, color);
6430 else
6431#endif
6432 {
6433 HBRUSH hbr2;
6434
6435 if (hbr == NULL)
6436 hbr2 = CreateSolidBrush(color);
6437 else
6438 hbr2 = hbr;
6439 FillRect(s_hdc, rcp, hbr2);
6440 if (hbr == NULL)
6441 DeleteBrush(hbr2);
6442 }
6443}
6444
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 void
6446gui_mch_draw_string(
6447 int row,
6448 int col,
6449 char_u *text,
6450 int len,
6451 int flags)
6452{
6453 static int *padding = NULL;
6454 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 const RECT *pcliprect = NULL;
6456 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 static WCHAR *unicodebuf = NULL;
6458 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006459 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 int y;
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 /*
6464 * Italic and bold text seems to have an extra row of pixels at the bottom
6465 * (below where the bottom of the character should be). If we draw the
6466 * characters with a solid background, the top row of pixels in the
6467 * character below will be overwritten. We can fix this by filling in the
6468 * background ourselves, to the correct character proportions, and then
6469 * writing the character in transparent mode. Still have a problem when
6470 * the character is "_", which gets written on to the character below.
6471 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6472 * pixel in their slots, which fixes the problem with the bottom row of
6473 * pixels. We still need this code because otherwise the top row of pixels
6474 * becomes a problem. - webb.
6475 */
6476 static HBRUSH hbr_cache[2] = {NULL, NULL};
6477 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6478 static int brush_lru = 0;
6479 HBRUSH hbr;
6480 RECT rc;
6481
6482 if (!(flags & DRAW_TRANSP))
6483 {
6484 /*
6485 * Clear background first.
6486 * Note: FillRect() excludes right and bottom of rectangle.
6487 */
6488 rc.left = FILL_X(col);
6489 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (has_mbyte)
6491 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006492 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006493 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 }
6495 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 rc.right = FILL_X(col + len);
6497 rc.bottom = FILL_Y(row + 1);
6498
Bram Moolenaar734a8672019-12-02 22:49:38 +01006499 // Cache the created brush, that saves a lot of time. We need two:
6500 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (gui.currBgColor == brush_color[0])
6502 {
6503 hbr = hbr_cache[0];
6504 brush_lru = 1;
6505 }
6506 else if (gui.currBgColor == brush_color[1])
6507 {
6508 hbr = hbr_cache[1];
6509 brush_lru = 0;
6510 }
6511 else
6512 {
6513 if (hbr_cache[brush_lru] != NULL)
6514 DeleteBrush(hbr_cache[brush_lru]);
6515 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6516 brush_color[brush_lru] = gui.currBgColor;
6517 hbr = hbr_cache[brush_lru];
6518 brush_lru = !brush_lru;
6519 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006520
Bram Moolenaar92467d32017-12-05 13:22:16 +01006521 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522
6523 SetBkMode(s_hdc, TRANSPARENT);
6524
6525 /*
6526 * When drawing block cursor, prevent inverted character spilling
6527 * over character cell (can happen with bold/italic)
6528 */
6529 if (flags & DRAW_CURSOR)
6530 {
6531 pcliprect = &rc;
6532 foptions = ETO_CLIPPED;
6533 }
6534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 SetTextColor(s_hdc, gui.currFgColor);
6536 SelectFont(s_hdc, gui.currFont);
6537
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006538#ifdef FEAT_DIRECTX
6539 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006540 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006541#endif
6542
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6544 {
K.Takata135e1522022-01-29 15:27:58 +00006545 int i;
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 vim_free(padding);
6548 pad_size = Columns;
6549
Bram Moolenaar734a8672019-12-02 22:49:38 +01006550 // Don't give an out-of-memory message here, it would call us
6551 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006552 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (padding != NULL)
6554 for (i = 0; i < pad_size; i++)
6555 padding[i] = gui.char_width;
6556 }
6557
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 /*
6559 * We have to provide the padding argument because italic and bold versions
6560 * of fixed-width fonts are often one pixel or so wider than their normal
6561 * versions.
6562 * No check for DRAW_BOLD, Windows will have done it already.
6563 */
6564
Bram Moolenaar734a8672019-12-02 22:49:38 +01006565 // Check if there are any UTF-8 characters. If not, use normal text
6566 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (enc_utf8)
6568 for (n = 0; n < len; ++n)
6569 if (text[n] >= 0x80)
6570 break;
6571
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006572#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006573 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6574 // required that unicode drawing routine, currently. So this forces it
6575 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006576 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006577 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006578#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006579
Bram Moolenaar734a8672019-12-02 22:49:38 +01006580 // Check if the Unicode buffer exists and is big enough. Create it
6581 // with the same length as the multi-byte string, the number of wide
6582 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006583 if ((enc_utf8
6584 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6585 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 && (unicodebuf == NULL || len > unibuflen))
6587 {
6588 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006589 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006592 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593
6594 unibuflen = len;
6595 }
6596
6597 if (enc_utf8 && n < len && unicodebuf != NULL)
6598 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006599 // Output UTF-8 characters. Composing characters should be
6600 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006601 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006602 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006603 int cells; // cell width of string up to composing char
6604 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006605 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006607 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006609 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006611 c = utf_ptr2char(text + i);
6612 if (c >= 0x10000)
6613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006614 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006615 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6616 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006617 }
6618 else
6619 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006620 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006621 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006622
6623 if (utf_iscomposing(c))
6624 cw = 0;
6625 else
6626 {
6627 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006628 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006629 cw = 1;
6630 }
6631
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (unicodepdy != NULL)
6633 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634 // Use unicodepdy to make characters fit as we expect, even
6635 // when the font uses different widths (e.g., bold character
6636 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006637 if (c >= 0x10000)
6638 {
6639 unicodepdy[wlen - 2] = cw * gui.char_width;
6640 unicodepdy[wlen - 1] = 0;
6641 }
6642 else
6643 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
6645 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006646 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006648#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006649 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006650 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006651 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006652 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006653 TEXT_X(col), TEXT_Y(row),
6654 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006655 gui.char_width, gui.currFgColor,
6656 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006657 }
6658 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006659#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006660 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6661 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006662 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006664 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006666 // If we want to display codepage data, and the current CP is not the
6667 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (unicodebuf != NULL)
6669 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006670 if (enc_latin9)
6671 latin9_to_ucs(text, len, unicodebuf);
6672 else
6673 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 MB_PRECOMPOSED,
6675 (char *)text, len,
6676 (LPWSTR)unicodebuf, unibuflen);
6677 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006678 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006679 // Use unicodepdy to make characters fit as we expect, even
6680 // when the font uses different widths (e.g., bold character
6681 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006682 if (unicodepdy != NULL)
6683 {
6684 int i;
6685 int cw;
6686
6687 for (i = 0; i < len; ++i)
6688 {
6689 cw = utf_char2cells(unicodebuf[i]);
6690 if (cw > 2)
6691 cw = 1;
6692 unicodepdy[i] = cw * gui.char_width;
6693 }
6694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006696 foptions, pcliprect, unicodebuf, len, unicodepdy);
6697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 }
6700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006703 // Windows will mess up RL text, so we have to draw it character by
6704 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006705 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6707 foptions, pcliprect, (char *)text, len, padding);
6708 else
6709#endif
6710 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6711 foptions, pcliprect, (char *)text, len, padding);
6712 }
6713
Bram Moolenaar734a8672019-12-02 22:49:38 +01006714 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (flags & DRAW_UNDERL)
6716 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006717 // When p_linespace is 0, overwrite the bottom row of pixels.
6718 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 if (p_linespace > 1)
6721 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006722 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006724
Bram Moolenaar734a8672019-12-02 22:49:38 +01006725 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006726 if (flags & DRAW_STRIKE)
6727 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006728 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006729 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006730 }
6731
Bram Moolenaar734a8672019-12-02 22:49:38 +01006732 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006733 if (flags & DRAW_UNDERC)
6734 {
6735 int x;
6736 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006737 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006738
6739 y = FILL_Y(row + 1) - 1;
6740 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6741 {
6742 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006743 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006744 }
6745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746}
6747
6748
6749/*
6750 * Output routines.
6751 */
6752
Bram Moolenaar734a8672019-12-02 22:49:38 +01006753/*
6754 * Flush any output to the screen
6755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 void
6757gui_mch_flush(void)
6758{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006759#if defined(FEAT_DIRECTX)
6760 if (IS_ENABLE_DIRECTX())
6761 DWriteContext_Flush(s_dwc);
6762#endif
6763
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 GdiFlush();
6765}
6766
6767 static void
6768clear_rect(RECT *rcp)
6769{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006770 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
6773
Bram Moolenaarc716c302006-01-21 22:12:51 +00006774 void
6775gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6776{
6777 RECT workarea_rect;
6778
6779 get_work_area(&workarea_rect);
6780
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006781 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006782 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6783 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006784
Bram Moolenaar734a8672019-12-02 22:49:38 +01006785 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6786 // the menubar for MSwin, we subtract it from the screen height, so that
6787 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006788 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006789 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6790 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6791 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6792 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006793}
6794
6795
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796#if defined(FEAT_MENU) || defined(PROTO)
6797/*
6798 * Add a sub menu to the menu bar.
6799 */
6800 void
6801gui_mch_add_menu(
6802 vimmenu_T *menu,
6803 int pos)
6804{
6805 vimmenu_T *parent = menu->parent;
6806
6807 menu->submenu_id = CreatePopupMenu();
6808 menu->id = s_menu_id++;
6809
6810 if (menu_is_menubar(menu->name))
6811 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006812 WCHAR *wn;
6813 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006815 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006816 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006817 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006819 infow.cbSize = sizeof(infow);
6820 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6821 | MIIM_SUBMENU;
6822 infow.dwItemData = (long_u)menu;
6823 infow.wID = menu->id;
6824 infow.fType = MFT_STRING;
6825 infow.dwTypeData = wn;
6826 infow.cch = (UINT)wcslen(wn);
6827 infow.hSubMenu = menu->submenu_id;
6828 InsertMenuItemW((parent == NULL)
6829 ? s_menuBar : parent->submenu_id,
6830 (UINT)pos, TRUE, &infow);
6831 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 }
6833
Bram Moolenaar734a8672019-12-02 22:49:38 +01006834 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 if (parent == NULL)
6836 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006837# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 else if (IsWindow(parent->tearoff_handle))
6839 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841}
6842
6843 void
6844gui_mch_show_popupmenu(vimmenu_T *menu)
6845{
6846 POINT mp;
6847
K.Takata45f9cfb2022-01-21 11:11:00 +00006848 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6850}
6851
6852 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006853gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855 vimmenu_T *menu = gui_find_menu(path_name);
6856
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006857 if (menu == NULL)
6858 return;
6859
6860 POINT p;
6861
6862 // Find the position of the current cursor
6863 GetDCOrgEx(s_hdc, &p);
6864 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006866 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006868 gui_mch_getmouse(&mx, &my);
6869 p.x += mx;
6870 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006872 else if (curwin != NULL)
6873 {
6874 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6875 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6876 }
6877 msg_scroll = FALSE;
6878 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879}
6880
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006881# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882/*
6883 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6884 * create it as a pseudo-"tearoff menu".
6885 */
6886 void
6887gui_make_tearoff(char_u *path_name)
6888{
6889 vimmenu_T *menu = gui_find_menu(path_name);
6890
Bram Moolenaar734a8672019-12-02 22:49:38 +01006891 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (menu != NULL)
6893 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6894}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896
6897/*
6898 * Add a menu item to a menu
6899 */
6900 void
6901gui_mch_add_menu_item(
6902 vimmenu_T *menu,
6903 int idx)
6904{
6905 vimmenu_T *parent = menu->parent;
6906
6907 menu->id = s_menu_id++;
6908 menu->submenu_id = NULL;
6909
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006910# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6912 {
6913 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6914 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6915 }
6916 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006917# endif
6918# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (menu_is_toolbar(parent->name))
6920 {
6921 TBBUTTON newtb;
6922
Bram Moolenaara80faa82020-04-12 19:37:17 +02006923 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 if (menu_is_separator(menu->name))
6925 {
6926 newtb.iBitmap = 0;
6927 newtb.fsStyle = TBSTYLE_SEP;
6928 }
6929 else
6930 {
6931 newtb.iBitmap = get_toolbar_bitmap(menu);
6932 newtb.fsStyle = TBSTYLE_BUTTON;
6933 }
6934 newtb.idCommand = menu->id;
6935 newtb.fsState = TBSTATE_ENABLED;
6936 newtb.iString = 0;
6937 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6938 (LPARAM)&newtb);
6939 menu->submenu_id = (HMENU)-1;
6940 }
6941 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006944 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006946 wn = enc_to_utf16(menu->name, NULL);
6947 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006949 InsertMenuW(parent->submenu_id, (UINT)idx,
6950 (menu_is_separator(menu->name)
6951 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6952 (UINT)menu->id, wn);
6953 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006955# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 if (IsWindow(parent->tearoff_handle))
6957 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960}
6961
6962/*
6963 * Destroy the machine specific menu widget.
6964 */
6965 void
6966gui_mch_destroy_menu(vimmenu_T *menu)
6967{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006968# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 /*
6970 * is this a toolbar button?
6971 */
6972 if (menu->submenu_id == (HMENU)-1)
6973 {
6974 int iButton;
6975
6976 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6977 (WPARAM)menu->id, 0);
6978 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6979 }
6980 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
6983 if (menu->parent != NULL
6984 && menu_is_popup(menu->parent->dname)
6985 && menu->parent->submenu_id != NULL)
6986 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6987 else
6988 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6989 if (menu->submenu_id != NULL)
6990 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006991# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 if (IsWindow(menu->tearoff_handle))
6993 DestroyWindow(menu->tearoff_handle);
6994 if (menu->parent != NULL
6995 && menu->parent->children != NULL
6996 && IsWindow(menu->parent->tearoff_handle))
6997 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006998 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 menu->modes = 0;
7000 rebuild_tearoff(menu->parent);
7001 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 }
7004}
7005
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007006# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 static void
7008rebuild_tearoff(vimmenu_T *menu)
7009{
Bram Moolenaar734a8672019-12-02 22:49:38 +01007010 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 char_u tbuf[128];
7012 RECT trect;
7013 RECT rct;
7014 RECT roct;
7015 int x, y;
7016
7017 HWND thwnd = menu->tearoff_handle;
7018
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007019 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 if (GetWindowRect(thwnd, &trect)
7021 && GetWindowRect(s_hwnd, &rct)
7022 && GetClientRect(s_hwnd, &roct))
7023 {
7024 x = trect.left - rct.left;
7025 y = (trect.top - rct.bottom + roct.bottom);
7026 }
7027 else
7028 {
7029 x = y = 0xffffL;
7030 }
7031 DestroyWindow(thwnd);
7032 if (menu->children != NULL)
7033 {
7034 gui_mch_tearoff(tbuf, menu, x, y);
7035 if (IsWindow(menu->tearoff_handle))
7036 (void) SetWindowPos(menu->tearoff_handle,
7037 NULL,
7038 (int)trect.left,
7039 (int)trect.top,
7040 0, 0,
7041 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7042 }
7043}
Bram Moolenaar734a8672019-12-02 22:49:38 +01007044# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046/*
7047 * Make a menu either grey or not grey.
7048 */
7049 void
7050gui_mch_menu_grey(
7051 vimmenu_T *menu,
7052 int grey)
7053{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007054# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 /*
7056 * is this a toolbar button?
7057 */
7058 if (menu->submenu_id == (HMENU)-1)
7059 {
7060 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00007061 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007064# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007065 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7066 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007068# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7070 {
7071 WORD menuID;
7072 HWND menuHandle;
7073
7074 /*
7075 * A tearoff button has changed state.
7076 */
7077 if (menu->children == NULL)
7078 menuID = (WORD)(menu->id);
7079 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007080 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7082 if (menuHandle)
7083 EnableWindow(menuHandle, !grey);
7084
7085 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
Bram Moolenaar734a8672019-12-02 22:49:38 +01007089#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091
Bram Moolenaar734a8672019-12-02 22:49:38 +01007092// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007095#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7098/*
7099 * stuff for dialogs
7100 */
7101
7102/*
7103 * The callback routine used by all the dialogs. Very simple. First,
7104 * acknowledges the INITDIALOG message so that Windows knows to do standard
7105 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7106 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7107 * number.
7108 */
7109 static LRESULT CALLBACK
7110dialog_callback(
7111 HWND hwnd,
7112 UINT message,
7113 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007114 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115{
7116 if (message == WM_INITDIALOG)
7117 {
7118 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 (void)SetFocus(hwnd);
7121 if (dialog_default_button > IDCANCEL)
7122 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007123 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007124 // We don't have a default, set focus on another element of the
7125 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007126 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 return FALSE;
7128 }
7129
7130 if (message == WM_COMMAND)
7131 {
7132 int button = LOWORD(wParam);
7133
Bram Moolenaar734a8672019-12-02 22:49:38 +01007134 // Don't end the dialog if something was selected that was
7135 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 if (button >= DLG_NONBUTTON_CONTROL)
7137 return TRUE;
7138
Bram Moolenaar734a8672019-12-02 22:49:38 +01007139 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007141 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007142 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007143 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007144
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007145 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7146 p = utf16_to_enc(wp, NULL);
John Marriott031f2272025-04-23 20:56:08 +02007147 if (p != NULL)
7148 {
7149 vim_strncpy(s_textfield, p, IOSIZE);
7150 vim_free(p);
7151 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007152 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 /*
7156 * Need to check for IDOK because if the user just hits Return to
7157 * accept the default value, some reason this is what we get.
7158 */
7159 if (button == IDOK)
7160 {
7161 if (dialog_default_button > IDCANCEL)
7162 EndDialog(hwnd, dialog_default_button);
7163 }
7164 else
7165 EndDialog(hwnd, button - IDCANCEL);
7166 return TRUE;
7167 }
7168
7169 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7170 {
7171 EndDialog(hwnd, 0);
7172 return TRUE;
7173 }
7174 return FALSE;
7175}
7176
7177/*
7178 * Create a dialog dynamically from the parameter strings.
7179 * type = type of dialog (question, alert, etc.)
7180 * title = dialog title. may be NULL for default title.
7181 * message = text to display. Dialog sizes to accommodate it.
7182 * buttons = '\n' separated list of button captions, default first.
7183 * dfltbutton = number of default button.
7184 *
7185 * This routine returns 1 if the first button is pressed,
7186 * 2 for the second, etc.
7187 *
7188 * 0 indicates Esc was pressed.
7189 * -1 for unexpected error
7190 *
7191 * If stubbing out this fn, return 1.
7192 */
7193
Bram Moolenaar734a8672019-12-02 22:49:38 +01007194static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 "IDR_VIM",
7197 "IDR_VIM_ERROR",
7198 "IDR_VIM_ALERT",
7199 "IDR_VIM_INFO",
7200 "IDR_VIM_QUESTION"
7201};
7202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 int
7204gui_mch_dialog(
7205 int type,
7206 char_u *title,
7207 char_u *message,
7208 char_u *buttons,
7209 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007210 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007211 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007214 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 int numButtons;
7216 int *buttonWidths, *buttonPositions;
7217 int buttonYpos;
7218 int nchar, i;
7219 DWORD lStyle;
7220 int dlgwidth = 0;
7221 int dlgheight;
7222 int editboxheight;
7223 int horizWidth = 0;
7224 int msgheight;
7225 char_u *pstart;
7226 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007227 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 char_u *tbuffer;
7229 RECT rect;
7230 HWND hwnd;
7231 HDC hdc;
7232 HFONT font, oldFont;
7233 TEXTMETRIC fontInfo;
7234 int fontHeight;
7235 int textWidth, minButtonWidth, messageWidth;
7236 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007237 int maxDialogHeight;
7238 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 int vertical;
7240 int dlgPaddingX;
7241 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007242# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007243 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007245# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007246 garray_T ga;
7247 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007248 int dlg_icon_width;
7249 int dlg_icon_height;
7250 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007252# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007253 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007254# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007255 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007256# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007257 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007258 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar748bf032005-02-02 23:04:36 +00007261 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007262 {
7263 load_dpi_func();
7264 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007265 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007266 }
7267 else
7268 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
7270 if ((type < 0) || (type > VIM_LAST_TYPE))
7271 type = 0;
7272
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 // allocate some memory for dialog template
7274 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007275 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007276 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278 if (p == NULL)
7279 return -1;
7280
7281 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007282 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7284 * const.
7285 */
7286 tbuffer = vim_strsave(buttons);
7287 if (tbuffer == NULL)
7288 return -1;
7289
Bram Moolenaar734a8672019-12-02 22:49:38 +01007290 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291
Bram Moolenaar734a8672019-12-02 22:49:38 +01007292 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 numButtons = 1;
7294 for (i = 0; tbuffer[i] != '\0'; i++)
7295 {
7296 if (tbuffer[i] == DLG_BUTTON_SEP)
7297 numButtons++;
7298 }
7299 if (dfltbutton >= numButtons)
7300 dfltbutton = -1;
7301
Bram Moolenaar734a8672019-12-02 22:49:38 +01007302 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007303 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (buttonWidths == NULL)
7305 return -1;
7306
Bram Moolenaar734a8672019-12-02 22:49:38 +01007307 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007308 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 if (buttonPositions == NULL)
7310 return -1;
7311
7312 /*
7313 * Calculate how big the dialog must be.
7314 */
7315 hwnd = GetDesktopWindow();
7316 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007317# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7319 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007320 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 use_lfSysmenu = TRUE;
7322 }
7323 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007324# endif
K.Takatad1c58992022-01-23 12:31:57 +00007325 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7326 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7327
7328 oldFont = SelectFont(hdc, font);
7329 dlgPaddingX = DLG_PADDING_X;
7330 dlgPaddingY = DLG_PADDING_Y;
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 GetTextMetrics(hdc, &fontInfo);
7333 fontHeight = fontInfo.tmHeight;
7334
Bram Moolenaar734a8672019-12-02 22:49:38 +01007335 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007336 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
Bram Moolenaar734a8672019-12-02 22:49:38 +01007338 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007339 if (s_hwnd == NULL)
7340 {
7341 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
Bram Moolenaar734a8672019-12-02 22:49:38 +01007343 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007344 get_work_area(&workarea_rect);
7345 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007346 if (maxDialogWidth > adjust_by_system_dpi(600))
7347 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007348 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007349 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007350 }
7351 else
7352 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007353 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007354 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007355 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007356 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7357 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7358 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7359 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007360
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007361 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007362 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7363 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7364 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7365 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7366 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007367 }
7368
Bram Moolenaar734a8672019-12-02 22:49:38 +01007369 // Set dlgwidth to width of message.
7370 // Copy the message into "ga", changing NL to CR-NL and inserting line
7371 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 pstart = message;
7373 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007374 msgheight = 0;
7375 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 do
7377 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007379
Bram Moolenaar734a8672019-12-02 22:49:38 +01007380 // Need to figure out where to break the string. The system does it
7381 // at a word boundary, which would mean we can't compute the number of
7382 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007383 textWidth = 0;
7384 last_white = NULL;
7385 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007386 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007387 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007388 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007389 && textWidth > maxDialogWidth * 3 / 4)
7390 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007391 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007392 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007393 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007394 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007395 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007396 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007397 textWidth = 0;
7398
7399 if (last_white != NULL)
7400 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007401 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007402 if (pend > last_white)
7403 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007404 pend = last_white + 1;
7405 last_white = NULL;
7406 }
7407 ga_append(&ga, '\r');
7408 ga_append(&ga, '\n');
7409 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007410 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007411
7412 while (--l >= 0)
7413 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007414 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007415 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007417
7418 ga_append(&ga, '\r');
7419 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 pstart = pend + 1;
7421 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007422
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007423 if (ga.ga_data != NULL)
7424 message = ga.ga_data;
7425
Bram Moolenaar734a8672019-12-02 22:49:38 +01007426 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007427
K.Takatac81e9bf2022-01-16 14:15:49 +00007428 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7429 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
K.Takatac81e9bf2022-01-16 14:15:49 +00007431 // Add width of icon to dlgwidth, and some space
7432 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7433 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7434
7435 if (msgheight < dlg_icon_height)
7436 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
7438 /*
7439 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007440 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007442 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (!vertical)
7444 {
7445 // Place buttons horizontally if they fit.
7446 horizWidth = dlgPaddingX;
7447 pstart = tbuffer;
7448 i = 0;
7449 do
7450 {
7451 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7452 if (pend == NULL)
7453 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007454 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 if (textWidth < minButtonWidth)
7456 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007457 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 buttonWidths[i] = textWidth;
7459 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007460 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 pstart = pend + 1;
7462 } while (*pend != NUL);
7463
7464 if (horizWidth > maxDialogWidth)
7465 vertical = TRUE; // Too wide to fit on the screen.
7466 else if (horizWidth > dlgwidth)
7467 dlgwidth = horizWidth;
7468 }
7469
7470 if (vertical)
7471 {
7472 // Stack buttons vertically.
7473 pstart = tbuffer;
7474 do
7475 {
7476 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7477 if (pend == NULL)
7478 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007479 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007480 textWidth += dlgPaddingX; // Padding within button
7481 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 if (textWidth > dlgwidth)
7483 dlgwidth = textWidth;
7484 pstart = pend + 1;
7485 } while (*pend != NUL);
7486 }
7487
7488 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007489 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaar734a8672019-12-02 22:49:38 +01007491 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007492 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 add_long(lStyle);
7495 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007496 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 add_word(0); // NumberOfItems(will change later)
7498 add_word(10); // x
7499 add_word(10); // y
7500 add_word(PixelToDialogX(dlgwidth)); // cx
7501
7502 // Dialog height.
7503 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007504 dlgheight = msgheight + 2 * dlgPaddingY
7505 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 else
7507 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7508
7509 // Dialog needs to be taller if contains an edit box.
7510 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7511 if (textfield != NULL)
7512 dlgheight += editboxheight;
7513
Bram Moolenaar734a8672019-12-02 22:49:38 +01007514 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007515 if (dlgheight > maxDialogHeight)
7516 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007517 msgheight = msgheight - (dlgheight - maxDialogHeight);
7518 dlgheight = maxDialogHeight;
7519 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007520 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007521 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007522 }
7523
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 add_word(PixelToDialogY(dlgheight));
7525
7526 add_word(0); // Menu
7527 add_word(0); // Class
7528
Bram Moolenaar734a8672019-12-02 22:49:38 +01007529 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007530 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7531 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 p += nchar;
7533
K.Takatad1c58992022-01-23 12:31:57 +00007534 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007535# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007536 if (use_lfSysmenu)
7537 {
7538 // point size
7539 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7540 GetDeviceCaps(hdc, LOGPIXELSY));
7541 wcscpy(p, lfSysmenu.lfFaceName);
7542 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
K.Takatad1c58992022-01-23 12:31:57 +00007544 else
7545# endif
7546 {
7547 *p++ = DLG_FONT_POINT_SIZE; // point size
7548 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7549 }
7550 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
7552 buttonYpos = msgheight + 2 * dlgPaddingY;
7553
7554 if (textfield != NULL)
7555 buttonYpos += editboxheight;
7556
7557 pstart = tbuffer;
7558 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007559 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 for (i = 0; i < numButtons; i++)
7561 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007562 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 for ( pend = pstart;
7564 *pend && (*pend != DLG_BUTTON_SEP);
7565 pend++)
7566 ;
7567
7568 if (*pend)
7569 *pend = '\0';
7570
7571 /*
7572 * old NOTE:
7573 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7574 * the focus to the first tab-able button and in so doing makes that
7575 * the default!! Grrr. Workaround: Make the default button the only
7576 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7577 * he/she can use arrow keys.
7578 *
7579 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007580 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 * dialog. Also needed for when the textfield is the default control.
7582 * It appears to work now (perhaps not on Win95?).
7583 */
7584 if (vertical)
7585 {
7586 p = add_dialog_element(p,
7587 (i == dfltbutton
7588 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7589 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007590 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 + 2 * fontHeight * i),
7592 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7593 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007594 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else
7597 {
7598 p = add_dialog_element(p,
7599 (i == dfltbutton
7600 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7601 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007602 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 PixelToDialogX(buttonWidths[i]),
7604 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007605 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007607 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 }
7609 *pnumitems += numButtons;
7610
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 p = add_dialog_element(p, SS_ICON,
7613 PixelToDialogX(dlgPaddingX),
7614 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007615 PixelToDialogX(dlg_icon_width),
7616 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7618 dlg_icons[type]);
7619
Bram Moolenaar734a8672019-12-02 22:49:38 +01007620 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007621 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007622 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007623 PixelToDialogY(dlgPaddingY),
7624 (WORD)(PixelToDialogX(messageWidth) + 1),
7625 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007626 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627
Bram Moolenaar734a8672019-12-02 22:49:38 +01007628 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (textfield != NULL)
7630 {
7631 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7632 PixelToDialogX(2 * dlgPaddingX),
7633 PixelToDialogY(2 * dlgPaddingY + msgheight),
7634 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7635 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007636 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 *pnumitems += 1;
7638 }
7639
7640 *pnumitems += 2;
7641
7642 SelectFont(hdc, oldFont);
7643 DeleteObject(font);
7644 ReleaseDC(hwnd, hdc);
7645
Bram Moolenaar734a8672019-12-02 22:49:38 +01007646 // Let the dialog_callback() function know which button to make default
7647 // If we have an edit box, make that the default. We also need to tell
7648 // dialog_callback() if this dialog contains an edit box or not. We do
7649 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (textfield != NULL)
7651 {
7652 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7653 s_textfield = textfield;
7654 }
7655 else
7656 {
7657 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7658 s_textfield = NULL;
7659 }
7660
Bram Moolenaar734a8672019-12-02 22:49:38 +01007661 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007663 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 (LPDLGTEMPLATE)pdlgtemplate,
7665 s_hwnd,
7666 (DLGPROC)dialog_callback);
7667
7668 LocalFree(LocalHandle(pdlgtemplate));
7669 vim_free(tbuffer);
7670 vim_free(buttonWidths);
7671 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007672 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673
Bram Moolenaar734a8672019-12-02 22:49:38 +01007674 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 (void)SetFocus(s_hwnd);
7676
7677 return nchar;
7678}
7679
Bram Moolenaar734a8672019-12-02 22:49:38 +01007680#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007681
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682/*
7683 * Put a simple element (basic class) onto a dialog template in memory.
7684 * return a pointer to where the next item should be added.
7685 *
7686 * parameters:
7687 * lStyle = additional style flags
7688 * (be careful, NT3.51 & Win32s will ignore the new ones)
7689 * x,y = x & y positions IN DIALOG UNITS
7690 * w,h = width and height IN DIALOG UNITS
7691 * Id = ID used in messages
7692 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7693 * caption = usually text or resource name
7694 *
7695 * TODO: use the length information noted here to enable the dialog creation
7696 * routines to work out more exactly how much memory they need to alloc.
7697 */
7698 static PWORD
7699add_dialog_element(
7700 PWORD p,
7701 DWORD lStyle,
7702 WORD x,
7703 WORD y,
7704 WORD w,
7705 WORD h,
7706 WORD Id,
7707 WORD clss,
7708 const char *caption)
7709{
7710 int nchar;
7711
Bram Moolenaar734a8672019-12-02 22:49:38 +01007712 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7714 *p++ = LOWORD(lStyle);
7715 *p++ = HIWORD(lStyle);
7716 *p++ = 0; // LOWORD (lExtendedStyle)
7717 *p++ = 0; // HIWORD (lExtendedStyle)
7718 *p++ = x;
7719 *p++ = y;
7720 *p++ = w;
7721 *p++ = h;
7722 *p++ = Id; //9 or 10 words in all
7723
7724 *p++ = (WORD)0xffff;
7725 *p++ = clss; //2 more here
7726
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007727 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 p += nchar;
7729
7730 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7731
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007732 return p; // total = 15 + strlen(caption) words
7733 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
7736
7737/*
7738 * Helper routine. Take an input pointer, return closest pointer that is
7739 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7740 */
7741 static LPWORD
7742lpwAlign(
7743 LPWORD lpIn)
7744{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007745 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007747 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 ul += 3;
7749 ul >>= 2;
7750 ul <<= 2;
7751 return (LPWORD)ul;
7752}
7753
7754/*
7755 * Helper routine. Takes second parameter as Ansi string, copies it to first
7756 * parameter as wide character (16-bits / char) string, and returns integer
7757 * number of wide characters (words) in string (including the trailing wide
7758 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007759 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7760 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 static int
7762nCopyAnsiToWideChar(
7763 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007764 LPSTR lpAnsiIn,
7765 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007768 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 int i;
7770 WCHAR *wn;
7771
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007772 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007774 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007775 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (wn != NULL)
7777 {
7778 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007779 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 vim_free(wn);
7781 }
7782 }
7783 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007784 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 nChar = MultiByteToWideChar(
7786 enc_codepage > 0 ? enc_codepage : CP_ACP,
7787 MB_PRECOMPOSED,
7788 lpAnsiIn, len,
7789 lpWCStr, len);
7790 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007791 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
7794 return nChar;
7795}
7796
7797
7798#ifdef FEAT_TEAROFF
7799/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007800 * Lookup menu handle from "menu_id".
7801 */
7802 static HMENU
7803tearoff_lookup_menuhandle(
7804 vimmenu_T *menu,
7805 WORD menu_id)
7806{
7807 for ( ; menu != NULL; menu = menu->next)
7808 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007809 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007810 continue;
7811 if (menu_is_separator(menu->dname))
7812 continue;
7813 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7814 return menu->submenu_id;
7815 }
7816 return NULL;
7817}
7818
7819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 * The callback function for all the modeless dialogs that make up the
7821 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7822 * thinking its menus have been clicked), and go away when closed.
7823 */
7824 static LRESULT CALLBACK
7825tearoff_callback(
7826 HWND hwnd,
7827 UINT message,
7828 WPARAM wParam,
7829 LPARAM lParam)
7830{
7831 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007832 {
7833 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
Bram Moolenaar734a8672019-12-02 22:49:38 +01007837 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 HandleMouseHide(message, lParam);
7839
7840 if (message == WM_COMMAND)
7841 {
7842 if ((WORD)(LOWORD(wParam)) & 0x8000)
7843 {
7844 POINT mp;
7845 RECT rect;
7846
7847 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7848 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007849 vimmenu_T *menu;
7850
7851 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007853 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7855 (int)rect.right - 8,
7856 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007857 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 s_hwnd,
7859 NULL);
7860 /*
7861 * NOTE: The pop-up menu can eat the mouse up event.
7862 * We deal with this in normal.c.
7863 */
7864 }
7865 }
7866 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007867 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7869 /*
7870 * Give main window the focus back: this is so after
7871 * choosing a tearoff button you can start typing again
7872 * straight away.
7873 */
7874 (void)SetFocus(s_hwnd);
7875 return TRUE;
7876 }
7877 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7878 {
7879 DestroyWindow(hwnd);
7880 return TRUE;
7881 }
7882
Bram Moolenaar734a8672019-12-02 22:49:38 +01007883 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (message == WM_EXITSIZEMOVE)
7885 (void)SetActiveWindow(s_hwnd);
7886
7887 return FALSE;
7888}
7889#endif
7890
7891
7892/*
K.Takatad1c58992022-01-23 12:31:57 +00007893 * Computes the dialog base units based on the current dialog font.
7894 * We don't use the GetDialogBaseUnits() API, because we don't use the
7895 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 */
7897 static void
7898get_dialog_font_metrics(void)
7899{
7900 HDC hdc;
7901 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 SIZE size;
7903#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007904 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905#endif
7906
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007908 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007909 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007910 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911#endif
7912 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007913 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914
K.Takatad1c58992022-01-23 12:31:57 +00007915 hdc = GetDC(s_hwnd);
7916 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007917 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007918 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919
K.Takataabe628e2022-01-23 16:25:17 +00007920 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007921 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922}
7923
7924#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7925/*
7926 * Create a pseudo-"tearoff menu" based on the child
7927 * items of a given menu pointer.
7928 */
7929 static void
7930gui_mch_tearoff(
7931 char_u *title,
7932 vimmenu_T *menu,
7933 int initX,
7934 int initY)
7935{
7936 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7937 int template_len;
7938 int nchar, textWidth, submenuWidth;
7939 DWORD lStyle;
7940 DWORD lExtendedStyle;
7941 WORD dlgwidth;
7942 WORD menuID;
7943 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007944 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 vimmenu_T *the_menu = menu;
7946 HWND hwnd;
7947 HDC hdc;
7948 HFONT font, oldFont;
7949 int col, spaceWidth, len;
7950 int columnWidths[2];
7951 char_u *label, *text;
7952 int acLen = 0;
7953 int nameLen;
7954 int padding0, padding1, padding2 = 0;
7955 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007956 int x;
7957 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007958# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007959 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962
7963 /*
7964 * If this menu is already torn off, move it to the mouse position.
7965 */
7966 if (IsWindow(menu->tearoff_handle))
7967 {
7968 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007969 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {
7971 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7972 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7973 }
7974 return;
7975 }
7976
7977 /*
7978 * Create a new tearoff.
7979 */
7980 if (*title == MNU_HIDDEN_CHAR)
7981 title++;
7982
Bram Moolenaar734a8672019-12-02 22:49:38 +01007983 // Allocate memory to store the dialog template. It's made bigger when
7984 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 template_len = DLG_ALLOC_SIZE;
7986 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7987 if (p == NULL)
7988 return;
7989
7990 hwnd = GetDesktopWindow();
7991 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007992# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7994 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007995 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 use_lfSysmenu = TRUE;
7997 }
7998 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007999# endif
K.Takatad1c58992022-01-23 12:31:57 +00008000 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
8001 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
8002
8003 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
Bram Moolenaar734a8672019-12-02 22:49:38 +01008005 // Calculate width of a single space. Used for padding columns to the
8006 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008007 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008
Bram Moolenaar734a8672019-12-02 22:49:38 +01008009 // Figure out max width of the text column, the accelerator column and the
8010 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 submenuWidth = 0;
8012 for (col = 0; col < 2; col++)
8013 {
8014 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02008015 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008017 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 text = (col == 0) ? pmenu->dname : pmenu->actext;
8019 if (text != NULL && *text != NUL)
8020 {
8021 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8022 if (textWidth > columnWidths[col])
8023 columnWidths[col] = textWidth;
8024 }
8025 if (pmenu->children != NULL)
8026 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8027 }
8028 }
8029 if (columnWidths[1] == 0)
8030 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008031 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 if (submenuWidth != 0)
8033 columnWidths[0] += submenuWidth;
8034 else
8035 columnWidths[0] += spaceWidth;
8036 }
8037 else
8038 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008039 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8041 columnWidths[1] += submenuWidth;
8042 }
8043
8044 /*
8045 * Now find the total width of our 'menu'.
8046 */
8047 textWidth = columnWidths[0] + columnWidths[1];
8048 if (submenuWidth != 0)
8049 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008050 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8052 textWidth += submenuWidth;
8053 }
8054 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8055 if (textWidth > dlgwidth)
8056 dlgwidth = textWidth;
8057 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8058
Bram Moolenaar734a8672019-12-02 22:49:38 +01008059 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00008060 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061
8062 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8063 *p++ = LOWORD(lStyle);
8064 *p++ = HIWORD(lStyle);
8065 *p++ = LOWORD(lExtendedStyle);
8066 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008067 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008069 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008071 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 else
8073 *p++ = PixelToDialogX(initX); // x
8074 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008075 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 else
8077 *p++ = PixelToDialogY(initY); // y
8078 *p++ = PixelToDialogX(dlgwidth); // cx
8079 ptrueheight = p;
8080 *p++ = 0; // dialog height: changed later anyway
8081 *p++ = 0; // Menu
8082 *p++ = 0; // Class
8083
Bram Moolenaar734a8672019-12-02 22:49:38 +01008084 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008086 ? (LPSTR)title
8087 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 p += nchar;
8089
K.Takatad1c58992022-01-23 12:31:57 +00008090 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008091# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00008092 if (use_lfSysmenu)
8093 {
8094 // point size
8095 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8096 GetDeviceCaps(hdc, LOGPIXELSY));
8097 wcscpy(p, lfSysmenu.lfFaceName);
8098 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 }
K.Takatad1c58992022-01-23 12:31:57 +00008100 else
8101# endif
8102 {
8103 *p++ = DLG_FONT_POINT_SIZE; // point size
8104 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8105 }
8106 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108 /*
8109 * Loop over all the items in the menu.
8110 * But skip over the tearbar.
8111 */
8112 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8113 menu = menu->children->next;
8114 else
8115 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008116 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 for ( ; menu != NULL; menu = menu->next)
8118 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008119 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 continue;
8121 if (menu_is_separator(menu->dname))
8122 {
8123 sepPadding += 3;
8124 continue;
8125 }
8126
Bram Moolenaar734a8672019-12-02 22:49:38 +01008127 // Check if there still is plenty of room in the template. Make it
8128 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8130 {
8131 WORD *newp;
8132
8133 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8134 if (newp != NULL)
8135 {
8136 template_len += 4096;
8137 mch_memmove(newp, pdlgtemplate,
8138 (char *)p - (char *)pdlgtemplate);
8139 p = newp + (p - pdlgtemplate);
8140 pnumitems = newp + (pnumitems - pdlgtemplate);
8141 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8142 LocalFree(LocalHandle(pdlgtemplate));
8143 pdlgtemplate = newp;
8144 }
8145 }
8146
Bram Moolenaar734a8672019-12-02 22:49:38 +01008147 // Figure out minimal length of this menu label. Use "name" for the
8148 // actual text, "dname" for estimating the displayed size. "name"
8149 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 len = nameLen = (int)STRLEN(menu->name);
8151 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8152 (int)STRLEN(menu->dname))) / spaceWidth;
8153 len += padding0;
8154
8155 if (menu->actext != NULL)
8156 {
8157 acLen = (int)STRLEN(menu->actext);
8158 len += acLen;
8159 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8160 }
8161 else
8162 textWidth = 0;
8163 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8164 len += padding1;
8165
8166 if (menu->children == NULL)
8167 {
8168 padding2 = submenuWidth / spaceWidth;
8169 len += padding2;
8170 menuID = (WORD)(menu->id);
8171 }
8172 else
8173 {
8174 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008175 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
8177
Bram Moolenaar734a8672019-12-02 22:49:38 +01008178 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008179 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (label == NULL)
8181 break;
8182
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008183 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008184 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008186 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 while (padding0-- > 0)
8188 *text++ = ' ';
8189 if (menu->actext != NULL)
8190 {
8191 STRNCPY(text, menu->actext, acLen);
8192 text += acLen;
8193 }
8194 while (padding1-- > 0)
8195 *text++ = ' ';
8196 if (menu->children != NULL)
8197 {
8198 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8199 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8200 }
8201 else
8202 {
8203 while (padding2-- > 0)
8204 *text++ = ' ';
8205 }
8206 *text = NUL;
8207
8208 /*
8209 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8210 * W95/NT4 it makes the tear-off look more like a menu.
8211 */
8212 p = add_dialog_element(p,
8213 BS_PUSHBUTTON|BS_LEFT,
8214 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8215 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8216 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8217 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008218 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 vim_free(label);
8220 (*pnumitems)++;
8221 }
8222
8223 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8224
8225
Bram Moolenaar734a8672019-12-02 22:49:38 +01008226 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008227 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008228 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 (LPDLGTEMPLATE)pdlgtemplate,
8230 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008231 (DLGPROC)tearoff_callback,
8232 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 LocalFree(LocalHandle(pdlgtemplate));
8235 SelectFont(hdc, oldFont);
8236 DeleteObject(font);
8237 ReleaseDC(hwnd, hdc);
8238
8239 /*
8240 * Reassert ourselves as the active window. This is so that after creating
8241 * a tearoff, the user doesn't have to click with the mouse just to start
8242 * typing again!
8243 */
8244 (void)SetActiveWindow(s_hwnd);
8245
Bram Moolenaar734a8672019-12-02 22:49:38 +01008246 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 force_menu_update = TRUE;
8248}
8249#endif
8250
8251#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008252# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254/*
8255 * Create the toolbar, initially unpopulated.
8256 * (just like the menu, there are no defaults, it's all
8257 * set up through menu.vim)
8258 */
8259 static void
8260initialise_toolbar(void)
8261{
8262 InitCommonControls();
8263 s_toolbarhwnd = CreateToolbarEx(
8264 s_hwnd,
8265 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8266 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008267 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008268 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 IDR_TOOLBAR1, // id of initial bitmap
8270 NULL,
8271 0, // initial number of buttons
8272 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8273 TOOLBAR_BUTTON_HEIGHT,
8274 TOOLBAR_BUTTON_WIDTH,
8275 TOOLBAR_BUTTON_HEIGHT,
8276 sizeof(TBBUTTON)
8277 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008278
8279 // Remove transparency from the toolbar to prevent the main window
8280 // background colour showing through
8281 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8282 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8283
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008284 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
8286 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008287
8288 update_toolbar_size();
8289}
8290
8291 static void
8292update_toolbar_size(void)
8293{
8294 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008295 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00008296
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008297 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00008298 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8299 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8300 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8301 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8302
8303 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8304 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8305 //TRACE("button size: %d, %d", w, h);
8306 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8307 gui.toolbar_height = h + 6;
8308
8309 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8310 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8311
8312 // TODO:
8313 // Currently, this function only updates the size of toolbar buttons.
8314 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315}
8316
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008317 static LRESULT CALLBACK
8318toolbar_wndproc(
8319 HWND hwnd,
8320 UINT uMsg,
8321 WPARAM wParam,
8322 LPARAM lParam)
8323{
8324 HandleMouseHide(uMsg, lParam);
8325 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8326}
8327
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 static int
8329get_toolbar_bitmap(vimmenu_T *menu)
8330{
8331 int i = -1;
8332
8333 /*
8334 * Check user bitmaps first, unless builtin is specified.
8335 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008336 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {
8338 char_u fname[MAXPATHL];
8339 HANDLE hbitmap = NULL;
8340
8341 if (menu->iconfile != NULL)
8342 {
8343 gui_find_iconfile(menu->iconfile, fname, "bmp");
8344 hbitmap = LoadImage(
8345 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008346 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 IMAGE_BITMAP,
8348 TOOLBAR_BUTTON_WIDTH,
8349 TOOLBAR_BUTTON_HEIGHT,
8350 LR_LOADFROMFILE |
8351 LR_LOADMAP3DCOLORS
8352 );
8353 }
8354
8355 /*
8356 * If the LoadImage call failed, or the "icon=" file
8357 * didn't exist or wasn't specified, try the menu name
8358 */
8359 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008360 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008361# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008362 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008363# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008364 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 hbitmap = LoadImage(
8366 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008367 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 IMAGE_BITMAP,
8369 TOOLBAR_BUTTON_WIDTH,
8370 TOOLBAR_BUTTON_HEIGHT,
8371 LR_LOADFROMFILE |
8372 LR_LOADMAP3DCOLORS
8373 );
8374
8375 if (hbitmap != NULL)
8376 {
8377 TBADDBITMAP tbAddBitmap;
8378
8379 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008380 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381
8382 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8383 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008384 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 }
8386 }
8387 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8388 i = menu->iconidx;
8389
8390 return i;
8391}
8392#endif
8393
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008394#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8395 static void
8396initialise_tabline(void)
8397{
8398 InitCommonControls();
8399
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008401 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008402 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008403 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008404 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008405
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008406 gui.tabline_height = TABLINE_HEIGHT;
8407
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008408 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008409}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008410
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008411/*
8412 * Get tabpage_T from POINT.
8413 */
8414 static tabpage_T *
8415GetTabFromPoint(
8416 HWND hWnd,
8417 POINT pt)
8418{
8419 tabpage_T *ptp = NULL;
8420
8421 if (gui_mch_showing_tabline())
8422 {
8423 TCHITTESTINFO htinfo;
8424 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008425 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008426 if (s_tabhwnd == hWnd)
8427 {
8428 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8429 if (idx != -1)
8430 ptp = find_tabpage(idx + 1);
8431 }
8432 }
8433 return ptp;
8434}
8435
8436static POINT s_pt = {0, 0};
8437static HCURSOR s_hCursor = NULL;
8438
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008439 static LRESULT CALLBACK
8440tabline_wndproc(
8441 HWND hwnd,
8442 UINT uMsg,
8443 WPARAM wParam,
8444 LPARAM lParam)
8445{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008446 POINT pt;
8447 tabpage_T *tp;
8448 RECT rect;
8449 int nCenter;
8450 int idx0;
8451 int idx1;
8452
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008453 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008454
8455 switch (uMsg)
8456 {
8457 case WM_LBUTTONDOWN:
8458 {
8459 s_pt.x = GET_X_LPARAM(lParam);
8460 s_pt.y = GET_Y_LPARAM(lParam);
8461 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008462 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008463 break;
8464 }
8465 case WM_MOUSEMOVE:
8466 if (GetCapture() == hwnd
8467 && ((wParam & MK_LBUTTON)) != 0)
8468 {
8469 pt.x = GET_X_LPARAM(lParam);
8470 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008471 if (abs(pt.x - s_pt.x) >
8472 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008473 {
8474 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8475
8476 tp = GetTabFromPoint(hwnd, pt);
8477 if (tp != NULL)
8478 {
8479 idx0 = tabpage_index(curtab) - 1;
8480 idx1 = tabpage_index(tp) - 1;
8481
8482 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8483 nCenter = rect.left + (rect.right - rect.left) / 2;
8484
Bram Moolenaar734a8672019-12-02 22:49:38 +01008485 // Check if the mouse cursor goes over the center of
8486 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008487 if ((idx0 < idx1) && (nCenter < pt.x))
8488 {
8489 tabpage_move(idx1 + 1);
8490 update_screen(0);
8491 }
8492 else if ((idx1 < idx0) && (pt.x < nCenter))
8493 {
8494 tabpage_move(idx1);
8495 update_screen(0);
8496 }
8497 }
8498 }
8499 }
8500 break;
8501 case WM_LBUTTONUP:
8502 {
8503 if (GetCapture() == hwnd)
8504 {
8505 SetCursor(s_hCursor);
8506 ReleaseCapture();
8507 }
8508 break;
8509 }
regomne3bdef102022-09-26 20:48:32 +01008510 case WM_MBUTTONUP:
8511 {
8512 TCHITTESTINFO htinfo;
8513
8514 htinfo.pt.x = GET_X_LPARAM(lParam);
8515 htinfo.pt.y = GET_Y_LPARAM(lParam);
8516 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8517 if (idx0 != -1)
8518 {
8519 idx0 += 1;
8520 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8521 }
8522 break;
8523 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008524 default:
8525 break;
8526 }
8527
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008528 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8529}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008530#endif
8531
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8533/*
8534 * Make the GUI window come to the foreground.
8535 */
8536 void
8537gui_mch_set_foreground(void)
8538{
8539 if (IsIconic(s_hwnd))
8540 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8541 SetForegroundWindow(s_hwnd);
8542}
8543#endif
8544
8545#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8546 static void
8547dyn_imm_load(void)
8548{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008549 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 if (hLibImm == NULL)
8551 return;
8552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008554 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008556 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008558 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008560 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008562 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008564 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008565 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008566 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008567 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008568 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008570 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008572 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008573 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008574 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
K.Takatab0b2b732022-01-19 12:59:21 +00008576 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 || pImmGetContext == NULL
8578 || pImmAssociateContext == NULL
8579 || pImmReleaseContext == NULL
8580 || pImmGetOpenStatus == NULL
8581 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008582 || pImmGetCompositionFontW == NULL
8583 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008585 || pImmGetConversionStatus == NULL
8586 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {
8588 FreeLibrary(hLibImm);
8589 hLibImm = NULL;
8590 pImmGetContext = NULL;
8591 return;
8592 }
8593
8594 return;
8595}
8596
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597#endif
8598
8599#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8600
8601# ifdef FEAT_XPM_W32
8602# define IMAGE_XPM 100
8603# endif
8604
8605typedef struct _signicon_t
8606{
8607 HANDLE hImage;
8608 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008609# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008610 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612} signicon_t;
8613
8614 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008615gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 signicon_t *sign;
8618 int x, y, w, h;
8619
8620 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8621 return;
8622
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008623# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008624 if (IS_ENABLE_DIRECTX())
8625 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008626# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008627
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 x = TEXT_X(col);
8629 y = TEXT_Y(row);
8630 w = gui.char_width * 2;
8631 h = gui.char_height;
8632 switch (sign->uType)
8633 {
8634 case IMAGE_BITMAP:
8635 {
8636 HDC hdcMem;
8637 HBITMAP hbmpOld;
8638
8639 hdcMem = CreateCompatibleDC(s_hdc);
8640 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8641 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8642 SelectObject(hdcMem, hbmpOld);
8643 DeleteDC(hdcMem);
8644 }
8645 break;
8646 case IMAGE_ICON:
8647 case IMAGE_CURSOR:
8648 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8649 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008650# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 case IMAGE_XPM:
8652 {
8653 HDC hdcMem;
8654 HBITMAP hbmpOld;
8655
8656 hdcMem = CreateCompatibleDC(s_hdc);
8657 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008658 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8660
8661 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008662 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8664 SelectObject(hdcMem, hbmpOld);
8665 DeleteDC(hdcMem);
8666 }
8667 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008668# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670}
8671
8672 static void
8673close_signicon_image(signicon_t *sign)
8674{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008675 if (sign == NULL)
8676 return;
8677
8678 switch (sign->uType)
8679 {
8680 case IMAGE_BITMAP:
8681 DeleteObject((HGDIOBJ)sign->hImage);
8682 break;
8683 case IMAGE_CURSOR:
8684 DestroyCursor((HCURSOR)sign->hImage);
8685 break;
8686 case IMAGE_ICON:
8687 DestroyIcon((HICON)sign->hImage);
8688 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008689# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008690 case IMAGE_XPM:
8691 DeleteObject((HBITMAP)sign->hImage);
8692 DeleteObject((HBITMAP)sign->hShape);
8693 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008694# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
8697
8698 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008699gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 signicon_t sign, *psign;
8702 char_u *ext;
8703
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008705 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 if (ext > signfile)
8707 {
8708 int do_load = 1;
8709
8710 if (!STRICMP(ext, ".bmp"))
8711 sign.uType = IMAGE_BITMAP;
8712 else if (!STRICMP(ext, ".ico"))
8713 sign.uType = IMAGE_ICON;
8714 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8715 sign.uType = IMAGE_CURSOR;
8716 else
8717 do_load = 0;
8718
8719 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008720 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 gui.char_width * 2, gui.char_height,
8722 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008723# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (!STRICMP(ext, ".xpm"))
8725 {
8726 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008727 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8728 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 }
8732
8733 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008734 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 *psign = sign;
8736
8737 if (!psign)
8738 {
8739 if (sign.hImage)
8740 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008741 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 return (void *)psign;
8744
8745}
8746
8747 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008748gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008750 if (sign == NULL)
8751 return;
8752
8753 close_signicon_image((signicon_t *)sign);
8754 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008758#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
Bram Moolenaar734a8672019-12-02 22:49:38 +01008760/*
8761 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008762 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008764 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8766 * to get current mouse position).
8767 *
8768 * Trying to use as more Windows services as possible, and as less
8769 * IE version as possible :)).
8770 *
8771 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8772 * BalloonEval struct.
8773 * 2) Enable/Disable simply create/kill BalloonEval Timer
8774 * 3) When there was enough inactivity, timer procedure posts
8775 * async request to debugger
8776 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8777 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008778 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779 */
8780
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008781 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008782make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008783{
K.Takata76687d22022-01-25 10:31:37 +00008784 TOOLINFOW *pti;
8785 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008786
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008787 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008788 if (pti == NULL)
8789 return;
8790
8791 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8792 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8793 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008794 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008795
8796 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8797 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8798
K.Takata76687d22022-01-25 10:31:37 +00008799 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008800 pti->uFlags = TTF_SUBCLASS;
8801 pti->hwnd = beval->target;
8802 pti->hinst = 0; // Don't use string resources
8803 pti->uId = ID_BEVAL_TOOLTIP;
8804
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008805 pti->lpszText = LPSTR_TEXTCALLBACKW;
8806 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8807 pti->lParam = (LPARAM)beval->tofree;
8808 // switch multiline tooltips on
8809 if (GetClientRect(s_textArea, &rect))
8810 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8811 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008812
8813 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8814 pti->rect.left = pt.x - 3;
8815 pti->rect.top = pt.y - 3;
8816 pti->rect.right = pt.x + 3;
8817 pti->rect.bottom = pt.y + 3;
8818
8819 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8820 // Make tooltip appear sooner.
8821 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8822 // I've performed some tests and it seems the longest possible life time
8823 // of tooltip is 30 seconds.
8824 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8825 /*
8826 * HACK: force tooltip to appear, because it'll not appear until
8827 * first mouse move. D*mn M$
8828 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8829 */
8830 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8831 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8832 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008833}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008836delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008838 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008842beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008843 HWND hwnd UNUSED,
8844 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008845 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008846 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 POINT pt;
8849 RECT rect;
8850
8851 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8852 return;
8853
8854 GetCursorPos(&pt);
8855 if (WindowFromPoint(pt) != s_textArea)
8856 return;
8857
8858 ScreenToClient(s_textArea, &pt);
8859 GetClientRect(s_textArea, &rect);
8860 if (!PtInRect(&rect, pt))
8861 return;
8862
K.Takataa8ec4912022-02-03 14:32:33 +00008863 if (last_user_activity > 0
8864 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 && (cur_beval->showState != ShS_PENDING
8866 || abs(cur_beval->x - pt.x) > 3
8867 || abs(cur_beval->y - pt.y) > 3))
8868 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008869 // Pointer resting in one place long enough, it's time to show
8870 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 cur_beval->showState = ShS_PENDING;
8872 cur_beval->x = pt.x;
8873 cur_beval->y = pt.y;
8874
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 if (cur_beval->msgCB != NULL)
8876 (*cur_beval->msgCB)(cur_beval, 0);
8877 }
8878}
8879
8880 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008881gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
K.Takataa8ec4912022-02-03 14:32:33 +00008883 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884}
8885
8886 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008887gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (beval == NULL)
8890 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008891 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8892 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
8895 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008896gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
8898 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008899
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008900 vim_free(beval->msg);
8901 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8902 if (beval->msg == NULL)
8903 {
8904 delete_tooltip(beval);
8905 beval->showState = ShS_NEUTRAL;
8906 return;
8907 }
8908
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (beval->showState == ShS_SHOWING)
8910 return;
8911 GetCursorPos(&pt);
8912 ScreenToClient(s_textArea, &pt);
8913
8914 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008916 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 gui_mch_disable_beval_area(cur_beval);
8918 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008919 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921}
8922
8923 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008924gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008925 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008926 char_u *mesg,
8927 void (*mesgCB)(BalloonEval *, int),
8928 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008930 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 BalloonEval *beval;
8932
8933 if (mesg != NULL && mesgCB != NULL)
8934 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008935 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 return NULL;
8937 }
8938
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008939 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 if (beval != NULL)
8941 {
8942 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 beval->msg = mesg;
8946 beval->msgCB = mesgCB;
8947 beval->clientData = clientData;
8948
8949 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 cur_beval = beval;
8951
8952 if (p_beval)
8953 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 }
8955 return beval;
8956}
8957
8958 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008959Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008961 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 return;
8963
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008964 if (cur_beval == NULL)
8965 return;
8966
8967 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008969 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008970 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008971 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 delete_tooltip(cur_beval);
8973 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008976 break;
8977 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008978 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008979 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008980 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008981 info->lpszText = (LPSTR)info->lParam;
8982 info->uFlags |= TTF_DI_SETITEM;
8983 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008984 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008985 case TTN_GETDISPINFOW:
8986 {
8987 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008988 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008989 info->lpszText = (LPWSTR)info->lParam;
8990 info->uFlags |= TTF_DI_SETITEM;
8991 }
8992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 }
8994}
8995
8996 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008997track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
8999 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
9000 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00009001 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002}
9003
9004 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01009005gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009007# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009008 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009009# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01009010 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 vim_free(beval);
9012}
Bram Moolenaar734a8672019-12-02 22:49:38 +01009013#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
9016/*
9017 * We have multiple signs to draw at the same location. Draw the
9018 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9019 */
9020 void
9021netbeans_draw_multisign_indicator(int row)
9022{
9023 int i;
9024 int y;
9025 int x;
9026
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009027 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009028 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009029
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 x = 0;
9031 y = TEXT_Y(row);
9032
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009033# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009034 if (IS_ENABLE_DIRECTX())
9035 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009036# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009037
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 for (i = 0; i < gui.char_height - 3; i++)
9039 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9040
9041 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9042 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9043 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9044 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9045 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9046 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9047 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9048}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009049#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009050
9051#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009052
Christopher Plewright20b795e2022-12-20 20:01:58 +00009053// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9054// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00009055// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00009056 static int
9057test_gui_w32_sendevent_mouse(dict_T *args)
9058{
9059 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009060 return FALSE;
9061
Christopher Plewright20b795e2022-12-20 20:01:58 +00009062 // Note: "move" is optional, requires fewer arguments
9063 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009064
Christopher Plewright20b795e2022-12-20 20:01:58 +00009065 if (!move && (!dict_has_key(args, "button")
9066 || !dict_has_key(args, "multiclick")
9067 || !dict_has_key(args, "modifiers")))
9068 return FALSE;
9069
9070 int row = (int)dict_get_number(args, "row");
9071 int col = (int)dict_get_number(args, "col");
9072
9073 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009074 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00009075 // the "move" argument expects row and col coordnates to be in pixels,
9076 // unless "cell" is specified and is TRUE.
9077 if (dict_get_bool(args, "cell", FALSE))
9078 {
9079 // calculate the middle of the character cell
9080 // Note: Cell coordinates are 1-based from vimscript
9081 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9082 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9083 gui_mouse_moved(pX, pY);
9084 }
9085 else
9086 gui_mouse_moved(col, row);
9087 }
9088 else
9089 {
9090 int button = (int)dict_get_number(args, "button");
9091 int repeated_click = (int)dict_get_number(args, "multiclick");
9092 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009093
Christopher Plewright20b795e2022-12-20 20:01:58 +00009094 // Reset the scroll values to known values.
9095 // XXX: Remove this when/if the scroll step is made configurable.
9096 mouse_set_hor_scroll_step(6);
9097 mouse_set_vert_scroll_step(3);
9098
9099 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9100 repeated_click, mods);
9101 }
9102 return TRUE;
9103}
9104
9105 static int
9106test_gui_w32_sendevent_keyboard(dict_T *args)
9107{
9108 INPUT inputs[1];
9109 INPUT modkeys[3];
9110 SecureZeroMemory(inputs, sizeof(INPUT));
9111 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9112
9113 char_u *event = dict_get_string(args, "event", TRUE);
9114
9115 if (event && (STRICMP(event, "keydown") == 0
9116 || STRICMP(event, "keyup") == 0))
9117 {
9118 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009119 if (vkCode <= 0 || vkCode >= 0xFF)
9120 {
9121 semsg(_(e_invalid_argument_nr), (long)vkCode);
9122 return FALSE;
9123 }
9124
Christopher Plewright20b795e2022-12-20 20:01:58 +00009125 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9126 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9127 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9128 || vkCode == VK_LMENU || vkCode == VK_RMENU );
9129
9130 BOOL unwrapMods = FALSE;
9131 int mods = (int)dict_get_number(args, "modifiers");
9132
9133 // If there are modifiers in the args, and it is not a keyup event and
9134 // vkCode is not a modifier key, then we generate virtual modifier key
9135 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009136 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009137 {
9138 int n = 0;
9139 if (mods & MOD_MASK_SHIFT)
9140 {
9141 modkeys[n].type = INPUT_KEYBOARD;
9142 modkeys[n].ki.wVk = VK_LSHIFT;
9143 n++;
9144 }
9145 if (mods & MOD_MASK_CTRL)
9146 {
9147 modkeys[n].type = INPUT_KEYBOARD;
9148 modkeys[n].ki.wVk = VK_LCONTROL;
9149 n++;
9150 }
9151 if (mods & MOD_MASK_ALT)
9152 {
9153 modkeys[n].type = INPUT_KEYBOARD;
9154 modkeys[n].ki.wVk = VK_LMENU;
9155 n++;
9156 }
9157 if (n)
9158 {
9159 (void)SetForegroundWindow(s_hwnd);
9160 SendInput(n, modkeys, sizeof(INPUT));
9161 }
9162 }
9163
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009164 inputs[0].type = INPUT_KEYBOARD;
9165 inputs[0].ki.wVk = vkCode;
9166 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009167 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009168 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009169 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009170 unwrapMods = TRUE;
9171 }
9172
K.Takatafef38d82022-09-07 19:03:42 +01009173 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009174 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00009175 vim_free(event);
9176
9177 if (unwrapMods)
9178 {
9179 modkeys[0].type = INPUT_KEYBOARD;
9180 modkeys[0].ki.wVk = VK_LSHIFT;
9181 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9182
9183 modkeys[1].type = INPUT_KEYBOARD;
9184 modkeys[1].ki.wVk = VK_LCONTROL;
9185 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9186
9187 modkeys[2].type = INPUT_KEYBOARD;
9188 modkeys[2].ki.wVk = VK_LMENU;
9189 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9190
9191 (void)SetForegroundWindow(s_hwnd);
9192 SendInput(3, modkeys, sizeof(INPUT));
9193 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009194 }
9195 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00009196 {
9197 if (event == NULL)
9198 {
9199 semsg(_(e_missing_argument_str), "event");
9200 }
9201 else
9202 {
9203 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9204 vim_free(event);
9205 }
9206 return FALSE;
9207 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009208 return TRUE;
9209}
Christopher Plewright20b795e2022-12-20 20:01:58 +00009210
Anton Sharonov68d94722024-01-23 23:19:02 +01009211 static int
9212test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9213{
9214 int handled = 0;
9215 char_u *strategy = dict_get_string(args, "strategy", TRUE);
9216
9217 if (strategy)
9218 {
9219 if (STRICMP(strategy, "classic") == 0)
9220 {
9221 handled = 1;
9222 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9223 }
9224 else if (STRICMP(strategy, "experimental") == 0)
9225 {
9226 handled = 1;
9227 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9228 }
9229 }
9230
9231 if (!handled)
9232 {
9233 if (strategy == NULL)
9234 {
9235 semsg(_(e_missing_argument_str), "strategy");
9236 }
9237 else
9238 {
9239 semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9240 vim_free(strategy);
9241 }
9242 return FALSE;
9243 }
9244 return TRUE;
9245}
9246
9247
Christopher Plewright20b795e2022-12-20 20:01:58 +00009248 int
9249test_gui_w32_sendevent(char_u *event, dict_T *args)
9250{
9251 if (STRICMP(event, "key") == 0)
9252 return test_gui_w32_sendevent_keyboard(args);
9253 else if (STRICMP(event, "mouse") == 0)
9254 return test_gui_w32_sendevent_mouse(args);
Anton Sharonov68d94722024-01-23 23:19:02 +01009255 else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9256 return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
Christopher Plewright20b795e2022-12-20 20:01:58 +00009257 else
9258 {
9259 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9260 return FALSE;
9261 }
9262}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009263#endif