blob: 9399c930ec51e6adfca9e2fabe32ea4af1e8a18e [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 {
2155 if ( dead_key == DEAD_KEY_SET_DEFAULT
2156 && (GetKeyState(VK_CONTROL) & 0x8000)
2157 && ( (vk == 221 && scan_code == 26) // AZERTY CTRL+dead_circumflex
2158 || (vk == 220 && scan_code == 41) // QWERTZ CTRL+dead_circumflex
2159 )
2160 )
2161 {
2162 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002163 // still hold as ESC
Anton Sharonov3f026672022-07-26 21:26:18 +01002164 PostMessageW(msg.hwnd, WM_CHAR, '[', msg.lParam);
2165 // ask _OnChar() to not touch this state, wait for next key
2166 // press and maintain knowledge that we are "poisoned" with
2167 // "dead state"
2168 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2169 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002170 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002171 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002172
2173 // Post the message as TranslateMessage would do.
2174 if (msg.message == WM_KEYDOWN)
2175 {
2176 for (i = 0; i < len; i++)
2177 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002178 }
2179 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002180 {
2181 for (i = 0; i < len; i++)
2182 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2183 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002184 }
2185 }
2186#ifdef FEAT_MBYTE_IME
2187 else if (msg.message == WM_IME_NOTIFY)
2188 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2189 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002190 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002191 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002192#endif
2193
2194#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002195 // Check for <F10>: Default effect is to select the menu. When <F10> is
2196 // mapped we need to stop it here to avoid strange effects (e.g., for the
2197 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002198 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2199 NULL, NULL) == NULL)
2200#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002201 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002202}
2203
2204/*
2205 * Catch up with any queued events. This may put keyboard input into the
2206 * input buffer, call resize call-backs, trigger timers etc. If there is
2207 * nothing in the event queue (& no timers pending), then we return
2208 * immediately.
2209 */
2210 void
2211gui_mch_update(void)
2212{
2213 MSG msg;
2214
2215 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002216 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002217 && !vim_is_input_buf_full())
2218 process_message();
2219}
2220
Bram Moolenaar4231da42016-06-02 14:30:04 +02002221 static void
2222remove_any_timer(void)
2223{
2224 MSG msg;
2225
2226 if (s_wait_timer != 0 && !s_timed_out)
2227 {
2228 KillTimer(NULL, s_wait_timer);
2229
Bram Moolenaar734a8672019-12-02 22:49:38 +01002230 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002231 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002232 ;
2233 s_wait_timer = 0;
2234 }
2235}
2236
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002237/*
2238 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2239 * from the keyboard.
2240 * wtime == -1 Wait forever.
2241 * wtime == 0 This should never happen.
2242 * wtime > 0 Wait wtime milliseconds for a character.
2243 * Returns OK if a character was found to be available within the given time,
2244 * or FAIL otherwise.
2245 */
2246 int
2247gui_mch_wait_for_chars(int wtime)
2248{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002249 int focus;
2250
2251 s_timed_out = FALSE;
2252
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002253 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002254 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002255 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002256 if (s_busy_processing)
2257 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002258
2259 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002260 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2261 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002262 }
2263
2264 allow_scrollbar = TRUE;
2265
2266 focus = gui.in_focus;
2267 while (!s_timed_out)
2268 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002269 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270 if (gui.in_focus != focus)
2271 {
2272 if (gui.in_focus)
2273 gui_mch_start_blink();
2274 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002275 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002276 focus = gui.in_focus;
2277 }
2278
2279 if (s_need_activate)
2280 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002281 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002282 s_need_activate = FALSE;
2283 }
2284
Bram Moolenaar4231da42016-06-02 14:30:04 +02002285#ifdef FEAT_TIMERS
2286 did_add_timer = FALSE;
2287#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002288#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002289 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002290 for (;;)
2291 {
2292 MSG msg;
2293
2294 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002295# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002296 if (did_add_timer)
2297 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002298# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002299 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002300 {
2301 process_message();
2302 break;
2303 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002304 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002305 // TODO: The 10 msec is a compromise between laggy response
2306 // and consuming more CPU time. Better would be to handle
2307 // channel messages when they arrive.
2308 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002309 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002310 break;
2311 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002312#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002313 // Don't use gui_mch_update() because then we will spin-lock until a
2314 // char arrives, instead we use GetMessage() to hang until an
2315 // event arrives. No need to check for input_buf_full because we are
2316 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002317 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002318#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002319
2320 if (input_available())
2321 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002322 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002323 allow_scrollbar = FALSE;
2324
Bram Moolenaar89c00032019-09-04 13:53:21 +02002325 // Clear pending mouse button, the release event may have been
2326 // taken by the dialog window. But don't do this when getting
2327 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002328 if (!s_getting_focus)
2329 s_button_pending = -1;
2330
2331 return OK;
2332 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002333
2334#ifdef FEAT_TIMERS
2335 if (did_add_timer)
2336 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002337 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002338 remove_any_timer();
2339 break;
2340 }
2341#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002342 }
2343 allow_scrollbar = FALSE;
2344 return FAIL;
2345}
2346
2347/*
2348 * Clear a rectangular region of the screen from text pos (row1, col1) to
2349 * (row2, col2) inclusive.
2350 */
2351 void
2352gui_mch_clear_block(
2353 int row1,
2354 int col1,
2355 int row2,
2356 int col2)
2357{
2358 RECT rc;
2359
2360 /*
2361 * Clear one extra pixel at the far right, for when bold characters have
2362 * spilled over to the window border.
2363 * Note: FillRect() excludes right and bottom of rectangle.
2364 */
2365 rc.left = FILL_X(col1);
2366 rc.top = FILL_Y(row1);
2367 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2368 rc.bottom = FILL_Y(row2 + 1);
2369 clear_rect(&rc);
2370}
2371
2372/*
2373 * Clear the whole text window.
2374 */
2375 void
2376gui_mch_clear_all(void)
2377{
2378 RECT rc;
2379
2380 rc.left = 0;
2381 rc.top = 0;
2382 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2383 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2384 clear_rect(&rc);
2385}
2386/*
2387 * Menu stuff.
2388 */
2389
2390 void
2391gui_mch_enable_menu(int flag)
2392{
2393#ifdef FEAT_MENU
2394 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2395#endif
2396}
2397
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002398 void
2399gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002400 int x UNUSED,
2401 int y UNUSED,
2402 int w UNUSED,
2403 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002404{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002405 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002406}
2407
2408#if defined(FEAT_MENU) || defined(PROTO)
2409/*
2410 * Make menu item hidden or not hidden
2411 */
2412 void
2413gui_mch_menu_hidden(
2414 vimmenu_T *menu,
2415 int hidden)
2416{
2417 /*
2418 * This doesn't do what we want. Hmm, just grey the menu items for now.
2419 */
2420 /*
2421 if (hidden)
2422 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2423 else
2424 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2425 */
2426 gui_mch_menu_grey(menu, hidden);
2427}
2428
2429/*
2430 * This is called after setting all the menus to grey/hidden or not.
2431 */
2432 void
2433gui_mch_draw_menubar(void)
2434{
2435 DrawMenuBar(s_hwnd);
2436}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002437#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002438
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002439/*
2440 * Return the RGB value of a pixel as a long.
2441 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002442 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002443gui_mch_get_rgb(guicolor_T pixel)
2444{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002445 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2446 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002447}
2448
2449#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002450/*
2451 * Convert pixels in X to dialog units
2452 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002453 static WORD
2454PixelToDialogX(int numPixels)
2455{
2456 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2457}
2458
Bram Moolenaar734a8672019-12-02 22:49:38 +01002459/*
2460 * Convert pixels in Y to dialog units
2461 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002462 static WORD
2463PixelToDialogY(int numPixels)
2464{
2465 return (WORD)((numPixels * 8) / s_dlgfntheight);
2466}
2467
Bram Moolenaar734a8672019-12-02 22:49:38 +01002468/*
2469 * Return the width in pixels of the given text in the given DC.
2470 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002471 static int
2472GetTextWidth(HDC hdc, char_u *str, int len)
2473{
2474 SIZE size;
2475
2476 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2477 return size.cx;
2478}
2479
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002480/*
2481 * Return the width in pixels of the given text in the given DC, taking care
2482 * of 'encoding' to active codepage conversion.
2483 */
2484 static int
2485GetTextWidthEnc(HDC hdc, char_u *str, int len)
2486{
2487 SIZE size;
2488 WCHAR *wstr;
2489 int n;
2490 int wlen = len;
2491
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002492 wstr = enc_to_utf16(str, &wlen);
2493 if (wstr == NULL)
2494 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002495
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002496 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2497 vim_free(wstr);
2498 if (n)
2499 return size.cx;
2500 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002502
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002503static void get_work_area(RECT *spi_rect);
2504
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002505/*
2506 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002507 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2508 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002509 */
2510 static BOOL
2511CenterWindow(
2512 HWND hwndChild,
2513 HWND hwndParent)
2514{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002515 HMONITOR mon;
2516 MONITORINFO moninfo;
2517 RECT rChild, rParent, rScreen;
2518 int wChild, hChild, wParent, hParent;
2519 int xNew, yNew;
2520 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002521
2522 GetWindowRect(hwndChild, &rChild);
2523 wChild = rChild.right - rChild.left;
2524 hChild = rChild.bottom - rChild.top;
2525
Bram Moolenaar734a8672019-12-02 22:49:38 +01002526 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002527 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002528 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002529 else
2530 GetWindowRect(hwndParent, &rParent);
2531 wParent = rParent.right - rParent.left;
2532 hParent = rParent.bottom - rParent.top;
2533
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002534 moninfo.cbSize = sizeof(MONITORINFO);
2535 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2536 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002537 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002538 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002539 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002540 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002541 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002542 hdc = GetDC(hwndChild);
2543 rScreen.left = 0;
2544 rScreen.top = 0;
2545 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2546 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2547 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548 }
2549
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002550 xNew = rParent.left + ((wParent - wChild) / 2);
2551 if (xNew < rScreen.left)
2552 xNew = rScreen.left;
2553 else if ((xNew + wChild) > rScreen.right)
2554 xNew = rScreen.right - wChild;
2555
2556 yNew = rParent.top + ((hParent - hChild) / 2);
2557 if (yNew < rScreen.top)
2558 yNew = rScreen.top;
2559 else if ((yNew + hChild) > rScreen.bottom)
2560 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002561
2562 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2563 SWP_NOSIZE | SWP_NOZORDER);
2564}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002565#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002566
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002567#if defined(FEAT_TOOLBAR) || defined(PROTO)
2568 void
2569gui_mch_show_toolbar(int showit)
2570{
2571 if (s_toolbarhwnd == NULL)
2572 return;
2573
2574 if (showit)
2575 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002576 // Enable unicode support
2577 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2578 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002579 ShowWindow(s_toolbarhwnd, SW_SHOW);
2580 }
2581 else
2582 ShowWindow(s_toolbarhwnd, SW_HIDE);
2583}
2584
Bram Moolenaar734a8672019-12-02 22:49:38 +01002585// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002586# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587
2588#endif
2589
2590#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2591 static void
2592add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2593{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002594 WCHAR *wn;
2595 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002596
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002597 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002598 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002599 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002600
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002601 infow.cbSize = sizeof(infow);
2602 infow.fMask = MIIM_TYPE | MIIM_ID;
2603 infow.wID = item_id;
2604 infow.fType = MFT_STRING;
2605 infow.dwTypeData = wn;
2606 infow.cch = (UINT)wcslen(wn);
2607 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2608 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002609}
2610
2611 static void
2612show_tabline_popup_menu(void)
2613{
2614 HMENU tab_pmenu;
2615 long rval;
2616 POINT pt;
2617
Bram Moolenaar734a8672019-12-02 22:49:38 +01002618 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002619 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002620 return;
2621
2622 tab_pmenu = CreatePopupMenu();
2623 if (tab_pmenu == NULL)
2624 return;
2625
2626 if (first_tabpage->tp_next != NULL)
2627 add_tabline_popup_menu_entry(tab_pmenu,
2628 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2629 add_tabline_popup_menu_entry(tab_pmenu,
2630 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2631 add_tabline_popup_menu_entry(tab_pmenu,
2632 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2633
2634 GetCursorPos(&pt);
2635 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2636 NULL);
2637
2638 DestroyMenu(tab_pmenu);
2639
Bram Moolenaar734a8672019-12-02 22:49:38 +01002640 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002641 if (rval > 0)
2642 {
2643 TCHITTESTINFO htinfo;
2644 int idx;
2645
2646 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2647 return;
2648
2649 htinfo.pt.x = pt.x;
2650 htinfo.pt.y = pt.y;
2651 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2652 if (idx == -1)
2653 idx = 0;
2654 else
2655 idx += 1;
2656
2657 send_tabline_menu_event(idx, (int)rval);
2658 }
2659}
2660
2661/*
2662 * Show or hide the tabline.
2663 */
2664 void
2665gui_mch_show_tabline(int showit)
2666{
2667 if (s_tabhwnd == NULL)
2668 return;
2669
2670 if (!showit != !showing_tabline)
2671 {
2672 if (showit)
2673 ShowWindow(s_tabhwnd, SW_SHOW);
2674 else
2675 ShowWindow(s_tabhwnd, SW_HIDE);
2676 showing_tabline = showit;
2677 }
2678}
2679
2680/*
2681 * Return TRUE when tabline is displayed.
2682 */
2683 int
2684gui_mch_showing_tabline(void)
2685{
2686 return s_tabhwnd != NULL && showing_tabline;
2687}
2688
2689/*
2690 * Update the labels of the tabline.
2691 */
2692 void
2693gui_mch_update_tabline(void)
2694{
2695 tabpage_T *tp;
2696 TCITEM tie;
2697 int nr = 0;
2698 int curtabidx = 0;
2699 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002700 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002701
2702 if (s_tabhwnd == NULL)
2703 return;
2704
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002705 // Enable unicode support
2706 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002707
2708 tie.mask = TCIF_TEXT;
2709 tie.iImage = -1;
2710
Bram Moolenaar734a8672019-12-02 22:49:38 +01002711 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002712 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2713
Bram Moolenaar734a8672019-12-02 22:49:38 +01002714 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002715 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2716 {
2717 if (tp == curtab)
2718 curtabidx = nr;
2719
2720 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2721 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002722 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002723 tie.pszText = "-Empty-";
2724 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2725 tabadded = 1;
2726 }
2727
2728 get_tabline_label(tp, FALSE);
2729 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002730
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002731 wstr = enc_to_utf16(NameBuff, NULL);
2732 if (wstr != NULL)
2733 {
2734 TCITEMW tiw;
2735
2736 tiw.mask = TCIF_TEXT;
2737 tiw.iImage = -1;
2738 tiw.pszText = wstr;
2739 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2740 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002741 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002742 }
2743
Bram Moolenaar734a8672019-12-02 22:49:38 +01002744 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002745 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2746 TabCtrl_DeleteItem(s_tabhwnd, nr);
2747
2748 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2749 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2750
Bram Moolenaar734a8672019-12-02 22:49:38 +01002751 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002752 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2753 RedrawWindow(s_tabhwnd, NULL, NULL,
2754 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2755
2756 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2757 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2758}
2759
2760/*
2761 * Set the current tab to "nr". First tab is 1.
2762 */
2763 void
2764gui_mch_set_curtab(int nr)
2765{
2766 if (s_tabhwnd == NULL)
2767 return;
2768
2769 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2770 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2771}
2772
2773#endif
2774
2775/*
2776 * ":simalt" command.
2777 */
2778 void
2779ex_simalt(exarg_T *eap)
2780{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002781 char_u *keys = eap->arg;
2782 int fill_typebuf = FALSE;
2783 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002784
2785 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2786 while (*keys)
2787 {
2788 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002789 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002790 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2791 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002792 fill_typebuf = TRUE;
2793 }
2794 if (fill_typebuf)
2795 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002796 // Put a NOP in the typeahead buffer so that the message will get
2797 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002798 key_name[0] = K_SPECIAL;
2799 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002800 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002801 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002802#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002803 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002804#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002805 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002806 }
2807}
2808
2809/*
2810 * Create the find & replace dialogs.
2811 * You can't have both at once: ":find" when replace is showing, destroys
2812 * the replace dialog first, and the other way around.
2813 */
2814#ifdef MSWIN_FIND_REPLACE
2815 static void
2816initialise_findrep(char_u *initial_string)
2817{
2818 int wword = FALSE;
2819 int mcase = !p_ic;
2820 char_u *entry_text;
2821
Bram Moolenaar734a8672019-12-02 22:49:38 +01002822 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002823 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2824
2825 s_findrep_struct.hwndOwner = s_hwnd;
2826 s_findrep_struct.Flags = FR_DOWN;
2827 if (mcase)
2828 s_findrep_struct.Flags |= FR_MATCHCASE;
2829 if (wword)
2830 s_findrep_struct.Flags |= FR_WHOLEWORD;
2831 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002832 {
2833 WCHAR *p = enc_to_utf16(entry_text, NULL);
2834 if (p != NULL)
2835 {
2836 int len = s_findrep_struct.wFindWhatLen - 1;
2837
2838 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2839 s_findrep_struct.lpstrFindWhat[len] = NUL;
2840 vim_free(p);
2841 }
2842 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002843 vim_free(entry_text);
2844}
2845#endif
2846
2847 static void
2848set_window_title(HWND hwnd, char *title)
2849{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002850 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002851 {
2852 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002853
Bram Moolenaar734a8672019-12-02 22:49:38 +01002854 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002855 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2856 if (wbuf != NULL)
2857 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002858 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002859 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002860 }
2861 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002862 else
2863 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002864}
2865
2866 void
2867gui_mch_find_dialog(exarg_T *eap)
2868{
2869#ifdef MSWIN_FIND_REPLACE
2870 if (s_findrep_msg != 0)
2871 {
2872 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2873 DestroyWindow(s_findrep_hwnd);
2874
2875 if (!IsWindow(s_findrep_hwnd))
2876 {
2877 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002878 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002879 }
2880
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002881 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002882 (void)SetFocus(s_findrep_hwnd);
2883
2884 s_findrep_is_find = TRUE;
2885 }
2886#endif
2887}
2888
2889
2890 void
2891gui_mch_replace_dialog(exarg_T *eap)
2892{
2893#ifdef MSWIN_FIND_REPLACE
2894 if (s_findrep_msg != 0)
2895 {
2896 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2897 DestroyWindow(s_findrep_hwnd);
2898
2899 if (!IsWindow(s_findrep_hwnd))
2900 {
2901 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002902 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 }
2904
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002905 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002906 (void)SetFocus(s_findrep_hwnd);
2907
2908 s_findrep_is_find = FALSE;
2909 }
2910#endif
2911}
2912
2913
2914/*
2915 * Set visibility of the pointer.
2916 */
2917 void
2918gui_mch_mousehide(int hide)
2919{
2920 if (hide != gui.pointer_hidden)
2921 {
2922 ShowCursor(!hide);
2923 gui.pointer_hidden = hide;
2924 }
2925}
2926
2927#ifdef FEAT_MENU
2928 static void
2929gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2930{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002931 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002932 gui_mch_mousehide(FALSE);
2933
2934 (void)TrackPopupMenu(
2935 (HMENU)menu->submenu_id,
2936 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2937 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002938 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002939 s_hwnd,
2940 NULL);
2941 /*
2942 * NOTE: The pop-up menu can eat the mouse up event.
2943 * We deal with this in normal.c.
2944 */
2945}
2946#endif
2947
2948/*
2949 * Got a message when the system will go down.
2950 */
2951 static void
2952_OnEndSession(void)
2953{
2954 getout_preserve_modified(1);
2955}
2956
2957/*
2958 * Get this message when the user clicks on the cross in the top right corner
2959 * of a Windows95 window.
2960 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002961 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002962_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002963{
2964 gui_shell_closed();
2965}
2966
2967/*
2968 * Get a message when the window is being destroyed.
2969 */
2970 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002971_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002972{
2973 if (!destroying)
2974 _OnClose(hwnd);
2975}
2976
2977 static void
2978_OnPaint(
2979 HWND hwnd)
2980{
2981 if (!IsMinimized(hwnd))
2982 {
2983 PAINTSTRUCT ps;
2984
Bram Moolenaar734a8672019-12-02 22:49:38 +01002985 out_flush(); // make sure all output has been processed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002986 (void)BeginPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002987
Bram Moolenaar734a8672019-12-02 22:49:38 +01002988 // prevent multi-byte characters from misprinting on an invalid
2989 // rectangle
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002990 if (has_mbyte)
2991 {
2992 RECT rect;
2993
2994 GetClientRect(hwnd, &rect);
2995 ps.rcPaint.left = rect.left;
2996 ps.rcPaint.right = rect.right;
2997 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002998
2999 if (!IsRectEmpty(&ps.rcPaint))
3000 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003001 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3002 ps.rcPaint.right - ps.rcPaint.left + 1,
3003 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3004 }
3005
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003006 EndPaint(hwnd, &ps);
3007 }
3008}
3009
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003010 static void
3011_OnSize(
3012 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003013 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003014 int cx,
3015 int cy)
3016{
K.Takatac81e9bf2022-01-16 14:15:49 +00003017 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003018 {
3019 gui_resize_shell(cx, cy);
3020
Bram Moolenaar734a8672019-12-02 22:49:38 +01003021 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003022 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003023 }
3024}
3025
3026 static void
3027_OnSetFocus(
3028 HWND hwnd,
3029 HWND hwndOldFocus)
3030{
3031 gui_focus_change(TRUE);
3032 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003033 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003034}
3035
3036 static void
3037_OnKillFocus(
3038 HWND hwnd,
3039 HWND hwndNewFocus)
3040{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003041 if (destroying)
3042 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043 gui_focus_change(FALSE);
3044 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003045 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003046}
3047
3048/*
3049 * Get a message when the user switches back to vim
3050 */
3051 static LRESULT
3052_OnActivateApp(
3053 HWND hwnd,
3054 BOOL fActivate,
3055 DWORD dwThreadId)
3056{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003057 // we call gui_focus_change() in _OnSetFocus()
3058 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003059 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003060}
3061
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003062 void
3063gui_mch_destroy_scrollbar(scrollbar_T *sb)
3064{
3065 DestroyWindow(sb->id);
3066}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003067
3068/*
3069 * Get current mouse coordinates in text window.
3070 */
3071 void
3072gui_mch_getmouse(int *x, int *y)
3073{
3074 RECT rct;
3075 POINT mp;
3076
3077 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003078 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003079 *x = (int)(mp.x - rct.left);
3080 *y = (int)(mp.y - rct.top);
3081}
3082
3083/*
3084 * Move mouse pointer to character at (x, y).
3085 */
3086 void
3087gui_mch_setmouse(int x, int y)
3088{
3089 RECT rct;
3090
3091 (void)GetWindowRect(s_textArea, &rct);
3092 (void)SetCursorPos(x + gui.border_offset + rct.left,
3093 y + gui.border_offset + rct.top);
3094}
3095
3096 static void
3097gui_mswin_get_valid_dimensions(
3098 int w,
3099 int h,
3100 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003101 int *valid_h,
3102 int *cols,
3103 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003104{
3105 int base_width, base_height;
3106
3107 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003108 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3109 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003110 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003111 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3112 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3113 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3114 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003115 *cols = (w - base_width) / gui.char_width;
3116 *rows = (h - base_height) / gui.char_height;
3117 *valid_w = base_width + *cols * gui.char_width;
3118 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003119}
3120
3121 void
3122gui_mch_flash(int msec)
3123{
3124 RECT rc;
3125
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003126#if defined(FEAT_DIRECTX)
3127 if (IS_ENABLE_DIRECTX())
3128 DWriteContext_Flush(s_dwc);
3129#endif
3130
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003131 /*
3132 * Note: InvertRect() excludes right and bottom of rectangle.
3133 */
3134 rc.left = 0;
3135 rc.top = 0;
3136 rc.right = gui.num_cols * gui.char_width;
3137 rc.bottom = gui.num_rows * gui.char_height;
3138 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003139 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140
Bram Moolenaar734a8672019-12-02 22:49:38 +01003141 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003142
3143 InvertRect(s_hdc, &rc);
3144}
3145
3146/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003147 * Check if the specified point is on-screen. (multi-monitor aware)
3148 */
3149 static BOOL
3150is_point_onscreen(int x, int y)
3151{
3152 POINT pt = {x, y};
3153
3154 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3155}
3156
3157/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003158 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003159 *
3160 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3161 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3162 * only works when the whole window is on-screen.
3163 */
3164 static BOOL
3165is_window_onscreen(HWND hwnd)
3166{
3167 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003168 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003169
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003170 GetClientRect(hwnd, &rc);
3171 p1.x = rc.left;
3172 p1.y = rc.top;
3173 p2.x = rc.right - 1;
3174 p2.y = rc.bottom - 1;
3175 ClientToScreen(hwnd, &p1);
3176 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003177
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003178 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003179 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003180 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003181 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003182 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003183 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003184 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003185 return FALSE;
3186 return TRUE;
3187}
3188
3189/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003190 * Return flags used for scrolling.
3191 * The SW_INVALIDATE is required when part of the window is covered or
3192 * off-screen. Refer to MS KB Q75236.
3193 */
3194 static int
3195get_scroll_flags(void)
3196{
3197 HWND hwnd;
3198 RECT rcVim, rcOther, rcDest;
3199
Bram Moolenaar185577e2020-10-29 20:08:21 +01003200 // Check if the window is (partly) off-screen.
3201 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003202 return SW_INVALIDATE;
3203
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003204 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003205 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003206 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3207 if (IsWindowVisible(hwnd))
3208 {
3209 GetWindowRect(hwnd, &rcOther);
3210 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3211 return SW_INVALIDATE;
3212 }
3213 return 0;
3214}
3215
3216/*
3217 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3218 * may not be scrolled out properly.
3219 * For gVim, when _OnScroll() is repeated, the character at the
3220 * previous cursor position may be left drawn after scroll.
3221 * The problem can be avoided by calling GetPixel() to get a pixel in
3222 * the region before ScrollWindowEx().
3223 */
3224 static void
3225intel_gpu_workaround(void)
3226{
3227 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3228}
3229
3230/*
3231 * Delete the given number of lines from the given row, scrolling up any
3232 * text further down within the scroll region.
3233 */
3234 void
3235gui_mch_delete_lines(
3236 int row,
3237 int num_lines)
3238{
3239 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003240
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003241 rc.left = FILL_X(gui.scroll_region_left);
3242 rc.right = FILL_X(gui.scroll_region_right + 1);
3243 rc.top = FILL_Y(row);
3244 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3245
Bram Moolenaar92467d32017-12-05 13:22:16 +01003246#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003247 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003248 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003249 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003250 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003251 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003252#endif
3253 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003254#if defined(FEAT_DIRECTX)
3255 if (IS_ENABLE_DIRECTX())
3256 DWriteContext_Flush(s_dwc);
3257#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003258 intel_gpu_workaround();
3259 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003260 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003261 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003262 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003263
Bram Moolenaar734a8672019-12-02 22:49:38 +01003264 // This seems to be required to avoid the cursor disappearing when
3265 // scrolling such that the cursor ends up in the top-left character on
3266 // the screen... But why? (Webb)
3267 // It's probably fixed by disabling drawing the cursor while scrolling.
3268 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003269
3270 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3271 gui.scroll_region_left,
3272 gui.scroll_region_bot, gui.scroll_region_right);
3273}
3274
3275/*
3276 * Insert the given number of lines before the given row, scrolling down any
3277 * following text within the scroll region.
3278 */
3279 void
3280gui_mch_insert_lines(
3281 int row,
3282 int num_lines)
3283{
3284 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003285
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003286 rc.left = FILL_X(gui.scroll_region_left);
3287 rc.right = FILL_X(gui.scroll_region_right + 1);
3288 rc.top = FILL_Y(row);
3289 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003290
3291#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003292 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003293 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003294 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003295 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003296 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003297#endif
3298 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003299#if defined(FEAT_DIRECTX)
3300 if (IS_ENABLE_DIRECTX())
3301 DWriteContext_Flush(s_dwc);
3302#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003303 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003304 // The SW_INVALIDATE is required when part of the window is covered or
3305 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003306 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003308 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003309 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003310
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003311 gui_clear_block(row, gui.scroll_region_left,
3312 row + num_lines - 1, gui.scroll_region_right);
3313}
3314
3315
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003316 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003317gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003318{
3319#if defined(FEAT_DIRECTX)
3320 DWriteContext_Close(s_dwc);
3321 DWrite_Final();
3322 s_dwc = NULL;
3323#endif
3324
3325 ReleaseDC(s_textArea, s_hdc);
3326 DeleteObject(s_brush);
3327
3328#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003329 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003330 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3331#endif
3332
Bram Moolenaar734a8672019-12-02 22:49:38 +01003333 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003334 if (s_hwnd != NULL)
3335 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003336 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 DestroyWindow(s_hwnd);
3338 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339}
3340
3341 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003342logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003343{
3344 char *p;
3345 char *res;
3346 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003347 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003348 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003349 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003350
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003351 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3352 if (font_name == NULL)
3353 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003354 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003355 quality_name = quality_id2name((int)lf.lfQuality);
3356
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003357 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003358 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003359 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003360 if (res != NULL)
3361 {
3362 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003363 // make a normal font string out of the lf thing:
3364 points = pixels_to_points(
3365 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3366 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3367 sprintf((char *)p, "%s:h%d", font_name, points);
3368 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003369 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003370 while (*p)
3371 {
3372 if (*p == ' ')
3373 *p = '_';
3374 ++p;
3375 }
3376 if (lf.lfItalic)
3377 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003378 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003379 STRCAT(p, ":b");
3380 if (lf.lfUnderline)
3381 STRCAT(p, ":u");
3382 if (lf.lfStrikeOut)
3383 STRCAT(p, ":s");
3384 if (charset_name != NULL)
3385 {
3386 STRCAT(p, ":c");
3387 STRCAT(p, charset_name);
3388 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003389 if (quality_name != NULL)
3390 {
3391 STRCAT(p, ":q");
3392 STRCAT(p, quality_name);
3393 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003394 }
3395
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003396 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003397 return (char_u *)res;
3398}
3399
3400
3401#ifdef FEAT_MBYTE_IME
3402/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003403 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003404 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003405 */
3406 static void
3407update_im_font(void)
3408{
K.Takatac81e9bf2022-01-16 14:15:49 +00003409 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003410
3411 if (p_guifontwide != NULL && *p_guifontwide != NUL
3412 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003413 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003414 norm_logfont = lf_wide;
3415 else
3416 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003417
3418 lf = norm_logfont;
3419 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3420 // Work around when PerMonitorV2 is not enabled in the process level.
3421 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3422 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003423}
3424#endif
3425
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426/*
3427 * Handler of gui.wide_font (p_guifontwide) changed notification.
3428 */
3429 void
3430gui_mch_wide_font_changed(void)
3431{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003432 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003433
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003434#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003436#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003437
3438 gui_mch_free_font(gui.wide_ital_font);
3439 gui.wide_ital_font = NOFONT;
3440 gui_mch_free_font(gui.wide_bold_font);
3441 gui.wide_bold_font = NOFONT;
3442 gui_mch_free_font(gui.wide_boldital_font);
3443 gui.wide_boldital_font = NOFONT;
3444
3445 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003446 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003447 {
3448 if (!lf.lfItalic)
3449 {
3450 lf.lfItalic = TRUE;
3451 gui.wide_ital_font = get_font_handle(&lf);
3452 lf.lfItalic = FALSE;
3453 }
3454 if (lf.lfWeight < FW_BOLD)
3455 {
3456 lf.lfWeight = FW_BOLD;
3457 gui.wide_bold_font = get_font_handle(&lf);
3458 if (!lf.lfItalic)
3459 {
3460 lf.lfItalic = TRUE;
3461 gui.wide_boldital_font = get_font_handle(&lf);
3462 }
3463 }
3464 }
3465}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003466
3467/*
3468 * Initialise vim to use the font with the given name.
3469 * Return FAIL if the font could not be loaded, OK otherwise.
3470 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003471 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003472gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003473{
K.Takatac81e9bf2022-01-16 14:15:49 +00003474 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003475 GuiFont font = NOFONT;
3476 char_u *p;
3477
Bram Moolenaar734a8672019-12-02 22:49:38 +01003478 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003479 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003480 {
3481 lfOrig = lf;
3482 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003483 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003484 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003485 if (font == NOFONT)
3486 return FAIL;
3487
3488 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003489 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003490#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003491 norm_logfont = lf;
3492 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003493 if (!s_in_dpichanged)
3494 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003495#endif
3496 gui_mch_free_font(gui.norm_font);
3497 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003498 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003499 GetFontSize(font);
3500
K.Takatac81e9bf2022-01-16 14:15:49 +00003501 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003502 if (p != NULL)
3503 {
3504 hl_set_font_name(p);
3505
Bram Moolenaar734a8672019-12-02 22:49:38 +01003506 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003507 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3508 {
3509 vim_free(p_guifont);
3510 p_guifont = p;
3511 }
3512 else
3513 vim_free(p);
3514 }
3515
3516 gui_mch_free_font(gui.ital_font);
3517 gui.ital_font = NOFONT;
3518 gui_mch_free_font(gui.bold_font);
3519 gui.bold_font = NOFONT;
3520 gui_mch_free_font(gui.boldital_font);
3521 gui.boldital_font = NOFONT;
3522
3523 if (!lf.lfItalic)
3524 {
3525 lf.lfItalic = TRUE;
3526 gui.ital_font = get_font_handle(&lf);
3527 lf.lfItalic = FALSE;
3528 }
3529 if (lf.lfWeight < FW_BOLD)
3530 {
3531 lf.lfWeight = FW_BOLD;
3532 gui.bold_font = get_font_handle(&lf);
3533 if (!lf.lfItalic)
3534 {
3535 lf.lfItalic = TRUE;
3536 gui.boldital_font = get_font_handle(&lf);
3537 }
3538 }
3539
3540 return OK;
3541}
3542
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003543/*
3544 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003545 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003546 */
3547 int
3548gui_mch_maximized(void)
3549{
3550 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003551 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003552
3553 wp.length = sizeof(WINDOWPLACEMENT);
3554 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003555 {
3556 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003557 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003558 && wp.flags == WPF_RESTORETOMAXIMIZED))
3559 return TRUE;
3560 if (wp.showCmd == SW_SHOWMINIMIZED)
3561 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003562
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003563 // Assume the window is snapped when the sizes from two APIs differ.
3564 GetWindowRect(s_hwnd, &rc);
3565 if ((rc.right - rc.left !=
3566 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3567 || (rc.bottom - rc.top !=
3568 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3569 return TRUE;
3570 }
3571 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003572}
3573
3574/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003575 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3576 * is set. Compute the new Rows and Columns. This is like resizing the
3577 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578 */
3579 void
3580gui_mch_newfont(void)
3581{
3582 RECT rect;
3583
3584 GetWindowRect(s_hwnd, &rect);
3585 if (win_socket_id == 0)
3586 {
3587 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003588 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3589 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003590 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003591 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3592 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3593 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3594 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003595 }
3596 else
3597 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003598 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003600 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003601 }
3602}
3603
3604/*
3605 * Set the window title
3606 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003607 void
3608gui_mch_settitle(
3609 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003610 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611{
3612 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3613}
3614
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003615#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003616// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3617// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003618static LPCSTR mshape_idcs[] =
3619{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003620 IDC_ARROW, // arrow
3621 MAKEINTRESOURCE(0), // blank
3622 IDC_IBEAM, // beam
3623 IDC_SIZENS, // updown
3624 IDC_SIZENS, // udsizing
3625 IDC_SIZEWE, // leftright
3626 IDC_SIZEWE, // lrsizing
3627 IDC_WAIT, // busy
3628 IDC_NO, // no
3629 IDC_ARROW, // crosshair
3630 IDC_ARROW, // hand1
3631 IDC_ARROW, // hand2
3632 IDC_ARROW, // pencil
3633 IDC_ARROW, // question
3634 IDC_ARROW, // right-arrow
3635 IDC_UPARROW, // up-arrow
3636 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003637};
3638
3639 void
3640mch_set_mouse_shape(int shape)
3641{
3642 LPCSTR idc;
3643
3644 if (shape == MSHAPE_HIDE)
3645 ShowCursor(FALSE);
3646 else
3647 {
3648 if (shape >= MSHAPE_NUMBERED)
3649 idc = IDC_ARROW;
3650 else
3651 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003652 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003653 if (!p_mh)
3654 {
3655 POINT mp;
3656
Bram Moolenaar734a8672019-12-02 22:49:38 +01003657 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003658 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003659 (void)SetCursorPos(mp.x, mp.y);
3660 ShowCursor(TRUE);
3661 }
3662 }
3663}
3664#endif
3665
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003666#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003667/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003668 * Wide version of convert_filter().
3669 */
3670 static WCHAR *
3671convert_filterW(char_u *s)
3672{
3673 char_u *tmp;
3674 int len;
3675 WCHAR *res;
3676
3677 tmp = convert_filter(s);
3678 if (tmp == NULL)
3679 return NULL;
3680 len = (int)STRLEN(s) + 3;
3681 res = enc_to_utf16(tmp, &len);
3682 vim_free(tmp);
3683 return res;
3684}
3685
3686/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003687 * Pop open a file browser and return the file selected, in allocated memory,
3688 * or NULL if Cancel is hit.
3689 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3690 * title - Title message for the file browser dialog.
3691 * dflt - Default name of file.
3692 * ext - Default extension to be added to files without extensions.
3693 * initdir - directory in which to open the browser (NULL = current dir)
3694 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003695 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003696 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003697gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003698 int saving,
3699 char_u *title,
3700 char_u *dflt,
3701 char_u *ext,
3702 char_u *initdir,
3703 char_u *filter)
3704{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003705 // We always use the wide function. This means enc_to_utf16() must work,
3706 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003707 OPENFILENAMEW fileStruct;
3708 WCHAR fileBuf[MAXPATHL];
3709 WCHAR *wp;
3710 int i;
3711 WCHAR *titlep = NULL;
3712 WCHAR *extp = NULL;
3713 WCHAR *initdirp = NULL;
3714 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003715 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003716 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003717
3718 if (dflt == NULL)
3719 fileBuf[0] = NUL;
3720 else
3721 {
3722 wp = enc_to_utf16(dflt, NULL);
3723 if (wp == NULL)
3724 fileBuf[0] = NUL;
3725 else
3726 {
3727 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3728 fileBuf[i] = wp[i];
3729 fileBuf[i] = NUL;
3730 vim_free(wp);
3731 }
3732 }
3733
Bram Moolenaar734a8672019-12-02 22:49:38 +01003734 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003735 filterp = convert_filterW(filter);
3736
Bram Moolenaara80faa82020-04-12 19:37:17 +02003737 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003738# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003739 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003740 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003741# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003742 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003743# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003744
3745 if (title != NULL)
3746 titlep = enc_to_utf16(title, NULL);
3747 fileStruct.lpstrTitle = titlep;
3748
3749 if (ext != NULL)
3750 extp = enc_to_utf16(ext, NULL);
3751 fileStruct.lpstrDefExt = extp;
3752
3753 fileStruct.lpstrFile = fileBuf;
3754 fileStruct.nMaxFile = MAXPATHL;
3755 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003756 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3757 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003758 if (initdir != NULL && *initdir != NUL)
3759 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003760 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003761 initdirp = enc_to_utf16(initdir, NULL);
3762 if (initdirp != NULL)
3763 {
3764 for (wp = initdirp; *wp != NUL; ++wp)
3765 if (*wp == '/')
3766 *wp = '\\';
3767 }
3768 fileStruct.lpstrInitialDir = initdirp;
3769 }
3770
3771 /*
3772 * TODO: Allow selection of multiple files. Needs another arg to this
3773 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3774 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3775 * files that don't exist yet, so I haven't put it in. What about
3776 * OFN_PATHMUSTEXIST?
3777 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3778 */
3779 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003780# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003781 if (curbuf->b_p_bin)
3782 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003783# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003784 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003785 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003786 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003787 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003788
3789 vim_free(filterp);
3790 vim_free(initdirp);
3791 vim_free(titlep);
3792 vim_free(extp);
3793
K.Takata14b8d6a2022-01-20 15:05:22 +00003794 if (!ret)
3795 return NULL;
3796
3797 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003798 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003799 if (p == NULL)
3800 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003801
Bram Moolenaar734a8672019-12-02 22:49:38 +01003802 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003803 SetFocus(s_hwnd);
3804
Bram Moolenaar734a8672019-12-02 22:49:38 +01003805 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003806 q = vim_strsave(shorten_fname1(p));
3807 vim_free(p);
3808 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003810
3811
3812/*
3813 * Convert the string s to the proper format for a filter string by replacing
3814 * the \t and \n delimiters with \0.
3815 * Returns the converted string in allocated memory.
3816 *
3817 * Keep in sync with convert_filterW() above!
3818 */
3819 static char_u *
3820convert_filter(char_u *s)
3821{
3822 char_u *res;
3823 unsigned s_len = (unsigned)STRLEN(s);
3824 unsigned i;
3825
3826 res = alloc(s_len + 3);
3827 if (res != NULL)
3828 {
3829 for (i = 0; i < s_len; ++i)
3830 if (s[i] == '\t' || s[i] == '\n')
3831 res[i] = '\0';
3832 else
3833 res[i] = s[i];
3834 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003835 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003836 res[s_len + 1] = NUL;
3837 res[s_len + 2] = NUL;
3838 }
3839 return res;
3840}
3841
3842/*
3843 * Select a directory.
3844 */
3845 char_u *
3846gui_mch_browsedir(char_u *title, char_u *initdir)
3847{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003848 // We fake this: Use a filter that doesn't select anything and a default
3849 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003850 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3851 initdir, (char_u *)_("Directory\t*.nothing\n"));
3852}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003853#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003854
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003855 static void
3856_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003857 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003858 HDROP hDrop)
3859{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003860#define BUFPATHLEN _MAX_PATH
3861#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003862 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003863 char szFile[BUFPATHLEN];
3864 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3865 UINT i;
3866 char_u **fnames;
3867 POINT pt;
3868 int_u modifiers = 0;
3869
Bram Moolenaar734a8672019-12-02 22:49:38 +01003870 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 DragQueryPoint(hDrop, &pt);
3872 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3873
3874 reset_VIsual();
3875
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003876 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003877
3878 if (fnames != NULL)
3879 for (i = 0; i < cFiles; ++i)
3880 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003881 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3882 fnames[i] = utf16_to_enc(wszFile, NULL);
3883 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884 {
3885 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3886 fnames[i] = vim_strsave((char_u *)szFile);
3887 }
3888 }
3889
3890 DragFinish(hDrop);
3891
3892 if (fnames != NULL)
3893 {
LemonBoy45684c62022-04-24 15:46:42 +01003894 int kbd_modifiers = get_active_modifiers();
3895
3896 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003897 modifiers |= MOUSE_SHIFT;
LemonBoy45684c62022-04-24 15:46:42 +01003898 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003899 modifiers |= MOUSE_CTRL;
LemonBoy45684c62022-04-24 15:46:42 +01003900 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003901 modifiers |= MOUSE_ALT;
3902
3903 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3904
3905 s_need_activate = TRUE;
3906 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003907}
3908
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003909 static int
3910_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003911 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003912 HWND hwndCtl,
3913 UINT code,
3914 int pos)
3915{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003916 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917 scrollbar_T *sb, *sb_info;
3918 long val;
3919 int dragging = FALSE;
3920 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003921 SCROLLINFO si;
3922
3923 si.cbSize = sizeof(si);
3924 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003925
3926 sb = gui_mswin_find_scrollbar(hwndCtl);
3927 if (sb == NULL)
3928 return 0;
3929
Bram Moolenaar734a8672019-12-02 22:49:38 +01003930 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003931 {
3932 /*
3933 * Careful: need to get scrollbar info out of first (left) scrollbar
3934 * for window, but keep real scrollbar too because we must pass it to
3935 * gui_drag_scrollbar().
3936 */
3937 sb_info = &sb->wp->w_scrollbars[0];
3938 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003939 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003940 sb_info = sb;
3941 val = sb_info->value;
3942
3943 switch (code)
3944 {
3945 case SB_THUMBTRACK:
3946 val = pos;
3947 dragging = TRUE;
3948 if (sb->scroll_shift > 0)
3949 val <<= sb->scroll_shift;
3950 break;
3951 case SB_LINEDOWN:
3952 val++;
3953 break;
3954 case SB_LINEUP:
3955 val--;
3956 break;
3957 case SB_PAGEDOWN:
3958 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3959 break;
3960 case SB_PAGEUP:
3961 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3962 break;
3963 case SB_TOP:
3964 val = 0;
3965 break;
3966 case SB_BOTTOM:
3967 val = sb_info->max;
3968 break;
3969 case SB_ENDSCROLL:
3970 if (prev_code == SB_THUMBTRACK)
3971 {
3972 /*
3973 * "pos" only gives us 16-bit data. In case of large file,
3974 * use GetScrollPos() which returns 32-bit. Unfortunately it
3975 * is not valid while the scrollbar is being dragged.
3976 */
3977 val = GetScrollPos(hwndCtl, SB_CTL);
3978 if (sb->scroll_shift > 0)
3979 val <<= sb->scroll_shift;
3980 }
3981 break;
3982
3983 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01003984 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003985 return 0;
3986 }
3987 prev_code = code;
3988
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003989 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
3990 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003991
3992 /*
3993 * When moving a vertical scrollbar, move the other vertical scrollbar too.
3994 */
3995 if (sb->wp != NULL)
3996 {
3997 scrollbar_T *sba = sb->wp->w_scrollbars;
3998 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
3999
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004000 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004001 }
4002
Bram Moolenaar734a8672019-12-02 22:49:38 +01004003 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004004 s_busy_processing = TRUE;
4005
Bram Moolenaar734a8672019-12-02 22:49:38 +01004006 // When "allow_scrollbar" is FALSE still need to remember the new
4007 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004008 dont_scroll = !allow_scrollbar;
4009
Bram Moolenaara338adc2018-01-31 20:51:47 +01004010 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004011 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004012 mch_enable_flush();
4013 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004014
4015 s_busy_processing = FALSE;
4016 dont_scroll = dont_scroll_save;
4017
4018 return 0;
4019}
4020
4021
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022#ifdef FEAT_XPM_W32
4023# include "xpm_w32.h"
4024#endif
4025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
Bram Moolenaar734a8672019-12-02 22:49:38 +01004027// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028#define TEAROFF_PADDING_X 2
4029#define TEAROFF_BUTTON_PAD_X 8
4030#define TEAROFF_MIN_WIDTH 200
4031#define TEAROFF_SUBMENU_LABEL ">>"
4032#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4033
4034
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004035#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036# define ID_BEVAL_TOOLTIP 200
4037# define BEVAL_TEXT_LEN MAXPATHL
4038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004040static UINT_PTR beval_timer_id = 0;
4041static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004042#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004043
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004044
Bram Moolenaar734a8672019-12-02 22:49:38 +01004045// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046
4047#ifdef FEAT_MENU
4048static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050
4051/*
4052 * Use the system font for dialogs and tear-off menus. Remove this line to
4053 * use DLG_FONT_NAME.
4054 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004055#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058#define VIM_CLASSW L"Vim"
4059
Bram Moolenaar734a8672019-12-02 22:49:38 +01004060// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4061// thus there should be room for every dialog. For tearoffs it's made bigger
4062// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063#define DLG_ALLOC_SIZE 16 * 1024
4064
4065/*
4066 * stuff for dialogs, menus, tearoffs etc.
4067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068static PWORD
4069add_dialog_element(
4070 PWORD p,
4071 DWORD lStyle,
4072 WORD x,
4073 WORD y,
4074 WORD w,
4075 WORD h,
4076 WORD Id,
4077 WORD clss,
4078 const char *caption);
4079static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004080static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004081#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084static void get_dialog_font_metrics(void);
4085
4086static int dialog_default_button = -1;
4087
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088#ifdef FEAT_TOOLBAR
4089static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004090static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004091static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004093#else
4094# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095#endif
4096
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004097#ifdef FEAT_GUI_TABLINE
4098static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004099static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004100#endif
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#ifdef FEAT_MBYTE_IME
4103static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4104static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4105#endif
4106#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4107# ifdef NOIME
4108typedef struct tagCOMPOSITIONFORM {
4109 DWORD dwStyle;
4110 POINT ptCurrentPos;
4111 RECT rcArea;
4112} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4113typedef HANDLE HIMC;
4114# endif
4115
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004116static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004117static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4118static HIMC (WINAPI *pImmGetContext)(HWND);
4119static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4120static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4121static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4122static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004123static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4124static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004125static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4126static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004127static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128static void dyn_imm_load(void);
4129#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130# define pImmGetCompositionStringW ImmGetCompositionStringW
4131# define pImmGetContext ImmGetContext
4132# define pImmAssociateContext ImmAssociateContext
4133# define pImmReleaseContext ImmReleaseContext
4134# define pImmGetOpenStatus ImmGetOpenStatus
4135# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004136# define pImmGetCompositionFontW ImmGetCompositionFontW
4137# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138# define pImmSetCompositionWindow ImmSetCompositionWindow
4139# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004140# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141#endif
4142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143#ifdef FEAT_MENU
4144/*
4145 * Figure out how high the menu bar is at the moment.
4146 */
4147 static int
4148gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004149 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 static int old_menu_height = -1;
4152
4153 RECT rc1, rc2;
4154 int num;
4155 int menu_height;
4156
4157 if (gui.menu_is_active)
4158 num = GetMenuItemCount(s_menuBar);
4159 else
4160 num = 0;
4161
4162 if (num == 0)
4163 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004164 else if (IsMinimized(s_hwnd))
4165 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004166 // The height of the menu cannot be determined while the window is
4167 // minimized. Take the previous height if the menu is changed in that
4168 // state, to avoid that Vim's vertical window size accidentally
4169 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004170 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 else
4173 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004174 /*
4175 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4176 * seem to have been set yet, so menu wraps in default window
4177 * width which is very narrow. Instead just return height of a
4178 * single menu item. Will still be wrong when the menu really
4179 * should wrap over more than one line.
4180 */
4181 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4182 if (gui.starting)
4183 menu_height = rc1.bottom - rc1.top + 1;
4184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004186 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4187 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 }
4190
4191 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004192 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004193 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
4195 return menu_height;
4196}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004197#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
4199
4200/*
4201 * Setup for the Intellimouse
4202 */
LemonBoyc27747e2022-05-07 12:25:40 +01004203 static long
4204mouse_vertical_scroll_step(void)
4205{
4206 UINT val;
4207 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4208 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4209 return 3; // Safe default;
4210}
4211
4212 static long
4213mouse_horizontal_scroll_step(void)
4214{
4215 UINT val;
4216 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4217 return (long)val;
4218 return 3; // Safe default;
4219}
4220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 static void
4222init_mouse_wheel(void)
4223{
LemonBoyc27747e2022-05-07 12:25:40 +01004224 // Get the default values for the horizontal and vertical scroll steps from
4225 // the system.
4226 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4227 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
Bram Moolenaarae20f342019-11-05 21:09:23 +01004230/*
LemonBoya773d842022-05-10 20:54:46 +01004231 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004234_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
LemonBoy365d8f72022-05-05 19:23:07 +01004236 int button;
4237 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004238 int modifiers = 0;
4239 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004240 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4241 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
Bram Moolenaarae20f342019-11-05 21:09:23 +01004243 wp = gui_mouse_window(FIND_POPUP);
4244
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004245#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004246 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004247 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004248 cmdarg_T cap;
4249 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004250
Bram Moolenaarae20f342019-11-05 21:09:23 +01004251 // Mouse hovers over popup window, scroll it if possible.
4252 mouse_row = wp->w_winrow;
4253 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004254 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004255 if (horizontal)
4256 {
4257 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4258 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4259 }
4260 else
4261 {
4262 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4263 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4264 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004265 clear_oparg(&oa);
4266 cap.oap = &oa;
4267 nv_mousescroll(&cap);
4268 update_screen(0);
4269 setcursor();
4270 out_flush();
4271 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004272 }
4273#endif
4274
Bram Moolenaarae20f342019-11-05 21:09:23 +01004275 if (wp == NULL || !p_scf)
4276 wp = curwin;
4277
LemonBoy365d8f72022-05-05 19:23:07 +01004278 // Translate the scroll event into an event that Vim can process so that
4279 // the user has a chance to map the scrollwheel buttons.
4280 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004281 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
LemonBoy365d8f72022-05-05 19:23:07 +01004283 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004284
4285 kbd_modifiers = get_active_modifiers();
4286
4287 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4288 modifiers |= MOUSE_SHIFT;
4289 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4290 modifiers |= MOUSE_CTRL;
4291 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4292 modifiers |= MOUSE_ALT;
4293
LemonBoya773d842022-05-10 20:54:46 +01004294 // The cursor position is relative to the upper-left corner of the screen.
4295 pt.x = GET_X_LPARAM(lParam);
4296 pt.y = GET_Y_LPARAM(lParam);
4297 ScreenToClient(s_textArea, &pt);
4298
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004299 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300}
4301
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004302#ifdef USE_SYSMENU_FONT
4303/*
4304 * Get Menu Font.
4305 * Return OK or FAIL.
4306 */
4307 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004308gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004309{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004310 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004311
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004312 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4313 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004314 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004315 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004316 &nm,
4317 0))
4318 return FAIL;
4319 *lf = nm.lfMenuFont;
4320 return OK;
4321}
4322#endif
4323
4324
4325#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4326/*
4327 * Set the GUI tabline font to the system menu font
4328 */
4329 static void
4330set_tabline_font(void)
4331{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004332 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004333 HFONT font;
4334 HWND hwnd;
4335 HDC hdc;
4336 HFONT hfntOld;
4337 TEXTMETRIC tm;
4338
4339 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4340 return;
4341
K.Takatac81e9bf2022-01-16 14:15:49 +00004342 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004343 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004344
4345 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4346
4347 /*
4348 * Compute the height of the font used for the tab text
4349 */
4350 hwnd = GetDesktopWindow();
4351 hdc = GetWindowDC(hwnd);
4352 hfntOld = SelectFont(hdc, font);
4353
4354 GetTextMetrics(hdc, &tm);
4355
4356 SelectFont(hdc, hfntOld);
4357 ReleaseDC(hwnd, hdc);
4358
4359 /*
4360 * The space used by the tab border and the space between the tab label
4361 * and the tab border is included as 7.
4362 */
4363 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4364}
K.Takatac81e9bf2022-01-16 14:15:49 +00004365#else
4366# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004367#endif
4368
Bram Moolenaar520470a2005-06-16 21:59:56 +00004369/*
4370 * Invoked when a setting was changed.
4371 */
4372 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004373_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004374{
LemonBoy365d8f72022-05-05 19:23:07 +01004375 switch (param)
4376 {
4377 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004378 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004379 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004380 case SPI_SETWHEELSCROLLCHARS:
4381 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004382 break;
4383 case SPI_SETNONCLIENTMETRICS:
4384 set_tabline_font();
4385 break;
4386 default:
4387 break;
4388 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004389 return 0;
4390}
4391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392#ifdef FEAT_NETBEANS_INTG
4393 static void
4394_OnWindowPosChanged(
4395 HWND hwnd,
4396 const LPWINDOWPOS lpwpos)
4397{
4398 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004399 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
4401 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4402 || lpwpos->cx != cx || lpwpos->cy != cy))
4403 {
4404 x = lpwpos->x;
4405 y = lpwpos->y;
4406 cx = lpwpos->cx;
4407 cy = lpwpos->cy;
4408 netbeans_frame_moved(x, y);
4409 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004410 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004411 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412}
4413#endif
4414
K.Takata4f2417f2021-06-05 16:25:32 +02004415
4416static HWND hwndTip = NULL;
4417
4418 static void
4419show_sizing_tip(int cols, int rows)
4420{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004421 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004422 char buf[32];
4423
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004424 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004425 ti.hwnd = s_hwnd;
4426 ti.uId = (UINT_PTR)s_hwnd;
4427 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4428 ti.lpszText = buf;
4429 sprintf(buf, "%dx%d", cols, rows);
4430 if (hwndTip == NULL)
4431 {
4432 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4433 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4434 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4435 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4436 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4437 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4438 }
4439 else
4440 {
4441 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4442 }
4443 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4444}
4445
4446 static void
4447destroy_sizing_tip(void)
4448{
4449 if (hwndTip != NULL)
4450 {
4451 DestroyWindow(hwndTip);
4452 hwndTip = NULL;
4453 }
4454}
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 static int
4457_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 UINT fwSide,
4459 LPRECT lprc)
4460{
4461 int w, h;
4462 int valid_w, valid_h;
4463 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004464 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 w = lprc->right - lprc->left;
4467 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004468 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 w_offset = w - valid_w;
4470 h_offset = h - valid_h;
4471
4472 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4473 || fwSide == WMSZ_BOTTOMLEFT)
4474 lprc->left += w_offset;
4475 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4476 || fwSide == WMSZ_BOTTOMRIGHT)
4477 lprc->right -= w_offset;
4478
4479 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4480 || fwSide == WMSZ_TOPRIGHT)
4481 lprc->top += h_offset;
4482 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4483 || fwSide == WMSZ_BOTTOMRIGHT)
4484 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004485
4486 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return TRUE;
4488}
4489
K.Takata92000e22022-01-20 15:10:57 +00004490#ifdef FEAT_GUI_TABLINE
4491 static void
4492_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4493{
4494 if (gui_mch_showing_tabline())
4495 {
4496 POINT pt;
4497 RECT rect;
4498
4499 /*
4500 * If the cursor is on the tabline, display the tab menu
4501 */
4502 GetCursorPos(&pt);
4503 GetWindowRect(s_textArea, &rect);
4504 if (pt.y < rect.top)
4505 {
4506 show_tabline_popup_menu();
4507 return;
4508 }
4509 }
4510 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4511}
4512
4513 static void
4514_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4515{
4516 /*
4517 * If the user double clicked the tabline, create a new tab
4518 */
4519 if (gui_mch_showing_tabline())
4520 {
4521 POINT pt;
4522 RECT rect;
4523
4524 GetCursorPos(&pt);
4525 GetWindowRect(s_textArea, &rect);
4526 if (pt.y < rect.top)
4527 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4528 }
4529 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4530}
4531#endif
4532
4533 static UINT
4534_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4535{
4536 UINT result;
4537 int x, y;
4538
4539 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4540 if (result != HTCLIENT)
4541 return result;
4542
4543#ifdef FEAT_GUI_TABLINE
4544 if (gui_mch_showing_tabline())
4545 {
4546 RECT rct;
4547
4548 // If the cursor is on the GUI tabline, don't process this event
4549 GetWindowRect(s_textArea, &rct);
4550 if (yPos < rct.top)
4551 return result;
4552 }
4553#endif
4554 (void)gui_mch_get_winpos(&x, &y);
4555 xPos -= x;
4556
4557 if (xPos < 48) // <VN> TODO should use system metric?
4558 return HTBOTTOMLEFT;
4559 else
4560 return HTBOTTOMRIGHT;
4561}
4562
4563#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4564 static LRESULT
4565_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4566{
4567 switch (hdr->code)
4568 {
4569 case TTN_GETDISPINFOW:
4570 case TTN_GETDISPINFO:
4571 {
4572 char_u *str = NULL;
4573 static void *tt_text = NULL;
4574
4575 VIM_CLEAR(tt_text);
4576
4577# ifdef FEAT_GUI_TABLINE
4578 if (gui_mch_showing_tabline()
4579 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4580 {
4581 POINT pt;
4582 /*
4583 * Mouse is over the GUI tabline. Display the
4584 * tooltip for the tab under the cursor
4585 *
4586 * Get the cursor position within the tab control
4587 */
4588 GetCursorPos(&pt);
4589 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4590 {
4591 TCHITTESTINFO htinfo;
4592 int idx;
4593
4594 /*
4595 * Get the tab under the cursor
4596 */
4597 htinfo.pt.x = pt.x;
4598 htinfo.pt.y = pt.y;
4599 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4600 if (idx != -1)
4601 {
4602 tabpage_T *tp;
4603
4604 tp = find_tabpage(idx + 1);
4605 if (tp != NULL)
4606 {
4607 get_tabline_label(tp, TRUE);
4608 str = NameBuff;
4609 }
4610 }
4611 }
4612 }
4613# endif
4614# ifdef FEAT_TOOLBAR
4615# ifdef FEAT_GUI_TABLINE
4616 else
4617# endif
4618 {
4619 UINT idButton;
4620 vimmenu_T *pMenu;
4621
4622 idButton = (UINT) hdr->idFrom;
4623 pMenu = gui_mswin_find_menu(root_menu, idButton);
4624 if (pMenu)
4625 str = pMenu->strings[MENU_INDEX_TIP];
4626 }
4627# endif
4628 if (str == NULL)
4629 break;
4630
4631 // Set the maximum width, this also enables using \n for
4632 // line break.
4633 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4634
4635 if (hdr->code == TTN_GETDISPINFOW)
4636 {
4637 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4638
4639 tt_text = enc_to_utf16(str, NULL);
4640 lpdi->lpszText = tt_text;
4641 // can't show tooltip if failed
4642 }
4643 else
4644 {
4645 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4646
4647 if (STRLEN(str) < sizeof(lpdi->szText)
4648 || ((tt_text = vim_strsave(str)) == NULL))
4649 vim_strncpy((char_u *)lpdi->szText, str,
4650 sizeof(lpdi->szText) - 1);
4651 else
4652 lpdi->lpszText = tt_text;
4653 }
4654 }
4655 break;
4656
4657# ifdef FEAT_GUI_TABLINE
4658 case TCN_SELCHANGE:
4659 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4660 {
4661 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4662 return 0L;
4663 }
4664 break;
4665
4666 case NM_RCLICK:
4667 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4668 {
4669 show_tabline_popup_menu();
4670 return 0L;
4671 }
4672 break;
4673# endif
4674
4675 default:
4676 break;
4677 }
4678 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4679}
4680#endif
4681
4682#if defined(MENUHINTS) && defined(FEAT_MENU)
4683 static LRESULT
4684_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4685{
4686 if (((UINT) HIWORD(wParam)
4687 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4688 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004689 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004690 {
4691 UINT idButton;
4692 vimmenu_T *pMenu;
4693 static int did_menu_tip = FALSE;
4694
4695 if (did_menu_tip)
4696 {
4697 msg_clr_cmdline();
4698 setcursor();
4699 out_flush();
4700 did_menu_tip = FALSE;
4701 }
4702
4703 idButton = (UINT)LOWORD(wParam);
4704 pMenu = gui_mswin_find_menu(root_menu, idButton);
4705 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4706 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4707 {
4708 ++msg_hist_off;
4709 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4710 --msg_hist_off;
4711 setcursor();
4712 out_flush();
4713 did_menu_tip = TRUE;
4714 }
4715 return 0L;
4716 }
4717 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4718}
4719#endif
4720
K.Takatac81e9bf2022-01-16 14:15:49 +00004721 static LRESULT
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004722_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +00004723{
4724 s_dpi = ydpi;
4725 s_in_dpichanged = TRUE;
4726 //TRACE("DPI: %d", ydpi);
4727
4728 update_scrollbar_size();
4729 update_toolbar_size();
4730 set_tabline_font();
4731
4732 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4733 gui_get_wide_font();
4734 gui_mswin_get_menu_height(FALSE);
4735#ifdef FEAT_MBYTE_IME
4736 im_set_position(gui.row, gui.col);
4737#endif
4738 InvalidateRect(hwnd, NULL, TRUE);
4739
4740 s_in_dpichanged = FALSE;
4741 return 0L;
4742}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
4744
4745 static LRESULT CALLBACK
4746_WndProc(
4747 HWND hwnd,
4748 UINT uMsg,
4749 WPARAM wParam,
4750 LPARAM lParam)
4751{
LemonBoya773d842022-05-10 20:54:46 +01004752 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4753 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 HandleMouseHide(uMsg, lParam);
4756
4757 s_uMsg = uMsg;
4758 s_wParam = wParam;
4759 s_lParam = lParam;
4760
4761 switch (uMsg)
4762 {
4763 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4764 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004765 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004767 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4769 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4770 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4771 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4772#ifdef FEAT_MENU
4773 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4774#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004775 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4776 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4778 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004779 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4780 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4782 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4783 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4784#ifdef FEAT_NETBEANS_INTG
4785 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4786#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004787#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004788 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4789 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004790#endif
K.Takata92000e22022-01-20 15:10:57 +00004791 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004792
Bram Moolenaar734a8672019-12-02 22:49:38 +01004793 case WM_QUERYENDSESSION: // System wants to go down.
4794 gui_shell_closed(); // Will exit when no changed buffers.
4795 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
4797 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004798 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004801 return 0L;
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 break;
4804
4805 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004806 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4807 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004808 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return 0L;
4810
4811 case WM_SYSCHAR:
4812 /*
4813 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4814 * shortcut key, handle like a typed ALT key, otherwise call Windows
4815 * ALT key handling.
4816 */
4817#ifdef FEAT_MENU
4818 if ( !gui.menu_is_active
4819 || p_wak[0] == 'n'
4820 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4821 )
4822#endif
4823 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004824 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 return 0L;
4826 }
4827#ifdef FEAT_MENU
4828 else
K.Takata4ac893f2022-01-20 12:44:28 +00004829 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
4831
4832 case WM_SYSKEYUP:
4833#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004834 // This used to be done only when menu is active: ALT key is used for
4835 // that. But that caused problems when menu is disabled and using
4836 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4837 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004838 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004840 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
4842
K.Takata4f2417f2021-06-05 16:25:32 +02004843 case WM_EXITSIZEMOVE:
4844 destroy_sizing_tip();
4845 break;
4846
Bram Moolenaar734a8672019-12-02 22:49:38 +01004847 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004851 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004852 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004853 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
Bram Moolenaar734a8672019-12-02 22:49:38 +01004855 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004856 case WM_SETTINGCHANGE:
4857 return _OnSettingChange((UINT)wParam);
4858
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004859#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004861 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#endif
K.Takata92000e22022-01-20 15:10:57 +00004863
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#if defined(MENUHINTS) && defined(FEAT_MENU)
4865 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004866 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869#ifdef FEAT_MBYTE_IME
4870 case WM_IME_NOTIFY:
4871 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004872 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004873 return 1L;
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 case WM_IME_COMPOSITION:
4876 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004877 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004878 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#endif
K.Takatac81e9bf2022-01-16 14:15:49 +00004880 case WM_DPICHANGED:
4881 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4882 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004886 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#endif
K.Takata92000e22022-01-20 15:10:57 +00004889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 }
4891
K.Takata4ac893f2022-01-20 12:44:28 +00004892 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893}
4894
4895/*
4896 * End of call-back routines
4897 */
4898
Bram Moolenaar734a8672019-12-02 22:49:38 +01004899// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900HWND vim_parent_hwnd = NULL;
4901
4902 static BOOL CALLBACK
4903FindWindowTitle(HWND hwnd, LPARAM lParam)
4904{
4905 char buf[2048];
4906 char *title = (char *)lParam;
4907
4908 if (GetWindowText(hwnd, buf, sizeof(buf)))
4909 {
4910 if (strstr(buf, title) != NULL)
4911 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004912 // Found it. Store the window ref. and quit searching if MDI
4913 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004915 if (vim_parent_hwnd != NULL)
4916 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004919 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
4921
4922/*
4923 * Invoked for '-P "title"' argument: search for parent application to open
4924 * our window in.
4925 */
4926 void
4927gui_mch_set_parent(char *title)
4928{
4929 EnumWindows(FindWindowTitle, (LPARAM)title);
4930 if (vim_parent_hwnd == NULL)
4931 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004932 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 mch_exit(2);
4934 }
4935}
4936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 static void
4939ole_error(char *arg)
4940{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004941 char buf[IOSIZE];
4942
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004943# ifdef VIMDLL
4944 gui.in_use = mch_is_gui_executable();
4945# endif
4946
Bram Moolenaar734a8672019-12-02 22:49:38 +01004947 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004948 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004949 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004950 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004954#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4955 static char *
4956gvim_error(void)
4957{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004958 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004959
4960 if (starting)
4961 {
4962 mch_errmsg(msg);
4963 mch_errmsg("\n");
4964 mch_exit(2);
4965 }
4966 return msg;
4967}
4968
4969 char *
4970gui_mch_do_spawn(char_u *arg)
4971{
4972 int len;
4973# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4974 char_u *session = NULL;
4975 LPWSTR tofree1 = NULL;
4976# endif
4977 WCHAR name[MAX_PATH];
4978 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
4979 STARTUPINFOW si = {sizeof(si)};
4980 PROCESS_INFORMATION pi;
4981
4982 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
4983 goto error;
4984 p = wcsrchr(name, L'\\');
4985 if (p == NULL)
4986 goto error;
4987 // Replace the executable name from vim(d).exe to gvim(d).exe.
4988# ifdef DEBUG
4989 wcscpy(p + 1, L"gvimd.exe");
4990# else
4991 wcscpy(p + 1, L"gvim.exe");
4992# endif
4993
4994# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
4995 if (starting)
4996# endif
4997 {
4998 // Pass the command line to the new process.
4999 p = GetCommandLineW();
5000 // Skip 1st argument.
5001 while (*p && *p != L' ' && *p != L'\t')
5002 {
5003 if (*p == L'"')
5004 {
5005 while (*p && *p != L'"')
5006 ++p;
5007 if (*p)
5008 ++p;
5009 }
5010 else
5011 ++p;
5012 }
5013 cmd = p;
5014 }
5015# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5016 else
5017 {
5018 // Create a session file and pass it to the new process.
5019 LPWSTR wsession;
5020 char_u *savebg;
5021 int ret;
5022
5023 session = vim_tempname('s', FALSE);
5024 if (session == NULL)
5025 goto error;
5026 savebg = p_bg;
5027 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5028 ret = write_session_file(session);
5029 vim_free(p_bg);
5030 p_bg = savebg;
5031 if (!ret)
5032 goto error;
5033 wsession = enc_to_utf16(session, NULL);
5034 if (wsession == NULL)
5035 goto error;
5036 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005037 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005038 if (cmd == NULL)
5039 {
5040 vim_free(wsession);
5041 goto error;
5042 }
5043 tofree1 = cmd;
5044 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5045 wsession, wsession);
5046 vim_free(wsession);
5047 }
5048# endif
5049
5050 // Check additional arguments to the `:gui` command.
5051 if (arg != NULL)
5052 {
5053 warg = enc_to_utf16(arg, NULL);
5054 if (warg == NULL)
5055 goto error;
5056 tofree2 = warg;
5057 }
5058 else
5059 warg = L"";
5060
5061 // Set up the new command line.
5062 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005063 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005064 if (newcmd == NULL)
5065 goto error;
5066 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5067
5068 // Spawn a new GUI process.
5069 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5070 NULL, NULL, &si, &pi))
5071 goto error;
5072 CloseHandle(pi.hProcess);
5073 CloseHandle(pi.hThread);
5074 mch_exit(0);
5075
5076error:
5077# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5078 if (session)
5079 mch_remove(session);
5080 vim_free(session);
5081 vim_free(tofree1);
5082# endif
5083 vim_free(newcmd);
5084 vim_free(tofree2);
5085 return gvim_error();
5086}
5087#endif
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089/*
5090 * Parse the GUI related command-line arguments. Any arguments used are
5091 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005092 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 */
5094 void
5095gui_mch_prepare(int *argc, char **argv)
5096{
5097 int silent = FALSE;
5098 int idx;
5099
Bram Moolenaar734a8672019-12-02 22:49:38 +01005100 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5102 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005103 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5105 && (argv[2][0] == '-' || argv[2][0] == '/'))
5106 {
5107 silent = TRUE;
5108 idx = 2;
5109 }
5110 else
5111 idx = 1;
5112
Bram Moolenaar734a8672019-12-02 22:49:38 +01005113 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (STRICMP(argv[idx] + 1, "register") == 0)
5115 {
5116#ifdef FEAT_OLE
5117 RegisterMe(silent);
5118 mch_exit(0);
5119#else
5120 if (!silent)
5121 ole_error("register");
5122 mch_exit(2);
5123#endif
5124 }
5125
Bram Moolenaar734a8672019-12-02 22:49:38 +01005126 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5128 {
5129#ifdef FEAT_OLE
5130 UnregisterMe(!silent);
5131 mch_exit(0);
5132#else
5133 if (!silent)
5134 ole_error("unregister");
5135 mch_exit(2);
5136#endif
5137 }
5138
Bram Moolenaar734a8672019-12-02 22:49:38 +01005139 // Ignore an -embedding argument. It is only relevant if the
5140 // application wants to treat the case when it is started manually
5141 // differently from the case where it is started via automation (and
5142 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5144 {
5145#ifdef FEAT_OLE
5146 *argc = 1;
5147#else
5148 ole_error("embedding");
5149 mch_exit(2);
5150#endif
5151 }
5152 }
5153
5154#ifdef FEAT_OLE
5155 {
5156 int bDoRestart = FALSE;
5157
5158 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005159 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (bDoRestart)
5161 mch_exit(0);
5162 }
5163#endif
5164
5165#ifdef FEAT_NETBEANS_INTG
5166 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005167 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 int arg;
5169
5170 for (arg = 1; arg < *argc; arg++)
5171 if (strncmp("-nb", argv[arg], 3) == 0)
5172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 netbeansArg = argv[arg];
5174 mch_memmove(&argv[arg], &argv[arg + 1],
5175 (--*argc - arg) * sizeof(char *));
5176 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005177 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181}
5182
K.Takatac81e9bf2022-01-16 14:15:49 +00005183 static void
5184load_dpi_func(void)
5185{
5186 HMODULE hUser32;
5187
5188 hUser32 = GetModuleHandle("user32.dll");
5189 if (hUser32 == NULL)
5190 goto fail;
5191
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005192 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5193 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5194 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005195 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005196 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5197 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005198
5199 if (pSetThreadDpiAwarenessContext != NULL)
5200 {
5201 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5202 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5203 if (oldctx != NULL)
5204 {
5205 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5206 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5207#ifdef DEBUG
5208 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5209 {
5210 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5211 }
5212#endif
5213 return;
5214 }
5215 }
5216
5217fail:
5218 // Disable PerMonitorV2 APIs.
5219 pGetDpiForSystem = stubGetDpiForSystem;
5220 pGetDpiForWindow = NULL;
5221 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5222 pSetThreadDpiAwarenessContext = NULL;
5223 pGetAwarenessFromDpiAwarenessContext = NULL;
5224}
5225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226/*
5227 * Initialise the GUI. Create all the windows, set up all the call-backs
5228 * etc.
5229 */
5230 int
5231gui_mch_init(void)
5232{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005234 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
Bram Moolenaar734a8672019-12-02 22:49:38 +01005237 // Return here if the window was already opened (happens when
5238 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005240 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242 /*
5243 * Load the tearoff bitmap
5244 */
5245#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005246 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247#endif
5248
K.Takatac81e9bf2022-01-16 14:15:49 +00005249 load_dpi_func();
5250
5251 s_dpi = pGetDpiForSystem();
5252 update_scrollbar_size();
5253
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005255 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
5257 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005258#ifdef FEAT_TOOLBAR
5259 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261
5262 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5263
Bram Moolenaar734a8672019-12-02 22:49:38 +01005264 // First try using the wide version, so that we can use any title.
5265 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005266 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005268 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 wndclassw.lpfnWndProc = _WndProc;
5270 wndclassw.cbClsExtra = 0;
5271 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005272 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5274 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5275 wndclassw.hbrBackground = s_brush;
5276 wndclassw.lpszMenuName = NULL;
5277 wndclassw.lpszClassName = szVimWndClassW;
5278
K.Takata4ac893f2022-01-20 12:44:28 +00005279 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 if (vim_parent_hwnd != NULL)
5284 {
5285#ifdef HAVE_TRY_EXCEPT
5286 __try
5287 {
5288#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005289 // Open inside the specified parent window.
5290 // TODO: last argument should point to a CLIENTCREATESTRUCT
5291 // structure.
5292 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005294 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005295 WS_OVERLAPPEDWINDOW | WS_CHILD
5296 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5298 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005299 100, // Any value will do
5300 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005302 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#ifdef HAVE_TRY_EXCEPT
5304 }
5305 __except(EXCEPTION_EXECUTE_HANDLER)
5306 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005307 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309#endif
5310 if (s_hwnd == NULL)
5311 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005312 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 mch_exit(2);
5314 }
5315 }
5316 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005317 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005318 // If the provided windowid is not valid reset it to zero, so that it
5319 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005320 if (IsWindow((HWND)win_socket_id) <= 0)
5321 win_socket_id = 0;
5322
Bram Moolenaar734a8672019-12-02 22:49:38 +01005323 // Create a window. If win_socket_id is not zero without border and
5324 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005325 s_hwnd = CreateWindowW(
5326 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005327 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5328 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005329 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5330 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005331 100, // Any value will do
5332 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005333 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005334 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005335 if (s_hwnd != NULL && win_socket_id != 0)
5336 {
5337 SetParent(s_hwnd, (HWND)win_socket_id);
5338 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5339 }
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 if (s_hwnd == NULL)
5343 return FAIL;
5344
K.Takatac81e9bf2022-01-16 14:15:49 +00005345 if (pGetDpiForWindow != NULL)
5346 {
5347 s_dpi = pGetDpiForWindow(s_hwnd);
5348 update_scrollbar_size();
5349 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5350 }
5351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5353 dyn_imm_load();
5354#endif
5355
Bram Moolenaar734a8672019-12-02 22:49:38 +01005356 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005357 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005358 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005359 wndclassw.style = CS_OWNDC;
5360 wndclassw.lpfnWndProc = _TextAreaWndProc;
5361 wndclassw.cbClsExtra = 0;
5362 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005363 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005364 wndclassw.hIcon = NULL;
5365 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5366 wndclassw.hbrBackground = NULL;
5367 wndclassw.lpszMenuName = NULL;
5368 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005369
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005370 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 return FAIL;
5372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005374 s_textArea = CreateWindowExW(
5375 0,
5376 szTextAreaClassW, L"Vim text area",
5377 WS_CHILD | WS_VISIBLE, 0, 0,
5378 100, // Any value will do for now
5379 100, // Any value will do for now
5380 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005381 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005382
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (s_textArea == NULL)
5384 return FAIL;
5385
Bram Moolenaar20321902016-02-17 12:30:17 +01005386#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005387 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005388 {
5389 HANDLE hIcon = NULL;
5390
5391 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005392 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005393 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005394#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005395
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396#ifdef FEAT_MENU
5397 s_menuBar = CreateMenu();
5398#endif
5399 s_hdc = GetDC(s_textArea);
5400
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
Bram Moolenaar734a8672019-12-02 22:49:38 +01005403 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005404 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
Bram Moolenaar734a8672019-12-02 22:49:38 +01005406 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 gui_mch_def_colors();
5408
Bram Moolenaar734a8672019-12-02 22:49:38 +01005409 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5410 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 set_normal_colors();
5412
5413 /*
5414 * Check that none of the colors are the same as the background color.
5415 * Then store the current values as the defaults.
5416 */
5417 gui_check_colors();
5418 gui.def_norm_pixel = gui.norm_pixel;
5419 gui.def_back_pixel = gui.back_pixel;
5420
Bram Moolenaar734a8672019-12-02 22:49:38 +01005421 // Get the colors for the highlight groups (gui_check_colors() might have
5422 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 highlight_gui_started();
5424
5425 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005426 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005428 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429
5430 /*
5431 * Set up for Intellimouse processing
5432 */
5433 init_mouse_wheel();
5434
5435 /*
5436 * compute a couple of metrics used for the dialogs
5437 */
5438 get_dialog_font_metrics();
5439#ifdef FEAT_TOOLBAR
5440 /*
5441 * Create the toolbar
5442 */
5443 initialise_toolbar();
5444#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005445#ifdef FEAT_GUI_TABLINE
5446 /*
5447 * Create the tabline
5448 */
5449 initialise_tabline();
5450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451#ifdef MSWIN_FIND_REPLACE
5452 /*
5453 * Initialise the dialog box stuff
5454 */
5455 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5456
Bram Moolenaar734a8672019-12-02 22:49:38 +01005457 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005459 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005461 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5463 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5464 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005467#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005468 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005469 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005470#endif
5471
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005472#ifdef FEAT_RENDER_OPTIONS
5473 if (p_rop)
5474 (void)gui_mch_set_rendering_options(p_rop);
5475#endif
5476
Bram Moolenaar748bf032005-02-02 23:04:36 +00005477theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005478 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005479 display_errors();
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 return OK;
5482}
5483
5484/*
5485 * Get the size of the screen, taking position on multiple monitors into
5486 * account (if supported).
5487 */
5488 static void
5489get_work_area(RECT *spi_rect)
5490{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005491 HMONITOR mon;
5492 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar734a8672019-12-02 22:49:38 +01005494 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005495 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005496 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005498 moninfo.cbSize = sizeof(MONITORINFO);
5499 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005501 *spi_rect = moninfo.rcWork;
5502 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
5504 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005505 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5507}
5508
5509/*
5510 * Set the size of the window to the given width and height in pixels.
5511 */
5512 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005513gui_mch_set_shellsize(
5514 int width,
5515 int height,
5516 int min_width UNUSED,
5517 int min_height UNUSED,
5518 int base_width UNUSED,
5519 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005520 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005523 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar734a8672019-12-02 22:49:38 +01005526 // Try to keep window completely on screen.
5527 // Get position of the screen work area. This is the part that is not
5528 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 get_work_area(&workarea_rect);
5530
Bram Moolenaar734a8672019-12-02 22:49:38 +01005531 // Resizing a maximized window looks very strange, unzoom it first.
5532 // But don't do it when still starting up, it may have been requested in
5533 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005534 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005536
5537 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538
Bram Moolenaar734a8672019-12-02 22:49:38 +01005539 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005540 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5541 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5542 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5543 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5544 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5545 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // The following should take care of keeping Vim on the same monitor, no
5548 // matter if the secondary monitor is left or right of the primary
5549 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005550 window_rect.right = window_rect.left + win_width;
5551 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005552
Bram Moolenaar734a8672019-12-02 22:49:38 +01005553 // If the window is going off the screen, move it on to the screen.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005554 if ((direction & RESIZE_HOR) && window_rect.right > workarea_rect.right)
5555 OffsetRect(&window_rect, workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005557 if ((direction & RESIZE_HOR) && window_rect.left < workarea_rect.left)
5558 OffsetRect(&window_rect, workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005560 if ((direction & RESIZE_VERT) && window_rect.bottom > workarea_rect.bottom)
5561 OffsetRect(&window_rect, 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005563 if ((direction & RESIZE_VERT) && window_rect.top < workarea_rect.top)
5564 OffsetRect(&window_rect, 0, workarea_rect.top - window_rect.top);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005566 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5567 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 SetActiveWindow(s_hwnd);
5570 SetFocus(s_hwnd);
5571
Bram Moolenaar734a8672019-12-02 22:49:38 +01005572 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574}
5575
5576
5577 void
5578gui_mch_set_scrollbar_thumb(
5579 scrollbar_T *sb,
5580 long val,
5581 long size,
5582 long max)
5583{
5584 SCROLLINFO info;
5585
5586 sb->scroll_shift = 0;
5587 while (max > 32767)
5588 {
5589 max = (max + 1) >> 1;
5590 val >>= 1;
5591 size >>= 1;
5592 ++sb->scroll_shift;
5593 }
5594
5595 if (sb->scroll_shift > 0)
5596 ++size;
5597
5598 info.cbSize = sizeof(info);
5599 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5600 info.nPos = val;
5601 info.nMin = 0;
5602 info.nMax = max;
5603 info.nPage = size;
5604 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5605}
5606
5607
5608/*
5609 * Set the current text font.
5610 */
5611 void
5612gui_mch_set_font(GuiFont font)
5613{
5614 gui.currFont = font;
5615}
5616
5617
5618/*
5619 * Set the current text foreground color.
5620 */
5621 void
5622gui_mch_set_fg_color(guicolor_T color)
5623{
5624 gui.currFgColor = color;
5625}
5626
5627/*
5628 * Set the current text background color.
5629 */
5630 void
5631gui_mch_set_bg_color(guicolor_T color)
5632{
5633 gui.currBgColor = color;
5634}
5635
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005636/*
5637 * Set the current text special color.
5638 */
5639 void
5640gui_mch_set_sp_color(guicolor_T color)
5641{
5642 gui.currSpColor = color;
5643}
5644
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005645#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646/*
5647 * Multi-byte handling, originally by Sung-Hoon Baek.
5648 * First static functions (no prototypes generated).
5649 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005650# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005651# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005652# endif
5653# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
5655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 * handle WM_IME_NOTIFY message
5657 */
5658 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005659_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 LRESULT lResult = 0;
5662 HIMC hImc;
5663
5664 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5665 return lResult;
5666 switch (dwCommand)
5667 {
5668 case IMN_SETOPENSTATUS:
5669 if (pImmGetOpenStatus(hImc))
5670 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005671 LOGFONTW lf = norm_logfont;
5672 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5673 // Work around when PerMonitorV2 is not enabled in the process level.
5674 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5675 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 im_set_position(gui.row, gui.col);
5677
Bram Moolenaar734a8672019-12-02 22:49:38 +01005678 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005679 State &= ~MODE_LANGMAP;
5680 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005682# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005683 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5685 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005686 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 int old_row = gui.row;
5688 int old_col = gui.col;
5689
5690 // This must be called here before
5691 // status_redraw_curbuf(), otherwise the mode
5692 // message may appear in the wrong position.
5693 showmode();
5694 status_redraw_curbuf();
5695 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005696 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 gui.row = old_row;
5698 gui.col = old_col;
5699 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005700# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
5703 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005704 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 lResult = 0;
5706 break;
5707 }
5708 pImmReleaseContext(hWnd, hImc);
5709 return lResult;
5710}
5711
5712 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005713_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
5715 char_u *ret;
5716 int len;
5717
Bram Moolenaar734a8672019-12-02 22:49:38 +01005718 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 return 0;
5720
5721 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5722 if (ret != NULL)
5723 {
5724 add_to_input_buf_csi(ret, len);
5725 vim_free(ret);
5726 return 1;
5727 }
5728 return 0;
5729}
5730
5731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 * void GetResultStr()
5733 *
5734 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5735 * get complete composition string
5736 */
5737 static char_u *
5738GetResultStr(HWND hwnd, int GCS, int *lenp)
5739{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005740 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005741 LONG ret;
5742 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 char_u *convbuf = NULL;
5744
5745 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5746 return NULL;
5747
K.Takatab0b2b732022-01-19 12:59:21 +00005748 // Get the length of the composition string.
5749 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5750 if (ret <= 0)
5751 return NULL;
5752
5753 // Allocate the requested buffer plus space for the NUL character.
5754 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 if (buf == NULL)
5756 return NULL;
5757
K.Takatab0b2b732022-01-19 12:59:21 +00005758 // Reads in the composition string.
5759 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5760 *lenp = ret / sizeof(WCHAR);
5761
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005762 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 pImmReleaseContext(hwnd, hIMC);
5764 vim_free(buf);
5765 return convbuf;
5766}
5767#endif
5768
Bram Moolenaar734a8672019-12-02 22:49:38 +01005769// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005770#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772/*
5773 * set font to IM.
5774 */
5775 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005776im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 HIMC hImc;
5779
5780 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5781 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005782 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 pImmReleaseContext(s_hwnd, hImc);
5784 }
5785}
5786
5787/*
5788 * Notify cursor position to IM.
5789 */
5790 void
5791im_set_position(int row, int col)
5792{
5793 HIMC hImc;
5794
5795 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5796 {
5797 COMPOSITIONFORM cfs;
5798
5799 cfs.dwStyle = CFS_POINT;
5800 cfs.ptCurrentPos.x = FILL_X(col);
5801 cfs.ptCurrentPos.y = FILL_Y(row);
5802 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005803 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5804 {
5805 // Work around when PerMonitorV2 is not enabled in the process level.
5806 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5807 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 pImmSetCompositionWindow(hImc, &cfs);
5810
5811 pImmReleaseContext(s_hwnd, hImc);
5812 }
5813}
5814
5815/*
5816 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5817 */
5818 void
5819im_set_active(int active)
5820{
5821 HIMC hImc;
5822 static HIMC hImcOld = (HIMC)0;
5823
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005824# ifdef VIMDLL
5825 if (!gui.in_use && !gui.starting)
5826 {
5827 mbyte_im_set_active(active);
5828 return;
5829 }
5830# endif
5831
Bram Moolenaar734a8672019-12-02 22:49:38 +01005832 if (pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
5834 if (p_imdisable)
5835 {
5836 if (hImcOld == (HIMC)0)
5837 {
5838 hImcOld = pImmGetContext(s_hwnd);
5839 if (hImcOld)
5840 pImmAssociateContext(s_hwnd, (HIMC)0);
5841 }
5842 active = FALSE;
5843 }
5844 else if (hImcOld != (HIMC)0)
5845 {
5846 pImmAssociateContext(s_hwnd, hImcOld);
5847 hImcOld = (HIMC)0;
5848 }
5849
5850 hImc = pImmGetContext(s_hwnd);
5851 if (hImc)
5852 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00005853 /*
5854 * for Korean ime
5855 */
5856 HKL hKL = GetKeyboardLayout(0);
5857
5858 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5859 {
5860 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5861 static BOOL bSaved = FALSE;
5862
5863 if (active)
5864 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005865 // if we have a saved conversion status, restore it
Bram Moolenaarca003e12006-03-17 23:19:38 +00005866 if (bSaved)
5867 pImmSetConversionStatus(hImc, dwConversionSaved,
5868 dwSentenceSaved);
5869 bSaved = FALSE;
5870 }
5871 else
5872 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005873 // save conversion status and disable korean
Bram Moolenaarca003e12006-03-17 23:19:38 +00005874 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5875 &dwSentenceSaved))
5876 {
5877 bSaved = TRUE;
5878 pImmSetConversionStatus(hImc,
5879 dwConversionSaved & ~(IME_CMODE_NATIVE
5880 | IME_CMODE_FULLSHAPE),
5881 dwSentenceSaved);
5882 }
5883 }
5884 }
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 pImmSetOpenStatus(hImc, active);
5887 pImmReleaseContext(s_hwnd, hImc);
5888 }
5889 }
5890}
5891
5892/*
5893 * Get IM status. When IM is on, return not 0. Else return 0.
5894 */
5895 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005896im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 int status = 0;
5899 HIMC hImc;
5900
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005901# ifdef VIMDLL
5902 if (!gui.in_use && !gui.starting)
5903 return mbyte_im_get_status();
5904# endif
5905
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5907 {
5908 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5909 pImmReleaseContext(s_hwnd, hImc);
5910 }
5911 return status;
5912}
5913
Bram Moolenaar734a8672019-12-02 22:49:38 +01005914#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005917/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005918 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005919 */
5920 static void
5921latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5922{
5923 int c;
5924
Bram Moolenaarca003e12006-03-17 23:19:38 +00005925 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005926 {
5927 c = *text++;
5928 switch (c)
5929 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005930 case 0xa4: c = 0x20ac; break; // euro
5931 case 0xa6: c = 0x0160; break; // S hat
5932 case 0xa8: c = 0x0161; break; // S -hat
5933 case 0xb4: c = 0x017d; break; // Z hat
5934 case 0xb8: c = 0x017e; break; // Z -hat
5935 case 0xbc: c = 0x0152; break; // OE
5936 case 0xbd: c = 0x0153; break; // oe
5937 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005938 }
5939 *unicodebuf++ = c;
5940 }
5941}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
5943#ifdef FEAT_RIGHTLEFT
5944/*
5945 * What is this for? In the case where you are using Win98 or Win2K or later,
5946 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
5947 * reverses the string sent to the TextOut... family. This sucks, because we
5948 * go to a lot of effort to do the right thing, and there doesn't seem to be a
5949 * way to tell Windblows not to do this!
5950 *
5951 * The short of it is that this 'RevOut' only gets called if you are running
5952 * one of the new, "improved" MS OSes, and only if you are running in
5953 * 'rightleft' mode. It makes display take *slightly* longer, but not
5954 * noticeably so.
5955 */
5956 static void
K.Takata135e1522022-01-29 15:27:58 +00005957RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 int col,
5959 int row,
5960 UINT foptions,
5961 CONST RECT *pcliprect,
5962 LPCTSTR text,
5963 UINT len,
5964 CONST INT *padding)
5965{
5966 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005968 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00005969 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005970 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971}
5972#endif
5973
Bram Moolenaar92467d32017-12-05 13:22:16 +01005974 static void
5975draw_line(
5976 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01005977 int y1,
5978 int x2,
5979 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01005980 COLORREF color)
5981{
5982#if defined(FEAT_DIRECTX)
5983 if (IS_ENABLE_DIRECTX())
5984 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
5985 else
5986#endif
5987 {
5988 HPEN hpen = CreatePen(PS_SOLID, 1, color);
5989 HPEN old_pen = SelectObject(s_hdc, hpen);
5990 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005991 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01005992 LineTo(s_hdc, x2, y2);
5993 DeleteObject(SelectObject(s_hdc, old_pen));
5994 }
5995}
5996
5997 static void
5998set_pixel(
5999 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006000 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006001 COLORREF color)
6002{
6003#if defined(FEAT_DIRECTX)
6004 if (IS_ENABLE_DIRECTX())
6005 DWriteContext_SetPixel(s_dwc, x, y, color);
6006 else
6007#endif
6008 SetPixel(s_hdc, x, y, color);
6009}
6010
6011 static void
6012fill_rect(
6013 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006014 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006015 COLORREF color)
6016{
6017#if defined(FEAT_DIRECTX)
6018 if (IS_ENABLE_DIRECTX())
6019 DWriteContext_FillRect(s_dwc, rcp, color);
6020 else
6021#endif
6022 {
6023 HBRUSH hbr2;
6024
6025 if (hbr == NULL)
6026 hbr2 = CreateSolidBrush(color);
6027 else
6028 hbr2 = hbr;
6029 FillRect(s_hdc, rcp, hbr2);
6030 if (hbr == NULL)
6031 DeleteBrush(hbr2);
6032 }
6033}
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 void
6036gui_mch_draw_string(
6037 int row,
6038 int col,
6039 char_u *text,
6040 int len,
6041 int flags)
6042{
6043 static int *padding = NULL;
6044 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 const RECT *pcliprect = NULL;
6046 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 static WCHAR *unicodebuf = NULL;
6048 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006049 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int y;
6052
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 /*
6054 * Italic and bold text seems to have an extra row of pixels at the bottom
6055 * (below where the bottom of the character should be). If we draw the
6056 * characters with a solid background, the top row of pixels in the
6057 * character below will be overwritten. We can fix this by filling in the
6058 * background ourselves, to the correct character proportions, and then
6059 * writing the character in transparent mode. Still have a problem when
6060 * the character is "_", which gets written on to the character below.
6061 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6062 * pixel in their slots, which fixes the problem with the bottom row of
6063 * pixels. We still need this code because otherwise the top row of pixels
6064 * becomes a problem. - webb.
6065 */
6066 static HBRUSH hbr_cache[2] = {NULL, NULL};
6067 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6068 static int brush_lru = 0;
6069 HBRUSH hbr;
6070 RECT rc;
6071
6072 if (!(flags & DRAW_TRANSP))
6073 {
6074 /*
6075 * Clear background first.
6076 * Note: FillRect() excludes right and bottom of rectangle.
6077 */
6078 rc.left = FILL_X(col);
6079 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 if (has_mbyte)
6081 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006082 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006083 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 }
6085 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 rc.right = FILL_X(col + len);
6087 rc.bottom = FILL_Y(row + 1);
6088
Bram Moolenaar734a8672019-12-02 22:49:38 +01006089 // Cache the created brush, that saves a lot of time. We need two:
6090 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (gui.currBgColor == brush_color[0])
6092 {
6093 hbr = hbr_cache[0];
6094 brush_lru = 1;
6095 }
6096 else if (gui.currBgColor == brush_color[1])
6097 {
6098 hbr = hbr_cache[1];
6099 brush_lru = 0;
6100 }
6101 else
6102 {
6103 if (hbr_cache[brush_lru] != NULL)
6104 DeleteBrush(hbr_cache[brush_lru]);
6105 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6106 brush_color[brush_lru] = gui.currBgColor;
6107 hbr = hbr_cache[brush_lru];
6108 brush_lru = !brush_lru;
6109 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006110
Bram Moolenaar92467d32017-12-05 13:22:16 +01006111 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113 SetBkMode(s_hdc, TRANSPARENT);
6114
6115 /*
6116 * When drawing block cursor, prevent inverted character spilling
6117 * over character cell (can happen with bold/italic)
6118 */
6119 if (flags & DRAW_CURSOR)
6120 {
6121 pcliprect = &rc;
6122 foptions = ETO_CLIPPED;
6123 }
6124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 SetTextColor(s_hdc, gui.currFgColor);
6126 SelectFont(s_hdc, gui.currFont);
6127
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006128#ifdef FEAT_DIRECTX
6129 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006130 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006131#endif
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6134 {
K.Takata135e1522022-01-29 15:27:58 +00006135 int i;
6136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 vim_free(padding);
6138 pad_size = Columns;
6139
Bram Moolenaar734a8672019-12-02 22:49:38 +01006140 // Don't give an out-of-memory message here, it would call us
6141 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006142 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (padding != NULL)
6144 for (i = 0; i < pad_size; i++)
6145 padding[i] = gui.char_width;
6146 }
6147
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 /*
6149 * We have to provide the padding argument because italic and bold versions
6150 * of fixed-width fonts are often one pixel or so wider than their normal
6151 * versions.
6152 * No check for DRAW_BOLD, Windows will have done it already.
6153 */
6154
Bram Moolenaar734a8672019-12-02 22:49:38 +01006155 // Check if there are any UTF-8 characters. If not, use normal text
6156 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (enc_utf8)
6158 for (n = 0; n < len; ++n)
6159 if (text[n] >= 0x80)
6160 break;
6161
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006162#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006163 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6164 // required that unicode drawing routine, currently. So this forces it
6165 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006166 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006167 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006168#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006169
Bram Moolenaar734a8672019-12-02 22:49:38 +01006170 // Check if the Unicode buffer exists and is big enough. Create it
6171 // with the same length as the multi-byte string, the number of wide
6172 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006173 if ((enc_utf8
6174 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6175 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 && (unicodebuf == NULL || len > unibuflen))
6177 {
6178 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006179 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006182 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 unibuflen = len;
6185 }
6186
6187 if (enc_utf8 && n < len && unicodebuf != NULL)
6188 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006189 // Output UTF-8 characters. Composing characters should be
6190 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006191 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006192 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006193 int cells; // cell width of string up to composing char
6194 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006195 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006197 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006199 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006201 c = utf_ptr2char(text + i);
6202 if (c >= 0x10000)
6203 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006204 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006205 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6206 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006207 }
6208 else
6209 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006210 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006211 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006212
6213 if (utf_iscomposing(c))
6214 cw = 0;
6215 else
6216 {
6217 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006218 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006219 cw = 1;
6220 }
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (unicodepdy != NULL)
6223 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006224 // Use unicodepdy to make characters fit as we expect, even
6225 // when the font uses different widths (e.g., bold character
6226 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006227 if (c >= 0x10000)
6228 {
6229 unicodepdy[wlen - 2] = cw * gui.char_width;
6230 unicodepdy[wlen - 1] = 0;
6231 }
6232 else
6233 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
6235 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006236 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006238#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006239 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006240 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006241 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006242 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006243 TEXT_X(col), TEXT_Y(row),
6244 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006245 gui.char_width, gui.currFgColor,
6246 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006247 }
6248 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006249#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006250 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6251 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006252 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006254 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006256 // If we want to display codepage data, and the current CP is not the
6257 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (unicodebuf != NULL)
6259 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006260 if (enc_latin9)
6261 latin9_to_ucs(text, len, unicodebuf);
6262 else
6263 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 MB_PRECOMPOSED,
6265 (char *)text, len,
6266 (LPWSTR)unicodebuf, unibuflen);
6267 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006268 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006269 // Use unicodepdy to make characters fit as we expect, even
6270 // when the font uses different widths (e.g., bold character
6271 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006272 if (unicodepdy != NULL)
6273 {
6274 int i;
6275 int cw;
6276
6277 for (i = 0; i < len; ++i)
6278 {
6279 cw = utf_char2cells(unicodebuf[i]);
6280 if (cw > 2)
6281 cw = 1;
6282 unicodepdy[i] = cw * gui.char_width;
6283 }
6284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006286 foptions, pcliprect, unicodebuf, len, unicodepdy);
6287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 }
6290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 {
6292#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006293 // Windows will mess up RL text, so we have to draw it character by
6294 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006295 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6297 foptions, pcliprect, (char *)text, len, padding);
6298 else
6299#endif
6300 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6301 foptions, pcliprect, (char *)text, len, padding);
6302 }
6303
Bram Moolenaar734a8672019-12-02 22:49:38 +01006304 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (flags & DRAW_UNDERL)
6306 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006307 // When p_linespace is 0, overwrite the bottom row of pixels.
6308 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 if (p_linespace > 1)
6311 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006312 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006314
Bram Moolenaar734a8672019-12-02 22:49:38 +01006315 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006316 if (flags & DRAW_STRIKE)
6317 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006318 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006319 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006320 }
6321
Bram Moolenaar734a8672019-12-02 22:49:38 +01006322 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006323 if (flags & DRAW_UNDERC)
6324 {
6325 int x;
6326 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006327 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006328
6329 y = FILL_Y(row + 1) - 1;
6330 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6331 {
6332 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006333 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006334 }
6335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
6338
6339/*
6340 * Output routines.
6341 */
6342
Bram Moolenaar734a8672019-12-02 22:49:38 +01006343/*
6344 * Flush any output to the screen
6345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 void
6347gui_mch_flush(void)
6348{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006349#if defined(FEAT_DIRECTX)
6350 if (IS_ENABLE_DIRECTX())
6351 DWriteContext_Flush(s_dwc);
6352#endif
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 GdiFlush();
6355}
6356
6357 static void
6358clear_rect(RECT *rcp)
6359{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006360 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
6363
Bram Moolenaarc716c302006-01-21 22:12:51 +00006364 void
6365gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6366{
6367 RECT workarea_rect;
6368
6369 get_work_area(&workarea_rect);
6370
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006372 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6373 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006374
Bram Moolenaar734a8672019-12-02 22:49:38 +01006375 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6376 // the menubar for MSwin, we subtract it from the screen height, so that
6377 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006378 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006379 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6380 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6381 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6382 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006383}
6384
6385
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386#if defined(FEAT_MENU) || defined(PROTO)
6387/*
6388 * Add a sub menu to the menu bar.
6389 */
6390 void
6391gui_mch_add_menu(
6392 vimmenu_T *menu,
6393 int pos)
6394{
6395 vimmenu_T *parent = menu->parent;
6396
6397 menu->submenu_id = CreatePopupMenu();
6398 menu->id = s_menu_id++;
6399
6400 if (menu_is_menubar(menu->name))
6401 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006402 WCHAR *wn;
6403 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006405 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006406 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006409 infow.cbSize = sizeof(infow);
6410 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6411 | MIIM_SUBMENU;
6412 infow.dwItemData = (long_u)menu;
6413 infow.wID = menu->id;
6414 infow.fType = MFT_STRING;
6415 infow.dwTypeData = wn;
6416 infow.cch = (UINT)wcslen(wn);
6417 infow.hSubMenu = menu->submenu_id;
6418 InsertMenuItemW((parent == NULL)
6419 ? s_menuBar : parent->submenu_id,
6420 (UINT)pos, TRUE, &infow);
6421 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423
Bram Moolenaar734a8672019-12-02 22:49:38 +01006424 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 if (parent == NULL)
6426 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006427# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 else if (IsWindow(parent->tearoff_handle))
6429 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433 void
6434gui_mch_show_popupmenu(vimmenu_T *menu)
6435{
6436 POINT mp;
6437
K.Takata45f9cfb2022-01-21 11:11:00 +00006438 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6440}
6441
6442 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006443gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 vimmenu_T *menu = gui_find_menu(path_name);
6446
6447 if (menu != NULL)
6448 {
6449 POINT p;
6450
Bram Moolenaar734a8672019-12-02 22:49:38 +01006451 // Find the position of the current cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 GetDCOrgEx(s_hdc, &p);
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006453 if (mouse_pos)
6454 {
6455 int mx, my;
6456
6457 gui_mch_getmouse(&mx, &my);
6458 p.x += mx;
6459 p.y += my;
6460 }
6461 else if (curwin != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 {
Bram Moolenaar53f81742017-09-22 14:35:51 +02006463 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6465 }
6466 msg_scroll = FALSE;
6467 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
6468 }
6469}
6470
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006471# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6474 * create it as a pseudo-"tearoff menu".
6475 */
6476 void
6477gui_make_tearoff(char_u *path_name)
6478{
6479 vimmenu_T *menu = gui_find_menu(path_name);
6480
Bram Moolenaar734a8672019-12-02 22:49:38 +01006481 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (menu != NULL)
6483 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6484}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006485# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487/*
6488 * Add a menu item to a menu
6489 */
6490 void
6491gui_mch_add_menu_item(
6492 vimmenu_T *menu,
6493 int idx)
6494{
6495 vimmenu_T *parent = menu->parent;
6496
6497 menu->id = s_menu_id++;
6498 menu->submenu_id = NULL;
6499
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006500# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6502 {
6503 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6504 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6505 }
6506 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006507# endif
6508# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (menu_is_toolbar(parent->name))
6510 {
6511 TBBUTTON newtb;
6512
Bram Moolenaara80faa82020-04-12 19:37:17 +02006513 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (menu_is_separator(menu->name))
6515 {
6516 newtb.iBitmap = 0;
6517 newtb.fsStyle = TBSTYLE_SEP;
6518 }
6519 else
6520 {
6521 newtb.iBitmap = get_toolbar_bitmap(menu);
6522 newtb.fsStyle = TBSTYLE_BUTTON;
6523 }
6524 newtb.idCommand = menu->id;
6525 newtb.fsState = TBSTATE_ENABLED;
6526 newtb.iString = 0;
6527 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6528 (LPARAM)&newtb);
6529 menu->submenu_id = (HMENU)-1;
6530 }
6531 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006532# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006534 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006536 wn = enc_to_utf16(menu->name, NULL);
6537 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006539 InsertMenuW(parent->submenu_id, (UINT)idx,
6540 (menu_is_separator(menu->name)
6541 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6542 (UINT)menu->id, wn);
6543 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006545# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (IsWindow(parent->tearoff_handle))
6547 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550}
6551
6552/*
6553 * Destroy the machine specific menu widget.
6554 */
6555 void
6556gui_mch_destroy_menu(vimmenu_T *menu)
6557{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006558# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 /*
6560 * is this a toolbar button?
6561 */
6562 if (menu->submenu_id == (HMENU)-1)
6563 {
6564 int iButton;
6565
6566 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6567 (WPARAM)menu->id, 0);
6568 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6569 }
6570 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
6573 if (menu->parent != NULL
6574 && menu_is_popup(menu->parent->dname)
6575 && menu->parent->submenu_id != NULL)
6576 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6577 else
6578 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6579 if (menu->submenu_id != NULL)
6580 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006581# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 if (IsWindow(menu->tearoff_handle))
6583 DestroyWindow(menu->tearoff_handle);
6584 if (menu->parent != NULL
6585 && menu->parent->children != NULL
6586 && IsWindow(menu->parent->tearoff_handle))
6587 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006588 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 menu->modes = 0;
6590 rebuild_tearoff(menu->parent);
6591 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594}
6595
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006596# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 static void
6598rebuild_tearoff(vimmenu_T *menu)
6599{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006600 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 char_u tbuf[128];
6602 RECT trect;
6603 RECT rct;
6604 RECT roct;
6605 int x, y;
6606
6607 HWND thwnd = menu->tearoff_handle;
6608
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006609 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (GetWindowRect(thwnd, &trect)
6611 && GetWindowRect(s_hwnd, &rct)
6612 && GetClientRect(s_hwnd, &roct))
6613 {
6614 x = trect.left - rct.left;
6615 y = (trect.top - rct.bottom + roct.bottom);
6616 }
6617 else
6618 {
6619 x = y = 0xffffL;
6620 }
6621 DestroyWindow(thwnd);
6622 if (menu->children != NULL)
6623 {
6624 gui_mch_tearoff(tbuf, menu, x, y);
6625 if (IsWindow(menu->tearoff_handle))
6626 (void) SetWindowPos(menu->tearoff_handle,
6627 NULL,
6628 (int)trect.left,
6629 (int)trect.top,
6630 0, 0,
6631 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6632 }
6633}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006634# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636/*
6637 * Make a menu either grey or not grey.
6638 */
6639 void
6640gui_mch_menu_grey(
6641 vimmenu_T *menu,
6642 int grey)
6643{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006644# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 /*
6646 * is this a toolbar button?
6647 */
6648 if (menu->submenu_id == (HMENU)-1)
6649 {
6650 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006651 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006654# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006655 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6656 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6660 {
6661 WORD menuID;
6662 HWND menuHandle;
6663
6664 /*
6665 * A tearoff button has changed state.
6666 */
6667 if (menu->children == NULL)
6668 menuID = (WORD)(menu->id);
6669 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006670 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6672 if (menuHandle)
6673 EnableWindow(menuHandle, !grey);
6674
6675 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677}
6678
Bram Moolenaar734a8672019-12-02 22:49:38 +01006679#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681
Bram Moolenaar734a8672019-12-02 22:49:38 +01006682// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006685#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6688/*
6689 * stuff for dialogs
6690 */
6691
6692/*
6693 * The callback routine used by all the dialogs. Very simple. First,
6694 * acknowledges the INITDIALOG message so that Windows knows to do standard
6695 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6696 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6697 * number.
6698 */
6699 static LRESULT CALLBACK
6700dialog_callback(
6701 HWND hwnd,
6702 UINT message,
6703 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006704 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
6706 if (message == WM_INITDIALOG)
6707 {
6708 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006709 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 (void)SetFocus(hwnd);
6711 if (dialog_default_button > IDCANCEL)
6712 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006713 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006714 // We don't have a default, set focus on another element of the
6715 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006716 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 return FALSE;
6718 }
6719
6720 if (message == WM_COMMAND)
6721 {
6722 int button = LOWORD(wParam);
6723
Bram Moolenaar734a8672019-12-02 22:49:38 +01006724 // Don't end the dialog if something was selected that was
6725 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (button >= DLG_NONBUTTON_CONTROL)
6727 return TRUE;
6728
Bram Moolenaar734a8672019-12-02 22:49:38 +01006729 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006731 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006732 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006733 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006734
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006735 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6736 p = utf16_to_enc(wp, NULL);
6737 vim_strncpy(s_textfield, p, IOSIZE);
6738 vim_free(p);
6739 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 /*
6743 * Need to check for IDOK because if the user just hits Return to
6744 * accept the default value, some reason this is what we get.
6745 */
6746 if (button == IDOK)
6747 {
6748 if (dialog_default_button > IDCANCEL)
6749 EndDialog(hwnd, dialog_default_button);
6750 }
6751 else
6752 EndDialog(hwnd, button - IDCANCEL);
6753 return TRUE;
6754 }
6755
6756 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6757 {
6758 EndDialog(hwnd, 0);
6759 return TRUE;
6760 }
6761 return FALSE;
6762}
6763
6764/*
6765 * Create a dialog dynamically from the parameter strings.
6766 * type = type of dialog (question, alert, etc.)
6767 * title = dialog title. may be NULL for default title.
6768 * message = text to display. Dialog sizes to accommodate it.
6769 * buttons = '\n' separated list of button captions, default first.
6770 * dfltbutton = number of default button.
6771 *
6772 * This routine returns 1 if the first button is pressed,
6773 * 2 for the second, etc.
6774 *
6775 * 0 indicates Esc was pressed.
6776 * -1 for unexpected error
6777 *
6778 * If stubbing out this fn, return 1.
6779 */
6780
Bram Moolenaar734a8672019-12-02 22:49:38 +01006781static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 "IDR_VIM",
6784 "IDR_VIM_ERROR",
6785 "IDR_VIM_ALERT",
6786 "IDR_VIM_INFO",
6787 "IDR_VIM_QUESTION"
6788};
6789
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 int
6791gui_mch_dialog(
6792 int type,
6793 char_u *title,
6794 char_u *message,
6795 char_u *buttons,
6796 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006797 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006798 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006801 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 int numButtons;
6803 int *buttonWidths, *buttonPositions;
6804 int buttonYpos;
6805 int nchar, i;
6806 DWORD lStyle;
6807 int dlgwidth = 0;
6808 int dlgheight;
6809 int editboxheight;
6810 int horizWidth = 0;
6811 int msgheight;
6812 char_u *pstart;
6813 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006814 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 char_u *tbuffer;
6816 RECT rect;
6817 HWND hwnd;
6818 HDC hdc;
6819 HFONT font, oldFont;
6820 TEXTMETRIC fontInfo;
6821 int fontHeight;
6822 int textWidth, minButtonWidth, messageWidth;
6823 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006824 int maxDialogHeight;
6825 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 int vertical;
6827 int dlgPaddingX;
6828 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006829# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006830 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006832# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006833 garray_T ga;
6834 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006835 int dlg_icon_width;
6836 int dlg_icon_height;
6837 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006839# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006840 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006841# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006842 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006843# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006844 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006845 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
Bram Moolenaar748bf032005-02-02 23:04:36 +00006848 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006849 {
6850 load_dpi_func();
6851 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006852 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006853 }
6854 else
6855 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856
6857 if ((type < 0) || (type > VIM_LAST_TYPE))
6858 type = 0;
6859
Bram Moolenaar734a8672019-12-02 22:49:38 +01006860 // allocate some memory for dialog template
6861 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006862 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006863 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865 if (p == NULL)
6866 return -1;
6867
6868 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006869 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6871 * const.
6872 */
6873 tbuffer = vim_strsave(buttons);
6874 if (tbuffer == NULL)
6875 return -1;
6876
Bram Moolenaar734a8672019-12-02 22:49:38 +01006877 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
Bram Moolenaar734a8672019-12-02 22:49:38 +01006879 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 numButtons = 1;
6881 for (i = 0; tbuffer[i] != '\0'; i++)
6882 {
6883 if (tbuffer[i] == DLG_BUTTON_SEP)
6884 numButtons++;
6885 }
6886 if (dfltbutton >= numButtons)
6887 dfltbutton = -1;
6888
Bram Moolenaar734a8672019-12-02 22:49:38 +01006889 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006890 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 if (buttonWidths == NULL)
6892 return -1;
6893
Bram Moolenaar734a8672019-12-02 22:49:38 +01006894 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006895 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 if (buttonPositions == NULL)
6897 return -1;
6898
6899 /*
6900 * Calculate how big the dialog must be.
6901 */
6902 hwnd = GetDesktopWindow();
6903 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006904# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6906 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006907 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 use_lfSysmenu = TRUE;
6909 }
6910 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006911# endif
K.Takatad1c58992022-01-23 12:31:57 +00006912 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6913 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6914
6915 oldFont = SelectFont(hdc, font);
6916 dlgPaddingX = DLG_PADDING_X;
6917 dlgPaddingY = DLG_PADDING_Y;
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 GetTextMetrics(hdc, &fontInfo);
6920 fontHeight = fontInfo.tmHeight;
6921
Bram Moolenaar734a8672019-12-02 22:49:38 +01006922 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006923 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
Bram Moolenaar734a8672019-12-02 22:49:38 +01006925 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006926 if (s_hwnd == NULL)
6927 {
6928 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
Bram Moolenaar734a8672019-12-02 22:49:38 +01006930 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006931 get_work_area(&workarea_rect);
6932 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006933 if (maxDialogWidth > adjust_by_system_dpi(600))
6934 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006935 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006936 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006937 }
6938 else
6939 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006940 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02006941 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006942 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006943 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
6944 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
6945 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
6946 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00006947
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006948 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006949 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
6950 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
6951 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
6952 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
6953 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006954 }
6955
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // Set dlgwidth to width of message.
6957 // Copy the message into "ga", changing NL to CR-NL and inserting line
6958 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 pstart = message;
6960 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006961 msgheight = 0;
6962 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 do
6964 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006965 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006966
Bram Moolenaar734a8672019-12-02 22:49:38 +01006967 // Need to figure out where to break the string. The system does it
6968 // at a word boundary, which would mean we can't compute the number of
6969 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006970 textWidth = 0;
6971 last_white = NULL;
6972 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00006973 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006974 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01006975 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006976 && textWidth > maxDialogWidth * 3 / 4)
6977 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02006978 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006979 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00006980 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006981 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006982 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006983 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006984 textWidth = 0;
6985
6986 if (last_white != NULL)
6987 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006988 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00006989 if (pend > last_white)
6990 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006991 pend = last_white + 1;
6992 last_white = NULL;
6993 }
6994 ga_append(&ga, '\r');
6995 ga_append(&ga, '\n');
6996 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006997 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006998
6999 while (--l >= 0)
7000 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007001 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007002 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007004
7005 ga_append(&ga, '\r');
7006 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 pstart = pend + 1;
7008 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007009
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007010 if (ga.ga_data != NULL)
7011 message = ga.ga_data;
7012
Bram Moolenaar734a8672019-12-02 22:49:38 +01007013 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007014
K.Takatac81e9bf2022-01-16 14:15:49 +00007015 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7016 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
K.Takatac81e9bf2022-01-16 14:15:49 +00007018 // Add width of icon to dlgwidth, and some space
7019 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7020 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7021
7022 if (msgheight < dlg_icon_height)
7023 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 /*
7026 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007027 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007029 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (!vertical)
7031 {
7032 // Place buttons horizontally if they fit.
7033 horizWidth = dlgPaddingX;
7034 pstart = tbuffer;
7035 i = 0;
7036 do
7037 {
7038 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7039 if (pend == NULL)
7040 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007041 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (textWidth < minButtonWidth)
7043 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007044 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 buttonWidths[i] = textWidth;
7046 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007047 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 pstart = pend + 1;
7049 } while (*pend != NUL);
7050
7051 if (horizWidth > maxDialogWidth)
7052 vertical = TRUE; // Too wide to fit on the screen.
7053 else if (horizWidth > dlgwidth)
7054 dlgwidth = horizWidth;
7055 }
7056
7057 if (vertical)
7058 {
7059 // Stack buttons vertically.
7060 pstart = tbuffer;
7061 do
7062 {
7063 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7064 if (pend == NULL)
7065 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007066 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007067 textWidth += dlgPaddingX; // Padding within button
7068 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 if (textWidth > dlgwidth)
7070 dlgwidth = textWidth;
7071 pstart = pend + 1;
7072 } while (*pend != NUL);
7073 }
7074
7075 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007076 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
Bram Moolenaar734a8672019-12-02 22:49:38 +01007078 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007079 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 add_long(lStyle);
7082 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007083 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 add_word(0); // NumberOfItems(will change later)
7085 add_word(10); // x
7086 add_word(10); // y
7087 add_word(PixelToDialogX(dlgwidth)); // cx
7088
7089 // Dialog height.
7090 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007091 dlgheight = msgheight + 2 * dlgPaddingY
7092 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 else
7094 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7095
7096 // Dialog needs to be taller if contains an edit box.
7097 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7098 if (textfield != NULL)
7099 dlgheight += editboxheight;
7100
Bram Moolenaar734a8672019-12-02 22:49:38 +01007101 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007102 if (dlgheight > maxDialogHeight)
7103 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007104 msgheight = msgheight - (dlgheight - maxDialogHeight);
7105 dlgheight = maxDialogHeight;
7106 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007107 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007108 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007109 }
7110
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 add_word(PixelToDialogY(dlgheight));
7112
7113 add_word(0); // Menu
7114 add_word(0); // Class
7115
Bram Moolenaar734a8672019-12-02 22:49:38 +01007116 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007117 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7118 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 p += nchar;
7120
K.Takatad1c58992022-01-23 12:31:57 +00007121 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007122# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007123 if (use_lfSysmenu)
7124 {
7125 // point size
7126 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7127 GetDeviceCaps(hdc, LOGPIXELSY));
7128 wcscpy(p, lfSysmenu.lfFaceName);
7129 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
K.Takatad1c58992022-01-23 12:31:57 +00007131 else
7132# endif
7133 {
7134 *p++ = DLG_FONT_POINT_SIZE; // point size
7135 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7136 }
7137 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 buttonYpos = msgheight + 2 * dlgPaddingY;
7140
7141 if (textfield != NULL)
7142 buttonYpos += editboxheight;
7143
7144 pstart = tbuffer;
7145 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007146 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 for (i = 0; i < numButtons; i++)
7148 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007149 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 for ( pend = pstart;
7151 *pend && (*pend != DLG_BUTTON_SEP);
7152 pend++)
7153 ;
7154
7155 if (*pend)
7156 *pend = '\0';
7157
7158 /*
7159 * old NOTE:
7160 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7161 * the focus to the first tab-able button and in so doing makes that
7162 * the default!! Grrr. Workaround: Make the default button the only
7163 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7164 * he/she can use arrow keys.
7165 *
7166 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007167 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * dialog. Also needed for when the textfield is the default control.
7169 * It appears to work now (perhaps not on Win95?).
7170 */
7171 if (vertical)
7172 {
7173 p = add_dialog_element(p,
7174 (i == dfltbutton
7175 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7176 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007177 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 + 2 * fontHeight * i),
7179 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7180 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007181 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 }
7183 else
7184 {
7185 p = add_dialog_element(p,
7186 (i == dfltbutton
7187 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7188 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007189 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 PixelToDialogX(buttonWidths[i]),
7191 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007192 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007194 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 }
7196 *pnumitems += numButtons;
7197
Bram Moolenaar734a8672019-12-02 22:49:38 +01007198 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 p = add_dialog_element(p, SS_ICON,
7200 PixelToDialogX(dlgPaddingX),
7201 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007202 PixelToDialogX(dlg_icon_width),
7203 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7205 dlg_icons[type]);
7206
Bram Moolenaar734a8672019-12-02 22:49:38 +01007207 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007208 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007209 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007210 PixelToDialogY(dlgPaddingY),
7211 (WORD)(PixelToDialogX(messageWidth) + 1),
7212 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007213 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar734a8672019-12-02 22:49:38 +01007215 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (textfield != NULL)
7217 {
7218 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7219 PixelToDialogX(2 * dlgPaddingX),
7220 PixelToDialogY(2 * dlgPaddingY + msgheight),
7221 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7222 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007223 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 *pnumitems += 1;
7225 }
7226
7227 *pnumitems += 2;
7228
7229 SelectFont(hdc, oldFont);
7230 DeleteObject(font);
7231 ReleaseDC(hwnd, hdc);
7232
Bram Moolenaar734a8672019-12-02 22:49:38 +01007233 // Let the dialog_callback() function know which button to make default
7234 // If we have an edit box, make that the default. We also need to tell
7235 // dialog_callback() if this dialog contains an edit box or not. We do
7236 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (textfield != NULL)
7238 {
7239 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7240 s_textfield = textfield;
7241 }
7242 else
7243 {
7244 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7245 s_textfield = NULL;
7246 }
7247
Bram Moolenaar734a8672019-12-02 22:49:38 +01007248 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007250 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 (LPDLGTEMPLATE)pdlgtemplate,
7252 s_hwnd,
7253 (DLGPROC)dialog_callback);
7254
7255 LocalFree(LocalHandle(pdlgtemplate));
7256 vim_free(tbuffer);
7257 vim_free(buttonWidths);
7258 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007259 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar734a8672019-12-02 22:49:38 +01007261 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 (void)SetFocus(s_hwnd);
7263
7264 return nchar;
7265}
7266
Bram Moolenaar734a8672019-12-02 22:49:38 +01007267#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007268
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269/*
7270 * Put a simple element (basic class) onto a dialog template in memory.
7271 * return a pointer to where the next item should be added.
7272 *
7273 * parameters:
7274 * lStyle = additional style flags
7275 * (be careful, NT3.51 & Win32s will ignore the new ones)
7276 * x,y = x & y positions IN DIALOG UNITS
7277 * w,h = width and height IN DIALOG UNITS
7278 * Id = ID used in messages
7279 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7280 * caption = usually text or resource name
7281 *
7282 * TODO: use the length information noted here to enable the dialog creation
7283 * routines to work out more exactly how much memory they need to alloc.
7284 */
7285 static PWORD
7286add_dialog_element(
7287 PWORD p,
7288 DWORD lStyle,
7289 WORD x,
7290 WORD y,
7291 WORD w,
7292 WORD h,
7293 WORD Id,
7294 WORD clss,
7295 const char *caption)
7296{
7297 int nchar;
7298
Bram Moolenaar734a8672019-12-02 22:49:38 +01007299 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7301 *p++ = LOWORD(lStyle);
7302 *p++ = HIWORD(lStyle);
7303 *p++ = 0; // LOWORD (lExtendedStyle)
7304 *p++ = 0; // HIWORD (lExtendedStyle)
7305 *p++ = x;
7306 *p++ = y;
7307 *p++ = w;
7308 *p++ = h;
7309 *p++ = Id; //9 or 10 words in all
7310
7311 *p++ = (WORD)0xffff;
7312 *p++ = clss; //2 more here
7313
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007314 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 p += nchar;
7316
7317 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7318
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007319 return p; // total = 15 + strlen(caption) words
7320 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321}
7322
7323
7324/*
7325 * Helper routine. Take an input pointer, return closest pointer that is
7326 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7327 */
7328 static LPWORD
7329lpwAlign(
7330 LPWORD lpIn)
7331{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007332 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007334 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 ul += 3;
7336 ul >>= 2;
7337 ul <<= 2;
7338 return (LPWORD)ul;
7339}
7340
7341/*
7342 * Helper routine. Takes second parameter as Ansi string, copies it to first
7343 * parameter as wide character (16-bits / char) string, and returns integer
7344 * number of wide characters (words) in string (including the trailing wide
7345 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007346 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7347 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 static int
7349nCopyAnsiToWideChar(
7350 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007351 LPSTR lpAnsiIn,
7352 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353{
7354 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007355 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 int i;
7357 WCHAR *wn;
7358
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007359 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007362 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (wn != NULL)
7364 {
7365 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 vim_free(wn);
7368 }
7369 }
7370 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007371 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 nChar = MultiByteToWideChar(
7373 enc_codepage > 0 ? enc_codepage : CP_ACP,
7374 MB_PRECOMPOSED,
7375 lpAnsiIn, len,
7376 lpWCStr, len);
7377 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007378 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380
7381 return nChar;
7382}
7383
7384
7385#ifdef FEAT_TEAROFF
7386/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007387 * Lookup menu handle from "menu_id".
7388 */
7389 static HMENU
7390tearoff_lookup_menuhandle(
7391 vimmenu_T *menu,
7392 WORD menu_id)
7393{
7394 for ( ; menu != NULL; menu = menu->next)
7395 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007396 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007397 continue;
7398 if (menu_is_separator(menu->dname))
7399 continue;
7400 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7401 return menu->submenu_id;
7402 }
7403 return NULL;
7404}
7405
7406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 * The callback function for all the modeless dialogs that make up the
7408 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7409 * thinking its menus have been clicked), and go away when closed.
7410 */
7411 static LRESULT CALLBACK
7412tearoff_callback(
7413 HWND hwnd,
7414 UINT message,
7415 WPARAM wParam,
7416 LPARAM lParam)
7417{
7418 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007419 {
7420 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
Bram Moolenaar734a8672019-12-02 22:49:38 +01007424 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 HandleMouseHide(message, lParam);
7426
7427 if (message == WM_COMMAND)
7428 {
7429 if ((WORD)(LOWORD(wParam)) & 0x8000)
7430 {
7431 POINT mp;
7432 RECT rect;
7433
7434 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7435 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007436 vimmenu_T *menu;
7437
7438 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007440 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7442 (int)rect.right - 8,
7443 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007444 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 s_hwnd,
7446 NULL);
7447 /*
7448 * NOTE: The pop-up menu can eat the mouse up event.
7449 * We deal with this in normal.c.
7450 */
7451 }
7452 }
7453 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007454 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7456 /*
7457 * Give main window the focus back: this is so after
7458 * choosing a tearoff button you can start typing again
7459 * straight away.
7460 */
7461 (void)SetFocus(s_hwnd);
7462 return TRUE;
7463 }
7464 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7465 {
7466 DestroyWindow(hwnd);
7467 return TRUE;
7468 }
7469
Bram Moolenaar734a8672019-12-02 22:49:38 +01007470 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (message == WM_EXITSIZEMOVE)
7472 (void)SetActiveWindow(s_hwnd);
7473
7474 return FALSE;
7475}
7476#endif
7477
7478
7479/*
K.Takatad1c58992022-01-23 12:31:57 +00007480 * Computes the dialog base units based on the current dialog font.
7481 * We don't use the GetDialogBaseUnits() API, because we don't use the
7482 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 */
7484 static void
7485get_dialog_font_metrics(void)
7486{
7487 HDC hdc;
7488 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 SIZE size;
7490#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007491 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492#endif
7493
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007495 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007496 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498#endif
7499 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007500 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
K.Takatad1c58992022-01-23 12:31:57 +00007502 hdc = GetDC(s_hwnd);
7503 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007504 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007505 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506
K.Takataabe628e2022-01-23 16:25:17 +00007507 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007508 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
7511#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7512/*
7513 * Create a pseudo-"tearoff menu" based on the child
7514 * items of a given menu pointer.
7515 */
7516 static void
7517gui_mch_tearoff(
7518 char_u *title,
7519 vimmenu_T *menu,
7520 int initX,
7521 int initY)
7522{
7523 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7524 int template_len;
7525 int nchar, textWidth, submenuWidth;
7526 DWORD lStyle;
7527 DWORD lExtendedStyle;
7528 WORD dlgwidth;
7529 WORD menuID;
7530 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007531 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 vimmenu_T *the_menu = menu;
7533 HWND hwnd;
7534 HDC hdc;
7535 HFONT font, oldFont;
7536 int col, spaceWidth, len;
7537 int columnWidths[2];
7538 char_u *label, *text;
7539 int acLen = 0;
7540 int nameLen;
7541 int padding0, padding1, padding2 = 0;
7542 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007543 int x;
7544 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007545# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007546 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549
7550 /*
7551 * If this menu is already torn off, move it to the mouse position.
7552 */
7553 if (IsWindow(menu->tearoff_handle))
7554 {
7555 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007556 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 {
7558 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7559 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7560 }
7561 return;
7562 }
7563
7564 /*
7565 * Create a new tearoff.
7566 */
7567 if (*title == MNU_HIDDEN_CHAR)
7568 title++;
7569
Bram Moolenaar734a8672019-12-02 22:49:38 +01007570 // Allocate memory to store the dialog template. It's made bigger when
7571 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 template_len = DLG_ALLOC_SIZE;
7573 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7574 if (p == NULL)
7575 return;
7576
7577 hwnd = GetDesktopWindow();
7578 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007579# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7581 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007582 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 use_lfSysmenu = TRUE;
7584 }
7585 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007586# endif
K.Takatad1c58992022-01-23 12:31:57 +00007587 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7588 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7589
7590 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591
Bram Moolenaar734a8672019-12-02 22:49:38 +01007592 // Calculate width of a single space. Used for padding columns to the
7593 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007594 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
Bram Moolenaar734a8672019-12-02 22:49:38 +01007596 // Figure out max width of the text column, the accelerator column and the
7597 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 submenuWidth = 0;
7599 for (col = 0; col < 2; col++)
7600 {
7601 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007602 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007604 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 text = (col == 0) ? pmenu->dname : pmenu->actext;
7606 if (text != NULL && *text != NUL)
7607 {
7608 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7609 if (textWidth > columnWidths[col])
7610 columnWidths[col] = textWidth;
7611 }
7612 if (pmenu->children != NULL)
7613 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7614 }
7615 }
7616 if (columnWidths[1] == 0)
7617 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007618 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (submenuWidth != 0)
7620 columnWidths[0] += submenuWidth;
7621 else
7622 columnWidths[0] += spaceWidth;
7623 }
7624 else
7625 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007626 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7628 columnWidths[1] += submenuWidth;
7629 }
7630
7631 /*
7632 * Now find the total width of our 'menu'.
7633 */
7634 textWidth = columnWidths[0] + columnWidths[1];
7635 if (submenuWidth != 0)
7636 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007637 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7639 textWidth += submenuWidth;
7640 }
7641 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7642 if (textWidth > dlgwidth)
7643 dlgwidth = textWidth;
7644 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7645
Bram Moolenaar734a8672019-12-02 22:49:38 +01007646 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007647 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648
7649 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7650 *p++ = LOWORD(lStyle);
7651 *p++ = HIWORD(lStyle);
7652 *p++ = LOWORD(lExtendedStyle);
7653 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007654 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007656 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007658 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 else
7660 *p++ = PixelToDialogX(initX); // x
7661 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007662 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 else
7664 *p++ = PixelToDialogY(initY); // y
7665 *p++ = PixelToDialogX(dlgwidth); // cx
7666 ptrueheight = p;
7667 *p++ = 0; // dialog height: changed later anyway
7668 *p++ = 0; // Menu
7669 *p++ = 0; // Class
7670
Bram Moolenaar734a8672019-12-02 22:49:38 +01007671 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007673 ? (LPSTR)title
7674 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 p += nchar;
7676
K.Takatad1c58992022-01-23 12:31:57 +00007677 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007678# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007679 if (use_lfSysmenu)
7680 {
7681 // point size
7682 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7683 GetDeviceCaps(hdc, LOGPIXELSY));
7684 wcscpy(p, lfSysmenu.lfFaceName);
7685 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
K.Takatad1c58992022-01-23 12:31:57 +00007687 else
7688# endif
7689 {
7690 *p++ = DLG_FONT_POINT_SIZE; // point size
7691 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7692 }
7693 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694
7695 /*
7696 * Loop over all the items in the menu.
7697 * But skip over the tearbar.
7698 */
7699 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7700 menu = menu->children->next;
7701 else
7702 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007703 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 for ( ; menu != NULL; menu = menu->next)
7705 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007706 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 continue;
7708 if (menu_is_separator(menu->dname))
7709 {
7710 sepPadding += 3;
7711 continue;
7712 }
7713
Bram Moolenaar734a8672019-12-02 22:49:38 +01007714 // Check if there still is plenty of room in the template. Make it
7715 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7717 {
7718 WORD *newp;
7719
7720 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7721 if (newp != NULL)
7722 {
7723 template_len += 4096;
7724 mch_memmove(newp, pdlgtemplate,
7725 (char *)p - (char *)pdlgtemplate);
7726 p = newp + (p - pdlgtemplate);
7727 pnumitems = newp + (pnumitems - pdlgtemplate);
7728 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7729 LocalFree(LocalHandle(pdlgtemplate));
7730 pdlgtemplate = newp;
7731 }
7732 }
7733
Bram Moolenaar734a8672019-12-02 22:49:38 +01007734 // Figure out minimal length of this menu label. Use "name" for the
7735 // actual text, "dname" for estimating the displayed size. "name"
7736 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 len = nameLen = (int)STRLEN(menu->name);
7738 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7739 (int)STRLEN(menu->dname))) / spaceWidth;
7740 len += padding0;
7741
7742 if (menu->actext != NULL)
7743 {
7744 acLen = (int)STRLEN(menu->actext);
7745 len += acLen;
7746 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7747 }
7748 else
7749 textWidth = 0;
7750 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7751 len += padding1;
7752
7753 if (menu->children == NULL)
7754 {
7755 padding2 = submenuWidth / spaceWidth;
7756 len += padding2;
7757 menuID = (WORD)(menu->id);
7758 }
7759 else
7760 {
7761 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007762 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
7764
Bram Moolenaar734a8672019-12-02 22:49:38 +01007765 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007766 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 if (label == NULL)
7768 break;
7769
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007770 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007771 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007773 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 while (padding0-- > 0)
7775 *text++ = ' ';
7776 if (menu->actext != NULL)
7777 {
7778 STRNCPY(text, menu->actext, acLen);
7779 text += acLen;
7780 }
7781 while (padding1-- > 0)
7782 *text++ = ' ';
7783 if (menu->children != NULL)
7784 {
7785 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7786 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7787 }
7788 else
7789 {
7790 while (padding2-- > 0)
7791 *text++ = ' ';
7792 }
7793 *text = NUL;
7794
7795 /*
7796 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7797 * W95/NT4 it makes the tear-off look more like a menu.
7798 */
7799 p = add_dialog_element(p,
7800 BS_PUSHBUTTON|BS_LEFT,
7801 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7802 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7803 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7804 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007805 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 vim_free(label);
7807 (*pnumitems)++;
7808 }
7809
7810 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7811
7812
Bram Moolenaar734a8672019-12-02 22:49:38 +01007813 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007814 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007815 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 (LPDLGTEMPLATE)pdlgtemplate,
7817 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007818 (DLGPROC)tearoff_callback,
7819 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821 LocalFree(LocalHandle(pdlgtemplate));
7822 SelectFont(hdc, oldFont);
7823 DeleteObject(font);
7824 ReleaseDC(hwnd, hdc);
7825
7826 /*
7827 * Reassert ourselves as the active window. This is so that after creating
7828 * a tearoff, the user doesn't have to click with the mouse just to start
7829 * typing again!
7830 */
7831 (void)SetActiveWindow(s_hwnd);
7832
Bram Moolenaar734a8672019-12-02 22:49:38 +01007833 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 force_menu_update = TRUE;
7835}
7836#endif
7837
7838#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007839# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841/*
7842 * Create the toolbar, initially unpopulated.
7843 * (just like the menu, there are no defaults, it's all
7844 * set up through menu.vim)
7845 */
7846 static void
7847initialise_toolbar(void)
7848{
7849 InitCommonControls();
7850 s_toolbarhwnd = CreateToolbarEx(
7851 s_hwnd,
7852 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7853 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007854 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007855 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 IDR_TOOLBAR1, // id of initial bitmap
7857 NULL,
7858 0, // initial number of buttons
7859 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7860 TOOLBAR_BUTTON_HEIGHT,
7861 TOOLBAR_BUTTON_WIDTH,
7862 TOOLBAR_BUTTON_HEIGHT,
7863 sizeof(TBBUTTON)
7864 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007865
7866 // Remove transparency from the toolbar to prevent the main window
7867 // background colour showing through
7868 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7869 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7870
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007871 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872
7873 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007874
7875 update_toolbar_size();
7876}
7877
7878 static void
7879update_toolbar_size(void)
7880{
7881 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007882 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007883
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007884 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007885 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7886 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7887 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7888 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7889
7890 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7891 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7892 //TRACE("button size: %d, %d", w, h);
7893 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7894 gui.toolbar_height = h + 6;
7895
7896 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7897 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7898
7899 // TODO:
7900 // Currently, this function only updates the size of toolbar buttons.
7901 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902}
7903
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007904 static LRESULT CALLBACK
7905toolbar_wndproc(
7906 HWND hwnd,
7907 UINT uMsg,
7908 WPARAM wParam,
7909 LPARAM lParam)
7910{
7911 HandleMouseHide(uMsg, lParam);
7912 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7913}
7914
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 static int
7916get_toolbar_bitmap(vimmenu_T *menu)
7917{
7918 int i = -1;
7919
7920 /*
7921 * Check user bitmaps first, unless builtin is specified.
7922 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007923 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {
7925 char_u fname[MAXPATHL];
7926 HANDLE hbitmap = NULL;
7927
7928 if (menu->iconfile != NULL)
7929 {
7930 gui_find_iconfile(menu->iconfile, fname, "bmp");
7931 hbitmap = LoadImage(
7932 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007933 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 IMAGE_BITMAP,
7935 TOOLBAR_BUTTON_WIDTH,
7936 TOOLBAR_BUTTON_HEIGHT,
7937 LR_LOADFROMFILE |
7938 LR_LOADMAP3DCOLORS
7939 );
7940 }
7941
7942 /*
7943 * If the LoadImage call failed, or the "icon=" file
7944 * didn't exist or wasn't specified, try the menu name
7945 */
7946 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007947 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007948# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007949 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007950# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02007951 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 hbitmap = LoadImage(
7953 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007954 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 IMAGE_BITMAP,
7956 TOOLBAR_BUTTON_WIDTH,
7957 TOOLBAR_BUTTON_HEIGHT,
7958 LR_LOADFROMFILE |
7959 LR_LOADMAP3DCOLORS
7960 );
7961
7962 if (hbitmap != NULL)
7963 {
7964 TBADDBITMAP tbAddBitmap;
7965
7966 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007967 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
7969 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
7970 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007971 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 }
7973 }
7974 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
7975 i = menu->iconidx;
7976
7977 return i;
7978}
7979#endif
7980
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007981#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
7982 static void
7983initialise_tabline(void)
7984{
7985 InitCommonControls();
7986
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007987 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007988 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007989 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007990 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007991 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007992
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007993 gui.tabline_height = TABLINE_HEIGHT;
7994
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00007995 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00007996}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007997
Bram Moolenaarca05aa22017-10-22 15:36:14 +02007998/*
7999 * Get tabpage_T from POINT.
8000 */
8001 static tabpage_T *
8002GetTabFromPoint(
8003 HWND hWnd,
8004 POINT pt)
8005{
8006 tabpage_T *ptp = NULL;
8007
8008 if (gui_mch_showing_tabline())
8009 {
8010 TCHITTESTINFO htinfo;
8011 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008012 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008013 if (s_tabhwnd == hWnd)
8014 {
8015 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8016 if (idx != -1)
8017 ptp = find_tabpage(idx + 1);
8018 }
8019 }
8020 return ptp;
8021}
8022
8023static POINT s_pt = {0, 0};
8024static HCURSOR s_hCursor = NULL;
8025
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008026 static LRESULT CALLBACK
8027tabline_wndproc(
8028 HWND hwnd,
8029 UINT uMsg,
8030 WPARAM wParam,
8031 LPARAM lParam)
8032{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008033 POINT pt;
8034 tabpage_T *tp;
8035 RECT rect;
8036 int nCenter;
8037 int idx0;
8038 int idx1;
8039
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008040 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008041
8042 switch (uMsg)
8043 {
8044 case WM_LBUTTONDOWN:
8045 {
8046 s_pt.x = GET_X_LPARAM(lParam);
8047 s_pt.y = GET_Y_LPARAM(lParam);
8048 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008049 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008050 break;
8051 }
8052 case WM_MOUSEMOVE:
8053 if (GetCapture() == hwnd
8054 && ((wParam & MK_LBUTTON)) != 0)
8055 {
8056 pt.x = GET_X_LPARAM(lParam);
8057 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008058 if (abs(pt.x - s_pt.x) >
8059 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008060 {
8061 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8062
8063 tp = GetTabFromPoint(hwnd, pt);
8064 if (tp != NULL)
8065 {
8066 idx0 = tabpage_index(curtab) - 1;
8067 idx1 = tabpage_index(tp) - 1;
8068
8069 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8070 nCenter = rect.left + (rect.right - rect.left) / 2;
8071
Bram Moolenaar734a8672019-12-02 22:49:38 +01008072 // Check if the mouse cursor goes over the center of
8073 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008074 if ((idx0 < idx1) && (nCenter < pt.x))
8075 {
8076 tabpage_move(idx1 + 1);
8077 update_screen(0);
8078 }
8079 else if ((idx1 < idx0) && (pt.x < nCenter))
8080 {
8081 tabpage_move(idx1);
8082 update_screen(0);
8083 }
8084 }
8085 }
8086 }
8087 break;
8088 case WM_LBUTTONUP:
8089 {
8090 if (GetCapture() == hwnd)
8091 {
8092 SetCursor(s_hCursor);
8093 ReleaseCapture();
8094 }
8095 break;
8096 }
regomne3bdef102022-09-26 20:48:32 +01008097 case WM_MBUTTONUP:
8098 {
8099 TCHITTESTINFO htinfo;
8100
8101 htinfo.pt.x = GET_X_LPARAM(lParam);
8102 htinfo.pt.y = GET_Y_LPARAM(lParam);
8103 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8104 if (idx0 != -1)
8105 {
8106 idx0 += 1;
8107 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8108 }
8109 break;
8110 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008111 default:
8112 break;
8113 }
8114
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008115 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8116}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008117#endif
8118
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8120/*
8121 * Make the GUI window come to the foreground.
8122 */
8123 void
8124gui_mch_set_foreground(void)
8125{
8126 if (IsIconic(s_hwnd))
8127 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8128 SetForegroundWindow(s_hwnd);
8129}
8130#endif
8131
8132#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8133 static void
8134dyn_imm_load(void)
8135{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008136 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (hLibImm == NULL)
8138 return;
8139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008141 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008143 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008145 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008147 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008149 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008151 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008152 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008153 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008154 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008155 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008157 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008159 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008160 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008161 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162
K.Takatab0b2b732022-01-19 12:59:21 +00008163 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 || pImmGetContext == NULL
8165 || pImmAssociateContext == NULL
8166 || pImmReleaseContext == NULL
8167 || pImmGetOpenStatus == NULL
8168 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008169 || pImmGetCompositionFontW == NULL
8170 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008172 || pImmGetConversionStatus == NULL
8173 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
8175 FreeLibrary(hLibImm);
8176 hLibImm = NULL;
8177 pImmGetContext = NULL;
8178 return;
8179 }
8180
8181 return;
8182}
8183
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#endif
8185
8186#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8187
8188# ifdef FEAT_XPM_W32
8189# define IMAGE_XPM 100
8190# endif
8191
8192typedef struct _signicon_t
8193{
8194 HANDLE hImage;
8195 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008196# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008197 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199} signicon_t;
8200
8201 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008202gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203{
8204 signicon_t *sign;
8205 int x, y, w, h;
8206
8207 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8208 return;
8209
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008210# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008211 if (IS_ENABLE_DIRECTX())
8212 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008213# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008214
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 x = TEXT_X(col);
8216 y = TEXT_Y(row);
8217 w = gui.char_width * 2;
8218 h = gui.char_height;
8219 switch (sign->uType)
8220 {
8221 case IMAGE_BITMAP:
8222 {
8223 HDC hdcMem;
8224 HBITMAP hbmpOld;
8225
8226 hdcMem = CreateCompatibleDC(s_hdc);
8227 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8228 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8229 SelectObject(hdcMem, hbmpOld);
8230 DeleteDC(hdcMem);
8231 }
8232 break;
8233 case IMAGE_ICON:
8234 case IMAGE_CURSOR:
8235 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8236 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008237# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 case IMAGE_XPM:
8239 {
8240 HDC hdcMem;
8241 HBITMAP hbmpOld;
8242
8243 hdcMem = CreateCompatibleDC(s_hdc);
8244 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008245 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8247
8248 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008249 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8251 SelectObject(hdcMem, hbmpOld);
8252 DeleteDC(hdcMem);
8253 }
8254 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 }
8257}
8258
8259 static void
8260close_signicon_image(signicon_t *sign)
8261{
8262 if (sign)
8263 switch (sign->uType)
8264 {
8265 case IMAGE_BITMAP:
8266 DeleteObject((HGDIOBJ)sign->hImage);
8267 break;
8268 case IMAGE_CURSOR:
8269 DestroyCursor((HCURSOR)sign->hImage);
8270 break;
8271 case IMAGE_ICON:
8272 DestroyIcon((HICON)sign->hImage);
8273 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008274# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 case IMAGE_XPM:
8276 DeleteObject((HBITMAP)sign->hImage);
8277 DeleteObject((HBITMAP)sign->hShape);
8278 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 }
8281}
8282
8283 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008284gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
8286 signicon_t sign, *psign;
8287 char_u *ext;
8288
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008290 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 if (ext > signfile)
8292 {
8293 int do_load = 1;
8294
8295 if (!STRICMP(ext, ".bmp"))
8296 sign.uType = IMAGE_BITMAP;
8297 else if (!STRICMP(ext, ".ico"))
8298 sign.uType = IMAGE_ICON;
8299 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8300 sign.uType = IMAGE_CURSOR;
8301 else
8302 do_load = 0;
8303
8304 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008305 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 gui.char_width * 2, gui.char_height,
8307 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008308# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 if (!STRICMP(ext, ".xpm"))
8310 {
8311 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008312 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8313 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008315# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317
8318 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008319 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 *psign = sign;
8321
8322 if (!psign)
8323 {
8324 if (sign.hImage)
8325 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008326 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 }
8328 return (void *)psign;
8329
8330}
8331
8332 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008333gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 if (sign)
8336 {
8337 close_signicon_image((signicon_t *)sign);
8338 vim_free(sign);
8339 }
8340}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008343#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344
Bram Moolenaar734a8672019-12-02 22:49:38 +01008345/*
8346 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008347 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008349 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8351 * to get current mouse position).
8352 *
8353 * Trying to use as more Windows services as possible, and as less
8354 * IE version as possible :)).
8355 *
8356 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8357 * BalloonEval struct.
8358 * 2) Enable/Disable simply create/kill BalloonEval Timer
8359 * 3) When there was enough inactivity, timer procedure posts
8360 * async request to debugger
8361 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8362 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008363 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 */
8365
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008366 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008367make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008368{
K.Takata76687d22022-01-25 10:31:37 +00008369 TOOLINFOW *pti;
8370 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008371
K.Takata76687d22022-01-25 10:31:37 +00008372 pti = alloc(sizeof(TOOLINFOW));
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008373 if (pti == NULL)
8374 return;
8375
8376 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8377 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8378 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008379 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008380
8381 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8382 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8383
K.Takata76687d22022-01-25 10:31:37 +00008384 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008385 pti->uFlags = TTF_SUBCLASS;
8386 pti->hwnd = beval->target;
8387 pti->hinst = 0; // Don't use string resources
8388 pti->uId = ID_BEVAL_TOOLTIP;
8389
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008390 pti->lpszText = LPSTR_TEXTCALLBACKW;
8391 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8392 pti->lParam = (LPARAM)beval->tofree;
8393 // switch multiline tooltips on
8394 if (GetClientRect(s_textArea, &rect))
8395 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8396 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008397
8398 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8399 pti->rect.left = pt.x - 3;
8400 pti->rect.top = pt.y - 3;
8401 pti->rect.right = pt.x + 3;
8402 pti->rect.bottom = pt.y + 3;
8403
8404 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8405 // Make tooltip appear sooner.
8406 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8407 // I've performed some tests and it seems the longest possible life time
8408 // of tooltip is 30 seconds.
8409 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8410 /*
8411 * HACK: force tooltip to appear, because it'll not appear until
8412 * first mouse move. D*mn M$
8413 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8414 */
8415 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8416 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8417 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008418}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008421delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008423 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
8426 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008427beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008428 HWND hwnd UNUSED,
8429 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008430 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008431 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 POINT pt;
8434 RECT rect;
8435
8436 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8437 return;
8438
8439 GetCursorPos(&pt);
8440 if (WindowFromPoint(pt) != s_textArea)
8441 return;
8442
8443 ScreenToClient(s_textArea, &pt);
8444 GetClientRect(s_textArea, &rect);
8445 if (!PtInRect(&rect, pt))
8446 return;
8447
K.Takataa8ec4912022-02-03 14:32:33 +00008448 if (last_user_activity > 0
8449 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 && (cur_beval->showState != ShS_PENDING
8451 || abs(cur_beval->x - pt.x) > 3
8452 || abs(cur_beval->y - pt.y) > 3))
8453 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008454 // Pointer resting in one place long enough, it's time to show
8455 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 cur_beval->showState = ShS_PENDING;
8457 cur_beval->x = pt.x;
8458 cur_beval->y = pt.y;
8459
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (cur_beval->msgCB != NULL)
8461 (*cur_beval->msgCB)(cur_beval, 0);
8462 }
8463}
8464
8465 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008466gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467{
K.Takataa8ec4912022-02-03 14:32:33 +00008468 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469}
8470
8471 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008472gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 if (beval == NULL)
8475 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008476 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8477 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478}
8479
8480 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008481gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482{
8483 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008484
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008485 vim_free(beval->msg);
8486 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8487 if (beval->msg == NULL)
8488 {
8489 delete_tooltip(beval);
8490 beval->showState = ShS_NEUTRAL;
8491 return;
8492 }
8493
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 if (beval->showState == ShS_SHOWING)
8495 return;
8496 GetCursorPos(&pt);
8497 ScreenToClient(s_textArea, &pt);
8498
8499 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008501 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 gui_mch_disable_beval_area(cur_beval);
8503 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008504 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
8507
8508 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008509gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008510 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008511 char_u *mesg,
8512 void (*mesgCB)(BalloonEval *, int),
8513 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008515 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 BalloonEval *beval;
8517
8518 if (mesg != NULL && mesgCB != NULL)
8519 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00008520 iemsg(_(e_cannot_create_ballooneval_with_both_message_and_callback));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 return NULL;
8522 }
8523
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008524 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 if (beval != NULL)
8526 {
8527 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
8529 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 beval->msg = mesg;
8531 beval->msgCB = mesgCB;
8532 beval->clientData = clientData;
8533
8534 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 cur_beval = beval;
8536
8537 if (p_beval)
8538 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 }
8540 return beval;
8541}
8542
8543 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008544Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008546 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 return;
8548
8549 if (cur_beval != NULL)
8550 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008551 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008553 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008554 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008555 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 delete_tooltip(cur_beval);
8557 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008560 break;
8561 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008562 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008563 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008564 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008565 info->lpszText = (LPSTR)info->lParam;
8566 info->uFlags |= TTF_DI_SETITEM;
8567 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008568 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008569 case TTN_GETDISPINFOW:
8570 {
8571 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008572 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008573 info->lpszText = (LPWSTR)info->lParam;
8574 info->uFlags |= TTF_DI_SETITEM;
8575 }
8576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 }
8578 }
8579}
8580
8581 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008582track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8585 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008586 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587}
8588
8589 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008590gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008592# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008593 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008594# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008595 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 vim_free(beval);
8597}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008598#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599
8600#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8601/*
8602 * We have multiple signs to draw at the same location. Draw the
8603 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8604 */
8605 void
8606netbeans_draw_multisign_indicator(int row)
8607{
8608 int i;
8609 int y;
8610 int x;
8611
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008612 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008613 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008614
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 x = 0;
8616 y = TEXT_Y(row);
8617
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008618# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008619 if (IS_ENABLE_DIRECTX())
8620 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008621# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 for (i = 0; i < gui.char_height - 3; i++)
8624 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8625
8626 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8627 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8628 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8629 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8630 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8631 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8632 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8633}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008634#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008635
8636#if defined(FEAT_EVAL) || defined(PROTO)
8637 int
8638test_gui_w32_sendevent(dict_T *args)
8639{
8640 char_u *event;
8641 INPUT inputs[1];
8642
Bram Moolenaard61efa52022-07-23 09:52:04 +01008643 event = dict_get_string(args, "event", TRUE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008644 if (event == NULL)
8645 return FALSE;
8646
8647 ZeroMemory(inputs, sizeof(inputs));
8648
8649 if (STRICMP(event, "keydown") == 0 || STRICMP(event, "keyup") == 0)
8650 {
8651 WORD vkCode;
8652
Bram Moolenaard61efa52022-07-23 09:52:04 +01008653 vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008654 if (vkCode <= 0 || vkCode >= 0xFF)
8655 {
8656 semsg(_(e_invalid_argument_nr), (long)vkCode);
8657 return FALSE;
8658 }
8659
8660 inputs[0].type = INPUT_KEYBOARD;
8661 inputs[0].ki.wVk = vkCode;
8662 if (STRICMP(event, "keyup") == 0)
8663 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
K.Takatafef38d82022-09-07 19:03:42 +01008664 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008665 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
8666 }
8667 else
8668 semsg(_(e_invalid_argument_str), event);
8669
8670 vim_free(event);
8671
8672 return TRUE;
8673}
8674#endif