blob: 721c4480d52fc5e82906212c4fcb5fdcd198fea6 [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
5525 {
5526 int bDoRestart = FALSE;
5527
5528 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005529 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (bDoRestart)
5531 mch_exit(0);
5532 }
5533#endif
5534
5535#ifdef FEAT_NETBEANS_INTG
5536 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005537 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 int arg;
5539
5540 for (arg = 1; arg < *argc; arg++)
5541 if (strncmp("-nb", argv[arg], 3) == 0)
5542 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 netbeansArg = argv[arg];
5544 mch_memmove(&argv[arg], &argv[arg + 1],
5545 (--*argc - arg) * sizeof(char *));
5546 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551}
5552
K.Takatac81e9bf2022-01-16 14:15:49 +00005553 static void
5554load_dpi_func(void)
5555{
5556 HMODULE hUser32;
5557
5558 hUser32 = GetModuleHandle("user32.dll");
5559 if (hUser32 == NULL)
5560 goto fail;
5561
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005562 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5563 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5564 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005565 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005566 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5567 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005568
5569 if (pSetThreadDpiAwarenessContext != NULL)
5570 {
5571 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5572 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5573 if (oldctx != NULL)
5574 {
5575 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5576 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5577#ifdef DEBUG
5578 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5579 {
5580 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5581 }
5582#endif
5583 return;
5584 }
5585 }
5586
5587fail:
5588 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005589 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005590 pGetDpiForWindow = NULL;
5591 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5592 pSetThreadDpiAwarenessContext = NULL;
5593 pGetAwarenessFromDpiAwarenessContext = NULL;
5594}
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596/*
5597 * Initialise the GUI. Create all the windows, set up all the call-backs
5598 * etc.
5599 */
5600 int
5601gui_mch_init(void)
5602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005604 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaar734a8672019-12-02 22:49:38 +01005607 // Return here if the window was already opened (happens when
5608 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005610 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 /*
5613 * Load the tearoff bitmap
5614 */
5615#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005616 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617#endif
5618
K.Takatac81e9bf2022-01-16 14:15:49 +00005619 load_dpi_func();
5620
5621 s_dpi = pGetDpiForSystem();
5622 update_scrollbar_size();
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005625 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626#endif
5627 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005628#ifdef FEAT_TOOLBAR
5629 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5633
Bram Moolenaar734a8672019-12-02 22:49:38 +01005634 // First try using the wide version, so that we can use any title.
5635 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005636 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005638 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 wndclassw.lpfnWndProc = _WndProc;
5640 wndclassw.cbClsExtra = 0;
5641 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005642 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5644 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5645 wndclassw.hbrBackground = s_brush;
5646 wndclassw.lpszMenuName = NULL;
5647 wndclassw.lpszClassName = szVimWndClassW;
5648
K.Takata4ac893f2022-01-20 12:44:28 +00005649 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (vim_parent_hwnd != NULL)
5654 {
5655#ifdef HAVE_TRY_EXCEPT
5656 __try
5657 {
5658#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005659 // Open inside the specified parent window.
5660 // TODO: last argument should point to a CLIENTCREATESTRUCT
5661 // structure.
5662 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005664 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005665 WS_OVERLAPPEDWINDOW | WS_CHILD
5666 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5668 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005669 100, // Any value will do
5670 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005672 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673#ifdef HAVE_TRY_EXCEPT
5674 }
5675 __except(EXCEPTION_EXECUTE_HANDLER)
5676 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005677 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679#endif
5680 if (s_hwnd == NULL)
5681 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005682 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 mch_exit(2);
5684 }
5685 }
5686 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005687 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005688 // If the provided windowid is not valid reset it to zero, so that it
5689 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005690 if (IsWindow((HWND)win_socket_id) <= 0)
5691 win_socket_id = 0;
5692
Bram Moolenaar734a8672019-12-02 22:49:38 +01005693 // Create a window. If win_socket_id is not zero without border and
5694 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005695 s_hwnd = CreateWindowW(
5696 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005697 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5698 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005699 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5700 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005701 100, // Any value will do
5702 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005703 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005704 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005705 if (s_hwnd != NULL && win_socket_id != 0)
5706 {
5707 SetParent(s_hwnd, (HWND)win_socket_id);
5708 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5709 }
5710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 if (s_hwnd == NULL)
5713 return FAIL;
5714
K.Takatac81e9bf2022-01-16 14:15:49 +00005715 if (pGetDpiForWindow != NULL)
5716 {
5717 s_dpi = pGetDpiForWindow(s_hwnd);
5718 update_scrollbar_size();
5719 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5720 }
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5723 dyn_imm_load();
5724#endif
5725
Bram Moolenaar734a8672019-12-02 22:49:38 +01005726 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005727 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005728 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005729 wndclassw.style = CS_OWNDC;
5730 wndclassw.lpfnWndProc = _TextAreaWndProc;
5731 wndclassw.cbClsExtra = 0;
5732 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005733 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005734 wndclassw.hIcon = NULL;
5735 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5736 wndclassw.hbrBackground = NULL;
5737 wndclassw.lpszMenuName = NULL;
5738 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005739
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005740 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return FAIL;
5742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005744 s_textArea = CreateWindowExW(
5745 0,
5746 szTextAreaClassW, L"Vim text area",
5747 WS_CHILD | WS_VISIBLE, 0, 0,
5748 100, // Any value will do for now
5749 100, // Any value will do for now
5750 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005751 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 if (s_textArea == NULL)
5754 return FAIL;
5755
Bram Moolenaar20321902016-02-17 12:30:17 +01005756#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005757 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005758 {
5759 HANDLE hIcon = NULL;
5760
5761 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005762 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005763 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005764#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005765
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766#ifdef FEAT_MENU
5767 s_menuBar = CreateMenu();
5768#endif
5769 s_hdc = GetDC(s_textArea);
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar734a8672019-12-02 22:49:38 +01005773 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005774 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
Bram Moolenaar734a8672019-12-02 22:49:38 +01005776 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 gui_mch_def_colors();
5778
Bram Moolenaar734a8672019-12-02 22:49:38 +01005779 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5780 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 set_normal_colors();
5782
5783 /*
5784 * Check that none of the colors are the same as the background color.
5785 * Then store the current values as the defaults.
5786 */
5787 gui_check_colors();
5788 gui.def_norm_pixel = gui.norm_pixel;
5789 gui.def_back_pixel = gui.back_pixel;
5790
Bram Moolenaar734a8672019-12-02 22:49:38 +01005791 // Get the colors for the highlight groups (gui_check_colors() might have
5792 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 highlight_gui_started();
5794
5795 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005796 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005798 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 /*
5801 * Set up for Intellimouse processing
5802 */
5803 init_mouse_wheel();
5804
5805 /*
5806 * compute a couple of metrics used for the dialogs
5807 */
5808 get_dialog_font_metrics();
5809#ifdef FEAT_TOOLBAR
5810 /*
5811 * Create the toolbar
5812 */
5813 initialise_toolbar();
5814#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005815#ifdef FEAT_GUI_TABLINE
5816 /*
5817 * Create the tabline
5818 */
5819 initialise_tabline();
5820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#ifdef MSWIN_FIND_REPLACE
5822 /*
5823 * Initialise the dialog box stuff
5824 */
5825 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5826
Bram Moolenaar734a8672019-12-02 22:49:38 +01005827 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005829 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005831 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5833 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5834 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005837#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005838 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005839 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005840#endif
5841
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005842#ifdef FEAT_RENDER_OPTIONS
5843 if (p_rop)
5844 (void)gui_mch_set_rendering_options(p_rop);
5845#endif
5846
Bram Moolenaar748bf032005-02-02 23:04:36 +00005847theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005848 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005849 display_errors();
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 return OK;
5852}
5853
5854/*
5855 * Get the size of the screen, taking position on multiple monitors into
5856 * account (if supported).
5857 */
5858 static void
5859get_work_area(RECT *spi_rect)
5860{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005861 HMONITOR mon;
5862 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar734a8672019-12-02 22:49:38 +01005864 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005865 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005866 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005868 moninfo.cbSize = sizeof(MONITORINFO);
5869 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005871 *spi_rect = moninfo.rcWork;
5872 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
5874 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005875 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5877}
5878
5879/*
5880 * Set the size of the window to the given width and height in pixels.
5881 */
5882 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005883gui_mch_set_shellsize(
5884 int width,
5885 int height,
5886 int min_width UNUSED,
5887 int min_height UNUSED,
5888 int base_width UNUSED,
5889 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005890 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005893 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895
Bram Moolenaar734a8672019-12-02 22:49:38 +01005896 // Try to keep window completely on screen.
5897 // Get position of the screen work area. This is the part that is not
5898 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 get_work_area(&workarea_rect);
5900
Bram Moolenaar734a8672019-12-02 22:49:38 +01005901 // Resizing a maximized window looks very strange, unzoom it first.
5902 // But don't do it when still starting up, it may have been requested in
5903 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005904 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005906
Ken Takata7086b3e2023-10-02 21:26:03 +02005907 if (s_in_dpichanged)
5908 // Use the suggested position when in WM_DPICHANGED.
5909 window_rect = s_suggested_rect;
5910 else
5911 // Use current position.
5912 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005915 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5916 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5917 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5918 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5919 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5920 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921
Bram Moolenaar734a8672019-12-02 22:49:38 +01005922 // The following should take care of keeping Vim on the same monitor, no
5923 // matter if the secondary monitor is left or right of the primary
5924 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005925 window_rect.right = window_rect.left + win_width;
5926 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005927
Bram Moolenaar734a8672019-12-02 22:49:38 +01005928 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005929 // Don't adjust the position when in WM_DPICHANGED.
5930 if (!s_in_dpichanged)
5931 {
5932 if ((direction & RESIZE_HOR)
5933 && window_rect.right > workarea_rect.right)
5934 OffsetRect(&window_rect,
5935 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936
Ken Takata7086b3e2023-10-02 21:26:03 +02005937 if ((direction & RESIZE_HOR)
5938 && window_rect.left < workarea_rect.left)
5939 OffsetRect(&window_rect,
5940 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
Ken Takata7086b3e2023-10-02 21:26:03 +02005942 if ((direction & RESIZE_VERT)
5943 && window_rect.bottom > workarea_rect.bottom)
5944 OffsetRect(&window_rect,
5945 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
Ken Takata7086b3e2023-10-02 21:26:03 +02005947 if ((direction & RESIZE_VERT)
5948 && window_rect.top < workarea_rect.top)
5949 OffsetRect(&window_rect,
5950 0, workarea_rect.top - window_rect.top);
5951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005953 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5954 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
Ken Takata7086b3e2023-10-02 21:26:03 +02005956 //TRACE("New pos: %d,%d New size: %d,%d",
5957 // window_rect.left, window_rect.top, win_width, win_height);
5958
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 SetActiveWindow(s_hwnd);
5960 SetFocus(s_hwnd);
5961
Bram Moolenaar734a8672019-12-02 22:49:38 +01005962 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964}
5965
5966
5967 void
5968gui_mch_set_scrollbar_thumb(
5969 scrollbar_T *sb,
5970 long val,
5971 long size,
5972 long max)
5973{
5974 SCROLLINFO info;
5975
5976 sb->scroll_shift = 0;
5977 while (max > 32767)
5978 {
5979 max = (max + 1) >> 1;
5980 val >>= 1;
5981 size >>= 1;
5982 ++sb->scroll_shift;
5983 }
5984
5985 if (sb->scroll_shift > 0)
5986 ++size;
5987
5988 info.cbSize = sizeof(info);
5989 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5990 info.nPos = val;
5991 info.nMin = 0;
5992 info.nMax = max;
5993 info.nPage = size;
5994 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5995}
5996
5997
5998/*
5999 * Set the current text font.
6000 */
6001 void
6002gui_mch_set_font(GuiFont font)
6003{
6004 gui.currFont = font;
6005}
6006
6007
6008/*
6009 * Set the current text foreground color.
6010 */
6011 void
6012gui_mch_set_fg_color(guicolor_T color)
6013{
6014 gui.currFgColor = color;
6015}
6016
6017/*
6018 * Set the current text background color.
6019 */
6020 void
6021gui_mch_set_bg_color(guicolor_T color)
6022{
6023 gui.currBgColor = color;
6024}
6025
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006026/*
6027 * Set the current text special color.
6028 */
6029 void
6030gui_mch_set_sp_color(guicolor_T color)
6031{
6032 gui.currSpColor = color;
6033}
6034
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006035#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036/*
6037 * Multi-byte handling, originally by Sung-Hoon Baek.
6038 * First static functions (no prototypes generated).
6039 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006040# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01006041# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006042# endif
6043# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 * handle WM_IME_NOTIFY message
6047 */
6048 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006049_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 LRESULT lResult = 0;
6052 HIMC hImc;
6053
6054 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6055 return lResult;
6056 switch (dwCommand)
6057 {
6058 case IMN_SETOPENSTATUS:
6059 if (pImmGetOpenStatus(hImc))
6060 {
K.Takatac81e9bf2022-01-16 14:15:49 +00006061 LOGFONTW lf = norm_logfont;
6062 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6063 // Work around when PerMonitorV2 is not enabled in the process level.
6064 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6065 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 im_set_position(gui.row, gui.col);
6067
Bram Moolenaar734a8672019-12-02 22:49:38 +01006068 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01006069 State &= ~MODE_LANGMAP;
6070 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006072# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006073 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6075 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006076 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 int old_row = gui.row;
6078 int old_col = gui.col;
6079
6080 // This must be called here before
6081 // status_redraw_curbuf(), otherwise the mode
6082 // message may appear in the wrong position.
6083 showmode();
6084 status_redraw_curbuf();
6085 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006086 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 gui.row = old_row;
6088 gui.col = old_col;
6089 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 }
6092 }
6093 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01006094 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 lResult = 0;
6096 break;
6097 }
6098 pImmReleaseContext(hWnd, hImc);
6099 return lResult;
6100}
6101
6102 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006103_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
6105 char_u *ret;
6106 int len;
6107
Bram Moolenaar734a8672019-12-02 22:49:38 +01006108 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 return 0;
6110
6111 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6112 if (ret != NULL)
6113 {
6114 add_to_input_buf_csi(ret, len);
6115 vim_free(ret);
6116 return 1;
6117 }
6118 return 0;
6119}
6120
6121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 * void GetResultStr()
6123 *
6124 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6125 * get complete composition string
6126 */
6127 static char_u *
6128GetResultStr(HWND hwnd, int GCS, int *lenp)
6129{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006130 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00006131 LONG ret;
6132 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 char_u *convbuf = NULL;
6134
6135 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6136 return NULL;
6137
K.Takatab0b2b732022-01-19 12:59:21 +00006138 // Get the length of the composition string.
6139 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6140 if (ret <= 0)
6141 return NULL;
6142
6143 // Allocate the requested buffer plus space for the NUL character.
6144 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 if (buf == NULL)
6146 return NULL;
6147
K.Takatab0b2b732022-01-19 12:59:21 +00006148 // Reads in the composition string.
6149 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6150 *lenp = ret / sizeof(WCHAR);
6151
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006152 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 pImmReleaseContext(hwnd, hIMC);
6154 vim_free(buf);
6155 return convbuf;
6156}
6157#endif
6158
Bram Moolenaar734a8672019-12-02 22:49:38 +01006159// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006160#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162/*
6163 * set font to IM.
6164 */
6165 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006166im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 HIMC hImc;
6169
6170 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6171 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006172 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 pImmReleaseContext(s_hwnd, hImc);
6174 }
6175}
6176
6177/*
6178 * Notify cursor position to IM.
6179 */
6180 void
6181im_set_position(int row, int col)
6182{
6183 HIMC hImc;
6184
6185 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6186 {
6187 COMPOSITIONFORM cfs;
6188
6189 cfs.dwStyle = CFS_POINT;
6190 cfs.ptCurrentPos.x = FILL_X(col);
6191 cfs.ptCurrentPos.y = FILL_Y(row);
6192 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00006193 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6194 {
6195 // Work around when PerMonitorV2 is not enabled in the process level.
6196 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6197 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 pImmSetCompositionWindow(hImc, &cfs);
6200
6201 pImmReleaseContext(s_hwnd, hImc);
6202 }
6203}
6204
6205/*
6206 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6207 */
6208 void
6209im_set_active(int active)
6210{
6211 HIMC hImc;
6212 static HIMC hImcOld = (HIMC)0;
6213
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006214# ifdef VIMDLL
6215 if (!gui.in_use && !gui.starting)
6216 {
6217 mbyte_im_set_active(active);
6218 return;
6219 }
6220# endif
6221
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006222 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6223 return;
6224
6225 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006227 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006229 hImcOld = pImmGetContext(s_hwnd);
6230 if (hImcOld)
6231 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006233 active = FALSE;
6234 }
6235 else if (hImcOld != (HIMC)0)
6236 {
6237 pImmAssociateContext(s_hwnd, hImcOld);
6238 hImcOld = (HIMC)0;
6239 }
6240
6241 hImc = pImmGetContext(s_hwnd);
6242 if (!hImc)
6243 return;
6244
6245 /*
6246 * for Korean ime
6247 */
6248 HKL hKL = GetKeyboardLayout(0);
6249
6250 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6251 {
6252 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6253 static BOOL bSaved = FALSE;
6254
6255 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006257 // if we have a saved conversion status, restore it
6258 if (bSaved)
6259 pImmSetConversionStatus(hImc, dwConversionSaved,
6260 dwSentenceSaved);
6261 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006263 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006265 // save conversion status and disable korean
6266 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6267 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00006268 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006269 bSaved = TRUE;
6270 pImmSetConversionStatus(hImc,
6271 dwConversionSaved & ~(IME_CMODE_NATIVE
6272 | IME_CMODE_FULLSHAPE),
6273 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 }
6276 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006277
6278 pImmSetOpenStatus(hImc, active);
6279 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280}
6281
6282/*
6283 * Get IM status. When IM is on, return not 0. Else return 0.
6284 */
6285 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006286im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 int status = 0;
6289 HIMC hImc;
6290
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006291# ifdef VIMDLL
6292 if (!gui.in_use && !gui.starting)
6293 return mbyte_im_get_status();
6294# endif
6295
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6297 {
6298 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6299 pImmReleaseContext(s_hwnd, hImc);
6300 }
6301 return status;
6302}
6303
Bram Moolenaar734a8672019-12-02 22:49:38 +01006304#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006307/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006308 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006309 */
6310 static void
6311latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6312{
6313 int c;
6314
Bram Moolenaarca003e12006-03-17 23:19:38 +00006315 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006316 {
6317 c = *text++;
6318 switch (c)
6319 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006320 case 0xa4: c = 0x20ac; break; // euro
6321 case 0xa6: c = 0x0160; break; // S hat
6322 case 0xa8: c = 0x0161; break; // S -hat
6323 case 0xb4: c = 0x017d; break; // Z hat
6324 case 0xb8: c = 0x017e; break; // Z -hat
6325 case 0xbc: c = 0x0152; break; // OE
6326 case 0xbd: c = 0x0153; break; // oe
6327 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006328 }
6329 *unicodebuf++ = c;
6330 }
6331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
6333#ifdef FEAT_RIGHTLEFT
6334/*
6335 * What is this for? In the case where you are using Win98 or Win2K or later,
6336 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6337 * reverses the string sent to the TextOut... family. This sucks, because we
6338 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6339 * way to tell Windblows not to do this!
6340 *
6341 * The short of it is that this 'RevOut' only gets called if you are running
6342 * one of the new, "improved" MS OSes, and only if you are running in
6343 * 'rightleft' mode. It makes display take *slightly* longer, but not
6344 * noticeably so.
6345 */
6346 static void
K.Takata135e1522022-01-29 15:27:58 +00006347RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 int col,
6349 int row,
6350 UINT foptions,
6351 CONST RECT *pcliprect,
6352 LPCTSTR text,
6353 UINT len,
6354 CONST INT *padding)
6355{
6356 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006358 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006359 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006360 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362#endif
6363
Bram Moolenaar92467d32017-12-05 13:22:16 +01006364 static void
6365draw_line(
6366 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006367 int y1,
6368 int x2,
6369 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006370 COLORREF color)
6371{
6372#if defined(FEAT_DIRECTX)
6373 if (IS_ENABLE_DIRECTX())
6374 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6375 else
6376#endif
6377 {
6378 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6379 HPEN old_pen = SelectObject(s_hdc, hpen);
6380 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006381 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006382 LineTo(s_hdc, x2, y2);
6383 DeleteObject(SelectObject(s_hdc, old_pen));
6384 }
6385}
6386
6387 static void
6388set_pixel(
6389 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006390 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006391 COLORREF color)
6392{
6393#if defined(FEAT_DIRECTX)
6394 if (IS_ENABLE_DIRECTX())
6395 DWriteContext_SetPixel(s_dwc, x, y, color);
6396 else
6397#endif
6398 SetPixel(s_hdc, x, y, color);
6399}
6400
6401 static void
6402fill_rect(
6403 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006404 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006405 COLORREF color)
6406{
6407#if defined(FEAT_DIRECTX)
6408 if (IS_ENABLE_DIRECTX())
6409 DWriteContext_FillRect(s_dwc, rcp, color);
6410 else
6411#endif
6412 {
6413 HBRUSH hbr2;
6414
6415 if (hbr == NULL)
6416 hbr2 = CreateSolidBrush(color);
6417 else
6418 hbr2 = hbr;
6419 FillRect(s_hdc, rcp, hbr2);
6420 if (hbr == NULL)
6421 DeleteBrush(hbr2);
6422 }
6423}
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 void
6426gui_mch_draw_string(
6427 int row,
6428 int col,
6429 char_u *text,
6430 int len,
6431 int flags)
6432{
6433 static int *padding = NULL;
6434 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 const RECT *pcliprect = NULL;
6436 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 static WCHAR *unicodebuf = NULL;
6438 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006439 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 int y;
6442
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 /*
6444 * Italic and bold text seems to have an extra row of pixels at the bottom
6445 * (below where the bottom of the character should be). If we draw the
6446 * characters with a solid background, the top row of pixels in the
6447 * character below will be overwritten. We can fix this by filling in the
6448 * background ourselves, to the correct character proportions, and then
6449 * writing the character in transparent mode. Still have a problem when
6450 * the character is "_", which gets written on to the character below.
6451 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6452 * pixel in their slots, which fixes the problem with the bottom row of
6453 * pixels. We still need this code because otherwise the top row of pixels
6454 * becomes a problem. - webb.
6455 */
6456 static HBRUSH hbr_cache[2] = {NULL, NULL};
6457 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6458 static int brush_lru = 0;
6459 HBRUSH hbr;
6460 RECT rc;
6461
6462 if (!(flags & DRAW_TRANSP))
6463 {
6464 /*
6465 * Clear background first.
6466 * Note: FillRect() excludes right and bottom of rectangle.
6467 */
6468 rc.left = FILL_X(col);
6469 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 if (has_mbyte)
6471 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006472 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006473 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 rc.right = FILL_X(col + len);
6477 rc.bottom = FILL_Y(row + 1);
6478
Bram Moolenaar734a8672019-12-02 22:49:38 +01006479 // Cache the created brush, that saves a lot of time. We need two:
6480 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 if (gui.currBgColor == brush_color[0])
6482 {
6483 hbr = hbr_cache[0];
6484 brush_lru = 1;
6485 }
6486 else if (gui.currBgColor == brush_color[1])
6487 {
6488 hbr = hbr_cache[1];
6489 brush_lru = 0;
6490 }
6491 else
6492 {
6493 if (hbr_cache[brush_lru] != NULL)
6494 DeleteBrush(hbr_cache[brush_lru]);
6495 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6496 brush_color[brush_lru] = gui.currBgColor;
6497 hbr = hbr_cache[brush_lru];
6498 brush_lru = !brush_lru;
6499 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006500
Bram Moolenaar92467d32017-12-05 13:22:16 +01006501 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
6503 SetBkMode(s_hdc, TRANSPARENT);
6504
6505 /*
6506 * When drawing block cursor, prevent inverted character spilling
6507 * over character cell (can happen with bold/italic)
6508 */
6509 if (flags & DRAW_CURSOR)
6510 {
6511 pcliprect = &rc;
6512 foptions = ETO_CLIPPED;
6513 }
6514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 SetTextColor(s_hdc, gui.currFgColor);
6516 SelectFont(s_hdc, gui.currFont);
6517
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006518#ifdef FEAT_DIRECTX
6519 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006520 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006521#endif
6522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6524 {
K.Takata135e1522022-01-29 15:27:58 +00006525 int i;
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 vim_free(padding);
6528 pad_size = Columns;
6529
Bram Moolenaar734a8672019-12-02 22:49:38 +01006530 // Don't give an out-of-memory message here, it would call us
6531 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006532 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (padding != NULL)
6534 for (i = 0; i < pad_size; i++)
6535 padding[i] = gui.char_width;
6536 }
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 /*
6539 * We have to provide the padding argument because italic and bold versions
6540 * of fixed-width fonts are often one pixel or so wider than their normal
6541 * versions.
6542 * No check for DRAW_BOLD, Windows will have done it already.
6543 */
6544
Bram Moolenaar734a8672019-12-02 22:49:38 +01006545 // Check if there are any UTF-8 characters. If not, use normal text
6546 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (enc_utf8)
6548 for (n = 0; n < len; ++n)
6549 if (text[n] >= 0x80)
6550 break;
6551
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006552#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006553 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6554 // required that unicode drawing routine, currently. So this forces it
6555 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006556 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006558#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006559
Bram Moolenaar734a8672019-12-02 22:49:38 +01006560 // Check if the Unicode buffer exists and is big enough. Create it
6561 // with the same length as the multi-byte string, the number of wide
6562 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006563 if ((enc_utf8
6564 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6565 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 && (unicodebuf == NULL || len > unibuflen))
6567 {
6568 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006569 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006572 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574 unibuflen = len;
6575 }
6576
6577 if (enc_utf8 && n < len && unicodebuf != NULL)
6578 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006579 // Output UTF-8 characters. Composing characters should be
6580 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006581 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006582 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006583 int cells; // cell width of string up to composing char
6584 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006585 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006587 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006589 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006591 c = utf_ptr2char(text + i);
6592 if (c >= 0x10000)
6593 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006594 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006595 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6596 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006597 }
6598 else
6599 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006600 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006601 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006602
6603 if (utf_iscomposing(c))
6604 cw = 0;
6605 else
6606 {
6607 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006608 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006609 cw = 1;
6610 }
6611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 if (unicodepdy != NULL)
6613 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006614 // Use unicodepdy to make characters fit as we expect, even
6615 // when the font uses different widths (e.g., bold character
6616 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006617 if (c >= 0x10000)
6618 {
6619 unicodepdy[wlen - 2] = cw * gui.char_width;
6620 unicodepdy[wlen - 1] = 0;
6621 }
6622 else
6623 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 }
6625 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006626 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006628#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006629 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006630 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006631 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006632 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006633 TEXT_X(col), TEXT_Y(row),
6634 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006635 gui.char_width, gui.currFgColor,
6636 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006637 }
6638 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006639#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006640 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6641 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006642 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006644 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006646 // If we want to display codepage data, and the current CP is not the
6647 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 if (unicodebuf != NULL)
6649 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006650 if (enc_latin9)
6651 latin9_to_ucs(text, len, unicodebuf);
6652 else
6653 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 MB_PRECOMPOSED,
6655 (char *)text, len,
6656 (LPWSTR)unicodebuf, unibuflen);
6657 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006658 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006659 // Use unicodepdy to make characters fit as we expect, even
6660 // when the font uses different widths (e.g., bold character
6661 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006662 if (unicodepdy != NULL)
6663 {
6664 int i;
6665 int cw;
6666
6667 for (i = 0; i < len; ++i)
6668 {
6669 cw = utf_char2cells(unicodebuf[i]);
6670 if (cw > 2)
6671 cw = 1;
6672 unicodepdy[i] = cw * gui.char_width;
6673 }
6674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006676 foptions, pcliprect, unicodebuf, len, unicodepdy);
6677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 }
6679 }
6680 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {
6682#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006683 // Windows will mess up RL text, so we have to draw it character by
6684 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006685 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6687 foptions, pcliprect, (char *)text, len, padding);
6688 else
6689#endif
6690 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6691 foptions, pcliprect, (char *)text, len, padding);
6692 }
6693
Bram Moolenaar734a8672019-12-02 22:49:38 +01006694 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (flags & DRAW_UNDERL)
6696 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006697 // When p_linespace is 0, overwrite the bottom row of pixels.
6698 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (p_linespace > 1)
6701 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006702 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006704
Bram Moolenaar734a8672019-12-02 22:49:38 +01006705 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006706 if (flags & DRAW_STRIKE)
6707 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006708 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006709 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006710 }
6711
Bram Moolenaar734a8672019-12-02 22:49:38 +01006712 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006713 if (flags & DRAW_UNDERC)
6714 {
6715 int x;
6716 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006717 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006718
6719 y = FILL_Y(row + 1) - 1;
6720 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6721 {
6722 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006723 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006724 }
6725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726}
6727
6728
6729/*
6730 * Output routines.
6731 */
6732
Bram Moolenaar734a8672019-12-02 22:49:38 +01006733/*
6734 * Flush any output to the screen
6735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 void
6737gui_mch_flush(void)
6738{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006739#if defined(FEAT_DIRECTX)
6740 if (IS_ENABLE_DIRECTX())
6741 DWriteContext_Flush(s_dwc);
6742#endif
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 GdiFlush();
6745}
6746
6747 static void
6748clear_rect(RECT *rcp)
6749{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006750 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751}
6752
6753
Bram Moolenaarc716c302006-01-21 22:12:51 +00006754 void
6755gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6756{
6757 RECT workarea_rect;
6758
6759 get_work_area(&workarea_rect);
6760
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006761 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006762 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6763 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006764
Bram Moolenaar734a8672019-12-02 22:49:38 +01006765 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6766 // the menubar for MSwin, we subtract it from the screen height, so that
6767 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006768 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006769 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6770 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6771 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6772 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006773}
6774
6775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776#if defined(FEAT_MENU) || defined(PROTO)
6777/*
6778 * Add a sub menu to the menu bar.
6779 */
6780 void
6781gui_mch_add_menu(
6782 vimmenu_T *menu,
6783 int pos)
6784{
6785 vimmenu_T *parent = menu->parent;
6786
6787 menu->submenu_id = CreatePopupMenu();
6788 menu->id = s_menu_id++;
6789
6790 if (menu_is_menubar(menu->name))
6791 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006792 WCHAR *wn;
6793 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006795 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006796 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006797 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006799 infow.cbSize = sizeof(infow);
6800 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6801 | MIIM_SUBMENU;
6802 infow.dwItemData = (long_u)menu;
6803 infow.wID = menu->id;
6804 infow.fType = MFT_STRING;
6805 infow.dwTypeData = wn;
6806 infow.cch = (UINT)wcslen(wn);
6807 infow.hSubMenu = menu->submenu_id;
6808 InsertMenuItemW((parent == NULL)
6809 ? s_menuBar : parent->submenu_id,
6810 (UINT)pos, TRUE, &infow);
6811 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 }
6813
Bram Moolenaar734a8672019-12-02 22:49:38 +01006814 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (parent == NULL)
6816 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006817# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 else if (IsWindow(parent->tearoff_handle))
6819 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821}
6822
6823 void
6824gui_mch_show_popupmenu(vimmenu_T *menu)
6825{
6826 POINT mp;
6827
K.Takata45f9cfb2022-01-21 11:11:00 +00006828 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6830}
6831
6832 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006833gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834{
6835 vimmenu_T *menu = gui_find_menu(path_name);
6836
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006837 if (menu == NULL)
6838 return;
6839
6840 POINT p;
6841
6842 // Find the position of the current cursor
6843 GetDCOrgEx(s_hdc, &p);
6844 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006846 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006848 gui_mch_getmouse(&mx, &my);
6849 p.x += mx;
6850 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006852 else if (curwin != NULL)
6853 {
6854 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6855 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6856 }
6857 msg_scroll = FALSE;
6858 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859}
6860
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006861# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862/*
6863 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6864 * create it as a pseudo-"tearoff menu".
6865 */
6866 void
6867gui_make_tearoff(char_u *path_name)
6868{
6869 vimmenu_T *menu = gui_find_menu(path_name);
6870
Bram Moolenaar734a8672019-12-02 22:49:38 +01006871 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (menu != NULL)
6873 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6874}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
6877/*
6878 * Add a menu item to a menu
6879 */
6880 void
6881gui_mch_add_menu_item(
6882 vimmenu_T *menu,
6883 int idx)
6884{
6885 vimmenu_T *parent = menu->parent;
6886
6887 menu->id = s_menu_id++;
6888 menu->submenu_id = NULL;
6889
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006890# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6892 {
6893 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6894 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6895 }
6896 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006897# endif
6898# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 if (menu_is_toolbar(parent->name))
6900 {
6901 TBBUTTON newtb;
6902
Bram Moolenaara80faa82020-04-12 19:37:17 +02006903 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 if (menu_is_separator(menu->name))
6905 {
6906 newtb.iBitmap = 0;
6907 newtb.fsStyle = TBSTYLE_SEP;
6908 }
6909 else
6910 {
6911 newtb.iBitmap = get_toolbar_bitmap(menu);
6912 newtb.fsStyle = TBSTYLE_BUTTON;
6913 }
6914 newtb.idCommand = menu->id;
6915 newtb.fsState = TBSTATE_ENABLED;
6916 newtb.iString = 0;
6917 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6918 (LPARAM)&newtb);
6919 menu->submenu_id = (HMENU)-1;
6920 }
6921 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006924 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006926 wn = enc_to_utf16(menu->name, NULL);
6927 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006929 InsertMenuW(parent->submenu_id, (UINT)idx,
6930 (menu_is_separator(menu->name)
6931 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6932 (UINT)menu->id, wn);
6933 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006935# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (IsWindow(parent->tearoff_handle))
6937 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 }
6940}
6941
6942/*
6943 * Destroy the machine specific menu widget.
6944 */
6945 void
6946gui_mch_destroy_menu(vimmenu_T *menu)
6947{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006948# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 /*
6950 * is this a toolbar button?
6951 */
6952 if (menu->submenu_id == (HMENU)-1)
6953 {
6954 int iButton;
6955
6956 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6957 (WPARAM)menu->id, 0);
6958 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6959 }
6960 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
6963 if (menu->parent != NULL
6964 && menu_is_popup(menu->parent->dname)
6965 && menu->parent->submenu_id != NULL)
6966 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6967 else
6968 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6969 if (menu->submenu_id != NULL)
6970 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006971# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (IsWindow(menu->tearoff_handle))
6973 DestroyWindow(menu->tearoff_handle);
6974 if (menu->parent != NULL
6975 && menu->parent->children != NULL
6976 && IsWindow(menu->parent->tearoff_handle))
6977 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006978 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 menu->modes = 0;
6980 rebuild_tearoff(menu->parent);
6981 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984}
6985
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006986# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 static void
6988rebuild_tearoff(vimmenu_T *menu)
6989{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 char_u tbuf[128];
6992 RECT trect;
6993 RECT rct;
6994 RECT roct;
6995 int x, y;
6996
6997 HWND thwnd = menu->tearoff_handle;
6998
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006999 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (GetWindowRect(thwnd, &trect)
7001 && GetWindowRect(s_hwnd, &rct)
7002 && GetClientRect(s_hwnd, &roct))
7003 {
7004 x = trect.left - rct.left;
7005 y = (trect.top - rct.bottom + roct.bottom);
7006 }
7007 else
7008 {
7009 x = y = 0xffffL;
7010 }
7011 DestroyWindow(thwnd);
7012 if (menu->children != NULL)
7013 {
7014 gui_mch_tearoff(tbuf, menu, x, y);
7015 if (IsWindow(menu->tearoff_handle))
7016 (void) SetWindowPos(menu->tearoff_handle,
7017 NULL,
7018 (int)trect.left,
7019 (int)trect.top,
7020 0, 0,
7021 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7022 }
7023}
Bram Moolenaar734a8672019-12-02 22:49:38 +01007024# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
7026/*
7027 * Make a menu either grey or not grey.
7028 */
7029 void
7030gui_mch_menu_grey(
7031 vimmenu_T *menu,
7032 int grey)
7033{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007034# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 /*
7036 * is this a toolbar button?
7037 */
7038 if (menu->submenu_id == (HMENU)-1)
7039 {
7040 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00007041 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007044# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007045 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7046 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007048# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7050 {
7051 WORD menuID;
7052 HWND menuHandle;
7053
7054 /*
7055 * A tearoff button has changed state.
7056 */
7057 if (menu->children == NULL)
7058 menuID = (WORD)(menu->id);
7059 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007060 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7062 if (menuHandle)
7063 EnableWindow(menuHandle, !grey);
7064
7065 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007066# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067}
7068
Bram Moolenaar734a8672019-12-02 22:49:38 +01007069#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
7071
Bram Moolenaar734a8672019-12-02 22:49:38 +01007072// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007075#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076
7077#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7078/*
7079 * stuff for dialogs
7080 */
7081
7082/*
7083 * The callback routine used by all the dialogs. Very simple. First,
7084 * acknowledges the INITDIALOG message so that Windows knows to do standard
7085 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7086 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7087 * number.
7088 */
7089 static LRESULT CALLBACK
7090dialog_callback(
7091 HWND hwnd,
7092 UINT message,
7093 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007094 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
7096 if (message == WM_INITDIALOG)
7097 {
7098 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007099 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 (void)SetFocus(hwnd);
7101 if (dialog_default_button > IDCANCEL)
7102 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007103 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007104 // We don't have a default, set focus on another element of the
7105 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007106 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 return FALSE;
7108 }
7109
7110 if (message == WM_COMMAND)
7111 {
7112 int button = LOWORD(wParam);
7113
Bram Moolenaar734a8672019-12-02 22:49:38 +01007114 // Don't end the dialog if something was selected that was
7115 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 if (button >= DLG_NONBUTTON_CONTROL)
7117 return TRUE;
7118
Bram Moolenaar734a8672019-12-02 22:49:38 +01007119 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007121 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007122 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007123 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007124
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007125 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7126 p = utf16_to_enc(wp, NULL);
7127 vim_strncpy(s_textfield, p, IOSIZE);
7128 vim_free(p);
7129 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
7132 /*
7133 * Need to check for IDOK because if the user just hits Return to
7134 * accept the default value, some reason this is what we get.
7135 */
7136 if (button == IDOK)
7137 {
7138 if (dialog_default_button > IDCANCEL)
7139 EndDialog(hwnd, dialog_default_button);
7140 }
7141 else
7142 EndDialog(hwnd, button - IDCANCEL);
7143 return TRUE;
7144 }
7145
7146 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7147 {
7148 EndDialog(hwnd, 0);
7149 return TRUE;
7150 }
7151 return FALSE;
7152}
7153
7154/*
7155 * Create a dialog dynamically from the parameter strings.
7156 * type = type of dialog (question, alert, etc.)
7157 * title = dialog title. may be NULL for default title.
7158 * message = text to display. Dialog sizes to accommodate it.
7159 * buttons = '\n' separated list of button captions, default first.
7160 * dfltbutton = number of default button.
7161 *
7162 * This routine returns 1 if the first button is pressed,
7163 * 2 for the second, etc.
7164 *
7165 * 0 indicates Esc was pressed.
7166 * -1 for unexpected error
7167 *
7168 * If stubbing out this fn, return 1.
7169 */
7170
Bram Moolenaar734a8672019-12-02 22:49:38 +01007171static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 "IDR_VIM",
7174 "IDR_VIM_ERROR",
7175 "IDR_VIM_ALERT",
7176 "IDR_VIM_INFO",
7177 "IDR_VIM_QUESTION"
7178};
7179
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 int
7181gui_mch_dialog(
7182 int type,
7183 char_u *title,
7184 char_u *message,
7185 char_u *buttons,
7186 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007187 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007188 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189{
7190 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007191 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 int numButtons;
7193 int *buttonWidths, *buttonPositions;
7194 int buttonYpos;
7195 int nchar, i;
7196 DWORD lStyle;
7197 int dlgwidth = 0;
7198 int dlgheight;
7199 int editboxheight;
7200 int horizWidth = 0;
7201 int msgheight;
7202 char_u *pstart;
7203 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007204 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 char_u *tbuffer;
7206 RECT rect;
7207 HWND hwnd;
7208 HDC hdc;
7209 HFONT font, oldFont;
7210 TEXTMETRIC fontInfo;
7211 int fontHeight;
7212 int textWidth, minButtonWidth, messageWidth;
7213 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007214 int maxDialogHeight;
7215 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 int vertical;
7217 int dlgPaddingX;
7218 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007219# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007220 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007222# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007223 garray_T ga;
7224 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007225 int dlg_icon_width;
7226 int dlg_icon_height;
7227 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007229# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007230 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007231# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007232 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007233# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007234 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007235 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar748bf032005-02-02 23:04:36 +00007238 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007239 {
7240 load_dpi_func();
7241 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007242 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007243 }
7244 else
7245 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 if ((type < 0) || (type > VIM_LAST_TYPE))
7248 type = 0;
7249
Bram Moolenaar734a8672019-12-02 22:49:38 +01007250 // allocate some memory for dialog template
7251 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007252 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007253 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 if (p == NULL)
7256 return -1;
7257
7258 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007259 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7261 * const.
7262 */
7263 tbuffer = vim_strsave(buttons);
7264 if (tbuffer == NULL)
7265 return -1;
7266
Bram Moolenaar734a8672019-12-02 22:49:38 +01007267 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
Bram Moolenaar734a8672019-12-02 22:49:38 +01007269 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 numButtons = 1;
7271 for (i = 0; tbuffer[i] != '\0'; i++)
7272 {
7273 if (tbuffer[i] == DLG_BUTTON_SEP)
7274 numButtons++;
7275 }
7276 if (dfltbutton >= numButtons)
7277 dfltbutton = -1;
7278
Bram Moolenaar734a8672019-12-02 22:49:38 +01007279 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007280 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 if (buttonWidths == NULL)
7282 return -1;
7283
Bram Moolenaar734a8672019-12-02 22:49:38 +01007284 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007285 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 if (buttonPositions == NULL)
7287 return -1;
7288
7289 /*
7290 * Calculate how big the dialog must be.
7291 */
7292 hwnd = GetDesktopWindow();
7293 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007294# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7296 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007297 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 use_lfSysmenu = TRUE;
7299 }
7300 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007301# endif
K.Takatad1c58992022-01-23 12:31:57 +00007302 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7303 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7304
7305 oldFont = SelectFont(hdc, font);
7306 dlgPaddingX = DLG_PADDING_X;
7307 dlgPaddingY = DLG_PADDING_Y;
7308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 GetTextMetrics(hdc, &fontInfo);
7310 fontHeight = fontInfo.tmHeight;
7311
Bram Moolenaar734a8672019-12-02 22:49:38 +01007312 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007313 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaar734a8672019-12-02 22:49:38 +01007315 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007316 if (s_hwnd == NULL)
7317 {
7318 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
Bram Moolenaar734a8672019-12-02 22:49:38 +01007320 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007321 get_work_area(&workarea_rect);
7322 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007323 if (maxDialogWidth > adjust_by_system_dpi(600))
7324 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007325 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007326 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007327 }
7328 else
7329 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007330 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007331 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007332 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007333 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7334 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7335 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7336 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007337
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007338 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007339 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7340 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7341 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7342 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7343 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007344 }
7345
Bram Moolenaar734a8672019-12-02 22:49:38 +01007346 // Set dlgwidth to width of message.
7347 // Copy the message into "ga", changing NL to CR-NL and inserting line
7348 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 pstart = message;
7350 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007351 msgheight = 0;
7352 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 do
7354 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007356
Bram Moolenaar734a8672019-12-02 22:49:38 +01007357 // Need to figure out where to break the string. The system does it
7358 // at a word boundary, which would mean we can't compute the number of
7359 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007360 textWidth = 0;
7361 last_white = NULL;
7362 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007363 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007364 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007365 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007366 && textWidth > maxDialogWidth * 3 / 4)
7367 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007368 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007369 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007370 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007372 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007373 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007374 textWidth = 0;
7375
7376 if (last_white != NULL)
7377 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007379 if (pend > last_white)
7380 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007381 pend = last_white + 1;
7382 last_white = NULL;
7383 }
7384 ga_append(&ga, '\r');
7385 ga_append(&ga, '\n');
7386 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007387 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007388
7389 while (--l >= 0)
7390 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007391 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007392 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007394
7395 ga_append(&ga, '\r');
7396 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 pstart = pend + 1;
7398 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007399
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007400 if (ga.ga_data != NULL)
7401 message = ga.ga_data;
7402
Bram Moolenaar734a8672019-12-02 22:49:38 +01007403 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007404
K.Takatac81e9bf2022-01-16 14:15:49 +00007405 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7406 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
K.Takatac81e9bf2022-01-16 14:15:49 +00007408 // Add width of icon to dlgwidth, and some space
7409 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7410 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7411
7412 if (msgheight < dlg_icon_height)
7413 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 /*
7416 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007417 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007419 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (!vertical)
7421 {
7422 // Place buttons horizontally if they fit.
7423 horizWidth = dlgPaddingX;
7424 pstart = tbuffer;
7425 i = 0;
7426 do
7427 {
7428 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7429 if (pend == NULL)
7430 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007431 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (textWidth < minButtonWidth)
7433 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007434 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 buttonWidths[i] = textWidth;
7436 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007437 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 pstart = pend + 1;
7439 } while (*pend != NUL);
7440
7441 if (horizWidth > maxDialogWidth)
7442 vertical = TRUE; // Too wide to fit on the screen.
7443 else if (horizWidth > dlgwidth)
7444 dlgwidth = horizWidth;
7445 }
7446
7447 if (vertical)
7448 {
7449 // Stack buttons vertically.
7450 pstart = tbuffer;
7451 do
7452 {
7453 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7454 if (pend == NULL)
7455 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007456 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007457 textWidth += dlgPaddingX; // Padding within button
7458 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (textWidth > dlgwidth)
7460 dlgwidth = textWidth;
7461 pstart = pend + 1;
7462 } while (*pend != NUL);
7463 }
7464
7465 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007466 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar734a8672019-12-02 22:49:38 +01007468 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007469 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
7471 add_long(lStyle);
7472 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007473 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 add_word(0); // NumberOfItems(will change later)
7475 add_word(10); // x
7476 add_word(10); // y
7477 add_word(PixelToDialogX(dlgwidth)); // cx
7478
7479 // Dialog height.
7480 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007481 dlgheight = msgheight + 2 * dlgPaddingY
7482 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 else
7484 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7485
7486 // Dialog needs to be taller if contains an edit box.
7487 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7488 if (textfield != NULL)
7489 dlgheight += editboxheight;
7490
Bram Moolenaar734a8672019-12-02 22:49:38 +01007491 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007492 if (dlgheight > maxDialogHeight)
7493 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007494 msgheight = msgheight - (dlgheight - maxDialogHeight);
7495 dlgheight = maxDialogHeight;
7496 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007497 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007498 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007499 }
7500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 add_word(PixelToDialogY(dlgheight));
7502
7503 add_word(0); // Menu
7504 add_word(0); // Class
7505
Bram Moolenaar734a8672019-12-02 22:49:38 +01007506 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007507 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7508 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 p += nchar;
7510
K.Takatad1c58992022-01-23 12:31:57 +00007511 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007512# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007513 if (use_lfSysmenu)
7514 {
7515 // point size
7516 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7517 GetDeviceCaps(hdc, LOGPIXELSY));
7518 wcscpy(p, lfSysmenu.lfFaceName);
7519 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 }
K.Takatad1c58992022-01-23 12:31:57 +00007521 else
7522# endif
7523 {
7524 *p++ = DLG_FONT_POINT_SIZE; // point size
7525 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7526 }
7527 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528
7529 buttonYpos = msgheight + 2 * dlgPaddingY;
7530
7531 if (textfield != NULL)
7532 buttonYpos += editboxheight;
7533
7534 pstart = tbuffer;
7535 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007536 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 for (i = 0; i < numButtons; i++)
7538 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007539 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 for ( pend = pstart;
7541 *pend && (*pend != DLG_BUTTON_SEP);
7542 pend++)
7543 ;
7544
7545 if (*pend)
7546 *pend = '\0';
7547
7548 /*
7549 * old NOTE:
7550 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7551 * the focus to the first tab-able button and in so doing makes that
7552 * the default!! Grrr. Workaround: Make the default button the only
7553 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7554 * he/she can use arrow keys.
7555 *
7556 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007557 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 * dialog. Also needed for when the textfield is the default control.
7559 * It appears to work now (perhaps not on Win95?).
7560 */
7561 if (vertical)
7562 {
7563 p = add_dialog_element(p,
7564 (i == dfltbutton
7565 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7566 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007567 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 + 2 * fontHeight * i),
7569 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7570 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007571 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573 else
7574 {
7575 p = add_dialog_element(p,
7576 (i == dfltbutton
7577 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7578 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007579 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 PixelToDialogX(buttonWidths[i]),
7581 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007582 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007584 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
7586 *pnumitems += numButtons;
7587
Bram Moolenaar734a8672019-12-02 22:49:38 +01007588 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 p = add_dialog_element(p, SS_ICON,
7590 PixelToDialogX(dlgPaddingX),
7591 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007592 PixelToDialogX(dlg_icon_width),
7593 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7595 dlg_icons[type]);
7596
Bram Moolenaar734a8672019-12-02 22:49:38 +01007597 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007598 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007599 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007600 PixelToDialogY(dlgPaddingY),
7601 (WORD)(PixelToDialogX(messageWidth) + 1),
7602 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007603 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
Bram Moolenaar734a8672019-12-02 22:49:38 +01007605 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (textfield != NULL)
7607 {
7608 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7609 PixelToDialogX(2 * dlgPaddingX),
7610 PixelToDialogY(2 * dlgPaddingY + msgheight),
7611 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7612 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007613 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 *pnumitems += 1;
7615 }
7616
7617 *pnumitems += 2;
7618
7619 SelectFont(hdc, oldFont);
7620 DeleteObject(font);
7621 ReleaseDC(hwnd, hdc);
7622
Bram Moolenaar734a8672019-12-02 22:49:38 +01007623 // Let the dialog_callback() function know which button to make default
7624 // If we have an edit box, make that the default. We also need to tell
7625 // dialog_callback() if this dialog contains an edit box or not. We do
7626 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 if (textfield != NULL)
7628 {
7629 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7630 s_textfield = textfield;
7631 }
7632 else
7633 {
7634 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7635 s_textfield = NULL;
7636 }
7637
Bram Moolenaar734a8672019-12-02 22:49:38 +01007638 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007640 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 (LPDLGTEMPLATE)pdlgtemplate,
7642 s_hwnd,
7643 (DLGPROC)dialog_callback);
7644
7645 LocalFree(LocalHandle(pdlgtemplate));
7646 vim_free(tbuffer);
7647 vim_free(buttonWidths);
7648 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007649 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650
Bram Moolenaar734a8672019-12-02 22:49:38 +01007651 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 (void)SetFocus(s_hwnd);
7653
7654 return nchar;
7655}
7656
Bram Moolenaar734a8672019-12-02 22:49:38 +01007657#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659/*
7660 * Put a simple element (basic class) onto a dialog template in memory.
7661 * return a pointer to where the next item should be added.
7662 *
7663 * parameters:
7664 * lStyle = additional style flags
7665 * (be careful, NT3.51 & Win32s will ignore the new ones)
7666 * x,y = x & y positions IN DIALOG UNITS
7667 * w,h = width and height IN DIALOG UNITS
7668 * Id = ID used in messages
7669 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7670 * caption = usually text or resource name
7671 *
7672 * TODO: use the length information noted here to enable the dialog creation
7673 * routines to work out more exactly how much memory they need to alloc.
7674 */
7675 static PWORD
7676add_dialog_element(
7677 PWORD p,
7678 DWORD lStyle,
7679 WORD x,
7680 WORD y,
7681 WORD w,
7682 WORD h,
7683 WORD Id,
7684 WORD clss,
7685 const char *caption)
7686{
7687 int nchar;
7688
Bram Moolenaar734a8672019-12-02 22:49:38 +01007689 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7691 *p++ = LOWORD(lStyle);
7692 *p++ = HIWORD(lStyle);
7693 *p++ = 0; // LOWORD (lExtendedStyle)
7694 *p++ = 0; // HIWORD (lExtendedStyle)
7695 *p++ = x;
7696 *p++ = y;
7697 *p++ = w;
7698 *p++ = h;
7699 *p++ = Id; //9 or 10 words in all
7700
7701 *p++ = (WORD)0xffff;
7702 *p++ = clss; //2 more here
7703
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007704 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 p += nchar;
7706
7707 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7708
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007709 return p; // total = 15 + strlen(caption) words
7710 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713
7714/*
7715 * Helper routine. Take an input pointer, return closest pointer that is
7716 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7717 */
7718 static LPWORD
7719lpwAlign(
7720 LPWORD lpIn)
7721{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007722 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007724 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 ul += 3;
7726 ul >>= 2;
7727 ul <<= 2;
7728 return (LPWORD)ul;
7729}
7730
7731/*
7732 * Helper routine. Takes second parameter as Ansi string, copies it to first
7733 * parameter as wide character (16-bits / char) string, and returns integer
7734 * number of wide characters (words) in string (including the trailing wide
7735 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007736 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7737 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 static int
7739nCopyAnsiToWideChar(
7740 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007741 LPSTR lpAnsiIn,
7742 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007745 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 int i;
7747 WCHAR *wn;
7748
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007749 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007751 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007752 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 if (wn != NULL)
7754 {
7755 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007756 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 vim_free(wn);
7758 }
7759 }
7760 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007761 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 nChar = MultiByteToWideChar(
7763 enc_codepage > 0 ? enc_codepage : CP_ACP,
7764 MB_PRECOMPOSED,
7765 lpAnsiIn, len,
7766 lpWCStr, len);
7767 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007768 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
7771 return nChar;
7772}
7773
7774
7775#ifdef FEAT_TEAROFF
7776/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007777 * Lookup menu handle from "menu_id".
7778 */
7779 static HMENU
7780tearoff_lookup_menuhandle(
7781 vimmenu_T *menu,
7782 WORD menu_id)
7783{
7784 for ( ; menu != NULL; menu = menu->next)
7785 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007786 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007787 continue;
7788 if (menu_is_separator(menu->dname))
7789 continue;
7790 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7791 return menu->submenu_id;
7792 }
7793 return NULL;
7794}
7795
7796/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 * The callback function for all the modeless dialogs that make up the
7798 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7799 * thinking its menus have been clicked), and go away when closed.
7800 */
7801 static LRESULT CALLBACK
7802tearoff_callback(
7803 HWND hwnd,
7804 UINT message,
7805 WPARAM wParam,
7806 LPARAM lParam)
7807{
7808 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007809 {
7810 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
Bram Moolenaar734a8672019-12-02 22:49:38 +01007814 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 HandleMouseHide(message, lParam);
7816
7817 if (message == WM_COMMAND)
7818 {
7819 if ((WORD)(LOWORD(wParam)) & 0x8000)
7820 {
7821 POINT mp;
7822 RECT rect;
7823
7824 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7825 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007826 vimmenu_T *menu;
7827
7828 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007830 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7832 (int)rect.right - 8,
7833 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007834 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 s_hwnd,
7836 NULL);
7837 /*
7838 * NOTE: The pop-up menu can eat the mouse up event.
7839 * We deal with this in normal.c.
7840 */
7841 }
7842 }
7843 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007844 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7846 /*
7847 * Give main window the focus back: this is so after
7848 * choosing a tearoff button you can start typing again
7849 * straight away.
7850 */
7851 (void)SetFocus(s_hwnd);
7852 return TRUE;
7853 }
7854 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7855 {
7856 DestroyWindow(hwnd);
7857 return TRUE;
7858 }
7859
Bram Moolenaar734a8672019-12-02 22:49:38 +01007860 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (message == WM_EXITSIZEMOVE)
7862 (void)SetActiveWindow(s_hwnd);
7863
7864 return FALSE;
7865}
7866#endif
7867
7868
7869/*
K.Takatad1c58992022-01-23 12:31:57 +00007870 * Computes the dialog base units based on the current dialog font.
7871 * We don't use the GetDialogBaseUnits() API, because we don't use the
7872 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 */
7874 static void
7875get_dialog_font_metrics(void)
7876{
7877 HDC hdc;
7878 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 SIZE size;
7880#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007881 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882#endif
7883
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007885 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007886 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#endif
7889 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007890 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891
K.Takatad1c58992022-01-23 12:31:57 +00007892 hdc = GetDC(s_hwnd);
7893 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007894 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007895 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896
K.Takataabe628e2022-01-23 16:25:17 +00007897 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007898 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
7900
7901#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7902/*
7903 * Create a pseudo-"tearoff menu" based on the child
7904 * items of a given menu pointer.
7905 */
7906 static void
7907gui_mch_tearoff(
7908 char_u *title,
7909 vimmenu_T *menu,
7910 int initX,
7911 int initY)
7912{
7913 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7914 int template_len;
7915 int nchar, textWidth, submenuWidth;
7916 DWORD lStyle;
7917 DWORD lExtendedStyle;
7918 WORD dlgwidth;
7919 WORD menuID;
7920 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007921 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 vimmenu_T *the_menu = menu;
7923 HWND hwnd;
7924 HDC hdc;
7925 HFONT font, oldFont;
7926 int col, spaceWidth, len;
7927 int columnWidths[2];
7928 char_u *label, *text;
7929 int acLen = 0;
7930 int nameLen;
7931 int padding0, padding1, padding2 = 0;
7932 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007933 int x;
7934 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007935# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007936 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939
7940 /*
7941 * If this menu is already torn off, move it to the mouse position.
7942 */
7943 if (IsWindow(menu->tearoff_handle))
7944 {
7945 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007946 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
7948 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7949 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7950 }
7951 return;
7952 }
7953
7954 /*
7955 * Create a new tearoff.
7956 */
7957 if (*title == MNU_HIDDEN_CHAR)
7958 title++;
7959
Bram Moolenaar734a8672019-12-02 22:49:38 +01007960 // Allocate memory to store the dialog template. It's made bigger when
7961 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 template_len = DLG_ALLOC_SIZE;
7963 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7964 if (p == NULL)
7965 return;
7966
7967 hwnd = GetDesktopWindow();
7968 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007969# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7971 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007972 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 use_lfSysmenu = TRUE;
7974 }
7975 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007976# endif
K.Takatad1c58992022-01-23 12:31:57 +00007977 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7978 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7979
7980 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
Bram Moolenaar734a8672019-12-02 22:49:38 +01007982 // Calculate width of a single space. Used for padding columns to the
7983 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007984 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
Bram Moolenaar734a8672019-12-02 22:49:38 +01007986 // Figure out max width of the text column, the accelerator column and the
7987 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 submenuWidth = 0;
7989 for (col = 0; col < 2; col++)
7990 {
7991 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007992 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007994 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 text = (col == 0) ? pmenu->dname : pmenu->actext;
7996 if (text != NULL && *text != NUL)
7997 {
7998 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7999 if (textWidth > columnWidths[col])
8000 columnWidths[col] = textWidth;
8001 }
8002 if (pmenu->children != NULL)
8003 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8004 }
8005 }
8006 if (columnWidths[1] == 0)
8007 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008008 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 if (submenuWidth != 0)
8010 columnWidths[0] += submenuWidth;
8011 else
8012 columnWidths[0] += spaceWidth;
8013 }
8014 else
8015 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008016 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8018 columnWidths[1] += submenuWidth;
8019 }
8020
8021 /*
8022 * Now find the total width of our 'menu'.
8023 */
8024 textWidth = columnWidths[0] + columnWidths[1];
8025 if (submenuWidth != 0)
8026 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008027 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8029 textWidth += submenuWidth;
8030 }
8031 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8032 if (textWidth > dlgwidth)
8033 dlgwidth = textWidth;
8034 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8035
Bram Moolenaar734a8672019-12-02 22:49:38 +01008036 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00008037 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8040 *p++ = LOWORD(lStyle);
8041 *p++ = HIWORD(lStyle);
8042 *p++ = LOWORD(lExtendedStyle);
8043 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008044 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008046 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008048 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 else
8050 *p++ = PixelToDialogX(initX); // x
8051 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008052 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 else
8054 *p++ = PixelToDialogY(initY); // y
8055 *p++ = PixelToDialogX(dlgwidth); // cx
8056 ptrueheight = p;
8057 *p++ = 0; // dialog height: changed later anyway
8058 *p++ = 0; // Menu
8059 *p++ = 0; // Class
8060
Bram Moolenaar734a8672019-12-02 22:49:38 +01008061 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008063 ? (LPSTR)title
8064 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 p += nchar;
8066
K.Takatad1c58992022-01-23 12:31:57 +00008067 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008068# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00008069 if (use_lfSysmenu)
8070 {
8071 // point size
8072 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8073 GetDeviceCaps(hdc, LOGPIXELSY));
8074 wcscpy(p, lfSysmenu.lfFaceName);
8075 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 }
K.Takatad1c58992022-01-23 12:31:57 +00008077 else
8078# endif
8079 {
8080 *p++ = DLG_FONT_POINT_SIZE; // point size
8081 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8082 }
8083 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084
8085 /*
8086 * Loop over all the items in the menu.
8087 * But skip over the tearbar.
8088 */
8089 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8090 menu = menu->children->next;
8091 else
8092 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008093 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 for ( ; menu != NULL; menu = menu->next)
8095 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008096 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 continue;
8098 if (menu_is_separator(menu->dname))
8099 {
8100 sepPadding += 3;
8101 continue;
8102 }
8103
Bram Moolenaar734a8672019-12-02 22:49:38 +01008104 // Check if there still is plenty of room in the template. Make it
8105 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8107 {
8108 WORD *newp;
8109
8110 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8111 if (newp != NULL)
8112 {
8113 template_len += 4096;
8114 mch_memmove(newp, pdlgtemplate,
8115 (char *)p - (char *)pdlgtemplate);
8116 p = newp + (p - pdlgtemplate);
8117 pnumitems = newp + (pnumitems - pdlgtemplate);
8118 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8119 LocalFree(LocalHandle(pdlgtemplate));
8120 pdlgtemplate = newp;
8121 }
8122 }
8123
Bram Moolenaar734a8672019-12-02 22:49:38 +01008124 // Figure out minimal length of this menu label. Use "name" for the
8125 // actual text, "dname" for estimating the displayed size. "name"
8126 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 len = nameLen = (int)STRLEN(menu->name);
8128 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8129 (int)STRLEN(menu->dname))) / spaceWidth;
8130 len += padding0;
8131
8132 if (menu->actext != NULL)
8133 {
8134 acLen = (int)STRLEN(menu->actext);
8135 len += acLen;
8136 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8137 }
8138 else
8139 textWidth = 0;
8140 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8141 len += padding1;
8142
8143 if (menu->children == NULL)
8144 {
8145 padding2 = submenuWidth / spaceWidth;
8146 len += padding2;
8147 menuID = (WORD)(menu->id);
8148 }
8149 else
8150 {
8151 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008152 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154
Bram Moolenaar734a8672019-12-02 22:49:38 +01008155 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008156 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (label == NULL)
8158 break;
8159
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008160 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008161 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008163 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 while (padding0-- > 0)
8165 *text++ = ' ';
8166 if (menu->actext != NULL)
8167 {
8168 STRNCPY(text, menu->actext, acLen);
8169 text += acLen;
8170 }
8171 while (padding1-- > 0)
8172 *text++ = ' ';
8173 if (menu->children != NULL)
8174 {
8175 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8176 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8177 }
8178 else
8179 {
8180 while (padding2-- > 0)
8181 *text++ = ' ';
8182 }
8183 *text = NUL;
8184
8185 /*
8186 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8187 * W95/NT4 it makes the tear-off look more like a menu.
8188 */
8189 p = add_dialog_element(p,
8190 BS_PUSHBUTTON|BS_LEFT,
8191 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8192 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8193 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8194 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008195 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 vim_free(label);
8197 (*pnumitems)++;
8198 }
8199
8200 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8201
8202
Bram Moolenaar734a8672019-12-02 22:49:38 +01008203 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008204 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008205 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 (LPDLGTEMPLATE)pdlgtemplate,
8207 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008208 (DLGPROC)tearoff_callback,
8209 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
8211 LocalFree(LocalHandle(pdlgtemplate));
8212 SelectFont(hdc, oldFont);
8213 DeleteObject(font);
8214 ReleaseDC(hwnd, hdc);
8215
8216 /*
8217 * Reassert ourselves as the active window. This is so that after creating
8218 * a tearoff, the user doesn't have to click with the mouse just to start
8219 * typing again!
8220 */
8221 (void)SetActiveWindow(s_hwnd);
8222
Bram Moolenaar734a8672019-12-02 22:49:38 +01008223 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 force_menu_update = TRUE;
8225}
8226#endif
8227
8228#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008229# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231/*
8232 * Create the toolbar, initially unpopulated.
8233 * (just like the menu, there are no defaults, it's all
8234 * set up through menu.vim)
8235 */
8236 static void
8237initialise_toolbar(void)
8238{
8239 InitCommonControls();
8240 s_toolbarhwnd = CreateToolbarEx(
8241 s_hwnd,
8242 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8243 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008244 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008245 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 IDR_TOOLBAR1, // id of initial bitmap
8247 NULL,
8248 0, // initial number of buttons
8249 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8250 TOOLBAR_BUTTON_HEIGHT,
8251 TOOLBAR_BUTTON_WIDTH,
8252 TOOLBAR_BUTTON_HEIGHT,
8253 sizeof(TBBUTTON)
8254 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008255
8256 // Remove transparency from the toolbar to prevent the main window
8257 // background colour showing through
8258 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8259 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8260
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008261 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262
8263 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008264
8265 update_toolbar_size();
8266}
8267
8268 static void
8269update_toolbar_size(void)
8270{
8271 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008272 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00008273
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008274 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00008275 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8276 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8277 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8278 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8279
8280 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8281 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8282 //TRACE("button size: %d, %d", w, h);
8283 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8284 gui.toolbar_height = h + 6;
8285
8286 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8287 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8288
8289 // TODO:
8290 // Currently, this function only updates the size of toolbar buttons.
8291 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292}
8293
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008294 static LRESULT CALLBACK
8295toolbar_wndproc(
8296 HWND hwnd,
8297 UINT uMsg,
8298 WPARAM wParam,
8299 LPARAM lParam)
8300{
8301 HandleMouseHide(uMsg, lParam);
8302 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8303}
8304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 static int
8306get_toolbar_bitmap(vimmenu_T *menu)
8307{
8308 int i = -1;
8309
8310 /*
8311 * Check user bitmaps first, unless builtin is specified.
8312 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008313 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {
8315 char_u fname[MAXPATHL];
8316 HANDLE hbitmap = NULL;
8317
8318 if (menu->iconfile != NULL)
8319 {
8320 gui_find_iconfile(menu->iconfile, fname, "bmp");
8321 hbitmap = LoadImage(
8322 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008323 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 IMAGE_BITMAP,
8325 TOOLBAR_BUTTON_WIDTH,
8326 TOOLBAR_BUTTON_HEIGHT,
8327 LR_LOADFROMFILE |
8328 LR_LOADMAP3DCOLORS
8329 );
8330 }
8331
8332 /*
8333 * If the LoadImage call failed, or the "icon=" file
8334 * didn't exist or wasn't specified, try the menu name
8335 */
8336 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008337 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008338# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008339 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008340# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008341 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 hbitmap = LoadImage(
8343 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008344 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 IMAGE_BITMAP,
8346 TOOLBAR_BUTTON_WIDTH,
8347 TOOLBAR_BUTTON_HEIGHT,
8348 LR_LOADFROMFILE |
8349 LR_LOADMAP3DCOLORS
8350 );
8351
8352 if (hbitmap != NULL)
8353 {
8354 TBADDBITMAP tbAddBitmap;
8355
8356 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008357 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358
8359 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8360 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008361 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 }
8363 }
8364 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8365 i = menu->iconidx;
8366
8367 return i;
8368}
8369#endif
8370
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008371#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8372 static void
8373initialise_tabline(void)
8374{
8375 InitCommonControls();
8376
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008377 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008378 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008379 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008380 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008381 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008382
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008383 gui.tabline_height = TABLINE_HEIGHT;
8384
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008385 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008386}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008387
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008388/*
8389 * Get tabpage_T from POINT.
8390 */
8391 static tabpage_T *
8392GetTabFromPoint(
8393 HWND hWnd,
8394 POINT pt)
8395{
8396 tabpage_T *ptp = NULL;
8397
8398 if (gui_mch_showing_tabline())
8399 {
8400 TCHITTESTINFO htinfo;
8401 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008402 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008403 if (s_tabhwnd == hWnd)
8404 {
8405 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8406 if (idx != -1)
8407 ptp = find_tabpage(idx + 1);
8408 }
8409 }
8410 return ptp;
8411}
8412
8413static POINT s_pt = {0, 0};
8414static HCURSOR s_hCursor = NULL;
8415
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008416 static LRESULT CALLBACK
8417tabline_wndproc(
8418 HWND hwnd,
8419 UINT uMsg,
8420 WPARAM wParam,
8421 LPARAM lParam)
8422{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008423 POINT pt;
8424 tabpage_T *tp;
8425 RECT rect;
8426 int nCenter;
8427 int idx0;
8428 int idx1;
8429
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008430 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008431
8432 switch (uMsg)
8433 {
8434 case WM_LBUTTONDOWN:
8435 {
8436 s_pt.x = GET_X_LPARAM(lParam);
8437 s_pt.y = GET_Y_LPARAM(lParam);
8438 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008439 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008440 break;
8441 }
8442 case WM_MOUSEMOVE:
8443 if (GetCapture() == hwnd
8444 && ((wParam & MK_LBUTTON)) != 0)
8445 {
8446 pt.x = GET_X_LPARAM(lParam);
8447 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008448 if (abs(pt.x - s_pt.x) >
8449 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008450 {
8451 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8452
8453 tp = GetTabFromPoint(hwnd, pt);
8454 if (tp != NULL)
8455 {
8456 idx0 = tabpage_index(curtab) - 1;
8457 idx1 = tabpage_index(tp) - 1;
8458
8459 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8460 nCenter = rect.left + (rect.right - rect.left) / 2;
8461
Bram Moolenaar734a8672019-12-02 22:49:38 +01008462 // Check if the mouse cursor goes over the center of
8463 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008464 if ((idx0 < idx1) && (nCenter < pt.x))
8465 {
8466 tabpage_move(idx1 + 1);
8467 update_screen(0);
8468 }
8469 else if ((idx1 < idx0) && (pt.x < nCenter))
8470 {
8471 tabpage_move(idx1);
8472 update_screen(0);
8473 }
8474 }
8475 }
8476 }
8477 break;
8478 case WM_LBUTTONUP:
8479 {
8480 if (GetCapture() == hwnd)
8481 {
8482 SetCursor(s_hCursor);
8483 ReleaseCapture();
8484 }
8485 break;
8486 }
regomne3bdef102022-09-26 20:48:32 +01008487 case WM_MBUTTONUP:
8488 {
8489 TCHITTESTINFO htinfo;
8490
8491 htinfo.pt.x = GET_X_LPARAM(lParam);
8492 htinfo.pt.y = GET_Y_LPARAM(lParam);
8493 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8494 if (idx0 != -1)
8495 {
8496 idx0 += 1;
8497 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8498 }
8499 break;
8500 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008501 default:
8502 break;
8503 }
8504
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008505 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8506}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008507#endif
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8510/*
8511 * Make the GUI window come to the foreground.
8512 */
8513 void
8514gui_mch_set_foreground(void)
8515{
8516 if (IsIconic(s_hwnd))
8517 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8518 SetForegroundWindow(s_hwnd);
8519}
8520#endif
8521
8522#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8523 static void
8524dyn_imm_load(void)
8525{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008526 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (hLibImm == NULL)
8528 return;
8529
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008531 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008533 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008535 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008537 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008539 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008541 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008542 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008543 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008544 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008545 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008547 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008549 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008550 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008551 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
K.Takatab0b2b732022-01-19 12:59:21 +00008553 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 || pImmGetContext == NULL
8555 || pImmAssociateContext == NULL
8556 || pImmReleaseContext == NULL
8557 || pImmGetOpenStatus == NULL
8558 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008559 || pImmGetCompositionFontW == NULL
8560 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008562 || pImmGetConversionStatus == NULL
8563 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
8565 FreeLibrary(hLibImm);
8566 hLibImm = NULL;
8567 pImmGetContext = NULL;
8568 return;
8569 }
8570
8571 return;
8572}
8573
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#endif
8575
8576#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8577
8578# ifdef FEAT_XPM_W32
8579# define IMAGE_XPM 100
8580# endif
8581
8582typedef struct _signicon_t
8583{
8584 HANDLE hImage;
8585 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008586# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008587 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589} signicon_t;
8590
8591 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008592gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 signicon_t *sign;
8595 int x, y, w, h;
8596
8597 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8598 return;
8599
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008600# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008601 if (IS_ENABLE_DIRECTX())
8602 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008603# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008604
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 x = TEXT_X(col);
8606 y = TEXT_Y(row);
8607 w = gui.char_width * 2;
8608 h = gui.char_height;
8609 switch (sign->uType)
8610 {
8611 case IMAGE_BITMAP:
8612 {
8613 HDC hdcMem;
8614 HBITMAP hbmpOld;
8615
8616 hdcMem = CreateCompatibleDC(s_hdc);
8617 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8618 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8619 SelectObject(hdcMem, hbmpOld);
8620 DeleteDC(hdcMem);
8621 }
8622 break;
8623 case IMAGE_ICON:
8624 case IMAGE_CURSOR:
8625 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8626 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008627# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 case IMAGE_XPM:
8629 {
8630 HDC hdcMem;
8631 HBITMAP hbmpOld;
8632
8633 hdcMem = CreateCompatibleDC(s_hdc);
8634 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008635 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8637
8638 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008639 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8641 SelectObject(hdcMem, hbmpOld);
8642 DeleteDC(hdcMem);
8643 }
8644 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 }
8647}
8648
8649 static void
8650close_signicon_image(signicon_t *sign)
8651{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008652 if (sign == NULL)
8653 return;
8654
8655 switch (sign->uType)
8656 {
8657 case IMAGE_BITMAP:
8658 DeleteObject((HGDIOBJ)sign->hImage);
8659 break;
8660 case IMAGE_CURSOR:
8661 DestroyCursor((HCURSOR)sign->hImage);
8662 break;
8663 case IMAGE_ICON:
8664 DestroyIcon((HICON)sign->hImage);
8665 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008666# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008667 case IMAGE_XPM:
8668 DeleteObject((HBITMAP)sign->hImage);
8669 DeleteObject((HBITMAP)sign->hShape);
8670 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008671# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673}
8674
8675 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008676gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 signicon_t sign, *psign;
8679 char_u *ext;
8680
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008682 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 if (ext > signfile)
8684 {
8685 int do_load = 1;
8686
8687 if (!STRICMP(ext, ".bmp"))
8688 sign.uType = IMAGE_BITMAP;
8689 else if (!STRICMP(ext, ".ico"))
8690 sign.uType = IMAGE_ICON;
8691 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8692 sign.uType = IMAGE_CURSOR;
8693 else
8694 do_load = 0;
8695
8696 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008697 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 gui.char_width * 2, gui.char_height,
8699 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008700# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 if (!STRICMP(ext, ".xpm"))
8702 {
8703 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008704 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8705 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709
8710 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008711 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 *psign = sign;
8713
8714 if (!psign)
8715 {
8716 if (sign.hImage)
8717 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008718 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720 return (void *)psign;
8721
8722}
8723
8724 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008725gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008727 if (sign == NULL)
8728 return;
8729
8730 close_signicon_image((signicon_t *)sign);
8731 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008735#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
Bram Moolenaar734a8672019-12-02 22:49:38 +01008737/*
8738 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008739 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008741 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8743 * to get current mouse position).
8744 *
8745 * Trying to use as more Windows services as possible, and as less
8746 * IE version as possible :)).
8747 *
8748 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8749 * BalloonEval struct.
8750 * 2) Enable/Disable simply create/kill BalloonEval Timer
8751 * 3) When there was enough inactivity, timer procedure posts
8752 * async request to debugger
8753 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8754 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008755 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 */
8757
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008758 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008759make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008760{
K.Takata76687d22022-01-25 10:31:37 +00008761 TOOLINFOW *pti;
8762 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008763
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008764 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008765 if (pti == NULL)
8766 return;
8767
8768 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8769 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8770 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008771 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008772
8773 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8774 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8775
K.Takata76687d22022-01-25 10:31:37 +00008776 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008777 pti->uFlags = TTF_SUBCLASS;
8778 pti->hwnd = beval->target;
8779 pti->hinst = 0; // Don't use string resources
8780 pti->uId = ID_BEVAL_TOOLTIP;
8781
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008782 pti->lpszText = LPSTR_TEXTCALLBACKW;
8783 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8784 pti->lParam = (LPARAM)beval->tofree;
8785 // switch multiline tooltips on
8786 if (GetClientRect(s_textArea, &rect))
8787 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8788 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008789
8790 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8791 pti->rect.left = pt.x - 3;
8792 pti->rect.top = pt.y - 3;
8793 pti->rect.right = pt.x + 3;
8794 pti->rect.bottom = pt.y + 3;
8795
8796 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8797 // Make tooltip appear sooner.
8798 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8799 // I've performed some tests and it seems the longest possible life time
8800 // of tooltip is 30 seconds.
8801 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8802 /*
8803 * HACK: force tooltip to appear, because it'll not appear until
8804 * first mouse move. D*mn M$
8805 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8806 */
8807 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8808 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8809 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008810}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008811
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008813delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008815 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816}
8817
8818 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008819beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008820 HWND hwnd UNUSED,
8821 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008822 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008823 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 POINT pt;
8826 RECT rect;
8827
8828 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8829 return;
8830
8831 GetCursorPos(&pt);
8832 if (WindowFromPoint(pt) != s_textArea)
8833 return;
8834
8835 ScreenToClient(s_textArea, &pt);
8836 GetClientRect(s_textArea, &rect);
8837 if (!PtInRect(&rect, pt))
8838 return;
8839
K.Takataa8ec4912022-02-03 14:32:33 +00008840 if (last_user_activity > 0
8841 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 && (cur_beval->showState != ShS_PENDING
8843 || abs(cur_beval->x - pt.x) > 3
8844 || abs(cur_beval->y - pt.y) > 3))
8845 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008846 // Pointer resting in one place long enough, it's time to show
8847 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 cur_beval->showState = ShS_PENDING;
8849 cur_beval->x = pt.x;
8850 cur_beval->y = pt.y;
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (cur_beval->msgCB != NULL)
8853 (*cur_beval->msgCB)(cur_beval, 0);
8854 }
8855}
8856
8857 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008858gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
K.Takataa8ec4912022-02-03 14:32:33 +00008860 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
8863 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008864gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (beval == NULL)
8867 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008868 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8869 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870}
8871
8872 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008873gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
8875 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008876
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008877 vim_free(beval->msg);
8878 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8879 if (beval->msg == NULL)
8880 {
8881 delete_tooltip(beval);
8882 beval->showState = ShS_NEUTRAL;
8883 return;
8884 }
8885
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 if (beval->showState == ShS_SHOWING)
8887 return;
8888 GetCursorPos(&pt);
8889 ScreenToClient(s_textArea, &pt);
8890
8891 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008893 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 gui_mch_disable_beval_area(cur_beval);
8895 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008896 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898}
8899
8900 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008901gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008902 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008903 char_u *mesg,
8904 void (*mesgCB)(BalloonEval *, int),
8905 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008907 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 BalloonEval *beval;
8909
8910 if (mesg != NULL && mesgCB != NULL)
8911 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008912 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 return NULL;
8914 }
8915
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008916 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 if (beval != NULL)
8918 {
8919 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
8921 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 beval->msg = mesg;
8923 beval->msgCB = mesgCB;
8924 beval->clientData = clientData;
8925
8926 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 cur_beval = beval;
8928
8929 if (p_beval)
8930 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 }
8932 return beval;
8933}
8934
8935 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008936Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008938 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 return;
8940
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008941 if (cur_beval == NULL)
8942 return;
8943
8944 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008946 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008947 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008948 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 delete_tooltip(cur_beval);
8950 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951
8952 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008953 break;
8954 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008955 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008956 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008957 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008958 info->lpszText = (LPSTR)info->lParam;
8959 info->uFlags |= TTF_DI_SETITEM;
8960 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008961 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008962 case TTN_GETDISPINFOW:
8963 {
8964 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008965 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008966 info->lpszText = (LPWSTR)info->lParam;
8967 info->uFlags |= TTF_DI_SETITEM;
8968 }
8969 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 }
8971}
8972
8973 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008974track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975{
8976 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8977 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008978 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979}
8980
8981 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008982gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008984# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008985 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008986# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008987 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 vim_free(beval);
8989}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008990#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991
8992#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8993/*
8994 * We have multiple signs to draw at the same location. Draw the
8995 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8996 */
8997 void
8998netbeans_draw_multisign_indicator(int row)
8999{
9000 int i;
9001 int y;
9002 int x;
9003
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009004 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009005 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009006
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 x = 0;
9008 y = TEXT_Y(row);
9009
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009010# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009011 if (IS_ENABLE_DIRECTX())
9012 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009013# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009014
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 for (i = 0; i < gui.char_height - 3; i++)
9016 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9017
9018 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9019 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9020 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9021 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9022 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9023 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9024 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9025}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009026#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009027
9028#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009029
Christopher Plewright20b795e2022-12-20 20:01:58 +00009030// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9031// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00009032// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00009033 static int
9034test_gui_w32_sendevent_mouse(dict_T *args)
9035{
9036 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009037 return FALSE;
9038
Christopher Plewright20b795e2022-12-20 20:01:58 +00009039 // Note: "move" is optional, requires fewer arguments
9040 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009041
Christopher Plewright20b795e2022-12-20 20:01:58 +00009042 if (!move && (!dict_has_key(args, "button")
9043 || !dict_has_key(args, "multiclick")
9044 || !dict_has_key(args, "modifiers")))
9045 return FALSE;
9046
9047 int row = (int)dict_get_number(args, "row");
9048 int col = (int)dict_get_number(args, "col");
9049
9050 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009051 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00009052 // the "move" argument expects row and col coordnates to be in pixels,
9053 // unless "cell" is specified and is TRUE.
9054 if (dict_get_bool(args, "cell", FALSE))
9055 {
9056 // calculate the middle of the character cell
9057 // Note: Cell coordinates are 1-based from vimscript
9058 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9059 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9060 gui_mouse_moved(pX, pY);
9061 }
9062 else
9063 gui_mouse_moved(col, row);
9064 }
9065 else
9066 {
9067 int button = (int)dict_get_number(args, "button");
9068 int repeated_click = (int)dict_get_number(args, "multiclick");
9069 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009070
Christopher Plewright20b795e2022-12-20 20:01:58 +00009071 // Reset the scroll values to known values.
9072 // XXX: Remove this when/if the scroll step is made configurable.
9073 mouse_set_hor_scroll_step(6);
9074 mouse_set_vert_scroll_step(3);
9075
9076 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9077 repeated_click, mods);
9078 }
9079 return TRUE;
9080}
9081
9082 static int
9083test_gui_w32_sendevent_keyboard(dict_T *args)
9084{
9085 INPUT inputs[1];
9086 INPUT modkeys[3];
9087 SecureZeroMemory(inputs, sizeof(INPUT));
9088 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9089
9090 char_u *event = dict_get_string(args, "event", TRUE);
9091
9092 if (event && (STRICMP(event, "keydown") == 0
9093 || STRICMP(event, "keyup") == 0))
9094 {
9095 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009096 if (vkCode <= 0 || vkCode >= 0xFF)
9097 {
9098 semsg(_(e_invalid_argument_nr), (long)vkCode);
9099 return FALSE;
9100 }
9101
Christopher Plewright20b795e2022-12-20 20:01:58 +00009102 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9103 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9104 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9105 || vkCode == VK_LMENU || vkCode == VK_RMENU );
9106
9107 BOOL unwrapMods = FALSE;
9108 int mods = (int)dict_get_number(args, "modifiers");
9109
9110 // If there are modifiers in the args, and it is not a keyup event and
9111 // vkCode is not a modifier key, then we generate virtual modifier key
9112 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009113 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009114 {
9115 int n = 0;
9116 if (mods & MOD_MASK_SHIFT)
9117 {
9118 modkeys[n].type = INPUT_KEYBOARD;
9119 modkeys[n].ki.wVk = VK_LSHIFT;
9120 n++;
9121 }
9122 if (mods & MOD_MASK_CTRL)
9123 {
9124 modkeys[n].type = INPUT_KEYBOARD;
9125 modkeys[n].ki.wVk = VK_LCONTROL;
9126 n++;
9127 }
9128 if (mods & MOD_MASK_ALT)
9129 {
9130 modkeys[n].type = INPUT_KEYBOARD;
9131 modkeys[n].ki.wVk = VK_LMENU;
9132 n++;
9133 }
9134 if (n)
9135 {
9136 (void)SetForegroundWindow(s_hwnd);
9137 SendInput(n, modkeys, sizeof(INPUT));
9138 }
9139 }
9140
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009141 inputs[0].type = INPUT_KEYBOARD;
9142 inputs[0].ki.wVk = vkCode;
9143 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009144 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009145 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009146 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009147 unwrapMods = TRUE;
9148 }
9149
K.Takatafef38d82022-09-07 19:03:42 +01009150 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009151 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00009152 vim_free(event);
9153
9154 if (unwrapMods)
9155 {
9156 modkeys[0].type = INPUT_KEYBOARD;
9157 modkeys[0].ki.wVk = VK_LSHIFT;
9158 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9159
9160 modkeys[1].type = INPUT_KEYBOARD;
9161 modkeys[1].ki.wVk = VK_LCONTROL;
9162 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9163
9164 modkeys[2].type = INPUT_KEYBOARD;
9165 modkeys[2].ki.wVk = VK_LMENU;
9166 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9167
9168 (void)SetForegroundWindow(s_hwnd);
9169 SendInput(3, modkeys, sizeof(INPUT));
9170 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009171 }
9172 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00009173 {
9174 if (event == NULL)
9175 {
9176 semsg(_(e_missing_argument_str), "event");
9177 }
9178 else
9179 {
9180 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9181 vim_free(event);
9182 }
9183 return FALSE;
9184 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009185 return TRUE;
9186}
Christopher Plewright20b795e2022-12-20 20:01:58 +00009187
Anton Sharonov68d94722024-01-23 23:19:02 +01009188 static int
9189test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9190{
9191 int handled = 0;
9192 char_u *strategy = dict_get_string(args, "strategy", TRUE);
9193
9194 if (strategy)
9195 {
9196 if (STRICMP(strategy, "classic") == 0)
9197 {
9198 handled = 1;
9199 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9200 }
9201 else if (STRICMP(strategy, "experimental") == 0)
9202 {
9203 handled = 1;
9204 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9205 }
9206 }
9207
9208 if (!handled)
9209 {
9210 if (strategy == NULL)
9211 {
9212 semsg(_(e_missing_argument_str), "strategy");
9213 }
9214 else
9215 {
9216 semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9217 vim_free(strategy);
9218 }
9219 return FALSE;
9220 }
9221 return TRUE;
9222}
9223
9224
Christopher Plewright20b795e2022-12-20 20:01:58 +00009225 int
9226test_gui_w32_sendevent(char_u *event, dict_T *args)
9227{
9228 if (STRICMP(event, "key") == 0)
9229 return test_gui_w32_sendevent_keyboard(args);
9230 else if (STRICMP(event, "mouse") == 0)
9231 return test_gui_w32_sendevent_mouse(args);
Anton Sharonov68d94722024-01-23 23:19:02 +01009232 else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9233 return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
Christopher Plewright20b795e2022-12-20 20:01:58 +00009234 else
9235 {
9236 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9237 return FALSE;
9238 }
9239}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009240#endif