blob: 6733ac062463ed6ceb5b37f7170a3c1b245e9355 [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;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100855 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
856 // is pressed, Windows claims that Ctrl is hold as well. That way we can
857 // recognize Right-ALT alone and be sure that not AltGr is hold.
858 if (!(GetKeyState(VK_CONTROL) & 0x8000)
859 && (GetKeyState(VK_RMENU) & 0x8000)
860 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
861 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100862
863 return modifiers;
864}
865
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100866/*
867 * Key hit, add it to the input buffer.
868 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100869 static void
870_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100871 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100872 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100873 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874{
875 char_u string[40];
876 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100877 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100878 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879
Anton Sharonov3f026672022-07-26 21:26:18 +0100880 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
881 return;
882
883 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
884 // process_message()
885 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
886 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100887
LemonBoy45684c62022-04-24 15:46:42 +0100888 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100889
890 ch = simplify_key(ch, &modifiers);
891 // remove the SHIFT modifier for keys where it's already included, e.g.,
892 // '(' and '*'
893 modifiers = may_remove_shift_modifier(modifiers, ch);
894
895 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
896 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
897 if (ch == CSI)
898 ch = K_CSI;
899
900 if (modifiers)
901 {
902 string[0] = CSI;
903 string[1] = KS_MODIFIER;
904 string[2] = modifiers;
905 add_to_input_buf(string, 3);
906 }
907
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100908 len = char_to_string(ch, string, 40, FALSE);
909 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
910 {
911 trash_input_buf();
912 got_int = TRUE;
913 }
914
915 add_to_input_buf(string, len);
916}
917
918/*
919 * Alt-Key hit, add it to the input buffer.
920 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100921 static void
922_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100923 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100924 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100925 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100926{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100927 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100928 int len;
929 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100930 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100931
Anton Sharonov3f026672022-07-26 21:26:18 +0100932 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100933
Bram Moolenaar734a8672019-12-02 22:49:38 +0100934 // OK, we have a character key (given by ch) which was entered with the
935 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
936 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
937 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100938 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100940 // remove the SHIFT modifier for keys where it's already included, e.g.,
941 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200942 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100943
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200944 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
945 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100946 if (ch == CSI)
947 ch = K_CSI;
948
949 len = 0;
950 if (modifiers)
951 {
952 string[len++] = CSI;
953 string[len++] = KS_MODIFIER;
954 string[len++] = modifiers;
955 }
956
957 if (IS_SPECIAL((int)ch))
958 {
959 string[len++] = CSI;
960 string[len++] = K_SECOND((int)ch);
961 string[len++] = K_THIRD((int)ch);
962 }
963 else
964 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100965 // Although the documentation isn't clear about it, we assume "ch" is
966 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100967 len += char_to_string(ch, string + len, 40 - len, TRUE);
968 }
969
970 add_to_input_buf(string, len);
971}
972
973 static void
974_OnMouseEvent(
975 int button,
976 int x,
977 int y,
978 int repeated_click,
979 UINT keyFlags)
980{
981 int vim_modifiers = 0x0;
982
983 s_getting_focus = FALSE;
984
985 if (keyFlags & MK_SHIFT)
986 vim_modifiers |= MOUSE_SHIFT;
987 if (keyFlags & MK_CONTROL)
988 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +0100989 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100990 vim_modifiers |= MOUSE_ALT;
991
992 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
993}
994
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100995 static void
996_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100997 HWND hwnd UNUSED,
998 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100999 int x,
1000 int y,
1001 UINT keyFlags)
1002{
1003 static LONG s_prevTime = 0;
1004
1005 LONG currentTime = GetMessageTime();
1006 int button = -1;
1007 int repeated_click;
1008
Bram Moolenaar734a8672019-12-02 22:49:38 +01001009 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001010 (void)SetFocus(s_hwnd);
1011
1012 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1013 button = MOUSE_LEFT;
1014 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1015 button = MOUSE_MIDDLE;
1016 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1017 button = MOUSE_RIGHT;
1018 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1019 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001020 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1021 }
1022 else if (s_uMsg == WM_CAPTURECHANGED)
1023 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001024 // on W95/NT4, somehow you get in here with an odd Msg
1025 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001026 if (s_button_pending == MOUSE_LEFT)
1027 button = MOUSE_RIGHT;
1028 else
1029 button = MOUSE_LEFT;
1030 }
1031 if (button >= 0)
1032 {
1033 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1034
1035 /*
1036 * Holding down the left and right buttons simulates pushing the middle
1037 * button.
1038 */
1039 if (repeated_click
1040 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1041 || (button == MOUSE_RIGHT
1042 && s_button_pending == MOUSE_LEFT)))
1043 {
1044 /*
1045 * Hmm, gui.c will ignore more than one button down at a time, so
1046 * pretend we let go of it first.
1047 */
1048 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1049 button = MOUSE_MIDDLE;
1050 repeated_click = FALSE;
1051 s_button_pending = -1;
1052 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1053 }
1054 else if ((repeated_click)
1055 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1056 {
1057 if (s_button_pending > -1)
1058 {
K.Takata45f9cfb2022-01-21 11:11:00 +00001059 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1060 s_button_pending = -1;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001061 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01001062 // TRACE("Button down at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001063 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1064 }
1065 else
1066 {
1067 /*
1068 * If this is the first press (i.e. not a multiple click) don't
1069 * action immediately, but store and wait for:
1070 * i) button-up
1071 * ii) mouse move
1072 * iii) another button press
1073 * before using it.
1074 * This enables us to make left+right simulate middle button,
1075 * without left or right being actioned first. The side-effect is
1076 * that if you click and hold the mouse without dragging, the
1077 * cursor doesn't move until you release the button. In practice
1078 * this is hardly a problem.
1079 */
1080 s_button_pending = button;
1081 s_x_pending = x;
1082 s_y_pending = y;
1083 s_kFlags_pending = keyFlags;
1084 }
1085
1086 s_prevTime = currentTime;
1087 }
1088}
1089
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001090 static void
1091_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001092 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001093 int x,
1094 int y,
1095 UINT keyFlags)
1096{
1097 int button;
1098
1099 s_getting_focus = FALSE;
1100 if (s_button_pending > -1)
1101 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001102 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001103 _OnMouseEvent(s_button_pending, s_x_pending,
1104 s_y_pending, FALSE, s_kFlags_pending);
1105 s_button_pending = -1;
1106 }
1107 if (s_uMsg == WM_MOUSEMOVE)
1108 {
1109 /*
1110 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1111 * down.
1112 */
1113 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1114 | MK_XBUTTON1 | MK_XBUTTON2)))
1115 {
1116 gui_mouse_moved(x, y);
1117 return;
1118 }
1119
1120 /*
1121 * While button is down, keep grabbing mouse move events when
1122 * the mouse goes outside the window
1123 */
1124 SetCapture(s_textArea);
1125 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001126 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001127 }
1128 else
1129 {
1130 ReleaseCapture();
1131 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001132 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001133 }
1134
1135 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1136}
1137
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001138 static void
1139_OnSizeTextArea(
1140 HWND hwnd UNUSED,
1141 UINT state UNUSED,
1142 int cx UNUSED,
1143 int cy UNUSED)
1144{
1145#if defined(FEAT_DIRECTX)
1146 if (IS_ENABLE_DIRECTX())
1147 directx_binddc();
1148#endif
1149}
1150
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001151#ifdef FEAT_MENU
1152/*
1153 * Find the vimmenu_T with the given id
1154 */
1155 static vimmenu_T *
1156gui_mswin_find_menu(
1157 vimmenu_T *pMenu,
1158 int id)
1159{
1160 vimmenu_T *pChildMenu;
1161
1162 while (pMenu)
1163 {
1164 if (pMenu->id == (UINT)id)
1165 break;
1166 if (pMenu->children != NULL)
1167 {
1168 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1169 if (pChildMenu)
1170 {
1171 pMenu = pChildMenu;
1172 break;
1173 }
1174 }
1175 pMenu = pMenu->next;
1176 }
1177 return pMenu;
1178}
1179
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180 static void
1181_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001182 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001183 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001184 HWND hwndCtl UNUSED,
1185 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001186{
1187 vimmenu_T *pMenu;
1188
1189 pMenu = gui_mswin_find_menu(root_menu, id);
1190 if (pMenu)
1191 gui_menu_cb(pMenu);
1192}
1193#endif
1194
1195#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001196/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001197 * Handle a Find/Replace window message.
1198 */
1199 static void
1200_OnFindRepl(void)
1201{
1202 int flags = 0;
1203 int down;
1204
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001205 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001206 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001207 (void)SetFocus(s_hwnd);
1208
1209 if (s_findrep_struct.Flags & FR_FINDNEXT)
1210 {
1211 flags = FRD_FINDNEXT;
1212
Bram Moolenaar734a8672019-12-02 22:49:38 +01001213 // Give main window the focus back: this is so the cursor isn't
1214 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001215 (void)SetFocus(s_hwnd);
1216 }
1217 else if (s_findrep_struct.Flags & FR_REPLACE)
1218 {
1219 flags = FRD_REPLACE;
1220
Bram Moolenaar734a8672019-12-02 22:49:38 +01001221 // Give main window the focus back: this is so the cursor isn't
1222 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001223 (void)SetFocus(s_hwnd);
1224 }
1225 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1226 {
1227 flags = FRD_REPLACEALL;
1228 }
1229
1230 if (flags != 0)
1231 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001232 char_u *p, *q;
1233
Bram Moolenaar734a8672019-12-02 22:49:38 +01001234 // Call the generic GUI function to do the actual work.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1236 flags |= FRD_WHOLE_WORD;
1237 if (s_findrep_struct.Flags & FR_MATCHCASE)
1238 flags |= FRD_MATCH_CASE;
1239 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001240 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1241 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1242 if (p != NULL && q != NULL)
1243 gui_do_findrepl(flags, p, q, down);
1244 vim_free(p);
1245 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001246 }
1247}
1248#endif
1249
1250 static void
1251HandleMouseHide(UINT uMsg, LPARAM lParam)
1252{
1253 static LPARAM last_lParam = 0L;
1254
Bram Moolenaar734a8672019-12-02 22:49:38 +01001255 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001256 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1257 {
1258 if (lParam == last_lParam)
1259 return;
1260 last_lParam = lParam;
1261 }
1262
Bram Moolenaar734a8672019-12-02 22:49:38 +01001263 // Handle specially, to centralise coding. We need to be sure we catch all
1264 // possible events which should cause us to restore the cursor (as it is a
1265 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001266 switch (uMsg)
1267 {
1268 case WM_KEYUP:
1269 case WM_CHAR:
1270 /*
1271 * blank out the pointer if necessary
1272 */
1273 if (p_mh)
1274 gui_mch_mousehide(TRUE);
1275 break;
1276
Bram Moolenaar734a8672019-12-02 22:49:38 +01001277 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001279 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001280 case WM_LBUTTONDOWN:
1281 case WM_LBUTTONUP:
1282 case WM_MBUTTONDOWN:
1283 case WM_MBUTTONUP:
1284 case WM_RBUTTONDOWN:
1285 case WM_RBUTTONUP:
1286 case WM_XBUTTONDOWN:
1287 case WM_XBUTTONUP:
1288 case WM_NCMOUSEMOVE:
1289 case WM_NCLBUTTONDOWN:
1290 case WM_NCLBUTTONUP:
1291 case WM_NCMBUTTONDOWN:
1292 case WM_NCMBUTTONUP:
1293 case WM_NCRBUTTONDOWN:
1294 case WM_NCRBUTTONUP:
1295 case WM_KILLFOCUS:
1296 /*
1297 * if the pointer is currently hidden, then we should show it.
1298 */
1299 gui_mch_mousehide(FALSE);
1300 break;
1301 }
1302}
1303
1304 static LRESULT CALLBACK
1305_TextAreaWndProc(
1306 HWND hwnd,
1307 UINT uMsg,
1308 WPARAM wParam,
1309 LPARAM lParam)
1310{
1311 /*
1312 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1313 hwnd, uMsg, wParam, lParam);
1314 */
1315
1316 HandleMouseHide(uMsg, lParam);
1317
1318 s_uMsg = uMsg;
1319 s_wParam = wParam;
1320 s_lParam = lParam;
1321
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001322#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001323 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001324#endif
1325
1326 switch (uMsg)
1327 {
1328 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1329 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1330 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1331 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1332 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1333 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1334 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1335 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1336 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1337 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1338 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1339 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1340 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1341 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001342 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001343
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001344#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001345 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1346 return TRUE;
1347#endif
1348 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001349 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001350 }
1351}
1352
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001353/*
1354 * Called when the foreground or background color has been changed.
1355 */
1356 void
1357gui_mch_new_colors(void)
1358{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001359 HBRUSH prevBrush;
1360
1361 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001362 prevBrush = (HBRUSH)SetClassLongPtr(
1363 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001364 InvalidateRect(s_hwnd, NULL, TRUE);
1365 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001366}
1367
1368/*
1369 * Set the colors to their default values.
1370 */
1371 void
1372gui_mch_def_colors(void)
1373{
1374 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1375 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1376 gui.def_norm_pixel = gui.norm_pixel;
1377 gui.def_back_pixel = gui.back_pixel;
1378}
1379
1380/*
1381 * Open the GUI window which was created by a call to gui_mch_init().
1382 */
1383 int
1384gui_mch_open(void)
1385{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001386 // Actually open the window, if not already visible
1387 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001388 if (!IsWindowVisible(s_hwnd))
1389 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1390
1391#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001392 // Init replace string here, so that we keep it when re-opening the
1393 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001394 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1395#endif
1396
1397 return OK;
1398}
1399
1400/*
1401 * Get the position of the top left corner of the window.
1402 */
1403 int
1404gui_mch_get_winpos(int *x, int *y)
1405{
1406 RECT rect;
1407
1408 GetWindowRect(s_hwnd, &rect);
1409 *x = rect.left;
1410 *y = rect.top;
1411 return OK;
1412}
1413
1414/*
1415 * Set the position of the top left corner of the window to the given
1416 * coordinates.
1417 */
1418 void
1419gui_mch_set_winpos(int x, int y)
1420{
1421 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1422 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1423}
1424 void
1425gui_mch_set_text_area_pos(int x, int y, int w, int h)
1426{
1427 static int oldx = 0;
1428 static int oldy = 0;
1429
1430 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1431
1432#ifdef FEAT_TOOLBAR
1433 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1434 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001435 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001436#endif
1437#if defined(FEAT_GUI_TABLINE)
1438 if (showing_tabline)
1439 {
1440 int top = 0;
1441 RECT rect;
1442
1443# ifdef FEAT_TOOLBAR
1444 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001445 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001446# endif
1447 GetClientRect(s_hwnd, &rect);
1448 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1449 }
1450#endif
1451
Bram Moolenaar734a8672019-12-02 22:49:38 +01001452 // When side scroll bar is unshown, the size of window will change.
1453 // then, the text area move left or right. thus client rect should be
1454 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001455 if (oldx != x || oldy != y)
1456 {
1457 InvalidateRect(s_hwnd, NULL, FALSE);
1458 oldx = x;
1459 oldy = y;
1460 }
1461}
1462
1463
1464/*
1465 * Scrollbar stuff:
1466 */
1467
1468 void
1469gui_mch_enable_scrollbar(
1470 scrollbar_T *sb,
1471 int flag)
1472{
1473 ShowScrollBar(sb->id, SB_CTL, flag);
1474
Bram Moolenaar734a8672019-12-02 22:49:38 +01001475 // TODO: When the window is maximized, the size of the window stays the
1476 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1477 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001478}
1479
1480 void
1481gui_mch_set_scrollbar_pos(
1482 scrollbar_T *sb,
1483 int x,
1484 int y,
1485 int w,
1486 int h)
1487{
1488 SetWindowPos(sb->id, NULL, x, y, w, h,
1489 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1490}
1491
Bram Moolenaar203ec772020-07-17 20:43:43 +02001492 int
1493gui_mch_get_scrollbar_xpadding(void)
1494{
1495 RECT rcTxt, rcWnd;
1496 int xpad;
1497
1498 GetWindowRect(s_textArea, &rcTxt);
1499 GetWindowRect(s_hwnd, &rcWnd);
1500 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001501 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1502 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001503 return (xpad < 0) ? 0 : xpad;
1504}
1505
1506 int
1507gui_mch_get_scrollbar_ypadding(void)
1508{
1509 RECT rcTxt, rcWnd;
1510 int ypad;
1511
1512 GetWindowRect(s_textArea, &rcTxt);
1513 GetWindowRect(s_hwnd, &rcWnd);
1514 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001515 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1516 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001517 return (ypad < 0) ? 0 : ypad;
1518}
1519
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001520 void
1521gui_mch_create_scrollbar(
1522 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001523 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001524{
1525 sb->id = CreateWindow(
1526 "SCROLLBAR", "Scrollbar",
1527 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001528 10, // Any value will do for now
1529 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001530 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001531 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532}
1533
1534/*
1535 * Find the scrollbar with the given hwnd.
1536 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001537 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001538gui_mswin_find_scrollbar(HWND hwnd)
1539{
1540 win_T *wp;
1541
1542 if (gui.bottom_sbar.id == hwnd)
1543 return &gui.bottom_sbar;
1544 FOR_ALL_WINDOWS(wp)
1545 {
1546 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1547 return &wp->w_scrollbars[SBAR_LEFT];
1548 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1549 return &wp->w_scrollbars[SBAR_RIGHT];
1550 }
1551 return NULL;
1552}
1553
K.Takatac81e9bf2022-01-16 14:15:49 +00001554 static void
1555update_scrollbar_size(void)
1556{
1557 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1558 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1559}
1560
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001561/*
K.Takataabe628e2022-01-23 16:25:17 +00001562 * Get the average character size of a font.
1563 */
1564 static void
1565GetAverageFontSize(HDC hdc, SIZE *size)
1566{
1567 // GetTextMetrics() may not return the right value in tmAveCharWidth
1568 // for some fonts. Do our own average computation.
1569 GetTextExtentPoint(hdc,
1570 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1571 52, size);
1572 size->cx = (size->cx / 26 + 1) / 2;
1573}
1574
1575/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001576 * Get the character size of a font.
1577 */
1578 static void
1579GetFontSize(GuiFont font)
1580{
1581 HWND hwnd = GetDesktopWindow();
1582 HDC hdc = GetWindowDC(hwnd);
1583 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001584 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001585 TEXTMETRIC tm;
1586
1587 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001588 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001589
K.Takataabe628e2022-01-23 16:25:17 +00001590 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001591 gui.char_height = tm.tmHeight + p_linespace;
1592
1593 SelectFont(hdc, hfntOld);
1594
1595 ReleaseDC(hwnd, hdc);
1596}
1597
1598/*
1599 * Adjust gui.char_height (after 'linespace' was changed).
1600 */
1601 int
1602gui_mch_adjust_charheight(void)
1603{
1604 GetFontSize(gui.norm_font);
1605 return OK;
1606}
1607
1608 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001609get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001610{
1611 HFONT font = NULL;
1612
Bram Moolenaar734a8672019-12-02 22:49:38 +01001613 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001614 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001615
1616 if (font == NULL)
1617 return NOFONT;
1618
1619 return (GuiFont)font;
1620}
1621
1622 static int
1623pixels_to_points(int pixels, int vertical)
1624{
1625 int points;
1626 HWND hwnd;
1627 HDC hdc;
1628
1629 hwnd = GetDesktopWindow();
1630 hdc = GetWindowDC(hwnd);
1631
1632 points = MulDiv(pixels, 72,
1633 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1634
1635 ReleaseDC(hwnd, hdc);
1636
1637 return points;
1638}
1639
1640 GuiFont
1641gui_mch_get_font(
1642 char_u *name,
1643 int giveErrorIfMissing)
1644{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001645 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001646 GuiFont font = NOFONT;
1647
1648 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001649 {
1650 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001651 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001652 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001653 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001654 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001655 return font;
1656}
1657
1658#if defined(FEAT_EVAL) || defined(PROTO)
1659/*
1660 * Return the name of font "font" in allocated memory.
1661 * Don't know how to get the actual name, thus use the provided name.
1662 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001663 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001664gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665{
1666 if (name == NULL)
1667 return NULL;
1668 return vim_strsave(name);
1669}
1670#endif
1671
1672 void
1673gui_mch_free_font(GuiFont font)
1674{
1675 if (font)
1676 DeleteObject((HFONT)font);
1677}
1678
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001679/*
1680 * Return the Pixel value (color) for the given color name.
1681 * Return INVALCOLOR for error.
1682 */
1683 guicolor_T
1684gui_mch_get_color(char_u *name)
1685{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001686 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001687
1688 typedef struct SysColorTable
1689 {
1690 char *name;
1691 int color;
1692 } SysColorTable;
1693
1694 static SysColorTable sys_table[] =
1695 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001696 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1697 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001698#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001699 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1700#endif
1701 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1702 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1703 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1704 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1705 {"SYS_DESKTOP", COLOR_DESKTOP},
1706 {"SYS_INFOBK", COLOR_INFOBK},
1707 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1708 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001709 {"SYS_BTNFACE", COLOR_BTNFACE},
1710 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1711 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1712 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1713 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1714 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1715 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1716 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1717 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1718 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1719 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1720 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1721 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1722 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1723 {"SYS_MENU", COLOR_MENU},
1724 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1725 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1726 {"SYS_WINDOW", COLOR_WINDOW},
1727 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1728 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1729 };
1730
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001731 /*
1732 * Try to look up a system colour.
1733 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001734 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001735 if (STRICMP(name, sys_table[i].name) == 0)
1736 return GetSysColor(sys_table[i].color);
1737
Bram Moolenaarab302212016-04-26 20:59:29 +02001738 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001739}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001740
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001741 guicolor_T
1742gui_mch_get_rgb_color(int r, int g, int b)
1743{
1744 return gui_get_rgb_color_cmn(r, g, b);
1745}
1746
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001747/*
1748 * Return OK if the key with the termcap name "name" is supported.
1749 */
1750 int
1751gui_mch_haskey(char_u *name)
1752{
1753 int i;
1754
1755 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001756 if (name[0] == special_keys[i].vim_code0
1757 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001758 return OK;
1759 return FAIL;
1760}
1761
1762 void
1763gui_mch_beep(void)
1764{
LemonBoy77771d32022-04-13 11:47:25 +01001765 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001766}
1767/*
1768 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1769 */
1770 void
1771gui_mch_invert_rectangle(
1772 int r,
1773 int c,
1774 int nr,
1775 int nc)
1776{
1777 RECT rc;
1778
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001779#if defined(FEAT_DIRECTX)
1780 if (IS_ENABLE_DIRECTX())
1781 DWriteContext_Flush(s_dwc);
1782#endif
1783
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001784 /*
1785 * Note: InvertRect() excludes right and bottom of rectangle.
1786 */
1787 rc.left = FILL_X(c);
1788 rc.top = FILL_Y(r);
1789 rc.right = rc.left + nc * gui.char_width;
1790 rc.bottom = rc.top + nr * gui.char_height;
1791 InvertRect(s_hdc, &rc);
1792}
1793
1794/*
1795 * Iconify the GUI window.
1796 */
1797 void
1798gui_mch_iconify(void)
1799{
1800 ShowWindow(s_hwnd, SW_MINIMIZE);
1801}
1802
1803/*
1804 * Draw a cursor without focus.
1805 */
1806 void
1807gui_mch_draw_hollow_cursor(guicolor_T color)
1808{
1809 HBRUSH hbr;
1810 RECT rc;
1811
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001812#if defined(FEAT_DIRECTX)
1813 if (IS_ENABLE_DIRECTX())
1814 DWriteContext_Flush(s_dwc);
1815#endif
1816
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001817 /*
1818 * Note: FrameRect() excludes right and bottom of rectangle.
1819 */
1820 rc.left = FILL_X(gui.col);
1821 rc.top = FILL_Y(gui.row);
1822 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001823 if (mb_lefthalve(gui.row, gui.col))
1824 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001825 rc.bottom = rc.top + gui.char_height;
1826 hbr = CreateSolidBrush(color);
1827 FrameRect(s_hdc, &rc, hbr);
1828 DeleteBrush(hbr);
1829}
1830/*
1831 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1832 * color "color".
1833 */
1834 void
1835gui_mch_draw_part_cursor(
1836 int w,
1837 int h,
1838 guicolor_T color)
1839{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001840 RECT rc;
1841
1842 /*
1843 * Note: FillRect() excludes right and bottom of rectangle.
1844 */
1845 rc.left =
1846#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001847 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001848 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1849#endif
1850 FILL_X(gui.col);
1851 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1852 rc.right = rc.left + w;
1853 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001854
Bram Moolenaar92467d32017-12-05 13:22:16 +01001855 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001856}
1857
1858
1859/*
1860 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1861 * dead key's nominal character and re-post the original message.
1862 */
1863 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001864outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001865{
1866 static MSG deadCharExpel;
1867
Anton Sharonov3f026672022-07-26 21:26:18 +01001868 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001869 return;
1870
Anton Sharonov3f026672022-07-26 21:26:18 +01001871 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001872
Bram Moolenaar734a8672019-12-02 22:49:38 +01001873 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001874 deadCharExpel.message = originalMsg.message;
1875 deadCharExpel.hwnd = originalMsg.hwnd;
1876 deadCharExpel.wParam = VK_SPACE;
1877
K.Takata4ac893f2022-01-20 12:44:28 +00001878 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001879
Bram Moolenaar734a8672019-12-02 22:49:38 +01001880 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1882 originalMsg.lParam);
1883}
1884
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001885/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001886 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1887 */
1888 static void
1889outputDeadKey_rePost(MSG originalMsg)
1890{
1891 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1892}
1893
1894/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001895 * Process a single Windows message.
1896 * If one is not available we hang until one is.
1897 */
1898 static void
1899process_message(void)
1900{
1901 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001902 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001903 char_u string[40];
1904 int i;
1905 int modifiers = 0;
1906 int key;
1907#ifdef FEAT_MENU
1908 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1909#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001910 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001911
K.Takatab7057bd2022-01-21 11:37:07 +00001912 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001913
1914#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001915 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001916 if (msg.message == WM_OLE)
1917 {
1918 char_u *str = (char_u *)msg.lParam;
1919 if (str == NULL || *str == NUL)
1920 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001921 // Message can't be ours, forward it. Fixes problem with Ultramon
1922 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001923 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001924 }
1925 else
1926 {
1927 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001928 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001929 }
1930 return;
1931 }
1932#endif
1933
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001934#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001935 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001936 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001937 {
1938 HandleMouseHide(msg.message, msg.lParam);
1939 return;
1940 }
1941#endif
1942
1943 /*
1944 * Check if it's a special key that we recognise. If not, call
1945 * TranslateMessage().
1946 */
1947 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1948 {
1949 vk = (int) msg.wParam;
1950
1951 /*
1952 * Handle dead keys in special conditions in other cases we let Windows
1953 * handle them and do not interfere.
1954 *
1955 * The dead_key flag must be reset on several occasions:
1956 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1957 * consumed at that point (This is when we let Windows combine the
1958 * dead character on its own)
1959 *
1960 * - Before doing something special such as regenerating keypresses to
1961 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001962 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001963 * immediately to _OnChar() (or _OnSysChar()).
1964 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001965
1966 /*
1967 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1968 * was handled by _WndProc, this keypress we want to process normally
1969 */
1970 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1971 dead_key = DEAD_KEY_OFF;
1972
1973 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001974 {
1975 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001976 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01001977 *
1978 * After dead key was pressed with Ctrl in some cases, ESC was
1979 * artificially injected and handled by _OnChar(), now we are
1980 * dealing with completely new key press from the user. If we don't
1981 * do anything, ToUnicode() call will interpret this vk+scan_code
1982 * under influence of "dead-modifier". To prevent this we translate
1983 * this message replacing current char from user with VK_SPACE,
1984 * which will cause WM_CHAR with dead_key's character itself. Using
1985 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1986 * ignore this one WM_CHAR event completely. Afterwards (due to
1987 * usage of PostMessage), this procedure is scheduled to be called
1988 * again with user char and on next entry we will clean
1989 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
1990 * outputDeadKey_rePost() since we do not wish to reset dead_key
1991 * value.
1992 */
1993 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
1994 {
1995 outputDeadKey_rePost_Ex(msg,
1996 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
1997 return;
1998 }
1999
2000 if (dead_key != DEAD_KEY_SET_DEFAULT)
2001 {
2002 // should never happen - is there a way to make ASSERT here?
2003 return;
2004 }
2005
2006 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002007 * If a dead key was pressed and the user presses VK_SPACE,
2008 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2009 * with the dead char now, so do nothing special and let Windows
2010 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002011 *
2012 * Note that VK_SPACE combines with the dead_key's character and
2013 * only one WM_CHAR will be generated by TranslateMessage(), in
2014 * the two other cases two WM_CHAR will be generated: the dead
2015 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2016 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002017 */
2018 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2019 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002020 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002021 TranslateMessage(&msg);
2022 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002023 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002024 // In modes where we are not typing, dead keys should behave
2025 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002026 else if ((get_real_state()
2027 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002028 {
2029 outputDeadKey_rePost(msg);
2030 return;
2031 }
2032 }
2033
Bram Moolenaar734a8672019-12-02 22:49:38 +01002034 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002035 if (vk == VK_CANCEL)
2036 {
2037 trash_input_buf();
2038 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002039 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002040 string[0] = Ctrl_C;
2041 add_to_input_buf(string, 1);
2042 }
2043
LemonBoy77fc0b02022-04-22 22:45:52 +01002044 // This is an IME event or a synthetic keystroke, let Windows handle it.
2045 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2046 {
2047 TranslateMessage(&msg);
2048 return;
2049 }
2050
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002051 for (i = 0; special_keys[i].key_sym != 0; i++)
2052 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002053 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002054 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002055 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002056 {
2057 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002058 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002059 * is a key that would normally trigger the dead key nominal
2060 * character output (such as a NUMPAD printable character or
2061 * the TAB key, etc...).
2062 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002063 if (dead_key == DEAD_KEY_SET_DEFAULT
2064 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002065 || vk == VK_TAB || vk == CAR))
2066 {
2067 outputDeadKey_rePost(msg);
2068 return;
2069 }
2070
2071#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002072 // Check for <F10>: Windows selects the menu. When <F10> is
2073 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002074 if (vk == VK_F10
2075 && gui.menu_is_active
2076 && check_map(k10, State, FALSE, TRUE, FALSE,
2077 NULL, NULL) == NULL)
2078 break;
2079#endif
LemonBoy45684c62022-04-24 15:46:42 +01002080 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002081
2082 if (special_keys[i].vim_code1 == NUL)
2083 key = special_keys[i].vim_code0;
2084 else
2085 key = TO_SPECIAL(special_keys[i].vim_code0,
2086 special_keys[i].vim_code1);
2087 key = simplify_key(key, &modifiers);
2088 if (key == CSI)
2089 key = K_CSI;
2090
2091 if (modifiers)
2092 {
2093 string[0] = CSI;
2094 string[1] = KS_MODIFIER;
2095 string[2] = modifiers;
2096 add_to_input_buf(string, 3);
2097 }
2098
2099 if (IS_SPECIAL(key))
2100 {
2101 string[0] = CSI;
2102 string[1] = K_SECOND(key);
2103 string[2] = K_THIRD(key);
2104 add_to_input_buf(string, 3);
2105 }
2106 else
2107 {
2108 int len;
2109
Bram Moolenaar734a8672019-12-02 22:49:38 +01002110 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002111 len = char_to_string(key, string, 40, FALSE);
2112 add_to_input_buf(string, len);
2113 }
2114 break;
2115 }
2116 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002117
2118 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002119 if (special_keys[i].key_sym == 0)
2120 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002121 WCHAR ch[8];
2122 int len;
2123 int i;
2124 UINT scan_code;
2125
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002126 // Construct the state table with only a few modifiers, we don't
2127 // really care about the presence of Ctrl/Alt as those modifiers are
2128 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002129 memset(keyboard_state, 0, 256);
2130 if (GetKeyState(VK_SHIFT) & 0x8000)
2131 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002132 if (GetKeyState(VK_CAPITAL) & 0x0001)
2133 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002134 // Alt-Gr is synthesized as Alt + Ctrl.
2135 if ((GetKeyState(VK_RMENU) & 0x8000)
2136 && (GetKeyState(VK_CONTROL) & 0x8000))
2137 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002138 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002139 keyboard_state[VK_CONTROL] = 0x80;
2140 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002141
2142 // Translate the virtual key according to the current keyboard
2143 // layout.
2144 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2145 // Convert the scan-code into a sequence of zero or more unicode
2146 // codepoints.
2147 // If this is a dead key ToUnicode returns a negative value.
2148 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2149 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002150 if (len < 0)
2151 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002152
2153 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002154 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002155 int wm_char = NUL;
2156
2157 if (dead_key == DEAD_KEY_SET_DEFAULT
2158 && (GetKeyState(VK_CONTROL) & 0x8000))
2159 {
2160 if ( // AZERTY CTRL+dead_circumflex
2161 (vk == 221 && scan_code == 26)
2162 // QWERTZ CTRL+dead_circumflex
2163 || (vk == 220 && scan_code == 41))
2164 wm_char = '[';
2165 if ( // QWERTZ CTRL+dead_two-overdots
2166 (vk == 192 && scan_code == 27))
2167 wm_char = ']';
2168 }
2169 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002170 {
2171 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002172 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002173 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002174 // ask _OnChar() to not touch this state, wait for next key
2175 // press and maintain knowledge that we are "poisoned" with
2176 // "dead state"
2177 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2178 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002179 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002180 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002181
2182 // Post the message as TranslateMessage would do.
2183 if (msg.message == WM_KEYDOWN)
2184 {
2185 for (i = 0; i < len; i++)
2186 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002187 }
2188 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002189 {
2190 for (i = 0; i < len; i++)
2191 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2192 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002193 }
2194 }
2195#ifdef FEAT_MBYTE_IME
2196 else if (msg.message == WM_IME_NOTIFY)
2197 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2198 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002199 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002200 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002201#endif
2202
2203#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002204 // Check for <F10>: Default effect is to select the menu. When <F10> is
2205 // mapped we need to stop it here to avoid strange effects (e.g., for the
2206 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002207 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2208 NULL, NULL) == NULL)
2209#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002210 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002211}
2212
2213/*
2214 * Catch up with any queued events. This may put keyboard input into the
2215 * input buffer, call resize call-backs, trigger timers etc. If there is
2216 * nothing in the event queue (& no timers pending), then we return
2217 * immediately.
2218 */
2219 void
2220gui_mch_update(void)
2221{
2222 MSG msg;
2223
2224 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002225 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002226 && !vim_is_input_buf_full())
2227 process_message();
2228}
2229
Bram Moolenaar4231da42016-06-02 14:30:04 +02002230 static void
2231remove_any_timer(void)
2232{
2233 MSG msg;
2234
2235 if (s_wait_timer != 0 && !s_timed_out)
2236 {
2237 KillTimer(NULL, s_wait_timer);
2238
Bram Moolenaar734a8672019-12-02 22:49:38 +01002239 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002240 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002241 ;
2242 s_wait_timer = 0;
2243 }
2244}
2245
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002246/*
2247 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2248 * from the keyboard.
2249 * wtime == -1 Wait forever.
2250 * wtime == 0 This should never happen.
2251 * wtime > 0 Wait wtime milliseconds for a character.
2252 * Returns OK if a character was found to be available within the given time,
2253 * or FAIL otherwise.
2254 */
2255 int
2256gui_mch_wait_for_chars(int wtime)
2257{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002258 int focus;
2259
2260 s_timed_out = FALSE;
2261
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002262 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002263 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002264 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002265 if (s_busy_processing)
2266 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002267
2268 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002269 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2270 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002271 }
2272
2273 allow_scrollbar = TRUE;
2274
2275 focus = gui.in_focus;
2276 while (!s_timed_out)
2277 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002278 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002279 if (gui.in_focus != focus)
2280 {
2281 if (gui.in_focus)
2282 gui_mch_start_blink();
2283 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002284 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002285 focus = gui.in_focus;
2286 }
2287
2288 if (s_need_activate)
2289 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002290 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291 s_need_activate = FALSE;
2292 }
2293
Bram Moolenaar4231da42016-06-02 14:30:04 +02002294#ifdef FEAT_TIMERS
2295 did_add_timer = FALSE;
2296#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002297#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002298 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002299 for (;;)
2300 {
2301 MSG msg;
2302
2303 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002304# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002305 if (did_add_timer)
2306 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002307# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002308 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002309 {
2310 process_message();
2311 break;
2312 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002313 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002314 // TODO: The 10 msec is a compromise between laggy response
2315 // and consuming more CPU time. Better would be to handle
2316 // channel messages when they arrive.
2317 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002318 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002319 break;
2320 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002321#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002322 // Don't use gui_mch_update() because then we will spin-lock until a
2323 // char arrives, instead we use GetMessage() to hang until an
2324 // event arrives. No need to check for input_buf_full because we are
2325 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002326 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002327#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328
2329 if (input_available())
2330 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002331 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002332 allow_scrollbar = FALSE;
2333
Bram Moolenaar89c00032019-09-04 13:53:21 +02002334 // Clear pending mouse button, the release event may have been
2335 // taken by the dialog window. But don't do this when getting
2336 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002337 if (!s_getting_focus)
2338 s_button_pending = -1;
2339
2340 return OK;
2341 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002342
2343#ifdef FEAT_TIMERS
2344 if (did_add_timer)
2345 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002346 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002347 remove_any_timer();
2348 break;
2349 }
2350#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002351 }
2352 allow_scrollbar = FALSE;
2353 return FAIL;
2354}
2355
2356/*
2357 * Clear a rectangular region of the screen from text pos (row1, col1) to
2358 * (row2, col2) inclusive.
2359 */
2360 void
2361gui_mch_clear_block(
2362 int row1,
2363 int col1,
2364 int row2,
2365 int col2)
2366{
2367 RECT rc;
2368
2369 /*
2370 * Clear one extra pixel at the far right, for when bold characters have
2371 * spilled over to the window border.
2372 * Note: FillRect() excludes right and bottom of rectangle.
2373 */
2374 rc.left = FILL_X(col1);
2375 rc.top = FILL_Y(row1);
2376 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2377 rc.bottom = FILL_Y(row2 + 1);
2378 clear_rect(&rc);
2379}
2380
2381/*
2382 * Clear the whole text window.
2383 */
2384 void
2385gui_mch_clear_all(void)
2386{
2387 RECT rc;
2388
2389 rc.left = 0;
2390 rc.top = 0;
2391 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2392 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2393 clear_rect(&rc);
2394}
2395/*
2396 * Menu stuff.
2397 */
2398
2399 void
2400gui_mch_enable_menu(int flag)
2401{
2402#ifdef FEAT_MENU
2403 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2404#endif
2405}
2406
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002407 void
2408gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002409 int x UNUSED,
2410 int y UNUSED,
2411 int w UNUSED,
2412 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002413{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002414 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002415}
2416
2417#if defined(FEAT_MENU) || defined(PROTO)
2418/*
2419 * Make menu item hidden or not hidden
2420 */
2421 void
2422gui_mch_menu_hidden(
2423 vimmenu_T *menu,
2424 int hidden)
2425{
2426 /*
2427 * This doesn't do what we want. Hmm, just grey the menu items for now.
2428 */
2429 /*
2430 if (hidden)
2431 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2432 else
2433 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2434 */
2435 gui_mch_menu_grey(menu, hidden);
2436}
2437
2438/*
2439 * This is called after setting all the menus to grey/hidden or not.
2440 */
2441 void
2442gui_mch_draw_menubar(void)
2443{
2444 DrawMenuBar(s_hwnd);
2445}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002446#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002448/*
2449 * Return the RGB value of a pixel as a long.
2450 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002451 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002452gui_mch_get_rgb(guicolor_T pixel)
2453{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002454 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2455 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002456}
2457
2458#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002459/*
2460 * Convert pixels in X to dialog units
2461 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462 static WORD
2463PixelToDialogX(int numPixels)
2464{
2465 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2466}
2467
Bram Moolenaar734a8672019-12-02 22:49:38 +01002468/*
2469 * Convert pixels in Y to dialog units
2470 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471 static WORD
2472PixelToDialogY(int numPixels)
2473{
2474 return (WORD)((numPixels * 8) / s_dlgfntheight);
2475}
2476
Bram Moolenaar734a8672019-12-02 22:49:38 +01002477/*
2478 * Return the width in pixels of the given text in the given DC.
2479 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480 static int
2481GetTextWidth(HDC hdc, char_u *str, int len)
2482{
2483 SIZE size;
2484
2485 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2486 return size.cx;
2487}
2488
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002489/*
2490 * Return the width in pixels of the given text in the given DC, taking care
2491 * of 'encoding' to active codepage conversion.
2492 */
2493 static int
2494GetTextWidthEnc(HDC hdc, char_u *str, int len)
2495{
2496 SIZE size;
2497 WCHAR *wstr;
2498 int n;
2499 int wlen = len;
2500
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002501 wstr = enc_to_utf16(str, &wlen);
2502 if (wstr == NULL)
2503 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002504
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002505 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2506 vim_free(wstr);
2507 if (n)
2508 return size.cx;
2509 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002510}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002511
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002512static void get_work_area(RECT *spi_rect);
2513
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002514/*
2515 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002516 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2517 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002518 */
2519 static BOOL
2520CenterWindow(
2521 HWND hwndChild,
2522 HWND hwndParent)
2523{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002524 HMONITOR mon;
2525 MONITORINFO moninfo;
2526 RECT rChild, rParent, rScreen;
2527 int wChild, hChild, wParent, hParent;
2528 int xNew, yNew;
2529 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002530
2531 GetWindowRect(hwndChild, &rChild);
2532 wChild = rChild.right - rChild.left;
2533 hChild = rChild.bottom - rChild.top;
2534
Bram Moolenaar734a8672019-12-02 22:49:38 +01002535 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002536 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002537 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002538 else
2539 GetWindowRect(hwndParent, &rParent);
2540 wParent = rParent.right - rParent.left;
2541 hParent = rParent.bottom - rParent.top;
2542
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002543 moninfo.cbSize = sizeof(MONITORINFO);
2544 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2545 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002546 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002547 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002549 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002550 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002551 hdc = GetDC(hwndChild);
2552 rScreen.left = 0;
2553 rScreen.top = 0;
2554 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2555 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2556 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002557 }
2558
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002559 xNew = rParent.left + ((wParent - wChild) / 2);
2560 if (xNew < rScreen.left)
2561 xNew = rScreen.left;
2562 else if ((xNew + wChild) > rScreen.right)
2563 xNew = rScreen.right - wChild;
2564
2565 yNew = rParent.top + ((hParent - hChild) / 2);
2566 if (yNew < rScreen.top)
2567 yNew = rScreen.top;
2568 else if ((yNew + hChild) > rScreen.bottom)
2569 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002570
2571 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2572 SWP_NOSIZE | SWP_NOZORDER);
2573}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002574#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002575
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002576#if defined(FEAT_TOOLBAR) || defined(PROTO)
2577 void
2578gui_mch_show_toolbar(int showit)
2579{
2580 if (s_toolbarhwnd == NULL)
2581 return;
2582
2583 if (showit)
2584 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002585 // Enable unicode support
2586 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2587 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002588 ShowWindow(s_toolbarhwnd, SW_SHOW);
2589 }
2590 else
2591 ShowWindow(s_toolbarhwnd, SW_HIDE);
2592}
2593
Bram Moolenaar734a8672019-12-02 22:49:38 +01002594// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002595# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596
2597#endif
2598
2599#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2600 static void
2601add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2602{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002603 WCHAR *wn;
2604 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002606 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002607 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002608 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002610 infow.cbSize = sizeof(infow);
2611 infow.fMask = MIIM_TYPE | MIIM_ID;
2612 infow.wID = item_id;
2613 infow.fType = MFT_STRING;
2614 infow.dwTypeData = wn;
2615 infow.cch = (UINT)wcslen(wn);
2616 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2617 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002618}
2619
2620 static void
2621show_tabline_popup_menu(void)
2622{
2623 HMENU tab_pmenu;
2624 long rval;
2625 POINT pt;
2626
Bram Moolenaar734a8672019-12-02 22:49:38 +01002627 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002628 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002629 return;
2630
2631 tab_pmenu = CreatePopupMenu();
2632 if (tab_pmenu == NULL)
2633 return;
2634
2635 if (first_tabpage->tp_next != NULL)
2636 add_tabline_popup_menu_entry(tab_pmenu,
2637 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2638 add_tabline_popup_menu_entry(tab_pmenu,
2639 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2640 add_tabline_popup_menu_entry(tab_pmenu,
2641 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2642
2643 GetCursorPos(&pt);
2644 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2645 NULL);
2646
2647 DestroyMenu(tab_pmenu);
2648
Bram Moolenaar734a8672019-12-02 22:49:38 +01002649 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002650 if (rval > 0)
2651 {
2652 TCHITTESTINFO htinfo;
2653 int idx;
2654
2655 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2656 return;
2657
2658 htinfo.pt.x = pt.x;
2659 htinfo.pt.y = pt.y;
2660 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2661 if (idx == -1)
2662 idx = 0;
2663 else
2664 idx += 1;
2665
2666 send_tabline_menu_event(idx, (int)rval);
2667 }
2668}
2669
2670/*
2671 * Show or hide the tabline.
2672 */
2673 void
2674gui_mch_show_tabline(int showit)
2675{
2676 if (s_tabhwnd == NULL)
2677 return;
2678
2679 if (!showit != !showing_tabline)
2680 {
2681 if (showit)
2682 ShowWindow(s_tabhwnd, SW_SHOW);
2683 else
2684 ShowWindow(s_tabhwnd, SW_HIDE);
2685 showing_tabline = showit;
2686 }
2687}
2688
2689/*
2690 * Return TRUE when tabline is displayed.
2691 */
2692 int
2693gui_mch_showing_tabline(void)
2694{
2695 return s_tabhwnd != NULL && showing_tabline;
2696}
2697
2698/*
2699 * Update the labels of the tabline.
2700 */
2701 void
2702gui_mch_update_tabline(void)
2703{
2704 tabpage_T *tp;
2705 TCITEM tie;
2706 int nr = 0;
2707 int curtabidx = 0;
2708 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002709 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002710
2711 if (s_tabhwnd == NULL)
2712 return;
2713
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002714 // Enable unicode support
2715 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002716
2717 tie.mask = TCIF_TEXT;
2718 tie.iImage = -1;
2719
Bram Moolenaar734a8672019-12-02 22:49:38 +01002720 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2722
Bram Moolenaar734a8672019-12-02 22:49:38 +01002723 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002724 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2725 {
2726 if (tp == curtab)
2727 curtabidx = nr;
2728
2729 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2730 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002731 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002732 tie.pszText = "-Empty-";
2733 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2734 tabadded = 1;
2735 }
2736
2737 get_tabline_label(tp, FALSE);
2738 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002739
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002740 wstr = enc_to_utf16(NameBuff, NULL);
2741 if (wstr != NULL)
2742 {
2743 TCITEMW tiw;
2744
2745 tiw.mask = TCIF_TEXT;
2746 tiw.iImage = -1;
2747 tiw.pszText = wstr;
2748 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2749 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002750 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751 }
2752
Bram Moolenaar734a8672019-12-02 22:49:38 +01002753 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002754 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2755 TabCtrl_DeleteItem(s_tabhwnd, nr);
2756
2757 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2758 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2759
Bram Moolenaar734a8672019-12-02 22:49:38 +01002760 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002761 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2762 RedrawWindow(s_tabhwnd, NULL, NULL,
2763 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2764
2765 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2766 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2767}
2768
2769/*
2770 * Set the current tab to "nr". First tab is 1.
2771 */
2772 void
2773gui_mch_set_curtab(int nr)
2774{
2775 if (s_tabhwnd == NULL)
2776 return;
2777
2778 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2779 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2780}
2781
2782#endif
2783
2784/*
2785 * ":simalt" command.
2786 */
2787 void
2788ex_simalt(exarg_T *eap)
2789{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002790 char_u *keys = eap->arg;
2791 int fill_typebuf = FALSE;
2792 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002793
2794 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2795 while (*keys)
2796 {
2797 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002798 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002799 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2800 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002801 fill_typebuf = TRUE;
2802 }
2803 if (fill_typebuf)
2804 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002805 // Put a NOP in the typeahead buffer so that the message will get
2806 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002807 key_name[0] = K_SPECIAL;
2808 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002809 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002810 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002811#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002812 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002813#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002814 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002815 }
2816}
2817
2818/*
2819 * Create the find & replace dialogs.
2820 * You can't have both at once: ":find" when replace is showing, destroys
2821 * the replace dialog first, and the other way around.
2822 */
2823#ifdef MSWIN_FIND_REPLACE
2824 static void
2825initialise_findrep(char_u *initial_string)
2826{
2827 int wword = FALSE;
2828 int mcase = !p_ic;
2829 char_u *entry_text;
2830
Bram Moolenaar734a8672019-12-02 22:49:38 +01002831 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002832 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2833
2834 s_findrep_struct.hwndOwner = s_hwnd;
2835 s_findrep_struct.Flags = FR_DOWN;
2836 if (mcase)
2837 s_findrep_struct.Flags |= FR_MATCHCASE;
2838 if (wword)
2839 s_findrep_struct.Flags |= FR_WHOLEWORD;
2840 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002841 {
2842 WCHAR *p = enc_to_utf16(entry_text, NULL);
2843 if (p != NULL)
2844 {
2845 int len = s_findrep_struct.wFindWhatLen - 1;
2846
2847 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2848 s_findrep_struct.lpstrFindWhat[len] = NUL;
2849 vim_free(p);
2850 }
2851 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002852 vim_free(entry_text);
2853}
2854#endif
2855
2856 static void
2857set_window_title(HWND hwnd, char *title)
2858{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002859 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002860 {
2861 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002862
Bram Moolenaar734a8672019-12-02 22:49:38 +01002863 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002864 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2865 if (wbuf != NULL)
2866 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002867 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002868 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002869 }
2870 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002871 else
2872 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002873}
2874
2875 void
2876gui_mch_find_dialog(exarg_T *eap)
2877{
2878#ifdef MSWIN_FIND_REPLACE
2879 if (s_findrep_msg != 0)
2880 {
2881 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2882 DestroyWindow(s_findrep_hwnd);
2883
2884 if (!IsWindow(s_findrep_hwnd))
2885 {
2886 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002887 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002888 }
2889
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002890 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002891 (void)SetFocus(s_findrep_hwnd);
2892
2893 s_findrep_is_find = TRUE;
2894 }
2895#endif
2896}
2897
2898
2899 void
2900gui_mch_replace_dialog(exarg_T *eap)
2901{
2902#ifdef MSWIN_FIND_REPLACE
2903 if (s_findrep_msg != 0)
2904 {
2905 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2906 DestroyWindow(s_findrep_hwnd);
2907
2908 if (!IsWindow(s_findrep_hwnd))
2909 {
2910 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002911 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002912 }
2913
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002914 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002915 (void)SetFocus(s_findrep_hwnd);
2916
2917 s_findrep_is_find = FALSE;
2918 }
2919#endif
2920}
2921
2922
2923/*
2924 * Set visibility of the pointer.
2925 */
2926 void
2927gui_mch_mousehide(int hide)
2928{
2929 if (hide != gui.pointer_hidden)
2930 {
2931 ShowCursor(!hide);
2932 gui.pointer_hidden = hide;
2933 }
2934}
2935
2936#ifdef FEAT_MENU
2937 static void
2938gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2939{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002940 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002941 gui_mch_mousehide(FALSE);
2942
2943 (void)TrackPopupMenu(
2944 (HMENU)menu->submenu_id,
2945 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2946 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002947 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002948 s_hwnd,
2949 NULL);
2950 /*
2951 * NOTE: The pop-up menu can eat the mouse up event.
2952 * We deal with this in normal.c.
2953 */
2954}
2955#endif
2956
2957/*
2958 * Got a message when the system will go down.
2959 */
2960 static void
2961_OnEndSession(void)
2962{
2963 getout_preserve_modified(1);
2964}
2965
2966/*
2967 * Get this message when the user clicks on the cross in the top right corner
2968 * of a Windows95 window.
2969 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002970 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002971_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002972{
2973 gui_shell_closed();
2974}
2975
2976/*
2977 * Get a message when the window is being destroyed.
2978 */
2979 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002980_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002981{
2982 if (!destroying)
2983 _OnClose(hwnd);
2984}
2985
2986 static void
2987_OnPaint(
2988 HWND hwnd)
2989{
2990 if (!IsMinimized(hwnd))
2991 {
2992 PAINTSTRUCT ps;
2993
Bram Moolenaar734a8672019-12-02 22:49:38 +01002994 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002995 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002996
Bram Moolenaar734a8672019-12-02 22:49:38 +01002997 // prevent multi-byte characters from misprinting on an invalid
2998 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002999 if (has_mbyte)
3000 {
3001 RECT rect;
3002
3003 GetClientRect(hwnd, &rect);
3004 ps.rcPaint.left = rect.left;
3005 ps.rcPaint.right = rect.right;
3006 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003007
3008 if (!IsRectEmpty(&ps.rcPaint))
3009 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003010 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3011 ps.rcPaint.right - ps.rcPaint.left + 1,
3012 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3013 }
3014
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015 EndPaint(hwnd, &ps);
3016 }
3017}
3018
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019 static void
3020_OnSize(
3021 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003022 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003023 int cx,
3024 int cy)
3025{
K.Takatac81e9bf2022-01-16 14:15:49 +00003026 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003027 {
3028 gui_resize_shell(cx, cy);
3029
Bram Moolenaar734a8672019-12-02 22:49:38 +01003030 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003031 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003032 }
3033}
3034
3035 static void
3036_OnSetFocus(
3037 HWND hwnd,
3038 HWND hwndOldFocus)
3039{
3040 gui_focus_change(TRUE);
3041 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003042 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043}
3044
3045 static void
3046_OnKillFocus(
3047 HWND hwnd,
3048 HWND hwndNewFocus)
3049{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003050 if (destroying)
3051 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003052 gui_focus_change(FALSE);
3053 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003054 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055}
3056
3057/*
3058 * Get a message when the user switches back to vim
3059 */
3060 static LRESULT
3061_OnActivateApp(
3062 HWND hwnd,
3063 BOOL fActivate,
3064 DWORD dwThreadId)
3065{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003066 // we call gui_focus_change() in _OnSetFocus()
3067 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003068 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003069}
3070
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003071 void
3072gui_mch_destroy_scrollbar(scrollbar_T *sb)
3073{
3074 DestroyWindow(sb->id);
3075}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003076
3077/*
3078 * Get current mouse coordinates in text window.
3079 */
3080 void
3081gui_mch_getmouse(int *x, int *y)
3082{
3083 RECT rct;
3084 POINT mp;
3085
3086 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003087 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088 *x = (int)(mp.x - rct.left);
3089 *y = (int)(mp.y - rct.top);
3090}
3091
3092/*
3093 * Move mouse pointer to character at (x, y).
3094 */
3095 void
3096gui_mch_setmouse(int x, int y)
3097{
3098 RECT rct;
3099
3100 (void)GetWindowRect(s_textArea, &rct);
3101 (void)SetCursorPos(x + gui.border_offset + rct.left,
3102 y + gui.border_offset + rct.top);
3103}
3104
3105 static void
3106gui_mswin_get_valid_dimensions(
3107 int w,
3108 int h,
3109 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003110 int *valid_h,
3111 int *cols,
3112 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003113{
3114 int base_width, base_height;
3115
3116 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003117 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3118 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003120 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3121 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3122 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3123 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003124 *cols = (w - base_width) / gui.char_width;
3125 *rows = (h - base_height) / gui.char_height;
3126 *valid_w = base_width + *cols * gui.char_width;
3127 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003128}
3129
3130 void
3131gui_mch_flash(int msec)
3132{
3133 RECT rc;
3134
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003135#if defined(FEAT_DIRECTX)
3136 if (IS_ENABLE_DIRECTX())
3137 DWriteContext_Flush(s_dwc);
3138#endif
3139
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140 /*
3141 * Note: InvertRect() excludes right and bottom of rectangle.
3142 */
3143 rc.left = 0;
3144 rc.top = 0;
3145 rc.right = gui.num_cols * gui.char_width;
3146 rc.bottom = gui.num_rows * gui.char_height;
3147 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003148 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003149
Bram Moolenaar734a8672019-12-02 22:49:38 +01003150 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003151
3152 InvertRect(s_hdc, &rc);
3153}
3154
3155/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003156 * Check if the specified point is on-screen. (multi-monitor aware)
3157 */
3158 static BOOL
3159is_point_onscreen(int x, int y)
3160{
3161 POINT pt = {x, y};
3162
3163 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3164}
3165
3166/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003167 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003168 *
3169 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3170 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3171 * only works when the whole window is on-screen.
3172 */
3173 static BOOL
3174is_window_onscreen(HWND hwnd)
3175{
3176 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003177 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003178
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003179 GetClientRect(hwnd, &rc);
3180 p1.x = rc.left;
3181 p1.y = rc.top;
3182 p2.x = rc.right - 1;
3183 p2.y = rc.bottom - 1;
3184 ClientToScreen(hwnd, &p1);
3185 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003186
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003187 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003188 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003189 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003190 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003191 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003192 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003193 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003194 return FALSE;
3195 return TRUE;
3196}
3197
3198/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003199 * Return flags used for scrolling.
3200 * The SW_INVALIDATE is required when part of the window is covered or
3201 * off-screen. Refer to MS KB Q75236.
3202 */
3203 static int
3204get_scroll_flags(void)
3205{
3206 HWND hwnd;
3207 RECT rcVim, rcOther, rcDest;
3208
Bram Moolenaar185577e2020-10-29 20:08:21 +01003209 // Check if the window is (partly) off-screen.
3210 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 return SW_INVALIDATE;
3212
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003213 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003214 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003215 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3216 if (IsWindowVisible(hwnd))
3217 {
3218 GetWindowRect(hwnd, &rcOther);
3219 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3220 return SW_INVALIDATE;
3221 }
3222 return 0;
3223}
3224
3225/*
3226 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3227 * may not be scrolled out properly.
3228 * For gVim, when _OnScroll() is repeated, the character at the
3229 * previous cursor position may be left drawn after scroll.
3230 * The problem can be avoided by calling GetPixel() to get a pixel in
3231 * the region before ScrollWindowEx().
3232 */
3233 static void
3234intel_gpu_workaround(void)
3235{
3236 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3237}
3238
3239/*
3240 * Delete the given number of lines from the given row, scrolling up any
3241 * text further down within the scroll region.
3242 */
3243 void
3244gui_mch_delete_lines(
3245 int row,
3246 int num_lines)
3247{
3248 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003249
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003250 rc.left = FILL_X(gui.scroll_region_left);
3251 rc.right = FILL_X(gui.scroll_region_right + 1);
3252 rc.top = FILL_Y(row);
3253 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3254
Bram Moolenaar92467d32017-12-05 13:22:16 +01003255#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003256 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003257 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003258 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003259 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003260 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003261#endif
3262 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003263#if defined(FEAT_DIRECTX)
3264 if (IS_ENABLE_DIRECTX())
3265 DWriteContext_Flush(s_dwc);
3266#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003267 intel_gpu_workaround();
3268 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003270 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003271 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003272
Bram Moolenaar734a8672019-12-02 22:49:38 +01003273 // This seems to be required to avoid the cursor disappearing when
3274 // scrolling such that the cursor ends up in the top-left character on
3275 // the screen... But why? (Webb)
3276 // It's probably fixed by disabling drawing the cursor while scrolling.
3277 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003278
3279 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3280 gui.scroll_region_left,
3281 gui.scroll_region_bot, gui.scroll_region_right);
3282}
3283
3284/*
3285 * Insert the given number of lines before the given row, scrolling down any
3286 * following text within the scroll region.
3287 */
3288 void
3289gui_mch_insert_lines(
3290 int row,
3291 int num_lines)
3292{
3293 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003294
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003295 rc.left = FILL_X(gui.scroll_region_left);
3296 rc.right = FILL_X(gui.scroll_region_right + 1);
3297 rc.top = FILL_Y(row);
3298 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003299
3300#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003301 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003302 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003303 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003304 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003305 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003306#endif
3307 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003308#if defined(FEAT_DIRECTX)
3309 if (IS_ENABLE_DIRECTX())
3310 DWriteContext_Flush(s_dwc);
3311#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003312 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003313 // The SW_INVALIDATE is required when part of the window is covered or
3314 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003315 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003316 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003317 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003318 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003319
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003320 gui_clear_block(row, gui.scroll_region_left,
3321 row + num_lines - 1, gui.scroll_region_right);
3322}
3323
3324
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003325 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003326gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003327{
3328#if defined(FEAT_DIRECTX)
3329 DWriteContext_Close(s_dwc);
3330 DWrite_Final();
3331 s_dwc = NULL;
3332#endif
3333
3334 ReleaseDC(s_textArea, s_hdc);
3335 DeleteObject(s_brush);
3336
3337#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003338 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3340#endif
3341
Bram Moolenaar734a8672019-12-02 22:49:38 +01003342 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343 if (s_hwnd != NULL)
3344 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003345 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003346 DestroyWindow(s_hwnd);
3347 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003348}
3349
3350 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003351logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003352{
3353 char *p;
3354 char *res;
3355 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003356 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003357 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003358 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003359
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003360 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3361 if (font_name == NULL)
3362 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003363 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003364 quality_name = quality_id2name((int)lf.lfQuality);
3365
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003366 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003367 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003368 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003369 if (res != NULL)
3370 {
3371 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003372 // make a normal font string out of the lf thing:
3373 points = pixels_to_points(
3374 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3375 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3376 sprintf((char *)p, "%s:h%d", font_name, points);
3377 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003378 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379 while (*p)
3380 {
3381 if (*p == ' ')
3382 *p = '_';
3383 ++p;
3384 }
3385 if (lf.lfItalic)
3386 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003387 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003388 STRCAT(p, ":b");
3389 if (lf.lfUnderline)
3390 STRCAT(p, ":u");
3391 if (lf.lfStrikeOut)
3392 STRCAT(p, ":s");
3393 if (charset_name != NULL)
3394 {
3395 STRCAT(p, ":c");
3396 STRCAT(p, charset_name);
3397 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003398 if (quality_name != NULL)
3399 {
3400 STRCAT(p, ":q");
3401 STRCAT(p, quality_name);
3402 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003403 }
3404
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003405 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003406 return (char_u *)res;
3407}
3408
3409
3410#ifdef FEAT_MBYTE_IME
3411/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003412 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003413 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003414 */
3415 static void
3416update_im_font(void)
3417{
K.Takatac81e9bf2022-01-16 14:15:49 +00003418 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003419
3420 if (p_guifontwide != NULL && *p_guifontwide != NUL
3421 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003422 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423 norm_logfont = lf_wide;
3424 else
3425 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003426
3427 lf = norm_logfont;
3428 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3429 // Work around when PerMonitorV2 is not enabled in the process level.
3430 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3431 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003432}
3433#endif
3434
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435/*
3436 * Handler of gui.wide_font (p_guifontwide) changed notification.
3437 */
3438 void
3439gui_mch_wide_font_changed(void)
3440{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003441 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003442
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003443#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003445#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003446
3447 gui_mch_free_font(gui.wide_ital_font);
3448 gui.wide_ital_font = NOFONT;
3449 gui_mch_free_font(gui.wide_bold_font);
3450 gui.wide_bold_font = NOFONT;
3451 gui_mch_free_font(gui.wide_boldital_font);
3452 gui.wide_boldital_font = NOFONT;
3453
3454 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003455 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 {
3457 if (!lf.lfItalic)
3458 {
3459 lf.lfItalic = TRUE;
3460 gui.wide_ital_font = get_font_handle(&lf);
3461 lf.lfItalic = FALSE;
3462 }
3463 if (lf.lfWeight < FW_BOLD)
3464 {
3465 lf.lfWeight = FW_BOLD;
3466 gui.wide_bold_font = get_font_handle(&lf);
3467 if (!lf.lfItalic)
3468 {
3469 lf.lfItalic = TRUE;
3470 gui.wide_boldital_font = get_font_handle(&lf);
3471 }
3472 }
3473 }
3474}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475
3476/*
3477 * Initialise vim to use the font with the given name.
3478 * Return FAIL if the font could not be loaded, OK otherwise.
3479 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003480 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003481gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003482{
K.Takatac81e9bf2022-01-16 14:15:49 +00003483 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003484 GuiFont font = NOFONT;
3485 char_u *p;
3486
Bram Moolenaar734a8672019-12-02 22:49:38 +01003487 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003488 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003489 {
3490 lfOrig = lf;
3491 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003493 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003494 if (font == NOFONT)
3495 return FAIL;
3496
3497 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003498 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003499#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 norm_logfont = lf;
3501 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003502 if (!s_in_dpichanged)
3503 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504#endif
3505 gui_mch_free_font(gui.norm_font);
3506 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003507 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003508 GetFontSize(font);
3509
K.Takatac81e9bf2022-01-16 14:15:49 +00003510 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003511 if (p != NULL)
3512 {
3513 hl_set_font_name(p);
3514
Bram Moolenaar734a8672019-12-02 22:49:38 +01003515 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3517 {
3518 vim_free(p_guifont);
3519 p_guifont = p;
3520 }
3521 else
3522 vim_free(p);
3523 }
3524
3525 gui_mch_free_font(gui.ital_font);
3526 gui.ital_font = NOFONT;
3527 gui_mch_free_font(gui.bold_font);
3528 gui.bold_font = NOFONT;
3529 gui_mch_free_font(gui.boldital_font);
3530 gui.boldital_font = NOFONT;
3531
3532 if (!lf.lfItalic)
3533 {
3534 lf.lfItalic = TRUE;
3535 gui.ital_font = get_font_handle(&lf);
3536 lf.lfItalic = FALSE;
3537 }
3538 if (lf.lfWeight < FW_BOLD)
3539 {
3540 lf.lfWeight = FW_BOLD;
3541 gui.bold_font = get_font_handle(&lf);
3542 if (!lf.lfItalic)
3543 {
3544 lf.lfItalic = TRUE;
3545 gui.boldital_font = get_font_handle(&lf);
3546 }
3547 }
3548
3549 return OK;
3550}
3551
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552/*
3553 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003554 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003555 */
3556 int
3557gui_mch_maximized(void)
3558{
3559 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003560 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003561
3562 wp.length = sizeof(WINDOWPLACEMENT);
3563 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003564 {
3565 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003566 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003567 && wp.flags == WPF_RESTORETOMAXIMIZED))
3568 return TRUE;
3569 if (wp.showCmd == SW_SHOWMINIMIZED)
3570 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003571
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003572 // Assume the window is snapped when the sizes from two APIs differ.
3573 GetWindowRect(s_hwnd, &rc);
3574 if ((rc.right - rc.left !=
3575 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3576 || (rc.bottom - rc.top !=
3577 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3578 return TRUE;
3579 }
3580 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003581}
3582
3583/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003584 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3585 * is set. Compute the new Rows and Columns. This is like resizing the
3586 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003587 */
3588 void
3589gui_mch_newfont(void)
3590{
3591 RECT rect;
3592
3593 GetWindowRect(s_hwnd, &rect);
3594 if (win_socket_id == 0)
3595 {
3596 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003597 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3598 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003600 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3601 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3602 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3603 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003604 }
3605 else
3606 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003607 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003608 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003609 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003610 }
3611}
3612
3613/*
3614 * Set the window title
3615 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 void
3617gui_mch_settitle(
3618 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003619 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620{
3621 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3622}
3623
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003624#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003625// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3626// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003627static LPCSTR mshape_idcs[] =
3628{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003629 IDC_ARROW, // arrow
3630 MAKEINTRESOURCE(0), // blank
3631 IDC_IBEAM, // beam
3632 IDC_SIZENS, // updown
3633 IDC_SIZENS, // udsizing
3634 IDC_SIZEWE, // leftright
3635 IDC_SIZEWE, // lrsizing
3636 IDC_WAIT, // busy
3637 IDC_NO, // no
3638 IDC_ARROW, // crosshair
3639 IDC_ARROW, // hand1
3640 IDC_ARROW, // hand2
3641 IDC_ARROW, // pencil
3642 IDC_ARROW, // question
3643 IDC_ARROW, // right-arrow
3644 IDC_UPARROW, // up-arrow
3645 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003646};
3647
3648 void
3649mch_set_mouse_shape(int shape)
3650{
3651 LPCSTR idc;
3652
3653 if (shape == MSHAPE_HIDE)
3654 ShowCursor(FALSE);
3655 else
3656 {
3657 if (shape >= MSHAPE_NUMBERED)
3658 idc = IDC_ARROW;
3659 else
3660 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003661 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003662 if (!p_mh)
3663 {
3664 POINT mp;
3665
Bram Moolenaar734a8672019-12-02 22:49:38 +01003666 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003667 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 (void)SetCursorPos(mp.x, mp.y);
3669 ShowCursor(TRUE);
3670 }
3671 }
3672}
3673#endif
3674
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003675#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003676/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003677 * Wide version of convert_filter().
3678 */
3679 static WCHAR *
3680convert_filterW(char_u *s)
3681{
3682 char_u *tmp;
3683 int len;
3684 WCHAR *res;
3685
3686 tmp = convert_filter(s);
3687 if (tmp == NULL)
3688 return NULL;
3689 len = (int)STRLEN(s) + 3;
3690 res = enc_to_utf16(tmp, &len);
3691 vim_free(tmp);
3692 return res;
3693}
3694
3695/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003696 * Pop open a file browser and return the file selected, in allocated memory,
3697 * or NULL if Cancel is hit.
3698 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3699 * title - Title message for the file browser dialog.
3700 * dflt - Default name of file.
3701 * ext - Default extension to be added to files without extensions.
3702 * initdir - directory in which to open the browser (NULL = current dir)
3703 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003704 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003705 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003706gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003707 int saving,
3708 char_u *title,
3709 char_u *dflt,
3710 char_u *ext,
3711 char_u *initdir,
3712 char_u *filter)
3713{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003714 // We always use the wide function. This means enc_to_utf16() must work,
3715 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 OPENFILENAMEW fileStruct;
3717 WCHAR fileBuf[MAXPATHL];
3718 WCHAR *wp;
3719 int i;
3720 WCHAR *titlep = NULL;
3721 WCHAR *extp = NULL;
3722 WCHAR *initdirp = NULL;
3723 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003724 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003725 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003726
3727 if (dflt == NULL)
3728 fileBuf[0] = NUL;
3729 else
3730 {
3731 wp = enc_to_utf16(dflt, NULL);
3732 if (wp == NULL)
3733 fileBuf[0] = NUL;
3734 else
3735 {
3736 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3737 fileBuf[i] = wp[i];
3738 fileBuf[i] = NUL;
3739 vim_free(wp);
3740 }
3741 }
3742
Bram Moolenaar734a8672019-12-02 22:49:38 +01003743 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003744 filterp = convert_filterW(filter);
3745
Bram Moolenaara80faa82020-04-12 19:37:17 +02003746 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003747# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003748 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003749 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003750# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003751 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003752# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003753
3754 if (title != NULL)
3755 titlep = enc_to_utf16(title, NULL);
3756 fileStruct.lpstrTitle = titlep;
3757
3758 if (ext != NULL)
3759 extp = enc_to_utf16(ext, NULL);
3760 fileStruct.lpstrDefExt = extp;
3761
3762 fileStruct.lpstrFile = fileBuf;
3763 fileStruct.nMaxFile = MAXPATHL;
3764 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003765 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3766 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003767 if (initdir != NULL && *initdir != NUL)
3768 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003769 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003770 initdirp = enc_to_utf16(initdir, NULL);
3771 if (initdirp != NULL)
3772 {
3773 for (wp = initdirp; *wp != NUL; ++wp)
3774 if (*wp == '/')
3775 *wp = '\\';
3776 }
3777 fileStruct.lpstrInitialDir = initdirp;
3778 }
3779
3780 /*
3781 * TODO: Allow selection of multiple files. Needs another arg to this
3782 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3783 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3784 * files that don't exist yet, so I haven't put it in. What about
3785 * OFN_PATHMUSTEXIST?
3786 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3787 */
3788 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003789# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003790 if (curbuf->b_p_bin)
3791 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003792# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003793 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003794 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003795 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003796 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003797
3798 vim_free(filterp);
3799 vim_free(initdirp);
3800 vim_free(titlep);
3801 vim_free(extp);
3802
K.Takata14b8d6a2022-01-20 15:05:22 +00003803 if (!ret)
3804 return NULL;
3805
3806 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003808 if (p == NULL)
3809 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003810
Bram Moolenaar734a8672019-12-02 22:49:38 +01003811 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003812 SetFocus(s_hwnd);
3813
Bram Moolenaar734a8672019-12-02 22:49:38 +01003814 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003815 q = vim_strsave(shorten_fname1(p));
3816 vim_free(p);
3817 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003818}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819
3820
3821/*
3822 * Convert the string s to the proper format for a filter string by replacing
3823 * the \t and \n delimiters with \0.
3824 * Returns the converted string in allocated memory.
3825 *
3826 * Keep in sync with convert_filterW() above!
3827 */
3828 static char_u *
3829convert_filter(char_u *s)
3830{
3831 char_u *res;
3832 unsigned s_len = (unsigned)STRLEN(s);
3833 unsigned i;
3834
3835 res = alloc(s_len + 3);
3836 if (res != NULL)
3837 {
3838 for (i = 0; i < s_len; ++i)
3839 if (s[i] == '\t' || s[i] == '\n')
3840 res[i] = '\0';
3841 else
3842 res[i] = s[i];
3843 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003844 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003845 res[s_len + 1] = NUL;
3846 res[s_len + 2] = NUL;
3847 }
3848 return res;
3849}
3850
3851/*
3852 * Select a directory.
3853 */
3854 char_u *
3855gui_mch_browsedir(char_u *title, char_u *initdir)
3856{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003857 // We fake this: Use a filter that doesn't select anything and a default
3858 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003859 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3860 initdir, (char_u *)_("Directory\t*.nothing\n"));
3861}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003862#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003864 static void
3865_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003866 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003867 HDROP hDrop)
3868{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003869#define BUFPATHLEN _MAX_PATH
3870#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003872 char szFile[BUFPATHLEN];
3873 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3874 UINT i;
3875 char_u **fnames;
3876 POINT pt;
3877 int_u modifiers = 0;
3878
Bram Moolenaar734a8672019-12-02 22:49:38 +01003879 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003880 DragQueryPoint(hDrop, &pt);
3881 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3882
3883 reset_VIsual();
3884
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003885 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003886
3887 if (fnames != NULL)
3888 for (i = 0; i < cFiles; ++i)
3889 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003890 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3891 fnames[i] = utf16_to_enc(wszFile, NULL);
3892 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003893 {
3894 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3895 fnames[i] = vim_strsave((char_u *)szFile);
3896 }
3897 }
3898
3899 DragFinish(hDrop);
3900
3901 if (fnames != NULL)
3902 {
LemonBoy45684c62022-04-24 15:46:42 +01003903 int kbd_modifiers = get_active_modifiers();
3904
3905 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003906 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003907 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003908 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003909 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003910 modifiers |= MOUSE_ALT;
3911
3912 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3913
3914 s_need_activate = TRUE;
3915 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003916}
3917
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003918 static int
3919_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003920 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921 HWND hwndCtl,
3922 UINT code,
3923 int pos)
3924{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003925 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003926 scrollbar_T *sb, *sb_info;
3927 long val;
3928 int dragging = FALSE;
3929 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 SCROLLINFO si;
3931
3932 si.cbSize = sizeof(si);
3933 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003934
3935 sb = gui_mswin_find_scrollbar(hwndCtl);
3936 if (sb == NULL)
3937 return 0;
3938
Bram Moolenaar734a8672019-12-02 22:49:38 +01003939 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003940 {
3941 /*
3942 * Careful: need to get scrollbar info out of first (left) scrollbar
3943 * for window, but keep real scrollbar too because we must pass it to
3944 * gui_drag_scrollbar().
3945 */
3946 sb_info = &sb->wp->w_scrollbars[0];
3947 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003948 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003949 sb_info = sb;
3950 val = sb_info->value;
3951
3952 switch (code)
3953 {
3954 case SB_THUMBTRACK:
3955 val = pos;
3956 dragging = TRUE;
3957 if (sb->scroll_shift > 0)
3958 val <<= sb->scroll_shift;
3959 break;
3960 case SB_LINEDOWN:
3961 val++;
3962 break;
3963 case SB_LINEUP:
3964 val--;
3965 break;
3966 case SB_PAGEDOWN:
3967 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3968 break;
3969 case SB_PAGEUP:
3970 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3971 break;
3972 case SB_TOP:
3973 val = 0;
3974 break;
3975 case SB_BOTTOM:
3976 val = sb_info->max;
3977 break;
3978 case SB_ENDSCROLL:
3979 if (prev_code == SB_THUMBTRACK)
3980 {
3981 /*
3982 * "pos" only gives us 16-bit data. In case of large file,
3983 * use GetScrollPos() which returns 32-bit. Unfortunately it
3984 * is not valid while the scrollbar is being dragged.
3985 */
3986 val = GetScrollPos(hwndCtl, SB_CTL);
3987 if (sb->scroll_shift > 0)
3988 val <<= sb->scroll_shift;
3989 }
3990 break;
3991
3992 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003993 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003994 return 0;
3995 }
3996 prev_code = code;
3997
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003998 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3999 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004000
4001 /*
4002 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4003 */
4004 if (sb->wp != NULL)
4005 {
4006 scrollbar_T *sba = sb->wp->w_scrollbars;
4007 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4008
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004009 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010 }
4011
Bram Moolenaar734a8672019-12-02 22:49:38 +01004012 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004013 s_busy_processing = TRUE;
4014
Bram Moolenaar734a8672019-12-02 22:49:38 +01004015 // When "allow_scrollbar" is FALSE still need to remember the new
4016 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004017 dont_scroll = !allow_scrollbar;
4018
Bram Moolenaara338adc2018-01-31 20:51:47 +01004019 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004020 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004021 mch_enable_flush();
4022 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004023
4024 s_busy_processing = FALSE;
4025 dont_scroll = dont_scroll_save;
4026
4027 return 0;
4028}
4029
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031#ifdef FEAT_XPM_W32
4032# include "xpm_w32.h"
4033#endif
4034
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaar734a8672019-12-02 22:49:38 +01004036// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#define TEAROFF_PADDING_X 2
4038#define TEAROFF_BUTTON_PAD_X 8
4039#define TEAROFF_MIN_WIDTH 200
4040#define TEAROFF_SUBMENU_LABEL ">>"
4041#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4042
4043
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004044#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045# define ID_BEVAL_TOOLTIP 200
4046# define BEVAL_TEXT_LEN MAXPATHL
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004049static UINT_PTR beval_timer_id = 0;
4050static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004051#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004052
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004053
Bram Moolenaar734a8672019-12-02 22:49:38 +01004054// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056#ifdef FEAT_MENU
4057static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060/*
4061 * Use the system font for dialogs and tear-off menus. Remove this line to
4062 * use DLG_FONT_NAME.
4063 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004064#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#define VIM_CLASSW L"Vim"
4068
Bram Moolenaar734a8672019-12-02 22:49:38 +01004069// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4070// thus there should be room for every dialog. For tearoffs it's made bigger
4071// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#define DLG_ALLOC_SIZE 16 * 1024
4073
4074/*
4075 * stuff for dialogs, menus, tearoffs etc.
4076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077static PWORD
4078add_dialog_element(
4079 PWORD p,
4080 DWORD lStyle,
4081 WORD x,
4082 WORD y,
4083 WORD w,
4084 WORD h,
4085 WORD Id,
4086 WORD clss,
4087 const char *caption);
4088static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004089static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004090#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093static void get_dialog_font_metrics(void);
4094
4095static int dialog_default_button = -1;
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097#ifdef FEAT_TOOLBAR
4098static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004099static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004100static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004102#else
4103# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
4105
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004106#ifdef FEAT_GUI_TABLINE
4107static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004108static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004109#endif
4110
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_MBYTE_IME
4112static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4113static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4114#endif
4115#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4116# ifdef NOIME
4117typedef struct tagCOMPOSITIONFORM {
4118 DWORD dwStyle;
4119 POINT ptCurrentPos;
4120 RECT rcArea;
4121} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4122typedef HANDLE HIMC;
4123# endif
4124
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004125static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004126static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4127static HIMC (WINAPI *pImmGetContext)(HWND);
4128static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4129static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4130static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4131static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004132static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4133static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004134static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4135static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004136static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137static void dyn_imm_load(void);
4138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139# define pImmGetCompositionStringW ImmGetCompositionStringW
4140# define pImmGetContext ImmGetContext
4141# define pImmAssociateContext ImmAssociateContext
4142# define pImmReleaseContext ImmReleaseContext
4143# define pImmGetOpenStatus ImmGetOpenStatus
4144# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004145# define pImmGetCompositionFontW ImmGetCompositionFontW
4146# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147# define pImmSetCompositionWindow ImmSetCompositionWindow
4148# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004149# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150#endif
4151
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152#ifdef FEAT_MENU
4153/*
4154 * Figure out how high the menu bar is at the moment.
4155 */
4156 static int
4157gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004158 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159{
4160 static int old_menu_height = -1;
4161
4162 RECT rc1, rc2;
4163 int num;
4164 int menu_height;
4165
4166 if (gui.menu_is_active)
4167 num = GetMenuItemCount(s_menuBar);
4168 else
4169 num = 0;
4170
4171 if (num == 0)
4172 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004173 else if (IsMinimized(s_hwnd))
4174 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004175 // The height of the menu cannot be determined while the window is
4176 // minimized. Take the previous height if the menu is changed in that
4177 // state, to avoid that Vim's vertical window size accidentally
4178 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004179 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 else
4182 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004183 /*
4184 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4185 * seem to have been set yet, so menu wraps in default window
4186 * width which is very narrow. Instead just return height of a
4187 * single menu item. Will still be wrong when the menu really
4188 * should wrap over more than one line.
4189 */
4190 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4191 if (gui.starting)
4192 menu_height = rc1.bottom - rc1.top + 1;
4193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004195 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4196 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004201 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004202 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 return menu_height;
4205}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004206#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208
4209/*
4210 * Setup for the Intellimouse
4211 */
LemonBoyc27747e2022-05-07 12:25:40 +01004212 static long
4213mouse_vertical_scroll_step(void)
4214{
4215 UINT val;
4216 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4217 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4218 return 3; // Safe default;
4219}
4220
4221 static long
4222mouse_horizontal_scroll_step(void)
4223{
4224 UINT val;
4225 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4226 return (long)val;
4227 return 3; // Safe default;
4228}
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 static void
4231init_mouse_wheel(void)
4232{
LemonBoyc27747e2022-05-07 12:25:40 +01004233 // Get the default values for the horizontal and vertical scroll steps from
4234 // the system.
4235 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4236 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237}
4238
Bram Moolenaarae20f342019-11-05 21:09:23 +01004239/*
LemonBoya773d842022-05-10 20:54:46 +01004240 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004243_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
LemonBoy365d8f72022-05-05 19:23:07 +01004245 int button;
4246 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004247 int modifiers = 0;
4248 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004249 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4250 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaarae20f342019-11-05 21:09:23 +01004252 wp = gui_mouse_window(FIND_POPUP);
4253
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004254#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004255 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004256 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004257 cmdarg_T cap;
4258 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004259
Bram Moolenaarae20f342019-11-05 21:09:23 +01004260 // Mouse hovers over popup window, scroll it if possible.
4261 mouse_row = wp->w_winrow;
4262 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004263 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004264 if (horizontal)
4265 {
4266 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4267 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4268 }
4269 else
4270 {
4271 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4272 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4273 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004274 clear_oparg(&oa);
4275 cap.oap = &oa;
4276 nv_mousescroll(&cap);
4277 update_screen(0);
4278 setcursor();
4279 out_flush();
4280 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004281 }
4282#endif
4283
Bram Moolenaarae20f342019-11-05 21:09:23 +01004284 if (wp == NULL || !p_scf)
4285 wp = curwin;
4286
LemonBoy365d8f72022-05-05 19:23:07 +01004287 // Translate the scroll event into an event that Vim can process so that
4288 // the user has a chance to map the scrollwheel buttons.
4289 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004290 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 else
LemonBoy365d8f72022-05-05 19:23:07 +01004292 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004293
4294 kbd_modifiers = get_active_modifiers();
4295
4296 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4297 modifiers |= MOUSE_SHIFT;
4298 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4299 modifiers |= MOUSE_CTRL;
4300 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4301 modifiers |= MOUSE_ALT;
4302
LemonBoya773d842022-05-10 20:54:46 +01004303 // The cursor position is relative to the upper-left corner of the screen.
4304 pt.x = GET_X_LPARAM(lParam);
4305 pt.y = GET_Y_LPARAM(lParam);
4306 ScreenToClient(s_textArea, &pt);
4307
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004308 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309}
4310
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311#ifdef USE_SYSMENU_FONT
4312/*
4313 * Get Menu Font.
4314 * Return OK or FAIL.
4315 */
4316 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004317gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004318{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004319 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004320
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004321 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4322 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004323 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004324 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004325 &nm,
4326 0))
4327 return FAIL;
4328 *lf = nm.lfMenuFont;
4329 return OK;
4330}
4331#endif
4332
4333
4334#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4335/*
4336 * Set the GUI tabline font to the system menu font
4337 */
4338 static void
4339set_tabline_font(void)
4340{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004341 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004342 HFONT font;
4343 HWND hwnd;
4344 HDC hdc;
4345 HFONT hfntOld;
4346 TEXTMETRIC tm;
4347
4348 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4349 return;
4350
K.Takatac81e9bf2022-01-16 14:15:49 +00004351 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004352 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004353
4354 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4355
4356 /*
4357 * Compute the height of the font used for the tab text
4358 */
4359 hwnd = GetDesktopWindow();
4360 hdc = GetWindowDC(hwnd);
4361 hfntOld = SelectFont(hdc, font);
4362
4363 GetTextMetrics(hdc, &tm);
4364
4365 SelectFont(hdc, hfntOld);
4366 ReleaseDC(hwnd, hdc);
4367
4368 /*
4369 * The space used by the tab border and the space between the tab label
4370 * and the tab border is included as 7.
4371 */
4372 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4373}
K.Takatac81e9bf2022-01-16 14:15:49 +00004374#else
4375# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004376#endif
4377
Bram Moolenaar520470a2005-06-16 21:59:56 +00004378/*
4379 * Invoked when a setting was changed.
4380 */
4381 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004382_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004383{
LemonBoy365d8f72022-05-05 19:23:07 +01004384 switch (param)
4385 {
4386 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004387 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004388 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004389 case SPI_SETWHEELSCROLLCHARS:
4390 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004391 break;
4392 case SPI_SETNONCLIENTMETRICS:
4393 set_tabline_font();
4394 break;
4395 default:
4396 break;
4397 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004398 return 0;
4399}
4400
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401#ifdef FEAT_NETBEANS_INTG
4402 static void
4403_OnWindowPosChanged(
4404 HWND hwnd,
4405 const LPWINDOWPOS lpwpos)
4406{
4407 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004408 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4411 || lpwpos->cx != cx || lpwpos->cy != cy))
4412 {
4413 x = lpwpos->x;
4414 y = lpwpos->y;
4415 cx = lpwpos->cx;
4416 cy = lpwpos->cy;
4417 netbeans_frame_moved(x, y);
4418 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004419 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004420 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421}
4422#endif
4423
K.Takata4f2417f2021-06-05 16:25:32 +02004424
4425static HWND hwndTip = NULL;
4426
4427 static void
4428show_sizing_tip(int cols, int rows)
4429{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004430 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004431 char buf[32];
4432
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004433 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004434 ti.hwnd = s_hwnd;
4435 ti.uId = (UINT_PTR)s_hwnd;
4436 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4437 ti.lpszText = buf;
4438 sprintf(buf, "%dx%d", cols, rows);
4439 if (hwndTip == NULL)
4440 {
4441 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4442 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4443 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4444 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4445 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4446 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4447 }
4448 else
4449 {
4450 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4451 }
4452 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4453}
4454
4455 static void
4456destroy_sizing_tip(void)
4457{
4458 if (hwndTip != NULL)
4459 {
4460 DestroyWindow(hwndTip);
4461 hwndTip = NULL;
4462 }
4463}
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 static int
4466_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 UINT fwSide,
4468 LPRECT lprc)
4469{
4470 int w, h;
4471 int valid_w, valid_h;
4472 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004473 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
4475 w = lprc->right - lprc->left;
4476 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004477 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 w_offset = w - valid_w;
4479 h_offset = h - valid_h;
4480
4481 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4482 || fwSide == WMSZ_BOTTOMLEFT)
4483 lprc->left += w_offset;
4484 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4485 || fwSide == WMSZ_BOTTOMRIGHT)
4486 lprc->right -= w_offset;
4487
4488 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4489 || fwSide == WMSZ_TOPRIGHT)
4490 lprc->top += h_offset;
4491 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4492 || fwSide == WMSZ_BOTTOMRIGHT)
4493 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004494
4495 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 return TRUE;
4497}
4498
K.Takata92000e22022-01-20 15:10:57 +00004499#ifdef FEAT_GUI_TABLINE
4500 static void
4501_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4502{
4503 if (gui_mch_showing_tabline())
4504 {
4505 POINT pt;
4506 RECT rect;
4507
4508 /*
4509 * If the cursor is on the tabline, display the tab menu
4510 */
4511 GetCursorPos(&pt);
4512 GetWindowRect(s_textArea, &rect);
4513 if (pt.y < rect.top)
4514 {
4515 show_tabline_popup_menu();
4516 return;
4517 }
4518 }
4519 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4520}
4521
4522 static void
4523_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4524{
4525 /*
4526 * If the user double clicked the tabline, create a new tab
4527 */
4528 if (gui_mch_showing_tabline())
4529 {
4530 POINT pt;
4531 RECT rect;
4532
4533 GetCursorPos(&pt);
4534 GetWindowRect(s_textArea, &rect);
4535 if (pt.y < rect.top)
4536 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4537 }
4538 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4539}
4540#endif
4541
4542 static UINT
4543_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4544{
4545 UINT result;
4546 int x, y;
4547
4548 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4549 if (result != HTCLIENT)
4550 return result;
4551
4552#ifdef FEAT_GUI_TABLINE
4553 if (gui_mch_showing_tabline())
4554 {
4555 RECT rct;
4556
4557 // If the cursor is on the GUI tabline, don't process this event
4558 GetWindowRect(s_textArea, &rct);
4559 if (yPos < rct.top)
4560 return result;
4561 }
4562#endif
4563 (void)gui_mch_get_winpos(&x, &y);
4564 xPos -= x;
4565
4566 if (xPos < 48) // <VN> TODO should use system metric?
4567 return HTBOTTOMLEFT;
4568 else
4569 return HTBOTTOMRIGHT;
4570}
4571
4572#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4573 static LRESULT
4574_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4575{
4576 switch (hdr->code)
4577 {
4578 case TTN_GETDISPINFOW:
4579 case TTN_GETDISPINFO:
4580 {
4581 char_u *str = NULL;
4582 static void *tt_text = NULL;
4583
4584 VIM_CLEAR(tt_text);
4585
4586# ifdef FEAT_GUI_TABLINE
4587 if (gui_mch_showing_tabline()
4588 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4589 {
4590 POINT pt;
4591 /*
4592 * Mouse is over the GUI tabline. Display the
4593 * tooltip for the tab under the cursor
4594 *
4595 * Get the cursor position within the tab control
4596 */
4597 GetCursorPos(&pt);
4598 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4599 {
4600 TCHITTESTINFO htinfo;
4601 int idx;
4602
4603 /*
4604 * Get the tab under the cursor
4605 */
4606 htinfo.pt.x = pt.x;
4607 htinfo.pt.y = pt.y;
4608 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4609 if (idx != -1)
4610 {
4611 tabpage_T *tp;
4612
4613 tp = find_tabpage(idx + 1);
4614 if (tp != NULL)
4615 {
4616 get_tabline_label(tp, TRUE);
4617 str = NameBuff;
4618 }
4619 }
4620 }
4621 }
4622# endif
4623# ifdef FEAT_TOOLBAR
4624# ifdef FEAT_GUI_TABLINE
4625 else
4626# endif
4627 {
4628 UINT idButton;
4629 vimmenu_T *pMenu;
4630
4631 idButton = (UINT) hdr->idFrom;
4632 pMenu = gui_mswin_find_menu(root_menu, idButton);
4633 if (pMenu)
4634 str = pMenu->strings[MENU_INDEX_TIP];
4635 }
4636# endif
4637 if (str == NULL)
4638 break;
4639
4640 // Set the maximum width, this also enables using \n for
4641 // line break.
4642 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4643
4644 if (hdr->code == TTN_GETDISPINFOW)
4645 {
4646 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4647
4648 tt_text = enc_to_utf16(str, NULL);
4649 lpdi->lpszText = tt_text;
4650 // can't show tooltip if failed
4651 }
4652 else
4653 {
4654 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4655
4656 if (STRLEN(str) < sizeof(lpdi->szText)
4657 || ((tt_text = vim_strsave(str)) == NULL))
4658 vim_strncpy((char_u *)lpdi->szText, str,
4659 sizeof(lpdi->szText) - 1);
4660 else
4661 lpdi->lpszText = tt_text;
4662 }
4663 }
4664 break;
4665
4666# ifdef FEAT_GUI_TABLINE
4667 case TCN_SELCHANGE:
4668 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4669 {
4670 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4671 return 0L;
4672 }
4673 break;
4674
4675 case NM_RCLICK:
4676 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4677 {
4678 show_tabline_popup_menu();
4679 return 0L;
4680 }
4681 break;
4682# endif
4683
4684 default:
4685 break;
4686 }
4687 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4688}
4689#endif
4690
4691#if defined(MENUHINTS) && defined(FEAT_MENU)
4692 static LRESULT
4693_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4694{
4695 if (((UINT) HIWORD(wParam)
4696 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4697 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004698 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004699 {
4700 UINT idButton;
4701 vimmenu_T *pMenu;
4702 static int did_menu_tip = FALSE;
4703
4704 if (did_menu_tip)
4705 {
4706 msg_clr_cmdline();
4707 setcursor();
4708 out_flush();
4709 did_menu_tip = FALSE;
4710 }
4711
4712 idButton = (UINT)LOWORD(wParam);
4713 pMenu = gui_mswin_find_menu(root_menu, idButton);
4714 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4715 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4716 {
4717 ++msg_hist_off;
4718 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4719 --msg_hist_off;
4720 setcursor();
4721 out_flush();
4722 did_menu_tip = TRUE;
4723 }
4724 return 0L;
4725 }
4726 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4727}
4728#endif
4729
K.Takatac81e9bf2022-01-16 14:15:49 +00004730 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004731_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004732{
4733 s_dpi = ydpi;
4734 s_in_dpichanged = TRUE;
4735 //TRACE("DPI: %d", ydpi);
4736
4737 update_scrollbar_size();
4738 update_toolbar_size();
4739 set_tabline_font();
4740
4741 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4742 gui_get_wide_font();
4743 gui_mswin_get_menu_height(FALSE);
4744#ifdef FEAT_MBYTE_IME
4745 im_set_position(gui.row, gui.col);
4746#endif
4747 InvalidateRect(hwnd, NULL, TRUE);
4748
4749 s_in_dpichanged = FALSE;
4750 return 0L;
4751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753
4754 static LRESULT CALLBACK
4755_WndProc(
4756 HWND hwnd,
4757 UINT uMsg,
4758 WPARAM wParam,
4759 LPARAM lParam)
4760{
LemonBoya773d842022-05-10 20:54:46 +01004761 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4762 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
4764 HandleMouseHide(uMsg, lParam);
4765
4766 s_uMsg = uMsg;
4767 s_wParam = wParam;
4768 s_lParam = lParam;
4769
4770 switch (uMsg)
4771 {
4772 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4773 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004774 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004776 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4778 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4779 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4780 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4781#ifdef FEAT_MENU
4782 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4783#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004784 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4785 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4787 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004788 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4789 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4791 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4792 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4793#ifdef FEAT_NETBEANS_INTG
4794 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4795#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004796#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004797 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4798 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004799#endif
K.Takata92000e22022-01-20 15:10:57 +00004800 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004801
Bram Moolenaar734a8672019-12-02 22:49:38 +01004802 case WM_QUERYENDSESSION: // System wants to go down.
4803 gui_shell_closed(); // Will exit when no changed buffers.
4804 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004807 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004810 return 0L;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 break;
4813
4814 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004815 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4816 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return 0L;
4819
4820 case WM_SYSCHAR:
4821 /*
4822 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4823 * shortcut key, handle like a typed ALT key, otherwise call Windows
4824 * ALT key handling.
4825 */
4826#ifdef FEAT_MENU
4827 if ( !gui.menu_is_active
4828 || p_wak[0] == 'n'
4829 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4830 )
4831#endif
4832 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004833 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return 0L;
4835 }
4836#ifdef FEAT_MENU
4837 else
K.Takata4ac893f2022-01-20 12:44:28 +00004838 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#endif
4840
4841 case WM_SYSKEYUP:
4842#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004843 // This used to be done only when menu is active: ALT key is used for
4844 // that. But that caused problems when menu is disabled and using
4845 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4846 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004847 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004849 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#endif
4851
K.Takata4f2417f2021-06-05 16:25:32 +02004852 case WM_EXITSIZEMOVE:
4853 destroy_sizing_tip();
4854 break;
4855
Bram Moolenaar734a8672019-12-02 22:49:38 +01004856 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004857 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004860 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004861 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004862 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar734a8672019-12-02 22:49:38 +01004864 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004865 case WM_SETTINGCHANGE:
4866 return _OnSettingChange((UINT)wParam);
4867
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004868#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004870 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871#endif
K.Takata92000e22022-01-20 15:10:57 +00004872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#if defined(MENUHINTS) && defined(FEAT_MENU)
4874 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004875 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877
4878#ifdef FEAT_MBYTE_IME
4879 case WM_IME_NOTIFY:
4880 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004881 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004882 return 1L;
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 case WM_IME_COMPOSITION:
4885 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004886 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004887 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004889 case WM_DPICHANGED:
4890 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4891 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004895 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#endif
K.Takata92000e22022-01-20 15:10:57 +00004898 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900
K.Takata4ac893f2022-01-20 12:44:28 +00004901 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902}
4903
4904/*
4905 * End of call-back routines
4906 */
4907
Bram Moolenaar734a8672019-12-02 22:49:38 +01004908// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909HWND vim_parent_hwnd = NULL;
4910
4911 static BOOL CALLBACK
4912FindWindowTitle(HWND hwnd, LPARAM lParam)
4913{
4914 char buf[2048];
4915 char *title = (char *)lParam;
4916
4917 if (GetWindowText(hwnd, buf, sizeof(buf)))
4918 {
4919 if (strstr(buf, title) != NULL)
4920 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004921 // Found it. Store the window ref. and quit searching if MDI
4922 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004924 if (vim_parent_hwnd != NULL)
4925 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
4927 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004928 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930
4931/*
4932 * Invoked for '-P "title"' argument: search for parent application to open
4933 * our window in.
4934 */
4935 void
4936gui_mch_set_parent(char *title)
4937{
4938 EnumWindows(FindWindowTitle, (LPARAM)title);
4939 if (vim_parent_hwnd == NULL)
4940 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004941 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 mch_exit(2);
4943 }
4944}
4945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004946#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 static void
4948ole_error(char *arg)
4949{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004950 char buf[IOSIZE];
4951
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004952# ifdef VIMDLL
4953 gui.in_use = mch_is_gui_executable();
4954# endif
4955
Bram Moolenaar734a8672019-12-02 22:49:38 +01004956 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004957 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004958 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004959 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004963#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4964 static char *
4965gvim_error(void)
4966{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004967 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004968
4969 if (starting)
4970 {
4971 mch_errmsg(msg);
4972 mch_errmsg("\n");
4973 mch_exit(2);
4974 }
4975 return msg;
4976}
4977
4978 char *
4979gui_mch_do_spawn(char_u *arg)
4980{
4981 int len;
4982# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4983 char_u *session = NULL;
4984 LPWSTR tofree1 = NULL;
4985# endif
4986 WCHAR name[MAX_PATH];
4987 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4988 STARTUPINFOW si = {sizeof(si)};
4989 PROCESS_INFORMATION pi;
4990
4991 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4992 goto error;
4993 p = wcsrchr(name, L'\\');
4994 if (p == NULL)
4995 goto error;
4996 // Replace the executable name from vim(d).exe to gvim(d).exe.
4997# ifdef DEBUG
4998 wcscpy(p + 1, L"gvimd.exe");
4999# else
5000 wcscpy(p + 1, L"gvim.exe");
5001# endif
5002
5003# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5004 if (starting)
5005# endif
5006 {
5007 // Pass the command line to the new process.
5008 p = GetCommandLineW();
5009 // Skip 1st argument.
5010 while (*p && *p != L' ' && *p != L'\t')
5011 {
5012 if (*p == L'"')
5013 {
5014 while (*p && *p != L'"')
5015 ++p;
5016 if (*p)
5017 ++p;
5018 }
5019 else
5020 ++p;
5021 }
5022 cmd = p;
5023 }
5024# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5025 else
5026 {
5027 // Create a session file and pass it to the new process.
5028 LPWSTR wsession;
5029 char_u *savebg;
5030 int ret;
5031
5032 session = vim_tempname('s', FALSE);
5033 if (session == NULL)
5034 goto error;
5035 savebg = p_bg;
5036 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5037 ret = write_session_file(session);
5038 vim_free(p_bg);
5039 p_bg = savebg;
5040 if (!ret)
5041 goto error;
5042 wsession = enc_to_utf16(session, NULL);
5043 if (wsession == NULL)
5044 goto error;
5045 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005046 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005047 if (cmd == NULL)
5048 {
5049 vim_free(wsession);
5050 goto error;
5051 }
5052 tofree1 = cmd;
5053 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5054 wsession, wsession);
5055 vim_free(wsession);
5056 }
5057# endif
5058
5059 // Check additional arguments to the `:gui` command.
5060 if (arg != NULL)
5061 {
5062 warg = enc_to_utf16(arg, NULL);
5063 if (warg == NULL)
5064 goto error;
5065 tofree2 = warg;
5066 }
5067 else
5068 warg = L"";
5069
5070 // Set up the new command line.
5071 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005072 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005073 if (newcmd == NULL)
5074 goto error;
5075 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5076
5077 // Spawn a new GUI process.
5078 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5079 NULL, NULL, &si, &pi))
5080 goto error;
5081 CloseHandle(pi.hProcess);
5082 CloseHandle(pi.hThread);
5083 mch_exit(0);
5084
5085error:
5086# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5087 if (session)
5088 mch_remove(session);
5089 vim_free(session);
5090 vim_free(tofree1);
5091# endif
5092 vim_free(newcmd);
5093 vim_free(tofree2);
5094 return gvim_error();
5095}
5096#endif
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098/*
5099 * Parse the GUI related command-line arguments. Any arguments used are
5100 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005101 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 */
5103 void
5104gui_mch_prepare(int *argc, char **argv)
5105{
5106 int silent = FALSE;
5107 int idx;
5108
Bram Moolenaar734a8672019-12-02 22:49:38 +01005109 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5111 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005112 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5114 && (argv[2][0] == '-' || argv[2][0] == '/'))
5115 {
5116 silent = TRUE;
5117 idx = 2;
5118 }
5119 else
5120 idx = 1;
5121
Bram Moolenaar734a8672019-12-02 22:49:38 +01005122 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 if (STRICMP(argv[idx] + 1, "register") == 0)
5124 {
5125#ifdef FEAT_OLE
5126 RegisterMe(silent);
5127 mch_exit(0);
5128#else
5129 if (!silent)
5130 ole_error("register");
5131 mch_exit(2);
5132#endif
5133 }
5134
Bram Moolenaar734a8672019-12-02 22:49:38 +01005135 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5137 {
5138#ifdef FEAT_OLE
5139 UnregisterMe(!silent);
5140 mch_exit(0);
5141#else
5142 if (!silent)
5143 ole_error("unregister");
5144 mch_exit(2);
5145#endif
5146 }
5147
Bram Moolenaar734a8672019-12-02 22:49:38 +01005148 // Ignore an -embedding argument. It is only relevant if the
5149 // application wants to treat the case when it is started manually
5150 // differently from the case where it is started via automation (and
5151 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5153 {
5154#ifdef FEAT_OLE
5155 *argc = 1;
5156#else
5157 ole_error("embedding");
5158 mch_exit(2);
5159#endif
5160 }
5161 }
5162
5163#ifdef FEAT_OLE
5164 {
5165 int bDoRestart = FALSE;
5166
5167 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005168 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (bDoRestart)
5170 mch_exit(0);
5171 }
5172#endif
5173
5174#ifdef FEAT_NETBEANS_INTG
5175 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005176 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 int arg;
5178
5179 for (arg = 1; arg < *argc; arg++)
5180 if (strncmp("-nb", argv[arg], 3) == 0)
5181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 netbeansArg = argv[arg];
5183 mch_memmove(&argv[arg], &argv[arg + 1],
5184 (--*argc - arg) * sizeof(char *));
5185 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005186 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191
K.Takatac81e9bf2022-01-16 14:15:49 +00005192 static void
5193load_dpi_func(void)
5194{
5195 HMODULE hUser32;
5196
5197 hUser32 = GetModuleHandle("user32.dll");
5198 if (hUser32 == NULL)
5199 goto fail;
5200
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005201 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5202 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5203 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005204 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005205 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5206 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005207
5208 if (pSetThreadDpiAwarenessContext != NULL)
5209 {
5210 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5211 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5212 if (oldctx != NULL)
5213 {
5214 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5215 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5216#ifdef DEBUG
5217 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5218 {
5219 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5220 }
5221#endif
5222 return;
5223 }
5224 }
5225
5226fail:
5227 // Disable PerMonitorV2 APIs.
5228 pGetDpiForSystem = stubGetDpiForSystem;
5229 pGetDpiForWindow = NULL;
5230 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5231 pSetThreadDpiAwarenessContext = NULL;
5232 pGetAwarenessFromDpiAwarenessContext = NULL;
5233}
5234
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235/*
5236 * Initialise the GUI. Create all the windows, set up all the call-backs
5237 * etc.
5238 */
5239 int
5240gui_mch_init(void)
5241{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005243 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
Bram Moolenaar734a8672019-12-02 22:49:38 +01005246 // Return here if the window was already opened (happens when
5247 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005249 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250
5251 /*
5252 * Load the tearoff bitmap
5253 */
5254#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005255 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257
K.Takatac81e9bf2022-01-16 14:15:49 +00005258 load_dpi_func();
5259
5260 s_dpi = pGetDpiForSystem();
5261 update_scrollbar_size();
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005264 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005267#ifdef FEAT_TOOLBAR
5268 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5272
Bram Moolenaar734a8672019-12-02 22:49:38 +01005273 // First try using the wide version, so that we can use any title.
5274 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005275 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005277 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 wndclassw.lpfnWndProc = _WndProc;
5279 wndclassw.cbClsExtra = 0;
5280 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005281 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5283 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5284 wndclassw.hbrBackground = s_brush;
5285 wndclassw.lpszMenuName = NULL;
5286 wndclassw.lpszClassName = szVimWndClassW;
5287
K.Takata4ac893f2022-01-20 12:44:28 +00005288 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (vim_parent_hwnd != NULL)
5293 {
5294#ifdef HAVE_TRY_EXCEPT
5295 __try
5296 {
5297#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005298 // Open inside the specified parent window.
5299 // TODO: last argument should point to a CLIENTCREATESTRUCT
5300 // structure.
5301 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005303 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005304 WS_OVERLAPPEDWINDOW | WS_CHILD
5305 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5307 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005308 100, // Any value will do
5309 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005311 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#ifdef HAVE_TRY_EXCEPT
5313 }
5314 __except(EXCEPTION_EXECUTE_HANDLER)
5315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005316 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
5318#endif
5319 if (s_hwnd == NULL)
5320 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005321 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 mch_exit(2);
5323 }
5324 }
5325 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005326 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005327 // If the provided windowid is not valid reset it to zero, so that it
5328 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005329 if (IsWindow((HWND)win_socket_id) <= 0)
5330 win_socket_id = 0;
5331
Bram Moolenaar734a8672019-12-02 22:49:38 +01005332 // Create a window. If win_socket_id is not zero without border and
5333 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005334 s_hwnd = CreateWindowW(
5335 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005336 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5337 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005338 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5339 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005340 100, // Any value will do
5341 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005342 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005343 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005344 if (s_hwnd != NULL && win_socket_id != 0)
5345 {
5346 SetParent(s_hwnd, (HWND)win_socket_id);
5347 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5348 }
5349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 if (s_hwnd == NULL)
5352 return FAIL;
5353
K.Takatac81e9bf2022-01-16 14:15:49 +00005354 if (pGetDpiForWindow != NULL)
5355 {
5356 s_dpi = pGetDpiForWindow(s_hwnd);
5357 update_scrollbar_size();
5358 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5359 }
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5362 dyn_imm_load();
5363#endif
5364
Bram Moolenaar734a8672019-12-02 22:49:38 +01005365 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005366 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005367 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005368 wndclassw.style = CS_OWNDC;
5369 wndclassw.lpfnWndProc = _TextAreaWndProc;
5370 wndclassw.cbClsExtra = 0;
5371 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005372 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005373 wndclassw.hIcon = NULL;
5374 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5375 wndclassw.hbrBackground = NULL;
5376 wndclassw.lpszMenuName = NULL;
5377 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005378
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005379 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 return FAIL;
5381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005383 s_textArea = CreateWindowExW(
5384 0,
5385 szTextAreaClassW, L"Vim text area",
5386 WS_CHILD | WS_VISIBLE, 0, 0,
5387 100, // Any value will do for now
5388 100, // Any value will do for now
5389 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005390 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 if (s_textArea == NULL)
5393 return FAIL;
5394
Bram Moolenaar20321902016-02-17 12:30:17 +01005395#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005396 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005397 {
5398 HANDLE hIcon = NULL;
5399
5400 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005401 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005402 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005403#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405#ifdef FEAT_MENU
5406 s_menuBar = CreateMenu();
5407#endif
5408 s_hdc = GetDC(s_textArea);
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar734a8672019-12-02 22:49:38 +01005412 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005413 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
Bram Moolenaar734a8672019-12-02 22:49:38 +01005415 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 gui_mch_def_colors();
5417
Bram Moolenaar734a8672019-12-02 22:49:38 +01005418 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5419 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 set_normal_colors();
5421
5422 /*
5423 * Check that none of the colors are the same as the background color.
5424 * Then store the current values as the defaults.
5425 */
5426 gui_check_colors();
5427 gui.def_norm_pixel = gui.norm_pixel;
5428 gui.def_back_pixel = gui.back_pixel;
5429
Bram Moolenaar734a8672019-12-02 22:49:38 +01005430 // Get the colors for the highlight groups (gui_check_colors() might have
5431 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 highlight_gui_started();
5433
5434 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005435 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005437 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
5439 /*
5440 * Set up for Intellimouse processing
5441 */
5442 init_mouse_wheel();
5443
5444 /*
5445 * compute a couple of metrics used for the dialogs
5446 */
5447 get_dialog_font_metrics();
5448#ifdef FEAT_TOOLBAR
5449 /*
5450 * Create the toolbar
5451 */
5452 initialise_toolbar();
5453#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005454#ifdef FEAT_GUI_TABLINE
5455 /*
5456 * Create the tabline
5457 */
5458 initialise_tabline();
5459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460#ifdef MSWIN_FIND_REPLACE
5461 /*
5462 * Initialise the dialog box stuff
5463 */
5464 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5465
Bram Moolenaar734a8672019-12-02 22:49:38 +01005466 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005468 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005470 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5472 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5473 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005476#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005477 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005478 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005479#endif
5480
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005481#ifdef FEAT_RENDER_OPTIONS
5482 if (p_rop)
5483 (void)gui_mch_set_rendering_options(p_rop);
5484#endif
5485
Bram Moolenaar748bf032005-02-02 23:04:36 +00005486theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005487 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005488 display_errors();
5489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 return OK;
5491}
5492
5493/*
5494 * Get the size of the screen, taking position on multiple monitors into
5495 * account (if supported).
5496 */
5497 static void
5498get_work_area(RECT *spi_rect)
5499{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005500 HMONITOR mon;
5501 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
Bram Moolenaar734a8672019-12-02 22:49:38 +01005503 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005504 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005505 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005507 moninfo.cbSize = sizeof(MONITORINFO);
5508 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005510 *spi_rect = moninfo.rcWork;
5511 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 }
5513 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005514 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5516}
5517
5518/*
5519 * Set the size of the window to the given width and height in pixels.
5520 */
5521 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005522gui_mch_set_shellsize(
5523 int width,
5524 int height,
5525 int min_width UNUSED,
5526 int min_height UNUSED,
5527 int base_width UNUSED,
5528 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005529 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005532 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaar734a8672019-12-02 22:49:38 +01005535 // Try to keep window completely on screen.
5536 // Get position of the screen work area. This is the part that is not
5537 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 get_work_area(&workarea_rect);
5539
Bram Moolenaar734a8672019-12-02 22:49:38 +01005540 // Resizing a maximized window looks very strange, unzoom it first.
5541 // But don't do it when still starting up, it may have been requested in
5542 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005543 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005545
5546 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
Bram Moolenaar734a8672019-12-02 22:49:38 +01005548 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005549 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5550 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5551 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5552 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5553 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5554 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaar734a8672019-12-02 22:49:38 +01005556 // The following should take care of keeping Vim on the same monitor, no
5557 // matter if the secondary monitor is left or right of the primary
5558 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005559 window_rect.right = window_rect.left + win_width;
5560 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005561
Bram Moolenaar734a8672019-12-02 22:49:38 +01005562 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005563 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5564 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005566 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5567 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005569 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5570 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005572 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5573 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005575 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5576 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 SetActiveWindow(s_hwnd);
5579 SetFocus(s_hwnd);
5580
Bram Moolenaar734a8672019-12-02 22:49:38 +01005581 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583}
5584
5585
5586 void
5587gui_mch_set_scrollbar_thumb(
5588 scrollbar_T *sb,
5589 long val,
5590 long size,
5591 long max)
5592{
5593 SCROLLINFO info;
5594
5595 sb->scroll_shift = 0;
5596 while (max > 32767)
5597 {
5598 max = (max + 1) >> 1;
5599 val >>= 1;
5600 size >>= 1;
5601 ++sb->scroll_shift;
5602 }
5603
5604 if (sb->scroll_shift > 0)
5605 ++size;
5606
5607 info.cbSize = sizeof(info);
5608 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5609 info.nPos = val;
5610 info.nMin = 0;
5611 info.nMax = max;
5612 info.nPage = size;
5613 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5614}
5615
5616
5617/*
5618 * Set the current text font.
5619 */
5620 void
5621gui_mch_set_font(GuiFont font)
5622{
5623 gui.currFont = font;
5624}
5625
5626
5627/*
5628 * Set the current text foreground color.
5629 */
5630 void
5631gui_mch_set_fg_color(guicolor_T color)
5632{
5633 gui.currFgColor = color;
5634}
5635
5636/*
5637 * Set the current text background color.
5638 */
5639 void
5640gui_mch_set_bg_color(guicolor_T color)
5641{
5642 gui.currBgColor = color;
5643}
5644
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005645/*
5646 * Set the current text special color.
5647 */
5648 void
5649gui_mch_set_sp_color(guicolor_T color)
5650{
5651 gui.currSpColor = color;
5652}
5653
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005654#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655/*
5656 * Multi-byte handling, originally by Sung-Hoon Baek.
5657 * First static functions (no prototypes generated).
5658 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005659# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005660# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005661# endif
5662# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663
5664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 * handle WM_IME_NOTIFY message
5666 */
5667 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005668_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670 LRESULT lResult = 0;
5671 HIMC hImc;
5672
5673 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5674 return lResult;
5675 switch (dwCommand)
5676 {
5677 case IMN_SETOPENSTATUS:
5678 if (pImmGetOpenStatus(hImc))
5679 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005680 LOGFONTW lf = norm_logfont;
5681 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5682 // Work around when PerMonitorV2 is not enabled in the process level.
5683 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5684 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 im_set_position(gui.row, gui.col);
5686
Bram Moolenaar734a8672019-12-02 22:49:38 +01005687 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005688 State &= ~MODE_LANGMAP;
5689 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005691# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005692 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5694 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005695 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 int old_row = gui.row;
5697 int old_col = gui.col;
5698
5699 // This must be called here before
5700 // status_redraw_curbuf(), otherwise the mode
5701 // message may appear in the wrong position.
5702 showmode();
5703 status_redraw_curbuf();
5704 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005705 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 gui.row = old_row;
5707 gui.col = old_col;
5708 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711 }
5712 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005713 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 lResult = 0;
5715 break;
5716 }
5717 pImmReleaseContext(hWnd, hImc);
5718 return lResult;
5719}
5720
5721 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005722_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 char_u *ret;
5725 int len;
5726
Bram Moolenaar734a8672019-12-02 22:49:38 +01005727 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 return 0;
5729
5730 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5731 if (ret != NULL)
5732 {
5733 add_to_input_buf_csi(ret, len);
5734 vim_free(ret);
5735 return 1;
5736 }
5737 return 0;
5738}
5739
5740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 * void GetResultStr()
5742 *
5743 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5744 * get complete composition string
5745 */
5746 static char_u *
5747GetResultStr(HWND hwnd, int GCS, int *lenp)
5748{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005749 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005750 LONG ret;
5751 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 char_u *convbuf = NULL;
5753
5754 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5755 return NULL;
5756
K.Takatab0b2b732022-01-19 12:59:21 +00005757 // Get the length of the composition string.
5758 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5759 if (ret <= 0)
5760 return NULL;
5761
5762 // Allocate the requested buffer plus space for the NUL character.
5763 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 if (buf == NULL)
5765 return NULL;
5766
K.Takatab0b2b732022-01-19 12:59:21 +00005767 // Reads in the composition string.
5768 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5769 *lenp = ret / sizeof(WCHAR);
5770
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005771 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 pImmReleaseContext(hwnd, hIMC);
5773 vim_free(buf);
5774 return convbuf;
5775}
5776#endif
5777
Bram Moolenaar734a8672019-12-02 22:49:38 +01005778// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005779#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781/*
5782 * set font to IM.
5783 */
5784 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005785im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 HIMC hImc;
5788
5789 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5790 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005791 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 pImmReleaseContext(s_hwnd, hImc);
5793 }
5794}
5795
5796/*
5797 * Notify cursor position to IM.
5798 */
5799 void
5800im_set_position(int row, int col)
5801{
5802 HIMC hImc;
5803
5804 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5805 {
5806 COMPOSITIONFORM cfs;
5807
5808 cfs.dwStyle = CFS_POINT;
5809 cfs.ptCurrentPos.x = FILL_X(col);
5810 cfs.ptCurrentPos.y = FILL_Y(row);
5811 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005812 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5813 {
5814 // Work around when PerMonitorV2 is not enabled in the process level.
5815 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5816 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 pImmSetCompositionWindow(hImc, &cfs);
5819
5820 pImmReleaseContext(s_hwnd, hImc);
5821 }
5822}
5823
5824/*
5825 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5826 */
5827 void
5828im_set_active(int active)
5829{
5830 HIMC hImc;
5831 static HIMC hImcOld = (HIMC)0;
5832
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005833# ifdef VIMDLL
5834 if (!gui.in_use && !gui.starting)
5835 {
5836 mbyte_im_set_active(active);
5837 return;
5838 }
5839# endif
5840
Bram Moolenaar734a8672019-12-02 22:49:38 +01005841 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
5843 if (p_imdisable)
5844 {
5845 if (hImcOld == (HIMC)0)
5846 {
5847 hImcOld = pImmGetContext(s_hwnd);
5848 if (hImcOld)
5849 pImmAssociateContext(s_hwnd, (HIMC)0);
5850 }
5851 active = FALSE;
5852 }
5853 else if (hImcOld != (HIMC)0)
5854 {
5855 pImmAssociateContext(s_hwnd, hImcOld);
5856 hImcOld = (HIMC)0;
5857 }
5858
5859 hImc = pImmGetContext(s_hwnd);
5860 if (hImc)
5861 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005862 /*
5863 * for Korean ime
5864 */
5865 HKL hKL = GetKeyboardLayout(0);
5866
5867 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5868 {
5869 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5870 static BOOL bSaved = FALSE;
5871
5872 if (active)
5873 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005874 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005875 if (bSaved)
5876 pImmSetConversionStatus(hImc, dwConversionSaved,
5877 dwSentenceSaved);
5878 bSaved = FALSE;
5879 }
5880 else
5881 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005882 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005883 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5884 &dwSentenceSaved))
5885 {
5886 bSaved = TRUE;
5887 pImmSetConversionStatus(hImc,
5888 dwConversionSaved & ~(IME_CMODE_NATIVE
5889 | IME_CMODE_FULLSHAPE),
5890 dwSentenceSaved);
5891 }
5892 }
5893 }
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 pImmSetOpenStatus(hImc, active);
5896 pImmReleaseContext(s_hwnd, hImc);
5897 }
5898 }
5899}
5900
5901/*
5902 * Get IM status. When IM is on, return not 0. Else return 0.
5903 */
5904 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005905im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 int status = 0;
5908 HIMC hImc;
5909
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005910# ifdef VIMDLL
5911 if (!gui.in_use && !gui.starting)
5912 return mbyte_im_get_status();
5913# endif
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5916 {
5917 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5918 pImmReleaseContext(s_hwnd, hImc);
5919 }
5920 return status;
5921}
5922
Bram Moolenaar734a8672019-12-02 22:49:38 +01005923#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005926/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005927 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005928 */
5929 static void
5930latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5931{
5932 int c;
5933
Bram Moolenaarca003e12006-03-17 23:19:38 +00005934 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005935 {
5936 c = *text++;
5937 switch (c)
5938 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005939 case 0xa4: c = 0x20ac; break; // euro
5940 case 0xa6: c = 0x0160; break; // S hat
5941 case 0xa8: c = 0x0161; break; // S -hat
5942 case 0xb4: c = 0x017d; break; // Z hat
5943 case 0xb8: c = 0x017e; break; // Z -hat
5944 case 0xbc: c = 0x0152; break; // OE
5945 case 0xbd: c = 0x0153; break; // oe
5946 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005947 }
5948 *unicodebuf++ = c;
5949 }
5950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
5952#ifdef FEAT_RIGHTLEFT
5953/*
5954 * What is this for? In the case where you are using Win98 or Win2K or later,
5955 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5956 * reverses the string sent to the TextOut... family. This sucks, because we
5957 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5958 * way to tell Windblows not to do this!
5959 *
5960 * The short of it is that this 'RevOut' only gets called if you are running
5961 * one of the new, "improved" MS OSes, and only if you are running in
5962 * 'rightleft' mode. It makes display take *slightly* longer, but not
5963 * noticeably so.
5964 */
5965 static void
K.Takata135e1522022-01-29 15:27:58 +00005966RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 int col,
5968 int row,
5969 UINT foptions,
5970 CONST RECT *pcliprect,
5971 LPCTSTR text,
5972 UINT len,
5973 CONST INT *padding)
5974{
5975 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005977 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005978 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005979 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981#endif
5982
Bram Moolenaar92467d32017-12-05 13:22:16 +01005983 static void
5984draw_line(
5985 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005986 int y1,
5987 int x2,
5988 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005989 COLORREF color)
5990{
5991#if defined(FEAT_DIRECTX)
5992 if (IS_ENABLE_DIRECTX())
5993 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5994 else
5995#endif
5996 {
5997 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5998 HPEN old_pen = SelectObject(s_hdc, hpen);
5999 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006000 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006001 LineTo(s_hdc, x2, y2);
6002 DeleteObject(SelectObject(s_hdc, old_pen));
6003 }
6004}
6005
6006 static void
6007set_pixel(
6008 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006009 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006010 COLORREF color)
6011{
6012#if defined(FEAT_DIRECTX)
6013 if (IS_ENABLE_DIRECTX())
6014 DWriteContext_SetPixel(s_dwc, x, y, color);
6015 else
6016#endif
6017 SetPixel(s_hdc, x, y, color);
6018}
6019
6020 static void
6021fill_rect(
6022 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006023 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006024 COLORREF color)
6025{
6026#if defined(FEAT_DIRECTX)
6027 if (IS_ENABLE_DIRECTX())
6028 DWriteContext_FillRect(s_dwc, rcp, color);
6029 else
6030#endif
6031 {
6032 HBRUSH hbr2;
6033
6034 if (hbr == NULL)
6035 hbr2 = CreateSolidBrush(color);
6036 else
6037 hbr2 = hbr;
6038 FillRect(s_hdc, rcp, hbr2);
6039 if (hbr == NULL)
6040 DeleteBrush(hbr2);
6041 }
6042}
6043
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 void
6045gui_mch_draw_string(
6046 int row,
6047 int col,
6048 char_u *text,
6049 int len,
6050 int flags)
6051{
6052 static int *padding = NULL;
6053 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 const RECT *pcliprect = NULL;
6055 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 static WCHAR *unicodebuf = NULL;
6057 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006058 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 int y;
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 /*
6063 * Italic and bold text seems to have an extra row of pixels at the bottom
6064 * (below where the bottom of the character should be). If we draw the
6065 * characters with a solid background, the top row of pixels in the
6066 * character below will be overwritten. We can fix this by filling in the
6067 * background ourselves, to the correct character proportions, and then
6068 * writing the character in transparent mode. Still have a problem when
6069 * the character is "_", which gets written on to the character below.
6070 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6071 * pixel in their slots, which fixes the problem with the bottom row of
6072 * pixels. We still need this code because otherwise the top row of pixels
6073 * becomes a problem. - webb.
6074 */
6075 static HBRUSH hbr_cache[2] = {NULL, NULL};
6076 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6077 static int brush_lru = 0;
6078 HBRUSH hbr;
6079 RECT rc;
6080
6081 if (!(flags & DRAW_TRANSP))
6082 {
6083 /*
6084 * Clear background first.
6085 * Note: FillRect() excludes right and bottom of rectangle.
6086 */
6087 rc.left = FILL_X(col);
6088 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 if (has_mbyte)
6090 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006091 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006092 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
6094 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 rc.right = FILL_X(col + len);
6096 rc.bottom = FILL_Y(row + 1);
6097
Bram Moolenaar734a8672019-12-02 22:49:38 +01006098 // Cache the created brush, that saves a lot of time. We need two:
6099 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 if (gui.currBgColor == brush_color[0])
6101 {
6102 hbr = hbr_cache[0];
6103 brush_lru = 1;
6104 }
6105 else if (gui.currBgColor == brush_color[1])
6106 {
6107 hbr = hbr_cache[1];
6108 brush_lru = 0;
6109 }
6110 else
6111 {
6112 if (hbr_cache[brush_lru] != NULL)
6113 DeleteBrush(hbr_cache[brush_lru]);
6114 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6115 brush_color[brush_lru] = gui.currBgColor;
6116 hbr = hbr_cache[brush_lru];
6117 brush_lru = !brush_lru;
6118 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006119
Bram Moolenaar92467d32017-12-05 13:22:16 +01006120 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 SetBkMode(s_hdc, TRANSPARENT);
6123
6124 /*
6125 * When drawing block cursor, prevent inverted character spilling
6126 * over character cell (can happen with bold/italic)
6127 */
6128 if (flags & DRAW_CURSOR)
6129 {
6130 pcliprect = &rc;
6131 foptions = ETO_CLIPPED;
6132 }
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 SetTextColor(s_hdc, gui.currFgColor);
6135 SelectFont(s_hdc, gui.currFont);
6136
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006137#ifdef FEAT_DIRECTX
6138 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006139 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006140#endif
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6143 {
K.Takata135e1522022-01-29 15:27:58 +00006144 int i;
6145
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 vim_free(padding);
6147 pad_size = Columns;
6148
Bram Moolenaar734a8672019-12-02 22:49:38 +01006149 // Don't give an out-of-memory message here, it would call us
6150 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006151 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 if (padding != NULL)
6153 for (i = 0; i < pad_size; i++)
6154 padding[i] = gui.char_width;
6155 }
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 /*
6158 * We have to provide the padding argument because italic and bold versions
6159 * of fixed-width fonts are often one pixel or so wider than their normal
6160 * versions.
6161 * No check for DRAW_BOLD, Windows will have done it already.
6162 */
6163
Bram Moolenaar734a8672019-12-02 22:49:38 +01006164 // Check if there are any UTF-8 characters. If not, use normal text
6165 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 if (enc_utf8)
6167 for (n = 0; n < len; ++n)
6168 if (text[n] >= 0x80)
6169 break;
6170
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006171#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006172 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6173 // required that unicode drawing routine, currently. So this forces it
6174 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006175 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006176 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006177#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006178
Bram Moolenaar734a8672019-12-02 22:49:38 +01006179 // Check if the Unicode buffer exists and is big enough. Create it
6180 // with the same length as the multi-byte string, the number of wide
6181 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006182 if ((enc_utf8
6183 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6184 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 && (unicodebuf == NULL || len > unibuflen))
6186 {
6187 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006188 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189
6190 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006191 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192
6193 unibuflen = len;
6194 }
6195
6196 if (enc_utf8 && n < len && unicodebuf != NULL)
6197 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006198 // Output UTF-8 characters. Composing characters should be
6199 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006200 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006201 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006202 int cells; // cell width of string up to composing char
6203 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006204 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006206 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006208 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006210 c = utf_ptr2char(text + i);
6211 if (c >= 0x10000)
6212 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006213 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006214 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6215 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006216 }
6217 else
6218 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006219 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006220 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006221
6222 if (utf_iscomposing(c))
6223 cw = 0;
6224 else
6225 {
6226 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006227 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006228 cw = 1;
6229 }
6230
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 if (unicodepdy != NULL)
6232 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006233 // Use unicodepdy to make characters fit as we expect, even
6234 // when the font uses different widths (e.g., bold character
6235 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006236 if (c >= 0x10000)
6237 {
6238 unicodepdy[wlen - 2] = cw * gui.char_width;
6239 unicodepdy[wlen - 1] = 0;
6240 }
6241 else
6242 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006245 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006247#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006248 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006249 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006250 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006251 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006252 TEXT_X(col), TEXT_Y(row),
6253 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006254 gui.char_width, gui.currFgColor,
6255 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006256 }
6257 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006258#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006259 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6260 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006261 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006263 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006265 // If we want to display codepage data, and the current CP is not the
6266 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 if (unicodebuf != NULL)
6268 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006269 if (enc_latin9)
6270 latin9_to_ucs(text, len, unicodebuf);
6271 else
6272 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 MB_PRECOMPOSED,
6274 (char *)text, len,
6275 (LPWSTR)unicodebuf, unibuflen);
6276 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006277 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006278 // Use unicodepdy to make characters fit as we expect, even
6279 // when the font uses different widths (e.g., bold character
6280 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006281 if (unicodepdy != NULL)
6282 {
6283 int i;
6284 int cw;
6285
6286 for (i = 0; i < len; ++i)
6287 {
6288 cw = utf_char2cells(unicodebuf[i]);
6289 if (cw > 2)
6290 cw = 1;
6291 unicodepdy[i] = cw * gui.char_width;
6292 }
6293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006295 foptions, pcliprect, unicodebuf, len, unicodepdy);
6296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 }
6299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
6301#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006302 // Windows will mess up RL text, so we have to draw it character by
6303 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006304 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6306 foptions, pcliprect, (char *)text, len, padding);
6307 else
6308#endif
6309 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6310 foptions, pcliprect, (char *)text, len, padding);
6311 }
6312
Bram Moolenaar734a8672019-12-02 22:49:38 +01006313 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (flags & DRAW_UNDERL)
6315 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006316 // When p_linespace is 0, overwrite the bottom row of pixels.
6317 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (p_linespace > 1)
6320 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006321 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006323
Bram Moolenaar734a8672019-12-02 22:49:38 +01006324 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006325 if (flags & DRAW_STRIKE)
6326 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006327 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006328 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006329 }
6330
Bram Moolenaar734a8672019-12-02 22:49:38 +01006331 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006332 if (flags & DRAW_UNDERC)
6333 {
6334 int x;
6335 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006336 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006337
6338 y = FILL_Y(row + 1) - 1;
6339 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6340 {
6341 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006342 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006343 }
6344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345}
6346
6347
6348/*
6349 * Output routines.
6350 */
6351
Bram Moolenaar734a8672019-12-02 22:49:38 +01006352/*
6353 * Flush any output to the screen
6354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 void
6356gui_mch_flush(void)
6357{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006358#if defined(FEAT_DIRECTX)
6359 if (IS_ENABLE_DIRECTX())
6360 DWriteContext_Flush(s_dwc);
6361#endif
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 GdiFlush();
6364}
6365
6366 static void
6367clear_rect(RECT *rcp)
6368{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006369 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370}
6371
6372
Bram Moolenaarc716c302006-01-21 22:12:51 +00006373 void
6374gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6375{
6376 RECT workarea_rect;
6377
6378 get_work_area(&workarea_rect);
6379
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006380 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006381 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6382 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006383
Bram Moolenaar734a8672019-12-02 22:49:38 +01006384 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6385 // the menubar for MSwin, we subtract it from the screen height, so that
6386 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006387 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006388 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6389 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6390 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6391 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006392}
6393
6394
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395#if defined(FEAT_MENU) || defined(PROTO)
6396/*
6397 * Add a sub menu to the menu bar.
6398 */
6399 void
6400gui_mch_add_menu(
6401 vimmenu_T *menu,
6402 int pos)
6403{
6404 vimmenu_T *parent = menu->parent;
6405
6406 menu->submenu_id = CreatePopupMenu();
6407 menu->id = s_menu_id++;
6408
6409 if (menu_is_menubar(menu->name))
6410 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006411 WCHAR *wn;
6412 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006414 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006415 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006416 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006418 infow.cbSize = sizeof(infow);
6419 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6420 | MIIM_SUBMENU;
6421 infow.dwItemData = (long_u)menu;
6422 infow.wID = menu->id;
6423 infow.fType = MFT_STRING;
6424 infow.dwTypeData = wn;
6425 infow.cch = (UINT)wcslen(wn);
6426 infow.hSubMenu = menu->submenu_id;
6427 InsertMenuItemW((parent == NULL)
6428 ? s_menuBar : parent->submenu_id,
6429 (UINT)pos, TRUE, &infow);
6430 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 }
6432
Bram Moolenaar734a8672019-12-02 22:49:38 +01006433 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 if (parent == NULL)
6435 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006436# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 else if (IsWindow(parent->tearoff_handle))
6438 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006439# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440}
6441
6442 void
6443gui_mch_show_popupmenu(vimmenu_T *menu)
6444{
6445 POINT mp;
6446
K.Takata45f9cfb2022-01-21 11:11:00 +00006447 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6449}
6450
6451 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006452gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 vimmenu_T *menu = gui_find_menu(path_name);
6455
6456 if (menu != NULL)
6457 {
6458 POINT p;
6459
Bram Moolenaar734a8672019-12-02 22:49:38 +01006460 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006462 if (mouse_pos)
6463 {
6464 int mx, my;
6465
6466 gui_mch_getmouse(&mx, &my);
6467 p.x += mx;
6468 p.y += my;
6469 }
6470 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006472 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6474 }
6475 msg_scroll = FALSE;
6476 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6477 }
6478}
6479
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006480# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481/*
6482 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6483 * create it as a pseudo-"tearoff menu".
6484 */
6485 void
6486gui_make_tearoff(char_u *path_name)
6487{
6488 vimmenu_T *menu = gui_find_menu(path_name);
6489
Bram Moolenaar734a8672019-12-02 22:49:38 +01006490 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (menu != NULL)
6492 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6493}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496/*
6497 * Add a menu item to a menu
6498 */
6499 void
6500gui_mch_add_menu_item(
6501 vimmenu_T *menu,
6502 int idx)
6503{
6504 vimmenu_T *parent = menu->parent;
6505
6506 menu->id = s_menu_id++;
6507 menu->submenu_id = NULL;
6508
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006509# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6511 {
6512 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6513 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6514 }
6515 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006516# endif
6517# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (menu_is_toolbar(parent->name))
6519 {
6520 TBBUTTON newtb;
6521
Bram Moolenaara80faa82020-04-12 19:37:17 +02006522 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (menu_is_separator(menu->name))
6524 {
6525 newtb.iBitmap = 0;
6526 newtb.fsStyle = TBSTYLE_SEP;
6527 }
6528 else
6529 {
6530 newtb.iBitmap = get_toolbar_bitmap(menu);
6531 newtb.fsStyle = TBSTYLE_BUTTON;
6532 }
6533 newtb.idCommand = menu->id;
6534 newtb.fsState = TBSTATE_ENABLED;
6535 newtb.iString = 0;
6536 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6537 (LPARAM)&newtb);
6538 menu->submenu_id = (HMENU)-1;
6539 }
6540 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006543 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006545 wn = enc_to_utf16(menu->name, NULL);
6546 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006548 InsertMenuW(parent->submenu_id, (UINT)idx,
6549 (menu_is_separator(menu->name)
6550 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6551 (UINT)menu->id, wn);
6552 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006554# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (IsWindow(parent->tearoff_handle))
6556 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559}
6560
6561/*
6562 * Destroy the machine specific menu widget.
6563 */
6564 void
6565gui_mch_destroy_menu(vimmenu_T *menu)
6566{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006567# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 /*
6569 * is this a toolbar button?
6570 */
6571 if (menu->submenu_id == (HMENU)-1)
6572 {
6573 int iButton;
6574
6575 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6576 (WPARAM)menu->id, 0);
6577 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6578 }
6579 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 if (menu->parent != NULL
6583 && menu_is_popup(menu->parent->dname)
6584 && menu->parent->submenu_id != NULL)
6585 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6586 else
6587 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6588 if (menu->submenu_id != NULL)
6589 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006590# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (IsWindow(menu->tearoff_handle))
6592 DestroyWindow(menu->tearoff_handle);
6593 if (menu->parent != NULL
6594 && menu->parent->children != NULL
6595 && IsWindow(menu->parent->tearoff_handle))
6596 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006597 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 menu->modes = 0;
6599 rebuild_tearoff(menu->parent);
6600 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603}
6604
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006605# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 static void
6607rebuild_tearoff(vimmenu_T *menu)
6608{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006609 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 char_u tbuf[128];
6611 RECT trect;
6612 RECT rct;
6613 RECT roct;
6614 int x, y;
6615
6616 HWND thwnd = menu->tearoff_handle;
6617
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006618 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 if (GetWindowRect(thwnd, &trect)
6620 && GetWindowRect(s_hwnd, &rct)
6621 && GetClientRect(s_hwnd, &roct))
6622 {
6623 x = trect.left - rct.left;
6624 y = (trect.top - rct.bottom + roct.bottom);
6625 }
6626 else
6627 {
6628 x = y = 0xffffL;
6629 }
6630 DestroyWindow(thwnd);
6631 if (menu->children != NULL)
6632 {
6633 gui_mch_tearoff(tbuf, menu, x, y);
6634 if (IsWindow(menu->tearoff_handle))
6635 (void) SetWindowPos(menu->tearoff_handle,
6636 NULL,
6637 (int)trect.left,
6638 (int)trect.top,
6639 0, 0,
6640 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6641 }
6642}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006643# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645/*
6646 * Make a menu either grey or not grey.
6647 */
6648 void
6649gui_mch_menu_grey(
6650 vimmenu_T *menu,
6651 int grey)
6652{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006653# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 /*
6655 * is this a toolbar button?
6656 */
6657 if (menu->submenu_id == (HMENU)-1)
6658 {
6659 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006660 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 }
6662 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006663# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006664 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6665 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006667# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6669 {
6670 WORD menuID;
6671 HWND menuHandle;
6672
6673 /*
6674 * A tearoff button has changed state.
6675 */
6676 if (menu->children == NULL)
6677 menuID = (WORD)(menu->id);
6678 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006679 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6681 if (menuHandle)
6682 EnableWindow(menuHandle, !grey);
6683
6684 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686}
6687
Bram Moolenaar734a8672019-12-02 22:49:38 +01006688#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689
6690
Bram Moolenaar734a8672019-12-02 22:49:38 +01006691// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006694#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6697/*
6698 * stuff for dialogs
6699 */
6700
6701/*
6702 * The callback routine used by all the dialogs. Very simple. First,
6703 * acknowledges the INITDIALOG message so that Windows knows to do standard
6704 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6705 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6706 * number.
6707 */
6708 static LRESULT CALLBACK
6709dialog_callback(
6710 HWND hwnd,
6711 UINT message,
6712 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006713 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 if (message == WM_INITDIALOG)
6716 {
6717 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006718 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 (void)SetFocus(hwnd);
6720 if (dialog_default_button > IDCANCEL)
6721 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006722 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006723 // We don't have a default, set focus on another element of the
6724 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006725 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 return FALSE;
6727 }
6728
6729 if (message == WM_COMMAND)
6730 {
6731 int button = LOWORD(wParam);
6732
Bram Moolenaar734a8672019-12-02 22:49:38 +01006733 // Don't end the dialog if something was selected that was
6734 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 if (button >= DLG_NONBUTTON_CONTROL)
6736 return TRUE;
6737
Bram Moolenaar734a8672019-12-02 22:49:38 +01006738 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006740 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006741 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006742 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006743
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006744 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6745 p = utf16_to_enc(wp, NULL);
6746 vim_strncpy(s_textfield, p, IOSIZE);
6747 vim_free(p);
6748 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750
6751 /*
6752 * Need to check for IDOK because if the user just hits Return to
6753 * accept the default value, some reason this is what we get.
6754 */
6755 if (button == IDOK)
6756 {
6757 if (dialog_default_button > IDCANCEL)
6758 EndDialog(hwnd, dialog_default_button);
6759 }
6760 else
6761 EndDialog(hwnd, button - IDCANCEL);
6762 return TRUE;
6763 }
6764
6765 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6766 {
6767 EndDialog(hwnd, 0);
6768 return TRUE;
6769 }
6770 return FALSE;
6771}
6772
6773/*
6774 * Create a dialog dynamically from the parameter strings.
6775 * type = type of dialog (question, alert, etc.)
6776 * title = dialog title. may be NULL for default title.
6777 * message = text to display. Dialog sizes to accommodate it.
6778 * buttons = '\n' separated list of button captions, default first.
6779 * dfltbutton = number of default button.
6780 *
6781 * This routine returns 1 if the first button is pressed,
6782 * 2 for the second, etc.
6783 *
6784 * 0 indicates Esc was pressed.
6785 * -1 for unexpected error
6786 *
6787 * If stubbing out this fn, return 1.
6788 */
6789
Bram Moolenaar734a8672019-12-02 22:49:38 +01006790static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 "IDR_VIM",
6793 "IDR_VIM_ERROR",
6794 "IDR_VIM_ALERT",
6795 "IDR_VIM_INFO",
6796 "IDR_VIM_QUESTION"
6797};
6798
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 int
6800gui_mch_dialog(
6801 int type,
6802 char_u *title,
6803 char_u *message,
6804 char_u *buttons,
6805 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006806 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006807 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006810 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 int numButtons;
6812 int *buttonWidths, *buttonPositions;
6813 int buttonYpos;
6814 int nchar, i;
6815 DWORD lStyle;
6816 int dlgwidth = 0;
6817 int dlgheight;
6818 int editboxheight;
6819 int horizWidth = 0;
6820 int msgheight;
6821 char_u *pstart;
6822 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006823 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 char_u *tbuffer;
6825 RECT rect;
6826 HWND hwnd;
6827 HDC hdc;
6828 HFONT font, oldFont;
6829 TEXTMETRIC fontInfo;
6830 int fontHeight;
6831 int textWidth, minButtonWidth, messageWidth;
6832 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006833 int maxDialogHeight;
6834 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 int vertical;
6836 int dlgPaddingX;
6837 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006838# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006839 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006841# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006842 garray_T ga;
6843 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006844 int dlg_icon_width;
6845 int dlg_icon_height;
6846 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006848# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006849 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006850# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006851 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006852# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006853 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006854 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
Bram Moolenaar748bf032005-02-02 23:04:36 +00006857 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006858 {
6859 load_dpi_func();
6860 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006861 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006862 }
6863 else
6864 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
6866 if ((type < 0) || (type > VIM_LAST_TYPE))
6867 type = 0;
6868
Bram Moolenaar734a8672019-12-02 22:49:38 +01006869 // allocate some memory for dialog template
6870 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006871 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006872 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 if (p == NULL)
6875 return -1;
6876
6877 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006878 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6880 * const.
6881 */
6882 tbuffer = vim_strsave(buttons);
6883 if (tbuffer == NULL)
6884 return -1;
6885
Bram Moolenaar734a8672019-12-02 22:49:38 +01006886 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887
Bram Moolenaar734a8672019-12-02 22:49:38 +01006888 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 numButtons = 1;
6890 for (i = 0; tbuffer[i] != '\0'; i++)
6891 {
6892 if (tbuffer[i] == DLG_BUTTON_SEP)
6893 numButtons++;
6894 }
6895 if (dfltbutton >= numButtons)
6896 dfltbutton = -1;
6897
Bram Moolenaar734a8672019-12-02 22:49:38 +01006898 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006899 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 if (buttonWidths == NULL)
6901 return -1;
6902
Bram Moolenaar734a8672019-12-02 22:49:38 +01006903 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006904 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (buttonPositions == NULL)
6906 return -1;
6907
6908 /*
6909 * Calculate how big the dialog must be.
6910 */
6911 hwnd = GetDesktopWindow();
6912 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006913# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6915 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006916 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 use_lfSysmenu = TRUE;
6918 }
6919 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006920# endif
K.Takatad1c58992022-01-23 12:31:57 +00006921 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6922 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6923
6924 oldFont = SelectFont(hdc, font);
6925 dlgPaddingX = DLG_PADDING_X;
6926 dlgPaddingY = DLG_PADDING_Y;
6927
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 GetTextMetrics(hdc, &fontInfo);
6929 fontHeight = fontInfo.tmHeight;
6930
Bram Moolenaar734a8672019-12-02 22:49:38 +01006931 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006932 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
Bram Moolenaar734a8672019-12-02 22:49:38 +01006934 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006935 if (s_hwnd == NULL)
6936 {
6937 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
Bram Moolenaar734a8672019-12-02 22:49:38 +01006939 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006940 get_work_area(&workarea_rect);
6941 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006942 if (maxDialogWidth > adjust_by_system_dpi(600))
6943 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006944 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006945 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006946 }
6947 else
6948 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006949 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006950 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006951 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006952 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6953 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6954 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6955 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006956
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006957 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006958 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6959 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6960 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6961 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6962 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006963 }
6964
Bram Moolenaar734a8672019-12-02 22:49:38 +01006965 // Set dlgwidth to width of message.
6966 // Copy the message into "ga", changing NL to CR-NL and inserting line
6967 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 pstart = message;
6969 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006970 msgheight = 0;
6971 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 do
6973 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006974 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006975
Bram Moolenaar734a8672019-12-02 22:49:38 +01006976 // Need to figure out where to break the string. The system does it
6977 // at a word boundary, which would mean we can't compute the number of
6978 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006979 textWidth = 0;
6980 last_white = NULL;
6981 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006982 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006983 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006984 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006985 && textWidth > maxDialogWidth * 3 / 4)
6986 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006987 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006988 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006989 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006990 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006992 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006993 textWidth = 0;
6994
6995 if (last_white != NULL)
6996 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006998 if (pend > last_white)
6999 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007000 pend = last_white + 1;
7001 last_white = NULL;
7002 }
7003 ga_append(&ga, '\r');
7004 ga_append(&ga, '\n');
7005 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007006 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007007
7008 while (--l >= 0)
7009 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007010 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007011 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007013
7014 ga_append(&ga, '\r');
7015 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 pstart = pend + 1;
7017 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007018
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007019 if (ga.ga_data != NULL)
7020 message = ga.ga_data;
7021
Bram Moolenaar734a8672019-12-02 22:49:38 +01007022 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007023
K.Takatac81e9bf2022-01-16 14:15:49 +00007024 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7025 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026
K.Takatac81e9bf2022-01-16 14:15:49 +00007027 // Add width of icon to dlgwidth, and some space
7028 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7029 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7030
7031 if (msgheight < dlg_icon_height)
7032 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 /*
7035 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007036 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007038 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 if (!vertical)
7040 {
7041 // Place buttons horizontally if they fit.
7042 horizWidth = dlgPaddingX;
7043 pstart = tbuffer;
7044 i = 0;
7045 do
7046 {
7047 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7048 if (pend == NULL)
7049 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007050 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (textWidth < minButtonWidth)
7052 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007053 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 buttonWidths[i] = textWidth;
7055 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007056 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 pstart = pend + 1;
7058 } while (*pend != NUL);
7059
7060 if (horizWidth > maxDialogWidth)
7061 vertical = TRUE; // Too wide to fit on the screen.
7062 else if (horizWidth > dlgwidth)
7063 dlgwidth = horizWidth;
7064 }
7065
7066 if (vertical)
7067 {
7068 // Stack buttons vertically.
7069 pstart = tbuffer;
7070 do
7071 {
7072 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7073 if (pend == NULL)
7074 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007075 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 textWidth += dlgPaddingX; // Padding within button
7077 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (textWidth > dlgwidth)
7079 dlgwidth = textWidth;
7080 pstart = pend + 1;
7081 } while (*pend != NUL);
7082 }
7083
7084 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007085 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
Bram Moolenaar734a8672019-12-02 22:49:38 +01007087 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007088 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090 add_long(lStyle);
7091 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007092 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 add_word(0); // NumberOfItems(will change later)
7094 add_word(10); // x
7095 add_word(10); // y
7096 add_word(PixelToDialogX(dlgwidth)); // cx
7097
7098 // Dialog height.
7099 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007100 dlgheight = msgheight + 2 * dlgPaddingY
7101 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 else
7103 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7104
7105 // Dialog needs to be taller if contains an edit box.
7106 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7107 if (textfield != NULL)
7108 dlgheight += editboxheight;
7109
Bram Moolenaar734a8672019-12-02 22:49:38 +01007110 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007111 if (dlgheight > maxDialogHeight)
7112 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007113 msgheight = msgheight - (dlgheight - maxDialogHeight);
7114 dlgheight = maxDialogHeight;
7115 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007117 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007118 }
7119
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 add_word(PixelToDialogY(dlgheight));
7121
7122 add_word(0); // Menu
7123 add_word(0); // Class
7124
Bram Moolenaar734a8672019-12-02 22:49:38 +01007125 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007126 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7127 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 p += nchar;
7129
K.Takatad1c58992022-01-23 12:31:57 +00007130 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007131# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007132 if (use_lfSysmenu)
7133 {
7134 // point size
7135 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7136 GetDeviceCaps(hdc, LOGPIXELSY));
7137 wcscpy(p, lfSysmenu.lfFaceName);
7138 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 }
K.Takatad1c58992022-01-23 12:31:57 +00007140 else
7141# endif
7142 {
7143 *p++ = DLG_FONT_POINT_SIZE; // point size
7144 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7145 }
7146 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 buttonYpos = msgheight + 2 * dlgPaddingY;
7149
7150 if (textfield != NULL)
7151 buttonYpos += editboxheight;
7152
7153 pstart = tbuffer;
7154 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007155 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 for (i = 0; i < numButtons; i++)
7157 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007158 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 for ( pend = pstart;
7160 *pend && (*pend != DLG_BUTTON_SEP);
7161 pend++)
7162 ;
7163
7164 if (*pend)
7165 *pend = '\0';
7166
7167 /*
7168 * old NOTE:
7169 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7170 * the focus to the first tab-able button and in so doing makes that
7171 * the default!! Grrr. Workaround: Make the default button the only
7172 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7173 * he/she can use arrow keys.
7174 *
7175 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007176 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 * dialog. Also needed for when the textfield is the default control.
7178 * It appears to work now (perhaps not on Win95?).
7179 */
7180 if (vertical)
7181 {
7182 p = add_dialog_element(p,
7183 (i == dfltbutton
7184 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7185 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007186 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 + 2 * fontHeight * i),
7188 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7189 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007190 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192 else
7193 {
7194 p = add_dialog_element(p,
7195 (i == dfltbutton
7196 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7197 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 PixelToDialogX(buttonWidths[i]),
7200 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007201 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007203 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 *pnumitems += numButtons;
7206
Bram Moolenaar734a8672019-12-02 22:49:38 +01007207 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 p = add_dialog_element(p, SS_ICON,
7209 PixelToDialogX(dlgPaddingX),
7210 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007211 PixelToDialogX(dlg_icon_width),
7212 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7214 dlg_icons[type]);
7215
Bram Moolenaar734a8672019-12-02 22:49:38 +01007216 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007217 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007218 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007219 PixelToDialogY(dlgPaddingY),
7220 (WORD)(PixelToDialogX(messageWidth) + 1),
7221 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007222 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
Bram Moolenaar734a8672019-12-02 22:49:38 +01007224 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 if (textfield != NULL)
7226 {
7227 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7228 PixelToDialogX(2 * dlgPaddingX),
7229 PixelToDialogY(2 * dlgPaddingY + msgheight),
7230 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7231 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007232 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 *pnumitems += 1;
7234 }
7235
7236 *pnumitems += 2;
7237
7238 SelectFont(hdc, oldFont);
7239 DeleteObject(font);
7240 ReleaseDC(hwnd, hdc);
7241
Bram Moolenaar734a8672019-12-02 22:49:38 +01007242 // Let the dialog_callback() function know which button to make default
7243 // If we have an edit box, make that the default. We also need to tell
7244 // dialog_callback() if this dialog contains an edit box or not. We do
7245 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 if (textfield != NULL)
7247 {
7248 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7249 s_textfield = textfield;
7250 }
7251 else
7252 {
7253 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7254 s_textfield = NULL;
7255 }
7256
Bram Moolenaar734a8672019-12-02 22:49:38 +01007257 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007259 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 (LPDLGTEMPLATE)pdlgtemplate,
7261 s_hwnd,
7262 (DLGPROC)dialog_callback);
7263
7264 LocalFree(LocalHandle(pdlgtemplate));
7265 vim_free(tbuffer);
7266 vim_free(buttonWidths);
7267 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007268 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269
Bram Moolenaar734a8672019-12-02 22:49:38 +01007270 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 (void)SetFocus(s_hwnd);
7272
7273 return nchar;
7274}
7275
Bram Moolenaar734a8672019-12-02 22:49:38 +01007276#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278/*
7279 * Put a simple element (basic class) onto a dialog template in memory.
7280 * return a pointer to where the next item should be added.
7281 *
7282 * parameters:
7283 * lStyle = additional style flags
7284 * (be careful, NT3.51 & Win32s will ignore the new ones)
7285 * x,y = x & y positions IN DIALOG UNITS
7286 * w,h = width and height IN DIALOG UNITS
7287 * Id = ID used in messages
7288 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7289 * caption = usually text or resource name
7290 *
7291 * TODO: use the length information noted here to enable the dialog creation
7292 * routines to work out more exactly how much memory they need to alloc.
7293 */
7294 static PWORD
7295add_dialog_element(
7296 PWORD p,
7297 DWORD lStyle,
7298 WORD x,
7299 WORD y,
7300 WORD w,
7301 WORD h,
7302 WORD Id,
7303 WORD clss,
7304 const char *caption)
7305{
7306 int nchar;
7307
Bram Moolenaar734a8672019-12-02 22:49:38 +01007308 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7310 *p++ = LOWORD(lStyle);
7311 *p++ = HIWORD(lStyle);
7312 *p++ = 0; // LOWORD (lExtendedStyle)
7313 *p++ = 0; // HIWORD (lExtendedStyle)
7314 *p++ = x;
7315 *p++ = y;
7316 *p++ = w;
7317 *p++ = h;
7318 *p++ = Id; //9 or 10 words in all
7319
7320 *p++ = (WORD)0xffff;
7321 *p++ = clss; //2 more here
7322
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007323 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 p += nchar;
7325
7326 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7327
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007328 return p; // total = 15 + strlen(caption) words
7329 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330}
7331
7332
7333/*
7334 * Helper routine. Take an input pointer, return closest pointer that is
7335 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7336 */
7337 static LPWORD
7338lpwAlign(
7339 LPWORD lpIn)
7340{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007341 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007343 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 ul += 3;
7345 ul >>= 2;
7346 ul <<= 2;
7347 return (LPWORD)ul;
7348}
7349
7350/*
7351 * Helper routine. Takes second parameter as Ansi string, copies it to first
7352 * parameter as wide character (16-bits / char) string, and returns integer
7353 * number of wide characters (words) in string (including the trailing wide
7354 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007355 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7356 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 static int
7358nCopyAnsiToWideChar(
7359 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007360 LPSTR lpAnsiIn,
7361 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007364 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 int i;
7366 WCHAR *wn;
7367
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007368 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007370 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007371 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 if (wn != NULL)
7373 {
7374 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007375 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 vim_free(wn);
7377 }
7378 }
7379 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007380 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 nChar = MultiByteToWideChar(
7382 enc_codepage > 0 ? enc_codepage : CP_ACP,
7383 MB_PRECOMPOSED,
7384 lpAnsiIn, len,
7385 lpWCStr, len);
7386 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007387 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
7390 return nChar;
7391}
7392
7393
7394#ifdef FEAT_TEAROFF
7395/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007396 * Lookup menu handle from "menu_id".
7397 */
7398 static HMENU
7399tearoff_lookup_menuhandle(
7400 vimmenu_T *menu,
7401 WORD menu_id)
7402{
7403 for ( ; menu != NULL; menu = menu->next)
7404 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007405 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007406 continue;
7407 if (menu_is_separator(menu->dname))
7408 continue;
7409 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7410 return menu->submenu_id;
7411 }
7412 return NULL;
7413}
7414
7415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 * The callback function for all the modeless dialogs that make up the
7417 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7418 * thinking its menus have been clicked), and go away when closed.
7419 */
7420 static LRESULT CALLBACK
7421tearoff_callback(
7422 HWND hwnd,
7423 UINT message,
7424 WPARAM wParam,
7425 LPARAM lParam)
7426{
7427 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007428 {
7429 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432
Bram Moolenaar734a8672019-12-02 22:49:38 +01007433 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 HandleMouseHide(message, lParam);
7435
7436 if (message == WM_COMMAND)
7437 {
7438 if ((WORD)(LOWORD(wParam)) & 0x8000)
7439 {
7440 POINT mp;
7441 RECT rect;
7442
7443 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7444 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007445 vimmenu_T *menu;
7446
7447 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007449 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7451 (int)rect.right - 8,
7452 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007453 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 s_hwnd,
7455 NULL);
7456 /*
7457 * NOTE: The pop-up menu can eat the mouse up event.
7458 * We deal with this in normal.c.
7459 */
7460 }
7461 }
7462 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007463 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7465 /*
7466 * Give main window the focus back: this is so after
7467 * choosing a tearoff button you can start typing again
7468 * straight away.
7469 */
7470 (void)SetFocus(s_hwnd);
7471 return TRUE;
7472 }
7473 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7474 {
7475 DestroyWindow(hwnd);
7476 return TRUE;
7477 }
7478
Bram Moolenaar734a8672019-12-02 22:49:38 +01007479 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 if (message == WM_EXITSIZEMOVE)
7481 (void)SetActiveWindow(s_hwnd);
7482
7483 return FALSE;
7484}
7485#endif
7486
7487
7488/*
K.Takatad1c58992022-01-23 12:31:57 +00007489 * Computes the dialog base units based on the current dialog font.
7490 * We don't use the GetDialogBaseUnits() API, because we don't use the
7491 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 */
7493 static void
7494get_dialog_font_metrics(void)
7495{
7496 HDC hdc;
7497 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 SIZE size;
7499#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007500 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501#endif
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007504 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007505 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#endif
7508 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007509 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
K.Takatad1c58992022-01-23 12:31:57 +00007511 hdc = GetDC(s_hwnd);
7512 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007513 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007514 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515
K.Takataabe628e2022-01-23 16:25:17 +00007516 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007517 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518}
7519
7520#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7521/*
7522 * Create a pseudo-"tearoff menu" based on the child
7523 * items of a given menu pointer.
7524 */
7525 static void
7526gui_mch_tearoff(
7527 char_u *title,
7528 vimmenu_T *menu,
7529 int initX,
7530 int initY)
7531{
7532 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7533 int template_len;
7534 int nchar, textWidth, submenuWidth;
7535 DWORD lStyle;
7536 DWORD lExtendedStyle;
7537 WORD dlgwidth;
7538 WORD menuID;
7539 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007540 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 vimmenu_T *the_menu = menu;
7542 HWND hwnd;
7543 HDC hdc;
7544 HFONT font, oldFont;
7545 int col, spaceWidth, len;
7546 int columnWidths[2];
7547 char_u *label, *text;
7548 int acLen = 0;
7549 int nameLen;
7550 int padding0, padding1, padding2 = 0;
7551 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007552 int x;
7553 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007554# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007555 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
7559 /*
7560 * If this menu is already torn off, move it to the mouse position.
7561 */
7562 if (IsWindow(menu->tearoff_handle))
7563 {
7564 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007565 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
7567 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7568 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7569 }
7570 return;
7571 }
7572
7573 /*
7574 * Create a new tearoff.
7575 */
7576 if (*title == MNU_HIDDEN_CHAR)
7577 title++;
7578
Bram Moolenaar734a8672019-12-02 22:49:38 +01007579 // Allocate memory to store the dialog template. It's made bigger when
7580 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 template_len = DLG_ALLOC_SIZE;
7582 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7583 if (p == NULL)
7584 return;
7585
7586 hwnd = GetDesktopWindow();
7587 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007588# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7590 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007591 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 use_lfSysmenu = TRUE;
7593 }
7594 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007595# endif
K.Takatad1c58992022-01-23 12:31:57 +00007596 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7597 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7598
7599 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600
Bram Moolenaar734a8672019-12-02 22:49:38 +01007601 // Calculate width of a single space. Used for padding columns to the
7602 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007603 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604
Bram Moolenaar734a8672019-12-02 22:49:38 +01007605 // Figure out max width of the text column, the accelerator column and the
7606 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 submenuWidth = 0;
7608 for (col = 0; col < 2; col++)
7609 {
7610 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007611 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007613 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 text = (col == 0) ? pmenu->dname : pmenu->actext;
7615 if (text != NULL && *text != NUL)
7616 {
7617 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7618 if (textWidth > columnWidths[col])
7619 columnWidths[col] = textWidth;
7620 }
7621 if (pmenu->children != NULL)
7622 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7623 }
7624 }
7625 if (columnWidths[1] == 0)
7626 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007627 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 if (submenuWidth != 0)
7629 columnWidths[0] += submenuWidth;
7630 else
7631 columnWidths[0] += spaceWidth;
7632 }
7633 else
7634 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007635 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7637 columnWidths[1] += submenuWidth;
7638 }
7639
7640 /*
7641 * Now find the total width of our 'menu'.
7642 */
7643 textWidth = columnWidths[0] + columnWidths[1];
7644 if (submenuWidth != 0)
7645 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007646 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7648 textWidth += submenuWidth;
7649 }
7650 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7651 if (textWidth > dlgwidth)
7652 dlgwidth = textWidth;
7653 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7654
Bram Moolenaar734a8672019-12-02 22:49:38 +01007655 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007656 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
7658 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7659 *p++ = LOWORD(lStyle);
7660 *p++ = HIWORD(lStyle);
7661 *p++ = LOWORD(lExtendedStyle);
7662 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007663 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007665 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007667 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 else
7669 *p++ = PixelToDialogX(initX); // x
7670 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007671 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 else
7673 *p++ = PixelToDialogY(initY); // y
7674 *p++ = PixelToDialogX(dlgwidth); // cx
7675 ptrueheight = p;
7676 *p++ = 0; // dialog height: changed later anyway
7677 *p++ = 0; // Menu
7678 *p++ = 0; // Class
7679
Bram Moolenaar734a8672019-12-02 22:49:38 +01007680 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007682 ? (LPSTR)title
7683 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 p += nchar;
7685
K.Takatad1c58992022-01-23 12:31:57 +00007686 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007687# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007688 if (use_lfSysmenu)
7689 {
7690 // point size
7691 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7692 GetDeviceCaps(hdc, LOGPIXELSY));
7693 wcscpy(p, lfSysmenu.lfFaceName);
7694 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
K.Takatad1c58992022-01-23 12:31:57 +00007696 else
7697# endif
7698 {
7699 *p++ = DLG_FONT_POINT_SIZE; // point size
7700 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7701 }
7702 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703
7704 /*
7705 * Loop over all the items in the menu.
7706 * But skip over the tearbar.
7707 */
7708 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7709 menu = menu->children->next;
7710 else
7711 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007712 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 for ( ; menu != NULL; menu = menu->next)
7714 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007715 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 continue;
7717 if (menu_is_separator(menu->dname))
7718 {
7719 sepPadding += 3;
7720 continue;
7721 }
7722
Bram Moolenaar734a8672019-12-02 22:49:38 +01007723 // Check if there still is plenty of room in the template. Make it
7724 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7726 {
7727 WORD *newp;
7728
7729 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7730 if (newp != NULL)
7731 {
7732 template_len += 4096;
7733 mch_memmove(newp, pdlgtemplate,
7734 (char *)p - (char *)pdlgtemplate);
7735 p = newp + (p - pdlgtemplate);
7736 pnumitems = newp + (pnumitems - pdlgtemplate);
7737 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7738 LocalFree(LocalHandle(pdlgtemplate));
7739 pdlgtemplate = newp;
7740 }
7741 }
7742
Bram Moolenaar734a8672019-12-02 22:49:38 +01007743 // Figure out minimal length of this menu label. Use "name" for the
7744 // actual text, "dname" for estimating the displayed size. "name"
7745 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 len = nameLen = (int)STRLEN(menu->name);
7747 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7748 (int)STRLEN(menu->dname))) / spaceWidth;
7749 len += padding0;
7750
7751 if (menu->actext != NULL)
7752 {
7753 acLen = (int)STRLEN(menu->actext);
7754 len += acLen;
7755 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7756 }
7757 else
7758 textWidth = 0;
7759 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7760 len += padding1;
7761
7762 if (menu->children == NULL)
7763 {
7764 padding2 = submenuWidth / spaceWidth;
7765 len += padding2;
7766 menuID = (WORD)(menu->id);
7767 }
7768 else
7769 {
7770 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007771 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
7773
Bram Moolenaar734a8672019-12-02 22:49:38 +01007774 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007775 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (label == NULL)
7777 break;
7778
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007779 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007780 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007782 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 while (padding0-- > 0)
7784 *text++ = ' ';
7785 if (menu->actext != NULL)
7786 {
7787 STRNCPY(text, menu->actext, acLen);
7788 text += acLen;
7789 }
7790 while (padding1-- > 0)
7791 *text++ = ' ';
7792 if (menu->children != NULL)
7793 {
7794 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7795 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7796 }
7797 else
7798 {
7799 while (padding2-- > 0)
7800 *text++ = ' ';
7801 }
7802 *text = NUL;
7803
7804 /*
7805 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7806 * W95/NT4 it makes the tear-off look more like a menu.
7807 */
7808 p = add_dialog_element(p,
7809 BS_PUSHBUTTON|BS_LEFT,
7810 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7811 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7812 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7813 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007814 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 vim_free(label);
7816 (*pnumitems)++;
7817 }
7818
7819 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7820
7821
Bram Moolenaar734a8672019-12-02 22:49:38 +01007822 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007823 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007824 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 (LPDLGTEMPLATE)pdlgtemplate,
7826 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007827 (DLGPROC)tearoff_callback,
7828 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 LocalFree(LocalHandle(pdlgtemplate));
7831 SelectFont(hdc, oldFont);
7832 DeleteObject(font);
7833 ReleaseDC(hwnd, hdc);
7834
7835 /*
7836 * Reassert ourselves as the active window. This is so that after creating
7837 * a tearoff, the user doesn't have to click with the mouse just to start
7838 * typing again!
7839 */
7840 (void)SetActiveWindow(s_hwnd);
7841
Bram Moolenaar734a8672019-12-02 22:49:38 +01007842 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 force_menu_update = TRUE;
7844}
7845#endif
7846
7847#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007848# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850/*
7851 * Create the toolbar, initially unpopulated.
7852 * (just like the menu, there are no defaults, it's all
7853 * set up through menu.vim)
7854 */
7855 static void
7856initialise_toolbar(void)
7857{
7858 InitCommonControls();
7859 s_toolbarhwnd = CreateToolbarEx(
7860 s_hwnd,
7861 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7862 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007863 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007864 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 IDR_TOOLBAR1, // id of initial bitmap
7866 NULL,
7867 0, // initial number of buttons
7868 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7869 TOOLBAR_BUTTON_HEIGHT,
7870 TOOLBAR_BUTTON_WIDTH,
7871 TOOLBAR_BUTTON_HEIGHT,
7872 sizeof(TBBUTTON)
7873 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007874
7875 // Remove transparency from the toolbar to prevent the main window
7876 // background colour showing through
7877 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7878 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7879
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007880 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881
7882 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007883
7884 update_toolbar_size();
7885}
7886
7887 static void
7888update_toolbar_size(void)
7889{
7890 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007891 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007892
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007893 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007894 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7895 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7896 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7897 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7898
7899 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7900 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7901 //TRACE("button size: %d, %d", w, h);
7902 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7903 gui.toolbar_height = h + 6;
7904
7905 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7906 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7907
7908 // TODO:
7909 // Currently, this function only updates the size of toolbar buttons.
7910 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911}
7912
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007913 static LRESULT CALLBACK
7914toolbar_wndproc(
7915 HWND hwnd,
7916 UINT uMsg,
7917 WPARAM wParam,
7918 LPARAM lParam)
7919{
7920 HandleMouseHide(uMsg, lParam);
7921 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7922}
7923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 static int
7925get_toolbar_bitmap(vimmenu_T *menu)
7926{
7927 int i = -1;
7928
7929 /*
7930 * Check user bitmaps first, unless builtin is specified.
7931 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007932 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {
7934 char_u fname[MAXPATHL];
7935 HANDLE hbitmap = NULL;
7936
7937 if (menu->iconfile != NULL)
7938 {
7939 gui_find_iconfile(menu->iconfile, fname, "bmp");
7940 hbitmap = LoadImage(
7941 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007942 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 IMAGE_BITMAP,
7944 TOOLBAR_BUTTON_WIDTH,
7945 TOOLBAR_BUTTON_HEIGHT,
7946 LR_LOADFROMFILE |
7947 LR_LOADMAP3DCOLORS
7948 );
7949 }
7950
7951 /*
7952 * If the LoadImage call failed, or the "icon=" file
7953 * didn't exist or wasn't specified, try the menu name
7954 */
7955 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007956 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007957# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007958 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007959# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007960 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 hbitmap = LoadImage(
7962 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007963 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 IMAGE_BITMAP,
7965 TOOLBAR_BUTTON_WIDTH,
7966 TOOLBAR_BUTTON_HEIGHT,
7967 LR_LOADFROMFILE |
7968 LR_LOADMAP3DCOLORS
7969 );
7970
7971 if (hbitmap != NULL)
7972 {
7973 TBADDBITMAP tbAddBitmap;
7974
7975 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007976 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977
7978 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7979 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007980 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 }
7982 }
7983 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7984 i = menu->iconidx;
7985
7986 return i;
7987}
7988#endif
7989
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007990#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7991 static void
7992initialise_tabline(void)
7993{
7994 InitCommonControls();
7995
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007996 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007997 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007998 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007999 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008000 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008001
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008002 gui.tabline_height = TABLINE_HEIGHT;
8003
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008004 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008005}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008006
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008007/*
8008 * Get tabpage_T from POINT.
8009 */
8010 static tabpage_T *
8011GetTabFromPoint(
8012 HWND hWnd,
8013 POINT pt)
8014{
8015 tabpage_T *ptp = NULL;
8016
8017 if (gui_mch_showing_tabline())
8018 {
8019 TCHITTESTINFO htinfo;
8020 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008021 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008022 if (s_tabhwnd == hWnd)
8023 {
8024 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8025 if (idx != -1)
8026 ptp = find_tabpage(idx + 1);
8027 }
8028 }
8029 return ptp;
8030}
8031
8032static POINT s_pt = {0, 0};
8033static HCURSOR s_hCursor = NULL;
8034
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008035 static LRESULT CALLBACK
8036tabline_wndproc(
8037 HWND hwnd,
8038 UINT uMsg,
8039 WPARAM wParam,
8040 LPARAM lParam)
8041{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008042 POINT pt;
8043 tabpage_T *tp;
8044 RECT rect;
8045 int nCenter;
8046 int idx0;
8047 int idx1;
8048
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008049 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008050
8051 switch (uMsg)
8052 {
8053 case WM_LBUTTONDOWN:
8054 {
8055 s_pt.x = GET_X_LPARAM(lParam);
8056 s_pt.y = GET_Y_LPARAM(lParam);
8057 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008058 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008059 break;
8060 }
8061 case WM_MOUSEMOVE:
8062 if (GetCapture() == hwnd
8063 && ((wParam & MK_LBUTTON)) != 0)
8064 {
8065 pt.x = GET_X_LPARAM(lParam);
8066 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008067 if (abs(pt.x - s_pt.x) >
8068 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008069 {
8070 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8071
8072 tp = GetTabFromPoint(hwnd, pt);
8073 if (tp != NULL)
8074 {
8075 idx0 = tabpage_index(curtab) - 1;
8076 idx1 = tabpage_index(tp) - 1;
8077
8078 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8079 nCenter = rect.left + (rect.right - rect.left) / 2;
8080
Bram Moolenaar734a8672019-12-02 22:49:38 +01008081 // Check if the mouse cursor goes over the center of
8082 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008083 if ((idx0 < idx1) && (nCenter < pt.x))
8084 {
8085 tabpage_move(idx1 + 1);
8086 update_screen(0);
8087 }
8088 else if ((idx1 < idx0) && (pt.x < nCenter))
8089 {
8090 tabpage_move(idx1);
8091 update_screen(0);
8092 }
8093 }
8094 }
8095 }
8096 break;
8097 case WM_LBUTTONUP:
8098 {
8099 if (GetCapture() == hwnd)
8100 {
8101 SetCursor(s_hCursor);
8102 ReleaseCapture();
8103 }
8104 break;
8105 }
regomne3bdef102022-09-26 20:48:32 +01008106 case WM_MBUTTONUP:
8107 {
8108 TCHITTESTINFO htinfo;
8109
8110 htinfo.pt.x = GET_X_LPARAM(lParam);
8111 htinfo.pt.y = GET_Y_LPARAM(lParam);
8112 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8113 if (idx0 != -1)
8114 {
8115 idx0 += 1;
8116 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8117 }
8118 break;
8119 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008120 default:
8121 break;
8122 }
8123
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008124 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8125}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008126#endif
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8129/*
8130 * Make the GUI window come to the foreground.
8131 */
8132 void
8133gui_mch_set_foreground(void)
8134{
8135 if (IsIconic(s_hwnd))
8136 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8137 SetForegroundWindow(s_hwnd);
8138}
8139#endif
8140
8141#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8142 static void
8143dyn_imm_load(void)
8144{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008145 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 if (hLibImm == NULL)
8147 return;
8148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008150 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008152 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008154 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008156 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008158 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008160 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008161 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008162 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008163 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008164 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008166 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008168 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008169 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008170 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171
K.Takatab0b2b732022-01-19 12:59:21 +00008172 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 || pImmGetContext == NULL
8174 || pImmAssociateContext == NULL
8175 || pImmReleaseContext == NULL
8176 || pImmGetOpenStatus == NULL
8177 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008178 || pImmGetCompositionFontW == NULL
8179 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008181 || pImmGetConversionStatus == NULL
8182 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 FreeLibrary(hLibImm);
8185 hLibImm = NULL;
8186 pImmGetContext = NULL;
8187 return;
8188 }
8189
8190 return;
8191}
8192
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193#endif
8194
8195#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8196
8197# ifdef FEAT_XPM_W32
8198# define IMAGE_XPM 100
8199# endif
8200
8201typedef struct _signicon_t
8202{
8203 HANDLE hImage;
8204 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008205# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008206 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208} signicon_t;
8209
8210 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008211gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213 signicon_t *sign;
8214 int x, y, w, h;
8215
8216 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8217 return;
8218
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008219# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008220 if (IS_ENABLE_DIRECTX())
8221 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008222# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008223
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 x = TEXT_X(col);
8225 y = TEXT_Y(row);
8226 w = gui.char_width * 2;
8227 h = gui.char_height;
8228 switch (sign->uType)
8229 {
8230 case IMAGE_BITMAP:
8231 {
8232 HDC hdcMem;
8233 HBITMAP hbmpOld;
8234
8235 hdcMem = CreateCompatibleDC(s_hdc);
8236 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8237 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8238 SelectObject(hdcMem, hbmpOld);
8239 DeleteDC(hdcMem);
8240 }
8241 break;
8242 case IMAGE_ICON:
8243 case IMAGE_CURSOR:
8244 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8245 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008246# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 case IMAGE_XPM:
8248 {
8249 HDC hdcMem;
8250 HBITMAP hbmpOld;
8251
8252 hdcMem = CreateCompatibleDC(s_hdc);
8253 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008254 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8256
8257 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008258 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8260 SelectObject(hdcMem, hbmpOld);
8261 DeleteDC(hdcMem);
8262 }
8263 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 }
8266}
8267
8268 static void
8269close_signicon_image(signicon_t *sign)
8270{
8271 if (sign)
8272 switch (sign->uType)
8273 {
8274 case IMAGE_BITMAP:
8275 DeleteObject((HGDIOBJ)sign->hImage);
8276 break;
8277 case IMAGE_CURSOR:
8278 DestroyCursor((HCURSOR)sign->hImage);
8279 break;
8280 case IMAGE_ICON:
8281 DestroyIcon((HICON)sign->hImage);
8282 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008283# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 case IMAGE_XPM:
8285 DeleteObject((HBITMAP)sign->hImage);
8286 DeleteObject((HBITMAP)sign->hShape);
8287 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
8290}
8291
8292 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008293gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 signicon_t sign, *psign;
8296 char_u *ext;
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008299 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (ext > signfile)
8301 {
8302 int do_load = 1;
8303
8304 if (!STRICMP(ext, ".bmp"))
8305 sign.uType = IMAGE_BITMAP;
8306 else if (!STRICMP(ext, ".ico"))
8307 sign.uType = IMAGE_ICON;
8308 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8309 sign.uType = IMAGE_CURSOR;
8310 else
8311 do_load = 0;
8312
8313 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008314 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 gui.char_width * 2, gui.char_height,
8316 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008317# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (!STRICMP(ext, ".xpm"))
8319 {
8320 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008321 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8322 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326
8327 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008328 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 *psign = sign;
8330
8331 if (!psign)
8332 {
8333 if (sign.hImage)
8334 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008335 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
8337 return (void *)psign;
8338
8339}
8340
8341 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008342gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 if (sign)
8345 {
8346 close_signicon_image((signicon_t *)sign);
8347 vim_free(sign);
8348 }
8349}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008352#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
Bram Moolenaar734a8672019-12-02 22:49:38 +01008354/*
8355 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008356 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008358 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8360 * to get current mouse position).
8361 *
8362 * Trying to use as more Windows services as possible, and as less
8363 * IE version as possible :)).
8364 *
8365 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8366 * BalloonEval struct.
8367 * 2) Enable/Disable simply create/kill BalloonEval Timer
8368 * 3) When there was enough inactivity, timer procedure posts
8369 * async request to debugger
8370 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8371 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008372 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 */
8374
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008375 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008376make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008377{
K.Takata76687d22022-01-25 10:31:37 +00008378 TOOLINFOW *pti;
8379 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008380
K.Takata76687d22022-01-25 10:31:37 +00008381 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008382 if (pti == NULL)
8383 return;
8384
8385 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8386 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8387 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008388 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008389
8390 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8391 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8392
K.Takata76687d22022-01-25 10:31:37 +00008393 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008394 pti->uFlags = TTF_SUBCLASS;
8395 pti->hwnd = beval->target;
8396 pti->hinst = 0; // Don't use string resources
8397 pti->uId = ID_BEVAL_TOOLTIP;
8398
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008399 pti->lpszText = LPSTR_TEXTCALLBACKW;
8400 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8401 pti->lParam = (LPARAM)beval->tofree;
8402 // switch multiline tooltips on
8403 if (GetClientRect(s_textArea, &rect))
8404 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8405 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008406
8407 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8408 pti->rect.left = pt.x - 3;
8409 pti->rect.top = pt.y - 3;
8410 pti->rect.right = pt.x + 3;
8411 pti->rect.bottom = pt.y + 3;
8412
8413 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8414 // Make tooltip appear sooner.
8415 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8416 // I've performed some tests and it seems the longest possible life time
8417 // of tooltip is 30 seconds.
8418 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8419 /*
8420 * HACK: force tooltip to appear, because it'll not appear until
8421 * first mouse move. D*mn M$
8422 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8423 */
8424 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8425 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8426 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008427}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008430delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008432 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433}
8434
8435 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008436beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008437 HWND hwnd UNUSED,
8438 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008439 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008440 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 POINT pt;
8443 RECT rect;
8444
8445 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8446 return;
8447
8448 GetCursorPos(&pt);
8449 if (WindowFromPoint(pt) != s_textArea)
8450 return;
8451
8452 ScreenToClient(s_textArea, &pt);
8453 GetClientRect(s_textArea, &rect);
8454 if (!PtInRect(&rect, pt))
8455 return;
8456
K.Takataa8ec4912022-02-03 14:32:33 +00008457 if (last_user_activity > 0
8458 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 && (cur_beval->showState != ShS_PENDING
8460 || abs(cur_beval->x - pt.x) > 3
8461 || abs(cur_beval->y - pt.y) > 3))
8462 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008463 // Pointer resting in one place long enough, it's time to show
8464 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 cur_beval->showState = ShS_PENDING;
8466 cur_beval->x = pt.x;
8467 cur_beval->y = pt.y;
8468
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 if (cur_beval->msgCB != NULL)
8470 (*cur_beval->msgCB)(cur_beval, 0);
8471 }
8472}
8473
8474 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008475gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476{
K.Takataa8ec4912022-02-03 14:32:33 +00008477 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478}
8479
8480 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008481gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (beval == NULL)
8484 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008485 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8486 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487}
8488
8489 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008490gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
8492 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008493
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008494 vim_free(beval->msg);
8495 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8496 if (beval->msg == NULL)
8497 {
8498 delete_tooltip(beval);
8499 beval->showState = ShS_NEUTRAL;
8500 return;
8501 }
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (beval->showState == ShS_SHOWING)
8504 return;
8505 GetCursorPos(&pt);
8506 ScreenToClient(s_textArea, &pt);
8507
8508 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008510 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 gui_mch_disable_beval_area(cur_beval);
8512 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008513 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515}
8516
8517 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008518gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008519 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008520 char_u *mesg,
8521 void (*mesgCB)(BalloonEval *, int),
8522 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008524 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 BalloonEval *beval;
8526
8527 if (mesg != NULL && mesgCB != NULL)
8528 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008529 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 return NULL;
8531 }
8532
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008533 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 if (beval != NULL)
8535 {
8536 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
8538 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 beval->msg = mesg;
8540 beval->msgCB = mesgCB;
8541 beval->clientData = clientData;
8542
8543 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 cur_beval = beval;
8545
8546 if (p_beval)
8547 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 }
8549 return beval;
8550}
8551
8552 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008553Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008555 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 return;
8557
8558 if (cur_beval != NULL)
8559 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008560 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008562 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008563 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008564 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 delete_tooltip(cur_beval);
8566 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
8568 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008569 break;
8570 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008571 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008572 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008573 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008574 info->lpszText = (LPSTR)info->lParam;
8575 info->uFlags |= TTF_DI_SETITEM;
8576 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008577 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008578 case TTN_GETDISPINFOW:
8579 {
8580 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008581 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008582 info->lpszText = (LPWSTR)info->lParam;
8583 info->uFlags |= TTF_DI_SETITEM;
8584 }
8585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587 }
8588}
8589
8590 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008591track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592{
8593 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8594 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008595 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597
8598 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008599gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008601# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008602 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008603# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008604 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 vim_free(beval);
8606}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008607#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
8609#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8610/*
8611 * We have multiple signs to draw at the same location. Draw the
8612 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8613 */
8614 void
8615netbeans_draw_multisign_indicator(int row)
8616{
8617 int i;
8618 int y;
8619 int x;
8620
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008621 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008622 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 x = 0;
8625 y = TEXT_Y(row);
8626
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008627# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008628 if (IS_ENABLE_DIRECTX())
8629 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008630# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008631
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 for (i = 0; i < gui.char_height - 3; i++)
8633 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8634
8635 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8636 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8637 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8638 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8639 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8640 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8641 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8642}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008643#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008644
8645#if defined(FEAT_EVAL) || defined(PROTO)
8646 int
8647test_gui_w32_sendevent(dict_T *args)
8648{
8649 char_u *event;
8650 INPUT inputs[1];
8651
Bram Moolenaard61efa52022-07-23 09:52:04 +01008652 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008653 if (event == NULL)
8654 return FALSE;
8655
8656 ZeroMemory(inputs, sizeof(inputs));
8657
8658 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8659 {
8660 WORD vkCode;
8661
Bram Moolenaard61efa52022-07-23 09:52:04 +01008662 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008663 if (vkCode <= 0 || vkCode >= 0xFF)
8664 {
8665 semsg(_(e_invalid_argument_nr), (long)vkCode);
8666 return FALSE;
8667 }
8668
8669 inputs[0].type = INPUT_KEYBOARD;
8670 inputs[0].ki.wVk = vkCode;
8671 if (STRICMP(event, "keyup") == 0)
8672 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
K.Takatafef38d82022-09-07 19:03:42 +01008673 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008674 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8675 }
8676 else
8677 semsg(_(e_invalid_argument_str), event);
8678
8679 vim_free(event);
8680
8681 return TRUE;
8682}
8683#endif