blob: f628dd663b85cc12281b6f04f0eee8aca03f74fe [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
277
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100278# ifndef __MINGW32__
279# include <shellapi.h>
280# endif
K.Takata2da11a42022-09-10 13:03:12 +0100281# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100282# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100283
Bram Moolenaar734a8672019-12-02 22:49:38 +0100284#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100285
286#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100287# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288#endif
289
Bram Moolenaar734a8672019-12-02 22:49:38 +0100290// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100291#define DLG_PADDING_X 10
292#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100293#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100294#define DLG_VERT_PADDING_Y 4
295#define DLG_ICON_WIDTH 34
296#define DLG_ICON_HEIGHT 34
297#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000298#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299#define DLG_FONT_POINT_SIZE 8
300#define DLG_MIN_MAX_WIDTH 400
301#define DLG_MIN_MAX_HEIGHT 400
302
Bram Moolenaar734a8672019-12-02 22:49:38 +0100303#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100304
K.Takatac81e9bf2022-01-16 14:15:49 +0000305#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100306# define WM_DPICHANGED 0x02E0
307#endif
308
Ken Takata7086b3e2023-10-02 21:26:03 +0200309#ifndef WM_GETDPISCALEDSIZE
310# define WM_GETDPISCALEDSIZE 0x02E4
311#endif
312
LemonBoy365d8f72022-05-05 19:23:07 +0100313#ifndef WM_MOUSEHWHEEL
314# define WM_MOUSEHWHEEL 0x020E
315#endif
316
317#ifndef SPI_GETWHEELSCROLLCHARS
318# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000319#endif
320
LemonBoyc27747e2022-05-07 12:25:40 +0100321#ifndef SPI_SETWHEELSCROLLCHARS
322# define SPI_SETWHEELSCROLLCHARS 0x006D
323#endif
324
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100327 * Define a few things for generating prototypes. This is just to avoid
328 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330# define APIENTRY
331# define CALLBACK
332# define CONST
333# define FAR
334# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200335# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200336# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100337# define _cdecl
338typedef int BOOL;
339typedef int BYTE;
340typedef int DWORD;
341typedef int WCHAR;
342typedef int ENUMLOGFONT;
343typedef int FINDREPLACE;
344typedef int HANDLE;
345typedef int HBITMAP;
346typedef int HBRUSH;
347typedef int HDROP;
348typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100349typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100350typedef int LPARAM;
351typedef int LPCREATESTRUCT;
352typedef int LPCSTR;
353typedef int LPCTSTR;
354typedef int LPRECT;
355typedef int LPSTR;
356typedef int LPWINDOWPOS;
357typedef int LPWORD;
358typedef int LRESULT;
359typedef int HRESULT;
360# undef MSG
361typedef int MSG;
362typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100363typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100364typedef int OSVERSIONINFO;
365typedef int PWORD;
366typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100367typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100368typedef int UINT;
369typedef int WORD;
370typedef int WPARAM;
371typedef int POINT;
372typedef void *HINSTANCE;
373typedef void *HMENU;
374typedef void *HWND;
375typedef void *HDC;
376typedef void VOID;
377typedef int LPNMHDR;
378typedef int LONG;
379typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200380typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200381typedef int COLORREF;
382typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100383#endif
384
K.Takata45f9cfb2022-01-21 11:11:00 +0000385static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100386static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100387static void clear_rect(RECT *rcp);
388
Bram Moolenaar734a8672019-12-02 22:49:38 +0100389static WORD s_dlgfntheight; // height of the dialog font
390static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100391
392#ifdef FEAT_MENU
393static HMENU s_menuBar = NULL;
394#endif
395#ifdef FEAT_TEAROFF
396static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100397static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100398#endif
399
Bram Moolenaar734a8672019-12-02 22:49:38 +0100400// Flag that is set while processing a message that must not be interrupted by
401// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100402static int s_busy_processing = FALSE;
403
Bram Moolenaar734a8672019-12-02 22:49:38 +0100404static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100405
406#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000407static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200408static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100409static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200410static int s_findrep_is_find; // TRUE for find dialog, FALSE
411 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100412#endif
413
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100414HWND s_hwnd = NULL;
415static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100416static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100417
418#ifdef FEAT_TOOLBAR
419static HWND s_toolbarhwnd = NULL;
420static WNDPROC s_toolbar_wndproc = NULL;
421#endif
422
423#ifdef FEAT_GUI_TABLINE
424static HWND s_tabhwnd = NULL;
425static WNDPROC s_tabline_wndproc = NULL;
426static int showing_tabline = 0;
427#endif
428
429static WPARAM s_wParam = 0;
430static LPARAM s_lParam = 0;
431
432static HWND s_textArea = NULL;
433static UINT s_uMsg = 0;
434
Bram Moolenaar734a8672019-12-02 22:49:38 +0100435static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100436
437static int s_need_activate = FALSE;
438
Bram Moolenaar734a8672019-12-02 22:49:38 +0100439// This variable is set when waiting for an event, which is the only moment
440// scrollbar dragging can be done directly. It's not allowed while commands
441// are executed, because it may move the cursor and that may cause unexpected
442// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100443static int allow_scrollbar = FALSE;
444
K.Takatac81e9bf2022-01-16 14:15:49 +0000445#ifndef _DPI_AWARENESS_CONTEXTS_
446typedef HANDLE DPI_AWARENESS_CONTEXT;
447
448typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000449 DPI_AWARENESS_INVALID = -1,
450 DPI_AWARENESS_UNAWARE = 0,
451 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000452 DPI_AWARENESS_PER_MONITOR_AWARE = 2
453} DPI_AWARENESS;
454
K.Takatab0b2b732022-01-19 12:59:21 +0000455# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
456# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000457# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
458# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
459# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
460#endif
461
462#define DEFAULT_DPI 96
463static int s_dpi = DEFAULT_DPI;
464static BOOL s_in_dpichanged = FALSE;
465static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
Ken Takata7086b3e2023-10-02 21:26:03 +0200466static RECT s_suggested_rect;
K.Takatac81e9bf2022-01-16 14:15:49 +0000467
468static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
469static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
470static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
471//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
472static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
473static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
474
K.Takatac81e9bf2022-01-16 14:15:49 +0000475 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100476stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000477{
478 return GetSystemMetrics(nIndex);
479}
480
481 static int
482adjust_fontsize_by_dpi(int size)
483{
484 return size * s_dpi / (int)pGetDpiForSystem();
485}
486
487 static int
488adjust_by_system_dpi(int size)
489{
490 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
491}
492
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100493#if defined(FEAT_DIRECTX)
494 static int
495directx_enabled(void)
496{
497 if (s_dwc != NULL)
498 return 1;
499 else if (s_directx_load_attempted)
500 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100501 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100502 DWrite_Init();
503 s_directx_load_attempted = 1;
504 s_dwc = DWriteContext_Open();
505 directx_binddc();
506 return s_dwc != NULL ? 1 : 0;
507}
508
509 static void
510directx_binddc(void)
511{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000512 if (s_textArea == NULL)
513 return;
514
515 RECT rect;
516 GetClientRect(s_textArea, &rect);
517 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100518}
519#endif
520
Bram Moolenaar734a8672019-12-02 22:49:38 +0100521extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100522
523static struct
524{
525 UINT key_sym;
526 char_u vim_code0;
527 char_u vim_code1;
528} special_keys[] =
529{
530 {VK_UP, 'k', 'u'},
531 {VK_DOWN, 'k', 'd'},
532 {VK_LEFT, 'k', 'l'},
533 {VK_RIGHT, 'k', 'r'},
534
535 {VK_F1, 'k', '1'},
536 {VK_F2, 'k', '2'},
537 {VK_F3, 'k', '3'},
538 {VK_F4, 'k', '4'},
539 {VK_F5, 'k', '5'},
540 {VK_F6, 'k', '6'},
541 {VK_F7, 'k', '7'},
542 {VK_F8, 'k', '8'},
543 {VK_F9, 'k', '9'},
544 {VK_F10, 'k', ';'},
545
546 {VK_F11, 'F', '1'},
547 {VK_F12, 'F', '2'},
548 {VK_F13, 'F', '3'},
549 {VK_F14, 'F', '4'},
550 {VK_F15, 'F', '5'},
551 {VK_F16, 'F', '6'},
552 {VK_F17, 'F', '7'},
553 {VK_F18, 'F', '8'},
554 {VK_F19, 'F', '9'},
555 {VK_F20, 'F', 'A'},
556
557 {VK_F21, 'F', 'B'},
558#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100559 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100560#endif
561 {VK_F22, 'F', 'C'},
562 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100563 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100564
565 {VK_HELP, '%', '1'},
566 {VK_BACK, 'k', 'b'},
567 {VK_INSERT, 'k', 'I'},
568 {VK_DELETE, 'k', 'D'},
569 {VK_HOME, 'k', 'h'},
570 {VK_END, '@', '7'},
571 {VK_PRIOR, 'k', 'P'},
572 {VK_NEXT, 'k', 'N'},
573 {VK_PRINT, '%', '9'},
574 {VK_ADD, 'K', '6'},
575 {VK_SUBTRACT, 'K', '7'},
576 {VK_DIVIDE, 'K', '8'},
577 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100578 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100579 {VK_DECIMAL, 'K', 'B'},
580
581 {VK_NUMPAD0, 'K', 'C'},
582 {VK_NUMPAD1, 'K', 'D'},
583 {VK_NUMPAD2, 'K', 'E'},
584 {VK_NUMPAD3, 'K', 'F'},
585 {VK_NUMPAD4, 'K', 'G'},
586 {VK_NUMPAD5, 'K', 'H'},
587 {VK_NUMPAD6, 'K', 'I'},
588 {VK_NUMPAD7, 'K', 'J'},
589 {VK_NUMPAD8, 'K', 'K'},
590 {VK_NUMPAD9, 'K', 'L'},
591
Bram Moolenaar734a8672019-12-02 22:49:38 +0100592 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100593 {VK_SPACE, ' ', NUL},
594 {VK_TAB, TAB, NUL},
595 {VK_ESCAPE, ESC, NUL},
596 {NL, NL, NUL},
597 {CAR, CAR, NUL},
598
Bram Moolenaar734a8672019-12-02 22:49:38 +0100599 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100600 {0, 0, 0}
601};
602
Bram Moolenaar734a8672019-12-02 22:49:38 +0100603// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100604static int s_button_pending = -1;
605
Bram Moolenaar734a8672019-12-02 22:49:38 +0100606// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
607// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100608static int s_getting_focus = FALSE;
609
610static int s_x_pending;
611static int s_y_pending;
612static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000613static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100614static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100615static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200616static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
617 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100618
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100619#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100620// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100621static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000622static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100623#endif
624
625/*
626 * For control IME.
627 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100628 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100629 */
K.Takata4ac893f2022-01-20 12:44:28 +0000630#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100631// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100632static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100633// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100634static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100635#endif
636
637#ifdef FEAT_MBYTE_IME
638static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
639#endif
640
641#if defined(FEAT_BROWSE)
642static char_u *convert_filter(char_u *s);
643#endif
644
645#ifdef DEBUG_PRINT_ERROR
646/*
647 * Print out the last Windows error message
648 */
649 static void
650print_windows_error(void)
651{
652 LPVOID lpMsgBuf;
653
654 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
655 NULL, GetLastError(),
656 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
657 (LPTSTR) &lpMsgBuf, 0, NULL);
658 TRACE1("Error: %s\n", lpMsgBuf);
659 LocalFree(lpMsgBuf);
660}
661#endif
662
663/*
664 * Cursor blink functions.
665 *
666 * This is a simple state machine:
667 * BLINK_NONE not blinking at all
668 * BLINK_OFF blinking, cursor is not shown
669 * BLINK_ON blinking, cursor is shown
670 */
671
672#define BLINK_NONE 0
673#define BLINK_OFF 1
674#define BLINK_ON 2
675
676static int blink_state = BLINK_NONE;
677static long_u blink_waittime = 700;
678static long_u blink_ontime = 400;
679static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000680static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681
Bram Moolenaar703a8042016-06-04 16:24:32 +0200682 int
683gui_mch_is_blinking(void)
684{
685 return blink_state != BLINK_NONE;
686}
687
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200688 int
689gui_mch_is_blink_off(void)
690{
691 return blink_state == BLINK_OFF;
692}
693
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100694 void
695gui_mch_set_blinking(long wait, long on, long off)
696{
697 blink_waittime = wait;
698 blink_ontime = on;
699 blink_offtime = off;
700}
701
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100702 static VOID CALLBACK
703_OnBlinkTimer(
704 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100705 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000706 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100707 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100708{
709 MSG msg;
710
711 /*
712 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
713 */
714
715 KillTimer(NULL, idEvent);
716
Bram Moolenaar734a8672019-12-02 22:49:38 +0100717 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000718 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719 ;
720
721 if (blink_state == BLINK_ON)
722 {
723 gui_undraw_cursor();
724 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000725 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100726 }
727 else
728 {
729 gui_update_cursor(TRUE, FALSE);
730 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000731 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100732 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100733 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100734}
735
736 static void
737gui_mswin_rm_blink_timer(void)
738{
739 MSG msg;
740
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000741 if (blink_timer == 0)
742 return;
743
744 KillTimer(NULL, blink_timer);
745 // Eat spurious WM_TIMER messages
746 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
747 ;
748 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100749}
750
751/*
752 * Stop the cursor blinking. Show the cursor if it wasn't shown.
753 */
754 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100755gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100756{
757 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100758 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100759 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100761 gui_mch_flush();
762 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100763 blink_state = BLINK_NONE;
764}
765
766/*
767 * Start the cursor blinking. If it was already blinking, this restarts the
768 * waiting time and shows the cursor.
769 */
770 void
771gui_mch_start_blink(void)
772{
773 gui_mswin_rm_blink_timer();
774
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100776 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
777 {
K.Takataa8ec4912022-02-03 14:32:33 +0000778 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100779 blink_state = BLINK_ON;
780 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100781 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100782 }
783}
784
785/*
786 * Call-back routines.
787 */
788
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100789 static VOID CALLBACK
790_OnTimer(
791 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100792 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000793 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100794 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100795{
796 MSG msg;
797
798 /*
799 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
800 */
801 KillTimer(NULL, idEvent);
802 s_timed_out = TRUE;
803
Bram Moolenaar734a8672019-12-02 22:49:38 +0100804 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000805 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100806 ;
807 if (idEvent == s_wait_timer)
808 s_wait_timer = 0;
809}
810
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 static void
812_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100813 HWND hwnd UNUSED,
814 UINT ch UNUSED,
815 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816{
Anton Sharonov68d94722024-01-23 23:19:02 +0100817 dead_key = DEAD_KEY_SET_DEFAULT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100818}
819
820/*
821 * Convert Unicode character "ch" to bytes in "string[slen]".
822 * When "had_alt" is TRUE the ALT key was included in "ch".
823 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200824 * Because the Windows API uses UTF-16, we have to deal with surrogate
825 * pairs; this is where we choose to deal with them: if "ch" is a high
826 * surrogate, it will be stored, and the length returned will be zero; the next
827 * char_to_string call will then include the high surrogate, decoding the pair
828 * of UTF-16 code units to a single Unicode code point, presuming it is the
829 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100830 */
831 static int
832char_to_string(int ch, char_u *string, int slen, int had_alt)
833{
834 int len;
835 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100836 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200837 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100838
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200839 if (surrogate_pending_ch != 0)
840 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100841 // We don't guarantee ch is a low surrogate to match the high surrogate
842 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200843 wstring[0] = surrogate_pending_ch;
844 wstring[1] = ch;
845 surrogate_pending_ch = 0;
846 len = 2;
847 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100848 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200849 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100850 // We don't have the entire code point yet, only the first UTF-16 code
851 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200852 surrogate_pending_ch = ch;
853 return 0;
854 }
855 else
856 {
857 wstring[0] = ch;
858 len = 1;
859 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200860
Bram Moolenaar734a8672019-12-02 22:49:38 +0100861 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
862 // "enc_codepage" is non-zero use the standard Win32 function,
863 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200864 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100865 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200866 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
867 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100868 // If we had included the ALT key into the character but now the
869 // upper bit is no longer set, that probably means the conversion
870 // failed. Convert the original character and set the upper bit
871 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200872 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100873 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200874 wstring[0] = ch & 0x7f;
875 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
876 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100877 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200878 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879 }
880 }
881 else
882 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200883 ws = utf16_to_enc(wstring, &len);
884 if (ws == NULL)
885 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100886 else
887 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100888 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200889 len = slen;
890 mch_memmove(string, ws, len);
891 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100892 }
893 }
894
895 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100896 {
897 string[0] = ch;
898 len = 1;
899 }
900
901 for (i = 0; i < len; ++i)
902 if (string[i] == CSI && len <= slen - 2)
903 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100904 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100905 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
906 string[++i] = KS_EXTRA;
907 string[++i] = (int)KE_CSI;
908 len += 2;
909 }
910
911 return len;
912}
913
Anton Sharonov68d94722024-01-23 23:19:02 +0100914/*
915 * Experimental implementation, introduced in v8.2.4807
916 * "processing key event in Win32 GUI is not ideal"
917 *
918 * TODO: since introduction, this experimental function started
919 * to be used as well outside of original key press/processing
920 * area, and usages not via "get_active_modifiers_via_ptr" should
921 * be watched.
922 */
LemonBoy45684c62022-04-24 15:46:42 +0100923 static int
Anton Sharonov68d94722024-01-23 23:19:02 +0100924get_active_modifiers_experimental(void)
LemonBoy45684c62022-04-24 15:46:42 +0100925{
926 int modifiers = 0;
927
928 if (GetKeyState(VK_CONTROL) & 0x8000)
929 modifiers |= MOD_MASK_CTRL;
930 if (GetKeyState(VK_SHIFT) & 0x8000)
931 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100932 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
933 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100934 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100935 if (GetKeyState(VK_LMENU) & 0x8000)
936 modifiers |= MOD_MASK_ALT;
937 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
938 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100939 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
940 // is pressed, Windows claims that Ctrl is hold as well. That way we can
941 // recognize Right-ALT alone and be sure that not AltGr is hold.
942 if (!(GetKeyState(VK_CONTROL) & 0x8000)
943 && (GetKeyState(VK_RMENU) & 0x8000)
944 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
945 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100946
947 return modifiers;
948}
949
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950/*
Anton Sharonov68d94722024-01-23 23:19:02 +0100951 * "Classic" implementation, existing prior to v8.2.4807
952 */
953 static int
954get_active_modifiers_classic(void)
955{
956 int modifiers = 0;
957
958 if (GetKeyState(VK_SHIFT) & 0x8000)
959 modifiers |= MOD_MASK_SHIFT;
960 /*
961 * Don't use caps-lock as shift, because these are special keys
962 * being considered here, and we only want letters to get
963 * shifted -- webb
964 */
965 /*
966 if (GetKeyState(VK_CAPITAL) & 0x0001)
967 modifiers ^= MOD_MASK_SHIFT;
968 */
969 if (GetKeyState(VK_CONTROL) & 0x8000)
970 modifiers |= MOD_MASK_CTRL;
971 if (GetKeyState(VK_MENU) & 0x8000)
972 modifiers |= MOD_MASK_ALT;
973
974 return modifiers;
975}
976
977 static int
978get_active_modifiers(void)
979{
980 return get_active_modifiers_experimental();
981}
982
983 static int
984get_active_modifiers_via_ptr(void)
985{
986 // marshal to corresponding implementation
987 return keycode_trans_strategy_used->ptr_get_active_modifiers();
988}
989
990/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100991 * Key hit, add it to the input buffer.
992 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100993 static void
994_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100995 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100996 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100997 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100998{
Anton Sharonov68d94722024-01-23 23:19:02 +0100999 // marshal to corresponding implementation
1000 keycode_trans_strategy_used->ptr_on_char(hwnd, cch, cRepeat);
1001}
1002
1003/*
1004 * Experimental implementation, introduced in v8.2.4807
1005 * "processing key event in Win32 GUI is not ideal"
1006 */
1007 static void
1008_OnChar_experimental(
1009 HWND hwnd UNUSED,
1010 UINT cch,
1011 int cRepeat UNUSED)
1012{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001013 char_u string[40];
1014 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +01001015 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +01001016 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001017
Anton Sharonov3f026672022-07-26 21:26:18 +01001018 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1019 return;
1020
1021 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
1022 // process_message()
1023 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1024 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001025
Anton Sharonov68d94722024-01-23 23:19:02 +01001026 modifiers = get_active_modifiers_experimental();
LemonBoy77fc0b02022-04-22 22:45:52 +01001027
1028 ch = simplify_key(ch, &modifiers);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001029
Christopher Plewright7b0afc12022-12-30 16:54:58 +00001030 // Some keys need adjustment when the Ctrl modifier is used.
1031 ++no_reduce_keys;
1032 ch = may_adjust_key_for_ctrl(modifiers, ch);
1033 --no_reduce_keys;
1034
LemonBoy77fc0b02022-04-22 22:45:52 +01001035 // remove the SHIFT modifier for keys where it's already included, e.g.,
1036 // '(' and '*'
1037 modifiers = may_remove_shift_modifier(modifiers, ch);
1038
1039 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1040 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1041 if (ch == CSI)
1042 ch = K_CSI;
1043
1044 if (modifiers)
1045 {
1046 string[0] = CSI;
1047 string[1] = KS_MODIFIER;
1048 string[2] = modifiers;
1049 add_to_input_buf(string, 3);
1050 }
1051
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001052 len = char_to_string(ch, string, 40, FALSE);
1053 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1054 {
1055 trash_input_buf();
1056 got_int = TRUE;
1057 }
1058
1059 add_to_input_buf(string, len);
1060}
1061
1062/*
Anton Sharonov68d94722024-01-23 23:19:02 +01001063 * "Classic" implementation, existing prior to v8.2.4807
1064 */
1065 static void
1066_OnChar_classic(
1067 HWND hwnd UNUSED,
1068 UINT ch,
1069 int cRepeat UNUSED)
1070{
1071 char_u string[40];
1072 int len = 0;
1073
1074 dead_key = 0;
1075
1076 len = char_to_string(ch, string, 40, FALSE);
1077 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
1078 {
1079 trash_input_buf();
1080 got_int = TRUE;
1081 }
1082
1083 add_to_input_buf(string, len);
1084}
1085
1086/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001087 * Alt-Key hit, add it to the input buffer.
1088 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001089 static void
1090_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001091 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001092 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001093 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001094{
Anton Sharonov68d94722024-01-23 23:19:02 +01001095 // marshal to corresponding implementation
1096 keycode_trans_strategy_used->ptr_on_sys_char(hwnd, cch, cRepeat);
1097}
1098
1099/*
1100 * Experimental implementation, introduced in v8.2.4807
1101 * "processing key event in Win32 GUI is not ideal"
1102 */
1103 static void
1104_OnSysChar_experimental(
1105 HWND hwnd UNUSED,
1106 UINT cch,
1107 int cRepeat UNUSED)
1108{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001109 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001110 int len;
1111 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001112 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001113
Anton Sharonov3f026672022-07-26 21:26:18 +01001114 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001115
Bram Moolenaar734a8672019-12-02 22:49:38 +01001116 // OK, we have a character key (given by ch) which was entered with the
1117 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1118 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1119 // CAPSLOCK is pressed) at this point.
Anton Sharonov68d94722024-01-23 23:19:02 +01001120 modifiers = get_active_modifiers_experimental();
1121 ch = simplify_key(ch, &modifiers);
1122 // remove the SHIFT modifier for keys where it's already included, e.g.,
1123 // '(' and '*'
1124 modifiers = may_remove_shift_modifier(modifiers, ch);
1125
1126 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1127 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
1128 if (ch == CSI)
1129 ch = K_CSI;
1130
1131 len = 0;
1132 if (modifiers)
1133 {
1134 string[len++] = CSI;
1135 string[len++] = KS_MODIFIER;
1136 string[len++] = modifiers;
1137 }
1138
1139 if (IS_SPECIAL((int)ch))
1140 {
1141 string[len++] = CSI;
1142 string[len++] = K_SECOND((int)ch);
1143 string[len++] = K_THIRD((int)ch);
1144 }
1145 else
1146 {
1147 // Although the documentation isn't clear about it, we assume "ch" is
1148 // a Unicode character.
1149 len += char_to_string(ch, string + len, 40 - len, TRUE);
1150 }
1151
1152 add_to_input_buf(string, len);
1153}
1154
1155/*
1156 * "Classic" implementation, existing prior to v8.2.4807
1157 */
1158 static void
1159_OnSysChar_classic(
1160 HWND hwnd UNUSED,
1161 UINT cch,
1162 int cRepeat UNUSED)
1163{
1164 char_u string[40]; // Enough for multibyte character
1165 int len;
1166 int modifiers;
1167 int ch = cch; // special keys are negative
1168
1169 dead_key = 0;
1170
1171 // TRACE("OnSysChar(%d, %c)\n", ch, ch);
1172
1173 // OK, we have a character key (given by ch) which was entered with the
1174 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
1175 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
1176 // CAPSLOCK is pressed) at this point.
1177 modifiers = MOD_MASK_ALT;
1178 if (GetKeyState(VK_SHIFT) & 0x8000)
1179 modifiers |= MOD_MASK_SHIFT;
1180 if (GetKeyState(VK_CONTROL) & 0x8000)
1181 modifiers |= MOD_MASK_CTRL;
1182
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001183 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +01001184 // remove the SHIFT modifier for keys where it's already included, e.g.,
1185 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +02001186 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001187
Bram Moolenaarfd615a32020-05-16 14:01:51 +02001188 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
1189 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 if (ch == CSI)
1191 ch = K_CSI;
1192
1193 len = 0;
1194 if (modifiers)
1195 {
1196 string[len++] = CSI;
1197 string[len++] = KS_MODIFIER;
1198 string[len++] = modifiers;
1199 }
1200
1201 if (IS_SPECIAL((int)ch))
1202 {
1203 string[len++] = CSI;
1204 string[len++] = K_SECOND((int)ch);
1205 string[len++] = K_THIRD((int)ch);
1206 }
1207 else
1208 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001209 // Although the documentation isn't clear about it, we assume "ch" is
1210 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001211 len += char_to_string(ch, string + len, 40 - len, TRUE);
1212 }
1213
1214 add_to_input_buf(string, len);
1215}
1216
1217 static void
1218_OnMouseEvent(
1219 int button,
1220 int x,
1221 int y,
1222 int repeated_click,
1223 UINT keyFlags)
1224{
1225 int vim_modifiers = 0x0;
1226
1227 s_getting_focus = FALSE;
1228
1229 if (keyFlags & MK_SHIFT)
1230 vim_modifiers |= MOUSE_SHIFT;
1231 if (keyFlags & MK_CONTROL)
1232 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +01001233 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001234 vim_modifiers |= MOUSE_ALT;
1235
1236 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1237}
1238
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001239 static void
1240_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001241 HWND hwnd UNUSED,
1242 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001243 int x,
1244 int y,
1245 UINT keyFlags)
1246{
1247 static LONG s_prevTime = 0;
1248
1249 LONG currentTime = GetMessageTime();
1250 int button = -1;
1251 int repeated_click;
1252
Bram Moolenaar734a8672019-12-02 22:49:38 +01001253 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001254 (void)SetFocus(s_hwnd);
1255
1256 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1257 button = MOUSE_LEFT;
1258 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1259 button = MOUSE_MIDDLE;
1260 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1261 button = MOUSE_RIGHT;
1262 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1263 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001264 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1265 }
1266 else if (s_uMsg == WM_CAPTURECHANGED)
1267 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001268 // on W95/NT4, somehow you get in here with an odd Msg
1269 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001270 if (s_button_pending == MOUSE_LEFT)
1271 button = MOUSE_RIGHT;
1272 else
1273 button = MOUSE_LEFT;
1274 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001275
1276 if (button < 0)
1277 return;
1278
1279 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1280
1281 /*
1282 * Holding down the left and right buttons simulates pushing the middle
1283 * button.
1284 */
1285 if (repeated_click
1286 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1287 || (button == MOUSE_RIGHT
1288 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001289 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001290 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001291 * Hmm, gui.c will ignore more than one button down at a time, so
1292 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001293 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001294 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1295 button = MOUSE_MIDDLE;
1296 repeated_click = FALSE;
1297 s_button_pending = -1;
1298 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001299 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001300 else if ((repeated_click)
1301 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1302 {
1303 if (s_button_pending > -1)
1304 {
1305 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1306 s_button_pending = -1;
1307 }
1308 // TRACE("Button down at x %d, y %d\n", x, y);
1309 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1310 }
1311 else
1312 {
1313 /*
1314 * If this is the first press (i.e. not a multiple click) don't
1315 * action immediately, but store and wait for:
1316 * i) button-up
1317 * ii) mouse move
1318 * iii) another button press
1319 * before using it.
1320 * This enables us to make left+right simulate middle button,
1321 * without left or right being actioned first. The side-effect is
1322 * that if you click and hold the mouse without dragging, the
1323 * cursor doesn't move until you release the button. In practice
1324 * this is hardly a problem.
1325 */
1326 s_button_pending = button;
1327 s_x_pending = x;
1328 s_y_pending = y;
1329 s_kFlags_pending = keyFlags;
1330 }
1331
1332 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001333}
1334
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001335 static void
1336_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001337 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001338 int x,
1339 int y,
1340 UINT keyFlags)
1341{
1342 int button;
1343
1344 s_getting_focus = FALSE;
1345 if (s_button_pending > -1)
1346 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001347 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001348 _OnMouseEvent(s_button_pending, s_x_pending,
1349 s_y_pending, FALSE, s_kFlags_pending);
1350 s_button_pending = -1;
1351 }
1352 if (s_uMsg == WM_MOUSEMOVE)
1353 {
1354 /*
1355 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1356 * down.
1357 */
1358 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1359 | MK_XBUTTON1 | MK_XBUTTON2)))
1360 {
1361 gui_mouse_moved(x, y);
1362 return;
1363 }
1364
1365 /*
1366 * While button is down, keep grabbing mouse move events when
1367 * the mouse goes outside the window
1368 */
1369 SetCapture(s_textArea);
1370 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001371 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001372 }
1373 else
1374 {
1375 ReleaseCapture();
1376 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001377 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001378 }
1379
1380 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1381}
1382
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001383 static void
1384_OnSizeTextArea(
1385 HWND hwnd UNUSED,
1386 UINT state UNUSED,
1387 int cx UNUSED,
1388 int cy UNUSED)
1389{
1390#if defined(FEAT_DIRECTX)
1391 if (IS_ENABLE_DIRECTX())
1392 directx_binddc();
1393#endif
1394}
1395
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001396#ifdef FEAT_MENU
1397/*
1398 * Find the vimmenu_T with the given id
1399 */
1400 static vimmenu_T *
1401gui_mswin_find_menu(
1402 vimmenu_T *pMenu,
1403 int id)
1404{
1405 vimmenu_T *pChildMenu;
1406
1407 while (pMenu)
1408 {
1409 if (pMenu->id == (UINT)id)
1410 break;
1411 if (pMenu->children != NULL)
1412 {
1413 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1414 if (pChildMenu)
1415 {
1416 pMenu = pChildMenu;
1417 break;
1418 }
1419 }
1420 pMenu = pMenu->next;
1421 }
1422 return pMenu;
1423}
1424
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001425 static void
1426_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001427 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001428 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001429 HWND hwndCtl UNUSED,
1430 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001431{
1432 vimmenu_T *pMenu;
1433
1434 pMenu = gui_mswin_find_menu(root_menu, id);
1435 if (pMenu)
1436 gui_menu_cb(pMenu);
1437}
1438#endif
1439
1440#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001441/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001442 * Handle a Find/Replace window message.
1443 */
1444 static void
1445_OnFindRepl(void)
1446{
1447 int flags = 0;
1448 int down;
1449
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001450 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001451 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001452 (void)SetFocus(s_hwnd);
1453
1454 if (s_findrep_struct.Flags & FR_FINDNEXT)
1455 {
1456 flags = FRD_FINDNEXT;
1457
Bram Moolenaar734a8672019-12-02 22:49:38 +01001458 // Give main window the focus back: this is so the cursor isn't
1459 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001460 (void)SetFocus(s_hwnd);
1461 }
1462 else if (s_findrep_struct.Flags & FR_REPLACE)
1463 {
1464 flags = FRD_REPLACE;
1465
Bram Moolenaar734a8672019-12-02 22:49:38 +01001466 // Give main window the focus back: this is so the cursor isn't
1467 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001468 (void)SetFocus(s_hwnd);
1469 }
1470 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1471 {
1472 flags = FRD_REPLACEALL;
1473 }
1474
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001475 if (flags == 0)
1476 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001477
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001478 char_u *p, *q;
1479
1480 // Call the generic GUI function to do the actual work.
1481 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1482 flags |= FRD_WHOLE_WORD;
1483 if (s_findrep_struct.Flags & FR_MATCHCASE)
1484 flags |= FRD_MATCH_CASE;
1485 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1486 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1487 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1488 if (p != NULL && q != NULL)
1489 gui_do_findrepl(flags, p, q, down);
1490 vim_free(p);
1491 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001492}
1493#endif
1494
1495 static void
1496HandleMouseHide(UINT uMsg, LPARAM lParam)
1497{
1498 static LPARAM last_lParam = 0L;
1499
Bram Moolenaar734a8672019-12-02 22:49:38 +01001500 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001501 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1502 {
1503 if (lParam == last_lParam)
1504 return;
1505 last_lParam = lParam;
1506 }
1507
Bram Moolenaar734a8672019-12-02 22:49:38 +01001508 // Handle specially, to centralise coding. We need to be sure we catch all
1509 // possible events which should cause us to restore the cursor (as it is a
1510 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001511 switch (uMsg)
1512 {
1513 case WM_KEYUP:
1514 case WM_CHAR:
1515 /*
1516 * blank out the pointer if necessary
1517 */
1518 if (p_mh)
1519 gui_mch_mousehide(TRUE);
1520 break;
1521
Bram Moolenaar734a8672019-12-02 22:49:38 +01001522 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001523 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001524 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525 case WM_LBUTTONDOWN:
1526 case WM_LBUTTONUP:
1527 case WM_MBUTTONDOWN:
1528 case WM_MBUTTONUP:
1529 case WM_RBUTTONDOWN:
1530 case WM_RBUTTONUP:
1531 case WM_XBUTTONDOWN:
1532 case WM_XBUTTONUP:
1533 case WM_NCMOUSEMOVE:
1534 case WM_NCLBUTTONDOWN:
1535 case WM_NCLBUTTONUP:
1536 case WM_NCMBUTTONDOWN:
1537 case WM_NCMBUTTONUP:
1538 case WM_NCRBUTTONDOWN:
1539 case WM_NCRBUTTONUP:
1540 case WM_KILLFOCUS:
1541 /*
1542 * if the pointer is currently hidden, then we should show it.
1543 */
1544 gui_mch_mousehide(FALSE);
1545 break;
1546 }
1547}
1548
1549 static LRESULT CALLBACK
1550_TextAreaWndProc(
1551 HWND hwnd,
1552 UINT uMsg,
1553 WPARAM wParam,
1554 LPARAM lParam)
1555{
1556 /*
1557 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1558 hwnd, uMsg, wParam, lParam);
1559 */
1560
1561 HandleMouseHide(uMsg, lParam);
1562
1563 s_uMsg = uMsg;
1564 s_wParam = wParam;
1565 s_lParam = lParam;
1566
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001567#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001568 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001569#endif
1570
1571 switch (uMsg)
1572 {
1573 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1574 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1575 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1576 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1577 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1578 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1579 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1580 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1581 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1582 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1583 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1584 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1585 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1586 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001587 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001588
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001589#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001590 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1591 return TRUE;
1592#endif
1593 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001594 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001595 }
1596}
1597
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001598/*
1599 * Called when the foreground or background color has been changed.
1600 */
1601 void
1602gui_mch_new_colors(void)
1603{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001604 HBRUSH prevBrush;
1605
1606 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001607 prevBrush = (HBRUSH)SetClassLongPtr(
1608 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001609 InvalidateRect(s_hwnd, NULL, TRUE);
1610 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001611}
1612
1613/*
1614 * Set the colors to their default values.
1615 */
1616 void
1617gui_mch_def_colors(void)
1618{
1619 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1620 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1621 gui.def_norm_pixel = gui.norm_pixel;
1622 gui.def_back_pixel = gui.back_pixel;
1623}
1624
1625/*
1626 * Open the GUI window which was created by a call to gui_mch_init().
1627 */
1628 int
1629gui_mch_open(void)
1630{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001631 // Actually open the window, if not already visible
1632 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001633 if (!IsWindowVisible(s_hwnd))
1634 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1635
1636#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001637 // Init replace string here, so that we keep it when re-opening the
1638 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001639 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1640#endif
1641
1642 return OK;
1643}
1644
1645/*
1646 * Get the position of the top left corner of the window.
1647 */
1648 int
1649gui_mch_get_winpos(int *x, int *y)
1650{
1651 RECT rect;
1652
1653 GetWindowRect(s_hwnd, &rect);
1654 *x = rect.left;
1655 *y = rect.top;
1656 return OK;
1657}
1658
1659/*
1660 * Set the position of the top left corner of the window to the given
1661 * coordinates.
1662 */
1663 void
1664gui_mch_set_winpos(int x, int y)
1665{
1666 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1667 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1668}
1669 void
1670gui_mch_set_text_area_pos(int x, int y, int w, int h)
1671{
1672 static int oldx = 0;
1673 static int oldy = 0;
1674
1675 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1676
1677#ifdef FEAT_TOOLBAR
1678 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1679 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001680 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001681#endif
1682#if defined(FEAT_GUI_TABLINE)
1683 if (showing_tabline)
1684 {
1685 int top = 0;
1686 RECT rect;
1687
1688# ifdef FEAT_TOOLBAR
1689 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001690 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691# endif
1692 GetClientRect(s_hwnd, &rect);
1693 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1694 }
1695#endif
1696
Bram Moolenaar734a8672019-12-02 22:49:38 +01001697 // When side scroll bar is unshown, the size of window will change.
1698 // then, the text area move left or right. thus client rect should be
1699 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001700 if (oldx != x || oldy != y)
1701 {
1702 InvalidateRect(s_hwnd, NULL, FALSE);
1703 oldx = x;
1704 oldy = y;
1705 }
1706}
1707
1708
1709/*
1710 * Scrollbar stuff:
1711 */
1712
1713 void
1714gui_mch_enable_scrollbar(
1715 scrollbar_T *sb,
1716 int flag)
1717{
1718 ShowScrollBar(sb->id, SB_CTL, flag);
1719
Bram Moolenaar734a8672019-12-02 22:49:38 +01001720 // TODO: When the window is maximized, the size of the window stays the
1721 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1722 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001723}
1724
1725 void
1726gui_mch_set_scrollbar_pos(
1727 scrollbar_T *sb,
1728 int x,
1729 int y,
1730 int w,
1731 int h)
1732{
1733 SetWindowPos(sb->id, NULL, x, y, w, h,
1734 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1735}
1736
Bram Moolenaar203ec772020-07-17 20:43:43 +02001737 int
1738gui_mch_get_scrollbar_xpadding(void)
1739{
1740 RECT rcTxt, rcWnd;
1741 int xpad;
1742
1743 GetWindowRect(s_textArea, &rcTxt);
1744 GetWindowRect(s_hwnd, &rcWnd);
1745 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001746 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1747 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001748 return (xpad < 0) ? 0 : xpad;
1749}
1750
1751 int
1752gui_mch_get_scrollbar_ypadding(void)
1753{
1754 RECT rcTxt, rcWnd;
1755 int ypad;
1756
1757 GetWindowRect(s_textArea, &rcTxt);
1758 GetWindowRect(s_hwnd, &rcWnd);
1759 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001760 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1761 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001762 return (ypad < 0) ? 0 : ypad;
1763}
1764
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001765 void
1766gui_mch_create_scrollbar(
1767 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001768 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001769{
1770 sb->id = CreateWindow(
1771 "SCROLLBAR", "Scrollbar",
1772 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001773 10, // Any value will do for now
1774 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001775 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001776 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777}
1778
1779/*
1780 * Find the scrollbar with the given hwnd.
1781 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001782 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001783gui_mswin_find_scrollbar(HWND hwnd)
1784{
1785 win_T *wp;
1786
1787 if (gui.bottom_sbar.id == hwnd)
1788 return &gui.bottom_sbar;
1789 FOR_ALL_WINDOWS(wp)
1790 {
1791 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1792 return &wp->w_scrollbars[SBAR_LEFT];
1793 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1794 return &wp->w_scrollbars[SBAR_RIGHT];
1795 }
1796 return NULL;
1797}
1798
K.Takatac81e9bf2022-01-16 14:15:49 +00001799 static void
1800update_scrollbar_size(void)
1801{
1802 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1803 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1804}
1805
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001806/*
K.Takataabe628e2022-01-23 16:25:17 +00001807 * Get the average character size of a font.
1808 */
1809 static void
1810GetAverageFontSize(HDC hdc, SIZE *size)
1811{
1812 // GetTextMetrics() may not return the right value in tmAveCharWidth
1813 // for some fonts. Do our own average computation.
1814 GetTextExtentPoint(hdc,
1815 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1816 52, size);
1817 size->cx = (size->cx / 26 + 1) / 2;
1818}
1819
1820/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001821 * Get the character size of a font.
1822 */
1823 static void
Ken Takatada5da652023-10-05 20:20:58 +02001824GetFontSize(GuiFont font, int *char_width, int *char_height)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825{
1826 HWND hwnd = GetDesktopWindow();
1827 HDC hdc = GetWindowDC(hwnd);
1828 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001829 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001830 TEXTMETRIC tm;
1831
1832 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001833 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001834
Ken Takatada5da652023-10-05 20:20:58 +02001835 if (char_width)
1836 *char_width = size.cx + tm.tmOverhang;
1837 if (char_height)
1838 *char_height = tm.tmHeight + p_linespace;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001839
1840 SelectFont(hdc, hfntOld);
1841
1842 ReleaseDC(hwnd, hdc);
1843}
1844
1845/*
Ken Takatada5da652023-10-05 20:20:58 +02001846 * Update the character size in "gui" structure with the specified font.
1847 */
1848 static void
1849UpdateFontSize(GuiFont font)
1850{
1851 GetFontSize(font, &gui.char_width, &gui.char_height);
1852}
1853
1854/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001855 * Adjust gui.char_height (after 'linespace' was changed).
1856 */
1857 int
1858gui_mch_adjust_charheight(void)
1859{
Ken Takatada5da652023-10-05 20:20:58 +02001860 UpdateFontSize(gui.norm_font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001861 return OK;
1862}
1863
1864 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001865get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001866{
1867 HFONT font = NULL;
1868
Bram Moolenaar734a8672019-12-02 22:49:38 +01001869 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001870 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001871
1872 if (font == NULL)
1873 return NOFONT;
1874
1875 return (GuiFont)font;
1876}
1877
1878 static int
1879pixels_to_points(int pixels, int vertical)
1880{
1881 int points;
1882 HWND hwnd;
1883 HDC hdc;
1884
1885 hwnd = GetDesktopWindow();
1886 hdc = GetWindowDC(hwnd);
1887
1888 points = MulDiv(pixels, 72,
1889 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1890
1891 ReleaseDC(hwnd, hdc);
1892
1893 return points;
1894}
1895
1896 GuiFont
1897gui_mch_get_font(
1898 char_u *name,
1899 int giveErrorIfMissing)
1900{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001901 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001902 GuiFont font = NOFONT;
1903
1904 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001905 {
1906 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001907 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001908 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001910 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911 return font;
1912}
1913
1914#if defined(FEAT_EVAL) || defined(PROTO)
1915/*
1916 * Return the name of font "font" in allocated memory.
1917 * Don't know how to get the actual name, thus use the provided name.
1918 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001919 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001920gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001921{
1922 if (name == NULL)
1923 return NULL;
1924 return vim_strsave(name);
1925}
1926#endif
1927
1928 void
1929gui_mch_free_font(GuiFont font)
1930{
1931 if (font)
1932 DeleteObject((HFONT)font);
1933}
1934
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001935/*
1936 * Return the Pixel value (color) for the given color name.
1937 * Return INVALCOLOR for error.
1938 */
1939 guicolor_T
1940gui_mch_get_color(char_u *name)
1941{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001942 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001943
1944 typedef struct SysColorTable
1945 {
1946 char *name;
1947 int color;
1948 } SysColorTable;
1949
1950 static SysColorTable sys_table[] =
1951 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001952 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1953 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001954#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001955 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1956#endif
1957 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1958 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1959 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1960 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1961 {"SYS_DESKTOP", COLOR_DESKTOP},
1962 {"SYS_INFOBK", COLOR_INFOBK},
1963 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1964 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001965 {"SYS_BTNFACE", COLOR_BTNFACE},
1966 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1967 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1968 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1969 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1970 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1971 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1972 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1973 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1974 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1975 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1976 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1977 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1978 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1979 {"SYS_MENU", COLOR_MENU},
1980 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1981 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1982 {"SYS_WINDOW", COLOR_WINDOW},
1983 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1984 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1985 };
1986
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001987 /*
1988 * Try to look up a system colour.
1989 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001990 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001991 if (STRICMP(name, sys_table[i].name) == 0)
1992 return GetSysColor(sys_table[i].color);
1993
Bram Moolenaarab302212016-04-26 20:59:29 +02001994 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001995}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001996
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001997 guicolor_T
1998gui_mch_get_rgb_color(int r, int g, int b)
1999{
2000 return gui_get_rgb_color_cmn(r, g, b);
2001}
2002
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002003/*
2004 * Return OK if the key with the termcap name "name" is supported.
2005 */
2006 int
2007gui_mch_haskey(char_u *name)
2008{
2009 int i;
2010
2011 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01002012 if (name[0] == special_keys[i].vim_code0
2013 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002014 return OK;
2015 return FAIL;
2016}
2017
2018 void
2019gui_mch_beep(void)
2020{
LemonBoy77771d32022-04-13 11:47:25 +01002021 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002022}
2023/*
2024 * Invert a rectangle from row r, column c, for nr rows and nc columns.
2025 */
2026 void
2027gui_mch_invert_rectangle(
2028 int r,
2029 int c,
2030 int nr,
2031 int nc)
2032{
2033 RECT rc;
2034
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002035#if defined(FEAT_DIRECTX)
2036 if (IS_ENABLE_DIRECTX())
2037 DWriteContext_Flush(s_dwc);
2038#endif
2039
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002040 /*
2041 * Note: InvertRect() excludes right and bottom of rectangle.
2042 */
2043 rc.left = FILL_X(c);
2044 rc.top = FILL_Y(r);
2045 rc.right = rc.left + nc * gui.char_width;
2046 rc.bottom = rc.top + nr * gui.char_height;
2047 InvertRect(s_hdc, &rc);
2048}
2049
2050/*
2051 * Iconify the GUI window.
2052 */
2053 void
2054gui_mch_iconify(void)
2055{
2056 ShowWindow(s_hwnd, SW_MINIMIZE);
2057}
2058
2059/*
2060 * Draw a cursor without focus.
2061 */
2062 void
2063gui_mch_draw_hollow_cursor(guicolor_T color)
2064{
2065 HBRUSH hbr;
2066 RECT rc;
2067
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002068#if defined(FEAT_DIRECTX)
2069 if (IS_ENABLE_DIRECTX())
2070 DWriteContext_Flush(s_dwc);
2071#endif
2072
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002073 /*
2074 * Note: FrameRect() excludes right and bottom of rectangle.
2075 */
2076 rc.left = FILL_X(gui.col);
2077 rc.top = FILL_Y(gui.row);
2078 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002079 if (mb_lefthalve(gui.row, gui.col))
2080 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081 rc.bottom = rc.top + gui.char_height;
2082 hbr = CreateSolidBrush(color);
2083 FrameRect(s_hdc, &rc, hbr);
2084 DeleteBrush(hbr);
2085}
2086/*
2087 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
2088 * color "color".
2089 */
2090 void
2091gui_mch_draw_part_cursor(
2092 int w,
2093 int h,
2094 guicolor_T color)
2095{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002096 RECT rc;
2097
2098 /*
2099 * Note: FillRect() excludes right and bottom of rectangle.
2100 */
2101 rc.left =
2102#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01002103 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
2105#endif
2106 FILL_X(gui.col);
2107 rc.top = FILL_Y(gui.row) + gui.char_height - h;
2108 rc.right = rc.left + w;
2109 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01002110
Bram Moolenaar92467d32017-12-05 13:22:16 +01002111 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112}
2113
2114
2115/*
2116 * Generates a VK_SPACE when the internal dead_key flag is set to output the
2117 * dead key's nominal character and re-post the original message.
2118 */
2119 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01002120outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002121{
2122 static MSG deadCharExpel;
2123
Anton Sharonov3f026672022-07-26 21:26:18 +01002124 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002125 return;
2126
Anton Sharonov3f026672022-07-26 21:26:18 +01002127 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002128
Bram Moolenaar734a8672019-12-02 22:49:38 +01002129 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002130 deadCharExpel.message = originalMsg.message;
2131 deadCharExpel.hwnd = originalMsg.hwnd;
2132 deadCharExpel.wParam = VK_SPACE;
2133
K.Takata4ac893f2022-01-20 12:44:28 +00002134 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002135
Bram Moolenaar734a8672019-12-02 22:49:38 +01002136 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002137 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
2138 originalMsg.lParam);
2139}
2140
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002141/*
Anton Sharonov3f026672022-07-26 21:26:18 +01002142 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
2143 */
2144 static void
2145outputDeadKey_rePost(MSG originalMsg)
2146{
2147 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
2148}
2149
2150/*
Anton Sharonov68d94722024-01-23 23:19:02 +01002151 * Refactored out part of process_message(), responsible for
2152 * handling the case of "not a special key"
2153 */
2154static void process_message_usual_key(UINT vk, const MSG *pmsg)
2155{
2156 // marshal to corresponding implementation
2157 keycode_trans_strategy_used->ptr_process_message_usual_key(vk, pmsg);
2158}
2159
2160/*
2161 * Experimental implementation, introduced in v8.2.4807
2162 * "processing key event in Win32 GUI is not ideal"
2163 */
2164static void process_message_usual_key_experimental(UINT vk, const MSG *pmsg)
2165{
2166 WCHAR ch[8];
2167 int len;
2168 int i;
2169 UINT scan_code;
2170 BYTE keyboard_state[256];
2171
2172 // Construct the state table with only a few modifiers, we don't
2173 // really care about the presence of Ctrl/Alt as those modifiers are
2174 // handled by Vim separately.
2175 memset(keyboard_state, 0, 256);
2176 if (GetKeyState(VK_SHIFT) & 0x8000)
2177 keyboard_state[VK_SHIFT] = 0x80;
2178 if (GetKeyState(VK_CAPITAL) & 0x0001)
2179 keyboard_state[VK_CAPITAL] = 0x01;
2180 // Alt-Gr is synthesized as Alt + Ctrl.
2181 if ((GetKeyState(VK_RMENU) & 0x8000)
2182 && (GetKeyState(VK_CONTROL) & 0x8000))
2183 {
2184 keyboard_state[VK_MENU] = 0x80;
2185 keyboard_state[VK_CONTROL] = 0x80;
2186 }
2187
2188 // Translate the virtual key according to the current keyboard
2189 // layout.
2190 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2191 // Convert the scan-code into a sequence of zero or more unicode
2192 // codepoints.
2193 // If this is a dead key ToUnicode returns a negative value.
2194 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2195 0);
2196 if (len < 0)
2197 dead_key = DEAD_KEY_SET_DEFAULT;
2198
2199 if (len <= 0)
2200 {
2201 int wm_char = NUL;
2202
2203 if (dead_key == DEAD_KEY_SET_DEFAULT
2204 && (GetKeyState(VK_CONTROL) & 0x8000))
2205 {
2206 if ( // AZERTY CTRL+dead_circumflex
2207 (vk == 221 && scan_code == 26)
2208 // QWERTZ CTRL+dead_circumflex
2209 || (vk == 220 && scan_code == 41))
2210 wm_char = '[';
2211 if ( // QWERTZ CTRL+dead_two-overdots
2212 (vk == 192 && scan_code == 27))
2213 wm_char = ']';
2214 }
2215 if (wm_char != NUL)
2216 {
2217 // post WM_CHAR='[' - which will be interpreted with CTRL
2218 // still hold as ESC
2219 PostMessageW(pmsg->hwnd, WM_CHAR, wm_char, pmsg->lParam);
2220 // ask _OnChar() to not touch this state, wait for next key
2221 // press and maintain knowledge that we are "poisoned" with
2222 // "dead state"
2223 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2224 }
2225 return;
2226 }
2227
2228 // Post the message as TranslateMessage would do.
2229 if (pmsg->message == WM_KEYDOWN)
2230 {
2231 for (i = 0; i < len; i++)
2232 PostMessageW(pmsg->hwnd, WM_CHAR, ch[i], pmsg->lParam);
2233 }
2234 else
2235 {
2236 for (i = 0; i < len; i++)
2237 PostMessageW(pmsg->hwnd, WM_SYSCHAR, ch[i], pmsg->lParam);
2238 }
2239}
2240
2241/*
2242 * "Classic" implementation, existing prior to v8.2.4807
2243 */
2244static void process_message_usual_key_classic(UINT vk, const MSG *pmsg)
2245{
2246 char_u string[40];
2247
2248 // Some keys need C-S- where they should only need C-.
2249 // Ignore 0xff, Windows XP sends it when NUMLOCK has changed since
2250 // system startup (Helmut Stiegler, 2003 Oct 3).
2251 if (vk != 0xff
2252 && (GetKeyState(VK_CONTROL) & 0x8000)
2253 && !(GetKeyState(VK_SHIFT) & 0x8000)
2254 && !(GetKeyState(VK_MENU) & 0x8000))
2255 {
2256 // CTRL-6 is '^'; Japanese keyboard maps '^' to vk == 0xDE
2257 if (vk == '6' || MapVirtualKey(vk, 2) == (UINT)'^')
2258 {
2259 string[0] = Ctrl_HAT;
2260 add_to_input_buf(string, 1);
2261 }
2262 // vk == 0xBD AZERTY for CTRL-'-', but CTRL-[ for * QWERTY!
2263 else if (vk == 0xBD) // QWERTY for CTRL-'-'
2264 {
2265 string[0] = Ctrl__;
2266 add_to_input_buf(string, 1);
2267 }
2268 // CTRL-2 is '@'; Japanese keyboard maps '@' to vk == 0xC0
2269 else if (vk == '2' || MapVirtualKey(vk, 2) == (UINT)'@')
2270 {
2271 string[0] = Ctrl_AT;
2272 add_to_input_buf(string, 1);
2273 }
2274 else
2275 TranslateMessage(pmsg);
2276 }
2277 else
2278 TranslateMessage(pmsg);
2279}
2280
2281/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002282 * Process a single Windows message.
2283 * If one is not available we hang until one is.
2284 */
2285 static void
2286process_message(void)
2287{
2288 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01002289 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002290 char_u string[40];
2291 int i;
2292 int modifiers = 0;
2293 int key;
2294#ifdef FEAT_MENU
2295 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
2296#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002297 static int keycode_trans_strategy_initialized = 0;
2298
2299 // lazy initialize - first time only
2300 if (!keycode_trans_strategy_initialized)
2301 {
2302 keycode_trans_strategy_initialized = 1;
2303 keycode_trans_strategy_init();
2304 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002305
K.Takatab7057bd2022-01-21 11:37:07 +00002306 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002307
2308#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002309 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310 if (msg.message == WM_OLE)
2311 {
2312 char_u *str = (char_u *)msg.lParam;
2313 if (str == NULL || *str == NUL)
2314 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002315 // Message can't be ours, forward it. Fixes problem with Ultramon
2316 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00002317 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002318 }
2319 else
2320 {
2321 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01002322 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002323 }
2324 return;
2325 }
2326#endif
2327
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01002329 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00002330 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002331 {
2332 HandleMouseHide(msg.message, msg.lParam);
2333 return;
2334 }
2335#endif
2336
2337 /*
2338 * Check if it's a special key that we recognise. If not, call
2339 * TranslateMessage().
2340 */
2341 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
2342 {
2343 vk = (int) msg.wParam;
2344
2345 /*
2346 * Handle dead keys in special conditions in other cases we let Windows
2347 * handle them and do not interfere.
2348 *
2349 * The dead_key flag must be reset on several occasions:
2350 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
2351 * consumed at that point (This is when we let Windows combine the
2352 * dead character on its own)
2353 *
2354 * - Before doing something special such as regenerating keypresses to
2355 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00002356 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002357 * immediately to _OnChar() (or _OnSysChar()).
2358 */
Anton Sharonov3f026672022-07-26 21:26:18 +01002359
2360 /*
2361 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
2362 * was handled by _WndProc, this keypress we want to process normally
2363 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002364 if (keycode_trans_strategy_used->is_experimental()
2365 && dead_key == DEAD_KEY_SKIP_ON_CHAR)
2366 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002367 dead_key = DEAD_KEY_OFF;
Anton Sharonov68d94722024-01-23 23:19:02 +01002368 }
Anton Sharonov3f026672022-07-26 21:26:18 +01002369
2370 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002371 {
2372 /*
dundargocc57b5bc2022-11-02 13:30:51 +00002373 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01002374 *
2375 * After dead key was pressed with Ctrl in some cases, ESC was
2376 * artificially injected and handled by _OnChar(), now we are
2377 * dealing with completely new key press from the user. If we don't
2378 * do anything, ToUnicode() call will interpret this vk+scan_code
2379 * under influence of "dead-modifier". To prevent this we translate
2380 * this message replacing current char from user with VK_SPACE,
2381 * which will cause WM_CHAR with dead_key's character itself. Using
2382 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
2383 * ignore this one WM_CHAR event completely. Afterwards (due to
2384 * usage of PostMessage), this procedure is scheduled to be called
2385 * again with user char and on next entry we will clean
2386 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2387 * outputDeadKey_rePost() since we do not wish to reset dead_key
2388 * value.
2389 */
Anton Sharonov68d94722024-01-23 23:19:02 +01002390 if (keycode_trans_strategy_used->is_experimental() &&
2391 dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
Anton Sharonov3f026672022-07-26 21:26:18 +01002392 {
2393 outputDeadKey_rePost_Ex(msg,
2394 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2395 return;
2396 }
2397
2398 if (dead_key != DEAD_KEY_SET_DEFAULT)
2399 {
2400 // should never happen - is there a way to make ASSERT here?
2401 return;
2402 }
2403
2404 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002405 * If a dead key was pressed and the user presses VK_SPACE,
2406 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2407 * with the dead char now, so do nothing special and let Windows
2408 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002409 *
2410 * Note that VK_SPACE combines with the dead_key's character and
2411 * only one WM_CHAR will be generated by TranslateMessage(), in
2412 * the two other cases two WM_CHAR will be generated: the dead
2413 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2414 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415 */
2416 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2417 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002418 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002419 TranslateMessage(&msg);
2420 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002421 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002422 // In modes where we are not typing, dead keys should behave
2423 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002424 else if ((get_real_state()
2425 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002426 {
2427 outputDeadKey_rePost(msg);
2428 return;
2429 }
2430 }
2431
Bram Moolenaar734a8672019-12-02 22:49:38 +01002432 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002433 if (vk == VK_CANCEL)
2434 {
2435 trash_input_buf();
2436 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002437 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438 string[0] = Ctrl_C;
2439 add_to_input_buf(string, 1);
2440 }
2441
LemonBoy77fc0b02022-04-22 22:45:52 +01002442 // This is an IME event or a synthetic keystroke, let Windows handle it.
2443 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2444 {
2445 TranslateMessage(&msg);
2446 return;
2447 }
2448
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002449 for (i = 0; special_keys[i].key_sym != 0; i++)
2450 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002451 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002453 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002454 {
2455 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002456 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002457 * is a key that would normally trigger the dead key nominal
2458 * character output (such as a NUMPAD printable character or
2459 * the TAB key, etc...).
2460 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002461 if (dead_key == DEAD_KEY_SET_DEFAULT
2462 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002463 || vk == VK_TAB || vk == CAR))
2464 {
2465 outputDeadKey_rePost(msg);
2466 return;
2467 }
2468
2469#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002470 // Check for <F10>: Windows selects the menu. When <F10> is
2471 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002472 if (vk == VK_F10
2473 && gui.menu_is_active
2474 && check_map(k10, State, FALSE, TRUE, FALSE,
2475 NULL, NULL) == NULL)
2476 break;
2477#endif
Anton Sharonov68d94722024-01-23 23:19:02 +01002478 modifiers = get_active_modifiers_via_ptr();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002479
2480 if (special_keys[i].vim_code1 == NUL)
2481 key = special_keys[i].vim_code0;
2482 else
2483 key = TO_SPECIAL(special_keys[i].vim_code0,
2484 special_keys[i].vim_code1);
2485 key = simplify_key(key, &modifiers);
2486 if (key == CSI)
2487 key = K_CSI;
2488
2489 if (modifiers)
2490 {
2491 string[0] = CSI;
2492 string[1] = KS_MODIFIER;
2493 string[2] = modifiers;
2494 add_to_input_buf(string, 3);
2495 }
2496
2497 if (IS_SPECIAL(key))
2498 {
2499 string[0] = CSI;
2500 string[1] = K_SECOND(key);
2501 string[2] = K_THIRD(key);
2502 add_to_input_buf(string, 3);
2503 }
2504 else
2505 {
2506 int len;
2507
Bram Moolenaar734a8672019-12-02 22:49:38 +01002508 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509 len = char_to_string(key, string, 40, FALSE);
2510 add_to_input_buf(string, len);
2511 }
2512 break;
2513 }
2514 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002515
2516 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002517 if (special_keys[i].key_sym == 0)
2518 {
Anton Sharonov68d94722024-01-23 23:19:02 +01002519 process_message_usual_key(vk, &msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520 }
2521 }
2522#ifdef FEAT_MBYTE_IME
2523 else if (msg.message == WM_IME_NOTIFY)
2524 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2525 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002526 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002527 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002528#endif
2529
2530#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002531 // Check for <F10>: Default effect is to select the menu. When <F10> is
2532 // mapped we need to stop it here to avoid strange effects (e.g., for the
2533 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2535 NULL, NULL) == NULL)
2536#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002537 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538}
2539
2540/*
2541 * Catch up with any queued events. This may put keyboard input into the
2542 * input buffer, call resize call-backs, trigger timers etc. If there is
2543 * nothing in the event queue (& no timers pending), then we return
2544 * immediately.
2545 */
2546 void
2547gui_mch_update(void)
2548{
2549 MSG msg;
2550
2551 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002552 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002553 && !vim_is_input_buf_full())
2554 process_message();
2555}
2556
Bram Moolenaar4231da42016-06-02 14:30:04 +02002557 static void
2558remove_any_timer(void)
2559{
2560 MSG msg;
2561
2562 if (s_wait_timer != 0 && !s_timed_out)
2563 {
2564 KillTimer(NULL, s_wait_timer);
2565
Bram Moolenaar734a8672019-12-02 22:49:38 +01002566 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002567 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002568 ;
2569 s_wait_timer = 0;
2570 }
2571}
2572
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002573/*
2574 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2575 * from the keyboard.
2576 * wtime == -1 Wait forever.
2577 * wtime == 0 This should never happen.
2578 * wtime > 0 Wait wtime milliseconds for a character.
2579 * Returns OK if a character was found to be available within the given time,
2580 * or FAIL otherwise.
2581 */
2582 int
2583gui_mch_wait_for_chars(int wtime)
2584{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002585 int focus;
2586
2587 s_timed_out = FALSE;
2588
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002589 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002590 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002591 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002592 if (s_busy_processing)
2593 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002594
2595 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002596 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2597 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002598 }
2599
2600 allow_scrollbar = TRUE;
2601
2602 focus = gui.in_focus;
2603 while (!s_timed_out)
2604 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002605 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002606 if (gui.in_focus != focus)
2607 {
2608 if (gui.in_focus)
2609 gui_mch_start_blink();
2610 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002611 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002612 focus = gui.in_focus;
2613 }
2614
2615 if (s_need_activate)
2616 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618 s_need_activate = FALSE;
2619 }
2620
Bram Moolenaar4231da42016-06-02 14:30:04 +02002621#ifdef FEAT_TIMERS
2622 did_add_timer = FALSE;
2623#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002624#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002625 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002626 for (;;)
2627 {
2628 MSG msg;
2629
2630 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002631# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002632 if (did_add_timer)
2633 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002635 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002636 {
2637 process_message();
2638 break;
2639 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002640 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002641 // TODO: The 10 msec is a compromise between laggy response
2642 // and consuming more CPU time. Better would be to handle
2643 // channel messages when they arrive.
2644 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002645 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002646 break;
2647 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002648#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002649 // Don't use gui_mch_update() because then we will spin-lock until a
2650 // char arrives, instead we use GetMessage() to hang until an
2651 // event arrives. No need to check for input_buf_full because we are
2652 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002653 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002654#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002655
2656 if (input_available())
2657 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002658 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002659 allow_scrollbar = FALSE;
2660
Bram Moolenaar89c00032019-09-04 13:53:21 +02002661 // Clear pending mouse button, the release event may have been
2662 // taken by the dialog window. But don't do this when getting
2663 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002664 if (!s_getting_focus)
2665 s_button_pending = -1;
2666
2667 return OK;
2668 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002669
2670#ifdef FEAT_TIMERS
2671 if (did_add_timer)
2672 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002673 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002674 remove_any_timer();
2675 break;
2676 }
2677#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002678 }
2679 allow_scrollbar = FALSE;
2680 return FAIL;
2681}
2682
2683/*
2684 * Clear a rectangular region of the screen from text pos (row1, col1) to
2685 * (row2, col2) inclusive.
2686 */
2687 void
2688gui_mch_clear_block(
2689 int row1,
2690 int col1,
2691 int row2,
2692 int col2)
2693{
2694 RECT rc;
2695
2696 /*
2697 * Clear one extra pixel at the far right, for when bold characters have
2698 * spilled over to the window border.
2699 * Note: FillRect() excludes right and bottom of rectangle.
2700 */
2701 rc.left = FILL_X(col1);
2702 rc.top = FILL_Y(row1);
2703 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2704 rc.bottom = FILL_Y(row2 + 1);
2705 clear_rect(&rc);
2706}
2707
2708/*
2709 * Clear the whole text window.
2710 */
2711 void
2712gui_mch_clear_all(void)
2713{
2714 RECT rc;
2715
2716 rc.left = 0;
2717 rc.top = 0;
2718 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2719 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2720 clear_rect(&rc);
2721}
2722/*
2723 * Menu stuff.
2724 */
2725
2726 void
2727gui_mch_enable_menu(int flag)
2728{
2729#ifdef FEAT_MENU
2730 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2731#endif
2732}
2733
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002734 void
2735gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002736 int x UNUSED,
2737 int y UNUSED,
2738 int w UNUSED,
2739 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002740{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002741 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742}
2743
2744#if defined(FEAT_MENU) || defined(PROTO)
2745/*
2746 * Make menu item hidden or not hidden
2747 */
2748 void
2749gui_mch_menu_hidden(
2750 vimmenu_T *menu,
2751 int hidden)
2752{
2753 /*
2754 * This doesn't do what we want. Hmm, just grey the menu items for now.
2755 */
2756 /*
2757 if (hidden)
2758 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2759 else
2760 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2761 */
2762 gui_mch_menu_grey(menu, hidden);
2763}
2764
2765/*
2766 * This is called after setting all the menus to grey/hidden or not.
2767 */
2768 void
2769gui_mch_draw_menubar(void)
2770{
2771 DrawMenuBar(s_hwnd);
2772}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002773#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002774
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002775/*
2776 * Return the RGB value of a pixel as a long.
2777 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002778 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002779gui_mch_get_rgb(guicolor_T pixel)
2780{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002781 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2782 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002783}
2784
2785#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002786/*
2787 * Convert pixels in X to dialog units
2788 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002789 static WORD
2790PixelToDialogX(int numPixels)
2791{
2792 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2793}
2794
Bram Moolenaar734a8672019-12-02 22:49:38 +01002795/*
2796 * Convert pixels in Y to dialog units
2797 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002798 static WORD
2799PixelToDialogY(int numPixels)
2800{
2801 return (WORD)((numPixels * 8) / s_dlgfntheight);
2802}
2803
Bram Moolenaar734a8672019-12-02 22:49:38 +01002804/*
2805 * Return the width in pixels of the given text in the given DC.
2806 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002807 static int
2808GetTextWidth(HDC hdc, char_u *str, int len)
2809{
2810 SIZE size;
2811
2812 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2813 return size.cx;
2814}
2815
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002816/*
2817 * Return the width in pixels of the given text in the given DC, taking care
2818 * of 'encoding' to active codepage conversion.
2819 */
2820 static int
2821GetTextWidthEnc(HDC hdc, char_u *str, int len)
2822{
2823 SIZE size;
2824 WCHAR *wstr;
2825 int n;
2826 int wlen = len;
2827
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002828 wstr = enc_to_utf16(str, &wlen);
2829 if (wstr == NULL)
2830 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002831
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002832 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2833 vim_free(wstr);
2834 if (n)
2835 return size.cx;
2836 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002837}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002838
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002839static void get_work_area(RECT *spi_rect);
2840
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002841/*
2842 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002843 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2844 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002845 */
2846 static BOOL
2847CenterWindow(
2848 HWND hwndChild,
2849 HWND hwndParent)
2850{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002851 HMONITOR mon;
2852 MONITORINFO moninfo;
2853 RECT rChild, rParent, rScreen;
2854 int wChild, hChild, wParent, hParent;
2855 int xNew, yNew;
2856 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857
2858 GetWindowRect(hwndChild, &rChild);
2859 wChild = rChild.right - rChild.left;
2860 hChild = rChild.bottom - rChild.top;
2861
Bram Moolenaar734a8672019-12-02 22:49:38 +01002862 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002863 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002864 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002865 else
2866 GetWindowRect(hwndParent, &rParent);
2867 wParent = rParent.right - rParent.left;
2868 hParent = rParent.bottom - rParent.top;
2869
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002870 moninfo.cbSize = sizeof(MONITORINFO);
2871 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2872 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002874 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002875 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002876 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002877 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002878 hdc = GetDC(hwndChild);
2879 rScreen.left = 0;
2880 rScreen.top = 0;
2881 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2882 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2883 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002884 }
2885
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002886 xNew = rParent.left + ((wParent - wChild) / 2);
2887 if (xNew < rScreen.left)
2888 xNew = rScreen.left;
2889 else if ((xNew + wChild) > rScreen.right)
2890 xNew = rScreen.right - wChild;
2891
2892 yNew = rParent.top + ((hParent - hChild) / 2);
2893 if (yNew < rScreen.top)
2894 yNew = rScreen.top;
2895 else if ((yNew + hChild) > rScreen.bottom)
2896 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002897
2898 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2899 SWP_NOSIZE | SWP_NOZORDER);
2900}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002901#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002902
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903#if defined(FEAT_TOOLBAR) || defined(PROTO)
2904 void
2905gui_mch_show_toolbar(int showit)
2906{
2907 if (s_toolbarhwnd == NULL)
2908 return;
2909
2910 if (showit)
2911 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002912 // Enable unicode support
2913 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2914 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915 ShowWindow(s_toolbarhwnd, SW_SHOW);
2916 }
2917 else
2918 ShowWindow(s_toolbarhwnd, SW_HIDE);
2919}
2920
Bram Moolenaar734a8672019-12-02 22:49:38 +01002921// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002922# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002923
2924#endif
2925
2926#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2927 static void
2928add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2929{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002930 WCHAR *wn;
2931 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002933 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002934 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002935 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002937 infow.cbSize = sizeof(infow);
2938 infow.fMask = MIIM_TYPE | MIIM_ID;
2939 infow.wID = item_id;
2940 infow.fType = MFT_STRING;
2941 infow.dwTypeData = wn;
2942 infow.cch = (UINT)wcslen(wn);
2943 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2944 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002945}
2946
2947 static void
2948show_tabline_popup_menu(void)
2949{
2950 HMENU tab_pmenu;
2951 long rval;
2952 POINT pt;
2953
Bram Moolenaar734a8672019-12-02 22:49:38 +01002954 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002955 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002956 return;
2957
2958 tab_pmenu = CreatePopupMenu();
2959 if (tab_pmenu == NULL)
2960 return;
2961
2962 if (first_tabpage->tp_next != NULL)
2963 add_tabline_popup_menu_entry(tab_pmenu,
2964 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2965 add_tabline_popup_menu_entry(tab_pmenu,
2966 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2967 add_tabline_popup_menu_entry(tab_pmenu,
2968 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2969
2970 GetCursorPos(&pt);
2971 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2972 NULL);
2973
2974 DestroyMenu(tab_pmenu);
2975
Bram Moolenaar734a8672019-12-02 22:49:38 +01002976 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002977 if (rval > 0)
2978 {
2979 TCHITTESTINFO htinfo;
2980 int idx;
2981
2982 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2983 return;
2984
2985 htinfo.pt.x = pt.x;
2986 htinfo.pt.y = pt.y;
2987 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2988 if (idx == -1)
2989 idx = 0;
2990 else
2991 idx += 1;
2992
2993 send_tabline_menu_event(idx, (int)rval);
2994 }
2995}
2996
2997/*
2998 * Show or hide the tabline.
2999 */
3000 void
3001gui_mch_show_tabline(int showit)
3002{
3003 if (s_tabhwnd == NULL)
3004 return;
3005
3006 if (!showit != !showing_tabline)
3007 {
3008 if (showit)
3009 ShowWindow(s_tabhwnd, SW_SHOW);
3010 else
3011 ShowWindow(s_tabhwnd, SW_HIDE);
3012 showing_tabline = showit;
3013 }
3014}
3015
3016/*
3017 * Return TRUE when tabline is displayed.
3018 */
3019 int
3020gui_mch_showing_tabline(void)
3021{
3022 return s_tabhwnd != NULL && showing_tabline;
3023}
3024
3025/*
3026 * Update the labels of the tabline.
3027 */
3028 void
3029gui_mch_update_tabline(void)
3030{
3031 tabpage_T *tp;
3032 TCITEM tie;
3033 int nr = 0;
3034 int curtabidx = 0;
3035 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003036 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003037
3038 if (s_tabhwnd == NULL)
3039 return;
3040
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003041 // Enable unicode support
3042 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043
3044 tie.mask = TCIF_TEXT;
3045 tie.iImage = -1;
3046
Bram Moolenaar734a8672019-12-02 22:49:38 +01003047 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003048 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
3049
Bram Moolenaar734a8672019-12-02 22:49:38 +01003050 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003051 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3052 {
3053 if (tp == curtab)
3054 curtabidx = nr;
3055
3056 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
3057 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003058 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059 tie.pszText = "-Empty-";
3060 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
3061 tabadded = 1;
3062 }
3063
3064 get_tabline_label(tp, FALSE);
3065 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003066
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003067 wstr = enc_to_utf16(NameBuff, NULL);
3068 if (wstr != NULL)
3069 {
3070 TCITEMW tiw;
3071
3072 tiw.mask = TCIF_TEXT;
3073 tiw.iImage = -1;
3074 tiw.pszText = wstr;
3075 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
3076 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003077 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003078 }
3079
Bram Moolenaar734a8672019-12-02 22:49:38 +01003080 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003081 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
3082 TabCtrl_DeleteItem(s_tabhwnd, nr);
3083
3084 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3085 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3086
Bram Moolenaar734a8672019-12-02 22:49:38 +01003087 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
3089 RedrawWindow(s_tabhwnd, NULL, NULL,
3090 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
3091
3092 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
3093 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
3094}
3095
3096/*
3097 * Set the current tab to "nr". First tab is 1.
3098 */
3099 void
3100gui_mch_set_curtab(int nr)
3101{
3102 if (s_tabhwnd == NULL)
3103 return;
3104
3105 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
3106 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
3107}
3108
3109#endif
3110
3111/*
3112 * ":simalt" command.
3113 */
3114 void
3115ex_simalt(exarg_T *eap)
3116{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003117 char_u *keys = eap->arg;
3118 int fill_typebuf = FALSE;
3119 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003120
3121 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
3122 while (*keys)
3123 {
3124 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01003125 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003126 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
3127 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003128 fill_typebuf = TRUE;
3129 }
3130 if (fill_typebuf)
3131 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003132 // Put a NOP in the typeahead buffer so that the message will get
3133 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003134 key_name[0] = K_SPECIAL;
3135 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02003136 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003137 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003138#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003139 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02003140#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02003141 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003142 }
3143}
3144
3145/*
3146 * Create the find & replace dialogs.
3147 * You can't have both at once: ":find" when replace is showing, destroys
3148 * the replace dialog first, and the other way around.
3149 */
3150#ifdef MSWIN_FIND_REPLACE
3151 static void
3152initialise_findrep(char_u *initial_string)
3153{
3154 int wword = FALSE;
3155 int mcase = !p_ic;
3156 char_u *entry_text;
3157
Bram Moolenaar734a8672019-12-02 22:49:38 +01003158 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003159 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
3160
3161 s_findrep_struct.hwndOwner = s_hwnd;
3162 s_findrep_struct.Flags = FR_DOWN;
3163 if (mcase)
3164 s_findrep_struct.Flags |= FR_MATCHCASE;
3165 if (wword)
3166 s_findrep_struct.Flags |= FR_WHOLEWORD;
3167 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003168 {
3169 WCHAR *p = enc_to_utf16(entry_text, NULL);
3170 if (p != NULL)
3171 {
3172 int len = s_findrep_struct.wFindWhatLen - 1;
3173
3174 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
3175 s_findrep_struct.lpstrFindWhat[len] = NUL;
3176 vim_free(p);
3177 }
3178 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003179 vim_free(entry_text);
3180}
3181#endif
3182
3183 static void
3184set_window_title(HWND hwnd, char *title)
3185{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003186 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187 {
3188 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003189
Bram Moolenaar734a8672019-12-02 22:49:38 +01003190 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003191 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
3192 if (wbuf != NULL)
3193 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02003194 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003195 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003196 }
3197 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02003198 else
3199 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003200}
3201
3202 void
3203gui_mch_find_dialog(exarg_T *eap)
3204{
3205#ifdef MSWIN_FIND_REPLACE
3206 if (s_findrep_msg != 0)
3207 {
3208 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
3209 DestroyWindow(s_findrep_hwnd);
3210
3211 if (!IsWindow(s_findrep_hwnd))
3212 {
3213 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003214 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 }
3216
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003217 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003218 (void)SetFocus(s_findrep_hwnd);
3219
3220 s_findrep_is_find = TRUE;
3221 }
3222#endif
3223}
3224
3225
3226 void
3227gui_mch_replace_dialog(exarg_T *eap)
3228{
3229#ifdef MSWIN_FIND_REPLACE
3230 if (s_findrep_msg != 0)
3231 {
3232 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
3233 DestroyWindow(s_findrep_hwnd);
3234
3235 if (!IsWindow(s_findrep_hwnd))
3236 {
3237 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00003238 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003239 }
3240
Bram Moolenaar9e42c862018-07-20 05:03:16 +02003241 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003242 (void)SetFocus(s_findrep_hwnd);
3243
3244 s_findrep_is_find = FALSE;
3245 }
3246#endif
3247}
3248
3249
3250/*
3251 * Set visibility of the pointer.
3252 */
3253 void
3254gui_mch_mousehide(int hide)
3255{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003256 if (hide == gui.pointer_hidden)
3257 return;
3258
3259 ShowCursor(!hide);
3260 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003261}
3262
3263#ifdef FEAT_MENU
3264 static void
3265gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
3266{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003267 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003268 gui_mch_mousehide(FALSE);
3269
3270 (void)TrackPopupMenu(
3271 (HMENU)menu->submenu_id,
3272 TPM_LEFTALIGN | TPM_LEFTBUTTON,
3273 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01003274 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003275 s_hwnd,
3276 NULL);
3277 /*
3278 * NOTE: The pop-up menu can eat the mouse up event.
3279 * We deal with this in normal.c.
3280 */
3281}
3282#endif
3283
3284/*
3285 * Got a message when the system will go down.
3286 */
3287 static void
3288_OnEndSession(void)
3289{
3290 getout_preserve_modified(1);
3291}
3292
3293/*
3294 * Get this message when the user clicks on the cross in the top right corner
3295 * of a Windows95 window.
3296 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003297 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003298_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003299{
3300 gui_shell_closed();
3301}
3302
3303/*
3304 * Get a message when the window is being destroyed.
3305 */
3306 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003307_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308{
3309 if (!destroying)
3310 _OnClose(hwnd);
3311}
3312
3313 static void
3314_OnPaint(
3315 HWND hwnd)
3316{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003317 if (IsMinimized(hwnd))
3318 return;
3319
3320 PAINTSTRUCT ps;
3321
3322 out_flush(); // make sure all output has been processed
3323 (void)BeginPaint(hwnd, &ps);
3324
3325 // prevent multi-byte characters from misprinting on an invalid
3326 // rectangle
3327 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003329 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003331 GetClientRect(hwnd, &rect);
3332 ps.rcPaint.left = rect.left;
3333 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003335
3336 if (!IsRectEmpty(&ps.rcPaint))
3337 {
3338 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3339 ps.rcPaint.right - ps.rcPaint.left + 1,
3340 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3341 }
3342
3343 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003344}
3345
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 static void
3347_OnSize(
3348 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003349 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350 int cx,
3351 int cy)
3352{
K.Takatac81e9bf2022-01-16 14:15:49 +00003353 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354 {
3355 gui_resize_shell(cx, cy);
3356
Bram Moolenaar734a8672019-12-02 22:49:38 +01003357 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003358 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359 }
3360}
3361
3362 static void
3363_OnSetFocus(
3364 HWND hwnd,
3365 HWND hwndOldFocus)
3366{
3367 gui_focus_change(TRUE);
3368 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003369 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003370}
3371
3372 static void
3373_OnKillFocus(
3374 HWND hwnd,
3375 HWND hwndNewFocus)
3376{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003377 if (destroying)
3378 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379 gui_focus_change(FALSE);
3380 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003381 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003382}
3383
3384/*
3385 * Get a message when the user switches back to vim
3386 */
3387 static LRESULT
3388_OnActivateApp(
3389 HWND hwnd,
3390 BOOL fActivate,
3391 DWORD dwThreadId)
3392{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003393 // we call gui_focus_change() in _OnSetFocus()
3394 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003395 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003396}
3397
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003398 void
3399gui_mch_destroy_scrollbar(scrollbar_T *sb)
3400{
3401 DestroyWindow(sb->id);
3402}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403
3404/*
3405 * Get current mouse coordinates in text window.
3406 */
3407 void
3408gui_mch_getmouse(int *x, int *y)
3409{
3410 RECT rct;
3411 POINT mp;
3412
3413 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003414 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 *x = (int)(mp.x - rct.left);
3416 *y = (int)(mp.y - rct.top);
3417}
3418
3419/*
3420 * Move mouse pointer to character at (x, y).
3421 */
3422 void
3423gui_mch_setmouse(int x, int y)
3424{
3425 RECT rct;
3426
3427 (void)GetWindowRect(s_textArea, &rct);
3428 (void)SetCursorPos(x + gui.border_offset + rct.left,
3429 y + gui.border_offset + rct.top);
3430}
3431
3432 static void
3433gui_mswin_get_valid_dimensions(
3434 int w,
3435 int h,
3436 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003437 int *valid_h,
3438 int *cols,
3439 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003440{
3441 int base_width, base_height;
3442
3443 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003444 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3445 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003447 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3448 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3449 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3450 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003451 *cols = (w - base_width) / gui.char_width;
3452 *rows = (h - base_height) / gui.char_height;
3453 *valid_w = base_width + *cols * gui.char_width;
3454 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003455}
3456
3457 void
3458gui_mch_flash(int msec)
3459{
3460 RECT rc;
3461
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003462#if defined(FEAT_DIRECTX)
3463 if (IS_ENABLE_DIRECTX())
3464 DWriteContext_Flush(s_dwc);
3465#endif
3466
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003467 /*
3468 * Note: InvertRect() excludes right and bottom of rectangle.
3469 */
3470 rc.left = 0;
3471 rc.top = 0;
3472 rc.right = gui.num_cols * gui.char_width;
3473 rc.bottom = gui.num_rows * gui.char_height;
3474 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003475 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476
Bram Moolenaar734a8672019-12-02 22:49:38 +01003477 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003478
3479 InvertRect(s_hdc, &rc);
3480}
3481
3482/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003483 * Check if the specified point is on-screen. (multi-monitor aware)
3484 */
3485 static BOOL
3486is_point_onscreen(int x, int y)
3487{
3488 POINT pt = {x, y};
3489
3490 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3491}
3492
3493/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003494 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003495 *
3496 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3497 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3498 * only works when the whole window is on-screen.
3499 */
3500 static BOOL
3501is_window_onscreen(HWND hwnd)
3502{
3503 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003504 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003505
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003506 GetClientRect(hwnd, &rc);
3507 p1.x = rc.left;
3508 p1.y = rc.top;
3509 p2.x = rc.right - 1;
3510 p2.y = rc.bottom - 1;
3511 ClientToScreen(hwnd, &p1);
3512 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003513
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003514 if (!is_point_onscreen(p1.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(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003517 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003518 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003519 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003520 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003521 return FALSE;
3522 return TRUE;
3523}
3524
3525/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003526 * Return flags used for scrolling.
3527 * The SW_INVALIDATE is required when part of the window is covered or
3528 * off-screen. Refer to MS KB Q75236.
3529 */
3530 static int
3531get_scroll_flags(void)
3532{
3533 HWND hwnd;
3534 RECT rcVim, rcOther, rcDest;
3535
Bram Moolenaar185577e2020-10-29 20:08:21 +01003536 // Check if the window is (partly) off-screen.
3537 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003538 return SW_INVALIDATE;
3539
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003540 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003541 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003542 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3543 if (IsWindowVisible(hwnd))
3544 {
3545 GetWindowRect(hwnd, &rcOther);
3546 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3547 return SW_INVALIDATE;
3548 }
3549 return 0;
3550}
3551
3552/*
3553 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3554 * may not be scrolled out properly.
3555 * For gVim, when _OnScroll() is repeated, the character at the
3556 * previous cursor position may be left drawn after scroll.
3557 * The problem can be avoided by calling GetPixel() to get a pixel in
3558 * the region before ScrollWindowEx().
3559 */
3560 static void
3561intel_gpu_workaround(void)
3562{
3563 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3564}
3565
3566/*
3567 * Delete the given number of lines from the given row, scrolling up any
3568 * text further down within the scroll region.
3569 */
3570 void
3571gui_mch_delete_lines(
3572 int row,
3573 int num_lines)
3574{
3575 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003576
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003577 rc.left = FILL_X(gui.scroll_region_left);
3578 rc.right = FILL_X(gui.scroll_region_right + 1);
3579 rc.top = FILL_Y(row);
3580 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3581
Bram Moolenaar92467d32017-12-05 13:22:16 +01003582#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003583 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003584 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003585 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003586 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003587 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003588#endif
3589 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003590#if defined(FEAT_DIRECTX)
3591 if (IS_ENABLE_DIRECTX())
3592 DWriteContext_Flush(s_dwc);
3593#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003594 intel_gpu_workaround();
3595 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003597 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003598 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599
Bram Moolenaar734a8672019-12-02 22:49:38 +01003600 // This seems to be required to avoid the cursor disappearing when
3601 // scrolling such that the cursor ends up in the top-left character on
3602 // the screen... But why? (Webb)
3603 // It's probably fixed by disabling drawing the cursor while scrolling.
3604 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003605
3606 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3607 gui.scroll_region_left,
3608 gui.scroll_region_bot, gui.scroll_region_right);
3609}
3610
3611/*
3612 * Insert the given number of lines before the given row, scrolling down any
3613 * following text within the scroll region.
3614 */
3615 void
3616gui_mch_insert_lines(
3617 int row,
3618 int num_lines)
3619{
3620 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003621
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 rc.left = FILL_X(gui.scroll_region_left);
3623 rc.right = FILL_X(gui.scroll_region_right + 1);
3624 rc.top = FILL_Y(row);
3625 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003626
3627#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003628 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003629 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003630 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003631 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003632 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003633#endif
3634 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003635#if defined(FEAT_DIRECTX)
3636 if (IS_ENABLE_DIRECTX())
3637 DWriteContext_Flush(s_dwc);
3638#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003639 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003640 // The SW_INVALIDATE is required when part of the window is covered or
3641 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003642 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003643 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003644 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003645 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003646
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003647 gui_clear_block(row, gui.scroll_region_left,
3648 row + num_lines - 1, gui.scroll_region_right);
3649}
3650
3651
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003652 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003653gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003654{
3655#if defined(FEAT_DIRECTX)
3656 DWriteContext_Close(s_dwc);
3657 DWrite_Final();
3658 s_dwc = NULL;
3659#endif
3660
3661 ReleaseDC(s_textArea, s_hdc);
3662 DeleteObject(s_brush);
3663
3664#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003665 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003666 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3667#endif
3668
Bram Moolenaar734a8672019-12-02 22:49:38 +01003669 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003670 if (s_hwnd != NULL)
3671 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003672 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003673 DestroyWindow(s_hwnd);
3674 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003675}
3676
3677 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003678logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003679{
3680 char *p;
3681 char *res;
3682 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003683 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003684 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003685 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003686
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003687 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3688 if (font_name == NULL)
3689 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003690 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003691 quality_name = quality_id2name((int)lf.lfQuality);
3692
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003693 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003694 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003695 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003696 if (res != NULL)
3697 {
3698 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003699 // make a normal font string out of the lf thing:
3700 points = pixels_to_points(
3701 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3702 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3703 sprintf((char *)p, "%s:h%d", font_name, points);
3704 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003705 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003706 while (*p)
3707 {
3708 if (*p == ' ')
3709 *p = '_';
3710 ++p;
3711 }
3712 if (lf.lfItalic)
3713 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003714 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003715 STRCAT(p, ":b");
3716 if (lf.lfUnderline)
3717 STRCAT(p, ":u");
3718 if (lf.lfStrikeOut)
3719 STRCAT(p, ":s");
3720 if (charset_name != NULL)
3721 {
3722 STRCAT(p, ":c");
3723 STRCAT(p, charset_name);
3724 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003725 if (quality_name != NULL)
3726 {
3727 STRCAT(p, ":q");
3728 STRCAT(p, quality_name);
3729 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003730 }
3731
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003732 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003733 return (char_u *)res;
3734}
3735
3736
3737#ifdef FEAT_MBYTE_IME
3738/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003739 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003740 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741 */
3742 static void
3743update_im_font(void)
3744{
K.Takatac81e9bf2022-01-16 14:15:49 +00003745 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003746
3747 if (p_guifontwide != NULL && *p_guifontwide != NUL
3748 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003749 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003750 norm_logfont = lf_wide;
3751 else
3752 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003753
3754 lf = norm_logfont;
3755 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3756 // Work around when PerMonitorV2 is not enabled in the process level.
3757 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3758 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003759}
3760#endif
3761
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003762/*
3763 * Handler of gui.wide_font (p_guifontwide) changed notification.
3764 */
3765 void
3766gui_mch_wide_font_changed(void)
3767{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003768 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003769
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003770#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003771 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003772#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003773
3774 gui_mch_free_font(gui.wide_ital_font);
3775 gui.wide_ital_font = NOFONT;
3776 gui_mch_free_font(gui.wide_bold_font);
3777 gui.wide_bold_font = NOFONT;
3778 gui_mch_free_font(gui.wide_boldital_font);
3779 gui.wide_boldital_font = NOFONT;
3780
3781 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003782 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 {
3784 if (!lf.lfItalic)
3785 {
3786 lf.lfItalic = TRUE;
3787 gui.wide_ital_font = get_font_handle(&lf);
3788 lf.lfItalic = FALSE;
3789 }
3790 if (lf.lfWeight < FW_BOLD)
3791 {
3792 lf.lfWeight = FW_BOLD;
3793 gui.wide_bold_font = get_font_handle(&lf);
3794 if (!lf.lfItalic)
3795 {
3796 lf.lfItalic = TRUE;
3797 gui.wide_boldital_font = get_font_handle(&lf);
3798 }
3799 }
3800 }
3801}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802
3803/*
3804 * Initialise vim to use the font with the given name.
3805 * Return FAIL if the font could not be loaded, OK otherwise.
3806 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003808gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809{
K.Takatac81e9bf2022-01-16 14:15:49 +00003810 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003811 GuiFont font = NOFONT;
3812 char_u *p;
3813
Bram Moolenaar734a8672019-12-02 22:49:38 +01003814 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003815 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003816 {
3817 lfOrig = lf;
3818 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003820 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003821 if (font == NOFONT)
3822 return FAIL;
3823
3824 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003825 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003826#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003827 norm_logfont = lf;
3828 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003829 if (!s_in_dpichanged)
3830 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831#endif
3832 gui_mch_free_font(gui.norm_font);
3833 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003834 current_font_height = lfOrig.lfHeight;
Ken Takatada5da652023-10-05 20:20:58 +02003835 UpdateFontSize(font);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003836
K.Takatac81e9bf2022-01-16 14:15:49 +00003837 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003838 if (p != NULL)
3839 {
3840 hl_set_font_name(p);
3841
Bram Moolenaar734a8672019-12-02 22:49:38 +01003842 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003843 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3844 {
3845 vim_free(p_guifont);
3846 p_guifont = p;
3847 }
3848 else
3849 vim_free(p);
3850 }
3851
3852 gui_mch_free_font(gui.ital_font);
3853 gui.ital_font = NOFONT;
3854 gui_mch_free_font(gui.bold_font);
3855 gui.bold_font = NOFONT;
3856 gui_mch_free_font(gui.boldital_font);
3857 gui.boldital_font = NOFONT;
3858
3859 if (!lf.lfItalic)
3860 {
3861 lf.lfItalic = TRUE;
3862 gui.ital_font = get_font_handle(&lf);
3863 lf.lfItalic = FALSE;
3864 }
3865 if (lf.lfWeight < FW_BOLD)
3866 {
3867 lf.lfWeight = FW_BOLD;
3868 gui.bold_font = get_font_handle(&lf);
3869 if (!lf.lfItalic)
3870 {
3871 lf.lfItalic = TRUE;
3872 gui.boldital_font = get_font_handle(&lf);
3873 }
3874 }
3875
3876 return OK;
3877}
3878
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003879/*
3880 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003881 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003882 */
3883 int
3884gui_mch_maximized(void)
3885{
3886 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003887 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003888
3889 wp.length = sizeof(WINDOWPLACEMENT);
3890 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003891 {
3892 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003894 && wp.flags == WPF_RESTORETOMAXIMIZED))
3895 return TRUE;
3896 if (wp.showCmd == SW_SHOWMINIMIZED)
3897 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003899 // Assume the window is snapped when the sizes from two APIs differ.
3900 GetWindowRect(s_hwnd, &rc);
3901 if ((rc.right - rc.left !=
3902 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3903 || (rc.bottom - rc.top !=
3904 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3905 return TRUE;
3906 }
3907 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908}
3909
3910/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003911 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3912 * is set. Compute the new Rows and Columns. This is like resizing the
3913 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914 */
3915 void
3916gui_mch_newfont(void)
3917{
3918 RECT rect;
3919
3920 GetWindowRect(s_hwnd, &rect);
3921 if (win_socket_id == 0)
3922 {
3923 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003924 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3925 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003927 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3928 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3929 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3930 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 }
3932 else
3933 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003934 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003935 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003936 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003937 }
3938}
3939
3940/*
3941 * Set the window title
3942 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003943 void
3944gui_mch_settitle(
3945 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003946 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003947{
3948 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3949}
3950
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003951#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003952// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3953// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003954static LPCSTR mshape_idcs[] =
3955{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003956 IDC_ARROW, // arrow
3957 MAKEINTRESOURCE(0), // blank
3958 IDC_IBEAM, // beam
3959 IDC_SIZENS, // updown
3960 IDC_SIZENS, // udsizing
3961 IDC_SIZEWE, // leftright
3962 IDC_SIZEWE, // lrsizing
3963 IDC_WAIT, // busy
3964 IDC_NO, // no
3965 IDC_ARROW, // crosshair
3966 IDC_ARROW, // hand1
3967 IDC_ARROW, // hand2
3968 IDC_ARROW, // pencil
3969 IDC_ARROW, // question
3970 IDC_ARROW, // right-arrow
3971 IDC_UPARROW, // up-arrow
3972 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003973};
3974
3975 void
3976mch_set_mouse_shape(int shape)
3977{
3978 LPCSTR idc;
3979
3980 if (shape == MSHAPE_HIDE)
3981 ShowCursor(FALSE);
3982 else
3983 {
3984 if (shape >= MSHAPE_NUMBERED)
3985 idc = IDC_ARROW;
3986 else
3987 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003988 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003989 if (!p_mh)
3990 {
3991 POINT mp;
3992
Bram Moolenaar734a8672019-12-02 22:49:38 +01003993 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003994 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003995 (void)SetCursorPos(mp.x, mp.y);
3996 ShowCursor(TRUE);
3997 }
3998 }
3999}
4000#endif
4001
Bram Moolenaara6b7a082016-08-10 20:53:05 +02004002#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004003/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004004 * Wide version of convert_filter().
4005 */
4006 static WCHAR *
4007convert_filterW(char_u *s)
4008{
4009 char_u *tmp;
4010 int len;
4011 WCHAR *res;
4012
4013 tmp = convert_filter(s);
4014 if (tmp == NULL)
4015 return NULL;
4016 len = (int)STRLEN(s) + 3;
4017 res = enc_to_utf16(tmp, &len);
4018 vim_free(tmp);
4019 return res;
4020}
4021
4022/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004023 * Pop open a file browser and return the file selected, in allocated memory,
4024 * or NULL if Cancel is hit.
4025 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
4026 * title - Title message for the file browser dialog.
4027 * dflt - Default name of file.
4028 * ext - Default extension to be added to files without extensions.
4029 * initdir - directory in which to open the browser (NULL = current dir)
4030 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004031 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01004032 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004033gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004034 int saving,
4035 char_u *title,
4036 char_u *dflt,
4037 char_u *ext,
4038 char_u *initdir,
4039 char_u *filter)
4040{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004041 // We always use the wide function. This means enc_to_utf16() must work,
4042 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004043 OPENFILENAMEW fileStruct;
4044 WCHAR fileBuf[MAXPATHL];
4045 WCHAR *wp;
4046 int i;
4047 WCHAR *titlep = NULL;
4048 WCHAR *extp = NULL;
4049 WCHAR *initdirp = NULL;
4050 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004051 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00004052 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004053
4054 if (dflt == NULL)
4055 fileBuf[0] = NUL;
4056 else
4057 {
4058 wp = enc_to_utf16(dflt, NULL);
4059 if (wp == NULL)
4060 fileBuf[0] = NUL;
4061 else
4062 {
4063 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
4064 fileBuf[i] = wp[i];
4065 fileBuf[i] = NUL;
4066 vim_free(wp);
4067 }
4068 }
4069
Bram Moolenaar734a8672019-12-02 22:49:38 +01004070 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004071 filterp = convert_filterW(filter);
4072
Bram Moolenaara80faa82020-04-12 19:37:17 +02004073 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004074# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01004075 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01004076 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004077# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004078 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004079# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004080
4081 if (title != NULL)
4082 titlep = enc_to_utf16(title, NULL);
4083 fileStruct.lpstrTitle = titlep;
4084
4085 if (ext != NULL)
4086 extp = enc_to_utf16(ext, NULL);
4087 fileStruct.lpstrDefExt = extp;
4088
4089 fileStruct.lpstrFile = fileBuf;
4090 fileStruct.nMaxFile = MAXPATHL;
4091 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004092 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
4093 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004094 if (initdir != NULL && *initdir != NUL)
4095 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004096 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004097 initdirp = enc_to_utf16(initdir, NULL);
4098 if (initdirp != NULL)
4099 {
4100 for (wp = initdirp; *wp != NUL; ++wp)
4101 if (*wp == '/')
4102 *wp = '\\';
4103 }
4104 fileStruct.lpstrInitialDir = initdirp;
4105 }
4106
4107 /*
4108 * TODO: Allow selection of multiple files. Needs another arg to this
4109 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
4110 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
4111 * files that don't exist yet, so I haven't put it in. What about
4112 * OFN_PATHMUSTEXIST?
4113 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
4114 */
4115 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004116# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004117 if (curbuf->b_p_bin)
4118 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01004119# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004120 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00004121 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004122 else
K.Takata14b8d6a2022-01-20 15:05:22 +00004123 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004124
4125 vim_free(filterp);
4126 vim_free(initdirp);
4127 vim_free(titlep);
4128 vim_free(extp);
4129
K.Takata14b8d6a2022-01-20 15:05:22 +00004130 if (!ret)
4131 return NULL;
4132
4133 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004134 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004135 if (p == NULL)
4136 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004137
Bram Moolenaar734a8672019-12-02 22:49:38 +01004138 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004139 SetFocus(s_hwnd);
4140
Bram Moolenaar734a8672019-12-02 22:49:38 +01004141 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02004142 q = vim_strsave(shorten_fname1(p));
4143 vim_free(p);
4144 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004145}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004146
4147
4148/*
4149 * Convert the string s to the proper format for a filter string by replacing
4150 * the \t and \n delimiters with \0.
4151 * Returns the converted string in allocated memory.
4152 *
4153 * Keep in sync with convert_filterW() above!
4154 */
4155 static char_u *
4156convert_filter(char_u *s)
4157{
4158 char_u *res;
4159 unsigned s_len = (unsigned)STRLEN(s);
4160 unsigned i;
4161
4162 res = alloc(s_len + 3);
4163 if (res != NULL)
4164 {
4165 for (i = 0; i < s_len; ++i)
4166 if (s[i] == '\t' || s[i] == '\n')
4167 res[i] = '\0';
4168 else
4169 res[i] = s[i];
4170 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004171 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004172 res[s_len + 1] = NUL;
4173 res[s_len + 2] = NUL;
4174 }
4175 return res;
4176}
4177
4178/*
4179 * Select a directory.
4180 */
4181 char_u *
4182gui_mch_browsedir(char_u *title, char_u *initdir)
4183{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004184 // We fake this: Use a filter that doesn't select anything and a default
4185 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004186 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
4187 initdir, (char_u *)_("Directory\t*.nothing\n"));
4188}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004189#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004190
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004191 static void
4192_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004193 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004194 HDROP hDrop)
4195{
Bram Moolenaar4033c552017-09-16 20:54:51 +02004196#define BUFPATHLEN _MAX_PATH
4197#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004198 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004199 char szFile[BUFPATHLEN];
4200 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
4201 UINT i;
4202 char_u **fnames;
4203 POINT pt;
4204 int_u modifiers = 0;
4205
Bram Moolenaar734a8672019-12-02 22:49:38 +01004206 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004207 DragQueryPoint(hDrop, &pt);
4208 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
4209
4210 reset_VIsual();
4211
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004212 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004213
4214 if (fnames != NULL)
4215 for (i = 0; i < cFiles; ++i)
4216 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004217 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
4218 fnames[i] = utf16_to_enc(wszFile, NULL);
4219 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004220 {
4221 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
4222 fnames[i] = vim_strsave((char_u *)szFile);
4223 }
4224 }
4225
4226 DragFinish(hDrop);
4227
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004228 if (fnames == NULL)
4229 return;
LemonBoy45684c62022-04-24 15:46:42 +01004230
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004231 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004232
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004233 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4234 modifiers |= MOUSE_SHIFT;
4235 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4236 modifiers |= MOUSE_CTRL;
4237 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4238 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004239
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004240 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
4241
4242 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004243}
4244
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004245 static int
4246_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01004247 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004248 HWND hwndCtl,
4249 UINT code,
4250 int pos)
4251{
Bram Moolenaar734a8672019-12-02 22:49:38 +01004252 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004253 scrollbar_T *sb, *sb_info;
4254 long val;
4255 int dragging = FALSE;
4256 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004257 SCROLLINFO si;
4258
4259 si.cbSize = sizeof(si);
4260 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004261
4262 sb = gui_mswin_find_scrollbar(hwndCtl);
4263 if (sb == NULL)
4264 return 0;
4265
Bram Moolenaar734a8672019-12-02 22:49:38 +01004266 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004267 {
4268 /*
4269 * Careful: need to get scrollbar info out of first (left) scrollbar
4270 * for window, but keep real scrollbar too because we must pass it to
4271 * gui_drag_scrollbar().
4272 */
4273 sb_info = &sb->wp->w_scrollbars[0];
4274 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004275 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004276 sb_info = sb;
4277 val = sb_info->value;
4278
4279 switch (code)
4280 {
4281 case SB_THUMBTRACK:
4282 val = pos;
4283 dragging = TRUE;
4284 if (sb->scroll_shift > 0)
4285 val <<= sb->scroll_shift;
4286 break;
4287 case SB_LINEDOWN:
4288 val++;
4289 break;
4290 case SB_LINEUP:
4291 val--;
4292 break;
4293 case SB_PAGEDOWN:
4294 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
4295 break;
4296 case SB_PAGEUP:
4297 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
4298 break;
4299 case SB_TOP:
4300 val = 0;
4301 break;
4302 case SB_BOTTOM:
4303 val = sb_info->max;
4304 break;
4305 case SB_ENDSCROLL:
4306 if (prev_code == SB_THUMBTRACK)
4307 {
4308 /*
4309 * "pos" only gives us 16-bit data. In case of large file,
4310 * use GetScrollPos() which returns 32-bit. Unfortunately it
4311 * is not valid while the scrollbar is being dragged.
4312 */
4313 val = GetScrollPos(hwndCtl, SB_CTL);
4314 if (sb->scroll_shift > 0)
4315 val <<= sb->scroll_shift;
4316 }
4317 break;
4318
4319 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004320 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004321 return 0;
4322 }
4323 prev_code = code;
4324
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004325 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4326 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004327
4328 /*
4329 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4330 */
4331 if (sb->wp != NULL)
4332 {
4333 scrollbar_T *sba = sb->wp->w_scrollbars;
4334 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4335
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004336 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004337 }
4338
Bram Moolenaar734a8672019-12-02 22:49:38 +01004339 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004340 s_busy_processing = TRUE;
4341
Bram Moolenaar734a8672019-12-02 22:49:38 +01004342 // When "allow_scrollbar" is FALSE still need to remember the new
4343 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004344 dont_scroll = !allow_scrollbar;
4345
Bram Moolenaara338adc2018-01-31 20:51:47 +01004346 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004347 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004348 mch_enable_flush();
4349 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004350
4351 s_busy_processing = FALSE;
4352 dont_scroll = dont_scroll_save;
4353
4354 return 0;
4355}
4356
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#ifdef FEAT_XPM_W32
4359# include "xpm_w32.h"
4360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
Bram Moolenaar734a8672019-12-02 22:49:38 +01004363// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364#define TEAROFF_PADDING_X 2
4365#define TEAROFF_BUTTON_PAD_X 8
4366#define TEAROFF_MIN_WIDTH 200
4367#define TEAROFF_SUBMENU_LABEL ">>"
4368#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4369
4370
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004371#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372# define ID_BEVAL_TOOLTIP 200
4373# define BEVAL_TEXT_LEN MAXPATHL
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004376static UINT_PTR beval_timer_id = 0;
4377static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004378#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004379
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004380
Bram Moolenaar734a8672019-12-02 22:49:38 +01004381// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383#ifdef FEAT_MENU
4384static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387/*
4388 * Use the system font for dialogs and tear-off menus. Remove this line to
4389 * use DLG_FONT_NAME.
4390 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004391#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#define VIM_CLASSW L"Vim"
4395
Bram Moolenaar734a8672019-12-02 22:49:38 +01004396// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4397// thus there should be room for every dialog. For tearoffs it's made bigger
4398// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399#define DLG_ALLOC_SIZE 16 * 1024
4400
4401/*
4402 * stuff for dialogs, menus, tearoffs etc.
4403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404static PWORD
4405add_dialog_element(
4406 PWORD p,
4407 DWORD lStyle,
4408 WORD x,
4409 WORD y,
4410 WORD w,
4411 WORD h,
4412 WORD Id,
4413 WORD clss,
4414 const char *caption);
4415static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004416static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004417#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420static void get_dialog_font_metrics(void);
4421
4422static int dialog_default_button = -1;
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_TOOLBAR
4425static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004426static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004427static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004429#else
4430# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431#endif
4432
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004433#ifdef FEAT_GUI_TABLINE
4434static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004435static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004436#endif
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifdef FEAT_MBYTE_IME
4439static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4440static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4441#endif
4442#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4443# ifdef NOIME
4444typedef struct tagCOMPOSITIONFORM {
4445 DWORD dwStyle;
4446 POINT ptCurrentPos;
4447 RECT rcArea;
4448} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4449typedef HANDLE HIMC;
4450# endif
4451
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004452static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004453static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4454static HIMC (WINAPI *pImmGetContext)(HWND);
4455static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4456static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4457static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4458static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004459static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4460static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004461static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4462static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004463static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464static void dyn_imm_load(void);
4465#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466# define pImmGetCompositionStringW ImmGetCompositionStringW
4467# define pImmGetContext ImmGetContext
4468# define pImmAssociateContext ImmAssociateContext
4469# define pImmReleaseContext ImmReleaseContext
4470# define pImmGetOpenStatus ImmGetOpenStatus
4471# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004472# define pImmGetCompositionFontW ImmGetCompositionFontW
4473# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474# define pImmSetCompositionWindow ImmSetCompositionWindow
4475# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004476# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#endif
4478
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#ifdef FEAT_MENU
4480/*
4481 * Figure out how high the menu bar is at the moment.
4482 */
4483 static int
4484gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004485 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 static int old_menu_height = -1;
4488
4489 RECT rc1, rc2;
4490 int num;
4491 int menu_height;
4492
4493 if (gui.menu_is_active)
4494 num = GetMenuItemCount(s_menuBar);
4495 else
4496 num = 0;
4497
4498 if (num == 0)
4499 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004500 else if (IsMinimized(s_hwnd))
4501 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004502 // The height of the menu cannot be determined while the window is
4503 // minimized. Take the previous height if the menu is changed in that
4504 // state, to avoid that Vim's vertical window size accidentally
4505 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004506 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 else
4509 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004510 /*
4511 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4512 * seem to have been set yet, so menu wraps in default window
4513 * width which is very narrow. Instead just return height of a
4514 * single menu item. Will still be wrong when the menu really
4515 * should wrap over more than one line.
4516 */
4517 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4518 if (gui.starting)
4519 menu_height = rc1.bottom - rc1.top + 1;
4520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004522 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4523 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525 }
4526
4527 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004528 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004529 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 return menu_height;
4532}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004533#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
4535
4536/*
4537 * Setup for the Intellimouse
4538 */
LemonBoyc27747e2022-05-07 12:25:40 +01004539 static long
4540mouse_vertical_scroll_step(void)
4541{
4542 UINT val;
4543 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4544 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4545 return 3; // Safe default;
4546}
4547
4548 static long
4549mouse_horizontal_scroll_step(void)
4550{
4551 UINT val;
4552 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4553 return (long)val;
4554 return 3; // Safe default;
4555}
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 static void
4558init_mouse_wheel(void)
4559{
LemonBoyc27747e2022-05-07 12:25:40 +01004560 // Get the default values for the horizontal and vertical scroll steps from
4561 // the system.
4562 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4563 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564}
4565
Bram Moolenaarae20f342019-11-05 21:09:23 +01004566/*
LemonBoya773d842022-05-10 20:54:46 +01004567 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004570_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
LemonBoy365d8f72022-05-05 19:23:07 +01004572 int button;
4573 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004574 int modifiers = 0;
4575 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004576 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4577 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaarae20f342019-11-05 21:09:23 +01004579 wp = gui_mouse_window(FIND_POPUP);
4580
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004581#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004582 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004583 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004584 cmdarg_T cap;
4585 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004586
Bram Moolenaarae20f342019-11-05 21:09:23 +01004587 // Mouse hovers over popup window, scroll it if possible.
4588 mouse_row = wp->w_winrow;
4589 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004590 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004591 if (horizontal)
4592 {
4593 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4594 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4595 }
4596 else
4597 {
4598 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4599 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4600 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004601 clear_oparg(&oa);
4602 cap.oap = &oa;
4603 nv_mousescroll(&cap);
4604 update_screen(0);
4605 setcursor();
4606 out_flush();
4607 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004608 }
4609#endif
4610
Bram Moolenaarae20f342019-11-05 21:09:23 +01004611 if (wp == NULL || !p_scf)
4612 wp = curwin;
4613
LemonBoy365d8f72022-05-05 19:23:07 +01004614 // Translate the scroll event into an event that Vim can process so that
4615 // the user has a chance to map the scrollwheel buttons.
4616 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004617 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else
LemonBoy365d8f72022-05-05 19:23:07 +01004619 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004620
4621 kbd_modifiers = get_active_modifiers();
4622
4623 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4624 modifiers |= MOUSE_SHIFT;
4625 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4626 modifiers |= MOUSE_CTRL;
4627 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4628 modifiers |= MOUSE_ALT;
4629
LemonBoya773d842022-05-10 20:54:46 +01004630 // The cursor position is relative to the upper-left corner of the screen.
4631 pt.x = GET_X_LPARAM(lParam);
4632 pt.y = GET_Y_LPARAM(lParam);
4633 ScreenToClient(s_textArea, &pt);
4634
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004635 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636}
4637
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004638#ifdef USE_SYSMENU_FONT
4639/*
4640 * Get Menu Font.
4641 * Return OK or FAIL.
4642 */
4643 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004644gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004645{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004646 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004647
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004648 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4649 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004650 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004651 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004652 &nm,
4653 0))
4654 return FAIL;
4655 *lf = nm.lfMenuFont;
4656 return OK;
4657}
4658#endif
4659
4660
4661#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4662/*
4663 * Set the GUI tabline font to the system menu font
4664 */
4665 static void
4666set_tabline_font(void)
4667{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004668 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004669 HFONT font;
4670 HWND hwnd;
4671 HDC hdc;
4672 HFONT hfntOld;
4673 TEXTMETRIC tm;
4674
4675 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4676 return;
4677
K.Takatac81e9bf2022-01-16 14:15:49 +00004678 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004679 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004680
4681 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4682
4683 /*
4684 * Compute the height of the font used for the tab text
4685 */
4686 hwnd = GetDesktopWindow();
4687 hdc = GetWindowDC(hwnd);
4688 hfntOld = SelectFont(hdc, font);
4689
4690 GetTextMetrics(hdc, &tm);
4691
4692 SelectFont(hdc, hfntOld);
4693 ReleaseDC(hwnd, hdc);
4694
4695 /*
4696 * The space used by the tab border and the space between the tab label
4697 * and the tab border is included as 7.
4698 */
4699 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4700}
K.Takatac81e9bf2022-01-16 14:15:49 +00004701#else
4702# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004703#endif
4704
Bram Moolenaar520470a2005-06-16 21:59:56 +00004705/*
4706 * Invoked when a setting was changed.
4707 */
4708 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004709_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004710{
LemonBoy365d8f72022-05-05 19:23:07 +01004711 switch (param)
4712 {
4713 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004714 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004715 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004716 case SPI_SETWHEELSCROLLCHARS:
4717 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004718 break;
4719 case SPI_SETNONCLIENTMETRICS:
4720 set_tabline_font();
4721 break;
4722 default:
4723 break;
4724 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004725 return 0;
4726}
4727
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#ifdef FEAT_NETBEANS_INTG
4729 static void
4730_OnWindowPosChanged(
4731 HWND hwnd,
4732 const LPWINDOWPOS lpwpos)
4733{
4734 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004735 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
4737 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4738 || lpwpos->cx != cx || lpwpos->cy != cy))
4739 {
4740 x = lpwpos->x;
4741 y = lpwpos->y;
4742 cx = lpwpos->cx;
4743 cy = lpwpos->cy;
4744 netbeans_frame_moved(x, y);
4745 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004746 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004747 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748}
4749#endif
4750
K.Takata4f2417f2021-06-05 16:25:32 +02004751
4752static HWND hwndTip = NULL;
4753
4754 static void
4755show_sizing_tip(int cols, int rows)
4756{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004757 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004758 char buf[32];
4759
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004760 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004761 ti.hwnd = s_hwnd;
4762 ti.uId = (UINT_PTR)s_hwnd;
4763 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4764 ti.lpszText = buf;
4765 sprintf(buf, "%dx%d", cols, rows);
4766 if (hwndTip == NULL)
4767 {
4768 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4769 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4770 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4771 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4772 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4773 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4774 }
4775 else
4776 {
4777 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4778 }
4779 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4780}
4781
4782 static void
4783destroy_sizing_tip(void)
4784{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004785 if (hwndTip == NULL)
4786 return;
4787
4788 DestroyWindow(hwndTip);
4789 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004790}
4791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 static int
4793_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 UINT fwSide,
4795 LPRECT lprc)
4796{
4797 int w, h;
4798 int valid_w, valid_h;
4799 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004800 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 w = lprc->right - lprc->left;
4803 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004804 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 w_offset = w - valid_w;
4806 h_offset = h - valid_h;
4807
4808 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4809 || fwSide == WMSZ_BOTTOMLEFT)
4810 lprc->left += w_offset;
4811 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4812 || fwSide == WMSZ_BOTTOMRIGHT)
4813 lprc->right -= w_offset;
4814
4815 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4816 || fwSide == WMSZ_TOPRIGHT)
4817 lprc->top += h_offset;
4818 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4819 || fwSide == WMSZ_BOTTOMRIGHT)
4820 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004821
4822 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 return TRUE;
4824}
4825
K.Takata92000e22022-01-20 15:10:57 +00004826#ifdef FEAT_GUI_TABLINE
4827 static void
4828_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4829{
4830 if (gui_mch_showing_tabline())
4831 {
4832 POINT pt;
4833 RECT rect;
4834
4835 /*
4836 * If the cursor is on the tabline, display the tab menu
4837 */
4838 GetCursorPos(&pt);
4839 GetWindowRect(s_textArea, &rect);
4840 if (pt.y < rect.top)
4841 {
4842 show_tabline_popup_menu();
4843 return;
4844 }
4845 }
4846 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4847}
4848
4849 static void
4850_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4851{
4852 /*
4853 * If the user double clicked the tabline, create a new tab
4854 */
4855 if (gui_mch_showing_tabline())
4856 {
4857 POINT pt;
4858 RECT rect;
4859
4860 GetCursorPos(&pt);
4861 GetWindowRect(s_textArea, &rect);
4862 if (pt.y < rect.top)
4863 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4864 }
4865 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4866}
4867#endif
4868
4869 static UINT
4870_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4871{
4872 UINT result;
4873 int x, y;
4874
4875 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4876 if (result != HTCLIENT)
4877 return result;
4878
4879#ifdef FEAT_GUI_TABLINE
4880 if (gui_mch_showing_tabline())
4881 {
4882 RECT rct;
4883
4884 // If the cursor is on the GUI tabline, don't process this event
4885 GetWindowRect(s_textArea, &rct);
4886 if (yPos < rct.top)
4887 return result;
4888 }
4889#endif
4890 (void)gui_mch_get_winpos(&x, &y);
4891 xPos -= x;
4892
4893 if (xPos < 48) // <VN> TODO should use system metric?
4894 return HTBOTTOMLEFT;
4895 else
4896 return HTBOTTOMRIGHT;
4897}
4898
4899#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4900 static LRESULT
4901_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4902{
4903 switch (hdr->code)
4904 {
4905 case TTN_GETDISPINFOW:
4906 case TTN_GETDISPINFO:
4907 {
4908 char_u *str = NULL;
4909 static void *tt_text = NULL;
4910
4911 VIM_CLEAR(tt_text);
4912
4913# ifdef FEAT_GUI_TABLINE
4914 if (gui_mch_showing_tabline()
4915 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4916 {
4917 POINT pt;
4918 /*
4919 * Mouse is over the GUI tabline. Display the
4920 * tooltip for the tab under the cursor
4921 *
4922 * Get the cursor position within the tab control
4923 */
4924 GetCursorPos(&pt);
4925 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4926 {
4927 TCHITTESTINFO htinfo;
4928 int idx;
4929
4930 /*
4931 * Get the tab under the cursor
4932 */
4933 htinfo.pt.x = pt.x;
4934 htinfo.pt.y = pt.y;
4935 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4936 if (idx != -1)
4937 {
4938 tabpage_T *tp;
4939
4940 tp = find_tabpage(idx + 1);
4941 if (tp != NULL)
4942 {
4943 get_tabline_label(tp, TRUE);
4944 str = NameBuff;
4945 }
4946 }
4947 }
4948 }
4949# endif
4950# ifdef FEAT_TOOLBAR
4951# ifdef FEAT_GUI_TABLINE
4952 else
4953# endif
4954 {
4955 UINT idButton;
4956 vimmenu_T *pMenu;
4957
4958 idButton = (UINT) hdr->idFrom;
4959 pMenu = gui_mswin_find_menu(root_menu, idButton);
4960 if (pMenu)
4961 str = pMenu->strings[MENU_INDEX_TIP];
4962 }
4963# endif
4964 if (str == NULL)
4965 break;
4966
4967 // Set the maximum width, this also enables using \n for
4968 // line break.
4969 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4970
4971 if (hdr->code == TTN_GETDISPINFOW)
4972 {
4973 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4974
4975 tt_text = enc_to_utf16(str, NULL);
4976 lpdi->lpszText = tt_text;
4977 // can't show tooltip if failed
4978 }
4979 else
4980 {
4981 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4982
4983 if (STRLEN(str) < sizeof(lpdi->szText)
4984 || ((tt_text = vim_strsave(str)) == NULL))
4985 vim_strncpy((char_u *)lpdi->szText, str,
4986 sizeof(lpdi->szText) - 1);
4987 else
4988 lpdi->lpszText = tt_text;
4989 }
4990 }
4991 break;
4992
4993# ifdef FEAT_GUI_TABLINE
4994 case TCN_SELCHANGE:
4995 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4996 {
4997 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4998 return 0L;
4999 }
5000 break;
5001
5002 case NM_RCLICK:
5003 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
5004 {
5005 show_tabline_popup_menu();
5006 return 0L;
5007 }
5008 break;
5009# endif
5010
5011 default:
5012 break;
5013 }
5014 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
5015}
5016#endif
5017
5018#if defined(MENUHINTS) && defined(FEAT_MENU)
5019 static LRESULT
5020_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
5021{
5022 if (((UINT) HIWORD(wParam)
5023 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
5024 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01005025 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00005026 {
5027 UINT idButton;
5028 vimmenu_T *pMenu;
5029 static int did_menu_tip = FALSE;
5030
5031 if (did_menu_tip)
5032 {
5033 msg_clr_cmdline();
5034 setcursor();
5035 out_flush();
5036 did_menu_tip = FALSE;
5037 }
5038
5039 idButton = (UINT)LOWORD(wParam);
5040 pMenu = gui_mswin_find_menu(root_menu, idButton);
5041 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
5042 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
5043 {
5044 ++msg_hist_off;
5045 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
5046 --msg_hist_off;
5047 setcursor();
5048 out_flush();
5049 did_menu_tip = TRUE;
5050 }
5051 return 0L;
5052 }
5053 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
5054}
5055#endif
5056
Ken Takata7086b3e2023-10-02 21:26:03 +02005057 static BOOL
5058_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
5059{
Ken Takatada5da652023-10-05 20:20:58 +02005060 int old_width, old_height;
5061 int new_width, new_height;
5062 LOGFONTW lf;
5063 HFONT font;
5064
Ken Takata7086b3e2023-10-02 21:26:03 +02005065 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
5066
5067 // Calculate new approximate size.
Ken Takatada5da652023-10-05 20:20:58 +02005068 GetFontSize(gui.norm_font, &old_width, &old_height); // Current size
5069 GetObjectW((HFONT)gui.norm_font, sizeof(lf), &lf);
5070 lf.lfHeight = lf.lfHeight * (int)dpi / s_dpi;
5071 font = CreateFontIndirectW(&lf);
5072 if (font)
5073 {
5074 GetFontSize((GuiFont)font, &new_width, &new_height); // New size
5075 DeleteFont(font);
5076 }
5077 else
5078 {
5079 new_width = old_width;
5080 new_height = old_height;
5081 }
5082 size->cx = size->cx * new_width / old_width;
5083 size->cy = size->cy * new_height / old_height;
Ken Takata7086b3e2023-10-02 21:26:03 +02005084 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
5085
Ken Takatada5da652023-10-05 20:20:58 +02005086 return TRUE;
Ken Takata7086b3e2023-10-02 21:26:03 +02005087}
5088
K.Takatac81e9bf2022-01-16 14:15:49 +00005089 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02005090_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00005091{
5092 s_dpi = ydpi;
5093 s_in_dpichanged = TRUE;
5094 //TRACE("DPI: %d", ydpi);
5095
Ken Takata7086b3e2023-10-02 21:26:03 +02005096 s_suggested_rect = *rc;
5097 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
5098 // rc->right - rc->left, rc->bottom - rc->top);
5099
K.Takatac81e9bf2022-01-16 14:15:49 +00005100 update_scrollbar_size();
5101 update_toolbar_size();
5102 set_tabline_font();
5103
5104 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
5105 gui_get_wide_font();
5106 gui_mswin_get_menu_height(FALSE);
5107#ifdef FEAT_MBYTE_IME
5108 im_set_position(gui.row, gui.col);
5109#endif
5110 InvalidateRect(hwnd, NULL, TRUE);
5111
5112 s_in_dpichanged = FALSE;
5113 return 0L;
5114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116
5117 static LRESULT CALLBACK
5118_WndProc(
5119 HWND hwnd,
5120 UINT uMsg,
5121 WPARAM wParam,
5122 LPARAM lParam)
5123{
LemonBoya773d842022-05-10 20:54:46 +01005124 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
5125 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 HandleMouseHide(uMsg, lParam);
5128
5129 s_uMsg = uMsg;
5130 s_wParam = wParam;
5131 s_lParam = lParam;
5132
5133 switch (uMsg)
5134 {
5135 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
5136 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005137 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005139 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
5141 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
5142 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
5143 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
5144#ifdef FEAT_MENU
5145 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
5146#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01005147 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
5148 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
5150 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005151 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
5152 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
5154 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
5155 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
5156#ifdef FEAT_NETBEANS_INTG
5157 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
5158#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00005159#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00005160 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
5161 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005162#endif
K.Takata92000e22022-01-20 15:10:57 +00005163 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00005164
Bram Moolenaar734a8672019-12-02 22:49:38 +01005165 case WM_QUERYENDSESSION: // System wants to go down.
5166 gui_shell_closed(); // Will exit when no changed buffers.
5167 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168
5169 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005170 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01005171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01005173 return 0L;
5174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 break;
5176
5177 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005178 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
5179 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005180 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 return 0L;
5182
5183 case WM_SYSCHAR:
5184 /*
5185 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
5186 * shortcut key, handle like a typed ALT key, otherwise call Windows
5187 * ALT key handling.
5188 */
5189#ifdef FEAT_MENU
5190 if ( !gui.menu_is_active
5191 || p_wak[0] == 'n'
5192 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
5193 )
5194#endif
5195 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005196 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 return 0L;
5198 }
5199#ifdef FEAT_MENU
5200 else
K.Takata4ac893f2022-01-20 12:44:28 +00005201 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#endif
5203
5204 case WM_SYSKEYUP:
5205#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005206 // This used to be done only when menu is active: ALT key is used for
5207 // that. But that caused problems when menu is disabled and using
5208 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
5209 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00005210 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01005212 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213#endif
5214
K.Takata4f2417f2021-06-05 16:25:32 +02005215 case WM_EXITSIZEMOVE:
5216 destroy_sizing_tip();
5217 break;
5218
Bram Moolenaar734a8672019-12-02 22:49:38 +01005219 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01005223 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01005224 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005225 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar734a8672019-12-02 22:49:38 +01005227 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00005228 case WM_SETTINGCHANGE:
5229 return _OnSettingChange((UINT)wParam);
5230
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005231#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00005233 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234#endif
K.Takata92000e22022-01-20 15:10:57 +00005235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236#if defined(MENUHINTS) && defined(FEAT_MENU)
5237 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00005238 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241#ifdef FEAT_MBYTE_IME
5242 case WM_IME_NOTIFY:
5243 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005244 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005245 return 1L;
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 case WM_IME_COMPOSITION:
5248 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00005249 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01005250 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02005252 case WM_GETDPISCALEDSIZE:
5253 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00005254 case WM_DPICHANGED:
5255 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
5256 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005260 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
K.Takata92000e22022-01-20 15:10:57 +00005263 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 }
5265
K.Takata4ac893f2022-01-20 12:44:28 +00005266 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269/*
5270 * End of call-back routines
5271 */
5272
Bram Moolenaar734a8672019-12-02 22:49:38 +01005273// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274HWND vim_parent_hwnd = NULL;
5275
5276 static BOOL CALLBACK
5277FindWindowTitle(HWND hwnd, LPARAM lParam)
5278{
5279 char buf[2048];
5280 char *title = (char *)lParam;
5281
5282 if (GetWindowText(hwnd, buf, sizeof(buf)))
5283 {
5284 if (strstr(buf, title) != NULL)
5285 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005286 // Found it. Store the window ref. and quit searching if MDI
5287 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005289 if (vim_parent_hwnd != NULL)
5290 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005293 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294}
5295
5296/*
5297 * Invoked for '-P "title"' argument: search for parent application to open
5298 * our window in.
5299 */
5300 void
5301gui_mch_set_parent(char *title)
5302{
5303 EnumWindows(FindWindowTitle, (LPARAM)title);
5304 if (vim_parent_hwnd == NULL)
5305 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005306 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 mch_exit(2);
5308 }
5309}
5310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 static void
5313ole_error(char *arg)
5314{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005315 char buf[IOSIZE];
5316
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02005317# ifdef VIMDLL
5318 gui.in_use = mch_is_gui_executable();
5319# endif
5320
Bram Moolenaar734a8672019-12-02 22:49:38 +01005321 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005322 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00005323 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00005324 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005328#if defined(GUI_MAY_SPAWN) || defined(PROTO)
5329 static char *
5330gvim_error(void)
5331{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005332 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005333
5334 if (starting)
5335 {
5336 mch_errmsg(msg);
5337 mch_errmsg("\n");
5338 mch_exit(2);
5339 }
5340 return msg;
5341}
5342
5343 char *
5344gui_mch_do_spawn(char_u *arg)
5345{
5346 int len;
5347# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5348 char_u *session = NULL;
5349 LPWSTR tofree1 = NULL;
5350# endif
5351 WCHAR name[MAX_PATH];
5352 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5353 STARTUPINFOW si = {sizeof(si)};
5354 PROCESS_INFORMATION pi;
5355
5356 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5357 goto error;
5358 p = wcsrchr(name, L'\\');
5359 if (p == NULL)
5360 goto error;
5361 // Replace the executable name from vim(d).exe to gvim(d).exe.
5362# ifdef DEBUG
5363 wcscpy(p + 1, L"gvimd.exe");
5364# else
5365 wcscpy(p + 1, L"gvim.exe");
5366# endif
5367
5368# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5369 if (starting)
5370# endif
5371 {
5372 // Pass the command line to the new process.
5373 p = GetCommandLineW();
5374 // Skip 1st argument.
5375 while (*p && *p != L' ' && *p != L'\t')
5376 {
5377 if (*p == L'"')
5378 {
5379 while (*p && *p != L'"')
5380 ++p;
5381 if (*p)
5382 ++p;
5383 }
5384 else
5385 ++p;
5386 }
5387 cmd = p;
5388 }
5389# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5390 else
5391 {
5392 // Create a session file and pass it to the new process.
5393 LPWSTR wsession;
5394 char_u *savebg;
5395 int ret;
5396
5397 session = vim_tempname('s', FALSE);
5398 if (session == NULL)
5399 goto error;
5400 savebg = p_bg;
5401 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5402 ret = write_session_file(session);
5403 vim_free(p_bg);
5404 p_bg = savebg;
5405 if (!ret)
5406 goto error;
5407 wsession = enc_to_utf16(session, NULL);
5408 if (wsession == NULL)
5409 goto error;
5410 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005411 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005412 if (cmd == NULL)
5413 {
5414 vim_free(wsession);
5415 goto error;
5416 }
5417 tofree1 = cmd;
5418 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5419 wsession, wsession);
5420 vim_free(wsession);
5421 }
5422# endif
5423
5424 // Check additional arguments to the `:gui` command.
5425 if (arg != NULL)
5426 {
5427 warg = enc_to_utf16(arg, NULL);
5428 if (warg == NULL)
5429 goto error;
5430 tofree2 = warg;
5431 }
5432 else
5433 warg = L"";
5434
5435 // Set up the new command line.
5436 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005437 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005438 if (newcmd == NULL)
5439 goto error;
5440 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5441
5442 // Spawn a new GUI process.
5443 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5444 NULL, NULL, &si, &pi))
5445 goto error;
5446 CloseHandle(pi.hProcess);
5447 CloseHandle(pi.hThread);
5448 mch_exit(0);
5449
5450error:
5451# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5452 if (session)
5453 mch_remove(session);
5454 vim_free(session);
5455 vim_free(tofree1);
5456# endif
5457 vim_free(newcmd);
5458 vim_free(tofree2);
5459 return gvim_error();
5460}
5461#endif
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463/*
5464 * Parse the GUI related command-line arguments. Any arguments used are
5465 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005466 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 */
5468 void
5469gui_mch_prepare(int *argc, char **argv)
5470{
5471 int silent = FALSE;
5472 int idx;
5473
Bram Moolenaar734a8672019-12-02 22:49:38 +01005474 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5476 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005477 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5479 && (argv[2][0] == '-' || argv[2][0] == '/'))
5480 {
5481 silent = TRUE;
5482 idx = 2;
5483 }
5484 else
5485 idx = 1;
5486
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (STRICMP(argv[idx] + 1, "register") == 0)
5489 {
5490#ifdef FEAT_OLE
5491 RegisterMe(silent);
5492 mch_exit(0);
5493#else
5494 if (!silent)
5495 ole_error("register");
5496 mch_exit(2);
5497#endif
5498 }
5499
Bram Moolenaar734a8672019-12-02 22:49:38 +01005500 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5502 {
5503#ifdef FEAT_OLE
5504 UnregisterMe(!silent);
5505 mch_exit(0);
5506#else
5507 if (!silent)
5508 ole_error("unregister");
5509 mch_exit(2);
5510#endif
5511 }
5512
Bram Moolenaar734a8672019-12-02 22:49:38 +01005513 // Ignore an -embedding argument. It is only relevant if the
5514 // application wants to treat the case when it is started manually
5515 // differently from the case where it is started via automation (and
5516 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5518 {
5519#ifdef FEAT_OLE
5520 *argc = 1;
5521#else
5522 ole_error("embedding");
5523 mch_exit(2);
5524#endif
5525 }
5526 }
5527
5528#ifdef FEAT_OLE
5529 {
5530 int bDoRestart = FALSE;
5531
5532 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005533 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (bDoRestart)
5535 mch_exit(0);
5536 }
5537#endif
5538
5539#ifdef FEAT_NETBEANS_INTG
5540 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005541 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 int arg;
5543
5544 for (arg = 1; arg < *argc; arg++)
5545 if (strncmp("-nb", argv[arg], 3) == 0)
5546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 netbeansArg = argv[arg];
5548 mch_memmove(&argv[arg], &argv[arg + 1],
5549 (--*argc - arg) * sizeof(char *));
5550 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005551 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 }
5554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555}
5556
K.Takatac81e9bf2022-01-16 14:15:49 +00005557 static void
5558load_dpi_func(void)
5559{
5560 HMODULE hUser32;
5561
5562 hUser32 = GetModuleHandle("user32.dll");
5563 if (hUser32 == NULL)
5564 goto fail;
5565
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005566 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5567 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5568 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005569 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005570 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5571 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005572
5573 if (pSetThreadDpiAwarenessContext != NULL)
5574 {
5575 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5576 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5577 if (oldctx != NULL)
5578 {
5579 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5580 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5581#ifdef DEBUG
5582 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5583 {
5584 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5585 }
5586#endif
5587 return;
5588 }
5589 }
5590
5591fail:
5592 // Disable PerMonitorV2 APIs.
Ken Takatad8cb1dd2024-01-12 18:09:43 +01005593 pGetDpiForSystem = vimGetDpiForSystem;
K.Takatac81e9bf2022-01-16 14:15:49 +00005594 pGetDpiForWindow = NULL;
5595 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5596 pSetThreadDpiAwarenessContext = NULL;
5597 pGetAwarenessFromDpiAwarenessContext = NULL;
5598}
5599
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600/*
5601 * Initialise the GUI. Create all the windows, set up all the call-backs
5602 * etc.
5603 */
5604 int
5605gui_mch_init(void)
5606{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005608 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
Bram Moolenaar734a8672019-12-02 22:49:38 +01005611 // Return here if the window was already opened (happens when
5612 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005614 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 /*
5617 * Load the tearoff bitmap
5618 */
5619#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005620 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#endif
5622
K.Takatac81e9bf2022-01-16 14:15:49 +00005623 load_dpi_func();
5624
5625 s_dpi = pGetDpiForSystem();
5626 update_scrollbar_size();
5627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005629 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#endif
5631 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005632#ifdef FEAT_TOOLBAR
5633 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5637
Bram Moolenaar734a8672019-12-02 22:49:38 +01005638 // First try using the wide version, so that we can use any title.
5639 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005640 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005642 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 wndclassw.lpfnWndProc = _WndProc;
5644 wndclassw.cbClsExtra = 0;
5645 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005646 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5648 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5649 wndclassw.hbrBackground = s_brush;
5650 wndclassw.lpszMenuName = NULL;
5651 wndclassw.lpszClassName = szVimWndClassW;
5652
K.Takata4ac893f2022-01-20 12:44:28 +00005653 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005654 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 if (vim_parent_hwnd != NULL)
5658 {
5659#ifdef HAVE_TRY_EXCEPT
5660 __try
5661 {
5662#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005663 // Open inside the specified parent window.
5664 // TODO: last argument should point to a CLIENTCREATESTRUCT
5665 // structure.
5666 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005668 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005669 WS_OVERLAPPEDWINDOW | WS_CHILD
5670 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5672 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005673 100, // Any value will do
5674 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005676 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677#ifdef HAVE_TRY_EXCEPT
5678 }
5679 __except(EXCEPTION_EXECUTE_HANDLER)
5680 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005681 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683#endif
5684 if (s_hwnd == NULL)
5685 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005686 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 mch_exit(2);
5688 }
5689 }
5690 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005691 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005692 // If the provided windowid is not valid reset it to zero, so that it
5693 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005694 if (IsWindow((HWND)win_socket_id) <= 0)
5695 win_socket_id = 0;
5696
Bram Moolenaar734a8672019-12-02 22:49:38 +01005697 // Create a window. If win_socket_id is not zero without border and
5698 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005699 s_hwnd = CreateWindowW(
5700 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005701 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5702 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005703 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5704 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005705 100, // Any value will do
5706 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005707 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005708 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005709 if (s_hwnd != NULL && win_socket_id != 0)
5710 {
5711 SetParent(s_hwnd, (HWND)win_socket_id);
5712 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5713 }
5714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 if (s_hwnd == NULL)
5717 return FAIL;
5718
K.Takatac81e9bf2022-01-16 14:15:49 +00005719 if (pGetDpiForWindow != NULL)
5720 {
5721 s_dpi = pGetDpiForWindow(s_hwnd);
5722 update_scrollbar_size();
5723 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5724 }
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5727 dyn_imm_load();
5728#endif
5729
Bram Moolenaar734a8672019-12-02 22:49:38 +01005730 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005731 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005732 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005733 wndclassw.style = CS_OWNDC;
5734 wndclassw.lpfnWndProc = _TextAreaWndProc;
5735 wndclassw.cbClsExtra = 0;
5736 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005737 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005738 wndclassw.hIcon = NULL;
5739 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5740 wndclassw.hbrBackground = NULL;
5741 wndclassw.lpszMenuName = NULL;
5742 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005743
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005744 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 return FAIL;
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005748 s_textArea = CreateWindowExW(
5749 0,
5750 szTextAreaClassW, L"Vim text area",
5751 WS_CHILD | WS_VISIBLE, 0, 0,
5752 100, // Any value will do for now
5753 100, // Any value will do for now
5754 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005755 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005756
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 if (s_textArea == NULL)
5758 return FAIL;
5759
Bram Moolenaar20321902016-02-17 12:30:17 +01005760#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005761 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005762 {
5763 HANDLE hIcon = NULL;
5764
5765 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005766 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005767 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005768#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770#ifdef FEAT_MENU
5771 s_menuBar = CreateMenu();
5772#endif
5773 s_hdc = GetDC(s_textArea);
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
Bram Moolenaar734a8672019-12-02 22:49:38 +01005777 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005778 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaar734a8672019-12-02 22:49:38 +01005780 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 gui_mch_def_colors();
5782
Bram Moolenaar734a8672019-12-02 22:49:38 +01005783 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5784 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 set_normal_colors();
5786
5787 /*
5788 * Check that none of the colors are the same as the background color.
5789 * Then store the current values as the defaults.
5790 */
5791 gui_check_colors();
5792 gui.def_norm_pixel = gui.norm_pixel;
5793 gui.def_back_pixel = gui.back_pixel;
5794
Bram Moolenaar734a8672019-12-02 22:49:38 +01005795 // Get the colors for the highlight groups (gui_check_colors() might have
5796 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 highlight_gui_started();
5798
5799 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005800 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005802 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
5804 /*
5805 * Set up for Intellimouse processing
5806 */
5807 init_mouse_wheel();
5808
5809 /*
5810 * compute a couple of metrics used for the dialogs
5811 */
5812 get_dialog_font_metrics();
5813#ifdef FEAT_TOOLBAR
5814 /*
5815 * Create the toolbar
5816 */
5817 initialise_toolbar();
5818#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005819#ifdef FEAT_GUI_TABLINE
5820 /*
5821 * Create the tabline
5822 */
5823 initialise_tabline();
5824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825#ifdef MSWIN_FIND_REPLACE
5826 /*
5827 * Initialise the dialog box stuff
5828 */
5829 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5830
Bram Moolenaar734a8672019-12-02 22:49:38 +01005831 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005833 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005835 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5837 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5838 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005841#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005842 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005843 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005844#endif
5845
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005846#ifdef FEAT_RENDER_OPTIONS
5847 if (p_rop)
5848 (void)gui_mch_set_rendering_options(p_rop);
5849#endif
5850
Bram Moolenaar748bf032005-02-02 23:04:36 +00005851theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005852 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005853 display_errors();
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 return OK;
5856}
5857
5858/*
5859 * Get the size of the screen, taking position on multiple monitors into
5860 * account (if supported).
5861 */
5862 static void
5863get_work_area(RECT *spi_rect)
5864{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005865 HMONITOR mon;
5866 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaar734a8672019-12-02 22:49:38 +01005868 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005869 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005870 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005872 moninfo.cbSize = sizeof(MONITORINFO);
5873 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005875 *spi_rect = moninfo.rcWork;
5876 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
5878 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005879 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5881}
5882
5883/*
5884 * Set the size of the window to the given width and height in pixels.
5885 */
5886 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005887gui_mch_set_shellsize(
5888 int width,
5889 int height,
5890 int min_width UNUSED,
5891 int min_height UNUSED,
5892 int base_width UNUSED,
5893 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005894 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005897 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar734a8672019-12-02 22:49:38 +01005900 // Try to keep window completely on screen.
5901 // Get position of the screen work area. This is the part that is not
5902 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 get_work_area(&workarea_rect);
5904
Bram Moolenaar734a8672019-12-02 22:49:38 +01005905 // Resizing a maximized window looks very strange, unzoom it first.
5906 // But don't do it when still starting up, it may have been requested in
5907 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005908 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005910
Ken Takata7086b3e2023-10-02 21:26:03 +02005911 if (s_in_dpichanged)
5912 // Use the suggested position when in WM_DPICHANGED.
5913 window_rect = s_suggested_rect;
5914 else
5915 // Use current position.
5916 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaar734a8672019-12-02 22:49:38 +01005918 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005919 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5920 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5921 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5922 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5923 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5924 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar734a8672019-12-02 22:49:38 +01005926 // The following should take care of keeping Vim on the same monitor, no
5927 // matter if the secondary monitor is left or right of the primary
5928 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005929 window_rect.right = window_rect.left + win_width;
5930 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005931
Bram Moolenaar734a8672019-12-02 22:49:38 +01005932 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005933 // Don't adjust the position when in WM_DPICHANGED.
5934 if (!s_in_dpichanged)
5935 {
5936 if ((direction & RESIZE_HOR)
5937 && window_rect.right > workarea_rect.right)
5938 OffsetRect(&window_rect,
5939 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
Ken Takata7086b3e2023-10-02 21:26:03 +02005941 if ((direction & RESIZE_HOR)
5942 && window_rect.left < workarea_rect.left)
5943 OffsetRect(&window_rect,
5944 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945
Ken Takata7086b3e2023-10-02 21:26:03 +02005946 if ((direction & RESIZE_VERT)
5947 && window_rect.bottom > workarea_rect.bottom)
5948 OffsetRect(&window_rect,
5949 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950
Ken Takata7086b3e2023-10-02 21:26:03 +02005951 if ((direction & RESIZE_VERT)
5952 && window_rect.top < workarea_rect.top)
5953 OffsetRect(&window_rect,
5954 0, workarea_rect.top - window_rect.top);
5955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005957 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5958 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
Ken Takata7086b3e2023-10-02 21:26:03 +02005960 //TRACE("New pos: %d,%d New size: %d,%d",
5961 // window_rect.left, window_rect.top, win_width, win_height);
5962
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 SetActiveWindow(s_hwnd);
5964 SetFocus(s_hwnd);
5965
Bram Moolenaar734a8672019-12-02 22:49:38 +01005966 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
5970
5971 void
5972gui_mch_set_scrollbar_thumb(
5973 scrollbar_T *sb,
5974 long val,
5975 long size,
5976 long max)
5977{
5978 SCROLLINFO info;
5979
5980 sb->scroll_shift = 0;
5981 while (max > 32767)
5982 {
5983 max = (max + 1) >> 1;
5984 val >>= 1;
5985 size >>= 1;
5986 ++sb->scroll_shift;
5987 }
5988
5989 if (sb->scroll_shift > 0)
5990 ++size;
5991
5992 info.cbSize = sizeof(info);
5993 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5994 info.nPos = val;
5995 info.nMin = 0;
5996 info.nMax = max;
5997 info.nPage = size;
5998 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5999}
6000
6001
6002/*
6003 * Set the current text font.
6004 */
6005 void
6006gui_mch_set_font(GuiFont font)
6007{
6008 gui.currFont = font;
6009}
6010
6011
6012/*
6013 * Set the current text foreground color.
6014 */
6015 void
6016gui_mch_set_fg_color(guicolor_T color)
6017{
6018 gui.currFgColor = color;
6019}
6020
6021/*
6022 * Set the current text background color.
6023 */
6024 void
6025gui_mch_set_bg_color(guicolor_T color)
6026{
6027 gui.currBgColor = color;
6028}
6029
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006030/*
6031 * Set the current text special color.
6032 */
6033 void
6034gui_mch_set_sp_color(guicolor_T color)
6035{
6036 gui.currSpColor = color;
6037}
6038
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006039#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040/*
6041 * Multi-byte handling, originally by Sung-Hoon Baek.
6042 * First static functions (no prototypes generated).
6043 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006044# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01006045# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006046# endif
6047# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048
6049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 * handle WM_IME_NOTIFY message
6051 */
6052 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006053_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 LRESULT lResult = 0;
6056 HIMC hImc;
6057
6058 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
6059 return lResult;
6060 switch (dwCommand)
6061 {
6062 case IMN_SETOPENSTATUS:
6063 if (pImmGetOpenStatus(hImc))
6064 {
K.Takatac81e9bf2022-01-16 14:15:49 +00006065 LOGFONTW lf = norm_logfont;
6066 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6067 // Work around when PerMonitorV2 is not enabled in the process level.
6068 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
6069 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 im_set_position(gui.row, gui.col);
6071
Bram Moolenaar734a8672019-12-02 22:49:38 +01006072 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01006073 State &= ~MODE_LANGMAP;
6074 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006076# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006077 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
6079 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006080 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 int old_row = gui.row;
6082 int old_col = gui.col;
6083
6084 // This must be called here before
6085 // status_redraw_curbuf(), otherwise the mode
6086 // message may appear in the wrong position.
6087 showmode();
6088 status_redraw_curbuf();
6089 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006090 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 gui.row = old_row;
6092 gui.col = old_col;
6093 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 }
6096 }
6097 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01006098 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 lResult = 0;
6100 break;
6101 }
6102 pImmReleaseContext(hWnd, hImc);
6103 return lResult;
6104}
6105
6106 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01006107_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 char_u *ret;
6110 int len;
6111
Bram Moolenaar734a8672019-12-02 22:49:38 +01006112 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 return 0;
6114
6115 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
6116 if (ret != NULL)
6117 {
6118 add_to_input_buf_csi(ret, len);
6119 vim_free(ret);
6120 return 1;
6121 }
6122 return 0;
6123}
6124
6125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 * void GetResultStr()
6127 *
6128 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
6129 * get complete composition string
6130 */
6131 static char_u *
6132GetResultStr(HWND hwnd, int GCS, int *lenp)
6133{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006134 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00006135 LONG ret;
6136 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 char_u *convbuf = NULL;
6138
6139 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
6140 return NULL;
6141
K.Takatab0b2b732022-01-19 12:59:21 +00006142 // Get the length of the composition string.
6143 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
6144 if (ret <= 0)
6145 return NULL;
6146
6147 // Allocate the requested buffer plus space for the NUL character.
6148 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (buf == NULL)
6150 return NULL;
6151
K.Takatab0b2b732022-01-19 12:59:21 +00006152 // Reads in the composition string.
6153 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
6154 *lenp = ret / sizeof(WCHAR);
6155
Bram Moolenaar36f692d2008-11-20 16:10:17 +00006156 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 pImmReleaseContext(hwnd, hIMC);
6158 vim_free(buf);
6159 return convbuf;
6160}
6161#endif
6162
Bram Moolenaar734a8672019-12-02 22:49:38 +01006163// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01006164#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166/*
6167 * set font to IM.
6168 */
6169 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006170im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 HIMC hImc;
6173
6174 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6175 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006176 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 pImmReleaseContext(s_hwnd, hImc);
6178 }
6179}
6180
6181/*
6182 * Notify cursor position to IM.
6183 */
6184 void
6185im_set_position(int row, int col)
6186{
6187 HIMC hImc;
6188
6189 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6190 {
6191 COMPOSITIONFORM cfs;
6192
6193 cfs.dwStyle = CFS_POINT;
6194 cfs.ptCurrentPos.x = FILL_X(col);
6195 cfs.ptCurrentPos.y = FILL_Y(row);
6196 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00006197 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
6198 {
6199 // Work around when PerMonitorV2 is not enabled in the process level.
6200 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
6201 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 pImmSetCompositionWindow(hImc, &cfs);
6204
6205 pImmReleaseContext(s_hwnd, hImc);
6206 }
6207}
6208
6209/*
6210 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
6211 */
6212 void
6213im_set_active(int active)
6214{
6215 HIMC hImc;
6216 static HIMC hImcOld = (HIMC)0;
6217
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006218# ifdef VIMDLL
6219 if (!gui.in_use && !gui.starting)
6220 {
6221 mbyte_im_set_active(active);
6222 return;
6223 }
6224# endif
6225
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006226 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
6227 return;
6228
6229 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006231 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006233 hImcOld = pImmGetContext(s_hwnd);
6234 if (hImcOld)
6235 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006237 active = FALSE;
6238 }
6239 else if (hImcOld != (HIMC)0)
6240 {
6241 pImmAssociateContext(s_hwnd, hImcOld);
6242 hImcOld = (HIMC)0;
6243 }
6244
6245 hImc = pImmGetContext(s_hwnd);
6246 if (!hImc)
6247 return;
6248
6249 /*
6250 * for Korean ime
6251 */
6252 HKL hKL = GetKeyboardLayout(0);
6253
6254 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
6255 {
6256 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
6257 static BOOL bSaved = FALSE;
6258
6259 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006261 // if we have a saved conversion status, restore it
6262 if (bSaved)
6263 pImmSetConversionStatus(hImc, dwConversionSaved,
6264 dwSentenceSaved);
6265 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006269 // save conversion status and disable korean
6270 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
6271 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00006272 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006273 bSaved = TRUE;
6274 pImmSetConversionStatus(hImc,
6275 dwConversionSaved & ~(IME_CMODE_NATIVE
6276 | IME_CMODE_FULLSHAPE),
6277 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006281
6282 pImmSetOpenStatus(hImc, active);
6283 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284}
6285
6286/*
6287 * Get IM status. When IM is on, return not 0. Else return 0.
6288 */
6289 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01006290im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 int status = 0;
6293 HIMC hImc;
6294
Bram Moolenaar310c32e2019-11-29 23:15:25 +01006295# ifdef VIMDLL
6296 if (!gui.in_use && !gui.starting)
6297 return mbyte_im_get_status();
6298# endif
6299
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
6301 {
6302 status = pImmGetOpenStatus(hImc) ? 1 : 0;
6303 pImmReleaseContext(s_hwnd, hImc);
6304 }
6305 return status;
6306}
6307
Bram Moolenaar734a8672019-12-02 22:49:38 +01006308#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006311/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00006312 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006313 */
6314 static void
6315latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
6316{
6317 int c;
6318
Bram Moolenaarca003e12006-03-17 23:19:38 +00006319 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006320 {
6321 c = *text++;
6322 switch (c)
6323 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324 case 0xa4: c = 0x20ac; break; // euro
6325 case 0xa6: c = 0x0160; break; // S hat
6326 case 0xa8: c = 0x0161; break; // S -hat
6327 case 0xb4: c = 0x017d; break; // Z hat
6328 case 0xb8: c = 0x017e; break; // Z -hat
6329 case 0xbc: c = 0x0152; break; // OE
6330 case 0xbd: c = 0x0153; break; // oe
6331 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006332 }
6333 *unicodebuf++ = c;
6334 }
6335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337#ifdef FEAT_RIGHTLEFT
6338/*
6339 * What is this for? In the case where you are using Win98 or Win2K or later,
6340 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6341 * reverses the string sent to the TextOut... family. This sucks, because we
6342 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6343 * way to tell Windblows not to do this!
6344 *
6345 * The short of it is that this 'RevOut' only gets called if you are running
6346 * one of the new, "improved" MS OSes, and only if you are running in
6347 * 'rightleft' mode. It makes display take *slightly* longer, but not
6348 * noticeably so.
6349 */
6350 static void
K.Takata135e1522022-01-29 15:27:58 +00006351RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 int col,
6353 int row,
6354 UINT foptions,
6355 CONST RECT *pcliprect,
6356 LPCTSTR text,
6357 UINT len,
6358 CONST INT *padding)
6359{
6360 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006362 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006363 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006364 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365}
6366#endif
6367
Bram Moolenaar92467d32017-12-05 13:22:16 +01006368 static void
6369draw_line(
6370 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006371 int y1,
6372 int x2,
6373 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006374 COLORREF color)
6375{
6376#if defined(FEAT_DIRECTX)
6377 if (IS_ENABLE_DIRECTX())
6378 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6379 else
6380#endif
6381 {
6382 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6383 HPEN old_pen = SelectObject(s_hdc, hpen);
6384 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006385 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006386 LineTo(s_hdc, x2, y2);
6387 DeleteObject(SelectObject(s_hdc, old_pen));
6388 }
6389}
6390
6391 static void
6392set_pixel(
6393 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006394 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006395 COLORREF color)
6396{
6397#if defined(FEAT_DIRECTX)
6398 if (IS_ENABLE_DIRECTX())
6399 DWriteContext_SetPixel(s_dwc, x, y, color);
6400 else
6401#endif
6402 SetPixel(s_hdc, x, y, color);
6403}
6404
6405 static void
6406fill_rect(
6407 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006408 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006409 COLORREF color)
6410{
6411#if defined(FEAT_DIRECTX)
6412 if (IS_ENABLE_DIRECTX())
6413 DWriteContext_FillRect(s_dwc, rcp, color);
6414 else
6415#endif
6416 {
6417 HBRUSH hbr2;
6418
6419 if (hbr == NULL)
6420 hbr2 = CreateSolidBrush(color);
6421 else
6422 hbr2 = hbr;
6423 FillRect(s_hdc, rcp, hbr2);
6424 if (hbr == NULL)
6425 DeleteBrush(hbr2);
6426 }
6427}
6428
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 void
6430gui_mch_draw_string(
6431 int row,
6432 int col,
6433 char_u *text,
6434 int len,
6435 int flags)
6436{
6437 static int *padding = NULL;
6438 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 const RECT *pcliprect = NULL;
6440 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 static WCHAR *unicodebuf = NULL;
6442 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006443 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 int y;
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 /*
6448 * Italic and bold text seems to have an extra row of pixels at the bottom
6449 * (below where the bottom of the character should be). If we draw the
6450 * characters with a solid background, the top row of pixels in the
6451 * character below will be overwritten. We can fix this by filling in the
6452 * background ourselves, to the correct character proportions, and then
6453 * writing the character in transparent mode. Still have a problem when
6454 * the character is "_", which gets written on to the character below.
6455 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6456 * pixel in their slots, which fixes the problem with the bottom row of
6457 * pixels. We still need this code because otherwise the top row of pixels
6458 * becomes a problem. - webb.
6459 */
6460 static HBRUSH hbr_cache[2] = {NULL, NULL};
6461 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6462 static int brush_lru = 0;
6463 HBRUSH hbr;
6464 RECT rc;
6465
6466 if (!(flags & DRAW_TRANSP))
6467 {
6468 /*
6469 * Clear background first.
6470 * Note: FillRect() excludes right and bottom of rectangle.
6471 */
6472 rc.left = FILL_X(col);
6473 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 if (has_mbyte)
6475 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006476 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006477 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 rc.right = FILL_X(col + len);
6481 rc.bottom = FILL_Y(row + 1);
6482
Bram Moolenaar734a8672019-12-02 22:49:38 +01006483 // Cache the created brush, that saves a lot of time. We need two:
6484 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if (gui.currBgColor == brush_color[0])
6486 {
6487 hbr = hbr_cache[0];
6488 brush_lru = 1;
6489 }
6490 else if (gui.currBgColor == brush_color[1])
6491 {
6492 hbr = hbr_cache[1];
6493 brush_lru = 0;
6494 }
6495 else
6496 {
6497 if (hbr_cache[brush_lru] != NULL)
6498 DeleteBrush(hbr_cache[brush_lru]);
6499 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6500 brush_color[brush_lru] = gui.currBgColor;
6501 hbr = hbr_cache[brush_lru];
6502 brush_lru = !brush_lru;
6503 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006504
Bram Moolenaar92467d32017-12-05 13:22:16 +01006505 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 SetBkMode(s_hdc, TRANSPARENT);
6508
6509 /*
6510 * When drawing block cursor, prevent inverted character spilling
6511 * over character cell (can happen with bold/italic)
6512 */
6513 if (flags & DRAW_CURSOR)
6514 {
6515 pcliprect = &rc;
6516 foptions = ETO_CLIPPED;
6517 }
6518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 SetTextColor(s_hdc, gui.currFgColor);
6520 SelectFont(s_hdc, gui.currFont);
6521
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006522#ifdef FEAT_DIRECTX
6523 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006524 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006525#endif
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6528 {
K.Takata135e1522022-01-29 15:27:58 +00006529 int i;
6530
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 vim_free(padding);
6532 pad_size = Columns;
6533
Bram Moolenaar734a8672019-12-02 22:49:38 +01006534 // Don't give an out-of-memory message here, it would call us
6535 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006536 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 if (padding != NULL)
6538 for (i = 0; i < pad_size; i++)
6539 padding[i] = gui.char_width;
6540 }
6541
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 /*
6543 * We have to provide the padding argument because italic and bold versions
6544 * of fixed-width fonts are often one pixel or so wider than their normal
6545 * versions.
6546 * No check for DRAW_BOLD, Windows will have done it already.
6547 */
6548
Bram Moolenaar734a8672019-12-02 22:49:38 +01006549 // Check if there are any UTF-8 characters. If not, use normal text
6550 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (enc_utf8)
6552 for (n = 0; n < len; ++n)
6553 if (text[n] >= 0x80)
6554 break;
6555
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006556#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006557 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6558 // required that unicode drawing routine, currently. So this forces it
6559 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006560 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006561 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006562#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006563
Bram Moolenaar734a8672019-12-02 22:49:38 +01006564 // Check if the Unicode buffer exists and is big enough. Create it
6565 // with the same length as the multi-byte string, the number of wide
6566 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006567 if ((enc_utf8
6568 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6569 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 && (unicodebuf == NULL || len > unibuflen))
6571 {
6572 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006573 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006576 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 unibuflen = len;
6579 }
6580
6581 if (enc_utf8 && n < len && unicodebuf != NULL)
6582 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006583 // Output UTF-8 characters. Composing characters should be
6584 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006585 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006586 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006587 int cells; // cell width of string up to composing char
6588 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006589 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006591 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006593 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006595 c = utf_ptr2char(text + i);
6596 if (c >= 0x10000)
6597 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006598 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006599 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6600 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006601 }
6602 else
6603 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006604 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006605 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006606
6607 if (utf_iscomposing(c))
6608 cw = 0;
6609 else
6610 {
6611 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006612 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006613 cw = 1;
6614 }
6615
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 if (unicodepdy != NULL)
6617 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006618 // Use unicodepdy to make characters fit as we expect, even
6619 // when the font uses different widths (e.g., bold character
6620 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006621 if (c >= 0x10000)
6622 {
6623 unicodepdy[wlen - 2] = cw * gui.char_width;
6624 unicodepdy[wlen - 1] = 0;
6625 }
6626 else
6627 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 }
6629 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006630 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006632#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006633 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006634 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006635 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006636 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006637 TEXT_X(col), TEXT_Y(row),
6638 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006639 gui.char_width, gui.currFgColor,
6640 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006641 }
6642 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006643#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006644 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6645 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006646 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006648 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006650 // If we want to display codepage data, and the current CP is not the
6651 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (unicodebuf != NULL)
6653 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006654 if (enc_latin9)
6655 latin9_to_ucs(text, len, unicodebuf);
6656 else
6657 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 MB_PRECOMPOSED,
6659 (char *)text, len,
6660 (LPWSTR)unicodebuf, unibuflen);
6661 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006662 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006663 // Use unicodepdy to make characters fit as we expect, even
6664 // when the font uses different widths (e.g., bold character
6665 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006666 if (unicodepdy != NULL)
6667 {
6668 int i;
6669 int cw;
6670
6671 for (i = 0; i < len; ++i)
6672 {
6673 cw = utf_char2cells(unicodebuf[i]);
6674 if (cw > 2)
6675 cw = 1;
6676 unicodepdy[i] = cw * gui.char_width;
6677 }
6678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006680 foptions, pcliprect, unicodebuf, len, unicodepdy);
6681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683 }
6684 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 {
6686#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006687 // Windows will mess up RL text, so we have to draw it character by
6688 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006689 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6691 foptions, pcliprect, (char *)text, len, padding);
6692 else
6693#endif
6694 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6695 foptions, pcliprect, (char *)text, len, padding);
6696 }
6697
Bram Moolenaar734a8672019-12-02 22:49:38 +01006698 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (flags & DRAW_UNDERL)
6700 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006701 // When p_linespace is 0, overwrite the bottom row of pixels.
6702 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 if (p_linespace > 1)
6705 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006706 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006708
Bram Moolenaar734a8672019-12-02 22:49:38 +01006709 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006710 if (flags & DRAW_STRIKE)
6711 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006712 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006713 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006714 }
6715
Bram Moolenaar734a8672019-12-02 22:49:38 +01006716 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006717 if (flags & DRAW_UNDERC)
6718 {
6719 int x;
6720 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006721 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006722
6723 y = FILL_Y(row + 1) - 1;
6724 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6725 {
6726 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006727 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006728 }
6729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730}
6731
6732
6733/*
6734 * Output routines.
6735 */
6736
Bram Moolenaar734a8672019-12-02 22:49:38 +01006737/*
6738 * Flush any output to the screen
6739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 void
6741gui_mch_flush(void)
6742{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006743#if defined(FEAT_DIRECTX)
6744 if (IS_ENABLE_DIRECTX())
6745 DWriteContext_Flush(s_dwc);
6746#endif
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 GdiFlush();
6749}
6750
6751 static void
6752clear_rect(RECT *rcp)
6753{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006754 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755}
6756
6757
Bram Moolenaarc716c302006-01-21 22:12:51 +00006758 void
6759gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6760{
6761 RECT workarea_rect;
6762
6763 get_work_area(&workarea_rect);
6764
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006765 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006766 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6767 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006768
Bram Moolenaar734a8672019-12-02 22:49:38 +01006769 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6770 // the menubar for MSwin, we subtract it from the screen height, so that
6771 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006772 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006773 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6774 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6775 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6776 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006777}
6778
6779
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#if defined(FEAT_MENU) || defined(PROTO)
6781/*
6782 * Add a sub menu to the menu bar.
6783 */
6784 void
6785gui_mch_add_menu(
6786 vimmenu_T *menu,
6787 int pos)
6788{
6789 vimmenu_T *parent = menu->parent;
6790
6791 menu->submenu_id = CreatePopupMenu();
6792 menu->id = s_menu_id++;
6793
6794 if (menu_is_menubar(menu->name))
6795 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006796 WCHAR *wn;
6797 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006799 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006800 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006801 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006803 infow.cbSize = sizeof(infow);
6804 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6805 | MIIM_SUBMENU;
6806 infow.dwItemData = (long_u)menu;
6807 infow.wID = menu->id;
6808 infow.fType = MFT_STRING;
6809 infow.dwTypeData = wn;
6810 infow.cch = (UINT)wcslen(wn);
6811 infow.hSubMenu = menu->submenu_id;
6812 InsertMenuItemW((parent == NULL)
6813 ? s_menuBar : parent->submenu_id,
6814 (UINT)pos, TRUE, &infow);
6815 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 }
6817
Bram Moolenaar734a8672019-12-02 22:49:38 +01006818 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (parent == NULL)
6820 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006821# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 else if (IsWindow(parent->tearoff_handle))
6823 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825}
6826
6827 void
6828gui_mch_show_popupmenu(vimmenu_T *menu)
6829{
6830 POINT mp;
6831
K.Takata45f9cfb2022-01-21 11:11:00 +00006832 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6834}
6835
6836 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006837gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 vimmenu_T *menu = gui_find_menu(path_name);
6840
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006841 if (menu == NULL)
6842 return;
6843
6844 POINT p;
6845
6846 // Find the position of the current cursor
6847 GetDCOrgEx(s_hdc, &p);
6848 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006850 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006852 gui_mch_getmouse(&mx, &my);
6853 p.x += mx;
6854 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006856 else if (curwin != NULL)
6857 {
6858 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6859 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6860 }
6861 msg_scroll = FALSE;
6862 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863}
6864
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006865# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6868 * create it as a pseudo-"tearoff menu".
6869 */
6870 void
6871gui_make_tearoff(char_u *path_name)
6872{
6873 vimmenu_T *menu = gui_find_menu(path_name);
6874
Bram Moolenaar734a8672019-12-02 22:49:38 +01006875 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (menu != NULL)
6877 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6878}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
6881/*
6882 * Add a menu item to a menu
6883 */
6884 void
6885gui_mch_add_menu_item(
6886 vimmenu_T *menu,
6887 int idx)
6888{
6889 vimmenu_T *parent = menu->parent;
6890
6891 menu->id = s_menu_id++;
6892 menu->submenu_id = NULL;
6893
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006894# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6896 {
6897 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6898 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6899 }
6900 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006901# endif
6902# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 if (menu_is_toolbar(parent->name))
6904 {
6905 TBBUTTON newtb;
6906
Bram Moolenaara80faa82020-04-12 19:37:17 +02006907 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 if (menu_is_separator(menu->name))
6909 {
6910 newtb.iBitmap = 0;
6911 newtb.fsStyle = TBSTYLE_SEP;
6912 }
6913 else
6914 {
6915 newtb.iBitmap = get_toolbar_bitmap(menu);
6916 newtb.fsStyle = TBSTYLE_BUTTON;
6917 }
6918 newtb.idCommand = menu->id;
6919 newtb.fsState = TBSTATE_ENABLED;
6920 newtb.iString = 0;
6921 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6922 (LPARAM)&newtb);
6923 menu->submenu_id = (HMENU)-1;
6924 }
6925 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006928 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006930 wn = enc_to_utf16(menu->name, NULL);
6931 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006933 InsertMenuW(parent->submenu_id, (UINT)idx,
6934 (menu_is_separator(menu->name)
6935 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6936 (UINT)menu->id, wn);
6937 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006939# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 if (IsWindow(parent->tearoff_handle))
6941 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944}
6945
6946/*
6947 * Destroy the machine specific menu widget.
6948 */
6949 void
6950gui_mch_destroy_menu(vimmenu_T *menu)
6951{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006952# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 /*
6954 * is this a toolbar button?
6955 */
6956 if (menu->submenu_id == (HMENU)-1)
6957 {
6958 int iButton;
6959
6960 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6961 (WPARAM)menu->id, 0);
6962 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6963 }
6964 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
6967 if (menu->parent != NULL
6968 && menu_is_popup(menu->parent->dname)
6969 && menu->parent->submenu_id != NULL)
6970 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6971 else
6972 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6973 if (menu->submenu_id != NULL)
6974 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006975# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 if (IsWindow(menu->tearoff_handle))
6977 DestroyWindow(menu->tearoff_handle);
6978 if (menu->parent != NULL
6979 && menu->parent->children != NULL
6980 && IsWindow(menu->parent->tearoff_handle))
6981 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006982 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 menu->modes = 0;
6984 rebuild_tearoff(menu->parent);
6985 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006986# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988}
6989
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006990# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 static void
6992rebuild_tearoff(vimmenu_T *menu)
6993{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006994 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 char_u tbuf[128];
6996 RECT trect;
6997 RECT rct;
6998 RECT roct;
6999 int x, y;
7000
7001 HWND thwnd = menu->tearoff_handle;
7002
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007003 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 if (GetWindowRect(thwnd, &trect)
7005 && GetWindowRect(s_hwnd, &rct)
7006 && GetClientRect(s_hwnd, &roct))
7007 {
7008 x = trect.left - rct.left;
7009 y = (trect.top - rct.bottom + roct.bottom);
7010 }
7011 else
7012 {
7013 x = y = 0xffffL;
7014 }
7015 DestroyWindow(thwnd);
7016 if (menu->children != NULL)
7017 {
7018 gui_mch_tearoff(tbuf, menu, x, y);
7019 if (IsWindow(menu->tearoff_handle))
7020 (void) SetWindowPos(menu->tearoff_handle,
7021 NULL,
7022 (int)trect.left,
7023 (int)trect.top,
7024 0, 0,
7025 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
7026 }
7027}
Bram Moolenaar734a8672019-12-02 22:49:38 +01007028# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029
7030/*
7031 * Make a menu either grey or not grey.
7032 */
7033 void
7034gui_mch_menu_grey(
7035 vimmenu_T *menu,
7036 int grey)
7037{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007038# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 /*
7040 * is this a toolbar button?
7041 */
7042 if (menu->submenu_id == (HMENU)-1)
7043 {
7044 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00007045 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007048# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02007049 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
7050 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007052# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
7054 {
7055 WORD menuID;
7056 HWND menuHandle;
7057
7058 /*
7059 * A tearoff button has changed state.
7060 */
7061 if (menu->children == NULL)
7062 menuID = (WORD)(menu->id);
7063 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007064 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
7066 if (menuHandle)
7067 EnableWindow(menuHandle, !grey);
7068
7069 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007070# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071}
7072
Bram Moolenaar734a8672019-12-02 22:49:38 +01007073#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
7075
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007079#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
7082/*
7083 * stuff for dialogs
7084 */
7085
7086/*
7087 * The callback routine used by all the dialogs. Very simple. First,
7088 * acknowledges the INITDIALOG message so that Windows knows to do standard
7089 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
7090 * pressed, return that button's ID - IDCANCEL (2), which is the button's
7091 * number.
7092 */
7093 static LRESULT CALLBACK
7094dialog_callback(
7095 HWND hwnd,
7096 UINT message,
7097 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01007098 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 if (message == WM_INITDIALOG)
7101 {
7102 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007103 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 (void)SetFocus(hwnd);
7105 if (dialog_default_button > IDCANCEL)
7106 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007107 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007108 // We don't have a default, set focus on another element of the
7109 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00007110 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 return FALSE;
7112 }
7113
7114 if (message == WM_COMMAND)
7115 {
7116 int button = LOWORD(wParam);
7117
Bram Moolenaar734a8672019-12-02 22:49:38 +01007118 // Don't end the dialog if something was selected that was
7119 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 if (button >= DLG_NONBUTTON_CONTROL)
7121 return TRUE;
7122
Bram Moolenaar734a8672019-12-02 22:49:38 +01007123 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007125 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007126 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007127 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007128
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02007129 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
7130 p = utf16_to_enc(wp, NULL);
7131 vim_strncpy(s_textfield, p, IOSIZE);
7132 vim_free(p);
7133 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00007134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /*
7137 * Need to check for IDOK because if the user just hits Return to
7138 * accept the default value, some reason this is what we get.
7139 */
7140 if (button == IDOK)
7141 {
7142 if (dialog_default_button > IDCANCEL)
7143 EndDialog(hwnd, dialog_default_button);
7144 }
7145 else
7146 EndDialog(hwnd, button - IDCANCEL);
7147 return TRUE;
7148 }
7149
7150 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7151 {
7152 EndDialog(hwnd, 0);
7153 return TRUE;
7154 }
7155 return FALSE;
7156}
7157
7158/*
7159 * Create a dialog dynamically from the parameter strings.
7160 * type = type of dialog (question, alert, etc.)
7161 * title = dialog title. may be NULL for default title.
7162 * message = text to display. Dialog sizes to accommodate it.
7163 * buttons = '\n' separated list of button captions, default first.
7164 * dfltbutton = number of default button.
7165 *
7166 * This routine returns 1 if the first button is pressed,
7167 * 2 for the second, etc.
7168 *
7169 * 0 indicates Esc was pressed.
7170 * -1 for unexpected error
7171 *
7172 * If stubbing out this fn, return 1.
7173 */
7174
Bram Moolenaar734a8672019-12-02 22:49:38 +01007175static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 "IDR_VIM",
7178 "IDR_VIM_ERROR",
7179 "IDR_VIM_ALERT",
7180 "IDR_VIM_INFO",
7181 "IDR_VIM_QUESTION"
7182};
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 int
7185gui_mch_dialog(
7186 int type,
7187 char_u *title,
7188 char_u *message,
7189 char_u *buttons,
7190 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007191 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007192 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193{
7194 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00007195 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int numButtons;
7197 int *buttonWidths, *buttonPositions;
7198 int buttonYpos;
7199 int nchar, i;
7200 DWORD lStyle;
7201 int dlgwidth = 0;
7202 int dlgheight;
7203 int editboxheight;
7204 int horizWidth = 0;
7205 int msgheight;
7206 char_u *pstart;
7207 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007208 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 char_u *tbuffer;
7210 RECT rect;
7211 HWND hwnd;
7212 HDC hdc;
7213 HFONT font, oldFont;
7214 TEXTMETRIC fontInfo;
7215 int fontHeight;
7216 int textWidth, minButtonWidth, messageWidth;
7217 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007218 int maxDialogHeight;
7219 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 int vertical;
7221 int dlgPaddingX;
7222 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007223# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007224 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007226# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007227 garray_T ga;
7228 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00007229 int dlg_icon_width;
7230 int dlg_icon_height;
7231 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007233# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01007234 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007235# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007236 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007237# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007238 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007239 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar748bf032005-02-02 23:04:36 +00007242 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00007243 {
7244 load_dpi_func();
7245 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00007246 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00007247 }
7248 else
7249 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 if ((type < 0) || (type > VIM_LAST_TYPE))
7252 type = 0;
7253
Bram Moolenaar734a8672019-12-02 22:49:38 +01007254 // allocate some memory for dialog template
7255 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007256 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007257 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258
7259 if (p == NULL)
7260 return -1;
7261
7262 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007263 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 * vim_strsave() doesn't take a const arg (why not?), so cast away the
7265 * const.
7266 */
7267 tbuffer = vim_strsave(buttons);
7268 if (tbuffer == NULL)
7269 return -1;
7270
Bram Moolenaar734a8672019-12-02 22:49:38 +01007271 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272
Bram Moolenaar734a8672019-12-02 22:49:38 +01007273 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 numButtons = 1;
7275 for (i = 0; tbuffer[i] != '\0'; i++)
7276 {
7277 if (tbuffer[i] == DLG_BUTTON_SEP)
7278 numButtons++;
7279 }
7280 if (dfltbutton >= numButtons)
7281 dfltbutton = -1;
7282
Bram Moolenaar734a8672019-12-02 22:49:38 +01007283 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007284 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 if (buttonWidths == NULL)
7286 return -1;
7287
Bram Moolenaar734a8672019-12-02 22:49:38 +01007288 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02007289 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 if (buttonPositions == NULL)
7291 return -1;
7292
7293 /*
7294 * Calculate how big the dialog must be.
7295 */
7296 hwnd = GetDesktopWindow();
7297 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007298# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7300 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007301 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 use_lfSysmenu = TRUE;
7303 }
7304 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007305# endif
K.Takatad1c58992022-01-23 12:31:57 +00007306 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7307 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7308
7309 oldFont = SelectFont(hdc, font);
7310 dlgPaddingX = DLG_PADDING_X;
7311 dlgPaddingY = DLG_PADDING_Y;
7312
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 GetTextMetrics(hdc, &fontInfo);
7314 fontHeight = fontInfo.tmHeight;
7315
Bram Moolenaar734a8672019-12-02 22:49:38 +01007316 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007317 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
Bram Moolenaar734a8672019-12-02 22:49:38 +01007319 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007320 if (s_hwnd == NULL)
7321 {
7322 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323
Bram Moolenaar734a8672019-12-02 22:49:38 +01007324 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007325 get_work_area(&workarea_rect);
7326 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00007327 if (maxDialogWidth > adjust_by_system_dpi(600))
7328 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007329 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02007330 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007331 }
7332 else
7333 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007334 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007335 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007336 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007337 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7338 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7339 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7340 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007341
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007342 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007343 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7344 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7345 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7346 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7347 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007348 }
7349
Bram Moolenaar734a8672019-12-02 22:49:38 +01007350 // Set dlgwidth to width of message.
7351 // Copy the message into "ga", changing NL to CR-NL and inserting line
7352 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 pstart = message;
7354 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007355 msgheight = 0;
7356 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 do
7358 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007359 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007360
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 // Need to figure out where to break the string. The system does it
7362 // at a word boundary, which would mean we can't compute the number of
7363 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007364 textWidth = 0;
7365 last_white = NULL;
7366 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007367 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007368 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007369 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007370 && textWidth > maxDialogWidth * 3 / 4)
7371 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007372 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007373 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007374 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007375 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007376 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007377 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007378 textWidth = 0;
7379
7380 if (last_white != NULL)
7381 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007382 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007383 if (pend > last_white)
7384 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007385 pend = last_white + 1;
7386 last_white = NULL;
7387 }
7388 ga_append(&ga, '\r');
7389 ga_append(&ga, '\n');
7390 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007391 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007392
7393 while (--l >= 0)
7394 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007395 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007396 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007398
7399 ga_append(&ga, '\r');
7400 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 pstart = pend + 1;
7402 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007403
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007404 if (ga.ga_data != NULL)
7405 message = ga.ga_data;
7406
Bram Moolenaar734a8672019-12-02 22:49:38 +01007407 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007408
K.Takatac81e9bf2022-01-16 14:15:49 +00007409 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7410 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
K.Takatac81e9bf2022-01-16 14:15:49 +00007412 // Add width of icon to dlgwidth, and some space
7413 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7414 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7415
7416 if (msgheight < dlg_icon_height)
7417 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418
7419 /*
7420 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007421 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007423 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (!vertical)
7425 {
7426 // Place buttons horizontally if they fit.
7427 horizWidth = dlgPaddingX;
7428 pstart = tbuffer;
7429 i = 0;
7430 do
7431 {
7432 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7433 if (pend == NULL)
7434 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007435 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 if (textWidth < minButtonWidth)
7437 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007438 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 buttonWidths[i] = textWidth;
7440 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007441 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 pstart = pend + 1;
7443 } while (*pend != NUL);
7444
7445 if (horizWidth > maxDialogWidth)
7446 vertical = TRUE; // Too wide to fit on the screen.
7447 else if (horizWidth > dlgwidth)
7448 dlgwidth = horizWidth;
7449 }
7450
7451 if (vertical)
7452 {
7453 // Stack buttons vertically.
7454 pstart = tbuffer;
7455 do
7456 {
7457 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7458 if (pend == NULL)
7459 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007460 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007461 textWidth += dlgPaddingX; // Padding within button
7462 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (textWidth > dlgwidth)
7464 dlgwidth = textWidth;
7465 pstart = pend + 1;
7466 } while (*pend != NUL);
7467 }
7468
7469 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471
Bram Moolenaar734a8672019-12-02 22:49:38 +01007472 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007473 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 add_long(lStyle);
7476 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007477 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 add_word(0); // NumberOfItems(will change later)
7479 add_word(10); // x
7480 add_word(10); // y
7481 add_word(PixelToDialogX(dlgwidth)); // cx
7482
7483 // Dialog height.
7484 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007485 dlgheight = msgheight + 2 * dlgPaddingY
7486 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 else
7488 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7489
7490 // Dialog needs to be taller if contains an edit box.
7491 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7492 if (textfield != NULL)
7493 dlgheight += editboxheight;
7494
Bram Moolenaar734a8672019-12-02 22:49:38 +01007495 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007496 if (dlgheight > maxDialogHeight)
7497 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007498 msgheight = msgheight - (dlgheight - maxDialogHeight);
7499 dlgheight = maxDialogHeight;
7500 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007501 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007502 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007503 }
7504
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 add_word(PixelToDialogY(dlgheight));
7506
7507 add_word(0); // Menu
7508 add_word(0); // Class
7509
Bram Moolenaar734a8672019-12-02 22:49:38 +01007510 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007511 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7512 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 p += nchar;
7514
K.Takatad1c58992022-01-23 12:31:57 +00007515 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007516# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007517 if (use_lfSysmenu)
7518 {
7519 // point size
7520 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7521 GetDeviceCaps(hdc, LOGPIXELSY));
7522 wcscpy(p, lfSysmenu.lfFaceName);
7523 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
K.Takatad1c58992022-01-23 12:31:57 +00007525 else
7526# endif
7527 {
7528 *p++ = DLG_FONT_POINT_SIZE; // point size
7529 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7530 }
7531 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 buttonYpos = msgheight + 2 * dlgPaddingY;
7534
7535 if (textfield != NULL)
7536 buttonYpos += editboxheight;
7537
7538 pstart = tbuffer;
7539 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007540 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 for (i = 0; i < numButtons; i++)
7542 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007543 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 for ( pend = pstart;
7545 *pend && (*pend != DLG_BUTTON_SEP);
7546 pend++)
7547 ;
7548
7549 if (*pend)
7550 *pend = '\0';
7551
7552 /*
7553 * old NOTE:
7554 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7555 * the focus to the first tab-able button and in so doing makes that
7556 * the default!! Grrr. Workaround: Make the default button the only
7557 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7558 * he/she can use arrow keys.
7559 *
7560 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007561 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 * dialog. Also needed for when the textfield is the default control.
7563 * It appears to work now (perhaps not on Win95?).
7564 */
7565 if (vertical)
7566 {
7567 p = add_dialog_element(p,
7568 (i == dfltbutton
7569 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7570 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007571 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 + 2 * fontHeight * i),
7573 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7574 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007575 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 }
7577 else
7578 {
7579 p = add_dialog_element(p,
7580 (i == dfltbutton
7581 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7582 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007583 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 PixelToDialogX(buttonWidths[i]),
7585 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007586 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007588 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590 *pnumitems += numButtons;
7591
Bram Moolenaar734a8672019-12-02 22:49:38 +01007592 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 p = add_dialog_element(p, SS_ICON,
7594 PixelToDialogX(dlgPaddingX),
7595 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007596 PixelToDialogX(dlg_icon_width),
7597 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7599 dlg_icons[type]);
7600
Bram Moolenaar734a8672019-12-02 22:49:38 +01007601 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007602 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007603 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007604 PixelToDialogY(dlgPaddingY),
7605 (WORD)(PixelToDialogX(messageWidth) + 1),
7606 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007607 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608
Bram Moolenaar734a8672019-12-02 22:49:38 +01007609 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 if (textfield != NULL)
7611 {
7612 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7613 PixelToDialogX(2 * dlgPaddingX),
7614 PixelToDialogY(2 * dlgPaddingY + msgheight),
7615 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7616 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007617 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 *pnumitems += 1;
7619 }
7620
7621 *pnumitems += 2;
7622
7623 SelectFont(hdc, oldFont);
7624 DeleteObject(font);
7625 ReleaseDC(hwnd, hdc);
7626
Bram Moolenaar734a8672019-12-02 22:49:38 +01007627 // Let the dialog_callback() function know which button to make default
7628 // If we have an edit box, make that the default. We also need to tell
7629 // dialog_callback() if this dialog contains an edit box or not. We do
7630 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 if (textfield != NULL)
7632 {
7633 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7634 s_textfield = textfield;
7635 }
7636 else
7637 {
7638 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7639 s_textfield = NULL;
7640 }
7641
Bram Moolenaar734a8672019-12-02 22:49:38 +01007642 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007644 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 (LPDLGTEMPLATE)pdlgtemplate,
7646 s_hwnd,
7647 (DLGPROC)dialog_callback);
7648
7649 LocalFree(LocalHandle(pdlgtemplate));
7650 vim_free(tbuffer);
7651 vim_free(buttonWidths);
7652 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007653 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654
Bram Moolenaar734a8672019-12-02 22:49:38 +01007655 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 (void)SetFocus(s_hwnd);
7657
7658 return nchar;
7659}
7660
Bram Moolenaar734a8672019-12-02 22:49:38 +01007661#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663/*
7664 * Put a simple element (basic class) onto a dialog template in memory.
7665 * return a pointer to where the next item should be added.
7666 *
7667 * parameters:
7668 * lStyle = additional style flags
7669 * (be careful, NT3.51 & Win32s will ignore the new ones)
7670 * x,y = x & y positions IN DIALOG UNITS
7671 * w,h = width and height IN DIALOG UNITS
7672 * Id = ID used in messages
7673 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7674 * caption = usually text or resource name
7675 *
7676 * TODO: use the length information noted here to enable the dialog creation
7677 * routines to work out more exactly how much memory they need to alloc.
7678 */
7679 static PWORD
7680add_dialog_element(
7681 PWORD p,
7682 DWORD lStyle,
7683 WORD x,
7684 WORD y,
7685 WORD w,
7686 WORD h,
7687 WORD Id,
7688 WORD clss,
7689 const char *caption)
7690{
7691 int nchar;
7692
Bram Moolenaar734a8672019-12-02 22:49:38 +01007693 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7695 *p++ = LOWORD(lStyle);
7696 *p++ = HIWORD(lStyle);
7697 *p++ = 0; // LOWORD (lExtendedStyle)
7698 *p++ = 0; // HIWORD (lExtendedStyle)
7699 *p++ = x;
7700 *p++ = y;
7701 *p++ = w;
7702 *p++ = h;
7703 *p++ = Id; //9 or 10 words in all
7704
7705 *p++ = (WORD)0xffff;
7706 *p++ = clss; //2 more here
7707
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007708 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 p += nchar;
7710
7711 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7712
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007713 return p; // total = 15 + strlen(caption) words
7714 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715}
7716
7717
7718/*
7719 * Helper routine. Take an input pointer, return closest pointer that is
7720 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7721 */
7722 static LPWORD
7723lpwAlign(
7724 LPWORD lpIn)
7725{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007726 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007728 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 ul += 3;
7730 ul >>= 2;
7731 ul <<= 2;
7732 return (LPWORD)ul;
7733}
7734
7735/*
7736 * Helper routine. Takes second parameter as Ansi string, copies it to first
7737 * parameter as wide character (16-bits / char) string, and returns integer
7738 * number of wide characters (words) in string (including the trailing wide
7739 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007740 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7741 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 static int
7743nCopyAnsiToWideChar(
7744 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007745 LPSTR lpAnsiIn,
7746 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007749 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 int i;
7751 WCHAR *wn;
7752
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007753 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007755 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007756 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 if (wn != NULL)
7758 {
7759 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007760 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 vim_free(wn);
7762 }
7763 }
7764 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007765 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 nChar = MultiByteToWideChar(
7767 enc_codepage > 0 ? enc_codepage : CP_ACP,
7768 MB_PRECOMPOSED,
7769 lpAnsiIn, len,
7770 lpWCStr, len);
7771 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007772 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774
7775 return nChar;
7776}
7777
7778
7779#ifdef FEAT_TEAROFF
7780/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007781 * Lookup menu handle from "menu_id".
7782 */
7783 static HMENU
7784tearoff_lookup_menuhandle(
7785 vimmenu_T *menu,
7786 WORD menu_id)
7787{
7788 for ( ; menu != NULL; menu = menu->next)
7789 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007790 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007791 continue;
7792 if (menu_is_separator(menu->dname))
7793 continue;
7794 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7795 return menu->submenu_id;
7796 }
7797 return NULL;
7798}
7799
7800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 * The callback function for all the modeless dialogs that make up the
7802 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7803 * thinking its menus have been clicked), and go away when closed.
7804 */
7805 static LRESULT CALLBACK
7806tearoff_callback(
7807 HWND hwnd,
7808 UINT message,
7809 WPARAM wParam,
7810 LPARAM lParam)
7811{
7812 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007813 {
7814 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817
Bram Moolenaar734a8672019-12-02 22:49:38 +01007818 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 HandleMouseHide(message, lParam);
7820
7821 if (message == WM_COMMAND)
7822 {
7823 if ((WORD)(LOWORD(wParam)) & 0x8000)
7824 {
7825 POINT mp;
7826 RECT rect;
7827
7828 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7829 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007830 vimmenu_T *menu;
7831
7832 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007834 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7836 (int)rect.right - 8,
7837 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007838 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 s_hwnd,
7840 NULL);
7841 /*
7842 * NOTE: The pop-up menu can eat the mouse up event.
7843 * We deal with this in normal.c.
7844 */
7845 }
7846 }
7847 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007848 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7850 /*
7851 * Give main window the focus back: this is so after
7852 * choosing a tearoff button you can start typing again
7853 * straight away.
7854 */
7855 (void)SetFocus(s_hwnd);
7856 return TRUE;
7857 }
7858 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7859 {
7860 DestroyWindow(hwnd);
7861 return TRUE;
7862 }
7863
Bram Moolenaar734a8672019-12-02 22:49:38 +01007864 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 if (message == WM_EXITSIZEMOVE)
7866 (void)SetActiveWindow(s_hwnd);
7867
7868 return FALSE;
7869}
7870#endif
7871
7872
7873/*
K.Takatad1c58992022-01-23 12:31:57 +00007874 * Computes the dialog base units based on the current dialog font.
7875 * We don't use the GetDialogBaseUnits() API, because we don't use the
7876 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 */
7878 static void
7879get_dialog_font_metrics(void)
7880{
7881 HDC hdc;
7882 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 SIZE size;
7884#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007885 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#endif
7887
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007889 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007890 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007891 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892#endif
7893 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007894 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895
K.Takatad1c58992022-01-23 12:31:57 +00007896 hdc = GetDC(s_hwnd);
7897 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007898 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007899 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
K.Takataabe628e2022-01-23 16:25:17 +00007901 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007902 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903}
7904
7905#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7906/*
7907 * Create a pseudo-"tearoff menu" based on the child
7908 * items of a given menu pointer.
7909 */
7910 static void
7911gui_mch_tearoff(
7912 char_u *title,
7913 vimmenu_T *menu,
7914 int initX,
7915 int initY)
7916{
7917 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7918 int template_len;
7919 int nchar, textWidth, submenuWidth;
7920 DWORD lStyle;
7921 DWORD lExtendedStyle;
7922 WORD dlgwidth;
7923 WORD menuID;
7924 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007925 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 vimmenu_T *the_menu = menu;
7927 HWND hwnd;
7928 HDC hdc;
7929 HFONT font, oldFont;
7930 int col, spaceWidth, len;
7931 int columnWidths[2];
7932 char_u *label, *text;
7933 int acLen = 0;
7934 int nameLen;
7935 int padding0, padding1, padding2 = 0;
7936 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007937 int x;
7938 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007939# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007940 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007942# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943
7944 /*
7945 * If this menu is already torn off, move it to the mouse position.
7946 */
7947 if (IsWindow(menu->tearoff_handle))
7948 {
7949 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007950 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 {
7952 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7953 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7954 }
7955 return;
7956 }
7957
7958 /*
7959 * Create a new tearoff.
7960 */
7961 if (*title == MNU_HIDDEN_CHAR)
7962 title++;
7963
Bram Moolenaar734a8672019-12-02 22:49:38 +01007964 // Allocate memory to store the dialog template. It's made bigger when
7965 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 template_len = DLG_ALLOC_SIZE;
7967 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7968 if (p == NULL)
7969 return;
7970
7971 hwnd = GetDesktopWindow();
7972 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007973# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7975 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007976 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 use_lfSysmenu = TRUE;
7978 }
7979 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007980# endif
K.Takatad1c58992022-01-23 12:31:57 +00007981 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7982 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7983
7984 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
Bram Moolenaar734a8672019-12-02 22:49:38 +01007986 // Calculate width of a single space. Used for padding columns to the
7987 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007988 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
Bram Moolenaar734a8672019-12-02 22:49:38 +01007990 // Figure out max width of the text column, the accelerator column and the
7991 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 submenuWidth = 0;
7993 for (col = 0; col < 2; col++)
7994 {
7995 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007996 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007998 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 text = (col == 0) ? pmenu->dname : pmenu->actext;
8000 if (text != NULL && *text != NUL)
8001 {
8002 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
8003 if (textWidth > columnWidths[col])
8004 columnWidths[col] = textWidth;
8005 }
8006 if (pmenu->children != NULL)
8007 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
8008 }
8009 }
8010 if (columnWidths[1] == 0)
8011 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008012 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (submenuWidth != 0)
8014 columnWidths[0] += submenuWidth;
8015 else
8016 columnWidths[0] += spaceWidth;
8017 }
8018 else
8019 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008020 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
8022 columnWidths[1] += submenuWidth;
8023 }
8024
8025 /*
8026 * Now find the total width of our 'menu'.
8027 */
8028 textWidth = columnWidths[0] + columnWidths[1];
8029 if (submenuWidth != 0)
8030 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008031 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
8033 textWidth += submenuWidth;
8034 }
8035 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
8036 if (textWidth > dlgwidth)
8037 dlgwidth = textWidth;
8038 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
8039
Bram Moolenaar734a8672019-12-02 22:49:38 +01008040 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00008041 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042
8043 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
8044 *p++ = LOWORD(lStyle);
8045 *p++ = HIWORD(lStyle);
8046 *p++ = LOWORD(lExtendedStyle);
8047 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008048 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008050 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008052 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 else
8054 *p++ = PixelToDialogX(initX); // x
8055 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00008056 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 else
8058 *p++ = PixelToDialogY(initY); // y
8059 *p++ = PixelToDialogX(dlgwidth); // cx
8060 ptrueheight = p;
8061 *p++ = 0; // dialog height: changed later anyway
8062 *p++ = 0; // Menu
8063 *p++ = 0; // Class
8064
Bram Moolenaar734a8672019-12-02 22:49:38 +01008065 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02008067 ? (LPSTR)title
8068 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 p += nchar;
8070
K.Takatad1c58992022-01-23 12:31:57 +00008071 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008072# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00008073 if (use_lfSysmenu)
8074 {
8075 // point size
8076 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
8077 GetDeviceCaps(hdc, LOGPIXELSY));
8078 wcscpy(p, lfSysmenu.lfFaceName);
8079 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 }
K.Takatad1c58992022-01-23 12:31:57 +00008081 else
8082# endif
8083 {
8084 *p++ = DLG_FONT_POINT_SIZE; // point size
8085 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
8086 }
8087 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088
8089 /*
8090 * Loop over all the items in the menu.
8091 * But skip over the tearbar.
8092 */
8093 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
8094 menu = menu->children->next;
8095 else
8096 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02008097 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 for ( ; menu != NULL; menu = menu->next)
8099 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008100 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 continue;
8102 if (menu_is_separator(menu->dname))
8103 {
8104 sepPadding += 3;
8105 continue;
8106 }
8107
Bram Moolenaar734a8672019-12-02 22:49:38 +01008108 // Check if there still is plenty of room in the template. Make it
8109 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
8111 {
8112 WORD *newp;
8113
8114 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
8115 if (newp != NULL)
8116 {
8117 template_len += 4096;
8118 mch_memmove(newp, pdlgtemplate,
8119 (char *)p - (char *)pdlgtemplate);
8120 p = newp + (p - pdlgtemplate);
8121 pnumitems = newp + (pnumitems - pdlgtemplate);
8122 ptrueheight = newp + (ptrueheight - pdlgtemplate);
8123 LocalFree(LocalHandle(pdlgtemplate));
8124 pdlgtemplate = newp;
8125 }
8126 }
8127
Bram Moolenaar734a8672019-12-02 22:49:38 +01008128 // Figure out minimal length of this menu label. Use "name" for the
8129 // actual text, "dname" for estimating the displayed size. "name"
8130 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 len = nameLen = (int)STRLEN(menu->name);
8132 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
8133 (int)STRLEN(menu->dname))) / spaceWidth;
8134 len += padding0;
8135
8136 if (menu->actext != NULL)
8137 {
8138 acLen = (int)STRLEN(menu->actext);
8139 len += acLen;
8140 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
8141 }
8142 else
8143 textWidth = 0;
8144 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
8145 len += padding1;
8146
8147 if (menu->children == NULL)
8148 {
8149 padding2 = submenuWidth / spaceWidth;
8150 len += padding2;
8151 menuID = (WORD)(menu->id);
8152 }
8153 else
8154 {
8155 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008156 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 }
8158
Bram Moolenaar734a8672019-12-02 22:49:38 +01008159 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02008160 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (label == NULL)
8162 break;
8163
Bram Moolenaarce0842a2005-07-18 21:58:11 +00008164 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008165 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01008167 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 while (padding0-- > 0)
8169 *text++ = ' ';
8170 if (menu->actext != NULL)
8171 {
8172 STRNCPY(text, menu->actext, acLen);
8173 text += acLen;
8174 }
8175 while (padding1-- > 0)
8176 *text++ = ' ';
8177 if (menu->children != NULL)
8178 {
8179 STRCPY(text, TEAROFF_SUBMENU_LABEL);
8180 text += STRLEN(TEAROFF_SUBMENU_LABEL);
8181 }
8182 else
8183 {
8184 while (padding2-- > 0)
8185 *text++ = ' ';
8186 }
8187 *text = NUL;
8188
8189 /*
8190 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
8191 * W95/NT4 it makes the tear-off look more like a menu.
8192 */
8193 p = add_dialog_element(p,
8194 BS_PUSHBUTTON|BS_LEFT,
8195 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
8196 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
8197 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
8198 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008199 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 vim_free(label);
8201 (*pnumitems)++;
8202 }
8203
8204 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
8205
8206
Bram Moolenaar734a8672019-12-02 22:49:38 +01008207 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02008208 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008209 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 (LPDLGTEMPLATE)pdlgtemplate,
8211 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02008212 (DLGPROC)tearoff_callback,
8213 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214
8215 LocalFree(LocalHandle(pdlgtemplate));
8216 SelectFont(hdc, oldFont);
8217 DeleteObject(font);
8218 ReleaseDC(hwnd, hdc);
8219
8220 /*
8221 * Reassert ourselves as the active window. This is so that after creating
8222 * a tearoff, the user doesn't have to click with the mouse just to start
8223 * typing again!
8224 */
8225 (void)SetActiveWindow(s_hwnd);
8226
Bram Moolenaar734a8672019-12-02 22:49:38 +01008227 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 force_menu_update = TRUE;
8229}
8230#endif
8231
8232#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008233# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235/*
8236 * Create the toolbar, initially unpopulated.
8237 * (just like the menu, there are no defaults, it's all
8238 * set up through menu.vim)
8239 */
8240 static void
8241initialise_toolbar(void)
8242{
8243 InitCommonControls();
8244 s_toolbarhwnd = CreateToolbarEx(
8245 s_hwnd,
8246 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
8247 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00008248 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008249 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 IDR_TOOLBAR1, // id of initial bitmap
8251 NULL,
8252 0, // initial number of buttons
8253 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
8254 TOOLBAR_BUTTON_HEIGHT,
8255 TOOLBAR_BUTTON_WIDTH,
8256 TOOLBAR_BUTTON_HEIGHT,
8257 sizeof(TBBUTTON)
8258 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01008259
8260 // Remove transparency from the toolbar to prevent the main window
8261 // background colour showing through
8262 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
8263 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
8264
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008265 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266
8267 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00008268
8269 update_toolbar_size();
8270}
8271
8272 static void
8273update_toolbar_size(void)
8274{
8275 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008276 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00008277
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008278 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00008279 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
8280 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
8281 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
8282 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
8283
8284 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
8285 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
8286 //TRACE("button size: %d, %d", w, h);
8287 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
8288 gui.toolbar_height = h + 6;
8289
8290 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
8291 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
8292
8293 // TODO:
8294 // Currently, this function only updates the size of toolbar buttons.
8295 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296}
8297
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008298 static LRESULT CALLBACK
8299toolbar_wndproc(
8300 HWND hwnd,
8301 UINT uMsg,
8302 WPARAM wParam,
8303 LPARAM lParam)
8304{
8305 HandleMouseHide(uMsg, lParam);
8306 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
8307}
8308
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 static int
8310get_toolbar_bitmap(vimmenu_T *menu)
8311{
8312 int i = -1;
8313
8314 /*
8315 * Check user bitmaps first, unless builtin is specified.
8316 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02008317 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 {
8319 char_u fname[MAXPATHL];
8320 HANDLE hbitmap = NULL;
8321
8322 if (menu->iconfile != NULL)
8323 {
8324 gui_find_iconfile(menu->iconfile, fname, "bmp");
8325 hbitmap = LoadImage(
8326 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008327 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 IMAGE_BITMAP,
8329 TOOLBAR_BUTTON_WIDTH,
8330 TOOLBAR_BUTTON_HEIGHT,
8331 LR_LOADFROMFILE |
8332 LR_LOADMAP3DCOLORS
8333 );
8334 }
8335
8336 /*
8337 * If the LoadImage call failed, or the "icon=" file
8338 * didn't exist or wasn't specified, try the menu name
8339 */
8340 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008341 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008342# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008343 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008344# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008345 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 hbitmap = LoadImage(
8347 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008348 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 IMAGE_BITMAP,
8350 TOOLBAR_BUTTON_WIDTH,
8351 TOOLBAR_BUTTON_HEIGHT,
8352 LR_LOADFROMFILE |
8353 LR_LOADMAP3DCOLORS
8354 );
8355
8356 if (hbitmap != NULL)
8357 {
8358 TBADDBITMAP tbAddBitmap;
8359
8360 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008361 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
8363 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8364 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008365 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 }
8367 }
8368 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8369 i = menu->iconidx;
8370
8371 return i;
8372}
8373#endif
8374
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008375#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8376 static void
8377initialise_tabline(void)
8378{
8379 InitCommonControls();
8380
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008381 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008382 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008383 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008384 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008385 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008386
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008387 gui.tabline_height = TABLINE_HEIGHT;
8388
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008389 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008390}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008391
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008392/*
8393 * Get tabpage_T from POINT.
8394 */
8395 static tabpage_T *
8396GetTabFromPoint(
8397 HWND hWnd,
8398 POINT pt)
8399{
8400 tabpage_T *ptp = NULL;
8401
8402 if (gui_mch_showing_tabline())
8403 {
8404 TCHITTESTINFO htinfo;
8405 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008406 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008407 if (s_tabhwnd == hWnd)
8408 {
8409 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8410 if (idx != -1)
8411 ptp = find_tabpage(idx + 1);
8412 }
8413 }
8414 return ptp;
8415}
8416
8417static POINT s_pt = {0, 0};
8418static HCURSOR s_hCursor = NULL;
8419
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008420 static LRESULT CALLBACK
8421tabline_wndproc(
8422 HWND hwnd,
8423 UINT uMsg,
8424 WPARAM wParam,
8425 LPARAM lParam)
8426{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008427 POINT pt;
8428 tabpage_T *tp;
8429 RECT rect;
8430 int nCenter;
8431 int idx0;
8432 int idx1;
8433
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008434 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008435
8436 switch (uMsg)
8437 {
8438 case WM_LBUTTONDOWN:
8439 {
8440 s_pt.x = GET_X_LPARAM(lParam);
8441 s_pt.y = GET_Y_LPARAM(lParam);
8442 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008443 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008444 break;
8445 }
8446 case WM_MOUSEMOVE:
8447 if (GetCapture() == hwnd
8448 && ((wParam & MK_LBUTTON)) != 0)
8449 {
8450 pt.x = GET_X_LPARAM(lParam);
8451 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008452 if (abs(pt.x - s_pt.x) >
8453 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008454 {
8455 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8456
8457 tp = GetTabFromPoint(hwnd, pt);
8458 if (tp != NULL)
8459 {
8460 idx0 = tabpage_index(curtab) - 1;
8461 idx1 = tabpage_index(tp) - 1;
8462
8463 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8464 nCenter = rect.left + (rect.right - rect.left) / 2;
8465
Bram Moolenaar734a8672019-12-02 22:49:38 +01008466 // Check if the mouse cursor goes over the center of
8467 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008468 if ((idx0 < idx1) && (nCenter < pt.x))
8469 {
8470 tabpage_move(idx1 + 1);
8471 update_screen(0);
8472 }
8473 else if ((idx1 < idx0) && (pt.x < nCenter))
8474 {
8475 tabpage_move(idx1);
8476 update_screen(0);
8477 }
8478 }
8479 }
8480 }
8481 break;
8482 case WM_LBUTTONUP:
8483 {
8484 if (GetCapture() == hwnd)
8485 {
8486 SetCursor(s_hCursor);
8487 ReleaseCapture();
8488 }
8489 break;
8490 }
regomne3bdef102022-09-26 20:48:32 +01008491 case WM_MBUTTONUP:
8492 {
8493 TCHITTESTINFO htinfo;
8494
8495 htinfo.pt.x = GET_X_LPARAM(lParam);
8496 htinfo.pt.y = GET_Y_LPARAM(lParam);
8497 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8498 if (idx0 != -1)
8499 {
8500 idx0 += 1;
8501 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8502 }
8503 break;
8504 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008505 default:
8506 break;
8507 }
8508
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008509 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8510}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008511#endif
8512
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8514/*
8515 * Make the GUI window come to the foreground.
8516 */
8517 void
8518gui_mch_set_foreground(void)
8519{
8520 if (IsIconic(s_hwnd))
8521 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8522 SetForegroundWindow(s_hwnd);
8523}
8524#endif
8525
8526#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8527 static void
8528dyn_imm_load(void)
8529{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008530 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 if (hLibImm == NULL)
8532 return;
8533
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008535 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008537 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008539 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008541 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008543 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008545 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008546 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008547 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008548 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008549 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008551 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008553 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008554 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008555 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556
K.Takatab0b2b732022-01-19 12:59:21 +00008557 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 || pImmGetContext == NULL
8559 || pImmAssociateContext == NULL
8560 || pImmReleaseContext == NULL
8561 || pImmGetOpenStatus == NULL
8562 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008563 || pImmGetCompositionFontW == NULL
8564 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008566 || pImmGetConversionStatus == NULL
8567 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {
8569 FreeLibrary(hLibImm);
8570 hLibImm = NULL;
8571 pImmGetContext = NULL;
8572 return;
8573 }
8574
8575 return;
8576}
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578#endif
8579
8580#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8581
8582# ifdef FEAT_XPM_W32
8583# define IMAGE_XPM 100
8584# endif
8585
8586typedef struct _signicon_t
8587{
8588 HANDLE hImage;
8589 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008590# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008591 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593} signicon_t;
8594
8595 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008596gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597{
8598 signicon_t *sign;
8599 int x, y, w, h;
8600
8601 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8602 return;
8603
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008604# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008605 if (IS_ENABLE_DIRECTX())
8606 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008607# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008608
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 x = TEXT_X(col);
8610 y = TEXT_Y(row);
8611 w = gui.char_width * 2;
8612 h = gui.char_height;
8613 switch (sign->uType)
8614 {
8615 case IMAGE_BITMAP:
8616 {
8617 HDC hdcMem;
8618 HBITMAP hbmpOld;
8619
8620 hdcMem = CreateCompatibleDC(s_hdc);
8621 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8622 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8623 SelectObject(hdcMem, hbmpOld);
8624 DeleteDC(hdcMem);
8625 }
8626 break;
8627 case IMAGE_ICON:
8628 case IMAGE_CURSOR:
8629 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8630 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008631# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 case IMAGE_XPM:
8633 {
8634 HDC hdcMem;
8635 HBITMAP hbmpOld;
8636
8637 hdcMem = CreateCompatibleDC(s_hdc);
8638 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008639 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8641
8642 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008643 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8645 SelectObject(hdcMem, hbmpOld);
8646 DeleteDC(hdcMem);
8647 }
8648 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 }
8651}
8652
8653 static void
8654close_signicon_image(signicon_t *sign)
8655{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008656 if (sign == NULL)
8657 return;
8658
8659 switch (sign->uType)
8660 {
8661 case IMAGE_BITMAP:
8662 DeleteObject((HGDIOBJ)sign->hImage);
8663 break;
8664 case IMAGE_CURSOR:
8665 DestroyCursor((HCURSOR)sign->hImage);
8666 break;
8667 case IMAGE_ICON:
8668 DestroyIcon((HICON)sign->hImage);
8669 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008670# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008671 case IMAGE_XPM:
8672 DeleteObject((HBITMAP)sign->hImage);
8673 DeleteObject((HBITMAP)sign->hShape);
8674 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008675# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677}
8678
8679 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008680gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 signicon_t sign, *psign;
8683 char_u *ext;
8684
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008686 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 if (ext > signfile)
8688 {
8689 int do_load = 1;
8690
8691 if (!STRICMP(ext, ".bmp"))
8692 sign.uType = IMAGE_BITMAP;
8693 else if (!STRICMP(ext, ".ico"))
8694 sign.uType = IMAGE_ICON;
8695 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8696 sign.uType = IMAGE_CURSOR;
8697 else
8698 do_load = 0;
8699
8700 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008701 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 gui.char_width * 2, gui.char_height,
8703 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008704# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 if (!STRICMP(ext, ".xpm"))
8706 {
8707 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008708 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8709 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 }
8713
8714 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008715 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 *psign = sign;
8717
8718 if (!psign)
8719 {
8720 if (sign.hImage)
8721 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008722 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 }
8724 return (void *)psign;
8725
8726}
8727
8728 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008729gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008731 if (sign == NULL)
8732 return;
8733
8734 close_signicon_image((signicon_t *)sign);
8735 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008739#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740
Bram Moolenaar734a8672019-12-02 22:49:38 +01008741/*
8742 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008743 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008745 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8747 * to get current mouse position).
8748 *
8749 * Trying to use as more Windows services as possible, and as less
8750 * IE version as possible :)).
8751 *
8752 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8753 * BalloonEval struct.
8754 * 2) Enable/Disable simply create/kill BalloonEval Timer
8755 * 3) When there was enough inactivity, timer procedure posts
8756 * async request to debugger
8757 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8758 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008759 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 */
8761
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008762 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008763make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008764{
K.Takata76687d22022-01-25 10:31:37 +00008765 TOOLINFOW *pti;
8766 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008767
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008768 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008769 if (pti == NULL)
8770 return;
8771
8772 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8773 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8774 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008775 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008776
8777 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8778 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8779
K.Takata76687d22022-01-25 10:31:37 +00008780 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008781 pti->uFlags = TTF_SUBCLASS;
8782 pti->hwnd = beval->target;
8783 pti->hinst = 0; // Don't use string resources
8784 pti->uId = ID_BEVAL_TOOLTIP;
8785
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008786 pti->lpszText = LPSTR_TEXTCALLBACKW;
8787 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8788 pti->lParam = (LPARAM)beval->tofree;
8789 // switch multiline tooltips on
8790 if (GetClientRect(s_textArea, &rect))
8791 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8792 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008793
8794 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8795 pti->rect.left = pt.x - 3;
8796 pti->rect.top = pt.y - 3;
8797 pti->rect.right = pt.x + 3;
8798 pti->rect.bottom = pt.y + 3;
8799
8800 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8801 // Make tooltip appear sooner.
8802 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8803 // I've performed some tests and it seems the longest possible life time
8804 // of tooltip is 30 seconds.
8805 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8806 /*
8807 * HACK: force tooltip to appear, because it'll not appear until
8808 * first mouse move. D*mn M$
8809 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8810 */
8811 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8812 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8813 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008814}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008817delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008819 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820}
8821
8822 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008823beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008824 HWND hwnd UNUSED,
8825 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008826 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008827 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 POINT pt;
8830 RECT rect;
8831
8832 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8833 return;
8834
8835 GetCursorPos(&pt);
8836 if (WindowFromPoint(pt) != s_textArea)
8837 return;
8838
8839 ScreenToClient(s_textArea, &pt);
8840 GetClientRect(s_textArea, &rect);
8841 if (!PtInRect(&rect, pt))
8842 return;
8843
K.Takataa8ec4912022-02-03 14:32:33 +00008844 if (last_user_activity > 0
8845 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 && (cur_beval->showState != ShS_PENDING
8847 || abs(cur_beval->x - pt.x) > 3
8848 || abs(cur_beval->y - pt.y) > 3))
8849 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008850 // Pointer resting in one place long enough, it's time to show
8851 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 cur_beval->showState = ShS_PENDING;
8853 cur_beval->x = pt.x;
8854 cur_beval->y = pt.y;
8855
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 if (cur_beval->msgCB != NULL)
8857 (*cur_beval->msgCB)(cur_beval, 0);
8858 }
8859}
8860
8861 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008862gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863{
K.Takataa8ec4912022-02-03 14:32:33 +00008864 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865}
8866
8867 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008868gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 if (beval == NULL)
8871 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008872 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8873 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874}
8875
8876 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008877gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878{
8879 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008880
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008881 vim_free(beval->msg);
8882 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8883 if (beval->msg == NULL)
8884 {
8885 delete_tooltip(beval);
8886 beval->showState = ShS_NEUTRAL;
8887 return;
8888 }
8889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (beval->showState == ShS_SHOWING)
8891 return;
8892 GetCursorPos(&pt);
8893 ScreenToClient(s_textArea, &pt);
8894
8895 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008897 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 gui_mch_disable_beval_area(cur_beval);
8899 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008900 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902}
8903
8904 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008905gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008906 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008907 char_u *mesg,
8908 void (*mesgCB)(BalloonEval *, int),
8909 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008911 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 BalloonEval *beval;
8913
8914 if (mesg != NULL && mesgCB != NULL)
8915 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008916 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 return NULL;
8918 }
8919
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008920 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 if (beval != NULL)
8922 {
8923 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924
8925 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926 beval->msg = mesg;
8927 beval->msgCB = mesgCB;
8928 beval->clientData = clientData;
8929
8930 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 cur_beval = beval;
8932
8933 if (p_beval)
8934 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936 return beval;
8937}
8938
8939 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008940Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008942 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 return;
8944
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008945 if (cur_beval == NULL)
8946 return;
8947
8948 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008950 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008951 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008952 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 delete_tooltip(cur_beval);
8954 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955
8956 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008957 break;
8958 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008959 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008960 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008961 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008962 info->lpszText = (LPSTR)info->lParam;
8963 info->uFlags |= TTF_DI_SETITEM;
8964 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008965 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008966 case TTN_GETDISPINFOW:
8967 {
8968 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008969 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008970 info->lpszText = (LPWSTR)info->lParam;
8971 info->uFlags |= TTF_DI_SETITEM;
8972 }
8973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 }
8975}
8976
8977 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008978track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8981 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008982 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
8984
8985 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008986gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008988# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008989 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008990# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008991 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 vim_free(beval);
8993}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008994#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
8996#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8997/*
8998 * We have multiple signs to draw at the same location. Draw the
8999 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
9000 */
9001 void
9002netbeans_draw_multisign_indicator(int row)
9003{
9004 int i;
9005 int y;
9006 int x;
9007
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009008 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02009009 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009010
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 x = 0;
9012 y = TEXT_Y(row);
9013
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009014# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009015 if (IS_ENABLE_DIRECTX())
9016 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01009017# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01009018
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 for (i = 0; i < gui.char_height - 3; i++)
9020 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
9021
9022 SetPixel(s_hdc, x+0, y, gui.currFgColor);
9023 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9024 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
9025 SetPixel(s_hdc, x+1, y, gui.currFgColor);
9026 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9027 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
9028 SetPixel(s_hdc, x+2, y, gui.currFgColor);
9029}
Bram Moolenaare0874f82016-01-24 20:36:41 +01009030#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009031
9032#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009033
Christopher Plewright20b795e2022-12-20 20:01:58 +00009034// TODO: at the moment, this is just a copy of test_gui_mouse_event.
9035// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00009036// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00009037 static int
9038test_gui_w32_sendevent_mouse(dict_T *args)
9039{
9040 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009041 return FALSE;
9042
Christopher Plewright20b795e2022-12-20 20:01:58 +00009043 // Note: "move" is optional, requires fewer arguments
9044 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009045
Christopher Plewright20b795e2022-12-20 20:01:58 +00009046 if (!move && (!dict_has_key(args, "button")
9047 || !dict_has_key(args, "multiclick")
9048 || !dict_has_key(args, "modifiers")))
9049 return FALSE;
9050
9051 int row = (int)dict_get_number(args, "row");
9052 int col = (int)dict_get_number(args, "col");
9053
9054 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009055 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00009056 // the "move" argument expects row and col coordnates to be in pixels,
9057 // unless "cell" is specified and is TRUE.
9058 if (dict_get_bool(args, "cell", FALSE))
9059 {
9060 // calculate the middle of the character cell
9061 // Note: Cell coordinates are 1-based from vimscript
9062 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
9063 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
9064 gui_mouse_moved(pX, pY);
9065 }
9066 else
9067 gui_mouse_moved(col, row);
9068 }
9069 else
9070 {
9071 int button = (int)dict_get_number(args, "button");
9072 int repeated_click = (int)dict_get_number(args, "multiclick");
9073 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009074
Christopher Plewright20b795e2022-12-20 20:01:58 +00009075 // Reset the scroll values to known values.
9076 // XXX: Remove this when/if the scroll step is made configurable.
9077 mouse_set_hor_scroll_step(6);
9078 mouse_set_vert_scroll_step(3);
9079
9080 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
9081 repeated_click, mods);
9082 }
9083 return TRUE;
9084}
9085
9086 static int
9087test_gui_w32_sendevent_keyboard(dict_T *args)
9088{
9089 INPUT inputs[1];
9090 INPUT modkeys[3];
9091 SecureZeroMemory(inputs, sizeof(INPUT));
9092 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
9093
9094 char_u *event = dict_get_string(args, "event", TRUE);
9095
9096 if (event && (STRICMP(event, "keydown") == 0
9097 || STRICMP(event, "keyup") == 0))
9098 {
9099 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009100 if (vkCode <= 0 || vkCode >= 0xFF)
9101 {
9102 semsg(_(e_invalid_argument_nr), (long)vkCode);
9103 return FALSE;
9104 }
9105
Christopher Plewright20b795e2022-12-20 20:01:58 +00009106 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
9107 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
9108 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
9109 || vkCode == VK_LMENU || vkCode == VK_RMENU );
9110
9111 BOOL unwrapMods = FALSE;
9112 int mods = (int)dict_get_number(args, "modifiers");
9113
9114 // If there are modifiers in the args, and it is not a keyup event and
9115 // vkCode is not a modifier key, then we generate virtual modifier key
9116 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009117 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009118 {
9119 int n = 0;
9120 if (mods & MOD_MASK_SHIFT)
9121 {
9122 modkeys[n].type = INPUT_KEYBOARD;
9123 modkeys[n].ki.wVk = VK_LSHIFT;
9124 n++;
9125 }
9126 if (mods & MOD_MASK_CTRL)
9127 {
9128 modkeys[n].type = INPUT_KEYBOARD;
9129 modkeys[n].ki.wVk = VK_LCONTROL;
9130 n++;
9131 }
9132 if (mods & MOD_MASK_ALT)
9133 {
9134 modkeys[n].type = INPUT_KEYBOARD;
9135 modkeys[n].ki.wVk = VK_LMENU;
9136 n++;
9137 }
9138 if (n)
9139 {
9140 (void)SetForegroundWindow(s_hwnd);
9141 SendInput(n, modkeys, sizeof(INPUT));
9142 }
9143 }
9144
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009145 inputs[0].type = INPUT_KEYBOARD;
9146 inputs[0].ki.wVk = vkCode;
9147 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009148 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009149 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01009150 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00009151 unwrapMods = TRUE;
9152 }
9153
K.Takatafef38d82022-09-07 19:03:42 +01009154 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009155 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00009156 vim_free(event);
9157
9158 if (unwrapMods)
9159 {
9160 modkeys[0].type = INPUT_KEYBOARD;
9161 modkeys[0].ki.wVk = VK_LSHIFT;
9162 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
9163
9164 modkeys[1].type = INPUT_KEYBOARD;
9165 modkeys[1].ki.wVk = VK_LCONTROL;
9166 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
9167
9168 modkeys[2].type = INPUT_KEYBOARD;
9169 modkeys[2].ki.wVk = VK_LMENU;
9170 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
9171
9172 (void)SetForegroundWindow(s_hwnd);
9173 SendInput(3, modkeys, sizeof(INPUT));
9174 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009175 }
9176 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00009177 {
9178 if (event == NULL)
9179 {
9180 semsg(_(e_missing_argument_str), "event");
9181 }
9182 else
9183 {
9184 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9185 vim_free(event);
9186 }
9187 return FALSE;
9188 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009189 return TRUE;
9190}
Christopher Plewright20b795e2022-12-20 20:01:58 +00009191
Anton Sharonov68d94722024-01-23 23:19:02 +01009192 static int
9193test_gui_w32_sendevent_set_keycode_trans_strategy(dict_T *args)
9194{
9195 int handled = 0;
9196 char_u *strategy = dict_get_string(args, "strategy", TRUE);
9197
9198 if (strategy)
9199 {
9200 if (STRICMP(strategy, "classic") == 0)
9201 {
9202 handled = 1;
9203 keycode_trans_strategy_used = &keycode_trans_strategy_classic;
9204 }
9205 else if (STRICMP(strategy, "experimental") == 0)
9206 {
9207 handled = 1;
9208 keycode_trans_strategy_used = &keycode_trans_strategy_experimental;
9209 }
9210 }
9211
9212 if (!handled)
9213 {
9214 if (strategy == NULL)
9215 {
9216 semsg(_(e_missing_argument_str), "strategy");
9217 }
9218 else
9219 {
9220 semsg(_(e_invalid_value_for_argument_str_str), "strategy", strategy);
9221 vim_free(strategy);
9222 }
9223 return FALSE;
9224 }
9225 return TRUE;
9226}
9227
9228
Christopher Plewright20b795e2022-12-20 20:01:58 +00009229 int
9230test_gui_w32_sendevent(char_u *event, dict_T *args)
9231{
9232 if (STRICMP(event, "key") == 0)
9233 return test_gui_w32_sendevent_keyboard(args);
9234 else if (STRICMP(event, "mouse") == 0)
9235 return test_gui_w32_sendevent_mouse(args);
Anton Sharonov68d94722024-01-23 23:19:02 +01009236 else if (STRICMP(event, "set_keycode_trans_strategy") == 0)
9237 return test_gui_w32_sendevent_set_keycode_trans_strategy(args);
Christopher Plewright20b795e2022-12-20 20:01:58 +00009238 else
9239 {
9240 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
9241 return FALSE;
9242 }
9243}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01009244#endif