blob: 0debae9bfa4a5930e9abecb924cb4ac1033b0686 [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 */
2160static void process_message_usual_key_experimental(UINT vk, const MSG *pmsg)
2161{
2162 WCHAR ch[8];
2163 int len;
2164 int i;
2165 UINT scan_code;
2166 BYTE keyboard_state[256];
2167
2168 // Construct the state table with only a few modifiers, we don't
2169 // really care about the presence of Ctrl/Alt as those modifiers are
2170 // handled by Vim separately.
2171 memset(keyboard_state, 0, 256);
2172 if (GetKeyState(VK_SHIFT) & 0x8000)
2173 keyboard_state[VK_SHIFT] = 0x80;
2174 if (GetKeyState(VK_CAPITAL) & 0x0001)
2175 keyboard_state[VK_CAPITAL] = 0x01;
2176 // Alt-Gr is synthesized as Alt + Ctrl.
2177 if ((GetKeyState(VK_RMENU) & 0x8000)
2178 && (GetKeyState(VK_CONTROL) & 0x8000))
2179 {
2180 keyboard_state[VK_MENU] = 0x80;
2181 keyboard_state[VK_CONTROL] = 0x80;
2182 }
2183
2184 // Translate the virtual key according to the current keyboard
2185 // layout.
2186 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2187 // Convert the scan-code into a sequence of zero or more unicode
2188 // codepoints.
2189 // If this is a dead key ToUnicode returns a negative value.
2190 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2191 0);
2192 if (len < 0)
2193 dead_key = DEAD_KEY_SET_DEFAULT;
2194
2195 if (len <= 0)
2196 {
2197 int wm_char = NUL;
2198
2199 if (dead_key == DEAD_KEY_SET_DEFAULT
2200 && (GetKeyState(VK_CONTROL) & 0x8000))
2201 {
2202 if ( // AZERTY CTRL+dead_circumflex
2203 (vk == 221 && scan_code == 26)
2204 // QWERTZ CTRL+dead_circumflex
2205 || (vk == 220 && scan_code == 41))
2206 wm_char = '[';
2207 if ( // QWERTZ CTRL+dead_two-overdots
2208 (vk == 192 && scan_code == 27))
2209 wm_char = ']';
2210 }
2211 if (wm_char != NUL)
2212 {
2213 // post WM_CHAR='[' - which will be interpreted with CTRL
2214 // still hold as ESC
2215 PostMessageW(pmsg->hwnd, WM_CHAR, wm_char, pmsg->lParam);
2216 // ask _OnChar() to not touch this state, wait for next key
2217 // press and maintain knowledge that we are "poisoned" with
2218 // "dead state"
2219 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2220 }
2221 return;
2222 }
2223
2224 // Post the message as TranslateMessage would do.
2225 if (pmsg->message == WM_KEYDOWN)
2226 {
2227 for (i = 0; i < len; i++)
2228 PostMessageW(pmsg->hwnd, WM_CHAR, ch[i], pmsg->lParam);
2229 }
2230 else
2231 {
2232 for (i = 0; i < len; i++)
2233 PostMessageW(pmsg->hwnd, WM_SYSCHAR, ch[i], pmsg->lParam);
2234 }
2235}
2236
2237/*
2238 * "Classic" implementation, existing prior to v8.2.4807
2239 */
2240static void process_message_usual_key_classic(UINT vk, const MSG *pmsg)
2241{
2242 char_u string[40];
2243
2244 // Some keys need C-S- where they should only need C-.
2245 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2246 // system startup (Helmut Stiegler, 2003 Oct 3).
2247 if (vk != 0xff
2248 && (GetKeyState(VK_CONTROL) & 0x8000)
2249 && !(GetKeyState(VK_SHIFT) & 0x8000)
2250 && !(GetKeyState(VK_MENU) & 0x8000))
2251 {
2252 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
2253 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2254 {
2255 string[0] = Ctrl_HAT;
2256 add_to_input_buf(string, 1);
2257 }
2258 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2259 else if (vk == 0xBD) // QWERTY for CTRL-'-'
2260 {
2261 string[0] = Ctrl__;
2262 add_to_input_buf(string, 1);
2263 }
2264 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
2265 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2266 {
2267 string[0] = Ctrl_AT;
2268 add_to_input_buf(string, 1);
2269 }
2270 else
2271 TranslateMessage(pmsg);
2272 }
2273 else
2274 TranslateMessage(pmsg);
2275}
2276
2277/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002278 * Process a single Windows message.
2279 * If one is not available we hang until one is.
2280 */
2281 static void
2282process_message(void)
2283{
2284 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002285 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002286 char_u string[40];
2287 int i;
2288 int modifiers = 0;
2289 int key;
2290#ifdef FEAT_MENU
2291 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
2292#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002293 static int keycode_trans_strategy_initialized = 0;
2294
2295 // lazy initialize - first time only
2296 if (!keycode_trans_strategy_initialized)
2297 {
2298 keycode_trans_strategy_initialized = 1;
2299 keycode_trans_strategy_init();
2300 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002301
K.Takatab7057bd2022-01-21 11:37:07 +00002302 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303
2304#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002305 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002306 if (msg.message == WM_OLE)
2307 {
2308 char_u *str = (char_u *)msg.lParam;
2309 if (str == NULL || *str == NUL)
2310 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002311 // Message can't be ours, forward it. Fixes problem with Ultramon
2312 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00002313 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002314 }
2315 else
2316 {
2317 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01002318 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319 }
2320 return;
2321 }
2322#endif
2323
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002324#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002325 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00002326 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002327 {
2328 HandleMouseHide(msg.message, msg.lParam);
2329 return;
2330 }
2331#endif
2332
2333 /*
2334 * Check if it's a special key that we recognise. If not, call
2335 * TranslateMessage().
2336 */
2337 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
2338 {
2339 vk = (int) msg.wParam;
2340
2341 /*
2342 * Handle dead keys in special conditions in other cases we let Windows
2343 * handle them and do not interfere.
2344 *
2345 * The dead_key flag must be reset on several occasions:
2346 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
2347 * consumed at that point (This is when we let Windows combine the
2348 * dead character on its own)
2349 *
2350 * - Before doing something special such as regenerating keypresses to
2351 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00002352 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002353 * immediately to _OnChar() (or _OnSysChar()).
2354 */
Anton Sharonov3f026672022-07-26 21:26:18 +01002355
2356 /*
2357 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
2358 * was handled by _WndProc, this keypress we want to process normally
2359 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002360 if (keycode_trans_strategy_used->is_experimental()
2361 && dead_key == DEAD_KEY_SKIP_ON_CHAR)
2362 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002363 dead_key = DEAD_KEY_OFF;
Anton Sharonov68d94722024-01-23 23:19:02 +01002364 }
Anton Sharonov3f026672022-07-26 21:26:18 +01002365
2366 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002367 {
2368 /*
dundargocc57b5bc2022-11-02 13:30:51 +00002369 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01002370 *
2371 * After dead key was pressed with Ctrl in some cases, ESC was
2372 * artificially injected and handled by _OnChar(), now we are
2373 * dealing with completely new key press from the user. If we don't
2374 * do anything, ToUnicode() call will interpret this vk+scan_code
2375 * under influence of "dead-modifier". To prevent this we translate
2376 * this message replacing current char from user with VK_SPACE,
2377 * which will cause WM_CHAR with dead_key's character itself. Using
2378 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
2379 * ignore this one WM_CHAR event completely. Afterwards (due to
2380 * usage of PostMessage), this procedure is scheduled to be called
2381 * again with user char and on next entry we will clean
2382 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2383 * outputDeadKey_rePost() since we do not wish to reset dead_key
2384 * value.
2385 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002386 if (keycode_trans_strategy_used->is_experimental() &&
2387 dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
Anton Sharonov3f026672022-07-26 21:26:18 +01002388 {
2389 outputDeadKey_rePost_Ex(msg,
2390 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2391 return;
2392 }
2393
2394 if (dead_key != DEAD_KEY_SET_DEFAULT)
2395 {
2396 // should never happen - is there a way to make ASSERT here?
2397 return;
2398 }
2399
2400 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002401 * If a dead key was pressed and the user presses VK_SPACE,
2402 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2403 * with the dead char now, so do nothing special and let Windows
2404 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002405 *
2406 * Note that VK_SPACE combines with the dead_key's character and
2407 * only one WM_CHAR will be generated by TranslateMessage(), in
2408 * the two other cases two WM_CHAR will be generated: the dead
2409 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2410 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002411 */
2412 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2413 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002414 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002415 TranslateMessage(&msg);
2416 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002417 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002418 // In modes where we are not typing, dead keys should behave
2419 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002420 else if ((get_real_state()
2421 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002422 {
2423 outputDeadKey_rePost(msg);
2424 return;
2425 }
2426 }
2427
Bram Moolenaar734a8672019-12-02 22:49:38 +01002428 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002429 if (vk == VK_CANCEL)
2430 {
2431 trash_input_buf();
2432 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002433 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002434 string[0] = Ctrl_C;
2435 add_to_input_buf(string, 1);
2436 }
2437
LemonBoy77fc0b02022-04-22 22:45:52 +01002438 // This is an IME event or a synthetic keystroke, let Windows handle it.
2439 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2440 {
2441 TranslateMessage(&msg);
2442 return;
2443 }
2444
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002445 for (i = 0; special_keys[i].key_sym != 0; i++)
2446 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002447 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002448 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002449 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002450 {
2451 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002452 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 * is a key that would normally trigger the dead key nominal
2454 * character output (such as a NUMPAD printable character or
2455 * the TAB key, etc...).
2456 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002457 if (dead_key == DEAD_KEY_SET_DEFAULT
2458 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002459 || vk == VK_TAB || vk == CAR))
2460 {
2461 outputDeadKey_rePost(msg);
2462 return;
2463 }
2464
2465#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002466 // Check for <F10>: Windows selects the menu. When <F10> is
2467 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002468 if (vk == VK_F10
2469 && gui.menu_is_active
2470 && check_map(k10, State, FALSE, TRUE, FALSE,
2471 NULL, NULL) == NULL)
2472 break;
2473#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002474 modifiers = get_active_modifiers_via_ptr();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002475
2476 if (special_keys[i].vim_code1 == NUL)
2477 key = special_keys[i].vim_code0;
2478 else
2479 key = TO_SPECIAL(special_keys[i].vim_code0,
2480 special_keys[i].vim_code1);
2481 key = simplify_key(key, &modifiers);
2482 if (key == CSI)
2483 key = K_CSI;
2484
2485 if (modifiers)
2486 {
2487 string[0] = CSI;
2488 string[1] = KS_MODIFIER;
2489 string[2] = modifiers;
2490 add_to_input_buf(string, 3);
2491 }
2492
2493 if (IS_SPECIAL(key))
2494 {
2495 string[0] = CSI;
2496 string[1] = K_SECOND(key);
2497 string[2] = K_THIRD(key);
2498 add_to_input_buf(string, 3);
2499 }
2500 else
2501 {
2502 int len;
2503
Bram Moolenaar734a8672019-12-02 22:49:38 +01002504 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505 len = char_to_string(key, string, 40, FALSE);
2506 add_to_input_buf(string, len);
2507 }
2508 break;
2509 }
2510 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002511
2512 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002513 if (special_keys[i].key_sym == 0)
2514 {
Anton Sharonov68d94722024-01-23 23:19:02 +01002515 process_message_usual_key(vk, &msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516 }
2517 }
2518#ifdef FEAT_MBYTE_IME
2519 else if (msg.message == WM_IME_NOTIFY)
2520 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2521 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002522 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002523 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002524#endif
2525
2526#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002527 // Check for <F10>: Default effect is to select the menu. When <F10> is
2528 // mapped we need to stop it here to avoid strange effects (e.g., for the
2529 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002530 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2531 NULL, NULL) == NULL)
2532#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002533 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534}
2535
2536/*
2537 * Catch up with any queued events. This may put keyboard input into the
2538 * input buffer, call resize call-backs, trigger timers etc. If there is
2539 * nothing in the event queue (& no timers pending), then we return
2540 * immediately.
2541 */
2542 void
2543gui_mch_update(void)
2544{
2545 MSG msg;
2546
2547 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002548 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002549 && !vim_is_input_buf_full())
2550 process_message();
2551}
2552
Bram Moolenaar4231da42016-06-02 14:30:04 +02002553 static void
2554remove_any_timer(void)
2555{
2556 MSG msg;
2557
2558 if (s_wait_timer != 0 && !s_timed_out)
2559 {
2560 KillTimer(NULL, s_wait_timer);
2561
Bram Moolenaar734a8672019-12-02 22:49:38 +01002562 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002563 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002564 ;
2565 s_wait_timer = 0;
2566 }
2567}
2568
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002569/*
2570 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2571 * from the keyboard.
2572 * wtime == -1 Wait forever.
2573 * wtime == 0 This should never happen.
2574 * wtime > 0 Wait wtime milliseconds for a character.
2575 * Returns OK if a character was found to be available within the given time,
2576 * or FAIL otherwise.
2577 */
2578 int
2579gui_mch_wait_for_chars(int wtime)
2580{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002581 int focus;
2582
2583 s_timed_out = FALSE;
2584
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002585 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002586 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002587 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002588 if (s_busy_processing)
2589 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002590
2591 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002592 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2593 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002594 }
2595
2596 allow_scrollbar = TRUE;
2597
2598 focus = gui.in_focus;
2599 while (!s_timed_out)
2600 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002601 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602 if (gui.in_focus != focus)
2603 {
2604 if (gui.in_focus)
2605 gui_mch_start_blink();
2606 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002607 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002608 focus = gui.in_focus;
2609 }
2610
2611 if (s_need_activate)
2612 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002613 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002614 s_need_activate = FALSE;
2615 }
2616
Bram Moolenaar4231da42016-06-02 14:30:04 +02002617#ifdef FEAT_TIMERS
2618 did_add_timer = FALSE;
2619#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002621 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002622 for (;;)
2623 {
2624 MSG msg;
2625
2626 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002627# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002628 if (did_add_timer)
2629 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002630# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002631 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002632 {
2633 process_message();
2634 break;
2635 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002636 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002637 // TODO: The 10 msec is a compromise between laggy response
2638 // and consuming more CPU time. Better would be to handle
2639 // channel messages when they arrive.
2640 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002641 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002642 break;
2643 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002644#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002645 // Don't use gui_mch_update() because then we will spin-lock until a
2646 // char arrives, instead we use GetMessage() to hang until an
2647 // event arrives. No need to check for input_buf_full because we are
2648 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002649 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002650#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002651
2652 if (input_available())
2653 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002654 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002655 allow_scrollbar = FALSE;
2656
Bram Moolenaar89c00032019-09-04 13:53:21 +02002657 // Clear pending mouse button, the release event may have been
2658 // taken by the dialog window. But don't do this when getting
2659 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002660 if (!s_getting_focus)
2661 s_button_pending = -1;
2662
2663 return OK;
2664 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002665
2666#ifdef FEAT_TIMERS
2667 if (did_add_timer)
2668 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002669 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002670 remove_any_timer();
2671 break;
2672 }
2673#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002674 }
2675 allow_scrollbar = FALSE;
2676 return FAIL;
2677}
2678
2679/*
2680 * Clear a rectangular region of the screen from text pos (row1, col1) to
2681 * (row2, col2) inclusive.
2682 */
2683 void
2684gui_mch_clear_block(
2685 int row1,
2686 int col1,
2687 int row2,
2688 int col2)
2689{
2690 RECT rc;
2691
2692 /*
2693 * Clear one extra pixel at the far right, for when bold characters have
2694 * spilled over to the window border.
2695 * Note: FillRect() excludes right and bottom of rectangle.
2696 */
2697 rc.left = FILL_X(col1);
2698 rc.top = FILL_Y(row1);
2699 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2700 rc.bottom = FILL_Y(row2 + 1);
2701 clear_rect(&rc);
2702}
2703
2704/*
2705 * Clear the whole text window.
2706 */
2707 void
2708gui_mch_clear_all(void)
2709{
2710 RECT rc;
2711
2712 rc.left = 0;
2713 rc.top = 0;
2714 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2715 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2716 clear_rect(&rc);
2717}
2718/*
2719 * Menu stuff.
2720 */
2721
2722 void
2723gui_mch_enable_menu(int flag)
2724{
2725#ifdef FEAT_MENU
2726 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2727#endif
2728}
2729
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730 void
2731gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002732 int x UNUSED,
2733 int y UNUSED,
2734 int w UNUSED,
2735 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002736{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002737 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738}
2739
2740#if defined(FEAT_MENU) || defined(PROTO)
2741/*
2742 * Make menu item hidden or not hidden
2743 */
2744 void
2745gui_mch_menu_hidden(
2746 vimmenu_T *menu,
2747 int hidden)
2748{
2749 /*
2750 * This doesn't do what we want. Hmm, just grey the menu items for now.
2751 */
2752 /*
2753 if (hidden)
2754 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2755 else
2756 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2757 */
2758 gui_mch_menu_grey(menu, hidden);
2759}
2760
2761/*
2762 * This is called after setting all the menus to grey/hidden or not.
2763 */
2764 void
2765gui_mch_draw_menubar(void)
2766{
2767 DrawMenuBar(s_hwnd);
2768}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002769#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002770
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002771/*
2772 * Return the RGB value of a pixel as a long.
2773 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002774 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002775gui_mch_get_rgb(guicolor_T pixel)
2776{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002777 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2778 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002779}
2780
2781#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002782/*
2783 * Convert pixels in X to dialog units
2784 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002785 static WORD
2786PixelToDialogX(int numPixels)
2787{
2788 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2789}
2790
Bram Moolenaar734a8672019-12-02 22:49:38 +01002791/*
2792 * Convert pixels in Y to dialog units
2793 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002794 static WORD
2795PixelToDialogY(int numPixels)
2796{
2797 return (WORD)((numPixels * 8) / s_dlgfntheight);
2798}
2799
Bram Moolenaar734a8672019-12-02 22:49:38 +01002800/*
2801 * Return the width in pixels of the given text in the given DC.
2802 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002803 static int
2804GetTextWidth(HDC hdc, char_u *str, int len)
2805{
2806 SIZE size;
2807
2808 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2809 return size.cx;
2810}
2811
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002812/*
2813 * Return the width in pixels of the given text in the given DC, taking care
2814 * of 'encoding' to active codepage conversion.
2815 */
2816 static int
2817GetTextWidthEnc(HDC hdc, char_u *str, int len)
2818{
2819 SIZE size;
2820 WCHAR *wstr;
2821 int n;
2822 int wlen = len;
2823
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002824 wstr = enc_to_utf16(str, &wlen);
2825 if (wstr == NULL)
2826 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002828 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2829 vim_free(wstr);
2830 if (n)
2831 return size.cx;
2832 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002833}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002834
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002835static void get_work_area(RECT *spi_rect);
2836
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002837/*
2838 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002839 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2840 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002841 */
2842 static BOOL
2843CenterWindow(
2844 HWND hwndChild,
2845 HWND hwndParent)
2846{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002847 HMONITOR mon;
2848 MONITORINFO moninfo;
2849 RECT rChild, rParent, rScreen;
2850 int wChild, hChild, wParent, hParent;
2851 int xNew, yNew;
2852 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002853
2854 GetWindowRect(hwndChild, &rChild);
2855 wChild = rChild.right - rChild.left;
2856 hChild = rChild.bottom - rChild.top;
2857
Bram Moolenaar734a8672019-12-02 22:49:38 +01002858 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002860 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002861 else
2862 GetWindowRect(hwndParent, &rParent);
2863 wParent = rParent.right - rParent.left;
2864 hParent = rParent.bottom - rParent.top;
2865
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002866 moninfo.cbSize = sizeof(MONITORINFO);
2867 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2868 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002870 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002871 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002872 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002874 hdc = GetDC(hwndChild);
2875 rScreen.left = 0;
2876 rScreen.top = 0;
2877 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2878 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2879 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 }
2881
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002882 xNew = rParent.left + ((wParent - wChild) / 2);
2883 if (xNew < rScreen.left)
2884 xNew = rScreen.left;
2885 else if ((xNew + wChild) > rScreen.right)
2886 xNew = rScreen.right - wChild;
2887
2888 yNew = rParent.top + ((hParent - hChild) / 2);
2889 if (yNew < rScreen.top)
2890 yNew = rScreen.top;
2891 else if ((yNew + hChild) > rScreen.bottom)
2892 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002893
2894 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2895 SWP_NOSIZE | SWP_NOZORDER);
2896}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002897#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002898
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002899#if defined(FEAT_TOOLBAR) || defined(PROTO)
2900 void
2901gui_mch_show_toolbar(int showit)
2902{
2903 if (s_toolbarhwnd == NULL)
2904 return;
2905
2906 if (showit)
2907 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002908 // Enable unicode support
2909 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2910 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002911 ShowWindow(s_toolbarhwnd, SW_SHOW);
2912 }
2913 else
2914 ShowWindow(s_toolbarhwnd, SW_HIDE);
2915}
2916
Bram Moolenaar734a8672019-12-02 22:49:38 +01002917// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002918# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002919
2920#endif
2921
2922#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2923 static void
2924add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2925{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002926 WCHAR *wn;
2927 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002928
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002929 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002930 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002931 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002933 infow.cbSize = sizeof(infow);
2934 infow.fMask = MIIM_TYPE | MIIM_ID;
2935 infow.wID = item_id;
2936 infow.fType = MFT_STRING;
2937 infow.dwTypeData = wn;
2938 infow.cch = (UINT)wcslen(wn);
2939 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2940 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941}
2942
2943 static void
2944show_tabline_popup_menu(void)
2945{
2946 HMENU tab_pmenu;
2947 long rval;
2948 POINT pt;
2949
Bram Moolenaar734a8672019-12-02 22:49:38 +01002950 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002951 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002952 return;
2953
2954 tab_pmenu = CreatePopupMenu();
2955 if (tab_pmenu == NULL)
2956 return;
2957
2958 if (first_tabpage->tp_next != NULL)
2959 add_tabline_popup_menu_entry(tab_pmenu,
2960 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2961 add_tabline_popup_menu_entry(tab_pmenu,
2962 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2963 add_tabline_popup_menu_entry(tab_pmenu,
2964 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2965
2966 GetCursorPos(&pt);
2967 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2968 NULL);
2969
2970 DestroyMenu(tab_pmenu);
2971
Bram Moolenaar734a8672019-12-02 22:49:38 +01002972 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002973 if (rval > 0)
2974 {
2975 TCHITTESTINFO htinfo;
2976 int idx;
2977
2978 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2979 return;
2980
2981 htinfo.pt.x = pt.x;
2982 htinfo.pt.y = pt.y;
2983 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2984 if (idx == -1)
2985 idx = 0;
2986 else
2987 idx += 1;
2988
2989 send_tabline_menu_event(idx, (int)rval);
2990 }
2991}
2992
2993/*
2994 * Show or hide the tabline.
2995 */
2996 void
2997gui_mch_show_tabline(int showit)
2998{
2999 if (s_tabhwnd == NULL)
3000 return;
3001
3002 if (!showit != !showing_tabline)
3003 {
3004 if (showit)
3005 ShowWindow(s_tabhwnd, SW_SHOW);
3006 else
3007 ShowWindow(s_tabhwnd, SW_HIDE);
3008 showing_tabline = showit;
3009 }
3010}
3011
3012/*
3013 * Return TRUE when tabline is displayed.
3014 */
3015 int
3016gui_mch_showing_tabline(void)
3017{
3018 return s_tabhwnd != NULL && showing_tabline;
3019}
3020
3021/*
3022 * Update the labels of the tabline.
3023 */
3024 void
3025gui_mch_update_tabline(void)
3026{
3027 tabpage_T *tp;
3028 TCITEM tie;
3029 int nr = 0;
3030 int curtabidx = 0;
3031 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003033
3034 if (s_tabhwnd == NULL)
3035 return;
3036
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003037 // Enable unicode support
3038 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039
3040 tie.mask = TCIF_TEXT;
3041 tie.iImage = -1;
3042
Bram Moolenaar734a8672019-12-02 22:49:38 +01003043 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
3045
Bram Moolenaar734a8672019-12-02 22:49:38 +01003046 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003047 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3048 {
3049 if (tp == curtab)
3050 curtabidx = nr;
3051
3052 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
3053 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003054 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055 tie.pszText = "-Empty-";
3056 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
3057 tabadded = 1;
3058 }
3059
3060 get_tabline_label(tp, FALSE);
3061 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003062
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003063 wstr = enc_to_utf16(NameBuff, NULL);
3064 if (wstr != NULL)
3065 {
3066 TCITEMW tiw;
3067
3068 tiw.mask = TCIF_TEXT;
3069 tiw.iImage = -1;
3070 tiw.pszText = wstr;
3071 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
3072 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003073 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003074 }
3075
Bram Moolenaar734a8672019-12-02 22:49:38 +01003076 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
3078 TabCtrl_DeleteItem(s_tabhwnd, nr);
3079
3080 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3081 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3082
Bram Moolenaar734a8672019-12-02 22:49:38 +01003083 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003084 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
3085 RedrawWindow(s_tabhwnd, NULL, NULL,
3086 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
3087
3088 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3089 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3090}
3091
3092/*
3093 * Set the current tab to "nr". First tab is 1.
3094 */
3095 void
3096gui_mch_set_curtab(int nr)
3097{
3098 if (s_tabhwnd == NULL)
3099 return;
3100
3101 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
3102 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
3103}
3104
3105#endif
3106
3107/*
3108 * ":simalt" command.
3109 */
3110 void
3111ex_simalt(exarg_T *eap)
3112{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003113 char_u *keys = eap->arg;
3114 int fill_typebuf = FALSE;
3115 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003116
3117 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
3118 while (*keys)
3119 {
3120 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01003121 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003122 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
3123 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003124 fill_typebuf = TRUE;
3125 }
3126 if (fill_typebuf)
3127 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003128 // Put a NOP in the typeahead buffer so that the message will get
3129 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003130 key_name[0] = K_SPECIAL;
3131 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02003132 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003133 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003134#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003135 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003136#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003137 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003138 }
3139}
3140
3141/*
3142 * Create the find & replace dialogs.
3143 * You can't have both at once: ":find" when replace is showing, destroys
3144 * the replace dialog first, and the other way around.
3145 */
3146#ifdef MSWIN_FIND_REPLACE
3147 static void
3148initialise_findrep(char_u *initial_string)
3149{
3150 int wword = FALSE;
3151 int mcase = !p_ic;
3152 char_u *entry_text;
3153
Bram Moolenaar734a8672019-12-02 22:49:38 +01003154 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003155 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
3156
3157 s_findrep_struct.hwndOwner = s_hwnd;
3158 s_findrep_struct.Flags = FR_DOWN;
3159 if (mcase)
3160 s_findrep_struct.Flags |= FR_MATCHCASE;
3161 if (wword)
3162 s_findrep_struct.Flags |= FR_WHOLEWORD;
3163 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003164 {
3165 WCHAR *p = enc_to_utf16(entry_text, NULL);
3166 if (p != NULL)
3167 {
3168 int len = s_findrep_struct.wFindWhatLen - 1;
3169
3170 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
3171 s_findrep_struct.lpstrFindWhat[len] = NUL;
3172 vim_free(p);
3173 }
3174 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003175 vim_free(entry_text);
3176}
3177#endif
3178
3179 static void
3180set_window_title(HWND hwnd, char *title)
3181{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003182 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003183 {
3184 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003185
Bram Moolenaar734a8672019-12-02 22:49:38 +01003186 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
3188 if (wbuf != NULL)
3189 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003190 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003192 }
3193 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003194 else
3195 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196}
3197
3198 void
3199gui_mch_find_dialog(exarg_T *eap)
3200{
3201#ifdef MSWIN_FIND_REPLACE
3202 if (s_findrep_msg != 0)
3203 {
3204 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
3205 DestroyWindow(s_findrep_hwnd);
3206
3207 if (!IsWindow(s_findrep_hwnd))
3208 {
3209 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003210 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 }
3212
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003213 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003214 (void)SetFocus(s_findrep_hwnd);
3215
3216 s_findrep_is_find = TRUE;
3217 }
3218#endif
3219}
3220
3221
3222 void
3223gui_mch_replace_dialog(exarg_T *eap)
3224{
3225#ifdef MSWIN_FIND_REPLACE
3226 if (s_findrep_msg != 0)
3227 {
3228 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
3229 DestroyWindow(s_findrep_hwnd);
3230
3231 if (!IsWindow(s_findrep_hwnd))
3232 {
3233 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003234 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003235 }
3236
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003237 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 (void)SetFocus(s_findrep_hwnd);
3239
3240 s_findrep_is_find = FALSE;
3241 }
3242#endif
3243}
3244
3245
3246/*
3247 * Set visibility of the pointer.
3248 */
3249 void
3250gui_mch_mousehide(int hide)
3251{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003252 if (hide == gui.pointer_hidden)
3253 return;
3254
3255 ShowCursor(!hide);
3256 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257}
3258
3259#ifdef FEAT_MENU
3260 static void
3261gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
3262{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003263 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003264 gui_mch_mousehide(FALSE);
3265
3266 (void)TrackPopupMenu(
3267 (HMENU)menu->submenu_id,
3268 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3269 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01003270 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003271 s_hwnd,
3272 NULL);
3273 /*
3274 * NOTE: The pop-up menu can eat the mouse up event.
3275 * We deal with this in normal.c.
3276 */
3277}
3278#endif
3279
3280/*
3281 * Got a message when the system will go down.
3282 */
3283 static void
3284_OnEndSession(void)
3285{
3286 getout_preserve_modified(1);
3287}
3288
3289/*
3290 * Get this message when the user clicks on the cross in the top right corner
3291 * of a Windows95 window.
3292 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003293 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003294_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295{
3296 gui_shell_closed();
3297}
3298
3299/*
3300 * Get a message when the window is being destroyed.
3301 */
3302 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003303_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003304{
3305 if (!destroying)
3306 _OnClose(hwnd);
3307}
3308
3309 static void
3310_OnPaint(
3311 HWND hwnd)
3312{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003313 if (IsMinimized(hwnd))
3314 return;
3315
3316 PAINTSTRUCT ps;
3317
3318 out_flush(); // make sure all output has been processed
3319 (void)BeginPaint(hwnd, &ps);
3320
3321 // prevent multi-byte characters from misprinting on an invalid
3322 // rectangle
3323 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003324 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003325 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003326
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003327 GetClientRect(hwnd, &rect);
3328 ps.rcPaint.left = rect.left;
3329 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003331
3332 if (!IsRectEmpty(&ps.rcPaint))
3333 {
3334 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3335 ps.rcPaint.right - ps.rcPaint.left + 1,
3336 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3337 }
3338
3339 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340}
3341
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003342 static void
3343_OnSize(
3344 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003345 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 int cx,
3347 int cy)
3348{
K.Takatac81e9bf2022-01-16 14:15:49 +00003349 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350 {
3351 gui_resize_shell(cx, cy);
3352
Bram Moolenaar734a8672019-12-02 22:49:38 +01003353 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 }
3356}
3357
3358 static void
3359_OnSetFocus(
3360 HWND hwnd,
3361 HWND hwndOldFocus)
3362{
3363 gui_focus_change(TRUE);
3364 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003365 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003366}
3367
3368 static void
3369_OnKillFocus(
3370 HWND hwnd,
3371 HWND hwndNewFocus)
3372{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003373 if (destroying)
3374 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375 gui_focus_change(FALSE);
3376 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003377 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003378}
3379
3380/*
3381 * Get a message when the user switches back to vim
3382 */
3383 static LRESULT
3384_OnActivateApp(
3385 HWND hwnd,
3386 BOOL fActivate,
3387 DWORD dwThreadId)
3388{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003389 // we call gui_focus_change() in _OnSetFocus()
3390 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003391 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003392}
3393
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 void
3395gui_mch_destroy_scrollbar(scrollbar_T *sb)
3396{
3397 DestroyWindow(sb->id);
3398}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003399
3400/*
3401 * Get current mouse coordinates in text window.
3402 */
3403 void
3404gui_mch_getmouse(int *x, int *y)
3405{
3406 RECT rct;
3407 POINT mp;
3408
3409 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003410 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 *x = (int)(mp.x - rct.left);
3412 *y = (int)(mp.y - rct.top);
3413}
3414
3415/*
3416 * Move mouse pointer to character at (x, y).
3417 */
3418 void
3419gui_mch_setmouse(int x, int y)
3420{
3421 RECT rct;
3422
3423 (void)GetWindowRect(s_textArea, &rct);
3424 (void)SetCursorPos(x + gui.border_offset + rct.left,
3425 y + gui.border_offset + rct.top);
3426}
3427
3428 static void
3429gui_mswin_get_valid_dimensions(
3430 int w,
3431 int h,
3432 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003433 int *valid_h,
3434 int *cols,
3435 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003436{
3437 int base_width, base_height;
3438
3439 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003440 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3441 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003442 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003443 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3444 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3445 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3446 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003447 *cols = (w - base_width) / gui.char_width;
3448 *rows = (h - base_height) / gui.char_height;
3449 *valid_w = base_width + *cols * gui.char_width;
3450 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003451}
3452
3453 void
3454gui_mch_flash(int msec)
3455{
3456 RECT rc;
3457
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003458#if defined(FEAT_DIRECTX)
3459 if (IS_ENABLE_DIRECTX())
3460 DWriteContext_Flush(s_dwc);
3461#endif
3462
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463 /*
3464 * Note: InvertRect() excludes right and bottom of rectangle.
3465 */
3466 rc.left = 0;
3467 rc.top = 0;
3468 rc.right = gui.num_cols * gui.char_width;
3469 rc.bottom = gui.num_rows * gui.char_height;
3470 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003471 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472
Bram Moolenaar734a8672019-12-02 22:49:38 +01003473 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003474
3475 InvertRect(s_hdc, &rc);
3476}
3477
3478/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003479 * Check if the specified point is on-screen. (multi-monitor aware)
3480 */
3481 static BOOL
3482is_point_onscreen(int x, int y)
3483{
3484 POINT pt = {x, y};
3485
3486 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3487}
3488
3489/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003490 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003491 *
3492 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3493 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3494 * only works when the whole window is on-screen.
3495 */
3496 static BOOL
3497is_window_onscreen(HWND hwnd)
3498{
3499 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003500 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003501
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003502 GetClientRect(hwnd, &rc);
3503 p1.x = rc.left;
3504 p1.y = rc.top;
3505 p2.x = rc.right - 1;
3506 p2.y = rc.bottom - 1;
3507 ClientToScreen(hwnd, &p1);
3508 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003509
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003510 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003511 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003512 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003513 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003514 if (!is_point_onscreen(p2.x, p1.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, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003517 return FALSE;
3518 return TRUE;
3519}
3520
3521/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003522 * Return flags used for scrolling.
3523 * The SW_INVALIDATE is required when part of the window is covered or
3524 * off-screen. Refer to MS KB Q75236.
3525 */
3526 static int
3527get_scroll_flags(void)
3528{
3529 HWND hwnd;
3530 RECT rcVim, rcOther, rcDest;
3531
Bram Moolenaar185577e2020-10-29 20:08:21 +01003532 // Check if the window is (partly) off-screen.
3533 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003534 return SW_INVALIDATE;
3535
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003536 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003537 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3539 if (IsWindowVisible(hwnd))
3540 {
3541 GetWindowRect(hwnd, &rcOther);
3542 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3543 return SW_INVALIDATE;
3544 }
3545 return 0;
3546}
3547
3548/*
3549 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3550 * may not be scrolled out properly.
3551 * For gVim, when _OnScroll() is repeated, the character at the
3552 * previous cursor position may be left drawn after scroll.
3553 * The problem can be avoided by calling GetPixel() to get a pixel in
3554 * the region before ScrollWindowEx().
3555 */
3556 static void
3557intel_gpu_workaround(void)
3558{
3559 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3560}
3561
3562/*
3563 * Delete the given number of lines from the given row, scrolling up any
3564 * text further down within the scroll region.
3565 */
3566 void
3567gui_mch_delete_lines(
3568 int row,
3569 int num_lines)
3570{
3571 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003572
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573 rc.left = FILL_X(gui.scroll_region_left);
3574 rc.right = FILL_X(gui.scroll_region_right + 1);
3575 rc.top = FILL_Y(row);
3576 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3577
Bram Moolenaar92467d32017-12-05 13:22:16 +01003578#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003579 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003580 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003581 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003582 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003583 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003584#endif
3585 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003586#if defined(FEAT_DIRECTX)
3587 if (IS_ENABLE_DIRECTX())
3588 DWriteContext_Flush(s_dwc);
3589#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003590 intel_gpu_workaround();
3591 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003592 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003593 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003594 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003595
Bram Moolenaar734a8672019-12-02 22:49:38 +01003596 // This seems to be required to avoid the cursor disappearing when
3597 // scrolling such that the cursor ends up in the top-left character on
3598 // the screen... But why? (Webb)
3599 // It's probably fixed by disabling drawing the cursor while scrolling.
3600 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003601
3602 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3603 gui.scroll_region_left,
3604 gui.scroll_region_bot, gui.scroll_region_right);
3605}
3606
3607/*
3608 * Insert the given number of lines before the given row, scrolling down any
3609 * following text within the scroll region.
3610 */
3611 void
3612gui_mch_insert_lines(
3613 int row,
3614 int num_lines)
3615{
3616 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003617
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618 rc.left = FILL_X(gui.scroll_region_left);
3619 rc.right = FILL_X(gui.scroll_region_right + 1);
3620 rc.top = FILL_Y(row);
3621 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003622
3623#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003624 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003625 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003626 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003627 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003628 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003629#endif
3630 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003631#if defined(FEAT_DIRECTX)
3632 if (IS_ENABLE_DIRECTX())
3633 DWriteContext_Flush(s_dwc);
3634#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003635 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003636 // The SW_INVALIDATE is required when part of the window is covered or
3637 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003638 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003640 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003641 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003642
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003643 gui_clear_block(row, gui.scroll_region_left,
3644 row + num_lines - 1, gui.scroll_region_right);
3645}
3646
3647
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003648 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003649gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650{
3651#if defined(FEAT_DIRECTX)
3652 DWriteContext_Close(s_dwc);
3653 DWrite_Final();
3654 s_dwc = NULL;
3655#endif
3656
3657 ReleaseDC(s_textArea, s_hdc);
3658 DeleteObject(s_brush);
3659
3660#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003661 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3663#endif
3664
Bram Moolenaar734a8672019-12-02 22:49:38 +01003665 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 if (s_hwnd != NULL)
3667 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003668 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003669 DestroyWindow(s_hwnd);
3670 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003671}
3672
3673 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003674logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675{
3676 char *p;
3677 char *res;
3678 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;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003681 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003682
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003683 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3684 if (font_name == NULL)
3685 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003687 quality_name = quality_id2name((int)lf.lfQuality);
3688
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003689 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003690 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003691 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692 if (res != NULL)
3693 {
3694 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003695 // make a normal font string out of the lf thing:
3696 points = pixels_to_points(
3697 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3698 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3699 sprintf((char *)p, "%s:h%d", font_name, points);
3700 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003701 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003702 while (*p)
3703 {
3704 if (*p == ' ')
3705 *p = '_';
3706 ++p;
3707 }
3708 if (lf.lfItalic)
3709 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003710 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003711 STRCAT(p, ":b");
3712 if (lf.lfUnderline)
3713 STRCAT(p, ":u");
3714 if (lf.lfStrikeOut)
3715 STRCAT(p, ":s");
3716 if (charset_name != NULL)
3717 {
3718 STRCAT(p, ":c");
3719 STRCAT(p, charset_name);
3720 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003721 if (quality_name != NULL)
3722 {
3723 STRCAT(p, ":q");
3724 STRCAT(p, quality_name);
3725 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726 }
3727
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003728 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003729 return (char_u *)res;
3730}
3731
3732
3733#ifdef FEAT_MBYTE_IME
3734/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003735 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003736 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003737 */
3738 static void
3739update_im_font(void)
3740{
K.Takatac81e9bf2022-01-16 14:15:49 +00003741 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003742
3743 if (p_guifontwide != NULL && *p_guifontwide != NUL
3744 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003745 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003746 norm_logfont = lf_wide;
3747 else
3748 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003749
3750 lf = norm_logfont;
3751 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3752 // Work around when PerMonitorV2 is not enabled in the process level.
3753 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3754 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755}
3756#endif
3757
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758/*
3759 * Handler of gui.wide_font (p_guifontwide) changed notification.
3760 */
3761 void
3762gui_mch_wide_font_changed(void)
3763{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003764 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003765
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003766#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003768#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769
3770 gui_mch_free_font(gui.wide_ital_font);
3771 gui.wide_ital_font = NOFONT;
3772 gui_mch_free_font(gui.wide_bold_font);
3773 gui.wide_bold_font = NOFONT;
3774 gui_mch_free_font(gui.wide_boldital_font);
3775 gui.wide_boldital_font = NOFONT;
3776
3777 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003778 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 {
3780 if (!lf.lfItalic)
3781 {
3782 lf.lfItalic = TRUE;
3783 gui.wide_ital_font = get_font_handle(&lf);
3784 lf.lfItalic = FALSE;
3785 }
3786 if (lf.lfWeight < FW_BOLD)
3787 {
3788 lf.lfWeight = FW_BOLD;
3789 gui.wide_bold_font = get_font_handle(&lf);
3790 if (!lf.lfItalic)
3791 {
3792 lf.lfItalic = TRUE;
3793 gui.wide_boldital_font = get_font_handle(&lf);
3794 }
3795 }
3796 }
3797}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798
3799/*
3800 * Initialise vim to use the font with the given name.
3801 * Return FAIL if the font could not be loaded, OK otherwise.
3802 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003804gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805{
K.Takatac81e9bf2022-01-16 14:15:49 +00003806 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807 GuiFont font = NOFONT;
3808 char_u *p;
3809
Bram Moolenaar734a8672019-12-02 22:49:38 +01003810 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003811 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003812 {
3813 lfOrig = lf;
3814 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003816 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003817 if (font == NOFONT)
3818 return FAIL;
3819
3820 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003821 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003822#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003823 norm_logfont = lf;
3824 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003825 if (!s_in_dpichanged)
3826 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827#endif
3828 gui_mch_free_font(gui.norm_font);
3829 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003830 current_font_height = lfOrig.lfHeight;
Ken Takatada5da652023-10-05 20:20:58 +02003831 UpdateFontSize(font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003832
K.Takatac81e9bf2022-01-16 14:15:49 +00003833 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003834 if (p != NULL)
3835 {
3836 hl_set_font_name(p);
3837
Bram Moolenaar734a8672019-12-02 22:49:38 +01003838 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003839 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3840 {
3841 vim_free(p_guifont);
3842 p_guifont = p;
3843 }
3844 else
3845 vim_free(p);
3846 }
3847
3848 gui_mch_free_font(gui.ital_font);
3849 gui.ital_font = NOFONT;
3850 gui_mch_free_font(gui.bold_font);
3851 gui.bold_font = NOFONT;
3852 gui_mch_free_font(gui.boldital_font);
3853 gui.boldital_font = NOFONT;
3854
3855 if (!lf.lfItalic)
3856 {
3857 lf.lfItalic = TRUE;
3858 gui.ital_font = get_font_handle(&lf);
3859 lf.lfItalic = FALSE;
3860 }
3861 if (lf.lfWeight < FW_BOLD)
3862 {
3863 lf.lfWeight = FW_BOLD;
3864 gui.bold_font = get_font_handle(&lf);
3865 if (!lf.lfItalic)
3866 {
3867 lf.lfItalic = TRUE;
3868 gui.boldital_font = get_font_handle(&lf);
3869 }
3870 }
3871
3872 return OK;
3873}
3874
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875/*
3876 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003877 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 */
3879 int
3880gui_mch_maximized(void)
3881{
3882 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003883 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884
3885 wp.length = sizeof(WINDOWPLACEMENT);
3886 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003887 {
3888 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003889 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003890 && wp.flags == WPF_RESTORETOMAXIMIZED))
3891 return TRUE;
3892 if (wp.showCmd == SW_SHOWMINIMIZED)
3893 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003894
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003895 // Assume the window is snapped when the sizes from two APIs differ.
3896 GetWindowRect(s_hwnd, &rc);
3897 if ((rc.right - rc.left !=
3898 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3899 || (rc.bottom - rc.top !=
3900 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3901 return TRUE;
3902 }
3903 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904}
3905
3906/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003907 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3908 * is set. Compute the new Rows and Columns. This is like resizing the
3909 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 */
3911 void
3912gui_mch_newfont(void)
3913{
3914 RECT rect;
3915
3916 GetWindowRect(s_hwnd, &rect);
3917 if (win_socket_id == 0)
3918 {
3919 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003920 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3921 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003923 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3924 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3925 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3926 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003927 }
3928 else
3929 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003930 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003932 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 }
3934}
3935
3936/*
3937 * Set the window title
3938 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003939 void
3940gui_mch_settitle(
3941 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003942 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003943{
3944 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3945}
3946
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003947#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003948// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3949// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003950static LPCSTR mshape_idcs[] =
3951{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003952 IDC_ARROW, // arrow
3953 MAKEINTRESOURCE(0), // blank
3954 IDC_IBEAM, // beam
3955 IDC_SIZENS, // updown
3956 IDC_SIZENS, // udsizing
3957 IDC_SIZEWE, // leftright
3958 IDC_SIZEWE, // lrsizing
3959 IDC_WAIT, // busy
3960 IDC_NO, // no
3961 IDC_ARROW, // crosshair
3962 IDC_ARROW, // hand1
3963 IDC_ARROW, // hand2
3964 IDC_ARROW, // pencil
3965 IDC_ARROW, // question
3966 IDC_ARROW, // right-arrow
3967 IDC_UPARROW, // up-arrow
3968 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003969};
3970
3971 void
3972mch_set_mouse_shape(int shape)
3973{
3974 LPCSTR idc;
3975
3976 if (shape == MSHAPE_HIDE)
3977 ShowCursor(FALSE);
3978 else
3979 {
3980 if (shape >= MSHAPE_NUMBERED)
3981 idc = IDC_ARROW;
3982 else
3983 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003984 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003985 if (!p_mh)
3986 {
3987 POINT mp;
3988
Bram Moolenaar734a8672019-12-02 22:49:38 +01003989 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003990 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003991 (void)SetCursorPos(mp.x, mp.y);
3992 ShowCursor(TRUE);
3993 }
3994 }
3995}
3996#endif
3997
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003998#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003999/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004000 * Wide version of convert_filter().
4001 */
4002 static WCHAR *
4003convert_filterW(char_u *s)
4004{
4005 char_u *tmp;
4006 int len;
4007 WCHAR *res;
4008
4009 tmp = convert_filter(s);
4010 if (tmp == NULL)
4011 return NULL;
4012 len = (int)STRLEN(s) + 3;
4013 res = enc_to_utf16(tmp, &len);
4014 vim_free(tmp);
4015 return res;
4016}
4017
4018/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004019 * Pop open a file browser and return the file selected, in allocated memory,
4020 * or NULL if Cancel is hit.
4021 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
4022 * title - Title message for the file browser dialog.
4023 * dflt - Default name of file.
4024 * ext - Default extension to be added to files without extensions.
4025 * initdir - directory in which to open the browser (NULL = current dir)
4026 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004027 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01004028 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004029gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004030 int saving,
4031 char_u *title,
4032 char_u *dflt,
4033 char_u *ext,
4034 char_u *initdir,
4035 char_u *filter)
4036{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004037 // We always use the wide function. This means enc_to_utf16() must work,
4038 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004039 OPENFILENAMEW fileStruct;
4040 WCHAR fileBuf[MAXPATHL];
4041 WCHAR *wp;
4042 int i;
4043 WCHAR *titlep = NULL;
4044 WCHAR *extp = NULL;
4045 WCHAR *initdirp = NULL;
4046 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004047 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00004048 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004049
4050 if (dflt == NULL)
4051 fileBuf[0] = NUL;
4052 else
4053 {
4054 wp = enc_to_utf16(dflt, NULL);
4055 if (wp == NULL)
4056 fileBuf[0] = NUL;
4057 else
4058 {
4059 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
4060 fileBuf[i] = wp[i];
4061 fileBuf[i] = NUL;
4062 vim_free(wp);
4063 }
4064 }
4065
Bram Moolenaar734a8672019-12-02 22:49:38 +01004066 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004067 filterp = convert_filterW(filter);
4068
Bram Moolenaara80faa82020-04-12 19:37:17 +02004069 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004070# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01004071 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01004072 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004073# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004074 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004075# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004076
4077 if (title != NULL)
4078 titlep = enc_to_utf16(title, NULL);
4079 fileStruct.lpstrTitle = titlep;
4080
4081 if (ext != NULL)
4082 extp = enc_to_utf16(ext, NULL);
4083 fileStruct.lpstrDefExt = extp;
4084
4085 fileStruct.lpstrFile = fileBuf;
4086 fileStruct.nMaxFile = MAXPATHL;
4087 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004088 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
4089 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004090 if (initdir != NULL && *initdir != NUL)
4091 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004092 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004093 initdirp = enc_to_utf16(initdir, NULL);
4094 if (initdirp != NULL)
4095 {
4096 for (wp = initdirp; *wp != NUL; ++wp)
4097 if (*wp == '/')
4098 *wp = '\\';
4099 }
4100 fileStruct.lpstrInitialDir = initdirp;
4101 }
4102
4103 /*
4104 * TODO: Allow selection of multiple files. Needs another arg to this
4105 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
4106 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
4107 * files that don't exist yet, so I haven't put it in. What about
4108 * OFN_PATHMUSTEXIST?
4109 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
4110 */
4111 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004112# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004113 if (curbuf->b_p_bin)
4114 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004115# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004116 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00004117 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004118 else
K.Takata14b8d6a2022-01-20 15:05:22 +00004119 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004120
4121 vim_free(filterp);
4122 vim_free(initdirp);
4123 vim_free(titlep);
4124 vim_free(extp);
4125
K.Takata14b8d6a2022-01-20 15:05:22 +00004126 if (!ret)
4127 return NULL;
4128
4129 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004130 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004131 if (p == NULL)
4132 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004133
Bram Moolenaar734a8672019-12-02 22:49:38 +01004134 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004135 SetFocus(s_hwnd);
4136
Bram Moolenaar734a8672019-12-02 22:49:38 +01004137 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004138 q = vim_strsave(shorten_fname1(p));
4139 vim_free(p);
4140 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004141}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004142
4143
4144/*
4145 * Convert the string s to the proper format for a filter string by replacing
4146 * the \t and \n delimiters with \0.
4147 * Returns the converted string in allocated memory.
4148 *
4149 * Keep in sync with convert_filterW() above!
4150 */
4151 static char_u *
4152convert_filter(char_u *s)
4153{
4154 char_u *res;
4155 unsigned s_len = (unsigned)STRLEN(s);
4156 unsigned i;
4157
4158 res = alloc(s_len + 3);
4159 if (res != NULL)
4160 {
4161 for (i = 0; i < s_len; ++i)
4162 if (s[i] == '\t' || s[i] == '\n')
4163 res[i] = '\0';
4164 else
4165 res[i] = s[i];
4166 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004167 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004168 res[s_len + 1] = NUL;
4169 res[s_len + 2] = NUL;
4170 }
4171 return res;
4172}
4173
4174/*
4175 * Select a directory.
4176 */
4177 char_u *
4178gui_mch_browsedir(char_u *title, char_u *initdir)
4179{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004180 // We fake this: Use a filter that doesn't select anything and a default
4181 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004182 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
4183 initdir, (char_u *)_("Directory\t*.nothing\n"));
4184}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004185#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004186
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004187 static void
4188_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004189 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004190 HDROP hDrop)
4191{
Bram Moolenaar4033c552017-09-16 20:54:51 +02004192#define BUFPATHLEN _MAX_PATH
4193#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004194 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004195 char szFile[BUFPATHLEN];
4196 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
4197 UINT i;
4198 char_u **fnames;
4199 POINT pt;
4200 int_u modifiers = 0;
4201
Bram Moolenaar734a8672019-12-02 22:49:38 +01004202 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004203 DragQueryPoint(hDrop, &pt);
4204 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
4205
4206 reset_VIsual();
4207
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004208 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004209
4210 if (fnames != NULL)
4211 for (i = 0; i < cFiles; ++i)
4212 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004213 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
4214 fnames[i] = utf16_to_enc(wszFile, NULL);
4215 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004216 {
4217 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
4218 fnames[i] = vim_strsave((char_u *)szFile);
4219 }
4220 }
4221
4222 DragFinish(hDrop);
4223
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004224 if (fnames == NULL)
4225 return;
LemonBoy45684c62022-04-24 15:46:42 +01004226
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004227 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004228
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004229 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4230 modifiers |= MOUSE_SHIFT;
4231 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4232 modifiers |= MOUSE_CTRL;
4233 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4234 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004235
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004236 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
4237
4238 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004239}
4240
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004241 static int
4242_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004243 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004244 HWND hwndCtl,
4245 UINT code,
4246 int pos)
4247{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004248 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004249 scrollbar_T *sb, *sb_info;
4250 long val;
4251 int dragging = FALSE;
4252 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004253 SCROLLINFO si;
4254
4255 si.cbSize = sizeof(si);
4256 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004257
4258 sb = gui_mswin_find_scrollbar(hwndCtl);
4259 if (sb == NULL)
4260 return 0;
4261
Bram Moolenaar734a8672019-12-02 22:49:38 +01004262 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004263 {
4264 /*
4265 * Careful: need to get scrollbar info out of first (left) scrollbar
4266 * for window, but keep real scrollbar too because we must pass it to
4267 * gui_drag_scrollbar().
4268 */
4269 sb_info = &sb->wp->w_scrollbars[0];
4270 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004271 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004272 sb_info = sb;
4273 val = sb_info->value;
4274
4275 switch (code)
4276 {
4277 case SB_THUMBTRACK:
4278 val = pos;
4279 dragging = TRUE;
4280 if (sb->scroll_shift > 0)
4281 val <<= sb->scroll_shift;
4282 break;
4283 case SB_LINEDOWN:
4284 val++;
4285 break;
4286 case SB_LINEUP:
4287 val--;
4288 break;
4289 case SB_PAGEDOWN:
4290 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4291 break;
4292 case SB_PAGEUP:
4293 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4294 break;
4295 case SB_TOP:
4296 val = 0;
4297 break;
4298 case SB_BOTTOM:
4299 val = sb_info->max;
4300 break;
4301 case SB_ENDSCROLL:
4302 if (prev_code == SB_THUMBTRACK)
4303 {
4304 /*
4305 * "pos" only gives us 16-bit data. In case of large file,
4306 * use GetScrollPos() which returns 32-bit. Unfortunately it
4307 * is not valid while the scrollbar is being dragged.
4308 */
4309 val = GetScrollPos(hwndCtl, SB_CTL);
4310 if (sb->scroll_shift > 0)
4311 val <<= sb->scroll_shift;
4312 }
4313 break;
4314
4315 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004316 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004317 return 0;
4318 }
4319 prev_code = code;
4320
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004321 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4322 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004323
4324 /*
4325 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4326 */
4327 if (sb->wp != NULL)
4328 {
4329 scrollbar_T *sba = sb->wp->w_scrollbars;
4330 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4331
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004332 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004333 }
4334
Bram Moolenaar734a8672019-12-02 22:49:38 +01004335 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004336 s_busy_processing = TRUE;
4337
Bram Moolenaar734a8672019-12-02 22:49:38 +01004338 // When "allow_scrollbar" is FALSE still need to remember the new
4339 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004340 dont_scroll = !allow_scrollbar;
4341
Bram Moolenaara338adc2018-01-31 20:51:47 +01004342 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004343 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004344 mch_enable_flush();
4345 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004346
4347 s_busy_processing = FALSE;
4348 dont_scroll = dont_scroll_save;
4349
4350 return 0;
4351}
4352
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#ifdef FEAT_XPM_W32
4355# include "xpm_w32.h"
4356#endif
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
Bram Moolenaar734a8672019-12-02 22:49:38 +01004359// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#define TEAROFF_PADDING_X 2
4361#define TEAROFF_BUTTON_PAD_X 8
4362#define TEAROFF_MIN_WIDTH 200
4363#define TEAROFF_SUBMENU_LABEL ">>"
4364#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4365
4366
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004367#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368# define ID_BEVAL_TOOLTIP 200
4369# define BEVAL_TEXT_LEN MAXPATHL
4370
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004372static UINT_PTR beval_timer_id = 0;
4373static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004374#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004375
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004376
Bram Moolenaar734a8672019-12-02 22:49:38 +01004377// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379#ifdef FEAT_MENU
4380static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383/*
4384 * Use the system font for dialogs and tear-off menus. Remove this line to
4385 * use DLG_FONT_NAME.
4386 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004387#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390#define VIM_CLASSW L"Vim"
4391
Bram Moolenaar734a8672019-12-02 22:49:38 +01004392// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4393// thus there should be room for every dialog. For tearoffs it's made bigger
4394// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395#define DLG_ALLOC_SIZE 16 * 1024
4396
4397/*
4398 * stuff for dialogs, menus, tearoffs etc.
4399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400static PWORD
4401add_dialog_element(
4402 PWORD p,
4403 DWORD lStyle,
4404 WORD x,
4405 WORD y,
4406 WORD w,
4407 WORD h,
4408 WORD Id,
4409 WORD clss,
4410 const char *caption);
4411static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004412static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004413#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416static void get_dialog_font_metrics(void);
4417
4418static int dialog_default_button = -1;
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420#ifdef FEAT_TOOLBAR
4421static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004422static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004423static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004425#else
4426# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427#endif
4428
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004429#ifdef FEAT_GUI_TABLINE
4430static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004431static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004432#endif
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434#ifdef FEAT_MBYTE_IME
4435static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4436static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4437#endif
4438#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4439# ifdef NOIME
4440typedef struct tagCOMPOSITIONFORM {
4441 DWORD dwStyle;
4442 POINT ptCurrentPos;
4443 RECT rcArea;
4444} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4445typedef HANDLE HIMC;
4446# endif
4447
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004448static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004449static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4450static HIMC (WINAPI *pImmGetContext)(HWND);
4451static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4452static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4453static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4454static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004455static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4456static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004457static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4458static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004459static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460static void dyn_imm_load(void);
4461#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462# define pImmGetCompositionStringW ImmGetCompositionStringW
4463# define pImmGetContext ImmGetContext
4464# define pImmAssociateContext ImmAssociateContext
4465# define pImmReleaseContext ImmReleaseContext
4466# define pImmGetOpenStatus ImmGetOpenStatus
4467# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004468# define pImmGetCompositionFontW ImmGetCompositionFontW
4469# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470# define pImmSetCompositionWindow ImmSetCompositionWindow
4471# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004472# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475#ifdef FEAT_MENU
4476/*
4477 * Figure out how high the menu bar is at the moment.
4478 */
4479 static int
4480gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004481 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482{
4483 static int old_menu_height = -1;
4484
4485 RECT rc1, rc2;
4486 int num;
4487 int menu_height;
4488
4489 if (gui.menu_is_active)
4490 num = GetMenuItemCount(s_menuBar);
4491 else
4492 num = 0;
4493
4494 if (num == 0)
4495 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004496 else if (IsMinimized(s_hwnd))
4497 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004498 // The height of the menu cannot be determined while the window is
4499 // minimized. Take the previous height if the menu is changed in that
4500 // state, to avoid that Vim's vertical window size accidentally
4501 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004502 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else
4505 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004506 /*
4507 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4508 * seem to have been set yet, so menu wraps in default window
4509 * width which is very narrow. Instead just return height of a
4510 * single menu item. Will still be wrong when the menu really
4511 * should wrap over more than one line.
4512 */
4513 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4514 if (gui.starting)
4515 menu_height = rc1.bottom - rc1.top + 1;
4516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004518 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4519 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 }
4522
4523 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004524 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004525 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526
4527 return menu_height;
4528}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004529#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531
4532/*
4533 * Setup for the Intellimouse
4534 */
LemonBoyc27747e2022-05-07 12:25:40 +01004535 static long
4536mouse_vertical_scroll_step(void)
4537{
4538 UINT val;
4539 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4540 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4541 return 3; // Safe default;
4542}
4543
4544 static long
4545mouse_horizontal_scroll_step(void)
4546{
4547 UINT val;
4548 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4549 return (long)val;
4550 return 3; // Safe default;
4551}
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 static void
4554init_mouse_wheel(void)
4555{
LemonBoyc27747e2022-05-07 12:25:40 +01004556 // Get the default values for the horizontal and vertical scroll steps from
4557 // the system.
4558 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4559 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560}
4561
Bram Moolenaarae20f342019-11-05 21:09:23 +01004562/*
LemonBoya773d842022-05-10 20:54:46 +01004563 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004566_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567{
LemonBoy365d8f72022-05-05 19:23:07 +01004568 int button;
4569 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004570 int modifiers = 0;
4571 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004572 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4573 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574
Bram Moolenaarae20f342019-11-05 21:09:23 +01004575 wp = gui_mouse_window(FIND_POPUP);
4576
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004577#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004578 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004579 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004580 cmdarg_T cap;
4581 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004582
Bram Moolenaarae20f342019-11-05 21:09:23 +01004583 // Mouse hovers over popup window, scroll it if possible.
4584 mouse_row = wp->w_winrow;
4585 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004586 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004587 if (horizontal)
4588 {
4589 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4590 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4591 }
4592 else
4593 {
4594 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4595 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4596 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004597 clear_oparg(&oa);
4598 cap.oap = &oa;
4599 nv_mousescroll(&cap);
4600 update_screen(0);
4601 setcursor();
4602 out_flush();
4603 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004604 }
4605#endif
4606
Bram Moolenaarae20f342019-11-05 21:09:23 +01004607 if (wp == NULL || !p_scf)
4608 wp = curwin;
4609
LemonBoy365d8f72022-05-05 19:23:07 +01004610 // Translate the scroll event into an event that Vim can process so that
4611 // the user has a chance to map the scrollwheel buttons.
4612 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004613 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 else
LemonBoy365d8f72022-05-05 19:23:07 +01004615 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004616
4617 kbd_modifiers = get_active_modifiers();
4618
4619 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4620 modifiers |= MOUSE_SHIFT;
4621 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4622 modifiers |= MOUSE_CTRL;
4623 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4624 modifiers |= MOUSE_ALT;
4625
LemonBoya773d842022-05-10 20:54:46 +01004626 // The cursor position is relative to the upper-left corner of the screen.
4627 pt.x = GET_X_LPARAM(lParam);
4628 pt.y = GET_Y_LPARAM(lParam);
4629 ScreenToClient(s_textArea, &pt);
4630
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004631 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632}
4633
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004634#ifdef USE_SYSMENU_FONT
4635/*
4636 * Get Menu Font.
4637 * Return OK or FAIL.
4638 */
4639 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004640gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004641{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004642 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004643
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004644 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4645 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004646 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004647 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004648 &nm,
4649 0))
4650 return FAIL;
4651 *lf = nm.lfMenuFont;
4652 return OK;
4653}
4654#endif
4655
4656
4657#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4658/*
4659 * Set the GUI tabline font to the system menu font
4660 */
4661 static void
4662set_tabline_font(void)
4663{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004664 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004665 HFONT font;
4666 HWND hwnd;
4667 HDC hdc;
4668 HFONT hfntOld;
4669 TEXTMETRIC tm;
4670
4671 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4672 return;
4673
K.Takatac81e9bf2022-01-16 14:15:49 +00004674 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004675 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004676
4677 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4678
4679 /*
4680 * Compute the height of the font used for the tab text
4681 */
4682 hwnd = GetDesktopWindow();
4683 hdc = GetWindowDC(hwnd);
4684 hfntOld = SelectFont(hdc, font);
4685
4686 GetTextMetrics(hdc, &tm);
4687
4688 SelectFont(hdc, hfntOld);
4689 ReleaseDC(hwnd, hdc);
4690
4691 /*
4692 * The space used by the tab border and the space between the tab label
4693 * and the tab border is included as 7.
4694 */
4695 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4696}
K.Takatac81e9bf2022-01-16 14:15:49 +00004697#else
4698# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004699#endif
4700
Bram Moolenaar520470a2005-06-16 21:59:56 +00004701/*
4702 * Invoked when a setting was changed.
4703 */
4704 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004705_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004706{
LemonBoy365d8f72022-05-05 19:23:07 +01004707 switch (param)
4708 {
4709 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004710 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004711 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004712 case SPI_SETWHEELSCROLLCHARS:
4713 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004714 break;
4715 case SPI_SETNONCLIENTMETRICS:
4716 set_tabline_font();
4717 break;
4718 default:
4719 break;
4720 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004721 return 0;
4722}
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#ifdef FEAT_NETBEANS_INTG
4725 static void
4726_OnWindowPosChanged(
4727 HWND hwnd,
4728 const LPWINDOWPOS lpwpos)
4729{
4730 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004731 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
4733 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4734 || lpwpos->cx != cx || lpwpos->cy != cy))
4735 {
4736 x = lpwpos->x;
4737 y = lpwpos->y;
4738 cx = lpwpos->cx;
4739 cy = lpwpos->cy;
4740 netbeans_frame_moved(x, y);
4741 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004742 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004743 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744}
4745#endif
4746
K.Takata4f2417f2021-06-05 16:25:32 +02004747
4748static HWND hwndTip = NULL;
4749
4750 static void
4751show_sizing_tip(int cols, int rows)
4752{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004753 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004754 char buf[32];
4755
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004756 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004757 ti.hwnd = s_hwnd;
4758 ti.uId = (UINT_PTR)s_hwnd;
4759 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4760 ti.lpszText = buf;
4761 sprintf(buf, "%dx%d", cols, rows);
4762 if (hwndTip == NULL)
4763 {
4764 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4765 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4766 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4767 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4768 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4769 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4770 }
4771 else
4772 {
4773 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4774 }
4775 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4776}
4777
4778 static void
4779destroy_sizing_tip(void)
4780{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004781 if (hwndTip == NULL)
4782 return;
4783
4784 DestroyWindow(hwndTip);
4785 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004786}
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 static int
4789_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 UINT fwSide,
4791 LPRECT lprc)
4792{
4793 int w, h;
4794 int valid_w, valid_h;
4795 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004796 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 w = lprc->right - lprc->left;
4799 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004800 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 w_offset = w - valid_w;
4802 h_offset = h - valid_h;
4803
4804 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4805 || fwSide == WMSZ_BOTTOMLEFT)
4806 lprc->left += w_offset;
4807 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4808 || fwSide == WMSZ_BOTTOMRIGHT)
4809 lprc->right -= w_offset;
4810
4811 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4812 || fwSide == WMSZ_TOPRIGHT)
4813 lprc->top += h_offset;
4814 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4815 || fwSide == WMSZ_BOTTOMRIGHT)
4816 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004817
4818 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 return TRUE;
4820}
4821
K.Takata92000e22022-01-20 15:10:57 +00004822#ifdef FEAT_GUI_TABLINE
4823 static void
4824_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4825{
4826 if (gui_mch_showing_tabline())
4827 {
4828 POINT pt;
4829 RECT rect;
4830
4831 /*
4832 * If the cursor is on the tabline, display the tab menu
4833 */
4834 GetCursorPos(&pt);
4835 GetWindowRect(s_textArea, &rect);
4836 if (pt.y < rect.top)
4837 {
4838 show_tabline_popup_menu();
4839 return;
4840 }
4841 }
4842 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4843}
4844
4845 static void
4846_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4847{
4848 /*
4849 * If the user double clicked the tabline, create a new tab
4850 */
4851 if (gui_mch_showing_tabline())
4852 {
4853 POINT pt;
4854 RECT rect;
4855
4856 GetCursorPos(&pt);
4857 GetWindowRect(s_textArea, &rect);
4858 if (pt.y < rect.top)
4859 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4860 }
4861 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4862}
4863#endif
4864
4865 static UINT
4866_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4867{
4868 UINT result;
4869 int x, y;
4870
4871 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4872 if (result != HTCLIENT)
4873 return result;
4874
4875#ifdef FEAT_GUI_TABLINE
4876 if (gui_mch_showing_tabline())
4877 {
4878 RECT rct;
4879
4880 // If the cursor is on the GUI tabline, don't process this event
4881 GetWindowRect(s_textArea, &rct);
4882 if (yPos < rct.top)
4883 return result;
4884 }
4885#endif
4886 (void)gui_mch_get_winpos(&x, &y);
4887 xPos -= x;
4888
4889 if (xPos < 48) // <VN> TODO should use system metric?
4890 return HTBOTTOMLEFT;
4891 else
4892 return HTBOTTOMRIGHT;
4893}
4894
4895#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4896 static LRESULT
4897_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4898{
4899 switch (hdr->code)
4900 {
4901 case TTN_GETDISPINFOW:
4902 case TTN_GETDISPINFO:
4903 {
4904 char_u *str = NULL;
4905 static void *tt_text = NULL;
4906
4907 VIM_CLEAR(tt_text);
4908
4909# ifdef FEAT_GUI_TABLINE
4910 if (gui_mch_showing_tabline()
4911 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4912 {
4913 POINT pt;
4914 /*
4915 * Mouse is over the GUI tabline. Display the
4916 * tooltip for the tab under the cursor
4917 *
4918 * Get the cursor position within the tab control
4919 */
4920 GetCursorPos(&pt);
4921 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4922 {
4923 TCHITTESTINFO htinfo;
4924 int idx;
4925
4926 /*
4927 * Get the tab under the cursor
4928 */
4929 htinfo.pt.x = pt.x;
4930 htinfo.pt.y = pt.y;
4931 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4932 if (idx != -1)
4933 {
4934 tabpage_T *tp;
4935
4936 tp = find_tabpage(idx + 1);
4937 if (tp != NULL)
4938 {
4939 get_tabline_label(tp, TRUE);
4940 str = NameBuff;
4941 }
4942 }
4943 }
4944 }
4945# endif
4946# ifdef FEAT_TOOLBAR
4947# ifdef FEAT_GUI_TABLINE
4948 else
4949# endif
4950 {
4951 UINT idButton;
4952 vimmenu_T *pMenu;
4953
4954 idButton = (UINT) hdr->idFrom;
4955 pMenu = gui_mswin_find_menu(root_menu, idButton);
4956 if (pMenu)
4957 str = pMenu->strings[MENU_INDEX_TIP];
4958 }
4959# endif
4960 if (str == NULL)
4961 break;
4962
4963 // Set the maximum width, this also enables using \n for
4964 // line break.
4965 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4966
4967 if (hdr->code == TTN_GETDISPINFOW)
4968 {
4969 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4970
4971 tt_text = enc_to_utf16(str, NULL);
4972 lpdi->lpszText = tt_text;
4973 // can't show tooltip if failed
4974 }
4975 else
4976 {
4977 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4978
4979 if (STRLEN(str) < sizeof(lpdi->szText)
4980 || ((tt_text = vim_strsave(str)) == NULL))
4981 vim_strncpy((char_u *)lpdi->szText, str,
4982 sizeof(lpdi->szText) - 1);
4983 else
4984 lpdi->lpszText = tt_text;
4985 }
4986 }
4987 break;
4988
4989# ifdef FEAT_GUI_TABLINE
4990 case TCN_SELCHANGE:
4991 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4992 {
4993 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4994 return 0L;
4995 }
4996 break;
4997
4998 case NM_RCLICK:
4999 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
5000 {
5001 show_tabline_popup_menu();
5002 return 0L;
5003 }
5004 break;
5005# endif
5006
5007 default:
5008 break;
5009 }
5010 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
5011}
5012#endif
5013
5014#if defined(MENUHINTS) && defined(FEAT_MENU)
5015 static LRESULT
5016_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
5017{
5018 if (((UINT) HIWORD(wParam)
5019 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5020 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01005021 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00005022 {
5023 UINT idButton;
5024 vimmenu_T *pMenu;
5025 static int did_menu_tip = FALSE;
5026
5027 if (did_menu_tip)
5028 {
5029 msg_clr_cmdline();
5030 setcursor();
5031 out_flush();
5032 did_menu_tip = FALSE;
5033 }
5034
5035 idButton = (UINT)LOWORD(wParam);
5036 pMenu = gui_mswin_find_menu(root_menu, idButton);
5037 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5038 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5039 {
5040 ++msg_hist_off;
5041 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
5042 --msg_hist_off;
5043 setcursor();
5044 out_flush();
5045 did_menu_tip = TRUE;
5046 }
5047 return 0L;
5048 }
5049 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
5050}
5051#endif
5052
Ken Takata7086b3e2023-10-02 21:26:03 +02005053 static BOOL
5054_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
5055{
Ken Takatada5da652023-10-05 20:20:58 +02005056 int old_width, old_height;
5057 int new_width, new_height;
5058 LOGFONTW lf;
5059 HFONT font;
5060
Ken Takata7086b3e2023-10-02 21:26:03 +02005061 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
5062
5063 // Calculate new approximate size.
Ken Takatada5da652023-10-05 20:20:58 +02005064 GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
5065 GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
5066 lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
5067 font = CreateFontIndirectW(&lf);
5068 if (font)
5069 {
5070 GetFontSize((GuiFont)font, &new_width, &new_height); // New size
5071 DeleteFont(font);
5072 }
5073 else
5074 {
5075 new_width = old_width;
5076 new_height = old_height;
5077 }
5078 size->cx = size->cx * new_width / old_width;
5079 size->cy = size->cy * new_height / old_height;
Ken Takata7086b3e2023-10-02 21:26:03 +02005080 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
5081
Ken Takatada5da652023-10-05 20:20:58 +02005082 return TRUE;
Ken Takata7086b3e2023-10-02 21:26:03 +02005083}
5084
K.Takatac81e9bf2022-01-16 14:15:49 +00005085 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02005086_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00005087{
5088 s_dpi = ydpi;
5089 s_in_dpichanged = TRUE;
5090 //TRACE("DPI: %d", ydpi);
5091
Ken Takata7086b3e2023-10-02 21:26:03 +02005092 s_suggested_rect = *rc;
5093 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
5094 // rc->right - rc->left, rc->bottom - rc->top);
5095
K.Takatac81e9bf2022-01-16 14:15:49 +00005096 update_scrollbar_size();
5097 update_toolbar_size();
5098 set_tabline_font();
5099
5100 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
5101 gui_get_wide_font();
5102 gui_mswin_get_menu_height(FALSE);
5103#ifdef FEAT_MBYTE_IME
5104 im_set_position(gui.row, gui.col);
5105#endif
5106 InvalidateRect(hwnd, NULL, TRUE);
5107
5108 s_in_dpichanged = FALSE;
5109 return 0L;
5110}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112
5113 static LRESULT CALLBACK
5114_WndProc(
5115 HWND hwnd,
5116 UINT uMsg,
5117 WPARAM wParam,
5118 LPARAM lParam)
5119{
LemonBoya773d842022-05-10 20:54:46 +01005120 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
5121 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 HandleMouseHide(uMsg, lParam);
5124
5125 s_uMsg = uMsg;
5126 s_wParam = wParam;
5127 s_lParam = lParam;
5128
5129 switch (uMsg)
5130 {
5131 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
5132 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005133 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005135 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
5137 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
5138 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
5139 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
5140#ifdef FEAT_MENU
5141 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
5142#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005143 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
5144 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
5146 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005147 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
5148 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5150 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5151 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5152#ifdef FEAT_NETBEANS_INTG
5153 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5154#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00005155#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00005156 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
5157 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005158#endif
K.Takata92000e22022-01-20 15:10:57 +00005159 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005160
Bram Moolenaar734a8672019-12-02 22:49:38 +01005161 case WM_QUERYENDSESSION: // System wants to go down.
5162 gui_shell_closed(); // Will exit when no changed buffers.
5163 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005166 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01005167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005169 return 0L;
5170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005174 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5175 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005176 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 return 0L;
5178
5179 case WM_SYSCHAR:
5180 /*
5181 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5182 * shortcut key, handle like a typed ALT key, otherwise call Windows
5183 * ALT key handling.
5184 */
5185#ifdef FEAT_MENU
5186 if ( !gui.menu_is_active
5187 || p_wak[0] == 'n'
5188 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5189 )
5190#endif
5191 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005192 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 return 0L;
5194 }
5195#ifdef FEAT_MENU
5196 else
K.Takata4ac893f2022-01-20 12:44:28 +00005197 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198#endif
5199
5200 case WM_SYSKEYUP:
5201#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005202 // This used to be done only when menu is active: ALT key is used for
5203 // that. But that caused problems when menu is disabled and using
5204 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
5205 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00005206 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01005208 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#endif
5210
K.Takata4f2417f2021-06-05 16:25:32 +02005211 case WM_EXITSIZEMOVE:
5212 destroy_sizing_tip();
5213 break;
5214
Bram Moolenaar734a8672019-12-02 22:49:38 +01005215 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005216 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01005219 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01005220 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005221 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
Bram Moolenaar734a8672019-12-02 22:49:38 +01005223 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00005224 case WM_SETTINGCHANGE:
5225 return _OnSettingChange((UINT)wParam);
5226
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005227#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00005229 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#endif
K.Takata92000e22022-01-20 15:10:57 +00005231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#if defined(MENUHINTS) && defined(FEAT_MENU)
5233 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00005234 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237#ifdef FEAT_MBYTE_IME
5238 case WM_IME_NOTIFY:
5239 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005240 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005241 return 1L;
5242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 case WM_IME_COMPOSITION:
5244 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005245 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005246 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02005248 case WM_GETDPISCALEDSIZE:
5249 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00005250 case WM_DPICHANGED:
5251 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5252 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005256 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#endif
K.Takata92000e22022-01-20 15:10:57 +00005259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
5261
K.Takata4ac893f2022-01-20 12:44:28 +00005262 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263}
5264
5265/*
5266 * End of call-back routines
5267 */
5268
Bram Moolenaar734a8672019-12-02 22:49:38 +01005269// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270HWND vim_parent_hwnd = NULL;
5271
5272 static BOOL CALLBACK
5273FindWindowTitle(HWND hwnd, LPARAM lParam)
5274{
5275 char buf[2048];
5276 char *title = (char *)lParam;
5277
5278 if (GetWindowText(hwnd, buf, sizeof(buf)))
5279 {
5280 if (strstr(buf, title) != NULL)
5281 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005282 // Found it. Store the window ref. and quit searching if MDI
5283 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005285 if (vim_parent_hwnd != NULL)
5286 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
5288 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005289 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290}
5291
5292/*
5293 * Invoked for '-P "title"' argument: search for parent application to open
5294 * our window in.
5295 */
5296 void
5297gui_mch_set_parent(char *title)
5298{
5299 EnumWindows(FindWindowTitle, (LPARAM)title);
5300 if (vim_parent_hwnd == NULL)
5301 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005302 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 mch_exit(2);
5304 }
5305}
5306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 static void
5309ole_error(char *arg)
5310{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005311 char buf[IOSIZE];
5312
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005313# ifdef VIMDLL
5314 gui.in_use = mch_is_gui_executable();
5315# endif
5316
Bram Moolenaar734a8672019-12-02 22:49:38 +01005317 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005318 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005319 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005320 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005324#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5325 static char *
5326gvim_error(void)
5327{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005328 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005329
5330 if (starting)
5331 {
5332 mch_errmsg(msg);
5333 mch_errmsg("\n");
5334 mch_exit(2);
5335 }
5336 return msg;
5337}
5338
5339 char *
5340gui_mch_do_spawn(char_u *arg)
5341{
5342 int len;
5343# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5344 char_u *session = NULL;
5345 LPWSTR tofree1 = NULL;
5346# endif
5347 WCHAR name[MAX_PATH];
5348 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5349 STARTUPINFOW si = {sizeof(si)};
5350 PROCESS_INFORMATION pi;
5351
5352 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5353 goto error;
5354 p = wcsrchr(name, L'\\');
5355 if (p == NULL)
5356 goto error;
5357 // Replace the executable name from vim(d).exe to gvim(d).exe.
5358# ifdef DEBUG
5359 wcscpy(p + 1, L"gvimd.exe");
5360# else
5361 wcscpy(p + 1, L"gvim.exe");
5362# endif
5363
5364# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5365 if (starting)
5366# endif
5367 {
5368 // Pass the command line to the new process.
5369 p = GetCommandLineW();
5370 // Skip 1st argument.
5371 while (*p && *p != L' ' && *p != L'\t')
5372 {
5373 if (*p == L'"')
5374 {
5375 while (*p && *p != L'"')
5376 ++p;
5377 if (*p)
5378 ++p;
5379 }
5380 else
5381 ++p;
5382 }
5383 cmd = p;
5384 }
5385# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5386 else
5387 {
5388 // Create a session file and pass it to the new process.
5389 LPWSTR wsession;
5390 char_u *savebg;
5391 int ret;
5392
5393 session = vim_tempname('s', FALSE);
5394 if (session == NULL)
5395 goto error;
5396 savebg = p_bg;
5397 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5398 ret = write_session_file(session);
5399 vim_free(p_bg);
5400 p_bg = savebg;
5401 if (!ret)
5402 goto error;
5403 wsession = enc_to_utf16(session, NULL);
5404 if (wsession == NULL)
5405 goto error;
5406 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005407 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005408 if (cmd == NULL)
5409 {
5410 vim_free(wsession);
5411 goto error;
5412 }
5413 tofree1 = cmd;
5414 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5415 wsession, wsession);
5416 vim_free(wsession);
5417 }
5418# endif
5419
5420 // Check additional arguments to the `:gui` command.
5421 if (arg != NULL)
5422 {
5423 warg = enc_to_utf16(arg, NULL);
5424 if (warg == NULL)
5425 goto error;
5426 tofree2 = warg;
5427 }
5428 else
5429 warg = L"";
5430
5431 // Set up the new command line.
5432 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005433 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005434 if (newcmd == NULL)
5435 goto error;
5436 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5437
5438 // Spawn a new GUI process.
5439 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5440 NULL, NULL, &si, &pi))
5441 goto error;
5442 CloseHandle(pi.hProcess);
5443 CloseHandle(pi.hThread);
5444 mch_exit(0);
5445
5446error:
5447# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5448 if (session)
5449 mch_remove(session);
5450 vim_free(session);
5451 vim_free(tofree1);
5452# endif
5453 vim_free(newcmd);
5454 vim_free(tofree2);
5455 return gvim_error();
5456}
5457#endif
5458
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459/*
5460 * Parse the GUI related command-line arguments. Any arguments used are
5461 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005462 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 */
5464 void
5465gui_mch_prepare(int *argc, char **argv)
5466{
5467 int silent = FALSE;
5468 int idx;
5469
Bram Moolenaar734a8672019-12-02 22:49:38 +01005470 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5472 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005473 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5475 && (argv[2][0] == '-' || argv[2][0] == '/'))
5476 {
5477 silent = TRUE;
5478 idx = 2;
5479 }
5480 else
5481 idx = 1;
5482
Bram Moolenaar734a8672019-12-02 22:49:38 +01005483 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 if (STRICMP(argv[idx] + 1, "register") == 0)
5485 {
5486#ifdef FEAT_OLE
5487 RegisterMe(silent);
5488 mch_exit(0);
5489#else
5490 if (!silent)
5491 ole_error("register");
5492 mch_exit(2);
5493#endif
5494 }
5495
Bram Moolenaar734a8672019-12-02 22:49:38 +01005496 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5498 {
5499#ifdef FEAT_OLE
5500 UnregisterMe(!silent);
5501 mch_exit(0);
5502#else
5503 if (!silent)
5504 ole_error("unregister");
5505 mch_exit(2);
5506#endif
5507 }
5508
Bram Moolenaar734a8672019-12-02 22:49:38 +01005509 // Ignore an -embedding argument. It is only relevant if the
5510 // application wants to treat the case when it is started manually
5511 // differently from the case where it is started via automation (and
5512 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5514 {
5515#ifdef FEAT_OLE
5516 *argc = 1;
5517#else
5518 ole_error("embedding");
5519 mch_exit(2);
5520#endif
5521 }
5522 }
5523
5524#ifdef FEAT_OLE
Ken Takatabaaf6de2024-07-29 20:57:19 +02005525# ifdef VIMDLL
5526 if (mch_is_gui_executable())
5527# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
5529 int bDoRestart = FALSE;
5530
5531 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005532 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (bDoRestart)
5534 mch_exit(0);
5535 }
5536#endif
5537
5538#ifdef FEAT_NETBEANS_INTG
5539 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005540 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 int arg;
5542
5543 for (arg = 1; arg < *argc; arg++)
5544 if (strncmp("-nb", argv[arg], 3) == 0)
5545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 netbeansArg = argv[arg];
5547 mch_memmove(&argv[arg], &argv[arg + 1],
5548 (--*argc - arg) * sizeof(char *));
5549 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005550 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554}
5555
K.Takatac81e9bf2022-01-16 14:15:49 +00005556 static void
5557load_dpi_func(void)
5558{
5559 HMODULE hUser32;
5560
5561 hUser32 = GetModuleHandle("user32.dll");
5562 if (hUser32 == NULL)
5563 goto fail;
5564
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005565 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5566 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5567 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005568 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005569 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5570 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005571
5572 if (pSetThreadDpiAwarenessContext != NULL)
5573 {
5574 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5575 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5576 if (oldctx != NULL)
5577 {
5578 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5579 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5580#ifdef DEBUG
5581 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5582 {
5583 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5584 }
5585#endif
5586 return;
5587 }
5588 }
5589
5590fail:
5591 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005592 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005593 pGetDpiForWindow = NULL;
5594 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5595 pSetThreadDpiAwarenessContext = NULL;
5596 pGetAwarenessFromDpiAwarenessContext = NULL;
5597}
5598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599/*
5600 * Initialise the GUI. Create all the windows, set up all the call-backs
5601 * etc.
5602 */
5603 int
5604gui_mch_init(void)
5605{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005607 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609
Bram Moolenaar734a8672019-12-02 22:49:38 +01005610 // Return here if the window was already opened (happens when
5611 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005613 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
5615 /*
5616 * Load the tearoff bitmap
5617 */
5618#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005619 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#endif
5621
K.Takatac81e9bf2022-01-16 14:15:49 +00005622 load_dpi_func();
5623
5624 s_dpi = pGetDpiForSystem();
5625 update_scrollbar_size();
5626
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005628 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629#endif
5630 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005631#ifdef FEAT_TOOLBAR
5632 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5636
Bram Moolenaar734a8672019-12-02 22:49:38 +01005637 // First try using the wide version, so that we can use any title.
5638 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005639 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005641 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 wndclassw.lpfnWndProc = _WndProc;
5643 wndclassw.cbClsExtra = 0;
5644 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005645 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5647 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5648 wndclassw.hbrBackground = s_brush;
5649 wndclassw.lpszMenuName = NULL;
5650 wndclassw.lpszClassName = szVimWndClassW;
5651
K.Takata4ac893f2022-01-20 12:44:28 +00005652 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005653 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 }
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (vim_parent_hwnd != NULL)
5657 {
5658#ifdef HAVE_TRY_EXCEPT
5659 __try
5660 {
5661#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005662 // Open inside the specified parent window.
5663 // TODO: last argument should point to a CLIENTCREATESTRUCT
5664 // structure.
5665 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005667 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005668 WS_OVERLAPPEDWINDOW | WS_CHILD
5669 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5671 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005672 100, // Any value will do
5673 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005675 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676#ifdef HAVE_TRY_EXCEPT
5677 }
5678 __except(EXCEPTION_EXECUTE_HANDLER)
5679 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005680 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682#endif
5683 if (s_hwnd == NULL)
5684 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005685 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 mch_exit(2);
5687 }
5688 }
5689 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005690 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005691 // If the provided windowid is not valid reset it to zero, so that it
5692 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005693 if (IsWindow((HWND)win_socket_id) <= 0)
5694 win_socket_id = 0;
5695
Bram Moolenaar734a8672019-12-02 22:49:38 +01005696 // Create a window. If win_socket_id is not zero without border and
5697 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005698 s_hwnd = CreateWindowW(
5699 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005700 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5701 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005702 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5703 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005704 100, // Any value will do
5705 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005706 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005707 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005708 if (s_hwnd != NULL && win_socket_id != 0)
5709 {
5710 SetParent(s_hwnd, (HWND)win_socket_id);
5711 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5712 }
5713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 if (s_hwnd == NULL)
5716 return FAIL;
5717
K.Takatac81e9bf2022-01-16 14:15:49 +00005718 if (pGetDpiForWindow != NULL)
5719 {
5720 s_dpi = pGetDpiForWindow(s_hwnd);
5721 update_scrollbar_size();
5722 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5723 }
5724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5726 dyn_imm_load();
5727#endif
5728
Bram Moolenaar734a8672019-12-02 22:49:38 +01005729 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005730 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005731 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005732 wndclassw.style = CS_OWNDC;
5733 wndclassw.lpfnWndProc = _TextAreaWndProc;
5734 wndclassw.cbClsExtra = 0;
5735 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005736 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005737 wndclassw.hIcon = NULL;
5738 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5739 wndclassw.hbrBackground = NULL;
5740 wndclassw.lpszMenuName = NULL;
5741 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005742
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005743 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 return FAIL;
5745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005747 s_textArea = CreateWindowExW(
5748 0,
5749 szTextAreaClassW, L"Vim text area",
5750 WS_CHILD | WS_VISIBLE, 0, 0,
5751 100, // Any value will do for now
5752 100, // Any value will do for now
5753 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005754 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 if (s_textArea == NULL)
5757 return FAIL;
5758
Bram Moolenaar20321902016-02-17 12:30:17 +01005759#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005760 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005761 {
5762 HANDLE hIcon = NULL;
5763
5764 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005765 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005766 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005767#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_MENU
5770 s_menuBar = CreateMenu();
5771#endif
5772 s_hdc = GetDC(s_textArea);
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar734a8672019-12-02 22:49:38 +01005776 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005777 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
Bram Moolenaar734a8672019-12-02 22:49:38 +01005779 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 gui_mch_def_colors();
5781
Bram Moolenaar734a8672019-12-02 22:49:38 +01005782 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5783 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 set_normal_colors();
5785
5786 /*
5787 * Check that none of the colors are the same as the background color.
5788 * Then store the current values as the defaults.
5789 */
5790 gui_check_colors();
5791 gui.def_norm_pixel = gui.norm_pixel;
5792 gui.def_back_pixel = gui.back_pixel;
5793
Bram Moolenaar734a8672019-12-02 22:49:38 +01005794 // Get the colors for the highlight groups (gui_check_colors() might have
5795 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 highlight_gui_started();
5797
5798 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005799 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005801 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802
5803 /*
5804 * Set up for Intellimouse processing
5805 */
5806 init_mouse_wheel();
5807
5808 /*
5809 * compute a couple of metrics used for the dialogs
5810 */
5811 get_dialog_font_metrics();
5812#ifdef FEAT_TOOLBAR
5813 /*
5814 * Create the toolbar
5815 */
5816 initialise_toolbar();
5817#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005818#ifdef FEAT_GUI_TABLINE
5819 /*
5820 * Create the tabline
5821 */
5822 initialise_tabline();
5823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824#ifdef MSWIN_FIND_REPLACE
5825 /*
5826 * Initialise the dialog box stuff
5827 */
5828 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5829
Bram Moolenaar734a8672019-12-02 22:49:38 +01005830 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005832 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005834 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5836 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5837 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005840#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005841 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005842 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005843#endif
5844
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005845#ifdef FEAT_RENDER_OPTIONS
5846 if (p_rop)
5847 (void)gui_mch_set_rendering_options(p_rop);
5848#endif
5849
Bram Moolenaar748bf032005-02-02 23:04:36 +00005850theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005851 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005852 display_errors();
5853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 return OK;
5855}
5856
5857/*
5858 * Get the size of the screen, taking position on multiple monitors into
5859 * account (if supported).
5860 */
5861 static void
5862get_work_area(RECT *spi_rect)
5863{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005864 HMONITOR mon;
5865 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
Bram Moolenaar734a8672019-12-02 22:49:38 +01005867 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005868 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005869 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005871 moninfo.cbSize = sizeof(MONITORINFO);
5872 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005874 *spi_rect = moninfo.rcWork;
5875 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
5877 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005878 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5880}
5881
5882/*
5883 * Set the size of the window to the given width and height in pixels.
5884 */
5885 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005886gui_mch_set_shellsize(
5887 int width,
5888 int height,
5889 int min_width UNUSED,
5890 int min_height UNUSED,
5891 int base_width UNUSED,
5892 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005893 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005896 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
Bram Moolenaar734a8672019-12-02 22:49:38 +01005899 // Try to keep window completely on screen.
5900 // Get position of the screen work area. This is the part that is not
5901 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 get_work_area(&workarea_rect);
5903
Bram Moolenaar734a8672019-12-02 22:49:38 +01005904 // Resizing a maximized window looks very strange, unzoom it first.
5905 // But don't do it when still starting up, it may have been requested in
5906 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005907 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005909
Ken Takata7086b3e2023-10-02 21:26:03 +02005910 if (s_in_dpichanged)
5911 // Use the suggested position when in WM_DPICHANGED.
5912 window_rect = s_suggested_rect;
5913 else
5914 // Use current position.
5915 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Bram Moolenaar734a8672019-12-02 22:49:38 +01005917 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005918 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5919 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5920 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5921 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5922 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5923 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar734a8672019-12-02 22:49:38 +01005925 // The following should take care of keeping Vim on the same monitor, no
5926 // matter if the secondary monitor is left or right of the primary
5927 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005928 window_rect.right = window_rect.left + win_width;
5929 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005930
Bram Moolenaar734a8672019-12-02 22:49:38 +01005931 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005932 // Don't adjust the position when in WM_DPICHANGED.
5933 if (!s_in_dpichanged)
5934 {
5935 if ((direction & RESIZE_HOR)
5936 && window_rect.right > workarea_rect.right)
5937 OffsetRect(&window_rect,
5938 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Ken Takata7086b3e2023-10-02 21:26:03 +02005940 if ((direction & RESIZE_HOR)
5941 && window_rect.left < workarea_rect.left)
5942 OffsetRect(&window_rect,
5943 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
Ken Takata7086b3e2023-10-02 21:26:03 +02005945 if ((direction & RESIZE_VERT)
5946 && window_rect.bottom > workarea_rect.bottom)
5947 OffsetRect(&window_rect,
5948 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
Ken Takata7086b3e2023-10-02 21:26:03 +02005950 if ((direction & RESIZE_VERT)
5951 && window_rect.top < workarea_rect.top)
5952 OffsetRect(&window_rect,
5953 0, workarea_rect.top - window_rect.top);
5954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005956 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5957 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
Ken Takata7086b3e2023-10-02 21:26:03 +02005959 //TRACE("New pos: %d,%d New size: %d,%d",
5960 // window_rect.left, window_rect.top, win_width, win_height);
5961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 SetActiveWindow(s_hwnd);
5963 SetFocus(s_hwnd);
5964
Bram Moolenaar734a8672019-12-02 22:49:38 +01005965 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967}
5968
5969
5970 void
5971gui_mch_set_scrollbar_thumb(
5972 scrollbar_T *sb,
5973 long val,
5974 long size,
5975 long max)
5976{
5977 SCROLLINFO info;
5978
5979 sb->scroll_shift = 0;
5980 while (max > 32767)
5981 {
5982 max = (max + 1) >> 1;
5983 val >>= 1;
5984 size >>= 1;
5985 ++sb->scroll_shift;
5986 }
5987
5988 if (sb->scroll_shift > 0)
5989 ++size;
5990
5991 info.cbSize = sizeof(info);
5992 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5993 info.nPos = val;
5994 info.nMin = 0;
5995 info.nMax = max;
5996 info.nPage = size;
5997 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5998}
5999
6000
6001/*
6002 * Set the current text font.
6003 */
6004 void
6005gui_mch_set_font(GuiFont font)
6006{
6007 gui.currFont = font;
6008}
6009
6010
6011/*
6012 * Set the current text foreground color.
6013 */
6014 void
6015gui_mch_set_fg_color(guicolor_T color)
6016{
6017 gui.currFgColor = color;
6018}
6019
6020/*
6021 * Set the current text background color.
6022 */
6023 void
6024gui_mch_set_bg_color(guicolor_T color)
6025{
6026 gui.currBgColor = color;
6027}
6028
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006029/*
6030 * Set the current text special color.
6031 */
6032 void
6033gui_mch_set_sp_color(guicolor_T color)
6034{
6035 gui.currSpColor = color;
6036}
6037
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006038#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039/*
6040 * Multi-byte handling, originally by Sung-Hoon Baek.
6041 * First static functions (no prototypes generated).
6042 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006043# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01006044# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006045# endif
6046# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
6048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 * handle WM_IME_NOTIFY message
6050 */
6051 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006052_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 LRESULT lResult = 0;
6055 HIMC hImc;
6056
6057 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6058 return lResult;
6059 switch (dwCommand)
6060 {
6061 case IMN_SETOPENSTATUS:
6062 if (pImmGetOpenStatus(hImc))
6063 {
K.Takatac81e9bf2022-01-16 14:15:49 +00006064 LOGFONTW lf = norm_logfont;
6065 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6066 // Work around when PerMonitorV2 is not enabled in the process level.
6067 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6068 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 im_set_position(gui.row, gui.col);
6070
Bram Moolenaar734a8672019-12-02 22:49:38 +01006071 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01006072 State &= ~MODE_LANGMAP;
6073 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006075# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006076 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6078 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006079 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 int old_row = gui.row;
6081 int old_col = gui.col;
6082
6083 // This must be called here before
6084 // status_redraw_curbuf(), otherwise the mode
6085 // message may appear in the wrong position.
6086 showmode();
6087 status_redraw_curbuf();
6088 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006089 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 gui.row = old_row;
6091 gui.col = old_col;
6092 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 }
6095 }
6096 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01006097 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 lResult = 0;
6099 break;
6100 }
6101 pImmReleaseContext(hWnd, hImc);
6102 return lResult;
6103}
6104
6105 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006106_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 char_u *ret;
6109 int len;
6110
Bram Moolenaar734a8672019-12-02 22:49:38 +01006111 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 return 0;
6113
6114 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6115 if (ret != NULL)
6116 {
6117 add_to_input_buf_csi(ret, len);
6118 vim_free(ret);
6119 return 1;
6120 }
6121 return 0;
6122}
6123
6124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 * void GetResultStr()
6126 *
6127 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6128 * get complete composition string
6129 */
6130 static char_u *
6131GetResultStr(HWND hwnd, int GCS, int *lenp)
6132{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006133 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00006134 LONG ret;
6135 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 char_u *convbuf = NULL;
6137
6138 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6139 return NULL;
6140
K.Takatab0b2b732022-01-19 12:59:21 +00006141 // Get the length of the composition string.
6142 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6143 if (ret <= 0)
6144 return NULL;
6145
6146 // Allocate the requested buffer plus space for the NUL character.
6147 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 if (buf == NULL)
6149 return NULL;
6150
K.Takatab0b2b732022-01-19 12:59:21 +00006151 // Reads in the composition string.
6152 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6153 *lenp = ret / sizeof(WCHAR);
6154
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006155 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 pImmReleaseContext(hwnd, hIMC);
6157 vim_free(buf);
6158 return convbuf;
6159}
6160#endif
6161
Bram Moolenaar734a8672019-12-02 22:49:38 +01006162// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006163#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165/*
6166 * set font to IM.
6167 */
6168 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006169im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 HIMC hImc;
6172
6173 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6174 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006175 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 pImmReleaseContext(s_hwnd, hImc);
6177 }
6178}
6179
6180/*
6181 * Notify cursor position to IM.
6182 */
6183 void
6184im_set_position(int row, int col)
6185{
6186 HIMC hImc;
6187
6188 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6189 {
6190 COMPOSITIONFORM cfs;
6191
6192 cfs.dwStyle = CFS_POINT;
6193 cfs.ptCurrentPos.x = FILL_X(col);
6194 cfs.ptCurrentPos.y = FILL_Y(row);
6195 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00006196 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6197 {
6198 // Work around when PerMonitorV2 is not enabled in the process level.
6199 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6200 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 pImmSetCompositionWindow(hImc, &cfs);
6203
6204 pImmReleaseContext(s_hwnd, hImc);
6205 }
6206}
6207
6208/*
6209 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6210 */
6211 void
6212im_set_active(int active)
6213{
6214 HIMC hImc;
6215 static HIMC hImcOld = (HIMC)0;
6216
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006217# ifdef VIMDLL
6218 if (!gui.in_use && !gui.starting)
6219 {
6220 mbyte_im_set_active(active);
6221 return;
6222 }
6223# endif
6224
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006225 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6226 return;
6227
6228 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006230 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006232 hImcOld = pImmGetContext(s_hwnd);
6233 if (hImcOld)
6234 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006236 active = FALSE;
6237 }
6238 else if (hImcOld != (HIMC)0)
6239 {
6240 pImmAssociateContext(s_hwnd, hImcOld);
6241 hImcOld = (HIMC)0;
6242 }
6243
6244 hImc = pImmGetContext(s_hwnd);
6245 if (!hImc)
6246 return;
6247
6248 /*
6249 * for Korean ime
6250 */
6251 HKL hKL = GetKeyboardLayout(0);
6252
6253 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6254 {
6255 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6256 static BOOL bSaved = FALSE;
6257
6258 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006260 // if we have a saved conversion status, restore it
6261 if (bSaved)
6262 pImmSetConversionStatus(hImc, dwConversionSaved,
6263 dwSentenceSaved);
6264 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006268 // save conversion status and disable korean
6269 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6270 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00006271 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006272 bSaved = TRUE;
6273 pImmSetConversionStatus(hImc,
6274 dwConversionSaved & ~(IME_CMODE_NATIVE
6275 | IME_CMODE_FULLSHAPE),
6276 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006280
6281 pImmSetOpenStatus(hImc, active);
6282 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283}
6284
6285/*
6286 * Get IM status. When IM is on, return not 0. Else return 0.
6287 */
6288 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006289im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 int status = 0;
6292 HIMC hImc;
6293
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006294# ifdef VIMDLL
6295 if (!gui.in_use && !gui.starting)
6296 return mbyte_im_get_status();
6297# endif
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6300 {
6301 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6302 pImmReleaseContext(s_hwnd, hImc);
6303 }
6304 return status;
6305}
6306
Bram Moolenaar734a8672019-12-02 22:49:38 +01006307#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006310/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006311 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006312 */
6313 static void
6314latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6315{
6316 int c;
6317
Bram Moolenaarca003e12006-03-17 23:19:38 +00006318 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006319 {
6320 c = *text++;
6321 switch (c)
6322 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006323 case 0xa4: c = 0x20ac; break; // euro
6324 case 0xa6: c = 0x0160; break; // S hat
6325 case 0xa8: c = 0x0161; break; // S -hat
6326 case 0xb4: c = 0x017d; break; // Z hat
6327 case 0xb8: c = 0x017e; break; // Z -hat
6328 case 0xbc: c = 0x0152; break; // OE
6329 case 0xbd: c = 0x0153; break; // oe
6330 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006331 }
6332 *unicodebuf++ = c;
6333 }
6334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335
6336#ifdef FEAT_RIGHTLEFT
6337/*
6338 * What is this for? In the case where you are using Win98 or Win2K or later,
6339 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6340 * reverses the string sent to the TextOut... family. This sucks, because we
6341 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6342 * way to tell Windblows not to do this!
6343 *
6344 * The short of it is that this 'RevOut' only gets called if you are running
6345 * one of the new, "improved" MS OSes, and only if you are running in
6346 * 'rightleft' mode. It makes display take *slightly* longer, but not
6347 * noticeably so.
6348 */
6349 static void
K.Takata135e1522022-01-29 15:27:58 +00006350RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 int col,
6352 int row,
6353 UINT foptions,
6354 CONST RECT *pcliprect,
6355 LPCTSTR text,
6356 UINT len,
6357 CONST INT *padding)
6358{
6359 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006361 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006362 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006363 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365#endif
6366
Bram Moolenaar92467d32017-12-05 13:22:16 +01006367 static void
6368draw_line(
6369 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006370 int y1,
6371 int x2,
6372 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006373 COLORREF color)
6374{
6375#if defined(FEAT_DIRECTX)
6376 if (IS_ENABLE_DIRECTX())
6377 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6378 else
6379#endif
6380 {
6381 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6382 HPEN old_pen = SelectObject(s_hdc, hpen);
6383 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006384 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006385 LineTo(s_hdc, x2, y2);
6386 DeleteObject(SelectObject(s_hdc, old_pen));
6387 }
6388}
6389
6390 static void
6391set_pixel(
6392 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006393 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006394 COLORREF color)
6395{
6396#if defined(FEAT_DIRECTX)
6397 if (IS_ENABLE_DIRECTX())
6398 DWriteContext_SetPixel(s_dwc, x, y, color);
6399 else
6400#endif
6401 SetPixel(s_hdc, x, y, color);
6402}
6403
6404 static void
6405fill_rect(
6406 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006407 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006408 COLORREF color)
6409{
6410#if defined(FEAT_DIRECTX)
6411 if (IS_ENABLE_DIRECTX())
6412 DWriteContext_FillRect(s_dwc, rcp, color);
6413 else
6414#endif
6415 {
6416 HBRUSH hbr2;
6417
6418 if (hbr == NULL)
6419 hbr2 = CreateSolidBrush(color);
6420 else
6421 hbr2 = hbr;
6422 FillRect(s_hdc, rcp, hbr2);
6423 if (hbr == NULL)
6424 DeleteBrush(hbr2);
6425 }
6426}
6427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 void
6429gui_mch_draw_string(
6430 int row,
6431 int col,
6432 char_u *text,
6433 int len,
6434 int flags)
6435{
6436 static int *padding = NULL;
6437 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 const RECT *pcliprect = NULL;
6439 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 static WCHAR *unicodebuf = NULL;
6441 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006442 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 int y;
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 /*
6447 * Italic and bold text seems to have an extra row of pixels at the bottom
6448 * (below where the bottom of the character should be). If we draw the
6449 * characters with a solid background, the top row of pixels in the
6450 * character below will be overwritten. We can fix this by filling in the
6451 * background ourselves, to the correct character proportions, and then
6452 * writing the character in transparent mode. Still have a problem when
6453 * the character is "_", which gets written on to the character below.
6454 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6455 * pixel in their slots, which fixes the problem with the bottom row of
6456 * pixels. We still need this code because otherwise the top row of pixels
6457 * becomes a problem. - webb.
6458 */
6459 static HBRUSH hbr_cache[2] = {NULL, NULL};
6460 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6461 static int brush_lru = 0;
6462 HBRUSH hbr;
6463 RECT rc;
6464
6465 if (!(flags & DRAW_TRANSP))
6466 {
6467 /*
6468 * Clear background first.
6469 * Note: FillRect() excludes right and bottom of rectangle.
6470 */
6471 rc.left = FILL_X(col);
6472 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 if (has_mbyte)
6474 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006475 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006476 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 rc.right = FILL_X(col + len);
6480 rc.bottom = FILL_Y(row + 1);
6481
Bram Moolenaar734a8672019-12-02 22:49:38 +01006482 // Cache the created brush, that saves a lot of time. We need two:
6483 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if (gui.currBgColor == brush_color[0])
6485 {
6486 hbr = hbr_cache[0];
6487 brush_lru = 1;
6488 }
6489 else if (gui.currBgColor == brush_color[1])
6490 {
6491 hbr = hbr_cache[1];
6492 brush_lru = 0;
6493 }
6494 else
6495 {
6496 if (hbr_cache[brush_lru] != NULL)
6497 DeleteBrush(hbr_cache[brush_lru]);
6498 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6499 brush_color[brush_lru] = gui.currBgColor;
6500 hbr = hbr_cache[brush_lru];
6501 brush_lru = !brush_lru;
6502 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006503
Bram Moolenaar92467d32017-12-05 13:22:16 +01006504 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 SetBkMode(s_hdc, TRANSPARENT);
6507
6508 /*
6509 * When drawing block cursor, prevent inverted character spilling
6510 * over character cell (can happen with bold/italic)
6511 */
6512 if (flags & DRAW_CURSOR)
6513 {
6514 pcliprect = &rc;
6515 foptions = ETO_CLIPPED;
6516 }
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 SetTextColor(s_hdc, gui.currFgColor);
6519 SelectFont(s_hdc, gui.currFont);
6520
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006521#ifdef FEAT_DIRECTX
6522 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006523 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006524#endif
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6527 {
K.Takata135e1522022-01-29 15:27:58 +00006528 int i;
6529
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 vim_free(padding);
6531 pad_size = Columns;
6532
Bram Moolenaar734a8672019-12-02 22:49:38 +01006533 // Don't give an out-of-memory message here, it would call us
6534 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006535 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (padding != NULL)
6537 for (i = 0; i < pad_size; i++)
6538 padding[i] = gui.char_width;
6539 }
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 /*
6542 * We have to provide the padding argument because italic and bold versions
6543 * of fixed-width fonts are often one pixel or so wider than their normal
6544 * versions.
6545 * No check for DRAW_BOLD, Windows will have done it already.
6546 */
6547
Bram Moolenaar734a8672019-12-02 22:49:38 +01006548 // Check if there are any UTF-8 characters. If not, use normal text
6549 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 if (enc_utf8)
6551 for (n = 0; n < len; ++n)
6552 if (text[n] >= 0x80)
6553 break;
6554
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006555#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006556 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6557 // required that unicode drawing routine, currently. So this forces it
6558 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006559 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006560 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006561#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006562
Bram Moolenaar734a8672019-12-02 22:49:38 +01006563 // Check if the Unicode buffer exists and is big enough. Create it
6564 // with the same length as the multi-byte string, the number of wide
6565 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006566 if ((enc_utf8
6567 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6568 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 && (unicodebuf == NULL || len > unibuflen))
6570 {
6571 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006572 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006575 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 unibuflen = len;
6578 }
6579
6580 if (enc_utf8 && n < len && unicodebuf != NULL)
6581 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006582 // Output UTF-8 characters. Composing characters should be
6583 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006584 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006585 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006586 int cells; // cell width of string up to composing char
6587 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006588 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006590 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006592 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006594 c = utf_ptr2char(text + i);
6595 if (c >= 0x10000)
6596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006597 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006598 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6599 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006600 }
6601 else
6602 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006603 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006604 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006605
6606 if (utf_iscomposing(c))
6607 cw = 0;
6608 else
6609 {
6610 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006611 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006612 cw = 1;
6613 }
6614
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (unicodepdy != NULL)
6616 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006617 // Use unicodepdy to make characters fit as we expect, even
6618 // when the font uses different widths (e.g., bold character
6619 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006620 if (c >= 0x10000)
6621 {
6622 unicodepdy[wlen - 2] = cw * gui.char_width;
6623 unicodepdy[wlen - 1] = 0;
6624 }
6625 else
6626 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
6628 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006629 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006631#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006632 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006633 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006635 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006636 TEXT_X(col), TEXT_Y(row),
6637 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006638 gui.char_width, gui.currFgColor,
6639 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006640 }
6641 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006642#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006643 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6644 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006645 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006647 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006649 // If we want to display codepage data, and the current CP is not the
6650 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 if (unicodebuf != NULL)
6652 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006653 if (enc_latin9)
6654 latin9_to_ucs(text, len, unicodebuf);
6655 else
6656 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 MB_PRECOMPOSED,
6658 (char *)text, len,
6659 (LPWSTR)unicodebuf, unibuflen);
6660 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006661 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006662 // Use unicodepdy to make characters fit as we expect, even
6663 // when the font uses different widths (e.g., bold character
6664 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006665 if (unicodepdy != NULL)
6666 {
6667 int i;
6668 int cw;
6669
6670 for (i = 0; i < len; ++i)
6671 {
6672 cw = utf_char2cells(unicodebuf[i]);
6673 if (cw > 2)
6674 cw = 1;
6675 unicodepdy[i] = cw * gui.char_width;
6676 }
6677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006679 foptions, pcliprect, unicodebuf, len, unicodepdy);
6680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 }
6682 }
6683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006686 // Windows will mess up RL text, so we have to draw it character by
6687 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006688 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6690 foptions, pcliprect, (char *)text, len, padding);
6691 else
6692#endif
6693 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6694 foptions, pcliprect, (char *)text, len, padding);
6695 }
6696
Bram Moolenaar734a8672019-12-02 22:49:38 +01006697 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (flags & DRAW_UNDERL)
6699 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006700 // When p_linespace is 0, overwrite the bottom row of pixels.
6701 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (p_linespace > 1)
6704 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006705 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006707
Bram Moolenaar734a8672019-12-02 22:49:38 +01006708 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006709 if (flags & DRAW_STRIKE)
6710 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006711 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006712 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006713 }
6714
Bram Moolenaar734a8672019-12-02 22:49:38 +01006715 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006716 if (flags & DRAW_UNDERC)
6717 {
6718 int x;
6719 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006720 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006721
6722 y = FILL_Y(row + 1) - 1;
6723 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6724 {
6725 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006726 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006727 }
6728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729}
6730
6731
6732/*
6733 * Output routines.
6734 */
6735
Bram Moolenaar734a8672019-12-02 22:49:38 +01006736/*
6737 * Flush any output to the screen
6738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 void
6740gui_mch_flush(void)
6741{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006742#if defined(FEAT_DIRECTX)
6743 if (IS_ENABLE_DIRECTX())
6744 DWriteContext_Flush(s_dwc);
6745#endif
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 GdiFlush();
6748}
6749
6750 static void
6751clear_rect(RECT *rcp)
6752{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006753 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755
6756
Bram Moolenaarc716c302006-01-21 22:12:51 +00006757 void
6758gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6759{
6760 RECT workarea_rect;
6761
6762 get_work_area(&workarea_rect);
6763
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006764 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006765 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6766 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006767
Bram Moolenaar734a8672019-12-02 22:49:38 +01006768 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6769 // the menubar for MSwin, we subtract it from the screen height, so that
6770 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006771 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006772 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6773 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6774 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6775 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006776}
6777
6778
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779#if defined(FEAT_MENU) || defined(PROTO)
6780/*
6781 * Add a sub menu to the menu bar.
6782 */
6783 void
6784gui_mch_add_menu(
6785 vimmenu_T *menu,
6786 int pos)
6787{
6788 vimmenu_T *parent = menu->parent;
6789
6790 menu->submenu_id = CreatePopupMenu();
6791 menu->id = s_menu_id++;
6792
6793 if (menu_is_menubar(menu->name))
6794 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006795 WCHAR *wn;
6796 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006798 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006799 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006800 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006802 infow.cbSize = sizeof(infow);
6803 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6804 | MIIM_SUBMENU;
6805 infow.dwItemData = (long_u)menu;
6806 infow.wID = menu->id;
6807 infow.fType = MFT_STRING;
6808 infow.dwTypeData = wn;
6809 infow.cch = (UINT)wcslen(wn);
6810 infow.hSubMenu = menu->submenu_id;
6811 InsertMenuItemW((parent == NULL)
6812 ? s_menuBar : parent->submenu_id,
6813 (UINT)pos, TRUE, &infow);
6814 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816
Bram Moolenaar734a8672019-12-02 22:49:38 +01006817 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (parent == NULL)
6819 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006820# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 else if (IsWindow(parent->tearoff_handle))
6822 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824}
6825
6826 void
6827gui_mch_show_popupmenu(vimmenu_T *menu)
6828{
6829 POINT mp;
6830
K.Takata45f9cfb2022-01-21 11:11:00 +00006831 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6833}
6834
6835 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006836gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838 vimmenu_T *menu = gui_find_menu(path_name);
6839
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006840 if (menu == NULL)
6841 return;
6842
6843 POINT p;
6844
6845 // Find the position of the current cursor
6846 GetDCOrgEx(s_hdc, &p);
6847 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006849 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006851 gui_mch_getmouse(&mx, &my);
6852 p.x += mx;
6853 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006855 else if (curwin != NULL)
6856 {
6857 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6858 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6859 }
6860 msg_scroll = FALSE;
6861 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862}
6863
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006864# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865/*
6866 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6867 * create it as a pseudo-"tearoff menu".
6868 */
6869 void
6870gui_make_tearoff(char_u *path_name)
6871{
6872 vimmenu_T *menu = gui_find_menu(path_name);
6873
Bram Moolenaar734a8672019-12-02 22:49:38 +01006874 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (menu != NULL)
6876 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6877}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
6880/*
6881 * Add a menu item to a menu
6882 */
6883 void
6884gui_mch_add_menu_item(
6885 vimmenu_T *menu,
6886 int idx)
6887{
6888 vimmenu_T *parent = menu->parent;
6889
6890 menu->id = s_menu_id++;
6891 menu->submenu_id = NULL;
6892
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006893# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6895 {
6896 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6897 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6898 }
6899 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006900# endif
6901# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 if (menu_is_toolbar(parent->name))
6903 {
6904 TBBUTTON newtb;
6905
Bram Moolenaara80faa82020-04-12 19:37:17 +02006906 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (menu_is_separator(menu->name))
6908 {
6909 newtb.iBitmap = 0;
6910 newtb.fsStyle = TBSTYLE_SEP;
6911 }
6912 else
6913 {
6914 newtb.iBitmap = get_toolbar_bitmap(menu);
6915 newtb.fsStyle = TBSTYLE_BUTTON;
6916 }
6917 newtb.idCommand = menu->id;
6918 newtb.fsState = TBSTATE_ENABLED;
6919 newtb.iString = 0;
6920 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6921 (LPARAM)&newtb);
6922 menu->submenu_id = (HMENU)-1;
6923 }
6924 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006925# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006927 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006929 wn = enc_to_utf16(menu->name, NULL);
6930 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006932 InsertMenuW(parent->submenu_id, (UINT)idx,
6933 (menu_is_separator(menu->name)
6934 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6935 (UINT)menu->id, wn);
6936 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006938# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 if (IsWindow(parent->tearoff_handle))
6940 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943}
6944
6945/*
6946 * Destroy the machine specific menu widget.
6947 */
6948 void
6949gui_mch_destroy_menu(vimmenu_T *menu)
6950{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006951# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 /*
6953 * is this a toolbar button?
6954 */
6955 if (menu->submenu_id == (HMENU)-1)
6956 {
6957 int iButton;
6958
6959 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6960 (WPARAM)menu->id, 0);
6961 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6962 }
6963 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006964# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
6966 if (menu->parent != NULL
6967 && menu_is_popup(menu->parent->dname)
6968 && menu->parent->submenu_id != NULL)
6969 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6970 else
6971 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6972 if (menu->submenu_id != NULL)
6973 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006974# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 if (IsWindow(menu->tearoff_handle))
6976 DestroyWindow(menu->tearoff_handle);
6977 if (menu->parent != NULL
6978 && menu->parent->children != NULL
6979 && IsWindow(menu->parent->tearoff_handle))
6980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 menu->modes = 0;
6983 rebuild_tearoff(menu->parent);
6984 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987}
6988
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006989# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 static void
6991rebuild_tearoff(vimmenu_T *menu)
6992{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006993 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 char_u tbuf[128];
6995 RECT trect;
6996 RECT rct;
6997 RECT roct;
6998 int x, y;
6999
7000 HWND thwnd = menu->tearoff_handle;
7001
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007002 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 if (GetWindowRect(thwnd, &trect)
7004 && GetWindowRect(s_hwnd, &rct)
7005 && GetClientRect(s_hwnd, &roct))
7006 {
7007 x = trect.left - rct.left;
7008 y = (trect.top - rct.bottom + roct.bottom);
7009 }
7010 else
7011 {
7012 x = y = 0xffffL;
7013 }
7014 DestroyWindow(thwnd);
7015 if (menu->children != NULL)
7016 {
7017 gui_mch_tearoff(tbuf, menu, x, y);
7018 if (IsWindow(menu->tearoff_handle))
7019 (void) SetWindowPos(menu->tearoff_handle,
7020 NULL,
7021 (int)trect.left,
7022 (int)trect.top,
7023 0, 0,
7024 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7025 }
7026}
Bram Moolenaar734a8672019-12-02 22:49:38 +01007027# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
7029/*
7030 * Make a menu either grey or not grey.
7031 */
7032 void
7033gui_mch_menu_grey(
7034 vimmenu_T *menu,
7035 int grey)
7036{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007037# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 /*
7039 * is this a toolbar button?
7040 */
7041 if (menu->submenu_id == (HMENU)-1)
7042 {
7043 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00007044 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007047# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007048 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7049 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007051# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7053 {
7054 WORD menuID;
7055 HWND menuHandle;
7056
7057 /*
7058 * A tearoff button has changed state.
7059 */
7060 if (menu->children == NULL)
7061 menuID = (WORD)(menu->id);
7062 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007063 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7065 if (menuHandle)
7066 EnableWindow(menuHandle, !grey);
7067
7068 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070}
7071
Bram Moolenaar734a8672019-12-02 22:49:38 +01007072#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
7074
Bram Moolenaar734a8672019-12-02 22:49:38 +01007075// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007078#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7081/*
7082 * stuff for dialogs
7083 */
7084
7085/*
7086 * The callback routine used by all the dialogs. Very simple. First,
7087 * acknowledges the INITDIALOG message so that Windows knows to do standard
7088 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7089 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7090 * number.
7091 */
7092 static LRESULT CALLBACK
7093dialog_callback(
7094 HWND hwnd,
7095 UINT message,
7096 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007097 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098{
7099 if (message == WM_INITDIALOG)
7100 {
7101 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007102 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 (void)SetFocus(hwnd);
7104 if (dialog_default_button > IDCANCEL)
7105 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007106 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007107 // We don't have a default, set focus on another element of the
7108 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007109 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 return FALSE;
7111 }
7112
7113 if (message == WM_COMMAND)
7114 {
7115 int button = LOWORD(wParam);
7116
Bram Moolenaar734a8672019-12-02 22:49:38 +01007117 // Don't end the dialog if something was selected that was
7118 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 if (button >= DLG_NONBUTTON_CONTROL)
7120 return TRUE;
7121
Bram Moolenaar734a8672019-12-02 22:49:38 +01007122 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007124 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007125 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007126 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007127
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007128 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7129 p = utf16_to_enc(wp, NULL);
John Marriott031f2272025-04-23 20:56:08 +02007130 if (p != NULL)
7131 {
7132 vim_strncpy(s_textfield, p, IOSIZE);
7133 vim_free(p);
7134 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007135 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 /*
7139 * Need to check for IDOK because if the user just hits Return to
7140 * accept the default value, some reason this is what we get.
7141 */
7142 if (button == IDOK)
7143 {
7144 if (dialog_default_button > IDCANCEL)
7145 EndDialog(hwnd, dialog_default_button);
7146 }
7147 else
7148 EndDialog(hwnd, button - IDCANCEL);
7149 return TRUE;
7150 }
7151
7152 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7153 {
7154 EndDialog(hwnd, 0);
7155 return TRUE;
7156 }
7157 return FALSE;
7158}
7159
7160/*
7161 * Create a dialog dynamically from the parameter strings.
7162 * type = type of dialog (question, alert, etc.)
7163 * title = dialog title. may be NULL for default title.
7164 * message = text to display. Dialog sizes to accommodate it.
7165 * buttons = '\n' separated list of button captions, default first.
7166 * dfltbutton = number of default button.
7167 *
7168 * This routine returns 1 if the first button is pressed,
7169 * 2 for the second, etc.
7170 *
7171 * 0 indicates Esc was pressed.
7172 * -1 for unexpected error
7173 *
7174 * If stubbing out this fn, return 1.
7175 */
7176
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 "IDR_VIM",
7180 "IDR_VIM_ERROR",
7181 "IDR_VIM_ALERT",
7182 "IDR_VIM_INFO",
7183 "IDR_VIM_QUESTION"
7184};
7185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int
7187gui_mch_dialog(
7188 int type,
7189 char_u *title,
7190 char_u *message,
7191 char_u *buttons,
7192 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007193 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007194 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195{
7196 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007197 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 int numButtons;
7199 int *buttonWidths, *buttonPositions;
7200 int buttonYpos;
7201 int nchar, i;
7202 DWORD lStyle;
7203 int dlgwidth = 0;
7204 int dlgheight;
7205 int editboxheight;
7206 int horizWidth = 0;
7207 int msgheight;
7208 char_u *pstart;
7209 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007210 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 char_u *tbuffer;
7212 RECT rect;
7213 HWND hwnd;
7214 HDC hdc;
7215 HFONT font, oldFont;
7216 TEXTMETRIC fontInfo;
7217 int fontHeight;
7218 int textWidth, minButtonWidth, messageWidth;
7219 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007220 int maxDialogHeight;
7221 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 int vertical;
7223 int dlgPaddingX;
7224 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007225# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007226 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007228# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007229 garray_T ga;
7230 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007231 int dlg_icon_width;
7232 int dlg_icon_height;
7233 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007235# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007236 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007237# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007238 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007239# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007240 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007241 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
Bram Moolenaar748bf032005-02-02 23:04:36 +00007244 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007245 {
7246 load_dpi_func();
7247 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007248 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007249 }
7250 else
7251 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 if ((type < 0) || (type > VIM_LAST_TYPE))
7254 type = 0;
7255
Bram Moolenaar734a8672019-12-02 22:49:38 +01007256 // allocate some memory for dialog template
7257 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007258 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007259 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 if (p == NULL)
7262 return -1;
7263
7264 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007265 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7267 * const.
7268 */
7269 tbuffer = vim_strsave(buttons);
7270 if (tbuffer == NULL)
7271 return -1;
7272
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274
Bram Moolenaar734a8672019-12-02 22:49:38 +01007275 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 numButtons = 1;
7277 for (i = 0; tbuffer[i] != '\0'; i++)
7278 {
7279 if (tbuffer[i] == DLG_BUTTON_SEP)
7280 numButtons++;
7281 }
7282 if (dfltbutton >= numButtons)
7283 dfltbutton = -1;
7284
Bram Moolenaar734a8672019-12-02 22:49:38 +01007285 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007286 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (buttonWidths == NULL)
7288 return -1;
7289
Bram Moolenaar734a8672019-12-02 22:49:38 +01007290 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007291 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (buttonPositions == NULL)
7293 return -1;
7294
7295 /*
7296 * Calculate how big the dialog must be.
7297 */
7298 hwnd = GetDesktopWindow();
7299 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007300# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7302 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007303 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 use_lfSysmenu = TRUE;
7305 }
7306 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007307# endif
K.Takatad1c58992022-01-23 12:31:57 +00007308 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7309 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7310
7311 oldFont = SelectFont(hdc, font);
7312 dlgPaddingX = DLG_PADDING_X;
7313 dlgPaddingY = DLG_PADDING_Y;
7314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 GetTextMetrics(hdc, &fontInfo);
7316 fontHeight = fontInfo.tmHeight;
7317
Bram Moolenaar734a8672019-12-02 22:49:38 +01007318 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007319 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320
Bram Moolenaar734a8672019-12-02 22:49:38 +01007321 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007322 if (s_hwnd == NULL)
7323 {
7324 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325
Bram Moolenaar734a8672019-12-02 22:49:38 +01007326 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007327 get_work_area(&workarea_rect);
7328 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007329 if (maxDialogWidth > adjust_by_system_dpi(600))
7330 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007331 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007332 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007333 }
7334 else
7335 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007336 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007337 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007338 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007339 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7340 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7341 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7342 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007343
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007344 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007345 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7346 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7347 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7348 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7349 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007350 }
7351
Bram Moolenaar734a8672019-12-02 22:49:38 +01007352 // Set dlgwidth to width of message.
7353 // Copy the message into "ga", changing NL to CR-NL and inserting line
7354 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 pstart = message;
7356 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007357 msgheight = 0;
7358 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 do
7360 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007362
Bram Moolenaar734a8672019-12-02 22:49:38 +01007363 // Need to figure out where to break the string. The system does it
7364 // at a word boundary, which would mean we can't compute the number of
7365 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007366 textWidth = 0;
7367 last_white = NULL;
7368 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007369 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007370 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007371 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007372 && textWidth > maxDialogWidth * 3 / 4)
7373 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007374 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007375 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007376 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007377 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007378 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007379 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007380 textWidth = 0;
7381
7382 if (last_white != NULL)
7383 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007384 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007385 if (pend > last_white)
7386 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007387 pend = last_white + 1;
7388 last_white = NULL;
7389 }
7390 ga_append(&ga, '\r');
7391 ga_append(&ga, '\n');
7392 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007393 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007394
7395 while (--l >= 0)
7396 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007397 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007398 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007400
7401 ga_append(&ga, '\r');
7402 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 pstart = pend + 1;
7404 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007405
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007406 if (ga.ga_data != NULL)
7407 message = ga.ga_data;
7408
Bram Moolenaar734a8672019-12-02 22:49:38 +01007409 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007410
K.Takatac81e9bf2022-01-16 14:15:49 +00007411 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7412 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
K.Takatac81e9bf2022-01-16 14:15:49 +00007414 // Add width of icon to dlgwidth, and some space
7415 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7416 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7417
7418 if (msgheight < dlg_icon_height)
7419 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
7421 /*
7422 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007423 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007425 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (!vertical)
7427 {
7428 // Place buttons horizontally if they fit.
7429 horizWidth = dlgPaddingX;
7430 pstart = tbuffer;
7431 i = 0;
7432 do
7433 {
7434 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7435 if (pend == NULL)
7436 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007437 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (textWidth < minButtonWidth)
7439 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007440 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 buttonWidths[i] = textWidth;
7442 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007443 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 pstart = pend + 1;
7445 } while (*pend != NUL);
7446
7447 if (horizWidth > maxDialogWidth)
7448 vertical = TRUE; // Too wide to fit on the screen.
7449 else if (horizWidth > dlgwidth)
7450 dlgwidth = horizWidth;
7451 }
7452
7453 if (vertical)
7454 {
7455 // Stack buttons vertically.
7456 pstart = tbuffer;
7457 do
7458 {
7459 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7460 if (pend == NULL)
7461 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007462 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007463 textWidth += dlgPaddingX; // Padding within button
7464 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 if (textWidth > dlgwidth)
7466 dlgwidth = textWidth;
7467 pstart = pend + 1;
7468 } while (*pend != NUL);
7469 }
7470
7471 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007472 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473
Bram Moolenaar734a8672019-12-02 22:49:38 +01007474 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007475 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476
7477 add_long(lStyle);
7478 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007479 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 add_word(0); // NumberOfItems(will change later)
7481 add_word(10); // x
7482 add_word(10); // y
7483 add_word(PixelToDialogX(dlgwidth)); // cx
7484
7485 // Dialog height.
7486 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007487 dlgheight = msgheight + 2 * dlgPaddingY
7488 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 else
7490 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7491
7492 // Dialog needs to be taller if contains an edit box.
7493 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7494 if (textfield != NULL)
7495 dlgheight += editboxheight;
7496
Bram Moolenaar734a8672019-12-02 22:49:38 +01007497 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007498 if (dlgheight > maxDialogHeight)
7499 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007500 msgheight = msgheight - (dlgheight - maxDialogHeight);
7501 dlgheight = maxDialogHeight;
7502 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007503 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007504 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007505 }
7506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 add_word(PixelToDialogY(dlgheight));
7508
7509 add_word(0); // Menu
7510 add_word(0); // Class
7511
Bram Moolenaar734a8672019-12-02 22:49:38 +01007512 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007513 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7514 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 p += nchar;
7516
K.Takatad1c58992022-01-23 12:31:57 +00007517 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007518# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007519 if (use_lfSysmenu)
7520 {
7521 // point size
7522 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7523 GetDeviceCaps(hdc, LOGPIXELSY));
7524 wcscpy(p, lfSysmenu.lfFaceName);
7525 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 }
K.Takatad1c58992022-01-23 12:31:57 +00007527 else
7528# endif
7529 {
7530 *p++ = DLG_FONT_POINT_SIZE; // point size
7531 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7532 }
7533 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534
7535 buttonYpos = msgheight + 2 * dlgPaddingY;
7536
7537 if (textfield != NULL)
7538 buttonYpos += editboxheight;
7539
7540 pstart = tbuffer;
7541 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007542 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 for (i = 0; i < numButtons; i++)
7544 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007545 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 for ( pend = pstart;
7547 *pend && (*pend != DLG_BUTTON_SEP);
7548 pend++)
7549 ;
7550
7551 if (*pend)
7552 *pend = '\0';
7553
7554 /*
7555 * old NOTE:
7556 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7557 * the focus to the first tab-able button and in so doing makes that
7558 * the default!! Grrr. Workaround: Make the default button the only
7559 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7560 * he/she can use arrow keys.
7561 *
7562 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007563 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 * dialog. Also needed for when the textfield is the default control.
7565 * It appears to work now (perhaps not on Win95?).
7566 */
7567 if (vertical)
7568 {
7569 p = add_dialog_element(p,
7570 (i == dfltbutton
7571 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7572 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007573 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 + 2 * fontHeight * i),
7575 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7576 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007577 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
7579 else
7580 {
7581 p = add_dialog_element(p,
7582 (i == dfltbutton
7583 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7584 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007585 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 PixelToDialogX(buttonWidths[i]),
7587 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007588 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007590 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592 *pnumitems += numButtons;
7593
Bram Moolenaar734a8672019-12-02 22:49:38 +01007594 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 p = add_dialog_element(p, SS_ICON,
7596 PixelToDialogX(dlgPaddingX),
7597 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007598 PixelToDialogX(dlg_icon_width),
7599 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7601 dlg_icons[type]);
7602
Bram Moolenaar734a8672019-12-02 22:49:38 +01007603 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007604 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007605 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007606 PixelToDialogY(dlgPaddingY),
7607 (WORD)(PixelToDialogX(messageWidth) + 1),
7608 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007609 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610
Bram Moolenaar734a8672019-12-02 22:49:38 +01007611 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (textfield != NULL)
7613 {
7614 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7615 PixelToDialogX(2 * dlgPaddingX),
7616 PixelToDialogY(2 * dlgPaddingY + msgheight),
7617 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7618 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007619 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 *pnumitems += 1;
7621 }
7622
7623 *pnumitems += 2;
7624
7625 SelectFont(hdc, oldFont);
7626 DeleteObject(font);
7627 ReleaseDC(hwnd, hdc);
7628
Bram Moolenaar734a8672019-12-02 22:49:38 +01007629 // Let the dialog_callback() function know which button to make default
7630 // If we have an edit box, make that the default. We also need to tell
7631 // dialog_callback() if this dialog contains an edit box or not. We do
7632 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 if (textfield != NULL)
7634 {
7635 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7636 s_textfield = textfield;
7637 }
7638 else
7639 {
7640 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7641 s_textfield = NULL;
7642 }
7643
Bram Moolenaar734a8672019-12-02 22:49:38 +01007644 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007646 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 (LPDLGTEMPLATE)pdlgtemplate,
7648 s_hwnd,
7649 (DLGPROC)dialog_callback);
7650
7651 LocalFree(LocalHandle(pdlgtemplate));
7652 vim_free(tbuffer);
7653 vim_free(buttonWidths);
7654 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007655 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656
Bram Moolenaar734a8672019-12-02 22:49:38 +01007657 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 (void)SetFocus(s_hwnd);
7659
7660 return nchar;
7661}
7662
Bram Moolenaar734a8672019-12-02 22:49:38 +01007663#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665/*
7666 * Put a simple element (basic class) onto a dialog template in memory.
7667 * return a pointer to where the next item should be added.
7668 *
7669 * parameters:
7670 * lStyle = additional style flags
7671 * (be careful, NT3.51 & Win32s will ignore the new ones)
7672 * x,y = x & y positions IN DIALOG UNITS
7673 * w,h = width and height IN DIALOG UNITS
7674 * Id = ID used in messages
7675 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7676 * caption = usually text or resource name
7677 *
7678 * TODO: use the length information noted here to enable the dialog creation
7679 * routines to work out more exactly how much memory they need to alloc.
7680 */
7681 static PWORD
7682add_dialog_element(
7683 PWORD p,
7684 DWORD lStyle,
7685 WORD x,
7686 WORD y,
7687 WORD w,
7688 WORD h,
7689 WORD Id,
7690 WORD clss,
7691 const char *caption)
7692{
7693 int nchar;
7694
Bram Moolenaar734a8672019-12-02 22:49:38 +01007695 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7697 *p++ = LOWORD(lStyle);
7698 *p++ = HIWORD(lStyle);
7699 *p++ = 0; // LOWORD (lExtendedStyle)
7700 *p++ = 0; // HIWORD (lExtendedStyle)
7701 *p++ = x;
7702 *p++ = y;
7703 *p++ = w;
7704 *p++ = h;
7705 *p++ = Id; //9 or 10 words in all
7706
7707 *p++ = (WORD)0xffff;
7708 *p++ = clss; //2 more here
7709
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007710 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 p += nchar;
7712
7713 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7714
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007715 return p; // total = 15 + strlen(caption) words
7716 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717}
7718
7719
7720/*
7721 * Helper routine. Take an input pointer, return closest pointer that is
7722 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7723 */
7724 static LPWORD
7725lpwAlign(
7726 LPWORD lpIn)
7727{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007728 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007730 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 ul += 3;
7732 ul >>= 2;
7733 ul <<= 2;
7734 return (LPWORD)ul;
7735}
7736
7737/*
7738 * Helper routine. Takes second parameter as Ansi string, copies it to first
7739 * parameter as wide character (16-bits / char) string, and returns integer
7740 * number of wide characters (words) in string (including the trailing wide
7741 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007742 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7743 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 static int
7745nCopyAnsiToWideChar(
7746 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007747 LPSTR lpAnsiIn,
7748 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
7750 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007751 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 int i;
7753 WCHAR *wn;
7754
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007755 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007757 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007758 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 if (wn != NULL)
7760 {
7761 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007762 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 vim_free(wn);
7764 }
7765 }
7766 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007767 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 nChar = MultiByteToWideChar(
7769 enc_codepage > 0 ? enc_codepage : CP_ACP,
7770 MB_PRECOMPOSED,
7771 lpAnsiIn, len,
7772 lpWCStr, len);
7773 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007774 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
7777 return nChar;
7778}
7779
7780
7781#ifdef FEAT_TEAROFF
7782/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007783 * Lookup menu handle from "menu_id".
7784 */
7785 static HMENU
7786tearoff_lookup_menuhandle(
7787 vimmenu_T *menu,
7788 WORD menu_id)
7789{
7790 for ( ; menu != NULL; menu = menu->next)
7791 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007792 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007793 continue;
7794 if (menu_is_separator(menu->dname))
7795 continue;
7796 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7797 return menu->submenu_id;
7798 }
7799 return NULL;
7800}
7801
7802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 * The callback function for all the modeless dialogs that make up the
7804 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7805 * thinking its menus have been clicked), and go away when closed.
7806 */
7807 static LRESULT CALLBACK
7808tearoff_callback(
7809 HWND hwnd,
7810 UINT message,
7811 WPARAM wParam,
7812 LPARAM lParam)
7813{
7814 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007815 {
7816 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
Bram Moolenaar734a8672019-12-02 22:49:38 +01007820 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 HandleMouseHide(message, lParam);
7822
7823 if (message == WM_COMMAND)
7824 {
7825 if ((WORD)(LOWORD(wParam)) & 0x8000)
7826 {
7827 POINT mp;
7828 RECT rect;
7829
7830 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7831 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007832 vimmenu_T *menu;
7833
7834 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007836 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7838 (int)rect.right - 8,
7839 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007840 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 s_hwnd,
7842 NULL);
7843 /*
7844 * NOTE: The pop-up menu can eat the mouse up event.
7845 * We deal with this in normal.c.
7846 */
7847 }
7848 }
7849 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007850 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7852 /*
7853 * Give main window the focus back: this is so after
7854 * choosing a tearoff button you can start typing again
7855 * straight away.
7856 */
7857 (void)SetFocus(s_hwnd);
7858 return TRUE;
7859 }
7860 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7861 {
7862 DestroyWindow(hwnd);
7863 return TRUE;
7864 }
7865
Bram Moolenaar734a8672019-12-02 22:49:38 +01007866 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 if (message == WM_EXITSIZEMOVE)
7868 (void)SetActiveWindow(s_hwnd);
7869
7870 return FALSE;
7871}
7872#endif
7873
7874
7875/*
K.Takatad1c58992022-01-23 12:31:57 +00007876 * Computes the dialog base units based on the current dialog font.
7877 * We don't use the GetDialogBaseUnits() API, because we don't use the
7878 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 */
7880 static void
7881get_dialog_font_metrics(void)
7882{
7883 HDC hdc;
7884 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 SIZE size;
7886#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007887 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#endif
7889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007891 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007892 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894#endif
7895 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007896 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
K.Takatad1c58992022-01-23 12:31:57 +00007898 hdc = GetDC(s_hwnd);
7899 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007900 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007901 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
K.Takataabe628e2022-01-23 16:25:17 +00007903 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007904 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905}
7906
7907#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7908/*
7909 * Create a pseudo-"tearoff menu" based on the child
7910 * items of a given menu pointer.
7911 */
7912 static void
7913gui_mch_tearoff(
7914 char_u *title,
7915 vimmenu_T *menu,
7916 int initX,
7917 int initY)
7918{
7919 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7920 int template_len;
7921 int nchar, textWidth, submenuWidth;
7922 DWORD lStyle;
7923 DWORD lExtendedStyle;
7924 WORD dlgwidth;
7925 WORD menuID;
7926 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007927 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 vimmenu_T *the_menu = menu;
7929 HWND hwnd;
7930 HDC hdc;
7931 HFONT font, oldFont;
7932 int col, spaceWidth, len;
7933 int columnWidths[2];
7934 char_u *label, *text;
7935 int acLen = 0;
7936 int nameLen;
7937 int padding0, padding1, padding2 = 0;
7938 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007939 int x;
7940 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007941# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007942 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945
7946 /*
7947 * If this menu is already torn off, move it to the mouse position.
7948 */
7949 if (IsWindow(menu->tearoff_handle))
7950 {
7951 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007952 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {
7954 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7955 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7956 }
7957 return;
7958 }
7959
7960 /*
7961 * Create a new tearoff.
7962 */
7963 if (*title == MNU_HIDDEN_CHAR)
7964 title++;
7965
Bram Moolenaar734a8672019-12-02 22:49:38 +01007966 // Allocate memory to store the dialog template. It's made bigger when
7967 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 template_len = DLG_ALLOC_SIZE;
7969 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7970 if (p == NULL)
7971 return;
7972
7973 hwnd = GetDesktopWindow();
7974 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007975# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7977 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007978 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 use_lfSysmenu = TRUE;
7980 }
7981 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007982# endif
K.Takatad1c58992022-01-23 12:31:57 +00007983 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7984 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7985
7986 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987
Bram Moolenaar734a8672019-12-02 22:49:38 +01007988 // Calculate width of a single space. Used for padding columns to the
7989 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007990 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
Bram Moolenaar734a8672019-12-02 22:49:38 +01007992 // Figure out max width of the text column, the accelerator column and the
7993 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 submenuWidth = 0;
7995 for (col = 0; col < 2; col++)
7996 {
7997 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007998 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008000 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 text = (col == 0) ? pmenu->dname : pmenu->actext;
8002 if (text != NULL && *text != NUL)
8003 {
8004 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8005 if (textWidth > columnWidths[col])
8006 columnWidths[col] = textWidth;
8007 }
8008 if (pmenu->children != NULL)
8009 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8010 }
8011 }
8012 if (columnWidths[1] == 0)
8013 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008014 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (submenuWidth != 0)
8016 columnWidths[0] += submenuWidth;
8017 else
8018 columnWidths[0] += spaceWidth;
8019 }
8020 else
8021 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008022 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8024 columnWidths[1] += submenuWidth;
8025 }
8026
8027 /*
8028 * Now find the total width of our 'menu'.
8029 */
8030 textWidth = columnWidths[0] + columnWidths[1];
8031 if (submenuWidth != 0)
8032 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008033 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8035 textWidth += submenuWidth;
8036 }
8037 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8038 if (textWidth > dlgwidth)
8039 dlgwidth = textWidth;
8040 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8041
Bram Moolenaar734a8672019-12-02 22:49:38 +01008042 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00008043 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8046 *p++ = LOWORD(lStyle);
8047 *p++ = HIWORD(lStyle);
8048 *p++ = LOWORD(lExtendedStyle);
8049 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008050 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008052 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008054 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 else
8056 *p++ = PixelToDialogX(initX); // x
8057 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008058 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 else
8060 *p++ = PixelToDialogY(initY); // y
8061 *p++ = PixelToDialogX(dlgwidth); // cx
8062 ptrueheight = p;
8063 *p++ = 0; // dialog height: changed later anyway
8064 *p++ = 0; // Menu
8065 *p++ = 0; // Class
8066
Bram Moolenaar734a8672019-12-02 22:49:38 +01008067 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008069 ? (LPSTR)title
8070 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 p += nchar;
8072
K.Takatad1c58992022-01-23 12:31:57 +00008073 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008074# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00008075 if (use_lfSysmenu)
8076 {
8077 // point size
8078 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8079 GetDeviceCaps(hdc, LOGPIXELSY));
8080 wcscpy(p, lfSysmenu.lfFaceName);
8081 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 }
K.Takatad1c58992022-01-23 12:31:57 +00008083 else
8084# endif
8085 {
8086 *p++ = DLG_FONT_POINT_SIZE; // point size
8087 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8088 }
8089 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
8091 /*
8092 * Loop over all the items in the menu.
8093 * But skip over the tearbar.
8094 */
8095 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8096 menu = menu->children->next;
8097 else
8098 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008099 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 for ( ; menu != NULL; menu = menu->next)
8101 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008102 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 continue;
8104 if (menu_is_separator(menu->dname))
8105 {
8106 sepPadding += 3;
8107 continue;
8108 }
8109
Bram Moolenaar734a8672019-12-02 22:49:38 +01008110 // Check if there still is plenty of room in the template. Make it
8111 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8113 {
8114 WORD *newp;
8115
8116 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8117 if (newp != NULL)
8118 {
8119 template_len += 4096;
8120 mch_memmove(newp, pdlgtemplate,
8121 (char *)p - (char *)pdlgtemplate);
8122 p = newp + (p - pdlgtemplate);
8123 pnumitems = newp + (pnumitems - pdlgtemplate);
8124 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8125 LocalFree(LocalHandle(pdlgtemplate));
8126 pdlgtemplate = newp;
8127 }
8128 }
8129
Bram Moolenaar734a8672019-12-02 22:49:38 +01008130 // Figure out minimal length of this menu label. Use "name" for the
8131 // actual text, "dname" for estimating the displayed size. "name"
8132 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 len = nameLen = (int)STRLEN(menu->name);
8134 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8135 (int)STRLEN(menu->dname))) / spaceWidth;
8136 len += padding0;
8137
8138 if (menu->actext != NULL)
8139 {
8140 acLen = (int)STRLEN(menu->actext);
8141 len += acLen;
8142 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8143 }
8144 else
8145 textWidth = 0;
8146 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8147 len += padding1;
8148
8149 if (menu->children == NULL)
8150 {
8151 padding2 = submenuWidth / spaceWidth;
8152 len += padding2;
8153 menuID = (WORD)(menu->id);
8154 }
8155 else
8156 {
8157 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008158 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 }
8160
Bram Moolenaar734a8672019-12-02 22:49:38 +01008161 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008162 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 if (label == NULL)
8164 break;
8165
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008166 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008167 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008169 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 while (padding0-- > 0)
8171 *text++ = ' ';
8172 if (menu->actext != NULL)
8173 {
8174 STRNCPY(text, menu->actext, acLen);
8175 text += acLen;
8176 }
8177 while (padding1-- > 0)
8178 *text++ = ' ';
8179 if (menu->children != NULL)
8180 {
8181 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8182 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8183 }
8184 else
8185 {
8186 while (padding2-- > 0)
8187 *text++ = ' ';
8188 }
8189 *text = NUL;
8190
8191 /*
8192 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8193 * W95/NT4 it makes the tear-off look more like a menu.
8194 */
8195 p = add_dialog_element(p,
8196 BS_PUSHBUTTON|BS_LEFT,
8197 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8198 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8199 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8200 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008201 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 vim_free(label);
8203 (*pnumitems)++;
8204 }
8205
8206 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8207
8208
Bram Moolenaar734a8672019-12-02 22:49:38 +01008209 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008210 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008211 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 (LPDLGTEMPLATE)pdlgtemplate,
8213 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008214 (DLGPROC)tearoff_callback,
8215 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216
8217 LocalFree(LocalHandle(pdlgtemplate));
8218 SelectFont(hdc, oldFont);
8219 DeleteObject(font);
8220 ReleaseDC(hwnd, hdc);
8221
8222 /*
8223 * Reassert ourselves as the active window. This is so that after creating
8224 * a tearoff, the user doesn't have to click with the mouse just to start
8225 * typing again!
8226 */
8227 (void)SetActiveWindow(s_hwnd);
8228
Bram Moolenaar734a8672019-12-02 22:49:38 +01008229 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 force_menu_update = TRUE;
8231}
8232#endif
8233
8234#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008235# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237/*
8238 * Create the toolbar, initially unpopulated.
8239 * (just like the menu, there are no defaults, it's all
8240 * set up through menu.vim)
8241 */
8242 static void
8243initialise_toolbar(void)
8244{
8245 InitCommonControls();
8246 s_toolbarhwnd = CreateToolbarEx(
8247 s_hwnd,
8248 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8249 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008250 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008251 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 IDR_TOOLBAR1, // id of initial bitmap
8253 NULL,
8254 0, // initial number of buttons
8255 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8256 TOOLBAR_BUTTON_HEIGHT,
8257 TOOLBAR_BUTTON_WIDTH,
8258 TOOLBAR_BUTTON_HEIGHT,
8259 sizeof(TBBUTTON)
8260 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008261
8262 // Remove transparency from the toolbar to prevent the main window
8263 // background colour showing through
8264 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8265 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8266
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008267 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008270
8271 update_toolbar_size();
8272}
8273
8274 static void
8275update_toolbar_size(void)
8276{
8277 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008278 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00008279
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008280 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00008281 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8282 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8283 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8284 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8285
8286 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8287 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8288 //TRACE("button size: %d, %d", w, h);
8289 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8290 gui.toolbar_height = h + 6;
8291
8292 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8293 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8294
8295 // TODO:
8296 // Currently, this function only updates the size of toolbar buttons.
8297 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
8299
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008300 static LRESULT CALLBACK
8301toolbar_wndproc(
8302 HWND hwnd,
8303 UINT uMsg,
8304 WPARAM wParam,
8305 LPARAM lParam)
8306{
8307 HandleMouseHide(uMsg, lParam);
8308 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8309}
8310
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 static int
8312get_toolbar_bitmap(vimmenu_T *menu)
8313{
8314 int i = -1;
8315
8316 /*
8317 * Check user bitmaps first, unless builtin is specified.
8318 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008319 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {
8321 char_u fname[MAXPATHL];
8322 HANDLE hbitmap = NULL;
8323
8324 if (menu->iconfile != NULL)
8325 {
8326 gui_find_iconfile(menu->iconfile, fname, "bmp");
8327 hbitmap = LoadImage(
8328 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008329 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 IMAGE_BITMAP,
8331 TOOLBAR_BUTTON_WIDTH,
8332 TOOLBAR_BUTTON_HEIGHT,
8333 LR_LOADFROMFILE |
8334 LR_LOADMAP3DCOLORS
8335 );
8336 }
8337
8338 /*
8339 * If the LoadImage call failed, or the "icon=" file
8340 * didn't exist or wasn't specified, try the menu name
8341 */
8342 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008343 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008344# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008345 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008346# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008347 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 hbitmap = LoadImage(
8349 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008350 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 IMAGE_BITMAP,
8352 TOOLBAR_BUTTON_WIDTH,
8353 TOOLBAR_BUTTON_HEIGHT,
8354 LR_LOADFROMFILE |
8355 LR_LOADMAP3DCOLORS
8356 );
8357
8358 if (hbitmap != NULL)
8359 {
8360 TBADDBITMAP tbAddBitmap;
8361
8362 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008363 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
8365 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8366 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008367 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 }
8369 }
8370 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8371 i = menu->iconidx;
8372
8373 return i;
8374}
8375#endif
8376
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008377#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8378 static void
8379initialise_tabline(void)
8380{
8381 InitCommonControls();
8382
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008384 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008385 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008386 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008387 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008388
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008389 gui.tabline_height = TABLINE_HEIGHT;
8390
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008391 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008392}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008393
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008394/*
8395 * Get tabpage_T from POINT.
8396 */
8397 static tabpage_T *
8398GetTabFromPoint(
8399 HWND hWnd,
8400 POINT pt)
8401{
8402 tabpage_T *ptp = NULL;
8403
8404 if (gui_mch_showing_tabline())
8405 {
8406 TCHITTESTINFO htinfo;
8407 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008408 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008409 if (s_tabhwnd == hWnd)
8410 {
8411 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8412 if (idx != -1)
8413 ptp = find_tabpage(idx + 1);
8414 }
8415 }
8416 return ptp;
8417}
8418
8419static POINT s_pt = {0, 0};
8420static HCURSOR s_hCursor = NULL;
8421
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008422 static LRESULT CALLBACK
8423tabline_wndproc(
8424 HWND hwnd,
8425 UINT uMsg,
8426 WPARAM wParam,
8427 LPARAM lParam)
8428{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008429 POINT pt;
8430 tabpage_T *tp;
8431 RECT rect;
8432 int nCenter;
8433 int idx0;
8434 int idx1;
8435
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008436 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008437
8438 switch (uMsg)
8439 {
8440 case WM_LBUTTONDOWN:
8441 {
8442 s_pt.x = GET_X_LPARAM(lParam);
8443 s_pt.y = GET_Y_LPARAM(lParam);
8444 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008445 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008446 break;
8447 }
8448 case WM_MOUSEMOVE:
8449 if (GetCapture() == hwnd
8450 && ((wParam & MK_LBUTTON)) != 0)
8451 {
8452 pt.x = GET_X_LPARAM(lParam);
8453 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008454 if (abs(pt.x - s_pt.x) >
8455 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008456 {
8457 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8458
8459 tp = GetTabFromPoint(hwnd, pt);
8460 if (tp != NULL)
8461 {
8462 idx0 = tabpage_index(curtab) - 1;
8463 idx1 = tabpage_index(tp) - 1;
8464
8465 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8466 nCenter = rect.left + (rect.right - rect.left) / 2;
8467
Bram Moolenaar734a8672019-12-02 22:49:38 +01008468 // Check if the mouse cursor goes over the center of
8469 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008470 if ((idx0 < idx1) && (nCenter < pt.x))
8471 {
8472 tabpage_move(idx1 + 1);
8473 update_screen(0);
8474 }
8475 else if ((idx1 < idx0) && (pt.x < nCenter))
8476 {
8477 tabpage_move(idx1);
8478 update_screen(0);
8479 }
8480 }
8481 }
8482 }
8483 break;
8484 case WM_LBUTTONUP:
8485 {
8486 if (GetCapture() == hwnd)
8487 {
8488 SetCursor(s_hCursor);
8489 ReleaseCapture();
8490 }
8491 break;
8492 }
regomne3bdef102022-09-26 20:48:32 +01008493 case WM_MBUTTONUP:
8494 {
8495 TCHITTESTINFO htinfo;
8496
8497 htinfo.pt.x = GET_X_LPARAM(lParam);
8498 htinfo.pt.y = GET_Y_LPARAM(lParam);
8499 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8500 if (idx0 != -1)
8501 {
8502 idx0 += 1;
8503 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8504 }
8505 break;
8506 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008507 default:
8508 break;
8509 }
8510
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008511 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8512}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008513#endif
8514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8516/*
8517 * Make the GUI window come to the foreground.
8518 */
8519 void
8520gui_mch_set_foreground(void)
8521{
8522 if (IsIconic(s_hwnd))
8523 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8524 SetForegroundWindow(s_hwnd);
8525}
8526#endif
8527
8528#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8529 static void
8530dyn_imm_load(void)
8531{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008532 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (hLibImm == NULL)
8534 return;
8535
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008537 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008539 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008541 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008543 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008545 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008547 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008548 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008549 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008550 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008551 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008553 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008555 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008556 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008557 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
K.Takatab0b2b732022-01-19 12:59:21 +00008559 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 || pImmGetContext == NULL
8561 || pImmAssociateContext == NULL
8562 || pImmReleaseContext == NULL
8563 || pImmGetOpenStatus == NULL
8564 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008565 || pImmGetCompositionFontW == NULL
8566 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008568 || pImmGetConversionStatus == NULL
8569 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {
8571 FreeLibrary(hLibImm);
8572 hLibImm = NULL;
8573 pImmGetContext = NULL;
8574 return;
8575 }
8576
8577 return;
8578}
8579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580#endif
8581
8582#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8583
8584# ifdef FEAT_XPM_W32
8585# define IMAGE_XPM 100
8586# endif
8587
8588typedef struct _signicon_t
8589{
8590 HANDLE hImage;
8591 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008592# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008593 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595} signicon_t;
8596
8597 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008598gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599{
8600 signicon_t *sign;
8601 int x, y, w, h;
8602
8603 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8604 return;
8605
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008606# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008607 if (IS_ENABLE_DIRECTX())
8608 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008609# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 x = TEXT_X(col);
8612 y = TEXT_Y(row);
8613 w = gui.char_width * 2;
8614 h = gui.char_height;
8615 switch (sign->uType)
8616 {
8617 case IMAGE_BITMAP:
8618 {
8619 HDC hdcMem;
8620 HBITMAP hbmpOld;
8621
8622 hdcMem = CreateCompatibleDC(s_hdc);
8623 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8624 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8625 SelectObject(hdcMem, hbmpOld);
8626 DeleteDC(hdcMem);
8627 }
8628 break;
8629 case IMAGE_ICON:
8630 case IMAGE_CURSOR:
8631 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8632 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008633# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 case IMAGE_XPM:
8635 {
8636 HDC hdcMem;
8637 HBITMAP hbmpOld;
8638
8639 hdcMem = CreateCompatibleDC(s_hdc);
8640 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008641 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8643
8644 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008645 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8647 SelectObject(hdcMem, hbmpOld);
8648 DeleteDC(hdcMem);
8649 }
8650 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653}
8654
8655 static void
8656close_signicon_image(signicon_t *sign)
8657{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008658 if (sign == NULL)
8659 return;
8660
8661 switch (sign->uType)
8662 {
8663 case IMAGE_BITMAP:
8664 DeleteObject((HGDIOBJ)sign->hImage);
8665 break;
8666 case IMAGE_CURSOR:
8667 DestroyCursor((HCURSOR)sign->hImage);
8668 break;
8669 case IMAGE_ICON:
8670 DestroyIcon((HICON)sign->hImage);
8671 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008672# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008673 case IMAGE_XPM:
8674 DeleteObject((HBITMAP)sign->hImage);
8675 DeleteObject((HBITMAP)sign->hShape);
8676 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008677# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679}
8680
8681 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008682gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683{
8684 signicon_t sign, *psign;
8685 char_u *ext;
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008688 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (ext > signfile)
8690 {
8691 int do_load = 1;
8692
8693 if (!STRICMP(ext, ".bmp"))
8694 sign.uType = IMAGE_BITMAP;
8695 else if (!STRICMP(ext, ".ico"))
8696 sign.uType = IMAGE_ICON;
8697 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8698 sign.uType = IMAGE_CURSOR;
8699 else
8700 do_load = 0;
8701
8702 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008703 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 gui.char_width * 2, gui.char_height,
8705 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008706# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 if (!STRICMP(ext, ".xpm"))
8708 {
8709 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008710 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8711 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
8715
8716 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008717 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 *psign = sign;
8719
8720 if (!psign)
8721 {
8722 if (sign.hImage)
8723 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008724 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 }
8726 return (void *)psign;
8727
8728}
8729
8730 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008731gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008733 if (sign == NULL)
8734 return;
8735
8736 close_signicon_image((signicon_t *)sign);
8737 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008741#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742
Bram Moolenaar734a8672019-12-02 22:49:38 +01008743/*
8744 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008745 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008747 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8749 * to get current mouse position).
8750 *
8751 * Trying to use as more Windows services as possible, and as less
8752 * IE version as possible :)).
8753 *
8754 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8755 * BalloonEval struct.
8756 * 2) Enable/Disable simply create/kill BalloonEval Timer
8757 * 3) When there was enough inactivity, timer procedure posts
8758 * async request to debugger
8759 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8760 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008761 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 */
8763
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008764 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008765make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008766{
K.Takata76687d22022-01-25 10:31:37 +00008767 TOOLINFOW *pti;
8768 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008769
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008770 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008771 if (pti == NULL)
8772 return;
8773
8774 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8775 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8776 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008777 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008778
8779 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8780 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8781
K.Takata76687d22022-01-25 10:31:37 +00008782 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008783 pti->uFlags = TTF_SUBCLASS;
8784 pti->hwnd = beval->target;
8785 pti->hinst = 0; // Don't use string resources
8786 pti->uId = ID_BEVAL_TOOLTIP;
8787
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008788 pti->lpszText = LPSTR_TEXTCALLBACKW;
8789 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8790 pti->lParam = (LPARAM)beval->tofree;
8791 // switch multiline tooltips on
8792 if (GetClientRect(s_textArea, &rect))
8793 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8794 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008795
8796 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8797 pti->rect.left = pt.x - 3;
8798 pti->rect.top = pt.y - 3;
8799 pti->rect.right = pt.x + 3;
8800 pti->rect.bottom = pt.y + 3;
8801
8802 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8803 // Make tooltip appear sooner.
8804 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8805 // I've performed some tests and it seems the longest possible life time
8806 // of tooltip is 30 seconds.
8807 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8808 /*
8809 * HACK: force tooltip to appear, because it'll not appear until
8810 * first mouse move. D*mn M$
8811 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8812 */
8813 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8814 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8815 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008816}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008819delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008821 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822}
8823
8824 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008825beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008826 HWND hwnd UNUSED,
8827 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008828 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008829 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 POINT pt;
8832 RECT rect;
8833
8834 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8835 return;
8836
8837 GetCursorPos(&pt);
8838 if (WindowFromPoint(pt) != s_textArea)
8839 return;
8840
8841 ScreenToClient(s_textArea, &pt);
8842 GetClientRect(s_textArea, &rect);
8843 if (!PtInRect(&rect, pt))
8844 return;
8845
K.Takataa8ec4912022-02-03 14:32:33 +00008846 if (last_user_activity > 0
8847 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 && (cur_beval->showState != ShS_PENDING
8849 || abs(cur_beval->x - pt.x) > 3
8850 || abs(cur_beval->y - pt.y) > 3))
8851 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008852 // Pointer resting in one place long enough, it's time to show
8853 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 cur_beval->showState = ShS_PENDING;
8855 cur_beval->x = pt.x;
8856 cur_beval->y = pt.y;
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (cur_beval->msgCB != NULL)
8859 (*cur_beval->msgCB)(cur_beval, 0);
8860 }
8861}
8862
8863 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008864gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
K.Takataa8ec4912022-02-03 14:32:33 +00008866 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867}
8868
8869 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008870gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 if (beval == NULL)
8873 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008874 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8875 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876}
8877
8878 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008879gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880{
8881 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008882
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008883 vim_free(beval->msg);
8884 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8885 if (beval->msg == NULL)
8886 {
8887 delete_tooltip(beval);
8888 beval->showState = ShS_NEUTRAL;
8889 return;
8890 }
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 if (beval->showState == ShS_SHOWING)
8893 return;
8894 GetCursorPos(&pt);
8895 ScreenToClient(s_textArea, &pt);
8896
8897 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008899 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 gui_mch_disable_beval_area(cur_beval);
8901 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008902 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904}
8905
8906 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008907gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008908 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008909 char_u *mesg,
8910 void (*mesgCB)(BalloonEval *, int),
8911 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008913 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 BalloonEval *beval;
8915
8916 if (mesg != NULL && mesgCB != NULL)
8917 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008918 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 return NULL;
8920 }
8921
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008922 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 if (beval != NULL)
8924 {
8925 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 beval->msg = mesg;
8929 beval->msgCB = mesgCB;
8930 beval->clientData = clientData;
8931
8932 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 cur_beval = beval;
8934
8935 if (p_beval)
8936 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 }
8938 return beval;
8939}
8940
8941 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008942Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008944 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 return;
8946
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008947 if (cur_beval == NULL)
8948 return;
8949
8950 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008952 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008953 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008954 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 delete_tooltip(cur_beval);
8956 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
8958 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008959 break;
8960 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008961 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008962 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008963 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008964 info->lpszText = (LPSTR)info->lParam;
8965 info->uFlags |= TTF_DI_SETITEM;
8966 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008967 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008968 case TTN_GETDISPINFOW:
8969 {
8970 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008971 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008972 info->lpszText = (LPWSTR)info->lParam;
8973 info->uFlags |= TTF_DI_SETITEM;
8974 }
8975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
8977}
8978
8979 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008980track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8983 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008984 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985}
8986
8987 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008988gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008990# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008991 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008992# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008993 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 vim_free(beval);
8995}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008996#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
8998#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8999/*
9000 * We have multiple signs to draw at the same location. Draw the
9001 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9002 */
9003 void
9004netbeans_draw_multisign_indicator(int row)
9005{
9006 int i;
9007 int y;
9008 int x;
9009
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009010 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009011 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009012
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 x = 0;
9014 y = TEXT_Y(row);
9015
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009016# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009017 if (IS_ENABLE_DIRECTX())
9018 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009019# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 for (i = 0; i < gui.char_height - 3; i++)
9022 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9023
9024 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9025 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9026 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9027 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9028 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9029 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9030 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9031}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009032#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009033
9034#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009035
Christopher Plewright20b795e2022-12-20 20:01:58 +00009036// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9037// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00009038// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00009039 static int
9040test_gui_w32_sendevent_mouse(dict_T *args)
9041{
9042 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009043 return FALSE;
9044
Christopher Plewright20b795e2022-12-20 20:01:58 +00009045 // Note: "move" is optional, requires fewer arguments
9046 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009047
Christopher Plewright20b795e2022-12-20 20:01:58 +00009048 if (!move && (!dict_has_key(args, "button")
9049 || !dict_has_key(args, "multiclick")
9050 || !dict_has_key(args, "modifiers")))
9051 return FALSE;
9052
9053 int row = (int)dict_get_number(args, "row");
9054 int col = (int)dict_get_number(args, "col");
9055
9056 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009057 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00009058 // the "move" argument expects row and col coordnates to be in pixels,
9059 // unless "cell" is specified and is TRUE.
9060 if (dict_get_bool(args, "cell", FALSE))
9061 {
9062 // calculate the middle of the character cell
9063 // Note: Cell coordinates are 1-based from vimscript
9064 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9065 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9066 gui_mouse_moved(pX, pY);
9067 }
9068 else
9069 gui_mouse_moved(col, row);
9070 }
9071 else
9072 {
9073 int button = (int)dict_get_number(args, "button");
9074 int repeated_click = (int)dict_get_number(args, "multiclick");
9075 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009076
Christopher Plewright20b795e2022-12-20 20:01:58 +00009077 // Reset the scroll values to known values.
9078 // XXX: Remove this when/if the scroll step is made configurable.
9079 mouse_set_hor_scroll_step(6);
9080 mouse_set_vert_scroll_step(3);
9081
9082 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9083 repeated_click, mods);
9084 }
9085 return TRUE;
9086}
9087
9088 static int
9089test_gui_w32_sendevent_keyboard(dict_T *args)
9090{
9091 INPUT inputs[1];
9092 INPUT modkeys[3];
9093 SecureZeroMemory(inputs, sizeof(INPUT));
9094 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9095
9096 char_u *event = dict_get_string(args, "event", TRUE);
9097
9098 if (event && (STRICMP(event, "keydown") == 0
9099 || STRICMP(event, "keyup") == 0))
9100 {
9101 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009102 if (vkCode <= 0 || vkCode >= 0xFF)
9103 {
9104 semsg(_(e_invalid_argument_nr), (long)vkCode);
9105 return FALSE;
9106 }
9107
Christopher Plewright20b795e2022-12-20 20:01:58 +00009108 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9109 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9110 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9111 || vkCode == VK_LMENU || vkCode == VK_RMENU );
9112
9113 BOOL unwrapMods = FALSE;
9114 int mods = (int)dict_get_number(args, "modifiers");
9115
9116 // If there are modifiers in the args, and it is not a keyup event and
9117 // vkCode is not a modifier key, then we generate virtual modifier key
9118 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009119 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009120 {
9121 int n = 0;
9122 if (mods & MOD_MASK_SHIFT)
9123 {
9124 modkeys[n].type = INPUT_KEYBOARD;
9125 modkeys[n].ki.wVk = VK_LSHIFT;
9126 n++;
9127 }
9128 if (mods & MOD_MASK_CTRL)
9129 {
9130 modkeys[n].type = INPUT_KEYBOARD;
9131 modkeys[n].ki.wVk = VK_LCONTROL;
9132 n++;
9133 }
9134 if (mods & MOD_MASK_ALT)
9135 {
9136 modkeys[n].type = INPUT_KEYBOARD;
9137 modkeys[n].ki.wVk = VK_LMENU;
9138 n++;
9139 }
9140 if (n)
9141 {
9142 (void)SetForegroundWindow(s_hwnd);
9143 SendInput(n, modkeys, sizeof(INPUT));
9144 }
9145 }
9146
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009147 inputs[0].type = INPUT_KEYBOARD;
9148 inputs[0].ki.wVk = vkCode;
9149 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009150 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009151 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009152 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009153 unwrapMods = TRUE;
9154 }
9155
K.Takatafef38d82022-09-07 19:03:42 +01009156 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009157 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00009158 vim_free(event);
9159
9160 if (unwrapMods)
9161 {
9162 modkeys[0].type = INPUT_KEYBOARD;
9163 modkeys[0].ki.wVk = VK_LSHIFT;
9164 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9165
9166 modkeys[1].type = INPUT_KEYBOARD;
9167 modkeys[1].ki.wVk = VK_LCONTROL;
9168 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9169
9170 modkeys[2].type = INPUT_KEYBOARD;
9171 modkeys[2].ki.wVk = VK_LMENU;
9172 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9173
9174 (void)SetForegroundWindow(s_hwnd);
9175 SendInput(3, modkeys, sizeof(INPUT));
9176 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009177 }
9178 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00009179 {
9180 if (event == NULL)
9181 {
9182 semsg(_(e_missing_argument_str), "event");
9183 }
9184 else
9185 {
9186 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9187 vim_free(event);
9188 }
9189 return FALSE;
9190 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009191 return TRUE;
9192}
Christopher Plewright20b795e2022-12-20 20:01:58 +00009193
Anton Sharonov68d94722024-01-23 23:19:02 +01009194 static int
9195test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9196{
9197 int handled = 0;
9198 char_u *strategy = dict_get_string(args, "strategy", TRUE);
9199
9200 if (strategy)
9201 {
9202 if (STRICMP(strategy, "classic") == 0)
9203 {
9204 handled = 1;
9205 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9206 }
9207 else if (STRICMP(strategy, "experimental") == 0)
9208 {
9209 handled = 1;
9210 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9211 }
9212 }
9213
9214 if (!handled)
9215 {
9216 if (strategy == NULL)
9217 {
9218 semsg(_(e_missing_argument_str), "strategy");
9219 }
9220 else
9221 {
9222 semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9223 vim_free(strategy);
9224 }
9225 return FALSE;
9226 }
9227 return TRUE;
9228}
9229
9230
Christopher Plewright20b795e2022-12-20 20:01:58 +00009231 int
9232test_gui_w32_sendevent(char_u *event, dict_T *args)
9233{
9234 if (STRICMP(event, "key") == 0)
9235 return test_gui_w32_sendevent_keyboard(args);
9236 else if (STRICMP(event, "mouse") == 0)
9237 return test_gui_w32_sendevent_mouse(args);
Anton Sharonov68d94722024-01-23 23:19:02 +01009238 else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9239 return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
Christopher Plewright20b795e2022-12-20 20:01:58 +00009240 else
9241 {
9242 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9243 return FALSE;
9244 }
9245}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009246#endif