blob: 3ade63113b03517daf09dd0df100a7e7e65c44cd [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
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020053#if defined(FEAT_RENDER_OPTIONS) || defined(PROTO)
54 int
55gui_mch_set_rendering_options(char_u *s)
56{
Bram Moolenaar7f88b652017-12-14 13:15:19 +010057# ifdef FEAT_DIRECTX
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020058 char_u *p, *q;
59
60 int dx_enable = 0;
61 int dx_flags = 0;
62 float dx_gamma = 0.0f;
63 float dx_contrast = 0.0f;
64 float dx_level = 0.0f;
65 int dx_geom = 0;
66 int dx_renmode = 0;
67 int dx_taamode = 0;
68
Bram Moolenaar734a8672019-12-02 22:49:38 +010069 // parse string as rendering options.
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020070 for (p = s; p != NULL && *p != NUL; )
71 {
72 char_u item[256];
73 char_u name[128];
74 char_u value[128];
75
Bram Moolenaarcde88542015-08-11 19:14:00 +020076 copy_option_part(&p, item, sizeof(item), ",");
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020077 if (p == NULL)
78 break;
79 q = &item[0];
80 copy_option_part(&q, name, sizeof(name), ":");
81 if (q == NULL)
82 return FAIL;
83 copy_option_part(&q, value, sizeof(value), ":");
84
85 if (STRCMP(name, "type") == 0)
86 {
87 if (STRCMP(value, "directx") == 0)
88 dx_enable = 1;
89 else
90 return FAIL;
91 }
92 else if (STRCMP(name, "gamma") == 0)
93 {
94 dx_flags |= 1 << 0;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +010095 dx_gamma = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020096 }
97 else if (STRCMP(name, "contrast") == 0)
98 {
99 dx_flags |= 1 << 1;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100100 dx_contrast = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200101 }
102 else if (STRCMP(name, "level") == 0)
103 {
104 dx_flags |= 1 << 2;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100105 dx_level = (float)atof((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200106 }
107 else if (STRCMP(name, "geom") == 0)
108 {
109 dx_flags |= 1 << 3;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100110 dx_geom = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200111 if (dx_geom < 0 || dx_geom > 2)
112 return FAIL;
113 }
114 else if (STRCMP(name, "renmode") == 0)
115 {
116 dx_flags |= 1 << 4;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100117 dx_renmode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200118 if (dx_renmode < 0 || dx_renmode > 6)
119 return FAIL;
120 }
121 else if (STRCMP(name, "taamode") == 0)
122 {
123 dx_flags |= 1 << 5;
Bram Moolenaar7f0608f2016-02-18 20:46:39 +0100124 dx_taamode = atoi((char *)value);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200125 if (dx_taamode < 0 || dx_taamode > 3)
126 return FAIL;
127 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100128 else if (STRCMP(name, "scrlines") == 0)
129 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100130 // Deprecated. Simply ignore it.
Bram Moolenaar92467d32017-12-05 13:22:16 +0100131 }
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200132 else
133 return FAIL;
134 }
135
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100136 if (!gui.in_use)
Bram Moolenaar734a8672019-12-02 22:49:38 +0100137 return OK; // only checking the syntax of the value
Bram Moolenaar3767c6e2017-12-05 16:57:56 +0100138
Bram Moolenaar734a8672019-12-02 22:49:38 +0100139 // Enable DirectX/DirectWrite
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200140 if (dx_enable)
141 {
142 if (!directx_enabled())
143 return FAIL;
144 DWriteContext_SetRenderingParams(s_dwc, NULL);
145 if (dx_flags)
146 {
147 DWriteRenderingParams param;
148 DWriteContext_GetRenderingParams(s_dwc, &param);
149 if (dx_flags & (1 << 0))
150 param.gamma = dx_gamma;
151 if (dx_flags & (1 << 1))
152 param.enhancedContrast = dx_contrast;
153 if (dx_flags & (1 << 2))
154 param.clearTypeLevel = dx_level;
155 if (dx_flags & (1 << 3))
156 param.pixelGeometry = dx_geom;
157 if (dx_flags & (1 << 4))
158 param.renderingMode = dx_renmode;
159 if (dx_flags & (1 << 5))
160 param.textAntialiasMode = dx_taamode;
161 DWriteContext_SetRenderingParams(s_dwc, &param);
162 }
163 }
164 s_directx_enabled = dx_enable;
165
166 return OK;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100167# else
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200168 return FAIL;
Bram Moolenaar7f88b652017-12-14 13:15:19 +0100169# endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +0200170}
171#endif
172
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173/*
174 * These are new in Windows ME/XP, only defined in recent compilers.
175 */
176#ifndef HANDLE_WM_XBUTTONUP
177# define HANDLE_WM_XBUTTONUP(hwnd, wParam, lParam, fn) \
178 ((fn)((hwnd), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
179#endif
180#ifndef HANDLE_WM_XBUTTONDOWN
181# define HANDLE_WM_XBUTTONDOWN(hwnd, wParam, lParam, fn) \
182 ((fn)((hwnd), FALSE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
183#endif
184#ifndef HANDLE_WM_XBUTTONDBLCLK
185# define HANDLE_WM_XBUTTONDBLCLK(hwnd, wParam, lParam, fn) \
186 ((fn)((hwnd), TRUE, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)(wParam)), 0L)
187#endif
188
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100189
Bram Moolenaar734a8672019-12-02 22:49:38 +0100190#include "version.h" // used by dialog box routine for default title
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100191#ifdef DEBUG
192# include <tchar.h>
193#endif
194
Bram Moolenaar734a8672019-12-02 22:49:38 +0100195// cproto fails on missing include files
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100196#ifndef PROTO
197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100198# ifndef __MINGW32__
199# include <shellapi.h>
200# endif
K.Takata2da11a42022-09-10 13:03:12 +0100201# include <commctrl.h>
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100202# include <windowsx.h>
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100203
Bram Moolenaar734a8672019-12-02 22:49:38 +0100204#endif // PROTO
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100205
206#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +0100207# define MENUHINTS // show menu hints in command line
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100208#endif
209
Bram Moolenaar734a8672019-12-02 22:49:38 +0100210// Some parameters for dialog boxes. All in pixels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100211#define DLG_PADDING_X 10
212#define DLG_PADDING_Y 10
Bram Moolenaar734a8672019-12-02 22:49:38 +0100213#define DLG_VERT_PADDING_X 4 // For vertical buttons
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100214#define DLG_VERT_PADDING_Y 4
215#define DLG_ICON_WIDTH 34
216#define DLG_ICON_HEIGHT 34
217#define DLG_MIN_WIDTH 150
K.Takatad1c58992022-01-23 12:31:57 +0000218#define DLG_FONT_NAME "MS Shell Dlg"
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100219#define DLG_FONT_POINT_SIZE 8
220#define DLG_MIN_MAX_WIDTH 400
221#define DLG_MIN_MAX_HEIGHT 400
222
Bram Moolenaar734a8672019-12-02 22:49:38 +0100223#define DLG_NONBUTTON_CONTROL 5000 // First ID of non-button controls
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100224
K.Takatac81e9bf2022-01-16 14:15:49 +0000225#ifndef WM_DPICHANGED
LemonBoy365d8f72022-05-05 19:23:07 +0100226# define WM_DPICHANGED 0x02E0
227#endif
228
229#ifndef WM_MOUSEHWHEEL
230# define WM_MOUSEHWHEEL 0x020E
231#endif
232
233#ifndef SPI_GETWHEELSCROLLCHARS
234# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000235#endif
236
LemonBoyc27747e2022-05-07 12:25:40 +0100237#ifndef SPI_SETWHEELSCROLLCHARS
238# define SPI_SETWHEELSCROLLCHARS 0x006D
239#endif
240
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100241#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100243 * Define a few things for generating prototypes. This is just to avoid
244 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100246# define APIENTRY
247# define CALLBACK
248# define CONST
249# define FAR
250# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200251# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200252# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100253# define _cdecl
254typedef int BOOL;
255typedef int BYTE;
256typedef int DWORD;
257typedef int WCHAR;
258typedef int ENUMLOGFONT;
259typedef int FINDREPLACE;
260typedef int HANDLE;
261typedef int HBITMAP;
262typedef int HBRUSH;
263typedef int HDROP;
264typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100265typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100266typedef int LPARAM;
267typedef int LPCREATESTRUCT;
268typedef int LPCSTR;
269typedef int LPCTSTR;
270typedef int LPRECT;
271typedef int LPSTR;
272typedef int LPWINDOWPOS;
273typedef int LPWORD;
274typedef int LRESULT;
275typedef int HRESULT;
276# undef MSG
277typedef int MSG;
278typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100279typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100280typedef int OSVERSIONINFO;
281typedef int PWORD;
282typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int UINT;
285typedef int WORD;
286typedef int WPARAM;
287typedef int POINT;
288typedef void *HINSTANCE;
289typedef void *HMENU;
290typedef void *HWND;
291typedef void *HDC;
292typedef void VOID;
293typedef int LPNMHDR;
294typedef int LONG;
295typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200296typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200297typedef int COLORREF;
298typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100299#endif
300
K.Takata45f9cfb2022-01-21 11:11:00 +0000301static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100302static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303static void clear_rect(RECT *rcp);
304
Bram Moolenaar734a8672019-12-02 22:49:38 +0100305static WORD s_dlgfntheight; // height of the dialog font
306static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307
308#ifdef FEAT_MENU
309static HMENU s_menuBar = NULL;
310#endif
311#ifdef FEAT_TEAROFF
312static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100313static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100314#endif
315
Bram Moolenaar734a8672019-12-02 22:49:38 +0100316// Flag that is set while processing a message that must not be interrupted by
317// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318static int s_busy_processing = FALSE;
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100321
322#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000323static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200324static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200326static int s_findrep_is_find; // TRUE for find dialog, FALSE
327 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100328#endif
329
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100330HWND s_hwnd = NULL;
331static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100332static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100333
334#ifdef FEAT_TOOLBAR
335static HWND s_toolbarhwnd = NULL;
336static WNDPROC s_toolbar_wndproc = NULL;
337#endif
338
339#ifdef FEAT_GUI_TABLINE
340static HWND s_tabhwnd = NULL;
341static WNDPROC s_tabline_wndproc = NULL;
342static int showing_tabline = 0;
343#endif
344
345static WPARAM s_wParam = 0;
346static LPARAM s_lParam = 0;
347
348static HWND s_textArea = NULL;
349static UINT s_uMsg = 0;
350
Bram Moolenaar734a8672019-12-02 22:49:38 +0100351static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100352
353static int s_need_activate = FALSE;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355// This variable is set when waiting for an event, which is the only moment
356// scrollbar dragging can be done directly. It's not allowed while commands
357// are executed, because it may move the cursor and that may cause unexpected
358// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100359static int allow_scrollbar = FALSE;
360
K.Takatac81e9bf2022-01-16 14:15:49 +0000361#ifndef _DPI_AWARENESS_CONTEXTS_
362typedef HANDLE DPI_AWARENESS_CONTEXT;
363
364typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000365 DPI_AWARENESS_INVALID = -1,
366 DPI_AWARENESS_UNAWARE = 0,
367 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000368 DPI_AWARENESS_PER_MONITOR_AWARE = 2
369} DPI_AWARENESS;
370
K.Takatab0b2b732022-01-19 12:59:21 +0000371# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
372# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000373# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
374# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
375# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
376#endif
377
378#define DEFAULT_DPI 96
379static int s_dpi = DEFAULT_DPI;
380static BOOL s_in_dpichanged = FALSE;
381static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
382
383static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
384static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
385static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
386//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
387static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
388static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
389
390 static UINT WINAPI
391stubGetDpiForSystem(void)
392{
393 HWND hwnd = GetDesktopWindow();
394 HDC hdc = GetWindowDC(hwnd);
395 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
396 ReleaseDC(hwnd, hdc);
397 return dpi;
398}
399
400 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100401stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000402{
403 return GetSystemMetrics(nIndex);
404}
405
406 static int
407adjust_fontsize_by_dpi(int size)
408{
409 return size * s_dpi / (int)pGetDpiForSystem();
410}
411
412 static int
413adjust_by_system_dpi(int size)
414{
415 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
416}
417
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100418#if defined(FEAT_DIRECTX)
419 static int
420directx_enabled(void)
421{
422 if (s_dwc != NULL)
423 return 1;
424 else if (s_directx_load_attempted)
425 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100426 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100427 DWrite_Init();
428 s_directx_load_attempted = 1;
429 s_dwc = DWriteContext_Open();
430 directx_binddc();
431 return s_dwc != NULL ? 1 : 0;
432}
433
434 static void
435directx_binddc(void)
436{
437 if (s_textArea != NULL)
438 {
439 RECT rect;
440 GetClientRect(s_textArea, &rect);
441 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
442 }
443}
444#endif
445
Bram Moolenaar734a8672019-12-02 22:49:38 +0100446extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100447
448static struct
449{
450 UINT key_sym;
451 char_u vim_code0;
452 char_u vim_code1;
453} special_keys[] =
454{
455 {VK_UP, 'k', 'u'},
456 {VK_DOWN, 'k', 'd'},
457 {VK_LEFT, 'k', 'l'},
458 {VK_RIGHT, 'k', 'r'},
459
460 {VK_F1, 'k', '1'},
461 {VK_F2, 'k', '2'},
462 {VK_F3, 'k', '3'},
463 {VK_F4, 'k', '4'},
464 {VK_F5, 'k', '5'},
465 {VK_F6, 'k', '6'},
466 {VK_F7, 'k', '7'},
467 {VK_F8, 'k', '8'},
468 {VK_F9, 'k', '9'},
469 {VK_F10, 'k', ';'},
470
471 {VK_F11, 'F', '1'},
472 {VK_F12, 'F', '2'},
473 {VK_F13, 'F', '3'},
474 {VK_F14, 'F', '4'},
475 {VK_F15, 'F', '5'},
476 {VK_F16, 'F', '6'},
477 {VK_F17, 'F', '7'},
478 {VK_F18, 'F', '8'},
479 {VK_F19, 'F', '9'},
480 {VK_F20, 'F', 'A'},
481
482 {VK_F21, 'F', 'B'},
483#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100484 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100485#endif
486 {VK_F22, 'F', 'C'},
487 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100488 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100489
490 {VK_HELP, '%', '1'},
491 {VK_BACK, 'k', 'b'},
492 {VK_INSERT, 'k', 'I'},
493 {VK_DELETE, 'k', 'D'},
494 {VK_HOME, 'k', 'h'},
495 {VK_END, '@', '7'},
496 {VK_PRIOR, 'k', 'P'},
497 {VK_NEXT, 'k', 'N'},
498 {VK_PRINT, '%', '9'},
499 {VK_ADD, 'K', '6'},
500 {VK_SUBTRACT, 'K', '7'},
501 {VK_DIVIDE, 'K', '8'},
502 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100503 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100504 {VK_DECIMAL, 'K', 'B'},
505
506 {VK_NUMPAD0, 'K', 'C'},
507 {VK_NUMPAD1, 'K', 'D'},
508 {VK_NUMPAD2, 'K', 'E'},
509 {VK_NUMPAD3, 'K', 'F'},
510 {VK_NUMPAD4, 'K', 'G'},
511 {VK_NUMPAD5, 'K', 'H'},
512 {VK_NUMPAD6, 'K', 'I'},
513 {VK_NUMPAD7, 'K', 'J'},
514 {VK_NUMPAD8, 'K', 'K'},
515 {VK_NUMPAD9, 'K', 'L'},
516
Bram Moolenaar734a8672019-12-02 22:49:38 +0100517 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100518 {VK_SPACE, ' ', NUL},
519 {VK_TAB, TAB, NUL},
520 {VK_ESCAPE, ESC, NUL},
521 {NL, NL, NUL},
522 {CAR, CAR, NUL},
523
Bram Moolenaar734a8672019-12-02 22:49:38 +0100524 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100525 {0, 0, 0}
526};
527
Bram Moolenaar734a8672019-12-02 22:49:38 +0100528// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100529static int s_button_pending = -1;
530
Bram Moolenaar734a8672019-12-02 22:49:38 +0100531// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
532// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100533static int s_getting_focus = FALSE;
534
535static int s_x_pending;
536static int s_y_pending;
537static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000538static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100539static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100540static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200541static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
542 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100543
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100544#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100545// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100546static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000547static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548#endif
549
550/*
551 * For control IME.
552 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100553 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100554 */
K.Takata4ac893f2022-01-20 12:44:28 +0000555#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100556// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100557static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100558// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100559static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100560#endif
561
562#ifdef FEAT_MBYTE_IME
563static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
564#endif
565
566#if defined(FEAT_BROWSE)
567static char_u *convert_filter(char_u *s);
568#endif
569
570#ifdef DEBUG_PRINT_ERROR
571/*
572 * Print out the last Windows error message
573 */
574 static void
575print_windows_error(void)
576{
577 LPVOID lpMsgBuf;
578
579 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
580 NULL, GetLastError(),
581 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
582 (LPTSTR) &lpMsgBuf, 0, NULL);
583 TRACE1("Error: %s\n", lpMsgBuf);
584 LocalFree(lpMsgBuf);
585}
586#endif
587
588/*
589 * Cursor blink functions.
590 *
591 * This is a simple state machine:
592 * BLINK_NONE not blinking at all
593 * BLINK_OFF blinking, cursor is not shown
594 * BLINK_ON blinking, cursor is shown
595 */
596
597#define BLINK_NONE 0
598#define BLINK_OFF 1
599#define BLINK_ON 2
600
601static int blink_state = BLINK_NONE;
602static long_u blink_waittime = 700;
603static long_u blink_ontime = 400;
604static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000605static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100606
Bram Moolenaar703a8042016-06-04 16:24:32 +0200607 int
608gui_mch_is_blinking(void)
609{
610 return blink_state != BLINK_NONE;
611}
612
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200613 int
614gui_mch_is_blink_off(void)
615{
616 return blink_state == BLINK_OFF;
617}
618
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100619 void
620gui_mch_set_blinking(long wait, long on, long off)
621{
622 blink_waittime = wait;
623 blink_ontime = on;
624 blink_offtime = off;
625}
626
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100627 static VOID CALLBACK
628_OnBlinkTimer(
629 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100630 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000631 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100632 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100633{
634 MSG msg;
635
636 /*
637 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
638 */
639
640 KillTimer(NULL, idEvent);
641
Bram Moolenaar734a8672019-12-02 22:49:38 +0100642 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000643 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100644 ;
645
646 if (blink_state == BLINK_ON)
647 {
648 gui_undraw_cursor();
649 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000650 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100651 }
652 else
653 {
654 gui_update_cursor(TRUE, FALSE);
655 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000656 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100657 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100658 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100659}
660
661 static void
662gui_mswin_rm_blink_timer(void)
663{
664 MSG msg;
665
666 if (blink_timer != 0)
667 {
668 KillTimer(NULL, blink_timer);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100669 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000670 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100671 ;
672 blink_timer = 0;
673 }
674}
675
676/*
677 * Stop the cursor blinking. Show the cursor if it wasn't shown.
678 */
679 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100680gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100681{
682 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100683 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100684 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100685 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100686 gui_mch_flush();
687 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100688 blink_state = BLINK_NONE;
689}
690
691/*
692 * Start the cursor blinking. If it was already blinking, this restarts the
693 * waiting time and shows the cursor.
694 */
695 void
696gui_mch_start_blink(void)
697{
698 gui_mswin_rm_blink_timer();
699
Bram Moolenaar734a8672019-12-02 22:49:38 +0100700 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100701 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
702 {
K.Takataa8ec4912022-02-03 14:32:33 +0000703 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100704 blink_state = BLINK_ON;
705 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100706 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100707 }
708}
709
710/*
711 * Call-back routines.
712 */
713
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100714 static VOID CALLBACK
715_OnTimer(
716 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100717 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000718 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100719 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100720{
721 MSG msg;
722
723 /*
724 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
725 */
726 KillTimer(NULL, idEvent);
727 s_timed_out = TRUE;
728
Bram Moolenaar734a8672019-12-02 22:49:38 +0100729 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000730 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100731 ;
732 if (idEvent == s_wait_timer)
733 s_wait_timer = 0;
734}
735
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 static void
737_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100738 HWND hwnd UNUSED,
739 UINT ch UNUSED,
740 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741{
742 dead_key = 1;
743}
744
745/*
746 * Convert Unicode character "ch" to bytes in "string[slen]".
747 * When "had_alt" is TRUE the ALT key was included in "ch".
748 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200749 * Because the Windows API uses UTF-16, we have to deal with surrogate
750 * pairs; this is where we choose to deal with them: if "ch" is a high
751 * surrogate, it will be stored, and the length returned will be zero; the next
752 * char_to_string call will then include the high surrogate, decoding the pair
753 * of UTF-16 code units to a single Unicode code point, presuming it is the
754 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100755 */
756 static int
757char_to_string(int ch, char_u *string, int slen, int had_alt)
758{
759 int len;
760 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100761 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200762 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100763
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200764 if (surrogate_pending_ch != 0)
765 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100766 // We don't guarantee ch is a low surrogate to match the high surrogate
767 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200768 wstring[0] = surrogate_pending_ch;
769 wstring[1] = ch;
770 surrogate_pending_ch = 0;
771 len = 2;
772 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100773 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200774 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100775 // We don't have the entire code point yet, only the first UTF-16 code
776 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200777 surrogate_pending_ch = ch;
778 return 0;
779 }
780 else
781 {
782 wstring[0] = ch;
783 len = 1;
784 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200785
Bram Moolenaar734a8672019-12-02 22:49:38 +0100786 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
787 // "enc_codepage" is non-zero use the standard Win32 function,
788 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200789 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100790 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
792 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100793 // If we had included the ALT key into the character but now the
794 // upper bit is no longer set, that probably means the conversion
795 // failed. Convert the original character and set the upper bit
796 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200797 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100798 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200799 wstring[0] = ch & 0x7f;
800 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
801 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100802 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200803 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100804 }
805 }
806 else
807 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 ws = utf16_to_enc(wstring, &len);
809 if (ws == NULL)
810 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100811 else
812 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100813 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200814 len = slen;
815 mch_memmove(string, ws, len);
816 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100817 }
818 }
819
820 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100821 {
822 string[0] = ch;
823 len = 1;
824 }
825
826 for (i = 0; i < len; ++i)
827 if (string[i] == CSI && len <= slen - 2)
828 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100829 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100830 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
831 string[++i] = KS_EXTRA;
832 string[++i] = (int)KE_CSI;
833 len += 2;
834 }
835
836 return len;
837}
838
LemonBoy45684c62022-04-24 15:46:42 +0100839 static int
840get_active_modifiers(void)
841{
842 int modifiers = 0;
843
844 if (GetKeyState(VK_CONTROL) & 0x8000)
845 modifiers |= MOD_MASK_CTRL;
846 if (GetKeyState(VK_SHIFT) & 0x8000)
847 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100848 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
849 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100850 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100851 if (GetKeyState(VK_LMENU) & 0x8000)
852 modifiers |= MOD_MASK_ALT;
853 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
854 modifiers &= ~MOD_MASK_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +0100855
856 return modifiers;
857}
858
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100859/*
860 * Key hit, add it to the input buffer.
861 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100862 static void
863_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100864 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100865 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100866 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100867{
868 char_u string[40];
869 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100870 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100871 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872
Anton Sharonov3f026672022-07-26 21:26:18 +0100873 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
874 return;
875
876 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
877 // process_message()
878 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
879 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880
LemonBoy45684c62022-04-24 15:46:42 +0100881 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100882
883 ch = simplify_key(ch, &modifiers);
884 // remove the SHIFT modifier for keys where it's already included, e.g.,
885 // '(' and '*'
886 modifiers = may_remove_shift_modifier(modifiers, ch);
887
888 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
889 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
890 if (ch == CSI)
891 ch = K_CSI;
892
893 if (modifiers)
894 {
895 string[0] = CSI;
896 string[1] = KS_MODIFIER;
897 string[2] = modifiers;
898 add_to_input_buf(string, 3);
899 }
900
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100901 len = char_to_string(ch, string, 40, FALSE);
902 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
903 {
904 trash_input_buf();
905 got_int = TRUE;
906 }
907
908 add_to_input_buf(string, len);
909}
910
911/*
912 * Alt-Key hit, add it to the input buffer.
913 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100914 static void
915_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100916 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100917 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100918 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100920 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921 int len;
922 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100923 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924
Anton Sharonov3f026672022-07-26 21:26:18 +0100925 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100926
Bram Moolenaar734a8672019-12-02 22:49:38 +0100927 // OK, we have a character key (given by ch) which was entered with the
928 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
929 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
930 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100931 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100933 // remove the SHIFT modifier for keys where it's already included, e.g.,
934 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200935 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100936
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200937 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
938 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939 if (ch == CSI)
940 ch = K_CSI;
941
942 len = 0;
943 if (modifiers)
944 {
945 string[len++] = CSI;
946 string[len++] = KS_MODIFIER;
947 string[len++] = modifiers;
948 }
949
950 if (IS_SPECIAL((int)ch))
951 {
952 string[len++] = CSI;
953 string[len++] = K_SECOND((int)ch);
954 string[len++] = K_THIRD((int)ch);
955 }
956 else
957 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100958 // Although the documentation isn't clear about it, we assume "ch" is
959 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100960 len += char_to_string(ch, string + len, 40 - len, TRUE);
961 }
962
963 add_to_input_buf(string, len);
964}
965
966 static void
967_OnMouseEvent(
968 int button,
969 int x,
970 int y,
971 int repeated_click,
972 UINT keyFlags)
973{
974 int vim_modifiers = 0x0;
975
976 s_getting_focus = FALSE;
977
978 if (keyFlags & MK_SHIFT)
979 vim_modifiers |= MOUSE_SHIFT;
980 if (keyFlags & MK_CONTROL)
981 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100982 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100983 vim_modifiers |= MOUSE_ALT;
984
985 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
986}
987
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100988 static void
989_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100990 HWND hwnd UNUSED,
991 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100992 int x,
993 int y,
994 UINT keyFlags)
995{
996 static LONG s_prevTime = 0;
997
998 LONG currentTime = GetMessageTime();
999 int button = -1;
1000 int repeated_click;
1001
Bram Moolenaar734a8672019-12-02 22:49:38 +01001002 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001003 (void)SetFocus(s_hwnd);
1004
1005 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1006 button = MOUSE_LEFT;
1007 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1008 button = MOUSE_MIDDLE;
1009 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1010 button = MOUSE_RIGHT;
1011 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1012 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001013 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1014 }
1015 else if (s_uMsg == WM_CAPTURECHANGED)
1016 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001017 // on W95/NT4, somehow you get in here with an odd Msg
1018 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001019 if (s_button_pending == MOUSE_LEFT)
1020 button = MOUSE_RIGHT;
1021 else
1022 button = MOUSE_LEFT;
1023 }
1024 if (button >= 0)
1025 {
1026 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1027
1028 /*
1029 * Holding down the left and right buttons simulates pushing the middle
1030 * button.
1031 */
1032 if (repeated_click
1033 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1034 || (button == MOUSE_RIGHT
1035 && s_button_pending == MOUSE_LEFT)))
1036 {
1037 /*
1038 * Hmm, gui.c will ignore more than one button down at a time, so
1039 * pretend we let go of it first.
1040 */
1041 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1042 button = MOUSE_MIDDLE;
1043 repeated_click = FALSE;
1044 s_button_pending = -1;
1045 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1046 }
1047 else if ((repeated_click)
1048 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1049 {
1050 if (s_button_pending > -1)
1051 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001052 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1053 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001054 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001055 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1057 }
1058 else
1059 {
1060 /*
1061 * If this is the first press (i.e. not a multiple click) don't
1062 * action immediately, but store and wait for:
1063 * i) button-up
1064 * ii) mouse move
1065 * iii) another button press
1066 * before using it.
1067 * This enables us to make left+right simulate middle button,
1068 * without left or right being actioned first. The side-effect is
1069 * that if you click and hold the mouse without dragging, the
1070 * cursor doesn't move until you release the button. In practice
1071 * this is hardly a problem.
1072 */
1073 s_button_pending = button;
1074 s_x_pending = x;
1075 s_y_pending = y;
1076 s_kFlags_pending = keyFlags;
1077 }
1078
1079 s_prevTime = currentTime;
1080 }
1081}
1082
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001083 static void
1084_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001085 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001086 int x,
1087 int y,
1088 UINT keyFlags)
1089{
1090 int button;
1091
1092 s_getting_focus = FALSE;
1093 if (s_button_pending > -1)
1094 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001095 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001096 _OnMouseEvent(s_button_pending, s_x_pending,
1097 s_y_pending, FALSE, s_kFlags_pending);
1098 s_button_pending = -1;
1099 }
1100 if (s_uMsg == WM_MOUSEMOVE)
1101 {
1102 /*
1103 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1104 * down.
1105 */
1106 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1107 | MK_XBUTTON1 | MK_XBUTTON2)))
1108 {
1109 gui_mouse_moved(x, y);
1110 return;
1111 }
1112
1113 /*
1114 * While button is down, keep grabbing mouse move events when
1115 * the mouse goes outside the window
1116 */
1117 SetCapture(s_textArea);
1118 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001119 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001120 }
1121 else
1122 {
1123 ReleaseCapture();
1124 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001125 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001126 }
1127
1128 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1129}
1130
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001131 static void
1132_OnSizeTextArea(
1133 HWND hwnd UNUSED,
1134 UINT state UNUSED,
1135 int cx UNUSED,
1136 int cy UNUSED)
1137{
1138#if defined(FEAT_DIRECTX)
1139 if (IS_ENABLE_DIRECTX())
1140 directx_binddc();
1141#endif
1142}
1143
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001144#ifdef FEAT_MENU
1145/*
1146 * Find the vimmenu_T with the given id
1147 */
1148 static vimmenu_T *
1149gui_mswin_find_menu(
1150 vimmenu_T *pMenu,
1151 int id)
1152{
1153 vimmenu_T *pChildMenu;
1154
1155 while (pMenu)
1156 {
1157 if (pMenu->id == (UINT)id)
1158 break;
1159 if (pMenu->children != NULL)
1160 {
1161 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1162 if (pChildMenu)
1163 {
1164 pMenu = pChildMenu;
1165 break;
1166 }
1167 }
1168 pMenu = pMenu->next;
1169 }
1170 return pMenu;
1171}
1172
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001173 static void
1174_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001175 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001176 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001177 HWND hwndCtl UNUSED,
1178 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001179{
1180 vimmenu_T *pMenu;
1181
1182 pMenu = gui_mswin_find_menu(root_menu, id);
1183 if (pMenu)
1184 gui_menu_cb(pMenu);
1185}
1186#endif
1187
1188#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001189/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001190 * Handle a Find/Replace window message.
1191 */
1192 static void
1193_OnFindRepl(void)
1194{
1195 int flags = 0;
1196 int down;
1197
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001199 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001200 (void)SetFocus(s_hwnd);
1201
1202 if (s_findrep_struct.Flags & FR_FINDNEXT)
1203 {
1204 flags = FRD_FINDNEXT;
1205
Bram Moolenaar734a8672019-12-02 22:49:38 +01001206 // Give main window the focus back: this is so the cursor isn't
1207 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001208 (void)SetFocus(s_hwnd);
1209 }
1210 else if (s_findrep_struct.Flags & FR_REPLACE)
1211 {
1212 flags = FRD_REPLACE;
1213
Bram Moolenaar734a8672019-12-02 22:49:38 +01001214 // Give main window the focus back: this is so the cursor isn't
1215 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001216 (void)SetFocus(s_hwnd);
1217 }
1218 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1219 {
1220 flags = FRD_REPLACEALL;
1221 }
1222
1223 if (flags != 0)
1224 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001225 char_u *p, *q;
1226
Bram Moolenaar734a8672019-12-02 22:49:38 +01001227 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001228 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1229 flags |= FRD_WHOLE_WORD;
1230 if (s_findrep_struct.Flags & FR_MATCHCASE)
1231 flags |= FRD_MATCH_CASE;
1232 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001233 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1234 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1235 if (p != NULL && q != NULL)
1236 gui_do_findrepl(flags, p, q, down);
1237 vim_free(p);
1238 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001239 }
1240}
1241#endif
1242
1243 static void
1244HandleMouseHide(UINT uMsg, LPARAM lParam)
1245{
1246 static LPARAM last_lParam = 0L;
1247
Bram Moolenaar734a8672019-12-02 22:49:38 +01001248 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001249 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1250 {
1251 if (lParam == last_lParam)
1252 return;
1253 last_lParam = lParam;
1254 }
1255
Bram Moolenaar734a8672019-12-02 22:49:38 +01001256 // Handle specially, to centralise coding. We need to be sure we catch all
1257 // possible events which should cause us to restore the cursor (as it is a
1258 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259 switch (uMsg)
1260 {
1261 case WM_KEYUP:
1262 case WM_CHAR:
1263 /*
1264 * blank out the pointer if necessary
1265 */
1266 if (p_mh)
1267 gui_mch_mousehide(TRUE);
1268 break;
1269
Bram Moolenaar734a8672019-12-02 22:49:38 +01001270 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001271 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001272 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001273 case WM_LBUTTONDOWN:
1274 case WM_LBUTTONUP:
1275 case WM_MBUTTONDOWN:
1276 case WM_MBUTTONUP:
1277 case WM_RBUTTONDOWN:
1278 case WM_RBUTTONUP:
1279 case WM_XBUTTONDOWN:
1280 case WM_XBUTTONUP:
1281 case WM_NCMOUSEMOVE:
1282 case WM_NCLBUTTONDOWN:
1283 case WM_NCLBUTTONUP:
1284 case WM_NCMBUTTONDOWN:
1285 case WM_NCMBUTTONUP:
1286 case WM_NCRBUTTONDOWN:
1287 case WM_NCRBUTTONUP:
1288 case WM_KILLFOCUS:
1289 /*
1290 * if the pointer is currently hidden, then we should show it.
1291 */
1292 gui_mch_mousehide(FALSE);
1293 break;
1294 }
1295}
1296
1297 static LRESULT CALLBACK
1298_TextAreaWndProc(
1299 HWND hwnd,
1300 UINT uMsg,
1301 WPARAM wParam,
1302 LPARAM lParam)
1303{
1304 /*
1305 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1306 hwnd, uMsg, wParam, lParam);
1307 */
1308
1309 HandleMouseHide(uMsg, lParam);
1310
1311 s_uMsg = uMsg;
1312 s_wParam = wParam;
1313 s_lParam = lParam;
1314
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001315#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001316 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001317#endif
1318
1319 switch (uMsg)
1320 {
1321 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1322 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1323 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1324 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1325 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1326 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1327 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1328 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1329 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1330 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1331 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1332 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1333 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1334 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001335 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001336
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001337#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001338 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1339 return TRUE;
1340#endif
1341 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001342 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001343 }
1344}
1345
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001346/*
1347 * Called when the foreground or background color has been changed.
1348 */
1349 void
1350gui_mch_new_colors(void)
1351{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001352 HBRUSH prevBrush;
1353
1354 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001355 prevBrush = (HBRUSH)SetClassLongPtr(
1356 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001357 InvalidateRect(s_hwnd, NULL, TRUE);
1358 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001359}
1360
1361/*
1362 * Set the colors to their default values.
1363 */
1364 void
1365gui_mch_def_colors(void)
1366{
1367 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1368 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1369 gui.def_norm_pixel = gui.norm_pixel;
1370 gui.def_back_pixel = gui.back_pixel;
1371}
1372
1373/*
1374 * Open the GUI window which was created by a call to gui_mch_init().
1375 */
1376 int
1377gui_mch_open(void)
1378{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001379 // Actually open the window, if not already visible
1380 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001381 if (!IsWindowVisible(s_hwnd))
1382 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1383
1384#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001385 // Init replace string here, so that we keep it when re-opening the
1386 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001387 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1388#endif
1389
1390 return OK;
1391}
1392
1393/*
1394 * Get the position of the top left corner of the window.
1395 */
1396 int
1397gui_mch_get_winpos(int *x, int *y)
1398{
1399 RECT rect;
1400
1401 GetWindowRect(s_hwnd, &rect);
1402 *x = rect.left;
1403 *y = rect.top;
1404 return OK;
1405}
1406
1407/*
1408 * Set the position of the top left corner of the window to the given
1409 * coordinates.
1410 */
1411 void
1412gui_mch_set_winpos(int x, int y)
1413{
1414 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1415 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1416}
1417 void
1418gui_mch_set_text_area_pos(int x, int y, int w, int h)
1419{
1420 static int oldx = 0;
1421 static int oldy = 0;
1422
1423 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1424
1425#ifdef FEAT_TOOLBAR
1426 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1427 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001428 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001429#endif
1430#if defined(FEAT_GUI_TABLINE)
1431 if (showing_tabline)
1432 {
1433 int top = 0;
1434 RECT rect;
1435
1436# ifdef FEAT_TOOLBAR
1437 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001438 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001439# endif
1440 GetClientRect(s_hwnd, &rect);
1441 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1442 }
1443#endif
1444
Bram Moolenaar734a8672019-12-02 22:49:38 +01001445 // When side scroll bar is unshown, the size of window will change.
1446 // then, the text area move left or right. thus client rect should be
1447 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448 if (oldx != x || oldy != y)
1449 {
1450 InvalidateRect(s_hwnd, NULL, FALSE);
1451 oldx = x;
1452 oldy = y;
1453 }
1454}
1455
1456
1457/*
1458 * Scrollbar stuff:
1459 */
1460
1461 void
1462gui_mch_enable_scrollbar(
1463 scrollbar_T *sb,
1464 int flag)
1465{
1466 ShowScrollBar(sb->id, SB_CTL, flag);
1467
Bram Moolenaar734a8672019-12-02 22:49:38 +01001468 // TODO: When the window is maximized, the size of the window stays the
1469 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1470 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001471}
1472
1473 void
1474gui_mch_set_scrollbar_pos(
1475 scrollbar_T *sb,
1476 int x,
1477 int y,
1478 int w,
1479 int h)
1480{
1481 SetWindowPos(sb->id, NULL, x, y, w, h,
1482 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1483}
1484
Bram Moolenaar203ec772020-07-17 20:43:43 +02001485 int
1486gui_mch_get_scrollbar_xpadding(void)
1487{
1488 RECT rcTxt, rcWnd;
1489 int xpad;
1490
1491 GetWindowRect(s_textArea, &rcTxt);
1492 GetWindowRect(s_hwnd, &rcWnd);
1493 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001494 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1495 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001496 return (xpad < 0) ? 0 : xpad;
1497}
1498
1499 int
1500gui_mch_get_scrollbar_ypadding(void)
1501{
1502 RECT rcTxt, rcWnd;
1503 int ypad;
1504
1505 GetWindowRect(s_textArea, &rcTxt);
1506 GetWindowRect(s_hwnd, &rcWnd);
1507 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001508 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1509 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001510 return (ypad < 0) ? 0 : ypad;
1511}
1512
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001513 void
1514gui_mch_create_scrollbar(
1515 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001516 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001517{
1518 sb->id = CreateWindow(
1519 "SCROLLBAR", "Scrollbar",
1520 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001521 10, // Any value will do for now
1522 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001523 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001524 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001525}
1526
1527/*
1528 * Find the scrollbar with the given hwnd.
1529 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001530 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001531gui_mswin_find_scrollbar(HWND hwnd)
1532{
1533 win_T *wp;
1534
1535 if (gui.bottom_sbar.id == hwnd)
1536 return &gui.bottom_sbar;
1537 FOR_ALL_WINDOWS(wp)
1538 {
1539 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1540 return &wp->w_scrollbars[SBAR_LEFT];
1541 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1542 return &wp->w_scrollbars[SBAR_RIGHT];
1543 }
1544 return NULL;
1545}
1546
K.Takatac81e9bf2022-01-16 14:15:49 +00001547 static void
1548update_scrollbar_size(void)
1549{
1550 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1551 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1552}
1553
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001554/*
K.Takataabe628e2022-01-23 16:25:17 +00001555 * Get the average character size of a font.
1556 */
1557 static void
1558GetAverageFontSize(HDC hdc, SIZE *size)
1559{
1560 // GetTextMetrics() may not return the right value in tmAveCharWidth
1561 // for some fonts. Do our own average computation.
1562 GetTextExtentPoint(hdc,
1563 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1564 52, size);
1565 size->cx = (size->cx / 26 + 1) / 2;
1566}
1567
1568/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001569 * Get the character size of a font.
1570 */
1571 static void
1572GetFontSize(GuiFont font)
1573{
1574 HWND hwnd = GetDesktopWindow();
1575 HDC hdc = GetWindowDC(hwnd);
1576 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001577 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001578 TEXTMETRIC tm;
1579
1580 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001581 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001582
K.Takataabe628e2022-01-23 16:25:17 +00001583 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001584 gui.char_height = tm.tmHeight + p_linespace;
1585
1586 SelectFont(hdc, hfntOld);
1587
1588 ReleaseDC(hwnd, hdc);
1589}
1590
1591/*
1592 * Adjust gui.char_height (after 'linespace' was changed).
1593 */
1594 int
1595gui_mch_adjust_charheight(void)
1596{
1597 GetFontSize(gui.norm_font);
1598 return OK;
1599}
1600
1601 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001602get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603{
1604 HFONT font = NULL;
1605
Bram Moolenaar734a8672019-12-02 22:49:38 +01001606 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001607 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001608
1609 if (font == NULL)
1610 return NOFONT;
1611
1612 return (GuiFont)font;
1613}
1614
1615 static int
1616pixels_to_points(int pixels, int vertical)
1617{
1618 int points;
1619 HWND hwnd;
1620 HDC hdc;
1621
1622 hwnd = GetDesktopWindow();
1623 hdc = GetWindowDC(hwnd);
1624
1625 points = MulDiv(pixels, 72,
1626 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1627
1628 ReleaseDC(hwnd, hdc);
1629
1630 return points;
1631}
1632
1633 GuiFont
1634gui_mch_get_font(
1635 char_u *name,
1636 int giveErrorIfMissing)
1637{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001638 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001639 GuiFont font = NOFONT;
1640
1641 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001642 {
1643 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001644 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001645 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001647 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001648 return font;
1649}
1650
1651#if defined(FEAT_EVAL) || defined(PROTO)
1652/*
1653 * Return the name of font "font" in allocated memory.
1654 * Don't know how to get the actual name, thus use the provided name.
1655 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001656 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001657gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658{
1659 if (name == NULL)
1660 return NULL;
1661 return vim_strsave(name);
1662}
1663#endif
1664
1665 void
1666gui_mch_free_font(GuiFont font)
1667{
1668 if (font)
1669 DeleteObject((HFONT)font);
1670}
1671
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001672/*
1673 * Return the Pixel value (color) for the given color name.
1674 * Return INVALCOLOR for error.
1675 */
1676 guicolor_T
1677gui_mch_get_color(char_u *name)
1678{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001679 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001680
1681 typedef struct SysColorTable
1682 {
1683 char *name;
1684 int color;
1685 } SysColorTable;
1686
1687 static SysColorTable sys_table[] =
1688 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001689 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1690 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001691#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001692 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1693#endif
1694 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1695 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1696 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1697 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1698 {"SYS_DESKTOP", COLOR_DESKTOP},
1699 {"SYS_INFOBK", COLOR_INFOBK},
1700 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1701 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001702 {"SYS_BTNFACE", COLOR_BTNFACE},
1703 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1704 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1705 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1706 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1707 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1708 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1709 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1710 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1711 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1712 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1713 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1714 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1715 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1716 {"SYS_MENU", COLOR_MENU},
1717 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1718 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1719 {"SYS_WINDOW", COLOR_WINDOW},
1720 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1721 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1722 };
1723
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001724 /*
1725 * Try to look up a system colour.
1726 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001727 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728 if (STRICMP(name, sys_table[i].name) == 0)
1729 return GetSysColor(sys_table[i].color);
1730
Bram Moolenaarab302212016-04-26 20:59:29 +02001731 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001732}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001733
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001734 guicolor_T
1735gui_mch_get_rgb_color(int r, int g, int b)
1736{
1737 return gui_get_rgb_color_cmn(r, g, b);
1738}
1739
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001740/*
1741 * Return OK if the key with the termcap name "name" is supported.
1742 */
1743 int
1744gui_mch_haskey(char_u *name)
1745{
1746 int i;
1747
1748 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001749 if (name[0] == special_keys[i].vim_code0
1750 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751 return OK;
1752 return FAIL;
1753}
1754
1755 void
1756gui_mch_beep(void)
1757{
LemonBoy77771d32022-04-13 11:47:25 +01001758 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759}
1760/*
1761 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1762 */
1763 void
1764gui_mch_invert_rectangle(
1765 int r,
1766 int c,
1767 int nr,
1768 int nc)
1769{
1770 RECT rc;
1771
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001772#if defined(FEAT_DIRECTX)
1773 if (IS_ENABLE_DIRECTX())
1774 DWriteContext_Flush(s_dwc);
1775#endif
1776
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001777 /*
1778 * Note: InvertRect() excludes right and bottom of rectangle.
1779 */
1780 rc.left = FILL_X(c);
1781 rc.top = FILL_Y(r);
1782 rc.right = rc.left + nc * gui.char_width;
1783 rc.bottom = rc.top + nr * gui.char_height;
1784 InvertRect(s_hdc, &rc);
1785}
1786
1787/*
1788 * Iconify the GUI window.
1789 */
1790 void
1791gui_mch_iconify(void)
1792{
1793 ShowWindow(s_hwnd, SW_MINIMIZE);
1794}
1795
1796/*
1797 * Draw a cursor without focus.
1798 */
1799 void
1800gui_mch_draw_hollow_cursor(guicolor_T color)
1801{
1802 HBRUSH hbr;
1803 RECT rc;
1804
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001805#if defined(FEAT_DIRECTX)
1806 if (IS_ENABLE_DIRECTX())
1807 DWriteContext_Flush(s_dwc);
1808#endif
1809
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001810 /*
1811 * Note: FrameRect() excludes right and bottom of rectangle.
1812 */
1813 rc.left = FILL_X(gui.col);
1814 rc.top = FILL_Y(gui.row);
1815 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001816 if (mb_lefthalve(gui.row, gui.col))
1817 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001818 rc.bottom = rc.top + gui.char_height;
1819 hbr = CreateSolidBrush(color);
1820 FrameRect(s_hdc, &rc, hbr);
1821 DeleteBrush(hbr);
1822}
1823/*
1824 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1825 * color "color".
1826 */
1827 void
1828gui_mch_draw_part_cursor(
1829 int w,
1830 int h,
1831 guicolor_T color)
1832{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001833 RECT rc;
1834
1835 /*
1836 * Note: FillRect() excludes right and bottom of rectangle.
1837 */
1838 rc.left =
1839#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001840 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001841 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1842#endif
1843 FILL_X(gui.col);
1844 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1845 rc.right = rc.left + w;
1846 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001847
Bram Moolenaar92467d32017-12-05 13:22:16 +01001848 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001849}
1850
1851
1852/*
1853 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1854 * dead key's nominal character and re-post the original message.
1855 */
1856 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001857outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001858{
1859 static MSG deadCharExpel;
1860
Anton Sharonov3f026672022-07-26 21:26:18 +01001861 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001862 return;
1863
Anton Sharonov3f026672022-07-26 21:26:18 +01001864 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001865
Bram Moolenaar734a8672019-12-02 22:49:38 +01001866 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001867 deadCharExpel.message = originalMsg.message;
1868 deadCharExpel.hwnd = originalMsg.hwnd;
1869 deadCharExpel.wParam = VK_SPACE;
1870
K.Takata4ac893f2022-01-20 12:44:28 +00001871 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872
Bram Moolenaar734a8672019-12-02 22:49:38 +01001873 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1875 originalMsg.lParam);
1876}
1877
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001878/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001879 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1880 */
1881 static void
1882outputDeadKey_rePost(MSG originalMsg)
1883{
1884 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1885}
1886
1887/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001888 * Process a single Windows message.
1889 * If one is not available we hang until one is.
1890 */
1891 static void
1892process_message(void)
1893{
1894 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001895 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001896 char_u string[40];
1897 int i;
1898 int modifiers = 0;
1899 int key;
1900#ifdef FEAT_MENU
1901 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1902#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001903 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001904
K.Takatab7057bd2022-01-21 11:37:07 +00001905 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001906
1907#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001908 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001909 if (msg.message == WM_OLE)
1910 {
1911 char_u *str = (char_u *)msg.lParam;
1912 if (str == NULL || *str == NUL)
1913 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001914 // Message can't be ours, forward it. Fixes problem with Ultramon
1915 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001916 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001917 }
1918 else
1919 {
1920 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001921 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001922 }
1923 return;
1924 }
1925#endif
1926
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001927#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001929 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001930 {
1931 HandleMouseHide(msg.message, msg.lParam);
1932 return;
1933 }
1934#endif
1935
1936 /*
1937 * Check if it's a special key that we recognise. If not, call
1938 * TranslateMessage().
1939 */
1940 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1941 {
1942 vk = (int) msg.wParam;
1943
1944 /*
1945 * Handle dead keys in special conditions in other cases we let Windows
1946 * handle them and do not interfere.
1947 *
1948 * The dead_key flag must be reset on several occasions:
1949 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1950 * consumed at that point (This is when we let Windows combine the
1951 * dead character on its own)
1952 *
1953 * - Before doing something special such as regenerating keypresses to
1954 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001955 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001956 * immediately to _OnChar() (or _OnSysChar()).
1957 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001958
1959 /*
1960 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1961 * was handled by _WndProc, this keypress we want to process normally
1962 */
1963 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1964 dead_key = DEAD_KEY_OFF;
1965
1966 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001967 {
1968 /*
Anton Sharonov3f026672022-07-26 21:26:18 +01001969 * Expell the dead key pressed with Ctrl in a special way.
1970 *
1971 * After dead key was pressed with Ctrl in some cases, ESC was
1972 * artificially injected and handled by _OnChar(), now we are
1973 * dealing with completely new key press from the user. If we don't
1974 * do anything, ToUnicode() call will interpret this vk+scan_code
1975 * under influence of "dead-modifier". To prevent this we translate
1976 * this message replacing current char from user with VK_SPACE,
1977 * which will cause WM_CHAR with dead_key's character itself. Using
1978 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1979 * ignore this one WM_CHAR event completely. Afterwards (due to
1980 * usage of PostMessage), this procedure is scheduled to be called
1981 * again with user char and on next entry we will clean
1982 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1983 * outputDeadKey_rePost() since we do not wish to reset dead_key
1984 * value.
1985 */
1986 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1987 {
1988 outputDeadKey_rePost_Ex(msg,
1989 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
1990 return;
1991 }
1992
1993 if (dead_key != DEAD_KEY_SET_DEFAULT)
1994 {
1995 // should never happen - is there a way to make ASSERT here?
1996 return;
1997 }
1998
1999 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002000 * If a dead key was pressed and the user presses VK_SPACE,
2001 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2002 * with the dead char now, so do nothing special and let Windows
2003 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002004 *
2005 * Note that VK_SPACE combines with the dead_key's character and
2006 * only one WM_CHAR will be generated by TranslateMessage(), in
2007 * the two other cases two WM_CHAR will be generated: the dead
2008 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2009 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002010 */
2011 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2012 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002013 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002014 TranslateMessage(&msg);
2015 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002016 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002017 // In modes where we are not typing, dead keys should behave
2018 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002019 else if ((get_real_state()
2020 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002021 {
2022 outputDeadKey_rePost(msg);
2023 return;
2024 }
2025 }
2026
Bram Moolenaar734a8672019-12-02 22:49:38 +01002027 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002028 if (vk == VK_CANCEL)
2029 {
2030 trash_input_buf();
2031 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002032 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002033 string[0] = Ctrl_C;
2034 add_to_input_buf(string, 1);
2035 }
2036
LemonBoy77fc0b02022-04-22 22:45:52 +01002037 // This is an IME event or a synthetic keystroke, let Windows handle it.
2038 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2039 {
2040 TranslateMessage(&msg);
2041 return;
2042 }
2043
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002044 for (i = 0; special_keys[i].key_sym != 0; i++)
2045 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002046 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002047 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002048 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002049 {
2050 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002051 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002052 * is a key that would normally trigger the dead key nominal
2053 * character output (such as a NUMPAD printable character or
2054 * the TAB key, etc...).
2055 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002056 if (dead_key == DEAD_KEY_SET_DEFAULT
2057 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002058 || vk == VK_TAB || vk == CAR))
2059 {
2060 outputDeadKey_rePost(msg);
2061 return;
2062 }
2063
2064#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // Check for <F10>: Windows selects the menu. When <F10> is
2066 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002067 if (vk == VK_F10
2068 && gui.menu_is_active
2069 && check_map(k10, State, FALSE, TRUE, FALSE,
2070 NULL, NULL) == NULL)
2071 break;
2072#endif
LemonBoy45684c62022-04-24 15:46:42 +01002073 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074
2075 if (special_keys[i].vim_code1 == NUL)
2076 key = special_keys[i].vim_code0;
2077 else
2078 key = TO_SPECIAL(special_keys[i].vim_code0,
2079 special_keys[i].vim_code1);
2080 key = simplify_key(key, &modifiers);
2081 if (key == CSI)
2082 key = K_CSI;
2083
2084 if (modifiers)
2085 {
2086 string[0] = CSI;
2087 string[1] = KS_MODIFIER;
2088 string[2] = modifiers;
2089 add_to_input_buf(string, 3);
2090 }
2091
2092 if (IS_SPECIAL(key))
2093 {
2094 string[0] = CSI;
2095 string[1] = K_SECOND(key);
2096 string[2] = K_THIRD(key);
2097 add_to_input_buf(string, 3);
2098 }
2099 else
2100 {
2101 int len;
2102
Bram Moolenaar734a8672019-12-02 22:49:38 +01002103 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002104 len = char_to_string(key, string, 40, FALSE);
2105 add_to_input_buf(string, len);
2106 }
2107 break;
2108 }
2109 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002110
2111 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002112 if (special_keys[i].key_sym == 0)
2113 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002114 WCHAR ch[8];
2115 int len;
2116 int i;
2117 UINT scan_code;
2118
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002119 // Construct the state table with only a few modifiers, we don't
2120 // really care about the presence of Ctrl/Alt as those modifiers are
2121 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002122 memset(keyboard_state, 0, 256);
2123 if (GetKeyState(VK_SHIFT) & 0x8000)
2124 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002125 if (GetKeyState(VK_CAPITAL) & 0x0001)
2126 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002127 // Alt-Gr is synthesized as Alt + Ctrl.
2128 if ((GetKeyState(VK_RMENU) & 0x8000)
2129 && (GetKeyState(VK_CONTROL) & 0x8000))
2130 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002131 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002132 keyboard_state[VK_CONTROL] = 0x80;
2133 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002134
2135 // Translate the virtual key according to the current keyboard
2136 // layout.
2137 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2138 // Convert the scan-code into a sequence of zero or more unicode
2139 // codepoints.
2140 // If this is a dead key ToUnicode returns a negative value.
2141 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2142 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002143 if (len < 0)
2144 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002145
2146 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002147 {
2148 if ( dead_key == DEAD_KEY_SET_DEFAULT
2149 && (GetKeyState(VK_CONTROL) & 0x8000)
2150 && ( (vk == 221 && scan_code == 26) // AZERTY CTRL+dead_circumflex
2151 || (vk == 220 && scan_code == 41) // QWERTZ CTRL+dead_circumflex
2152 )
2153 )
2154 {
2155 // post WM_CHAR='[' - which will be interpreted with CTRL
2156 // stil hold as ESC
2157 PostMessageW(msg.hwnd, WM_CHAR, '[', msg.lParam);
2158 // ask _OnChar() to not touch this state, wait for next key
2159 // press and maintain knowledge that we are "poisoned" with
2160 // "dead state"
2161 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2162 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002163 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002164 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002165
2166 // Post the message as TranslateMessage would do.
2167 if (msg.message == WM_KEYDOWN)
2168 {
2169 for (i = 0; i < len; i++)
2170 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002171 }
2172 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002173 {
2174 for (i = 0; i < len; i++)
2175 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2176 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002177 }
2178 }
2179#ifdef FEAT_MBYTE_IME
2180 else if (msg.message == WM_IME_NOTIFY)
2181 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2182 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002183 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002184 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002185#endif
2186
2187#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002188 // Check for <F10>: Default effect is to select the menu. When <F10> is
2189 // mapped we need to stop it here to avoid strange effects (e.g., for the
2190 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002191 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2192 NULL, NULL) == NULL)
2193#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002194 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002195}
2196
2197/*
2198 * Catch up with any queued events. This may put keyboard input into the
2199 * input buffer, call resize call-backs, trigger timers etc. If there is
2200 * nothing in the event queue (& no timers pending), then we return
2201 * immediately.
2202 */
2203 void
2204gui_mch_update(void)
2205{
2206 MSG msg;
2207
2208 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002209 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002210 && !vim_is_input_buf_full())
2211 process_message();
2212}
2213
Bram Moolenaar4231da42016-06-02 14:30:04 +02002214 static void
2215remove_any_timer(void)
2216{
2217 MSG msg;
2218
2219 if (s_wait_timer != 0 && !s_timed_out)
2220 {
2221 KillTimer(NULL, s_wait_timer);
2222
Bram Moolenaar734a8672019-12-02 22:49:38 +01002223 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002224 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002225 ;
2226 s_wait_timer = 0;
2227 }
2228}
2229
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002230/*
2231 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2232 * from the keyboard.
2233 * wtime == -1 Wait forever.
2234 * wtime == 0 This should never happen.
2235 * wtime > 0 Wait wtime milliseconds for a character.
2236 * Returns OK if a character was found to be available within the given time,
2237 * or FAIL otherwise.
2238 */
2239 int
2240gui_mch_wait_for_chars(int wtime)
2241{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002242 int focus;
2243
2244 s_timed_out = FALSE;
2245
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002246 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002247 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002248 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249 if (s_busy_processing)
2250 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002251
2252 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002253 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2254 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002255 }
2256
2257 allow_scrollbar = TRUE;
2258
2259 focus = gui.in_focus;
2260 while (!s_timed_out)
2261 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002262 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263 if (gui.in_focus != focus)
2264 {
2265 if (gui.in_focus)
2266 gui_mch_start_blink();
2267 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002268 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002269 focus = gui.in_focus;
2270 }
2271
2272 if (s_need_activate)
2273 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002274 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275 s_need_activate = FALSE;
2276 }
2277
Bram Moolenaar4231da42016-06-02 14:30:04 +02002278#ifdef FEAT_TIMERS
2279 did_add_timer = FALSE;
2280#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002282 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002283 for (;;)
2284 {
2285 MSG msg;
2286
2287 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002288# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002289 if (did_add_timer)
2290 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002291# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002292 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002293 {
2294 process_message();
2295 break;
2296 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002297 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002298 // TODO: The 10 msec is a compromise between laggy response
2299 // and consuming more CPU time. Better would be to handle
2300 // channel messages when they arrive.
2301 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002302 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002303 break;
2304 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002305#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002306 // Don't use gui_mch_update() because then we will spin-lock until a
2307 // char arrives, instead we use GetMessage() to hang until an
2308 // event arrives. No need to check for input_buf_full because we are
2309 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002310 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002311#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002312
2313 if (input_available())
2314 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002315 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002316 allow_scrollbar = FALSE;
2317
Bram Moolenaar89c00032019-09-04 13:53:21 +02002318 // Clear pending mouse button, the release event may have been
2319 // taken by the dialog window. But don't do this when getting
2320 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002321 if (!s_getting_focus)
2322 s_button_pending = -1;
2323
2324 return OK;
2325 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002326
2327#ifdef FEAT_TIMERS
2328 if (did_add_timer)
2329 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002330 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002331 remove_any_timer();
2332 break;
2333 }
2334#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002335 }
2336 allow_scrollbar = FALSE;
2337 return FAIL;
2338}
2339
2340/*
2341 * Clear a rectangular region of the screen from text pos (row1, col1) to
2342 * (row2, col2) inclusive.
2343 */
2344 void
2345gui_mch_clear_block(
2346 int row1,
2347 int col1,
2348 int row2,
2349 int col2)
2350{
2351 RECT rc;
2352
2353 /*
2354 * Clear one extra pixel at the far right, for when bold characters have
2355 * spilled over to the window border.
2356 * Note: FillRect() excludes right and bottom of rectangle.
2357 */
2358 rc.left = FILL_X(col1);
2359 rc.top = FILL_Y(row1);
2360 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2361 rc.bottom = FILL_Y(row2 + 1);
2362 clear_rect(&rc);
2363}
2364
2365/*
2366 * Clear the whole text window.
2367 */
2368 void
2369gui_mch_clear_all(void)
2370{
2371 RECT rc;
2372
2373 rc.left = 0;
2374 rc.top = 0;
2375 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2376 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2377 clear_rect(&rc);
2378}
2379/*
2380 * Menu stuff.
2381 */
2382
2383 void
2384gui_mch_enable_menu(int flag)
2385{
2386#ifdef FEAT_MENU
2387 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2388#endif
2389}
2390
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002391 void
2392gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002393 int x UNUSED,
2394 int y UNUSED,
2395 int w UNUSED,
2396 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002397{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002398 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002399}
2400
2401#if defined(FEAT_MENU) || defined(PROTO)
2402/*
2403 * Make menu item hidden or not hidden
2404 */
2405 void
2406gui_mch_menu_hidden(
2407 vimmenu_T *menu,
2408 int hidden)
2409{
2410 /*
2411 * This doesn't do what we want. Hmm, just grey the menu items for now.
2412 */
2413 /*
2414 if (hidden)
2415 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2416 else
2417 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2418 */
2419 gui_mch_menu_grey(menu, hidden);
2420}
2421
2422/*
2423 * This is called after setting all the menus to grey/hidden or not.
2424 */
2425 void
2426gui_mch_draw_menubar(void)
2427{
2428 DrawMenuBar(s_hwnd);
2429}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002430#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002431
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002432/*
2433 * Return the RGB value of a pixel as a long.
2434 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002435 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002436gui_mch_get_rgb(guicolor_T pixel)
2437{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002438 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2439 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002440}
2441
2442#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002443/*
2444 * Convert pixels in X to dialog units
2445 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002446 static WORD
2447PixelToDialogX(int numPixels)
2448{
2449 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2450}
2451
Bram Moolenaar734a8672019-12-02 22:49:38 +01002452/*
2453 * Convert pixels in Y to dialog units
2454 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002455 static WORD
2456PixelToDialogY(int numPixels)
2457{
2458 return (WORD)((numPixels * 8) / s_dlgfntheight);
2459}
2460
Bram Moolenaar734a8672019-12-02 22:49:38 +01002461/*
2462 * Return the width in pixels of the given text in the given DC.
2463 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002464 static int
2465GetTextWidth(HDC hdc, char_u *str, int len)
2466{
2467 SIZE size;
2468
2469 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2470 return size.cx;
2471}
2472
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002473/*
2474 * Return the width in pixels of the given text in the given DC, taking care
2475 * of 'encoding' to active codepage conversion.
2476 */
2477 static int
2478GetTextWidthEnc(HDC hdc, char_u *str, int len)
2479{
2480 SIZE size;
2481 WCHAR *wstr;
2482 int n;
2483 int wlen = len;
2484
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002485 wstr = enc_to_utf16(str, &wlen);
2486 if (wstr == NULL)
2487 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002488
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002489 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2490 vim_free(wstr);
2491 if (n)
2492 return size.cx;
2493 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002494}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002496static void get_work_area(RECT *spi_rect);
2497
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002498/*
2499 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002500 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2501 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502 */
2503 static BOOL
2504CenterWindow(
2505 HWND hwndChild,
2506 HWND hwndParent)
2507{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002508 HMONITOR mon;
2509 MONITORINFO moninfo;
2510 RECT rChild, rParent, rScreen;
2511 int wChild, hChild, wParent, hParent;
2512 int xNew, yNew;
2513 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514
2515 GetWindowRect(hwndChild, &rChild);
2516 wChild = rChild.right - rChild.left;
2517 hChild = rChild.bottom - rChild.top;
2518
Bram Moolenaar734a8672019-12-02 22:49:38 +01002519 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002520 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002521 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522 else
2523 GetWindowRect(hwndParent, &rParent);
2524 wParent = rParent.right - rParent.left;
2525 hParent = rParent.bottom - rParent.top;
2526
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002527 moninfo.cbSize = sizeof(MONITORINFO);
2528 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2529 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002530 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002531 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002532 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002533 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002534 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002535 hdc = GetDC(hwndChild);
2536 rScreen.left = 0;
2537 rScreen.top = 0;
2538 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2539 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2540 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541 }
2542
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002543 xNew = rParent.left + ((wParent - wChild) / 2);
2544 if (xNew < rScreen.left)
2545 xNew = rScreen.left;
2546 else if ((xNew + wChild) > rScreen.right)
2547 xNew = rScreen.right - wChild;
2548
2549 yNew = rParent.top + ((hParent - hChild) / 2);
2550 if (yNew < rScreen.top)
2551 yNew = rScreen.top;
2552 else if ((yNew + hChild) > rScreen.bottom)
2553 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002554
2555 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2556 SWP_NOSIZE | SWP_NOZORDER);
2557}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002558#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002559
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002560#if defined(FEAT_TOOLBAR) || defined(PROTO)
2561 void
2562gui_mch_show_toolbar(int showit)
2563{
2564 if (s_toolbarhwnd == NULL)
2565 return;
2566
2567 if (showit)
2568 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002569 // Enable unicode support
2570 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2571 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002572 ShowWindow(s_toolbarhwnd, SW_SHOW);
2573 }
2574 else
2575 ShowWindow(s_toolbarhwnd, SW_HIDE);
2576}
2577
Bram Moolenaar734a8672019-12-02 22:49:38 +01002578// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002579# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002580
2581#endif
2582
2583#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2584 static void
2585add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2586{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002587 WCHAR *wn;
2588 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002589
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002590 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002591 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002592 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002593
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002594 infow.cbSize = sizeof(infow);
2595 infow.fMask = MIIM_TYPE | MIIM_ID;
2596 infow.wID = item_id;
2597 infow.fType = MFT_STRING;
2598 infow.dwTypeData = wn;
2599 infow.cch = (UINT)wcslen(wn);
2600 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2601 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002602}
2603
2604 static void
2605show_tabline_popup_menu(void)
2606{
2607 HMENU tab_pmenu;
2608 long rval;
2609 POINT pt;
2610
Bram Moolenaar734a8672019-12-02 22:49:38 +01002611 // When ignoring events don't show the menu.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002612 if (hold_gui_events
2613# ifdef FEAT_CMDWIN
2614 || cmdwin_type != 0
2615# endif
2616 )
2617 return;
2618
2619 tab_pmenu = CreatePopupMenu();
2620 if (tab_pmenu == NULL)
2621 return;
2622
2623 if (first_tabpage->tp_next != NULL)
2624 add_tabline_popup_menu_entry(tab_pmenu,
2625 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2626 add_tabline_popup_menu_entry(tab_pmenu,
2627 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2628 add_tabline_popup_menu_entry(tab_pmenu,
2629 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2630
2631 GetCursorPos(&pt);
2632 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2633 NULL);
2634
2635 DestroyMenu(tab_pmenu);
2636
Bram Moolenaar734a8672019-12-02 22:49:38 +01002637 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002638 if (rval > 0)
2639 {
2640 TCHITTESTINFO htinfo;
2641 int idx;
2642
2643 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2644 return;
2645
2646 htinfo.pt.x = pt.x;
2647 htinfo.pt.y = pt.y;
2648 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2649 if (idx == -1)
2650 idx = 0;
2651 else
2652 idx += 1;
2653
2654 send_tabline_menu_event(idx, (int)rval);
2655 }
2656}
2657
2658/*
2659 * Show or hide the tabline.
2660 */
2661 void
2662gui_mch_show_tabline(int showit)
2663{
2664 if (s_tabhwnd == NULL)
2665 return;
2666
2667 if (!showit != !showing_tabline)
2668 {
2669 if (showit)
2670 ShowWindow(s_tabhwnd, SW_SHOW);
2671 else
2672 ShowWindow(s_tabhwnd, SW_HIDE);
2673 showing_tabline = showit;
2674 }
2675}
2676
2677/*
2678 * Return TRUE when tabline is displayed.
2679 */
2680 int
2681gui_mch_showing_tabline(void)
2682{
2683 return s_tabhwnd != NULL && showing_tabline;
2684}
2685
2686/*
2687 * Update the labels of the tabline.
2688 */
2689 void
2690gui_mch_update_tabline(void)
2691{
2692 tabpage_T *tp;
2693 TCITEM tie;
2694 int nr = 0;
2695 int curtabidx = 0;
2696 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002697 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002698
2699 if (s_tabhwnd == NULL)
2700 return;
2701
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002702 // Enable unicode support
2703 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002704
2705 tie.mask = TCIF_TEXT;
2706 tie.iImage = -1;
2707
Bram Moolenaar734a8672019-12-02 22:49:38 +01002708 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002709 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2710
Bram Moolenaar734a8672019-12-02 22:49:38 +01002711 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2713 {
2714 if (tp == curtab)
2715 curtabidx = nr;
2716
2717 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2718 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002719 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002720 tie.pszText = "-Empty-";
2721 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2722 tabadded = 1;
2723 }
2724
2725 get_tabline_label(tp, FALSE);
2726 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002727
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002728 wstr = enc_to_utf16(NameBuff, NULL);
2729 if (wstr != NULL)
2730 {
2731 TCITEMW tiw;
2732
2733 tiw.mask = TCIF_TEXT;
2734 tiw.iImage = -1;
2735 tiw.pszText = wstr;
2736 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2737 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002738 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739 }
2740
Bram Moolenaar734a8672019-12-02 22:49:38 +01002741 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2743 TabCtrl_DeleteItem(s_tabhwnd, nr);
2744
2745 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2746 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2747
Bram Moolenaar734a8672019-12-02 22:49:38 +01002748 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002749 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2750 RedrawWindow(s_tabhwnd, NULL, NULL,
2751 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2752
2753 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2754 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2755}
2756
2757/*
2758 * Set the current tab to "nr". First tab is 1.
2759 */
2760 void
2761gui_mch_set_curtab(int nr)
2762{
2763 if (s_tabhwnd == NULL)
2764 return;
2765
2766 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2767 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2768}
2769
2770#endif
2771
2772/*
2773 * ":simalt" command.
2774 */
2775 void
2776ex_simalt(exarg_T *eap)
2777{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002778 char_u *keys = eap->arg;
2779 int fill_typebuf = FALSE;
2780 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002781
2782 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2783 while (*keys)
2784 {
2785 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002786 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002787 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2788 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002789 fill_typebuf = TRUE;
2790 }
2791 if (fill_typebuf)
2792 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002793 // Put a NOP in the typeahead buffer so that the message will get
2794 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002795 key_name[0] = K_SPECIAL;
2796 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002797 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002798 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002799#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002800 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002801#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002802 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002803 }
2804}
2805
2806/*
2807 * Create the find & replace dialogs.
2808 * You can't have both at once: ":find" when replace is showing, destroys
2809 * the replace dialog first, and the other way around.
2810 */
2811#ifdef MSWIN_FIND_REPLACE
2812 static void
2813initialise_findrep(char_u *initial_string)
2814{
2815 int wword = FALSE;
2816 int mcase = !p_ic;
2817 char_u *entry_text;
2818
Bram Moolenaar734a8672019-12-02 22:49:38 +01002819 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002820 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2821
2822 s_findrep_struct.hwndOwner = s_hwnd;
2823 s_findrep_struct.Flags = FR_DOWN;
2824 if (mcase)
2825 s_findrep_struct.Flags |= FR_MATCHCASE;
2826 if (wword)
2827 s_findrep_struct.Flags |= FR_WHOLEWORD;
2828 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002829 {
2830 WCHAR *p = enc_to_utf16(entry_text, NULL);
2831 if (p != NULL)
2832 {
2833 int len = s_findrep_struct.wFindWhatLen - 1;
2834
2835 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2836 s_findrep_struct.lpstrFindWhat[len] = NUL;
2837 vim_free(p);
2838 }
2839 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002840 vim_free(entry_text);
2841}
2842#endif
2843
2844 static void
2845set_window_title(HWND hwnd, char *title)
2846{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002847 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002848 {
2849 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002850
Bram Moolenaar734a8672019-12-02 22:49:38 +01002851 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2853 if (wbuf != NULL)
2854 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002855 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002856 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002857 }
2858 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002859 else
2860 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002861}
2862
2863 void
2864gui_mch_find_dialog(exarg_T *eap)
2865{
2866#ifdef MSWIN_FIND_REPLACE
2867 if (s_findrep_msg != 0)
2868 {
2869 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2870 DestroyWindow(s_findrep_hwnd);
2871
2872 if (!IsWindow(s_findrep_hwnd))
2873 {
2874 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002875 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876 }
2877
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002878 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 (void)SetFocus(s_findrep_hwnd);
2880
2881 s_findrep_is_find = TRUE;
2882 }
2883#endif
2884}
2885
2886
2887 void
2888gui_mch_replace_dialog(exarg_T *eap)
2889{
2890#ifdef MSWIN_FIND_REPLACE
2891 if (s_findrep_msg != 0)
2892 {
2893 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2894 DestroyWindow(s_findrep_hwnd);
2895
2896 if (!IsWindow(s_findrep_hwnd))
2897 {
2898 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002899 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900 }
2901
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002902 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 (void)SetFocus(s_findrep_hwnd);
2904
2905 s_findrep_is_find = FALSE;
2906 }
2907#endif
2908}
2909
2910
2911/*
2912 * Set visibility of the pointer.
2913 */
2914 void
2915gui_mch_mousehide(int hide)
2916{
2917 if (hide != gui.pointer_hidden)
2918 {
2919 ShowCursor(!hide);
2920 gui.pointer_hidden = hide;
2921 }
2922}
2923
2924#ifdef FEAT_MENU
2925 static void
2926gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2927{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002928 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002929 gui_mch_mousehide(FALSE);
2930
2931 (void)TrackPopupMenu(
2932 (HMENU)menu->submenu_id,
2933 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2934 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002935 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002936 s_hwnd,
2937 NULL);
2938 /*
2939 * NOTE: The pop-up menu can eat the mouse up event.
2940 * We deal with this in normal.c.
2941 */
2942}
2943#endif
2944
2945/*
2946 * Got a message when the system will go down.
2947 */
2948 static void
2949_OnEndSession(void)
2950{
2951 getout_preserve_modified(1);
2952}
2953
2954/*
2955 * Get this message when the user clicks on the cross in the top right corner
2956 * of a Windows95 window.
2957 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002958 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002959_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960{
2961 gui_shell_closed();
2962}
2963
2964/*
2965 * Get a message when the window is being destroyed.
2966 */
2967 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002968_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002969{
2970 if (!destroying)
2971 _OnClose(hwnd);
2972}
2973
2974 static void
2975_OnPaint(
2976 HWND hwnd)
2977{
2978 if (!IsMinimized(hwnd))
2979 {
2980 PAINTSTRUCT ps;
2981
Bram Moolenaar734a8672019-12-02 22:49:38 +01002982 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002983 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984
Bram Moolenaar734a8672019-12-02 22:49:38 +01002985 // prevent multi-byte characters from misprinting on an invalid
2986 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987 if (has_mbyte)
2988 {
2989 RECT rect;
2990
2991 GetClientRect(hwnd, &rect);
2992 ps.rcPaint.left = rect.left;
2993 ps.rcPaint.right = rect.right;
2994 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995
2996 if (!IsRectEmpty(&ps.rcPaint))
2997 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002998 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
2999 ps.rcPaint.right - ps.rcPaint.left + 1,
3000 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3001 }
3002
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003003 EndPaint(hwnd, &ps);
3004 }
3005}
3006
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003007 static void
3008_OnSize(
3009 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003010 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003011 int cx,
3012 int cy)
3013{
K.Takatac81e9bf2022-01-16 14:15:49 +00003014 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015 {
3016 gui_resize_shell(cx, cy);
3017
Bram Moolenaar734a8672019-12-02 22:49:38 +01003018 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003020 }
3021}
3022
3023 static void
3024_OnSetFocus(
3025 HWND hwnd,
3026 HWND hwndOldFocus)
3027{
3028 gui_focus_change(TRUE);
3029 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003030 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003031}
3032
3033 static void
3034_OnKillFocus(
3035 HWND hwnd,
3036 HWND hwndNewFocus)
3037{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003038 if (destroying)
3039 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003040 gui_focus_change(FALSE);
3041 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003042 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043}
3044
3045/*
3046 * Get a message when the user switches back to vim
3047 */
3048 static LRESULT
3049_OnActivateApp(
3050 HWND hwnd,
3051 BOOL fActivate,
3052 DWORD dwThreadId)
3053{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003054 // we call gui_focus_change() in _OnSetFocus()
3055 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003056 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003057}
3058
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003059 void
3060gui_mch_destroy_scrollbar(scrollbar_T *sb)
3061{
3062 DestroyWindow(sb->id);
3063}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003064
3065/*
3066 * Get current mouse coordinates in text window.
3067 */
3068 void
3069gui_mch_getmouse(int *x, int *y)
3070{
3071 RECT rct;
3072 POINT mp;
3073
3074 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003075 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076 *x = (int)(mp.x - rct.left);
3077 *y = (int)(mp.y - rct.top);
3078}
3079
3080/*
3081 * Move mouse pointer to character at (x, y).
3082 */
3083 void
3084gui_mch_setmouse(int x, int y)
3085{
3086 RECT rct;
3087
3088 (void)GetWindowRect(s_textArea, &rct);
3089 (void)SetCursorPos(x + gui.border_offset + rct.left,
3090 y + gui.border_offset + rct.top);
3091}
3092
3093 static void
3094gui_mswin_get_valid_dimensions(
3095 int w,
3096 int h,
3097 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003098 int *valid_h,
3099 int *cols,
3100 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003101{
3102 int base_width, base_height;
3103
3104 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003105 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3106 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003107 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003108 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3109 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3110 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3111 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003112 *cols = (w - base_width) / gui.char_width;
3113 *rows = (h - base_height) / gui.char_height;
3114 *valid_w = base_width + *cols * gui.char_width;
3115 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003116}
3117
3118 void
3119gui_mch_flash(int msec)
3120{
3121 RECT rc;
3122
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003123#if defined(FEAT_DIRECTX)
3124 if (IS_ENABLE_DIRECTX())
3125 DWriteContext_Flush(s_dwc);
3126#endif
3127
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003128 /*
3129 * Note: InvertRect() excludes right and bottom of rectangle.
3130 */
3131 rc.left = 0;
3132 rc.top = 0;
3133 rc.right = gui.num_cols * gui.char_width;
3134 rc.bottom = gui.num_rows * gui.char_height;
3135 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003136 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003137
Bram Moolenaar734a8672019-12-02 22:49:38 +01003138 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003139
3140 InvertRect(s_hdc, &rc);
3141}
3142
3143/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003144 * Check if the specified point is on-screen. (multi-monitor aware)
3145 */
3146 static BOOL
3147is_point_onscreen(int x, int y)
3148{
3149 POINT pt = {x, y};
3150
3151 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3152}
3153
3154/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003155 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003156 *
3157 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3158 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3159 * only works when the whole window is on-screen.
3160 */
3161 static BOOL
3162is_window_onscreen(HWND hwnd)
3163{
3164 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003165 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003166
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003167 GetClientRect(hwnd, &rc);
3168 p1.x = rc.left;
3169 p1.y = rc.top;
3170 p2.x = rc.right - 1;
3171 p2.y = rc.bottom - 1;
3172 ClientToScreen(hwnd, &p1);
3173 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003174
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003175 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003176 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003177 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003178 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003179 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003180 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003181 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003182 return FALSE;
3183 return TRUE;
3184}
3185
3186/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003187 * Return flags used for scrolling.
3188 * The SW_INVALIDATE is required when part of the window is covered or
3189 * off-screen. Refer to MS KB Q75236.
3190 */
3191 static int
3192get_scroll_flags(void)
3193{
3194 HWND hwnd;
3195 RECT rcVim, rcOther, rcDest;
3196
Bram Moolenaar185577e2020-10-29 20:08:21 +01003197 // Check if the window is (partly) off-screen.
3198 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 return SW_INVALIDATE;
3200
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003201 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003202 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003203 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3204 if (IsWindowVisible(hwnd))
3205 {
3206 GetWindowRect(hwnd, &rcOther);
3207 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3208 return SW_INVALIDATE;
3209 }
3210 return 0;
3211}
3212
3213/*
3214 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3215 * may not be scrolled out properly.
3216 * For gVim, when _OnScroll() is repeated, the character at the
3217 * previous cursor position may be left drawn after scroll.
3218 * The problem can be avoided by calling GetPixel() to get a pixel in
3219 * the region before ScrollWindowEx().
3220 */
3221 static void
3222intel_gpu_workaround(void)
3223{
3224 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3225}
3226
3227/*
3228 * Delete the given number of lines from the given row, scrolling up any
3229 * text further down within the scroll region.
3230 */
3231 void
3232gui_mch_delete_lines(
3233 int row,
3234 int num_lines)
3235{
3236 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003237
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003238 rc.left = FILL_X(gui.scroll_region_left);
3239 rc.right = FILL_X(gui.scroll_region_right + 1);
3240 rc.top = FILL_Y(row);
3241 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3242
Bram Moolenaar92467d32017-12-05 13:22:16 +01003243#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003244 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003245 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003246 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003247 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003248 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003249#endif
3250 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003251#if defined(FEAT_DIRECTX)
3252 if (IS_ENABLE_DIRECTX())
3253 DWriteContext_Flush(s_dwc);
3254#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003255 intel_gpu_workaround();
3256 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003257 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003258 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003259 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260
Bram Moolenaar734a8672019-12-02 22:49:38 +01003261 // This seems to be required to avoid the cursor disappearing when
3262 // scrolling such that the cursor ends up in the top-left character on
3263 // the screen... But why? (Webb)
3264 // It's probably fixed by disabling drawing the cursor while scrolling.
3265 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003266
3267 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3268 gui.scroll_region_left,
3269 gui.scroll_region_bot, gui.scroll_region_right);
3270}
3271
3272/*
3273 * Insert the given number of lines before the given row, scrolling down any
3274 * following text within the scroll region.
3275 */
3276 void
3277gui_mch_insert_lines(
3278 int row,
3279 int num_lines)
3280{
3281 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003282
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003283 rc.left = FILL_X(gui.scroll_region_left);
3284 rc.right = FILL_X(gui.scroll_region_right + 1);
3285 rc.top = FILL_Y(row);
3286 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003287
3288#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003289 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003290 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003291 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003292 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003293 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003294#endif
3295 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003296#if defined(FEAT_DIRECTX)
3297 if (IS_ENABLE_DIRECTX())
3298 DWriteContext_Flush(s_dwc);
3299#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003300 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003301 // The SW_INVALIDATE is required when part of the window is covered or
3302 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003303 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003304 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003305 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003306 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003308 gui_clear_block(row, gui.scroll_region_left,
3309 row + num_lines - 1, gui.scroll_region_right);
3310}
3311
3312
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003313 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003314gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003315{
3316#if defined(FEAT_DIRECTX)
3317 DWriteContext_Close(s_dwc);
3318 DWrite_Final();
3319 s_dwc = NULL;
3320#endif
3321
3322 ReleaseDC(s_textArea, s_hdc);
3323 DeleteObject(s_brush);
3324
3325#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003326 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3328#endif
3329
Bram Moolenaar734a8672019-12-02 22:49:38 +01003330 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331 if (s_hwnd != NULL)
3332 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003333 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334 DestroyWindow(s_hwnd);
3335 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003336}
3337
3338 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003339logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003340{
3341 char *p;
3342 char *res;
3343 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003344 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003345 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003346 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003347
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003348 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3349 if (font_name == NULL)
3350 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003352 quality_name = quality_id2name((int)lf.lfQuality);
3353
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003354 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003355 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003356 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003357 if (res != NULL)
3358 {
3359 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003360 // make a normal font string out of the lf thing:
3361 points = pixels_to_points(
3362 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3363 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3364 sprintf((char *)p, "%s:h%d", font_name, points);
3365 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003366 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003367 while (*p)
3368 {
3369 if (*p == ' ')
3370 *p = '_';
3371 ++p;
3372 }
3373 if (lf.lfItalic)
3374 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003375 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003376 STRCAT(p, ":b");
3377 if (lf.lfUnderline)
3378 STRCAT(p, ":u");
3379 if (lf.lfStrikeOut)
3380 STRCAT(p, ":s");
3381 if (charset_name != NULL)
3382 {
3383 STRCAT(p, ":c");
3384 STRCAT(p, charset_name);
3385 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003386 if (quality_name != NULL)
3387 {
3388 STRCAT(p, ":q");
3389 STRCAT(p, quality_name);
3390 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 }
3392
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003393 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 return (char_u *)res;
3395}
3396
3397
3398#ifdef FEAT_MBYTE_IME
3399/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003400 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003401 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003402 */
3403 static void
3404update_im_font(void)
3405{
K.Takatac81e9bf2022-01-16 14:15:49 +00003406 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003407
3408 if (p_guifontwide != NULL && *p_guifontwide != NUL
3409 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003410 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003411 norm_logfont = lf_wide;
3412 else
3413 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003414
3415 lf = norm_logfont;
3416 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3417 // Work around when PerMonitorV2 is not enabled in the process level.
3418 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3419 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003420}
3421#endif
3422
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423/*
3424 * Handler of gui.wide_font (p_guifontwide) changed notification.
3425 */
3426 void
3427gui_mch_wide_font_changed(void)
3428{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003429 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003430
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003431#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003433#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003434
3435 gui_mch_free_font(gui.wide_ital_font);
3436 gui.wide_ital_font = NOFONT;
3437 gui_mch_free_font(gui.wide_bold_font);
3438 gui.wide_bold_font = NOFONT;
3439 gui_mch_free_font(gui.wide_boldital_font);
3440 gui.wide_boldital_font = NOFONT;
3441
3442 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003443 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444 {
3445 if (!lf.lfItalic)
3446 {
3447 lf.lfItalic = TRUE;
3448 gui.wide_ital_font = get_font_handle(&lf);
3449 lf.lfItalic = FALSE;
3450 }
3451 if (lf.lfWeight < FW_BOLD)
3452 {
3453 lf.lfWeight = FW_BOLD;
3454 gui.wide_bold_font = get_font_handle(&lf);
3455 if (!lf.lfItalic)
3456 {
3457 lf.lfItalic = TRUE;
3458 gui.wide_boldital_font = get_font_handle(&lf);
3459 }
3460 }
3461 }
3462}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003463
3464/*
3465 * Initialise vim to use the font with the given name.
3466 * Return FAIL if the font could not be loaded, OK otherwise.
3467 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003468 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003469gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003470{
K.Takatac81e9bf2022-01-16 14:15:49 +00003471 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003472 GuiFont font = NOFONT;
3473 char_u *p;
3474
Bram Moolenaar734a8672019-12-02 22:49:38 +01003475 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003476 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003477 {
3478 lfOrig = lf;
3479 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003481 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482 if (font == NOFONT)
3483 return FAIL;
3484
3485 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003486 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003487#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488 norm_logfont = lf;
3489 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003490 if (!s_in_dpichanged)
3491 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492#endif
3493 gui_mch_free_font(gui.norm_font);
3494 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003495 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496 GetFontSize(font);
3497
K.Takatac81e9bf2022-01-16 14:15:49 +00003498 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003499 if (p != NULL)
3500 {
3501 hl_set_font_name(p);
3502
Bram Moolenaar734a8672019-12-02 22:49:38 +01003503 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3505 {
3506 vim_free(p_guifont);
3507 p_guifont = p;
3508 }
3509 else
3510 vim_free(p);
3511 }
3512
3513 gui_mch_free_font(gui.ital_font);
3514 gui.ital_font = NOFONT;
3515 gui_mch_free_font(gui.bold_font);
3516 gui.bold_font = NOFONT;
3517 gui_mch_free_font(gui.boldital_font);
3518 gui.boldital_font = NOFONT;
3519
3520 if (!lf.lfItalic)
3521 {
3522 lf.lfItalic = TRUE;
3523 gui.ital_font = get_font_handle(&lf);
3524 lf.lfItalic = FALSE;
3525 }
3526 if (lf.lfWeight < FW_BOLD)
3527 {
3528 lf.lfWeight = FW_BOLD;
3529 gui.bold_font = get_font_handle(&lf);
3530 if (!lf.lfItalic)
3531 {
3532 lf.lfItalic = TRUE;
3533 gui.boldital_font = get_font_handle(&lf);
3534 }
3535 }
3536
3537 return OK;
3538}
3539
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003540/*
3541 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003542 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543 */
3544 int
3545gui_mch_maximized(void)
3546{
3547 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003548 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003549
3550 wp.length = sizeof(WINDOWPLACEMENT);
3551 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003552 {
3553 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003554 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003555 && wp.flags == WPF_RESTORETOMAXIMIZED))
3556 return TRUE;
3557 if (wp.showCmd == SW_SHOWMINIMIZED)
3558 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003559
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003560 // Assume the window is snapped when the sizes from two APIs differ.
3561 GetWindowRect(s_hwnd, &rc);
3562 if ((rc.right - rc.left !=
3563 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3564 || (rc.bottom - rc.top !=
3565 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3566 return TRUE;
3567 }
3568 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003569}
3570
3571/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003572 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3573 * is set. Compute the new Rows and Columns. This is like resizing the
3574 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003575 */
3576 void
3577gui_mch_newfont(void)
3578{
3579 RECT rect;
3580
3581 GetWindowRect(s_hwnd, &rect);
3582 if (win_socket_id == 0)
3583 {
3584 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003585 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3586 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003588 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3589 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3590 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3591 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003592 }
3593 else
3594 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003595 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003596 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003597 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003598 }
3599}
3600
3601/*
3602 * Set the window title
3603 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604 void
3605gui_mch_settitle(
3606 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003607 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003608{
3609 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3610}
3611
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003612#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003613// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3614// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003615static LPCSTR mshape_idcs[] =
3616{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003617 IDC_ARROW, // arrow
3618 MAKEINTRESOURCE(0), // blank
3619 IDC_IBEAM, // beam
3620 IDC_SIZENS, // updown
3621 IDC_SIZENS, // udsizing
3622 IDC_SIZEWE, // leftright
3623 IDC_SIZEWE, // lrsizing
3624 IDC_WAIT, // busy
3625 IDC_NO, // no
3626 IDC_ARROW, // crosshair
3627 IDC_ARROW, // hand1
3628 IDC_ARROW, // hand2
3629 IDC_ARROW, // pencil
3630 IDC_ARROW, // question
3631 IDC_ARROW, // right-arrow
3632 IDC_UPARROW, // up-arrow
3633 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003634};
3635
3636 void
3637mch_set_mouse_shape(int shape)
3638{
3639 LPCSTR idc;
3640
3641 if (shape == MSHAPE_HIDE)
3642 ShowCursor(FALSE);
3643 else
3644 {
3645 if (shape >= MSHAPE_NUMBERED)
3646 idc = IDC_ARROW;
3647 else
3648 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003649 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003650 if (!p_mh)
3651 {
3652 POINT mp;
3653
Bram Moolenaar734a8672019-12-02 22:49:38 +01003654 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003655 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003656 (void)SetCursorPos(mp.x, mp.y);
3657 ShowCursor(TRUE);
3658 }
3659 }
3660}
3661#endif
3662
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003663#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003664/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003665 * Wide version of convert_filter().
3666 */
3667 static WCHAR *
3668convert_filterW(char_u *s)
3669{
3670 char_u *tmp;
3671 int len;
3672 WCHAR *res;
3673
3674 tmp = convert_filter(s);
3675 if (tmp == NULL)
3676 return NULL;
3677 len = (int)STRLEN(s) + 3;
3678 res = enc_to_utf16(tmp, &len);
3679 vim_free(tmp);
3680 return res;
3681}
3682
3683/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003684 * Pop open a file browser and return the file selected, in allocated memory,
3685 * or NULL if Cancel is hit.
3686 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3687 * title - Title message for the file browser dialog.
3688 * dflt - Default name of file.
3689 * ext - Default extension to be added to files without extensions.
3690 * initdir - directory in which to open the browser (NULL = current dir)
3691 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003692 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003693 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003694gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 int saving,
3696 char_u *title,
3697 char_u *dflt,
3698 char_u *ext,
3699 char_u *initdir,
3700 char_u *filter)
3701{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003702 // We always use the wide function. This means enc_to_utf16() must work,
3703 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 OPENFILENAMEW fileStruct;
3705 WCHAR fileBuf[MAXPATHL];
3706 WCHAR *wp;
3707 int i;
3708 WCHAR *titlep = NULL;
3709 WCHAR *extp = NULL;
3710 WCHAR *initdirp = NULL;
3711 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003712 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003713 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003714
3715 if (dflt == NULL)
3716 fileBuf[0] = NUL;
3717 else
3718 {
3719 wp = enc_to_utf16(dflt, NULL);
3720 if (wp == NULL)
3721 fileBuf[0] = NUL;
3722 else
3723 {
3724 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3725 fileBuf[i] = wp[i];
3726 fileBuf[i] = NUL;
3727 vim_free(wp);
3728 }
3729 }
3730
Bram Moolenaar734a8672019-12-02 22:49:38 +01003731 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003732 filterp = convert_filterW(filter);
3733
Bram Moolenaara80faa82020-04-12 19:37:17 +02003734 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003735# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003736 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003737 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003738# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003739 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003740# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003741
3742 if (title != NULL)
3743 titlep = enc_to_utf16(title, NULL);
3744 fileStruct.lpstrTitle = titlep;
3745
3746 if (ext != NULL)
3747 extp = enc_to_utf16(ext, NULL);
3748 fileStruct.lpstrDefExt = extp;
3749
3750 fileStruct.lpstrFile = fileBuf;
3751 fileStruct.nMaxFile = MAXPATHL;
3752 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003753 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3754 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003755 if (initdir != NULL && *initdir != NUL)
3756 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003757 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 initdirp = enc_to_utf16(initdir, NULL);
3759 if (initdirp != NULL)
3760 {
3761 for (wp = initdirp; *wp != NUL; ++wp)
3762 if (*wp == '/')
3763 *wp = '\\';
3764 }
3765 fileStruct.lpstrInitialDir = initdirp;
3766 }
3767
3768 /*
3769 * TODO: Allow selection of multiple files. Needs another arg to this
3770 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3771 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3772 * files that don't exist yet, so I haven't put it in. What about
3773 * OFN_PATHMUSTEXIST?
3774 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3775 */
3776 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003777# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003778 if (curbuf->b_p_bin)
3779 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003780# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003782 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003783 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003784 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003785
3786 vim_free(filterp);
3787 vim_free(initdirp);
3788 vim_free(titlep);
3789 vim_free(extp);
3790
K.Takata14b8d6a2022-01-20 15:05:22 +00003791 if (!ret)
3792 return NULL;
3793
3794 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003796 if (p == NULL)
3797 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798
Bram Moolenaar734a8672019-12-02 22:49:38 +01003799 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003800 SetFocus(s_hwnd);
3801
Bram Moolenaar734a8672019-12-02 22:49:38 +01003802 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003803 q = vim_strsave(shorten_fname1(p));
3804 vim_free(p);
3805 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003806}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807
3808
3809/*
3810 * Convert the string s to the proper format for a filter string by replacing
3811 * the \t and \n delimiters with \0.
3812 * Returns the converted string in allocated memory.
3813 *
3814 * Keep in sync with convert_filterW() above!
3815 */
3816 static char_u *
3817convert_filter(char_u *s)
3818{
3819 char_u *res;
3820 unsigned s_len = (unsigned)STRLEN(s);
3821 unsigned i;
3822
3823 res = alloc(s_len + 3);
3824 if (res != NULL)
3825 {
3826 for (i = 0; i < s_len; ++i)
3827 if (s[i] == '\t' || s[i] == '\n')
3828 res[i] = '\0';
3829 else
3830 res[i] = s[i];
3831 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003832 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003833 res[s_len + 1] = NUL;
3834 res[s_len + 2] = NUL;
3835 }
3836 return res;
3837}
3838
3839/*
3840 * Select a directory.
3841 */
3842 char_u *
3843gui_mch_browsedir(char_u *title, char_u *initdir)
3844{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003845 // We fake this: Use a filter that doesn't select anything and a default
3846 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003847 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3848 initdir, (char_u *)_("Directory\t*.nothing\n"));
3849}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003850#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003851
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003852 static void
3853_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003854 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 HDROP hDrop)
3856{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003857#define BUFPATHLEN _MAX_PATH
3858#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003860 char szFile[BUFPATHLEN];
3861 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3862 UINT i;
3863 char_u **fnames;
3864 POINT pt;
3865 int_u modifiers = 0;
3866
Bram Moolenaar734a8672019-12-02 22:49:38 +01003867 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003868 DragQueryPoint(hDrop, &pt);
3869 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3870
3871 reset_VIsual();
3872
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003873 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003874
3875 if (fnames != NULL)
3876 for (i = 0; i < cFiles; ++i)
3877 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003878 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3879 fnames[i] = utf16_to_enc(wszFile, NULL);
3880 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003881 {
3882 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3883 fnames[i] = vim_strsave((char_u *)szFile);
3884 }
3885 }
3886
3887 DragFinish(hDrop);
3888
3889 if (fnames != NULL)
3890 {
LemonBoy45684c62022-04-24 15:46:42 +01003891 int kbd_modifiers = get_active_modifiers();
3892
3893 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003894 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003895 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003896 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003897 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898 modifiers |= MOUSE_ALT;
3899
3900 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3901
3902 s_need_activate = TRUE;
3903 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003904}
3905
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 static int
3907_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003908 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909 HWND hwndCtl,
3910 UINT code,
3911 int pos)
3912{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003913 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003914 scrollbar_T *sb, *sb_info;
3915 long val;
3916 int dragging = FALSE;
3917 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918 SCROLLINFO si;
3919
3920 si.cbSize = sizeof(si);
3921 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003922
3923 sb = gui_mswin_find_scrollbar(hwndCtl);
3924 if (sb == NULL)
3925 return 0;
3926
Bram Moolenaar734a8672019-12-02 22:49:38 +01003927 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928 {
3929 /*
3930 * Careful: need to get scrollbar info out of first (left) scrollbar
3931 * for window, but keep real scrollbar too because we must pass it to
3932 * gui_drag_scrollbar().
3933 */
3934 sb_info = &sb->wp->w_scrollbars[0];
3935 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003936 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003937 sb_info = sb;
3938 val = sb_info->value;
3939
3940 switch (code)
3941 {
3942 case SB_THUMBTRACK:
3943 val = pos;
3944 dragging = TRUE;
3945 if (sb->scroll_shift > 0)
3946 val <<= sb->scroll_shift;
3947 break;
3948 case SB_LINEDOWN:
3949 val++;
3950 break;
3951 case SB_LINEUP:
3952 val--;
3953 break;
3954 case SB_PAGEDOWN:
3955 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3956 break;
3957 case SB_PAGEUP:
3958 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3959 break;
3960 case SB_TOP:
3961 val = 0;
3962 break;
3963 case SB_BOTTOM:
3964 val = sb_info->max;
3965 break;
3966 case SB_ENDSCROLL:
3967 if (prev_code == SB_THUMBTRACK)
3968 {
3969 /*
3970 * "pos" only gives us 16-bit data. In case of large file,
3971 * use GetScrollPos() which returns 32-bit. Unfortunately it
3972 * is not valid while the scrollbar is being dragged.
3973 */
3974 val = GetScrollPos(hwndCtl, SB_CTL);
3975 if (sb->scroll_shift > 0)
3976 val <<= sb->scroll_shift;
3977 }
3978 break;
3979
3980 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003981 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003982 return 0;
3983 }
3984 prev_code = code;
3985
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003986 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3987 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003988
3989 /*
3990 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3991 */
3992 if (sb->wp != NULL)
3993 {
3994 scrollbar_T *sba = sb->wp->w_scrollbars;
3995 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3996
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003997 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003998 }
3999
Bram Moolenaar734a8672019-12-02 22:49:38 +01004000 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004001 s_busy_processing = TRUE;
4002
Bram Moolenaar734a8672019-12-02 22:49:38 +01004003 // When "allow_scrollbar" is FALSE still need to remember the new
4004 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004005 dont_scroll = !allow_scrollbar;
4006
Bram Moolenaara338adc2018-01-31 20:51:47 +01004007 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004008 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004009 mch_enable_flush();
4010 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011
4012 s_busy_processing = FALSE;
4013 dont_scroll = dont_scroll_save;
4014
4015 return 0;
4016}
4017
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#ifdef FEAT_XPM_W32
4020# include "xpm_w32.h"
4021#endif
4022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
Bram Moolenaar734a8672019-12-02 22:49:38 +01004024// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#define TEAROFF_PADDING_X 2
4026#define TEAROFF_BUTTON_PAD_X 8
4027#define TEAROFF_MIN_WIDTH 200
4028#define TEAROFF_SUBMENU_LABEL ">>"
4029#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4030
4031
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004032#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033# define ID_BEVAL_TOOLTIP 200
4034# define BEVAL_TEXT_LEN MAXPATHL
4035
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004037static UINT_PTR beval_timer_id = 0;
4038static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004039#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004040
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004041
Bram Moolenaar734a8672019-12-02 22:49:38 +01004042// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044#ifdef FEAT_MENU
4045static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048/*
4049 * Use the system font for dialogs and tear-off menus. Remove this line to
4050 * use DLG_FONT_NAME.
4051 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004052#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055#define VIM_CLASSW L"Vim"
4056
Bram Moolenaar734a8672019-12-02 22:49:38 +01004057// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4058// thus there should be room for every dialog. For tearoffs it's made bigger
4059// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060#define DLG_ALLOC_SIZE 16 * 1024
4061
4062/*
4063 * stuff for dialogs, menus, tearoffs etc.
4064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065static PWORD
4066add_dialog_element(
4067 PWORD p,
4068 DWORD lStyle,
4069 WORD x,
4070 WORD y,
4071 WORD w,
4072 WORD h,
4073 WORD Id,
4074 WORD clss,
4075 const char *caption);
4076static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004077static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004078#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081static void get_dialog_font_metrics(void);
4082
4083static int dialog_default_button = -1;
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#ifdef FEAT_TOOLBAR
4086static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004087static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004088static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004090#else
4091# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092#endif
4093
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004094#ifdef FEAT_GUI_TABLINE
4095static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004096static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004097#endif
4098
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#ifdef FEAT_MBYTE_IME
4100static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4101static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4102#endif
4103#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4104# ifdef NOIME
4105typedef struct tagCOMPOSITIONFORM {
4106 DWORD dwStyle;
4107 POINT ptCurrentPos;
4108 RECT rcArea;
4109} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4110typedef HANDLE HIMC;
4111# endif
4112
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004113static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004114static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4115static HIMC (WINAPI *pImmGetContext)(HWND);
4116static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4117static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4118static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4119static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004120static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4121static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004122static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4123static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004124static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125static void dyn_imm_load(void);
4126#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127# define pImmGetCompositionStringW ImmGetCompositionStringW
4128# define pImmGetContext ImmGetContext
4129# define pImmAssociateContext ImmAssociateContext
4130# define pImmReleaseContext ImmReleaseContext
4131# define pImmGetOpenStatus ImmGetOpenStatus
4132# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004133# define pImmGetCompositionFontW ImmGetCompositionFontW
4134# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135# define pImmSetCompositionWindow ImmSetCompositionWindow
4136# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004137# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
4139
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140#ifdef FEAT_MENU
4141/*
4142 * Figure out how high the menu bar is at the moment.
4143 */
4144 static int
4145gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004146 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147{
4148 static int old_menu_height = -1;
4149
4150 RECT rc1, rc2;
4151 int num;
4152 int menu_height;
4153
4154 if (gui.menu_is_active)
4155 num = GetMenuItemCount(s_menuBar);
4156 else
4157 num = 0;
4158
4159 if (num == 0)
4160 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004161 else if (IsMinimized(s_hwnd))
4162 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004163 // The height of the menu cannot be determined while the window is
4164 // minimized. Take the previous height if the menu is changed in that
4165 // state, to avoid that Vim's vertical window size accidentally
4166 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004167 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004171 /*
4172 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4173 * seem to have been set yet, so menu wraps in default window
4174 * width which is very narrow. Instead just return height of a
4175 * single menu item. Will still be wrong when the menu really
4176 * should wrap over more than one line.
4177 */
4178 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4179 if (gui.starting)
4180 menu_height = rc1.bottom - rc1.top + 1;
4181 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004183 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4184 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 }
4187
4188 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004189 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004190 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
4192 return menu_height;
4193}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004194#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196
4197/*
4198 * Setup for the Intellimouse
4199 */
LemonBoyc27747e2022-05-07 12:25:40 +01004200 static long
4201mouse_vertical_scroll_step(void)
4202{
4203 UINT val;
4204 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4205 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4206 return 3; // Safe default;
4207}
4208
4209 static long
4210mouse_horizontal_scroll_step(void)
4211{
4212 UINT val;
4213 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4214 return (long)val;
4215 return 3; // Safe default;
4216}
4217
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 static void
4219init_mouse_wheel(void)
4220{
LemonBoyc27747e2022-05-07 12:25:40 +01004221 // Get the default values for the horizontal and vertical scroll steps from
4222 // the system.
4223 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4224 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225}
4226
Bram Moolenaarae20f342019-11-05 21:09:23 +01004227/*
LemonBoya773d842022-05-10 20:54:46 +01004228 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004231_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
LemonBoy365d8f72022-05-05 19:23:07 +01004233 int button;
4234 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004235 int modifiers = 0;
4236 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004237 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4238 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaarae20f342019-11-05 21:09:23 +01004240 wp = gui_mouse_window(FIND_POPUP);
4241
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004242#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004243 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004244 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004245 cmdarg_T cap;
4246 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004247
Bram Moolenaarae20f342019-11-05 21:09:23 +01004248 // Mouse hovers over popup window, scroll it if possible.
4249 mouse_row = wp->w_winrow;
4250 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004251 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004252 if (horizontal)
4253 {
4254 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4255 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4256 }
4257 else
4258 {
4259 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4260 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4261 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004262 clear_oparg(&oa);
4263 cap.oap = &oa;
4264 nv_mousescroll(&cap);
4265 update_screen(0);
4266 setcursor();
4267 out_flush();
4268 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004269 }
4270#endif
4271
Bram Moolenaarae20f342019-11-05 21:09:23 +01004272 if (wp == NULL || !p_scf)
4273 wp = curwin;
4274
LemonBoy365d8f72022-05-05 19:23:07 +01004275 // Translate the scroll event into an event that Vim can process so that
4276 // the user has a chance to map the scrollwheel buttons.
4277 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004278 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 else
LemonBoy365d8f72022-05-05 19:23:07 +01004280 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004281
4282 kbd_modifiers = get_active_modifiers();
4283
4284 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4285 modifiers |= MOUSE_SHIFT;
4286 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4287 modifiers |= MOUSE_CTRL;
4288 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4289 modifiers |= MOUSE_ALT;
4290
LemonBoya773d842022-05-10 20:54:46 +01004291 // The cursor position is relative to the upper-left corner of the screen.
4292 pt.x = GET_X_LPARAM(lParam);
4293 pt.y = GET_Y_LPARAM(lParam);
4294 ScreenToClient(s_textArea, &pt);
4295
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004296 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297}
4298
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004299#ifdef USE_SYSMENU_FONT
4300/*
4301 * Get Menu Font.
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004305gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004306{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004307 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004308
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004309 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4310 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004312 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004313 &nm,
4314 0))
4315 return FAIL;
4316 *lf = nm.lfMenuFont;
4317 return OK;
4318}
4319#endif
4320
4321
4322#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4323/*
4324 * Set the GUI tabline font to the system menu font
4325 */
4326 static void
4327set_tabline_font(void)
4328{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004329 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004330 HFONT font;
4331 HWND hwnd;
4332 HDC hdc;
4333 HFONT hfntOld;
4334 TEXTMETRIC tm;
4335
4336 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4337 return;
4338
K.Takatac81e9bf2022-01-16 14:15:49 +00004339 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004340 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004341
4342 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4343
4344 /*
4345 * Compute the height of the font used for the tab text
4346 */
4347 hwnd = GetDesktopWindow();
4348 hdc = GetWindowDC(hwnd);
4349 hfntOld = SelectFont(hdc, font);
4350
4351 GetTextMetrics(hdc, &tm);
4352
4353 SelectFont(hdc, hfntOld);
4354 ReleaseDC(hwnd, hdc);
4355
4356 /*
4357 * The space used by the tab border and the space between the tab label
4358 * and the tab border is included as 7.
4359 */
4360 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4361}
K.Takatac81e9bf2022-01-16 14:15:49 +00004362#else
4363# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004364#endif
4365
Bram Moolenaar520470a2005-06-16 21:59:56 +00004366/*
4367 * Invoked when a setting was changed.
4368 */
4369 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004370_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004371{
LemonBoy365d8f72022-05-05 19:23:07 +01004372 switch (param)
4373 {
4374 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004375 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004376 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004377 case SPI_SETWHEELSCROLLCHARS:
4378 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004379 break;
4380 case SPI_SETNONCLIENTMETRICS:
4381 set_tabline_font();
4382 break;
4383 default:
4384 break;
4385 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004386 return 0;
4387}
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389#ifdef FEAT_NETBEANS_INTG
4390 static void
4391_OnWindowPosChanged(
4392 HWND hwnd,
4393 const LPWINDOWPOS lpwpos)
4394{
4395 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004396 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4399 || lpwpos->cx != cx || lpwpos->cy != cy))
4400 {
4401 x = lpwpos->x;
4402 y = lpwpos->y;
4403 cx = lpwpos->cx;
4404 cy = lpwpos->cy;
4405 netbeans_frame_moved(x, y);
4406 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004407 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004408 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409}
4410#endif
4411
K.Takata4f2417f2021-06-05 16:25:32 +02004412
4413static HWND hwndTip = NULL;
4414
4415 static void
4416show_sizing_tip(int cols, int rows)
4417{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004418 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004419 char buf[32];
4420
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004421 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004422 ti.hwnd = s_hwnd;
4423 ti.uId = (UINT_PTR)s_hwnd;
4424 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4425 ti.lpszText = buf;
4426 sprintf(buf, "%dx%d", cols, rows);
4427 if (hwndTip == NULL)
4428 {
4429 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4430 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4431 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4432 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4433 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4434 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4435 }
4436 else
4437 {
4438 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4439 }
4440 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4441}
4442
4443 static void
4444destroy_sizing_tip(void)
4445{
4446 if (hwndTip != NULL)
4447 {
4448 DestroyWindow(hwndTip);
4449 hwndTip = NULL;
4450 }
4451}
4452
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 static int
4454_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 UINT fwSide,
4456 LPRECT lprc)
4457{
4458 int w, h;
4459 int valid_w, valid_h;
4460 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004461 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 w = lprc->right - lprc->left;
4464 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004465 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 w_offset = w - valid_w;
4467 h_offset = h - valid_h;
4468
4469 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4470 || fwSide == WMSZ_BOTTOMLEFT)
4471 lprc->left += w_offset;
4472 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4473 || fwSide == WMSZ_BOTTOMRIGHT)
4474 lprc->right -= w_offset;
4475
4476 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4477 || fwSide == WMSZ_TOPRIGHT)
4478 lprc->top += h_offset;
4479 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4480 || fwSide == WMSZ_BOTTOMRIGHT)
4481 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004482
4483 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return TRUE;
4485}
4486
K.Takata92000e22022-01-20 15:10:57 +00004487#ifdef FEAT_GUI_TABLINE
4488 static void
4489_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4490{
4491 if (gui_mch_showing_tabline())
4492 {
4493 POINT pt;
4494 RECT rect;
4495
4496 /*
4497 * If the cursor is on the tabline, display the tab menu
4498 */
4499 GetCursorPos(&pt);
4500 GetWindowRect(s_textArea, &rect);
4501 if (pt.y < rect.top)
4502 {
4503 show_tabline_popup_menu();
4504 return;
4505 }
4506 }
4507 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4508}
4509
4510 static void
4511_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4512{
4513 /*
4514 * If the user double clicked the tabline, create a new tab
4515 */
4516 if (gui_mch_showing_tabline())
4517 {
4518 POINT pt;
4519 RECT rect;
4520
4521 GetCursorPos(&pt);
4522 GetWindowRect(s_textArea, &rect);
4523 if (pt.y < rect.top)
4524 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4525 }
4526 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4527}
4528#endif
4529
4530 static UINT
4531_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4532{
4533 UINT result;
4534 int x, y;
4535
4536 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4537 if (result != HTCLIENT)
4538 return result;
4539
4540#ifdef FEAT_GUI_TABLINE
4541 if (gui_mch_showing_tabline())
4542 {
4543 RECT rct;
4544
4545 // If the cursor is on the GUI tabline, don't process this event
4546 GetWindowRect(s_textArea, &rct);
4547 if (yPos < rct.top)
4548 return result;
4549 }
4550#endif
4551 (void)gui_mch_get_winpos(&x, &y);
4552 xPos -= x;
4553
4554 if (xPos < 48) // <VN> TODO should use system metric?
4555 return HTBOTTOMLEFT;
4556 else
4557 return HTBOTTOMRIGHT;
4558}
4559
4560#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4561 static LRESULT
4562_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4563{
4564 switch (hdr->code)
4565 {
4566 case TTN_GETDISPINFOW:
4567 case TTN_GETDISPINFO:
4568 {
4569 char_u *str = NULL;
4570 static void *tt_text = NULL;
4571
4572 VIM_CLEAR(tt_text);
4573
4574# ifdef FEAT_GUI_TABLINE
4575 if (gui_mch_showing_tabline()
4576 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4577 {
4578 POINT pt;
4579 /*
4580 * Mouse is over the GUI tabline. Display the
4581 * tooltip for the tab under the cursor
4582 *
4583 * Get the cursor position within the tab control
4584 */
4585 GetCursorPos(&pt);
4586 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4587 {
4588 TCHITTESTINFO htinfo;
4589 int idx;
4590
4591 /*
4592 * Get the tab under the cursor
4593 */
4594 htinfo.pt.x = pt.x;
4595 htinfo.pt.y = pt.y;
4596 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4597 if (idx != -1)
4598 {
4599 tabpage_T *tp;
4600
4601 tp = find_tabpage(idx + 1);
4602 if (tp != NULL)
4603 {
4604 get_tabline_label(tp, TRUE);
4605 str = NameBuff;
4606 }
4607 }
4608 }
4609 }
4610# endif
4611# ifdef FEAT_TOOLBAR
4612# ifdef FEAT_GUI_TABLINE
4613 else
4614# endif
4615 {
4616 UINT idButton;
4617 vimmenu_T *pMenu;
4618
4619 idButton = (UINT) hdr->idFrom;
4620 pMenu = gui_mswin_find_menu(root_menu, idButton);
4621 if (pMenu)
4622 str = pMenu->strings[MENU_INDEX_TIP];
4623 }
4624# endif
4625 if (str == NULL)
4626 break;
4627
4628 // Set the maximum width, this also enables using \n for
4629 // line break.
4630 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4631
4632 if (hdr->code == TTN_GETDISPINFOW)
4633 {
4634 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4635
4636 tt_text = enc_to_utf16(str, NULL);
4637 lpdi->lpszText = tt_text;
4638 // can't show tooltip if failed
4639 }
4640 else
4641 {
4642 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4643
4644 if (STRLEN(str) < sizeof(lpdi->szText)
4645 || ((tt_text = vim_strsave(str)) == NULL))
4646 vim_strncpy((char_u *)lpdi->szText, str,
4647 sizeof(lpdi->szText) - 1);
4648 else
4649 lpdi->lpszText = tt_text;
4650 }
4651 }
4652 break;
4653
4654# ifdef FEAT_GUI_TABLINE
4655 case TCN_SELCHANGE:
4656 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4657 {
4658 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4659 return 0L;
4660 }
4661 break;
4662
4663 case NM_RCLICK:
4664 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4665 {
4666 show_tabline_popup_menu();
4667 return 0L;
4668 }
4669 break;
4670# endif
4671
4672 default:
4673 break;
4674 }
4675 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4676}
4677#endif
4678
4679#if defined(MENUHINTS) && defined(FEAT_MENU)
4680 static LRESULT
4681_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4682{
4683 if (((UINT) HIWORD(wParam)
4684 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4685 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004686 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004687 {
4688 UINT idButton;
4689 vimmenu_T *pMenu;
4690 static int did_menu_tip = FALSE;
4691
4692 if (did_menu_tip)
4693 {
4694 msg_clr_cmdline();
4695 setcursor();
4696 out_flush();
4697 did_menu_tip = FALSE;
4698 }
4699
4700 idButton = (UINT)LOWORD(wParam);
4701 pMenu = gui_mswin_find_menu(root_menu, idButton);
4702 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4703 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4704 {
4705 ++msg_hist_off;
4706 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4707 --msg_hist_off;
4708 setcursor();
4709 out_flush();
4710 did_menu_tip = TRUE;
4711 }
4712 return 0L;
4713 }
4714 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4715}
4716#endif
4717
K.Takatac81e9bf2022-01-16 14:15:49 +00004718 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004719_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004720{
4721 s_dpi = ydpi;
4722 s_in_dpichanged = TRUE;
4723 //TRACE("DPI: %d", ydpi);
4724
4725 update_scrollbar_size();
4726 update_toolbar_size();
4727 set_tabline_font();
4728
4729 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4730 gui_get_wide_font();
4731 gui_mswin_get_menu_height(FALSE);
4732#ifdef FEAT_MBYTE_IME
4733 im_set_position(gui.row, gui.col);
4734#endif
4735 InvalidateRect(hwnd, NULL, TRUE);
4736
4737 s_in_dpichanged = FALSE;
4738 return 0L;
4739}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741
4742 static LRESULT CALLBACK
4743_WndProc(
4744 HWND hwnd,
4745 UINT uMsg,
4746 WPARAM wParam,
4747 LPARAM lParam)
4748{
LemonBoya773d842022-05-10 20:54:46 +01004749 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4750 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 HandleMouseHide(uMsg, lParam);
4753
4754 s_uMsg = uMsg;
4755 s_wParam = wParam;
4756 s_lParam = lParam;
4757
4758 switch (uMsg)
4759 {
4760 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4761 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004762 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004764 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4766 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4767 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4768 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4769#ifdef FEAT_MENU
4770 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4771#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004772 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4773 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4775 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004776 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4777 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4779 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4780 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4781#ifdef FEAT_NETBEANS_INTG
4782 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4783#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004784#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004785 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4786 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004787#endif
K.Takata92000e22022-01-20 15:10:57 +00004788 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004789
Bram Moolenaar734a8672019-12-02 22:49:38 +01004790 case WM_QUERYENDSESSION: // System wants to go down.
4791 gui_shell_closed(); // Will exit when no changed buffers.
4792 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793
4794 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004795 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004798 return 0L;
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 break;
4801
4802 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004803 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4804 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004805 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return 0L;
4807
4808 case WM_SYSCHAR:
4809 /*
4810 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4811 * shortcut key, handle like a typed ALT key, otherwise call Windows
4812 * ALT key handling.
4813 */
4814#ifdef FEAT_MENU
4815 if ( !gui.menu_is_active
4816 || p_wak[0] == 'n'
4817 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4818 )
4819#endif
4820 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004821 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return 0L;
4823 }
4824#ifdef FEAT_MENU
4825 else
K.Takata4ac893f2022-01-20 12:44:28 +00004826 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#endif
4828
4829 case WM_SYSKEYUP:
4830#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004831 // This used to be done only when menu is active: ALT key is used for
4832 // that. But that caused problems when menu is disabled and using
4833 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4834 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004835 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004837 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839
K.Takata4f2417f2021-06-05 16:25:32 +02004840 case WM_EXITSIZEMOVE:
4841 destroy_sizing_tip();
4842 break;
4843
Bram Moolenaar734a8672019-12-02 22:49:38 +01004844 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004845 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004848 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004849 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004850 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
Bram Moolenaar734a8672019-12-02 22:49:38 +01004852 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004853 case WM_SETTINGCHANGE:
4854 return _OnSettingChange((UINT)wParam);
4855
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004856#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004858 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#endif
K.Takata92000e22022-01-20 15:10:57 +00004860
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#if defined(MENUHINTS) && defined(FEAT_MENU)
4862 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004863 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865
4866#ifdef FEAT_MBYTE_IME
4867 case WM_IME_NOTIFY:
4868 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004869 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004870 return 1L;
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 case WM_IME_COMPOSITION:
4873 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004874 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004875 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004877 case WM_DPICHANGED:
4878 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4879 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004883 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#endif
K.Takata92000e22022-01-20 15:10:57 +00004886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888
K.Takata4ac893f2022-01-20 12:44:28 +00004889 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890}
4891
4892/*
4893 * End of call-back routines
4894 */
4895
Bram Moolenaar734a8672019-12-02 22:49:38 +01004896// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897HWND vim_parent_hwnd = NULL;
4898
4899 static BOOL CALLBACK
4900FindWindowTitle(HWND hwnd, LPARAM lParam)
4901{
4902 char buf[2048];
4903 char *title = (char *)lParam;
4904
4905 if (GetWindowText(hwnd, buf, sizeof(buf)))
4906 {
4907 if (strstr(buf, title) != NULL)
4908 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004909 // Found it. Store the window ref. and quit searching if MDI
4910 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004912 if (vim_parent_hwnd != NULL)
4913 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004916 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917}
4918
4919/*
4920 * Invoked for '-P "title"' argument: search for parent application to open
4921 * our window in.
4922 */
4923 void
4924gui_mch_set_parent(char *title)
4925{
4926 EnumWindows(FindWindowTitle, (LPARAM)title);
4927 if (vim_parent_hwnd == NULL)
4928 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004929 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 mch_exit(2);
4931 }
4932}
4933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004934#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 static void
4936ole_error(char *arg)
4937{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004938 char buf[IOSIZE];
4939
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004940# ifdef VIMDLL
4941 gui.in_use = mch_is_gui_executable();
4942# endif
4943
Bram Moolenaar734a8672019-12-02 22:49:38 +01004944 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004945 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004946 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004947 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004951#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4952 static char *
4953gvim_error(void)
4954{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004955 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004956
4957 if (starting)
4958 {
4959 mch_errmsg(msg);
4960 mch_errmsg("\n");
4961 mch_exit(2);
4962 }
4963 return msg;
4964}
4965
4966 char *
4967gui_mch_do_spawn(char_u *arg)
4968{
4969 int len;
4970# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4971 char_u *session = NULL;
4972 LPWSTR tofree1 = NULL;
4973# endif
4974 WCHAR name[MAX_PATH];
4975 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4976 STARTUPINFOW si = {sizeof(si)};
4977 PROCESS_INFORMATION pi;
4978
4979 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4980 goto error;
4981 p = wcsrchr(name, L'\\');
4982 if (p == NULL)
4983 goto error;
4984 // Replace the executable name from vim(d).exe to gvim(d).exe.
4985# ifdef DEBUG
4986 wcscpy(p + 1, L"gvimd.exe");
4987# else
4988 wcscpy(p + 1, L"gvim.exe");
4989# endif
4990
4991# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4992 if (starting)
4993# endif
4994 {
4995 // Pass the command line to the new process.
4996 p = GetCommandLineW();
4997 // Skip 1st argument.
4998 while (*p && *p != L' ' && *p != L'\t')
4999 {
5000 if (*p == L'"')
5001 {
5002 while (*p && *p != L'"')
5003 ++p;
5004 if (*p)
5005 ++p;
5006 }
5007 else
5008 ++p;
5009 }
5010 cmd = p;
5011 }
5012# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5013 else
5014 {
5015 // Create a session file and pass it to the new process.
5016 LPWSTR wsession;
5017 char_u *savebg;
5018 int ret;
5019
5020 session = vim_tempname('s', FALSE);
5021 if (session == NULL)
5022 goto error;
5023 savebg = p_bg;
5024 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5025 ret = write_session_file(session);
5026 vim_free(p_bg);
5027 p_bg = savebg;
5028 if (!ret)
5029 goto error;
5030 wsession = enc_to_utf16(session, NULL);
5031 if (wsession == NULL)
5032 goto error;
5033 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005034 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005035 if (cmd == NULL)
5036 {
5037 vim_free(wsession);
5038 goto error;
5039 }
5040 tofree1 = cmd;
5041 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5042 wsession, wsession);
5043 vim_free(wsession);
5044 }
5045# endif
5046
5047 // Check additional arguments to the `:gui` command.
5048 if (arg != NULL)
5049 {
5050 warg = enc_to_utf16(arg, NULL);
5051 if (warg == NULL)
5052 goto error;
5053 tofree2 = warg;
5054 }
5055 else
5056 warg = L"";
5057
5058 // Set up the new command line.
5059 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005060 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005061 if (newcmd == NULL)
5062 goto error;
5063 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5064
5065 // Spawn a new GUI process.
5066 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5067 NULL, NULL, &si, &pi))
5068 goto error;
5069 CloseHandle(pi.hProcess);
5070 CloseHandle(pi.hThread);
5071 mch_exit(0);
5072
5073error:
5074# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5075 if (session)
5076 mch_remove(session);
5077 vim_free(session);
5078 vim_free(tofree1);
5079# endif
5080 vim_free(newcmd);
5081 vim_free(tofree2);
5082 return gvim_error();
5083}
5084#endif
5085
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086/*
5087 * Parse the GUI related command-line arguments. Any arguments used are
5088 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005089 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
5091 void
5092gui_mch_prepare(int *argc, char **argv)
5093{
5094 int silent = FALSE;
5095 int idx;
5096
Bram Moolenaar734a8672019-12-02 22:49:38 +01005097 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5099 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005100 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5102 && (argv[2][0] == '-' || argv[2][0] == '/'))
5103 {
5104 silent = TRUE;
5105 idx = 2;
5106 }
5107 else
5108 idx = 1;
5109
Bram Moolenaar734a8672019-12-02 22:49:38 +01005110 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (STRICMP(argv[idx] + 1, "register") == 0)
5112 {
5113#ifdef FEAT_OLE
5114 RegisterMe(silent);
5115 mch_exit(0);
5116#else
5117 if (!silent)
5118 ole_error("register");
5119 mch_exit(2);
5120#endif
5121 }
5122
Bram Moolenaar734a8672019-12-02 22:49:38 +01005123 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5125 {
5126#ifdef FEAT_OLE
5127 UnregisterMe(!silent);
5128 mch_exit(0);
5129#else
5130 if (!silent)
5131 ole_error("unregister");
5132 mch_exit(2);
5133#endif
5134 }
5135
Bram Moolenaar734a8672019-12-02 22:49:38 +01005136 // Ignore an -embedding argument. It is only relevant if the
5137 // application wants to treat the case when it is started manually
5138 // differently from the case where it is started via automation (and
5139 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5141 {
5142#ifdef FEAT_OLE
5143 *argc = 1;
5144#else
5145 ole_error("embedding");
5146 mch_exit(2);
5147#endif
5148 }
5149 }
5150
5151#ifdef FEAT_OLE
5152 {
5153 int bDoRestart = FALSE;
5154
5155 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005156 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (bDoRestart)
5158 mch_exit(0);
5159 }
5160#endif
5161
5162#ifdef FEAT_NETBEANS_INTG
5163 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005164 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 int arg;
5166
5167 for (arg = 1; arg < *argc; arg++)
5168 if (strncmp("-nb", argv[arg], 3) == 0)
5169 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 netbeansArg = argv[arg];
5171 mch_memmove(&argv[arg], &argv[arg + 1],
5172 (--*argc - arg) * sizeof(char *));
5173 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005174 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178}
5179
K.Takatac81e9bf2022-01-16 14:15:49 +00005180 static void
5181load_dpi_func(void)
5182{
5183 HMODULE hUser32;
5184
5185 hUser32 = GetModuleHandle("user32.dll");
5186 if (hUser32 == NULL)
5187 goto fail;
5188
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005189 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5190 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5191 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005192 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005193 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5194 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005195
5196 if (pSetThreadDpiAwarenessContext != NULL)
5197 {
5198 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5199 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5200 if (oldctx != NULL)
5201 {
5202 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5203 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5204#ifdef DEBUG
5205 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5206 {
5207 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5208 }
5209#endif
5210 return;
5211 }
5212 }
5213
5214fail:
5215 // Disable PerMonitorV2 APIs.
5216 pGetDpiForSystem = stubGetDpiForSystem;
5217 pGetDpiForWindow = NULL;
5218 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5219 pSetThreadDpiAwarenessContext = NULL;
5220 pGetAwarenessFromDpiAwarenessContext = NULL;
5221}
5222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223/*
5224 * Initialise the GUI. Create all the windows, set up all the call-backs
5225 * etc.
5226 */
5227 int
5228gui_mch_init(void)
5229{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005231 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar734a8672019-12-02 22:49:38 +01005234 // Return here if the window was already opened (happens when
5235 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005237 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 /*
5240 * Load the tearoff bitmap
5241 */
5242#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005243 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245
K.Takatac81e9bf2022-01-16 14:15:49 +00005246 load_dpi_func();
5247
5248 s_dpi = pGetDpiForSystem();
5249 update_scrollbar_size();
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005252 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
5254 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005255#ifdef FEAT_TOOLBAR
5256 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5260
Bram Moolenaar734a8672019-12-02 22:49:38 +01005261 // First try using the wide version, so that we can use any title.
5262 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005263 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005265 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 wndclassw.lpfnWndProc = _WndProc;
5267 wndclassw.cbClsExtra = 0;
5268 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005269 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5271 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5272 wndclassw.hbrBackground = s_brush;
5273 wndclassw.lpszMenuName = NULL;
5274 wndclassw.lpszClassName = szVimWndClassW;
5275
K.Takata4ac893f2022-01-20 12:44:28 +00005276 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (vim_parent_hwnd != NULL)
5281 {
5282#ifdef HAVE_TRY_EXCEPT
5283 __try
5284 {
5285#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005286 // Open inside the specified parent window.
5287 // TODO: last argument should point to a CLIENTCREATESTRUCT
5288 // structure.
5289 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005291 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005292 WS_OVERLAPPEDWINDOW | WS_CHILD
5293 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5295 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005296 100, // Any value will do
5297 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005299 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300#ifdef HAVE_TRY_EXCEPT
5301 }
5302 __except(EXCEPTION_EXECUTE_HANDLER)
5303 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005304 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306#endif
5307 if (s_hwnd == NULL)
5308 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005309 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 mch_exit(2);
5311 }
5312 }
5313 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005314 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005315 // If the provided windowid is not valid reset it to zero, so that it
5316 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005317 if (IsWindow((HWND)win_socket_id) <= 0)
5318 win_socket_id = 0;
5319
Bram Moolenaar734a8672019-12-02 22:49:38 +01005320 // Create a window. If win_socket_id is not zero without border and
5321 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005322 s_hwnd = CreateWindowW(
5323 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005324 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5325 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005326 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5327 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005328 100, // Any value will do
5329 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005330 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005331 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005332 if (s_hwnd != NULL && win_socket_id != 0)
5333 {
5334 SetParent(s_hwnd, (HWND)win_socket_id);
5335 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5336 }
5337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 if (s_hwnd == NULL)
5340 return FAIL;
5341
K.Takatac81e9bf2022-01-16 14:15:49 +00005342 if (pGetDpiForWindow != NULL)
5343 {
5344 s_dpi = pGetDpiForWindow(s_hwnd);
5345 update_scrollbar_size();
5346 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5347 }
5348
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5350 dyn_imm_load();
5351#endif
5352
Bram Moolenaar734a8672019-12-02 22:49:38 +01005353 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005354 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005355 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005356 wndclassw.style = CS_OWNDC;
5357 wndclassw.lpfnWndProc = _TextAreaWndProc;
5358 wndclassw.cbClsExtra = 0;
5359 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005360 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005361 wndclassw.hIcon = NULL;
5362 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5363 wndclassw.hbrBackground = NULL;
5364 wndclassw.lpszMenuName = NULL;
5365 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005366
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005367 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 return FAIL;
5369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005371 s_textArea = CreateWindowExW(
5372 0,
5373 szTextAreaClassW, L"Vim text area",
5374 WS_CHILD | WS_VISIBLE, 0, 0,
5375 100, // Any value will do for now
5376 100, // Any value will do for now
5377 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005378 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 if (s_textArea == NULL)
5381 return FAIL;
5382
Bram Moolenaar20321902016-02-17 12:30:17 +01005383#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005384 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005385 {
5386 HANDLE hIcon = NULL;
5387
5388 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005389 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005390 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005391#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#ifdef FEAT_MENU
5394 s_menuBar = CreateMenu();
5395#endif
5396 s_hdc = GetDC(s_textArea);
5397
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399
Bram Moolenaar734a8672019-12-02 22:49:38 +01005400 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005401 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaar734a8672019-12-02 22:49:38 +01005403 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 gui_mch_def_colors();
5405
Bram Moolenaar734a8672019-12-02 22:49:38 +01005406 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5407 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 set_normal_colors();
5409
5410 /*
5411 * Check that none of the colors are the same as the background color.
5412 * Then store the current values as the defaults.
5413 */
5414 gui_check_colors();
5415 gui.def_norm_pixel = gui.norm_pixel;
5416 gui.def_back_pixel = gui.back_pixel;
5417
Bram Moolenaar734a8672019-12-02 22:49:38 +01005418 // Get the colors for the highlight groups (gui_check_colors() might have
5419 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 highlight_gui_started();
5421
5422 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005423 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005425 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 /*
5428 * Set up for Intellimouse processing
5429 */
5430 init_mouse_wheel();
5431
5432 /*
5433 * compute a couple of metrics used for the dialogs
5434 */
5435 get_dialog_font_metrics();
5436#ifdef FEAT_TOOLBAR
5437 /*
5438 * Create the toolbar
5439 */
5440 initialise_toolbar();
5441#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005442#ifdef FEAT_GUI_TABLINE
5443 /*
5444 * Create the tabline
5445 */
5446 initialise_tabline();
5447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448#ifdef MSWIN_FIND_REPLACE
5449 /*
5450 * Initialise the dialog box stuff
5451 */
5452 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5453
Bram Moolenaar734a8672019-12-02 22:49:38 +01005454 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005456 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005458 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5460 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5461 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005464#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005465 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005466 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005467#endif
5468
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005469#ifdef FEAT_RENDER_OPTIONS
5470 if (p_rop)
5471 (void)gui_mch_set_rendering_options(p_rop);
5472#endif
5473
Bram Moolenaar748bf032005-02-02 23:04:36 +00005474theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005475 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005476 display_errors();
5477
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 return OK;
5479}
5480
5481/*
5482 * Get the size of the screen, taking position on multiple monitors into
5483 * account (if supported).
5484 */
5485 static void
5486get_work_area(RECT *spi_rect)
5487{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005488 HMONITOR mon;
5489 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaar734a8672019-12-02 22:49:38 +01005491 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005492 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005493 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005495 moninfo.cbSize = sizeof(MONITORINFO);
5496 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005498 *spi_rect = moninfo.rcWork;
5499 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005502 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5504}
5505
5506/*
5507 * Set the size of the window to the given width and height in pixels.
5508 */
5509 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005510gui_mch_set_shellsize(
5511 int width,
5512 int height,
5513 int min_width UNUSED,
5514 int min_height UNUSED,
5515 int base_width UNUSED,
5516 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005517 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005520 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar734a8672019-12-02 22:49:38 +01005523 // Try to keep window completely on screen.
5524 // Get position of the screen work area. This is the part that is not
5525 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 get_work_area(&workarea_rect);
5527
Bram Moolenaar734a8672019-12-02 22:49:38 +01005528 // Resizing a maximized window looks very strange, unzoom it first.
5529 // But don't do it when still starting up, it may have been requested in
5530 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005531 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005533
5534 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar734a8672019-12-02 22:49:38 +01005536 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005537 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5538 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5539 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5540 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5541 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5542 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaar734a8672019-12-02 22:49:38 +01005544 // The following should take care of keeping Vim on the same monitor, no
5545 // matter if the secondary monitor is left or right of the primary
5546 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005547 window_rect.right = window_rect.left + win_width;
5548 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005549
Bram Moolenaar734a8672019-12-02 22:49:38 +01005550 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005551 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5552 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005554 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5555 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005557 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5558 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005560 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5561 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005563 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5564 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 SetActiveWindow(s_hwnd);
5567 SetFocus(s_hwnd);
5568
Bram Moolenaar734a8672019-12-02 22:49:38 +01005569 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
5573
5574 void
5575gui_mch_set_scrollbar_thumb(
5576 scrollbar_T *sb,
5577 long val,
5578 long size,
5579 long max)
5580{
5581 SCROLLINFO info;
5582
5583 sb->scroll_shift = 0;
5584 while (max > 32767)
5585 {
5586 max = (max + 1) >> 1;
5587 val >>= 1;
5588 size >>= 1;
5589 ++sb->scroll_shift;
5590 }
5591
5592 if (sb->scroll_shift > 0)
5593 ++size;
5594
5595 info.cbSize = sizeof(info);
5596 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5597 info.nPos = val;
5598 info.nMin = 0;
5599 info.nMax = max;
5600 info.nPage = size;
5601 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5602}
5603
5604
5605/*
5606 * Set the current text font.
5607 */
5608 void
5609gui_mch_set_font(GuiFont font)
5610{
5611 gui.currFont = font;
5612}
5613
5614
5615/*
5616 * Set the current text foreground color.
5617 */
5618 void
5619gui_mch_set_fg_color(guicolor_T color)
5620{
5621 gui.currFgColor = color;
5622}
5623
5624/*
5625 * Set the current text background color.
5626 */
5627 void
5628gui_mch_set_bg_color(guicolor_T color)
5629{
5630 gui.currBgColor = color;
5631}
5632
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005633/*
5634 * Set the current text special color.
5635 */
5636 void
5637gui_mch_set_sp_color(guicolor_T color)
5638{
5639 gui.currSpColor = color;
5640}
5641
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005642#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643/*
5644 * Multi-byte handling, originally by Sung-Hoon Baek.
5645 * First static functions (no prototypes generated).
5646 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005647# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005648# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005649# endif
5650# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * handle WM_IME_NOTIFY message
5654 */
5655 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005656_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 LRESULT lResult = 0;
5659 HIMC hImc;
5660
5661 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5662 return lResult;
5663 switch (dwCommand)
5664 {
5665 case IMN_SETOPENSTATUS:
5666 if (pImmGetOpenStatus(hImc))
5667 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005668 LOGFONTW lf = norm_logfont;
5669 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5670 // Work around when PerMonitorV2 is not enabled in the process level.
5671 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5672 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 im_set_position(gui.row, gui.col);
5674
Bram Moolenaar734a8672019-12-02 22:49:38 +01005675 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005676 State &= ~MODE_LANGMAP;
5677 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005679# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005680 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5682 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005683 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 int old_row = gui.row;
5685 int old_col = gui.col;
5686
5687 // This must be called here before
5688 // status_redraw_curbuf(), otherwise the mode
5689 // message may appear in the wrong position.
5690 showmode();
5691 status_redraw_curbuf();
5692 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005693 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 gui.row = old_row;
5695 gui.col = old_col;
5696 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005697# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 }
5700 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005701 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 lResult = 0;
5703 break;
5704 }
5705 pImmReleaseContext(hWnd, hImc);
5706 return lResult;
5707}
5708
5709 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005710_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
5712 char_u *ret;
5713 int len;
5714
Bram Moolenaar734a8672019-12-02 22:49:38 +01005715 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 return 0;
5717
5718 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5719 if (ret != NULL)
5720 {
5721 add_to_input_buf_csi(ret, len);
5722 vim_free(ret);
5723 return 1;
5724 }
5725 return 0;
5726}
5727
5728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 * void GetResultStr()
5730 *
5731 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5732 * get complete composition string
5733 */
5734 static char_u *
5735GetResultStr(HWND hwnd, int GCS, int *lenp)
5736{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005737 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005738 LONG ret;
5739 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 char_u *convbuf = NULL;
5741
5742 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5743 return NULL;
5744
K.Takatab0b2b732022-01-19 12:59:21 +00005745 // Get the length of the composition string.
5746 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5747 if (ret <= 0)
5748 return NULL;
5749
5750 // Allocate the requested buffer plus space for the NUL character.
5751 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 if (buf == NULL)
5753 return NULL;
5754
K.Takatab0b2b732022-01-19 12:59:21 +00005755 // Reads in the composition string.
5756 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5757 *lenp = ret / sizeof(WCHAR);
5758
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005759 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 pImmReleaseContext(hwnd, hIMC);
5761 vim_free(buf);
5762 return convbuf;
5763}
5764#endif
5765
Bram Moolenaar734a8672019-12-02 22:49:38 +01005766// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005767#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769/*
5770 * set font to IM.
5771 */
5772 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005773im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 HIMC hImc;
5776
5777 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5778 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005779 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 pImmReleaseContext(s_hwnd, hImc);
5781 }
5782}
5783
5784/*
5785 * Notify cursor position to IM.
5786 */
5787 void
5788im_set_position(int row, int col)
5789{
5790 HIMC hImc;
5791
5792 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5793 {
5794 COMPOSITIONFORM cfs;
5795
5796 cfs.dwStyle = CFS_POINT;
5797 cfs.ptCurrentPos.x = FILL_X(col);
5798 cfs.ptCurrentPos.y = FILL_Y(row);
5799 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005800 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5801 {
5802 // Work around when PerMonitorV2 is not enabled in the process level.
5803 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5804 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 pImmSetCompositionWindow(hImc, &cfs);
5807
5808 pImmReleaseContext(s_hwnd, hImc);
5809 }
5810}
5811
5812/*
5813 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5814 */
5815 void
5816im_set_active(int active)
5817{
5818 HIMC hImc;
5819 static HIMC hImcOld = (HIMC)0;
5820
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005821# ifdef VIMDLL
5822 if (!gui.in_use && !gui.starting)
5823 {
5824 mbyte_im_set_active(active);
5825 return;
5826 }
5827# endif
5828
Bram Moolenaar734a8672019-12-02 22:49:38 +01005829 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
5831 if (p_imdisable)
5832 {
5833 if (hImcOld == (HIMC)0)
5834 {
5835 hImcOld = pImmGetContext(s_hwnd);
5836 if (hImcOld)
5837 pImmAssociateContext(s_hwnd, (HIMC)0);
5838 }
5839 active = FALSE;
5840 }
5841 else if (hImcOld != (HIMC)0)
5842 {
5843 pImmAssociateContext(s_hwnd, hImcOld);
5844 hImcOld = (HIMC)0;
5845 }
5846
5847 hImc = pImmGetContext(s_hwnd);
5848 if (hImc)
5849 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005850 /*
5851 * for Korean ime
5852 */
5853 HKL hKL = GetKeyboardLayout(0);
5854
5855 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5856 {
5857 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5858 static BOOL bSaved = FALSE;
5859
5860 if (active)
5861 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005862 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005863 if (bSaved)
5864 pImmSetConversionStatus(hImc, dwConversionSaved,
5865 dwSentenceSaved);
5866 bSaved = FALSE;
5867 }
5868 else
5869 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005870 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005871 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5872 &dwSentenceSaved))
5873 {
5874 bSaved = TRUE;
5875 pImmSetConversionStatus(hImc,
5876 dwConversionSaved & ~(IME_CMODE_NATIVE
5877 | IME_CMODE_FULLSHAPE),
5878 dwSentenceSaved);
5879 }
5880 }
5881 }
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 pImmSetOpenStatus(hImc, active);
5884 pImmReleaseContext(s_hwnd, hImc);
5885 }
5886 }
5887}
5888
5889/*
5890 * Get IM status. When IM is on, return not 0. Else return 0.
5891 */
5892 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005893im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 int status = 0;
5896 HIMC hImc;
5897
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005898# ifdef VIMDLL
5899 if (!gui.in_use && !gui.starting)
5900 return mbyte_im_get_status();
5901# endif
5902
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5904 {
5905 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5906 pImmReleaseContext(s_hwnd, hImc);
5907 }
5908 return status;
5909}
5910
Bram Moolenaar734a8672019-12-02 22:49:38 +01005911#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005914/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005915 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005916 */
5917 static void
5918latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5919{
5920 int c;
5921
Bram Moolenaarca003e12006-03-17 23:19:38 +00005922 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005923 {
5924 c = *text++;
5925 switch (c)
5926 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005927 case 0xa4: c = 0x20ac; break; // euro
5928 case 0xa6: c = 0x0160; break; // S hat
5929 case 0xa8: c = 0x0161; break; // S -hat
5930 case 0xb4: c = 0x017d; break; // Z hat
5931 case 0xb8: c = 0x017e; break; // Z -hat
5932 case 0xbc: c = 0x0152; break; // OE
5933 case 0xbd: c = 0x0153; break; // oe
5934 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005935 }
5936 *unicodebuf++ = c;
5937 }
5938}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
5940#ifdef FEAT_RIGHTLEFT
5941/*
5942 * What is this for? In the case where you are using Win98 or Win2K or later,
5943 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5944 * reverses the string sent to the TextOut... family. This sucks, because we
5945 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5946 * way to tell Windblows not to do this!
5947 *
5948 * The short of it is that this 'RevOut' only gets called if you are running
5949 * one of the new, "improved" MS OSes, and only if you are running in
5950 * 'rightleft' mode. It makes display take *slightly* longer, but not
5951 * noticeably so.
5952 */
5953 static void
K.Takata135e1522022-01-29 15:27:58 +00005954RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 int col,
5956 int row,
5957 UINT foptions,
5958 CONST RECT *pcliprect,
5959 LPCTSTR text,
5960 UINT len,
5961 CONST INT *padding)
5962{
5963 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005965 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005966 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005967 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969#endif
5970
Bram Moolenaar92467d32017-12-05 13:22:16 +01005971 static void
5972draw_line(
5973 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005974 int y1,
5975 int x2,
5976 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005977 COLORREF color)
5978{
5979#if defined(FEAT_DIRECTX)
5980 if (IS_ENABLE_DIRECTX())
5981 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5982 else
5983#endif
5984 {
5985 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5986 HPEN old_pen = SelectObject(s_hdc, hpen);
5987 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005988 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005989 LineTo(s_hdc, x2, y2);
5990 DeleteObject(SelectObject(s_hdc, old_pen));
5991 }
5992}
5993
5994 static void
5995set_pixel(
5996 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005997 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005998 COLORREF color)
5999{
6000#if defined(FEAT_DIRECTX)
6001 if (IS_ENABLE_DIRECTX())
6002 DWriteContext_SetPixel(s_dwc, x, y, color);
6003 else
6004#endif
6005 SetPixel(s_hdc, x, y, color);
6006}
6007
6008 static void
6009fill_rect(
6010 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006011 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006012 COLORREF color)
6013{
6014#if defined(FEAT_DIRECTX)
6015 if (IS_ENABLE_DIRECTX())
6016 DWriteContext_FillRect(s_dwc, rcp, color);
6017 else
6018#endif
6019 {
6020 HBRUSH hbr2;
6021
6022 if (hbr == NULL)
6023 hbr2 = CreateSolidBrush(color);
6024 else
6025 hbr2 = hbr;
6026 FillRect(s_hdc, rcp, hbr2);
6027 if (hbr == NULL)
6028 DeleteBrush(hbr2);
6029 }
6030}
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 void
6033gui_mch_draw_string(
6034 int row,
6035 int col,
6036 char_u *text,
6037 int len,
6038 int flags)
6039{
6040 static int *padding = NULL;
6041 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 const RECT *pcliprect = NULL;
6043 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 static WCHAR *unicodebuf = NULL;
6045 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006046 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 int y;
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 /*
6051 * Italic and bold text seems to have an extra row of pixels at the bottom
6052 * (below where the bottom of the character should be). If we draw the
6053 * characters with a solid background, the top row of pixels in the
6054 * character below will be overwritten. We can fix this by filling in the
6055 * background ourselves, to the correct character proportions, and then
6056 * writing the character in transparent mode. Still have a problem when
6057 * the character is "_", which gets written on to the character below.
6058 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6059 * pixel in their slots, which fixes the problem with the bottom row of
6060 * pixels. We still need this code because otherwise the top row of pixels
6061 * becomes a problem. - webb.
6062 */
6063 static HBRUSH hbr_cache[2] = {NULL, NULL};
6064 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6065 static int brush_lru = 0;
6066 HBRUSH hbr;
6067 RECT rc;
6068
6069 if (!(flags & DRAW_TRANSP))
6070 {
6071 /*
6072 * Clear background first.
6073 * Note: FillRect() excludes right and bottom of rectangle.
6074 */
6075 rc.left = FILL_X(col);
6076 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (has_mbyte)
6078 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006079 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006080 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
6082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 rc.right = FILL_X(col + len);
6084 rc.bottom = FILL_Y(row + 1);
6085
Bram Moolenaar734a8672019-12-02 22:49:38 +01006086 // Cache the created brush, that saves a lot of time. We need two:
6087 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (gui.currBgColor == brush_color[0])
6089 {
6090 hbr = hbr_cache[0];
6091 brush_lru = 1;
6092 }
6093 else if (gui.currBgColor == brush_color[1])
6094 {
6095 hbr = hbr_cache[1];
6096 brush_lru = 0;
6097 }
6098 else
6099 {
6100 if (hbr_cache[brush_lru] != NULL)
6101 DeleteBrush(hbr_cache[brush_lru]);
6102 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6103 brush_color[brush_lru] = gui.currBgColor;
6104 hbr = hbr_cache[brush_lru];
6105 brush_lru = !brush_lru;
6106 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006107
Bram Moolenaar92467d32017-12-05 13:22:16 +01006108 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110 SetBkMode(s_hdc, TRANSPARENT);
6111
6112 /*
6113 * When drawing block cursor, prevent inverted character spilling
6114 * over character cell (can happen with bold/italic)
6115 */
6116 if (flags & DRAW_CURSOR)
6117 {
6118 pcliprect = &rc;
6119 foptions = ETO_CLIPPED;
6120 }
6121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 SetTextColor(s_hdc, gui.currFgColor);
6123 SelectFont(s_hdc, gui.currFont);
6124
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006125#ifdef FEAT_DIRECTX
6126 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006127 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006128#endif
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6131 {
K.Takata135e1522022-01-29 15:27:58 +00006132 int i;
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 vim_free(padding);
6135 pad_size = Columns;
6136
Bram Moolenaar734a8672019-12-02 22:49:38 +01006137 // Don't give an out-of-memory message here, it would call us
6138 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006139 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (padding != NULL)
6141 for (i = 0; i < pad_size; i++)
6142 padding[i] = gui.char_width;
6143 }
6144
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 /*
6146 * We have to provide the padding argument because italic and bold versions
6147 * of fixed-width fonts are often one pixel or so wider than their normal
6148 * versions.
6149 * No check for DRAW_BOLD, Windows will have done it already.
6150 */
6151
Bram Moolenaar734a8672019-12-02 22:49:38 +01006152 // Check if there are any UTF-8 characters. If not, use normal text
6153 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 if (enc_utf8)
6155 for (n = 0; n < len; ++n)
6156 if (text[n] >= 0x80)
6157 break;
6158
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006159#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006160 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6161 // required that unicode drawing routine, currently. So this forces it
6162 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006163 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006164 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006165#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006166
Bram Moolenaar734a8672019-12-02 22:49:38 +01006167 // Check if the Unicode buffer exists and is big enough. Create it
6168 // with the same length as the multi-byte string, the number of wide
6169 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006170 if ((enc_utf8
6171 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6172 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 && (unicodebuf == NULL || len > unibuflen))
6174 {
6175 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006176 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006179 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 unibuflen = len;
6182 }
6183
6184 if (enc_utf8 && n < len && unicodebuf != NULL)
6185 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006186 // Output UTF-8 characters. Composing characters should be
6187 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006188 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006189 int wlen; // string length in words
6190 int clen; // string length in characters
6191 int cells; // cell width of string up to composing char
6192 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006193 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006195 wlen = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006196 clen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006198 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006200 c = utf_ptr2char(text + i);
6201 if (c >= 0x10000)
6202 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006203 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006204 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6205 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006206 }
6207 else
6208 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006209 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006210 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006211
6212 if (utf_iscomposing(c))
6213 cw = 0;
6214 else
6215 {
6216 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006217 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006218 cw = 1;
6219 }
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (unicodepdy != NULL)
6222 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006223 // Use unicodepdy to make characters fit as we expect, even
6224 // when the font uses different widths (e.g., bold character
6225 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006226 if (c >= 0x10000)
6227 {
6228 unicodepdy[wlen - 2] = cw * gui.char_width;
6229 unicodepdy[wlen - 1] = 0;
6230 }
6231 else
6232 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
6234 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006235 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaarca003e12006-03-17 23:19:38 +00006236 ++clen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006238#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006239 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006240 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006241 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006242 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006243 TEXT_X(col), TEXT_Y(row),
6244 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006245 gui.char_width, gui.currFgColor,
6246 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006247 }
6248 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006249#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006250 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6251 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006254 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006256 // If we want to display codepage data, and the current CP is not the
6257 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (unicodebuf != NULL)
6259 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006260 if (enc_latin9)
6261 latin9_to_ucs(text, len, unicodebuf);
6262 else
6263 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 MB_PRECOMPOSED,
6265 (char *)text, len,
6266 (LPWSTR)unicodebuf, unibuflen);
6267 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006268 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006269 // Use unicodepdy to make characters fit as we expect, even
6270 // when the font uses different widths (e.g., bold character
6271 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006272 if (unicodepdy != NULL)
6273 {
6274 int i;
6275 int cw;
6276
6277 for (i = 0; i < len; ++i)
6278 {
6279 cw = utf_char2cells(unicodebuf[i]);
6280 if (cw > 2)
6281 cw = 1;
6282 unicodepdy[i] = cw * gui.char_width;
6283 }
6284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006286 foptions, pcliprect, unicodebuf, len, unicodepdy);
6287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 }
6290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
6292#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006293 // Windows will mess up RL text, so we have to draw it character by
6294 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006295 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6297 foptions, pcliprect, (char *)text, len, padding);
6298 else
6299#endif
6300 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6301 foptions, pcliprect, (char *)text, len, padding);
6302 }
6303
Bram Moolenaar734a8672019-12-02 22:49:38 +01006304 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (flags & DRAW_UNDERL)
6306 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006307 // When p_linespace is 0, overwrite the bottom row of pixels.
6308 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (p_linespace > 1)
6311 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006312 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006314
Bram Moolenaar734a8672019-12-02 22:49:38 +01006315 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006316 if (flags & DRAW_STRIKE)
6317 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006318 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006319 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006320 }
6321
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006323 if (flags & DRAW_UNDERC)
6324 {
6325 int x;
6326 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006327 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006328
6329 y = FILL_Y(row + 1) - 1;
6330 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6331 {
6332 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006333 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006334 }
6335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
6338
6339/*
6340 * Output routines.
6341 */
6342
Bram Moolenaar734a8672019-12-02 22:49:38 +01006343/*
6344 * Flush any output to the screen
6345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 void
6347gui_mch_flush(void)
6348{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006349#if defined(FEAT_DIRECTX)
6350 if (IS_ENABLE_DIRECTX())
6351 DWriteContext_Flush(s_dwc);
6352#endif
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 GdiFlush();
6355}
6356
6357 static void
6358clear_rect(RECT *rcp)
6359{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006360 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
6363
Bram Moolenaarc716c302006-01-21 22:12:51 +00006364 void
6365gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6366{
6367 RECT workarea_rect;
6368
6369 get_work_area(&workarea_rect);
6370
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006372 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6373 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006374
Bram Moolenaar734a8672019-12-02 22:49:38 +01006375 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6376 // the menubar for MSwin, we subtract it from the screen height, so that
6377 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006378 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006379 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6380 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6381 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6382 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006383}
6384
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386#if defined(FEAT_MENU) || defined(PROTO)
6387/*
6388 * Add a sub menu to the menu bar.
6389 */
6390 void
6391gui_mch_add_menu(
6392 vimmenu_T *menu,
6393 int pos)
6394{
6395 vimmenu_T *parent = menu->parent;
6396
6397 menu->submenu_id = CreatePopupMenu();
6398 menu->id = s_menu_id++;
6399
6400 if (menu_is_menubar(menu->name))
6401 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006402 WCHAR *wn;
6403 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006405 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006406 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006409 infow.cbSize = sizeof(infow);
6410 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6411 | MIIM_SUBMENU;
6412 infow.dwItemData = (long_u)menu;
6413 infow.wID = menu->id;
6414 infow.fType = MFT_STRING;
6415 infow.dwTypeData = wn;
6416 infow.cch = (UINT)wcslen(wn);
6417 infow.hSubMenu = menu->submenu_id;
6418 InsertMenuItemW((parent == NULL)
6419 ? s_menuBar : parent->submenu_id,
6420 (UINT)pos, TRUE, &infow);
6421 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423
Bram Moolenaar734a8672019-12-02 22:49:38 +01006424 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (parent == NULL)
6426 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006427# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 else if (IsWindow(parent->tearoff_handle))
6429 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433 void
6434gui_mch_show_popupmenu(vimmenu_T *menu)
6435{
6436 POINT mp;
6437
K.Takata45f9cfb2022-01-21 11:11:00 +00006438 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6440}
6441
6442 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006443gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 vimmenu_T *menu = gui_find_menu(path_name);
6446
6447 if (menu != NULL)
6448 {
6449 POINT p;
6450
Bram Moolenaar734a8672019-12-02 22:49:38 +01006451 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006453 if (mouse_pos)
6454 {
6455 int mx, my;
6456
6457 gui_mch_getmouse(&mx, &my);
6458 p.x += mx;
6459 p.y += my;
6460 }
6461 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006463 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6465 }
6466 msg_scroll = FALSE;
6467 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6468 }
6469}
6470
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006471# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6474 * create it as a pseudo-"tearoff menu".
6475 */
6476 void
6477gui_make_tearoff(char_u *path_name)
6478{
6479 vimmenu_T *menu = gui_find_menu(path_name);
6480
Bram Moolenaar734a8672019-12-02 22:49:38 +01006481 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (menu != NULL)
6483 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6484}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487/*
6488 * Add a menu item to a menu
6489 */
6490 void
6491gui_mch_add_menu_item(
6492 vimmenu_T *menu,
6493 int idx)
6494{
6495 vimmenu_T *parent = menu->parent;
6496
6497 menu->id = s_menu_id++;
6498 menu->submenu_id = NULL;
6499
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006500# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6502 {
6503 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6504 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6505 }
6506 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006507# endif
6508# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (menu_is_toolbar(parent->name))
6510 {
6511 TBBUTTON newtb;
6512
Bram Moolenaara80faa82020-04-12 19:37:17 +02006513 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (menu_is_separator(menu->name))
6515 {
6516 newtb.iBitmap = 0;
6517 newtb.fsStyle = TBSTYLE_SEP;
6518 }
6519 else
6520 {
6521 newtb.iBitmap = get_toolbar_bitmap(menu);
6522 newtb.fsStyle = TBSTYLE_BUTTON;
6523 }
6524 newtb.idCommand = menu->id;
6525 newtb.fsState = TBSTATE_ENABLED;
6526 newtb.iString = 0;
6527 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6528 (LPARAM)&newtb);
6529 menu->submenu_id = (HMENU)-1;
6530 }
6531 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 wn = enc_to_utf16(menu->name, NULL);
6537 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006539 InsertMenuW(parent->submenu_id, (UINT)idx,
6540 (menu_is_separator(menu->name)
6541 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6542 (UINT)menu->id, wn);
6543 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006545# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (IsWindow(parent->tearoff_handle))
6547 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550}
6551
6552/*
6553 * Destroy the machine specific menu widget.
6554 */
6555 void
6556gui_mch_destroy_menu(vimmenu_T *menu)
6557{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006558# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 /*
6560 * is this a toolbar button?
6561 */
6562 if (menu->submenu_id == (HMENU)-1)
6563 {
6564 int iButton;
6565
6566 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6567 (WPARAM)menu->id, 0);
6568 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6569 }
6570 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
6573 if (menu->parent != NULL
6574 && menu_is_popup(menu->parent->dname)
6575 && menu->parent->submenu_id != NULL)
6576 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6577 else
6578 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6579 if (menu->submenu_id != NULL)
6580 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006581# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if (IsWindow(menu->tearoff_handle))
6583 DestroyWindow(menu->tearoff_handle);
6584 if (menu->parent != NULL
6585 && menu->parent->children != NULL
6586 && IsWindow(menu->parent->tearoff_handle))
6587 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006588 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 menu->modes = 0;
6590 rebuild_tearoff(menu->parent);
6591 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594}
6595
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006596# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 static void
6598rebuild_tearoff(vimmenu_T *menu)
6599{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006600 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 char_u tbuf[128];
6602 RECT trect;
6603 RECT rct;
6604 RECT roct;
6605 int x, y;
6606
6607 HWND thwnd = menu->tearoff_handle;
6608
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006609 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (GetWindowRect(thwnd, &trect)
6611 && GetWindowRect(s_hwnd, &rct)
6612 && GetClientRect(s_hwnd, &roct))
6613 {
6614 x = trect.left - rct.left;
6615 y = (trect.top - rct.bottom + roct.bottom);
6616 }
6617 else
6618 {
6619 x = y = 0xffffL;
6620 }
6621 DestroyWindow(thwnd);
6622 if (menu->children != NULL)
6623 {
6624 gui_mch_tearoff(tbuf, menu, x, y);
6625 if (IsWindow(menu->tearoff_handle))
6626 (void) SetWindowPos(menu->tearoff_handle,
6627 NULL,
6628 (int)trect.left,
6629 (int)trect.top,
6630 0, 0,
6631 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6632 }
6633}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636/*
6637 * Make a menu either grey or not grey.
6638 */
6639 void
6640gui_mch_menu_grey(
6641 vimmenu_T *menu,
6642 int grey)
6643{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006644# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 /*
6646 * is this a toolbar button?
6647 */
6648 if (menu->submenu_id == (HMENU)-1)
6649 {
6650 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006651 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006654# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006655 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6656 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6660 {
6661 WORD menuID;
6662 HWND menuHandle;
6663
6664 /*
6665 * A tearoff button has changed state.
6666 */
6667 if (menu->children == NULL)
6668 menuID = (WORD)(menu->id);
6669 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006670 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6672 if (menuHandle)
6673 EnableWindow(menuHandle, !grey);
6674
6675 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
6678
Bram Moolenaar734a8672019-12-02 22:49:38 +01006679#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681
Bram Moolenaar734a8672019-12-02 22:49:38 +01006682// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006685#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6688/*
6689 * stuff for dialogs
6690 */
6691
6692/*
6693 * The callback routine used by all the dialogs. Very simple. First,
6694 * acknowledges the INITDIALOG message so that Windows knows to do standard
6695 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6696 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6697 * number.
6698 */
6699 static LRESULT CALLBACK
6700dialog_callback(
6701 HWND hwnd,
6702 UINT message,
6703 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006704 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 if (message == WM_INITDIALOG)
6707 {
6708 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006709 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 (void)SetFocus(hwnd);
6711 if (dialog_default_button > IDCANCEL)
6712 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006713 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006714 // We don't have a default, set focus on another element of the
6715 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006716 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 return FALSE;
6718 }
6719
6720 if (message == WM_COMMAND)
6721 {
6722 int button = LOWORD(wParam);
6723
Bram Moolenaar734a8672019-12-02 22:49:38 +01006724 // Don't end the dialog if something was selected that was
6725 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (button >= DLG_NONBUTTON_CONTROL)
6727 return TRUE;
6728
Bram Moolenaar734a8672019-12-02 22:49:38 +01006729 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006731 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006732 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006733 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006734
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006735 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6736 p = utf16_to_enc(wp, NULL);
6737 vim_strncpy(s_textfield, p, IOSIZE);
6738 vim_free(p);
6739 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 /*
6743 * Need to check for IDOK because if the user just hits Return to
6744 * accept the default value, some reason this is what we get.
6745 */
6746 if (button == IDOK)
6747 {
6748 if (dialog_default_button > IDCANCEL)
6749 EndDialog(hwnd, dialog_default_button);
6750 }
6751 else
6752 EndDialog(hwnd, button - IDCANCEL);
6753 return TRUE;
6754 }
6755
6756 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6757 {
6758 EndDialog(hwnd, 0);
6759 return TRUE;
6760 }
6761 return FALSE;
6762}
6763
6764/*
6765 * Create a dialog dynamically from the parameter strings.
6766 * type = type of dialog (question, alert, etc.)
6767 * title = dialog title. may be NULL for default title.
6768 * message = text to display. Dialog sizes to accommodate it.
6769 * buttons = '\n' separated list of button captions, default first.
6770 * dfltbutton = number of default button.
6771 *
6772 * This routine returns 1 if the first button is pressed,
6773 * 2 for the second, etc.
6774 *
6775 * 0 indicates Esc was pressed.
6776 * -1 for unexpected error
6777 *
6778 * If stubbing out this fn, return 1.
6779 */
6780
Bram Moolenaar734a8672019-12-02 22:49:38 +01006781static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 "IDR_VIM",
6784 "IDR_VIM_ERROR",
6785 "IDR_VIM_ALERT",
6786 "IDR_VIM_INFO",
6787 "IDR_VIM_QUESTION"
6788};
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int
6791gui_mch_dialog(
6792 int type,
6793 char_u *title,
6794 char_u *message,
6795 char_u *buttons,
6796 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006797 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006798 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006801 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 int numButtons;
6803 int *buttonWidths, *buttonPositions;
6804 int buttonYpos;
6805 int nchar, i;
6806 DWORD lStyle;
6807 int dlgwidth = 0;
6808 int dlgheight;
6809 int editboxheight;
6810 int horizWidth = 0;
6811 int msgheight;
6812 char_u *pstart;
6813 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006814 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 char_u *tbuffer;
6816 RECT rect;
6817 HWND hwnd;
6818 HDC hdc;
6819 HFONT font, oldFont;
6820 TEXTMETRIC fontInfo;
6821 int fontHeight;
6822 int textWidth, minButtonWidth, messageWidth;
6823 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006824 int maxDialogHeight;
6825 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 int vertical;
6827 int dlgPaddingX;
6828 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006829# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006830 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006832# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006833 garray_T ga;
6834 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006835 int dlg_icon_width;
6836 int dlg_icon_height;
6837 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006839# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006840 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006841# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006842 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006843# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006844 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006845 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar748bf032005-02-02 23:04:36 +00006848 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006849 {
6850 load_dpi_func();
6851 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006852 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006853 }
6854 else
6855 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 if ((type < 0) || (type > VIM_LAST_TYPE))
6858 type = 0;
6859
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // allocate some memory for dialog template
6861 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006862 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006863 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865 if (p == NULL)
6866 return -1;
6867
6868 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006869 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6871 * const.
6872 */
6873 tbuffer = vim_strsave(buttons);
6874 if (tbuffer == NULL)
6875 return -1;
6876
Bram Moolenaar734a8672019-12-02 22:49:38 +01006877 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaar734a8672019-12-02 22:49:38 +01006879 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 numButtons = 1;
6881 for (i = 0; tbuffer[i] != '\0'; i++)
6882 {
6883 if (tbuffer[i] == DLG_BUTTON_SEP)
6884 numButtons++;
6885 }
6886 if (dfltbutton >= numButtons)
6887 dfltbutton = -1;
6888
Bram Moolenaar734a8672019-12-02 22:49:38 +01006889 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006890 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (buttonWidths == NULL)
6892 return -1;
6893
Bram Moolenaar734a8672019-12-02 22:49:38 +01006894 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006895 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (buttonPositions == NULL)
6897 return -1;
6898
6899 /*
6900 * Calculate how big the dialog must be.
6901 */
6902 hwnd = GetDesktopWindow();
6903 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006904# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6906 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006907 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 use_lfSysmenu = TRUE;
6909 }
6910 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006911# endif
K.Takatad1c58992022-01-23 12:31:57 +00006912 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6913 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6914
6915 oldFont = SelectFont(hdc, font);
6916 dlgPaddingX = DLG_PADDING_X;
6917 dlgPaddingY = DLG_PADDING_Y;
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 GetTextMetrics(hdc, &fontInfo);
6920 fontHeight = fontInfo.tmHeight;
6921
Bram Moolenaar734a8672019-12-02 22:49:38 +01006922 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006923 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
Bram Moolenaar734a8672019-12-02 22:49:38 +01006925 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006926 if (s_hwnd == NULL)
6927 {
6928 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
Bram Moolenaar734a8672019-12-02 22:49:38 +01006930 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006931 get_work_area(&workarea_rect);
6932 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006933 if (maxDialogWidth > adjust_by_system_dpi(600))
6934 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006936 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006937 }
6938 else
6939 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006940 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006941 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006942 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6944 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6945 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6946 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006947
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006949 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6950 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6951 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6952 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6953 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006954 }
6955
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // Set dlgwidth to width of message.
6957 // Copy the message into "ga", changing NL to CR-NL and inserting line
6958 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 pstart = message;
6960 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961 msgheight = 0;
6962 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 do
6964 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006965 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006966
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 // Need to figure out where to break the string. The system does it
6968 // at a word boundary, which would mean we can't compute the number of
6969 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006970 textWidth = 0;
6971 last_white = NULL;
6972 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006973 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006974 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006975 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006976 && textWidth > maxDialogWidth * 3 / 4)
6977 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006978 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006979 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006982 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006983 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984 textWidth = 0;
6985
6986 if (last_white != NULL)
6987 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006989 if (pend > last_white)
6990 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 pend = last_white + 1;
6992 last_white = NULL;
6993 }
6994 ga_append(&ga, '\r');
6995 ga_append(&ga, '\n');
6996 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006997 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998
6999 while (--l >= 0)
7000 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007001 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007002 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007004
7005 ga_append(&ga, '\r');
7006 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 pstart = pend + 1;
7008 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007009
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007010 if (ga.ga_data != NULL)
7011 message = ga.ga_data;
7012
Bram Moolenaar734a8672019-12-02 22:49:38 +01007013 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007014
K.Takatac81e9bf2022-01-16 14:15:49 +00007015 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7016 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
K.Takatac81e9bf2022-01-16 14:15:49 +00007018 // Add width of icon to dlgwidth, and some space
7019 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7020 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7021
7022 if (msgheight < dlg_icon_height)
7023 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 /*
7026 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007027 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007029 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (!vertical)
7031 {
7032 // Place buttons horizontally if they fit.
7033 horizWidth = dlgPaddingX;
7034 pstart = tbuffer;
7035 i = 0;
7036 do
7037 {
7038 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7039 if (pend == NULL)
7040 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007041 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (textWidth < minButtonWidth)
7043 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007044 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 buttonWidths[i] = textWidth;
7046 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007047 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 pstart = pend + 1;
7049 } while (*pend != NUL);
7050
7051 if (horizWidth > maxDialogWidth)
7052 vertical = TRUE; // Too wide to fit on the screen.
7053 else if (horizWidth > dlgwidth)
7054 dlgwidth = horizWidth;
7055 }
7056
7057 if (vertical)
7058 {
7059 // Stack buttons vertically.
7060 pstart = tbuffer;
7061 do
7062 {
7063 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7064 if (pend == NULL)
7065 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007066 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007067 textWidth += dlgPaddingX; // Padding within button
7068 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (textWidth > dlgwidth)
7070 dlgwidth = textWidth;
7071 pstart = pend + 1;
7072 } while (*pend != NUL);
7073 }
7074
7075 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar734a8672019-12-02 22:49:38 +01007078 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007079 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 add_long(lStyle);
7082 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 add_word(0); // NumberOfItems(will change later)
7085 add_word(10); // x
7086 add_word(10); // y
7087 add_word(PixelToDialogX(dlgwidth)); // cx
7088
7089 // Dialog height.
7090 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007091 dlgheight = msgheight + 2 * dlgPaddingY
7092 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 else
7094 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7095
7096 // Dialog needs to be taller if contains an edit box.
7097 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7098 if (textfield != NULL)
7099 dlgheight += editboxheight;
7100
Bram Moolenaar734a8672019-12-02 22:49:38 +01007101 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007102 if (dlgheight > maxDialogHeight)
7103 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007104 msgheight = msgheight - (dlgheight - maxDialogHeight);
7105 dlgheight = maxDialogHeight;
7106 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007107 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007108 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007109 }
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 add_word(PixelToDialogY(dlgheight));
7112
7113 add_word(0); // Menu
7114 add_word(0); // Class
7115
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007117 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7118 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 p += nchar;
7120
K.Takatad1c58992022-01-23 12:31:57 +00007121 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007122# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007123 if (use_lfSysmenu)
7124 {
7125 // point size
7126 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7127 GetDeviceCaps(hdc, LOGPIXELSY));
7128 wcscpy(p, lfSysmenu.lfFaceName);
7129 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
K.Takatad1c58992022-01-23 12:31:57 +00007131 else
7132# endif
7133 {
7134 *p++ = DLG_FONT_POINT_SIZE; // point size
7135 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7136 }
7137 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 buttonYpos = msgheight + 2 * dlgPaddingY;
7140
7141 if (textfield != NULL)
7142 buttonYpos += editboxheight;
7143
7144 pstart = tbuffer;
7145 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007146 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 for (i = 0; i < numButtons; i++)
7148 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007149 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 for ( pend = pstart;
7151 *pend && (*pend != DLG_BUTTON_SEP);
7152 pend++)
7153 ;
7154
7155 if (*pend)
7156 *pend = '\0';
7157
7158 /*
7159 * old NOTE:
7160 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7161 * the focus to the first tab-able button and in so doing makes that
7162 * the default!! Grrr. Workaround: Make the default button the only
7163 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7164 * he/she can use arrow keys.
7165 *
7166 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007167 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * dialog. Also needed for when the textfield is the default control.
7169 * It appears to work now (perhaps not on Win95?).
7170 */
7171 if (vertical)
7172 {
7173 p = add_dialog_element(p,
7174 (i == dfltbutton
7175 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7176 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 + 2 * fontHeight * i),
7179 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7180 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007181 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183 else
7184 {
7185 p = add_dialog_element(p,
7186 (i == dfltbutton
7187 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7188 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007189 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 PixelToDialogX(buttonWidths[i]),
7191 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007192 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007194 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 }
7196 *pnumitems += numButtons;
7197
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 p = add_dialog_element(p, SS_ICON,
7200 PixelToDialogX(dlgPaddingX),
7201 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007202 PixelToDialogX(dlg_icon_width),
7203 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7205 dlg_icons[type]);
7206
Bram Moolenaar734a8672019-12-02 22:49:38 +01007207 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007208 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007209 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007210 PixelToDialogY(dlgPaddingY),
7211 (WORD)(PixelToDialogX(messageWidth) + 1),
7212 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007213 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar734a8672019-12-02 22:49:38 +01007215 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (textfield != NULL)
7217 {
7218 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7219 PixelToDialogX(2 * dlgPaddingX),
7220 PixelToDialogY(2 * dlgPaddingY + msgheight),
7221 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7222 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007223 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 *pnumitems += 1;
7225 }
7226
7227 *pnumitems += 2;
7228
7229 SelectFont(hdc, oldFont);
7230 DeleteObject(font);
7231 ReleaseDC(hwnd, hdc);
7232
Bram Moolenaar734a8672019-12-02 22:49:38 +01007233 // Let the dialog_callback() function know which button to make default
7234 // If we have an edit box, make that the default. We also need to tell
7235 // dialog_callback() if this dialog contains an edit box or not. We do
7236 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (textfield != NULL)
7238 {
7239 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7240 s_textfield = textfield;
7241 }
7242 else
7243 {
7244 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7245 s_textfield = NULL;
7246 }
7247
Bram Moolenaar734a8672019-12-02 22:49:38 +01007248 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007250 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 (LPDLGTEMPLATE)pdlgtemplate,
7252 s_hwnd,
7253 (DLGPROC)dialog_callback);
7254
7255 LocalFree(LocalHandle(pdlgtemplate));
7256 vim_free(tbuffer);
7257 vim_free(buttonWidths);
7258 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007259 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar734a8672019-12-02 22:49:38 +01007261 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 (void)SetFocus(s_hwnd);
7263
7264 return nchar;
7265}
7266
Bram Moolenaar734a8672019-12-02 22:49:38 +01007267#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269/*
7270 * Put a simple element (basic class) onto a dialog template in memory.
7271 * return a pointer to where the next item should be added.
7272 *
7273 * parameters:
7274 * lStyle = additional style flags
7275 * (be careful, NT3.51 & Win32s will ignore the new ones)
7276 * x,y = x & y positions IN DIALOG UNITS
7277 * w,h = width and height IN DIALOG UNITS
7278 * Id = ID used in messages
7279 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7280 * caption = usually text or resource name
7281 *
7282 * TODO: use the length information noted here to enable the dialog creation
7283 * routines to work out more exactly how much memory they need to alloc.
7284 */
7285 static PWORD
7286add_dialog_element(
7287 PWORD p,
7288 DWORD lStyle,
7289 WORD x,
7290 WORD y,
7291 WORD w,
7292 WORD h,
7293 WORD Id,
7294 WORD clss,
7295 const char *caption)
7296{
7297 int nchar;
7298
Bram Moolenaar734a8672019-12-02 22:49:38 +01007299 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7301 *p++ = LOWORD(lStyle);
7302 *p++ = HIWORD(lStyle);
7303 *p++ = 0; // LOWORD (lExtendedStyle)
7304 *p++ = 0; // HIWORD (lExtendedStyle)
7305 *p++ = x;
7306 *p++ = y;
7307 *p++ = w;
7308 *p++ = h;
7309 *p++ = Id; //9 or 10 words in all
7310
7311 *p++ = (WORD)0xffff;
7312 *p++ = clss; //2 more here
7313
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007314 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 p += nchar;
7316
7317 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7318
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007319 return p; // total = 15 + strlen(caption) words
7320 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
7323
7324/*
7325 * Helper routine. Take an input pointer, return closest pointer that is
7326 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7327 */
7328 static LPWORD
7329lpwAlign(
7330 LPWORD lpIn)
7331{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007332 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007334 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 ul += 3;
7336 ul >>= 2;
7337 ul <<= 2;
7338 return (LPWORD)ul;
7339}
7340
7341/*
7342 * Helper routine. Takes second parameter as Ansi string, copies it to first
7343 * parameter as wide character (16-bits / char) string, and returns integer
7344 * number of wide characters (words) in string (including the trailing wide
7345 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007346 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7347 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 static int
7349nCopyAnsiToWideChar(
7350 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007351 LPSTR lpAnsiIn,
7352 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
7354 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 int i;
7357 WCHAR *wn;
7358
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007359 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007362 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (wn != NULL)
7364 {
7365 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 vim_free(wn);
7368 }
7369 }
7370 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 nChar = MultiByteToWideChar(
7373 enc_codepage > 0 ? enc_codepage : CP_ACP,
7374 MB_PRECOMPOSED,
7375 lpAnsiIn, len,
7376 lpWCStr, len);
7377 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
7381 return nChar;
7382}
7383
7384
7385#ifdef FEAT_TEAROFF
7386/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007387 * Lookup menu handle from "menu_id".
7388 */
7389 static HMENU
7390tearoff_lookup_menuhandle(
7391 vimmenu_T *menu,
7392 WORD menu_id)
7393{
7394 for ( ; menu != NULL; menu = menu->next)
7395 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007396 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007397 continue;
7398 if (menu_is_separator(menu->dname))
7399 continue;
7400 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7401 return menu->submenu_id;
7402 }
7403 return NULL;
7404}
7405
7406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * The callback function for all the modeless dialogs that make up the
7408 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7409 * thinking its menus have been clicked), and go away when closed.
7410 */
7411 static LRESULT CALLBACK
7412tearoff_callback(
7413 HWND hwnd,
7414 UINT message,
7415 WPARAM wParam,
7416 LPARAM lParam)
7417{
7418 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007419 {
7420 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 HandleMouseHide(message, lParam);
7426
7427 if (message == WM_COMMAND)
7428 {
7429 if ((WORD)(LOWORD(wParam)) & 0x8000)
7430 {
7431 POINT mp;
7432 RECT rect;
7433
7434 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7435 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007436 vimmenu_T *menu;
7437
7438 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007440 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7442 (int)rect.right - 8,
7443 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007444 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 s_hwnd,
7446 NULL);
7447 /*
7448 * NOTE: The pop-up menu can eat the mouse up event.
7449 * We deal with this in normal.c.
7450 */
7451 }
7452 }
7453 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007454 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7456 /*
7457 * Give main window the focus back: this is so after
7458 * choosing a tearoff button you can start typing again
7459 * straight away.
7460 */
7461 (void)SetFocus(s_hwnd);
7462 return TRUE;
7463 }
7464 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7465 {
7466 DestroyWindow(hwnd);
7467 return TRUE;
7468 }
7469
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (message == WM_EXITSIZEMOVE)
7472 (void)SetActiveWindow(s_hwnd);
7473
7474 return FALSE;
7475}
7476#endif
7477
7478
7479/*
K.Takatad1c58992022-01-23 12:31:57 +00007480 * Computes the dialog base units based on the current dialog font.
7481 * We don't use the GetDialogBaseUnits() API, because we don't use the
7482 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 */
7484 static void
7485get_dialog_font_metrics(void)
7486{
7487 HDC hdc;
7488 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 SIZE size;
7490#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007491 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492#endif
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007495 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007496 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498#endif
7499 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007500 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
K.Takatad1c58992022-01-23 12:31:57 +00007502 hdc = GetDC(s_hwnd);
7503 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007504 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007505 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
K.Takataabe628e2022-01-23 16:25:17 +00007507 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007508 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
7511#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7512/*
7513 * Create a pseudo-"tearoff menu" based on the child
7514 * items of a given menu pointer.
7515 */
7516 static void
7517gui_mch_tearoff(
7518 char_u *title,
7519 vimmenu_T *menu,
7520 int initX,
7521 int initY)
7522{
7523 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7524 int template_len;
7525 int nchar, textWidth, submenuWidth;
7526 DWORD lStyle;
7527 DWORD lExtendedStyle;
7528 WORD dlgwidth;
7529 WORD menuID;
7530 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 vimmenu_T *the_menu = menu;
7533 HWND hwnd;
7534 HDC hdc;
7535 HFONT font, oldFont;
7536 int col, spaceWidth, len;
7537 int columnWidths[2];
7538 char_u *label, *text;
7539 int acLen = 0;
7540 int nameLen;
7541 int padding0, padding1, padding2 = 0;
7542 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007543 int x;
7544 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007545# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007546 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 /*
7551 * If this menu is already torn off, move it to the mouse position.
7552 */
7553 if (IsWindow(menu->tearoff_handle))
7554 {
7555 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007556 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
7558 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7559 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7560 }
7561 return;
7562 }
7563
7564 /*
7565 * Create a new tearoff.
7566 */
7567 if (*title == MNU_HIDDEN_CHAR)
7568 title++;
7569
Bram Moolenaar734a8672019-12-02 22:49:38 +01007570 // Allocate memory to store the dialog template. It's made bigger when
7571 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 template_len = DLG_ALLOC_SIZE;
7573 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7574 if (p == NULL)
7575 return;
7576
7577 hwnd = GetDesktopWindow();
7578 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007579# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7581 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007582 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 use_lfSysmenu = TRUE;
7584 }
7585 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007586# endif
K.Takatad1c58992022-01-23 12:31:57 +00007587 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7588 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7589
7590 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
Bram Moolenaar734a8672019-12-02 22:49:38 +01007592 // Calculate width of a single space. Used for padding columns to the
7593 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007594 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
Bram Moolenaar734a8672019-12-02 22:49:38 +01007596 // Figure out max width of the text column, the accelerator column and the
7597 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 submenuWidth = 0;
7599 for (col = 0; col < 2; col++)
7600 {
7601 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007602 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007604 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 text = (col == 0) ? pmenu->dname : pmenu->actext;
7606 if (text != NULL && *text != NUL)
7607 {
7608 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7609 if (textWidth > columnWidths[col])
7610 columnWidths[col] = textWidth;
7611 }
7612 if (pmenu->children != NULL)
7613 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7614 }
7615 }
7616 if (columnWidths[1] == 0)
7617 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007618 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (submenuWidth != 0)
7620 columnWidths[0] += submenuWidth;
7621 else
7622 columnWidths[0] += spaceWidth;
7623 }
7624 else
7625 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007626 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7628 columnWidths[1] += submenuWidth;
7629 }
7630
7631 /*
7632 * Now find the total width of our 'menu'.
7633 */
7634 textWidth = columnWidths[0] + columnWidths[1];
7635 if (submenuWidth != 0)
7636 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007637 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7639 textWidth += submenuWidth;
7640 }
7641 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7642 if (textWidth > dlgwidth)
7643 dlgwidth = textWidth;
7644 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7645
Bram Moolenaar734a8672019-12-02 22:49:38 +01007646 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007647 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7650 *p++ = LOWORD(lStyle);
7651 *p++ = HIWORD(lStyle);
7652 *p++ = LOWORD(lExtendedStyle);
7653 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007654 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007656 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007658 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 else
7660 *p++ = PixelToDialogX(initX); // x
7661 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007662 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 else
7664 *p++ = PixelToDialogY(initY); // y
7665 *p++ = PixelToDialogX(dlgwidth); // cx
7666 ptrueheight = p;
7667 *p++ = 0; // dialog height: changed later anyway
7668 *p++ = 0; // Menu
7669 *p++ = 0; // Class
7670
Bram Moolenaar734a8672019-12-02 22:49:38 +01007671 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007673 ? (LPSTR)title
7674 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 p += nchar;
7676
K.Takatad1c58992022-01-23 12:31:57 +00007677 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007678# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007679 if (use_lfSysmenu)
7680 {
7681 // point size
7682 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7683 GetDeviceCaps(hdc, LOGPIXELSY));
7684 wcscpy(p, lfSysmenu.lfFaceName);
7685 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
K.Takatad1c58992022-01-23 12:31:57 +00007687 else
7688# endif
7689 {
7690 *p++ = DLG_FONT_POINT_SIZE; // point size
7691 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7692 }
7693 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694
7695 /*
7696 * Loop over all the items in the menu.
7697 * But skip over the tearbar.
7698 */
7699 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7700 menu = menu->children->next;
7701 else
7702 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007703 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 for ( ; menu != NULL; menu = menu->next)
7705 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007706 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 continue;
7708 if (menu_is_separator(menu->dname))
7709 {
7710 sepPadding += 3;
7711 continue;
7712 }
7713
Bram Moolenaar734a8672019-12-02 22:49:38 +01007714 // Check if there still is plenty of room in the template. Make it
7715 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7717 {
7718 WORD *newp;
7719
7720 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7721 if (newp != NULL)
7722 {
7723 template_len += 4096;
7724 mch_memmove(newp, pdlgtemplate,
7725 (char *)p - (char *)pdlgtemplate);
7726 p = newp + (p - pdlgtemplate);
7727 pnumitems = newp + (pnumitems - pdlgtemplate);
7728 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7729 LocalFree(LocalHandle(pdlgtemplate));
7730 pdlgtemplate = newp;
7731 }
7732 }
7733
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // Figure out minimal length of this menu label. Use "name" for the
7735 // actual text, "dname" for estimating the displayed size. "name"
7736 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 len = nameLen = (int)STRLEN(menu->name);
7738 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7739 (int)STRLEN(menu->dname))) / spaceWidth;
7740 len += padding0;
7741
7742 if (menu->actext != NULL)
7743 {
7744 acLen = (int)STRLEN(menu->actext);
7745 len += acLen;
7746 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7747 }
7748 else
7749 textWidth = 0;
7750 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7751 len += padding1;
7752
7753 if (menu->children == NULL)
7754 {
7755 padding2 = submenuWidth / spaceWidth;
7756 len += padding2;
7757 menuID = (WORD)(menu->id);
7758 }
7759 else
7760 {
7761 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007762 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764
Bram Moolenaar734a8672019-12-02 22:49:38 +01007765 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007766 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (label == NULL)
7768 break;
7769
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007770 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007771 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007773 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 while (padding0-- > 0)
7775 *text++ = ' ';
7776 if (menu->actext != NULL)
7777 {
7778 STRNCPY(text, menu->actext, acLen);
7779 text += acLen;
7780 }
7781 while (padding1-- > 0)
7782 *text++ = ' ';
7783 if (menu->children != NULL)
7784 {
7785 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7786 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7787 }
7788 else
7789 {
7790 while (padding2-- > 0)
7791 *text++ = ' ';
7792 }
7793 *text = NUL;
7794
7795 /*
7796 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7797 * W95/NT4 it makes the tear-off look more like a menu.
7798 */
7799 p = add_dialog_element(p,
7800 BS_PUSHBUTTON|BS_LEFT,
7801 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7802 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7803 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7804 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007805 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 vim_free(label);
7807 (*pnumitems)++;
7808 }
7809
7810 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7811
7812
Bram Moolenaar734a8672019-12-02 22:49:38 +01007813 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007814 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007815 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 (LPDLGTEMPLATE)pdlgtemplate,
7817 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007818 (DLGPROC)tearoff_callback,
7819 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821 LocalFree(LocalHandle(pdlgtemplate));
7822 SelectFont(hdc, oldFont);
7823 DeleteObject(font);
7824 ReleaseDC(hwnd, hdc);
7825
7826 /*
7827 * Reassert ourselves as the active window. This is so that after creating
7828 * a tearoff, the user doesn't have to click with the mouse just to start
7829 * typing again!
7830 */
7831 (void)SetActiveWindow(s_hwnd);
7832
Bram Moolenaar734a8672019-12-02 22:49:38 +01007833 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 force_menu_update = TRUE;
7835}
7836#endif
7837
7838#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007839# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841/*
7842 * Create the toolbar, initially unpopulated.
7843 * (just like the menu, there are no defaults, it's all
7844 * set up through menu.vim)
7845 */
7846 static void
7847initialise_toolbar(void)
7848{
7849 InitCommonControls();
7850 s_toolbarhwnd = CreateToolbarEx(
7851 s_hwnd,
7852 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7853 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007854 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007855 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 IDR_TOOLBAR1, // id of initial bitmap
7857 NULL,
7858 0, // initial number of buttons
7859 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7860 TOOLBAR_BUTTON_HEIGHT,
7861 TOOLBAR_BUTTON_WIDTH,
7862 TOOLBAR_BUTTON_HEIGHT,
7863 sizeof(TBBUTTON)
7864 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007865
7866 // Remove transparency from the toolbar to prevent the main window
7867 // background colour showing through
7868 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7869 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7870
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007871 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872
7873 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007874
7875 update_toolbar_size();
7876}
7877
7878 static void
7879update_toolbar_size(void)
7880{
7881 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007882 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007883
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007884 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007885 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7886 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7887 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7888 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7889
7890 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7891 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7892 //TRACE("button size: %d, %d", w, h);
7893 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7894 gui.toolbar_height = h + 6;
7895
7896 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7897 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7898
7899 // TODO:
7900 // Currently, this function only updates the size of toolbar buttons.
7901 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007904 static LRESULT CALLBACK
7905toolbar_wndproc(
7906 HWND hwnd,
7907 UINT uMsg,
7908 WPARAM wParam,
7909 LPARAM lParam)
7910{
7911 HandleMouseHide(uMsg, lParam);
7912 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7913}
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 static int
7916get_toolbar_bitmap(vimmenu_T *menu)
7917{
7918 int i = -1;
7919
7920 /*
7921 * Check user bitmaps first, unless builtin is specified.
7922 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007923 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
7925 char_u fname[MAXPATHL];
7926 HANDLE hbitmap = NULL;
7927
7928 if (menu->iconfile != NULL)
7929 {
7930 gui_find_iconfile(menu->iconfile, fname, "bmp");
7931 hbitmap = LoadImage(
7932 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007933 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 IMAGE_BITMAP,
7935 TOOLBAR_BUTTON_WIDTH,
7936 TOOLBAR_BUTTON_HEIGHT,
7937 LR_LOADFROMFILE |
7938 LR_LOADMAP3DCOLORS
7939 );
7940 }
7941
7942 /*
7943 * If the LoadImage call failed, or the "icon=" file
7944 * didn't exist or wasn't specified, try the menu name
7945 */
7946 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007947 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007948# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007949 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007950# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007951 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 hbitmap = LoadImage(
7953 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007954 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 IMAGE_BITMAP,
7956 TOOLBAR_BUTTON_WIDTH,
7957 TOOLBAR_BUTTON_HEIGHT,
7958 LR_LOADFROMFILE |
7959 LR_LOADMAP3DCOLORS
7960 );
7961
7962 if (hbitmap != NULL)
7963 {
7964 TBADDBITMAP tbAddBitmap;
7965
7966 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007967 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
7969 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7970 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007971 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 }
7974 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7975 i = menu->iconidx;
7976
7977 return i;
7978}
7979#endif
7980
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007981#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7982 static void
7983initialise_tabline(void)
7984{
7985 InitCommonControls();
7986
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007987 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007988 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007989 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007990 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007991 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007992
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007993 gui.tabline_height = TABLINE_HEIGHT;
7994
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007995 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007996}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007997
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007998/*
7999 * Get tabpage_T from POINT.
8000 */
8001 static tabpage_T *
8002GetTabFromPoint(
8003 HWND hWnd,
8004 POINT pt)
8005{
8006 tabpage_T *ptp = NULL;
8007
8008 if (gui_mch_showing_tabline())
8009 {
8010 TCHITTESTINFO htinfo;
8011 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008012 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008013 if (s_tabhwnd == hWnd)
8014 {
8015 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8016 if (idx != -1)
8017 ptp = find_tabpage(idx + 1);
8018 }
8019 }
8020 return ptp;
8021}
8022
8023static POINT s_pt = {0, 0};
8024static HCURSOR s_hCursor = NULL;
8025
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008026 static LRESULT CALLBACK
8027tabline_wndproc(
8028 HWND hwnd,
8029 UINT uMsg,
8030 WPARAM wParam,
8031 LPARAM lParam)
8032{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008033 POINT pt;
8034 tabpage_T *tp;
8035 RECT rect;
8036 int nCenter;
8037 int idx0;
8038 int idx1;
8039
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008040 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008041
8042 switch (uMsg)
8043 {
8044 case WM_LBUTTONDOWN:
8045 {
8046 s_pt.x = GET_X_LPARAM(lParam);
8047 s_pt.y = GET_Y_LPARAM(lParam);
8048 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008049 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008050 break;
8051 }
8052 case WM_MOUSEMOVE:
8053 if (GetCapture() == hwnd
8054 && ((wParam & MK_LBUTTON)) != 0)
8055 {
8056 pt.x = GET_X_LPARAM(lParam);
8057 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008058 if (abs(pt.x - s_pt.x) >
8059 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008060 {
8061 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8062
8063 tp = GetTabFromPoint(hwnd, pt);
8064 if (tp != NULL)
8065 {
8066 idx0 = tabpage_index(curtab) - 1;
8067 idx1 = tabpage_index(tp) - 1;
8068
8069 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8070 nCenter = rect.left + (rect.right - rect.left) / 2;
8071
Bram Moolenaar734a8672019-12-02 22:49:38 +01008072 // Check if the mouse cursor goes over the center of
8073 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008074 if ((idx0 < idx1) && (nCenter < pt.x))
8075 {
8076 tabpage_move(idx1 + 1);
8077 update_screen(0);
8078 }
8079 else if ((idx1 < idx0) && (pt.x < nCenter))
8080 {
8081 tabpage_move(idx1);
8082 update_screen(0);
8083 }
8084 }
8085 }
8086 }
8087 break;
8088 case WM_LBUTTONUP:
8089 {
8090 if (GetCapture() == hwnd)
8091 {
8092 SetCursor(s_hCursor);
8093 ReleaseCapture();
8094 }
8095 break;
8096 }
regomne3bdef102022-09-26 20:48:32 +01008097 case WM_MBUTTONUP:
8098 {
8099 TCHITTESTINFO htinfo;
8100
8101 htinfo.pt.x = GET_X_LPARAM(lParam);
8102 htinfo.pt.y = GET_Y_LPARAM(lParam);
8103 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8104 if (idx0 != -1)
8105 {
8106 idx0 += 1;
8107 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8108 }
8109 break;
8110 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008111 default:
8112 break;
8113 }
8114
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008115 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8116}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008117#endif
8118
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8120/*
8121 * Make the GUI window come to the foreground.
8122 */
8123 void
8124gui_mch_set_foreground(void)
8125{
8126 if (IsIconic(s_hwnd))
8127 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8128 SetForegroundWindow(s_hwnd);
8129}
8130#endif
8131
8132#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8133 static void
8134dyn_imm_load(void)
8135{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008136 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (hLibImm == NULL)
8138 return;
8139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008141 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008143 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008145 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008147 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008149 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008151 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008152 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008153 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008154 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008155 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008157 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008159 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008160 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008161 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
K.Takatab0b2b732022-01-19 12:59:21 +00008163 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 || pImmGetContext == NULL
8165 || pImmAssociateContext == NULL
8166 || pImmReleaseContext == NULL
8167 || pImmGetOpenStatus == NULL
8168 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008169 || pImmGetCompositionFontW == NULL
8170 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008172 || pImmGetConversionStatus == NULL
8173 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 FreeLibrary(hLibImm);
8176 hLibImm = NULL;
8177 pImmGetContext = NULL;
8178 return;
8179 }
8180
8181 return;
8182}
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#endif
8185
8186#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8187
8188# ifdef FEAT_XPM_W32
8189# define IMAGE_XPM 100
8190# endif
8191
8192typedef struct _signicon_t
8193{
8194 HANDLE hImage;
8195 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008196# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008197 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199} signicon_t;
8200
8201 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008202gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203{
8204 signicon_t *sign;
8205 int x, y, w, h;
8206
8207 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8208 return;
8209
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008210# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008211 if (IS_ENABLE_DIRECTX())
8212 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008213# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 x = TEXT_X(col);
8216 y = TEXT_Y(row);
8217 w = gui.char_width * 2;
8218 h = gui.char_height;
8219 switch (sign->uType)
8220 {
8221 case IMAGE_BITMAP:
8222 {
8223 HDC hdcMem;
8224 HBITMAP hbmpOld;
8225
8226 hdcMem = CreateCompatibleDC(s_hdc);
8227 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8228 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8229 SelectObject(hdcMem, hbmpOld);
8230 DeleteDC(hdcMem);
8231 }
8232 break;
8233 case IMAGE_ICON:
8234 case IMAGE_CURSOR:
8235 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8236 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008237# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 case IMAGE_XPM:
8239 {
8240 HDC hdcMem;
8241 HBITMAP hbmpOld;
8242
8243 hdcMem = CreateCompatibleDC(s_hdc);
8244 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008245 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8247
8248 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008249 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8251 SelectObject(hdcMem, hbmpOld);
8252 DeleteDC(hdcMem);
8253 }
8254 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 }
8257}
8258
8259 static void
8260close_signicon_image(signicon_t *sign)
8261{
8262 if (sign)
8263 switch (sign->uType)
8264 {
8265 case IMAGE_BITMAP:
8266 DeleteObject((HGDIOBJ)sign->hImage);
8267 break;
8268 case IMAGE_CURSOR:
8269 DestroyCursor((HCURSOR)sign->hImage);
8270 break;
8271 case IMAGE_ICON:
8272 DestroyIcon((HICON)sign->hImage);
8273 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008274# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 case IMAGE_XPM:
8276 DeleteObject((HBITMAP)sign->hImage);
8277 DeleteObject((HBITMAP)sign->hShape);
8278 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
8281}
8282
8283 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008284gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
8286 signicon_t sign, *psign;
8287 char_u *ext;
8288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008290 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 if (ext > signfile)
8292 {
8293 int do_load = 1;
8294
8295 if (!STRICMP(ext, ".bmp"))
8296 sign.uType = IMAGE_BITMAP;
8297 else if (!STRICMP(ext, ".ico"))
8298 sign.uType = IMAGE_ICON;
8299 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8300 sign.uType = IMAGE_CURSOR;
8301 else
8302 do_load = 0;
8303
8304 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008305 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 gui.char_width * 2, gui.char_height,
8307 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008308# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (!STRICMP(ext, ".xpm"))
8310 {
8311 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008312 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8313 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317
8318 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008319 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 *psign = sign;
8321
8322 if (!psign)
8323 {
8324 if (sign.hImage)
8325 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008326 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 return (void *)psign;
8329
8330}
8331
8332 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008333gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 if (sign)
8336 {
8337 close_signicon_image((signicon_t *)sign);
8338 vim_free(sign);
8339 }
8340}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008343#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar734a8672019-12-02 22:49:38 +01008345/*
8346 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008347 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008349 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8351 * to get current mouse position).
8352 *
8353 * Trying to use as more Windows services as possible, and as less
8354 * IE version as possible :)).
8355 *
8356 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8357 * BalloonEval struct.
8358 * 2) Enable/Disable simply create/kill BalloonEval Timer
8359 * 3) When there was enough inactivity, timer procedure posts
8360 * async request to debugger
8361 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8362 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008363 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 */
8365
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008366 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008367make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008368{
K.Takata76687d22022-01-25 10:31:37 +00008369 TOOLINFOW *pti;
8370 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008371
K.Takata76687d22022-01-25 10:31:37 +00008372 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008373 if (pti == NULL)
8374 return;
8375
8376 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8377 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8378 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008379 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008380
8381 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8382 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8383
K.Takata76687d22022-01-25 10:31:37 +00008384 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008385 pti->uFlags = TTF_SUBCLASS;
8386 pti->hwnd = beval->target;
8387 pti->hinst = 0; // Don't use string resources
8388 pti->uId = ID_BEVAL_TOOLTIP;
8389
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008390 pti->lpszText = LPSTR_TEXTCALLBACKW;
8391 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8392 pti->lParam = (LPARAM)beval->tofree;
8393 // switch multiline tooltips on
8394 if (GetClientRect(s_textArea, &rect))
8395 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8396 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008397
8398 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8399 pti->rect.left = pt.x - 3;
8400 pti->rect.top = pt.y - 3;
8401 pti->rect.right = pt.x + 3;
8402 pti->rect.bottom = pt.y + 3;
8403
8404 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8405 // Make tooltip appear sooner.
8406 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8407 // I've performed some tests and it seems the longest possible life time
8408 // of tooltip is 30 seconds.
8409 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8410 /*
8411 * HACK: force tooltip to appear, because it'll not appear until
8412 * first mouse move. D*mn M$
8413 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8414 */
8415 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8416 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8417 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008421delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008423 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
8426 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008427beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008428 HWND hwnd UNUSED,
8429 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008430 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008431 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 POINT pt;
8434 RECT rect;
8435
8436 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8437 return;
8438
8439 GetCursorPos(&pt);
8440 if (WindowFromPoint(pt) != s_textArea)
8441 return;
8442
8443 ScreenToClient(s_textArea, &pt);
8444 GetClientRect(s_textArea, &rect);
8445 if (!PtInRect(&rect, pt))
8446 return;
8447
K.Takataa8ec4912022-02-03 14:32:33 +00008448 if (last_user_activity > 0
8449 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 && (cur_beval->showState != ShS_PENDING
8451 || abs(cur_beval->x - pt.x) > 3
8452 || abs(cur_beval->y - pt.y) > 3))
8453 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008454 // Pointer resting in one place long enough, it's time to show
8455 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 cur_beval->showState = ShS_PENDING;
8457 cur_beval->x = pt.x;
8458 cur_beval->y = pt.y;
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (cur_beval->msgCB != NULL)
8461 (*cur_beval->msgCB)(cur_beval, 0);
8462 }
8463}
8464
8465 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008466gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
K.Takataa8ec4912022-02-03 14:32:33 +00008468 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
8470
8471 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008472gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 if (beval == NULL)
8475 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008476 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8477 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478}
8479
8480 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008481gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008484
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008485 vim_free(beval->msg);
8486 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8487 if (beval->msg == NULL)
8488 {
8489 delete_tooltip(beval);
8490 beval->showState = ShS_NEUTRAL;
8491 return;
8492 }
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 if (beval->showState == ShS_SHOWING)
8495 return;
8496 GetCursorPos(&pt);
8497 ScreenToClient(s_textArea, &pt);
8498
8499 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008501 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 gui_mch_disable_beval_area(cur_beval);
8503 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008504 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
8507
8508 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008509gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008510 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008511 char_u *mesg,
8512 void (*mesgCB)(BalloonEval *, int),
8513 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008515 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 BalloonEval *beval;
8517
8518 if (mesg != NULL && mesgCB != NULL)
8519 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008520 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 return NULL;
8522 }
8523
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008524 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (beval != NULL)
8526 {
8527 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 beval->msg = mesg;
8531 beval->msgCB = mesgCB;
8532 beval->clientData = clientData;
8533
8534 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 cur_beval = beval;
8536
8537 if (p_beval)
8538 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
8540 return beval;
8541}
8542
8543 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008544Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008546 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 return;
8548
8549 if (cur_beval != NULL)
8550 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008551 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008553 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008554 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008555 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 delete_tooltip(cur_beval);
8557 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008560 break;
8561 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008562 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008563 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008564 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008565 info->lpszText = (LPSTR)info->lParam;
8566 info->uFlags |= TTF_DI_SETITEM;
8567 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008568 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008569 case TTN_GETDISPINFOW:
8570 {
8571 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008572 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008573 info->lpszText = (LPWSTR)info->lParam;
8574 info->uFlags |= TTF_DI_SETITEM;
8575 }
8576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 }
8578 }
8579}
8580
8581 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008582track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8585 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008586 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008590gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008592# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008593 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008594# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008595 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 vim_free(beval);
8597}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008598#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8601/*
8602 * We have multiple signs to draw at the same location. Draw the
8603 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8604 */
8605 void
8606netbeans_draw_multisign_indicator(int row)
8607{
8608 int i;
8609 int y;
8610 int x;
8611
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008612 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008613 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 x = 0;
8616 y = TEXT_Y(row);
8617
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008618# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008619 if (IS_ENABLE_DIRECTX())
8620 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008621# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 for (i = 0; i < gui.char_height - 3; i++)
8624 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8625
8626 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8627 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8628 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8629 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8630 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8631 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8632 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8633}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008634#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008635
8636#if defined(FEAT_EVAL) || defined(PROTO)
8637 int
8638test_gui_w32_sendevent(dict_T *args)
8639{
8640 char_u *event;
8641 INPUT inputs[1];
8642
Bram Moolenaard61efa52022-07-23 09:52:04 +01008643 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008644 if (event == NULL)
8645 return FALSE;
8646
8647 ZeroMemory(inputs, sizeof(inputs));
8648
8649 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8650 {
8651 WORD vkCode;
8652
Bram Moolenaard61efa52022-07-23 09:52:04 +01008653 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008654 if (vkCode <= 0 || vkCode >= 0xFF)
8655 {
8656 semsg(_(e_invalid_argument_nr), (long)vkCode);
8657 return FALSE;
8658 }
8659
8660 inputs[0].type = INPUT_KEYBOARD;
8661 inputs[0].ki.wVk = vkCode;
8662 if (STRICMP(event, "keyup") == 0)
8663 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
K.Takatafef38d82022-09-07 19:03:42 +01008664 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008665 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8666 }
8667 else
8668 semsg(_(e_invalid_argument_str), event);
8669
8670 vim_free(event);
8671
8672 return TRUE;
8673}
8674#endif