blob: 85adec67e9e54e9b748727807bfc3a94eae46724 [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
Ken Takata7086b3e2023-10-02 21:26:03 +0200229#ifndef WM_GETDPISCALEDSIZE
230# define WM_GETDPISCALEDSIZE 0x02E4
231#endif
232
LemonBoy365d8f72022-05-05 19:23:07 +0100233#ifndef WM_MOUSEHWHEEL
234# define WM_MOUSEHWHEEL 0x020E
235#endif
236
237#ifndef SPI_GETWHEELSCROLLCHARS
238# define SPI_GETWHEELSCROLLCHARS 0x006C
K.Takatac81e9bf2022-01-16 14:15:49 +0000239#endif
240
LemonBoyc27747e2022-05-07 12:25:40 +0100241#ifndef SPI_SETWHEELSCROLLCHARS
242# define SPI_SETWHEELSCROLLCHARS 0x006D
243#endif
244
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100245#ifdef PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100247 * Define a few things for generating prototypes. This is just to avoid
248 * syntax errors, the defines do not need to be correct.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100250# define APIENTRY
251# define CALLBACK
252# define CONST
253# define FAR
254# define NEAR
Bram Moolenaar945c8572020-07-17 22:17:03 +0200255# define WINAPI
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200256# undef _cdecl
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100257# define _cdecl
258typedef int BOOL;
259typedef int BYTE;
260typedef int DWORD;
261typedef int WCHAR;
262typedef int ENUMLOGFONT;
263typedef int FINDREPLACE;
264typedef int HANDLE;
265typedef int HBITMAP;
266typedef int HBRUSH;
267typedef int HDROP;
268typedef int INT;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100269typedef int LOGFONTW[];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100270typedef int LPARAM;
271typedef int LPCREATESTRUCT;
272typedef int LPCSTR;
273typedef int LPCTSTR;
274typedef int LPRECT;
275typedef int LPSTR;
276typedef int LPWINDOWPOS;
277typedef int LPWORD;
278typedef int LRESULT;
279typedef int HRESULT;
280# undef MSG
281typedef int MSG;
282typedef int NEWTEXTMETRIC;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100283typedef int NMHDR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100284typedef int OSVERSIONINFO;
285typedef int PWORD;
286typedef int RECT;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +0100287typedef int SIZE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100288typedef int UINT;
289typedef int WORD;
290typedef int WPARAM;
291typedef int POINT;
292typedef void *HINSTANCE;
293typedef void *HMENU;
294typedef void *HWND;
295typedef void *HDC;
296typedef void VOID;
297typedef int LPNMHDR;
298typedef int LONG;
299typedef int WNDPROC;
Bram Moolenaara6b7a082016-08-10 20:53:05 +0200300typedef int UINT_PTR;
Bram Moolenaarb1c91982018-05-17 17:04:55 +0200301typedef int COLORREF;
302typedef int HCURSOR;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100303#endif
304
K.Takata45f9cfb2022-01-21 11:11:00 +0000305static void _OnPaint(HWND hwnd);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100306static void fill_rect(const RECT *rcp, HBRUSH hbr, COLORREF color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100307static void clear_rect(RECT *rcp);
308
Bram Moolenaar734a8672019-12-02 22:49:38 +0100309static WORD s_dlgfntheight; // height of the dialog font
310static WORD s_dlgfntwidth; // width of the dialog font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100311
312#ifdef FEAT_MENU
313static HMENU s_menuBar = NULL;
314#endif
315#ifdef FEAT_TEAROFF
316static void rebuild_tearoff(vimmenu_T *menu);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100317static HBITMAP s_htearbitmap; // bitmap used to indicate tearoff
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100318#endif
319
Bram Moolenaar734a8672019-12-02 22:49:38 +0100320// Flag that is set while processing a message that must not be interrupted by
321// processing another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100322static int s_busy_processing = FALSE;
323
Bram Moolenaar734a8672019-12-02 22:49:38 +0100324static int destroying = FALSE; // call DestroyWindow() ourselves
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100325
326#ifdef MSWIN_FIND_REPLACE
K.Takatac81e9bf2022-01-16 14:15:49 +0000327static UINT s_findrep_msg = 0;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200328static FINDREPLACEW s_findrep_struct;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100329static HWND s_findrep_hwnd = NULL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200330static int s_findrep_is_find; // TRUE for find dialog, FALSE
331 // for find/replace dialog
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100332#endif
333
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100334HWND s_hwnd = NULL;
335static HDC s_hdc = NULL;
Bram Moolenaarab85ca42019-11-15 22:41:14 +0100336static HBRUSH s_brush = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100337
338#ifdef FEAT_TOOLBAR
339static HWND s_toolbarhwnd = NULL;
340static WNDPROC s_toolbar_wndproc = NULL;
341#endif
342
343#ifdef FEAT_GUI_TABLINE
344static HWND s_tabhwnd = NULL;
345static WNDPROC s_tabline_wndproc = NULL;
346static int showing_tabline = 0;
347#endif
348
349static WPARAM s_wParam = 0;
350static LPARAM s_lParam = 0;
351
352static HWND s_textArea = NULL;
353static UINT s_uMsg = 0;
354
Bram Moolenaar734a8672019-12-02 22:49:38 +0100355static char_u *s_textfield; // Used by dialogs to pass back strings
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100356
357static int s_need_activate = FALSE;
358
Bram Moolenaar734a8672019-12-02 22:49:38 +0100359// This variable is set when waiting for an event, which is the only moment
360// scrollbar dragging can be done directly. It's not allowed while commands
361// are executed, because it may move the cursor and that may cause unexpected
362// problems (e.g., while ":s" is working).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100363static int allow_scrollbar = FALSE;
364
K.Takatac81e9bf2022-01-16 14:15:49 +0000365#ifndef _DPI_AWARENESS_CONTEXTS_
366typedef HANDLE DPI_AWARENESS_CONTEXT;
367
368typedef enum DPI_AWARENESS {
K.Takatab0b2b732022-01-19 12:59:21 +0000369 DPI_AWARENESS_INVALID = -1,
370 DPI_AWARENESS_UNAWARE = 0,
371 DPI_AWARENESS_SYSTEM_AWARE = 1,
K.Takatac81e9bf2022-01-16 14:15:49 +0000372 DPI_AWARENESS_PER_MONITOR_AWARE = 2
373} DPI_AWARENESS;
374
K.Takatab0b2b732022-01-19 12:59:21 +0000375# define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT)-1)
376# define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT)-2)
K.Takatac81e9bf2022-01-16 14:15:49 +0000377# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT)-3)
378# define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT)-4)
379# define DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((DPI_AWARENESS_CONTEXT)-5)
380#endif
381
382#define DEFAULT_DPI 96
383static int s_dpi = DEFAULT_DPI;
384static BOOL s_in_dpichanged = FALSE;
385static DPI_AWARENESS s_process_dpi_aware = DPI_AWARENESS_INVALID;
Ken Takata7086b3e2023-10-02 21:26:03 +0200386static RECT s_suggested_rect;
K.Takatac81e9bf2022-01-16 14:15:49 +0000387
388static UINT (WINAPI *pGetDpiForSystem)(void) = NULL;
389static UINT (WINAPI *pGetDpiForWindow)(HWND hwnd) = NULL;
390static int (WINAPI *pGetSystemMetricsForDpi)(int, UINT) = NULL;
391//static INT (WINAPI *pGetWindowDpiAwarenessContext)(HWND hwnd) = NULL;
392static DPI_AWARENESS_CONTEXT (WINAPI *pSetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext) = NULL;
393static DPI_AWARENESS (WINAPI *pGetAwarenessFromDpiAwarenessContext)(DPI_AWARENESS_CONTEXT) = NULL;
394
395 static UINT WINAPI
396stubGetDpiForSystem(void)
397{
398 HWND hwnd = GetDesktopWindow();
399 HDC hdc = GetWindowDC(hwnd);
400 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
401 ReleaseDC(hwnd, hdc);
402 return dpi;
403}
404
405 static int WINAPI
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +0100406stubGetSystemMetricsForDpi(int nIndex, UINT dpi UNUSED)
K.Takatac81e9bf2022-01-16 14:15:49 +0000407{
408 return GetSystemMetrics(nIndex);
409}
410
411 static int
412adjust_fontsize_by_dpi(int size)
413{
414 return size * s_dpi / (int)pGetDpiForSystem();
415}
416
417 static int
418adjust_by_system_dpi(int size)
419{
420 return size * (int)pGetDpiForSystem() / DEFAULT_DPI;
421}
422
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100423#if defined(FEAT_DIRECTX)
424 static int
425directx_enabled(void)
426{
427 if (s_dwc != NULL)
428 return 1;
429 else if (s_directx_load_attempted)
430 return 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100431 // load DirectX
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100432 DWrite_Init();
433 s_directx_load_attempted = 1;
434 s_dwc = DWriteContext_Open();
435 directx_binddc();
436 return s_dwc != NULL ? 1 : 0;
437}
438
439 static void
440directx_binddc(void)
441{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000442 if (s_textArea == NULL)
443 return;
444
445 RECT rect;
446 GetClientRect(s_textArea, &rect);
447 DWriteContext_BindDC(s_dwc, s_hdc, &rect);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +0100448}
449#endif
450
Bram Moolenaar734a8672019-12-02 22:49:38 +0100451extern int current_font_height; // this is in os_mswin.c
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100452
453static struct
454{
455 UINT key_sym;
456 char_u vim_code0;
457 char_u vim_code1;
458} special_keys[] =
459{
460 {VK_UP, 'k', 'u'},
461 {VK_DOWN, 'k', 'd'},
462 {VK_LEFT, 'k', 'l'},
463 {VK_RIGHT, 'k', 'r'},
464
465 {VK_F1, 'k', '1'},
466 {VK_F2, 'k', '2'},
467 {VK_F3, 'k', '3'},
468 {VK_F4, 'k', '4'},
469 {VK_F5, 'k', '5'},
470 {VK_F6, 'k', '6'},
471 {VK_F7, 'k', '7'},
472 {VK_F8, 'k', '8'},
473 {VK_F9, 'k', '9'},
474 {VK_F10, 'k', ';'},
475
476 {VK_F11, 'F', '1'},
477 {VK_F12, 'F', '2'},
478 {VK_F13, 'F', '3'},
479 {VK_F14, 'F', '4'},
480 {VK_F15, 'F', '5'},
481 {VK_F16, 'F', '6'},
482 {VK_F17, 'F', '7'},
483 {VK_F18, 'F', '8'},
484 {VK_F19, 'F', '9'},
485 {VK_F20, 'F', 'A'},
486
487 {VK_F21, 'F', 'B'},
488#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar734a8672019-12-02 22:49:38 +0100489 {VK_PAUSE, 'F', 'B'}, // Pause == F21 (see gui_gtk_x11.c)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100490#endif
491 {VK_F22, 'F', 'C'},
492 {VK_F23, 'F', 'D'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100493 {VK_F24, 'F', 'E'}, // winuser.h defines up to F24
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100494
495 {VK_HELP, '%', '1'},
496 {VK_BACK, 'k', 'b'},
497 {VK_INSERT, 'k', 'I'},
498 {VK_DELETE, 'k', 'D'},
499 {VK_HOME, 'k', 'h'},
500 {VK_END, '@', '7'},
501 {VK_PRIOR, 'k', 'P'},
502 {VK_NEXT, 'k', 'N'},
503 {VK_PRINT, '%', '9'},
504 {VK_ADD, 'K', '6'},
505 {VK_SUBTRACT, 'K', '7'},
506 {VK_DIVIDE, 'K', '8'},
507 {VK_MULTIPLY, 'K', '9'},
Bram Moolenaar734a8672019-12-02 22:49:38 +0100508 {VK_SEPARATOR, 'K', 'A'}, // Keypad Enter
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100509 {VK_DECIMAL, 'K', 'B'},
510
511 {VK_NUMPAD0, 'K', 'C'},
512 {VK_NUMPAD1, 'K', 'D'},
513 {VK_NUMPAD2, 'K', 'E'},
514 {VK_NUMPAD3, 'K', 'F'},
515 {VK_NUMPAD4, 'K', 'G'},
516 {VK_NUMPAD5, 'K', 'H'},
517 {VK_NUMPAD6, 'K', 'I'},
518 {VK_NUMPAD7, 'K', 'J'},
519 {VK_NUMPAD8, 'K', 'K'},
520 {VK_NUMPAD9, 'K', 'L'},
521
Bram Moolenaar734a8672019-12-02 22:49:38 +0100522 // Keys that we want to be able to use any modifier with:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100523 {VK_SPACE, ' ', NUL},
524 {VK_TAB, TAB, NUL},
525 {VK_ESCAPE, ESC, NUL},
526 {NL, NL, NUL},
527 {CAR, CAR, NUL},
528
Bram Moolenaar734a8672019-12-02 22:49:38 +0100529 // End of list marker:
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100530 {0, 0, 0}
531};
532
Bram Moolenaar734a8672019-12-02 22:49:38 +0100533// Local variables
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100534static int s_button_pending = -1;
535
Bram Moolenaar734a8672019-12-02 22:49:38 +0100536// s_getting_focus is set when we got focus but didn't see mouse-up event yet,
537// so don't reset s_button_pending.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100538static int s_getting_focus = FALSE;
539
540static int s_x_pending;
541static int s_y_pending;
542static UINT s_kFlags_pending;
K.Takataa8ec4912022-02-03 14:32:33 +0000543static UINT_PTR s_wait_timer = 0; // Timer for get char from user
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100544static int s_timed_out = FALSE;
Anton Sharonov3f026672022-07-26 21:26:18 +0100545static int dead_key = DEAD_KEY_OFF;
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200546static UINT surrogate_pending_ch = 0; // 0: no surrogate pending,
547 // else a high surrogate
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100548
Bram Moolenaarc3719bd2017-11-18 22:13:31 +0100549#ifdef FEAT_BEVAL_GUI
Bram Moolenaar734a8672019-12-02 22:49:38 +0100550// balloon-eval WM_NOTIFY_HANDLER
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100551static void Handle_WM_Notify(HWND hwnd, LPNMHDR pnmh);
K.Takataa8ec4912022-02-03 14:32:33 +0000552static void track_user_activity(UINT uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100553#endif
554
555/*
556 * For control IME.
557 *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100558 * These LOGFONTW used for IME.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100559 */
K.Takata4ac893f2022-01-20 12:44:28 +0000560#ifdef FEAT_MBYTE_IME
Bram Moolenaar734a8672019-12-02 22:49:38 +0100561// holds LOGFONTW for 'guifontwide' if available, otherwise 'guifont'
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100562static LOGFONTW norm_logfont;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100563// holds LOGFONTW for 'guifont' always.
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100564static LOGFONTW sub_logfont;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100565#endif
566
567#ifdef FEAT_MBYTE_IME
568static LRESULT _OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData);
569#endif
570
571#if defined(FEAT_BROWSE)
572static char_u *convert_filter(char_u *s);
573#endif
574
575#ifdef DEBUG_PRINT_ERROR
576/*
577 * Print out the last Windows error message
578 */
579 static void
580print_windows_error(void)
581{
582 LPVOID lpMsgBuf;
583
584 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
585 NULL, GetLastError(),
586 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
587 (LPTSTR) &lpMsgBuf, 0, NULL);
588 TRACE1("Error: %s\n", lpMsgBuf);
589 LocalFree(lpMsgBuf);
590}
591#endif
592
593/*
594 * Cursor blink functions.
595 *
596 * This is a simple state machine:
597 * BLINK_NONE not blinking at all
598 * BLINK_OFF blinking, cursor is not shown
599 * BLINK_ON blinking, cursor is shown
600 */
601
602#define BLINK_NONE 0
603#define BLINK_OFF 1
604#define BLINK_ON 2
605
606static int blink_state = BLINK_NONE;
607static long_u blink_waittime = 700;
608static long_u blink_ontime = 400;
609static long_u blink_offtime = 250;
K.Takataa8ec4912022-02-03 14:32:33 +0000610static UINT_PTR blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100611
Bram Moolenaar703a8042016-06-04 16:24:32 +0200612 int
613gui_mch_is_blinking(void)
614{
615 return blink_state != BLINK_NONE;
616}
617
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200618 int
619gui_mch_is_blink_off(void)
620{
621 return blink_state == BLINK_OFF;
622}
623
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100624 void
625gui_mch_set_blinking(long wait, long on, long off)
626{
627 blink_waittime = wait;
628 blink_ontime = on;
629 blink_offtime = off;
630}
631
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100632 static VOID CALLBACK
633_OnBlinkTimer(
634 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100635 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000636 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100637 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100638{
639 MSG msg;
640
641 /*
642 TRACE2("Got timer event, id %d, blink_timer %d\n", idEvent, blink_timer);
643 */
644
645 KillTimer(NULL, idEvent);
646
Bram Moolenaar734a8672019-12-02 22:49:38 +0100647 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000648 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100649 ;
650
651 if (blink_state == BLINK_ON)
652 {
653 gui_undraw_cursor();
654 blink_state = BLINK_OFF;
K.Takataa8ec4912022-02-03 14:32:33 +0000655 blink_timer = SetTimer(NULL, 0, (UINT)blink_offtime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100656 }
657 else
658 {
659 gui_update_cursor(TRUE, FALSE);
660 blink_state = BLINK_ON;
K.Takataa8ec4912022-02-03 14:32:33 +0000661 blink_timer = SetTimer(NULL, 0, (UINT)blink_ontime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100662 }
Bram Moolenaar92467d32017-12-05 13:22:16 +0100663 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100664}
665
666 static void
667gui_mswin_rm_blink_timer(void)
668{
669 MSG msg;
670
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000671 if (blink_timer == 0)
672 return;
673
674 KillTimer(NULL, blink_timer);
675 // Eat spurious WM_TIMER messages
676 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
677 ;
678 blink_timer = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100679}
680
681/*
682 * Stop the cursor blinking. Show the cursor if it wasn't shown.
683 */
684 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100685gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100686{
687 gui_mswin_rm_blink_timer();
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100688 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaar92467d32017-12-05 13:22:16 +0100689 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100690 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100691 gui_mch_flush();
692 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100693 blink_state = BLINK_NONE;
694}
695
696/*
697 * Start the cursor blinking. If it was already blinking, this restarts the
698 * waiting time and shows the cursor.
699 */
700 void
701gui_mch_start_blink(void)
702{
703 gui_mswin_rm_blink_timer();
704
Bram Moolenaar734a8672019-12-02 22:49:38 +0100705 // Only switch blinking on if none of the times is zero
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100706 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
707 {
K.Takataa8ec4912022-02-03 14:32:33 +0000708 blink_timer = SetTimer(NULL, 0, (UINT)blink_waittime, _OnBlinkTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100709 blink_state = BLINK_ON;
710 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +0100711 gui_mch_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100712 }
713}
714
715/*
716 * Call-back routines.
717 */
718
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100719 static VOID CALLBACK
720_OnTimer(
721 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100722 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +0000723 UINT_PTR idEvent,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100724 DWORD dwTime UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100725{
726 MSG msg;
727
728 /*
729 TRACE2("Got timer event, id %d, s_wait_timer %d\n", idEvent, s_wait_timer);
730 */
731 KillTimer(NULL, idEvent);
732 s_timed_out = TRUE;
733
Bram Moolenaar734a8672019-12-02 22:49:38 +0100734 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +0000735 while (PeekMessageW(&msg, hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100736 ;
737 if (idEvent == s_wait_timer)
738 s_wait_timer = 0;
739}
740
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100741 static void
742_OnDeadChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100743 HWND hwnd UNUSED,
744 UINT ch UNUSED,
745 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100746{
747 dead_key = 1;
748}
749
750/*
751 * Convert Unicode character "ch" to bytes in "string[slen]".
752 * When "had_alt" is TRUE the ALT key was included in "ch".
753 * Return the length.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200754 * Because the Windows API uses UTF-16, we have to deal with surrogate
755 * pairs; this is where we choose to deal with them: if "ch" is a high
756 * surrogate, it will be stored, and the length returned will be zero; the next
757 * char_to_string call will then include the high surrogate, decoding the pair
758 * of UTF-16 code units to a single Unicode code point, presuming it is the
759 * matching low surrogate.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100760 */
761 static int
762char_to_string(int ch, char_u *string, int slen, int had_alt)
763{
764 int len;
765 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100766 WCHAR wstring[2];
Bram Moolenaar945ec092016-06-08 21:17:43 +0200767 char_u *ws = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100768
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200769 if (surrogate_pending_ch != 0)
770 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100771 // We don't guarantee ch is a low surrogate to match the high surrogate
772 // we already have; it should be, but if it isn't, tough luck.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200773 wstring[0] = surrogate_pending_ch;
774 wstring[1] = ch;
775 surrogate_pending_ch = 0;
776 len = 2;
777 }
Bram Moolenaar734a8672019-12-02 22:49:38 +0100778 else if (ch >= 0xD800 && ch <= 0xDBFF) // high surrogate
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200779 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100780 // We don't have the entire code point yet, only the first UTF-16 code
781 // unit; so just remember it and use it in the next call.
Bram Moolenaarf1f2f832018-04-24 16:04:57 +0200782 surrogate_pending_ch = ch;
783 return 0;
784 }
785 else
786 {
787 wstring[0] = ch;
788 len = 1;
789 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200790
Bram Moolenaar734a8672019-12-02 22:49:38 +0100791 // "ch" is a UTF-16 character. Convert it to a string of bytes. When
792 // "enc_codepage" is non-zero use the standard Win32 function,
793 // otherwise use our own conversion function (e.g., for UTF-8).
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200794 if (enc_codepage > 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100795 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200796 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
797 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100798 // If we had included the ALT key into the character but now the
799 // upper bit is no longer set, that probably means the conversion
800 // failed. Convert the original character and set the upper bit
801 // afterwards.
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200802 if (had_alt && len == 1 && ch >= 0x80 && string[0] < 0x80)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100803 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200804 wstring[0] = ch & 0x7f;
805 len = WideCharToMultiByte(enc_codepage, 0, wstring, len,
806 (LPSTR)string, slen, 0, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100807 if (len == 1) // safety check
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200808 string[0] |= 0x80;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100809 }
810 }
811 else
812 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200813 ws = utf16_to_enc(wstring, &len);
814 if (ws == NULL)
815 len = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100816 else
817 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100818 if (len > slen) // just in case
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200819 len = slen;
820 mch_memmove(string, ws, len);
821 vim_free(ws);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100822 }
823 }
824
825 if (len == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100826 {
827 string[0] = ch;
828 len = 1;
829 }
830
831 for (i = 0; i < len; ++i)
832 if (string[i] == CSI && len <= slen - 2)
833 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100834 // Insert CSI as K_CSI.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100835 mch_memmove(string + i + 3, string + i + 1, len - i - 1);
836 string[++i] = KS_EXTRA;
837 string[++i] = (int)KE_CSI;
838 len += 2;
839 }
840
841 return len;
842}
843
LemonBoy45684c62022-04-24 15:46:42 +0100844 static int
845get_active_modifiers(void)
846{
847 int modifiers = 0;
848
849 if (GetKeyState(VK_CONTROL) & 0x8000)
850 modifiers |= MOD_MASK_CTRL;
851 if (GetKeyState(VK_SHIFT) & 0x8000)
852 modifiers |= MOD_MASK_SHIFT;
LemonBoy202b4bd2022-04-28 19:50:54 +0100853 // Windows handles Ctrl + Alt as AltGr and vice-versa. We can distinguish
854 // the two cases by checking whether the left or the right Alt key is
LemonBoy45684c62022-04-24 15:46:42 +0100855 // pressed.
LemonBoy202b4bd2022-04-28 19:50:54 +0100856 if (GetKeyState(VK_LMENU) & 0x8000)
857 modifiers |= MOD_MASK_ALT;
858 if ((modifiers & MOD_MASK_CTRL) && (GetKeyState(VK_RMENU) & 0x8000))
859 modifiers &= ~MOD_MASK_CTRL;
Anton Sharonov8d8b9752022-10-07 16:28:48 +0100860 // Add RightALT only if it is hold alone (without Ctrl), because if AltGr
861 // is pressed, Windows claims that Ctrl is hold as well. That way we can
862 // recognize Right-ALT alone and be sure that not AltGr is hold.
863 if (!(GetKeyState(VK_CONTROL) & 0x8000)
864 && (GetKeyState(VK_RMENU) & 0x8000)
865 && !(GetKeyState(VK_LMENU) & 0x8000)) // seems AltGr has both set
866 modifiers |= MOD_MASK_ALT;
LemonBoy45684c62022-04-24 15:46:42 +0100867
868 return modifiers;
869}
870
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100871/*
872 * Key hit, add it to the input buffer.
873 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100874 static void
875_OnChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100876 HWND hwnd UNUSED,
LemonBoy77fc0b02022-04-22 22:45:52 +0100877 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100878 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100879{
880 char_u string[40];
881 int len = 0;
LemonBoy45684c62022-04-24 15:46:42 +0100882 int modifiers;
LemonBoy77fc0b02022-04-22 22:45:52 +0100883 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100884
Anton Sharonov3f026672022-07-26 21:26:18 +0100885 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
886 return;
887
888 // keep DEAD_KEY_TRANSIENT_IN_ON_CHAR value for later handling in
889 // process_message()
890 if (dead_key != DEAD_KEY_TRANSIENT_IN_ON_CHAR)
891 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100892
LemonBoy45684c62022-04-24 15:46:42 +0100893 modifiers = get_active_modifiers();
LemonBoy77fc0b02022-04-22 22:45:52 +0100894
895 ch = simplify_key(ch, &modifiers);
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +0000896
Christopher Plewright7b0afc12022-12-30 16:54:58 +0000897 // Some keys need adjustment when the Ctrl modifier is used.
898 ++no_reduce_keys;
899 ch = may_adjust_key_for_ctrl(modifiers, ch);
900 --no_reduce_keys;
901
LemonBoy77fc0b02022-04-22 22:45:52 +0100902 // remove the SHIFT modifier for keys where it's already included, e.g.,
903 // '(' and '*'
904 modifiers = may_remove_shift_modifier(modifiers, ch);
905
906 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
907 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
908 if (ch == CSI)
909 ch = K_CSI;
910
911 if (modifiers)
912 {
913 string[0] = CSI;
914 string[1] = KS_MODIFIER;
915 string[2] = modifiers;
916 add_to_input_buf(string, 3);
917 }
918
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919 len = char_to_string(ch, string, 40, FALSE);
920 if (len == 1 && string[0] == Ctrl_C && ctrl_c_interrupts)
921 {
922 trash_input_buf();
923 got_int = TRUE;
924 }
925
926 add_to_input_buf(string, len);
927}
928
929/*
930 * Alt-Key hit, add it to the input buffer.
931 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100932 static void
933_OnSysChar(
Bram Moolenaar1266d672017-02-01 13:43:36 +0100934 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100935 UINT cch,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100936 int cRepeat UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100937{
Bram Moolenaar734a8672019-12-02 22:49:38 +0100938 char_u string[40]; // Enough for multibyte character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100939 int len;
940 int modifiers;
Bram Moolenaar734a8672019-12-02 22:49:38 +0100941 int ch = cch; // special keys are negative
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100942
Anton Sharonov3f026672022-07-26 21:26:18 +0100943 dead_key = DEAD_KEY_OFF;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944
Bram Moolenaar734a8672019-12-02 22:49:38 +0100945 // OK, we have a character key (given by ch) which was entered with the
946 // ALT key pressed. Eg, if the user presses Alt-A, then ch == 'A'. Note
947 // that the system distinguishes Alt-a and Alt-A (Alt-Shift-a unless
948 // CAPSLOCK is pressed) at this point.
LemonBoy45684c62022-04-24 15:46:42 +0100949 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100950 ch = simplify_key(ch, &modifiers);
Bram Moolenaar734a8672019-12-02 22:49:38 +0100951 // remove the SHIFT modifier for keys where it's already included, e.g.,
952 // '(' and '*'
Bram Moolenaardaff0fb2020-09-27 13:16:46 +0200953 modifiers = may_remove_shift_modifier(modifiers, ch);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100954
Bram Moolenaarfd615a32020-05-16 14:01:51 +0200955 // Unify modifiers somewhat. No longer use ALT to set the 8th bit.
956 ch = extract_modifiers(ch, &modifiers, FALSE, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100957 if (ch == CSI)
958 ch = K_CSI;
959
960 len = 0;
961 if (modifiers)
962 {
963 string[len++] = CSI;
964 string[len++] = KS_MODIFIER;
965 string[len++] = modifiers;
966 }
967
968 if (IS_SPECIAL((int)ch))
969 {
970 string[len++] = CSI;
971 string[len++] = K_SECOND((int)ch);
972 string[len++] = K_THIRD((int)ch);
973 }
974 else
975 {
Bram Moolenaar734a8672019-12-02 22:49:38 +0100976 // Although the documentation isn't clear about it, we assume "ch" is
977 // a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100978 len += char_to_string(ch, string + len, 40 - len, TRUE);
979 }
980
981 add_to_input_buf(string, len);
982}
983
984 static void
985_OnMouseEvent(
986 int button,
987 int x,
988 int y,
989 int repeated_click,
990 UINT keyFlags)
991{
992 int vim_modifiers = 0x0;
993
994 s_getting_focus = FALSE;
995
996 if (keyFlags & MK_SHIFT)
997 vim_modifiers |= MOUSE_SHIFT;
998 if (keyFlags & MK_CONTROL)
999 vim_modifiers |= MOUSE_CTRL;
LemonBoy202b4bd2022-04-28 19:50:54 +01001000 if (GetKeyState(VK_LMENU) & 0x8000)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001001 vim_modifiers |= MOUSE_ALT;
1002
1003 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1004}
1005
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001006 static void
1007_OnMouseButtonDown(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001008 HWND hwnd UNUSED,
1009 BOOL fDoubleClick UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001010 int x,
1011 int y,
1012 UINT keyFlags)
1013{
1014 static LONG s_prevTime = 0;
1015
1016 LONG currentTime = GetMessageTime();
1017 int button = -1;
1018 int repeated_click;
1019
Bram Moolenaar734a8672019-12-02 22:49:38 +01001020 // Give main window the focus: this is so the cursor isn't hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001021 (void)SetFocus(s_hwnd);
1022
1023 if (s_uMsg == WM_LBUTTONDOWN || s_uMsg == WM_LBUTTONDBLCLK)
1024 button = MOUSE_LEFT;
1025 else if (s_uMsg == WM_MBUTTONDOWN || s_uMsg == WM_MBUTTONDBLCLK)
1026 button = MOUSE_MIDDLE;
1027 else if (s_uMsg == WM_RBUTTONDOWN || s_uMsg == WM_RBUTTONDBLCLK)
1028 button = MOUSE_RIGHT;
1029 else if (s_uMsg == WM_XBUTTONDOWN || s_uMsg == WM_XBUTTONDBLCLK)
1030 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001031 button = ((GET_XBUTTON_WPARAM(s_wParam) == 1) ? MOUSE_X1 : MOUSE_X2);
1032 }
1033 else if (s_uMsg == WM_CAPTURECHANGED)
1034 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001035 // on W95/NT4, somehow you get in here with an odd Msg
1036 // if you press one button while holding down the other..
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001037 if (s_button_pending == MOUSE_LEFT)
1038 button = MOUSE_RIGHT;
1039 else
1040 button = MOUSE_LEFT;
1041 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001042
1043 if (button < 0)
1044 return;
1045
1046 repeated_click = ((int)(currentTime - s_prevTime) < p_mouset);
1047
1048 /*
1049 * Holding down the left and right buttons simulates pushing the middle
1050 * button.
1051 */
1052 if (repeated_click
1053 && ((button == MOUSE_LEFT && s_button_pending == MOUSE_RIGHT)
1054 || (button == MOUSE_RIGHT
1055 && s_button_pending == MOUSE_LEFT)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001056 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001057 /*
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001058 * Hmm, gui.c will ignore more than one button down at a time, so
1059 * pretend we let go of it first.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001060 */
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001061 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, 0x0);
1062 button = MOUSE_MIDDLE;
1063 repeated_click = FALSE;
1064 s_button_pending = -1;
1065 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001066 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001067 else if ((repeated_click)
1068 || (mouse_model_popup() && (button == MOUSE_RIGHT)))
1069 {
1070 if (s_button_pending > -1)
1071 {
1072 _OnMouseEvent(s_button_pending, x, y, FALSE, keyFlags);
1073 s_button_pending = -1;
1074 }
1075 // TRACE("Button down at x %d, y %d\n", x, y);
1076 _OnMouseEvent(button, x, y, repeated_click, keyFlags);
1077 }
1078 else
1079 {
1080 /*
1081 * If this is the first press (i.e. not a multiple click) don't
1082 * action immediately, but store and wait for:
1083 * i) button-up
1084 * ii) mouse move
1085 * iii) another button press
1086 * before using it.
1087 * This enables us to make left+right simulate middle button,
1088 * without left or right being actioned first. The side-effect is
1089 * that if you click and hold the mouse without dragging, the
1090 * cursor doesn't move until you release the button. In practice
1091 * this is hardly a problem.
1092 */
1093 s_button_pending = button;
1094 s_x_pending = x;
1095 s_y_pending = y;
1096 s_kFlags_pending = keyFlags;
1097 }
1098
1099 s_prevTime = currentTime;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001100}
1101
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001102 static void
1103_OnMouseMoveOrRelease(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001104 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001105 int x,
1106 int y,
1107 UINT keyFlags)
1108{
1109 int button;
1110
1111 s_getting_focus = FALSE;
1112 if (s_button_pending > -1)
1113 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001114 // Delayed action for mouse down event
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001115 _OnMouseEvent(s_button_pending, s_x_pending,
1116 s_y_pending, FALSE, s_kFlags_pending);
1117 s_button_pending = -1;
1118 }
1119 if (s_uMsg == WM_MOUSEMOVE)
1120 {
1121 /*
1122 * It's only a MOUSE_DRAG if one or more mouse buttons are being held
1123 * down.
1124 */
1125 if (!(keyFlags & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON
1126 | MK_XBUTTON1 | MK_XBUTTON2)))
1127 {
1128 gui_mouse_moved(x, y);
1129 return;
1130 }
1131
1132 /*
1133 * While button is down, keep grabbing mouse move events when
1134 * the mouse goes outside the window
1135 */
1136 SetCapture(s_textArea);
1137 button = MOUSE_DRAG;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001138 // TRACE(" move at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001139 }
1140 else
1141 {
1142 ReleaseCapture();
1143 button = MOUSE_RELEASE;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001144 // TRACE(" up at x %d, y %d\n", x, y);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001145 }
1146
1147 _OnMouseEvent(button, x, y, FALSE, keyFlags);
1148}
1149
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001150 static void
1151_OnSizeTextArea(
1152 HWND hwnd UNUSED,
1153 UINT state UNUSED,
1154 int cx UNUSED,
1155 int cy UNUSED)
1156{
1157#if defined(FEAT_DIRECTX)
1158 if (IS_ENABLE_DIRECTX())
1159 directx_binddc();
1160#endif
1161}
1162
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001163#ifdef FEAT_MENU
1164/*
1165 * Find the vimmenu_T with the given id
1166 */
1167 static vimmenu_T *
1168gui_mswin_find_menu(
1169 vimmenu_T *pMenu,
1170 int id)
1171{
1172 vimmenu_T *pChildMenu;
1173
1174 while (pMenu)
1175 {
1176 if (pMenu->id == (UINT)id)
1177 break;
1178 if (pMenu->children != NULL)
1179 {
1180 pChildMenu = gui_mswin_find_menu(pMenu->children, id);
1181 if (pChildMenu)
1182 {
1183 pMenu = pChildMenu;
1184 break;
1185 }
1186 }
1187 pMenu = pMenu->next;
1188 }
1189 return pMenu;
1190}
1191
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192 static void
1193_OnMenu(
Bram Moolenaar1266d672017-02-01 13:43:36 +01001194 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001195 int id,
Bram Moolenaar1266d672017-02-01 13:43:36 +01001196 HWND hwndCtl UNUSED,
1197 UINT codeNotify UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001198{
1199 vimmenu_T *pMenu;
1200
1201 pMenu = gui_mswin_find_menu(root_menu, id);
1202 if (pMenu)
1203 gui_menu_cb(pMenu);
1204}
1205#endif
1206
1207#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001208/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001209 * Handle a Find/Replace window message.
1210 */
1211 static void
1212_OnFindRepl(void)
1213{
1214 int flags = 0;
1215 int down;
1216
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001217 if (s_findrep_struct.Flags & FR_DIALOGTERM)
Bram Moolenaar734a8672019-12-02 22:49:38 +01001218 // Give main window the focus back.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001219 (void)SetFocus(s_hwnd);
1220
1221 if (s_findrep_struct.Flags & FR_FINDNEXT)
1222 {
1223 flags = FRD_FINDNEXT;
1224
Bram Moolenaar734a8672019-12-02 22:49:38 +01001225 // Give main window the focus back: this is so the cursor isn't
1226 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001227 (void)SetFocus(s_hwnd);
1228 }
1229 else if (s_findrep_struct.Flags & FR_REPLACE)
1230 {
1231 flags = FRD_REPLACE;
1232
Bram Moolenaar734a8672019-12-02 22:49:38 +01001233 // Give main window the focus back: this is so the cursor isn't
1234 // hollow.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001235 (void)SetFocus(s_hwnd);
1236 }
1237 else if (s_findrep_struct.Flags & FR_REPLACEALL)
1238 {
1239 flags = FRD_REPLACEALL;
1240 }
1241
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001242 if (flags == 0)
1243 return;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001244
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00001245 char_u *p, *q;
1246
1247 // Call the generic GUI function to do the actual work.
1248 if (s_findrep_struct.Flags & FR_WHOLEWORD)
1249 flags |= FRD_WHOLE_WORD;
1250 if (s_findrep_struct.Flags & FR_MATCHCASE)
1251 flags |= FRD_MATCH_CASE;
1252 down = (s_findrep_struct.Flags & FR_DOWN) != 0;
1253 p = utf16_to_enc(s_findrep_struct.lpstrFindWhat, NULL);
1254 q = utf16_to_enc(s_findrep_struct.lpstrReplaceWith, NULL);
1255 if (p != NULL && q != NULL)
1256 gui_do_findrepl(flags, p, q, down);
1257 vim_free(p);
1258 vim_free(q);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001259}
1260#endif
1261
1262 static void
1263HandleMouseHide(UINT uMsg, LPARAM lParam)
1264{
1265 static LPARAM last_lParam = 0L;
1266
Bram Moolenaar734a8672019-12-02 22:49:38 +01001267 // We sometimes get a mousemove when the mouse didn't move...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001268 if (uMsg == WM_MOUSEMOVE || uMsg == WM_NCMOUSEMOVE)
1269 {
1270 if (lParam == last_lParam)
1271 return;
1272 last_lParam = lParam;
1273 }
1274
Bram Moolenaar734a8672019-12-02 22:49:38 +01001275 // Handle specially, to centralise coding. We need to be sure we catch all
1276 // possible events which should cause us to restore the cursor (as it is a
1277 // shared resource, we take full responsibility for it).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001278 switch (uMsg)
1279 {
1280 case WM_KEYUP:
1281 case WM_CHAR:
1282 /*
1283 * blank out the pointer if necessary
1284 */
1285 if (p_mh)
1286 gui_mch_mousehide(TRUE);
1287 break;
1288
Bram Moolenaar734a8672019-12-02 22:49:38 +01001289 case WM_SYSKEYUP: // show the pointer when a system-key is pressed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001290 case WM_SYSCHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01001291 case WM_MOUSEMOVE: // show the pointer on any mouse action
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001292 case WM_LBUTTONDOWN:
1293 case WM_LBUTTONUP:
1294 case WM_MBUTTONDOWN:
1295 case WM_MBUTTONUP:
1296 case WM_RBUTTONDOWN:
1297 case WM_RBUTTONUP:
1298 case WM_XBUTTONDOWN:
1299 case WM_XBUTTONUP:
1300 case WM_NCMOUSEMOVE:
1301 case WM_NCLBUTTONDOWN:
1302 case WM_NCLBUTTONUP:
1303 case WM_NCMBUTTONDOWN:
1304 case WM_NCMBUTTONUP:
1305 case WM_NCRBUTTONDOWN:
1306 case WM_NCRBUTTONUP:
1307 case WM_KILLFOCUS:
1308 /*
1309 * if the pointer is currently hidden, then we should show it.
1310 */
1311 gui_mch_mousehide(FALSE);
1312 break;
1313 }
1314}
1315
1316 static LRESULT CALLBACK
1317_TextAreaWndProc(
1318 HWND hwnd,
1319 UINT uMsg,
1320 WPARAM wParam,
1321 LPARAM lParam)
1322{
1323 /*
1324 TRACE("TextAreaWndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x\n",
1325 hwnd, uMsg, wParam, lParam);
1326 */
1327
1328 HandleMouseHide(uMsg, lParam);
1329
1330 s_uMsg = uMsg;
1331 s_wParam = wParam;
1332 s_lParam = lParam;
1333
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001334#ifdef FEAT_BEVAL_GUI
K.Takataa8ec4912022-02-03 14:32:33 +00001335 track_user_activity(uMsg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001336#endif
1337
1338 switch (uMsg)
1339 {
1340 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK,_OnMouseButtonDown);
1341 HANDLE_MSG(hwnd, WM_LBUTTONDOWN,_OnMouseButtonDown);
1342 HANDLE_MSG(hwnd, WM_LBUTTONUP, _OnMouseMoveOrRelease);
1343 HANDLE_MSG(hwnd, WM_MBUTTONDBLCLK,_OnMouseButtonDown);
1344 HANDLE_MSG(hwnd, WM_MBUTTONDOWN,_OnMouseButtonDown);
1345 HANDLE_MSG(hwnd, WM_MBUTTONUP, _OnMouseMoveOrRelease);
1346 HANDLE_MSG(hwnd, WM_MOUSEMOVE, _OnMouseMoveOrRelease);
1347 HANDLE_MSG(hwnd, WM_PAINT, _OnPaint);
1348 HANDLE_MSG(hwnd, WM_RBUTTONDBLCLK,_OnMouseButtonDown);
1349 HANDLE_MSG(hwnd, WM_RBUTTONDOWN,_OnMouseButtonDown);
1350 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnMouseMoveOrRelease);
1351 HANDLE_MSG(hwnd, WM_XBUTTONDBLCLK,_OnMouseButtonDown);
1352 HANDLE_MSG(hwnd, WM_XBUTTONDOWN,_OnMouseButtonDown);
1353 HANDLE_MSG(hwnd, WM_XBUTTONUP, _OnMouseMoveOrRelease);
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001354 HANDLE_MSG(hwnd, WM_SIZE, _OnSizeTextArea);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001355
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01001356#ifdef FEAT_BEVAL_GUI
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001357 case WM_NOTIFY: Handle_WM_Notify(hwnd, (LPNMHDR)lParam);
1358 return TRUE;
1359#endif
1360 default:
K.Takata4ac893f2022-01-20 12:44:28 +00001361 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001362 }
1363}
1364
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001365/*
1366 * Called when the foreground or background color has been changed.
1367 */
1368 void
1369gui_mch_new_colors(void)
1370{
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001371 HBRUSH prevBrush;
1372
1373 s_brush = CreateSolidBrush(gui.back_pixel);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001374 prevBrush = (HBRUSH)SetClassLongPtr(
1375 s_hwnd, GCLP_HBRBACKGROUND, (LONG_PTR)s_brush);
Bram Moolenaarab85ca42019-11-15 22:41:14 +01001376 InvalidateRect(s_hwnd, NULL, TRUE);
1377 DeleteObject(prevBrush);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001378}
1379
1380/*
1381 * Set the colors to their default values.
1382 */
1383 void
1384gui_mch_def_colors(void)
1385{
1386 gui.norm_pixel = GetSysColor(COLOR_WINDOWTEXT);
1387 gui.back_pixel = GetSysColor(COLOR_WINDOW);
1388 gui.def_norm_pixel = gui.norm_pixel;
1389 gui.def_back_pixel = gui.back_pixel;
1390}
1391
1392/*
1393 * Open the GUI window which was created by a call to gui_mch_init().
1394 */
1395 int
1396gui_mch_open(void)
1397{
Bram Moolenaar734a8672019-12-02 22:49:38 +01001398 // Actually open the window, if not already visible
1399 // (may be done already in gui_mch_set_shellsize)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001400 if (!IsWindowVisible(s_hwnd))
1401 ShowWindow(s_hwnd, SW_SHOWDEFAULT);
1402
1403#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001404 // Init replace string here, so that we keep it when re-opening the
1405 // dialog.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001406 s_findrep_struct.lpstrReplaceWith[0] = NUL;
1407#endif
1408
1409 return OK;
1410}
1411
1412/*
1413 * Get the position of the top left corner of the window.
1414 */
1415 int
1416gui_mch_get_winpos(int *x, int *y)
1417{
1418 RECT rect;
1419
1420 GetWindowRect(s_hwnd, &rect);
1421 *x = rect.left;
1422 *y = rect.top;
1423 return OK;
1424}
1425
1426/*
1427 * Set the position of the top left corner of the window to the given
1428 * coordinates.
1429 */
1430 void
1431gui_mch_set_winpos(int x, int y)
1432{
1433 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1434 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1435}
1436 void
1437gui_mch_set_text_area_pos(int x, int y, int w, int h)
1438{
1439 static int oldx = 0;
1440 static int oldy = 0;
1441
1442 SetWindowPos(s_textArea, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
1443
1444#ifdef FEAT_TOOLBAR
1445 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
1446 SendMessage(s_toolbarhwnd, WM_SIZE,
K.Takatac81e9bf2022-01-16 14:15:49 +00001447 (WPARAM)0, MAKELPARAM(w, gui.toolbar_height));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001448#endif
1449#if defined(FEAT_GUI_TABLINE)
1450 if (showing_tabline)
1451 {
1452 int top = 0;
1453 RECT rect;
1454
1455# ifdef FEAT_TOOLBAR
1456 if (vim_strchr(p_go, GO_TOOLBAR) != NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00001457 top = gui.toolbar_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001458# endif
1459 GetClientRect(s_hwnd, &rect);
1460 MoveWindow(s_tabhwnd, 0, top, rect.right, gui.tabline_height, TRUE);
1461 }
1462#endif
1463
Bram Moolenaar734a8672019-12-02 22:49:38 +01001464 // When side scroll bar is unshown, the size of window will change.
1465 // then, the text area move left or right. thus client rect should be
1466 // forcedly redrawn. (Yasuhiro Matsumoto)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001467 if (oldx != x || oldy != y)
1468 {
1469 InvalidateRect(s_hwnd, NULL, FALSE);
1470 oldx = x;
1471 oldy = y;
1472 }
1473}
1474
1475
1476/*
1477 * Scrollbar stuff:
1478 */
1479
1480 void
1481gui_mch_enable_scrollbar(
1482 scrollbar_T *sb,
1483 int flag)
1484{
1485 ShowScrollBar(sb->id, SB_CTL, flag);
1486
Bram Moolenaar734a8672019-12-02 22:49:38 +01001487 // TODO: When the window is maximized, the size of the window stays the
1488 // same, thus the size of the text area changes. On Win98 it's OK, on Win
1489 // NT 4.0 it's not...
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001490}
1491
1492 void
1493gui_mch_set_scrollbar_pos(
1494 scrollbar_T *sb,
1495 int x,
1496 int y,
1497 int w,
1498 int h)
1499{
1500 SetWindowPos(sb->id, NULL, x, y, w, h,
1501 SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW);
1502}
1503
Bram Moolenaar203ec772020-07-17 20:43:43 +02001504 int
1505gui_mch_get_scrollbar_xpadding(void)
1506{
1507 RECT rcTxt, rcWnd;
1508 int xpad;
1509
1510 GetWindowRect(s_textArea, &rcTxt);
1511 GetWindowRect(s_hwnd, &rcWnd);
1512 xpad = rcWnd.right - rcTxt.right - gui.scrollbar_width
K.Takatac81e9bf2022-01-16 14:15:49 +00001513 - pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi)
1514 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001515 return (xpad < 0) ? 0 : xpad;
1516}
1517
1518 int
1519gui_mch_get_scrollbar_ypadding(void)
1520{
1521 RECT rcTxt, rcWnd;
1522 int ypad;
1523
1524 GetWindowRect(s_textArea, &rcTxt);
1525 GetWindowRect(s_hwnd, &rcWnd);
1526 ypad = rcWnd.bottom - rcTxt.bottom - gui.scrollbar_height
K.Takatac81e9bf2022-01-16 14:15:49 +00001527 - pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi)
1528 - pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi);
Bram Moolenaar203ec772020-07-17 20:43:43 +02001529 return (ypad < 0) ? 0 : ypad;
1530}
1531
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001532 void
1533gui_mch_create_scrollbar(
1534 scrollbar_T *sb,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001535 int orient) // SBAR_VERT or SBAR_HORIZ
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001536{
1537 sb->id = CreateWindow(
1538 "SCROLLBAR", "Scrollbar",
1539 WS_CHILD | ((orient == SBAR_VERT) ? SBS_VERT : SBS_HORZ), 0, 0,
Bram Moolenaar734a8672019-12-02 22:49:38 +01001540 10, // Any value will do for now
1541 10, // Any value will do for now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001542 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001543 g_hinst, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001544}
1545
1546/*
1547 * Find the scrollbar with the given hwnd.
1548 */
Bram Moolenaar98af99f2020-07-16 22:30:31 +02001549 static scrollbar_T *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001550gui_mswin_find_scrollbar(HWND hwnd)
1551{
1552 win_T *wp;
1553
1554 if (gui.bottom_sbar.id == hwnd)
1555 return &gui.bottom_sbar;
1556 FOR_ALL_WINDOWS(wp)
1557 {
1558 if (wp->w_scrollbars[SBAR_LEFT].id == hwnd)
1559 return &wp->w_scrollbars[SBAR_LEFT];
1560 if (wp->w_scrollbars[SBAR_RIGHT].id == hwnd)
1561 return &wp->w_scrollbars[SBAR_RIGHT];
1562 }
1563 return NULL;
1564}
1565
K.Takatac81e9bf2022-01-16 14:15:49 +00001566 static void
1567update_scrollbar_size(void)
1568{
1569 gui.scrollbar_width = pGetSystemMetricsForDpi(SM_CXVSCROLL, s_dpi);
1570 gui.scrollbar_height = pGetSystemMetricsForDpi(SM_CYHSCROLL, s_dpi);
1571}
1572
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001573/*
K.Takataabe628e2022-01-23 16:25:17 +00001574 * Get the average character size of a font.
1575 */
1576 static void
1577GetAverageFontSize(HDC hdc, SIZE *size)
1578{
1579 // GetTextMetrics() may not return the right value in tmAveCharWidth
1580 // for some fonts. Do our own average computation.
1581 GetTextExtentPoint(hdc,
1582 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
1583 52, size);
1584 size->cx = (size->cx / 26 + 1) / 2;
1585}
1586
1587/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001588 * Get the character size of a font.
1589 */
1590 static void
1591GetFontSize(GuiFont font)
1592{
1593 HWND hwnd = GetDesktopWindow();
1594 HDC hdc = GetWindowDC(hwnd);
1595 HFONT hfntOld = SelectFont(hdc, (HFONT)font);
Bram Moolenaar93d77b22019-05-07 22:52:50 +02001596 SIZE size;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001597 TEXTMETRIC tm;
1598
1599 GetTextMetrics(hdc, &tm);
K.Takataabe628e2022-01-23 16:25:17 +00001600 GetAverageFontSize(hdc, &size);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001601
K.Takataabe628e2022-01-23 16:25:17 +00001602 gui.char_width = size.cx + tm.tmOverhang;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001603 gui.char_height = tm.tmHeight + p_linespace;
1604
1605 SelectFont(hdc, hfntOld);
1606
1607 ReleaseDC(hwnd, hdc);
1608}
1609
1610/*
1611 * Adjust gui.char_height (after 'linespace' was changed).
1612 */
1613 int
1614gui_mch_adjust_charheight(void)
1615{
1616 GetFontSize(gui.norm_font);
1617 return OK;
1618}
1619
1620 static GuiFont
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001621get_font_handle(LOGFONTW *lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001622{
1623 HFONT font = NULL;
1624
Bram Moolenaar734a8672019-12-02 22:49:38 +01001625 // Load the font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001626 font = CreateFontIndirectW(lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001627
1628 if (font == NULL)
1629 return NOFONT;
1630
1631 return (GuiFont)font;
1632}
1633
1634 static int
1635pixels_to_points(int pixels, int vertical)
1636{
1637 int points;
1638 HWND hwnd;
1639 HDC hdc;
1640
1641 hwnd = GetDesktopWindow();
1642 hdc = GetWindowDC(hwnd);
1643
1644 points = MulDiv(pixels, 72,
1645 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX));
1646
1647 ReleaseDC(hwnd, hdc);
1648
1649 return points;
1650}
1651
1652 GuiFont
1653gui_mch_get_font(
1654 char_u *name,
1655 int giveErrorIfMissing)
1656{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001657 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001658 GuiFont font = NOFONT;
1659
1660 if (get_logfont(&lf, name, NULL, giveErrorIfMissing) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00001661 {
1662 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001663 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00001664 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001665 if (font == NOFONT && giveErrorIfMissing)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001666 semsg(_(e_unknown_font_str), name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001667 return font;
1668}
1669
1670#if defined(FEAT_EVAL) || defined(PROTO)
1671/*
1672 * Return the name of font "font" in allocated memory.
1673 * Don't know how to get the actual name, thus use the provided name.
1674 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001675 char_u *
Bram Moolenaar1266d672017-02-01 13:43:36 +01001676gui_mch_get_fontname(GuiFont font UNUSED, char_u *name)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001677{
1678 if (name == NULL)
1679 return NULL;
1680 return vim_strsave(name);
1681}
1682#endif
1683
1684 void
1685gui_mch_free_font(GuiFont font)
1686{
1687 if (font)
1688 DeleteObject((HFONT)font);
1689}
1690
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001691/*
1692 * Return the Pixel value (color) for the given color name.
1693 * Return INVALCOLOR for error.
1694 */
1695 guicolor_T
1696gui_mch_get_color(char_u *name)
1697{
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001698 int i;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001699
1700 typedef struct SysColorTable
1701 {
1702 char *name;
1703 int color;
1704 } SysColorTable;
1705
1706 static SysColorTable sys_table[] =
1707 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001708 {"SYS_3DDKSHADOW", COLOR_3DDKSHADOW},
1709 {"SYS_3DHILIGHT", COLOR_3DHILIGHT},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001710#ifdef COLOR_3DHIGHLIGHT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001711 {"SYS_3DHIGHLIGHT", COLOR_3DHIGHLIGHT},
1712#endif
1713 {"SYS_BTNHILIGHT", COLOR_BTNHILIGHT},
1714 {"SYS_BTNHIGHLIGHT", COLOR_BTNHIGHLIGHT},
1715 {"SYS_3DLIGHT", COLOR_3DLIGHT},
1716 {"SYS_3DSHADOW", COLOR_3DSHADOW},
1717 {"SYS_DESKTOP", COLOR_DESKTOP},
1718 {"SYS_INFOBK", COLOR_INFOBK},
1719 {"SYS_INFOTEXT", COLOR_INFOTEXT},
1720 {"SYS_3DFACE", COLOR_3DFACE},
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001721 {"SYS_BTNFACE", COLOR_BTNFACE},
1722 {"SYS_BTNSHADOW", COLOR_BTNSHADOW},
1723 {"SYS_ACTIVEBORDER", COLOR_ACTIVEBORDER},
1724 {"SYS_ACTIVECAPTION", COLOR_ACTIVECAPTION},
1725 {"SYS_APPWORKSPACE", COLOR_APPWORKSPACE},
1726 {"SYS_BACKGROUND", COLOR_BACKGROUND},
1727 {"SYS_BTNTEXT", COLOR_BTNTEXT},
1728 {"SYS_CAPTIONTEXT", COLOR_CAPTIONTEXT},
1729 {"SYS_GRAYTEXT", COLOR_GRAYTEXT},
1730 {"SYS_HIGHLIGHT", COLOR_HIGHLIGHT},
1731 {"SYS_HIGHLIGHTTEXT", COLOR_HIGHLIGHTTEXT},
1732 {"SYS_INACTIVEBORDER", COLOR_INACTIVEBORDER},
1733 {"SYS_INACTIVECAPTION", COLOR_INACTIVECAPTION},
1734 {"SYS_INACTIVECAPTIONTEXT", COLOR_INACTIVECAPTIONTEXT},
1735 {"SYS_MENU", COLOR_MENU},
1736 {"SYS_MENUTEXT", COLOR_MENUTEXT},
1737 {"SYS_SCROLLBAR", COLOR_SCROLLBAR},
1738 {"SYS_WINDOW", COLOR_WINDOW},
1739 {"SYS_WINDOWFRAME", COLOR_WINDOWFRAME},
1740 {"SYS_WINDOWTEXT", COLOR_WINDOWTEXT}
1741 };
1742
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001743 /*
1744 * Try to look up a system colour.
1745 */
Yegappan Lakshmanana34b4462022-06-11 10:43:26 +01001746 for (i = 0; i < (int)ARRAY_LENGTH(sys_table); i++)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001747 if (STRICMP(name, sys_table[i].name) == 0)
1748 return GetSysColor(sys_table[i].color);
1749
Bram Moolenaarab302212016-04-26 20:59:29 +02001750 return gui_get_color_cmn(name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001751}
Bram Moolenaarc285fe72016-04-26 21:51:48 +02001752
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001753 guicolor_T
1754gui_mch_get_rgb_color(int r, int g, int b)
1755{
1756 return gui_get_rgb_color_cmn(r, g, b);
1757}
1758
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001759/*
1760 * Return OK if the key with the termcap name "name" is supported.
1761 */
1762 int
1763gui_mch_haskey(char_u *name)
1764{
1765 int i;
1766
1767 for (i = 0; special_keys[i].vim_code1 != NUL; i++)
LemonBoy202b4bd2022-04-28 19:50:54 +01001768 if (name[0] == special_keys[i].vim_code0
1769 && name[1] == special_keys[i].vim_code1)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770 return OK;
1771 return FAIL;
1772}
1773
1774 void
1775gui_mch_beep(void)
1776{
LemonBoy77771d32022-04-13 11:47:25 +01001777 MessageBeep((UINT)-1);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001778}
1779/*
1780 * Invert a rectangle from row r, column c, for nr rows and nc columns.
1781 */
1782 void
1783gui_mch_invert_rectangle(
1784 int r,
1785 int c,
1786 int nr,
1787 int nc)
1788{
1789 RECT rc;
1790
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001791#if defined(FEAT_DIRECTX)
1792 if (IS_ENABLE_DIRECTX())
1793 DWriteContext_Flush(s_dwc);
1794#endif
1795
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001796 /*
1797 * Note: InvertRect() excludes right and bottom of rectangle.
1798 */
1799 rc.left = FILL_X(c);
1800 rc.top = FILL_Y(r);
1801 rc.right = rc.left + nc * gui.char_width;
1802 rc.bottom = rc.top + nr * gui.char_height;
1803 InvertRect(s_hdc, &rc);
1804}
1805
1806/*
1807 * Iconify the GUI window.
1808 */
1809 void
1810gui_mch_iconify(void)
1811{
1812 ShowWindow(s_hwnd, SW_MINIMIZE);
1813}
1814
1815/*
1816 * Draw a cursor without focus.
1817 */
1818 void
1819gui_mch_draw_hollow_cursor(guicolor_T color)
1820{
1821 HBRUSH hbr;
1822 RECT rc;
1823
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001824#if defined(FEAT_DIRECTX)
1825 if (IS_ENABLE_DIRECTX())
1826 DWriteContext_Flush(s_dwc);
1827#endif
1828
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001829 /*
1830 * Note: FrameRect() excludes right and bottom of rectangle.
1831 */
1832 rc.left = FILL_X(gui.col);
1833 rc.top = FILL_Y(gui.row);
1834 rc.right = rc.left + gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001835 if (mb_lefthalve(gui.row, gui.col))
1836 rc.right += gui.char_width;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001837 rc.bottom = rc.top + gui.char_height;
1838 hbr = CreateSolidBrush(color);
1839 FrameRect(s_hdc, &rc, hbr);
1840 DeleteBrush(hbr);
1841}
1842/*
1843 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
1844 * color "color".
1845 */
1846 void
1847gui_mch_draw_part_cursor(
1848 int w,
1849 int h,
1850 guicolor_T color)
1851{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001852 RECT rc;
1853
1854 /*
1855 * Note: FillRect() excludes right and bottom of rectangle.
1856 */
1857 rc.left =
1858#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01001859 // vertical line should be on the right of current point
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001860 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
1861#endif
1862 FILL_X(gui.col);
1863 rc.top = FILL_Y(gui.row) + gui.char_height - h;
1864 rc.right = rc.left + w;
1865 rc.bottom = rc.top + h;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01001866
Bram Moolenaar92467d32017-12-05 13:22:16 +01001867 fill_rect(&rc, NULL, color);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001868}
1869
1870
1871/*
1872 * Generates a VK_SPACE when the internal dead_key flag is set to output the
1873 * dead key's nominal character and re-post the original message.
1874 */
1875 static void
Anton Sharonov3f026672022-07-26 21:26:18 +01001876outputDeadKey_rePost_Ex(MSG originalMsg, int dead_key2set)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001877{
1878 static MSG deadCharExpel;
1879
Anton Sharonov3f026672022-07-26 21:26:18 +01001880 if (dead_key == DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001881 return;
1882
Anton Sharonov3f026672022-07-26 21:26:18 +01001883 dead_key = dead_key2set;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001884
Bram Moolenaar734a8672019-12-02 22:49:38 +01001885 // Make Windows generate the dead key's character
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001886 deadCharExpel.message = originalMsg.message;
1887 deadCharExpel.hwnd = originalMsg.hwnd;
1888 deadCharExpel.wParam = VK_SPACE;
1889
K.Takata4ac893f2022-01-20 12:44:28 +00001890 TranslateMessage(&deadCharExpel);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001891
Bram Moolenaar734a8672019-12-02 22:49:38 +01001892 // re-generate the current character free of the dead char influence
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001893 PostMessage(originalMsg.hwnd, originalMsg.message, originalMsg.wParam,
1894 originalMsg.lParam);
1895}
1896
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001897/*
Anton Sharonov3f026672022-07-26 21:26:18 +01001898 * Wrapper for outputDeadKey_rePost_Ex which always reset dead_key value.
1899 */
1900 static void
1901outputDeadKey_rePost(MSG originalMsg)
1902{
1903 outputDeadKey_rePost_Ex(originalMsg, DEAD_KEY_OFF);
1904}
1905
1906/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001907 * Process a single Windows message.
1908 * If one is not available we hang until one is.
1909 */
1910 static void
1911process_message(void)
1912{
1913 MSG msg;
Bram Moolenaar734a8672019-12-02 22:49:38 +01001914 UINT vk = 0; // Virtual key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001915 char_u string[40];
1916 int i;
1917 int modifiers = 0;
1918 int key;
1919#ifdef FEAT_MENU
1920 static char_u k10[] = {K_SPECIAL, 'k', ';', 0};
1921#endif
LemonBoy77fc0b02022-04-22 22:45:52 +01001922 BYTE keyboard_state[256];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001923
K.Takatab7057bd2022-01-21 11:37:07 +00001924 GetMessageW(&msg, NULL, 0, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001925
1926#ifdef FEAT_OLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001927 // Look after OLE Automation commands
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001928 if (msg.message == WM_OLE)
1929 {
1930 char_u *str = (char_u *)msg.lParam;
1931 if (str == NULL || *str == NUL)
1932 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01001933 // Message can't be ours, forward it. Fixes problem with Ultramon
1934 // 3.0.4
K.Takatab7057bd2022-01-21 11:37:07 +00001935 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001936 }
1937 else
1938 {
1939 add_to_input_buf(str, (int)STRLEN(str));
Bram Moolenaar734a8672019-12-02 22:49:38 +01001940 vim_free(str); // was allocated in CVim::SendKeys()
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001941 }
1942 return;
1943 }
1944#endif
1945
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001946#ifdef MSWIN_FIND_REPLACE
Bram Moolenaar734a8672019-12-02 22:49:38 +01001947 // Don't process messages used by the dialog
K.Takatab7057bd2022-01-21 11:37:07 +00001948 if (s_findrep_hwnd != NULL && IsDialogMessageW(s_findrep_hwnd, &msg))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001949 {
1950 HandleMouseHide(msg.message, msg.lParam);
1951 return;
1952 }
1953#endif
1954
1955 /*
1956 * Check if it's a special key that we recognise. If not, call
1957 * TranslateMessage().
1958 */
1959 if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
1960 {
1961 vk = (int) msg.wParam;
1962
1963 /*
1964 * Handle dead keys in special conditions in other cases we let Windows
1965 * handle them and do not interfere.
1966 *
1967 * The dead_key flag must be reset on several occasions:
1968 * - in _OnChar() (or _OnSysChar()) as any dead key was necessarily
1969 * consumed at that point (This is when we let Windows combine the
1970 * dead character on its own)
1971 *
1972 * - Before doing something special such as regenerating keypresses to
1973 * expel the dead character as this could trigger an infinite loop if
K.Takata4ac893f2022-01-20 12:44:28 +00001974 * for some reason TranslateMessage() do not trigger a call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001975 * immediately to _OnChar() (or _OnSysChar()).
1976 */
Anton Sharonov3f026672022-07-26 21:26:18 +01001977
1978 /*
1979 * We are at the moment after WM_CHAR with DEAD_KEY_SKIP_ON_CHAR event
1980 * was handled by _WndProc, this keypress we want to process normally
1981 */
1982 if (dead_key == DEAD_KEY_SKIP_ON_CHAR)
1983 dead_key = DEAD_KEY_OFF;
1984
1985 if (dead_key != DEAD_KEY_OFF)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001986 {
1987 /*
dundargocc57b5bc2022-11-02 13:30:51 +00001988 * Expel the dead key pressed with Ctrl in a special way.
Anton Sharonov3f026672022-07-26 21:26:18 +01001989 *
1990 * After dead key was pressed with Ctrl in some cases, ESC was
1991 * artificially injected and handled by _OnChar(), now we are
1992 * dealing with completely new key press from the user. If we don't
1993 * do anything, ToUnicode() call will interpret this vk+scan_code
1994 * under influence of "dead-modifier". To prevent this we translate
1995 * this message replacing current char from user with VK_SPACE,
1996 * which will cause WM_CHAR with dead_key's character itself. Using
1997 * DEAD_KEY_SKIP_ON_CHAR value of dead_char we force _OnChar() to
1998 * ignore this one WM_CHAR event completely. Afterwards (due to
1999 * usage of PostMessage), this procedure is scheduled to be called
2000 * again with user char and on next entry we will clean
2001 * DEAD_KEY_SKIP_ON_CHAR. We cannot use original
2002 * outputDeadKey_rePost() since we do not wish to reset dead_key
2003 * value.
2004 */
2005 if (dead_key == DEAD_KEY_TRANSIENT_IN_ON_CHAR)
2006 {
2007 outputDeadKey_rePost_Ex(msg,
2008 /*dead_key2set=*/DEAD_KEY_SKIP_ON_CHAR);
2009 return;
2010 }
2011
2012 if (dead_key != DEAD_KEY_SET_DEFAULT)
2013 {
2014 // should never happen - is there a way to make ASSERT here?
2015 return;
2016 }
2017
2018 /*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002019 * If a dead key was pressed and the user presses VK_SPACE,
2020 * VK_BACK, or VK_ESCAPE it means that he actually wants to deal
2021 * with the dead char now, so do nothing special and let Windows
2022 * handle it.
LemonBoy45684c62022-04-24 15:46:42 +01002023 *
2024 * Note that VK_SPACE combines with the dead_key's character and
2025 * only one WM_CHAR will be generated by TranslateMessage(), in
2026 * the two other cases two WM_CHAR will be generated: the dead
2027 * char and VK_BACK or VK_ESCAPE. That is most likely what the
2028 * user expects.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002029 */
2030 if ((vk == VK_SPACE || vk == VK_BACK || vk == VK_ESCAPE))
2031 {
Anton Sharonov3f026672022-07-26 21:26:18 +01002032 dead_key = DEAD_KEY_OFF;
LemonBoy45684c62022-04-24 15:46:42 +01002033 TranslateMessage(&msg);
2034 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002035 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01002036 // In modes where we are not typing, dead keys should behave
2037 // normally
Bram Moolenaar24959102022-05-07 20:01:16 +01002038 else if ((get_real_state()
2039 & (MODE_INSERT | MODE_CMDLINE | MODE_SELECT)) == 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002040 {
2041 outputDeadKey_rePost(msg);
2042 return;
2043 }
2044 }
2045
Bram Moolenaar734a8672019-12-02 22:49:38 +01002046 // Check for CTRL-BREAK
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002047 if (vk == VK_CANCEL)
2048 {
2049 trash_input_buf();
2050 got_int = TRUE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02002051 ctrl_break_was_pressed = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002052 string[0] = Ctrl_C;
2053 add_to_input_buf(string, 1);
2054 }
2055
LemonBoy77fc0b02022-04-22 22:45:52 +01002056 // This is an IME event or a synthetic keystroke, let Windows handle it.
2057 if (vk == VK_PROCESSKEY || vk == VK_PACKET)
2058 {
2059 TranslateMessage(&msg);
2060 return;
2061 }
2062
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002063 for (i = 0; special_keys[i].key_sym != 0; i++)
2064 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002065 // ignore VK_SPACE when ALT key pressed: system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002066 if (special_keys[i].key_sym == vk
Anton Sharonov6b568b12022-07-31 12:26:05 +01002067 && (vk != VK_SPACE || !(GetKeyState(VK_MENU) & 0x8000)))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002068 {
2069 /*
Bram Moolenaar945ec092016-06-08 21:17:43 +02002070 * Behave as expected if we have a dead key and the special key
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002071 * is a key that would normally trigger the dead key nominal
2072 * character output (such as a NUMPAD printable character or
2073 * the TAB key, etc...).
2074 */
Anton Sharonov6b568b12022-07-31 12:26:05 +01002075 if (dead_key == DEAD_KEY_SET_DEFAULT
2076 && (special_keys[i].vim_code0 == 'K'
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002077 || vk == VK_TAB || vk == CAR))
2078 {
2079 outputDeadKey_rePost(msg);
2080 return;
2081 }
2082
2083#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002084 // Check for <F10>: Windows selects the menu. When <F10> is
2085 // mapped we want to use the mapping instead.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002086 if (vk == VK_F10
2087 && gui.menu_is_active
2088 && check_map(k10, State, FALSE, TRUE, FALSE,
2089 NULL, NULL) == NULL)
2090 break;
2091#endif
LemonBoy45684c62022-04-24 15:46:42 +01002092 modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002093
2094 if (special_keys[i].vim_code1 == NUL)
2095 key = special_keys[i].vim_code0;
2096 else
2097 key = TO_SPECIAL(special_keys[i].vim_code0,
2098 special_keys[i].vim_code1);
2099 key = simplify_key(key, &modifiers);
2100 if (key == CSI)
2101 key = K_CSI;
2102
2103 if (modifiers)
2104 {
2105 string[0] = CSI;
2106 string[1] = KS_MODIFIER;
2107 string[2] = modifiers;
2108 add_to_input_buf(string, 3);
2109 }
2110
2111 if (IS_SPECIAL(key))
2112 {
2113 string[0] = CSI;
2114 string[1] = K_SECOND(key);
2115 string[2] = K_THIRD(key);
2116 add_to_input_buf(string, 3);
2117 }
2118 else
2119 {
2120 int len;
2121
Bram Moolenaar734a8672019-12-02 22:49:38 +01002122 // Handle "key" as a Unicode character.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002123 len = char_to_string(key, string, 40, FALSE);
2124 add_to_input_buf(string, len);
2125 }
2126 break;
2127 }
2128 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002129
2130 // Not a special key.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002131 if (special_keys[i].key_sym == 0)
2132 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002133 WCHAR ch[8];
2134 int len;
2135 int i;
2136 UINT scan_code;
2137
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002138 // Construct the state table with only a few modifiers, we don't
2139 // really care about the presence of Ctrl/Alt as those modifiers are
2140 // handled by Vim separately.
LemonBoy77fc0b02022-04-22 22:45:52 +01002141 memset(keyboard_state, 0, 256);
2142 if (GetKeyState(VK_SHIFT) & 0x8000)
2143 keyboard_state[VK_SHIFT] = 0x80;
LemonBoy0de73692022-04-23 11:08:11 +01002144 if (GetKeyState(VK_CAPITAL) & 0x0001)
2145 keyboard_state[VK_CAPITAL] = 0x01;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002146 // Alt-Gr is synthesized as Alt + Ctrl.
2147 if ((GetKeyState(VK_RMENU) & 0x8000)
2148 && (GetKeyState(VK_CONTROL) & 0x8000))
2149 {
LemonBoy77fc0b02022-04-22 22:45:52 +01002150 keyboard_state[VK_MENU] = 0x80;
Bram Moolenaar7bb6d562022-06-25 13:48:25 +01002151 keyboard_state[VK_CONTROL] = 0x80;
2152 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002153
2154 // Translate the virtual key according to the current keyboard
2155 // layout.
2156 scan_code = MapVirtualKey(vk, MAPVK_VK_TO_VSC);
2157 // Convert the scan-code into a sequence of zero or more unicode
2158 // codepoints.
2159 // If this is a dead key ToUnicode returns a negative value.
2160 len = ToUnicode(vk, scan_code, keyboard_state, ch, ARRAY_LENGTH(ch),
2161 0);
Anton Sharonov3f026672022-07-26 21:26:18 +01002162 if (len < 0)
2163 dead_key = DEAD_KEY_SET_DEFAULT;
LemonBoy77fc0b02022-04-22 22:45:52 +01002164
2165 if (len <= 0)
Anton Sharonov3f026672022-07-26 21:26:18 +01002166 {
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002167 int wm_char = NUL;
2168
2169 if (dead_key == DEAD_KEY_SET_DEFAULT
2170 && (GetKeyState(VK_CONTROL) & 0x8000))
2171 {
2172 if ( // AZERTY CTRL+dead_circumflex
2173 (vk == 221 && scan_code == 26)
2174 // QWERTZ CTRL+dead_circumflex
2175 || (vk == 220 && scan_code == 41))
2176 wm_char = '[';
2177 if ( // QWERTZ CTRL+dead_two-overdots
2178 (vk == 192 && scan_code == 27))
2179 wm_char = ']';
2180 }
2181 if (wm_char != NUL)
Anton Sharonov3f026672022-07-26 21:26:18 +01002182 {
2183 // post WM_CHAR='[' - which will be interpreted with CTRL
dundargocc57b5bc2022-11-02 13:30:51 +00002184 // still hold as ESC
Aedin Louis Xavierf10952e2022-11-16 12:02:28 +00002185 PostMessageW(msg.hwnd, WM_CHAR, wm_char, msg.lParam);
Anton Sharonov3f026672022-07-26 21:26:18 +01002186 // ask _OnChar() to not touch this state, wait for next key
2187 // press and maintain knowledge that we are "poisoned" with
2188 // "dead state"
2189 dead_key = DEAD_KEY_TRANSIENT_IN_ON_CHAR;
2190 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002191 return;
Anton Sharonov3f026672022-07-26 21:26:18 +01002192 }
LemonBoy77fc0b02022-04-22 22:45:52 +01002193
2194 // Post the message as TranslateMessage would do.
2195 if (msg.message == WM_KEYDOWN)
2196 {
2197 for (i = 0; i < len; i++)
2198 PostMessageW(msg.hwnd, WM_CHAR, ch[i], msg.lParam);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002199 }
2200 else
LemonBoy77fc0b02022-04-22 22:45:52 +01002201 {
2202 for (i = 0; i < len; i++)
2203 PostMessageW(msg.hwnd, WM_SYSCHAR, ch[i], msg.lParam);
2204 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002205 }
2206 }
2207#ifdef FEAT_MBYTE_IME
2208 else if (msg.message == WM_IME_NOTIFY)
2209 _OnImeNotify(msg.hwnd, (DWORD)msg.wParam, (DWORD)msg.lParam);
2210 else if (msg.message == WM_KEYUP && im_get_status())
Bram Moolenaar734a8672019-12-02 22:49:38 +01002211 // added for non-MS IME (Yasuhiro Matsumoto)
K.Takata4ac893f2022-01-20 12:44:28 +00002212 TranslateMessage(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002213#endif
2214
2215#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01002216 // Check for <F10>: Default effect is to select the menu. When <F10> is
2217 // mapped we need to stop it here to avoid strange effects (e.g., for the
2218 // key-up event)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002219 if (vk != VK_F10 || check_map(k10, State, FALSE, TRUE, FALSE,
2220 NULL, NULL) == NULL)
2221#endif
K.Takatab7057bd2022-01-21 11:37:07 +00002222 DispatchMessageW(&msg);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002223}
2224
2225/*
2226 * Catch up with any queued events. This may put keyboard input into the
2227 * input buffer, call resize call-backs, trigger timers etc. If there is
2228 * nothing in the event queue (& no timers pending), then we return
2229 * immediately.
2230 */
2231 void
2232gui_mch_update(void)
2233{
2234 MSG msg;
2235
2236 if (!s_busy_processing)
K.Takatab7057bd2022-01-21 11:37:07 +00002237 while (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002238 && !vim_is_input_buf_full())
2239 process_message();
2240}
2241
Bram Moolenaar4231da42016-06-02 14:30:04 +02002242 static void
2243remove_any_timer(void)
2244{
2245 MSG msg;
2246
2247 if (s_wait_timer != 0 && !s_timed_out)
2248 {
2249 KillTimer(NULL, s_wait_timer);
2250
Bram Moolenaar734a8672019-12-02 22:49:38 +01002251 // Eat spurious WM_TIMER messages
K.Takatab7057bd2022-01-21 11:37:07 +00002252 while (PeekMessageW(&msg, s_hwnd, WM_TIMER, WM_TIMER, PM_REMOVE))
Bram Moolenaar4231da42016-06-02 14:30:04 +02002253 ;
2254 s_wait_timer = 0;
2255 }
2256}
2257
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002258/*
2259 * GUI input routine called by gui_wait_for_chars(). Waits for a character
2260 * from the keyboard.
2261 * wtime == -1 Wait forever.
2262 * wtime == 0 This should never happen.
2263 * wtime > 0 Wait wtime milliseconds for a character.
2264 * Returns OK if a character was found to be available within the given time,
2265 * or FAIL otherwise.
2266 */
2267 int
2268gui_mch_wait_for_chars(int wtime)
2269{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002270 int focus;
2271
2272 s_timed_out = FALSE;
2273
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002274 if (wtime >= 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002275 {
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002276 // Don't do anything while processing a (scroll) message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002277 if (s_busy_processing)
2278 return FAIL;
Bram Moolenaar12dfc9e2019-01-28 22:32:58 +01002279
2280 // When called with "wtime" zero, just want one msec.
K.Takataa8ec4912022-02-03 14:32:33 +00002281 s_wait_timer = SetTimer(NULL, 0, (UINT)(wtime == 0 ? 1 : wtime),
2282 _OnTimer);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002283 }
2284
2285 allow_scrollbar = TRUE;
2286
2287 focus = gui.in_focus;
2288 while (!s_timed_out)
2289 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002290 // Stop or start blinking when focus changes
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002291 if (gui.in_focus != focus)
2292 {
2293 if (gui.in_focus)
2294 gui_mch_start_blink();
2295 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01002296 gui_mch_stop_blink(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002297 focus = gui.in_focus;
2298 }
2299
2300 if (s_need_activate)
2301 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002302 (void)SetForegroundWindow(s_hwnd);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002303 s_need_activate = FALSE;
2304 }
2305
Bram Moolenaar4231da42016-06-02 14:30:04 +02002306#ifdef FEAT_TIMERS
2307 did_add_timer = FALSE;
2308#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002309#ifdef MESSAGE_QUEUE
Bram Moolenaar89c00032019-09-04 13:53:21 +02002310 // Check channel I/O while waiting for a message.
Bram Moolenaar9186a272016-02-23 19:34:01 +01002311 for (;;)
2312 {
2313 MSG msg;
2314
2315 parse_queued_messages();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002316# ifdef FEAT_TIMERS
Bram Moolenaar89c00032019-09-04 13:53:21 +02002317 if (did_add_timer)
2318 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002319# endif
K.Takatab7057bd2022-01-21 11:37:07 +00002320 if (PeekMessageW(&msg, NULL, 0, 0, PM_NOREMOVE))
Bram Moolenaar62426e12017-08-13 15:37:58 +02002321 {
2322 process_message();
2323 break;
2324 }
Bram Moolenaar89c00032019-09-04 13:53:21 +02002325 else if (input_available()
Bram Moolenaar032f40a2020-11-18 15:21:50 +01002326 // TODO: The 10 msec is a compromise between laggy response
2327 // and consuming more CPU time. Better would be to handle
2328 // channel messages when they arrive.
2329 || MsgWaitForMultipleObjects(0, NULL, FALSE, 10,
Bram Moolenaar89c00032019-09-04 13:53:21 +02002330 QS_ALLINPUT) != WAIT_TIMEOUT)
Bram Moolenaar9186a272016-02-23 19:34:01 +01002331 break;
2332 }
Bram Moolenaar62426e12017-08-13 15:37:58 +02002333#else
Bram Moolenaar89c00032019-09-04 13:53:21 +02002334 // Don't use gui_mch_update() because then we will spin-lock until a
2335 // char arrives, instead we use GetMessage() to hang until an
2336 // event arrives. No need to check for input_buf_full because we are
2337 // returning as soon as it contains a single char -- webb
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002338 process_message();
Bram Moolenaar62426e12017-08-13 15:37:58 +02002339#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002340
2341 if (input_available())
2342 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02002343 remove_any_timer();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002344 allow_scrollbar = FALSE;
2345
Bram Moolenaar89c00032019-09-04 13:53:21 +02002346 // Clear pending mouse button, the release event may have been
2347 // taken by the dialog window. But don't do this when getting
2348 // focus, we need the mouse-up event then.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002349 if (!s_getting_focus)
2350 s_button_pending = -1;
2351
2352 return OK;
2353 }
Bram Moolenaar4231da42016-06-02 14:30:04 +02002354
2355#ifdef FEAT_TIMERS
2356 if (did_add_timer)
2357 {
Bram Moolenaar89c00032019-09-04 13:53:21 +02002358 // Need to recompute the waiting time.
Bram Moolenaar4231da42016-06-02 14:30:04 +02002359 remove_any_timer();
2360 break;
2361 }
2362#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002363 }
2364 allow_scrollbar = FALSE;
2365 return FAIL;
2366}
2367
2368/*
2369 * Clear a rectangular region of the screen from text pos (row1, col1) to
2370 * (row2, col2) inclusive.
2371 */
2372 void
2373gui_mch_clear_block(
2374 int row1,
2375 int col1,
2376 int row2,
2377 int col2)
2378{
2379 RECT rc;
2380
2381 /*
2382 * Clear one extra pixel at the far right, for when bold characters have
2383 * spilled over to the window border.
2384 * Note: FillRect() excludes right and bottom of rectangle.
2385 */
2386 rc.left = FILL_X(col1);
2387 rc.top = FILL_Y(row1);
2388 rc.right = FILL_X(col2 + 1) + (col2 == Columns - 1);
2389 rc.bottom = FILL_Y(row2 + 1);
2390 clear_rect(&rc);
2391}
2392
2393/*
2394 * Clear the whole text window.
2395 */
2396 void
2397gui_mch_clear_all(void)
2398{
2399 RECT rc;
2400
2401 rc.left = 0;
2402 rc.top = 0;
2403 rc.right = Columns * gui.char_width + 2 * gui.border_width;
2404 rc.bottom = Rows * gui.char_height + 2 * gui.border_width;
2405 clear_rect(&rc);
2406}
2407/*
2408 * Menu stuff.
2409 */
2410
2411 void
2412gui_mch_enable_menu(int flag)
2413{
2414#ifdef FEAT_MENU
2415 SetMenu(s_hwnd, flag ? s_menuBar : NULL);
2416#endif
2417}
2418
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002419 void
2420gui_mch_set_menu_pos(
Bram Moolenaar1266d672017-02-01 13:43:36 +01002421 int x UNUSED,
2422 int y UNUSED,
2423 int w UNUSED,
2424 int h UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002425{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002426 // It will be in the right place anyway
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002427}
2428
2429#if defined(FEAT_MENU) || defined(PROTO)
2430/*
2431 * Make menu item hidden or not hidden
2432 */
2433 void
2434gui_mch_menu_hidden(
2435 vimmenu_T *menu,
2436 int hidden)
2437{
2438 /*
2439 * This doesn't do what we want. Hmm, just grey the menu items for now.
2440 */
2441 /*
2442 if (hidden)
2443 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_DISABLED);
2444 else
2445 EnableMenuItem(s_menuBar, menu->id, MF_BYCOMMAND | MF_ENABLED);
2446 */
2447 gui_mch_menu_grey(menu, hidden);
2448}
2449
2450/*
2451 * This is called after setting all the menus to grey/hidden or not.
2452 */
2453 void
2454gui_mch_draw_menubar(void)
2455{
2456 DrawMenuBar(s_hwnd);
2457}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002458#endif // FEAT_MENU
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002459
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002460/*
2461 * Return the RGB value of a pixel as a long.
2462 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002463 guicolor_T
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002464gui_mch_get_rgb(guicolor_T pixel)
2465{
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02002466 return (guicolor_T)((GetRValue(pixel) << 16) + (GetGValue(pixel) << 8)
2467 + GetBValue(pixel));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002468}
2469
2470#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01002471/*
2472 * Convert pixels in X to dialog units
2473 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002474 static WORD
2475PixelToDialogX(int numPixels)
2476{
2477 return (WORD)((numPixels * 4) / s_dlgfntwidth);
2478}
2479
Bram Moolenaar734a8672019-12-02 22:49:38 +01002480/*
2481 * Convert pixels in Y to dialog units
2482 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002483 static WORD
2484PixelToDialogY(int numPixels)
2485{
2486 return (WORD)((numPixels * 8) / s_dlgfntheight);
2487}
2488
Bram Moolenaar734a8672019-12-02 22:49:38 +01002489/*
2490 * Return the width in pixels of the given text in the given DC.
2491 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002492 static int
2493GetTextWidth(HDC hdc, char_u *str, int len)
2494{
2495 SIZE size;
2496
2497 GetTextExtentPoint(hdc, (LPCSTR)str, len, &size);
2498 return size.cx;
2499}
2500
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002501/*
2502 * Return the width in pixels of the given text in the given DC, taking care
2503 * of 'encoding' to active codepage conversion.
2504 */
2505 static int
2506GetTextWidthEnc(HDC hdc, char_u *str, int len)
2507{
2508 SIZE size;
2509 WCHAR *wstr;
2510 int n;
2511 int wlen = len;
2512
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002513 wstr = enc_to_utf16(str, &wlen);
2514 if (wstr == NULL)
2515 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002516
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002517 n = GetTextExtentPointW(hdc, wstr, wlen, &size);
2518 vim_free(wstr);
2519 if (n)
2520 return size.cx;
2521 return 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002522}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002523
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002524static void get_work_area(RECT *spi_rect);
2525
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002526/*
2527 * A quick little routine that will center one window over another, handy for
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002528 * dialog boxes. Taken from the Win32SDK samples and modified for multiple
2529 * monitors.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002530 */
2531 static BOOL
2532CenterWindow(
2533 HWND hwndChild,
2534 HWND hwndParent)
2535{
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002536 HMONITOR mon;
2537 MONITORINFO moninfo;
2538 RECT rChild, rParent, rScreen;
2539 int wChild, hChild, wParent, hParent;
2540 int xNew, yNew;
2541 HDC hdc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002542
2543 GetWindowRect(hwndChild, &rChild);
2544 wChild = rChild.right - rChild.left;
2545 hChild = rChild.bottom - rChild.top;
2546
Bram Moolenaar734a8672019-12-02 22:49:38 +01002547 // If Vim is minimized put the window in the middle of the screen.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002548 if (hwndParent == NULL || IsMinimized(hwndParent))
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002549 get_work_area(&rParent);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002550 else
2551 GetWindowRect(hwndParent, &rParent);
2552 wParent = rParent.right - rParent.left;
2553 hParent = rParent.bottom - rParent.top;
2554
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002555 moninfo.cbSize = sizeof(MONITORINFO);
2556 mon = MonitorFromWindow(hwndChild, MONITOR_DEFAULTTOPRIMARY);
2557 if (mon != NULL && GetMonitorInfo(mon, &moninfo))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002558 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002559 rScreen = moninfo.rcWork;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002560 }
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002561 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002562 {
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002563 hdc = GetDC(hwndChild);
2564 rScreen.left = 0;
2565 rScreen.top = 0;
2566 rScreen.right = GetDeviceCaps(hdc, HORZRES);
2567 rScreen.bottom = GetDeviceCaps(hdc, VERTRES);
2568 ReleaseDC(hwndChild, hdc);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002569 }
2570
Bram Moolenaar87f3d202016-12-01 20:18:50 +01002571 xNew = rParent.left + ((wParent - wChild) / 2);
2572 if (xNew < rScreen.left)
2573 xNew = rScreen.left;
2574 else if ((xNew + wChild) > rScreen.right)
2575 xNew = rScreen.right - wChild;
2576
2577 yNew = rParent.top + ((hParent - hChild) / 2);
2578 if (yNew < rScreen.top)
2579 yNew = rScreen.top;
2580 else if ((yNew + hChild) > rScreen.bottom)
2581 yNew = rScreen.bottom - hChild;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002582
2583 return SetWindowPos(hwndChild, NULL, xNew, yNew, 0, 0,
2584 SWP_NOSIZE | SWP_NOZORDER);
2585}
Bram Moolenaar734a8672019-12-02 22:49:38 +01002586#endif // FEAT_GUI_DIALOG
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002587
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002588#if defined(FEAT_TOOLBAR) || defined(PROTO)
2589 void
2590gui_mch_show_toolbar(int showit)
2591{
2592 if (s_toolbarhwnd == NULL)
2593 return;
2594
2595 if (showit)
2596 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002597 // Enable unicode support
2598 SendMessage(s_toolbarhwnd, TB_SETUNICODEFORMAT, (WPARAM)TRUE,
2599 (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002600 ShowWindow(s_toolbarhwnd, SW_SHOW);
2601 }
2602 else
2603 ShowWindow(s_toolbarhwnd, SW_HIDE);
2604}
2605
Bram Moolenaar734a8672019-12-02 22:49:38 +01002606// The number of bitmaps is fixed. Exit is missing!
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002607# define TOOLBAR_BITMAP_COUNT 31
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002608
2609#endif
2610
2611#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2612 static void
2613add_tabline_popup_menu_entry(HMENU pmenu, UINT item_id, char_u *item_text)
2614{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002615 WCHAR *wn;
2616 MENUITEMINFOW infow;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002617
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002618 wn = enc_to_utf16(item_text, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002619 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002620 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002621
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002622 infow.cbSize = sizeof(infow);
2623 infow.fMask = MIIM_TYPE | MIIM_ID;
2624 infow.wID = item_id;
2625 infow.fType = MFT_STRING;
2626 infow.dwTypeData = wn;
2627 infow.cch = (UINT)wcslen(wn);
2628 InsertMenuItemW(pmenu, item_id, FALSE, &infow);
2629 vim_free(wn);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002630}
2631
2632 static void
2633show_tabline_popup_menu(void)
2634{
2635 HMENU tab_pmenu;
2636 long rval;
2637 POINT pt;
2638
Bram Moolenaar734a8672019-12-02 22:49:38 +01002639 // When ignoring events don't show the menu.
Martin Tournoij7904fa42022-10-04 16:28:45 +01002640 if (hold_gui_events || cmdwin_type != 0)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002641 return;
2642
2643 tab_pmenu = CreatePopupMenu();
2644 if (tab_pmenu == NULL)
2645 return;
2646
2647 if (first_tabpage->tp_next != NULL)
2648 add_tabline_popup_menu_entry(tab_pmenu,
2649 TABLINE_MENU_CLOSE, (char_u *)_("Close tab"));
2650 add_tabline_popup_menu_entry(tab_pmenu,
2651 TABLINE_MENU_NEW, (char_u *)_("New tab"));
2652 add_tabline_popup_menu_entry(tab_pmenu,
2653 TABLINE_MENU_OPEN, (char_u *)_("Open tab..."));
2654
2655 GetCursorPos(&pt);
2656 rval = TrackPopupMenuEx(tab_pmenu, TPM_RETURNCMD, pt.x, pt.y, s_tabhwnd,
2657 NULL);
2658
2659 DestroyMenu(tab_pmenu);
2660
Bram Moolenaar734a8672019-12-02 22:49:38 +01002661 // Add the string cmd into input buffer
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002662 if (rval > 0)
2663 {
2664 TCHITTESTINFO htinfo;
2665 int idx;
2666
2667 if (ScreenToClient(s_tabhwnd, &pt) == 0)
2668 return;
2669
2670 htinfo.pt.x = pt.x;
2671 htinfo.pt.y = pt.y;
2672 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
2673 if (idx == -1)
2674 idx = 0;
2675 else
2676 idx += 1;
2677
2678 send_tabline_menu_event(idx, (int)rval);
2679 }
2680}
2681
2682/*
2683 * Show or hide the tabline.
2684 */
2685 void
2686gui_mch_show_tabline(int showit)
2687{
2688 if (s_tabhwnd == NULL)
2689 return;
2690
2691 if (!showit != !showing_tabline)
2692 {
2693 if (showit)
2694 ShowWindow(s_tabhwnd, SW_SHOW);
2695 else
2696 ShowWindow(s_tabhwnd, SW_HIDE);
2697 showing_tabline = showit;
2698 }
2699}
2700
2701/*
2702 * Return TRUE when tabline is displayed.
2703 */
2704 int
2705gui_mch_showing_tabline(void)
2706{
2707 return s_tabhwnd != NULL && showing_tabline;
2708}
2709
2710/*
2711 * Update the labels of the tabline.
2712 */
2713 void
2714gui_mch_update_tabline(void)
2715{
2716 tabpage_T *tp;
2717 TCITEM tie;
2718 int nr = 0;
2719 int curtabidx = 0;
2720 int tabadded = 0;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002721 WCHAR *wstr = NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002722
2723 if (s_tabhwnd == NULL)
2724 return;
2725
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002726 // Enable unicode support
2727 SendMessage(s_tabhwnd, CCM_SETUNICODEFORMAT, (WPARAM)TRUE, (LPARAM)0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002728
2729 tie.mask = TCIF_TEXT;
2730 tie.iImage = -1;
2731
Bram Moolenaar734a8672019-12-02 22:49:38 +01002732 // Disable redraw for tab updates to eliminate O(N^2) draws.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002733 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)FALSE, 0);
2734
Bram Moolenaar734a8672019-12-02 22:49:38 +01002735 // Add a label for each tab page. They all contain the same text area.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002736 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2737 {
2738 if (tp == curtab)
2739 curtabidx = nr;
2740
2741 if (nr >= TabCtrl_GetItemCount(s_tabhwnd))
2742 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002743 // Add the tab
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002744 tie.pszText = "-Empty-";
2745 TabCtrl_InsertItem(s_tabhwnd, nr, &tie);
2746 tabadded = 1;
2747 }
2748
2749 get_tabline_label(tp, FALSE);
2750 tie.pszText = (LPSTR)NameBuff;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002751
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002752 wstr = enc_to_utf16(NameBuff, NULL);
2753 if (wstr != NULL)
2754 {
2755 TCITEMW tiw;
2756
2757 tiw.mask = TCIF_TEXT;
2758 tiw.iImage = -1;
2759 tiw.pszText = wstr;
2760 SendMessage(s_tabhwnd, TCM_SETITEMW, (WPARAM)nr, (LPARAM)&tiw);
2761 vim_free(wstr);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002762 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002763 }
2764
Bram Moolenaar734a8672019-12-02 22:49:38 +01002765 // Remove any old labels.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002766 while (nr < TabCtrl_GetItemCount(s_tabhwnd))
2767 TabCtrl_DeleteItem(s_tabhwnd, nr);
2768
2769 if (!tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2770 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2771
Bram Moolenaar734a8672019-12-02 22:49:38 +01002772 // Re-enable redraw and redraw.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002773 SendMessage(s_tabhwnd, WM_SETREDRAW, (WPARAM)TRUE, 0);
2774 RedrawWindow(s_tabhwnd, NULL, NULL,
2775 RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
2776
2777 if (tabadded && TabCtrl_GetCurSel(s_tabhwnd) != curtabidx)
2778 TabCtrl_SetCurSel(s_tabhwnd, curtabidx);
2779}
2780
2781/*
2782 * Set the current tab to "nr". First tab is 1.
2783 */
2784 void
2785gui_mch_set_curtab(int nr)
2786{
2787 if (s_tabhwnd == NULL)
2788 return;
2789
2790 if (TabCtrl_GetCurSel(s_tabhwnd) != nr - 1)
2791 TabCtrl_SetCurSel(s_tabhwnd, nr - 1);
2792}
2793
2794#endif
2795
2796/*
2797 * ":simalt" command.
2798 */
2799 void
2800ex_simalt(exarg_T *eap)
2801{
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002802 char_u *keys = eap->arg;
2803 int fill_typebuf = FALSE;
2804 char_u key_name[4];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002805
2806 PostMessage(s_hwnd, WM_SYSCOMMAND, (WPARAM)SC_KEYMENU, (LPARAM)0);
2807 while (*keys)
2808 {
2809 if (*keys == '~')
Bram Moolenaar734a8672019-12-02 22:49:38 +01002810 *keys = ' '; // for showing system menu
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002811 PostMessage(s_hwnd, WM_CHAR, (WPARAM)*keys, (LPARAM)0);
2812 keys++;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002813 fill_typebuf = TRUE;
2814 }
2815 if (fill_typebuf)
2816 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01002817 // Put a NOP in the typeahead buffer so that the message will get
2818 // processed.
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002819 key_name[0] = K_SPECIAL;
2820 key_name[1] = KS_EXTRA;
Bram Moolenaara21ccb72017-04-29 17:40:22 +02002821 key_name[2] = KE_NOP;
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002822 key_name[3] = NUL;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002823#if defined(FEAT_CLIENTSERVER) || defined(FEAT_EVAL)
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002824 typebuf_was_filled = TRUE;
Bram Moolenaar93bbf332019-10-23 21:43:16 +02002825#endif
Bram Moolenaar7a85b0f2017-04-22 15:17:40 +02002826 (void)ins_typebuf(key_name, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002827 }
2828}
2829
2830/*
2831 * Create the find & replace dialogs.
2832 * You can't have both at once: ":find" when replace is showing, destroys
2833 * the replace dialog first, and the other way around.
2834 */
2835#ifdef MSWIN_FIND_REPLACE
2836 static void
2837initialise_findrep(char_u *initial_string)
2838{
2839 int wword = FALSE;
2840 int mcase = !p_ic;
2841 char_u *entry_text;
2842
Bram Moolenaar734a8672019-12-02 22:49:38 +01002843 // Get the search string to use.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002844 entry_text = get_find_dialog_text(initial_string, &wword, &mcase);
2845
2846 s_findrep_struct.hwndOwner = s_hwnd;
2847 s_findrep_struct.Flags = FR_DOWN;
2848 if (mcase)
2849 s_findrep_struct.Flags |= FR_MATCHCASE;
2850 if (wword)
2851 s_findrep_struct.Flags |= FR_WHOLEWORD;
2852 if (entry_text != NULL && *entry_text != NUL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002853 {
2854 WCHAR *p = enc_to_utf16(entry_text, NULL);
2855 if (p != NULL)
2856 {
2857 int len = s_findrep_struct.wFindWhatLen - 1;
2858
2859 wcsncpy(s_findrep_struct.lpstrFindWhat, p, len);
2860 s_findrep_struct.lpstrFindWhat[len] = NUL;
2861 vim_free(p);
2862 }
2863 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002864 vim_free(entry_text);
2865}
2866#endif
2867
2868 static void
2869set_window_title(HWND hwnd, char *title)
2870{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002871 if (title != NULL)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002872 {
2873 WCHAR *wbuf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002874
Bram Moolenaar734a8672019-12-02 22:49:38 +01002875 // Convert the title from 'encoding' to UTF-16.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002876 wbuf = (WCHAR *)enc_to_utf16((char_u *)title, NULL);
2877 if (wbuf != NULL)
2878 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002879 SetWindowTextW(hwnd, wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002880 vim_free(wbuf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002881 }
2882 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02002883 else
2884 (void)SetWindowTextW(hwnd, NULL);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002885}
2886
2887 void
2888gui_mch_find_dialog(exarg_T *eap)
2889{
2890#ifdef MSWIN_FIND_REPLACE
2891 if (s_findrep_msg != 0)
2892 {
2893 if (IsWindow(s_findrep_hwnd) && !s_findrep_is_find)
2894 DestroyWindow(s_findrep_hwnd);
2895
2896 if (!IsWindow(s_findrep_hwnd))
2897 {
2898 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002899 s_findrep_hwnd = FindTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002900 }
2901
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002902 set_window_title(s_findrep_hwnd, _("Find string"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002903 (void)SetFocus(s_findrep_hwnd);
2904
2905 s_findrep_is_find = TRUE;
2906 }
2907#endif
2908}
2909
2910
2911 void
2912gui_mch_replace_dialog(exarg_T *eap)
2913{
2914#ifdef MSWIN_FIND_REPLACE
2915 if (s_findrep_msg != 0)
2916 {
2917 if (IsWindow(s_findrep_hwnd) && s_findrep_is_find)
2918 DestroyWindow(s_findrep_hwnd);
2919
2920 if (!IsWindow(s_findrep_hwnd))
2921 {
2922 initialise_findrep(eap->arg);
K.Takata45f9cfb2022-01-21 11:11:00 +00002923 s_findrep_hwnd = ReplaceTextW(&s_findrep_struct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002924 }
2925
Bram Moolenaar9e42c862018-07-20 05:03:16 +02002926 set_window_title(s_findrep_hwnd, _("Find & Replace"));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002927 (void)SetFocus(s_findrep_hwnd);
2928
2929 s_findrep_is_find = FALSE;
2930 }
2931#endif
2932}
2933
2934
2935/*
2936 * Set visibility of the pointer.
2937 */
2938 void
2939gui_mch_mousehide(int hide)
2940{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00002941 if (hide == gui.pointer_hidden)
2942 return;
2943
2944 ShowCursor(!hide);
2945 gui.pointer_hidden = hide;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002946}
2947
2948#ifdef FEAT_MENU
2949 static void
2950gui_mch_show_popupmenu_at(vimmenu_T *menu, int x, int y)
2951{
Bram Moolenaar734a8672019-12-02 22:49:38 +01002952 // Unhide the mouse, we don't get move events here.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002953 gui_mch_mousehide(FALSE);
2954
2955 (void)TrackPopupMenu(
2956 (HMENU)menu->submenu_id,
2957 TPM_LEFTALIGN | TPM_LEFTBUTTON,
2958 x, y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01002959 (int)0, //reserved param
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002960 s_hwnd,
2961 NULL);
2962 /*
2963 * NOTE: The pop-up menu can eat the mouse up event.
2964 * We deal with this in normal.c.
2965 */
2966}
2967#endif
2968
2969/*
2970 * Got a message when the system will go down.
2971 */
2972 static void
2973_OnEndSession(void)
2974{
2975 getout_preserve_modified(1);
2976}
2977
2978/*
2979 * Get this message when the user clicks on the cross in the top right corner
2980 * of a Windows95 window.
2981 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002982 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002983_OnClose(HWND hwnd UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002984{
2985 gui_shell_closed();
2986}
2987
2988/*
2989 * Get a message when the window is being destroyed.
2990 */
2991 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01002992_OnDestroy(HWND hwnd)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01002993{
2994 if (!destroying)
2995 _OnClose(hwnd);
2996}
2997
2998 static void
2999_OnPaint(
3000 HWND hwnd)
3001{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003002 if (IsMinimized(hwnd))
3003 return;
3004
3005 PAINTSTRUCT ps;
3006
3007 out_flush(); // make sure all output has been processed
3008 (void)BeginPaint(hwnd, &ps);
3009
3010 // prevent multi-byte characters from misprinting on an invalid
3011 // rectangle
3012 if (has_mbyte)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003013 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003014 RECT rect;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003015
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003016 GetClientRect(hwnd, &rect);
3017 ps.rcPaint.left = rect.left;
3018 ps.rcPaint.right = rect.right;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003019 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003020
3021 if (!IsRectEmpty(&ps.rcPaint))
3022 {
3023 gui_redraw(ps.rcPaint.left, ps.rcPaint.top,
3024 ps.rcPaint.right - ps.rcPaint.left + 1,
3025 ps.rcPaint.bottom - ps.rcPaint.top + 1);
3026 }
3027
3028 EndPaint(hwnd, &ps);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003029}
3030
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003031 static void
3032_OnSize(
3033 HWND hwnd,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003034 UINT state UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003035 int cx,
3036 int cy)
3037{
K.Takatac81e9bf2022-01-16 14:15:49 +00003038 if (!IsMinimized(hwnd) && !s_in_dpichanged)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003039 {
3040 gui_resize_shell(cx, cy);
3041
Bram Moolenaar734a8672019-12-02 22:49:38 +01003042 // Menu bar may wrap differently now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003043 gui_mswin_get_menu_height(TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003044 }
3045}
3046
3047 static void
3048_OnSetFocus(
3049 HWND hwnd,
3050 HWND hwndOldFocus)
3051{
3052 gui_focus_change(TRUE);
3053 s_getting_focus = TRUE;
K.Takata4ac893f2022-01-20 12:44:28 +00003054 (void)DefWindowProcW(hwnd, WM_SETFOCUS, (WPARAM)hwndOldFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003055}
3056
3057 static void
3058_OnKillFocus(
3059 HWND hwnd,
3060 HWND hwndNewFocus)
3061{
Bram Moolenaar3c620b02022-02-24 11:39:43 +00003062 if (destroying)
3063 return;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003064 gui_focus_change(FALSE);
3065 s_getting_focus = FALSE;
K.Takata4ac893f2022-01-20 12:44:28 +00003066 (void)DefWindowProcW(hwnd, WM_KILLFOCUS, (WPARAM)hwndNewFocus, 0);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003067}
3068
3069/*
3070 * Get a message when the user switches back to vim
3071 */
3072 static LRESULT
3073_OnActivateApp(
3074 HWND hwnd,
3075 BOOL fActivate,
3076 DWORD dwThreadId)
3077{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003078 // we call gui_focus_change() in _OnSetFocus()
3079 // gui_focus_change((int)fActivate);
K.Takata4ac893f2022-01-20 12:44:28 +00003080 return DefWindowProcW(hwnd, WM_ACTIVATEAPP, fActivate, (DWORD)dwThreadId);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003081}
3082
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003083 void
3084gui_mch_destroy_scrollbar(scrollbar_T *sb)
3085{
3086 DestroyWindow(sb->id);
3087}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003088
3089/*
3090 * Get current mouse coordinates in text window.
3091 */
3092 void
3093gui_mch_getmouse(int *x, int *y)
3094{
3095 RECT rct;
3096 POINT mp;
3097
3098 (void)GetWindowRect(s_textArea, &rct);
K.Takata45f9cfb2022-01-21 11:11:00 +00003099 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003100 *x = (int)(mp.x - rct.left);
3101 *y = (int)(mp.y - rct.top);
3102}
3103
3104/*
3105 * Move mouse pointer to character at (x, y).
3106 */
3107 void
3108gui_mch_setmouse(int x, int y)
3109{
3110 RECT rct;
3111
3112 (void)GetWindowRect(s_textArea, &rct);
3113 (void)SetCursorPos(x + gui.border_offset + rct.left,
3114 y + gui.border_offset + rct.top);
3115}
3116
3117 static void
3118gui_mswin_get_valid_dimensions(
3119 int w,
3120 int h,
3121 int *valid_w,
K.Takata4f2417f2021-06-05 16:25:32 +02003122 int *valid_h,
3123 int *cols,
3124 int *rows)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003125{
3126 int base_width, base_height;
3127
3128 base_width = gui_get_base_width()
K.Takatac81e9bf2022-01-16 14:15:49 +00003129 + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3130 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003131 base_height = gui_get_base_height()
K.Takatac81e9bf2022-01-16 14:15:49 +00003132 + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3133 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3134 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3135 + gui_mswin_get_menu_height(FALSE);
K.Takata4f2417f2021-06-05 16:25:32 +02003136 *cols = (w - base_width) / gui.char_width;
3137 *rows = (h - base_height) / gui.char_height;
3138 *valid_w = base_width + *cols * gui.char_width;
3139 *valid_h = base_height + *rows * gui.char_height;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003140}
3141
3142 void
3143gui_mch_flash(int msec)
3144{
3145 RECT rc;
3146
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003147#if defined(FEAT_DIRECTX)
3148 if (IS_ENABLE_DIRECTX())
3149 DWriteContext_Flush(s_dwc);
3150#endif
3151
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003152 /*
3153 * Note: InvertRect() excludes right and bottom of rectangle.
3154 */
3155 rc.left = 0;
3156 rc.top = 0;
3157 rc.right = gui.num_cols * gui.char_width;
3158 rc.bottom = gui.num_rows * gui.char_height;
3159 InvertRect(s_hdc, &rc);
Bram Moolenaar734a8672019-12-02 22:49:38 +01003160 gui_mch_flush(); // make sure it's displayed
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003161
Bram Moolenaar734a8672019-12-02 22:49:38 +01003162 ui_delay((long)msec, TRUE); // wait for a few msec
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003163
3164 InvertRect(s_hdc, &rc);
3165}
3166
3167/*
Bram Moolenaar185577e2020-10-29 20:08:21 +01003168 * Check if the specified point is on-screen. (multi-monitor aware)
3169 */
3170 static BOOL
3171is_point_onscreen(int x, int y)
3172{
3173 POINT pt = {x, y};
3174
3175 return MonitorFromPoint(pt, MONITOR_DEFAULTTONULL) != NULL;
3176}
3177
3178/*
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003179 * Check if the whole client area of the specified window is on-screen.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003180 *
3181 * Note about DirectX: Windows 10 1809 or above no longer maintains image of
3182 * the window portion that is off-screen. Scrolling by DWriteContext_Scroll()
3183 * only works when the whole window is on-screen.
3184 */
3185 static BOOL
3186is_window_onscreen(HWND hwnd)
3187{
3188 RECT rc;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003189 POINT p1, p2;
Bram Moolenaar185577e2020-10-29 20:08:21 +01003190
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003191 GetClientRect(hwnd, &rc);
3192 p1.x = rc.left;
3193 p1.y = rc.top;
3194 p2.x = rc.right - 1;
3195 p2.y = rc.bottom - 1;
3196 ClientToScreen(hwnd, &p1);
3197 ClientToScreen(hwnd, &p2);
Bram Moolenaar185577e2020-10-29 20:08:21 +01003198
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003199 if (!is_point_onscreen(p1.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003200 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003201 if (!is_point_onscreen(p1.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003202 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003203 if (!is_point_onscreen(p2.x, p1.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003204 return FALSE;
Bram Moolenaarf01af9c2022-03-01 16:02:26 +00003205 if (!is_point_onscreen(p2.x, p2.y))
Bram Moolenaar185577e2020-10-29 20:08:21 +01003206 return FALSE;
3207 return TRUE;
3208}
3209
3210/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003211 * Return flags used for scrolling.
3212 * The SW_INVALIDATE is required when part of the window is covered or
3213 * off-screen. Refer to MS KB Q75236.
3214 */
3215 static int
3216get_scroll_flags(void)
3217{
3218 HWND hwnd;
3219 RECT rcVim, rcOther, rcDest;
3220
Bram Moolenaar185577e2020-10-29 20:08:21 +01003221 // Check if the window is (partly) off-screen.
3222 if (!is_window_onscreen(s_hwnd))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003223 return SW_INVALIDATE;
3224
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00003225 // Check if there is a window (partly) on top of us.
Bram Moolenaar185577e2020-10-29 20:08:21 +01003226 GetWindowRect(s_hwnd, &rcVim);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003227 for (hwnd = s_hwnd; (hwnd = GetWindow(hwnd, GW_HWNDPREV)) != (HWND)0; )
3228 if (IsWindowVisible(hwnd))
3229 {
3230 GetWindowRect(hwnd, &rcOther);
3231 if (IntersectRect(&rcDest, &rcVim, &rcOther))
3232 return SW_INVALIDATE;
3233 }
3234 return 0;
3235}
3236
3237/*
3238 * On some Intel GPUs, the regions drawn just prior to ScrollWindowEx()
3239 * may not be scrolled out properly.
3240 * For gVim, when _OnScroll() is repeated, the character at the
3241 * previous cursor position may be left drawn after scroll.
3242 * The problem can be avoided by calling GetPixel() to get a pixel in
3243 * the region before ScrollWindowEx().
3244 */
3245 static void
3246intel_gpu_workaround(void)
3247{
3248 GetPixel(s_hdc, FILL_X(gui.col), FILL_Y(gui.row));
3249}
3250
3251/*
3252 * Delete the given number of lines from the given row, scrolling up any
3253 * text further down within the scroll region.
3254 */
3255 void
3256gui_mch_delete_lines(
3257 int row,
3258 int num_lines)
3259{
3260 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003261
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003262 rc.left = FILL_X(gui.scroll_region_left);
3263 rc.right = FILL_X(gui.scroll_region_right + 1);
3264 rc.top = FILL_Y(row);
3265 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
3266
Bram Moolenaar92467d32017-12-05 13:22:16 +01003267#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003268 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003269 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003270 DWriteContext_Scroll(s_dwc, 0, -num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003271 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003272 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003273#endif
3274 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003275#if defined(FEAT_DIRECTX)
3276 if (IS_ENABLE_DIRECTX())
3277 DWriteContext_Flush(s_dwc);
3278#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003279 intel_gpu_workaround();
3280 ScrollWindowEx(s_textArea, 0, -num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003281 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003282 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003283 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003284
Bram Moolenaar734a8672019-12-02 22:49:38 +01003285 // This seems to be required to avoid the cursor disappearing when
3286 // scrolling such that the cursor ends up in the top-left character on
3287 // the screen... But why? (Webb)
3288 // It's probably fixed by disabling drawing the cursor while scrolling.
3289 // gui.cursor_is_valid = FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003290
3291 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
3292 gui.scroll_region_left,
3293 gui.scroll_region_bot, gui.scroll_region_right);
3294}
3295
3296/*
3297 * Insert the given number of lines before the given row, scrolling down any
3298 * following text within the scroll region.
3299 */
3300 void
3301gui_mch_insert_lines(
3302 int row,
3303 int num_lines)
3304{
3305 RECT rc;
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01003306
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003307 rc.left = FILL_X(gui.scroll_region_left);
3308 rc.right = FILL_X(gui.scroll_region_right + 1);
3309 rc.top = FILL_Y(row);
3310 rc.bottom = FILL_Y(gui.scroll_region_bot + 1);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003311
3312#if defined(FEAT_DIRECTX)
Bram Moolenaar185577e2020-10-29 20:08:21 +01003313 if (IS_ENABLE_DIRECTX() && is_window_onscreen(s_hwnd))
Bram Moolenaar92467d32017-12-05 13:22:16 +01003314 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01003315 DWriteContext_Scroll(s_dwc, 0, num_lines * gui.char_height, &rc);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003316 }
Bram Moolenaara338adc2018-01-31 20:51:47 +01003317 else
Bram Moolenaar92467d32017-12-05 13:22:16 +01003318#endif
3319 {
Bram Moolenaar185577e2020-10-29 20:08:21 +01003320#if defined(FEAT_DIRECTX)
3321 if (IS_ENABLE_DIRECTX())
3322 DWriteContext_Flush(s_dwc);
3323#endif
Bram Moolenaar92467d32017-12-05 13:22:16 +01003324 intel_gpu_workaround();
Bram Moolenaar734a8672019-12-02 22:49:38 +01003325 // The SW_INVALIDATE is required when part of the window is covered or
3326 // off-screen. How do we avoid it when it's not needed?
Bram Moolenaar92467d32017-12-05 13:22:16 +01003327 ScrollWindowEx(s_textArea, 0, num_lines * gui.char_height,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003328 &rc, &rc, NULL, NULL, get_scroll_flags());
Bram Moolenaar7f88b652017-12-14 13:15:19 +01003329 UpdateWindow(s_textArea);
Bram Moolenaar92467d32017-12-05 13:22:16 +01003330 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003331
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003332 gui_clear_block(row, gui.scroll_region_left,
3333 row + num_lines - 1, gui.scroll_region_right);
3334}
3335
3336
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003337 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01003338gui_mch_exit(int rc UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003339{
3340#if defined(FEAT_DIRECTX)
3341 DWriteContext_Close(s_dwc);
3342 DWrite_Final();
3343 s_dwc = NULL;
3344#endif
3345
3346 ReleaseDC(s_textArea, s_hdc);
3347 DeleteObject(s_brush);
3348
3349#ifdef FEAT_TEAROFF
Bram Moolenaar734a8672019-12-02 22:49:38 +01003350 // Unload the tearoff bitmap
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003351 (void)DeleteObject((HGDIOBJ)s_htearbitmap);
3352#endif
3353
Bram Moolenaar734a8672019-12-02 22:49:38 +01003354 // Destroy our window (if we have one).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003355 if (s_hwnd != NULL)
3356 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003357 destroying = TRUE; // ignore WM_DESTROY message now
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003358 DestroyWindow(s_hwnd);
3359 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003360}
3361
3362 static char_u *
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003363logfont2name(LOGFONTW lf)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003364{
3365 char *p;
3366 char *res;
3367 char *charset_name;
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003368 char *quality_name;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003369 char *font_name;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003370 int points;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003371
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003372 font_name = (char *)utf16_to_enc(lf.lfFaceName, NULL);
3373 if (font_name == NULL)
3374 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003375 charset_name = charset_id2name((int)lf.lfCharSet);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003376 quality_name = quality_id2name((int)lf.lfQuality);
3377
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003378 res = alloc(strlen(font_name) + 30
Bram Moolenaar2155a6a2019-04-27 19:15:45 +02003379 + (charset_name == NULL ? 0 : strlen(charset_name) + 2)
Bram Moolenaar964b3742019-05-24 18:54:09 +02003380 + (quality_name == NULL ? 0 : strlen(quality_name) + 2));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003381 if (res != NULL)
3382 {
3383 p = res;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003384 // make a normal font string out of the lf thing:
3385 points = pixels_to_points(
3386 lf.lfHeight < 0 ? -lf.lfHeight : lf.lfHeight, TRUE);
3387 if (lf.lfWeight == FW_NORMAL || lf.lfWeight == FW_BOLD)
3388 sprintf((char *)p, "%s:h%d", font_name, points);
3389 else
Bram Moolenaara0e67fc2019-04-29 21:46:26 +02003390 sprintf((char *)p, "%s:h%d:W%ld", font_name, points, lf.lfWeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003391 while (*p)
3392 {
3393 if (*p == ' ')
3394 *p = '_';
3395 ++p;
3396 }
3397 if (lf.lfItalic)
3398 STRCAT(p, ":i");
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003399 if (lf.lfWeight == FW_BOLD)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003400 STRCAT(p, ":b");
3401 if (lf.lfUnderline)
3402 STRCAT(p, ":u");
3403 if (lf.lfStrikeOut)
3404 STRCAT(p, ":s");
3405 if (charset_name != NULL)
3406 {
3407 STRCAT(p, ":c");
3408 STRCAT(p, charset_name);
3409 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003410 if (quality_name != NULL)
3411 {
3412 STRCAT(p, ":q");
3413 STRCAT(p, quality_name);
3414 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003415 }
3416
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003417 vim_free(font_name);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003418 return (char_u *)res;
3419}
3420
3421
3422#ifdef FEAT_MBYTE_IME
3423/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003424 * Set correct LOGFONTW to IME. Use 'guifontwide' if available, otherwise use
K.Takatac81e9bf2022-01-16 14:15:49 +00003425 * 'guifont'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003426 */
3427 static void
3428update_im_font(void)
3429{
K.Takatac81e9bf2022-01-16 14:15:49 +00003430 LOGFONTW lf_wide, lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003431
3432 if (p_guifontwide != NULL && *p_guifontwide != NUL
3433 && gui.wide_font != NOFONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003434 && GetObjectW((HFONT)gui.wide_font, sizeof(lf_wide), &lf_wide))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003435 norm_logfont = lf_wide;
3436 else
3437 norm_logfont = sub_logfont;
K.Takatac81e9bf2022-01-16 14:15:49 +00003438
3439 lf = norm_logfont;
3440 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
3441 // Work around when PerMonitorV2 is not enabled in the process level.
3442 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
3443 im_set_font(&lf);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003444}
3445#endif
3446
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003447/*
3448 * Handler of gui.wide_font (p_guifontwide) changed notification.
3449 */
3450 void
3451gui_mch_wide_font_changed(void)
3452{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003453 LOGFONTW lf;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003454
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003455#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003456 update_im_font();
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003457#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003458
3459 gui_mch_free_font(gui.wide_ital_font);
3460 gui.wide_ital_font = NOFONT;
3461 gui_mch_free_font(gui.wide_bold_font);
3462 gui.wide_bold_font = NOFONT;
3463 gui_mch_free_font(gui.wide_boldital_font);
3464 gui.wide_boldital_font = NOFONT;
3465
3466 if (gui.wide_font
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003467 && GetObjectW((HFONT)gui.wide_font, sizeof(lf), &lf))
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003468 {
3469 if (!lf.lfItalic)
3470 {
3471 lf.lfItalic = TRUE;
3472 gui.wide_ital_font = get_font_handle(&lf);
3473 lf.lfItalic = FALSE;
3474 }
3475 if (lf.lfWeight < FW_BOLD)
3476 {
3477 lf.lfWeight = FW_BOLD;
3478 gui.wide_bold_font = get_font_handle(&lf);
3479 if (!lf.lfItalic)
3480 {
3481 lf.lfItalic = TRUE;
3482 gui.wide_boldital_font = get_font_handle(&lf);
3483 }
3484 }
3485 }
3486}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003487
3488/*
3489 * Initialise vim to use the font with the given name.
3490 * Return FAIL if the font could not be loaded, OK otherwise.
3491 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003492 int
Bram Moolenaar1266d672017-02-01 13:43:36 +01003493gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003494{
K.Takatac81e9bf2022-01-16 14:15:49 +00003495 LOGFONTW lf, lfOrig;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003496 GuiFont font = NOFONT;
3497 char_u *p;
3498
Bram Moolenaar734a8672019-12-02 22:49:38 +01003499 // Load the font
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003500 if (get_logfont(&lf, font_name, NULL, TRUE) == OK)
K.Takatac81e9bf2022-01-16 14:15:49 +00003501 {
3502 lfOrig = lf;
3503 lf.lfHeight = adjust_fontsize_by_dpi(lf.lfHeight);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003504 font = get_font_handle(&lf);
K.Takatac81e9bf2022-01-16 14:15:49 +00003505 }
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003506 if (font == NOFONT)
3507 return FAIL;
3508
3509 if (font_name == NULL)
K.Takata45f9cfb2022-01-21 11:11:00 +00003510 font_name = (char_u *)"";
K.Takata4ac893f2022-01-20 12:44:28 +00003511#ifdef FEAT_MBYTE_IME
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003512 norm_logfont = lf;
3513 sub_logfont = lf;
K.Takatac81e9bf2022-01-16 14:15:49 +00003514 if (!s_in_dpichanged)
3515 update_im_font();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003516#endif
3517 gui_mch_free_font(gui.norm_font);
3518 gui.norm_font = font;
K.Takatac81e9bf2022-01-16 14:15:49 +00003519 current_font_height = lfOrig.lfHeight;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003520 GetFontSize(font);
3521
K.Takatac81e9bf2022-01-16 14:15:49 +00003522 p = logfont2name(lfOrig);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003523 if (p != NULL)
3524 {
3525 hl_set_font_name(p);
3526
Bram Moolenaar734a8672019-12-02 22:49:38 +01003527 // When setting 'guifont' to "*" replace it with the actual font name.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003528 if (STRCMP(font_name, "*") == 0 && STRCMP(p_guifont, "*") == 0)
3529 {
3530 vim_free(p_guifont);
3531 p_guifont = p;
3532 }
3533 else
3534 vim_free(p);
3535 }
3536
3537 gui_mch_free_font(gui.ital_font);
3538 gui.ital_font = NOFONT;
3539 gui_mch_free_font(gui.bold_font);
3540 gui.bold_font = NOFONT;
3541 gui_mch_free_font(gui.boldital_font);
3542 gui.boldital_font = NOFONT;
3543
3544 if (!lf.lfItalic)
3545 {
3546 lf.lfItalic = TRUE;
3547 gui.ital_font = get_font_handle(&lf);
3548 lf.lfItalic = FALSE;
3549 }
3550 if (lf.lfWeight < FW_BOLD)
3551 {
3552 lf.lfWeight = FW_BOLD;
3553 gui.bold_font = get_font_handle(&lf);
3554 if (!lf.lfItalic)
3555 {
3556 lf.lfItalic = TRUE;
3557 gui.boldital_font = get_font_handle(&lf);
3558 }
3559 }
3560
3561 return OK;
3562}
3563
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003564/*
3565 * Return TRUE if the GUI window is maximized, filling the whole screen.
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003566 * Also return TRUE if the window is snapped.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003567 */
3568 int
3569gui_mch_maximized(void)
3570{
3571 WINDOWPLACEMENT wp;
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003572 RECT rc;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003573
3574 wp.length = sizeof(WINDOWPLACEMENT);
3575 if (GetWindowPlacement(s_hwnd, &wp))
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003576 {
3577 if (wp.showCmd == SW_SHOWMAXIMIZED
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003578 || (wp.showCmd == SW_SHOWMINIMIZED
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003579 && wp.flags == WPF_RESTORETOMAXIMIZED))
3580 return TRUE;
3581 if (wp.showCmd == SW_SHOWMINIMIZED)
3582 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003583
Bram Moolenaarb68ced52020-07-17 22:26:53 +02003584 // Assume the window is snapped when the sizes from two APIs differ.
3585 GetWindowRect(s_hwnd, &rc);
3586 if ((rc.right - rc.left !=
3587 wp.rcNormalPosition.right - wp.rcNormalPosition.left)
3588 || (rc.bottom - rc.top !=
3589 wp.rcNormalPosition.bottom - wp.rcNormalPosition.top))
3590 return TRUE;
3591 }
3592 return FALSE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003593}
3594
3595/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01003596 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
3597 * is set. Compute the new Rows and Columns. This is like resizing the
3598 * window.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003599 */
3600 void
3601gui_mch_newfont(void)
3602{
3603 RECT rect;
3604
3605 GetWindowRect(s_hwnd, &rect);
3606 if (win_socket_id == 0)
3607 {
3608 gui_resize_shell(rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00003609 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
3610 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003611 rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00003612 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
3613 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
3614 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
3615 - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003616 }
3617 else
3618 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003619 // Inside another window, don't use the frame and border.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003620 gui_resize_shell(rect.right - rect.left,
K.Takatac81e9bf2022-01-16 14:15:49 +00003621 rect.bottom - rect.top - gui_mswin_get_menu_height(FALSE));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003622 }
3623}
3624
3625/*
3626 * Set the window title
3627 */
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003628 void
3629gui_mch_settitle(
3630 char_u *title,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003631 char_u *icon UNUSED)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003632{
3633 set_window_title(s_hwnd, (title == NULL ? "VIM" : (char *)title));
3634}
3635
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003636#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar734a8672019-12-02 22:49:38 +01003637// Table for shape IDCs. Keep in sync with the mshape_names[] table in
3638// misc2.c!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003639static LPCSTR mshape_idcs[] =
3640{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003641 IDC_ARROW, // arrow
3642 MAKEINTRESOURCE(0), // blank
3643 IDC_IBEAM, // beam
3644 IDC_SIZENS, // updown
3645 IDC_SIZENS, // udsizing
3646 IDC_SIZEWE, // leftright
3647 IDC_SIZEWE, // lrsizing
3648 IDC_WAIT, // busy
3649 IDC_NO, // no
3650 IDC_ARROW, // crosshair
3651 IDC_ARROW, // hand1
3652 IDC_ARROW, // hand2
3653 IDC_ARROW, // pencil
3654 IDC_ARROW, // question
3655 IDC_ARROW, // right-arrow
3656 IDC_UPARROW, // up-arrow
3657 IDC_ARROW // last one
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003658};
3659
3660 void
3661mch_set_mouse_shape(int shape)
3662{
3663 LPCSTR idc;
3664
3665 if (shape == MSHAPE_HIDE)
3666 ShowCursor(FALSE);
3667 else
3668 {
3669 if (shape >= MSHAPE_NUMBERED)
3670 idc = IDC_ARROW;
3671 else
3672 idc = mshape_idcs[shape];
Bram Moolenaara0754902019-11-19 23:01:28 +01003673 SetClassLongPtr(s_textArea, GCLP_HCURSOR, (LONG_PTR)LoadCursor(NULL, idc));
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003674 if (!p_mh)
3675 {
3676 POINT mp;
3677
Bram Moolenaar734a8672019-12-02 22:49:38 +01003678 // Set the position to make it redrawn with the new shape.
K.Takata45f9cfb2022-01-21 11:11:00 +00003679 (void)GetCursorPos(&mp);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003680 (void)SetCursorPos(mp.x, mp.y);
3681 ShowCursor(TRUE);
3682 }
3683 }
3684}
3685#endif
3686
Bram Moolenaara6b7a082016-08-10 20:53:05 +02003687#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003688/*
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003689 * Wide version of convert_filter().
3690 */
3691 static WCHAR *
3692convert_filterW(char_u *s)
3693{
3694 char_u *tmp;
3695 int len;
3696 WCHAR *res;
3697
3698 tmp = convert_filter(s);
3699 if (tmp == NULL)
3700 return NULL;
3701 len = (int)STRLEN(s) + 3;
3702 res = enc_to_utf16(tmp, &len);
3703 vim_free(tmp);
3704 return res;
3705}
3706
3707/*
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003708 * Pop open a file browser and return the file selected, in allocated memory,
3709 * or NULL if Cancel is hit.
3710 * saving - TRUE if the file will be saved to, FALSE if it will be opened.
3711 * title - Title message for the file browser dialog.
3712 * dflt - Default name of file.
3713 * ext - Default extension to be added to files without extensions.
3714 * initdir - directory in which to open the browser (NULL = current dir)
3715 * filter - Filter for matched files to choose from.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003716 */
Bram Moolenaar091806d2019-01-24 16:27:46 +01003717 char_u *
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01003718gui_mch_browse(
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003719 int saving,
3720 char_u *title,
3721 char_u *dflt,
3722 char_u *ext,
3723 char_u *initdir,
3724 char_u *filter)
3725{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003726 // We always use the wide function. This means enc_to_utf16() must work,
3727 // otherwise it fails miserably!
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003728 OPENFILENAMEW fileStruct;
3729 WCHAR fileBuf[MAXPATHL];
3730 WCHAR *wp;
3731 int i;
3732 WCHAR *titlep = NULL;
3733 WCHAR *extp = NULL;
3734 WCHAR *initdirp = NULL;
3735 WCHAR *filterp;
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003736 char_u *p, *q;
K.Takata14b8d6a2022-01-20 15:05:22 +00003737 BOOL ret;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003738
3739 if (dflt == NULL)
3740 fileBuf[0] = NUL;
3741 else
3742 {
3743 wp = enc_to_utf16(dflt, NULL);
3744 if (wp == NULL)
3745 fileBuf[0] = NUL;
3746 else
3747 {
3748 for (i = 0; wp[i] != NUL && i < MAXPATHL - 1; ++i)
3749 fileBuf[i] = wp[i];
3750 fileBuf[i] = NUL;
3751 vim_free(wp);
3752 }
3753 }
3754
Bram Moolenaar734a8672019-12-02 22:49:38 +01003755 // Convert the filter to Windows format.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003756 filterp = convert_filterW(filter);
3757
Bram Moolenaara80faa82020-04-12 19:37:17 +02003758 CLEAR_FIELD(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003759# ifdef OPENFILENAME_SIZE_VERSION_400W
Bram Moolenaar734a8672019-12-02 22:49:38 +01003760 // be compatible with Windows NT 4.0
Bram Moolenaar89e375a2016-03-15 18:09:57 +01003761 fileStruct.lStructSize = OPENFILENAME_SIZE_VERSION_400W;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003762# else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003763 fileStruct.lStructSize = sizeof(fileStruct);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003764# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003765
3766 if (title != NULL)
3767 titlep = enc_to_utf16(title, NULL);
3768 fileStruct.lpstrTitle = titlep;
3769
3770 if (ext != NULL)
3771 extp = enc_to_utf16(ext, NULL);
3772 fileStruct.lpstrDefExt = extp;
3773
3774 fileStruct.lpstrFile = fileBuf;
3775 fileStruct.nMaxFile = MAXPATHL;
3776 fileStruct.lpstrFilter = filterp;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003777 fileStruct.hwndOwner = s_hwnd; // main Vim window is owner
3778 // has an initial dir been specified?
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003779 if (initdir != NULL && *initdir != NUL)
3780 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01003781 // Must have backslashes here, no matter what 'shellslash' says
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003782 initdirp = enc_to_utf16(initdir, NULL);
3783 if (initdirp != NULL)
3784 {
3785 for (wp = initdirp; *wp != NUL; ++wp)
3786 if (*wp == '/')
3787 *wp = '\\';
3788 }
3789 fileStruct.lpstrInitialDir = initdirp;
3790 }
3791
3792 /*
3793 * TODO: Allow selection of multiple files. Needs another arg to this
3794 * function to ask for it, and need to use OFN_ALLOWMULTISELECT below.
3795 * Also, should we use OFN_FILEMUSTEXIST when opening? Vim can edit on
3796 * files that don't exist yet, so I haven't put it in. What about
3797 * OFN_PATHMUSTEXIST?
3798 * Don't use OFN_OVERWRITEPROMPT, Vim has its own ":confirm" dialog.
3799 */
3800 fileStruct.Flags = (OFN_NOCHANGEDIR | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003801# ifdef FEAT_SHORTCUT
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003802 if (curbuf->b_p_bin)
3803 fileStruct.Flags |= OFN_NODEREFERENCELINKS;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003804# endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003805 if (saving)
K.Takata14b8d6a2022-01-20 15:05:22 +00003806 ret = GetSaveFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003807 else
K.Takata14b8d6a2022-01-20 15:05:22 +00003808 ret = GetOpenFileNameW(&fileStruct);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003809
3810 vim_free(filterp);
3811 vim_free(initdirp);
3812 vim_free(titlep);
3813 vim_free(extp);
3814
K.Takata14b8d6a2022-01-20 15:05:22 +00003815 if (!ret)
3816 return NULL;
3817
3818 // Convert from UTF-16 to 'encoding'.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003819 p = utf16_to_enc(fileBuf, NULL);
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003820 if (p == NULL)
3821 return NULL;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003822
Bram Moolenaar734a8672019-12-02 22:49:38 +01003823 // Give focus back to main window (when using MDI).
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003824 SetFocus(s_hwnd);
3825
Bram Moolenaar734a8672019-12-02 22:49:38 +01003826 // Shorten the file name if possible
Bram Moolenaar7ff8a3c2018-09-22 14:39:15 +02003827 q = vim_strsave(shorten_fname1(p));
3828 vim_free(p);
3829 return q;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003830}
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003831
3832
3833/*
3834 * Convert the string s to the proper format for a filter string by replacing
3835 * the \t and \n delimiters with \0.
3836 * Returns the converted string in allocated memory.
3837 *
3838 * Keep in sync with convert_filterW() above!
3839 */
3840 static char_u *
3841convert_filter(char_u *s)
3842{
3843 char_u *res;
3844 unsigned s_len = (unsigned)STRLEN(s);
3845 unsigned i;
3846
3847 res = alloc(s_len + 3);
3848 if (res != NULL)
3849 {
3850 for (i = 0; i < s_len; ++i)
3851 if (s[i] == '\t' || s[i] == '\n')
3852 res[i] = '\0';
3853 else
3854 res[i] = s[i];
3855 res[s_len] = NUL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01003856 // Add two extra NULs to make sure it's properly terminated.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003857 res[s_len + 1] = NUL;
3858 res[s_len + 2] = NUL;
3859 }
3860 return res;
3861}
3862
3863/*
3864 * Select a directory.
3865 */
3866 char_u *
3867gui_mch_browsedir(char_u *title, char_u *initdir)
3868{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003869 // We fake this: Use a filter that doesn't select anything and a default
3870 // file name that won't be used.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003871 return gui_mch_browse(0, title, (char_u *)_("Not Used"), NULL,
3872 initdir, (char_u *)_("Directory\t*.nothing\n"));
3873}
Bram Moolenaar734a8672019-12-02 22:49:38 +01003874#endif // FEAT_BROWSE
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003875
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003876 static void
3877_OnDropFiles(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003878 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003879 HDROP hDrop)
3880{
Bram Moolenaar4033c552017-09-16 20:54:51 +02003881#define BUFPATHLEN _MAX_PATH
3882#define DRAGQVAL 0xFFFFFFFF
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003883 WCHAR wszFile[BUFPATHLEN];
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003884 char szFile[BUFPATHLEN];
3885 UINT cFiles = DragQueryFile(hDrop, DRAGQVAL, NULL, 0);
3886 UINT i;
3887 char_u **fnames;
3888 POINT pt;
3889 int_u modifiers = 0;
3890
Bram Moolenaar734a8672019-12-02 22:49:38 +01003891 // Obtain dropped position
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003892 DragQueryPoint(hDrop, &pt);
3893 MapWindowPoints(s_hwnd, s_textArea, &pt, 1);
3894
3895 reset_VIsual();
3896
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003897 fnames = ALLOC_MULT(char_u *, cFiles);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003898
3899 if (fnames != NULL)
3900 for (i = 0; i < cFiles; ++i)
3901 {
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003902 if (DragQueryFileW(hDrop, i, wszFile, BUFPATHLEN) > 0)
3903 fnames[i] = utf16_to_enc(wszFile, NULL);
3904 else
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003905 {
3906 DragQueryFile(hDrop, i, szFile, BUFPATHLEN);
3907 fnames[i] = vim_strsave((char_u *)szFile);
3908 }
3909 }
3910
3911 DragFinish(hDrop);
3912
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003913 if (fnames == NULL)
3914 return;
LemonBoy45684c62022-04-24 15:46:42 +01003915
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003916 int kbd_modifiers = get_active_modifiers();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003917
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003918 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
3919 modifiers |= MOUSE_SHIFT;
3920 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
3921 modifiers |= MOUSE_CTRL;
3922 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
3923 modifiers |= MOUSE_ALT;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003924
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00003925 gui_handle_drop(pt.x, pt.y, modifiers, fnames, cFiles);
3926
3927 s_need_activate = TRUE;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003928}
3929
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003930 static int
3931_OnScroll(
Bram Moolenaar1266d672017-02-01 13:43:36 +01003932 HWND hwnd UNUSED,
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003933 HWND hwndCtl,
3934 UINT code,
3935 int pos)
3936{
Bram Moolenaar734a8672019-12-02 22:49:38 +01003937 static UINT prev_code = 0; // code of previous call
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003938 scrollbar_T *sb, *sb_info;
3939 long val;
3940 int dragging = FALSE;
3941 int dont_scroll_save = dont_scroll;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003942 SCROLLINFO si;
3943
3944 si.cbSize = sizeof(si);
3945 si.fMask = SIF_POS;
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003946
3947 sb = gui_mswin_find_scrollbar(hwndCtl);
3948 if (sb == NULL)
3949 return 0;
3950
Bram Moolenaar734a8672019-12-02 22:49:38 +01003951 if (sb->wp != NULL) // Left or right scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003952 {
3953 /*
3954 * Careful: need to get scrollbar info out of first (left) scrollbar
3955 * for window, but keep real scrollbar too because we must pass it to
3956 * gui_drag_scrollbar().
3957 */
3958 sb_info = &sb->wp->w_scrollbars[0];
3959 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01003960 else // Bottom scrollbar
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01003961 sb_info = sb;
3962 val = sb_info->value;
3963
3964 switch (code)
3965 {
3966 case SB_THUMBTRACK:
3967 val = pos;
3968 dragging = TRUE;
3969 if (sb->scroll_shift > 0)
3970 val <<= sb->scroll_shift;
3971 break;
3972 case SB_LINEDOWN:
3973 val++;
3974 break;
3975 case SB_LINEUP:
3976 val--;
3977 break;
3978 case SB_PAGEDOWN:
3979 val += (sb_info->size > 2 ? sb_info->size - 2 : 1);
3980 break;
3981 case SB_PAGEUP:
3982 val -= (sb_info->size > 2 ? sb_info->size - 2 : 1);
3983 break;
3984 case SB_TOP:
3985 val = 0;
3986 break;
3987 case SB_BOTTOM:
3988 val = sb_info->max;
3989 break;
3990 case SB_ENDSCROLL:
3991 if (prev_code == SB_THUMBTRACK)
3992 {
3993 /*
3994 * "pos" only gives us 16-bit data. In case of large file,
3995 * use GetScrollPos() which returns 32-bit. Unfortunately it
3996 * is not valid while the scrollbar is being dragged.
3997 */
3998 val = GetScrollPos(hwndCtl, SB_CTL);
3999 if (sb->scroll_shift > 0)
4000 val <<= sb->scroll_shift;
4001 }
4002 break;
4003
4004 default:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004005 // TRACE("Unknown scrollbar event %d\n", code);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004006 return 0;
4007 }
4008 prev_code = code;
4009
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004010 si.nPos = (sb->scroll_shift > 0) ? val >> sb->scroll_shift : val;
4011 SetScrollInfo(hwndCtl, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004012
4013 /*
4014 * When moving a vertical scrollbar, move the other vertical scrollbar too.
4015 */
4016 if (sb->wp != NULL)
4017 {
4018 scrollbar_T *sba = sb->wp->w_scrollbars;
4019 HWND id = sba[ (sb == sba + SBAR_LEFT) ? SBAR_RIGHT : SBAR_LEFT].id;
4020
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004021 SetScrollInfo(id, SB_CTL, &si, TRUE);
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004022 }
4023
Bram Moolenaar734a8672019-12-02 22:49:38 +01004024 // Don't let us be interrupted here by another message.
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004025 s_busy_processing = TRUE;
4026
Bram Moolenaar734a8672019-12-02 22:49:38 +01004027 // When "allow_scrollbar" is FALSE still need to remember the new
4028 // position, but don't actually scroll by setting "dont_scroll".
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004029 dont_scroll = !allow_scrollbar;
4030
Bram Moolenaara338adc2018-01-31 20:51:47 +01004031 mch_disable_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004032 gui_drag_scrollbar(sb, val, dragging);
Bram Moolenaara338adc2018-01-31 20:51:47 +01004033 mch_enable_flush();
4034 gui_may_flush();
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01004035
4036 s_busy_processing = FALSE;
4037 dont_scroll = dont_scroll_save;
4038
4039 return 0;
4040}
4041
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#ifdef FEAT_XPM_W32
4044# include "xpm_w32.h"
4045#endif
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
Bram Moolenaar734a8672019-12-02 22:49:38 +01004048// Some parameters for tearoff menus. All in pixels.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#define TEAROFF_PADDING_X 2
4050#define TEAROFF_BUTTON_PAD_X 8
4051#define TEAROFF_MIN_WIDTH 200
4052#define TEAROFF_SUBMENU_LABEL ">>"
4053#define TEAROFF_COLUMN_PADDING 3 // # spaces to pad column with.
4054
4055
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01004056#ifdef FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057# define ID_BEVAL_TOOLTIP 200
4058# define BEVAL_TEXT_LEN MAXPATHL
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060static BalloonEval *cur_beval = NULL;
K.Takataa8ec4912022-02-03 14:32:33 +00004061static UINT_PTR beval_timer_id = 0;
4062static DWORD last_user_activity = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01004063#endif // defined(FEAT_BEVAL_GUI)
Bram Moolenaar45360022005-07-21 21:08:21 +00004064
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004065
Bram Moolenaar734a8672019-12-02 22:49:38 +01004066// Local variables:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067
4068#ifdef FEAT_MENU
4069static UINT s_menu_id = 100;
Bram Moolenaar786989b2010-10-27 12:15:33 +02004070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071
4072/*
4073 * Use the system font for dialogs and tear-off menus. Remove this line to
4074 * use DLG_FONT_NAME.
4075 */
Bram Moolenaar786989b2010-10-27 12:15:33 +02004076#define USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078#define VIM_NAME "vim"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#define VIM_CLASSW L"Vim"
4080
Bram Moolenaar734a8672019-12-02 22:49:38 +01004081// Initial size for the dialog template. For gui_mch_dialog() it's fixed,
4082// thus there should be room for every dialog. For tearoffs it's made bigger
4083// when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#define DLG_ALLOC_SIZE 16 * 1024
4085
4086/*
4087 * stuff for dialogs, menus, tearoffs etc.
4088 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089static PWORD
4090add_dialog_element(
4091 PWORD p,
4092 DWORD lStyle,
4093 WORD x,
4094 WORD y,
4095 WORD w,
4096 WORD h,
4097 WORD Id,
4098 WORD clss,
4099 const char *caption);
4100static LPWORD lpwAlign(LPWORD);
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02004101static int nCopyAnsiToWideChar(LPWORD, LPSTR, BOOL);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004102#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103static void gui_mch_tearoff(char_u *title, vimmenu_T *menu, int initX, int initY);
Bram Moolenaar065bbac2016-02-20 13:08:46 +01004104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105static void get_dialog_font_metrics(void);
4106
4107static int dialog_default_button = -1;
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#ifdef FEAT_TOOLBAR
4110static void initialise_toolbar(void);
K.Takatac81e9bf2022-01-16 14:15:49 +00004111static void update_toolbar_size(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004112static LRESULT CALLBACK toolbar_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113static int get_toolbar_bitmap(vimmenu_T *menu);
K.Takatac81e9bf2022-01-16 14:15:49 +00004114#else
4115# define update_toolbar_size()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#endif
4117
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004118#ifdef FEAT_GUI_TABLINE
4119static void initialise_tabline(void);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02004120static LRESULT CALLBACK tabline_wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004121#endif
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123#ifdef FEAT_MBYTE_IME
4124static LRESULT _OnImeComposition(HWND hwnd, WPARAM dbcs, LPARAM param);
4125static char_u *GetResultStr(HWND hwnd, int GCS, int *lenp);
4126#endif
4127#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
4128# ifdef NOIME
4129typedef struct tagCOMPOSITIONFORM {
4130 DWORD dwStyle;
4131 POINT ptCurrentPos;
4132 RECT rcArea;
4133} COMPOSITIONFORM, *PCOMPOSITIONFORM, NEAR *NPCOMPOSITIONFORM, FAR *LPCOMPOSITIONFORM;
4134typedef HANDLE HIMC;
4135# endif
4136
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004137static HINSTANCE hLibImm = NULL;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004138static LONG (WINAPI *pImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
4139static HIMC (WINAPI *pImmGetContext)(HWND);
4140static HIMC (WINAPI *pImmAssociateContext)(HWND, HIMC);
4141static BOOL (WINAPI *pImmReleaseContext)(HWND, HIMC);
4142static BOOL (WINAPI *pImmGetOpenStatus)(HIMC);
4143static BOOL (WINAPI *pImmSetOpenStatus)(HIMC, BOOL);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004144static BOOL (WINAPI *pImmGetCompositionFontW)(HIMC, LPLOGFONTW);
4145static BOOL (WINAPI *pImmSetCompositionFontW)(HIMC, LPLOGFONTW);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004146static BOOL (WINAPI *pImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
4147static BOOL (WINAPI *pImmGetConversionStatus)(HIMC, LPDWORD, LPDWORD);
Bram Moolenaarca003e12006-03-17 23:19:38 +00004148static BOOL (WINAPI *pImmSetConversionStatus)(HIMC, DWORD, DWORD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149static void dyn_imm_load(void);
4150#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151# define pImmGetCompositionStringW ImmGetCompositionStringW
4152# define pImmGetContext ImmGetContext
4153# define pImmAssociateContext ImmAssociateContext
4154# define pImmReleaseContext ImmReleaseContext
4155# define pImmGetOpenStatus ImmGetOpenStatus
4156# define pImmSetOpenStatus ImmSetOpenStatus
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004157# define pImmGetCompositionFontW ImmGetCompositionFontW
4158# define pImmSetCompositionFontW ImmSetCompositionFontW
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159# define pImmSetCompositionWindow ImmSetCompositionWindow
4160# define pImmGetConversionStatus ImmGetConversionStatus
Bram Moolenaarca003e12006-03-17 23:19:38 +00004161# define pImmSetConversionStatus ImmSetConversionStatus
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162#endif
4163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164#ifdef FEAT_MENU
4165/*
4166 * Figure out how high the menu bar is at the moment.
4167 */
4168 static int
4169gui_mswin_get_menu_height(
Bram Moolenaar734a8672019-12-02 22:49:38 +01004170 int fix_window) // If TRUE, resize window if menu height changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 static int old_menu_height = -1;
4173
4174 RECT rc1, rc2;
4175 int num;
4176 int menu_height;
4177
4178 if (gui.menu_is_active)
4179 num = GetMenuItemCount(s_menuBar);
4180 else
4181 num = 0;
4182
4183 if (num == 0)
4184 menu_height = 0;
Bram Moolenaar71371b12015-03-24 17:57:12 +01004185 else if (IsMinimized(s_hwnd))
4186 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004187 // The height of the menu cannot be determined while the window is
4188 // minimized. Take the previous height if the menu is changed in that
4189 // state, to avoid that Vim's vertical window size accidentally
4190 // increases due to the unaccounted-for menu height.
Bram Moolenaar71371b12015-03-24 17:57:12 +01004191 menu_height = old_menu_height == -1 ? 0 : old_menu_height;
4192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 else
4194 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004195 /*
4196 * In case 'lines' is set in _vimrc/_gvimrc window width doesn't
4197 * seem to have been set yet, so menu wraps in default window
4198 * width which is very narrow. Instead just return height of a
4199 * single menu item. Will still be wrong when the menu really
4200 * should wrap over more than one line.
4201 */
4202 GetMenuItemRect(s_hwnd, s_menuBar, 0, &rc1);
4203 if (gui.starting)
4204 menu_height = rc1.bottom - rc1.top + 1;
4205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004207 GetMenuItemRect(s_hwnd, s_menuBar, num - 1, &rc2);
4208 menu_height = rc2.bottom - rc1.top + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 if (fix_window && menu_height != old_menu_height)
Bram Moolenaarafa24992006-03-27 20:58:26 +00004213 gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
Bram Moolenaar71371b12015-03-24 17:57:12 +01004214 old_menu_height = menu_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 return menu_height;
4217}
Bram Moolenaar734a8672019-12-02 22:49:38 +01004218#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220
4221/*
4222 * Setup for the Intellimouse
4223 */
LemonBoyc27747e2022-05-07 12:25:40 +01004224 static long
4225mouse_vertical_scroll_step(void)
4226{
4227 UINT val;
4228 if (SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &val, 0))
4229 return (val != WHEEL_PAGESCROLL) ? (long)val : -1;
4230 return 3; // Safe default;
4231}
4232
4233 static long
4234mouse_horizontal_scroll_step(void)
4235{
4236 UINT val;
4237 if (SystemParametersInfo(SPI_GETWHEELSCROLLCHARS, 0, &val, 0))
4238 return (long)val;
4239 return 3; // Safe default;
4240}
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 static void
4243init_mouse_wheel(void)
4244{
LemonBoyc27747e2022-05-07 12:25:40 +01004245 // Get the default values for the horizontal and vertical scroll steps from
4246 // the system.
4247 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
4248 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249}
4250
Bram Moolenaarae20f342019-11-05 21:09:23 +01004251/*
LemonBoya773d842022-05-10 20:54:46 +01004252 * Mouse scroll event handler.
Bram Moolenaarae20f342019-11-05 21:09:23 +01004253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 static void
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004255_OnMouseWheel(HWND hwnd UNUSED, WPARAM wParam, LPARAM lParam, int horizontal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256{
LemonBoy365d8f72022-05-05 19:23:07 +01004257 int button;
4258 win_T *wp;
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004259 int modifiers = 0;
4260 int kbd_modifiers;
LemonBoya773d842022-05-10 20:54:46 +01004261 int zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4262 POINT pt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
Bram Moolenaarae20f342019-11-05 21:09:23 +01004264 wp = gui_mouse_window(FIND_POPUP);
4265
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004266#ifdef FEAT_PROP_POPUP
Bram Moolenaarae20f342019-11-05 21:09:23 +01004267 if (wp != NULL && popup_is_popup(wp))
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004268 {
Bram Moolenaarae20f342019-11-05 21:09:23 +01004269 cmdarg_T cap;
4270 oparg_T oa;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004271
Bram Moolenaarae20f342019-11-05 21:09:23 +01004272 // Mouse hovers over popup window, scroll it if possible.
4273 mouse_row = wp->w_winrow;
4274 mouse_col = wp->w_wincol;
Bram Moolenaara80faa82020-04-12 19:37:17 +02004275 CLEAR_FIELD(cap);
LemonBoy365d8f72022-05-05 19:23:07 +01004276 if (horizontal)
4277 {
4278 cap.arg = zDelta < 0 ? MSCR_LEFT : MSCR_RIGHT;
4279 cap.cmdchar = zDelta < 0 ? K_MOUSELEFT : K_MOUSERIGHT;
4280 }
4281 else
4282 {
4283 cap.arg = zDelta < 0 ? MSCR_UP : MSCR_DOWN;
4284 cap.cmdchar = zDelta < 0 ? K_MOUSEUP : K_MOUSEDOWN;
4285 }
Bram Moolenaarae20f342019-11-05 21:09:23 +01004286 clear_oparg(&oa);
4287 cap.oap = &oa;
4288 nv_mousescroll(&cap);
4289 update_screen(0);
4290 setcursor();
4291 out_flush();
4292 return;
Bram Moolenaar0630bb62019-11-04 22:52:12 +01004293 }
4294#endif
4295
Bram Moolenaarae20f342019-11-05 21:09:23 +01004296 if (wp == NULL || !p_scf)
4297 wp = curwin;
4298
LemonBoy365d8f72022-05-05 19:23:07 +01004299 // Translate the scroll event into an event that Vim can process so that
4300 // the user has a chance to map the scrollwheel buttons.
4301 if (horizontal)
LemonBoy365d8f72022-05-05 19:23:07 +01004302 button = zDelta >= 0 ? MOUSE_6 : MOUSE_7;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 else
LemonBoy365d8f72022-05-05 19:23:07 +01004304 button = zDelta >= 0 ? MOUSE_4 : MOUSE_5;
LemonBoy365d8f72022-05-05 19:23:07 +01004305
4306 kbd_modifiers = get_active_modifiers();
4307
4308 if ((kbd_modifiers & MOD_MASK_SHIFT) != 0)
4309 modifiers |= MOUSE_SHIFT;
4310 if ((kbd_modifiers & MOD_MASK_CTRL) != 0)
4311 modifiers |= MOUSE_CTRL;
4312 if ((kbd_modifiers & MOD_MASK_ALT) != 0)
4313 modifiers |= MOUSE_ALT;
4314
LemonBoya773d842022-05-10 20:54:46 +01004315 // The cursor position is relative to the upper-left corner of the screen.
4316 pt.x = GET_X_LPARAM(lParam);
4317 pt.y = GET_Y_LPARAM(lParam);
4318 ScreenToClient(s_textArea, &pt);
4319
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +01004320 gui_send_mouse_event(button, pt.x, pt.y, FALSE, modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321}
4322
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004323#ifdef USE_SYSMENU_FONT
4324/*
4325 * Get Menu Font.
4326 * Return OK or FAIL.
4327 */
4328 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004329gui_w32_get_menu_font(LOGFONTW *lf)
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004330{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004331 NONCLIENTMETRICSW nm;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004332
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004333 nm.cbSize = sizeof(NONCLIENTMETRICSW);
4334 if (!SystemParametersInfoW(
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004335 SPI_GETNONCLIENTMETRICS,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004336 sizeof(NONCLIENTMETRICSW),
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004337 &nm,
4338 0))
4339 return FAIL;
4340 *lf = nm.lfMenuFont;
4341 return OK;
4342}
4343#endif
4344
4345
4346#if defined(FEAT_GUI_TABLINE) && defined(USE_SYSMENU_FONT)
4347/*
4348 * Set the GUI tabline font to the system menu font
4349 */
4350 static void
4351set_tabline_font(void)
4352{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004353 LOGFONTW lfSysmenu;
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004354 HFONT font;
4355 HWND hwnd;
4356 HDC hdc;
4357 HFONT hfntOld;
4358 TEXTMETRIC tm;
4359
4360 if (gui_w32_get_menu_font(&lfSysmenu) != OK)
4361 return;
4362
K.Takatac81e9bf2022-01-16 14:15:49 +00004363 lfSysmenu.lfHeight = adjust_fontsize_by_dpi(lfSysmenu.lfHeight);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01004364 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004365
4366 SendMessage(s_tabhwnd, WM_SETFONT, (WPARAM)font, TRUE);
4367
4368 /*
4369 * Compute the height of the font used for the tab text
4370 */
4371 hwnd = GetDesktopWindow();
4372 hdc = GetWindowDC(hwnd);
4373 hfntOld = SelectFont(hdc, font);
4374
4375 GetTextMetrics(hdc, &tm);
4376
4377 SelectFont(hdc, hfntOld);
4378 ReleaseDC(hwnd, hdc);
4379
4380 /*
4381 * The space used by the tab border and the space between the tab label
4382 * and the tab border is included as 7.
4383 */
4384 gui.tabline_height = tm.tmHeight + tm.tmInternalLeading + 7;
4385}
K.Takatac81e9bf2022-01-16 14:15:49 +00004386#else
4387# define set_tabline_font()
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00004388#endif
4389
Bram Moolenaar520470a2005-06-16 21:59:56 +00004390/*
4391 * Invoked when a setting was changed.
4392 */
4393 static LRESULT CALLBACK
LemonBoy365d8f72022-05-05 19:23:07 +01004394_OnSettingChange(UINT param)
Bram Moolenaar520470a2005-06-16 21:59:56 +00004395{
LemonBoy365d8f72022-05-05 19:23:07 +01004396 switch (param)
4397 {
4398 case SPI_SETWHEELSCROLLLINES:
LemonBoyc27747e2022-05-07 12:25:40 +01004399 mouse_set_vert_scroll_step(mouse_vertical_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004400 break;
LemonBoyc27747e2022-05-07 12:25:40 +01004401 case SPI_SETWHEELSCROLLCHARS:
4402 mouse_set_hor_scroll_step(mouse_horizontal_scroll_step());
LemonBoy365d8f72022-05-05 19:23:07 +01004403 break;
4404 case SPI_SETNONCLIENTMETRICS:
4405 set_tabline_font();
4406 break;
4407 default:
4408 break;
4409 }
Bram Moolenaar520470a2005-06-16 21:59:56 +00004410 return 0;
4411}
4412
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413#ifdef FEAT_NETBEANS_INTG
4414 static void
4415_OnWindowPosChanged(
4416 HWND hwnd,
4417 const LPWINDOWPOS lpwpos)
4418{
4419 static int x = 0, y = 0, cx = 0, cy = 0;
Bram Moolenaarf12d9832016-01-29 21:11:25 +01004420 extern int WSInitialized;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
4422 if (WSInitialized && (lpwpos->x != x || lpwpos->y != y
4423 || lpwpos->cx != cx || lpwpos->cy != cy))
4424 {
4425 x = lpwpos->x;
4426 y = lpwpos->y;
4427 cx = lpwpos->cx;
4428 cy = lpwpos->cy;
4429 netbeans_frame_moved(x, y);
4430 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004431 // Allow to send WM_SIZE and WM_MOVE
K.Takata4ac893f2022-01-20 12:44:28 +00004432 FORWARD_WM_WINDOWPOSCHANGED(hwnd, lpwpos, DefWindowProcW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
4434#endif
4435
K.Takata4f2417f2021-06-05 16:25:32 +02004436
4437static HWND hwndTip = NULL;
4438
4439 static void
4440show_sizing_tip(int cols, int rows)
4441{
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004442 TOOLINFOA ti;
K.Takata4f2417f2021-06-05 16:25:32 +02004443 char buf[32];
4444
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01004445 ti.cbSize = sizeof(ti);
K.Takata4f2417f2021-06-05 16:25:32 +02004446 ti.hwnd = s_hwnd;
4447 ti.uId = (UINT_PTR)s_hwnd;
4448 ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
4449 ti.lpszText = buf;
4450 sprintf(buf, "%dx%d", cols, rows);
4451 if (hwndTip == NULL)
4452 {
4453 hwndTip = CreateWindowExA(0, TOOLTIPS_CLASSA, NULL,
4454 WS_POPUP | TTS_ALWAYSTIP | TTS_NOPREFIX,
4455 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
4456 s_hwnd, NULL, GetModuleHandle(NULL), NULL);
4457 SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
4458 SendMessage(hwndTip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&ti);
4459 }
4460 else
4461 {
4462 SendMessage(hwndTip, TTM_UPDATETIPTEXT, 0, (LPARAM)&ti);
4463 }
4464 SendMessage(hwndTip, TTM_POPUP, 0, 0);
4465}
4466
4467 static void
4468destroy_sizing_tip(void)
4469{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00004470 if (hwndTip == NULL)
4471 return;
4472
4473 DestroyWindow(hwndTip);
4474 hwndTip = NULL;
K.Takata4f2417f2021-06-05 16:25:32 +02004475}
4476
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 static int
4478_DuringSizing(
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 UINT fwSide,
4480 LPRECT lprc)
4481{
4482 int w, h;
4483 int valid_w, valid_h;
4484 int w_offset, h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004485 int cols, rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 w = lprc->right - lprc->left;
4488 h = lprc->bottom - lprc->top;
K.Takata4f2417f2021-06-05 16:25:32 +02004489 gui_mswin_get_valid_dimensions(w, h, &valid_w, &valid_h, &cols, &rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 w_offset = w - valid_w;
4491 h_offset = h - valid_h;
4492
4493 if (fwSide == WMSZ_LEFT || fwSide == WMSZ_TOPLEFT
4494 || fwSide == WMSZ_BOTTOMLEFT)
4495 lprc->left += w_offset;
4496 else if (fwSide == WMSZ_RIGHT || fwSide == WMSZ_TOPRIGHT
4497 || fwSide == WMSZ_BOTTOMRIGHT)
4498 lprc->right -= w_offset;
4499
4500 if (fwSide == WMSZ_TOP || fwSide == WMSZ_TOPLEFT
4501 || fwSide == WMSZ_TOPRIGHT)
4502 lprc->top += h_offset;
4503 else if (fwSide == WMSZ_BOTTOM || fwSide == WMSZ_BOTTOMLEFT
4504 || fwSide == WMSZ_BOTTOMRIGHT)
4505 lprc->bottom -= h_offset;
K.Takata4f2417f2021-06-05 16:25:32 +02004506
4507 show_sizing_tip(cols, rows);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 return TRUE;
4509}
4510
K.Takata92000e22022-01-20 15:10:57 +00004511#ifdef FEAT_GUI_TABLINE
4512 static void
4513_OnRButtonUp(HWND hwnd, int x, int y, UINT keyFlags)
4514{
4515 if (gui_mch_showing_tabline())
4516 {
4517 POINT pt;
4518 RECT rect;
4519
4520 /*
4521 * If the cursor is on the tabline, display the tab menu
4522 */
4523 GetCursorPos(&pt);
4524 GetWindowRect(s_textArea, &rect);
4525 if (pt.y < rect.top)
4526 {
4527 show_tabline_popup_menu();
4528 return;
4529 }
4530 }
4531 FORWARD_WM_RBUTTONUP(hwnd, x, y, keyFlags, DefWindowProcW);
4532}
4533
4534 static void
4535_OnLButtonDown(HWND hwnd, BOOL fDoubleClick, int x, int y, UINT keyFlags)
4536{
4537 /*
4538 * If the user double clicked the tabline, create a new tab
4539 */
4540 if (gui_mch_showing_tabline())
4541 {
4542 POINT pt;
4543 RECT rect;
4544
4545 GetCursorPos(&pt);
4546 GetWindowRect(s_textArea, &rect);
4547 if (pt.y < rect.top)
4548 send_tabline_menu_event(0, TABLINE_MENU_NEW);
4549 }
4550 FORWARD_WM_LBUTTONDOWN(hwnd, fDoubleClick, x, y, keyFlags, DefWindowProcW);
4551}
4552#endif
4553
4554 static UINT
4555_OnNCHitTest(HWND hwnd, int xPos, int yPos)
4556{
4557 UINT result;
4558 int x, y;
4559
4560 result = FORWARD_WM_NCHITTEST(hwnd, xPos, yPos, DefWindowProcW);
4561 if (result != HTCLIENT)
4562 return result;
4563
4564#ifdef FEAT_GUI_TABLINE
4565 if (gui_mch_showing_tabline())
4566 {
4567 RECT rct;
4568
4569 // If the cursor is on the GUI tabline, don't process this event
4570 GetWindowRect(s_textArea, &rct);
4571 if (yPos < rct.top)
4572 return result;
4573 }
4574#endif
4575 (void)gui_mch_get_winpos(&x, &y);
4576 xPos -= x;
4577
4578 if (xPos < 48) // <VN> TODO should use system metric?
4579 return HTBOTTOMLEFT;
4580 else
4581 return HTBOTTOMRIGHT;
4582}
4583
4584#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
4585 static LRESULT
4586_OnNotify(HWND hwnd, UINT id, NMHDR *hdr)
4587{
4588 switch (hdr->code)
4589 {
4590 case TTN_GETDISPINFOW:
4591 case TTN_GETDISPINFO:
4592 {
4593 char_u *str = NULL;
4594 static void *tt_text = NULL;
4595
4596 VIM_CLEAR(tt_text);
4597
4598# ifdef FEAT_GUI_TABLINE
4599 if (gui_mch_showing_tabline()
4600 && hdr->hwndFrom == TabCtrl_GetToolTips(s_tabhwnd))
4601 {
4602 POINT pt;
4603 /*
4604 * Mouse is over the GUI tabline. Display the
4605 * tooltip for the tab under the cursor
4606 *
4607 * Get the cursor position within the tab control
4608 */
4609 GetCursorPos(&pt);
4610 if (ScreenToClient(s_tabhwnd, &pt) != 0)
4611 {
4612 TCHITTESTINFO htinfo;
4613 int idx;
4614
4615 /*
4616 * Get the tab under the cursor
4617 */
4618 htinfo.pt.x = pt.x;
4619 htinfo.pt.y = pt.y;
4620 idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
4621 if (idx != -1)
4622 {
4623 tabpage_T *tp;
4624
4625 tp = find_tabpage(idx + 1);
4626 if (tp != NULL)
4627 {
4628 get_tabline_label(tp, TRUE);
4629 str = NameBuff;
4630 }
4631 }
4632 }
4633 }
4634# endif
4635# ifdef FEAT_TOOLBAR
4636# ifdef FEAT_GUI_TABLINE
4637 else
4638# endif
4639 {
4640 UINT idButton;
4641 vimmenu_T *pMenu;
4642
4643 idButton = (UINT) hdr->idFrom;
4644 pMenu = gui_mswin_find_menu(root_menu, idButton);
4645 if (pMenu)
4646 str = pMenu->strings[MENU_INDEX_TIP];
4647 }
4648# endif
4649 if (str == NULL)
4650 break;
4651
4652 // Set the maximum width, this also enables using \n for
4653 // line break.
4654 SendMessage(hdr->hwndFrom, TTM_SETMAXTIPWIDTH, 0, 500);
4655
4656 if (hdr->code == TTN_GETDISPINFOW)
4657 {
4658 LPNMTTDISPINFOW lpdi = (LPNMTTDISPINFOW)hdr;
4659
4660 tt_text = enc_to_utf16(str, NULL);
4661 lpdi->lpszText = tt_text;
4662 // can't show tooltip if failed
4663 }
4664 else
4665 {
4666 LPNMTTDISPINFO lpdi = (LPNMTTDISPINFO)hdr;
4667
4668 if (STRLEN(str) < sizeof(lpdi->szText)
4669 || ((tt_text = vim_strsave(str)) == NULL))
4670 vim_strncpy((char_u *)lpdi->szText, str,
4671 sizeof(lpdi->szText) - 1);
4672 else
4673 lpdi->lpszText = tt_text;
4674 }
4675 }
4676 break;
4677
4678# ifdef FEAT_GUI_TABLINE
4679 case TCN_SELCHANGE:
4680 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4681 {
4682 send_tabline_event(TabCtrl_GetCurSel(s_tabhwnd) + 1);
4683 return 0L;
4684 }
4685 break;
4686
4687 case NM_RCLICK:
4688 if (gui_mch_showing_tabline() && (hdr->hwndFrom == s_tabhwnd))
4689 {
4690 show_tabline_popup_menu();
4691 return 0L;
4692 }
4693 break;
4694# endif
4695
4696 default:
4697 break;
4698 }
4699 return DefWindowProcW(hwnd, WM_NOTIFY, (WPARAM)id, (LPARAM)hdr);
4700}
4701#endif
4702
4703#if defined(MENUHINTS) && defined(FEAT_MENU)
4704 static LRESULT
4705_OnMenuSelect(HWND hwnd, WPARAM wParam, LPARAM lParam)
4706{
4707 if (((UINT) HIWORD(wParam)
4708 & (0xffff ^ (MF_MOUSESELECT + MF_BITMAP + MF_POPUP)))
4709 == MF_HILITE
Bram Moolenaar24959102022-05-07 20:01:16 +01004710 && (State & MODE_CMDLINE) == 0)
K.Takata92000e22022-01-20 15:10:57 +00004711 {
4712 UINT idButton;
4713 vimmenu_T *pMenu;
4714 static int did_menu_tip = FALSE;
4715
4716 if (did_menu_tip)
4717 {
4718 msg_clr_cmdline();
4719 setcursor();
4720 out_flush();
4721 did_menu_tip = FALSE;
4722 }
4723
4724 idButton = (UINT)LOWORD(wParam);
4725 pMenu = gui_mswin_find_menu(root_menu, idButton);
4726 if (pMenu != NULL && pMenu->strings[MENU_INDEX_TIP] != 0
4727 && GetMenuState(s_menuBar, pMenu->id, MF_BYCOMMAND) != -1)
4728 {
4729 ++msg_hist_off;
4730 msg((char *)pMenu->strings[MENU_INDEX_TIP]);
4731 --msg_hist_off;
4732 setcursor();
4733 out_flush();
4734 did_menu_tip = TRUE;
4735 }
4736 return 0L;
4737 }
4738 return DefWindowProcW(hwnd, WM_MENUSELECT, wParam, lParam);
4739}
4740#endif
4741
Ken Takata7086b3e2023-10-02 21:26:03 +02004742 static BOOL
4743_OnGetDpiScaledSize(HWND hwnd, UINT dpi, SIZE *size)
4744{
4745 //TRACE("DPI: %d, SIZE=(%d,%d), s_dpi: %d", dpi, size->cx, size->cy, s_dpi);
4746
4747 // Calculate new approximate size.
4748 // FIXME: If a bitmap font (e.g. FixedSys) is used, the font size may not
4749 // be changed. In that case, the calculated size can be wrong.
4750 size->cx = size->cx * dpi / s_dpi;
4751 size->cy = size->cy * dpi / s_dpi;
4752 //TRACE("New approx. SIZE=(%d,%d)", size->cx, size->cy);
4753
4754 return FALSE;
4755}
4756
K.Takatac81e9bf2022-01-16 14:15:49 +00004757 static LRESULT
Ken Takata7086b3e2023-10-02 21:26:03 +02004758_OnDpiChanged(HWND hwnd, UINT xdpi UNUSED, UINT ydpi, RECT *rc)
K.Takatac81e9bf2022-01-16 14:15:49 +00004759{
4760 s_dpi = ydpi;
4761 s_in_dpichanged = TRUE;
4762 //TRACE("DPI: %d", ydpi);
4763
Ken Takata7086b3e2023-10-02 21:26:03 +02004764 s_suggested_rect = *rc;
4765 //TRACE("Suggested pos&size: %d,%d %d,%d", rc->left, rc->top,
4766 // rc->right - rc->left, rc->bottom - rc->top);
4767
K.Takatac81e9bf2022-01-16 14:15:49 +00004768 update_scrollbar_size();
4769 update_toolbar_size();
4770 set_tabline_font();
4771
4772 gui_init_font(*p_guifont == NUL ? hl_get_font_name() : p_guifont, FALSE);
4773 gui_get_wide_font();
4774 gui_mswin_get_menu_height(FALSE);
4775#ifdef FEAT_MBYTE_IME
4776 im_set_position(gui.row, gui.col);
4777#endif
4778 InvalidateRect(hwnd, NULL, TRUE);
4779
4780 s_in_dpichanged = FALSE;
4781 return 0L;
4782}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784
4785 static LRESULT CALLBACK
4786_WndProc(
4787 HWND hwnd,
4788 UINT uMsg,
4789 WPARAM wParam,
4790 LPARAM lParam)
4791{
LemonBoya773d842022-05-10 20:54:46 +01004792 // ch_log(NULL, "WndProc: hwnd = %08x, msg = %x, wParam = %x, lParam = %x",
4793 // hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
4795 HandleMouseHide(uMsg, lParam);
4796
4797 s_uMsg = uMsg;
4798 s_wParam = wParam;
4799 s_lParam = lParam;
4800
4801 switch (uMsg)
4802 {
4803 HANDLE_MSG(hwnd, WM_DEADCHAR, _OnDeadChar);
4804 HANDLE_MSG(hwnd, WM_SYSDEADCHAR, _OnDeadChar);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004805 // HANDLE_MSG(hwnd, WM_ACTIVATE, _OnActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 HANDLE_MSG(hwnd, WM_CLOSE, _OnClose);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004807 // HANDLE_MSG(hwnd, WM_COMMAND, _OnCommand);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 HANDLE_MSG(hwnd, WM_DESTROY, _OnDestroy);
4809 HANDLE_MSG(hwnd, WM_DROPFILES, _OnDropFiles);
4810 HANDLE_MSG(hwnd, WM_HSCROLL, _OnScroll);
4811 HANDLE_MSG(hwnd, WM_KILLFOCUS, _OnKillFocus);
4812#ifdef FEAT_MENU
4813 HANDLE_MSG(hwnd, WM_COMMAND, _OnMenu);
4814#endif
Bram Moolenaar734a8672019-12-02 22:49:38 +01004815 // HANDLE_MSG(hwnd, WM_MOVE, _OnMove);
4816 // HANDLE_MSG(hwnd, WM_NCACTIVATE, _OnNCActivate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 HANDLE_MSG(hwnd, WM_SETFOCUS, _OnSetFocus);
4818 HANDLE_MSG(hwnd, WM_SIZE, _OnSize);
Bram Moolenaar734a8672019-12-02 22:49:38 +01004819 // HANDLE_MSG(hwnd, WM_SYSCOMMAND, _OnSysCommand);
4820 // HANDLE_MSG(hwnd, WM_SYSKEYDOWN, _OnAltKey);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 HANDLE_MSG(hwnd, WM_VSCROLL, _OnScroll);
4822 // HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGING, _OnWindowPosChanging);
4823 HANDLE_MSG(hwnd, WM_ACTIVATEAPP, _OnActivateApp);
4824#ifdef FEAT_NETBEANS_INTG
4825 HANDLE_MSG(hwnd, WM_WINDOWPOSCHANGED, _OnWindowPosChanged);
4826#endif
Bram Moolenaarafa24992006-03-27 20:58:26 +00004827#ifdef FEAT_GUI_TABLINE
K.Takata92000e22022-01-20 15:10:57 +00004828 HANDLE_MSG(hwnd, WM_RBUTTONUP, _OnRButtonUp);
4829 HANDLE_MSG(hwnd, WM_LBUTTONDBLCLK, _OnLButtonDown);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004830#endif
K.Takata92000e22022-01-20 15:10:57 +00004831 HANDLE_MSG(hwnd, WM_NCHITTEST, _OnNCHitTest);
Bram Moolenaarafa24992006-03-27 20:58:26 +00004832
Bram Moolenaar734a8672019-12-02 22:49:38 +01004833 case WM_QUERYENDSESSION: // System wants to go down.
4834 gui_shell_closed(); // Will exit when no changed buffers.
4835 return FALSE; // Do NOT allow system to go down.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 case WM_ENDSESSION:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004838 if (wParam) // system only really goes down when wParam is TRUE
Bram Moolenaar213ae482011-12-15 21:51:36 +01004839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 _OnEndSession();
Bram Moolenaar213ae482011-12-15 21:51:36 +01004841 return 0L;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 break;
4844
4845 case WM_CHAR:
Bram Moolenaar734a8672019-12-02 22:49:38 +01004846 // Don't use HANDLE_MSG() for WM_CHAR, it truncates wParam to a single
4847 // byte while we want the UTF-16 character value.
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004848 _OnChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 return 0L;
4850
4851 case WM_SYSCHAR:
4852 /*
4853 * if 'winaltkeys' is "no", or it's "menu" and it's not a menu
4854 * shortcut key, handle like a typed ALT key, otherwise call Windows
4855 * ALT key handling.
4856 */
4857#ifdef FEAT_MENU
4858 if ( !gui.menu_is_active
4859 || p_wak[0] == 'n'
4860 || (p_wak[0] == 'm' && !gui_is_menu_shortcut((int)wParam))
4861 )
4862#endif
4863 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004864 _OnSysChar(hwnd, (UINT)wParam, (int)(short)LOWORD(lParam));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 return 0L;
4866 }
4867#ifdef FEAT_MENU
4868 else
K.Takata4ac893f2022-01-20 12:44:28 +00004869 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#endif
4871
4872 case WM_SYSKEYUP:
4873#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01004874 // This used to be done only when menu is active: ALT key is used for
4875 // that. But that caused problems when menu is disabled and using
4876 // Alt-Tab-Esc: get into a strange state where no mouse-moved events
4877 // are received, mouse pointer remains hidden.
K.Takata4ac893f2022-01-20 12:44:28 +00004878 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#else
Bram Moolenaar213ae482011-12-15 21:51:36 +01004880 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
4882
K.Takata4f2417f2021-06-05 16:25:32 +02004883 case WM_EXITSIZEMOVE:
4884 destroy_sizing_tip();
4885 break;
4886
Bram Moolenaar734a8672019-12-02 22:49:38 +01004887 case WM_SIZING: // HANDLE_MSG doesn't seem to handle this one
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004888 return _DuringSizing((UINT)wParam, (LPRECT)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 case WM_MOUSEWHEEL:
LemonBoy365d8f72022-05-05 19:23:07 +01004891 case WM_MOUSEHWHEEL:
LemonBoya773d842022-05-10 20:54:46 +01004892 _OnMouseWheel(hwnd, wParam, lParam, uMsg == WM_MOUSEHWHEEL);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004893 return 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar734a8672019-12-02 22:49:38 +01004895 // Notification for change in SystemParametersInfo()
Bram Moolenaar520470a2005-06-16 21:59:56 +00004896 case WM_SETTINGCHANGE:
4897 return _OnSettingChange((UINT)wParam);
4898
Bram Moolenaar3991dab2006-03-27 17:01:56 +00004899#if defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 case WM_NOTIFY:
K.Takata92000e22022-01-20 15:10:57 +00004901 return _OnNotify(hwnd, (UINT)wParam, (NMHDR*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
K.Takata92000e22022-01-20 15:10:57 +00004903
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#if defined(MENUHINTS) && defined(FEAT_MENU)
4905 case WM_MENUSELECT:
K.Takata92000e22022-01-20 15:10:57 +00004906 return _OnMenuSelect(hwnd, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909#ifdef FEAT_MBYTE_IME
4910 case WM_IME_NOTIFY:
4911 if (!_OnImeNotify(hwnd, (DWORD)wParam, (DWORD)lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004912 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004913 return 1L;
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 case WM_IME_COMPOSITION:
4916 if (!_OnImeComposition(hwnd, wParam, lParam))
K.Takata4ac893f2022-01-20 12:44:28 +00004917 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar213ae482011-12-15 21:51:36 +01004918 return 1L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919#endif
Ken Takata7086b3e2023-10-02 21:26:03 +02004920 case WM_GETDPISCALEDSIZE:
4921 return _OnGetDpiScaledSize(hwnd, (UINT)wParam, (SIZE *)lParam);
K.Takatac81e9bf2022-01-16 14:15:49 +00004922 case WM_DPICHANGED:
4923 return _OnDpiChanged(hwnd, (UINT)LOWORD(wParam), (UINT)HIWORD(wParam),
4924 (RECT*)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925
4926 default:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#ifdef MSWIN_FIND_REPLACE
Bram Moolenaarcea912a2016-10-12 14:20:24 +02004928 if (uMsg == s_findrep_msg && s_findrep_msg != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 _OnFindRepl();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930#endif
K.Takata92000e22022-01-20 15:10:57 +00004931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933
K.Takata4ac893f2022-01-20 12:44:28 +00004934 return DefWindowProcW(hwnd, uMsg, wParam, lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * End of call-back routines
4939 */
4940
Bram Moolenaar734a8672019-12-02 22:49:38 +01004941// parent window, if specified with -P
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942HWND vim_parent_hwnd = NULL;
4943
4944 static BOOL CALLBACK
4945FindWindowTitle(HWND hwnd, LPARAM lParam)
4946{
4947 char buf[2048];
4948 char *title = (char *)lParam;
4949
4950 if (GetWindowText(hwnd, buf, sizeof(buf)))
4951 {
4952 if (strstr(buf, title) != NULL)
4953 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01004954 // Found it. Store the window ref. and quit searching if MDI
4955 // works.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 vim_parent_hwnd = FindWindowEx(hwnd, NULL, "MDIClient", NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004957 if (vim_parent_hwnd != NULL)
4958 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
4960 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01004961 return TRUE; // continue searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962}
4963
4964/*
4965 * Invoked for '-P "title"' argument: search for parent application to open
4966 * our window in.
4967 */
4968 void
4969gui_mch_set_parent(char *title)
4970{
4971 EnumWindows(FindWindowTitle, (LPARAM)title);
4972 if (vim_parent_hwnd == NULL)
4973 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004974 semsg(_(e_cannot_find_window_title_str), title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 mch_exit(2);
4976 }
4977}
4978
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004979#ifndef FEAT_OLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 static void
4981ole_error(char *arg)
4982{
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004983 char buf[IOSIZE];
4984
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02004985# ifdef VIMDLL
4986 gui.in_use = mch_is_gui_executable();
4987# endif
4988
Bram Moolenaar734a8672019-12-02 22:49:38 +01004989 // Can't use emsg() here, we have not finished initialisation yet.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004990 vim_snprintf(buf, IOSIZE,
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00004991 _(e_argument_not_supported_str_use_ole_version), arg);
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00004992 mch_errmsg(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaarafde13b2019-04-28 19:46:49 +02004996#if defined(GUI_MAY_SPAWN) || defined(PROTO)
4997 static char *
4998gvim_error(void)
4999{
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005000 char *msg = _(e_gui_cannot_be_used_cannot_execute_gvim_exe);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005001
5002 if (starting)
5003 {
5004 mch_errmsg(msg);
5005 mch_errmsg("\n");
5006 mch_exit(2);
5007 }
5008 return msg;
5009}
5010
5011 char *
5012gui_mch_do_spawn(char_u *arg)
5013{
5014 int len;
5015# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5016 char_u *session = NULL;
5017 LPWSTR tofree1 = NULL;
5018# endif
5019 WCHAR name[MAX_PATH];
5020 LPWSTR cmd, newcmd = NULL, p, warg, tofree2 = NULL;
5021 STARTUPINFOW si = {sizeof(si)};
5022 PROCESS_INFORMATION pi;
5023
5024 if (!GetModuleFileNameW(g_hinst, name, MAX_PATH))
5025 goto error;
5026 p = wcsrchr(name, L'\\');
5027 if (p == NULL)
5028 goto error;
5029 // Replace the executable name from vim(d).exe to gvim(d).exe.
5030# ifdef DEBUG
5031 wcscpy(p + 1, L"gvimd.exe");
5032# else
5033 wcscpy(p + 1, L"gvim.exe");
5034# endif
5035
5036# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5037 if (starting)
5038# endif
5039 {
5040 // Pass the command line to the new process.
5041 p = GetCommandLineW();
5042 // Skip 1st argument.
5043 while (*p && *p != L' ' && *p != L'\t')
5044 {
5045 if (*p == L'"')
5046 {
5047 while (*p && *p != L'"')
5048 ++p;
5049 if (*p)
5050 ++p;
5051 }
5052 else
5053 ++p;
5054 }
5055 cmd = p;
5056 }
5057# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5058 else
5059 {
5060 // Create a session file and pass it to the new process.
5061 LPWSTR wsession;
5062 char_u *savebg;
5063 int ret;
5064
5065 session = vim_tempname('s', FALSE);
5066 if (session == NULL)
5067 goto error;
5068 savebg = p_bg;
5069 p_bg = vim_strsave((char_u *)"light"); // Set 'bg' to "light".
5070 ret = write_session_file(session);
5071 vim_free(p_bg);
5072 p_bg = savebg;
5073 if (!ret)
5074 goto error;
5075 wsession = enc_to_utf16(session, NULL);
5076 if (wsession == NULL)
5077 goto error;
5078 len = (int)wcslen(wsession) * 2 + 27 + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005079 cmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005080 if (cmd == NULL)
5081 {
5082 vim_free(wsession);
5083 goto error;
5084 }
5085 tofree1 = cmd;
5086 _snwprintf(cmd, len, L" -S \"%s\" -c \"call delete('%s')\"",
5087 wsession, wsession);
5088 vim_free(wsession);
5089 }
5090# endif
5091
5092 // Check additional arguments to the `:gui` command.
5093 if (arg != NULL)
5094 {
5095 warg = enc_to_utf16(arg, NULL);
5096 if (warg == NULL)
5097 goto error;
5098 tofree2 = warg;
5099 }
5100 else
5101 warg = L"";
5102
5103 // Set up the new command line.
5104 len = (int)wcslen(name) + (int)wcslen(cmd) + (int)wcslen(warg) + 4;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005105 newcmd = ALLOC_MULT(WCHAR, len);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005106 if (newcmd == NULL)
5107 goto error;
5108 _snwprintf(newcmd, len, L"\"%s\"%s %s", name, cmd, warg);
5109
5110 // Spawn a new GUI process.
5111 if (!CreateProcessW(NULL, newcmd, NULL, NULL, TRUE, 0,
5112 NULL, NULL, &si, &pi))
5113 goto error;
5114 CloseHandle(pi.hProcess);
5115 CloseHandle(pi.hThread);
5116 mch_exit(0);
5117
5118error:
5119# if defined(FEAT_SESSION) && defined(EXPERIMENTAL_GUI_CMD)
5120 if (session)
5121 mch_remove(session);
5122 vim_free(session);
5123 vim_free(tofree1);
5124# endif
5125 vim_free(newcmd);
5126 vim_free(tofree2);
5127 return gvim_error();
5128}
5129#endif
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131/*
5132 * Parse the GUI related command-line arguments. Any arguments used are
5133 * deleted from argv, and *argc is decremented accordingly. This is called
K.Takataeeec2542021-06-02 13:28:16 +02005134 * when Vim is started, whether or not the GUI has been started.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 */
5136 void
5137gui_mch_prepare(int *argc, char **argv)
5138{
5139 int silent = FALSE;
5140 int idx;
5141
Bram Moolenaar734a8672019-12-02 22:49:38 +01005142 // Check for special OLE command line parameters
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if ((*argc == 2 || *argc == 3) && (argv[1][0] == '-' || argv[1][0] == '/'))
5144 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005145 // Check for a "-silent" argument first.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (*argc == 3 && STRICMP(argv[1] + 1, "silent") == 0
5147 && (argv[2][0] == '-' || argv[2][0] == '/'))
5148 {
5149 silent = TRUE;
5150 idx = 2;
5151 }
5152 else
5153 idx = 1;
5154
Bram Moolenaar734a8672019-12-02 22:49:38 +01005155 // Register Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (STRICMP(argv[idx] + 1, "register") == 0)
5157 {
5158#ifdef FEAT_OLE
5159 RegisterMe(silent);
5160 mch_exit(0);
5161#else
5162 if (!silent)
5163 ole_error("register");
5164 mch_exit(2);
5165#endif
5166 }
5167
Bram Moolenaar734a8672019-12-02 22:49:38 +01005168 // Unregister Vim as an OLE Automation server
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (STRICMP(argv[idx] + 1, "unregister") == 0)
5170 {
5171#ifdef FEAT_OLE
5172 UnregisterMe(!silent);
5173 mch_exit(0);
5174#else
5175 if (!silent)
5176 ole_error("unregister");
5177 mch_exit(2);
5178#endif
5179 }
5180
Bram Moolenaar734a8672019-12-02 22:49:38 +01005181 // Ignore an -embedding argument. It is only relevant if the
5182 // application wants to treat the case when it is started manually
5183 // differently from the case where it is started via automation (and
5184 // we don't).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (STRICMP(argv[idx] + 1, "embedding") == 0)
5186 {
5187#ifdef FEAT_OLE
5188 *argc = 1;
5189#else
5190 ole_error("embedding");
5191 mch_exit(2);
5192#endif
5193 }
5194 }
5195
5196#ifdef FEAT_OLE
5197 {
5198 int bDoRestart = FALSE;
5199
5200 InitOLE(&bDoRestart);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005201 // automatically exit after registering
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (bDoRestart)
5203 mch_exit(0);
5204 }
5205#endif
5206
5207#ifdef FEAT_NETBEANS_INTG
5208 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005209 // stolen from gui_x11.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 int arg;
5211
5212 for (arg = 1; arg < *argc; arg++)
5213 if (strncmp("-nb", argv[arg], 3) == 0)
5214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 netbeansArg = argv[arg];
5216 mch_memmove(&argv[arg], &argv[arg + 1],
5217 (--*argc - arg) * sizeof(char *));
5218 argv[*argc] = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01005219 break; // enough?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223}
5224
K.Takatac81e9bf2022-01-16 14:15:49 +00005225 static void
5226load_dpi_func(void)
5227{
5228 HMODULE hUser32;
5229
5230 hUser32 = GetModuleHandle("user32.dll");
5231 if (hUser32 == NULL)
5232 goto fail;
5233
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005234 pGetDpiForSystem = (UINT (WINAPI *)(void))GetProcAddress(hUser32, "GetDpiForSystem");
5235 pGetDpiForWindow = (UINT (WINAPI *)(HWND))GetProcAddress(hUser32, "GetDpiForWindow");
5236 pGetSystemMetricsForDpi = (int (WINAPI *)(int, UINT))GetProcAddress(hUser32, "GetSystemMetricsForDpi");
K.Takatac81e9bf2022-01-16 14:15:49 +00005237 //pGetWindowDpiAwarenessContext = (void*)GetProcAddress(hUser32, "GetWindowDpiAwarenessContext");
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01005238 pSetThreadDpiAwarenessContext = (DPI_AWARENESS_CONTEXT (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "SetThreadDpiAwarenessContext");
5239 pGetAwarenessFromDpiAwarenessContext = (DPI_AWARENESS (WINAPI *)(DPI_AWARENESS_CONTEXT))GetProcAddress(hUser32, "GetAwarenessFromDpiAwarenessContext");
K.Takatac81e9bf2022-01-16 14:15:49 +00005240
5241 if (pSetThreadDpiAwarenessContext != NULL)
5242 {
5243 DPI_AWARENESS_CONTEXT oldctx = pSetThreadDpiAwarenessContext(
5244 DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
5245 if (oldctx != NULL)
5246 {
5247 TRACE("DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 enabled");
5248 s_process_dpi_aware = pGetAwarenessFromDpiAwarenessContext(oldctx);
5249#ifdef DEBUG
5250 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5251 {
5252 TRACE("WARNING: PerMonitorV2 is not enabled in the process level for some reasons. IME window may not shown correctly.");
5253 }
5254#endif
5255 return;
5256 }
5257 }
5258
5259fail:
5260 // Disable PerMonitorV2 APIs.
5261 pGetDpiForSystem = stubGetDpiForSystem;
5262 pGetDpiForWindow = NULL;
5263 pGetSystemMetricsForDpi = stubGetSystemMetricsForDpi;
5264 pSetThreadDpiAwarenessContext = NULL;
5265 pGetAwarenessFromDpiAwarenessContext = NULL;
5266}
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268/*
5269 * Initialise the GUI. Create all the windows, set up all the call-backs
5270 * etc.
5271 */
5272 int
5273gui_mch_init(void)
5274{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 const WCHAR szVimWndClassW[] = VIM_CLASSW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005276 const WCHAR szTextAreaClassW[] = L"VimTextArea";
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 WNDCLASSW wndclassw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
Bram Moolenaar734a8672019-12-02 22:49:38 +01005279 // Return here if the window was already opened (happens when
5280 // gui_mch_dialog() is called early).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (s_hwnd != NULL)
Bram Moolenaar748bf032005-02-02 23:04:36 +00005282 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 /*
5285 * Load the tearoff bitmap
5286 */
5287#ifdef FEAT_TEAROFF
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005288 s_htearbitmap = LoadBitmap(g_hinst, "IDB_TEAROFF");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#endif
5290
K.Takatac81e9bf2022-01-16 14:15:49 +00005291 load_dpi_func();
5292
5293 s_dpi = pGetDpiForSystem();
5294 update_scrollbar_size();
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296#ifdef FEAT_MENU
Bram Moolenaar734a8672019-12-02 22:49:38 +01005297 gui.menu_height = 0; // Windows takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298#endif
5299 gui.border_width = 0;
K.Takatac81e9bf2022-01-16 14:15:49 +00005300#ifdef FEAT_TOOLBAR
5301 gui.toolbar_height = TOOLBAR_BUTTON_HEIGHT + TOOLBAR_BORDER_HEIGHT;
5302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 s_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
5305
Bram Moolenaar734a8672019-12-02 22:49:38 +01005306 // First try using the wide version, so that we can use any title.
5307 // Otherwise only characters in the active codepage will work.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005308 if (GetClassInfoW(g_hinst, szVimWndClassW, &wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005310 wndclassw.style = CS_DBLCLKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 wndclassw.lpfnWndProc = _WndProc;
5312 wndclassw.cbClsExtra = 0;
5313 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005314 wndclassw.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 wndclassw.hIcon = LoadIcon(wndclassw.hInstance, "IDR_VIM");
5316 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5317 wndclassw.hbrBackground = s_brush;
5318 wndclassw.lpszMenuName = NULL;
5319 wndclassw.lpszClassName = szVimWndClassW;
5320
K.Takata4ac893f2022-01-20 12:44:28 +00005321 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005322 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 if (vim_parent_hwnd != NULL)
5326 {
5327#ifdef HAVE_TRY_EXCEPT
5328 __try
5329 {
5330#endif
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005331 // Open inside the specified parent window.
5332 // TODO: last argument should point to a CLIENTCREATESTRUCT
5333 // structure.
5334 s_hwnd = CreateWindowExW(
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 WS_EX_MDICHILD,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005336 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005337 WS_OVERLAPPEDWINDOW | WS_CHILD
5338 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | 0xC000,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5340 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005341 100, // Any value will do
5342 100, // Any value will do
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 vim_parent_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005344 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345#ifdef HAVE_TRY_EXCEPT
5346 }
5347 __except(EXCEPTION_EXECUTE_HANDLER)
5348 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005349 // NOP
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351#endif
5352 if (s_hwnd == NULL)
5353 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00005354 emsg(_(e_unable_to_open_window_inside_mdi_application));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 mch_exit(2);
5356 }
5357 }
5358 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00005359 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005360 // If the provided windowid is not valid reset it to zero, so that it
5361 // is ignored and we open our own window.
Bram Moolenaar78e17622007-08-30 10:26:19 +00005362 if (IsWindow((HWND)win_socket_id) <= 0)
5363 win_socket_id = 0;
5364
Bram Moolenaar734a8672019-12-02 22:49:38 +01005365 // Create a window. If win_socket_id is not zero without border and
5366 // titlebar, it will be reparented below.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005367 s_hwnd = CreateWindowW(
5368 szVimWndClassW, L"Vim MSWindows GUI",
Bram Moolenaare78c2062011-08-10 15:56:27 +02005369 (win_socket_id == 0 ? WS_OVERLAPPEDWINDOW : WS_POPUP)
5370 | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
Bram Moolenaar78e17622007-08-30 10:26:19 +00005371 gui_win_x == -1 ? CW_USEDEFAULT : gui_win_x,
5372 gui_win_y == -1 ? CW_USEDEFAULT : gui_win_y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01005373 100, // Any value will do
5374 100, // Any value will do
Bram Moolenaar78e17622007-08-30 10:26:19 +00005375 NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005376 g_hinst, NULL);
Bram Moolenaar78e17622007-08-30 10:26:19 +00005377 if (s_hwnd != NULL && win_socket_id != 0)
5378 {
5379 SetParent(s_hwnd, (HWND)win_socket_id);
5380 ShowWindow(s_hwnd, SW_SHOWMAXIMIZED);
5381 }
5382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383
5384 if (s_hwnd == NULL)
5385 return FAIL;
5386
K.Takatac81e9bf2022-01-16 14:15:49 +00005387 if (pGetDpiForWindow != NULL)
5388 {
5389 s_dpi = pGetDpiForWindow(s_hwnd);
5390 update_scrollbar_size();
5391 //TRACE("System DPI: %d, DPI: %d", pGetDpiForSystem(), s_dpi);
5392 }
5393
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
5395 dyn_imm_load();
5396#endif
5397
Bram Moolenaar734a8672019-12-02 22:49:38 +01005398 // Create the text area window
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005399 if (GetClassInfoW(g_hinst, szTextAreaClassW, &wndclassw) == 0)
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005400 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005401 wndclassw.style = CS_OWNDC;
5402 wndclassw.lpfnWndProc = _TextAreaWndProc;
5403 wndclassw.cbClsExtra = 0;
5404 wndclassw.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005405 wndclassw.hInstance = g_hinst;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005406 wndclassw.hIcon = NULL;
5407 wndclassw.hCursor = LoadCursor(NULL, IDC_ARROW);
5408 wndclassw.hbrBackground = NULL;
5409 wndclassw.lpszMenuName = NULL;
5410 wndclassw.lpszClassName = szTextAreaClassW;
Bram Moolenaar33d0b692010-02-17 16:31:32 +01005411
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005412 if (RegisterClassW(&wndclassw) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return FAIL;
5414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005416 s_textArea = CreateWindowExW(
5417 0,
5418 szTextAreaClassW, L"Vim text area",
5419 WS_CHILD | WS_VISIBLE, 0, 0,
5420 100, // Any value will do for now
5421 100, // Any value will do for now
5422 s_hwnd, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02005423 g_hinst, NULL);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02005424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (s_textArea == NULL)
5426 return FAIL;
5427
Bram Moolenaar20321902016-02-17 12:30:17 +01005428#ifdef FEAT_LIBCALL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005429 // Try loading an icon from $RUNTIMEPATH/bitmaps/vim.ico.
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005430 {
5431 HANDLE hIcon = NULL;
5432
5433 if (mch_icon_load(&hIcon) == OK && hIcon != NULL)
Bram Moolenaar0f519a02014-10-06 18:10:09 +02005434 SendMessage(s_hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005435 }
Bram Moolenaar20321902016-02-17 12:30:17 +01005436#endif
Bram Moolenaarcddc91c2014-09-23 21:53:41 +02005437
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438#ifdef FEAT_MENU
5439 s_menuBar = CreateMenu();
5440#endif
5441 s_hdc = GetDC(s_textArea);
5442
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 DragAcceptFiles(s_hwnd, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
Bram Moolenaar734a8672019-12-02 22:49:38 +01005445 // Do we need to bother with this?
K.Takatac81e9bf2022-01-16 14:15:49 +00005446 // m_fMouseAvail = pGetSystemMetricsForDpi(SM_MOUSEPRESENT, s_dpi);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
Bram Moolenaar734a8672019-12-02 22:49:38 +01005448 // Get background/foreground colors from the system
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 gui_mch_def_colors();
5450
Bram Moolenaar734a8672019-12-02 22:49:38 +01005451 // Get the colors from the "Normal" group (set in syntax.c or in a vimrc
5452 // file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 set_normal_colors();
5454
5455 /*
5456 * Check that none of the colors are the same as the background color.
5457 * Then store the current values as the defaults.
5458 */
5459 gui_check_colors();
5460 gui.def_norm_pixel = gui.norm_pixel;
5461 gui.def_back_pixel = gui.back_pixel;
5462
Bram Moolenaar734a8672019-12-02 22:49:38 +01005463 // Get the colors for the highlight groups (gui_check_colors() might have
5464 // changed them)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 highlight_gui_started();
5466
5467 /*
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005468 * Start out by adding the configured border width into the border offset.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
Bram Moolenaar97b0b0e2015-11-19 20:23:37 +01005470 gui.border_offset = gui.border_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 /*
5473 * Set up for Intellimouse processing
5474 */
5475 init_mouse_wheel();
5476
5477 /*
5478 * compute a couple of metrics used for the dialogs
5479 */
5480 get_dialog_font_metrics();
5481#ifdef FEAT_TOOLBAR
5482 /*
5483 * Create the toolbar
5484 */
5485 initialise_toolbar();
5486#endif
Bram Moolenaar3991dab2006-03-27 17:01:56 +00005487#ifdef FEAT_GUI_TABLINE
5488 /*
5489 * Create the tabline
5490 */
5491 initialise_tabline();
5492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493#ifdef MSWIN_FIND_REPLACE
5494 /*
5495 * Initialise the dialog box stuff
5496 */
5497 s_findrep_msg = RegisterWindowMessage(FINDMSGSTRING);
5498
Bram Moolenaar734a8672019-12-02 22:49:38 +01005499 // Initialise the struct
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 s_findrep_struct.lStructSize = sizeof(s_findrep_struct);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005501 s_findrep_struct.lpstrFindWhat = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 s_findrep_struct.lpstrFindWhat[0] = NUL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005503 s_findrep_struct.lpstrReplaceWith = ALLOC_MULT(WCHAR, MSWIN_FR_BUFSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 s_findrep_struct.lpstrReplaceWith[0] = NUL;
5505 s_findrep_struct.wFindWhatLen = MSWIN_FR_BUFSIZE;
5506 s_findrep_struct.wReplaceWithLen = MSWIN_FR_BUFSIZE;
5507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005509#ifdef FEAT_EVAL
Bram Moolenaar734a8672019-12-02 22:49:38 +01005510 // set the v:windowid variable
Bram Moolenaar7154b322011-05-25 21:18:06 +02005511 set_vim_var_nr(VV_WINDOWID, HandleToLong(s_hwnd));
Bram Moolenaar264e9fd2010-10-27 12:33:17 +02005512#endif
5513
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02005514#ifdef FEAT_RENDER_OPTIONS
5515 if (p_rop)
5516 (void)gui_mch_set_rendering_options(p_rop);
5517#endif
5518
Bram Moolenaar748bf032005-02-02 23:04:36 +00005519theend:
Bram Moolenaar734a8672019-12-02 22:49:38 +01005520 // Display any pending error messages
Bram Moolenaar748bf032005-02-02 23:04:36 +00005521 display_errors();
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 return OK;
5524}
5525
5526/*
5527 * Get the size of the screen, taking position on multiple monitors into
5528 * account (if supported).
5529 */
5530 static void
5531get_work_area(RECT *spi_rect)
5532{
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005533 HMONITOR mon;
5534 MONITORINFO moninfo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar734a8672019-12-02 22:49:38 +01005536 // work out which monitor the window is on, and get *its* work area
Bram Moolenaar87f3d202016-12-01 20:18:50 +01005537 mon = MonitorFromWindow(s_hwnd, MONITOR_DEFAULTTOPRIMARY);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005538 if (mon != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005540 moninfo.cbSize = sizeof(MONITORINFO);
5541 if (GetMonitorInfo(mon, &moninfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005543 *spi_rect = moninfo.rcWork;
5544 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01005547 // this is the old method...
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 SystemParametersInfo(SPI_GETWORKAREA, 0, spi_rect, 0);
5549}
5550
5551/*
5552 * Set the size of the window to the given width and height in pixels.
5553 */
5554 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01005555gui_mch_set_shellsize(
5556 int width,
5557 int height,
5558 int min_width UNUSED,
5559 int min_height UNUSED,
5560 int base_width UNUSED,
5561 int base_height UNUSED,
Bram Moolenaarafa24992006-03-27 20:58:26 +00005562 int direction)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 RECT workarea_rect;
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005565 RECT window_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 int win_width, win_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
Bram Moolenaar734a8672019-12-02 22:49:38 +01005568 // Try to keep window completely on screen.
5569 // Get position of the screen work area. This is the part that is not
5570 // used by the taskbar or appbars.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 get_work_area(&workarea_rect);
5572
Bram Moolenaar734a8672019-12-02 22:49:38 +01005573 // Resizing a maximized window looks very strange, unzoom it first.
5574 // But don't do it when still starting up, it may have been requested in
5575 // the shortcut.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005576 if (IsZoomed(s_hwnd) && starting == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 ShowWindow(s_hwnd, SW_SHOWNORMAL);
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005578
Ken Takata7086b3e2023-10-02 21:26:03 +02005579 if (s_in_dpichanged)
5580 // Use the suggested position when in WM_DPICHANGED.
5581 window_rect = s_suggested_rect;
5582 else
5583 // Use current position.
5584 GetWindowRect(s_hwnd, &window_rect);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar734a8672019-12-02 22:49:38 +01005586 // compute the size of the outside of the window
K.Takatac81e9bf2022-01-16 14:15:49 +00005587 win_width = width + (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
5588 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
5589 win_height = height + (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
5590 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
5591 + pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
5592 + gui_mswin_get_menu_height(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar734a8672019-12-02 22:49:38 +01005594 // The following should take care of keeping Vim on the same monitor, no
5595 // matter if the secondary monitor is left or right of the primary
5596 // monitor.
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005597 window_rect.right = window_rect.left + win_width;
5598 window_rect.bottom = window_rect.top + win_height;
Bram Moolenaar56a907a2006-05-06 21:44:30 +00005599
Bram Moolenaar734a8672019-12-02 22:49:38 +01005600 // If the window is going off the screen, move it on to the screen.
Ken Takata7086b3e2023-10-02 21:26:03 +02005601 // Don't adjust the position when in WM_DPICHANGED.
5602 if (!s_in_dpichanged)
5603 {
5604 if ((direction & RESIZE_HOR)
5605 && window_rect.right > workarea_rect.right)
5606 OffsetRect(&window_rect,
5607 workarea_rect.right - window_rect.right, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
Ken Takata7086b3e2023-10-02 21:26:03 +02005609 if ((direction & RESIZE_HOR)
5610 && window_rect.left < workarea_rect.left)
5611 OffsetRect(&window_rect,
5612 workarea_rect.left - window_rect.left, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Ken Takata7086b3e2023-10-02 21:26:03 +02005614 if ((direction & RESIZE_VERT)
5615 && window_rect.bottom > workarea_rect.bottom)
5616 OffsetRect(&window_rect,
5617 0, workarea_rect.bottom - window_rect.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
Ken Takata7086b3e2023-10-02 21:26:03 +02005619 if ((direction & RESIZE_VERT)
5620 && window_rect.top < workarea_rect.top)
5621 OffsetRect(&window_rect,
5622 0, workarea_rect.top - window_rect.top);
5623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaar98af99f2020-07-16 22:30:31 +02005625 MoveWindow(s_hwnd, window_rect.left, window_rect.top,
5626 win_width, win_height, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Ken Takata7086b3e2023-10-02 21:26:03 +02005628 //TRACE("New pos: %d,%d New size: %d,%d",
5629 // window_rect.left, window_rect.top, win_width, win_height);
5630
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 SetActiveWindow(s_hwnd);
5632 SetFocus(s_hwnd);
5633
Bram Moolenaar734a8672019-12-02 22:49:38 +01005634 // Menu may wrap differently now
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636}
5637
5638
5639 void
5640gui_mch_set_scrollbar_thumb(
5641 scrollbar_T *sb,
5642 long val,
5643 long size,
5644 long max)
5645{
5646 SCROLLINFO info;
5647
5648 sb->scroll_shift = 0;
5649 while (max > 32767)
5650 {
5651 max = (max + 1) >> 1;
5652 val >>= 1;
5653 size >>= 1;
5654 ++sb->scroll_shift;
5655 }
5656
5657 if (sb->scroll_shift > 0)
5658 ++size;
5659
5660 info.cbSize = sizeof(info);
5661 info.fMask = SIF_POS | SIF_RANGE | SIF_PAGE;
5662 info.nPos = val;
5663 info.nMin = 0;
5664 info.nMax = max;
5665 info.nPage = size;
5666 SetScrollInfo(sb->id, SB_CTL, &info, TRUE);
5667}
5668
5669
5670/*
5671 * Set the current text font.
5672 */
5673 void
5674gui_mch_set_font(GuiFont font)
5675{
5676 gui.currFont = font;
5677}
5678
5679
5680/*
5681 * Set the current text foreground color.
5682 */
5683 void
5684gui_mch_set_fg_color(guicolor_T color)
5685{
5686 gui.currFgColor = color;
5687}
5688
5689/*
5690 * Set the current text background color.
5691 */
5692 void
5693gui_mch_set_bg_color(guicolor_T color)
5694{
5695 gui.currBgColor = color;
5696}
5697
Bram Moolenaare2cc9702005-03-15 22:43:58 +00005698/*
5699 * Set the current text special color.
5700 */
5701 void
5702gui_mch_set_sp_color(guicolor_T color)
5703{
5704 gui.currSpColor = color;
5705}
5706
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005707#ifdef FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708/*
5709 * Multi-byte handling, originally by Sung-Hoon Baek.
5710 * First static functions (no prototypes generated).
5711 */
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005712# ifdef _MSC_VER
Bram Moolenaar734a8672019-12-02 22:49:38 +01005713# include <ime.h> // Apparently not needed for Cygwin or MinGW.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005714# endif
5715# include <imm.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 * handle WM_IME_NOTIFY message
5719 */
5720 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005721_OnImeNotify(HWND hWnd, DWORD dwCommand, DWORD dwData UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 LRESULT lResult = 0;
5724 HIMC hImc;
5725
5726 if (!pImmGetContext || (hImc = pImmGetContext(hWnd)) == (HIMC)0)
5727 return lResult;
5728 switch (dwCommand)
5729 {
5730 case IMN_SETOPENSTATUS:
5731 if (pImmGetOpenStatus(hImc))
5732 {
K.Takatac81e9bf2022-01-16 14:15:49 +00005733 LOGFONTW lf = norm_logfont;
5734 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5735 // Work around when PerMonitorV2 is not enabled in the process level.
5736 lf.lfHeight = lf.lfHeight * DEFAULT_DPI / s_dpi;
5737 pImmSetCompositionFontW(hImc, &lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 im_set_position(gui.row, gui.col);
5739
Bram Moolenaar734a8672019-12-02 22:49:38 +01005740 // Disable langmap
Bram Moolenaar24959102022-05-07 20:01:16 +01005741 State &= ~MODE_LANGMAP;
5742 if (State & MODE_INSERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005744# if defined(FEAT_KEYMAP)
Bram Moolenaar734a8672019-12-02 22:49:38 +01005745 // Unshown 'keymap' in status lines
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
5747 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005748 // Save cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 int old_row = gui.row;
5750 int old_col = gui.col;
5751
5752 // This must be called here before
5753 // status_redraw_curbuf(), otherwise the mode
5754 // message may appear in the wrong position.
5755 showmode();
5756 status_redraw_curbuf();
5757 update_screen(0);
Bram Moolenaar734a8672019-12-02 22:49:38 +01005758 // Restore cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 gui.row = old_row;
5760 gui.col = old_col;
5761 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01005762# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
5764 }
5765 gui_update_cursor(TRUE, FALSE);
Bram Moolenaar92467d32017-12-05 13:22:16 +01005766 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 lResult = 0;
5768 break;
5769 }
5770 pImmReleaseContext(hWnd, hImc);
5771 return lResult;
5772}
5773
5774 static LRESULT
Bram Moolenaar1266d672017-02-01 13:43:36 +01005775_OnImeComposition(HWND hwnd, WPARAM dbcs UNUSED, LPARAM param)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 char_u *ret;
5778 int len;
5779
Bram Moolenaar734a8672019-12-02 22:49:38 +01005780 if ((param & GCS_RESULTSTR) == 0) // Composition unfinished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 return 0;
5782
5783 ret = GetResultStr(hwnd, GCS_RESULTSTR, &len);
5784 if (ret != NULL)
5785 {
5786 add_to_input_buf_csi(ret, len);
5787 vim_free(ret);
5788 return 1;
5789 }
5790 return 0;
5791}
5792
5793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 * void GetResultStr()
5795 *
5796 * This handles WM_IME_COMPOSITION with GCS_RESULTSTR flag on.
5797 * get complete composition string
5798 */
5799 static char_u *
5800GetResultStr(HWND hwnd, int GCS, int *lenp)
5801{
Bram Moolenaar734a8672019-12-02 22:49:38 +01005802 HIMC hIMC; // Input context handle.
K.Takatab0b2b732022-01-19 12:59:21 +00005803 LONG ret;
5804 WCHAR *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 char_u *convbuf = NULL;
5806
5807 if (!pImmGetContext || (hIMC = pImmGetContext(hwnd)) == (HIMC)0)
5808 return NULL;
5809
K.Takatab0b2b732022-01-19 12:59:21 +00005810 // Get the length of the composition string.
5811 ret = pImmGetCompositionStringW(hIMC, GCS, NULL, 0);
5812 if (ret <= 0)
5813 return NULL;
5814
5815 // Allocate the requested buffer plus space for the NUL character.
5816 buf = alloc(ret + sizeof(WCHAR));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 if (buf == NULL)
5818 return NULL;
5819
K.Takatab0b2b732022-01-19 12:59:21 +00005820 // Reads in the composition string.
5821 pImmGetCompositionStringW(hIMC, GCS, buf, ret);
5822 *lenp = ret / sizeof(WCHAR);
5823
Bram Moolenaar36f692d2008-11-20 16:10:17 +00005824 convbuf = utf16_to_enc(buf, lenp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 pImmReleaseContext(hwnd, hIMC);
5826 vim_free(buf);
5827 return convbuf;
5828}
5829#endif
5830
Bram Moolenaar734a8672019-12-02 22:49:38 +01005831// For global functions we need prototypes.
Bram Moolenaarbdb81392017-11-27 23:24:08 +01005832#if defined(FEAT_MBYTE_IME) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834/*
5835 * set font to IM.
5836 */
5837 void
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005838im_set_font(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 HIMC hImc;
5841
5842 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5843 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01005844 pImmSetCompositionFontW(hImc, lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 pImmReleaseContext(s_hwnd, hImc);
5846 }
5847}
5848
5849/*
5850 * Notify cursor position to IM.
5851 */
5852 void
5853im_set_position(int row, int col)
5854{
5855 HIMC hImc;
5856
5857 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5858 {
5859 COMPOSITIONFORM cfs;
5860
5861 cfs.dwStyle = CFS_POINT;
5862 cfs.ptCurrentPos.x = FILL_X(col);
5863 cfs.ptCurrentPos.y = FILL_Y(row);
5864 MapWindowPoints(s_textArea, s_hwnd, &cfs.ptCurrentPos, 1);
K.Takatac81e9bf2022-01-16 14:15:49 +00005865 if (s_process_dpi_aware == DPI_AWARENESS_UNAWARE)
5866 {
5867 // Work around when PerMonitorV2 is not enabled in the process level.
5868 cfs.ptCurrentPos.x = cfs.ptCurrentPos.x * DEFAULT_DPI / s_dpi;
5869 cfs.ptCurrentPos.y = cfs.ptCurrentPos.y * DEFAULT_DPI / s_dpi;
5870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 pImmSetCompositionWindow(hImc, &cfs);
5872
5873 pImmReleaseContext(s_hwnd, hImc);
5874 }
5875}
5876
5877/*
5878 * Set IM status on ("active" is TRUE) or off ("active" is FALSE).
5879 */
5880 void
5881im_set_active(int active)
5882{
5883 HIMC hImc;
5884 static HIMC hImcOld = (HIMC)0;
5885
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005886# ifdef VIMDLL
5887 if (!gui.in_use && !gui.starting)
5888 {
5889 mbyte_im_set_active(active);
5890 return;
5891 }
5892# endif
5893
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005894 if (!pImmGetContext) // if NULL imm32.dll wasn't loaded (yet)
5895 return;
5896
5897 if (p_imdisable)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005899 if (hImcOld == (HIMC)0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005901 hImcOld = pImmGetContext(s_hwnd);
5902 if (hImcOld)
5903 pImmAssociateContext(s_hwnd, (HIMC)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005905 active = FALSE;
5906 }
5907 else if (hImcOld != (HIMC)0)
5908 {
5909 pImmAssociateContext(s_hwnd, hImcOld);
5910 hImcOld = (HIMC)0;
5911 }
5912
5913 hImc = pImmGetContext(s_hwnd);
5914 if (!hImc)
5915 return;
5916
5917 /*
5918 * for Korean ime
5919 */
5920 HKL hKL = GetKeyboardLayout(0);
5921
5922 if (LOWORD(hKL) == MAKELANGID(LANG_KOREAN, SUBLANG_KOREAN))
5923 {
5924 static DWORD dwConversionSaved = 0, dwSentenceSaved = 0;
5925 static BOOL bSaved = FALSE;
5926
5927 if (active)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005929 // if we have a saved conversion status, restore it
5930 if (bSaved)
5931 pImmSetConversionStatus(hImc, dwConversionSaved,
5932 dwSentenceSaved);
5933 bSaved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005937 // save conversion status and disable korean
5938 if (pImmGetConversionStatus(hImc, &dwConversionSaved,
5939 &dwSentenceSaved))
Bram Moolenaarca003e12006-03-17 23:19:38 +00005940 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005941 bSaved = TRUE;
5942 pImmSetConversionStatus(hImc,
5943 dwConversionSaved & ~(IME_CMODE_NATIVE
5944 | IME_CMODE_FULLSHAPE),
5945 dwSentenceSaved);
Bram Moolenaarca003e12006-03-17 23:19:38 +00005946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 }
5948 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00005949
5950 pImmSetOpenStatus(hImc, active);
5951 pImmReleaseContext(s_hwnd, hImc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952}
5953
5954/*
5955 * Get IM status. When IM is on, return not 0. Else return 0.
5956 */
5957 int
Bram Moolenaar68c2f632016-01-30 17:24:07 +01005958im_get_status(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 int status = 0;
5961 HIMC hImc;
5962
Bram Moolenaar310c32e2019-11-29 23:15:25 +01005963# ifdef VIMDLL
5964 if (!gui.in_use && !gui.starting)
5965 return mbyte_im_get_status();
5966# endif
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 if (pImmGetContext && (hImc = pImmGetContext(s_hwnd)) != (HIMC)0)
5969 {
5970 status = pImmGetOpenStatus(hImc) ? 1 : 0;
5971 pImmReleaseContext(s_hwnd, hImc);
5972 }
5973 return status;
5974}
5975
Bram Moolenaar734a8672019-12-02 22:49:38 +01005976#endif // FEAT_MBYTE_IME
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005979/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00005980 * Convert latin9 text "text[len]" to ucs-2 in "unicodebuf".
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005981 */
5982 static void
5983latin9_to_ucs(char_u *text, int len, WCHAR *unicodebuf)
5984{
5985 int c;
5986
Bram Moolenaarca003e12006-03-17 23:19:38 +00005987 while (--len >= 0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005988 {
5989 c = *text++;
5990 switch (c)
5991 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01005992 case 0xa4: c = 0x20ac; break; // euro
5993 case 0xa6: c = 0x0160; break; // S hat
5994 case 0xa8: c = 0x0161; break; // S -hat
5995 case 0xb4: c = 0x017d; break; // Z hat
5996 case 0xb8: c = 0x017e; break; // Z -hat
5997 case 0xbc: c = 0x0152; break; // OE
5998 case 0xbd: c = 0x0153; break; // oe
5999 case 0xbe: c = 0x0178; break; // Y
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006000 }
6001 *unicodebuf++ = c;
6002 }
6003}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004
6005#ifdef FEAT_RIGHTLEFT
6006/*
6007 * What is this for? In the case where you are using Win98 or Win2K or later,
6008 * and you are using a Hebrew font (or Arabic!), Windows does you a favor and
6009 * reverses the string sent to the TextOut... family. This sucks, because we
6010 * go to a lot of effort to do the right thing, and there doesn't seem to be a
6011 * way to tell Windblows not to do this!
6012 *
6013 * The short of it is that this 'RevOut' only gets called if you are running
6014 * one of the new, "improved" MS OSes, and only if you are running in
6015 * 'rightleft' mode. It makes display take *slightly* longer, but not
6016 * noticeably so.
6017 */
6018 static void
K.Takata135e1522022-01-29 15:27:58 +00006019RevOut( HDC hdc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 int col,
6021 int row,
6022 UINT foptions,
6023 CONST RECT *pcliprect,
6024 LPCTSTR text,
6025 UINT len,
6026 CONST INT *padding)
6027{
6028 int ix;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006030 for (ix = 0; ix < (int)len; ++ix)
K.Takata135e1522022-01-29 15:27:58 +00006031 ExtTextOut(hdc, col + TEXT_X(ix), row, foptions,
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006032 pcliprect, text + ix, 1, padding);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033}
6034#endif
6035
Bram Moolenaar92467d32017-12-05 13:22:16 +01006036 static void
6037draw_line(
6038 int x1,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006039 int y1,
6040 int x2,
6041 int y2,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006042 COLORREF color)
6043{
6044#if defined(FEAT_DIRECTX)
6045 if (IS_ENABLE_DIRECTX())
6046 DWriteContext_DrawLine(s_dwc, x1, y1, x2, y2, color);
6047 else
6048#endif
6049 {
6050 HPEN hpen = CreatePen(PS_SOLID, 1, color);
6051 HPEN old_pen = SelectObject(s_hdc, hpen);
6052 MoveToEx(s_hdc, x1, y1, NULL);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006053 // Note: LineTo() excludes the last pixel in the line.
Bram Moolenaar92467d32017-12-05 13:22:16 +01006054 LineTo(s_hdc, x2, y2);
6055 DeleteObject(SelectObject(s_hdc, old_pen));
6056 }
6057}
6058
6059 static void
6060set_pixel(
6061 int x,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006062 int y,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006063 COLORREF color)
6064{
6065#if defined(FEAT_DIRECTX)
6066 if (IS_ENABLE_DIRECTX())
6067 DWriteContext_SetPixel(s_dwc, x, y, color);
6068 else
6069#endif
6070 SetPixel(s_hdc, x, y, color);
6071}
6072
6073 static void
6074fill_rect(
6075 const RECT *rcp,
Bram Moolenaard385b5d2018-12-27 22:43:08 +01006076 HBRUSH hbr,
Bram Moolenaar92467d32017-12-05 13:22:16 +01006077 COLORREF color)
6078{
6079#if defined(FEAT_DIRECTX)
6080 if (IS_ENABLE_DIRECTX())
6081 DWriteContext_FillRect(s_dwc, rcp, color);
6082 else
6083#endif
6084 {
6085 HBRUSH hbr2;
6086
6087 if (hbr == NULL)
6088 hbr2 = CreateSolidBrush(color);
6089 else
6090 hbr2 = hbr;
6091 FillRect(s_hdc, rcp, hbr2);
6092 if (hbr == NULL)
6093 DeleteBrush(hbr2);
6094 }
6095}
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 void
6098gui_mch_draw_string(
6099 int row,
6100 int col,
6101 char_u *text,
6102 int len,
6103 int flags)
6104{
6105 static int *padding = NULL;
6106 static int pad_size = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 const RECT *pcliprect = NULL;
6108 UINT foptions = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 static WCHAR *unicodebuf = NULL;
6110 static int *unicodepdy = NULL;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006111 static int unibuflen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 int n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 int y;
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /*
6116 * Italic and bold text seems to have an extra row of pixels at the bottom
6117 * (below where the bottom of the character should be). If we draw the
6118 * characters with a solid background, the top row of pixels in the
6119 * character below will be overwritten. We can fix this by filling in the
6120 * background ourselves, to the correct character proportions, and then
6121 * writing the character in transparent mode. Still have a problem when
6122 * the character is "_", which gets written on to the character below.
6123 * New fix: set gui.char_ascent to -1. This shifts all characters up one
6124 * pixel in their slots, which fixes the problem with the bottom row of
6125 * pixels. We still need this code because otherwise the top row of pixels
6126 * becomes a problem. - webb.
6127 */
6128 static HBRUSH hbr_cache[2] = {NULL, NULL};
6129 static guicolor_T brush_color[2] = {INVALCOLOR, INVALCOLOR};
6130 static int brush_lru = 0;
6131 HBRUSH hbr;
6132 RECT rc;
6133
6134 if (!(flags & DRAW_TRANSP))
6135 {
6136 /*
6137 * Clear background first.
6138 * Note: FillRect() excludes right and bottom of rectangle.
6139 */
6140 rc.left = FILL_X(col);
6141 rc.top = FILL_Y(row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (has_mbyte)
6143 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006144 // Compute the length in display cells.
Bram Moolenaar72597a52010-07-18 15:31:08 +02006145 rc.right = FILL_X(col + mb_string2cells(text, len));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 }
6147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 rc.right = FILL_X(col + len);
6149 rc.bottom = FILL_Y(row + 1);
6150
Bram Moolenaar734a8672019-12-02 22:49:38 +01006151 // Cache the created brush, that saves a lot of time. We need two:
6152 // one for cursor background and one for the normal background.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 if (gui.currBgColor == brush_color[0])
6154 {
6155 hbr = hbr_cache[0];
6156 brush_lru = 1;
6157 }
6158 else if (gui.currBgColor == brush_color[1])
6159 {
6160 hbr = hbr_cache[1];
6161 brush_lru = 0;
6162 }
6163 else
6164 {
6165 if (hbr_cache[brush_lru] != NULL)
6166 DeleteBrush(hbr_cache[brush_lru]);
6167 hbr_cache[brush_lru] = CreateSolidBrush(gui.currBgColor);
6168 brush_color[brush_lru] = gui.currBgColor;
6169 hbr = hbr_cache[brush_lru];
6170 brush_lru = !brush_lru;
6171 }
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006172
Bram Moolenaar92467d32017-12-05 13:22:16 +01006173 fill_rect(&rc, hbr, gui.currBgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
6175 SetBkMode(s_hdc, TRANSPARENT);
6176
6177 /*
6178 * When drawing block cursor, prevent inverted character spilling
6179 * over character cell (can happen with bold/italic)
6180 */
6181 if (flags & DRAW_CURSOR)
6182 {
6183 pcliprect = &rc;
6184 foptions = ETO_CLIPPED;
6185 }
6186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 SetTextColor(s_hdc, gui.currFgColor);
6188 SelectFont(s_hdc, gui.currFont);
6189
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006190#ifdef FEAT_DIRECTX
6191 if (IS_ENABLE_DIRECTX())
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006192 DWriteContext_SetFont(s_dwc, (HFONT)gui.currFont);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006193#endif
6194
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 if (pad_size != Columns || padding == NULL || padding[0] != gui.char_width)
6196 {
K.Takata135e1522022-01-29 15:27:58 +00006197 int i;
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 vim_free(padding);
6200 pad_size = Columns;
6201
Bram Moolenaar734a8672019-12-02 22:49:38 +01006202 // Don't give an out-of-memory message here, it would call us
6203 // recursively.
Bram Moolenaar59edb002019-05-28 23:32:47 +02006204 padding = LALLOC_MULT(int, pad_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 if (padding != NULL)
6206 for (i = 0; i < pad_size; i++)
6207 padding[i] = gui.char_width;
6208 }
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /*
6211 * We have to provide the padding argument because italic and bold versions
6212 * of fixed-width fonts are often one pixel or so wider than their normal
6213 * versions.
6214 * No check for DRAW_BOLD, Windows will have done it already.
6215 */
6216
Bram Moolenaar734a8672019-12-02 22:49:38 +01006217 // Check if there are any UTF-8 characters. If not, use normal text
6218 // output to speed up output.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (enc_utf8)
6220 for (n = 0; n < len; ++n)
6221 if (text[n] >= 0x80)
6222 break;
6223
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006224#if defined(FEAT_DIRECTX)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006225 // Quick hack to enable DirectWrite. To use DirectWrite (antialias), it is
6226 // required that unicode drawing routine, currently. So this forces it
6227 // enabled.
Bram Moolenaar7f88b652017-12-14 13:15:19 +01006228 if (IS_ENABLE_DIRECTX())
Bram Moolenaar734a8672019-12-02 22:49:38 +01006229 n = 0; // Keep n < len, to enter block for unicode.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006230#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006231
Bram Moolenaar734a8672019-12-02 22:49:38 +01006232 // Check if the Unicode buffer exists and is big enough. Create it
6233 // with the same length as the multi-byte string, the number of wide
6234 // characters is always equal or smaller.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006235 if ((enc_utf8
6236 || (enc_codepage > 0 && (int)GetACP() != enc_codepage)
6237 || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 && (unicodebuf == NULL || len > unibuflen))
6239 {
6240 vim_free(unicodebuf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006241 unicodebuf = LALLOC_MULT(WCHAR, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 vim_free(unicodepdy);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006244 unicodepdy = LALLOC_MULT(int, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 unibuflen = len;
6247 }
6248
6249 if (enc_utf8 && n < len && unicodebuf != NULL)
6250 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006251 // Output UTF-8 characters. Composing characters should be
6252 // handled here.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006253 int i;
Bram Moolenaar734a8672019-12-02 22:49:38 +01006254 int wlen; // string length in words
Bram Moolenaar734a8672019-12-02 22:49:38 +01006255 int cells; // cell width of string up to composing char
6256 int cw; // width of current cell
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006257 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00006259 wlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 cells = 0;
Bram Moolenaarca003e12006-03-17 23:19:38 +00006261 for (i = 0; i < len; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006263 c = utf_ptr2char(text + i);
6264 if (c >= 0x10000)
6265 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006266 // Turn into UTF-16 encoding.
Bram Moolenaarca003e12006-03-17 23:19:38 +00006267 unicodebuf[wlen++] = ((c - 0x10000) >> 10) + 0xD800;
6268 unicodebuf[wlen++] = ((c - 0x10000) & 0x3ff) + 0xDC00;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006269 }
6270 else
6271 {
Bram Moolenaarca003e12006-03-17 23:19:38 +00006272 unicodebuf[wlen++] = c;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006273 }
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006274
6275 if (utf_iscomposing(c))
6276 cw = 0;
6277 else
6278 {
6279 cw = utf_char2cells(c);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006280 if (cw > 2) // don't use 4 for unprintable char
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006281 cw = 1;
6282 }
6283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 if (unicodepdy != NULL)
6285 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006286 // Use unicodepdy to make characters fit as we expect, even
6287 // when the font uses different widths (e.g., bold character
6288 // is wider).
Bram Moolenaard804fdf2016-02-27 16:04:58 +01006289 if (c >= 0x10000)
6290 {
6291 unicodepdy[wlen - 2] = cw * gui.char_width;
6292 unicodepdy[wlen - 1] = 0;
6293 }
6294 else
6295 unicodepdy[wlen - 1] = cw * gui.char_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297 cells += cw;
Bram Moolenaara6ce1cc2017-10-28 19:23:11 +02006298 i += utf_ptr2len_len(text + i, len - i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 }
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006300#if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006301 if (IS_ENABLE_DIRECTX())
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006302 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006303 // Add one to "cells" for italics.
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006304 DWriteContext_DrawText(s_dwc, unicodebuf, wlen,
Bram Moolenaar60ebd522019-03-21 20:50:12 +01006305 TEXT_X(col), TEXT_Y(row),
6306 FILL_X(cells + 1), FILL_Y(1) - p_linespace,
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006307 gui.char_width, gui.currFgColor,
6308 foptions, pcliprect, unicodepdy);
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006309 }
6310 else
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01006311#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02006312 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
6313 foptions, pcliprect, unicodebuf, wlen, unicodepdy);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006314 len = cells; // used for underlining
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 }
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006316 else if ((enc_codepage > 0 && (int)GetACP() != enc_codepage) || enc_latin9)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006318 // If we want to display codepage data, and the current CP is not the
6319 // ANSI one, we need to go via Unicode.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 if (unicodebuf != NULL)
6321 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006322 if (enc_latin9)
6323 latin9_to_ucs(text, len, unicodebuf);
6324 else
6325 len = MultiByteToWideChar(enc_codepage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 MB_PRECOMPOSED,
6327 (char *)text, len,
6328 (LPWSTR)unicodebuf, unibuflen);
6329 if (len != 0)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006330 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006331 // Use unicodepdy to make characters fit as we expect, even
6332 // when the font uses different widths (e.g., bold character
6333 // is wider).
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006334 if (unicodepdy != NULL)
6335 {
6336 int i;
6337 int cw;
6338
6339 for (i = 0; i < len; ++i)
6340 {
6341 cw = utf_char2cells(unicodebuf[i]);
6342 if (cw > 2)
6343 cw = 1;
6344 unicodepdy[i] = cw * gui.char_width;
6345 }
6346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 ExtTextOutW(s_hdc, TEXT_X(col), TEXT_Y(row),
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006348 foptions, pcliprect, unicodebuf, len, unicodepdy);
6349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 }
6351 }
6352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 {
6354#ifdef FEAT_RIGHTLEFT
Bram Moolenaar734a8672019-12-02 22:49:38 +01006355 // Windows will mess up RL text, so we have to draw it character by
6356 // character. Only do this if RL is on, since it's slow.
Bram Moolenaar4ee40b02014-09-19 16:13:53 +02006357 if (curwin->w_p_rl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 RevOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6359 foptions, pcliprect, (char *)text, len, padding);
6360 else
6361#endif
6362 ExtTextOut(s_hdc, TEXT_X(col), TEXT_Y(row),
6363 foptions, pcliprect, (char *)text, len, padding);
6364 }
6365
Bram Moolenaar734a8672019-12-02 22:49:38 +01006366 // Underline
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 if (flags & DRAW_UNDERL)
6368 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006369 // When p_linespace is 0, overwrite the bottom row of pixels.
6370 // Otherwise put the line just below the character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 y = FILL_Y(row + 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (p_linespace > 1)
6373 y -= p_linespace - 1;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006374 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currFgColor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006376
Bram Moolenaar734a8672019-12-02 22:49:38 +01006377 // Strikethrough
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006378 if (flags & DRAW_STRIKE)
6379 {
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006380 y = FILL_Y(row + 1) - gui.char_height/2;
Bram Moolenaar92467d32017-12-05 13:22:16 +01006381 draw_line(FILL_X(col), y, FILL_X(col + len), y, gui.currSpColor);
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006382 }
6383
Bram Moolenaar734a8672019-12-02 22:49:38 +01006384 // Undercurl
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006385 if (flags & DRAW_UNDERC)
6386 {
6387 int x;
6388 int offset;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006389 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006390
6391 y = FILL_Y(row + 1) - 1;
6392 for (x = FILL_X(col); x < FILL_X(col + len); ++x)
6393 {
6394 offset = val[x % 8];
Bram Moolenaar92467d32017-12-05 13:22:16 +01006395 set_pixel(x, y - offset, gui.currSpColor);
Bram Moolenaare2cc9702005-03-15 22:43:58 +00006396 }
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398}
6399
6400
6401/*
6402 * Output routines.
6403 */
6404
Bram Moolenaar734a8672019-12-02 22:49:38 +01006405/*
6406 * Flush any output to the screen
6407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 void
6409gui_mch_flush(void)
6410{
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01006411#if defined(FEAT_DIRECTX)
6412 if (IS_ENABLE_DIRECTX())
6413 DWriteContext_Flush(s_dwc);
6414#endif
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 GdiFlush();
6417}
6418
6419 static void
6420clear_rect(RECT *rcp)
6421{
Bram Moolenaar92467d32017-12-05 13:22:16 +01006422 fill_rect(rcp, NULL, gui.back_pixel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425
Bram Moolenaarc716c302006-01-21 22:12:51 +00006426 void
6427gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
6428{
6429 RECT workarea_rect;
6430
6431 get_work_area(&workarea_rect);
6432
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006433 *screen_w = workarea_rect.right - workarea_rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00006434 - (pGetSystemMetricsForDpi(SM_CXFRAME, s_dpi) +
6435 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2;
Bram Moolenaarc716c302006-01-21 22:12:51 +00006436
Bram Moolenaar734a8672019-12-02 22:49:38 +01006437 // FIXME: dirty trick: Because the gui_get_base_height() doesn't include
6438 // the menubar for MSwin, we subtract it from the screen height, so that
6439 // the window size can be made to fit on the screen.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006440 *screen_h = workarea_rect.bottom - workarea_rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00006441 - (pGetSystemMetricsForDpi(SM_CYFRAME, s_dpi) +
6442 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, s_dpi)) * 2
6443 - pGetSystemMetricsForDpi(SM_CYCAPTION, s_dpi)
6444 - gui_mswin_get_menu_height(FALSE);
Bram Moolenaarc716c302006-01-21 22:12:51 +00006445}
6446
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448#if defined(FEAT_MENU) || defined(PROTO)
6449/*
6450 * Add a sub menu to the menu bar.
6451 */
6452 void
6453gui_mch_add_menu(
6454 vimmenu_T *menu,
6455 int pos)
6456{
6457 vimmenu_T *parent = menu->parent;
6458
6459 menu->submenu_id = CreatePopupMenu();
6460 menu->id = s_menu_id++;
6461
6462 if (menu_is_menubar(menu->name))
6463 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006464 WCHAR *wn;
6465 MENUITEMINFOW infow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006467 wn = enc_to_utf16(menu->name, NULL);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006468 if (wn == NULL)
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006469 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006471 infow.cbSize = sizeof(infow);
6472 infow.fMask = MIIM_DATA | MIIM_TYPE | MIIM_ID
6473 | MIIM_SUBMENU;
6474 infow.dwItemData = (long_u)menu;
6475 infow.wID = menu->id;
6476 infow.fType = MFT_STRING;
6477 infow.dwTypeData = wn;
6478 infow.cch = (UINT)wcslen(wn);
6479 infow.hSubMenu = menu->submenu_id;
6480 InsertMenuItemW((parent == NULL)
6481 ? s_menuBar : parent->submenu_id,
6482 (UINT)pos, TRUE, &infow);
6483 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485
Bram Moolenaar734a8672019-12-02 22:49:38 +01006486 // Fix window size if menu may have wrapped
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (parent == NULL)
6488 gui_mswin_get_menu_height(!gui.starting);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006489# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 else if (IsWindow(parent->tearoff_handle))
6491 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493}
6494
6495 void
6496gui_mch_show_popupmenu(vimmenu_T *menu)
6497{
6498 POINT mp;
6499
K.Takata45f9cfb2022-01-21 11:11:00 +00006500 (void)GetCursorPos(&mp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 gui_mch_show_popupmenu_at(menu, (int)mp.x, (int)mp.y);
6502}
6503
6504 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00006505gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
6507 vimmenu_T *menu = gui_find_menu(path_name);
6508
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006509 if (menu == NULL)
6510 return;
6511
6512 POINT p;
6513
6514 // Find the position of the current cursor
6515 GetDCOrgEx(s_hdc, &p);
6516 if (mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006518 int mx, my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006520 gui_mch_getmouse(&mx, &my);
6521 p.x += mx;
6522 p.y += my;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 }
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00006524 else if (curwin != NULL)
6525 {
6526 p.x += TEXT_X(curwin->w_wincol + curwin->w_wcol + 1);
6527 p.y += TEXT_Y(W_WINROW(curwin) + curwin->w_wrow + 1);
6528 }
6529 msg_scroll = FALSE;
6530 gui_mch_show_popupmenu_at(menu, (int)p.x, (int)p.y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531}
6532
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006533# if defined(FEAT_TEAROFF) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534/*
6535 * Given a menu descriptor, e.g. "File.New", find it in the menu hierarchy and
6536 * create it as a pseudo-"tearoff menu".
6537 */
6538 void
6539gui_make_tearoff(char_u *path_name)
6540{
6541 vimmenu_T *menu = gui_find_menu(path_name);
6542
Bram Moolenaar734a8672019-12-02 22:49:38 +01006543 // Found the menu, so tear it off.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (menu != NULL)
6545 gui_mch_tearoff(menu->dname, menu, 0xffffL, 0xffffL);
6546}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006547# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548
6549/*
6550 * Add a menu item to a menu
6551 */
6552 void
6553gui_mch_add_menu_item(
6554 vimmenu_T *menu,
6555 int idx)
6556{
6557 vimmenu_T *parent = menu->parent;
6558
6559 menu->id = s_menu_id++;
6560 menu->submenu_id = NULL;
6561
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006562# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (STRNCMP(menu->name, TEAR_STRING, TEAR_LEN) == 0)
6564 {
6565 InsertMenu(parent->submenu_id, (UINT)idx, MF_BITMAP|MF_BYPOSITION,
6566 (UINT)menu->id, (LPCTSTR) s_htearbitmap);
6567 }
6568 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006569# endif
6570# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (menu_is_toolbar(parent->name))
6572 {
6573 TBBUTTON newtb;
6574
Bram Moolenaara80faa82020-04-12 19:37:17 +02006575 CLEAR_FIELD(newtb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 if (menu_is_separator(menu->name))
6577 {
6578 newtb.iBitmap = 0;
6579 newtb.fsStyle = TBSTYLE_SEP;
6580 }
6581 else
6582 {
6583 newtb.iBitmap = get_toolbar_bitmap(menu);
6584 newtb.fsStyle = TBSTYLE_BUTTON;
6585 }
6586 newtb.idCommand = menu->id;
6587 newtb.fsState = TBSTATE_ENABLED;
6588 newtb.iString = 0;
6589 SendMessage(s_toolbarhwnd, TB_INSERTBUTTON, (WPARAM)idx,
6590 (LPARAM)&newtb);
6591 menu->submenu_id = (HMENU)-1;
6592 }
6593 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006596 WCHAR *wn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006598 wn = enc_to_utf16(menu->name, NULL);
6599 if (wn != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006601 InsertMenuW(parent->submenu_id, (UINT)idx,
6602 (menu_is_separator(menu->name)
6603 ? MF_SEPARATOR : MF_STRING) | MF_BYPOSITION,
6604 (UINT)menu->id, wn);
6605 vim_free(wn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006607# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (IsWindow(parent->tearoff_handle))
6609 rebuild_tearoff(parent);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612}
6613
6614/*
6615 * Destroy the machine specific menu widget.
6616 */
6617 void
6618gui_mch_destroy_menu(vimmenu_T *menu)
6619{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006620# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 /*
6622 * is this a toolbar button?
6623 */
6624 if (menu->submenu_id == (HMENU)-1)
6625 {
6626 int iButton;
6627
6628 iButton = (int)SendMessage(s_toolbarhwnd, TB_COMMANDTOINDEX,
6629 (WPARAM)menu->id, 0);
6630 SendMessage(s_toolbarhwnd, TB_DELETEBUTTON, (WPARAM)iButton, 0);
6631 }
6632 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 {
6635 if (menu->parent != NULL
6636 && menu_is_popup(menu->parent->dname)
6637 && menu->parent->submenu_id != NULL)
6638 RemoveMenu(menu->parent->submenu_id, menu->id, MF_BYCOMMAND);
6639 else
6640 RemoveMenu(s_menuBar, menu->id, MF_BYCOMMAND);
6641 if (menu->submenu_id != NULL)
6642 DestroyMenu(menu->submenu_id);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006643# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (IsWindow(menu->tearoff_handle))
6645 DestroyWindow(menu->tearoff_handle);
6646 if (menu->parent != NULL
6647 && menu->parent->children != NULL
6648 && IsWindow(menu->parent->tearoff_handle))
6649 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01006650 // This menu must not show up when rebuilding the tearoff window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 menu->modes = 0;
6652 rebuild_tearoff(menu->parent);
6653 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 }
6656}
6657
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006658# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 static void
6660rebuild_tearoff(vimmenu_T *menu)
6661{
Bram Moolenaar734a8672019-12-02 22:49:38 +01006662 //hackish
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 char_u tbuf[128];
6664 RECT trect;
6665 RECT rct;
6666 RECT roct;
6667 int x, y;
6668
6669 HWND thwnd = menu->tearoff_handle;
6670
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006671 GetWindowText(thwnd, (LPSTR)tbuf, 127);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (GetWindowRect(thwnd, &trect)
6673 && GetWindowRect(s_hwnd, &rct)
6674 && GetClientRect(s_hwnd, &roct))
6675 {
6676 x = trect.left - rct.left;
6677 y = (trect.top - rct.bottom + roct.bottom);
6678 }
6679 else
6680 {
6681 x = y = 0xffffL;
6682 }
6683 DestroyWindow(thwnd);
6684 if (menu->children != NULL)
6685 {
6686 gui_mch_tearoff(tbuf, menu, x, y);
6687 if (IsWindow(menu->tearoff_handle))
6688 (void) SetWindowPos(menu->tearoff_handle,
6689 NULL,
6690 (int)trect.left,
6691 (int)trect.top,
6692 0, 0,
6693 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
6694 }
6695}
Bram Moolenaar734a8672019-12-02 22:49:38 +01006696# endif // FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697
6698/*
6699 * Make a menu either grey or not grey.
6700 */
6701 void
6702gui_mch_menu_grey(
6703 vimmenu_T *menu,
6704 int grey)
6705{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006706# ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 /*
6708 * is this a toolbar button?
6709 */
6710 if (menu->submenu_id == (HMENU)-1)
6711 {
6712 SendMessage(s_toolbarhwnd, TB_ENABLEBUTTON,
K.Takata45f9cfb2022-01-21 11:11:00 +00006713 (WPARAM)menu->id, (LPARAM) MAKELONG((grey ? FALSE : TRUE), 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006716# endif
Bram Moolenaar762f1752016-06-04 22:36:17 +02006717 (void)EnableMenuItem(menu->parent ? menu->parent->submenu_id : s_menuBar,
6718 menu->id, MF_BYCOMMAND | (grey ? MF_GRAYED : MF_ENABLED));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006720# ifdef FEAT_TEAROFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if ((menu->parent != NULL) && (IsWindow(menu->parent->tearoff_handle)))
6722 {
6723 WORD menuID;
6724 HWND menuHandle;
6725
6726 /*
6727 * A tearoff button has changed state.
6728 */
6729 if (menu->children == NULL)
6730 menuID = (WORD)(menu->id);
6731 else
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006732 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 menuHandle = GetDlgItem(menu->parent->tearoff_handle, menuID);
6734 if (menuHandle)
6735 EnableWindow(menuHandle, !grey);
6736
6737 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739}
6740
Bram Moolenaar734a8672019-12-02 22:49:38 +01006741#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743
Bram Moolenaar734a8672019-12-02 22:49:38 +01006744// define some macros used to make the dialogue creation more readable
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746#define add_word(x) *p++ = (x)
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006747#define add_long(x) dwp = (DWORD *)p; *dwp++ = (x); p = (WORD *)dwp
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748
6749#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
6750/*
6751 * stuff for dialogs
6752 */
6753
6754/*
6755 * The callback routine used by all the dialogs. Very simple. First,
6756 * acknowledges the INITDIALOG message so that Windows knows to do standard
6757 * dialog stuff (Return = default, Esc = cancel....) Second, if a button is
6758 * pressed, return that button's ID - IDCANCEL (2), which is the button's
6759 * number.
6760 */
6761 static LRESULT CALLBACK
6762dialog_callback(
6763 HWND hwnd,
6764 UINT message,
6765 WPARAM wParam,
Bram Moolenaar1266d672017-02-01 13:43:36 +01006766 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
6768 if (message == WM_INITDIALOG)
6769 {
6770 CenterWindow(hwnd, GetWindow(hwnd, GW_OWNER));
Bram Moolenaar734a8672019-12-02 22:49:38 +01006771 // Set focus to the dialog. Set the default button, if specified.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 (void)SetFocus(hwnd);
6773 if (dialog_default_button > IDCANCEL)
6774 (void)SetFocus(GetDlgItem(hwnd, dialog_default_button));
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006775 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01006776 // We don't have a default, set focus on another element of the
6777 // dialog window, probably the icon
Bram Moolenaar2b80e652007-08-14 14:57:55 +00006778 (void)SetFocus(GetDlgItem(hwnd, DLG_NONBUTTON_CONTROL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 return FALSE;
6780 }
6781
6782 if (message == WM_COMMAND)
6783 {
6784 int button = LOWORD(wParam);
6785
Bram Moolenaar734a8672019-12-02 22:49:38 +01006786 // Don't end the dialog if something was selected that was
6787 // not a button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 if (button >= DLG_NONBUTTON_CONTROL)
6789 return TRUE;
6790
Bram Moolenaar734a8672019-12-02 22:49:38 +01006791 // If the edit box exists, copy the string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (s_textfield != NULL)
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006793 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006794 WCHAR *wp = ALLOC_MULT(WCHAR, IOSIZE);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006795 char_u *p;
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006796
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02006797 GetDlgItemTextW(hwnd, DLG_NONBUTTON_CONTROL + 2, wp, IOSIZE);
6798 p = utf16_to_enc(wp, NULL);
6799 vim_strncpy(s_textfield, p, IOSIZE);
6800 vim_free(p);
6801 vim_free(wp);
Bram Moolenaar3ca9a8a2009-01-28 20:23:17 +00006802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
6804 /*
6805 * Need to check for IDOK because if the user just hits Return to
6806 * accept the default value, some reason this is what we get.
6807 */
6808 if (button == IDOK)
6809 {
6810 if (dialog_default_button > IDCANCEL)
6811 EndDialog(hwnd, dialog_default_button);
6812 }
6813 else
6814 EndDialog(hwnd, button - IDCANCEL);
6815 return TRUE;
6816 }
6817
6818 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
6819 {
6820 EndDialog(hwnd, 0);
6821 return TRUE;
6822 }
6823 return FALSE;
6824}
6825
6826/*
6827 * Create a dialog dynamically from the parameter strings.
6828 * type = type of dialog (question, alert, etc.)
6829 * title = dialog title. may be NULL for default title.
6830 * message = text to display. Dialog sizes to accommodate it.
6831 * buttons = '\n' separated list of button captions, default first.
6832 * dfltbutton = number of default button.
6833 *
6834 * This routine returns 1 if the first button is pressed,
6835 * 2 for the second, etc.
6836 *
6837 * 0 indicates Esc was pressed.
6838 * -1 for unexpected error
6839 *
6840 * If stubbing out this fn, return 1.
6841 */
6842
Bram Moolenaar734a8672019-12-02 22:49:38 +01006843static const char *dlg_icons[] = // must match names in resource file
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 "IDR_VIM",
6846 "IDR_VIM_ERROR",
6847 "IDR_VIM_ALERT",
6848 "IDR_VIM_INFO",
6849 "IDR_VIM_QUESTION"
6850};
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 int
6853gui_mch_dialog(
6854 int type,
6855 char_u *title,
6856 char_u *message,
6857 char_u *buttons,
6858 int dfltbutton,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01006859 char_u *textfield,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006860 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 WORD *p, *pdlgtemplate, *pnumitems;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006863 DWORD *dwp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 int numButtons;
6865 int *buttonWidths, *buttonPositions;
6866 int buttonYpos;
6867 int nchar, i;
6868 DWORD lStyle;
6869 int dlgwidth = 0;
6870 int dlgheight;
6871 int editboxheight;
6872 int horizWidth = 0;
6873 int msgheight;
6874 char_u *pstart;
6875 char_u *pend;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006876 char_u *last_white;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 char_u *tbuffer;
6878 RECT rect;
6879 HWND hwnd;
6880 HDC hdc;
6881 HFONT font, oldFont;
6882 TEXTMETRIC fontInfo;
6883 int fontHeight;
6884 int textWidth, minButtonWidth, messageWidth;
6885 int maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00006886 int maxDialogHeight;
6887 int scroll_flag = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 int vertical;
6889 int dlgPaddingX;
6890 int dlgPaddingY;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006891# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006892 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006894# endif
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006895 garray_T ga;
6896 int l;
K.Takatac81e9bf2022-01-16 14:15:49 +00006897 int dlg_icon_width;
6898 int dlg_icon_height;
6899 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006901# ifndef NO_CONSOLE
Bram Moolenaar734a8672019-12-02 22:49:38 +01006902 // Don't output anything in silent mode ("ex -s")
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006903# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006904 if (!(gui.in_use || gui.starting))
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006905# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006906 if (silent_mode)
Bram Moolenaar734a8672019-12-02 22:49:38 +01006907 return dfltbutton; // return default option
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
Bram Moolenaar748bf032005-02-02 23:04:36 +00006910 if (s_hwnd == NULL)
K.Takatac81e9bf2022-01-16 14:15:49 +00006911 {
6912 load_dpi_func();
6913 s_dpi = dpi = pGetDpiForSystem();
Bram Moolenaar748bf032005-02-02 23:04:36 +00006914 get_dialog_font_metrics();
K.Takatac81e9bf2022-01-16 14:15:49 +00006915 }
6916 else
6917 dpi = pGetDpiForSystem();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919 if ((type < 0) || (type > VIM_LAST_TYPE))
6920 type = 0;
6921
Bram Moolenaar734a8672019-12-02 22:49:38 +01006922 // allocate some memory for dialog template
6923 // TODO should compute this really
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006924 pdlgtemplate = p = (PWORD)LocalAlloc(LPTR,
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006925 DLG_ALLOC_SIZE + STRLEN(message) * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 if (p == NULL)
6928 return -1;
6929
6930 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02006931 * make a copy of 'buttons' to fiddle with it. compiler grizzles because
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 * vim_strsave() doesn't take a const arg (why not?), so cast away the
6933 * const.
6934 */
6935 tbuffer = vim_strsave(buttons);
6936 if (tbuffer == NULL)
6937 return -1;
6938
Bram Moolenaar734a8672019-12-02 22:49:38 +01006939 --dfltbutton; // Change from one-based to zero-based
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
Bram Moolenaar734a8672019-12-02 22:49:38 +01006941 // Count buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 numButtons = 1;
6943 for (i = 0; tbuffer[i] != '\0'; i++)
6944 {
6945 if (tbuffer[i] == DLG_BUTTON_SEP)
6946 numButtons++;
6947 }
6948 if (dfltbutton >= numButtons)
6949 dfltbutton = -1;
6950
Bram Moolenaar734a8672019-12-02 22:49:38 +01006951 // Allocate array to hold the width of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006952 buttonWidths = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (buttonWidths == NULL)
6954 return -1;
6955
Bram Moolenaar734a8672019-12-02 22:49:38 +01006956 // Allocate array to hold the X position of each button
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006957 buttonPositions = ALLOC_MULT(int, numButtons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 if (buttonPositions == NULL)
6959 return -1;
6960
6961 /*
6962 * Calculate how big the dialog must be.
6963 */
6964 hwnd = GetDesktopWindow();
6965 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006966# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
6968 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01006969 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 use_lfSysmenu = TRUE;
6971 }
6972 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01006973# endif
K.Takatad1c58992022-01-23 12:31:57 +00006974 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
6975 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
6976
6977 oldFont = SelectFont(hdc, font);
6978 dlgPaddingX = DLG_PADDING_X;
6979 dlgPaddingY = DLG_PADDING_Y;
6980
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 GetTextMetrics(hdc, &fontInfo);
6982 fontHeight = fontInfo.tmHeight;
6983
Bram Moolenaar734a8672019-12-02 22:49:38 +01006984 // Minimum width for horizontal button
Bram Moolenaar418f81b2016-02-16 20:12:02 +01006985 minButtonWidth = GetTextWidth(hdc, (char_u *)"Cancel", 6);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaar734a8672019-12-02 22:49:38 +01006987 // Maximum width of a dialog, if possible
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006988 if (s_hwnd == NULL)
6989 {
6990 RECT workarea_rect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
Bram Moolenaar734a8672019-12-02 22:49:38 +01006992 // We don't have a window, use the desktop area.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006993 get_work_area(&workarea_rect);
6994 maxDialogWidth = workarea_rect.right - workarea_rect.left - 100;
K.Takatac81e9bf2022-01-16 14:15:49 +00006995 if (maxDialogWidth > adjust_by_system_dpi(600))
6996 maxDialogWidth = adjust_by_system_dpi(600);
Bram Moolenaar734a8672019-12-02 22:49:38 +01006997 // Leave some room for the taskbar.
Bram Moolenaar1b1b0942013-08-01 13:20:42 +02006998 maxDialogHeight = workarea_rect.bottom - workarea_rect.top - 150;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00006999 }
7000 else
7001 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007002 // Use our own window for the size, unless it's very small.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007003 GetWindowRect(s_hwnd, &rect);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007004 maxDialogWidth = rect.right - rect.left
K.Takatac81e9bf2022-01-16 14:15:49 +00007005 - (pGetSystemMetricsForDpi(SM_CXFRAME, dpi) +
7006 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 2;
7007 if (maxDialogWidth < adjust_by_system_dpi(DLG_MIN_MAX_WIDTH))
7008 maxDialogWidth = adjust_by_system_dpi(DLG_MIN_MAX_WIDTH);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007009
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007010 maxDialogHeight = rect.bottom - rect.top
K.Takatac81e9bf2022-01-16 14:15:49 +00007011 - (pGetSystemMetricsForDpi(SM_CYFRAME, dpi) +
7012 pGetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi)) * 4
7013 - pGetSystemMetricsForDpi(SM_CYCAPTION, dpi);
7014 if (maxDialogHeight < adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT))
7015 maxDialogHeight = adjust_by_system_dpi(DLG_MIN_MAX_HEIGHT);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007016 }
7017
Bram Moolenaar734a8672019-12-02 22:49:38 +01007018 // Set dlgwidth to width of message.
7019 // Copy the message into "ga", changing NL to CR-NL and inserting line
7020 // breaks where needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 pstart = message;
7022 messageWidth = 0;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007023 msgheight = 0;
7024 ga_init2(&ga, sizeof(char), 500);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 do
7026 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007027 msgheight += fontHeight; // at least one line
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007028
Bram Moolenaar734a8672019-12-02 22:49:38 +01007029 // Need to figure out where to break the string. The system does it
7030 // at a word boundary, which would mean we can't compute the number of
7031 // wrapped lines.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007032 textWidth = 0;
7033 last_white = NULL;
7034 for (pend = pstart; *pend != NUL && *pend != '\n'; )
Bram Moolenaar748bf032005-02-02 23:04:36 +00007035 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007036 l = (*mb_ptr2len)(pend);
Bram Moolenaar1c465442017-03-12 20:10:05 +01007037 if (l == 1 && VIM_ISWHITE(*pend)
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007038 && textWidth > maxDialogWidth * 3 / 4)
7039 last_white = pend;
Bram Moolenaarf05d8112013-06-26 12:58:32 +02007040 textWidth += GetTextWidthEnc(hdc, pend, l);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007041 if (textWidth >= maxDialogWidth)
Bram Moolenaar748bf032005-02-02 23:04:36 +00007042 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007043 // Line will wrap.
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007044 messageWidth = maxDialogWidth;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007045 msgheight += fontHeight;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007046 textWidth = 0;
7047
7048 if (last_white != NULL)
7049 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007050 // break the line just after a space
K.Takatac81e9bf2022-01-16 14:15:49 +00007051 if (pend > last_white)
7052 ga.ga_len -= (int)(pend - (last_white + 1));
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007053 pend = last_white + 1;
7054 last_white = NULL;
7055 }
7056 ga_append(&ga, '\r');
7057 ga_append(&ga, '\n');
7058 continue;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007059 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007060
7061 while (--l >= 0)
7062 ga_append(&ga, *pend++);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007063 }
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007064 if (textWidth > messageWidth)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 messageWidth = textWidth;
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007066
7067 ga_append(&ga, '\r');
7068 ga_append(&ga, '\n');
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 pstart = pend + 1;
7070 } while (*pend != NUL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00007071
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007072 if (ga.ga_data != NULL)
7073 message = ga.ga_data;
7074
Bram Moolenaar734a8672019-12-02 22:49:38 +01007075 messageWidth += 10; // roundoff space
Bram Moolenaar748bf032005-02-02 23:04:36 +00007076
K.Takatac81e9bf2022-01-16 14:15:49 +00007077 dlg_icon_width = adjust_by_system_dpi(DLG_ICON_WIDTH);
7078 dlg_icon_height = adjust_by_system_dpi(DLG_ICON_HEIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
K.Takatac81e9bf2022-01-16 14:15:49 +00007080 // Add width of icon to dlgwidth, and some space
7081 dlgwidth = messageWidth + dlg_icon_width + 3 * dlgPaddingX
7082 + pGetSystemMetricsForDpi(SM_CXVSCROLL, dpi);
7083
7084 if (msgheight < dlg_icon_height)
7085 msgheight = dlg_icon_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
7087 /*
7088 * Check button names. A long one will make the dialog wider.
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007089 * When called early (-register error message) p_go isn't initialized.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 */
Bram Moolenaaraea02fa2007-05-04 20:29:09 +00007091 vertical = (p_go != NULL && vim_strchr(p_go, GO_VERTICAL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 if (!vertical)
7093 {
7094 // Place buttons horizontally if they fit.
7095 horizWidth = dlgPaddingX;
7096 pstart = tbuffer;
7097 i = 0;
7098 do
7099 {
7100 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7101 if (pend == NULL)
7102 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007103 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (textWidth < minButtonWidth)
7105 textWidth = minButtonWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007106 textWidth += dlgPaddingX; // Padding within button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 buttonWidths[i] = textWidth;
7108 buttonPositions[i++] = horizWidth;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007109 horizWidth += textWidth + dlgPaddingX; // Pad between buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 pstart = pend + 1;
7111 } while (*pend != NUL);
7112
7113 if (horizWidth > maxDialogWidth)
7114 vertical = TRUE; // Too wide to fit on the screen.
7115 else if (horizWidth > dlgwidth)
7116 dlgwidth = horizWidth;
7117 }
7118
7119 if (vertical)
7120 {
7121 // Stack buttons vertically.
7122 pstart = tbuffer;
7123 do
7124 {
7125 pend = vim_strchr(pstart, DLG_BUTTON_SEP);
7126 if (pend == NULL)
7127 pend = pstart + STRLEN(pstart); // Last button name.
Bram Moolenaarb052fe02013-06-26 13:16:20 +02007128 textWidth = GetTextWidthEnc(hdc, pstart, (int)(pend - pstart));
Bram Moolenaar734a8672019-12-02 22:49:38 +01007129 textWidth += dlgPaddingX; // Padding within button
7130 textWidth += DLG_VERT_PADDING_X * 2; // Padding around button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 if (textWidth > dlgwidth)
7132 dlgwidth = textWidth;
7133 pstart = pend + 1;
7134 } while (*pend != NUL);
7135 }
7136
7137 if (dlgwidth < DLG_MIN_WIDTH)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007138 dlgwidth = DLG_MIN_WIDTH; // Don't allow a really thin dialog!
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
Bram Moolenaar734a8672019-12-02 22:49:38 +01007140 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007141 lStyle = DS_MODALFRAME | WS_CAPTION | DS_3DLOOK | WS_VISIBLE | DS_SETFONT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 add_long(lStyle);
7144 add_long(0); // (lExtendedStyle)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007145 pnumitems = p; //save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 add_word(0); // NumberOfItems(will change later)
7147 add_word(10); // x
7148 add_word(10); // y
7149 add_word(PixelToDialogX(dlgwidth)); // cx
7150
7151 // Dialog height.
7152 if (vertical)
Bram Moolenaara95d8232013-08-07 15:27:11 +02007153 dlgheight = msgheight + 2 * dlgPaddingY
7154 + DLG_VERT_PADDING_Y + 2 * fontHeight * numButtons;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 else
7156 dlgheight = msgheight + 3 * dlgPaddingY + 2 * fontHeight;
7157
7158 // Dialog needs to be taller if contains an edit box.
7159 editboxheight = fontHeight + dlgPaddingY + 4 * DLG_VERT_PADDING_Y;
7160 if (textfield != NULL)
7161 dlgheight += editboxheight;
7162
Bram Moolenaar734a8672019-12-02 22:49:38 +01007163 // Restrict the size to a maximum. Causes a scrollbar to show up.
Bram Moolenaara95d8232013-08-07 15:27:11 +02007164 if (dlgheight > maxDialogHeight)
7165 {
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +02007166 msgheight = msgheight - (dlgheight - maxDialogHeight);
7167 dlgheight = maxDialogHeight;
7168 scroll_flag = WS_VSCROLL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007169 // Make sure scrollbar doesn't appear in the middle of the dialog
K.Takatac81e9bf2022-01-16 14:15:49 +00007170 messageWidth = dlgwidth - dlg_icon_width - 3 * dlgPaddingX;
Bram Moolenaara95d8232013-08-07 15:27:11 +02007171 }
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 add_word(PixelToDialogY(dlgheight));
7174
7175 add_word(0); // Menu
7176 add_word(0); // Class
7177
Bram Moolenaar734a8672019-12-02 22:49:38 +01007178 // copy the title of the dialog
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007179 nchar = nCopyAnsiToWideChar(p, (title ? (LPSTR)title
7180 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 p += nchar;
7182
K.Takatad1c58992022-01-23 12:31:57 +00007183 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007184# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007185 if (use_lfSysmenu)
7186 {
7187 // point size
7188 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7189 GetDeviceCaps(hdc, LOGPIXELSY));
7190 wcscpy(p, lfSysmenu.lfFaceName);
7191 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 }
K.Takatad1c58992022-01-23 12:31:57 +00007193 else
7194# endif
7195 {
7196 *p++ = DLG_FONT_POINT_SIZE; // point size
7197 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7198 }
7199 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
7201 buttonYpos = msgheight + 2 * dlgPaddingY;
7202
7203 if (textfield != NULL)
7204 buttonYpos += editboxheight;
7205
7206 pstart = tbuffer;
7207 if (!vertical)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007208 horizWidth = (dlgwidth - horizWidth) / 2; // Now it's X offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 for (i = 0; i < numButtons; i++)
7210 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007211 // get end of this button.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 for ( pend = pstart;
7213 *pend && (*pend != DLG_BUTTON_SEP);
7214 pend++)
7215 ;
7216
7217 if (*pend)
7218 *pend = '\0';
7219
7220 /*
7221 * old NOTE:
7222 * setting the BS_DEFPUSHBUTTON style doesn't work because Windows sets
7223 * the focus to the first tab-able button and in so doing makes that
7224 * the default!! Grrr. Workaround: Make the default button the only
7225 * one with WS_TABSTOP style. Means user can't tab between buttons, but
7226 * he/she can use arrow keys.
7227 *
7228 * new NOTE: BS_DEFPUSHBUTTON is required to be able to select the
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007229 * right button when hitting <Enter>. E.g., for the ":confirm quit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 * dialog. Also needed for when the textfield is the default control.
7231 * It appears to work now (perhaps not on Win95?).
7232 */
7233 if (vertical)
7234 {
7235 p = add_dialog_element(p,
7236 (i == dfltbutton
7237 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7238 PixelToDialogX(DLG_VERT_PADDING_X),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007239 PixelToDialogY(buttonYpos // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 + 2 * fontHeight * i),
7241 PixelToDialogX(dlgwidth - 2 * DLG_VERT_PADDING_X),
7242 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007243 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
7246 {
7247 p = add_dialog_element(p,
7248 (i == dfltbutton
7249 ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON) | WS_TABSTOP,
7250 PixelToDialogX(horizWidth + buttonPositions[i]),
Bram Moolenaar734a8672019-12-02 22:49:38 +01007251 PixelToDialogY(buttonYpos), // TBK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 PixelToDialogX(buttonWidths[i]),
7253 (WORD)(PixelToDialogY(2 * fontHeight) - 1),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007254 (WORD)(IDCANCEL + 1 + i), (WORD)0x0080, (char *)pstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 }
Bram Moolenaar734a8672019-12-02 22:49:38 +01007256 pstart = pend + 1; //next button
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258 *pnumitems += numButtons;
7259
Bram Moolenaar734a8672019-12-02 22:49:38 +01007260 // Vim icon
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 p = add_dialog_element(p, SS_ICON,
7262 PixelToDialogX(dlgPaddingX),
7263 PixelToDialogY(dlgPaddingY),
K.Takatac81e9bf2022-01-16 14:15:49 +00007264 PixelToDialogX(dlg_icon_width),
7265 PixelToDialogY(dlg_icon_height),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 DLG_NONBUTTON_CONTROL + 0, (WORD)0x0082,
7267 dlg_icons[type]);
7268
Bram Moolenaar734a8672019-12-02 22:49:38 +01007269 // Dialog message
Bram Moolenaar748bf032005-02-02 23:04:36 +00007270 p = add_dialog_element(p, ES_LEFT|scroll_flag|ES_MULTILINE|ES_READONLY,
K.Takatac81e9bf2022-01-16 14:15:49 +00007271 PixelToDialogX(2 * dlgPaddingX + dlg_icon_width),
Bram Moolenaar748bf032005-02-02 23:04:36 +00007272 PixelToDialogY(dlgPaddingY),
7273 (WORD)(PixelToDialogX(messageWidth) + 1),
7274 PixelToDialogY(msgheight),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007275 DLG_NONBUTTON_CONTROL + 1, (WORD)0x0081, (char *)message);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276
Bram Moolenaar734a8672019-12-02 22:49:38 +01007277 // Edit box
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 if (textfield != NULL)
7279 {
7280 p = add_dialog_element(p, ES_LEFT|ES_AUTOHSCROLL|WS_TABSTOP|WS_BORDER,
7281 PixelToDialogX(2 * dlgPaddingX),
7282 PixelToDialogY(2 * dlgPaddingY + msgheight),
7283 PixelToDialogX(dlgwidth - 4 * dlgPaddingX),
7284 PixelToDialogY(fontHeight + dlgPaddingY),
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007285 DLG_NONBUTTON_CONTROL + 2, (WORD)0x0081, (char *)textfield);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 *pnumitems += 1;
7287 }
7288
7289 *pnumitems += 2;
7290
7291 SelectFont(hdc, oldFont);
7292 DeleteObject(font);
7293 ReleaseDC(hwnd, hdc);
7294
Bram Moolenaar734a8672019-12-02 22:49:38 +01007295 // Let the dialog_callback() function know which button to make default
7296 // If we have an edit box, make that the default. We also need to tell
7297 // dialog_callback() if this dialog contains an edit box or not. We do
7298 // this by setting s_textfield if it does.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (textfield != NULL)
7300 {
7301 dialog_default_button = DLG_NONBUTTON_CONTROL + 2;
7302 s_textfield = textfield;
7303 }
7304 else
7305 {
7306 dialog_default_button = IDCANCEL + 1 + dfltbutton;
7307 s_textfield = NULL;
7308 }
7309
Bram Moolenaar734a8672019-12-02 22:49:38 +01007310 // show the dialog box modally and get a return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 nchar = (int)DialogBoxIndirect(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007312 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 (LPDLGTEMPLATE)pdlgtemplate,
7314 s_hwnd,
7315 (DLGPROC)dialog_callback);
7316
7317 LocalFree(LocalHandle(pdlgtemplate));
7318 vim_free(tbuffer);
7319 vim_free(buttonWidths);
7320 vim_free(buttonPositions);
Bram Moolenaar3a7c85b2005-02-05 21:39:53 +00007321 vim_free(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
Bram Moolenaar734a8672019-12-02 22:49:38 +01007323 // Focus back to our window (for when MDI is used).
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 (void)SetFocus(s_hwnd);
7325
7326 return nchar;
7327}
7328
Bram Moolenaar734a8672019-12-02 22:49:38 +01007329#endif // FEAT_GUI_DIALOG
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331/*
7332 * Put a simple element (basic class) onto a dialog template in memory.
7333 * return a pointer to where the next item should be added.
7334 *
7335 * parameters:
7336 * lStyle = additional style flags
7337 * (be careful, NT3.51 & Win32s will ignore the new ones)
7338 * x,y = x & y positions IN DIALOG UNITS
7339 * w,h = width and height IN DIALOG UNITS
7340 * Id = ID used in messages
7341 * clss = class ID, e.g 0x0080 for a button, 0x0082 for a static
7342 * caption = usually text or resource name
7343 *
7344 * TODO: use the length information noted here to enable the dialog creation
7345 * routines to work out more exactly how much memory they need to alloc.
7346 */
7347 static PWORD
7348add_dialog_element(
7349 PWORD p,
7350 DWORD lStyle,
7351 WORD x,
7352 WORD y,
7353 WORD w,
7354 WORD h,
7355 WORD Id,
7356 WORD clss,
7357 const char *caption)
7358{
7359 int nchar;
7360
Bram Moolenaar734a8672019-12-02 22:49:38 +01007361 p = lpwAlign(p); // Align to dword boundary
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 lStyle = lStyle | WS_VISIBLE | WS_CHILD;
7363 *p++ = LOWORD(lStyle);
7364 *p++ = HIWORD(lStyle);
7365 *p++ = 0; // LOWORD (lExtendedStyle)
7366 *p++ = 0; // HIWORD (lExtendedStyle)
7367 *p++ = x;
7368 *p++ = y;
7369 *p++ = w;
7370 *p++ = h;
7371 *p++ = Id; //9 or 10 words in all
7372
7373 *p++ = (WORD)0xffff;
7374 *p++ = clss; //2 more here
7375
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007376 nchar = nCopyAnsiToWideChar(p, (LPSTR)caption, TRUE); //strlen(caption)+1
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 p += nchar;
7378
7379 *p++ = 0; // advance pointer over nExtraStuff WORD - 2 more
7380
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00007381 return p; // total = 15 + strlen(caption) words
7382 // bytes read = 2 * total
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383}
7384
7385
7386/*
7387 * Helper routine. Take an input pointer, return closest pointer that is
7388 * aligned on a DWORD (4 byte) boundary. Taken from the Win32SDK samples.
7389 */
7390 static LPWORD
7391lpwAlign(
7392 LPWORD lpIn)
7393{
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007394 long_u ul;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007396 ul = (long_u)lpIn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 ul += 3;
7398 ul >>= 2;
7399 ul <<= 2;
7400 return (LPWORD)ul;
7401}
7402
7403/*
7404 * Helper routine. Takes second parameter as Ansi string, copies it to first
7405 * parameter as wide character (16-bits / char) string, and returns integer
7406 * number of wide characters (words) in string (including the trailing wide
7407 * char NULL). Partly taken from the Win32SDK samples.
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007408 * If "use_enc" is TRUE, 'encoding' is used for "lpAnsiIn". If FALSE, current
7409 * ACP is used for "lpAnsiIn". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 static int
7411nCopyAnsiToWideChar(
7412 LPWORD lpWCStr,
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007413 LPSTR lpAnsiIn,
7414 BOOL use_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415{
7416 int nChar = 0;
Bram Moolenaar734a8672019-12-02 22:49:38 +01007417 int len = lstrlen(lpAnsiIn) + 1; // include NUL character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int i;
7419 WCHAR *wn;
7420
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007421 if (use_enc && enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007423 // Not a codepage, use our own conversion function.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007424 wn = enc_to_utf16((char_u *)lpAnsiIn, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (wn != NULL)
7426 {
7427 wcscpy(lpWCStr, wn);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007428 nChar = (int)wcslen(wn) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 vim_free(wn);
7430 }
7431 }
7432 if (nChar == 0)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007433 // Use Win32 conversion function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 nChar = MultiByteToWideChar(
7435 enc_codepage > 0 ? enc_codepage : CP_ACP,
7436 MB_PRECOMPOSED,
7437 lpAnsiIn, len,
7438 lpWCStr, len);
7439 for (i = 0; i < nChar; ++i)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007440 if (lpWCStr[i] == (WORD)'\t') // replace tabs with spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 lpWCStr[i] = (WORD)' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442
7443 return nChar;
7444}
7445
7446
7447#ifdef FEAT_TEAROFF
7448/*
Bram Moolenaar66857f42017-10-22 16:43:20 +02007449 * Lookup menu handle from "menu_id".
7450 */
7451 static HMENU
7452tearoff_lookup_menuhandle(
7453 vimmenu_T *menu,
7454 WORD menu_id)
7455{
7456 for ( ; menu != NULL; menu = menu->next)
7457 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007458 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar66857f42017-10-22 16:43:20 +02007459 continue;
7460 if (menu_is_separator(menu->dname))
7461 continue;
7462 if ((WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000) == menu_id)
7463 return menu->submenu_id;
7464 }
7465 return NULL;
7466}
7467
7468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 * The callback function for all the modeless dialogs that make up the
7470 * "tearoff menus" Very simple - forward button presses (to fool Vim into
7471 * thinking its menus have been clicked), and go away when closed.
7472 */
7473 static LRESULT CALLBACK
7474tearoff_callback(
7475 HWND hwnd,
7476 UINT message,
7477 WPARAM wParam,
7478 LPARAM lParam)
7479{
7480 if (message == WM_INITDIALOG)
Bram Moolenaar66857f42017-10-22 16:43:20 +02007481 {
7482 SetWindowLongPtr(hwnd, DWLP_USER, (LONG_PTR)lParam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 return (TRUE);
Bram Moolenaar66857f42017-10-22 16:43:20 +02007484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
Bram Moolenaar734a8672019-12-02 22:49:38 +01007486 // May show the mouse pointer again.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 HandleMouseHide(message, lParam);
7488
7489 if (message == WM_COMMAND)
7490 {
7491 if ((WORD)(LOWORD(wParam)) & 0x8000)
7492 {
7493 POINT mp;
7494 RECT rect;
7495
7496 if (GetCursorPos(&mp) && GetWindowRect(hwnd, &rect))
7497 {
Bram Moolenaar66857f42017-10-22 16:43:20 +02007498 vimmenu_T *menu;
7499
7500 menu = (vimmenu_T*)GetWindowLongPtr(hwnd, DWLP_USER);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 (void)TrackPopupMenu(
Bram Moolenaar66857f42017-10-22 16:43:20 +02007502 tearoff_lookup_menuhandle(menu, LOWORD(wParam)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 TPM_LEFTALIGN | TPM_LEFTBUTTON,
7504 (int)rect.right - 8,
7505 (int)mp.y,
Bram Moolenaar734a8672019-12-02 22:49:38 +01007506 (int)0, // reserved param
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 s_hwnd,
7508 NULL);
7509 /*
7510 * NOTE: The pop-up menu can eat the mouse up event.
7511 * We deal with this in normal.c.
7512 */
7513 }
7514 }
7515 else
Bram Moolenaar734a8672019-12-02 22:49:38 +01007516 // Pass on messages to the main Vim window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 PostMessage(s_hwnd, WM_COMMAND, LOWORD(wParam), 0);
7518 /*
7519 * Give main window the focus back: this is so after
7520 * choosing a tearoff button you can start typing again
7521 * straight away.
7522 */
7523 (void)SetFocus(s_hwnd);
7524 return TRUE;
7525 }
7526 if ((message == WM_SYSCOMMAND) && (wParam == SC_CLOSE))
7527 {
7528 DestroyWindow(hwnd);
7529 return TRUE;
7530 }
7531
Bram Moolenaar734a8672019-12-02 22:49:38 +01007532 // When moved around, give main window the focus back.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 if (message == WM_EXITSIZEMOVE)
7534 (void)SetActiveWindow(s_hwnd);
7535
7536 return FALSE;
7537}
7538#endif
7539
7540
7541/*
K.Takatad1c58992022-01-23 12:31:57 +00007542 * Computes the dialog base units based on the current dialog font.
7543 * We don't use the GetDialogBaseUnits() API, because we don't use the
7544 * (old-style) system font.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 */
7546 static void
7547get_dialog_font_metrics(void)
7548{
7549 HDC hdc;
7550 HFONT hfontTools = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 SIZE size;
7552#ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007553 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554#endif
7555
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556#ifdef USE_SYSMENU_FONT
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007557 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007558 hfontTools = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007559 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560#endif
7561 hfontTools = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
K.Takatac81e9bf2022-01-16 14:15:49 +00007562 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
K.Takatad1c58992022-01-23 12:31:57 +00007564 hdc = GetDC(s_hwnd);
7565 SelectObject(hdc, hfontTools);
K.Takataabe628e2022-01-23 16:25:17 +00007566 GetAverageFontSize(hdc, &size);
K.Takatad1c58992022-01-23 12:31:57 +00007567 ReleaseDC(s_hwnd, hdc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
K.Takataabe628e2022-01-23 16:25:17 +00007569 s_dlgfntwidth = (WORD)size.cx;
K.Takatad1c58992022-01-23 12:31:57 +00007570 s_dlgfntheight = (WORD)size.cy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571}
7572
7573#if defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7574/*
7575 * Create a pseudo-"tearoff menu" based on the child
7576 * items of a given menu pointer.
7577 */
7578 static void
7579gui_mch_tearoff(
7580 char_u *title,
7581 vimmenu_T *menu,
7582 int initX,
7583 int initY)
7584{
7585 WORD *p, *pdlgtemplate, *pnumitems, *ptrueheight;
7586 int template_len;
7587 int nchar, textWidth, submenuWidth;
7588 DWORD lStyle;
7589 DWORD lExtendedStyle;
7590 WORD dlgwidth;
7591 WORD menuID;
7592 vimmenu_T *pmenu;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007593 vimmenu_T *top_menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 vimmenu_T *the_menu = menu;
7595 HWND hwnd;
7596 HDC hdc;
7597 HFONT font, oldFont;
7598 int col, spaceWidth, len;
7599 int columnWidths[2];
7600 char_u *label, *text;
7601 int acLen = 0;
7602 int nameLen;
7603 int padding0, padding1, padding2 = 0;
7604 int sepPadding=0;
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007605 int x;
7606 int y;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007607# ifdef USE_SYSMENU_FONT
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007608 LOGFONTW lfSysmenu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 int use_lfSysmenu = FALSE;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612 /*
7613 * If this menu is already torn off, move it to the mouse position.
7614 */
7615 if (IsWindow(menu->tearoff_handle))
7616 {
7617 POINT mp;
K.Takata45f9cfb2022-01-21 11:11:00 +00007618 if (GetCursorPos(&mp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 {
7620 SetWindowPos(menu->tearoff_handle, NULL, mp.x, mp.y, 0, 0,
7621 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
7622 }
7623 return;
7624 }
7625
7626 /*
7627 * Create a new tearoff.
7628 */
7629 if (*title == MNU_HIDDEN_CHAR)
7630 title++;
7631
Bram Moolenaar734a8672019-12-02 22:49:38 +01007632 // Allocate memory to store the dialog template. It's made bigger when
7633 // needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 template_len = DLG_ALLOC_SIZE;
7635 pdlgtemplate = p = (WORD *)LocalAlloc(LPTR, template_len);
7636 if (p == NULL)
7637 return;
7638
7639 hwnd = GetDesktopWindow();
7640 hdc = GetWindowDC(hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007641# ifdef USE_SYSMENU_FONT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (gui_w32_get_menu_font(&lfSysmenu) == OK)
7643 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01007644 font = CreateFontIndirectW(&lfSysmenu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 use_lfSysmenu = TRUE;
7646 }
7647 else
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007648# endif
K.Takatad1c58992022-01-23 12:31:57 +00007649 font = CreateFont(-DLG_FONT_POINT_SIZE, 0, 0, 0, 0, 0, 0, 0,
7650 0, 0, 0, 0, VARIABLE_PITCH, DLG_FONT_NAME);
7651
7652 oldFont = SelectFont(hdc, font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653
Bram Moolenaar734a8672019-12-02 22:49:38 +01007654 // Calculate width of a single space. Used for padding columns to the
7655 // right width.
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007656 spaceWidth = GetTextWidth(hdc, (char_u *)" ", 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657
Bram Moolenaar734a8672019-12-02 22:49:38 +01007658 // Figure out max width of the text column, the accelerator column and the
7659 // optional submenu column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 submenuWidth = 0;
7661 for (col = 0; col < 2; col++)
7662 {
7663 columnWidths[col] = 0;
Bram Moolenaar00d253e2020-04-06 22:13:01 +02007664 FOR_ALL_CHILD_MENUS(menu, pmenu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007666 // Use "dname" here to compute the width of the visible text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 text = (col == 0) ? pmenu->dname : pmenu->actext;
7668 if (text != NULL && *text != NUL)
7669 {
7670 textWidth = GetTextWidthEnc(hdc, text, (int)STRLEN(text));
7671 if (textWidth > columnWidths[col])
7672 columnWidths[col] = textWidth;
7673 }
7674 if (pmenu->children != NULL)
7675 submenuWidth = TEAROFF_COLUMN_PADDING * spaceWidth;
7676 }
7677 }
7678 if (columnWidths[1] == 0)
7679 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007680 // no accelerators
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 if (submenuWidth != 0)
7682 columnWidths[0] += submenuWidth;
7683 else
7684 columnWidths[0] += spaceWidth;
7685 }
7686 else
7687 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007688 // there is an accelerator column
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 columnWidths[0] += TEAROFF_COLUMN_PADDING * spaceWidth;
7690 columnWidths[1] += submenuWidth;
7691 }
7692
7693 /*
7694 * Now find the total width of our 'menu'.
7695 */
7696 textWidth = columnWidths[0] + columnWidths[1];
7697 if (submenuWidth != 0)
7698 {
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007699 submenuWidth = GetTextWidth(hdc, (char_u *)TEAROFF_SUBMENU_LABEL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 (int)STRLEN(TEAROFF_SUBMENU_LABEL));
7701 textWidth += submenuWidth;
7702 }
7703 dlgwidth = GetTextWidthEnc(hdc, title, (int)STRLEN(title));
7704 if (textWidth > dlgwidth)
7705 dlgwidth = textWidth;
7706 dlgwidth += 2 * TEAROFF_PADDING_X + TEAROFF_BUTTON_PAD_X;
7707
Bram Moolenaar734a8672019-12-02 22:49:38 +01007708 // start to fill in the dlgtemplate information. addressing by WORDs
K.Takatad1c58992022-01-23 12:31:57 +00007709 lStyle = DS_MODALFRAME | WS_CAPTION | WS_SYSMENU | DS_SETFONT | WS_VISIBLE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710
7711 lExtendedStyle = WS_EX_TOOLWINDOW|WS_EX_STATICEDGE;
7712 *p++ = LOWORD(lStyle);
7713 *p++ = HIWORD(lStyle);
7714 *p++ = LOWORD(lExtendedStyle);
7715 *p++ = HIWORD(lExtendedStyle);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007716 pnumitems = p; // save where the number of items must be stored
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 *p++ = 0; // NumberOfItems(will change later)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007718 gui_mch_getmouse(&x, &y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (initX == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007720 *p++ = PixelToDialogX(x); // x
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 else
7722 *p++ = PixelToDialogX(initX); // x
7723 if (initY == 0xffffL)
Bram Moolenaar9588a0f2005-01-08 21:45:39 +00007724 *p++ = PixelToDialogY(y); // y
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 else
7726 *p++ = PixelToDialogY(initY); // y
7727 *p++ = PixelToDialogX(dlgwidth); // cx
7728 ptrueheight = p;
7729 *p++ = 0; // dialog height: changed later anyway
7730 *p++ = 0; // Menu
7731 *p++ = 0; // Class
7732
Bram Moolenaar734a8672019-12-02 22:49:38 +01007733 // copy the title of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 nchar = nCopyAnsiToWideChar(p, ((*title)
Bram Moolenaar6edeaf32017-09-26 14:46:04 +02007735 ? (LPSTR)title
7736 : (LPSTR)("Vim "VIM_VERSION_MEDIUM)), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 p += nchar;
7738
K.Takatad1c58992022-01-23 12:31:57 +00007739 // do the font, since DS_3DLOOK doesn't work properly
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007740# ifdef USE_SYSMENU_FONT
K.Takatad1c58992022-01-23 12:31:57 +00007741 if (use_lfSysmenu)
7742 {
7743 // point size
7744 *p++ = -MulDiv(lfSysmenu.lfHeight, 72,
7745 GetDeviceCaps(hdc, LOGPIXELSY));
7746 wcscpy(p, lfSysmenu.lfFaceName);
7747 nchar = (int)wcslen(lfSysmenu.lfFaceName) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 }
K.Takatad1c58992022-01-23 12:31:57 +00007749 else
7750# endif
7751 {
7752 *p++ = DLG_FONT_POINT_SIZE; // point size
7753 nchar = nCopyAnsiToWideChar(p, DLG_FONT_NAME, FALSE);
7754 }
7755 p += nchar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
7757 /*
7758 * Loop over all the items in the menu.
7759 * But skip over the tearbar.
7760 */
7761 if (STRCMP(menu->children->name, TEAR_STRING) == 0)
7762 menu = menu->children->next;
7763 else
7764 menu = menu->children;
Bram Moolenaar66857f42017-10-22 16:43:20 +02007765 top_menu = menu;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 for ( ; menu != NULL; menu = menu->next)
7767 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01007768 if (menu->modes == 0) // this menu has just been deleted
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 continue;
7770 if (menu_is_separator(menu->dname))
7771 {
7772 sepPadding += 3;
7773 continue;
7774 }
7775
Bram Moolenaar734a8672019-12-02 22:49:38 +01007776 // Check if there still is plenty of room in the template. Make it
7777 // larger when needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 if (((char *)p - (char *)pdlgtemplate) + 1000 > template_len)
7779 {
7780 WORD *newp;
7781
7782 newp = (WORD *)LocalAlloc(LPTR, template_len + 4096);
7783 if (newp != NULL)
7784 {
7785 template_len += 4096;
7786 mch_memmove(newp, pdlgtemplate,
7787 (char *)p - (char *)pdlgtemplate);
7788 p = newp + (p - pdlgtemplate);
7789 pnumitems = newp + (pnumitems - pdlgtemplate);
7790 ptrueheight = newp + (ptrueheight - pdlgtemplate);
7791 LocalFree(LocalHandle(pdlgtemplate));
7792 pdlgtemplate = newp;
7793 }
7794 }
7795
Bram Moolenaar734a8672019-12-02 22:49:38 +01007796 // Figure out minimal length of this menu label. Use "name" for the
7797 // actual text, "dname" for estimating the displayed size. "name"
7798 // has "&a" for mnemonic and includes the accelerator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 len = nameLen = (int)STRLEN(menu->name);
7800 padding0 = (columnWidths[0] - GetTextWidthEnc(hdc, menu->dname,
7801 (int)STRLEN(menu->dname))) / spaceWidth;
7802 len += padding0;
7803
7804 if (menu->actext != NULL)
7805 {
7806 acLen = (int)STRLEN(menu->actext);
7807 len += acLen;
7808 textWidth = GetTextWidthEnc(hdc, menu->actext, acLen);
7809 }
7810 else
7811 textWidth = 0;
7812 padding1 = (columnWidths[1] - textWidth) / spaceWidth;
7813 len += padding1;
7814
7815 if (menu->children == NULL)
7816 {
7817 padding2 = submenuWidth / spaceWidth;
7818 len += padding2;
7819 menuID = (WORD)(menu->id);
7820 }
7821 else
7822 {
7823 len += (int)STRLEN(TEAROFF_SUBMENU_LABEL);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007824 menuID = (WORD)((long_u)(menu->submenu_id) | (DWORD)0x8000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 }
7826
Bram Moolenaar734a8672019-12-02 22:49:38 +01007827 // Allocate menu label and fill it in
Bram Moolenaar964b3742019-05-24 18:54:09 +02007828 text = label = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 if (label == NULL)
7830 break;
7831
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007832 vim_strncpy(text, menu->name, nameLen);
Bram Moolenaar734a8672019-12-02 22:49:38 +01007833 text = vim_strchr(text, TAB); // stop at TAB before actext
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 if (text == NULL)
Bram Moolenaar734a8672019-12-02 22:49:38 +01007835 text = label + nameLen; // no actext, use whole name
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 while (padding0-- > 0)
7837 *text++ = ' ';
7838 if (menu->actext != NULL)
7839 {
7840 STRNCPY(text, menu->actext, acLen);
7841 text += acLen;
7842 }
7843 while (padding1-- > 0)
7844 *text++ = ' ';
7845 if (menu->children != NULL)
7846 {
7847 STRCPY(text, TEAROFF_SUBMENU_LABEL);
7848 text += STRLEN(TEAROFF_SUBMENU_LABEL);
7849 }
7850 else
7851 {
7852 while (padding2-- > 0)
7853 *text++ = ' ';
7854 }
7855 *text = NUL;
7856
7857 /*
7858 * BS_LEFT will just be ignored on Win32s/NT3.5x - on
7859 * W95/NT4 it makes the tear-off look more like a menu.
7860 */
7861 p = add_dialog_element(p,
7862 BS_PUSHBUTTON|BS_LEFT,
7863 (WORD)PixelToDialogX(TEAROFF_PADDING_X),
7864 (WORD)(sepPadding + 1 + 13 * (*pnumitems)),
7865 (WORD)PixelToDialogX(dlgwidth - 2 * TEAROFF_PADDING_X),
7866 (WORD)12,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007867 menuID, (WORD)0x0080, (char *)label);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 vim_free(label);
7869 (*pnumitems)++;
7870 }
7871
7872 *ptrueheight = (WORD)(sepPadding + 1 + 13 * (*pnumitems));
7873
7874
Bram Moolenaar734a8672019-12-02 22:49:38 +01007875 // show modelessly
Bram Moolenaar66857f42017-10-22 16:43:20 +02007876 the_menu->tearoff_handle = CreateDialogIndirectParam(
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007877 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 (LPDLGTEMPLATE)pdlgtemplate,
7879 s_hwnd,
Bram Moolenaar66857f42017-10-22 16:43:20 +02007880 (DLGPROC)tearoff_callback,
7881 (LPARAM)top_menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
7883 LocalFree(LocalHandle(pdlgtemplate));
7884 SelectFont(hdc, oldFont);
7885 DeleteObject(font);
7886 ReleaseDC(hwnd, hdc);
7887
7888 /*
7889 * Reassert ourselves as the active window. This is so that after creating
7890 * a tearoff, the user doesn't have to click with the mouse just to start
7891 * typing again!
7892 */
7893 (void)SetActiveWindow(s_hwnd);
7894
Bram Moolenaar734a8672019-12-02 22:49:38 +01007895 // make sure the right buttons are enabled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 force_menu_update = TRUE;
7897}
7898#endif
7899
7900#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01007901# include "gui_w32_rc.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903/*
7904 * Create the toolbar, initially unpopulated.
7905 * (just like the menu, there are no defaults, it's all
7906 * set up through menu.vim)
7907 */
7908 static void
7909initialise_toolbar(void)
7910{
7911 InitCommonControls();
7912 s_toolbarhwnd = CreateToolbarEx(
7913 s_hwnd,
7914 WS_CHILD | TBSTYLE_TOOLTIPS | TBSTYLE_FLAT,
7915 4000, //any old big number
Bram Moolenaar2c7a7632007-05-10 18:19:11 +00007916 31, //number of images in initial bitmap
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007917 g_hinst,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 IDR_TOOLBAR1, // id of initial bitmap
7919 NULL,
7920 0, // initial number of buttons
7921 TOOLBAR_BUTTON_WIDTH, //api guide is wrong!
7922 TOOLBAR_BUTTON_HEIGHT,
7923 TOOLBAR_BUTTON_WIDTH,
7924 TOOLBAR_BUTTON_HEIGHT,
7925 sizeof(TBBUTTON)
7926 );
Bram Moolenaar5f763342019-11-17 22:54:10 +01007927
7928 // Remove transparency from the toolbar to prevent the main window
7929 // background colour showing through
7930 SendMessage(s_toolbarhwnd, TB_SETSTYLE, 0,
7931 SendMessage(s_toolbarhwnd, TB_GETSTYLE, 0, 0) & ~TBSTYLE_TRANSPARENT);
7932
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007933 s_toolbar_wndproc = SubclassWindow(s_toolbarhwnd, toolbar_wndproc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934
7935 gui_mch_show_toolbar(vim_strchr(p_go, GO_TOOLBAR) != NULL);
K.Takatac81e9bf2022-01-16 14:15:49 +00007936
7937 update_toolbar_size();
7938}
7939
7940 static void
7941update_toolbar_size(void)
7942{
7943 int w, h;
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007944 TBMETRICS tbm;
K.Takatac81e9bf2022-01-16 14:15:49 +00007945
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01007946 tbm.cbSize = sizeof(TBMETRICS);
K.Takatac81e9bf2022-01-16 14:15:49 +00007947 tbm.dwMask = TBMF_PAD | TBMF_BUTTONSPACING;
7948 SendMessage(s_toolbarhwnd, TB_GETMETRICS, 0, (LPARAM)&tbm);
7949 //TRACE("Pad: %d, %d", tbm.cxPad, tbm.cyPad);
7950 //TRACE("ButtonSpacing: %d, %d", tbm.cxButtonSpacing, tbm.cyButtonSpacing);
7951
7952 w = (TOOLBAR_BUTTON_WIDTH + tbm.cxPad) * s_dpi / DEFAULT_DPI;
7953 h = (TOOLBAR_BUTTON_HEIGHT + tbm.cyPad) * s_dpi / DEFAULT_DPI;
7954 //TRACE("button size: %d, %d", w, h);
7955 SendMessage(s_toolbarhwnd, TB_SETBUTTONSIZE, 0, MAKELPARAM(w, h));
7956 gui.toolbar_height = h + 6;
7957
7958 //DWORD s = SendMessage(s_toolbarhwnd, TB_GETBUTTONSIZE, 0, 0);
7959 //TRACE("actual button size: %d, %d", LOWORD(s), HIWORD(s));
7960
7961 // TODO:
7962 // Currently, this function only updates the size of toolbar buttons.
7963 // It would be nice if the toolbar images are resized based on DPI.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964}
7965
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02007966 static LRESULT CALLBACK
7967toolbar_wndproc(
7968 HWND hwnd,
7969 UINT uMsg,
7970 WPARAM wParam,
7971 LPARAM lParam)
7972{
7973 HandleMouseHide(uMsg, lParam);
7974 return CallWindowProc(s_toolbar_wndproc, hwnd, uMsg, wParam, lParam);
7975}
7976
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 static int
7978get_toolbar_bitmap(vimmenu_T *menu)
7979{
7980 int i = -1;
7981
7982 /*
7983 * Check user bitmaps first, unless builtin is specified.
7984 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02007985 if (!menu->icon_builtin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
7987 char_u fname[MAXPATHL];
7988 HANDLE hbitmap = NULL;
7989
7990 if (menu->iconfile != NULL)
7991 {
7992 gui_find_iconfile(menu->iconfile, fname, "bmp");
7993 hbitmap = LoadImage(
7994 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01007995 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 IMAGE_BITMAP,
7997 TOOLBAR_BUTTON_WIDTH,
7998 TOOLBAR_BUTTON_HEIGHT,
7999 LR_LOADFROMFILE |
8000 LR_LOADMAP3DCOLORS
8001 );
8002 }
8003
8004 /*
8005 * If the LoadImage call failed, or the "icon=" file
8006 * didn't exist or wasn't specified, try the menu name
8007 */
8008 if (hbitmap == NULL
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008009 && (gui_find_bitmap(
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008010# ifdef FEAT_MULTI_LANG
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008011 menu->en_dname != NULL ? menu->en_dname :
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008012# endif
Bram Moolenaara5f5c8b2013-06-27 22:29:38 +02008013 menu->dname, fname, "bmp") == OK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 hbitmap = LoadImage(
8015 NULL,
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008016 (LPCSTR)fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 IMAGE_BITMAP,
8018 TOOLBAR_BUTTON_WIDTH,
8019 TOOLBAR_BUTTON_HEIGHT,
8020 LR_LOADFROMFILE |
8021 LR_LOADMAP3DCOLORS
8022 );
8023
8024 if (hbitmap != NULL)
8025 {
8026 TBADDBITMAP tbAddBitmap;
8027
8028 tbAddBitmap.hInst = NULL;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008029 tbAddBitmap.nID = (long_u)hbitmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030
8031 i = (int)SendMessage(s_toolbarhwnd, TB_ADDBITMAP,
8032 (WPARAM)1, (LPARAM)&tbAddBitmap);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008033 // i will be set to -1 if it fails
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 }
8035 }
8036 if (i == -1 && menu->iconidx >= 0 && menu->iconidx < TOOLBAR_BITMAP_COUNT)
8037 i = menu->iconidx;
8038
8039 return i;
8040}
8041#endif
8042
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008043#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
8044 static void
8045initialise_tabline(void)
8046{
8047 InitCommonControls();
8048
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008049 s_tabhwnd = CreateWindow(WC_TABCONTROL, "Vim tabline",
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008050 WS_CHILD|TCS_FOCUSNEVER|TCS_TOOLTIPS,
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008051 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008052 CW_USEDEFAULT, s_hwnd, NULL, g_hinst, NULL);
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008053 s_tabline_wndproc = SubclassWindow(s_tabhwnd, tabline_wndproc);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008054
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008055 gui.tabline_height = TABLINE_HEIGHT;
8056
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00008057 set_tabline_font();
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008058}
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008059
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008060/*
8061 * Get tabpage_T from POINT.
8062 */
8063 static tabpage_T *
8064GetTabFromPoint(
8065 HWND hWnd,
8066 POINT pt)
8067{
8068 tabpage_T *ptp = NULL;
8069
8070 if (gui_mch_showing_tabline())
8071 {
8072 TCHITTESTINFO htinfo;
8073 htinfo.pt = pt;
K.Takatac81e9bf2022-01-16 14:15:49 +00008074 // ignore if a window under cursor is not tabcontrol.
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008075 if (s_tabhwnd == hWnd)
8076 {
8077 int idx = TabCtrl_HitTest(s_tabhwnd, &htinfo);
8078 if (idx != -1)
8079 ptp = find_tabpage(idx + 1);
8080 }
8081 }
8082 return ptp;
8083}
8084
8085static POINT s_pt = {0, 0};
8086static HCURSOR s_hCursor = NULL;
8087
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008088 static LRESULT CALLBACK
8089tabline_wndproc(
8090 HWND hwnd,
8091 UINT uMsg,
8092 WPARAM wParam,
8093 LPARAM lParam)
8094{
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008095 POINT pt;
8096 tabpage_T *tp;
8097 RECT rect;
8098 int nCenter;
8099 int idx0;
8100 int idx1;
8101
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008102 HandleMouseHide(uMsg, lParam);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008103
8104 switch (uMsg)
8105 {
8106 case WM_LBUTTONDOWN:
8107 {
8108 s_pt.x = GET_X_LPARAM(lParam);
8109 s_pt.y = GET_Y_LPARAM(lParam);
8110 SetCapture(hwnd);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008111 s_hCursor = GetCursor(); // backup default cursor
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008112 break;
8113 }
8114 case WM_MOUSEMOVE:
8115 if (GetCapture() == hwnd
8116 && ((wParam & MK_LBUTTON)) != 0)
8117 {
8118 pt.x = GET_X_LPARAM(lParam);
8119 pt.y = s_pt.y;
K.Takatac81e9bf2022-01-16 14:15:49 +00008120 if (abs(pt.x - s_pt.x) >
8121 pGetSystemMetricsForDpi(SM_CXDRAG, s_dpi))
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008122 {
8123 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
8124
8125 tp = GetTabFromPoint(hwnd, pt);
8126 if (tp != NULL)
8127 {
8128 idx0 = tabpage_index(curtab) - 1;
8129 idx1 = tabpage_index(tp) - 1;
8130
8131 TabCtrl_GetItemRect(hwnd, idx1, &rect);
8132 nCenter = rect.left + (rect.right - rect.left) / 2;
8133
Bram Moolenaar734a8672019-12-02 22:49:38 +01008134 // Check if the mouse cursor goes over the center of
8135 // the next tab to prevent "flickering".
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008136 if ((idx0 < idx1) && (nCenter < pt.x))
8137 {
8138 tabpage_move(idx1 + 1);
8139 update_screen(0);
8140 }
8141 else if ((idx1 < idx0) && (pt.x < nCenter))
8142 {
8143 tabpage_move(idx1);
8144 update_screen(0);
8145 }
8146 }
8147 }
8148 }
8149 break;
8150 case WM_LBUTTONUP:
8151 {
8152 if (GetCapture() == hwnd)
8153 {
8154 SetCursor(s_hCursor);
8155 ReleaseCapture();
8156 }
8157 break;
8158 }
regomne3bdef102022-09-26 20:48:32 +01008159 case WM_MBUTTONUP:
8160 {
8161 TCHITTESTINFO htinfo;
8162
8163 htinfo.pt.x = GET_X_LPARAM(lParam);
8164 htinfo.pt.y = GET_Y_LPARAM(lParam);
8165 idx0 = TabCtrl_HitTest(hwnd, &htinfo);
8166 if (idx0 != -1)
8167 {
8168 idx0 += 1;
8169 send_tabline_menu_event(idx0, TABLINE_MENU_CLOSE);
8170 }
8171 break;
8172 }
Bram Moolenaarca05aa22017-10-22 15:36:14 +02008173 default:
8174 break;
8175 }
8176
Bram Moolenaar5f919ee2013-07-21 17:46:43 +02008177 return CallWindowProc(s_tabline_wndproc, hwnd, uMsg, wParam, lParam);
8178}
Bram Moolenaar3991dab2006-03-27 17:01:56 +00008179#endif
8180
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181#if defined(FEAT_OLE) || defined(FEAT_EVAL) || defined(PROTO)
8182/*
8183 * Make the GUI window come to the foreground.
8184 */
8185 void
8186gui_mch_set_foreground(void)
8187{
8188 if (IsIconic(s_hwnd))
8189 SendMessage(s_hwnd, WM_SYSCOMMAND, SC_RESTORE, 0);
8190 SetForegroundWindow(s_hwnd);
8191}
8192#endif
8193
8194#if defined(FEAT_MBYTE_IME) && defined(DYNAMIC_IME)
8195 static void
8196dyn_imm_load(void)
8197{
Bram Moolenaarebbcb822010-10-23 14:02:54 +02008198 hLibImm = vimLoadLib("imm32.dll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 if (hLibImm == NULL)
8200 return;
8201
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 pImmGetCompositionStringW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008203 = (LONG (WINAPI *)(HIMC, DWORD, LPVOID, DWORD))GetProcAddress(hLibImm, "ImmGetCompositionStringW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 pImmGetContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008205 = (HIMC (WINAPI *)(HWND))GetProcAddress(hLibImm, "ImmGetContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 pImmAssociateContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008207 = (HIMC (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmAssociateContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 pImmReleaseContext
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008209 = (BOOL (WINAPI *)(HWND, HIMC))GetProcAddress(hLibImm, "ImmReleaseContext");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 pImmGetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008211 = (BOOL (WINAPI *)(HIMC))GetProcAddress(hLibImm, "ImmGetOpenStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 pImmSetOpenStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008213 = (BOOL (WINAPI *)(HIMC, BOOL))GetProcAddress(hLibImm, "ImmSetOpenStatus");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008214 pImmGetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008215 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmGetCompositionFontW");
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008216 pImmSetCompositionFontW
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008217 = (BOOL (WINAPI *)(HIMC, LPLOGFONTW))GetProcAddress(hLibImm, "ImmSetCompositionFontW");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 pImmSetCompositionWindow
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008219 = (BOOL (WINAPI *)(HIMC, LPCOMPOSITIONFORM))GetProcAddress(hLibImm, "ImmSetCompositionWindow");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 pImmGetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008221 = (BOOL (WINAPI *)(HIMC, LPDWORD, LPDWORD))GetProcAddress(hLibImm, "ImmGetConversionStatus");
Bram Moolenaarca003e12006-03-17 23:19:38 +00008222 pImmSetConversionStatus
Bram Moolenaar35d7a2f2022-06-09 20:53:54 +01008223 = (BOOL (WINAPI *)(HIMC, DWORD, DWORD))GetProcAddress(hLibImm, "ImmSetConversionStatus");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
K.Takatab0b2b732022-01-19 12:59:21 +00008225 if ( pImmGetCompositionStringW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 || pImmGetContext == NULL
8227 || pImmAssociateContext == NULL
8228 || pImmReleaseContext == NULL
8229 || pImmGetOpenStatus == NULL
8230 || pImmSetOpenStatus == NULL
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01008231 || pImmGetCompositionFontW == NULL
8232 || pImmSetCompositionFontW == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 || pImmSetCompositionWindow == NULL
Bram Moolenaarca003e12006-03-17 23:19:38 +00008234 || pImmGetConversionStatus == NULL
8235 || pImmSetConversionStatus == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
8237 FreeLibrary(hLibImm);
8238 hLibImm = NULL;
8239 pImmGetContext = NULL;
8240 return;
8241 }
8242
8243 return;
8244}
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246#endif
8247
8248#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
8249
8250# ifdef FEAT_XPM_W32
8251# define IMAGE_XPM 100
8252# endif
8253
8254typedef struct _signicon_t
8255{
8256 HANDLE hImage;
8257 UINT uType;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008258# ifdef FEAT_XPM_W32
Bram Moolenaar734a8672019-12-02 22:49:38 +01008259 HANDLE hShape; // Mask bitmap handle
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261} signicon_t;
8262
8263 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008264gui_mch_drawsign(int row, int col, int typenr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 signicon_t *sign;
8267 int x, y, w, h;
8268
8269 if (!gui.in_use || (sign = (signicon_t *)sign_get_image(typenr)) == NULL)
8270 return;
8271
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008272# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008273 if (IS_ENABLE_DIRECTX())
8274 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008275# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 x = TEXT_X(col);
8278 y = TEXT_Y(row);
8279 w = gui.char_width * 2;
8280 h = gui.char_height;
8281 switch (sign->uType)
8282 {
8283 case IMAGE_BITMAP:
8284 {
8285 HDC hdcMem;
8286 HBITMAP hbmpOld;
8287
8288 hdcMem = CreateCompatibleDC(s_hdc);
8289 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hImage);
8290 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCCOPY);
8291 SelectObject(hdcMem, hbmpOld);
8292 DeleteDC(hdcMem);
8293 }
8294 break;
8295 case IMAGE_ICON:
8296 case IMAGE_CURSOR:
8297 DrawIconEx(s_hdc, x, y, (HICON)sign->hImage, w, h, 0, NULL, DI_NORMAL);
8298 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008299# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 case IMAGE_XPM:
8301 {
8302 HDC hdcMem;
8303 HBITMAP hbmpOld;
8304
8305 hdcMem = CreateCompatibleDC(s_hdc);
8306 hbmpOld = (HBITMAP)SelectObject(hdcMem, sign->hShape);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008307 // Make hole
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCAND);
8309
8310 SelectObject(hdcMem, sign->hImage);
Bram Moolenaar734a8672019-12-02 22:49:38 +01008311 // Paint sign
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 BitBlt(s_hdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT);
8313 SelectObject(hdcMem, hbmpOld);
8314 DeleteDC(hdcMem);
8315 }
8316 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319}
8320
8321 static void
8322close_signicon_image(signicon_t *sign)
8323{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008324 if (sign == NULL)
8325 return;
8326
8327 switch (sign->uType)
8328 {
8329 case IMAGE_BITMAP:
8330 DeleteObject((HGDIOBJ)sign->hImage);
8331 break;
8332 case IMAGE_CURSOR:
8333 DestroyCursor((HCURSOR)sign->hImage);
8334 break;
8335 case IMAGE_ICON:
8336 DestroyIcon((HICON)sign->hImage);
8337 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008338# ifdef FEAT_XPM_W32
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008339 case IMAGE_XPM:
8340 DeleteObject((HBITMAP)sign->hImage);
8341 DeleteObject((HBITMAP)sign->hShape);
8342 break;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008343# endif
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347 void *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008348gui_mch_register_sign(char_u *signfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 signicon_t sign, *psign;
8351 char_u *ext;
8352
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 sign.hImage = NULL;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008354 ext = signfile + STRLEN(signfile) - 4; // get extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 if (ext > signfile)
8356 {
8357 int do_load = 1;
8358
8359 if (!STRICMP(ext, ".bmp"))
8360 sign.uType = IMAGE_BITMAP;
8361 else if (!STRICMP(ext, ".ico"))
8362 sign.uType = IMAGE_ICON;
8363 else if (!STRICMP(ext, ".cur") || !STRICMP(ext, ".ani"))
8364 sign.uType = IMAGE_CURSOR;
8365 else
8366 do_load = 0;
8367
8368 if (do_load)
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008369 sign.hImage = (HANDLE)LoadImage(NULL, (LPCSTR)signfile, sign.uType,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 gui.char_width * 2, gui.char_height,
8371 LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008372# ifdef FEAT_XPM_W32
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if (!STRICMP(ext, ".xpm"))
8374 {
8375 sign.uType = IMAGE_XPM;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008376 LoadXpmImage((char *)signfile, (HBITMAP *)&sign.hImage,
8377 (HBITMAP *)&sign.hShape);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008379# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 }
8381
8382 psign = NULL;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008383 if (sign.hImage && (psign = ALLOC_ONE(signicon_t)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 *psign = sign;
8385
8386 if (!psign)
8387 {
8388 if (sign.hImage)
8389 close_signicon_image(&sign);
Bram Moolenaar74409f62022-01-01 15:58:22 +00008390 emsg(_(e_couldnt_read_in_sign_data));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 }
8392 return (void *)psign;
8393
8394}
8395
8396 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008397gui_mch_destroy_sign(void *sign)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008399 if (sign == NULL)
8400 return;
8401
8402 close_signicon_image((signicon_t *)sign);
8403 vim_free(sign);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
Bram Moolenaar5c06f8b2005-05-31 22:14:58 +00008405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008407#if defined(FEAT_BEVAL_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408
Bram Moolenaar734a8672019-12-02 22:49:38 +01008409/*
8410 * BALLOON-EVAL IMPLEMENTATION FOR WINDOWS.
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00008411 * Added by Sergey Khorev <sergey.khorev@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 *
Bram Moolenaarc3719bd2017-11-18 22:13:31 +01008413 * The only reused thing is beval.h and get_beval_info()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 * from gui_beval.c (note it uses x and y of the BalloonEval struct
8415 * to get current mouse position).
8416 *
8417 * Trying to use as more Windows services as possible, and as less
8418 * IE version as possible :)).
8419 *
8420 * 1) Don't create ToolTip in gui_mch_create_beval_area, only initialize
8421 * BalloonEval struct.
8422 * 2) Enable/Disable simply create/kill BalloonEval Timer
8423 * 3) When there was enough inactivity, timer procedure posts
8424 * async request to debugger
8425 * 4) gui_mch_post_balloon (invoked from netbeans.c) creates tooltip control
8426 * and performs some actions to show it ASAP
Bram Moolenaar446cb832008-06-24 21:56:24 +00008427 * 5) WM_NOTIFY:TTN_POP destroys created tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 */
8429
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008430 static void
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02008431make_tooltip(BalloonEval *beval, char *text, POINT pt)
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008432{
K.Takata76687d22022-01-25 10:31:37 +00008433 TOOLINFOW *pti;
8434 RECT rect;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008435
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008436 pti = ALLOC_ONE(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008437 if (pti == NULL)
8438 return;
8439
8440 beval->balloon = CreateWindowExW(WS_EX_TOPMOST, TOOLTIPS_CLASSW,
8441 NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
8442 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008443 beval->target, NULL, g_hinst, NULL);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008444
8445 SetWindowPos(beval->balloon, HWND_TOPMOST, 0, 0, 0, 0,
8446 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
8447
K.Takata76687d22022-01-25 10:31:37 +00008448 pti->cbSize = sizeof(TOOLINFOW);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008449 pti->uFlags = TTF_SUBCLASS;
8450 pti->hwnd = beval->target;
8451 pti->hinst = 0; // Don't use string resources
8452 pti->uId = ID_BEVAL_TOOLTIP;
8453
Bram Moolenaar0bd663a2022-01-22 10:24:47 +00008454 pti->lpszText = LPSTR_TEXTCALLBACKW;
8455 beval->tofree = enc_to_utf16((char_u*)text, NULL);
8456 pti->lParam = (LPARAM)beval->tofree;
8457 // switch multiline tooltips on
8458 if (GetClientRect(s_textArea, &rect))
8459 SendMessageW(beval->balloon, TTM_SETMAXTIPWIDTH, 0,
8460 (LPARAM)rect.right);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008461
8462 // Limit ballooneval bounding rect to CursorPos neighbourhood.
8463 pti->rect.left = pt.x - 3;
8464 pti->rect.top = pt.y - 3;
8465 pti->rect.right = pt.x + 3;
8466 pti->rect.bottom = pt.y + 3;
8467
8468 SendMessageW(beval->balloon, TTM_ADDTOOLW, 0, (LPARAM)pti);
8469 // Make tooltip appear sooner.
8470 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_INITIAL, 10);
8471 // I've performed some tests and it seems the longest possible life time
8472 // of tooltip is 30 seconds.
8473 SendMessageW(beval->balloon, TTM_SETDELAYTIME, TTDT_AUTOPOP, 30000);
8474 /*
8475 * HACK: force tooltip to appear, because it'll not appear until
8476 * first mouse move. D*mn M$
8477 * Amazingly moving (2, 2) and then (-1, -1) the mouse doesn't move.
8478 */
8479 mouse_event(MOUSEEVENTF_MOVE, 2, 2, 0, 0);
8480 mouse_event(MOUSEEVENTF_MOVE, (DWORD)-1, (DWORD)-1, 0, 0);
8481 vim_free(pti);
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008482}
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008483
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 static void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008485delete_tooltip(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
Bram Moolenaar8e5f5b42015-08-26 23:12:38 +02008487 PostMessage(beval->balloon, WM_CLOSE, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488}
8489
8490 static VOID CALLBACK
K.Takataa8ec4912022-02-03 14:32:33 +00008491beval_timer_proc(
Bram Moolenaar1266d672017-02-01 13:43:36 +01008492 HWND hwnd UNUSED,
8493 UINT uMsg UNUSED,
K.Takataa8ec4912022-02-03 14:32:33 +00008494 UINT_PTR idEvent UNUSED,
Bram Moolenaar1266d672017-02-01 13:43:36 +01008495 DWORD dwTime)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496{
8497 POINT pt;
8498 RECT rect;
8499
8500 if (cur_beval == NULL || cur_beval->showState == ShS_SHOWING || !p_beval)
8501 return;
8502
8503 GetCursorPos(&pt);
8504 if (WindowFromPoint(pt) != s_textArea)
8505 return;
8506
8507 ScreenToClient(s_textArea, &pt);
8508 GetClientRect(s_textArea, &rect);
8509 if (!PtInRect(&rect, pt))
8510 return;
8511
K.Takataa8ec4912022-02-03 14:32:33 +00008512 if (last_user_activity > 0
8513 && (dwTime - last_user_activity) >= (DWORD)p_bdlay
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 && (cur_beval->showState != ShS_PENDING
8515 || abs(cur_beval->x - pt.x) > 3
8516 || abs(cur_beval->y - pt.y) > 3))
8517 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008518 // Pointer resting in one place long enough, it's time to show
8519 // the tooltip.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 cur_beval->showState = ShS_PENDING;
8521 cur_beval->x = pt.x;
8522 cur_beval->y = pt.y;
8523
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 if (cur_beval->msgCB != NULL)
8525 (*cur_beval->msgCB)(cur_beval, 0);
8526 }
8527}
8528
8529 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008530gui_mch_disable_beval_area(BalloonEval *beval UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
K.Takataa8ec4912022-02-03 14:32:33 +00008532 KillTimer(s_textArea, beval_timer_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533}
8534
8535 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008536gui_mch_enable_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (beval == NULL)
8539 return;
K.Takataa8ec4912022-02-03 14:32:33 +00008540 beval_timer_id = SetTimer(s_textArea, 0, (UINT)(p_bdlay / 2),
8541 beval_timer_proc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542}
8543
8544 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008545gui_mch_post_balloon(BalloonEval *beval, char_u *mesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 POINT pt;
Bram Moolenaar1c465442017-03-12 20:10:05 +01008548
Bram Moolenaarbe0a2592019-05-09 13:50:16 +02008549 vim_free(beval->msg);
8550 beval->msg = mesg == NULL ? NULL : vim_strsave(mesg);
8551 if (beval->msg == NULL)
8552 {
8553 delete_tooltip(beval);
8554 beval->showState = ShS_NEUTRAL;
8555 return;
8556 }
8557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 if (beval->showState == ShS_SHOWING)
8559 return;
8560 GetCursorPos(&pt);
8561 ScreenToClient(s_textArea, &pt);
8562
8563 if (abs(beval->x - pt.x) < 3 && abs(beval->y - pt.y) < 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008565 // cursor is still here
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 gui_mch_disable_beval_area(cur_beval);
8567 beval->showState = ShS_SHOWING;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01008568 make_tooltip(beval, (char *)mesg, pt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570}
8571
8572 BalloonEval *
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008573gui_mch_create_beval_area(
Bram Moolenaar734a8672019-12-02 22:49:38 +01008574 void *target UNUSED, // ignored, always use s_textArea
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008575 char_u *mesg,
8576 void (*mesgCB)(BalloonEval *, int),
8577 void *clientData)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008579 // partially stolen from gui_beval.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 BalloonEval *beval;
8581
8582 if (mesg != NULL && mesgCB != NULL)
8583 {
RestorerZ68ebcee2023-05-31 17:12:14 +01008584 iemsg(e_cannot_create_ballooneval_with_both_message_and_callback);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 return NULL;
8586 }
8587
Bram Moolenaarc799fe22019-05-28 23:08:19 +02008588 beval = ALLOC_CLEAR_ONE(BalloonEval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (beval != NULL)
8590 {
8591 beval->target = s_textArea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
8593 beval->showState = ShS_NEUTRAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 beval->msg = mesg;
8595 beval->msgCB = mesgCB;
8596 beval->clientData = clientData;
8597
8598 InitCommonControls();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 cur_beval = beval;
8600
8601 if (p_beval)
8602 gui_mch_enable_beval_area(beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 }
8604 return beval;
8605}
8606
8607 static void
Bram Moolenaar1266d672017-02-01 13:43:36 +01008608Handle_WM_Notify(HWND hwnd UNUSED, LPNMHDR pnmh)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
Bram Moolenaar734a8672019-12-02 22:49:38 +01008610 if (pnmh->idFrom != ID_BEVAL_TOOLTIP) // it is not our tooltip
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 return;
8612
Yegappan Lakshmanan7f8b2552023-01-08 13:44:24 +00008613 if (cur_beval == NULL)
8614 return;
8615
8616 switch (pnmh->code)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 {
Bram Moolenaar45360022005-07-21 21:08:21 +00008618 case TTN_SHOW:
Bram Moolenaar45360022005-07-21 21:08:21 +00008619 break;
Bram Moolenaar734a8672019-12-02 22:49:38 +01008620 case TTN_POP: // Before tooltip disappear
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 delete_tooltip(cur_beval);
8622 gui_mch_enable_beval_area(cur_beval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624 cur_beval->showState = ShS_NEUTRAL;
Bram Moolenaar45360022005-07-21 21:08:21 +00008625 break;
8626 case TTN_GETDISPINFO:
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008627 {
Bram Moolenaar734a8672019-12-02 22:49:38 +01008628 // if you get there then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008629 NMTTDISPINFO *info = (NMTTDISPINFO *)pnmh;
Bram Moolenaar6c9176d2008-01-03 19:45:15 +00008630 info->lpszText = (LPSTR)info->lParam;
8631 info->uFlags |= TTF_DI_SETITEM;
8632 }
Bram Moolenaar45360022005-07-21 21:08:21 +00008633 break;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008634 case TTN_GETDISPINFOW:
8635 {
8636 // if we get here then we have new common controls
K.Takata76687d22022-01-25 10:31:37 +00008637 NMTTDISPINFOW *info = (NMTTDISPINFOW *)pnmh;
Bram Moolenaard385b5d2018-12-27 22:43:08 +01008638 info->lpszText = (LPWSTR)info->lParam;
8639 info->uFlags |= TTF_DI_SETITEM;
8640 }
8641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643}
8644
8645 static void
K.Takataa8ec4912022-02-03 14:32:33 +00008646track_user_activity(UINT uMsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647{
8648 if ((uMsg >= WM_MOUSEFIRST && uMsg <= WM_MOUSELAST)
8649 || (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST))
K.Takataa8ec4912022-02-03 14:32:33 +00008650 last_user_activity = GetTickCount();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653 void
Bram Moolenaar68c2f632016-01-30 17:24:07 +01008654gui_mch_destroy_beval_area(BalloonEval *beval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008656# ifdef FEAT_VARTABS
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008657 vim_free(beval->vts);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008658# endif
Bram Moolenaar6d9e71a2018-12-28 19:13:34 +01008659 vim_free(beval->tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 vim_free(beval);
8661}
Bram Moolenaar734a8672019-12-02 22:49:38 +01008662#endif // FEAT_BEVAL_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
8664#if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
8665/*
8666 * We have multiple signs to draw at the same location. Draw the
8667 * multi-sign indicator (down-arrow) instead. This is the Win32 version.
8668 */
8669 void
8670netbeans_draw_multisign_indicator(int row)
8671{
8672 int i;
8673 int y;
8674 int x;
8675
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008676 if (!netbeans_active())
Bram Moolenaarcc448b32010-07-14 16:52:17 +02008677 return;
Bram Moolenaarb26e6322010-05-22 21:34:09 +02008678
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 x = 0;
8680 y = TEXT_Y(row);
8681
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008682# if defined(FEAT_DIRECTX)
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008683 if (IS_ENABLE_DIRECTX())
8684 DWriteContext_Flush(s_dwc);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01008685# endif
Bram Moolenaard7ccc4d2017-11-26 14:29:32 +01008686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 for (i = 0; i < gui.char_height - 3; i++)
8688 SetPixel(s_hdc, x+2, y++, gui.currFgColor);
8689
8690 SetPixel(s_hdc, x+0, y, gui.currFgColor);
8691 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8692 SetPixel(s_hdc, x+4, y++, gui.currFgColor);
8693 SetPixel(s_hdc, x+1, y, gui.currFgColor);
8694 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8695 SetPixel(s_hdc, x+3, y++, gui.currFgColor);
8696 SetPixel(s_hdc, x+2, y, gui.currFgColor);
8697}
Bram Moolenaare0874f82016-01-24 20:36:41 +01008698#endif
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008699
8700#if defined(FEAT_EVAL) || defined(PROTO)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008701
Christopher Plewright20b795e2022-12-20 20:01:58 +00008702// TODO: at the moment, this is just a copy of test_gui_mouse_event.
8703// But, we could instead generate actual Win32 mouse event messages,
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00008704// ie. to make it consistent with test_gui_w32_sendevent_keyboard.
Christopher Plewright20b795e2022-12-20 20:01:58 +00008705 static int
8706test_gui_w32_sendevent_mouse(dict_T *args)
8707{
8708 if (!dict_has_key(args, "row") || !dict_has_key(args, "col"))
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008709 return FALSE;
8710
Christopher Plewright20b795e2022-12-20 20:01:58 +00008711 // Note: "move" is optional, requires fewer arguments
8712 int move = (int)dict_get_bool(args, "move", FALSE);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008713
Christopher Plewright20b795e2022-12-20 20:01:58 +00008714 if (!move && (!dict_has_key(args, "button")
8715 || !dict_has_key(args, "multiclick")
8716 || !dict_has_key(args, "modifiers")))
8717 return FALSE;
8718
8719 int row = (int)dict_get_number(args, "row");
8720 int col = (int)dict_get_number(args, "col");
8721
8722 if (move)
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008723 {
Christopher Plewright20b795e2022-12-20 20:01:58 +00008724 // the "move" argument expects row and col coordnates to be in pixels,
8725 // unless "cell" is specified and is TRUE.
8726 if (dict_get_bool(args, "cell", FALSE))
8727 {
8728 // calculate the middle of the character cell
8729 // Note: Cell coordinates are 1-based from vimscript
8730 int pY = (row - 1) * gui.char_height + gui.char_height / 2;
8731 int pX = (col - 1) * gui.char_width + gui.char_width / 2;
8732 gui_mouse_moved(pX, pY);
8733 }
8734 else
8735 gui_mouse_moved(col, row);
8736 }
8737 else
8738 {
8739 int button = (int)dict_get_number(args, "button");
8740 int repeated_click = (int)dict_get_number(args, "multiclick");
8741 int_u mods = (int)dict_get_number(args, "modifiers");
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008742
Christopher Plewright20b795e2022-12-20 20:01:58 +00008743 // Reset the scroll values to known values.
8744 // XXX: Remove this when/if the scroll step is made configurable.
8745 mouse_set_hor_scroll_step(6);
8746 mouse_set_vert_scroll_step(3);
8747
8748 gui_send_mouse_event(button, TEXT_X(col - 1), TEXT_Y(row - 1),
8749 repeated_click, mods);
8750 }
8751 return TRUE;
8752}
8753
8754 static int
8755test_gui_w32_sendevent_keyboard(dict_T *args)
8756{
8757 INPUT inputs[1];
8758 INPUT modkeys[3];
8759 SecureZeroMemory(inputs, sizeof(INPUT));
8760 SecureZeroMemory(modkeys, 3 * sizeof(INPUT));
8761
8762 char_u *event = dict_get_string(args, "event", TRUE);
8763
8764 if (event && (STRICMP(event, "keydown") == 0
8765 || STRICMP(event, "keyup") == 0))
8766 {
8767 WORD vkCode = dict_get_number_def(args, "keycode", 0);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008768 if (vkCode <= 0 || vkCode >= 0xFF)
8769 {
8770 semsg(_(e_invalid_argument_nr), (long)vkCode);
8771 return FALSE;
8772 }
8773
Christopher Plewright20b795e2022-12-20 20:01:58 +00008774 BOOL isModKey = (vkCode == VK_SHIFT || vkCode == VK_CONTROL
8775 || vkCode == VK_MENU || vkCode == VK_LSHIFT || vkCode == VK_RSHIFT
8776 || vkCode == VK_LCONTROL || vkCode == VK_RCONTROL
8777 || vkCode == VK_LMENU || vkCode == VK_RMENU );
8778
8779 BOOL unwrapMods = FALSE;
8780 int mods = (int)dict_get_number(args, "modifiers");
8781
8782 // If there are modifiers in the args, and it is not a keyup event and
8783 // vkCode is not a modifier key, then we generate virtual modifier key
8784 // messages before sending the actual key message.
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008785 if (mods && STRICMP(event, "keydown") == 0 && !isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008786 {
8787 int n = 0;
8788 if (mods & MOD_MASK_SHIFT)
8789 {
8790 modkeys[n].type = INPUT_KEYBOARD;
8791 modkeys[n].ki.wVk = VK_LSHIFT;
8792 n++;
8793 }
8794 if (mods & MOD_MASK_CTRL)
8795 {
8796 modkeys[n].type = INPUT_KEYBOARD;
8797 modkeys[n].ki.wVk = VK_LCONTROL;
8798 n++;
8799 }
8800 if (mods & MOD_MASK_ALT)
8801 {
8802 modkeys[n].type = INPUT_KEYBOARD;
8803 modkeys[n].ki.wVk = VK_LMENU;
8804 n++;
8805 }
8806 if (n)
8807 {
8808 (void)SetForegroundWindow(s_hwnd);
8809 SendInput(n, modkeys, sizeof(INPUT));
8810 }
8811 }
8812
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008813 inputs[0].type = INPUT_KEYBOARD;
8814 inputs[0].ki.wVk = vkCode;
8815 if (STRICMP(event, "keyup") == 0)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008816 {
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008817 inputs[0].ki.dwFlags = KEYEVENTF_KEYUP;
Bram Moolenaarc9471b12023-05-09 15:00:00 +01008818 if (!isModKey)
Christopher Plewright20b795e2022-12-20 20:01:58 +00008819 unwrapMods = TRUE;
8820 }
8821
K.Takatafef38d82022-09-07 19:03:42 +01008822 (void)SetForegroundWindow(s_hwnd);
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008823 SendInput(ARRAYSIZE(inputs), inputs, sizeof(INPUT));
Christopher Plewright20b795e2022-12-20 20:01:58 +00008824 vim_free(event);
8825
8826 if (unwrapMods)
8827 {
8828 modkeys[0].type = INPUT_KEYBOARD;
8829 modkeys[0].ki.wVk = VK_LSHIFT;
8830 modkeys[0].ki.dwFlags = KEYEVENTF_KEYUP;
8831
8832 modkeys[1].type = INPUT_KEYBOARD;
8833 modkeys[1].ki.wVk = VK_LCONTROL;
8834 modkeys[1].ki.dwFlags = KEYEVENTF_KEYUP;
8835
8836 modkeys[2].type = INPUT_KEYBOARD;
8837 modkeys[2].ki.wVk = VK_LMENU;
8838 modkeys[2].ki.dwFlags = KEYEVENTF_KEYUP;
8839
8840 (void)SetForegroundWindow(s_hwnd);
8841 SendInput(3, modkeys, sizeof(INPUT));
8842 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008843 }
8844 else
Christopher Plewright20b795e2022-12-20 20:01:58 +00008845 {
8846 if (event == NULL)
8847 {
8848 semsg(_(e_missing_argument_str), "event");
8849 }
8850 else
8851 {
8852 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8853 vim_free(event);
8854 }
8855 return FALSE;
8856 }
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008857 return TRUE;
8858}
Christopher Plewright20b795e2022-12-20 20:01:58 +00008859
8860 int
8861test_gui_w32_sendevent(char_u *event, dict_T *args)
8862{
8863 if (STRICMP(event, "key") == 0)
8864 return test_gui_w32_sendevent_keyboard(args);
8865 else if (STRICMP(event, "mouse") == 0)
8866 return test_gui_w32_sendevent_mouse(args);
8867 else
8868 {
8869 semsg(_(e_invalid_value_for_argument_str_str), "event", event);
8870 return FALSE;
8871 }
8872}
Yegappan Lakshmanan81a3ff92022-07-23 05:04:16 +01008873#endif